You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[MLX] Qwen return last-token logits from forward; make SamplingHead operate on (B, vocab) (#20604)
Summary
Qwen's MLX forward now returns last-token logits (B, vocab) instead of
(B, S, vocab), so lm_head runs on one position per prefill instead of
the whole sequence. SamplingHead correspondingly drops its internal
last-token slice and samples directly from (B, vocab), making it
model-agnostic.
Changes
- backends/mlx/llm/sampling.py — SamplingHead drops its internal
logits[:, -1, :] slice; samples a token (B) directly from (B, vocab),
removing the (B, S, vocab) path.
- examples/models/qwen3_5_moe/export.py — _clean_forward returns
lm_head(x[:, -1, :]) → (B, vocab) instead of all S positions, so lm_head
runs once per prefill (chunk) instead of over the whole sequence.
- run.py + test_chunked_prefill.py — consumers of qwen's forward output,
both dropping the [0, -1, :] indexing.
- test_ops.py + test_sample.py — op-level tests of SamplingHead/sample
shape + node counts (upstream's top-k PR).
- No C++ changes — the runtime sampler already handles 2D; chunked
prefill result is identical.
Testing
- MLX sample op tests pass on-device.
- Prefill ~19% faster (1505-token prompt): ~1352 → ~1612 tok/s; decode
unchanged (~75 tok/s).
0 commit comments