@mcmcjs/stan-wasm 0.0.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 +21 -0
- package/README.md +70 -0
- package/dist/chunk-3CMLUNUW.js +206 -0
- package/dist/chunk-3CMLUNUW.js.map +1 -0
- package/dist/index.cjs +229 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.cjs +377 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +17 -0
- package/dist/react/index.d.ts +17 -0
- package/dist/react/index.js +163 -0
- package/dist/react/index.js.map +1 -0
- package/dist/sampler-4wflmHRA.d.cts +76 -0
- package/dist/sampler-4wflmHRA.d.ts +76 -0
- package/dist/worker.js +360 -0
- package/dist/worker.js.map +1 -0
- package/package.json +76 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
type SamplingOpts = {
|
|
2
|
+
num_chains: number;
|
|
3
|
+
num_warmup: number;
|
|
4
|
+
num_samples: number;
|
|
5
|
+
init_radius: number;
|
|
6
|
+
seed?: number;
|
|
7
|
+
};
|
|
8
|
+
type StanVariableInputs = Record<string, unknown>;
|
|
9
|
+
type SampleConfig = SamplingOpts & {
|
|
10
|
+
data: string | StanVariableInputs;
|
|
11
|
+
inits?: string | StanVariableInputs | StanVariableInputs[];
|
|
12
|
+
refresh?: number;
|
|
13
|
+
};
|
|
14
|
+
type Progress = {
|
|
15
|
+
chain: number;
|
|
16
|
+
iteration: number;
|
|
17
|
+
totalIterations: number;
|
|
18
|
+
percent: number;
|
|
19
|
+
warmup: boolean;
|
|
20
|
+
};
|
|
21
|
+
type ConsoleMessage = {
|
|
22
|
+
text: string;
|
|
23
|
+
level: "log" | "error";
|
|
24
|
+
};
|
|
25
|
+
type StanRun = {
|
|
26
|
+
draws: number[][];
|
|
27
|
+
paramNames: string[];
|
|
28
|
+
computeTimeSec: number;
|
|
29
|
+
consoleMessages: ConsoleMessage[];
|
|
30
|
+
sampleConfig: SampleConfig;
|
|
31
|
+
};
|
|
32
|
+
type SamplerEvent = {
|
|
33
|
+
type: "progress";
|
|
34
|
+
report: Progress;
|
|
35
|
+
} | {
|
|
36
|
+
type: "done";
|
|
37
|
+
run: StanRun;
|
|
38
|
+
} | {
|
|
39
|
+
type: "error";
|
|
40
|
+
error: string;
|
|
41
|
+
};
|
|
42
|
+
type SamplerListener = (event: SamplerEvent) => void;
|
|
43
|
+
type CompileStatus = "idle" | "compiling" | "ready" | "error";
|
|
44
|
+
type SampleStatus = "idle" | "loading" | "sampling" | "done" | "error";
|
|
45
|
+
type SamplerState = {
|
|
46
|
+
compileStatus: CompileStatus;
|
|
47
|
+
sampleStatus: SampleStatus;
|
|
48
|
+
compileError: string | null;
|
|
49
|
+
sampleError: string | null;
|
|
50
|
+
progress: Progress | null;
|
|
51
|
+
latestRun: StanRun | null;
|
|
52
|
+
consoleMessages: ConsoleMessage[];
|
|
53
|
+
modelId: string | null;
|
|
54
|
+
mainJsUrl: string | null;
|
|
55
|
+
};
|
|
56
|
+
type CompileResult = {
|
|
57
|
+
modelId: string;
|
|
58
|
+
mainJsUrl: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
interface StanSamplerOptions {
|
|
62
|
+
compileServerUrl: string;
|
|
63
|
+
passcode?: string;
|
|
64
|
+
workerUrl?: string | URL;
|
|
65
|
+
}
|
|
66
|
+
declare class StanSampler {
|
|
67
|
+
#private;
|
|
68
|
+
constructor(opts: StanSamplerOptions);
|
|
69
|
+
get isReady(): boolean;
|
|
70
|
+
get lastCompile(): CompileResult | null;
|
|
71
|
+
compile(stanCode: string, signal?: AbortSignal): Promise<CompileResult>;
|
|
72
|
+
sample(config: SampleConfig, listener?: SamplerListener): Promise<StanRun>;
|
|
73
|
+
cancel(): void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { type CompileResult as C, type Progress as P, type SampleConfig as S, type CompileStatus as a, type ConsoleMessage as b, type SampleStatus as c, type SamplerEvent as d, type SamplerListener as e, type SamplerState as f, type SamplingOpts as g, type StanRun as h, StanSampler as i, type StanSamplerOptions as j, type StanVariableInputs as k };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
type SamplingOpts = {
|
|
2
|
+
num_chains: number;
|
|
3
|
+
num_warmup: number;
|
|
4
|
+
num_samples: number;
|
|
5
|
+
init_radius: number;
|
|
6
|
+
seed?: number;
|
|
7
|
+
};
|
|
8
|
+
type StanVariableInputs = Record<string, unknown>;
|
|
9
|
+
type SampleConfig = SamplingOpts & {
|
|
10
|
+
data: string | StanVariableInputs;
|
|
11
|
+
inits?: string | StanVariableInputs | StanVariableInputs[];
|
|
12
|
+
refresh?: number;
|
|
13
|
+
};
|
|
14
|
+
type Progress = {
|
|
15
|
+
chain: number;
|
|
16
|
+
iteration: number;
|
|
17
|
+
totalIterations: number;
|
|
18
|
+
percent: number;
|
|
19
|
+
warmup: boolean;
|
|
20
|
+
};
|
|
21
|
+
type ConsoleMessage = {
|
|
22
|
+
text: string;
|
|
23
|
+
level: "log" | "error";
|
|
24
|
+
};
|
|
25
|
+
type StanRun = {
|
|
26
|
+
draws: number[][];
|
|
27
|
+
paramNames: string[];
|
|
28
|
+
computeTimeSec: number;
|
|
29
|
+
consoleMessages: ConsoleMessage[];
|
|
30
|
+
sampleConfig: SampleConfig;
|
|
31
|
+
};
|
|
32
|
+
type SamplerEvent = {
|
|
33
|
+
type: "progress";
|
|
34
|
+
report: Progress;
|
|
35
|
+
} | {
|
|
36
|
+
type: "done";
|
|
37
|
+
run: StanRun;
|
|
38
|
+
} | {
|
|
39
|
+
type: "error";
|
|
40
|
+
error: string;
|
|
41
|
+
};
|
|
42
|
+
type SamplerListener = (event: SamplerEvent) => void;
|
|
43
|
+
type CompileStatus = "idle" | "compiling" | "ready" | "error";
|
|
44
|
+
type SampleStatus = "idle" | "loading" | "sampling" | "done" | "error";
|
|
45
|
+
type SamplerState = {
|
|
46
|
+
compileStatus: CompileStatus;
|
|
47
|
+
sampleStatus: SampleStatus;
|
|
48
|
+
compileError: string | null;
|
|
49
|
+
sampleError: string | null;
|
|
50
|
+
progress: Progress | null;
|
|
51
|
+
latestRun: StanRun | null;
|
|
52
|
+
consoleMessages: ConsoleMessage[];
|
|
53
|
+
modelId: string | null;
|
|
54
|
+
mainJsUrl: string | null;
|
|
55
|
+
};
|
|
56
|
+
type CompileResult = {
|
|
57
|
+
modelId: string;
|
|
58
|
+
mainJsUrl: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
interface StanSamplerOptions {
|
|
62
|
+
compileServerUrl: string;
|
|
63
|
+
passcode?: string;
|
|
64
|
+
workerUrl?: string | URL;
|
|
65
|
+
}
|
|
66
|
+
declare class StanSampler {
|
|
67
|
+
#private;
|
|
68
|
+
constructor(opts: StanSamplerOptions);
|
|
69
|
+
get isReady(): boolean;
|
|
70
|
+
get lastCompile(): CompileResult | null;
|
|
71
|
+
compile(stanCode: string, signal?: AbortSignal): Promise<CompileResult>;
|
|
72
|
+
sample(config: SampleConfig, listener?: SamplerListener): Promise<StanRun>;
|
|
73
|
+
cancel(): void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { type CompileResult as C, type Progress as P, type SampleConfig as S, type CompileStatus as a, type ConsoleMessage as b, type SampleStatus as c, type SamplerEvent as d, type SamplerListener as e, type SamplerState as f, type SamplingOpts as g, type StanRun as h, StanSampler as i, type StanSamplerOptions as j, type StanVariableInputs as k };
|
package/dist/worker.js
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
// ../../node_modules/.pnpm/tinystan@0.3.3/node_modules/tinystan/dist/util.mjs
|
|
2
|
+
var prepareStanJSON = (obj) => {
|
|
3
|
+
if (typeof obj === "string") return obj;
|
|
4
|
+
else return JSON.stringify(obj);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// ../../node_modules/.pnpm/tinystan@0.3.3/node_modules/tinystan/dist/index.mjs
|
|
8
|
+
var HMC_SAMPLER_VARIABLES = [
|
|
9
|
+
"lp__",
|
|
10
|
+
"accept_stat__",
|
|
11
|
+
"stepsize__",
|
|
12
|
+
"treedepth__",
|
|
13
|
+
"n_leapfrog__",
|
|
14
|
+
"divergent__",
|
|
15
|
+
"energy__"
|
|
16
|
+
];
|
|
17
|
+
var PATHFINDER_VARIABLES = [
|
|
18
|
+
"lp_approx__",
|
|
19
|
+
"lp__",
|
|
20
|
+
"path__"
|
|
21
|
+
];
|
|
22
|
+
var { NULL, PTR_SIZE, defaultSamplerParams, defaultPathfinderParams } = {
|
|
23
|
+
NULL: 0,
|
|
24
|
+
PTR_SIZE: 4,
|
|
25
|
+
defaultSamplerParams: {
|
|
26
|
+
data: "",
|
|
27
|
+
num_chains: 4,
|
|
28
|
+
inits: "",
|
|
29
|
+
seed: null,
|
|
30
|
+
id: 1,
|
|
31
|
+
init_radius: 2,
|
|
32
|
+
num_warmup: 1e3,
|
|
33
|
+
num_samples: 1e3,
|
|
34
|
+
metric: 2,
|
|
35
|
+
save_inv_metric: false,
|
|
36
|
+
init_inv_metric: null,
|
|
37
|
+
adapt: true,
|
|
38
|
+
delta: 0.8,
|
|
39
|
+
gamma: 0.05,
|
|
40
|
+
kappa: 0.75,
|
|
41
|
+
t0: 10,
|
|
42
|
+
init_buffer: 75,
|
|
43
|
+
term_buffer: 50,
|
|
44
|
+
window: 25,
|
|
45
|
+
save_warmup: false,
|
|
46
|
+
stepsize: 1,
|
|
47
|
+
stepsize_jitter: 0,
|
|
48
|
+
max_depth: 10,
|
|
49
|
+
refresh: 100,
|
|
50
|
+
num_threads: -1
|
|
51
|
+
},
|
|
52
|
+
defaultPathfinderParams: {
|
|
53
|
+
data: "",
|
|
54
|
+
num_paths: 4,
|
|
55
|
+
inits: "",
|
|
56
|
+
seed: null,
|
|
57
|
+
id: 1,
|
|
58
|
+
init_radius: 2,
|
|
59
|
+
num_draws: 1e3,
|
|
60
|
+
max_history_size: 5,
|
|
61
|
+
init_alpha: 1e-3,
|
|
62
|
+
tol_obj: 1e-12,
|
|
63
|
+
tol_rel_obj: 1e4,
|
|
64
|
+
tol_grad: 1e-8,
|
|
65
|
+
tol_rel_grad: 1e7,
|
|
66
|
+
tol_param: 1e-8,
|
|
67
|
+
num_iterations: 1e3,
|
|
68
|
+
num_elbo_draws: 25,
|
|
69
|
+
num_multi_draws: 1e3,
|
|
70
|
+
calculate_lp: true,
|
|
71
|
+
psis_resample: true,
|
|
72
|
+
refresh: 100,
|
|
73
|
+
num_threads: -1
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
var src_default = class StanModel {
|
|
77
|
+
m;
|
|
78
|
+
printErrorCallback;
|
|
79
|
+
sep;
|
|
80
|
+
constructor(m, pc) {
|
|
81
|
+
this.m = m;
|
|
82
|
+
this.printErrorCallback = pc;
|
|
83
|
+
this.sep = String.fromCharCode(m._tinystan_separator_char());
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Load a StanModel from a WASM module.
|
|
87
|
+
*
|
|
88
|
+
* @param {Function} createModule A function that resolves to a WASM module. This is
|
|
89
|
+
* much like the one Emscripten creates for you with `-sMODULARIZE`.
|
|
90
|
+
* @param {PrintCallback | null} printCallback A callback that will be called
|
|
91
|
+
* with any print statements from Stan. If null, this will default to `console.log`.
|
|
92
|
+
* @returns {Promise<StanModel>} A promise that resolves to a `StanModel`
|
|
93
|
+
*/
|
|
94
|
+
static async load(createModule, printCallback = null, printErrorCallback = null) {
|
|
95
|
+
printErrorCallback = printErrorCallback ?? printCallback;
|
|
96
|
+
return new StanModel(await createModule({
|
|
97
|
+
print: printCallback,
|
|
98
|
+
printErr: printErrorCallback
|
|
99
|
+
}), printErrorCallback);
|
|
100
|
+
}
|
|
101
|
+
encodeString(s) {
|
|
102
|
+
const len = this.m.lengthBytesUTF8(s) + 1;
|
|
103
|
+
const ptr = this.m._malloc(len);
|
|
104
|
+
this.m.stringToUTF8(s, ptr, len);
|
|
105
|
+
return ptr;
|
|
106
|
+
}
|
|
107
|
+
handleError(rc, err_ptr) {
|
|
108
|
+
if (rc == 0) {
|
|
109
|
+
this.m._free(err_ptr);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const err = this.m.getValue(err_ptr, "*");
|
|
113
|
+
const err_msg_ptr = this.m._tinystan_get_error_message(err);
|
|
114
|
+
const err_msg = "Exception from Stan:\n" + this.m.UTF8ToString(err_msg_ptr);
|
|
115
|
+
this.m._tinystan_destroy_error(err);
|
|
116
|
+
this.m._free(err_ptr);
|
|
117
|
+
this.printErrorCallback?.(err_msg);
|
|
118
|
+
throw new Error(err_msg);
|
|
119
|
+
}
|
|
120
|
+
encodeInits(inits) {
|
|
121
|
+
if (Array.isArray(inits)) return this.encodeString(inits.map((i) => prepareStanJSON(i)).join(this.sep));
|
|
122
|
+
else return this.encodeString(prepareStanJSON(inits));
|
|
123
|
+
}
|
|
124
|
+
/** @ignore
|
|
125
|
+
* withModel serves as something akin to a context manager in
|
|
126
|
+
* Python. It accepts the arguments needed to construct a model
|
|
127
|
+
* (data and seed) and a callback.
|
|
128
|
+
*
|
|
129
|
+
* The callback takes in the model and a deferredFree function.
|
|
130
|
+
* The memory for the allocated model and any pointers which are "registered"
|
|
131
|
+
* by calling deferredFree will be cleaned up when the callback completes,
|
|
132
|
+
* regardless of if this is a normal return or an exception.
|
|
133
|
+
*
|
|
134
|
+
* The result of the callback is then returned or re-thrown.
|
|
135
|
+
*/
|
|
136
|
+
withModel(data, seed, f) {
|
|
137
|
+
const data_ptr = this.encodeString(prepareStanJSON(data));
|
|
138
|
+
const err_ptr = this.m._malloc(PTR_SIZE);
|
|
139
|
+
const model2 = this.m._tinystan_create_model(data_ptr, seed, NULL, err_ptr);
|
|
140
|
+
this.m._free(data_ptr);
|
|
141
|
+
this.handleError(model2 === 0, err_ptr);
|
|
142
|
+
const ptrs = [];
|
|
143
|
+
const deferredFree = (p) => ptrs.push(p);
|
|
144
|
+
try {
|
|
145
|
+
return f(model2, deferredFree);
|
|
146
|
+
} finally {
|
|
147
|
+
ptrs.forEach((p) => this.m._free(p));
|
|
148
|
+
this.m._tinystan_destroy_model(model2);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Sample using NUTS-HMC.
|
|
153
|
+
* @param {SamplerParams} p A (partially-specified) `SamplerParams` object.
|
|
154
|
+
* If a property is not specified, the default value will be used.
|
|
155
|
+
* @returns {StanDraws} A StanDraws object containing the parameter names and the draws
|
|
156
|
+
*/
|
|
157
|
+
sample(p) {
|
|
158
|
+
const { data, num_chains, inits, seed, id, init_radius, num_warmup, num_samples, metric, save_inv_metric, adapt, delta, gamma, kappa, t0, init_buffer, term_buffer, window, save_warmup, stepsize, stepsize_jitter, max_depth, refresh, num_threads } = {
|
|
159
|
+
...defaultSamplerParams,
|
|
160
|
+
...p
|
|
161
|
+
};
|
|
162
|
+
if (num_chains < 1) throw new Error("num_chains must be at least 1");
|
|
163
|
+
if (num_warmup < 0) throw new Error("num_warmup must be non-negative");
|
|
164
|
+
if (num_samples < 1) throw new Error("num_samples must be at least 1");
|
|
165
|
+
const seed_ = seed ?? Math.floor(Math.random() * Math.pow(2, 32));
|
|
166
|
+
return this.withModel(data, seed_, (model2, deferredFree) => {
|
|
167
|
+
const rawParamNames = this.m.UTF8ToString(this.m._tinystan_model_param_names(model2));
|
|
168
|
+
const paramNames = HMC_SAMPLER_VARIABLES.concat(rawParamNames.split(","));
|
|
169
|
+
const n_params = paramNames.length;
|
|
170
|
+
const free_params = this.m._tinystan_model_num_free_params(model2);
|
|
171
|
+
const init_inv_metric_ptr = NULL;
|
|
172
|
+
let inv_metric_out = NULL;
|
|
173
|
+
let stepsize_out = NULL;
|
|
174
|
+
if (adapt) {
|
|
175
|
+
stepsize_out = this.m._malloc(num_chains * Float64Array.BYTES_PER_ELEMENT);
|
|
176
|
+
if (save_inv_metric) if (metric === 1) inv_metric_out = this.m._malloc(num_chains * free_params * free_params * Float64Array.BYTES_PER_ELEMENT);
|
|
177
|
+
else inv_metric_out = this.m._malloc(num_chains * free_params * Float64Array.BYTES_PER_ELEMENT);
|
|
178
|
+
}
|
|
179
|
+
deferredFree(stepsize_out);
|
|
180
|
+
deferredFree(inv_metric_out);
|
|
181
|
+
const inits_ptr = this.encodeInits(inits);
|
|
182
|
+
deferredFree(inits_ptr);
|
|
183
|
+
const n_draws = num_chains * (save_warmup ? num_samples + num_warmup : num_samples);
|
|
184
|
+
const n_out = n_draws * n_params;
|
|
185
|
+
const out_ptr = this.m._malloc(n_out * Float64Array.BYTES_PER_ELEMENT);
|
|
186
|
+
deferredFree(out_ptr);
|
|
187
|
+
const err_ptr = this.m._malloc(PTR_SIZE);
|
|
188
|
+
const result = this.m._tinystan_sample(model2, num_chains, inits_ptr, seed_, id, init_radius, num_warmup, num_samples, metric.valueOf(), init_inv_metric_ptr, adapt ? 1 : 0, delta, gamma, kappa, t0, init_buffer, term_buffer, window, save_warmup ? 1 : 0, stepsize, stepsize_jitter, max_depth, refresh, num_threads, out_ptr, n_out, stepsize_out, inv_metric_out, err_ptr);
|
|
189
|
+
this.handleError(result, err_ptr);
|
|
190
|
+
const out_buffer = this.m.HEAPF64.subarray(out_ptr / Float64Array.BYTES_PER_ELEMENT, out_ptr / Float64Array.BYTES_PER_ELEMENT + n_out);
|
|
191
|
+
const draws = Array.from({ length: n_params }, (_, i) => Array.from({ length: n_draws }, (_2, j) => out_buffer[i + n_params * j]));
|
|
192
|
+
let stepsizes;
|
|
193
|
+
let inv_metric_array;
|
|
194
|
+
if (adapt) {
|
|
195
|
+
const stepsize_buffer = this.m.HEAPF64.subarray(stepsize_out / Float64Array.BYTES_PER_ELEMENT, stepsize_out / Float64Array.BYTES_PER_ELEMENT + num_chains);
|
|
196
|
+
stepsizes = Array.from({ length: num_chains }, (_, i) => stepsize_buffer[i]);
|
|
197
|
+
if (save_inv_metric) if (metric === 1) {
|
|
198
|
+
const inv_metric_buffer = this.m.HEAPF64.subarray(inv_metric_out / Float64Array.BYTES_PER_ELEMENT, inv_metric_out / Float64Array.BYTES_PER_ELEMENT + num_chains * free_params * free_params);
|
|
199
|
+
inv_metric_array = Array.from({ length: num_chains }, (_, i) => Array.from({ length: free_params }, (_2, j) => Array.from({ length: free_params }, (_3, k) => inv_metric_buffer[i * free_params * free_params + j * free_params + k])));
|
|
200
|
+
} else {
|
|
201
|
+
const inv_metric_buffer = this.m.HEAPF64.subarray(inv_metric_out / Float64Array.BYTES_PER_ELEMENT, inv_metric_out / Float64Array.BYTES_PER_ELEMENT + num_chains * free_params);
|
|
202
|
+
inv_metric_array = Array.from({ length: num_chains }, (_, i) => Array.from({ length: free_params }, (_2, j) => inv_metric_buffer[i * free_params + j]));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
paramNames,
|
|
207
|
+
draws,
|
|
208
|
+
inv_metric: inv_metric_array,
|
|
209
|
+
stepsize: stepsizes
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Approximate the posterior using Pathfinder.
|
|
215
|
+
* @param {PathfinderParams} p A (partially-specified) `PathfinderParams` object.
|
|
216
|
+
* If a property is not specified, the default value will be used.
|
|
217
|
+
* @returns {StanDraws} A StanDraws object containing the parameter names and the
|
|
218
|
+
* approximate draws
|
|
219
|
+
*/
|
|
220
|
+
pathfinder(p) {
|
|
221
|
+
const { data, num_paths, inits, seed, id, init_radius, num_draws, max_history_size, init_alpha, tol_obj, tol_rel_obj, tol_grad, tol_rel_grad, tol_param, num_iterations, num_elbo_draws, num_multi_draws, calculate_lp, psis_resample, refresh, num_threads } = {
|
|
222
|
+
...defaultPathfinderParams,
|
|
223
|
+
...p
|
|
224
|
+
};
|
|
225
|
+
if (num_paths < 1) throw new Error("num_paths must be at least 1");
|
|
226
|
+
if (num_draws < 1) throw new Error("num_draws must be at least 1");
|
|
227
|
+
if (num_multi_draws < 1) throw new Error("num_multi_draws must be at least 1");
|
|
228
|
+
const output_rows = calculate_lp && psis_resample ? num_multi_draws : num_draws * num_paths;
|
|
229
|
+
const seed_ = seed !== null ? seed : Math.floor(Math.random() * Math.pow(2, 32));
|
|
230
|
+
return this.withModel(data, seed_, (model2, deferredFree) => {
|
|
231
|
+
const rawParamNames = this.m.UTF8ToString(this.m._tinystan_model_param_names(model2));
|
|
232
|
+
const paramNames = PATHFINDER_VARIABLES.concat(rawParamNames.split(","));
|
|
233
|
+
const n_params = paramNames.length;
|
|
234
|
+
if (this.m._tinystan_model_num_free_params(model2) === 0) throw new Error("Model has no parameters.");
|
|
235
|
+
const inits_ptr = this.encodeInits(inits);
|
|
236
|
+
deferredFree(inits_ptr);
|
|
237
|
+
const n_out = output_rows * n_params;
|
|
238
|
+
const out = this.m._malloc(n_out * Float64Array.BYTES_PER_ELEMENT);
|
|
239
|
+
deferredFree(out);
|
|
240
|
+
const err_ptr = this.m._malloc(PTR_SIZE);
|
|
241
|
+
const result = this.m._tinystan_pathfinder(model2, num_paths, inits_ptr, seed_, id, init_radius, num_draws, max_history_size, init_alpha, tol_obj, tol_rel_obj, tol_grad, tol_rel_grad, tol_param, num_iterations, num_elbo_draws, num_multi_draws, calculate_lp ? 1 : 0, psis_resample ? 1 : 0, refresh, num_threads, out, n_out, err_ptr);
|
|
242
|
+
this.handleError(result, err_ptr);
|
|
243
|
+
const out_buffer = this.m.HEAPF64.subarray(out / Float64Array.BYTES_PER_ELEMENT, out / Float64Array.BYTES_PER_ELEMENT + n_out);
|
|
244
|
+
return {
|
|
245
|
+
paramNames,
|
|
246
|
+
draws: Array.from({ length: n_params }, (_, i) => Array.from({ length: output_rows }, (_2, j) => out_buffer[i + n_params * j]))
|
|
247
|
+
};
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Get the version of the Stan library being used.
|
|
252
|
+
* @returns {string} The version of the Stan library being used,
|
|
253
|
+
* in the form "major.minor.patch"
|
|
254
|
+
*/
|
|
255
|
+
stanVersion() {
|
|
256
|
+
const major = this.m._malloc(4);
|
|
257
|
+
const minor = this.m._malloc(4);
|
|
258
|
+
const patch = this.m._malloc(4);
|
|
259
|
+
this.m._tinystan_stan_version(major, minor, patch);
|
|
260
|
+
const version = this.m.getValue(major, "i32") + "." + this.m.getValue(minor, "i32") + "." + this.m.getValue(patch, "i32");
|
|
261
|
+
this.m._free(major);
|
|
262
|
+
this.m._free(minor);
|
|
263
|
+
this.m._free(patch);
|
|
264
|
+
return version;
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// src/worker.ts
|
|
269
|
+
var postReply = (message) => self.postMessage(message);
|
|
270
|
+
var model = null;
|
|
271
|
+
var consoleMessages = [];
|
|
272
|
+
var parseProgress = (msg) => {
|
|
273
|
+
let normalized = msg;
|
|
274
|
+
if (normalized.startsWith("Iteration:")) {
|
|
275
|
+
normalized = `Chain [1] ${normalized}`;
|
|
276
|
+
}
|
|
277
|
+
normalized = normalized.replace(/\[|\]/g, "");
|
|
278
|
+
const parts = normalized.split(/\s+/);
|
|
279
|
+
const chain = Number.parseInt(parts[1] ?? "0", 10);
|
|
280
|
+
const iteration = Number.parseInt(parts[3] ?? "0", 10);
|
|
281
|
+
const totalIterations = Number.parseInt(parts[5] ?? "0", 10);
|
|
282
|
+
const percentRaw = parts[6] ?? "0%";
|
|
283
|
+
const percent = Number.parseInt(percentRaw.slice(0, -1), 10);
|
|
284
|
+
const warmup = parts[7] === "(Warmup)";
|
|
285
|
+
return { chain, iteration, totalIterations, percent, warmup };
|
|
286
|
+
};
|
|
287
|
+
var progressPrintCallback = (msg) => {
|
|
288
|
+
if (!msg) return;
|
|
289
|
+
consoleMessages.push({ text: msg, level: "log" });
|
|
290
|
+
if (msg.includes("Iteration:")) {
|
|
291
|
+
postReply({ kind: "progress" /* Progress */, report: parseProgress(msg) });
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
var errorPrintCallback = (msg) => {
|
|
295
|
+
consoleMessages.push({ text: msg, level: "error" });
|
|
296
|
+
};
|
|
297
|
+
var normalizeInits = (raw) => {
|
|
298
|
+
if (raw === void 0 || raw === null) return void 0;
|
|
299
|
+
if (typeof raw === "string") return raw;
|
|
300
|
+
return raw;
|
|
301
|
+
};
|
|
302
|
+
self.onmessage = async (event) => {
|
|
303
|
+
const data = event.data;
|
|
304
|
+
if (!data || typeof data !== "object" || !("kind" in data)) return;
|
|
305
|
+
switch (data.kind) {
|
|
306
|
+
case "load" /* Load */: {
|
|
307
|
+
try {
|
|
308
|
+
const mod = await import(
|
|
309
|
+
/* @vite-ignore */
|
|
310
|
+
data.url
|
|
311
|
+
);
|
|
312
|
+
model = await src_default.load(mod.default, progressPrintCallback, errorPrintCallback);
|
|
313
|
+
const stanVersion = typeof model.stanVersion === "function" ? model.stanVersion() : "unknown";
|
|
314
|
+
postReply({ kind: "modelLoaded" /* ModelLoaded */, stanVersion });
|
|
315
|
+
} catch (err) {
|
|
316
|
+
postReply({
|
|
317
|
+
kind: "modelLoadError" /* ModelLoadError */,
|
|
318
|
+
error: err instanceof Error ? err.message : String(err)
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case "sample" /* Sample */: {
|
|
324
|
+
if (!model) {
|
|
325
|
+
postReply({
|
|
326
|
+
kind: "stanReturn" /* StanReturn */,
|
|
327
|
+
error: "Model not loaded \u2014 call compile() and await it before sample()"
|
|
328
|
+
});
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
consoleMessages = [];
|
|
332
|
+
try {
|
|
333
|
+
const cfg = data.sampleConfig;
|
|
334
|
+
const inits = normalizeInits(cfg.inits);
|
|
335
|
+
const tinystanArgs = { ...cfg };
|
|
336
|
+
if (inits === void 0) {
|
|
337
|
+
delete tinystanArgs.inits;
|
|
338
|
+
} else {
|
|
339
|
+
tinystanArgs.inits = inits;
|
|
340
|
+
}
|
|
341
|
+
const { paramNames, draws } = model.sample(tinystanArgs);
|
|
342
|
+
postReply({
|
|
343
|
+
kind: "stanReturn" /* StanReturn */,
|
|
344
|
+
error: null,
|
|
345
|
+
draws,
|
|
346
|
+
paramNames,
|
|
347
|
+
consoleMessages,
|
|
348
|
+
sampleConfig: cfg
|
|
349
|
+
});
|
|
350
|
+
} catch (err) {
|
|
351
|
+
postReply({
|
|
352
|
+
kind: "stanReturn" /* StanReturn */,
|
|
353
|
+
error: err instanceof Error ? err.message : String(err)
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../node_modules/.pnpm/tinystan@0.3.3/node_modules/tinystan/src/util.ts","../../../node_modules/.pnpm/tinystan@0.3.3/node_modules/tinystan/src/constants.ts","../../../node_modules/.pnpm/tinystan@0.3.3/node_modules/tinystan/src/model.ts","../../../node_modules/.pnpm/tinystan@0.3.3/node_modules/tinystan/src/index.ts","../src/worker.ts"],"sourcesContent":["import { PrintCallback, StanVariableInputs } from \"./types\";\n\n// Stan expects a JSON object (\"{ ... }\") at the top level.\n// If the input is already a string, e.g. it came from a textarea,\n// we just return it as is.\nexport const prepareStanJSON = (obj: string | StanVariableInputs): string => {\n if (typeof obj === \"string\") {\n return obj;\n } else {\n return JSON.stringify(obj);\n }\n};\n\n// A printCallback that \"soaks up\" all the printed text.\nexport const printCallbackSponge = (): {\n printCallback: PrintCallback;\n getStdout: () => string;\n clearStdout: () => void;\n} => {\n const stdoutHolder = { text: \"\" };\n const printCallback = (...args: unknown[]) => {\n const text = args.join(\" \");\n stdoutHolder.text = stdoutHolder.text + text + \"\\n\";\n };\n\n const getStdout = () => stdoutHolder.text;\n const clearStdout = () => (stdoutHolder.text = \"\");\n\n return { printCallback, getStdout, clearStdout };\n};\n","import { SamplerParams, PathfinderParams, HMCMetric } from \"./types\";\n\nexport const HMC_SAMPLER_VARIABLES = [\n \"lp__\",\n \"accept_stat__\",\n \"stepsize__\",\n \"treedepth__\",\n \"n_leapfrog__\",\n \"divergent__\",\n \"energy__\",\n];\nexport const PATHFINDER_VARIABLES = [\"lp_approx__\", \"lp__\", \"path__\"];\n\n// NULL can be any pointer type\nconst NULL = 0 as any;\nconst PTR_SIZE = 4;\n\nconst defaultSamplerParams: SamplerParams = {\n data: \"\",\n num_chains: 4,\n inits: \"\",\n seed: null,\n id: 1,\n init_radius: 2,\n num_warmup: 1000,\n num_samples: 1000,\n metric: HMCMetric.DIAGONAL,\n save_inv_metric: false,\n init_inv_metric: null, // currently unused\n adapt: true,\n delta: 0.8,\n gamma: 0.05,\n kappa: 0.75,\n t0: 10,\n init_buffer: 75,\n term_buffer: 50,\n window: 25,\n save_warmup: false,\n stepsize: 1,\n stepsize_jitter: 0,\n max_depth: 10,\n refresh: 100,\n num_threads: -1,\n};\nconst defaultPathfinderParams: PathfinderParams = {\n data: \"\",\n num_paths: 4,\n inits: \"\",\n seed: null,\n id: 1,\n init_radius: 2,\n num_draws: 1000,\n max_history_size: 5,\n init_alpha: 0.001,\n tol_obj: 1e-12,\n tol_rel_obj: 10000,\n tol_grad: 1e-8,\n tol_rel_grad: 10000000,\n tol_param: 1e-8,\n num_iterations: 1000,\n num_elbo_draws: 25,\n num_multi_draws: 1000,\n calculate_lp: true,\n psis_resample: true,\n refresh: 100,\n num_threads: -1,\n};\n\nexport const internalConstants = {\n NULL,\n PTR_SIZE,\n defaultSamplerParams,\n defaultPathfinderParams,\n};\n","import { prepareStanJSON } from \"./util\";\nimport {\n HMC_SAMPLER_VARIABLES,\n PATHFINDER_VARIABLES,\n internalConstants,\n} from \"./constants\";\nconst { NULL, PTR_SIZE, defaultSamplerParams, defaultPathfinderParams } =\n internalConstants;\n\nimport { HMCMetric } from \"./types\";\nimport type {\n SamplerParams,\n PathfinderParams,\n StanVariableInputs,\n PrintCallback,\n StanDraws,\n internalTypes,\n} from \"./types\";\ntype WasmModule = internalTypes[\"WasmModule\"];\ntype ptr = internalTypes[\"ptr\"];\ntype model_ptr = internalTypes[\"model_ptr\"];\ntype error_ptr = internalTypes[\"error_ptr\"];\ntype cstr = internalTypes[\"cstr\"];\n\n/**\n * StanModel is a class that wraps the WASM module and provides a\n * higher-level interface to the Stan library, abstracting away things\n * like memory management and error handling.\n */\nexport default class StanModel {\n private m: WasmModule;\n private printErrorCallback: PrintCallback | null;\n // used to send multiple JSON values in one string\n private sep: string;\n\n private constructor(m: WasmModule, pc: PrintCallback | null) {\n this.m = m;\n this.printErrorCallback = pc;\n this.sep = String.fromCharCode(m._tinystan_separator_char());\n }\n\n /**\n * Load a StanModel from a WASM module.\n *\n * @param {Function} createModule A function that resolves to a WASM module. This is\n * much like the one Emscripten creates for you with `-sMODULARIZE`.\n * @param {PrintCallback | null} printCallback A callback that will be called\n * with any print statements from Stan. If null, this will default to `console.log`.\n * @returns {Promise<StanModel>} A promise that resolves to a `StanModel`\n */\n public static async load(\n createModule: (moduleArg?: object) => Promise<object>,\n printCallback: PrintCallback | null = null,\n printErrorCallback: PrintCallback | null = null,\n ): Promise<StanModel> {\n // Create the initial object which will have the rest of the WASM\n // functions attached to it\n // See https://emscripten.org/docs/api_reference/module.html\n printErrorCallback = printErrorCallback ?? printCallback;\n const prototype = { print: printCallback, printErr: printErrorCallback };\n\n const module = await createModule(prototype);\n return new StanModel(module as WasmModule, printErrorCallback);\n }\n\n private encodeString(s: string): cstr {\n const len = this.m.lengthBytesUTF8(s) + 1;\n const ptr = this.m._malloc(len) as unknown as cstr;\n this.m.stringToUTF8(s, ptr, len);\n return ptr;\n }\n\n private handleError(rc: number | boolean, err_ptr: ptr): void {\n if (rc == 0) {\n this.m._free(err_ptr);\n return;\n }\n\n const err = this.m.getValue(err_ptr, \"*\") as error_ptr;\n const err_msg_ptr = this.m._tinystan_get_error_message(err);\n const err_msg = \"Exception from Stan:\\n\" + this.m.UTF8ToString(err_msg_ptr);\n this.m._tinystan_destroy_error(err);\n this.m._free(err_ptr);\n this.printErrorCallback?.(err_msg);\n throw new Error(err_msg);\n }\n\n private encodeInits(\n inits: string | StanVariableInputs | string[] | StanVariableInputs[],\n ): cstr {\n if (Array.isArray(inits)) {\n return this.encodeString(\n inits.map(i => prepareStanJSON(i)).join(this.sep),\n );\n } else {\n return this.encodeString(prepareStanJSON(inits));\n }\n }\n\n /** @ignore\n * withModel serves as something akin to a context manager in\n * Python. It accepts the arguments needed to construct a model\n * (data and seed) and a callback.\n *\n * The callback takes in the model and a deferredFree function.\n * The memory for the allocated model and any pointers which are \"registered\"\n * by calling deferredFree will be cleaned up when the callback completes,\n * regardless of if this is a normal return or an exception.\n *\n * The result of the callback is then returned or re-thrown.\n */\n private withModel<T>(\n data: string | StanVariableInputs,\n seed: number,\n f: (model: model_ptr, deferredFree: (p: ptr | cstr) => void) => T,\n ): T {\n const data_ptr = this.encodeString(prepareStanJSON(data));\n const err_ptr = this.m._malloc(PTR_SIZE);\n const model = this.m._tinystan_create_model(data_ptr, seed, NULL, err_ptr);\n this.m._free(data_ptr);\n\n this.handleError(model === 0, err_ptr);\n\n const ptrs: (ptr | cstr)[] = [];\n const deferredFree = (p: ptr | cstr) => ptrs.push(p);\n\n try {\n return f(model, deferredFree);\n } finally {\n ptrs.forEach(p => this.m._free(p));\n this.m._tinystan_destroy_model(model);\n }\n }\n\n /**\n * Sample using NUTS-HMC.\n * @param {SamplerParams} p A (partially-specified) `SamplerParams` object.\n * If a property is not specified, the default value will be used.\n * @returns {StanDraws} A StanDraws object containing the parameter names and the draws\n */\n public sample(p: Partial<SamplerParams>): StanDraws {\n const {\n data,\n num_chains,\n inits,\n seed,\n id,\n init_radius,\n num_warmup,\n num_samples,\n metric,\n save_inv_metric,\n adapt,\n delta,\n gamma,\n kappa,\n t0,\n init_buffer,\n term_buffer,\n window,\n save_warmup,\n stepsize,\n stepsize_jitter,\n max_depth,\n refresh,\n num_threads,\n } = { ...defaultSamplerParams, ...p };\n\n if (num_chains < 1) {\n throw new Error(\"num_chains must be at least 1\");\n }\n if (num_warmup < 0) {\n throw new Error(\"num_warmup must be non-negative\");\n }\n if (num_samples < 1) {\n throw new Error(\"num_samples must be at least 1\");\n }\n\n const seed_ = seed ?? Math.floor(Math.random() * Math.pow(2, 32));\n\n return this.withModel(data, seed_, (model, deferredFree) => {\n // Get the parameter names\n const rawParamNames = this.m.UTF8ToString(\n this.m._tinystan_model_param_names(model),\n );\n const paramNames = HMC_SAMPLER_VARIABLES.concat(rawParamNames.split(\",\"));\n\n const n_params = paramNames.length;\n\n const free_params = this.m._tinystan_model_num_free_params(model);\n\n // TODO: allow init_inv_metric to be specified\n const init_inv_metric_ptr = NULL;\n\n let inv_metric_out = NULL;\n let stepsize_out = NULL;\n\n if (adapt) {\n stepsize_out = this.m._malloc(\n num_chains * Float64Array.BYTES_PER_ELEMENT,\n );\n if (save_inv_metric) {\n if (metric === HMCMetric.DENSE)\n inv_metric_out = this.m._malloc(\n num_chains *\n free_params *\n free_params *\n Float64Array.BYTES_PER_ELEMENT,\n );\n else\n inv_metric_out = this.m._malloc(\n num_chains * free_params * Float64Array.BYTES_PER_ELEMENT,\n );\n }\n }\n deferredFree(stepsize_out);\n deferredFree(inv_metric_out);\n\n const inits_ptr = this.encodeInits(inits);\n deferredFree(inits_ptr);\n\n const n_draws =\n num_chains * (save_warmup ? num_samples + num_warmup : num_samples);\n const n_out = n_draws * n_params;\n\n // Allocate memory for the output\n const out_ptr = this.m._malloc(n_out * Float64Array.BYTES_PER_ELEMENT);\n deferredFree(out_ptr);\n\n const err_ptr = this.m._malloc(PTR_SIZE);\n\n // Sample from the model\n const result = this.m._tinystan_sample(\n model,\n num_chains,\n inits_ptr,\n seed_,\n id,\n init_radius,\n num_warmup,\n num_samples,\n metric.valueOf(),\n init_inv_metric_ptr,\n adapt ? 1 : 0,\n delta,\n gamma,\n kappa,\n t0,\n init_buffer,\n term_buffer,\n window,\n save_warmup ? 1 : 0,\n stepsize,\n stepsize_jitter,\n max_depth,\n refresh,\n num_threads,\n out_ptr,\n n_out,\n stepsize_out,\n inv_metric_out,\n err_ptr,\n );\n\n this.handleError(result, err_ptr);\n\n const out_buffer = this.m.HEAPF64.subarray(\n out_ptr / Float64Array.BYTES_PER_ELEMENT,\n out_ptr / Float64Array.BYTES_PER_ELEMENT + n_out,\n );\n\n // copy out parameters of interest\n const draws: number[][] = Array.from({ length: n_params }, (_, i) =>\n Array.from({ length: n_draws }, (_, j) => out_buffer[i + n_params * j]),\n );\n\n let stepsizes: number[] | undefined;\n let inv_metric_array: number[][] | number[][][] | undefined;\n if (adapt) {\n const stepsize_buffer = this.m.HEAPF64.subarray(\n stepsize_out / Float64Array.BYTES_PER_ELEMENT,\n stepsize_out / Float64Array.BYTES_PER_ELEMENT + num_chains,\n );\n stepsizes = Array.from(\n { length: num_chains },\n (_, i) => stepsize_buffer[i],\n );\n if (save_inv_metric) {\n if (metric === HMCMetric.DENSE) {\n const inv_metric_buffer = this.m.HEAPF64.subarray(\n inv_metric_out / Float64Array.BYTES_PER_ELEMENT,\n inv_metric_out / Float64Array.BYTES_PER_ELEMENT +\n num_chains * free_params * free_params,\n );\n\n inv_metric_array = Array.from({ length: num_chains }, (_, i) =>\n Array.from({ length: free_params }, (_, j) =>\n Array.from(\n { length: free_params },\n (_, k) =>\n inv_metric_buffer[\n i * free_params * free_params + j * free_params + k\n ],\n ),\n ),\n );\n } else {\n const inv_metric_buffer = this.m.HEAPF64.subarray(\n inv_metric_out / Float64Array.BYTES_PER_ELEMENT,\n inv_metric_out / Float64Array.BYTES_PER_ELEMENT +\n num_chains * free_params,\n );\n inv_metric_array = Array.from({ length: num_chains }, (_, i) =>\n Array.from(\n { length: free_params },\n (_, j) => inv_metric_buffer[i * free_params + j],\n ),\n );\n }\n }\n }\n\n return {\n paramNames,\n draws,\n inv_metric: inv_metric_array,\n stepsize: stepsizes,\n };\n });\n }\n\n /**\n * Approximate the posterior using Pathfinder.\n * @param {PathfinderParams} p A (partially-specified) `PathfinderParams` object.\n * If a property is not specified, the default value will be used.\n * @returns {StanDraws} A StanDraws object containing the parameter names and the\n * approximate draws\n */\n public pathfinder(p: Partial<PathfinderParams>): StanDraws {\n const {\n data,\n num_paths,\n inits,\n seed,\n id,\n init_radius,\n num_draws,\n max_history_size,\n init_alpha,\n tol_obj,\n tol_rel_obj,\n tol_grad,\n tol_rel_grad,\n tol_param,\n num_iterations,\n num_elbo_draws,\n num_multi_draws,\n calculate_lp,\n psis_resample,\n refresh,\n num_threads,\n } = { ...defaultPathfinderParams, ...p };\n\n if (num_paths < 1) {\n throw new Error(\"num_paths must be at least 1\");\n }\n if (num_draws < 1) {\n throw new Error(\"num_draws must be at least 1\");\n }\n if (num_multi_draws < 1) {\n throw new Error(\"num_multi_draws must be at least 1\");\n }\n\n const output_rows =\n calculate_lp && psis_resample ? num_multi_draws : num_draws * num_paths;\n\n const seed_ =\n seed !== null ? seed : Math.floor(Math.random() * Math.pow(2, 32));\n\n return this.withModel(data, seed_, (model, deferredFree) => {\n const rawParamNames = this.m.UTF8ToString(\n this.m._tinystan_model_param_names(model),\n );\n const paramNames = PATHFINDER_VARIABLES.concat(rawParamNames.split(\",\"));\n\n const n_params = paramNames.length;\n\n const free_params = this.m._tinystan_model_num_free_params(model);\n if (free_params === 0) {\n throw new Error(\"Model has no parameters.\");\n }\n\n const inits_ptr = this.encodeInits(inits);\n deferredFree(inits_ptr);\n\n const n_out = output_rows * n_params;\n const out = this.m._malloc(n_out * Float64Array.BYTES_PER_ELEMENT);\n deferredFree(out);\n const err_ptr = this.m._malloc(PTR_SIZE);\n\n const result = this.m._tinystan_pathfinder(\n model,\n num_paths,\n inits_ptr,\n seed_,\n id,\n init_radius,\n num_draws,\n max_history_size,\n init_alpha,\n tol_obj,\n tol_rel_obj,\n tol_grad,\n tol_rel_grad,\n tol_param,\n num_iterations,\n num_elbo_draws,\n num_multi_draws,\n calculate_lp ? 1 : 0,\n psis_resample ? 1 : 0,\n refresh,\n num_threads,\n out,\n n_out,\n err_ptr,\n );\n this.handleError(result, err_ptr);\n\n const out_buffer = this.m.HEAPF64.subarray(\n out / Float64Array.BYTES_PER_ELEMENT,\n out / Float64Array.BYTES_PER_ELEMENT + n_out,\n );\n\n const draws: number[][] = Array.from({ length: n_params }, (_, i) =>\n Array.from(\n { length: output_rows },\n (_, j) => out_buffer[i + n_params * j],\n ),\n );\n\n return { paramNames, draws };\n });\n }\n\n /**\n * Get the version of the Stan library being used.\n * @returns {string} The version of the Stan library being used,\n * in the form \"major.minor.patch\"\n */\n public stanVersion(): string {\n const major = this.m._malloc(4);\n const minor = this.m._malloc(4);\n const patch = this.m._malloc(4);\n this.m._tinystan_stan_version(major, minor, patch);\n const version =\n this.m.getValue(major, \"i32\") +\n \".\" +\n this.m.getValue(minor, \"i32\") +\n \".\" +\n this.m.getValue(patch, \"i32\");\n this.m._free(major);\n this.m._free(minor);\n this.m._free(patch);\n return version;\n }\n}\n","import type {\n StanDraws,\n SamplerParams,\n PathfinderParams,\n PrintCallback,\n StanVariableInputs,\n} from \"./types\";\nexport type {\n StanDraws,\n SamplerParams,\n PathfinderParams,\n PrintCallback,\n StanVariableInputs,\n};\n\nimport StanModel from \"./model\";\nexport default StanModel;\n","/// <reference lib=\"webworker\" />\n\nimport StanModel from \"tinystan\";\nimport {\n type ConsoleMessage,\n type Progress,\n WorkerReply,\n type WorkerReplyMessage,\n WorkerRequest,\n type WorkerRequestMessage,\n} from \"./types\";\n\ndeclare const self: DedicatedWorkerGlobalScope;\n\nconst postReply = (message: WorkerReplyMessage) => self.postMessage(message);\n\nlet model: StanModel | null = null;\nlet consoleMessages: ConsoleMessage[] = [];\n\nconst parseProgress = (msg: string): Progress => {\n let normalized = msg;\n if (normalized.startsWith(\"Iteration:\")) {\n normalized = `Chain [1] ${normalized}`;\n }\n normalized = normalized.replace(/\\[|\\]/g, \"\");\n const parts = normalized.split(/\\s+/);\n const chain = Number.parseInt(parts[1] ?? \"0\", 10);\n const iteration = Number.parseInt(parts[3] ?? \"0\", 10);\n const totalIterations = Number.parseInt(parts[5] ?? \"0\", 10);\n const percentRaw = parts[6] ?? \"0%\";\n const percent = Number.parseInt(percentRaw.slice(0, -1), 10);\n const warmup = parts[7] === \"(Warmup)\";\n return { chain, iteration, totalIterations, percent, warmup };\n};\n\nconst progressPrintCallback = (msg: string) => {\n if (!msg) return;\n consoleMessages.push({ text: msg, level: \"log\" });\n if (msg.includes(\"Iteration:\")) {\n postReply({ kind: WorkerReply.Progress, report: parseProgress(msg) });\n }\n};\n\nconst errorPrintCallback = (msg: string) => {\n consoleMessages.push({ text: msg, level: \"error\" });\n};\n\nconst normalizeInits = (\n raw: unknown,\n): string | Record<string, unknown> | Record<string, unknown>[] | undefined => {\n if (raw === undefined || raw === null) return undefined;\n if (typeof raw === \"string\") return raw;\n return raw as Record<string, unknown> | Record<string, unknown>[];\n};\n\nself.onmessage = async (event: MessageEvent<WorkerRequestMessage>) => {\n const data = event.data;\n if (!data || typeof data !== \"object\" || !(\"kind\" in data)) return;\n\n switch (data.kind) {\n case WorkerRequest.Load: {\n try {\n const mod = await import(/* @vite-ignore */ data.url);\n model = await StanModel.load(mod.default, progressPrintCallback, errorPrintCallback);\n const stanVersion =\n typeof model.stanVersion === \"function\" ? model.stanVersion() : \"unknown\";\n postReply({ kind: WorkerReply.ModelLoaded, stanVersion });\n } catch (err) {\n postReply({\n kind: WorkerReply.ModelLoadError,\n error: err instanceof Error ? err.message : String(err),\n });\n }\n break;\n }\n\n case WorkerRequest.Sample: {\n if (!model) {\n postReply({\n kind: WorkerReply.StanReturn,\n error: \"Model not loaded — call compile() and await it before sample()\",\n });\n return;\n }\n consoleMessages = [];\n try {\n const cfg = data.sampleConfig;\n const inits = normalizeInits(cfg.inits);\n const tinystanArgs: Record<string, unknown> = { ...cfg };\n if (inits === undefined) {\n delete tinystanArgs.inits;\n } else {\n tinystanArgs.inits = inits;\n }\n const { paramNames, draws } = model.sample(tinystanArgs);\n postReply({\n kind: WorkerReply.StanReturn,\n error: null,\n draws,\n paramNames,\n consoleMessages,\n sampleConfig: cfg,\n });\n } catch (err) {\n postReply({\n kind: WorkerReply.StanReturn,\n error: err instanceof Error ? err.message : String(err),\n });\n }\n break;\n }\n }\n};\n"],"mappings":";AAKA,IAAa,kBAAA,CAAmB,QAA6C;AAC3E,MAAI,OAAO,QAAQ,SACjB,QAAO;MAEP,QAAO,KAAK,UAAU,GAAG;AAE7B;;;ACTA,IAAa,wBAAwB;EACnC;EACA;EACA;EACA;EACA;EACA;EACA;AACF;AACA,IAAa,uBAAuB;EAAC;EAAe;EAAQ;AAAQ;ACLpE,IAAM,EAAE,MAAM,UAAU,sBAAsB,wBAAA,IAC5C;ED8DA,MAAA;EACA,UAAA;EACA,sBAAA;IArDA,MAAM;IACN,YAAY;IACZ,OAAO;IACP,MAAM;IACN,IAAI;IACJ,aAAa;IACb,YAAY;IACZ,aAAa;IACb,QAAA;IACA,iBAAiB;IACjB,iBAAiB;IACjB,OAAO;IACP,OAAO;IACP,OAAO;IACP,OAAO;IACP,IAAI;IACJ,aAAa;IACb,aAAa;IACb,QAAQ;IACR,aAAa;IACb,UAAU;IACV,iBAAiB;IACjB,WAAW;IACX,SAAS;IACT,aAAa;EA6Bb;EACA,yBAAA;IA3BA,MAAM;IACN,WAAW;IACX,OAAO;IACP,MAAM;IACN,IAAI;IACJ,aAAa;IACb,WAAW;IACX,kBAAkB;IAClB,YAAY;IACZ,SAAS;IACT,aAAa;IACb,UAAU;IACV,cAAc;IACd,WAAW;IACX,gBAAgB;IAChB,gBAAgB;IAChB,iBAAiB;IACjB,cAAc;IACd,eAAe;IACf,SAAS;IACT,aAAa;EAOb;ACjEA;ACSF,IAAA,cAAe,MDaM,UAAU;EAC7B;EACA;EAEA;EAEA,YAAoB,GAAe,IAA0B;AAC3D,SAAK,IAAI;AACT,SAAK,qBAAqB;AAC1B,SAAK,MAAM,OAAO,aAAa,EAAE,yBAAyB,CAAC;EAC7D;;;;;;;;;;EAWA,aAAoB,KAClB,cACA,gBAAsC,MACtC,qBAA2C,M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;AACF,cAAM,MAAM,MAAM;AAAA;AAAA,UAA0B,KAAK;AAAA;AACjD,gBAAQ,MAAM,YAAU,KAAK,IAAI,SAAS,uBAAuB,kBAAkB;AACnF,cAAM,cACJ,OAAO,MAAM,gBAAgB,aAAa,MAAM,YAAY,IAAI;AAClE,kBAAU,EAAE,uCAA+B,YAAY,CAAC;AAAA,MAC1D,SAAS,KAAK;AACZ,kBAAU;AAAA,UACR;AAAA,UACA,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAAA,QACxD,CAAC;AAAA,MACH;AACA;AAAA,IACF;AAAA,IAEA,4BAA2B;AACzB,UAAI,CAAC,OAAO;AACV,kBAAU;AAAA,UACR;AAAA,UACA,OAAO;AAAA,QACT,CAAC;AACD;AAAA,MACF;AACA,wBAAkB,CAAC;AACnB,UAAI;AACF,cAAM,MAAM,KAAK;AACjB,cAAM,QAAQ,eAAe,IAAI,KAAK;AACtC,cAAM,eAAwC,EAAE,GAAG,IAAI;AACvD,YAAI,UAAU,QAAW;AACvB,iBAAO,aAAa;AAAA,QACtB,OAAO;AACL,uBAAa,QAAQ;AAAA,QACvB;AACA,cAAM,EAAE,YAAY,MAAM,IAAI,MAAM,OAAO,YAAY;AACvD,kBAAU;AAAA,UACR;AAAA,UACA,OAAO;AAAA,UACP;AAAA,UACA;AAAA,UACA;AAAA,UACA,cAAc;AAAA,QAChB,CAAC;AAAA,MACH,SAAS,KAAK;AACZ,kBAAU;AAAA,UACR;AAAA,UACA,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAAA,QACxD,CAAC;AAAA,MACH;AACA;AAAA,IACF;AAAA,EACF;AACF;","names":["model","_"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mcmcjs/stan-wasm",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Run Stan models in the browser: a WebAssembly sampling runtime with a typed compile-server client and an optional React adapter.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mcmc",
|
|
7
|
+
"bayesian",
|
|
8
|
+
"inference",
|
|
9
|
+
"stan",
|
|
10
|
+
"wasm",
|
|
11
|
+
"webassembly",
|
|
12
|
+
"sampler"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Shravan Goswami",
|
|
17
|
+
"email": "contact@shravangoswami.com",
|
|
18
|
+
"url": "https://shravangoswami.com"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/mcmcjs/mcmcjs/tree/main/packages/stan-wasm#readme",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/mcmcjs/mcmcjs.git",
|
|
24
|
+
"directory": "packages/stan-wasm"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/mcmcjs/mcmcjs/issues"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"main": "./dist/index.cjs",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.js",
|
|
37
|
+
"require": "./dist/index.cjs"
|
|
38
|
+
},
|
|
39
|
+
"./react": {
|
|
40
|
+
"types": "./dist/react/index.d.ts",
|
|
41
|
+
"import": "./dist/react/index.js",
|
|
42
|
+
"require": "./dist/react/index.cjs"
|
|
43
|
+
},
|
|
44
|
+
"./worker": {
|
|
45
|
+
"import": "./dist/worker.js"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist"
|
|
50
|
+
],
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=22"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsup",
|
|
59
|
+
"typecheck": "tsc --noEmit"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"tinystan": "^0.3.2"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"react": ">=18 <20"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"react": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@types/react": "^18.3.0",
|
|
74
|
+
"react": "^18.3.0"
|
|
75
|
+
}
|
|
76
|
+
}
|