Skip to content

qwen27 metal: draft SenseNova-Loop-9B with published mtp.safetensors (MTP) - #3868

Merged
ashhart merged 16 commits into
ashhart:mainfrom
gilby:sensenova-loop-metal-mtp
Nov 25, 2026
Merged

ashhart merged 16 commits into
ashhart:mainfrom
gilby:sensenova-loop-metal-mtp

Conversation

@gilby

@gilby gilby commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Adds native Multi-Token Prediction (MTP) speculative drafting on Metal for SenseNova-Loop-9B using the checkpoint's published mtp.safetensors head (TF_MTP_DRAFTS, TF_MTP_BITS, TF_MTP_PLAIN_GUARD), fusing drafter prompt absorb and hold rounds (draft_round), using keysOnly intermediate prompt-chunk absorb, and batching active streams (sn_mtp_batch) with identical token output (token_sha) to --no-drafts.

Rebased onto #3859 (b61c6f90b3) and resolved zig/src/native/qwen27_served.zig cleanly by preserving #3859's q.simd_attention.sharedPrefix(model.device.tensorUnits()) memory sizing while threading o.mtp_dir, mtp_ring, tap_count = 1, lanes.drafted.Drafted, and TF_MTP_* options; added 9d7f872be9 (@hasField(@TypeOf(h.*), "mtp")) in zig/src/families/qwen27/residency.zig so shared residency tests compile cleanly.

Checks on this head

  • Head commit: 9d7f872be9e73c777c5aaa75913eb29be1814a6c
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: b61c6f90b3 (7380 files, 167 problems) -> 9d7f872be9 (7387 files, 167 problems), 0 new (tools/zig/lean_new.py and tools/pr_check.py --base b61c6f90b3 --pr 3868: 0 error(s), 0 warning(s)).
  • Tests run, with the command and pass, fail and skip counts:
    • zig fmt --check and git diff --check b61c6f90b3..HEAD: passed.
    • zig build -j6 dev-tools native -Dcpu=apple_m1: exited 0.
    • zig build -j6 test-metal-residency-owners-host -Dcpu=apple_m1 on 9d7f872be9: 76/78 passed (remaining 2 crashes are pre-existing upstream 1b394565cc GLM e.dflash undefined field).
    • Targeted host/golden/contract compile & host checks ran before 9d7f872be9 (a6aa74047b: 36/39 steps passed including test-golden 324/337 + 69/69, test-metal-launch-map-host 1163 registered launches, 2 controls, 0 missing, test-qwen27, test-kernel-contract); 9d7f872be9 only changed the @hasField guard in zig/src/families/qwen27/residency.zig and re-runs full host CI upon push.
    • Remote serving receipts on 9d7f872be9 (SenseNova-Loop-9B-AWQ-g128, --parallel 4 --context 36864 --prompt-cache-gib 0, TF_MTP_BITS=4 TF_MTP_DRAFTS=3 TF_MTP_PLAIN_GUARD=1) across Apple M5 Ultra (bench_openai.py, bench_concurrent.py --alone --serial 8/8 cells 0 failed/0 unmeasured/0 unequal, bench_cache.py --require-cold) and Apple M5 Pro (bench_openai.py & bench_cache.py --require-cold): passed (see Receipt below).
  • CI: Triggered upon push of 9d7f872be9 (pr-3868-rebased).
  • Not run, and why: Standalone local GPU tf-sensenova-oracle was not re-run after rebase onto #3859 (No GPU CI runs full host checks; stacked PRs validate q27/sensenova tf-kernel-contract on Apple M3 Ultra and Apple M5 Max).

Receipt

  • Main against branch:
    • Apple M5 Ultra (SenseNova-Loop-9B-AWQ-g128, 9d7f872be9, --parallel 4 --context 36864 --prompt-cache-gib 0, TF_MTP_BITS=4 TF_MTP_DRAFTS=3 TF_MTP_PLAIN_GUARD=1): startup info: sensenova mtp: bits=4 depth=3 abs_rows=2048 step_ms=1.86 plain_guard=true slot_bytes=151101504; drafts: MTP + copies (18.8 GiB vs --no-drafts 14.9 GiB).
    • tools/bench_concurrent.py --levels 1,4 --temperatures 0,1.0 --tokens 128 --reps 2 --alone --serial: 0 failed, 0 unmeasured, 0 unequal across all 8 cells (alone 2/2 at c=1 and 8/8 at c=4; serial 1/1 at T=0 and 4/4 at T=1.0; plain_drafted: 0, vacuous: none):
      • code T=0: c=1 121.2 tok/s (tok/chunk 2.844, sha=339b36c97870), c=4 agg 342.8 tok/s (per 92.4, steady 388.6, accepted 83)
      • chat T=0: c=1 80.4 tok/s (tok/chunk 1.882, sha=791252c1c8f1), c=4 agg 239.3 tok/s (per 63.8, steady 273.2, accepted 60)
      • code T=1.0: c=1 109.3 tok/s (tok/chunk 2.51), c=4 agg 294.4 tok/s (per 79.5, steady 349.4, accepted 310)
      • chat T=1.0: c=1 85.8 tok/s (tok/chunk 2.0), c=4 agg 237.5 tok/s (per 68.9, steady 283.8, accepted 252)
    • Apple M5 Pro (64 GiB, macOS 27.2.1, SenseNova-Loop-9B-AWQ-g128, 9d7f872be9): bench_cache.py --require-cold (2,571 prompt tok, qualified=True, token_sha=eaa68149fc60, first TTFT 3.421s, rep [3.330, 3.348]s, decode 50.34 / 48.45 / 48.75 tok/s); bench_openai.py fib T=0.0 50.75 tok/s (sha=2f1d93533424), T=1.0 47.93 tok/s, chat T=0.0 35.67 tok/s (sha=c011df2b114c), T=1.0 36.93 tok/s.
  • Boxes it reaches: Apple Silicon Metal (qwen27 lane backend on M-series Macs).
  • New switch? Default, accepted values, profile row:
    • TF_MTP_DRAFTS: whole u32 clamped to 1..window-1 (0 clamps to 1), unset defaults to 3 on SenseNova-Loop MTP.
    • TF_MTP_PLAIN_GUARD: 0 or 1, unset/blank defaults to 1.
    • TF_MTP_BITS: 4 or 16, unset/blank defaults to 4 (target verification always runs in checkpoint weights).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: TensorFold 9d7f872be9e73c777c5aaa75913eb29be1814a6c, Zig 0.17.0, Apple M5 Ultra & Apple M5 Pro (64 GiB, macOS 27.2.1), SenseNova-Loop-9B-AWQ-g128 (sensenova/SenseNova-Loop-9B derivative).
  • Paths and platforms the change reaches, which I ran and which I could not: Native Metal qwen27 serving, MTP speculative drafting, and shared residency helpers (macOS host checks plus Apple M5 Ultra and Apple M5 Pro serving receipts completed; non-Metal backends untouched).
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    • tools/bench_openai.py --tokens 256 --reps 3 (--no-drafts vs MTP on Apple M5 Ultra): all 12/12 token_sha and output_sha256 pairs identical (fibonacci-raw T=0.0: 2f333243860d x3; fibonacci-raw T=1.0: ['1a0e8659a9ca', '1ecfdd251491', 'bfdf5c973665']; gpu-chat-no-think T=0.0: deb79c46255a x3; gpu-chat-no-think T=1.0: ['650226c66939', 'e63d8ce93536', 'bb2767d5fc07']).
    • tools/bench_concurrent.py: 0 unequal across all 8 cells (alone 2/2 at c=1, 8/8 at c=4; serial 1/1 at T=0, 4/4 at T=1.0).
    • tools/bench_cache.py --require-cold: identical token_sha=eaa68149fc60 across --no-drafts and MTP (cached_tokens=0 cold on all 5 rows).
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): Apple M5 Ultra --no-drafts (baseline #3859 behavior) vs MTP comparison on SenseNova-Loop-9B-AWQ-g128 (9d7f872be9) above.
  • Decode speed before and after, from tools/bench_openai.py:
    • Apple M5 Ultra (SenseNova-Loop-9B-AWQ-g128, --tokens 256 --reps 3, --no-drafts -> MTP):
      • fibonacci-raw T=0.0: 54.46 -> 117.51 tok/s (2.16x, tok/chunk 2.783, ttft 0.0580s -> 0.0608s)
      • fibonacci-raw T=1.0: 54.97 -> 114.04 tok/s (2.07x, tok/chunk 2.667, ttft 0.0614s -> 0.0592s)
      • gpu-chat-no-think (thinking=off) T=0.0: 54.32 -> 82.58 tok/s (1.52x, tok/chunk 1.954, ttft 0.0578s -> 0.0583s)
      • gpu-chat-no-think (thinking=off) T=1.0: 55.59 -> 79.48 tok/s (1.43x, tok/chunk 1.855, ttft 0.0608s -> 0.0600s)
    • Apple M5 Pro (SenseNova-Loop-9B-AWQ-g128, MTP): fib T=0.0 50.75 tok/s (T=1.0 47.93), chat T=0.0 35.67 tok/s (T=1.0 36.93).
  • 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:
    • Apple M5 Ultra (SenseNova-Loop-9B-AWQ-g128, 2,571 prompt tokens, tokens=64, repeats=2, streams=2, cached_tokens=0 cold on all 5 rows):
      • --no-drafts: qualified: true, first ttft 0.9855s (decode 53.94 tok/s), repeated 0.9328s / 0.9332s (decode 53.88 / 53.94 tok/s)
      • MTP: qualified: true, first ttft 1.0012s (decode 105.63 tok/s), repeated 0.9527s / 0.9566s (decode 105.47 / 106.01 tok/s)
    • Apple M5 Pro (SenseNova-Loop-9B-AWQ-g128, MTP): first ttft 3.421s, repeated 3.330s / 3.348s (qualified: true).
  • Licence of any new weights or drafter, or "n/a": Apache-2.0 (sensenova/SenseNova-Loop-9B published mtp.safetensors).

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.

  • 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

@ashhart

ashhart commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby for the MTP implementation and the direct BF16/AWQ evidence on this head. The targeted host suites, oracle checks, serial/single-request equality, and M5/M3 kernel-contract controls cover the new drafting and batched-round paths. The MTP increment reuses the parent stack's kernels rather than adding new ones.

The remaining blocker is application to current main. The supplied diff has nine rejected hunks in CHANGELOG.md, docs/environment.md, docs/optimization-ledger.md, docs/recipes/README.md, zig/build/qwen27.zig, zig/src/native/qwen27_served.zig, zig/src/platform/environment.zig, and zig/src/platform/environment_test.zig.

Please rebase the stack while preserving both the current-main additions and the SenseNova MTP changes, then refresh the focused host checks and exactness evidence on the resulting head. The reviewed source otherwise has no remaining finding in this pass.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked exact head cd992f1da against main 210e3b0c5. The current trial merge has 12 conflicted files. Nine are already present when merging parent #3859 by itself. The three specific to this increment are docs/environment.md, qwen27_served.zig and environment_test.zig.

The important code hunk is prompt admission in qwen27_served.zig. The combined call to reserve.fit needs main's sharedPrefix result plus this PR's MTP ring byte count and single tap when MTP is active. The environment conflict can retain main's added entries alongside the new MTP defaults and invalid-value cases.

On this PR head, zig build test-sensenova-loop-config-host test-qwen27-equality-host test-qwen27 test-environment-host -Dcpu=apple_m1 -j6 --summary all on an M5 Max with Zig 0.17.0 passed 215 tests with three skips. This is host coverage of the existing head. The rebased Metal oracle and served exactness checks are still open.

Igor Lessio - AIFLOW LABS

gilby added a commit to gilby/TensorFold that referenced this pull request Nov 21, 2026
@gilby
gilby force-pushed the sensenova-loop-metal-mtp branch from cd992f1 to 9d7f872 Compare November 21, 2026 19:21
@gilby
gilby marked this pull request as ready for review November 21, 2026 19:21
ashhart pushed a commit to gilby/TensorFold that referenced this pull request Nov 21, 2026
ashhart pushed a commit to gilby/TensorFold that referenced this pull request Nov 21, 2026
@AIFlowML

Copy link
Copy Markdown
Contributor

I rechecked the published tip 9d7f872be against main 308d5e1fd. It has nine conflict paths. Four also conflict when trial-merging the published #3859 parent.

The rebased stack carried by #4415 gives a smaller update path. Its MTP slice ends at b3bf97d52, after the #3859 slice at e44562a32 and before the MLX-affine work. The MTP slice trial-merged cleanly into the same main. git diff --cached --check passed, and on M5 Max zig build test-sensenova-loop-config-host test-qwen27 test-metal-residency-owners-host -Dcpu=apple_m1 -j4 --summary all passed 56/56 steps, with 284 tests passed, five skipped and zero failures.

The original published branch was the conflict check. The rebased slice was the host test. Checkpoint serving and token parity were outside this run.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart force-pushed the sensenova-loop-metal-mtp branch from 9d7f872 to f835eb8 Compare November 22, 2026 00:38
@AIFlowML

Copy link
Copy Markdown
Contributor

The refreshed f835eb815 head applies cleanly to main 1eed88bb0. On M5 Max with Zig 0.17.0, test-qwen27 passed 203 tests with five skips, and all 78 residency-owner tests passed.

zig build test-qwen27 test-metal-residency-owners-host test-draft-metal-host \
  -Dcpu=apple_m1 -j4 --summary all

The draft metadata suite hits the same Qwen27 assertion on main and on the trial. I isolated it to the fixture in zig/src/native/draft_sources_metal_test.zig: it creates b: q.Metal = undefined and calls facts() without writing b.external. Initializing b.external = false passes all 12 cases on main. Initializing that field and the new b.features_state = .{} on the trial passes all 13 cases. Both were test-only local edits.

This covers the revised host and residency paths. Checkpoint token parity and throughput remain device work.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby for adding MTP drafting to SenseNova-Loop. One small fix: TF_MTP_DRAFTS moved from the CUDA scope to the common scope, so a build that never reads it, such as XPU, now refuses to start on a bad value where main ignored it. Metal does read it now, so checking it there is right. Could you limit the rule to CUDA and Metal, and add a test that an XPU build still ignores it?

  • Synthena

ashhart pushed a commit to gilby/TensorFold that referenced this pull request Nov 23, 2026
ashhart pushed a commit to gilby/TensorFold that referenced this pull request Nov 23, 2026
@gilby

gilby commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in commit c0653a7f66:

  • Scoped TF_MTP_DRAFTS to .cuda and .metal via Rule.also (.scope = .cuda, .also = .metal) with Rule.matches() / Flags.includesRule() in zig/src/platform/environment.zig, restoring XPU startup behavior (ignoring TF_MTP_DRAFTS) while keeping validation active on both CUDA and Metal.
  • Added regression environment: TF_MTP_DRAFTS is checked on CUDA and Metal and ignored on XPU in zig/src/platform/environment_test.zig.

Verified on this commit: zig build test-environment-host --summary all (33/33 tests passed), zig fmt --check build.zig zig, git diff --check origin/main...HEAD, python3 tools/pr_check.py --base origin/main (0 error(s)), and python3 tools/zig/lean_check.py (165 vs 165, 0 new).

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The fix on c0653a7f66 is right: TF_MTP_DRAFTS is now checked on CUDA and Metal through the rule's second scope, both the startup check and the scope check use it, and the new test shows an XPU build still ignores the value. That closes the point from our last review.

The overlaps with main are ours to sort out when we rebase.

  • Synthena

@ashhart
ashhart force-pushed the sensenova-loop-metal-mtp branch from c0653a7 to 3f83cb7 Compare November 24, 2026 10:13
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby for bringing both onto main yourself. We read the range-diff against the head we reviewed. The conflict work folds main's tied-head opt-in into the parse options and widths, and keeps the stage-plane record on the default path. The rest is context and the 600-line split. This one is in our landing queue now, and #4415 follows once it lands.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

A correction to my note above, @gilby: we made the rebase onto main, not you, so there was nothing for you to do there. The rest stands: it's in our landing queue, and #4415 follows once it lands.

  • Synthena

gilby and others added 16 commits November 25, 2026 00:53
The net change of ashhart#3859 at c07419f over its merge base 6858510,
carried as one commit because four of the branch's merge commits hold changes of their own.
Main 881e224 added the prompt-tail slices retained by the rebase. Move the unchanged round fingerprint into its own module so the combined runner remains below 600 lines.
Main moved keysForward to prompt_tail.zig. This PR's copy passed the shifted norm and YaRN rope, so the moved one must too. Both default to the plain path, so other Qwen models are unchanged.
The net change of ashhart#3859 at c07419f over its merge base 6858510,
carried as one commit because four of the branch's merge commits hold changes of their own.
… each with its own host field and tap slots

Main's 4f8b193/090c9c4a9 line serves Qwen3.8-27B's MTP head under --mtp-drafts (Host.mtp: qwen27_mtp.zig's Mtp,
its taps through qwen27_features.zig's prepare and rows in the lane backend's table). This PR's head for
SenseNova-Loop came with a Host.mtp of its own type and the same two table slots, plus prefill_features.

- qwen27_served: the PR's head is Host.sn_mtp; it loads only when --mtp-drafts is off (so at most one head drafts),
  sets the backend's sn_features before its wrapper takes the backend's table, and frees as the PR placed it.
- qwen27_lanes: Metal.sn_features picks the table. With it, the PR's slots (sensenova_features.zig's
  prefill_features, prepare_features and features); without it, main's table unchanged, so a Qwen3.8-27B prompt under
  --mtp-drafts keeps main's prompt pass (Drafted streams prompts through prefill_features whenever one is set).
- residency: the PR's three resident-set lines name sn_mtp, the type whose buffers they add.

Main's Qwen3.8-27B MTP, DFlash2 and plain paths keep main's code; SenseNova-Loop's keeps the PR's.
…main

Main's growth beside this series' SenseNova-Loop lane changes put the file
at 605 lines. The tail encoder's choice becomes one if/else line and the
argmax tail's body one call; no behaviour changes.
…s solo prompt rows

attention.zig, attention_plan.zig, decode_round.zig and qwen27_slots.zig now carry
main's solo prompt rows with this stack's SenseNova-Loop changes; their pins are
regenerated from the merged files, main's notes kept.
@ashhart
ashhart force-pushed the sensenova-loop-metal-mtp branch from 0351ce9 to 4164b9b Compare November 25, 2026 02:26
@ashhart
ashhart merged commit 94e0f2d into ashhart:main Nov 25, 2026
12 checks passed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. 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.

akol1 added a commit to akol1/TensorFold that referenced this pull request Nov 25, 2026
… pinned main's slots; this PR adds rank 1's begin and the chunked-pass announce, hash refreshed with provenance)
akol1 added a commit to akol1/TensorFold that referenced this pull request Nov 25, 2026
… pinned main's slots; this PR adds rank 1's begin and the chunked-pass announce, hash refreshed with provenance)
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.

SenseNova-Loop-9B (sensenova_loop) on Metal: M5 Ultra and M3 Ultra, BF16 and AWQ 4-bit, 512K YaRN, vision and MTP

3 participants