Skip to content

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

Merged
ashhart merged 7 commits into
ashhart:mainfrom
Whq02:mercury/cpu-contract-1
Nov 8, 2026
Merged

ashhart merged 7 commits into
ashhart:mainfrom
Whq02:mercury/cpu-contract-1

Conversation

@Whq02

@Whq02 Whq02 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

What this changes

Before: the kernel contract (zig/src/core/contract/) has Metal, CUDA, HIP, XPU and Vulkan kernels in it, and on the CPU
only the harness's own test fakes (fakes.zig's row sums); nothing in the tree runs a CPU projection or norm through gates
1 to 4. After: two CPU kernels are in the tree and registered, zig/src/families/qwen3_5/cpu_kernels.zig's affine
projection (Qwen3.5-2B's MLX 4-bit, groups of 64, y = x · Wᵀ with the qmm_* Metal kernels' arithmetic: each
group's 64 products in one fp32 fma chain, the group's scale and bias into one of S accumulation chains, the chains
reduced pairwise, one rounding to bf16) and its RMS norm (bf16(w · (x · rsqrt(mean(x²) + eps))), the squares summed
as qwen35_norm sums them: 16 a lane in an fma chain, 32 lanes in a tree, 512-wide blocks in turn; rsqrt is
1 / sqrt through float64, rounded once). Both run through gates
1 to 4 with eight planted controls on any host with no device: zig build cpu-contract prints the SUMMARY line,
zig build test-kernel-contract (CI's no-device step) runs the same gates as a test, and zig build test runs the
kernels' own test that the output bytes are the same at 1 to 16 threads and at column splits every 8 columns to 256
and every 256 beyond (each alignment of the four-tile pass), and on x86_64 and arm64 (a digest of a 128-row, 2048-wide
projection and a 128-row norm, pinned). This is the first of the three pull requests agreed on #2448; no engine calls
these kernels yet, no served path changes, default builds are unchanged.

The terms, from the maintainer's answer on #2448: "We'd judge the portable shape, against our float64 reference, not
against Metal's bits. It should be exact against itself: x86_64 and arm64 equal, at every thread count, which no test
varies yet." "Contract: gates 1–4 with planted controls, host-only, no gate 5. Adding that path to the harness can be
part of the first PR." "The first PR registers the CPU projection and norm, passing at every tiling and thread count,
with each control red as planted and CI running it with no device." The measured-table shape stays out of the tree, as
asked; these two kernels are the portable shape (no table, no measured function). The near-tie clause ("It should agree
with the reference except at near-ties, each listed with its margin.") has no entry here: a near-tie is a top-token
difference in a model's logits, and this pull request runs no model; gates 3 and 4 bound each output against float64,
none past its bound (the worst 0.996 of it); the list comes with the second pull request's fidelity packet.

  • Issue: Refs A CPU backend for Qwen3.5-2B (Windows x86_64 included): wanted, and under the Vulkan terms? #2448 (the first of the three pull requests it agreed; the 2B engine with its fidelity packet, then
    Windows, remain).
  • Stacked on: main (3d54a01), six commits.
  • atc overlap --json: run 2026-11-07T21:08Z on this head against origin/main at 3d54a01 (ATC 0.9.0; complete: false, pullFilesLimit): changedFiles 8, 68 open pull requests share a file (build.zig or
    docs/kernel-contract.md; nothing shares the six new files), 10 clean, 58 unknown, 0 conflicts. git merge-tree of all 68 heads against this head and against main alone gives the same result for every one: 15 merge
    clean both ways, 53 conflict with main alone already, in the same files with and without this branch. The build hook
    is one line among the wire calls at the end of build(), a region no open pull request touches; the docs are one
    paragraph under Running it, one under Coverage and one line in Adding a kernel's step 1.

Checks on this head

  • Head commit: 21956f5
  • 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 3d54a01, 8 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 new file is under 350 lines; every comment is
    one line.
  • Tests run, with the command and pass, fail and skip counts (Zig 0.17.0, an arm64 Mac with 15 logical CPUs):
    • zig build cpu-contract --summary all: SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 red as planted, 0 missed; 0 limit problems (no launch to check); gates 101288 ms, exit 0 (the gates' time is the
      float64 references and gate 4's replay, not the kernels). The four: the projection at 16, 32 and 8 chains and the
      norm; the gate lines are below.
    • zig build test-cpu-contract-host --summary all: 5/5 steps; the contract's 5 tests passed (the gates as a test, the
      rig's guards, the two fixtures' gate bindings and depths) and the kernels' 3 (run under test-cpu-kernels, the same
      binary), 0 failed, 0 skipped.
    • zig build test-kernel-contract --summary all, from an empty local cache: Build Summary: 30/30 steps succeeded; 226/226 tests passed (the existing host contract tests, plus test-cpu-contract-host).
    • zig build test-cpu-kernels --summary all (what zig build test adds): 3/3 passed, 0 failed, 0 skipped.
    • 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: 641/756 steps succeeded (62 failed); 9897/9967 tests passed (70 skipped), no test failed; 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.
    • x86_64: the kernels' tests cross-compiled for x86_64-linux-musl at -mcpu=x86_64_v3 (AVX2 and FMA, what CI's
      -Dfixed-cpu compiles for) and at -mcpu=baseline (SSE2, the fma a correctly rounded library call), run in an
      x86_64 Linux container under emulation on the same Mac: 3/3 passed each, so the pinned digest computed on arm64 is
      the x86_64 bytes too, with and without hardware fma (run on the previous tip; this tip changes the test's name
      only).
    • Red before green: with the spawned threads' ranges one tile short, the sweep fails (slices differ. first difference occurs at index 2040); with the chains reduced in one sequential chain instead of the pairwise tree,
      the digest moves (0xae93e2f573b522de to 0x3f34039ee6e5378e, expected 12579647728012042974, found 4554269104191911822); each control's FAIL line and CONTROL ... red at ..., as planted are in the SUMMARY
      run.
  • CI: not run yet; read after opening. The zig job's test-kernel-contract step grows by the gates run (about two
    minutes on this Mac, the largest part of it the 248,320-wide head: its gate 4 replay, which the harness runs on one
    thread, and its 508,559,360 codes filled, packed and copied at each of its eight full-size prepare calls, 14 in
    all, one a shape) and test by the kernels' own tests (under a second).
  • Not run, and why: the gates on an x86_64 processor itself (CI's ubuntu-latest runs them on x86_64 once this is
    open; the kernels' pinned-digest test ran on x86_64 under emulation only); the Metal shader compiles inside zig build test (no Metal compiler on this Mac, above); Metal, CUDA, HIP, XPU and Vulkan: no GPU kernel changes.

Receipt

n/a: these kernels have no caller yet (the 2B engine that calls them is the second pull request), so the change cannot
reach inference; the host checks are under Checks on this head.

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: cpu projection q4 g64, 16 chains (served
    at the 2B's widths, 2048, 4096, 512 and 6144 outputs over 2048 inputs and 2048 over 6144, each at one row, a 128-row
    prompt chunk and a 32-row verify window), cpu projection q4 g64, 32 chains (the delta rule's a and b, 16 outputs,
    the same three row counts), cpu projection q4 g64, 8 chains (the head, 248,320 outputs, one row and 32), and cpu RMS norm (2048-wide rows, 128, 32 and 1 of them). Each chain count is what the public qmm_<N>_<K>.metal sources
    pin as S for that width. Edges from the tiles: rows 1, 7, 8, 9 and 128; columns 8, 24, 32 and 40 (tiles of 8,
    passes of 4 tiles); inputs 64 and 128 (groups of 64); the norm at 512 and 1024. None replaces a kernel. Each
    registration's runs take 1 to 8 threads in turn (1 + run mod 8), so gate 2's five byte-identical runs are five
    thread counts (the 16-output a and b projections have two tiles, so all five of theirs run on two) and gate 3's
    shapes cross thread counts and tilings; the kernels' own test pins the bytes at 1 to 16 threads and at column splits
    every 8 columns to 256 and every 256 beyond on fixed inputs.

  • A planted control for each new kernel, and each one fails as planted: the projection zeroing a non-finite activation
    (gate 1), writing its activations (gate 2), skipping the rows past its last block of eight (gate 3; it passes at
    whole blocks and fails at rows 1, 7 and 9), and summing each group in half (gate 4); the norm taking a non-finite
    input as zero (gate 1), writing its output over its input (gate 2), scaling by the next element's weight (gate 3) and
    summing its squares in bf16 (gate 4). All eight red as planted, none missed.

  • 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)
    SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 red as planted, 0 missed; 0 limit problems (no launch to check); gates 101288 ms
    

    The worst-of-bound figures near 1 are the output's own bf16 rounding, which the bound allows exactly; the sums'
    fp32 error is the gamma(depth) · magnitude term under it. The norm declares bf16 = 0 for gate 4 with its reason,
    as the Metal RMS norm does: the simulated bf16 sum misses by 0.3x only, so that part is stated, not proven; the fp16
    margin is proven (256x). Gate 5 has nothing to check: a CPU kernel has no launch.

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

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 two planted
    faults above and the eight controls; no dependency to skip on.)
  • 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.

Whq02 added a commit to Whq02/TensorFold that referenced this pull request Nov 7, 2026
Whq02 added 7 commits November 8, 2026 00:11
… registered in the kernel contract (gates 1 to 4 with eight planted controls, no device; the same bytes at every thread count and column split, on x86_64 and arm64)
… a changed reduction order moves the digest), and the served shapes name the 32-row verify window, the head's included
… splits every 8 columns to 256 and every 256 beyond, and the doc says the two-tile projections run their five gate-2 runs on two threads
@ashhart
ashhart force-pushed the mercury/cpu-contract-1 branch from 4886cf4 to 6f5b2b2 Compare November 8, 2026 00:11
@ashhart
ashhart merged commit 3ace0f6 into ashhart:main Nov 8, 2026
5 of 7 checks passed
@ashhart

ashhart commented Nov 8, 2026

Copy link
Copy Markdown
Owner

Thank you @Whq02. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

@ashhart

ashhart commented Nov 8, 2026

Copy link
Copy Markdown
Owner

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.

2 participants