Repository navigation
Qwen3.5 family: the geometry comes from the checkpoint, and Qwen3.8-27B is served - #520
BobClawblaw wants to merge 2 commits into
Conversation
…7B is served The family had the 2B's shape as comptime constants and as constexpr lines in each kernel. The kernels are now templates whose dimensions the loader prepends as macros when it compiles them; the seven generated projection files were one body with different constants and become one, compiled once per distinct shape of the checkpoint with the row decoder's split-K and tile rules kept as code. config.zig reads the dimensions and matches them against a two-entry table, the pinned 2B and Qwen3.8-27B (hidden 5,120, 64 layers, 48 DeltaNet value heads over 16 key heads, 24 query heads over 4, a separate output head, bf16 A_log). Buffers, strides, grids and thread counts come from that geometry; two were invisible on the 2B, where hidden and both mixers' rows are all 2,048: the query buffer takes the wider of the DeltaNet and attention rows, and the attention partial kernel gets a simdgroup per query head of a group. The 2B is bit-identical to main (tf-qwen35-forward logits on 300- and 777-token fixtures), passes its 49 exactness checks and loads the same pipelines. The 27B passes the same 49 checks; against stock mlx-lm its top-1 agreement matches mlx-lm's own one-row versus chunked agreement at every length (NLL within 0.002); behind the server 120 of 120 concurrent replies equal solo and 72 of 72 solo equal plain, 36 tok/s a stream and 104-108 at eight, 16 GiB RSS. The fidelity tool falls back to the repository's docs for its third corpus now that the Python engine's left the tree. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Receipt from an M5 Max (Mac17,6, 128 GB, macOS 27.0), Zig 0.17.0, Metal Toolchain 27A266a, Checks
Server (
One data point on tuning. On this machine the Python engine's one-row target forward took 37 ms, about 27 tok/s (0.6.0; 29 tok/s end-to-end serial on 0.2.0), on Small thing seen at startup, also on |
|
Thanks for the receipt. The exactness picture matches the M5 Ultra runs, and the fidelity script needing an mlx-lm 0.32.0 environment is expected; the Ultra rows in the description cover that one. The The tuning data point is useful. Split-K ruled out with the drift you measured, and the Python engine's one-row forward at about 27 tok/s on the same quantization, leaves the row decoder's tile width and the eight-slice partials as the candidates, as you say. That is the same lever #196 pulled for one-stream servers, so a 32-wide A/B on the 27B's 5,120-output projections is the natural follow-up for the family after this lands. I will track it separately rather than widen this PR. |
|
Following up on the tuning data point: the tile-width A/B is on #527 and the result is that the lever was elsewhere. Each step ended with a host memcpy of every DeltaNet layer's recurrent state into the stream's cache, 151 MB a stream on the 27B. #528, stacked on this PR, copies it on the GPU inside the step's command buffer: one-stream decode 36.0-36.3 to 38.5-38.9 tok/s and eight streams 106-110 to 131-138 aggregate on the M5 Ultra, with the 2B and 27B logits byte-identical to this PR's binary and all 49 exactness checks passing. The |
|
Follow-up to my receipt above, with a correction: tile width is not where the M5 Max time goes. Tile width. Where a one-row step goes (1,000-token context, each kernel label timed alone with the others left out of the forward, medians): GPU 41.0 ms, wall 44.4 ms. Projections are 40 ms of it:
All other kernels together take about 2.3 ms ( So on this chip the large projections run at about 385 GB/s, and the time beyond that is in the small projections (a and b alone are about 1 ms for 13 MB of weights) and in per-step host time. For comparison, the Python engine's one-row target forward was 37.2 ms here, against 41.0 ms of GPU time now. Joining a and b into one 96-output projection would change One thing to avoid: cutting the forward into several command buffers looked 12% faster, but the buffers are untracked, so the pieces overlapped on the GPU and the logits changed from run to run. Ordered with an |
|
@jkuepker, if you want to run the follow-ups on the M5 Max, two branches on the fork, both on top of this PR's head:
For either: The bench prints each projection shape at 1 to 32 rows, the FP32 path beside the matrix kernel with its numeric distance and a row-independence check, then the one-row step: The server runs the matrix family by default; The two numbers that matter: the |
|
Thanks, that breakdown settles the Max side, and it reconciles with the Ultra in a way that matters for what to do next. The two dies are bound by different things. On your Max the large projections run at about 385 of 460 GB/s, 84% of the chip: memory-bound. On the Ultra the same kernel reaches about 700 of 1,200, 58%, and removing the matrix multiplies halves its time: tensor-unit bound (the nibble decomposition issues 32 accumulates a kilobyte of weights, and one row uses an eighth of each). That is why the FP32 family on Your 2.4 ms with every dispatch left out is the host memcpy. The small projections are the bit-exact lever you have left on the Max, and your table says where. Two fusions keep the bytes: the attention k and v (1,024 outputs each, 32 threadgroups apiece on a 40-core die) as one 2,048-output projection, which The command-buffer split warning is a good one to have on the record. |
…tep as 128, not 0,128 The startup line split the prefill step into thousands and a zero-padded remainder, which assumed a step of 1,000 or more. Nemotron and GLM meet that; the Qwen3.5 family's 128-token step printed as "0,128". The status line's comma grouping in live.zig becomes a public `commas` for integers of any size, and the startup line uses it. Seen in the receipt on #520.
|
Follow-up to my profile above: the 27B's prefill is held back by its projections, and two commits on top of this PR's head (094cbaa) roughly triple it on an M5 Max. Branch: jkuepker/TensorFold@094cbaa...jkuepker:TensorFold:qwen35-nax-prefill Why prefill was slow. A GPU profile of one 1,024-row prompt forward ( The change.
Speed (localeval, drafts off,
The decode dip in the third row was run order: run alternately, 128 and 512 both decode at 20.9–21.1. For reference, Python 0.6.0 on the same checkpoint prefills 739 / 846 / 826, so 16K is still about half. Correctness.
Not addressed: decode on code with drafts on is 47 tok/s here against Python's 333 with DFlash2, and I haven't looked at the drafter yet. |
|
Update to the prefill comment above: a third commit on the same branch moves prompt-chunk attention onto the tensor units, and 16K prefill goes from 401 to about 620 tok/s. Branch: jkuepker/TensorFold@094cbaa...jkuepker:TensorFold:qwen35-nax-prefill Why. With the projections on NAX, a 512-row prompt chunk at 16K context took 1,695 ms on the GPU profile, of which per-row attention was 1,056 ms (62%). The change (
Speed (localeval, drafts off, 27B 4-bit, M5 Max; prompt tok/s at ~1k / 4k / 16.7k; the second and third rows were run alternately in one session, the 094cbaa row is from the earlier comment):
1024-row chunks were 1–2% faster than 512 and are not taken. Decode is unchanged within run-to-run noise. Correctness.
Next: drafting. Code decode is still about 45 tok/s here against Python's 333 with DFlash2. A read of the lane core suggests three engine gaps before any drafter port:
I'm looking at (3) first. |
|
@jkuepker, before you start on (3): it is already done. #528 ( I tried merging it onto On (1) and (2), a note from the code as it stands: |
|
Thank you, @BobClawblaw, and thanks to @jkuepker for the M5 Max receipt and the profiling in this thread. Turning the family's comptime geometry into a checked table, with the 2B's logits byte-identical and 49 of 49 cache checks on the 27B, is solid work. 1.0.3 shipped Qwen3.8-27B on its own engine (34c4424, https://github.com/ashhart/TensorFold/releases/tag/v1.0.3), with DFlash2 drafts and prompt reuse, qualified on an M5 Max and an M3 Ultra. A qwen3_5 checkpoint with the 27B's geometry opens that engine (zig/src/native/metal.zig), so this PR would add a second route for the same checkpoint through the 2B's row decoder. We'd rather keep one path per model, so we're closing it. Two things from this thread carry over to the 27B engine, with your names on them: concurrent streams (1.0.3 serves the 27B one request at a time, and your runs show eight streams equal to their solo runs) and the small-projection fusions you two worked out. If you'd like to take either to the 27B family, a PR against main is very welcome. Reopen this if we've misread it. |
…tep as 128, not 0,128 The startup line split the prefill step into thousands and a zero-padded remainder, which assumed a step of 1,000 or more. Nemotron and GLM meet that; the Qwen3.5 family's 128-token step printed as "0,128". The status line's comma grouping in live.zig becomes a public `commas` for integers of any size, and the startup line uses it. Seen in the receipt on ashhart#520.
Help-list item 5, a new checkpoint for the native server: Qwen3.8-27B (
TensorFold/Qwen3.8-27B-MLX-4bit) through the existing Qwen3.5 family. It is the same architecture as the pinned 2B (qwen3_5_text, linear attention three layers in four, affine 4-bit group 64), only wider: hidden 5,120, 64 layers, 48 DeltaNet value heads over 16 key heads, 24 query heads over 4, and a separate output head. The family had the 2B's shape as comptime constants and asconstexprlines in each kernel; this makes the geometry a checkpoint property, with a two-entry admission table so nothing else is let in unqualified.What changes
zig/kernels/metal/qwen3_5/: the kernels become geometry-free templates. Theconstexprdimension lines come out, and the family prepends them as macros when it compiles each template (Metal has no program-scopeconstexpr). The seven generated projection files were one body with different constants (diffed), so oneqmm.metalreplaces them; the loader compiles it once per distinct (outputs, inputs) shape of the checkpoint, with the row decoder's split-K and tile rules (splits,tiles) kept as code. The layout kernels' literal 2048/4096/8/2 becomeHID,QIN,QH,KVH,HD; the merge kernel'sw * 8becomesw * QH.config.zig:Geometry(hidden, intermediate, layers, the four head counts, tied) with the derived rows (convDim,kInner,vInner,qInner,kvInner,deltaBytes,convBytes);geometrieslists the 2B and the 27B;parsematches the config's dimensions against that table and checkstie_word_embeddingsagainst the entry. The fixture test now builds both configs and keeps the rejection cases.kernels.zig: compiles the fixed templates and the shape list at load;projection(n, k)is a runtime lookup.weights.zig:blockssized by the geometry,lm_headwhen untied, andA_logmaterialized as f32 for the kernel (the 2B stores it f32, the 27B bf16; mlx-lm keeps it out of its dtype cast too).state.zig,forward.zig,qualify.zig,backend.zig: buffer sizes, strides, grids and thread counts from the geometry. Two of those mattered for the 27B and were invisible on the 2B, where hidden, the DeltaNet value rows and the attention rows all happen to be 2,048: the query buffer must hold the wider of the DeltaNet and attention rows, and the attention partial kernel needs a simdgroup per query head of a group (six for the 27B, four for the 2B).tools/zig/qwen35_fidelity.py: its third corpus came from the Python engine'sprefill_cold, which left the tree in 1.0.0; it now falls back to this repository's docs when that import is missing. (The operation-capture tool still imports the removedgen_qwen35_kernels.py; the forward and fidelity checks below do not need it.)Checks
The 2B is unchanged, bit for bit.
tf-qwen35-forwardlogits frommain's binary and this branch's are byte-identical on 300- and 777-token fixtures;tf-qwen35-exactpasses its 49 forward and cache checks;tf-qwen35-checkloads the same 20 pipelines plus the shared tied head.The 27B (M5 Ultra, 256 GB, macOS 27.0.1, Zig 0.17.0, Metal Toolchain 27A266a, revision 22d8d538):
tf-qwen35-check: loads, 22 pipelines, separate head.tf-qwen35-exact: 49 of 49 forward and cache checks (windows of 1 to 32 rows at six prefixes against one-row execution, committed logits, recurrent state and attention caches, partial keeps, unequal streams).tools/zig/qwen35_fidelity.pyagainst stock mlx-lm 0.32.0 (mlx 0.32.3):--parallel 8 --context 32768),tools/bench_concurrent.py --alone --serialat 1, 2, 4 and 8 streams, sampled and greedy, 128 tokens: 120 of 120 concurrent replies equal their solo runs and 72 of 72 solo runs equal"draft": false; aggregate 36 tok/s at one stream, 53-54 at two, 79-81 at four, 104-108 at eight.tools/bench_openai.py --tokens 64 --reps 5 --temperatures 1.0,0: 36.0-36.2 tok/s medians on the code and chat prompts, sampled and greedy, first token in 0.05-0.08 s. No drafter: this checkpoint carries no MTP weights and the native server has no DFlash2, so the lanes carry context copies only.zig build test: passes (the geometry table, the 2B's shape list, the sampler); lean checker 512, the baseline.Memory
A stream's attention cache is 64 KiB a token (16 attention layers, 4 heads of 256, keys and values), so
--contextsizes memory per stream; the forward's scratch for attention partials is 24 query heads wide. The server came up in 2.6-3 s and held 15.9-16.1 GiB RSS at--parallel 8 --context 32768.Docs
README and ZIG-PREVIEW model tables, the 2B recipe's scope paragraph, and a "Native Metal" section in the Qwen3.8-27B recipe with the run command, scope and these measurements.
Independent of #497, #499, #502, #518 and #519: nothing outside the Qwen3.5 family, its kernels, its tests and these docs.
🤖 Generated with Claude Code