Skip to content

qwen27 metal: M5 takes TF_QWEN27_METAL_KV_PAGES; a paged prompt chunk finds its two pages once - #3180

Merged
ashhart merged 3 commits into
ashhart:mainfrom
CodingEmil:m5-prompt-pages
Nov 20, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
CodingEmil:m5-prompt-pages

Conversation

@CodingEmil

@CodingEmil CodingEmil commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: TF_QWEN27_METAL_KV_PAGES=1 refuses to start on a Mac with tensor units (M5), and a paged prompt pass reads the page table at every 32-key step. After: M5 Macs take the flag, and the paged prompt kernels (q27_attn_prompt*, both the tensor-unit and the _sg twin) find a 512-key chunk's two pages once and pick the right base per step. Output and cache bits are unchanged; with the flag off nothing changes.

Both parts in one pull request as agreed in #1293 (the refusal goes because the paged kernels now ran on an M5, and the M5 numbers are what showed the per-step cost).

Checks on this head

  • Head commit: 7238ed8e9fb9741d81263c5f9db5161c3d42307e, base 2ba55ec7d32d0bec446bb012678550be7ef52895 (Release 1.2.9).
  • Measured head: 068ccebabd4340507d946c99565ff4ddf11fe389 on base d6c4cce602eef4aff235625a5325bb617a9674ff. Every device cell, the test suite and the kernel contract below ran there. While they ran, main moved 17 commits to the 1.2.9 cut, which renamed the changelog's Unreleased section, so the commit was rebased once more to merge. git range-diff d6c4cce60..068ccebab 2ba55ec7d..7238ed8e9 shows only the changelog hunk moving; paged.zig (19b29c1d) and qwen27_served.zig (f4b1108d) are the same blobs on both heads. None of the 17 commits touches zig/kernels/metal, a qwen27 source, the lanes, the KV pool or the prompt cache (they are tests, the Living Weights v2 tool, the Ollama-compatible API, opt-in token IDs on streams, a build check and the release). The device cells were not repeated on 7238ed8e9.
  • The Unreleased line carries no measurement (the numbers are below).
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 341 and 341, 0 new (tools/zig/lean_new.py), rerun on 7238ed8e9 against 2ba55ec7d; the same counts as on the measured head. Inherited and untouched, e.g. tools/zig/reuse_served.py:265 (a docstring past 120 columns) and tools/zig/flashnext_hc_coalesce.py:1 (a multi-line docstring).
  • Tests run, with the command and pass, fail and skip counts (all on the measured head 068ccebab, M5 Ultra):
    • zig build test test-golden -Dcpu=apple_m1: 868/868 steps succeeded; 14001 of 14119 tests passed, 118 skipped, 0 failed. Goldens: 322/335 and 55/55 equal to server goldens, 0 unexpected differences (13 known, recorded separately).
    • tf-qwen27-attention-pages: PASS, 21 rounds, 6696960 output values and every cache row the same bits, paged and contiguous. With --shared-prefix the same. On a Mac with tensor units the tool takes the shared-prefix plan in both runs (zig/tests/qwen27/attention_pages.zig:64), so on the M5 the two runs are one case; the flag matters on Macs without tensor units.
    • tf-qwen27-pages-resume: PASS, restored equal to fresh, and restored, moved and fresh slots the same digests as main's. It prints 190 allocator leak lines at exit, the same 190 with paging off (Metal: KV pages from the core pool #1293): they come from the harness.
    • zig build native tf-qwen27-attention-pages tf-qwen27-pages-resume tf-kernel-contract -Dcpu=apple_m1: builds.
    • zig fmt --check build.zig zig: clean on the measured head; the two Zig files are unchanged since. git diff --check and tools/pr_check.py --base origin/main --pr 3180 rerun on 7238ed8e9: clean, 0 errors, 0 warnings.
    • Author and committer of the commit: CodingEmil <116643407+CodingEmil@users.noreply.github.com>, no attribution trailers.
  • CI: pending on this head.
  • Not run, and why: Macs without tensor units (M1 to M4), which run the _sg twin, because I have only an M5. jschmied's M3 Pro cells in this PR's thread cover it, measured on the previous head 60f31ecb6, whose paged kernel sources are the same blobs as this head's. zig build dev-tools was not rerun, because the change edits no API a tf-* tool calls (the tools above were built and run). Sampled temperatures, 2K and 64K cold prompts, and --drafter decode speed were not run.

Receipt

  • Main against branch: below. Main is the measured base's binary (d6c4cce60), the branch is the measured head's binary (068ccebab) with TF_QWEN27_METAL_KV_PAGES=1 unless a cell says otherwise. Each measurement has a fresh server with --context 65536 --parallel 2 --prompt-cache-gib 0, the same checkpoint and settings, and no other model process on the machine.

  • Boxes it reaches: Metal, Qwen3.8-27B, only with TF_QWEN27_METAL_KV_PAGES=1. With it unset, the branch's tokens equal main's (flag off, below).

  • New switch? No. The existing TF_QWEN27_METAL_KV_PAGES (default off) now also starts on tensor-unit Macs.

  • Environment: measured base d6c4cce60 and measured head 068ccebab, both built from those trees with -Dcpu=apple_m1. Zig 0.17.0, Xcode 27.2 (27B52), Metal toolchain 27B47, macOS 27.2 (26B118), Apple M5 Ultra, 96 GB. Checkpoint TensorFold/Qwen3.8-27B-MLX-4bit at 22d8d538, drafter z-lab/Qwen3.8-27B-DFlash2 at 50307d4c. All cells measured on 2026-11-14 in one sitting.

  • Paths and platforms: the paged prompt kernels (the tensor-unit q27_attn_prompt_8 path and the _sg twin) and the start-up refusal. Ran: M5 Ultra (tensor units). Not run by me: M1 to M4 (_sg); an M3 Pro ran the previous head (see above).

  • Exactness, tools/bench_concurrent.py --tokens 256 --reps 3 --temperatures 0 --levels 1,2 --alone --serial with --drafter, code and chat prompts:

    • Main and branch: each equal alone (3/3 at 1 stream, 6/6 at 2) and serial (1/1) at both levels.
    • All 54 token_sha256 are the same between main and branch (a10c9b84…4130 code, a06fab34…8f52 chat).
    • The decode cells without the drafter give the same token_sha (a10c9b848ed9, a06fab348fbe), so drafted equals plain.
    • The cold 8K and 32K prompts give the same token_sha256 in all runs (d2e888be…61ee, 53568b8b…b7d1).
    • Earlier runs in Metal: KV pages from the core pool #1293 cover a resumed prompt (slot and prompt cache) equal to a cold one with and without the flag.
  • Flag off equal to main: the measured head's binary without the variable, the same requests.

    • bench_concurrent with --drafter: equal alone and serial, all 54 token_sha256 the same as main's.
    • Cold 8K and 32K: the same token_sha256 as main's.
    • bench_openai decode: the same token_sha as main's, 48.05 / 48.02 t/s against main's 48.14 / 48.10 and 48.02 / 47.99.
  • Main against this branch: posted in Metal: KV pages from the core pool #1293 on main's kernels; every row below is on the measured head, with this change's kernels.

  • Decode, tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0, no drafter, alternated main, branch, branch, main (median t/s, fibonacci-raw / gpu-chat-no-think):

    run main branch
    1 48.14 / 48.10 48.53 / 48.48
    2 48.02 / 47.99 48.49 / 48.46

    Branch 0.8% faster (each run within 0.5% of the 2026-11-13 cells on main's kernels).

    Two streams, bench_concurrent.py --levels 2 (aggregate t/s, code / chat):

    run main branch
    1 93.6 / 95.7 94.2 / 96.2
    2 93.6 / 95.6 94.1 / 96.1
  • Prompt speed, tools/bench_cache.py --require-cold --streams 1 --tokens 64 with a fixed token array, alternated main, branch, branch, main; cold ttft_s, cached_tokens 0 in every run:

    prompt main branch change
    8K 5.277 / 5.274 5.294 / 5.293 +0.3%
    32K 23.216 / 23.205 23.370 / 23.363 +0.7%

    Without this change the flag's cost was +1.0% at 8K and +2.7 to 2.8% at 32K on the same machine (Metal: KV pages from the core pool #1293). The remaining cost is the flag's and opt-in; main's default path is not touched, and flag off its tokens equal main's.

  • Raw results: the JSON files of every cell (bench_concurrent, bench_openai, bench_cache), the two token arrays, the server logs and the test and contract logs: https://gist.github.com/CodingEmil/02308e5f3575c1311ddc08a289e7a842

  • Licence of any new weights or drafter: n/a, none.

Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: none new. The paged twins are main's prompt kernels with only the K/V address edited (zig/kernels/metal/qwen27/paged.zig). They are not registered with the contract; tf-qwen27-attention-pages checks them bit for bit against main's contiguous kernels (above).
  • A planted control for each new kernel: n/a, no new kernel.
  • Each backend's SUMMARY line and exit status, M5 Ultra:
    • zig build kernel-contract -Dcpu=apple_m1 refuses before any kernel on this Xcode, on main (d6c4cce60) as on the measured head, exit 1: rotary probes refuse this deployment: run tf-rotary-sweep and pass --rotary-accuracy FILE (UnrecordedRotaryCompiler).
    • tf-kernel-contract --only q27, measured head: SUMMARY 24 kernels: 24 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 18039 ms, limits 184 ms, exit 0.
    • tf-kernel-contract --only q27, main: SUMMARY 24 kernels: 24 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems; gates 17853 ms, limits 29 ms, exit 0.
  • Chips I could not run: M1 to M4.

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. (No new test; the existing tf-qwen27-attention-pages covers the edited kernels.)

  • 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.

  • 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. (Default path unchanged, flag off equal to main. With the opt-in flag: decode 0.8% faster at one stream and 0.5 to 0.6% faster at two; cold prompts 0.3% (8K) and 0.7% (32K) slower. Sampled and --drafter speed not run.)

@jschmied

Copy link
Copy Markdown
Contributor

@CodingEmil the M3 Pro cells you asked for in #1293, on head 60f31ecb6 against its base 13d7ce052. Bits are equal everywhere; speed on the M3 Pro is level, with the head a hair slower.

Apple M3 Pro, 14-core GPU (no tensor units), 36 GB, macOS 27.2, Zig 0.17.0, -Dcpu=apple_m1. Model TensorFold/Qwen3.8-27B-MLX-4bit. The Mac was in daily use during the runs.

tf-qwen27-attention-pages: PASS on head and main, plain and --shared-prefix: 21 rounds, 6,696,960 output values and every cache row the same bits, paged and contiguous (197 shuffled pages of 1,049,088 bytes). The tool has no must-fail control: its planted cases (non-finite V) sit inside the bit-equality check.

Cold prompt, A-B-B-A (main, head, head, main, two rounds; fresh server per run, TF_QWEN27_METAL_KV_PAGES=1 on both, --no-drafts --prompt-cache-gib 0 --parallel 1, greedy, 32 tokens; time = prefill_seconds):

prompt main runs (s) head runs (s) median main / head delta head slower than its round's main mean tokens
8,113 114.28 114.87 114.37 114.23 114.62 114.53 114.63 114.43 114.32 / 114.58 +0.23 % 3 of 4 (−0.04..+0.29 %) 24596a46… all 8 equal
32,303 514.98 514.64 514.40 514.58 515.08 514.85 514.58 514.99 514.61 / 514.92 +0.06 % 4 of 4 (+0.01..+0.10 %) f5f813ec… all 8 equal

So on this M3 Pro the change is exact and within 0.3 % of main either way; I can't tell whether the M5 gain should show here.

Binaries (md5): tensorfold-native head c832458193bd80f9558dbcba3343df4f, main 31f3a61778d6420c0b75314043dd8a64; tf-qwen27-attention-pages head 1ef65251b2b9098e8b37d458c4c5e364, main 9eb8b68c40403fb702e2c113c217a97b.

@CodingEmil

Copy link
Copy Markdown
Contributor Author

@ashhart the six M5 Ultra cells you asked for in #1293 are now in the description. They are all measured on 068ccebab (this change on main d6c4cce60), on one machine in one sitting. Raw files: https://gist.github.com/CodingEmil/02308e5f3575c1311ddc08a289e7a842

One caveat up front: while the cells ran, main moved 17 commits to the 1.2.9 cut and renamed the changelog's Unreleased section, so the pushed head is 7238ed8e9 on 2ba55ec7d, the same commit rebased once more. Only its changelog hunk moved (git range-diff d6c4cce60..068ccebab 2ba55ec7d..7238ed8e9), the two Zig files are the same blobs, and none of the 17 commits touches a Metal kernel, a qwen27 source, the lanes, the KV pool or the prompt cache. I did not repeat the device cells on 7238ed8e9; say if you want them there.

  • 8k cold prefill: Receipt, "Prompt speed". 0.3% slower at 8K and 0.7% slower at 32K with the flag on, A-B-B-A, with the same token_sha256 in every run.
  • Decode: Receipt, "Decode". bench_openai, A-B-B-A: branch 0.8% faster; two streams 0.5 to 0.6% faster.
  • The bench_concurrent exactness cells: Receipt, "Exactness". Equal alone and serial at 1 and 2 streams with --drafter; all 54 token_sha256 the same as main's.
  • zig build test: "Checks on this head". test test-golden: 14001 of 14119 passed, 118 skipped, 0 failed; 0 unexpected golden differences.
  • The kernel contract: Kernel contract section. The full zig build kernel-contract refuses before any kernel on this Xcode, on main as on the measured head (UnrecordedRotaryCompiler, message quoted). tf-kernel-contract --only q27 passes 24 of 24 kernels on both.
  • Flag off equal to main: Receipt, "Flag off equal to main". Same token_sha256 as main for bench_concurrent with --drafter, cold 8K/32K and bench_openai.

@jschmied's M3 Pro cells are measured on the previous head 60f31ecb6 (base 13d7ce052). The paged kernel they exercise is the same code on the measured and the pushed head.

  • paged.zig and every source it builds the paged kernels from have the same blobs on both heads: attention_prompt.metal, attention_prompt_sg.metal, attention_mpp.metal, attention_io.metal, core/kv_pages.h, core/kv_page_tensor.h, core/source_edit.zig. Check with git rev-parse 60f31ecb6:<file> 7238ed8e9:<file>; paged.zig is 19b29c1d on both.
  • Main did change other Qwen27 code between the two bases, such as the prompt glue kernels and the long-copy driver. jschmied's cells don't cover those on the new head.

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

@CodingEmil, FIXES: the modified paged prompt kernels still bypass the kernel contract. Its attention fixture explicitly selects the contiguous library. The separate shuffled-page checker compares bytes and plants non-finite inputs, but has no faulted-kernel controls.

Please register the paged M5 and simdgroup variants with shuffled-page replacement cases, 255/256 and 511/512 boundaries, partial final pages, non-finite cases and controls that fail their intended gates, plus the paged variants' launch limits.

The two-page address calculation preserves each 32-key step's address by inspection. The 25 host tests pass, and the native binary, page checkers and contract tool compile. We will run the default-path, paged prompt, DFlash2 and resumed/fresh device checks on our side once the contract covers these kernels.

  • Synthena (Ash's Agent)

ashhart added a commit to CodingEmil/TensorFold that referenced this pull request Nov 19, 2026
…unch limits

Answer the requested M5 and simdgroup contract coverage with shuffled pages, page and chunk seams, partial pages, independent nonfinite and stress references, contiguous byte replacements, and real source faults for gates 1 and 3. Keep production arithmetic unchanged.
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @CodingEmil. To get this moving, we finished our last ask ourselves, keeping your commit as it was: the paged M5 and simdgroup prompt-attention variants are now registered in the kernel contract, with shuffled-page replacement cases, the 255/256 and 511/512 boundaries, partial final pages, non-finite cases, planted controls that fail their gates, and the paged variants' launch limits. We also rebased it onto current main. What remains is ours: the contract run and the default, paged, DFlash2 and resumed checks on a Mac. Nothing is needed from you, and #3377 can follow once this lands.

  • Synthena

ashhart added a commit to CodingEmil/TensorFold that referenced this pull request Nov 20, 2026
…unch limits

Answer the requested M5 and simdgroup contract coverage with shuffled pages, page and chunk seams, partial pages, independent nonfinite and stress references, contiguous byte replacements, and real source faults for gates 1 and 3. Keep production arithmetic unchanged.
@AIFlowML

Copy link
Copy Markdown
Contributor

@ashhart @CodingEmil I ran the new paged-prompt contract on Apple M5 Max at 975703f09 with Zig 0.17.0. The first detail to add to the receipt is this head and the two filters for the newly registered cases. The description still points to the earlier contract run.

zig build tf-kernel-contract tf-qwen27-attention-pages tf-qwen27-pages-resume -Dcpu=apple_m1
./zig-out/bin/tf-kernel-contract --only 'qwen27 paged prompt' --no-limits
./zig-out/bin/tf-kernel-contract --no-gates --only 'qwen27 prompt m5 paged'

All 49 build steps passed. The four paged prompt kernels passed gates 1–4, both planted controls failed their intended gates, and all eight M5 launch records passed. The standalone attention checker matched 6,696,960 outputs and every cache row across 21 rounds. The synthetic resume checker matched restored, moved and fresh digests. It emitted the same 190 allocator diagnostics on this head and its merge base, f0d5ddb74.

This covers the M5 kernels and model-free parity checks. The simdgroup limits filter selected zero launches on this machine. The served checkpoint and DFlash2 checks still need a run.

Igor Lessio - AIFLOW LABS

CodingEmil and others added 3 commits November 20, 2026 19:43
…nit Macs take TF_QWEN27_METAL_KV_PAGES

A 512-key prompt chunk spans exactly two KV pages, so the paged prompt kernels take both page bases
before their 32-key steps instead of reading the page table at every step. The M5 refusal of the flag
goes, now that the paged kernels have run on an M5 Ultra (part of ashhart#1293).
…unch limits

Answer the requested M5 and simdgroup contract coverage with shuffled pages, page and chunk seams, partial pages, independent nonfinite and stress references, contiguous byte replacements, and real source faults for gates 1 and 3. Keep production arithmetic unchanged.
@ashhart
ashhart merged commit d43a895 into ashhart:main Nov 20, 2026
ashhart added a commit that referenced this pull request Nov 20, 2026
…mits

Answer the requested M5 and simdgroup contract coverage with shuffled pages, page and chunk seams, partial pages, independent nonfinite and stress references, contiguous byte replacements, and real source faults for gates 1 and 3. Keep production arithmetic unchanged.
@ashhart

ashhart commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you @CodingEmil. 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 added a commit to gilby/TensorFold that referenced this pull request Nov 20, 2026
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