ZML, investigated.
Decoding 4.8× faster.

An inference stack in Zig.
Eight pull requests, with evidence.

THE PROJECT / ZML

One codebase.
Many accelerators.

ZML is a production inference stack built with Zig, MLIR and Bazel. Models compile through OpenXLA/PJRT to CPUs, NVIDIA, AMD and other accelerators. The work covered portability, resources, backend selection, metric exports and CPU decoding speed.

Upstream
zml/zml
Zig · Apache-2.0
Base revision
b71982fda7ef733e98362520b7cfe62e33eee985
All eight branches start from this revision and are independent of each other.
Review status
8 PRs open for review on moha-bekh/zml
Not yet proposed to zml/zml.

THE MEASURED RESULT

Same tokens.
A fifth of the time.

Qwen3.5-0.8B in BF16 on CPU, batch 1, greedy decoding. 48 processes in rotated order; medians of process medians, warmup excluded. Every process produced exactly the tokens of master, all 128 tokens of the long prompt included.

DECODE / 128 TOKENS

×4.8

0.432 → 0.090 s per token

From 2.3 to 11.2 tokens per second. A full 128-token turn goes from 55.6 to 12.0 s. PR #7 contributes ×1.2; PR #8 multiplies that by about 4.

Method and series in PR #8

PEAK MEMORY

−20%

3.42 → 2.75 GiB resident

The fallback path converted every BF16 weight to FP32 on every call: about 1 GB of fresh memory per token for the 248k-row output layer alone.

Per-layer state in PR #7

Ryzen 9 3900X (AVX2), shared host with a common lock, frequency not fixed. One model, two prompts, 32- and 128-token caps.

THE TWO CAUSES

Profile the decode.
Remove the wasted work.

Both optimizations start from a CPU profile. Neither changes the model; each removes work the runtime was doing for nothing.

01 / PR #7 · PER-LAYER STATE

Stop copying every layer’s state.

Each linear-attention layer received the recurrent state of all 18 layers and copied it twice (18 MiB each time) to update one. Each layer now owns its buffers. In the profile, memcpy falls from 15.7 s to 0.3 s of CPU per run; decode is ×1.19–1.20 faster in every scenario.

02 / PR #8 · TWO-ROW DOT

Give the fast library a matrix.

XLA refuses vector dots to YNNPACK, so each batch-1 projection fell back to code that converts all BF16 weights to FP32. Duplicating the input row makes it a matrix dot that YNNPACK accepts. Microbenchmark on the output layer: 190 ms → 12 ms. Kernel page zeroing drops from ~30% to ~1% of CPU.

THE PULL REQUESTS

Two optimizations.
Fixes, tests and a guide.

Each PR contains its change, its tests and the commands that were run. They are open for review on the fork before any proposal to ZML.

ContributionWhat changedPRStatus
Two-row BF16 dot on CPURoute single-row BF16 Linear layers through YNNPACK. 30 test cases against the original dot and an f64 oracle; 8- and 64-row inputs unchanged bit for bit.#8 In review
Qwen per-layer linear stateOne state buffer per linear-attention layer. All layer states compared over prefill and 32 decode steps on CPU (1, 2, 4 partitions) and on an RTX 2080 Ti.#7 In review
zml-smi CSV escapingA field containing a comma or quotes broke the CSV export. Fields are escaped; tests fail before and pass after; output re-read by Python’s CSV reader.#1 In review
zml-smi PCIe unitsPrometheus applied a factor of 128 to NVML values reported in KB/s. Converted to bytes/s with the decimal convention of the existing TUI; checked through the real collector.#2 In review
Qwen KV replicationRebases the existing upstream fix zml/zml#602, keeping its author, and adds eight CPU regression graphs for dense and MoE models.#3 In review
CPU runtime documentationELF dependencies of the CPU bundle and a NixOS recipe, checked in Debian 13, Debian 11 and NixOS VMs.#4 In review
Backend selection testsTen scenarios cover backend selection and plugin loading errors; with GPUs hidden, auto-selection continues on CPU.#5 In review
Portable attention on SM75Forced and automatic portable attention paths checked against an f64 reference on a real RTX 2080 Ti, at causal positions 0, 31 and 63.#6 In review

Status snapshot: 2026-12-05 UTC. “In review” means open on the fork; nothing has been merged into ZML.

THE TOOLS / WHAT THEY ACTUALLY DID

Find the cause.
Check it elsewhere.

The tools supplied evidence; reading the code turned it into patches.

01 / OMNISCIENT

Differential CPU profiles.

Omn profiles with kernel symbols showed the state copies and the page zeroing; per-call timing of CopyThunk confirmed the copies. Two pieces of feedback went back to Omn; the first is already fixed. The tool did not change the code.

02 / DISTROPROOF

Controlled VMs.

Debian 13, Debian 11 and NixOS VMs checked ELF loading, dlopen and access to the PJRT API for the CPU documentation. This is not inference and not an official support list.

VALIDATION & LIMITS

The evidence has a scope.
So do these results.

The integrated branch with all eight PRs passed the CPU tests, the SM75 attention test and a Qwen smoke run. Its speed matches PR #7 plus #8 within the measured spread: the other branches add no measured speedup.

Measurement scope
One model (0.8B), one AVX2 CPU, batch 1, two prompts, a shared host. Other model sizes, AVX-512, ARM and multi-GPU remain unmeasured. Prefill is barely affected.
Numerics
The two-row dot changes the FP32 summation order, so logits are not bit-identical. The drift is of the same order as XLA’s own SSE4.2 setting, which changed one greedy output where this change altered none.
A workaround
The root cause is in XLA, and openxla/xla#50736 (by the XLA team, since merged) fixes it there. Once ZML moves to an XLA that includes it, the two-row branch should be removed.

REVIEW & REPRODUCTION

Read the pull requests.
Rerun the commands.

Each PR lists the commands that were run, the measured series and its limits. The benchmark scripts and raw data are published with PR #7.

Independent work by moha-bekh, assisted by AI for analysis and scripts. No endorsement by the ZML maintainers is implied.