{"slug": "chronos-2-in-96-mb-of-webassembly", "title": "Chronos-2 in 96 MB of WebAssembly", "summary": "A developer compiled Amazon's full 120M-parameter Chronos-2 time-series forecasting model into a 119 KiB Rust/WebAssembly SIMD engine that runs resident inside a single Cloudflare Worker, using 95.9 MB of peak WASM memory versus 959.8 MB RSS for PyTorch FP32 on CPU — roughly 10× less RAM — with an 88.7 MB weight file against 478.0 MB. Across 224 public benchmarks and 9 deciles the engine stayed within ±0.12% of FP32 on SQL, WQL and MASE, ran 1.16× faster than single-core PyTorch on 512/64 forecasts (96.6 ms vs 112.0 ms) and 1.11× faster across 3,800 benchmark forecasts, and served 261 ms p50 latency at the edge. The engine avoids 4-bit quantization and calibration data by assigning precision by structural sensitivity: FP32 for I/O boundary heads and norms (3.1 MB), IEEE FP16 for large I/O MLPs (9.4 MB), Q8_G64 for the 17 most sensitive encoder projections (15.3 MB) and split-plane 6-bit Q6_G64 for the remaining 67 encoder projections (60.8 MB).", "body_md": "**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.**\n\nNo GPUs. No containers. No external inference APIs. No calibration data or retraining.\n\n|  | PyTorch FP32 (CPU) | This Engine (WASM SIMD) | Delta | \n|---|---|---|---|\n| **Weight file** | 478.0 MB | **88.7 MB** | **5.4× smaller** | \n| **Peak memory (`8,192 / 1,024`)** | 959.8 MB RSS | **95.9 MB WASM** (fits 128 MiB Worker) | **~10× less RAM** | \n| **Runtime binary** | Python + PyTorch + OpenBLAS | **119 KiB**`.wasm` (`37.5 KiB` gz) | Zero runtime deps | \n| **Single-core CPU (`512 / 64` p50)** | 112.0 ms | **96.6 ms** | **1.16× faster** | \n| **3,800 benchmark forecasts (`512` ctx)** | 403.9 s | **363.1 s** | **1.11× faster** (`1.42×` on Chronos-II) | \n| **Accuracy (224 tasks, 9 deciles)** | baseline | **±0.12%** SQL / WQL / MASE | Indistinguishable from FP32 | \n| **Live Cloudflare Workers edge** | Cannot run (OOM) | **261 ms** p50 (`512/64` ) ·**1.03 s** R2 cold | `0` warm R2 reads | \n\nSupports 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`).\n\nFive engineering choices make the full 120M model fit in 95.9 MB and outperform PyTorch CPU without losing accuracy:\n\n1. \n**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.\n2. \n**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`.)*\n3. \n**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:\n  - Tiny I/O boundary heads & all norms/biases stay in **FP32** (`3.1 MB` ).\n  - Large I/O MLPs use **IEEE FP16** (`9.4 MB` ), decoded branchlessly on the fly in SIMD with zero RAM expansion.\n  - 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` ).\n  - 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.\n4. Tiny I/O boundary heads & all norms/biases stay in \n5. \n**Two exact weight-space transforms (zero accuracy loss in FP32).**\n  - **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)` .\n  - **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.\n6. \n**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).\n\nRequires Linux x86-64 (or macOS/ARM64 for local builds), Node.js 22+, Python 3.12, and Rust via `rustup` with `wasm32-unknown-unknown`.\n\n```\n./scripts/bootstrap.sh   # downloads pinned amazon/chronos-2, packs 88.7 MB weights, builds 119 KiB WASM\nnpm test                 # runs Worker, ABI, and 8,192/1,024 memory tests\nnpm run serve            # starts local workerd on http://127.0.0.1:8787\n```\n\nRun a forecast:\n\n```\ncurl -sS http://127.0.0.1:8787/forecast \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"history\":[10,12,11,13,15,14,10,12,11,13,15,14,10,12,11,13,15],\"horizon\":7}'\n```\n\n- `GET /health` — liveness check (`{\"status\":\"ok\"}` ); does not load model weights (`1 ms` startup).\n- `POST /forecast` — accepts`{\"history\": [...], \"horizon\": N}` (`application/json` ,`≤ 256 KiB` ):\n  - `history` :**1–8,192** finite float32 values (`null` supported for missing observations; at least one observed value required).\n  - `horizon` : integer in**1–1,024** .\n  - 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).\n\nRequires a Cloudflare Workers Paid plan (`[limits] cpu_ms = 30000` for `8,192`-context requests) and R2 enabled on your account.\n\n```\n# 1. Create a private R2 bucket and upload the packed 88.7 MB weight file\nkey=$(node -p 'require(\"./worker/generated/artifact.json\").key')\ncf r2 buckets create-by-name chronos-2-artifacts\ncf r2 buckets objects upload \"$key\" \\\n  --bucket-name chronos-2-artifacts \\\n  --content-type application/octet-stream \\\n  --file \".cache/artifacts/$key\"\n\n# 2. Configure your route / access policy in wrangler.toml, then deploy\nnpx wrangler deploy\n```\n\nSee [docs/deployment.md](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/docs/deployment.md) for Wrangler R2 upload commands and production notes.\n\n```\n# 43-task + 8-synthetic fast validation suite in local workerd (~75 s)\nnpm run evaluate\n\n# Full 224-task public benchmarks (Chronos-II + fev-bench + GIFT-Eval)\n.cache/venv/bin/python campaigns/rfc5/run_official.py --context 512  --series-limit 16 --window-limit 1 --cpu 8 --output .cache/c2edge30/official-512\n.cache/venv/bin/python campaigns/rfc5/run_official.py --context 8192 --series-limit 1  --window-limit 1 --cpu 8 --output .cache/c2edge30/official-8192\n\n# Regenerate README SVG charts\npython3 scripts/generate-charts.py\n```\n\n- [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\n- [Cloudflare Workers Deployment Guide](https://github.com/pquerna/chronos-wasm-autoresearch/blob/main/docs/deployment.md) — R2 setup, Wrangler deployment, and live edge numbers\n- [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\n\nApache-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).", "url": "https://wpnews.pro/news/chronos-2-in-96-mb-of-webassembly", "canonical_source": "https://github.com/pquerna/chronos-wasm-autoresearch", "published_at": "2026-12-04 21:07:39+00:00", "updated_at": "2026-12-04 21:16:12.233552+00:00", "lang": "en", "topics": ["machine-learning", "ai-infrastructure", "ai-tools", "mlops"], "entities": ["Amazon", "Chronos-2", "Cloudflare Workers", "WebAssembly", "PyTorch", "Rust", "OpenBLAS", "chronos-2-small"], "also_reported_by": [], "takeaway": "Packing Chronos-2's weights by structural sensitivity — FP32 for norms and I/O heads, FP16 for large I/O MLPs, Q8_G64 for the 17 most sensitive encoder projections and 6-bit Q6_G64 for the other 67 — cut the weight file from 478.0 MB to 88.7 MB and peak memory from 959.8 MB to 95.9 MB while staying ", "recommendation": {"advice": "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.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/chronos-2-in-96-mb-of-webassembly", "markdown": "https://wpnews.pro/news/chronos-2-in-96-mb-of-webassembly.md", "text": "https://wpnews.pro/news/chronos-2-in-96-mb-of-webassembly.txt", "jsonld": "https://wpnews.pro/news/chronos-2-in-96-mb-of-webassembly.jsonld"}}