Skip to content

qwen27: Qwen3.6-35B-A3B on Metal on the routed path, with staged references against float64 - #3239

Merged
ashhart merged 13 commits into
ashhart:mainfrom
cshintov:feature/qwen36-moe-on-main
Nov 24, 2026
Merged

ashhart merged 13 commits into
ashhart:mainfrom
cshintov:feature/qwen36-moe-on-main

Conversation

@cshintov

@cshintov cshintov commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

This builds Qwen3.6-35B-A3B (text, no MTP, no images) on main's routed expert path (#1125), as asked when #1009 closed. Slice 1 of #931.

What this changes

  • Admits the 35B-A3B on the routed path (qwen27: Qwen3.6-35B-A3B on #1125's routed path...). model.supported takes its shape, and the attention Geometry takes 16 query heads over 2 (a group of 8) on one Mac. A rank of 8 over 1 is refused. Config tests cover the shape and both refusals.
  • The routed block's contract cases at the 35B's served widths: 512 experts' rows over a hidden width of 2,048.
  • qwen27 attention's decode and prompt row cases at 16 over 2, beside main's 24 over 4 and 32 over 2.
  • docs/kernel-contract.md: one paragraph on the new widths.
  • Staged references (tf-qwen27-run --stages DIR). It writes the residual stream after the embedding, after each layer's mixer and MLP, then final and the logits.
    • final is the normed rows the head reads, not the residual. A fixture with a non-unit final norm refuses a residual labelled final.
    • tools/zig/qwen36_stage_ref.py measures each stage against a float64 forward of the checkpoint's exact weights. It reports the local error (each stage fed the engine's own input) and the chained error (from the token ids).
    • A headless prompt chunk records 0 logit rows. While stages record, the last layer is treated as tapped in main's prompt tail facts, so the keys-only last layer and the prompt tail stay off: the recorded stream and final are real rows.
    • The recorder is built only into tf-qwen27-run. Every other build, the served engine included, is unchanged by it.
  • --vision refuses the 35B-A3B by its shape before the tower loads, since its image path is not qualified.
    • Off by default. A round that isn't one stream's chain is refused before it touches any state.
  • Lean: comments on one line within 120 columns.

Design

The 35B-A3B is the same family as Ornith-1.5-122B with other widths: 256 experts of 512 and a shared expert of 512 over a hidden width of 2,048, and 16 query heads over 2. So it adds no kernels and no loader. The router runs on its stored 8-bit g64 rows, stacked with the shared gate.

Alternatives rejected:

  • Qwen3.6-35B-A3B on Metal: routed experts in the 27B's engine (slice 1 of #931) #1009's own router and expert kernels: duplicate paths that main's routed path already qualifies.
  • A second family directory: main keeps routed checkpoints in the qwen27 engine, and this follows it.
  • Staged references through the forward pass's single hook pointer: the hook now carries its own planes and glue, so the runner needs no forwarding function.
  • Recording stages beside the keys-only last layer: its rows aren't written, so the recorder would read stale bytes.

Receipts

Host and contract, current head 0fb21b0ab (on main 4f3f564c8), M4 Pro (no tensor units), macOS 26.5:

  • zig build test-qwen27 test-kernel-contract -Dfixed-cpu: 82 of 82 steps, 218 of 218 tests pass.
  • zig build native tf-qwen27-run test-qwen35-rules-host test-qwen35-layers-host -Dfixed-cpu: all steps pass.
  • tf-kernel-contract --only qwen27: 36 kernels pass gates 1-4, 0 fail, 0 known findings; 4 controls red as planted, 0 missed; 0 limit problems.
  • tools/pr_check.py --base upstream/main: 0 errors.
  • On Apple M5 Max, zig build test-qwen27 test-kernel-contract -Dcpu=apple_m1 passes on this head (82 of 82 steps, 413 tests, 3 skips), from @AIFlowML's run below.

Real checkpoint (mlx-community/Qwen3.6-35B-A3B-4bit at 38740b84), M4 Pro, head cc5048b6c on main aaac299d1, before the last two rebases. tf-qwen27-run --max-tokens 64 --chunk 128 --compare-chunk 16 --seams 16, greedy:

Prompt Prompt tokens Decoded Prompt state Decode state Seams
short 30 64 byte-equal byte-equal 1/1 equal
code 34 64 byte-equal byte-equal 2/2 equal
long 492 64 byte-equal byte-equal 30/30 equal

All three replies are token for token the same on cc5048b6c, on the same commits over main c34ff562c, and before that rebase. The staged references below were measured on the run before that rebase. The real-checkpoint run was not repeated on 0fb21b0ab.

Staged references for the long prompt plus 72 teacher rows, against a float64 forward of the same weights:

  • Local error per stage (median of the means over stages): Metal 0.25%, mlx_lm 0.31%.
  • final local max: 0.19%.
  • The worst local stage is one row each, where the float64 router gap is under 0.015 and bf16 routes to another expert: Metal 15.6% at layer 12, mlx_lm 17.8% at layer 13.
  • The chained error grows alike in both engines, to a median of 42% by layer 39, from bf16 rounding and routing flips.
  • Top-1 against float64: Metal 72/72, mlx_lm 71/72.

Not run, and why

  • Drafts on against draft: false. This engine's Metal drafter reads DFlash2 contracts only (the 27B's and Ornith's). The 35B-A3B's only published drafter is DFlash v1 (sliding-window layers, no selector), so there is no drafted Metal run to compare. A drafter for it is a separate PR.
  • The M5 cases. The prompt and prefix kernels on tensor units, and the affine gathers at the 35B's widths: their records pass here, but their gates run only on tensor units.
  • The full zig build test. It was not run on this host; the qwen27 and kernel-contract suites above cover the changed files.
  • A dense 27B A/B. The 27B's paths change only where staged references are asked for, which is off by default.

@cshintov
cshintov force-pushed the feature/qwen36-moe-on-main branch from cc5048b to ee8a658 Compare November 14, 2026 02:43
@ashhart

ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thanks @cshintov. One build fix remains after the rebase.

At stage_planes.zig:6 and qwen27.zig:52, the code references core.qwen35_stages, but the production tensorfold.zig core does not export it. The host geometry check fails with "root source file struct 'tensorfold' has no member named 'qwen35_stages'". The standalone tools use separate core shims, so they miss this caller.

Please export the recorder through the production core and rerun zig build native test-qwen-dense-geometry-host. Once that passes, we will run the device check on our side for staged reference equality and served tokens.

  • Synthena (Ash's Agent)

@cshintov

Copy link
Copy Markdown
Contributor Author

Exported qwen35_stages from the production core (tensorfold.zig), next to qwen35. At 742ebb4 (rebased onto main 4ddc28c), zig build native test-qwen-dense-geometry-host passes (72 of 72 steps, 111 of 111 tests) on an M4 Pro, and zig build test-qwen27 still passes (180 of 180). Lean: 341 against 341, 0 new.

@cshintov
cshintov force-pushed the feature/qwen36-moe-on-main branch from ee8a658 to 742ebb4 Compare November 14, 2026 12:13
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 14, 2026
@ashhart

ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thanks @cshintov. The production core export now compiles through the actual family recorder caller, and all 35 focused host cases pass with zero new lean findings. The missing-export control fails as expected. We will run the device check on our side on M5 for the native build, staged reference equality and served token equality.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

DEVICE. Thanks @cshintov, the native build and 136 host tests pass, as do 46 contract kernels with all six controls caught. Native stages match across 128/16-row chunks, and solo/plain plus four concurrent drafted replies are identical.

The independent references disagree: at reply position 41 MLX chooses 16251 and native chooses 328. On the same teacher-forced context, float64 favors 328; native matches its top-1 at 64/64 positions, MLX at 63/64. Stage bytes also differ between engines, starting at layer 0's mixer. The exact-MLX comparison therefore remains red; we need to settle that reference criterion before calling the gate green. No bound or expected token has been changed.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Settling the reference question: for a new checkpoint on our routed path, the reference is the float64 forward of the checkpoint's own weights, which tools/zig/qwen36_stage_ref.py computes. mlx_lm is a second engine with its own rounding, so it isn't the reference. On that criterion this head passes:

  • native matches float64's top-1 at 64 of 64 teacher-forced positions, and mlx_lm at 63;
  • at position 41, where the two engines split, float64 favors native's 328;
  • the local stage error is lower than mlx_lm's (your 0.25% against 0.31%);
  • staged chunks, solo, plain and four concurrent drafted replies are all identical.

So this is READY, and it's queued to land.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

@cshintov, a correction to my note above. The reference question is settled, but this head can't land until its own CI passes, and three jobs fail on 0343160:

  1. zig and zig-windows: the Qwen 3.5 config-rule host tests still count main's admissions. qwen35_rules_host ("real configs, each entry point admits what main admitted, and by the table") expects 2 configs from src/families/qwen27/config_test.zig and finds 3. Three qwen35_layers_host cases and three more qwen35_rules_host cases fail the same way. The 35B-A3B is a new admission, so those tables need its row: what it admits, through which entry point, and the refusal the other entry points keep. Please don't relax the rule that each entry point admits what main admitted.
  2. pytest: tools/zig/qwen36_stage_ref_test.py imports mlx, which CI doesn't have (ModuleNotFoundError: No module named 'mlx'). Skip it when mlx is missing, as the repository's other reference tests do, or keep the import inside the function that needs it.

zig build test-host -Dfixed-cpu and python3 -m pytest -q tools/zig/qwen36_stage_ref_test.py reproduce both. It goes back in the landing queue once CI is green on the new head. Our M5 result stands, since neither fix touches the served path.

  • Synthena (Ash's Agent)

@cshintov

Copy link
Copy Markdown
Contributor Author

Fixed on ace0d46 (rebased onto main e7e2424):

  1. zig and zig-windows: the 35B-A3B config in qwen27/config_test.zig is now a counted real config (3 in that file). It has its own row in both tables. Metal's 27B and HIP admit it (every fourth of 40 layers), as main's rules did. The 2B refuses it with UnsupportedQwenConfig, CUDA with UnsupportedActivation (the config has no hidden_act), and XPU with UnsupportedQwenShape. The rule that each entry point admits what main admitted is unchanged; only the totals move, 44 to 46 and 21 to 23.
  2. pytest: qwen36_stage_ref_test.py skips the module when mlx or mlx_lm is missing, the way pack_compare_test.py does for numpy. With mlx blocked, pytest reports 1 skipped; with mlx, 10 pass.

test-host is the Linux aggregate, so on an M4 Pro I ran the steps under it: zig build test-qwen35-rules-host test-qwen35-layers-host -Dfixed-cpu (13 of 13) and zig build test-cuda-host -Dfixed-cpu (exit 0). tools/pr_check.py: 0 errors, lean 337 against 337. The changelog line is back under Unreleased after the 1.2.9 cut.

@ashhart

ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks @cshintov, the earlier build and CI asks are answered. On an M5 Ultra, all 46 focused contract kernels pass with six controls caught. All 83 stage planes match across chunks, float64 agrees at 64/64 positions, and drafts, concurrency and resumed replies remain exact.

The existing dense 27B still needs its default cost resolved. With the cache flag omitted, the greedy four-request median is 3.0355 s against 3.0201 s, +0.51%, with disjoint runs. Cache-off greedy and sampled spans are also disjoint at +0.33% and +0.21%. The default recorder hooks are the shared addition to check; these timings do not locate the cause.

One scope gap also remains: this is described as text only, but --vision opens the 35B checkpoint, loads its tower and advertises vision. Please refuse that mode by name for this slice, or qualify and document its image path. The text checks establish no image-quality result.

  • Synthena

@cshintov
cshintov force-pushed the feature/qwen36-moe-on-main branch from ace0d46 to c9987fd Compare November 17, 2026 04:13
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 17, 2026
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 17, 2026
@cshintov

Copy link
Copy Markdown
Contributor Author

Both points are addressed on c9987fd (rebased onto main 3f1290f).

Default cost of the dense 27B. The stage recorder is now compiled in only when the program's root declares qwen27_stages = true, which tf-qwen27-run does. Every other build keeps main's pass hooks (forward.Mixer) and Runner: no stage field, no check. To show the served engine is unchanged, I built tensorfold-native from this head and from the same tree with forward.zig and decode_round.zig reset to main's. Built from two folders whose paths have the same length, then compared section by section:

Section Size (bytes) Differing bytes
__TEXT,__text 9,638,584 0
__TEXT,__const 2,037,424 0
__TEXT,__cstring 4,366,753 0
__TEXT,__eh_frame / __unwind_info 571,752 / 63,168 0
__DATA,__const / __DATA,__data 329,632 / 36,920 0

Only the UUID and __LINKEDIT (symbol strings and code signature, which carry the build path) differ, so the served engine's machine code is byte-identical to a build without the hooks. I did not time it again: the +0.51% you measured on the earlier head is not from the recorder in this build, and I can't say from this what caused it.

--vision on the 35B. qwen27_served.open refuses it before the tower loads: "--vision: Qwen3.6-35B-A3B is served as text only; its image path is not qualified". The check is the model's shape (model.a3b, the predicate supported admits it by), so Ornith-1.5-122B and the dense 27B are unaffected. The changelog line says so.

Checks on an M4 Pro: zig build native test-qwen27 tf-qwen27-run test-qwen35-rules-host test-qwen35-layers-host -Dfixed-cpu (83 of 83 steps, 195 of 195 tests); pytest tools/zig/qwen36_stage_ref_test.py: 10 passed; tools/pr_check.py: 0 errors, lean 221 against 221.

@ashhart ashhart mentioned this pull request Nov 18, 2026
4 tasks done
@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thank you @cshintov. Both points are resolved.

The stage recorder is now built only into tf-qwen27-run, and the served engine's code sections are byte-identical to a build without those hooks, so the earlier default span does not come from this change. --vision now refuses the 35B by name before the tower loads, and the other models keep their paths.

The release line in CHANGELOG.md is ours to place, so the collision there is on our side. This is ready to land.

  • Synthena

@ashhart
ashhart force-pushed the feature/qwen36-moe-on-main branch from c9987fd to 41857b8 Compare November 19, 2026 00:45
ashhart pushed a commit to cshintov/TensorFold that referenced this pull request Nov 19, 2026
ashhart pushed a commit to cshintov/TensorFold that referenced this pull request Nov 19, 2026
@cshintov
cshintov force-pushed the feature/qwen36-moe-on-main branch from 41857b8 to 0fb21b0 Compare November 20, 2026 06:38
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 20, 2026
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 20, 2026
@cshintov

Copy link
Copy Markdown
Contributor Author

Rebased onto main 4f3f564 at 0fb21b0 to clear the conflict with the prompt tail change (881e224).

  • The Runner's own keysOnly is gone, as on main. Main's prompt_tail.facts now feeds both the keys-only pass and the tail. While stages are recorded, stage_planes.begin starts the round's planes and marks the last layer as read, the same as a tap on it. That keeps both the keys-only pass and the tail off for a recorded round. When the recorder is not built in, begin returns the facts unchanged.
  • Your 600-line fold commit no longer applied, because the line it folded is gone. Its place is taken by the stage_planes.begin commit, and decode_round.zig is at 600 lines.
  • Main's new zero head count test counts real configs, so its expected numbers now include the 35B-A3B's bases: Metal 27B goes from 8 to 10, HIP from 18 to 20, and the configs without head_dim from 13 to 15.

Checks on an M4 Pro: zig build native test-qwen27 tf-qwen27-run test-qwen35-rules-host test-qwen35-layers-host -Dfixed-cpu passes all 83 of 83 steps, and tools/pr_check.py reports 0 errors.

@AIFlowML

Copy link
Copy Markdown
Contributor

@cshintov @ashhart I followed the two documented test commands on Apple M5 Max with Zig 0.17.0 at 0fb21b0ab:

zig build test-qwen27 test-kernel-contract -Dcpu=apple_m1 -j8 --summary all

Both targets passed, with 82/82 build steps, 413 tests passed and three skips. The first detail I'd add to the PR description is this head beside the kernel-contract result. The rebase comment names the head and the M4 checks, while the description's contract result is from the earlier revision.

The head test-merged cleanly against current main 1ece6e6db. I ran the model-free tests above. The full 35B checkpoint and its device comparisons remain covered by the separate device receipt.

Igor Lessio - AIFLOW LABS

@cshintov
cshintov force-pushed the feature/qwen36-moe-on-main branch from d7f3dd6 to ce59a24 Compare November 22, 2026 12:38
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 22, 2026
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 22, 2026
@cshintov
cshintov force-pushed the feature/qwen36-moe-on-main branch from ce59a24 to be8c034 Compare November 23, 2026 11:21
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 23, 2026
cshintov added a commit to cshintov/TensorFold that referenced this pull request Nov 23, 2026
@ashhart
ashhart force-pushed the feature/qwen36-moe-on-main branch from be8c034 to 8cb4892 Compare November 23, 2026 13:23
ashhart pushed a commit to cshintov/TensorFold that referenced this pull request Nov 23, 2026
ashhart pushed a commit to cshintov/TensorFold that referenced this pull request Nov 23, 2026
@ashhart
ashhart force-pushed the feature/qwen36-moe-on-main branch from 8cb4892 to b42c940 Compare November 24, 2026 00:50
cshintov and others added 13 commits November 24, 2026 04:50
… float64 forward

One stage vocabulary (families/qwen3_5/stages.zig): the residual stream after the embedding and after each
layer's mixer and MLP, then final (the normed rows the head reads, not the residual) and the logits.
tools/zig/qwen36_stage_ref.py measures each against float64 of the checkpoint's exact weights; a non-unit
final norm fixture refuses a residual labelled final.
…, as the recorder allows

While stages record, the last layer counts as tapped, so main's keys-only last layer stays off and the
recorded stream and final are real rows. The stage hook (planes and glue) and the admit check move into
stage_planes.zig, so the runner needs no forwarding function, and planes.end runs after the round is
committed, so a recorder error leaves the runner consistent.
The A3B config in qwen27/config_test.zig is a new real config: Metal's 27B
and HIP admit it, as main's rules did; the 2B, CUDA and XPU keep their
refusal. The stage reference test needs mlx, which CI does not install.
@ashhart
ashhart force-pushed the feature/qwen36-moe-on-main branch from b42c940 to c278a42 Compare November 24, 2026 05:38
@ashhart
ashhart merged commit 8466dfa into ashhart:main Nov 24, 2026
12 of 23 checks passed
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

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

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