Skip to content

perf: 10x prefill on Qwen3.5/3.6 GDN via asyncEval + kernel path - #49

Merged
ekryski merged 3 commits into
ekryski:alphafrom
TheTom:perf/alpha-prefill-async-eval
Apr 16, 2026
Merged

ekryski merged 3 commits into
ekryski:alphafrom
TheTom:perf/alpha-prefill-async-eval

Conversation

@TheTom

@TheTom TheTom commented Apr 16, 2026

Copy link
Copy Markdown

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:

ctx 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

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 gatedDeltaOps fallback path was syncing every 64 tokens. 128 halves the syncs with no memory-peak cost. Overridable via GDN_EVAL_INTERVAL. Minor; mostly relevant if the kernel path is ever disabled.

2. fix(gated-delta): use Metal kernel at T>1 with fp32 state

Root 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 typed q.dtype (bf16), compounding rounding error across the recurrence. Python mlx-lm uses fp32 state — same kernel is correct there.

  • Kernel source: write o_state as StT (not InT), matching Python
  • Swift dispatch: pass StT template + use state.dtype for output
  • gatedDeltaUpdate: create state in fp32 and upcast caller input

Prefill 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 one

LLMModel.prepare was calling eval(cache) + MLX.Memory.clearCache() after every prefill chunk. eval is 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 asyncEval on the cache's inner state per chunk (non-blocking), with one terminal eval(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:

MLX_BENCH_MODEL=mlx-community/Qwen3.6-35B-A3B-4bit MLX_BENCH_METHOD=summarization MLX_BENCH_CONTEXT=2048 swift test -c release --filter InferenceBenchmarks/benchmark

Test plan

  • Qwen3.6-35B-A3B-4bit ctx 128/512/1k/2k/4k/8k/32k all coherent + much faster
  • Gemma 4 E2B no regression (19725 tok/s at ctx=1k)
  • Llama 3.2 3B no regression (6710 tok/s at ctx=1k)
  • Trace-verified the live prefill path goes through LLMModelFactory → Qwen35MoEModel → Qwen35GatedDeltaNet → gatedDeltaUpdate → gatedDeltaKernel with stateType=float32
  • Coherency sample at ctx=32k still produces clean output

🤖 Generated with Claude Code

TheTom and others added 3 commits April 16, 2026 16:02
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
@ekryski
ekryski merged commit 83ff028 into ekryski:alpha Apr 16, 2026
@ekryski

ekryski commented Apr 16, 2026

Copy link
Copy Markdown
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.

@TheTom

TheTom commented Apr 16, 2026

Copy link
Copy Markdown
Author

Added a follow-up commit 32bf5ba — same asyncEval idea applied to the per-layer eval barriers in Qwen35 and NemotronH. Both called eval(hidden, cache) after every layer during prefill which was another CPU↔GPU sync point (40-48 syncs per chunk).

Small but real short-context win:

Model ctx=128 pp tok/s (before → after)
Nemotron-Cascade-2 30B-A3B 863 → 960 (+11%)
Qwen3.5 2B 2837 → 3018 (+6%)
Qwen3.5 9B 1255 → 1320 (+5%)
Qwen3.6 35B-A3B 718 → 712 (noise)

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.

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.

2 participants