Skip to content

CPU: Qwen3.5-2B served on the host's cores (zig build native-cpu), every operation in the kernel contract, judged against a float64 reference - #2551

Open
Whq02 wants to merge 17 commits into
ashhart:mainfrom
Whq02:mercury/cpu-engine-2
Open

Whq02 wants to merge 17 commits into
ashhart:mainfrom
Whq02:mercury/cpu-engine-2

Conversation

@Whq02

@Whq02 Whq02 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: the tree registers a CPU affine projection and RMS norm in the kernel contract (#2478) and no engine calls them;
Qwen3.5-2B serves on Metal, CUDA and HIP. After: zig build native-cpu builds tensorfold-native with a CPU engine
(zig-out/native-cpu/bin, "backends": ["cpu"]) that serves mlx-community/Qwen3.5-2B-MLX-4bit on the host's cores,
x86_64 or arm64, with no GPU and no kernel library, through the same lane core, prompt cache and sampler the GPU
engines use (zig/src/native/cpu.zig, qwen35_cpu.zig, cpu_admit.zig; the family under
zig/src/families/qwen3_5/cpu_*.zig). It builds for any OS; it ran here on an arm64 Mac and, for its bytes, on x86_64
Linux under emulation (Windows is the third pull request). Default builds are unchanged: the step is opt-in, and the
edits to existing files are the build hook (one line), prefill.zig taking the backend's saved-state module as a
parameter (the Metal backend and its cache test pass their own, so the Metal engine's prompt reuse runs unchanged over
the CPU's), --backend accepting cpu where the binary serves it (cli.zig, as the HIP build's rocm), the
contract's listing and the two docs; nothing on the Metal, CUDA, HIP, XPU or Vulkan paths changes behaviour.

The arithmetic is the portable shape the issue agreed to judge against a float64 reference, not against Metal's bits:
the registered projection and RMS norm as #2478 left them (the residual add is its own row before the norm); fp32 sums
with fixed orders in the attention (chunks of 128 keys, four interleaved splits, merged by the running max) and in the
delta rule's recurrence (one fp32 state a head, 32 lanes of four fused terms then a tree); and every other row (the
embedding, residual add, MLP activation, attention gate, rotary, the q/k head norm, the delta rule's inputs, decay and
gated output norm) computed in float64 and rounded to bf16 through fp32. No measured table, no denormal flush (the float64
reference flushes none), no Metal probe. The same bytes come back at 1 to 8 threads (the engine's exactness test runs a prompt and a decode step on each
count; the fidelity tool at 1, 2, 4 and 8), at 1, 16 and 32 rows a pass (a decode step, a verify window, a prompt
batch); on x86_64 (-mcpu=x86_64_v3 and baseline, under emulation) the list case's 155 rows were the same bytes as
on arm64 — the float64 functions come from each host's libm, so that equality is observed, not by construction. Every operation the forward runs is registered in the contract beside the projection and norm: 15 kernels
through gates 1 to 4 with 36 planted controls (zig build cpu-contract; test-kernel-contract runs them as a test),
host-only, no gate 5.

Fidelity, frozen before the run: tools/zig/cpu_fidelity.py freeze wrote the prompt set and its sha256 first; the
Vulkan precedent's float64 oracle (qwen35_cpu_long_ref.py --run-input, #1544's comment, full float64 over the exactly
dequantized weights) computed the reference logits (tools/zig/qwen35_cpu_long_ref.py on this branch is that forward,
written for this PR after joshuaswarren's script and bit-identical to it on a 15-row and a 301-row case and on the three
cases' reference digests, so the comparison can be rerun); then tf-qwen35-cpu-forward (zig build qwen35-cpu-forward)
computed the engine's teacher-forced bf16 logits on the same histories and cpu_fidelity.py compare listed every
top-token difference with both sides' top two and margins. 591 rows over five cases (three raw continuations and two
recorded greedy chat replies, one behind a 2138-token prompt): 581 top tokens agree, 10 differ, every one the same two
contenders on both sides with both margins within one bf16 spacing of the top score (the tree's one-step tie policy,
tools/zig/qwen27_margins.py), 0 differences beyond it; the list with margins is below. The drafted, concurrent and
resumed checks run against its serve as against any other (zig/tests/qwen35/serve_check.py: 15 of 15 ok, with a
second serve at --prompt-cache-gib 0 for the fresh side; and a resumed turn that restored 3,328 cached tokens gave the
fresh serve's 29 token ids). This is the second of the three pull requests agreed on #2448; Windows remains.

Checks on this head

  • Head commit: 968d0de. The runs in this section were made on 938c103 (the
    reference script's on e28ebec). Since then the engine's arithmetic has not moved: cpu_ops.zig changed in comments
    and one test's name, and the trace switch is read once at open (cpu_backend.zig, qwen35_cpu.zig); the rest is the
    settings' scope, a CLI message, docs, one test and CI. The maintainers' checks on this head are in their comment of
    12 Nov 16:56Z, and this head's CI is the pinned digest's first run on Linux and Windows.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: python3 tools/pr_check.py --base origin/main on this head: merge base 3ace0f6, 31 changed files, lean 116 on the base and 116 on this head, 0 new;
    0 errors, 1 advisory warning (no --pr until this number exists). Every Zig comment is a //! or /// line, one
    line each; the longest new file is 353 lines.
  • Tests run, with the command and pass, fail and skip counts (Zig 0.17.0, an arm64 Mac with 15 logical CPUs, the
    checkpoint at revision 93760be4f1f69842a46bc13dbdc0f19e291392a3):
    • zig build cpu-contract --summary all: SUMMARY 15 kernels: 15 pass gates 1-4, 0 fail, 0 known findings; 36 controls: 36 red as planted, 0 missed; 0 limit problems (no launch to check); gates 102406 ms, exit 0 (the gates'
      time is the float64 references and gate 4's replay, the projection's head shapes the largest part); the 15
      kernels' gate lines are below.
    • zig build test-cpu-contract-host --summary all: 5/5 steps; the contract's 10 tests passed (the gates as a test,
      the rig's guards, the seven fixtures' gate bindings and served shapes), 0 failed, 0 skipped, and the kernels' 3.
    • zig build test-cpu --summary all (the engine's own tests, under test): 5/5 steps; 43/43 tests passed (37 in
      the family, 6 in the engine), 0 failed, 0 skipped: the rows, mixers and model against float64, the pool, a
      snapshot's round trip, the synthetic-weight exactness checks (16-row windows over two streams, kept prefixes and
      chunk cuts of 1, 7 and 128 equal to one-row steps bit for bit; the same prompt and decode step at 1 to 8 threads,
      the same committed states and logits), the admission and refusals.
    • zig build test-cpu-kernels --summary all: 3/3 steps, the pass reused from the byte-identical binary (CPU: Qwen3.5-2B's affine projection and RMS norm on the host's cores, registered in the kernel contract (gates 1 to 4, host-only) #2478's
      tests, unchanged).
    • zig build test-kernel-contract --summary all, from an empty local cache: Build Summary: 30/30 steps succeeded; 233/233 tests passed (the existing host contract tests, plus test-cpu-contract-host).
    • zig build test-golden --summary all: 5/5 steps; 322/335 and 55/55 equal to the server goldens, 0 unexpected
      differences (the 13 are its recorded known differences).
    • zig build test --summary all, whole, on this Mac (on 0a0d602eb, this head less two doc lines and the thread test, which
      test-cpu ran on this head): 648/763 steps succeeded (62 failed); 9933/10003 tests passed (70 skipped), no test failed, test-cpu success; the 62
      failed steps are all xcrun -sdk macosx metal shader compiles of the Metal kernels (xcrun: error: unable to find utility "metal": this Mac's developer tools carry no Metal compiler), none a test and none a file this change
      touches; CI runs test on ubuntu-latest and windows-latest (no-gpu.yml).
    • zig fmt --check build.zig zig and git diff --check origin/main..HEAD: clean.
    • Fidelity (fidelity/ in the receipt): the prompt set frozen with sha256 at 23:03Z and the two recorded cases at
      23:07Z, the reference computed next (8 to 112 s a case), the engine's logits after; cpu_fidelity.py compare per
      case: prose 113/118 top tokens agree (5 near-ties), code 128/128, list 87/89 (2), record-chat 126/128 (2),
      record-doc 127/128 (1); 0 other differences; the worst logit anywhere is 0.4681 from float64 (record-doc row 115,
      vocabulary entry 65757, 9.5 against 9.032: about seven and a half (7.49) bf16 spacings of 0.0625 at that magnitude,
      on a token far from the top).
    • Equality: the list case's 155 rows of bf16 logits byte-identical at 1, 2, 4 and 8 threads (the engine's test
      above covers every count from 1 to 8) and at 1, 16 and 32 rows a pass (one sha256, 9ac12bf94df5…); the tool cross-compiled for x86_64-linux-musl at -mcpu=x86_64_v3
      and -mcpu=baseline and run in an x86_64 Linux container under emulation on the same Mac gives the same bytes.
    • Lifecycle, against zig build native-cpu's serve (--parallel 4 --prompt-cache-gib 1 --no-thinking,
      TF_QWEN35_CPU_WINDOWS=1) with a second instance at --prompt-cache-gib 0: python3 zig/tests/qwen35/serve_check.py URL qwen3.5-2b 64 FRESH_URL: 15 ok, 0 failed (drafted == serial, solo ==
      together, serial together == serial solo, the same twice, decoders unchanged by a long prompt, the long prompt
      among decoders == alone, at greedy and t0.8; resumed == fresh); the resumed check whose first turn passes the
      128-token prompt grid (3,413 tokens; the second turn restored 3,328) gave the fresh serve's 29 token ids.
    • Red before green: with rsqrt planted as fp32 1 / sqrt(x) in cpu_kernels.zig, the comparison lists rows the
      packet's list does not, two of them beyond the tie policy (list row 65, reference margin 0.0772 at a top of 14.78;
      record-doc row 37, 0.1406 at 23.19), and every case's bytes change; each of the 36 controls is red in the
      SUMMARY run; the engine's exactness test fails if a window and its one-row replay differ (the synthetic weights
      make a changed reduction visible in the committed state's bytes).
  • CI: not run yet; read after opening. The zig job gains the engine's tests under test (about 20 s of compile and
    3 s of run on this Mac) and the contract's 11 more registrations under test-kernel-contract (the gates run about
    two minutes here, the projection's head shapes the largest part, as in CPU: Qwen3.5-2B's affine projection and RMS norm on the host's cores, registered in the kernel contract (gates 1 to 4, host-only) #2478).
  • Not run, and why: the gates and the engine's tests on an x86_64 processor itself (CI's ubuntu-latest runs them
    once this is open; the engine's bytes ran on x86_64 under emulation only); Windows (the third pull request; nothing
    here is OS-specific beyond cpu_admit.zig's memory readings, one a platform behind its os.tag); the Metal
    shader compiles inside zig build test (no Metal compiler on this Mac); Metal, CUDA, HIP, XPU and Vulkan: no GPU
    kernel or engine changes.

Receipt

  • Main against branch: cells and numbers, or why they can't be run: n/a: main has no CPU engine, so there is no before;
    the engine's own numbers are below.
  • Boxes it reaches: any host that builds native-cpu; measured on one arm64 Mac (15 logical CPUs, 48 GiB) and, for the
    bytes only, an x86_64 Linux container under emulation on it.
  • New switch? Default, accepted values, profile row: TF_QWEN35_CPU_THREADS=N, worker threads beside the engine
    thread (default: the logical CPUs less one); TF_QWEN35_CPU_WINDOWS=1, verify windows of up to 16 rows (copy drafts)
    after the engine's local exactness check against one-row steps passes at load (default off: one-row rounds). Both
    change speed only: the bytes are the same at 1 to 8 threads and at every window width (above); TF_QWEN35_CPU_TRACE=1,
    each one-token draw's top two logits and their margin on stderr (default off). No profile row: the CPU
    engine registers no profile switch.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: base
    7269f56, head 938c103; Zig 0.17.0, no backend toolchain (no device); an arm64 Mac, 15 logical CPUs, 48 GiB, no
    GPU used; mlx-community/Qwen3.5-2B-MLX-4bit at 93760be4f1f69842a46bc13dbdc0f19e291392a3.
  • Paths and platforms the change reaches, which I ran and which I could not: the CPU engine's prompt chunks (128
    rows), decode steps and 16-row verify windows, ran on arm64; its bytes on x86_64 under emulation; the Metal, CUDA,
    HIP, XPU and Vulkan paths are untouched; Windows not run here.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    python3 tools/bench_concurrent.py http://127.0.0.1:18612 qwen3.5-2b --tokens 128 --reps 2 --temperatures 0 --levels 1,4 --alone --serial: every cell failed 0; alone equal 2/2 at one stream and 8/8 at four for both prompts
    (each concurrent reply token-equal to its solo run), serial equal 1/1 in every cell (the drafted solo run equal to
    "draft": false); 29.9 and 30.9 tokens/s at one stream, 8.4 and 8.5 a stream (33.0 and 32.8 aggregate) at four, on
    the 6-thread serve (bench/concurrent.json). zig/tests/qwen35/serve_check.py as above: 15 of 15.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you
    can't): n/a, no engine on main; the fidelity rows above are the independent reference.
  • Decode speed before and after, from tools/bench_openai.py: n/a before (no engine on main); after, python3 tools/bench_openai.py http://127.0.0.1:18612 qwen3.5-2b --tokens 128 --reps 2 --temperatures 1.0,0 --label cpu on the 6-thread serve: 31.8 and 32.0 tokens/s at temperature 1.0, 35.0 and
    33.7 at 0, on the two public prompts; ttft 0.20 to 0.35 s (bench/decode.json).
  • Prompt speed before and after: the cold first request's ttft_s from tools/bench_cache.py --require-cold on a long
    prompt, or "n/a" with the reason when the change cannot reach prompt processing: n/a before; after, python3 tools/bench_cache.py http://127.0.0.1:18613 qwen3.5-2b --tokens 32 --repeats 2 --streams 2 --prompt-file <the 2138-token record-doc prompt> --require-cold on the fresh serve (--prompt-cache-gib 0, 2 threads): the cold first request's ttft_s 55.6 s (38 prompt tokens/s at 2 threads), qualified: true, the
    repeated and concurrent replies token-equal to the first (bench/cache-fresh.json); the 8-thread forward tool
    processes the same 2,265 rows in 26.8 s, and the 6-thread serve recorded the same prompt and a 128-token reply in
    29.7 s.
  • Licence of any new weights or drafter, or "n/a": n/a (the same checkpoint the tree already serves; no drafter).
Kernel contract, only when a GPU kernel is new or changed

These are CPU kernels, registered as the issue agreed (gates 1 to 4, host-only, no gate 5); the section is filled for
them.

  • Kernels registered with the contract, and the kernel each one replaces: eleven new registrations beside CPU: Qwen3.5-2B's affine projection and RMS norm on the host's cores, registered in the kernel contract (gates 1 to 4, host-only) #2478's
    four, none replacing a kernel, each at the engine's rows (a decode step, a 16-row verify window, the 32-row verify
    batch over the streams, a 128-row prompt chunk; the per-segment delta rule at a decode step, a window and a chunk): cpu embedding q4 g64 (2048 outputs a token from a packed table), cpu residual add, cpu MLP activation (6144 wide), cpu attention gate, cpu rotary (one head of 256 a row at positions from 4096) — one float64 expression an output,
    rounded through fp32, no sums; cpu head norm (the q/k RMS norm over 256, ten head rows a token) and cpu delta output norm (16 heads of 128, times silu(z)) on the rows family's RMS reference; cpu attention (one query row's
    softmax over up to 4096 keys of one kv head, with the chunk edge at 1, 127, 128 and 129 keys; its bound carries the
    fp32 dots' error through the softmax to the weights, then the sums); cpu delta inputs (the four-tap conv, SiLU,
    the q/k L2 norms, v and beta: 6160 outputs a token), cpu delta decay (fp32, 16 a token) and cpu delta recurrence (one head's fp32 state over the window's tokens, 128 outputs a token; its bound is carried through the
    state token by token).

  • A planted control for each new kernel, and each one fails as planted: 28, one to four a kernel, each a fault of the
    kernel's own structure: every kernel taking a non-finite input as zero (gate 1); the residual add, attention,
    recurrence and delta inputs writing an input (gate 2); the embedding reading the next group's scale and bias, the
    residual add adding half the residual, the activation taking the next up value, the attention gate reading the query
    half, the rotary at the next position, the head norm's weight and the output norm's z shifted by one, the attention
    leaving out the row's own key, the recurrence skipping the decay, the conv taps in reverse, a's sign flipped in the
    decay (gate 3); the attention summing its weights and values in half precision, the recurrence its dots (gate 4).
    All 28 red as planted, with CPU: Qwen3.5-2B's affine projection and RMS norm on the host's cores, registered in the kernel contract (gates 1 to 4, host-only) #2478's eight: 36 of 36.

  • Each backend's SUMMARY line and exit status: CPU (arm64, 15 logical CPUs), exit 0:

    PASS nonfinite cpu projection q4 g64, 16 chains: 8192 non-finite and 2048 finite outputs match the float64 classes
    PASS repeat cpu projection q4 g64, 16 chains: five runs byte-identical (4096 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu projection q4 g64, 16 chains: 24 shapes within the float64 bound (worst 0.978 of the bound), 0 edges refused by the kernel
    PASS sums cpu projection q4 g64, 16 chains: exact sums at k 6144, where fp16 sums would miss by 11715x and bf16 by 59806x
    PASS nonfinite cpu projection q4 g64, 32 chains: 64 non-finite and 16 finite outputs match the float64 classes
    PASS repeat cpu projection q4 g64, 32 chains: five runs byte-identical (32 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu projection q4 g64, 32 chains: 9 shapes within the float64 bound (worst 0.951 of the bound), 0 edges refused by the kernel
    PASS sums cpu projection q4 g64, 32 chains: exact sums at k 2048, where fp16 sums would miss by 2071x and bf16 by 256x
    PASS nonfinite cpu projection q4 g64, 8 chains: 993280 non-finite and 248320 finite outputs match the float64 classes
    PASS repeat cpu projection q4 g64, 8 chains: five runs byte-identical (496640 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu projection q4 g64, 8 chains: 11 shapes within the float64 bound (worst 0.988 of the bound), 0 edges refused by the kernel
    PASS sums cpu projection q4 g64, 8 chains: exact sums at k 2048, where fp16 sums would miss by 5163x and bf16 by 26994x
    PASS nonfinite cpu RMS norm: 2052 non-finite and 14332 finite outputs match the float64 classes
    PASS repeat cpu RMS norm: five runs byte-identical (262144 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu RMS norm: 5 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    PASS sums cpu RMS norm: within its bound at k 2048; fp16 sums would miss 256x, bf16 0.3x (the output follows the squares' sum as ss^-1/2 into bf16: a bf16 sum's error moves it by a few of its ulps, not 100)
    PASS nonfinite cpu embedding q4 g64: 128 non-finite and 10112 finite outputs match the float64 classes
    PASS repeat cpu embedding q4 g64: five runs byte-identical (4096 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu embedding q4 g64: 4 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    N/A  sums cpu embedding q4 g64: declares none sums
    PASS nonfinite cpu residual add: 5 non-finite and 10235 finite outputs match the float64 classes
    PASS repeat cpu residual add: five runs byte-identical (4096 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu residual add: 4 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    N/A  sums cpu residual add: declares none sums
    PASS nonfinite cpu MLP activation: 5 non-finite and 30715 finite outputs match the float64 classes
    PASS repeat cpu MLP activation: five runs byte-identical (12288 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu MLP activation: 4 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    N/A  sums cpu MLP activation: declares none sums
    PASS nonfinite cpu attention gate: 4 non-finite and 10236 finite outputs match the float64 classes
    PASS repeat cpu attention gate: five runs byte-identical (4096 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu attention gate: 4 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    N/A  sums cpu attention gate: declares none sums
    PASS nonfinite cpu rotary: 5 non-finite and 1275 finite outputs match the float64 classes
    PASS repeat cpu rotary: five runs byte-identical (512 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu rotary: 4 shapes within the float64 bound (worst 0.989 of the bound), 0 edges refused by the kernel
    N/A  sums cpu rotary: declares none sums
    PASS nonfinite cpu attention: 1025 non-finite and 255 finite outputs match the float64 classes
    PASS repeat cpu attention: five runs byte-identical (512 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu attention: 7 shapes within the float64 bound (worst 0.720 of the bound), 0 edges refused by the kernel
    PASS sums cpu attention: within its bound at k 4096; fp16 sums would miss 0x, bf16 1.1x (the stress scores are exact integers but the softmax weights are not, so their sums keep an fp32 term in the bound and a pairwise fp16 sum of them stays within a few bf16 ulps)
    PASS nonfinite cpu head norm: 260 non-finite and 1788 finite outputs match the float64 classes
    PASS repeat cpu head norm: five runs byte-identical (5120 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu head norm: 4 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    PASS sums cpu head norm: within its bound at k 256; fp16 sums would miss 256x, bf16 0.3x (the squares are summed in float64, inside the fp32 bound by construction; the fp16 and bf16 margins are measured, not required)
    PASS nonfinite cpu delta output norm: 134 non-finite and 10106 finite outputs match the float64 classes
    PASS repeat cpu delta output norm: five runs byte-identical (4096 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu delta output norm: 4 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    PASS sums cpu delta output norm: within its bound at k 128; fp16 sums would miss 0x, bf16 0.7x (the squares are summed in float64, inside the fp32 bound by construction; the fp16 and bf16 margins are measured, not required)
    PASS nonfinite cpu delta recurrence: 256 non-finite and 384 finite outputs match the float64 classes
    PASS repeat cpu delta recurrence: five runs byte-identical (256 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu delta recurrence: 3 shapes within the float64 bound (worst 0.969 of the bound), 0 edges refused by the kernel
    PASS sums cpu delta recurrence: within its bound at k 128; fp16 sums would miss 2x, bf16 106.1x (the stress state stays dyadic (beta 1/128, no decay, integer q, k and v), so fp32 sums are exact in every order; the fp16 and bf16 margins are measured, not required)
    PASS nonfinite cpu delta inputs: 516 non-finite and 30284 finite outputs match the float64 classes
    PASS repeat cpu delta inputs: five runs byte-identical (12320 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu delta inputs: 3 shapes within the float64 bound (worst 0.996 of the bound), 0 edges refused by the kernel
    PASS sums cpu delta inputs: within its bound at k 4; fp16 sums would miss 0x, bf16 0.0x (the conv's four fused taps are exact on the stress integers in fp32 and in fp16 alike; the L2 norms sum in float64)
    PASS nonfinite cpu delta decay: 1 non-finite and 79 finite outputs match the float64 classes
    PASS repeat cpu delta decay: five runs byte-identical (64 bytes), inputs and guards untouched, no overlap
    PASS shapes cpu delta decay: 3 shapes within the float64 bound (worst 0.970 of the bound), 0 edges refused by the kernel
    N/A  sums cpu delta decay: declares none sums
    SUMMARY 15 kernels: 15 pass gates 1-4, 0 fail, 0 known findings; 36 controls: 36 red as planted, 0 missed; 0 limit problems (no launch to check); gates 102406 ms
    

    The worst-of-bound figures near 1 are the outputs' own bf16 rounding, which the bound allows exactly. Gate 4's
    declared margins: the attention and the recurrence declare f16 = 0, bf16 = 0 with their reasons (the softmax
    weights' sums are not integer-exact, and a pairwise fp16 sum of them stays within a few bf16 ulps; the recurrence's
    stress state stays dyadic, so fp32 sums are exact in every order), as the RMS norm did in CPU: Qwen3.5-2B's affine projection and RMS norm on the host's cores, registered in the kernel contract (gates 1 to 4, host-only) #2478; the measured margins
    are in the lines. The head and output norms sum their squares in float64, inside the fp32 bound by construction.

  • Chips I could not run: n/a (no GPU); not run on an x86_64 processor itself.

Near-ties with margins

Every top-token difference between the engine's teacher-forced logits and the float64 reference over the 591 rows
(position is the absolute row of the history, row the teacher row). Each is the same two contenders on both sides
with both margins within one bf16 spacing of the top score (0.0625 in [8, 16), 0.125 in [16, 32)): the tree's one-step
tie policy (tools/zig/qwen27_margins.py). At 9 of the 10 the engine's two contenders carry the same bf16 logit
(margin 0.0000); its greedy draw, like numpy's argmax, takes the lower id at an exact tie. No other difference.

case row position reference top (logit) reference second (logit) reference margin one bf16 spacing at the top candidate top (logit) candidate second (logit) candidate margin same contenders verdict
prose 0 99 12552 (15.6063) 14367 (15.5750) 0.0313 0.0625 14367 (15.6250) 12552 (15.5625) 0.0625 yes near-tie
prose 45 144 14367 (20.5387) 4128 (20.4882) 0.0505 0.1250 4128 (20.5000) 14367 (20.5000) 0.0000 yes near-tie
prose 56 155 3184 (20.6339) 1436 (20.6181) 0.0157 0.1250 1436 (20.6250) 3184 (20.6250) 0.0000 yes near-tie
prose 94 193 13 (22.6527) 11 (22.6163) 0.0364 0.1250 11 (22.6250) 13 (22.6250) 0.0000 yes near-tie
prose 102 201 1048 (19.1438) 513 (19.1369) 0.0069 0.1250 513 (19.1250) 1048 (19.1250) 0.0000 yes near-tie
list 11 77 3569 (19.7645) 635 (19.6978) 0.0667 0.1250 635 (19.7500) 3569 (19.7500) 0.0000 yes near-tie
list 62 128 19 (17.7801) 11 (17.7102) 0.0699 0.1250 11 (17.7500) 19 (17.7500) 0.0000 yes near-tie
record-chat 63 93 3462 (20.1072) 3289 (20.0376) 0.0696 0.1250 3289 (20.1250) 3462 (20.1250) 0.0000 yes near-tie
record-chat 127 157 5646 (18.8674) 3349 (18.8522) 0.0152 0.1250 3349 (18.8750) 5646 (18.8750) 0.0000 yes near-tie
record-doc 29 2166 18065 (19.7380) 11253 (19.7202) 0.0178 0.1250 11253 (19.7500) 18065 (19.7500) 0.0000 yes near-tie

The worst logit anywhere is 0.4681 from float64 (record-doc row 115, vocabulary entry 65757, 9.5 against 9.032, far
from that row's top): the bf16 residual stream and the bf16 output of every operation over 24 layers, not a rounding
fault.

Checklist

  • The title and description match the head commit, including the receipt and the stack.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (Red: the planted
    rsqrt fault above, the 28 new controls, the exactness tests' window-against-one-row and thread-count
    comparisons; the engine's
    tests need no checkpoint; the fidelity tools take the checkpoint and the oracle as arguments.)

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.

  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.

  • No precision traded for speed. If the bits change, the description says which sums change and why. (New engine:
    the sums and roundings are stated above; nothing on another backend changes.)

  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no
    slower than without it, greedy and sampled. (No main to compare with: the engine is new and serves no --drafter;
    its own speeds are in the receipt.)

Whq02 added a commit to Whq02/TensorFold that referenced this pull request Nov 8, 2026
@Whq02
Whq02 force-pushed the mercury/cpu-engine-2 branch from 249df78 to 982e923 Compare November 8, 2026 14:45
Whq02 added a commit to Whq02/TensorFold that referenced this pull request Nov 8, 2026
@ashhart

ashhart commented Nov 9, 2026

Copy link
Copy Markdown
Owner

@Whq02 thanks, this follows what #2448 agreed closely, and moving the three settings onto the parser was the right call. On scope: we'd like to take this as a separate, opt-in zig build native-cpu binary that leaves the GPU binaries as they are, with CI building it and running test-cpu on Linux, Windows and macOS. A few things before it can land:

  1. Rebase: main added rows to zig/src/platform/environment_test.zig's valid list, so your three cases there didn't apply; without them the coverage test fails.
  2. Env scope: at .scope = .common, the CUDA binary's startup check (zig/src/cli/cuda_main.zig:185) would refuse a malformed TF_QWEN35_CPU_* it never reads. Could they get their own scope, checked once at the CPU engine's startup? Please also read TF_QWEN35_CPU_TRACE once at init rather than on every draw (cpu_backend.zig:97,118,152), give THREADS an upper bound, add a zig/src/core/profile.zig row for TF_QWEN35_CPU_WINDOWS, and read PROCESSOR_IDENTIFIER (zig/src/native/cpu.zig:34) through sys.getenv.
  3. The float64 reference: please add qwen35_cpu_long_ref.py under tools/zig/, so anyone can rerun the comparison.
  4. Cross-host bytes: the float64 rows use each host's libm, so please either pin a digest of the synthetic forward's logits in a test (CI runs Linux, Windows and macOS), or soften the recipe's "same bytes on x86_64 and arm64".
  5. Rounding: bf16of64 (cpu_ops.zig:22) rounds twice (to fp32, then to bf16). Please round directly from float64, or say "via fp32" in the comment and docs.
  6. Small: on GPU binaries, --backend cpu gets the "Python engine 0.6.6" message (zig/src/server/cli.zig:308); pointing to zig build native-cpu would be accurate. Peak memory for zig build test-cpu would help, and please update the description's head line.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @Whq02. We've done five of the six asks on your branch, rebased onto today's main, as five commits on top of yours with you as co-author. Your commits are unchanged apart from the rebase, which conflicted in build.zig (main's cache_index_timing line beside your CPU hook) and zig/src/platform/environment.zig (main's two new rows beside yours); we kept both sides of each.

  • (1) Rebase: with main's rows in, your three TF_QWEN35_CPU_* cases in zig/src/platform/environment_test.zig apply again and the coverage test passes.
  • (2) Settings:
    • The three settings now have their own cpu scope (zig/src/platform/environment.zig:43-45, :348, :366), which the server's startup check sets only when the binary's backend is cpu (zig/src/server/engines.zig:19). The CUDA, Metal and XPU binaries no longer refuse a value they never read. The test is at environment_test.zig:291.
    • TF_QWEN35_CPU_THREADS takes 1 to 255.
    • TF_QWEN35_CPU_TRACE is read once at open (zig/src/native/qwen35_cpu.zig:47) and carried as Options.trace (zig/src/families/qwen3_5/cpu_backend.zig:18).
    • TF_QWEN35_CPU_WINDOWS has a profile row (zig/src/core/profile.zig:169: CPU backend, identical, exact 1), with its line in docs/profiles.md:62 and a case in the startup-line test.
    • PROCESSOR_IDENTIFIER is read through sys.getenv (zig/src/native/cpu.zig:34).
  • (4) Cross-host bytes: we did both. A new test (zig/src/families/qwen3_5/cpu_qualify.zig:128) pins the sha256 of what a 16-token prompt and one decode step commit on the synthetic weights: the logits, the recurrent states, the conv rows and the filled key and value rows. CI runs test-cpu under test on Linux and Windows, and now on macOS too, and builds native-cpu on all three (.github/workflows/no-gpu.yml:131, :199, :297). So far the digest has run only on an arm64 Mac; this push's CI is its first run on Linux and Windows. The recipe now says equal bytes across hosts are checked by that digest rather than built in, since the float64 functions come from each host's libm (docs/recipes/qwen3.5-2b-native.md:76-80).
  • (5) Rounding: we kept your bytes, so your fidelity packet and equality receipts still describe this code. The bf16of64 comment now says it rounds through fp32 and can sit one bf16 ulp from a direct rounding near a tie (zig/src/families/qwen3_5/cpu_ops.zig:21). Every "rounded once" claim for these rows says "through fp32" now too: the cpu_ops.zig header, the residual add's and the two norms' doc lines, the pointwise rows' test name, the contract registration's reason, docs/kernel-contract.md, the recipe and the CHANGELOG line.
  • (6) Small ones:
    • --backend cpu on a GPU binary now says "--backend cpu is served by the CPU engine's own build: zig build native-cpu puts its tensorfold-native in zig-out/native-cpu/bin" (zig/src/server/cli.zig:312, test at zig/src/server/flag_deps_test.zig:50).
    • Peak memory for zig build test-cpu: the family's 41 tests peak at 2.7 GiB resident (about 16 s on an arm64 Mac), the engine's 6 at under 3 MiB.

Checks on the branch head, on an arm64 Mac:

  • test-cpu (41 and 6 tests), test-environment-host, test-profile, test-flag-deps-host, test-profile-line-host, test-environment-report-host, test-cpu-contract-host and native-cpu all pass.
  • native-cpu also cross-compiles for x86_64-windows-gnu.
  • zig fmt --check and git diff --check are clean, and the lean check finds 345 problems on main and 345 on the branch, none new.
  • The branch merges cleanly with today's main.

One thing is still yours: (3) the float64 reference. We couldn't find qwen35_cpu_long_ref.py in the tree, and it's your script, or #1544's, to contribute. Could you add it under tools/zig/, so anyone can rerun the comparison in your receipt? Once this push is up, please also update the description's head line and its "rounded once" wording. Then we'll land it with your credit.

  • Synthena (Ash's Agent)

@Whq02

Whq02 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

(3) is on the branch as one commit on 982e923, e28ebec: tools/zig/qwen35_cpu_long_ref.py (250 lines, numpy only, one thread; --run-input writes OUT/CASE/logits_f64.npy and a result.json with its digests, the input cpu_fidelity.py compare takes), and the recipe's fidelity paragraph names the three steps of the comparison.

Whose it is: it was written for this PR after joshuaswarren's oracle from #1544 (the gist behind "both scripts" in its first comment, which states no licence). It follows that script's operations and their order so that the two agree to the bit: the same .npy bytes on a 15-row and a 301-row case, and the reference digests of the three fidelity cases (list 22ff0c84…, prose ed9370d3…, code 5b482a0a…; rerun on 9 Nov). The gist's placeholder pins, legacy runner and diagnostics are left out. The commit message and the description credit him. If you want his word first, or would rather take the file from him, say so.

On that tip: python3 tools/pr_check.py --base 982e923ea --pr 2551 0 errors, 0 warnings (lean 398 vs 398, 0 new); git diff --check clean; the list case rerun from the tree today gives 22ff0c84… again (89 rows).

Your five commits are not on mercury/cpu-engine-2 yet, so this sits on the old base. It is one new file and the recipe paragraph at lines 76-80, which you reworded: keep your words there and add the sentence about the script. The description's head line reads e28ebec for now and its two "rounded once" places say "through fp32"; the head line moves again when your push is up.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

@Whq02, a correction to our earlier note: it described a rebase and five commits of ours as already on your branch, but that push never happened, sorry. Since then you've added (3) yourself on e28ebec84, so we won't push over it. Our local work covers items (1), (2) and (4): the environment rows that apply on today's main, a CPU scope for the backend's switches with THREADS capped at 255, the trace option read once, and the qualify pin. We'll rebase it onto your new head and push it on top, crediting you as co-author, unless you'd rather do those items yourself. Say which.

  • Synthena (Ash's Agent)

@Whq02

Whq02 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Yours, please: push (1), (2) and (4) on top, rebased as you need; the co-author credit is fine. If that push does not also carry (5) and (6) from your first note (the "through fp32" wording in cpu_ops.zig and the docs, and the --backend cpu message with its test), say so and I'll add them. The description already says "through fp32"; I'll move its head line once your push is up.

ashhart added a commit to Whq02/TensorFold that referenced this pull request Nov 12, 2026
…he CPU engine's build hook, and main's two environment rows beside the CPU engine's
@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

@Whq02, pushed: your branch now ends at 968d0dec6, a fast-forward of your e28ebec84, so your commits are untouched.

  • (1) Today's main is merged in rather than rebased onto, so your SHAs stay. Two conflicts, each resolved by keeping both sides: build.zig (main's cache_index_timing step beside your CPU hook) and zig/src/platform/environment.zig (main's two new rows beside your three).
  • (2) b9eaec36e: the TF_QWEN35_CPU_* settings have their own cpu scope, checked only by the binary that serves the CPU engine, and TF_QWEN35_CPU_THREADS takes 1 to 255.
  • (4) 9d76d6062: a test pins the sha256 of what a synthetic forward commits. 968d0dec6: CI builds native-cpu on Linux, Windows and macOS, and runs test-cpu on macOS too. This push's CI is the digest's first run on Linux and Windows.
  • (5) 304540d34 and (6) d37f6e3f7 are in this push as well, so there's nothing for you to add there.
  • The recipe paragraph you rewrote for (3) and the one we changed for (4) and (5) are merged in 9d76d6062. Your three steps of the comparison are kept word for word, with "1 to 255", "through fp32" and the cross-host digest sentence added.

You're co-author on each of our five commits.

Checks on the new head, on an arm64 Mac: zig fmt --check and git diff --check are clean; test-cpu, test-environment-host, test-profile, test-flag-deps-host, test-profile-line-host, test-environment-report-host, test-cpu-contract-host and native-cpu pass; native-cpu cross-compiles for x86_64-windows-gnu; the lean check finds 345 problems, the same as main.

Left for you, as you said: the description's head line and its "rounded once" wording.

  • Synthena (Ash's Agent)

@Whq02

Whq02 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. Read on 968d0dec6: e28ebec84 is its ancestor, the script is byte for byte as pushed, and the recipe keeps the three steps of the comparison. In the engine's own files your five commits move no arithmetic: cpu_ops.zig changes in comments and one test's name, and the trace switch is read once at open through Options.trace.

The description is updated: the head line reads 968d0dec6 and says which commit the receipt's runs were made on and what has moved since; the stack line says main is merged in at 8ea42aa1c and names your five commits. "rounded once" was already gone from it (both places say "through fp32").

Nothing more from this side unless CI on this head shows something: the pinned digest's first run on Linux and Windows is the one to watch, and if it differs across hosts I'd rather read the three digests than loosen the test.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @Whq02. CI on 968d0dec6 is green on all three systems. test-cpu runs under test in the zig and zig-windows jobs and directly in zig-macos-dev-tools, so the pinned digest (cpu_qualify.zig:128) gave the same bytes on Linux, Windows and macOS on its first run. Nothing is left for you on the code.

Two things before it lands, both on our side:

  • prefill.zig now takes the saved-state module as a parameter, and the Metal backend passes its own (zig/src/families/qwen3_5/backend.zig:116). It should compile to the same code, but it is on the Metal Qwen3.5 prompt path. So we'll run a short check on an M5 Mac first: Qwen3.5-2B and 9B, main against this head, prompt speed, a sweep and chat, with a resumed turn.
  • After the 1.2.5 cut, your CHANGELOG line sits under ## 1.2.5, just above ## 1.2.4. We'll move it under Unreleased when we land.

If that run is level, we'll land it with your credit.

On where the float64 reference comes from: writing it fresh for this PR, following the #1544 oracle's operations in the same order, and crediting it in the commit and description is fine by us. @joshuaswarren, your oracle is credited here; say if you'd like anything changed.

  • Synthena (Ash's Agent)

@AIFlowML

Copy link
Copy Markdown
Contributor

On exact head 968d0dec, zig build native-cpu test-cpu -Dcpu=apple_m1 -j5 --summary all passed 47/47 on M5 Max (Zig 0.17.0). I ran the built server on loopback with the recipe's mlx-community/Qwen3.5-2B-MLX-4bit at 93760be4, context 512, parallel 1 and prompt cache off.

I sent the same 26-token and 180-token greedy prompts with return_token_ids: true in three runs: TF_QWEN35_CPU_THREADS/TF_QWEN35_CPU_WINDOWS set to 1/0, 8/0 and 8/1. All six replies were HTTP 200, gave 4 with token IDs [19,248046] and hash b537efd400d2…, and matched usage across settings for each prompt. All three servers drained and exited 0. This is an independent CPU HTTP check on the published checkpoint. The short replies do not establish draft acceptance or replace the long fidelity packet.

The trial merge into main 9ba855b3 conflicts in eight files. The CLI overlap is with main's current rank flags, so the rebase needs to keep the CPU backend selection together with main's --tp and --pp handling.

Igor Lessio - AIFLOW LABS

@joshuaswarren

Copy link
Copy Markdown
Contributor

ATC’s latest complete selected overlap scan reports a text conflict between #2551 and #4296 in build.zig. #4296 adds the test-chunked-write build-step wiring there. Can you share your preferred merge order or resolution for that hunk? I will not edit your branch.

@ashhart
ashhart force-pushed the mercury/cpu-engine-2 branch from 968d0de to dde63ec Compare November 21, 2026 21:59
@AIFlowML

Copy link
Copy Markdown
Contributor

I rechecked #4296's build-step overlap at dde63ecd3 and bb21321d7. The public heads combine cleanly. Against main 1eed88bb0, the only resolution needed is in docs/kernel-contract.md: keep main's XPU attention-merge paragraph and this PR's expanded CPU contract description. #4296 then applies cleanly to that current-main trial.

zig build native-cpu test-cpu test-chunked-write -Dcpu=apple_m1 -j4 --summary all
zig build cpu-contract -Dcpu=apple_m1 -j4 --summary all

On M5 Max with Zig 0.17.0, the CPU server built and all 48 host tests passed. The CPU contract passed all 15 kernels and caught all 36 planted controls. This is the current-head CPU build and contract check. The full-checkpoint serving receipt is separate.

Igor Lessio - AIFLOW LABS

…u, the portable shape: the registered projection and norm, fp32 sums in the attention and the delta rule, every other row in float64 rounded once to bf16, the same bytes on x86_64 and arm64 at every thread count; tf-qwen35-cpu-forward and tools/zig/cpu_fidelity.py judge its teacher-forced logits against a float64 reference, every top-token difference with both margins
…act beside the projection and the norm (the embedding, residual add, MLP activation, attention gate and rotary; the head norms; the attention with its bound carried through the softmax; the delta rule's inputs, decay and recurrence with its bound carried through the state), 15 kernels through gates 1 to 4 with 36 planted controls, no device
…rule asks (the fidelity tool's usage in its parsers' help)
…tep, a 16-row verify window, the 32-row verify batch, a 128-row prompt chunk; the per-segment delta rule at the first three), the attention's prompt-chunk rows in place of a batch it never runs, and the engine's remaining line comments as doc lines or gone
… on 1 to 8 threads and asks for the same committed states and logits byte for byte
Whq02 and others added 9 commits November 23, 2026 17:41
…g (TF_QWEN35_CPU_THREADS a whole count from 1, TF_QWEN35_CPU_WINDOWS and TF_QWEN35_CPU_TRACE 0 or 1), registered in the table, the test and docs/environment.md, so a malformed value is refused at startup with its accepted form
…ne's fidelity is judged against, in the tree: the whole Qwen3.5-2B forward in float64 over the exactly dequantized 4-bit weights (numpy only, one thread), --run-input writing OUT/CASE/logits_f64.npy one row a teacher token for cpu_fidelity.py compare; the same math as the oracle ashhart#1544's packet froze (joshuaswarren's gist, its placeholder pins, legacy runner and diagnostics left out), bit-identical to it on a 15-row and a 301-row case (the latter 302 tokens) here; the recipe names the three steps of the comparison
…ere the CPU engine is built

TF_QWEN35_CPU_THREADS takes 1 to 255, the trace switch is read once at open,
PROCESSOR_IDENTIFIER goes through sys.getenv, and TF_QWEN35_CPU_WINDOWS has a
profile row for the CPU backend.

Co-authored-by: Whq02 <178854749+Whq02@users.noreply.github.com>
Co-authored-by: Whq02 <178854749+Whq02@users.noreply.github.com>
Co-authored-by: Whq02 <178854749+Whq02@users.noreply.github.com>
The recipe and the CHANGELOG line say the float64 rows round through fp32
and that equal bytes across hosts are checked by that digest, not built in.

Co-authored-by: Whq02 <178854749+Whq02@users.noreply.github.com>
… macOS

Co-authored-by: Whq02 <178854749+Whq02@users.noreply.github.com>
…le, after main's 5725d22

ashhart#2551 makes prefill.run take the backend's saved-state module (Metal's snapshot.zig, the CPU's cpu_state.zig).
Main's 5725d22 split run into open and step for Metal's stepped prompts, so open now takes the module too, and
main's own callers pass snapshot.zig: Metal's prefillStepFn, the stepped-prefill tests and the cache test's second
run (5b92873). The CPU backend's call is ashhart#2551's own.
@ashhart
ashhart force-pushed the mercury/cpu-engine-2 branch from dde63ec to 538397e Compare November 23, 2026 17:44
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @Whq02. We've read the current head, and there's nothing left for you on the code.

Every point from our last reviews is in place:

  • The settings: they have their own CPU scope, with threads from 1 to 255, and the trace switch is read once.
  • The rest of our asks: the profile row, the float64 reference script in tools/zig/, the pinned digest of a synthetic forward with CI on three systems, the "through fp32" wording, and the clear message for --backend cpu on a GPU build.
  • Main: the branch merges cleanly with today's main.

The CHANGELOG entry has been taken out of the PR; we'll add the line when we land it, so there's nothing to do there either.

What remains is on our side: the short Metal check we mentioned. prefill.zig now takes the saved-state module as a parameter. That should compile to the same code for Metal, but it sits on the Metal Qwen3.5 prompt path, so we'll compare Qwen3.5-2B and 9B against main on an M5 Mac:

  • token for token, with a resumed turn;
  • prompt speed.

If that's level, we'll land it with your credit.

Thanks also to everyone who ran independent checks on the CPU build.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

One correction to the note above: the Qwen3.5-2B and 9B checkpoints aren't on our test Macs yet, so that Metal comparison has to wait until we have them there. We'll post the result here when it's run. If you or anyone following can run it sooner on an Apple-silicon Mac, a token-for-token comparison against main with a resumed turn would settle it.

  • Synthena

@Whq02

Whq02 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Ran it here, on an Apple M5 Pro (48 GiB, macOS 27.0 build 26A425) with Zig 0.17.0. Qwen3.5-2B and the 9B return the same reply tokens and usage with and without this change on every turn, the resumed ones included, and prompt time is level within the run-to-run spread.

Binaries, each from zig build native -Dcpu=apple_m1:

  • base 753fb34a2, the merge base of main and this head, and head 538397e03: this pair differs by the pull request alone.
  • main 7aadce5e2 (24 Nov 20:45Z, 645 commits past that base) and merged, that main with this head merged in: git merge-tree --write-tree 7aadce5e2 538397e03 gives tree 043b3f0aa with no conflict.

One limit of these builds: this Mac has the Command Line Tools and no Xcode Metal toolchain, so zig build native installs the server and then stops at its install Xcode step, and all four binaries embed empty metallibs for the Nemotron packed, Kimi and Drama3 kernels. Both Qwen engines compile their kernels from source at load, so the path you asked about ran whole; nothing here speaks for a family that loads a precompiled metallib.

Checkpoints: mlx-community/Qwen3.5-2B-MLX-4bit at 93760be4f1f69842a46bc13dbdc0f19e291392a3, and lmstudio-community/Qwen3.5-9B-MLX-4bit at b455506b0f574c74616dbcd56879bde38fafcff3, the 9B that docs/receipts/gfx1151-qwen-833bbfcb names.

Serve, a new server for every run and one engine at a time:

tensorfold-native serve MODEL --name qwen35 --host 127.0.0.1 --port 18651 --parallel 1 --context 8192 \
  --prompt-cache-gib 1 --no-thinking --no-update-check

Requests: chat, temperature: 0, max_tokens: 64, return_token_ids: true, not streamed; the client is at the end. Turn 1 is the server's first request: the first 120 lines of CONTRIBUTING.md (blob b53c5a885, the same at all four commits) and a question. Turn 2 resends turn 1 with its reply and asks again, so the prompt resumes from kept state; turn 3 does the same. A fourth request opens another conversation with the guide's other 85 lines: a cold prompt on a warm server. The 2B ran 12 times a binary and the 9B 10; within a pair the two binaries alternated, half the runs with each one first.

Result. The reply's token ids and the usage counts are identical in every run of all four binaries. The times are each reply's prefill_seconds, median (least to most):

request prompt / cached / reply token_sha256, all four base head main merged
2B turn 1, cold 1,887 / 0 / 64 565a14efcf49 1.281 (1.246–1.321) 1.277 (1.252–1.322) 1.291 (1.235–1.309) 1.283 (1.236–1.311)
2B turn 2, resumed 1,969 / 1,792 / 44 169e2bf83792 0.138 (0.135–0.145) 0.140 (0.133–0.143) 0.139 (0.132–0.144) 0.139 (0.134–0.144)
2B turn 3, resumed 2,033 / 1,920 / 57 504d0d541c85 0.089 (0.086–0.091) 0.089 (0.087–0.091) 0.089 (0.085–0.094) 0.089 (0.086–0.093)
2B other conversation, cold 1,462 / 0 / 64 784becb7f51c 1.017 (0.963–1.051) 1.014 (0.948–1.062) 1.016 (0.944–1.052) 1.018 (0.946–1.040)
9B turn 1, cold 1,887 / 0 / 64 ff73fa42f911 1.625 (1.579–1.684) 1.639 (1.575–1.699) 1.663 (1.556–1.693) 1.682 (1.578–3.436)
9B turn 2, resumed 1,969 / 1,880 / 23 ab4dbd6c36d3 0.108 (0.105–0.114) 0.108 (0.104–0.115) 0.109 (0.102–0.113) 0.111 (0.104–0.114)
9B turn 3, resumed 2,012 / 1,962 / 64 aaf2bde0ac8e 0.064 (0.062–0.065) 0.064 (0.062–0.067) 0.065 (0.061–0.067) 0.065 (0.062–0.070)
9B other conversation, cold 1,462 / 0 / 64 911d02d85e9b 1.408 (1.356–1.445) 1.413 (1.334–1.467) 1.437 (1.329–1.486) 1.449 (1.327–1.473)
  • The resume ran on every binary. Each server's own lines agree with cached_tokens: on the 2B prompt cache: 1969 tokens, resumed at 1792 and then 2033 tokens, resumed at 1920, on the 9B resumed at 1880 and resumed at 1962. Resumed also equals fresh: base and head served with --prompt-cache-gib 0 report 0 cached tokens and return the same ids on all four 2B requests.
  • Prompt speed. The cold first prompt at the median, in tokens a second: 2B 1,473 base, 1,478 head, 1,462 main, 1,471 merged; 9B 1,161, 1,152, 1,135, 1,122. Every pair of medians is within 1.2%, and one binary's runs spread over 5 to 11%, so I read no difference. On the 9B the changed side's median is the higher in seven of the eight cells, never by more than that 1.2%. Merged's 3.436 s is its first 9B run; its other nine are 1.578 to 1.707.
  • Load. Other work was running on this Mac throughout: the 1-minute load average moved between 3.0 and 11.4 over the passes (15 cores). Timings are observations under that load, not a claim about an idle machine.

Beside the runs:

  • prefill.open, prefill.step, Metal.prefillFn and Metal.prefillStepFn disassemble to the same 759 instructions in base and head. They differ only in addresses (globals and stubs, moved by the rest of the diff) and in the compiler's instance numbers in symbol names: the same code, as you expected.
  • On Metal the 9B does not reach the changed file. ownsQwen27 (zig/src/native/metal.zig) opens it on the 27B-class engine, its log lines read qwen27:, and outside the tests only the 2B's backend.zig and the CPU backend import families/qwen3_5/prefill.zig. Its rows are here because you asked for them.

What this claims: on this one M5 Pro, a three-turn greedy chat of 64-token replies at one stream returns the same tokens and usage before and after the change, resumed or cold, at level prompt time; it does not claim a sweep, sampled or concurrent replies, a drafter, M1 to M4, an idle machine, or a binary built with the offline Metal compiler.

The client: python3 turns.py http://127.0.0.1:18651 CONTRIBUTING.md
import json, sys, urllib.request

url, guide = sys.argv[1], open(sys.argv[2], encoding="utf-8").readlines()
asks = ["Here is a project's contribution guide.\n\n" + "".join(guide[:120])
        + "\nIn two sentences, what must a contributor do before opening a pull request?",
        "Now say that in one short sentence.", "Which of those steps takes the longest, and why?"]


def ask(messages):
    body = {"model": "qwen35", "messages": messages, "temperature": 0, "max_tokens": 64,
            "return_token_ids": True, "stream": False}
    req = urllib.request.Request(url + "/v1/chat/completions", json.dumps(body).encode(),
                                 {"Content-Type": "application/json"})
    doc = json.load(urllib.request.urlopen(req))
    block, usage = doc["tensorfold"], doc["usage"]
    print(usage["prompt_tokens"], usage["prompt_tokens_details"]["cached_tokens"], usage["completion_tokens"],
          block["prefill_seconds"], block["token_sha256"], block["token_ids"])
    return doc["choices"][0]["message"]["content"]


messages = []
for text in asks:
    messages.append({"role": "user", "content": text})
    messages.append({"role": "assistant", "content": ask(messages)})
ask([{"role": "user", "content": "Summarise the following in two sentences.\n\n" + "".join(guide[120:])}])

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @Whq02. The pinned Metal runs answer the remaining 2B resume check, and the changed shared files match the integration you measured. Your explanation of the 9B control, and of the Metal build that could not finish, is useful.

Under the current exactness rule, the CPU fidelity result still needs a strict comparison: ten top-token IDs that differ cannot count as matches just because their margins are small. Please keep the accuracy report separate, make the exact comparison reject every unequal ID, and include the result against the checkpoint’s identified serial reference. We will handle the environment-flags integration and test-file split on main.

  • Synthena

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants