@seantalts/stanli 0.26.0 → 0.27.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/README.md CHANGED
@@ -105,8 +105,9 @@ mode does no PSIS resampling.
105
105
  | Function | Does |
106
106
  | --- | --- |
107
107
  | `sample({code \| mir, data, seed, warmup, samples, delta, sampler, maxError, pathfinderInit, fastMath, onProgress, onLive})` | Compile (unless `mir` is given) and draw. Defaults: `seed` 1, `warmup` 1000, `samples` 1000, `delta` 0.8, `sampler` `"nuts"`. |
108
- | `compile({code, fastMath})` | Compile Stan source to portable MIR once, for reuse across `sample()` calls. |
108
+ | `compile({code, fastMath})` | Compile Stan source to portable MIR once, for reuse across `sample()` calls. Returns `{mir, warnings, o1Fallback}`; `o1Fallback` is null unless the model was too large for O1 and was compiled from unoptimized MIR. |
109
109
  | `diagnose(fit \| fits)` | Text diagnostic report for NUTS fits. |
110
+ | `diagnoseReport(fit \| fits)` | `{ text, failed, unavailable }`: the report and its verdict as counts. |
110
111
  | `preload({chains})` | Start loading the compiler and WASM runtime in the background. |
111
112
 
112
113
  `data` is an object or JSON text; `NaN` and infinities are passed through as
@@ -116,12 +117,11 @@ Pathfinder), for live plots.
116
117
  ## Platforms
117
118
 
118
119
  The compiler loads only when a call passes Stan source. It is about 3 MB raw
119
- and 425 KB gzipped. The package also carries the stock `stancjs.bc.js` of
120
- about the same size for one release cycle, loaded only if the preferred
121
- compiler is unavailable. `preload()` at page idle lets the first `sample()`
122
- skip the fetch and parse. An app with a fixed model can compile it at build
123
- time (`stanc --O1 --debug-optimized-mir model.stan`) and pass `mir`; then no
124
- compiler loads and the WASM runtime alone is about 1.5 MB gzipped.
120
+ and 425 KB gzipped. `preload()` at page idle lets the first `sample()`
121
+ skip the fetch and parse. An app with a fixed model can save the `mir` that
122
+ `compile()` returns (or that `stanli.stan_to_mir()` returns in Python) and
123
+ pass it to `sample()`; then no compiler loads and the WASM runtime alone is
124
+ about 1.5 MB gzipped.
125
125
 
126
126
  ## Limits
127
127
 
package/index.mjs CHANGED
@@ -10,10 +10,8 @@
10
10
  //
11
11
  // Workers pool up to the hardware's concurrency, so independent calls --
12
12
  // one chain each -- run simultaneously. Compiling once and passing `mir`
13
- // keeps the 3.0 MB preferred compiler in a single worker (or out of the page
14
- // entirely, if the model was precompiled at build time with
15
- // stanc --O1 --debug-optimized-mir). The package carries stock stancjs for one
16
- // rollback cycle, but the worker loads it only if the portable compiler fails.
13
+ // keeps the 3.0 MB compiler in a single worker (or out of the page entirely,
14
+ // if the model's MIR was saved from an earlier compile()).
17
15
 
18
16
  const pool = [];
19
17
  const waiters = [];
@@ -102,9 +100,11 @@ function fastMathOption(value) {
102
100
  * @param {boolean} [opts.fastMath=false] Compile in fast mode (fused
103
101
  * multiply-add rewriting; numerics may differ from CmdStan in the last
104
102
  * bits). Pass the same value to sample() together with the returned MIR.
105
- * Needs the portable stanli compiler.
106
- * @returns {Promise<{mir: string, fastMath: boolean,
107
- * ms: {stanc: number}}>} */
103
+ * @returns {Promise<{mir: string, fastMath: boolean, warnings: string[],
104
+ * o1Fallback: string|null, ms: {stanc: number}}>}
105
+ * `warnings` are the compiler's warnings. `o1Fallback` is null, or the
106
+ * notice that the model exceeded the compiler's size budget for O1 and
107
+ * was compiled from unoptimized MIR (correct, possibly slower). */
108
108
  export function compile(opts) {
109
109
  return request({ cmd: "compile", code: opts.code,
110
110
  fastMath: fastMathOption(opts.fastMath) }, opts);
@@ -138,9 +138,9 @@ function pathfinderInitOptions(value) {
138
138
  * @param {Object} opts
139
139
  * @param {string} [opts.code] Stan source (compiled in the worker by
140
140
  * stanc3, which loads lazily on first use).
141
- * @param {string} [opts.mir] Precompiled MIR (from `compile()` here, or
142
- * `stanc --O1 --debug-optimized-mir` at build time). When given, neither
143
- * browser compiler loads: the runtime alone is ~1.5 MB gzipped.
141
+ * @param {string} [opts.mir] Precompiled MIR (from `compile()`, or
142
+ * from `stan_to_mir()` in the Python package). When given, the compiler
143
+ * does not load: the runtime alone is ~1.5 MB gzipped.
144
144
  * @param {Object|string} [opts.data] Data as an object or JSON text.
145
145
  * @param {number} [opts.seed=1] Run seed: transformed-data RNG at
146
146
  * model construction (as CmdStan seeds it), then sampler and GQ RNG.
@@ -174,6 +174,7 @@ function pathfinderInitOptions(value) {
174
174
  * @returns {Promise<{names: string[], samples: number, generatedStart: number,
175
175
  * columns: Object<string, Float64Array>,
176
176
  * exactLp: boolean, fastMath: boolean,
177
+ * o1Fallback: string|null,
177
178
  * sampler: string, maxDepth: number|null,
178
179
  * samplerStats: Float64Array|null,
179
180
  * pathfinder?: {path: {iter, lp}[], khat: number,
@@ -187,6 +188,7 @@ function pathfinderInitOptions(value) {
187
188
  * NUTS also returns post-warmup samplerStats in draw-major order:
188
189
  * lp__, accept_stat__, stepsize__, treedepth__, n_leapfrog__,
189
190
  * divergent__, energy__. Other methods return null for samplerStats.
191
+ * `o1Fallback` is as in compile(), and null when `mir` was given.
190
192
  */
191
193
  // JSON.stringify writes null for a non-finite number, which Stan's data
192
194
  // reader rejects. It takes these quoted tokens.
@@ -240,6 +242,13 @@ export function sample(opts) {
240
242
  * WALNUTS and Pathfinder do not expose the required sampler statistics.
241
243
  * @returns {Promise<string>} */
242
244
  export async function diagnose(fits) {
245
+ return (await diagnoseReport(fits)).text;
246
+ }
247
+
248
+ /** diagnose(), with the report's verdict as numbers: `failed` checks and
249
+ * `unavailable` ones, which could not be computed.
250
+ * @returns {Promise<{text: string, failed: number, unavailable: number}>} */
251
+ export async function diagnoseReport(fits) {
243
252
  const chains = Array.isArray(fits) ? fits : [fits];
244
253
  const first = chains[0];
245
254
  if (!first || !Number.isInteger(first.samples) || first.samples < 1 ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seantalts/stanli",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "description": "Full Stan in the browser: stanc3 compiles the model in JS, a WASM runtime lowers it to an op graph and runs NUTS. No server, no C++ toolchain.",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
@@ -13,7 +13,6 @@
13
13
  "stanli.js",
14
14
  "stanli.wasm",
15
15
  "stanli-compiler.js",
16
- "stancjs.bc.js",
17
16
  "README.md",
18
17
  "LICENSE"
19
18
  ],