Repository navigation
perf: 10x prefill on Qwen3.5/3.6 GDN via asyncEval + kernel path - #49
Merged
Merged
Conversation
Qwen3.6-35B-A3B prefill on M5 Max @ ctx=1024: 64 tok barrier: 247 tok/s 128 tok barrier: 297 tok/s (+20%) 256 tok barrier: 286 tok/s (+16%) 512 tok barrier: 293 tok/s (+19%) 1024 tok barrier: 281 tok/s (+14%) GPU peak unchanged at 18.54 GB across all eval intervals — the 64-token cadence was syncing the GPU pipeline too aggressively (30 GDN layers × 8 syncs per 512-token chunk = 240 syncs per prefill chunk) without memory benefit. 128 cuts that in half with no memory cost. Still overridable via GDN_EVAL_INTERVAL env var for further tuning. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-Authored-By: tturney@psyguard.ai
Root cause of the previously-flagged "kernel correctness bug at T>1 (max diff 0.25 vs ops)" was precision, not a kernel bug: the Swift state was typed q.dtype (bf16), so accumulation across T recurrence steps compounded rounding error. Python mlx-lm has always used fp32 state, which is why the same kernel is correct there. Three coupled fixes: - Kernel source: write o_state as StT (not InT), matching Python's kernel - Swift dispatch: pass StT template + use state.dtype for output - gatedDeltaUpdate: create state in fp32 (and upcast if caller passes bf16) Prefill path now routes through the single-dispatch Metal kernel instead of the Swift-side T-loop ops fallback. One Metal dispatch for all T timesteps per GDN layer, matching Python. Qwen3.6-35B-A3B prefill on M5 Max is correct and now equivalent to, rather than bypassing, the kernel — residual Swift-vs-Python perf gap is framework-level dispatch overhead, not kernel choice (see BuildLog investigation). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-Authored-By: tturney@psyguard.ai
Previously `LLMModel.prepare` called `eval(cache)` + `MLX.Memory.clearCache()`
after every prefill chunk. `eval` is a blocking sync: the CPU would idle
while the GPU finished chunk N, then build chunk N+1's graph serially,
then sync again. This drained the pipeline after each chunk.
Python mlx-lm gets pipelined prefill for free because its bindings defer
eval until a value is read, so the next chunk's graph builds while the
GPU works on the previous chunk.
Swap to `asyncEval` on the cache's inner state per chunk — non-blocking,
so the CPU can immediately start graph-building the next chunk. One
terminal `eval(cache)` + `clearCache()` after the full prefill loop.
Qwen3.6-35B-A3B-4bit prefill on M5 Max 128GB (ctx, tok/s):
before → after (×)
128 260 696 2.7x
512 235 2201 9.4x
1k 244 3130 12.8x
2k 270 3937 14.6x
4k 265 3769 14.2x
8k 268 3416 12.7x
32k 522 2040 3.9x
At ctx=2k the gap vs Python mlx-lm (≈4000 tok/s) closes to within 2%.
No regression on non-GDN models:
- Gemma 4 E2B 4-bit: 19725 tok/s @ 1k (baseline 20206, ~within noise)
- Llama 3.2 3B 4-bit: 6710 tok/s @ 1k (baseline 6837, ~within noise)
Decode speed unchanged (different code path).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: tturney@psyguard.ai
Owner
|
Confirmed that I saw that about 4-5X prefill speed increase across Qwen3.5-35b MoE model and Qwen3.5-0.8b. Should apply to all Qwen3.5 models since they are the same architecture. |
Author
|
Added a follow-up commit Small but real short-context win:
Wins taper to noise at ctx≥1k — GPU is already compute-bound on per-layer work at that point. Coherency unchanged. Non-GDN / non-Mamba models don't have this pattern so they're untouched. |
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
Qwen3.6-35B-A3B-4bit prefill on M5 Max 128GB jumps 14.6x at ctx=2k, bringing Swift within ~2% of Python mlx-lm:
No regression on non-GDN models: Gemma 4 E2B (19,725 vs baseline 20,206) and Llama 3.2 3B (6,710 vs 6,837) are both within run-to-run noise.
Three commits
1.
perf(gated-delta): bump prefill eval interval 64 → 128 for +20%The
gatedDeltaOpsfallback path was syncing every 64 tokens. 128 halves the syncs with no memory-peak cost. Overridable viaGDN_EVAL_INTERVAL. Minor; mostly relevant if the kernel path is ever disabled.2.
fix(gated-delta): use Metal kernel at T>1 with fp32 stateRoot cause of the old
"kernel correctness bug at T>1 (max diff 0.25 vs ops)"TODO was precision, not a kernel bug. Swift state was typedq.dtype(bf16), compounding rounding error across the recurrence. Python mlx-lm uses fp32 state — same kernel is correct there.o_stateasStT(notInT), matching PythonStTtemplate + usestate.dtypefor outputgatedDeltaUpdate: create state in fp32 and upcast caller inputPrefill now dispatches one Metal kernel per GDN layer for all T timesteps, instead of a Swift-side T-loop ops fallback.
3.
perf(prefill): asyncEval between chunks — 10x prefill on GDN arches← the big oneLLMModel.preparewas callingeval(cache)+MLX.Memory.clearCache()after every prefill chunk.evalis a blocking sync: CPU idled while GPU finished chunk N, then CPU serially built chunk N+1's graph, then sync again. Pipeline drained after every chunk.Python mlx-lm pipelines for free because its bindings defer eval until a value is read — the next chunk's graph builds while the GPU works on the previous one.
Swap to
asyncEvalon the cache's inner state per chunk (non-blocking), with one terminaleval(cache)+clearCache()after the full prefill loop. CPU and GPU now pipeline.How to verify
Run the benchmark at a few ctx points; GDN-heavy arches see the biggest jump, dense GQA arches should be within noise:
Test plan
LLMModelFactory → Qwen35MoEModel → Qwen35GatedDeltaNet → gatedDeltaUpdate → gatedDeltaKernelwithstateType=float32🤖 Generated with Claude Code