Skip to content

qwen27 metal: support SenseNova-Loop MLX-affine g64 checkpoints - #4415

Merged
ashhart merged 5 commits into
ashhart:mainfrom
gilby:sensenova-loop-mlx-affine
Nov 25, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
gilby:sensenova-loop-mlx-affine

Conversation

@gilby

@gilby gilby commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What this changes

Adds Metal loading, execution-step physical layer aliasing, and speculative MTP drafting support for SenseNova-Loop-9B MLX-affine group-64 checkpoints (mlx-q4g64, mlx-q5g64, mlx-q6g64, mlx-q8g64) with either BF16 or quantized embed_tokens. zig/src/families/qwen27/loop_affine.zig loads unique physical layers, applies tensor-unit laneOrder strictly to physical matrices before aliasing the 56 runtime execution steps, and adds host width-resolution and admission tests for 4/5/6/8-bit g64 configs (pure GPU path; no Neural Engine files or hooks).

Checks on this head

  • Head commit: 552de160c4f708b7f3ee00f4ccac7a54a7b701a6
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 9d7f872be9 (7387 files, 167 problems) -> 552de160c4 (7388 files, 167 problems), 0 new (tools/zig/lean_new.py and tools/pr_check.py --base 9d7f872be9: 0 error(s), 1 warning(s)).
  • Tests run, with the command and pass, fail and skip counts:
    • zig fmt --check and git diff --check 9d7f872be9..HEAD: passed.
    • zig build -j6 test-qwen27 dev-tools native -Dcpu=apple_m1: exited 0 (includes repackedAffine and admit resolves and admits SenseNova-Loop MLX-affine g64 4/5/6/8-bit configs).
    • Host check suite (test-metal-launch-map-host: 1163 registered launches, 2 controls, 0 missing, plus qwen27/environment/residency host steps): exited 0.
    • Remote serving receipt on Apple M5 Pro (64 GiB, macOS 27.2.1, --prompt-cache-gib 0): verified across SenseNova-Loop-9B-AWQ-g128 (#3868 parity), SenseNova-Loop-9B-MLX-Q4-hq, and SenseNova-Loop-9B-MLX-Q8 (see Receipt below).
  • CI: Triggered upon push of 552de160c4.
  • Not run, and why: Multi-chip oracle/bench matrix outside Apple M5 Pro AWQ-g128, MLX-Q4-hq, and MLX-Q8; non-Metal backends untouched.

Receipt

  • Main against branch:
    • Apple M5 Pro (64 GiB, macOS 27.2.1, --prompt-cache-gib 0), #3868 (9d7f872be9) vs PR docs: show model speeds on TensorFold hero #4 (552de160c4) on SenseNova-Loop-9B-AWQ-g128:
      • bench_cache.py --require-cold (2,571 prompt tokens, qualified=True, all 5 rows cold): 100% exact token_sha=eaa68149fc60 & output_sha256=bc28c652a031 match (first TTFT 3.421s -> 3.511s, rep [3.330, 3.348]s -> [3.395, 3.413]s, decode 50.34 -> 48.61 tok/s, rep 48.45 -> 48.66 & 48.75 -> 48.48 tok/s).
      • bench_openai.py: 20/20 exact token_sha & output_sha256 match across fibonacci-raw (T=0.0: 50.75 -> 50.67 tok/s, sha=2f1d93533424; T=1.0: 47.93 -> 47.08 tok/s) and gpu-chat-no-think (T=0.0: 35.67 -> 35.66 tok/s, sha=c011df2b114c; T=1.0: 36.93 -> 36.89 tok/s).
    • Apple M5 Pro (64 GiB, macOS 27.2.1), SenseNova-Loop-9B-MLX-Q4-hq (PR docs: show model speeds on TensorFold hero #4):
      • 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 & 8/8, serial 1/1 & 4/4, plain_drafted=0): code T=0 c=1 41.7 tok/s / c=4 agg 140.4 tok/s (per 36.1, steady 149.7, accepted=77, sha=b5eabd2f8348), chat T=0 c=1 31.7 tok/s / c=4 agg 108.0 tok/s (per 27.9, steady 115.0, accepted=61, sha=ce0afb1a0d65), code T=1.0 c=1 35.2 tok/s / c=4 agg 119.6 tok/s, chat T=1.0 c=1 30.3 tok/s / c=4 agg 102.7 tok/s.
    • Apple M5 Pro (64 GiB, macOS 27.2.1), SenseNova-Loop-9B-MLX-Q8 (PR docs: show model speeds on TensorFold hero #4):
      • bench_concurrent.py --levels 1,4 --temperatures 0 --tokens 128 --reps 2 --alone --serial: 0 failed/unequal (alone 2/2 & 8/8, serial 1/1): code c=1 36.0 tok/s / c=4 agg 114.4 tok/s (per 29.4, steady 121.6), chat c=1 25.1 tok/s / c=4 agg 88.3 tok/s (per 22.9, steady 94.9).
  • Boxes it reaches: Apple Silicon Metal (qwen27 lane backend on M-series Macs).
  • New switch? Default, accepted values, profile row: None (uses existing checkpoint quantization config metadata and #3868 MTP env vars).
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: TensorFold 552de160c4f708b7f3ee00f4ccac7a54a7b701a6, Zig 0.17.0, Apple M5 Pro (64 GiB, macOS 27.2.1), checkpoints SenseNova-Loop-9B-AWQ-g128, SenseNova-Loop-9B-MLX-Q4-hq, and SenseNova-Loop-9B-MLX-Q8 (sensenova/SenseNova-Loop-9B derivatives).
  • Paths and platforms the change reaches, which I ran and which I could not: Native Metal qwen27 SenseNova-Loop MLX-affine g64 loader, MTP scratch/batch sizing, and oracle harness (macOS build/host checks plus Apple M5 Pro serving across AWQ-g128, MLX-Q4-hq, and MLX-Q8 completed; MLX g64 format unsupported on #3868 before this PR).
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    • SenseNova-Loop-9B-AWQ-g128 (#3868 vs PR docs: show model speeds on TensorFold hero #4): 20/20 bench_openai.py and 5/5 bench_cache.py --require-cold exact token_sha & output_sha256 match (token_sha=eaa68149fc60, output_sha256=bc28c652a031; fib=2f1d93533424, chat=c011df2b114c).
    • SenseNova-Loop-9B-MLX-Q4-hq: bench_openai.py --no-drafts greedy vs MTP draft-on 100% token_sha match (fib=21e104d752ff, chat=6d8124a912ce); bench_concurrent.py 0 failed, 0 unmeasured, 0 unequal across all 8 cells (alone 2/2 & 8/8, serial 1/1 & 4/4, plain_drafted=0).
    • SenseNova-Loop-9B-MLX-Q8: bench_cache.py (token_sha=eaa68149fc60), bench_openai.py (fib=67c0a36e36a0, chat=7f52b1da112e), and bench_concurrent.py (0 failed/unequal across c=1/c=4).
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): #3868 (9d7f872be9) vs PR docs: show model speeds on TensorFold hero #4 (552de160c4) on SenseNova-Loop-9B-AWQ-g128 (Apple M5 Pro) shows 100% exact token parity above; MLX-Q4-hq and MLX-Q8 checkpoints are newly enabled by this PR.
  • Decode speed before and after, from tools/bench_openai.py (Apple M5 Pro, 64 GiB):
    • SenseNova-Loop-9B-AWQ-g128 (#3868 -> PR #4): fib T=0.0 50.75 -> 50.67 tok/s (sha=2f1d93533424), T=1.0 47.93 -> 47.08 tok/s; chat T=0.0 35.67 -> 35.66 tok/s (sha=c011df2b114c), T=1.0 36.93 -> 36.89 tok/s.
    • SenseNova-Loop-9B-MLX-Q4-hq (PR #4, --no-drafts -> MTP): fib T=0.0 21.82 -> 38.02 tok/s (1.74x), chat T=0.0 21.80 -> 31.21 tok/s (1.43x); T=1.0 med fib 39.35 tok/s, chat 28.17 tok/s.
    • SenseNova-Loop-9B-MLX-Q8 (PR #4, MTP): fib T=0.0 33.69 tok/s, chat T=0.0 23.15 tok/s; T=1.0 fib 30.69 tok/s, chat 22.42 tok/s.
  • 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 Pro, 2,571 prompt tokens, qualified=True):
    • SenseNova-Loop-9B-AWQ-g128 (#3868 -> PR #4, all 5 rows cold): first TTFT 3.421s -> 3.511s, rep [3.330, 3.348]s -> [3.395, 3.413]s, decode 50.34 -> 48.61 tok/s (rep 48.45 -> 48.66 & 48.75 -> 48.48 tok/s).
    • SenseNova-Loop-9B-MLX-Q4-hq (PR #4): cold TTFT 4.846s (first), 4.717s / 4.677s (rep), decode 39.99 / 42.51 / 41.28 tok/s, concurrent streams=2 makespan 10.987s (token_sha=eaa68149fc60).
    • SenseNova-Loop-9B-MLX-Q8 (PR #4): cold TTFT 7.628s / 7.506s / 7.525s, decode 33.65 / 33.16 / 32.92 tok/s (token_sha=eaa68149fc60).
  • Licence of any new weights or drafter, or "n/a": Apache-2.0 (sensenova/SenseNova-Loop-9B quantized weights).

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

@AIFlowML

AIFlowML commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

The published head 5f664cef7, which includes the lane MTP residency and prompt-tail follow-up, trial-merges cleanly into main 308d5e1fd.

On M5 Max (macOS 27.2, Zig 0.17.0), zig build test-qwen27 test-metal-residency-owners-host -Dcpu=apple_m1 -j4 --summary all passed 53/53 steps, with 285 tests passed, five skipped, and zero failures. git diff --cached --check also passed on the trial. This validates the newer head through the family and residency targets. Checkpoint serving and the device oracle matrix were outside this run.

The trial includes the rebased #3859 and #3868 slices carried by #4415. The published tips of those two parent PRs still need to be brought onto that stack. Standalone parent evidence

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. Three correctness fixes are needed on the current head:

  1. Mixed affine/BF16 checkpoints are refused before loading. zig/src/families/qwen27/widths.zig:112,145-150,190-192 sends an explicit unquantized embedding override through one, which turns its null spec into error.UnqualifiedQwenFormat. This contradicts the stated BF16-embedding support; the BF16 loader already exists at zig/src/families/qwen27/loop_affine.zig:149-155. Resolve the embedding separately and map a null spec to BF16 only for looped models. Keep layers/head strict. Add an admission regression with affine layers/head and an unquantized embedding override; it currently fails before the assertions.

  2. Failed MTP initialization releases the same weights twice. zig/src/families/qwen27/sn_mtp.zig:86-96 registers errdefer w_owned.deinit(), while Mtp.init at lines 80-83 and the served caller at zig/src/native/qwen27_served.zig:318-324,368-370 already release their shallow copy on failure. Delete only the inner error destructor at sn_mtp.zig:91. Add a recording-device test that injects a later constructor failure and checks that every allocation is released exactly once and nothing remains live.

  3. Some accepted F32 AWQ scales reconstruct the wrong weights. zig/src/families/sensenova_loop/awq.zig:133-135,212-217 rounds -zero * scale to F32 and all consumers then calculate q * scale + bias. For scale 0x3f800001, zero point 15 and code 15, the raw value is zero but packed reconstruction is -2^-23. Keep the small central fix: compute the bias in F64 and reject it if an F32 round trip is not exact, then propagate that error from losslessMeta through packing. Add fail-before packParts cases for both layouts using those values. The existing F16/BF16 cases must remain accepted.

These are source-derived failures; the new regression tests need to exercise the actual loader, constructor and packing paths rather than only their expected test helpers.

  • Synthena

@gilby

gilby commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Fixed all three items in commit 2a06988617:

  1. Mixed affine/BF16 embeddings admission (zig/src/families/qwen27/widths.zig): Widths.resolve now resolves model.embed_tokens separately and maps a null override spec (false or {}) to .bf16 when c.looped is true, while keeping all layers, lm_head, and non-looped embeddings strict. Added admission regression looped affine resolves explicit unquantized embeddings as bf16 while keeping strict head/layer checks.
  2. Failed MTP init double-free (zig/src/families/qwen27/sn_mtp.zig): Removed the redundant inner errdefer w_owned.deinit() in initFromWeights so caller error cleanup (Mtp.init and qwen27_served.zig) releases the weights once. Added recording-device regression residency owner Qwen27 MTP: failed initFromWeights leaves caller weights owned once and frees internal buffers (zig/residency_qwen27_mtp_tests.zig, with inert Library/Pipeline/CompileOptions stubs in zig/residency_recording_metal.zig) injecting buffer failures across early/mid/late constructor allocations and verifying record.bad == 0 and record.allFreed().
  3. Lossy F32 AWQ scales (zig/src/families/sensenova_loop/awq.zig): losslessMeta now computes -zero * scale in f64, rejects non-finite scales or any bias that does not round-trip bit-exact in f32 with error.LossyAwqScaleBias, and propagates the error through packParts. Added test_awq: lossy F32 scales fail losslessMeta and packParts while exact scales reconstruct zero testing scale 0x3f800001, zp = 15, q = 15 for both .m5_tile and .m3_row while preserving exact F16/BF16/F32 cases.

Verified on this commit:

  • zig build test-metal-residency-owners-host --summary all (81/81 tests passed)
  • zig build test-qwen27 --summary all (186/191 tests passed, 5 skipped)
  • zig test zig/src/families/sensenova_loop/awq.zig --test-filter "test_awq:" (All 4 tests passed)
  • zig build test-sensenova-loop-config-host check-qwen27-speed-host compile-metal-residency-host --summary all (69/69 steps succeeded)
  • 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. All three are fixed on 2a06988617:

  1. Mixed embeddings: model.embed_tokens is resolved on its own, an unquantized override maps to BF16 only on looped models, and layers, lm_head and non-looped embeddings stay strict. The new test runs the real resolveModel and admit, and checks that a non-looped config and an unquantized head are still refused.
  2. MTP init: the inner error destructor is gone, so the caller releases the weights once. The recording-device test fails the constructor at early, middle and late allocations and checks that everything is freed exactly once.
  3. F32 AWQ scales: the bias is computed in F64 and refused unless it round-trips exactly in F32, and packParts passes the error up. The 0x3f800001 / zero point 15 case fails for both layouts, and exact scales still reconstruct zero.

That closes the three points from our last review. Since this sits on #3868, the two land in order, and the overlaps with main are ours to sort out.

  • Synthena

@ashhart
ashhart force-pushed the sensenova-loop-mlx-affine branch 4 times, most recently from 6130ffe to d8f7793 Compare November 25, 2026 03:01
@ashhart
ashhart force-pushed the sensenova-loop-mlx-affine branch from d8f7793 to 06af4b5 Compare November 25, 2026 10:01
@ashhart
ashhart merged commit 7998c5a into ashhart:main Nov 25, 2026
2 of 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.

ashhart added a commit to BlivionIaG/TensorFold that referenced this pull request Nov 25, 2026
ashhart added a commit that referenced this pull request Nov 25, 2026
ashhart added a commit to plotarmordev/TensorFold that referenced this pull request Nov 25, 2026
ashhart added a commit that referenced this pull request Nov 25, 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.

3 participants