tapewasm 0.1.0 → 0.2.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/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 habakan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -7,4 +7,4 @@ See the [repository](https://github.com/habakan/tapewasm) for what this is for
7
7
  and how a front end uses it. The npm package of the same name is this crate
8
8
  built with `wasm-pack`.
9
9
 
10
- Licensed under Apache-2.0.
10
+ Licensed under either of Apache-2.0 or MIT, at your option.
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
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,
5
6
  compileTape,
6
7
  tapewasmVersion,
package/index.js CHANGED
@@ -5,6 +5,7 @@
5
5
  import init from "./pkg/tapewasm.js";
6
6
  export {
7
7
  AotSampler,
8
+ AdviResult,
8
9
  CompiledTape,
9
10
  compileTape,
10
11
  tapewasmVersion,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tapewasm",
3
- "version": "0.1.0",
3
+ "version": "0.2.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",
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "./pkg/*": "./pkg/*"
15
15
  },
16
- "files": ["index.js", "index.d.ts", "pkg/", "LICENSE", "README.md"],
16
+ "files": ["index.js", "index.d.ts", "pkg/", "LICENSE-APACHE", "LICENSE-MIT", "README.md"],
17
17
  "scripts": {
18
18
  "build:wasm": "make -C .. wasm"
19
19
  },
@@ -23,5 +23,5 @@
23
23
  },
24
24
  "homepage": "https://github.com/habakan/tapewasm#readme",
25
25
  "keywords": ["autodiff", "gradient", "bayesian", "mcmc", "nuts", "webassembly", "wasm"],
26
- "license": "Apache-2.0"
26
+ "license": "MIT OR Apache-2.0"
27
27
  }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 habakan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/pkg/README.md CHANGED
@@ -7,4 +7,4 @@ See the [repository](https://github.com/habakan/tapewasm) for what this is for
7
7
  and how a front end uses it. The npm package of the same name is this crate
8
8
  built with `wasm-pack`.
9
9
 
10
- Licensed under Apache-2.0.
10
+ Licensed under either of Apache-2.0 or MIT, at your option.
package/pkg/package.json CHANGED
@@ -2,8 +2,8 @@
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.0",
6
- "license": "Apache-2.0",
5
+ "version": "0.2.0",
6
+ "license": "MIT OR Apache-2.0",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "https://github.com/habakan/tapewasm"
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,32 @@
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
+ advi(init: Float64Array, num_iters: number, mc_samples: number, learning_rate: number, seed: bigint, snapshot_every: number): AdviResult;
14
65
  /**
15
66
  * `[log_prob, d/dparam...]`.
16
67
  */
@@ -98,8 +149,15 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
98
149
 
99
150
  export interface InitOutput {
100
151
  readonly memory: WebAssembly.Memory;
152
+ readonly __wbg_adviresult_free: (a: number, b: number) => void;
101
153
  readonly __wbg_aotsampler_free: (a: number, b: number) => void;
102
154
  readonly __wbg_compiledtape_free: (a: number, b: number) => void;
155
+ readonly adviresult_elboTrace: (a: number) => [number, number];
156
+ readonly adviresult_mu: (a: number) => [number, number];
157
+ readonly adviresult_muSnapshots: (a: number) => [number, number];
158
+ readonly adviresult_sigma: (a: number) => [number, number];
159
+ readonly adviresult_snapshotIters: (a: number) => [number, number];
160
+ readonly aotsampler_advi: (a: number, b: number, c: number, d: number, e: number, f: number, g: bigint, h: number) => [number, number, number];
103
161
  readonly aotsampler_logProbGrad: (a: number, b: number, c: number) => [number, number, number, number];
104
162
  readonly aotsampler_nParams: (a: number) => number;
105
163
  readonly aotsampler_new: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
package/pkg/tapewasm.js CHANGED
@@ -1,11 +1,84 @@
1
1
  /* @ts-self-types="./tapewasm.d.ts" */
2
- import { aot_logp } from './snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js';
3
- import * as import1 from "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js"
4
- import * as import2 from "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js"
5
- import * as import3 from "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js"
6
- import * as import4 from "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js"
2
+ import { aot_logp } from './snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js';
3
+ import * as import1 from "./snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js"
4
+ import * as import2 from "./snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js"
5
+ import * as import3 from "./snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js"
6
+ import * as import4 from "./snippets/tapewasm-3fbd96d1430b56d5/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,47 @@ 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
+ * @param {Float64Array} init
125
+ * @param {number} num_iters
126
+ * @param {number} mc_samples
127
+ * @param {number} learning_rate
128
+ * @param {bigint} seed
129
+ * @param {number} snapshot_every
130
+ * @returns {AdviResult}
131
+ */
132
+ advi(init, num_iters, mc_samples, learning_rate, seed, snapshot_every) {
133
+ const ptr0 = passArrayF64ToWasm0(init, wasm.__wbindgen_malloc);
134
+ const len0 = WASM_VECTOR_LEN;
135
+ const ret = wasm.aotsampler_advi(this.__wbg_ptr, ptr0, len0, num_iters, mc_samples, learning_rate, seed, snapshot_every);
136
+ if (ret[2]) {
137
+ throw takeFromExternrefTable0(ret[1]);
138
+ }
139
+ return AdviResult.__wrap(ret[0]);
140
+ }
27
141
  /**
28
142
  * `[log_prob, d/dparam...]`.
29
143
  * @param {Float64Array} params
@@ -253,7 +367,7 @@ function __wbg_get_imports() {
253
367
  __wbg___wbindgen_throw_5d9e815e6fdf150f: function(arg0, arg1) {
254
368
  throw new Error(getStringFromWasm0(arg0, arg1));
255
369
  },
256
- __wbg_aot_logp_50f9146d22ee7ef4: function(arg0, arg1, arg2, arg3) {
370
+ __wbg_aot_logp_897c94d09e163a67: function(arg0, arg1, arg2, arg3) {
257
371
  const ret = aot_logp(arg0 >>> 0, arg1 >>> 0, arg2 >>> 0, arg3 >>> 0);
258
372
  return ret;
259
373
  },
@@ -292,13 +406,16 @@ function __wbg_get_imports() {
292
406
  return {
293
407
  __proto__: null,
294
408
  "./tapewasm_bg.js": import0,
295
- "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js": import1,
296
- "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js": import2,
297
- "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js": import3,
298
- "./snippets/tapewasm-86b4af57c39247cc/js/aot_bridge.js": import4,
409
+ "./snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js": import1,
410
+ "./snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js": import2,
411
+ "./snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js": import3,
412
+ "./snippets/tapewasm-3fbd96d1430b56d5/js/aot_bridge.js": import4,
299
413
  };
300
414
  }
301
415
 
416
+ const AdviResultFinalization = (typeof FinalizationRegistry === 'undefined')
417
+ ? { register: () => {}, unregister: () => {} }
418
+ : new FinalizationRegistry(ptr => wasm.__wbg_adviresult_free(ptr, 1));
302
419
  const AotSamplerFinalization = (typeof FinalizationRegistry === 'undefined')
303
420
  ? { register: () => {}, unregister: () => {} }
304
421
  : new FinalizationRegistry(ptr => wasm.__wbg_aotsampler_free(ptr, 1));
Binary file
@@ -1,8 +1,15 @@
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 adviresult_elboTrace: (a: number) => [number, number];
8
+ export const adviresult_mu: (a: number) => [number, number];
9
+ export const adviresult_muSnapshots: (a: number) => [number, number];
10
+ export const adviresult_sigma: (a: number) => [number, number];
11
+ export const adviresult_snapshotIters: (a: number) => [number, number];
12
+ export const aotsampler_advi: (a: number, b: number, c: number, d: number, e: number, f: number, g: bigint, h: number) => [number, number, number];
6
13
  export const aotsampler_logProbGrad: (a: number, b: number, c: number) => [number, number, number, number];
7
14
  export const aotsampler_nParams: (a: number) => number;
8
15
  export const aotsampler_new: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
File without changes
File without changes