# Chronos-2 in 96 MB of WebAssembly

> Source: <https://github.com/pquerna/chronos-wasm-autoresearch>
> Published: 2026-12-04 21:07:39+00:00

**Recommendation:** Pack weights by structural sensitivity instead of uniform 4-bit quantization — keep norms and I/O heads in FP32 and use 6-bit split-plane for less sensitive projections — to cut Chronos-2 memory roughly 10× with ±0.12% accuracy loss.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

**Full 120M-parameter [Amazon Chronos-2](https://huggingface.co/amazon/chronos-2) time-series forecasting compiled into a 119 KiB Rust/WASM SIMD engine — ~10× less RAM than PyTorch, faster on single-core CPU for typical forecasts, within ±0.12% of FP32 across 224 public benchmarks, and running resident inside a single Cloudflare Worker.**

No GPUs. No containers. No external inference APIs. No calibration data or retraining.

|  | PyTorch FP32 (CPU) | This Engine (WASM SIMD) | Delta | 
|---|---|---|---|
| **Weight file** | 478.0 MB | **88.7 MB** | **5.4× smaller** | 
| **Peak memory (`8,192 / 1,024`)** | 959.8 MB RSS | **95.9 MB WASM** (fits 128 MiB Worker) | **~10× less RAM** | 
| **Runtime binary** | Python + PyTorch + OpenBLAS | **119 KiB**`.wasm` (`37.5 KiB` gz) | Zero runtime deps | 
| **Single-core CPU (`512 / 64` p50)** | 112.0 ms | **96.6 ms** | **1.16× faster** | 
| **3,800 benchmark forecasts (`512` ctx)** | 403.9 s | **363.1 s** | **1.11× faster** (`1.42×` on Chronos-II) | 
| **Accuracy (224 tasks, 9 deciles)** | baseline | **±0.12%** SQL / WQL / MASE | Indistinguishable from FP32 | 
| **Live Cloudflare Workers edge** | Cannot run (OOM) | **261 ms** p50 (`512/64` ) ·**1.03 s** R2 cold | `0` warm R2 reads | 

Supports the full univariate `amazon/chronos-2` contract: **1–8,192** context length (including `null` missing values), **1–1,024** forecast horizon, and all **9 native deciles** (`0.1` through `0.9`).

Five engineering choices make the full 120M model fit in 95.9 MB and outperform PyTorch CPU without losing accuracy:

1. 
**A bespoke 119 KiB WASM engine specialized to one model.** Instead of shipping a general-purpose ML runtime,[`engine/src/c2edge30.rs`](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/engine/src/c2edge30.rs) hardcodes every`amazon/chronos-2` dimension (`12 layers, D=768, FF=3072, patch=16, 9 deciles` ) and computes every tensor's byte offset at compile time (`const usize` ). There is no header parser, no dynamic shape dispatch, and no per-layer heap allocation. Activations run through a**32-token micro-batched scratch arena** with L2-tiled 4-query head-major attention, keeping total working RAM flat at**6.5 MB** even at`8,192` context /`1,024` horizon.
2. 
**Why WebAssembly beats PyTorch CPU on typical forecasts.** For`1–512` step histories, transformer inference is memory-bandwidth bound, not FLOP bound. Streaming`88.7 MB` of packed weights moves**5.4× fewer bytes** than FP32 (`478 MB` ), eliminates Python/PyTorch dispatch overhead, and drops 12 matrix multiplies via exact`O @ V` fusion. Custom 16-token WASM SIMD128 micro-kernels (`i32x4_dot_i16x8` ) run**1.11×–1.42× faster** than single-core PyTorch OpenBLAS FP32 across 3,800 public benchmark forecasts.*(At `8,192 / 1,024`, quadratic attention FLOPs dominate and PyTorch OpenBLAS is slightly faster: `1.38 s` vs `1.67 s`.)*
3. 
**Zero 4-bit quantization, zero calibration data.** Standard 4-bit quantization degrades Chronos-2 on spiky and high-dynamic-range series, while switching to Amazon's smaller 28M`chronos-2-small` drops accuracy by`+4%` to`+14%` . Instead,[`model/pack.py`](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/model/pack.py) assigns precision purely by structural sensitivity with zero calibration data:
  - Tiny I/O boundary heads & all norms/biases stay in **FP32** (`3.1 MB` ).
  - Large I/O MLPs use **IEEE FP16** (`9.4 MB` ), decoded branchlessly on the fly in SIMD with zero RAM expansion.
  - The 17 most sensitive encoder projections (all 12 `V` matrices,`L0/L1/L11` FFN`wi` , and`L11`` ov` /`wo` ) use**`Q8_G64`** (` 15.3 MB` ).
  - The remaining 67 encoder projections use **split-plane 6-bit (`Q6_G64`: `low4 + high2`)** (` 60.8 MB` ), fitted with 3-iteration closed-form alternating least-squares per 64 weights and unpacked with zero byte shuffles in WASM SIMD128.
4. Tiny I/O boundary heads & all norms/biases stay in 
5. 
**Two exact weight-space transforms (zero accuracy loss in FP32).**
  - **Diagonal-split exact `O @ V` fusion:** For univariate series, Chronos-2's group-attention softmax has a single active key, so attention weights are identically`1.0` . The exporter multiplies`M = O @ V` exactly in FP64 (eliminating 12 matrices), pulls out the 768-element diagonal into**FP32** (`36 KB` total — capturing ~25% of matrix energy), and quantizes only the smooth off-diagonal`M − diag(M)` .
  - **Free FFN neuron sorting:** Because`W_o · ReLU(W_i · x)` is invariant to permuting hidden neurons, sorting the 3,072 neurons by ascending`max(|W_o[:, j]|)` clusters outlier columns into shared 64-element quantization blocks — cutting`W_o` quantization error by up to**19%** at`0` bytes and`0` runtime cycles.
6. 
**Zero-copy BYOB streaming from Cloudflare R2.** Buffering an`88.7 MB` model in JS heap before copying to WASM blows past Cloudflare Workers' 128 MiB isolate ceiling. On cold start,[`worker/handler.mjs`](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/worker/handler.mjs) streams the single R2 object through a reused`128 KiB` BYOB buffer straight into`WebAssembly.Memory` while verifying`SHA-256` incrementally (`1.03 s` cold on live edge,`0` R2 reads on every warm request after).

Requires Linux x86-64 (or macOS/ARM64 for local builds), Node.js 22+, Python 3.12, and Rust via `rustup` with `wasm32-unknown-unknown`.

```
./scripts/bootstrap.sh   # downloads pinned amazon/chronos-2, packs 88.7 MB weights, builds 119 KiB WASM
npm test                 # runs Worker, ABI, and 8,192/1,024 memory tests
npm run serve            # starts local workerd on http://127.0.0.1:8787
```

Run a forecast:

```
curl -sS http://127.0.0.1:8787/forecast \
  -H 'Content-Type: application/json' \
  -d '{"history":[10,12,11,13,15,14,10,12,11,13,15,14,10,12,11,13,15],"horizon":7}'
```

- `GET /health` — liveness check (`{"status":"ok"}` ); does not load model weights (`1 ms` startup).
- `POST /forecast` — accepts`{"history": [...], "horizon": N}` (`application/json` ,`≤ 256 KiB` ):
  - `history` :**1–8,192** finite float32 values (`null` supported for missing observations; at least one observed value required).
  - `horizon` : integer in**1–1,024** .
  - Returns `{"quantiles": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], "values": [[...], ...]}` (raw native model outputs; no post-processing).

Requires a Cloudflare Workers Paid plan (`[limits] cpu_ms = 30000` for `8,192`-context requests) and R2 enabled on your account.

```
# 1. Create a private R2 bucket and upload the packed 88.7 MB weight file
key=$(node -p 'require("./worker/generated/artifact.json").key')
cf r2 buckets create-by-name chronos-2-artifacts
cf r2 buckets objects upload "$key" \
  --bucket-name chronos-2-artifacts \
  --content-type application/octet-stream \
  --file ".cache/artifacts/$key"

# 2. Configure your route / access policy in wrangler.toml, then deploy
npx wrangler deploy
```

See [docs/deployment.md](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/docs/deployment.md) for Wrangler R2 upload commands and production notes.

```
# 43-task + 8-synthetic fast validation suite in local workerd (~75 s)
npm run evaluate

# Full 224-task public benchmarks (Chronos-II + fev-bench + GIFT-Eval)
.cache/venv/bin/python campaigns/rfc5/run_official.py --context 512  --series-limit 16 --window-limit 1 --cpu 8 --output .cache/c2edge30/official-512
.cache/venv/bin/python campaigns/rfc5/run_official.py --context 8192 --series-limit 1  --window-limit 1 --cpu 8 --output .cache/c2edge30/official-8192

# Regenerate README SVG charts
python3 scripts/generate-charts.py
```

- [Model Artifact & Memory Layout](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/docs/model-artifact.md) — exact byte layout, mixed-precision split, and weight transforms
- [Cloudflare Workers Deployment Guide](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/docs/deployment.md) — R2 setup, Wrangler deployment, and live edge numbers
- [Full 224-Task Benchmark & Edge Verification Report](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/docs/results/2026-12-04-c2edge30-m2-qualification.md) — complete suite tables, paired CPU timings, and live soak results

Apache-2.0 — see [LICENSE](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/LICENSE). Model weights are not redistributed; `model/fetch.py` downloads the pinned Apache-2.0 `amazon/chronos-2` checkpoint directly from Hugging Face and verifies its SHA-256 digests against [model/lock.json](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/model/lock.json).
