tapewasm 0.1.1 → 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/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/pkg/package.json +1 -1
- package/pkg/tapewasm.d.ts +58 -0
- package/pkg/tapewasm.js +127 -10
- package/pkg/tapewasm_bg.wasm +0 -0
- package/pkg/tapewasm_bg.wasm.d.ts +7 -0
- /package/pkg/snippets/{tapewasm-efcc61b460c384cc → tapewasm-3fbd96d1430b56d5}/js/aot_bridge.js +0 -0
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
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.
|
|
5
|
+
"version": "0.2.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,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-
|
|
3
|
-
import * as import1 from "./snippets/tapewasm-
|
|
4
|
-
import * as import2 from "./snippets/tapewasm-
|
|
5
|
-
import * as import3 from "./snippets/tapewasm-
|
|
6
|
-
import * as import4 from "./snippets/tapewasm-
|
|
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
|
-
|
|
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-
|
|
296
|
-
"./snippets/tapewasm-
|
|
297
|
-
"./snippets/tapewasm-
|
|
298
|
-
"./snippets/tapewasm-
|
|
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));
|
package/pkg/tapewasm_bg.wasm
CHANGED
|
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];
|
/package/pkg/snippets/{tapewasm-efcc61b460c384cc → tapewasm-3fbd96d1430b56d5}/js/aot_bridge.js
RENAMED
|
File without changes
|