Repository navigation
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
Conversation
Whq02
added a commit
to Whq02/TensorFold
that referenced
this pull request
Nov 7, 2026
… 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
… after the contract's host tests it joins
… 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
force-pushed
the
mercury/cpu-contract-1
branch
from
November 8, 2026 00:11
4886cf4 to
6f5b2b2
Compare
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. |
7 of 8 tasks
Owner
|
Thanks @Whq02! This is in TensorFold 1.2.5, out now: https://github.com/ashhart/TensorFold/releases/tag/v1.2.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Before: the kernel contract (
zig/src/core/contract/) has Metal, CUDA, HIP, XPU and Vulkan kernels in it, and on the CPUonly the harness's own test fakes (
fakes.zig's row sums); nothing in the tree runs a CPU projection or norm through gates1 to 4. After: two CPU kernels are in the tree and registered,
zig/src/families/qwen3_5/cpu_kernels.zig's affineprojection (Qwen3.5-2B's MLX 4-bit, groups of 64,
y = x · Wᵀwith theqmm_*Metal kernels' arithmetic: eachgroup'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 summedas
qwen35_normsums them: 16 a lane in an fma chain, 32 lanes in a tree, 512-wide blocks in turn;rsqrtis1 / sqrtthrough float64, rounded once). Both run through gates1 to 4 with eight planted controls on any host with no device:
zig build cpu-contractprints theSUMMARYline,zig build test-kernel-contract(CI's no-device step) runs the same gates as a test, andzig build testruns thekernels' 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.
Windows, remain).
atc overlap --json: run 2026-11-07T21:08Z on this head againstorigin/mainat 3d54a01 (ATC 0.9.0;complete: false,pullFilesLimit):changedFiles8, 68 open pull requests share a file (build.zigordocs/kernel-contract.md; nothing shares the six new files), 10clean, 58unknown, 0conflicts.git merge-treeof all 68 heads against this head and against main alone gives the same result for every one: 15 mergeclean 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
wirecalls at the end ofbuild(), a region no open pull request touches; the docs are oneparagraph under Running it, one under Coverage and one line in Adding a kernel's step 1.
Checks on this head
python3 tools/zig/lean_check.py, on the base and on this head:python3 tools/pr_check.py --base origin/mainon 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
--pruntil this number exists). Every new file is under 350 lines; every comment isone line.
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 thefloat64 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, therig's guards, the two fixtures' gate bindings and depths) and the kernels' 3 (run under
test-cpu-kernels, the samebinary), 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, plustest-cpu-contract-host).zig build test-cpu-kernels --summary all(whatzig build testadds): 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 unexpecteddifferences (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 allxcrun -sdk macosx metalshader compiles of the Metalkernels (
xcrun: error: unable to find utility "metal": this Mac's developer tools carry no Metal compiler), nonea test and none a file this change touches; CI runs
teston ubuntu-latest and windows-latest (no-gpu.yml).zig fmt --check build.zig zigandgit diff --check origin/main..HEAD: clean.x86_64-linux-muslat-mcpu=x86_64_v3(AVX2 and FMA, what CI's-Dfixed-cpucompiles for) and at-mcpu=baseline(SSE2, the fma a correctly rounded library call), run in anx86_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).
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'sFAILline andCONTROL ... red at ..., as plantedare in theSUMMARYrun.
zigjob'stest-kernel-contractstep grows by the gates run (about twominutes 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
preparecalls, 14 inall, one a shape) and
testby the kernels' own tests (under a second).ubuntu-latestruns them on x86_64 once this isopen; 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(servedat 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), andcpu RMS norm(2048-wide rows, 128, 32 and 1 of them). Each chain count is what the publicqmm_<N>_<K>.metalsourcespin as
Sfor 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 fivethread 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
SUMMARYline and exit status: CPU (arm64, 15 logical CPUs), exit 0: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) · magnitudeterm under it. The norm declaresbf16 = 0for 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
faults above and the eight controls; no dependency to skip on.)