tapewasm 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  // Types for `index.js`; the shapes come from `pkg/`, which `make wasm` writes.
2
2
  export {
3
3
  AotSampler,
4
+ AdviResult,
4
5
  CompiledTape,
6
+ SampleResult,
5
7
  compileTape,
6
8
  tapewasmVersion,
7
9
  setAotExports,
package/index.js CHANGED
@@ -5,7 +5,9 @@
5
5
  import init from "./pkg/tapewasm.js";
6
6
  export {
7
7
  AotSampler,
8
+ AdviResult,
8
9
  CompiledTape,
10
+ SampleResult,
9
11
  compileTape,
10
12
  tapewasmVersion,
11
13
  setAotExports,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tapewasm",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Compile an autodiff tape to a WebAssembly module and sample it with nuts-rs, in the browser.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/pkg/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "tapewasm",
3
3
  "type": "module",
4
4
  "description": "Compile an autodiff tape to a wasm module and sample it with nuts-rs, in the browser. The wasm-bindgen API over tapewasm-codegen.",
5
- "version": "0.1.1",
5
+ "version": "0.3.0",
6
6
  "license": "MIT OR Apache-2.0",
7
7
  "repository": {
8
8
  "type": "git",
package/pkg/tapewasm.d.ts CHANGED
@@ -1,6 +1,31 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
 
4
+ /**
5
+ * The fitted mean-field variational distribution and its ELBO trajectory.
6
+ */
7
+ export class AdviResult {
8
+ private constructor();
9
+ free(): void;
10
+ [Symbol.dispose](): void;
11
+ /**
12
+ * The ELBO estimate after each iteration, in order.
13
+ */
14
+ readonly elboTrace: Float64Array;
15
+ /**
16
+ * The raw (pre-averaging) iterate `μ` every `snapshot_every` iterations,
17
+ * one snapshot's `n_params` values after another. Empty unless `advi`
18
+ * was called with `snapshot_every > 0`.
19
+ */
20
+ readonly muSnapshots: Float64Array;
21
+ readonly mu: Float64Array;
22
+ readonly sigma: Float64Array;
23
+ /**
24
+ * The iteration number each entry of `muSnapshots` was taken at.
25
+ */
26
+ readonly snapshotIters: Float64Array;
27
+ }
28
+
4
29
  /**
5
30
  * A sampler over one compiled module.
6
31
  *
@@ -11,6 +36,37 @@
11
36
  export class AotSampler {
12
37
  free(): void;
13
38
  [Symbol.dispose](): void;
39
+ /**
40
+ * Mean-field ADVI: fits `q(θ) = N(μ, diag(σ²))` to the log density by
41
+ * Adam-ascending the ELBO under the reparameterization trick
42
+ * `θ = μ + σ·η`, `η ~ N(0, I)`. `init` seeds `μ`; `ω = log σ` starts at
43
+ * `0` (`σ = 1`).
44
+ *
45
+ * The ELBO gradient combines the tape's `∂logp/∂θ` (from `log_prob_grad`)
46
+ * with the reparameterization's own Jacobian and the mean-field Gaussian
47
+ * entropy's gradient, both exact rather than estimated:
48
+ * `∂ELBO/∂μ_i = ∂logp/∂θ_i`, `∂ELBO/∂ω_i = ∂logp/∂θ_i · σ_i · η_i + 1`.
49
+ * Each is averaged over `mc_samples` reparameterized draws per iteration.
50
+ *
51
+ * `AdviResult::mu`/`sigma` average `(μ, ω)` over the second half of
52
+ * `num_iters` (Polyak averaging): a constant Adam step size never settles
53
+ * on one point, so the plain last iterate keeps wandering a "noise ball"
54
+ * around the optimum. `elbo_trace` still reports the raw iterate's ELBO
55
+ * per step, for diagnosing convergence rather than for the fitted result.
56
+ *
57
+ * `snapshot_every` (0 disables it) additionally copies the raw iterate
58
+ * `μ` every that-many iterations into `AdviResult::muSnapshots` — a film
59
+ * strip of the one training run, rather than several shorter ones
60
+ * spliced together: restarting Adam's moment estimates partway through
61
+ * measurably converges to a worse optimum, so this is the only way to
62
+ * watch a run progress without paying for that.
63
+ *
64
+ * `on_snapshot`, if given, is also called at each snapshot with the
65
+ * iteration, a copy of that `μ`, and the ELBO trace since the previous
66
+ * call — so a run inside a Worker can report as it goes rather than only
67
+ * once it returns.
68
+ */
69
+ advi(init: Float64Array, num_iters: number, mc_samples: number, learning_rate: number, seed: bigint, snapshot_every: number, on_snapshot?: ((iter: number, mu: Float64Array, elbo: Float64Array) => void) | null): AdviResult;
14
70
  /**
15
71
  * `[log_prob, d/dparam...]`.
16
72
  */
@@ -27,10 +83,28 @@ export class AotSampler {
27
83
  * an empty array to go without.
28
84
  */
29
85
  constructor(n_params: number, scratch_init: Float64Array, layout_id: number, param_names: string[]);
86
+ /**
87
+ * [`sample`](Self::sample) with each draw's sampler statistics beside it —
88
+ * what ArviZ keeps as `sample_stats`. `chain` only labels the run: a
89
+ * different seed is what keeps two chains apart.
90
+ */
91
+ sampleWithStats(init: Float64Array, num_warmup: number, num_draws: number, seed: bigint, chain: number): SampleResult;
30
92
  /**
31
93
  * `num_warmup + num_draws` draws, row-major, `n_params` wide.
32
94
  */
33
95
  sample(init: Float64Array, num_warmup: number, num_draws: number, seed: bigint): Float64Array;
96
+ /**
97
+ * Estimate the diagonal metric from the gradients as well as the draws, as
98
+ * nuts-rs does by default. Off unless set, to match the reference
99
+ * posteriors; see [`nuts_settings`].
100
+ */
101
+ setGradBasedEstimate(on: boolean): void;
102
+ /**
103
+ * Aim warmup's step-size adaptation at this acceptance rate instead of
104
+ * nuts-rs's 0.8. Higher adapts a smaller step: fewer divergences on a hard
105
+ * geometry, more gradients per draw.
106
+ */
107
+ setTargetAccept(target: number): void;
34
108
  readonly nParams: number;
35
109
  }
36
110
 
@@ -51,6 +125,37 @@ export class CompiledTape {
51
125
  readonly wasm: Uint8Array;
52
126
  }
53
127
 
128
+ /**
129
+ * Draws and, beside each, the sampler's statistics — warmup first, as
130
+ * [`AotSampler::sample`] returns them.
131
+ */
132
+ export class SampleResult {
133
+ private constructor();
134
+ free(): void;
135
+ [Symbol.dispose](): void;
136
+ /**
137
+ * 1 where the draw's trajectory diverged.
138
+ */
139
+ readonly diverging: Uint8Array;
140
+ /**
141
+ * `num_warmup + num_draws` draws, row-major, `n_params` wide.
142
+ */
143
+ readonly draws: Float64Array;
144
+ /**
145
+ * The log density at each draw.
146
+ */
147
+ readonly lp: Float64Array;
148
+ /**
149
+ * Leapfrog steps the draw's trajectory took.
150
+ */
151
+ readonly numSteps: Uint32Array;
152
+ readonly stepSize: Float64Array;
153
+ /**
154
+ * 1 for the warmup draws.
155
+ */
156
+ readonly tuning: Uint8Array;
157
+ }
158
+
54
159
  /**
55
160
  * Release the bound module's exports. The next draw will throw.
56
161
  */
@@ -66,8 +171,15 @@ export function clearAotExports(): void;
66
171
  *
67
172
  * The format is not an artifact and carries no compatibility promise: a tape
68
173
  * is written and consumed inside one call.
174
+ *
175
+ * `reroll` says when a vectorised statement becomes a wasm loop: `"auto"`
176
+ * (the default, straight-line below a size threshold), `"always"` or
177
+ * `"never"`. Which is faster is an engine's preference, not the model's —
178
+ * on one real model straight-line was faster on V8 and slower on
179
+ * SpiderMonkey and JavaScriptCore — and `"always"` is also the smallest
180
+ * module, often by an order of magnitude.
69
181
  */
70
- export function compileTape(tape: string): CompiledTape;
182
+ export function compileTape(tape: string, reroll?: string | null): CompiledTape;
71
183
 
72
184
  /**
73
185
  * Forwards Rust panics to `console.error` with a message and backtrace rather
@@ -98,28 +210,46 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
98
210
 
99
211
  export interface InitOutput {
100
212
  readonly memory: WebAssembly.Memory;
213
+ readonly __wbg_adviresult_free: (a: number, b: number) => void;
101
214
  readonly __wbg_aotsampler_free: (a: number, b: number) => void;
102
215
  readonly __wbg_compiledtape_free: (a: number, b: number) => void;
216
+ readonly __wbg_sampleresult_free: (a: number, b: number) => void;
217
+ readonly adviresult_elboTrace: (a: number) => [number, number];
218
+ readonly adviresult_mu: (a: number) => [number, number];
219
+ readonly adviresult_muSnapshots: (a: number) => [number, number];
220
+ readonly adviresult_sigma: (a: number) => [number, number];
221
+ readonly adviresult_snapshotIters: (a: number) => [number, number];
222
+ readonly aotsampler_advi: (a: number, b: number, c: number, d: number, e: number, f: number, g: bigint, h: number, i: number) => [number, number, number];
103
223
  readonly aotsampler_logProbGrad: (a: number, b: number, c: number) => [number, number, number, number];
104
224
  readonly aotsampler_nParams: (a: number) => number;
105
225
  readonly aotsampler_new: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
106
226
  readonly aotsampler_sample: (a: number, b: number, c: number, d: number, e: number, f: bigint) => [number, number, number, number];
227
+ readonly aotsampler_sampleWithStats: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: number) => [number, number, number];
228
+ readonly aotsampler_setGradBasedEstimate: (a: number, b: number) => void;
229
+ readonly aotsampler_setTargetAccept: (a: number, b: number) => [number, number];
107
230
  readonly clearAotExports: () => void;
108
- readonly compileTape: (a: number, b: number) => [number, number, number];
231
+ readonly compileTape: (a: number, b: number, c: number, d: number) => [number, number, number];
109
232
  readonly compiledtape_layoutId: (a: number) => number;
110
233
  readonly compiledtape_nParams: (a: number) => number;
111
234
  readonly compiledtape_scratchInit: (a: number) => [number, number];
112
235
  readonly compiledtape_wasm: (a: number) => [number, number];
113
236
  readonly init_panic_hook: () => void;
237
+ readonly sampleresult_diverging: (a: number) => [number, number];
238
+ readonly sampleresult_draws: (a: number) => [number, number];
239
+ readonly sampleresult_lp: (a: number) => [number, number];
240
+ readonly sampleresult_numSteps: (a: number) => [number, number];
241
+ readonly sampleresult_stepSize: (a: number) => [number, number];
242
+ readonly sampleresult_tuning: (a: number) => [number, number];
114
243
  readonly setAotExports: (a: any) => void;
115
244
  readonly sharedMemory: () => any;
116
245
  readonly tapewasmVersion: () => [number, number];
117
246
  readonly __wbindgen_malloc: (a: number, b: number) => number;
118
247
  readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
119
248
  readonly __wbindgen_free: (a: number, b: number, c: number) => void;
249
+ readonly __wbindgen_exn_store: (a: number) => void;
250
+ readonly __externref_table_alloc: () => number;
120
251
  readonly __wbindgen_externrefs: WebAssembly.Table;
121
252
  readonly __externref_table_dealloc: (a: number) => void;
122
- readonly __externref_table_alloc: () => number;
123
253
  readonly __wbindgen_start: () => void;
124
254
  }
125
255
 
package/pkg/tapewasm.js CHANGED
@@ -1,11 +1,84 @@
1
1
  /* @ts-self-types="./tapewasm.d.ts" */
2
- import { aot_logp } from './snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js';
3
- import * as import1 from "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js"
4
- import * as import2 from "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js"
5
- import * as import3 from "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js"
6
- import * as import4 from "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js"
2
+ import { aot_logp } from './snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js';
3
+ import * as import1 from "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js"
4
+ import * as import2 from "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js"
5
+ import * as import3 from "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js"
6
+ import * as import4 from "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js"
7
7
 
8
8
 
9
+ /**
10
+ * The fitted mean-field variational distribution and its ELBO trajectory.
11
+ */
12
+ export class AdviResult {
13
+ static __wrap(ptr) {
14
+ const obj = Object.create(AdviResult.prototype);
15
+ obj.__wbg_ptr = ptr;
16
+ AdviResultFinalization.register(obj, obj.__wbg_ptr, obj);
17
+ return obj;
18
+ }
19
+ __destroy_into_raw() {
20
+ const ptr = this.__wbg_ptr;
21
+ this.__wbg_ptr = 0;
22
+ AdviResultFinalization.unregister(this);
23
+ return ptr;
24
+ }
25
+ free() {
26
+ const ptr = this.__destroy_into_raw();
27
+ wasm.__wbg_adviresult_free(ptr, 0);
28
+ }
29
+ /**
30
+ * The ELBO estimate after each iteration, in order.
31
+ * @returns {Float64Array}
32
+ */
33
+ get elboTrace() {
34
+ const ret = wasm.adviresult_elboTrace(this.__wbg_ptr);
35
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
36
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
37
+ return v1;
38
+ }
39
+ /**
40
+ * The raw (pre-averaging) iterate `μ` every `snapshot_every` iterations,
41
+ * one snapshot's `n_params` values after another. Empty unless `advi`
42
+ * was called with `snapshot_every > 0`.
43
+ * @returns {Float64Array}
44
+ */
45
+ get muSnapshots() {
46
+ const ret = wasm.adviresult_muSnapshots(this.__wbg_ptr);
47
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
48
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
49
+ return v1;
50
+ }
51
+ /**
52
+ * @returns {Float64Array}
53
+ */
54
+ get mu() {
55
+ const ret = wasm.adviresult_mu(this.__wbg_ptr);
56
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
57
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
58
+ return v1;
59
+ }
60
+ /**
61
+ * @returns {Float64Array}
62
+ */
63
+ get sigma() {
64
+ const ret = wasm.adviresult_sigma(this.__wbg_ptr);
65
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
66
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
67
+ return v1;
68
+ }
69
+ /**
70
+ * The iteration number each entry of `muSnapshots` was taken at.
71
+ * @returns {Float64Array}
72
+ */
73
+ get snapshotIters() {
74
+ const ret = wasm.adviresult_snapshotIters(this.__wbg_ptr);
75
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
76
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
77
+ return v1;
78
+ }
79
+ }
80
+ if (Symbol.dispose) AdviResult.prototype[Symbol.dispose] = AdviResult.prototype.free;
81
+
9
82
  /**
10
83
  * A sampler over one compiled module.
11
84
  *
@@ -24,6 +97,53 @@ export class AotSampler {
24
97
  const ptr = this.__destroy_into_raw();
25
98
  wasm.__wbg_aotsampler_free(ptr, 0);
26
99
  }
100
+ /**
101
+ * Mean-field ADVI: fits `q(θ) = N(μ, diag(σ²))` to the log density by
102
+ * Adam-ascending the ELBO under the reparameterization trick
103
+ * `θ = μ + σ·η`, `η ~ N(0, I)`. `init` seeds `μ`; `ω = log σ` starts at
104
+ * `0` (`σ = 1`).
105
+ *
106
+ * The ELBO gradient combines the tape's `∂logp/∂θ` (from `log_prob_grad`)
107
+ * with the reparameterization's own Jacobian and the mean-field Gaussian
108
+ * entropy's gradient, both exact rather than estimated:
109
+ * `∂ELBO/∂μ_i = ∂logp/∂θ_i`, `∂ELBO/∂ω_i = ∂logp/∂θ_i · σ_i · η_i + 1`.
110
+ * Each is averaged over `mc_samples` reparameterized draws per iteration.
111
+ *
112
+ * `AdviResult::mu`/`sigma` average `(μ, ω)` over the second half of
113
+ * `num_iters` (Polyak averaging): a constant Adam step size never settles
114
+ * on one point, so the plain last iterate keeps wandering a "noise ball"
115
+ * around the optimum. `elbo_trace` still reports the raw iterate's ELBO
116
+ * per step, for diagnosing convergence rather than for the fitted result.
117
+ *
118
+ * `snapshot_every` (0 disables it) additionally copies the raw iterate
119
+ * `μ` every that-many iterations into `AdviResult::muSnapshots` — a film
120
+ * strip of the one training run, rather than several shorter ones
121
+ * spliced together: restarting Adam's moment estimates partway through
122
+ * measurably converges to a worse optimum, so this is the only way to
123
+ * watch a run progress without paying for that.
124
+ *
125
+ * `on_snapshot`, if given, is also called at each snapshot with the
126
+ * iteration, a copy of that `μ`, and the ELBO trace since the previous
127
+ * call — so a run inside a Worker can report as it goes rather than only
128
+ * once it returns.
129
+ * @param {Float64Array} init
130
+ * @param {number} num_iters
131
+ * @param {number} mc_samples
132
+ * @param {number} learning_rate
133
+ * @param {bigint} seed
134
+ * @param {number} snapshot_every
135
+ * @param {((iter: number, mu: Float64Array, elbo: Float64Array) => void) | null} [on_snapshot]
136
+ * @returns {AdviResult}
137
+ */
138
+ advi(init, num_iters, mc_samples, learning_rate, seed, snapshot_every, on_snapshot) {
139
+ const ptr0 = passArrayF64ToWasm0(init, wasm.__wbindgen_malloc);
140
+ const len0 = WASM_VECTOR_LEN;
141
+ const ret = wasm.aotsampler_advi(this.__wbg_ptr, ptr0, len0, num_iters, mc_samples, learning_rate, seed, snapshot_every, isLikeNone(on_snapshot) ? 0 : addToExternrefTable0(on_snapshot));
142
+ if (ret[2]) {
143
+ throw takeFromExternrefTable0(ret[1]);
144
+ }
145
+ return AdviResult.__wrap(ret[0]);
146
+ }
27
147
  /**
28
148
  * `[log_prob, d/dparam...]`.
29
149
  * @param {Float64Array} params
@@ -75,6 +195,26 @@ export class AotSampler {
75
195
  AotSamplerFinalization.register(this, this.__wbg_ptr, this);
76
196
  return this;
77
197
  }
198
+ /**
199
+ * [`sample`](Self::sample) with each draw's sampler statistics beside it —
200
+ * what ArviZ keeps as `sample_stats`. `chain` only labels the run: a
201
+ * different seed is what keeps two chains apart.
202
+ * @param {Float64Array} init
203
+ * @param {number} num_warmup
204
+ * @param {number} num_draws
205
+ * @param {bigint} seed
206
+ * @param {number} chain
207
+ * @returns {SampleResult}
208
+ */
209
+ sampleWithStats(init, num_warmup, num_draws, seed, chain) {
210
+ const ptr0 = passArrayF64ToWasm0(init, wasm.__wbindgen_malloc);
211
+ const len0 = WASM_VECTOR_LEN;
212
+ const ret = wasm.aotsampler_sampleWithStats(this.__wbg_ptr, ptr0, len0, num_warmup, num_draws, seed, chain);
213
+ if (ret[2]) {
214
+ throw takeFromExternrefTable0(ret[1]);
215
+ }
216
+ return SampleResult.__wrap(ret[0]);
217
+ }
78
218
  /**
79
219
  * `num_warmup + num_draws` draws, row-major, `n_params` wide.
80
220
  * @param {Float64Array} init
@@ -94,6 +234,27 @@ export class AotSampler {
94
234
  wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
95
235
  return v2;
96
236
  }
237
+ /**
238
+ * Estimate the diagonal metric from the gradients as well as the draws, as
239
+ * nuts-rs does by default. Off unless set, to match the reference
240
+ * posteriors; see [`nuts_settings`].
241
+ * @param {boolean} on
242
+ */
243
+ setGradBasedEstimate(on) {
244
+ wasm.aotsampler_setGradBasedEstimate(this.__wbg_ptr, on);
245
+ }
246
+ /**
247
+ * Aim warmup's step-size adaptation at this acceptance rate instead of
248
+ * nuts-rs's 0.8. Higher adapts a smaller step: fewer divergences on a hard
249
+ * geometry, more gradients per draw.
250
+ * @param {number} target
251
+ */
252
+ setTargetAccept(target) {
253
+ const ret = wasm.aotsampler_setTargetAccept(this.__wbg_ptr, target);
254
+ if (ret[1]) {
255
+ throw takeFromExternrefTable0(ret[0]);
256
+ }
257
+ }
97
258
  }
98
259
  if (Symbol.dispose) AotSampler.prototype[Symbol.dispose] = AotSampler.prototype.free;
99
260
 
@@ -154,6 +315,89 @@ export class CompiledTape {
154
315
  }
155
316
  if (Symbol.dispose) CompiledTape.prototype[Symbol.dispose] = CompiledTape.prototype.free;
156
317
 
318
+ /**
319
+ * Draws and, beside each, the sampler's statistics — warmup first, as
320
+ * [`AotSampler::sample`] returns them.
321
+ */
322
+ export class SampleResult {
323
+ static __wrap(ptr) {
324
+ const obj = Object.create(SampleResult.prototype);
325
+ obj.__wbg_ptr = ptr;
326
+ SampleResultFinalization.register(obj, obj.__wbg_ptr, obj);
327
+ return obj;
328
+ }
329
+ __destroy_into_raw() {
330
+ const ptr = this.__wbg_ptr;
331
+ this.__wbg_ptr = 0;
332
+ SampleResultFinalization.unregister(this);
333
+ return ptr;
334
+ }
335
+ free() {
336
+ const ptr = this.__destroy_into_raw();
337
+ wasm.__wbg_sampleresult_free(ptr, 0);
338
+ }
339
+ /**
340
+ * 1 where the draw's trajectory diverged.
341
+ * @returns {Uint8Array}
342
+ */
343
+ get diverging() {
344
+ const ret = wasm.sampleresult_diverging(this.__wbg_ptr);
345
+ var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
346
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
347
+ return v1;
348
+ }
349
+ /**
350
+ * `num_warmup + num_draws` draws, row-major, `n_params` wide.
351
+ * @returns {Float64Array}
352
+ */
353
+ get draws() {
354
+ const ret = wasm.sampleresult_draws(this.__wbg_ptr);
355
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
356
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
357
+ return v1;
358
+ }
359
+ /**
360
+ * The log density at each draw.
361
+ * @returns {Float64Array}
362
+ */
363
+ get lp() {
364
+ const ret = wasm.sampleresult_lp(this.__wbg_ptr);
365
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
366
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
367
+ return v1;
368
+ }
369
+ /**
370
+ * Leapfrog steps the draw's trajectory took.
371
+ * @returns {Uint32Array}
372
+ */
373
+ get numSteps() {
374
+ const ret = wasm.sampleresult_numSteps(this.__wbg_ptr);
375
+ var v1 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
376
+ wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
377
+ return v1;
378
+ }
379
+ /**
380
+ * @returns {Float64Array}
381
+ */
382
+ get stepSize() {
383
+ const ret = wasm.sampleresult_stepSize(this.__wbg_ptr);
384
+ var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
385
+ wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
386
+ return v1;
387
+ }
388
+ /**
389
+ * 1 for the warmup draws.
390
+ * @returns {Uint8Array}
391
+ */
392
+ get tuning() {
393
+ const ret = wasm.sampleresult_tuning(this.__wbg_ptr);
394
+ var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
395
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
396
+ return v1;
397
+ }
398
+ }
399
+ if (Symbol.dispose) SampleResult.prototype[Symbol.dispose] = SampleResult.prototype.free;
400
+
157
401
  /**
158
402
  * Release the bound module's exports. The next draw will throw.
159
403
  */
@@ -171,13 +415,23 @@ export function clearAotExports() {
171
415
  *
172
416
  * The format is not an artifact and carries no compatibility promise: a tape
173
417
  * is written and consumed inside one call.
418
+ *
419
+ * `reroll` says when a vectorised statement becomes a wasm loop: `"auto"`
420
+ * (the default, straight-line below a size threshold), `"always"` or
421
+ * `"never"`. Which is faster is an engine's preference, not the model's —
422
+ * on one real model straight-line was faster on V8 and slower on
423
+ * SpiderMonkey and JavaScriptCore — and `"always"` is also the smallest
424
+ * module, often by an order of magnitude.
174
425
  * @param {string} tape
426
+ * @param {string | null} [reroll]
175
427
  * @returns {CompiledTape}
176
428
  */
177
- export function compileTape(tape) {
429
+ export function compileTape(tape, reroll) {
178
430
  const ptr0 = passStringToWasm0(tape, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
179
431
  const len0 = WASM_VECTOR_LEN;
180
- const ret = wasm.compileTape(ptr0, len0);
432
+ var ptr1 = isLikeNone(reroll) ? 0 : passStringToWasm0(reroll, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
433
+ var len1 = WASM_VECTOR_LEN;
434
+ const ret = wasm.compileTape(ptr0, len0, ptr1, len1);
181
435
  if (ret[2]) {
182
436
  throw takeFromExternrefTable0(ret[1]);
183
437
  }
@@ -238,6 +492,13 @@ function __wbg_get_imports() {
238
492
  const ret = Error(getStringFromWasm0(arg0, arg1));
239
493
  return ret;
240
494
  },
495
+ __wbg___wbindgen_debug_string_0e68cf47c9cbd9b0: function(arg0, arg1) {
496
+ const ret = debugString(arg1);
497
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
498
+ const len1 = WASM_VECTOR_LEN;
499
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
500
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
501
+ },
241
502
  __wbg___wbindgen_memory_3f8442e22540244f: function() {
242
503
  const ret = wasm.memory;
243
504
  return ret;
@@ -253,10 +514,18 @@ function __wbg_get_imports() {
253
514
  __wbg___wbindgen_throw_5d9e815e6fdf150f: function(arg0, arg1) {
254
515
  throw new Error(getStringFromWasm0(arg0, arg1));
255
516
  },
256
- __wbg_aot_logp_f9a6d0f4683f5890: function(arg0, arg1, arg2, arg3) {
517
+ __wbg_aot_logp_73e1e4a75800334a: function(arg0, arg1, arg2, arg3) {
257
518
  const ret = aot_logp(arg0 >>> 0, arg1 >>> 0, arg2 >>> 0, arg3 >>> 0);
258
519
  return ret;
259
520
  },
521
+ __wbg_call_5c65f3296b077120: function() { return handleError(function (arg0, arg1, arg2, arg3, arg4, arg5, arg6) {
522
+ var v0 = getArrayF64FromWasm0(arg3, arg4).slice();
523
+ wasm.__wbindgen_free(arg3, arg4 * 8, 8);
524
+ var v1 = getArrayF64FromWasm0(arg5, arg6).slice();
525
+ wasm.__wbindgen_free(arg5, arg6 * 8, 8);
526
+ const ret = arg0.call(arg1, arg2, v0, v1);
527
+ return ret;
528
+ }, arguments); },
260
529
  __wbg_error_757e9472f8410341: function(arg0, arg1) {
261
530
  let deferred0_0;
262
531
  let deferred0_1;
@@ -292,19 +561,25 @@ function __wbg_get_imports() {
292
561
  return {
293
562
  __proto__: null,
294
563
  "./tapewasm_bg.js": import0,
295
- "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js": import1,
296
- "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js": import2,
297
- "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js": import3,
298
- "./snippets/tapewasm-efcc61b460c384cc/js/aot_bridge.js": import4,
564
+ "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js": import1,
565
+ "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js": import2,
566
+ "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js": import3,
567
+ "./snippets/tapewasm-6c5813ce65cccbcf/js/aot_bridge.js": import4,
299
568
  };
300
569
  }
301
570
 
571
+ const AdviResultFinalization = (typeof FinalizationRegistry === 'undefined')
572
+ ? { register: () => {}, unregister: () => {} }
573
+ : new FinalizationRegistry(ptr => wasm.__wbg_adviresult_free(ptr, 1));
302
574
  const AotSamplerFinalization = (typeof FinalizationRegistry === 'undefined')
303
575
  ? { register: () => {}, unregister: () => {} }
304
576
  : new FinalizationRegistry(ptr => wasm.__wbg_aotsampler_free(ptr, 1));
305
577
  const CompiledTapeFinalization = (typeof FinalizationRegistry === 'undefined')
306
578
  ? { register: () => {}, unregister: () => {} }
307
579
  : new FinalizationRegistry(ptr => wasm.__wbg_compiledtape_free(ptr, 1));
580
+ const SampleResultFinalization = (typeof FinalizationRegistry === 'undefined')
581
+ ? { register: () => {}, unregister: () => {} }
582
+ : new FinalizationRegistry(ptr => wasm.__wbg_sampleresult_free(ptr, 1));
308
583
 
309
584
  function addToExternrefTable0(obj) {
310
585
  const idx = wasm.__externref_table_alloc();
@@ -312,11 +587,81 @@ function addToExternrefTable0(obj) {
312
587
  return idx;
313
588
  }
314
589
 
590
+ function debugString(val) {
591
+ // primitive types
592
+ const type = typeof val;
593
+ if (type == 'number' || type == 'boolean' || val == null) {
594
+ return `${val}`;
595
+ }
596
+ if (type == 'string') {
597
+ return `"${val}"`;
598
+ }
599
+ if (type == 'symbol') {
600
+ const description = val.description;
601
+ if (description == null) {
602
+ return 'Symbol';
603
+ } else {
604
+ return `Symbol(${description})`;
605
+ }
606
+ }
607
+ if (type == 'function') {
608
+ const name = val.name;
609
+ if (typeof name == 'string' && name.length > 0) {
610
+ return `Function(${name})`;
611
+ } else {
612
+ return 'Function';
613
+ }
614
+ }
615
+ // objects
616
+ if (Array.isArray(val)) {
617
+ const length = val.length;
618
+ let debug = '[';
619
+ if (length > 0) {
620
+ debug += debugString(val[0]);
621
+ }
622
+ for(let i = 1; i < length; i++) {
623
+ debug += ', ' + debugString(val[i]);
624
+ }
625
+ debug += ']';
626
+ return debug;
627
+ }
628
+ // Test for built-in
629
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
630
+ let className;
631
+ if (builtInMatches && builtInMatches.length > 1) {
632
+ className = builtInMatches[1];
633
+ } else {
634
+ // Failed to match the standard '[object ClassName]'
635
+ return toString.call(val);
636
+ }
637
+ if (className == 'Object') {
638
+ // we're a user defined class or Object
639
+ // JSON.stringify avoids problems with cycles, and is generally much
640
+ // easier than looping through ownProperties of `val`.
641
+ try {
642
+ return 'Object(' + JSON.stringify(val) + ')';
643
+ } catch (_) {
644
+ return 'Object';
645
+ }
646
+ }
647
+ // errors
648
+ if (val instanceof Error) {
649
+ return `${val.name}: ${val.message}\n${val.stack}`;
650
+ }
651
+ // TODO we could test for more things here, like `Set`s and `Map`s.
652
+ return className;
653
+ }
654
+
315
655
  function getArrayF64FromWasm0(ptr, len) {
316
656
  ptr = ptr >>> 0;
317
657
  return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
318
658
  }
319
659
 
660
+ function getArrayU32FromWasm0(ptr, len) {
661
+ ptr = ptr >>> 0;
662
+ return getUint32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
663
+ }
664
+
320
665
  function getArrayU8FromWasm0(ptr, len) {
321
666
  ptr = ptr >>> 0;
322
667
  return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
@@ -342,6 +687,14 @@ function getStringFromWasm0(ptr, len) {
342
687
  return decodeText(ptr >>> 0, len);
343
688
  }
344
689
 
690
+ let cachedUint32ArrayMemory0 = null;
691
+ function getUint32ArrayMemory0() {
692
+ if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {
693
+ cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);
694
+ }
695
+ return cachedUint32ArrayMemory0;
696
+ }
697
+
345
698
  let cachedUint8ArrayMemory0 = null;
346
699
  function getUint8ArrayMemory0() {
347
700
  if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
@@ -350,6 +703,15 @@ function getUint8ArrayMemory0() {
350
703
  return cachedUint8ArrayMemory0;
351
704
  }
352
705
 
706
+ function handleError(f, args) {
707
+ try {
708
+ return f.apply(this, args);
709
+ } catch (e) {
710
+ const idx = addToExternrefTable0(e);
711
+ wasm.__wbindgen_exn_store(idx);
712
+ }
713
+ }
714
+
353
715
  function isLikeNone(x) {
354
716
  return x === undefined || x === null;
355
717
  }
@@ -450,6 +812,7 @@ function __wbg_finalize_init(instance, module) {
450
812
  wasmModule = module;
451
813
  cachedDataViewMemory0 = null;
452
814
  cachedFloat64ArrayMemory0 = null;
815
+ cachedUint32ArrayMemory0 = null;
453
816
  cachedUint8ArrayMemory0 = null;
454
817
  wasm.__wbindgen_start();
455
818
  return wasm;
Binary file
@@ -1,26 +1,44 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
4
+ export const __wbg_adviresult_free: (a: number, b: number) => void;
4
5
  export const __wbg_aotsampler_free: (a: number, b: number) => void;
5
6
  export const __wbg_compiledtape_free: (a: number, b: number) => void;
7
+ export const __wbg_sampleresult_free: (a: number, b: number) => void;
8
+ export const adviresult_elboTrace: (a: number) => [number, number];
9
+ export const adviresult_mu: (a: number) => [number, number];
10
+ export const adviresult_muSnapshots: (a: number) => [number, number];
11
+ export const adviresult_sigma: (a: number) => [number, number];
12
+ export const adviresult_snapshotIters: (a: number) => [number, number];
13
+ export const aotsampler_advi: (a: number, b: number, c: number, d: number, e: number, f: number, g: bigint, h: number, i: number) => [number, number, number];
6
14
  export const aotsampler_logProbGrad: (a: number, b: number, c: number) => [number, number, number, number];
7
15
  export const aotsampler_nParams: (a: number) => number;
8
16
  export const aotsampler_new: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
9
17
  export const aotsampler_sample: (a: number, b: number, c: number, d: number, e: number, f: bigint) => [number, number, number, number];
18
+ export const aotsampler_sampleWithStats: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: number) => [number, number, number];
19
+ export const aotsampler_setGradBasedEstimate: (a: number, b: number) => void;
20
+ export const aotsampler_setTargetAccept: (a: number, b: number) => [number, number];
10
21
  export const clearAotExports: () => void;
11
- export const compileTape: (a: number, b: number) => [number, number, number];
22
+ export const compileTape: (a: number, b: number, c: number, d: number) => [number, number, number];
12
23
  export const compiledtape_layoutId: (a: number) => number;
13
24
  export const compiledtape_nParams: (a: number) => number;
14
25
  export const compiledtape_scratchInit: (a: number) => [number, number];
15
26
  export const compiledtape_wasm: (a: number) => [number, number];
16
27
  export const init_panic_hook: () => void;
28
+ export const sampleresult_diverging: (a: number) => [number, number];
29
+ export const sampleresult_draws: (a: number) => [number, number];
30
+ export const sampleresult_lp: (a: number) => [number, number];
31
+ export const sampleresult_numSteps: (a: number) => [number, number];
32
+ export const sampleresult_stepSize: (a: number) => [number, number];
33
+ export const sampleresult_tuning: (a: number) => [number, number];
17
34
  export const setAotExports: (a: any) => void;
18
35
  export const sharedMemory: () => any;
19
36
  export const tapewasmVersion: () => [number, number];
20
37
  export const __wbindgen_malloc: (a: number, b: number) => number;
21
38
  export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
22
39
  export const __wbindgen_free: (a: number, b: number, c: number) => void;
40
+ export const __wbindgen_exn_store: (a: number) => void;
41
+ export const __externref_table_alloc: () => number;
23
42
  export const __wbindgen_externrefs: WebAssembly.Table;
24
43
  export const __externref_table_dealloc: (a: number) => void;
25
- export const __externref_table_alloc: () => number;
26
44
  export const __wbindgen_start: () => void;