stanwasm 0.1.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.
@@ -0,0 +1,195 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * One compiled Stan model. Holds both the parsed AST (`Model`) and a
6
+ * pre-traced `Compiled` for fast log-prob evaluation. Sampling consumes
7
+ * the `Compiled` and re-builds it from the retained AST afterwards, so
8
+ * the same `StanModel` instance can be sampled repeatedly.
9
+ */
10
+ export class StanModel {
11
+ free(): void;
12
+ [Symbol.dispose](): void;
13
+ /**
14
+ * AOT-compile this model to a self-contained wasm module. Returns the
15
+ * wasm bytes (callers can pass these to `WebAssembly.instantiate` to
16
+ * obtain an independent log_prob_grad runtime — useful for Web Workers
17
+ * or for inspection).
18
+ */
19
+ compileToWasm(): Uint8Array;
20
+ /**
21
+ * Constrained values of `parameters` + `transformed parameters` for one
22
+ * unconstrained draw (e.g. one row out of `sample()`'s output), flattened
23
+ * in `paramNames()` order.
24
+ */
25
+ constrainDraw(unconstrained: Float64Array): Float64Array;
26
+ /**
27
+ * Stop step-sampling early (or clean up after it finished naturally —
28
+ * safe to call either way) and restore `logProbGrad`/`sample` by
29
+ * re-tracing, same as `sample()` does at the end of a run.
30
+ */
31
+ finishStepSampling(): void;
32
+ /**
33
+ * Names of the top-level `generated quantities` declarations, flattened
34
+ * the same way as `paramNames()`.
35
+ */
36
+ genQuantityNames(): string[];
37
+ /**
38
+ * Evaluate `generated quantities` for a batch of unconstrained draws
39
+ * (e.g. `sample()`'s output). `draws` is a flat row-major buffer of
40
+ * shape `(n_draws, n_params)`; the result is `(n_draws, n_gen_quantities)`,
41
+ * row-major, in `genQuantityNames()` order. A single RNG stream (seeded
42
+ * by `seed`) is shared across all draws so repeated `_rng` calls don't
43
+ * repeat the same values draw-to-draw.
44
+ *
45
+ * Note: unlike `sampleViaAot`, there is no AOT-compiled counterpart of
46
+ * this method — `compileToWasm` only exports `log_prob_grad`. Generated
47
+ * quantities involve RNG and branching that the flat-tape AOT codegen
48
+ * doesn't model, and (running once per draw rather than once per NUTS
49
+ * leapfrog step) don't need it for performance.
50
+ */
51
+ generatedQuantities(draws: Float64Array, num_draws: number, seed: bigint): Float64Array;
52
+ /**
53
+ * Evaluate log_prob and gradient at `params`. Returns a flat array of
54
+ * length `n_params + 1`: the log-prob is at index 0, gradients follow.
55
+ */
56
+ logProbGrad(params: Float64Array): Float64Array;
57
+ /**
58
+ * Parse `stan_src`, bind `data_json`, trace the model on the autodiff
59
+ * tape, and return a handle ready for sampling.
60
+ */
61
+ constructor(stan_src: string, data_json: string);
62
+ /**
63
+ * Constrained parameter names (parameters then transformed parameters).
64
+ */
65
+ paramNames(): string[];
66
+ /**
67
+ * Run NUTS sampling. Returns a flat row-major buffer of shape
68
+ * `(num_warmup + num_draws) × n_params`. Tuning draws come first.
69
+ */
70
+ sample(init: Float64Array, num_warmup: number, num_draws: number, seed: bigint): Float64Array;
71
+ /**
72
+ * Same as `sample`, but evaluates `log_prob_grad` through a
73
+ * pre-instantiated AOT-compiled model wasm bound via `setAotExports`.
74
+ * V8 JITs the unrolled forward+backward pass in the AOT module, which
75
+ * can be substantially faster than the in-process tape replay used by
76
+ * `sample`. Both produce identical samples for a given seed.
77
+ */
78
+ sampleViaAot(init: Float64Array, num_warmup: number, num_draws: number, seed: bigint): Float64Array;
79
+ /**
80
+ * Start a step-by-step NUTS run: unlike `sample()`, which runs the whole
81
+ * chain inside one wasm call and returns only at the end, this leaves
82
+ * the sampler's state alive in the `StanModel` instance so `stepDraw()`
83
+ * can advance it one draw at a time — genuinely watching the sampler
84
+ * work, not replaying an already-finished chain. Consumes the internal
85
+ * `Compiled` the same way `sample()` does; call `finishStepSampling()`
86
+ * (or exhaust `stepDraw()` up to `num_warmup + num_draws` calls, which
87
+ * does it automatically) before using `logProbGrad`/`sample` again.
88
+ */
89
+ startStepSampling(init: Float64Array, num_warmup: number, num_draws: number, seed: bigint): void;
90
+ /**
91
+ * Advance the step-sampling chain started by `startStepSampling` by
92
+ * exactly one draw. Returns a flat array: `n_params` position values,
93
+ * then `1.0`/`0.0` for whether this draw was still in the warmup
94
+ * (tuning) phase, then `1.0`/`0.0` for whether it diverged, then the
95
+ * leapfrog `step_size` and `num_steps` nuts-rs actually used for this
96
+ * draw — these come straight out of nuts-rs's own dual-averaging
97
+ * adaptation and trajectory-length search, not anything this crate
98
+ * computes, so they're a way to show the real sampler internals at
99
+ * work rather than just the resulting draw. Once the requested
100
+ * `num_warmup + num_draws` draws have all been returned, this
101
+ * automatically restores `logProbGrad`/`sample` (by re-tracing, same as
102
+ * `sample()` does) and further calls fail until `startStepSampling` runs
103
+ * again.
104
+ */
105
+ stepDraw(): Float64Array;
106
+ /**
107
+ * Number of unconstrained parameters.
108
+ */
109
+ readonly n_params: number;
110
+ }
111
+
112
+ /**
113
+ * Release the bound AOT exports. The next `sampleViaAot` call will throw.
114
+ */
115
+ export function clearAotExports(): void;
116
+
117
+ /**
118
+ * Runs once when the wasm module is instantiated. Forwards Rust panics
119
+ * (Stan-typo'd names and invalid RNG parameters are now clean `JsError`s
120
+ * instead, but a handful of internal-invariant panics remain, e.g. index
121
+ * out of bounds on a malformed AST) to `console.error` with a real message
122
+ * and backtrace, instead of an opaque `RuntimeError: unreachable`. The
123
+ * panicking call still traps the instance — this is diagnostics, not
124
+ * recovery — but it means a bug report can include what actually broke.
125
+ */
126
+ export function init_panic_hook(): void;
127
+
128
+ /**
129
+ * Bind a freshly-instantiated AOT model wasm's exports so subsequent
130
+ * `sampleViaAot` calls dispatch through it. Pass `instance.exports`.
131
+ */
132
+ export function setAotExports(exports: any): void;
133
+
134
+ /**
135
+ * Returns the linear memory backing this wasm module. Pass to
136
+ * `WebAssembly.instantiate` as the `stan.memory` import when bringing up an
137
+ * AOT model so the two modules share buffers (zero-copy bridge).
138
+ */
139
+ export function sharedMemory(): any;
140
+
141
+ export function version(): string;
142
+
143
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
144
+
145
+ export interface InitOutput {
146
+ readonly memory: WebAssembly.Memory;
147
+ readonly __wbg_stanmodel_free: (a: number, b: number) => void;
148
+ readonly setAotExports: (a: any) => void;
149
+ readonly stanmodel_compileToWasm: (a: number) => [number, number, number, number];
150
+ readonly stanmodel_constrainDraw: (a: number, b: number, c: number) => [number, number, number, number];
151
+ readonly stanmodel_finishStepSampling: (a: number) => void;
152
+ readonly stanmodel_genQuantityNames: (a: number) => [number, number];
153
+ readonly stanmodel_generatedQuantities: (a: number, b: number, c: number, d: number, e: bigint) => [number, number, number, number];
154
+ readonly stanmodel_logProbGrad: (a: number, b: number, c: number) => [number, number, number, number];
155
+ readonly stanmodel_n_params: (a: number) => number;
156
+ readonly stanmodel_new: (a: number, b: number, c: number, d: number) => [number, number, number];
157
+ readonly stanmodel_paramNames: (a: number) => [number, number];
158
+ readonly stanmodel_sample: (a: number, b: number, c: number, d: number, e: number, f: bigint) => [number, number, number, number];
159
+ readonly stanmodel_sampleViaAot: (a: number, b: number, c: number, d: number, e: number, f: bigint) => [number, number, number, number];
160
+ readonly stanmodel_startStepSampling: (a: number, b: number, c: number, d: number, e: number, f: bigint) => [number, number];
161
+ readonly stanmodel_stepDraw: (a: number) => [number, number, number, number];
162
+ readonly version: () => [number, number];
163
+ readonly clearAotExports: () => void;
164
+ readonly init_panic_hook: () => void;
165
+ readonly sharedMemory: () => any;
166
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
167
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
168
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
169
+ readonly __wbindgen_externrefs: WebAssembly.Table;
170
+ readonly __externref_table_dealloc: (a: number) => void;
171
+ readonly __externref_drop_slice: (a: number, b: number) => void;
172
+ readonly __wbindgen_start: () => void;
173
+ }
174
+
175
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
176
+
177
+ /**
178
+ * Instantiates the given `module`, which can either be bytes or
179
+ * a precompiled `WebAssembly.Module`.
180
+ *
181
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
182
+ *
183
+ * @returns {InitOutput}
184
+ */
185
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
186
+
187
+ /**
188
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
189
+ * for everything else, calls `WebAssembly.instantiate` directly.
190
+ *
191
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
192
+ *
193
+ * @returns {Promise<InitOutput>}
194
+ */
195
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;