Skip to content

Qwen3.8 metal perf - #51217

Draft
tarekziade wants to merge 152 commits into
mainfrom
qwen3.8-metal-perf
Draft

tarekziade wants to merge 152 commits into
mainfrom
qwen3.8-metal-perf

Conversation

@tarekziade

@tarekziade tarekziade commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

🤗 Transformers CI

Draft for SSD offload

…ad op, uncompressed checkpoints no longer force it
… the checkpoint format; drop no-op after-load hook
…hts load decompressed, compressed-tensors only sets up activation / kv cache quantization
…ad op, uncompressed checkpoints no longer force it
… the checkpoint format; drop no-op after-load hook
…hts load decompressed, compressed-tensors only sets up activation / kv cache quantization
Replaces WeightTransform.quantization_operation: the op is appended last to each param's converter.
…converters of modules keeping their packed tensors
…buffer kept; serialize visits every module; gptq/awq/auto_round/quark dequantize moved to a follow-up
…vfp4 loads pre-quantized checkpoints with dequantize=True
…ion and shared-expert linears (41.2 -> 42.5 tok/s)
…ype; Qwen3.8 fine ablation + synthetic 512-expert checkpoint script
…r missed routes only, guesses through the router, per-layer pending index; bench: local dirs, router-skew sim, trace
…host thread installs misses (Qwen3.8 synthetic 0.25 cache: 11.7 -> ~23 tok/s, exact)
…-only pass 2, no pool copy around the wait (Qwen3.8 synthetic 0.25 cache: ~23 -> 37-41 tok/s, exact)
…te table publication, next-layer guesses for read-ahead (Qwen3.8 synthetic 0.25 cache: 42.5-42.7 tok/s, exact)
…, ArthurZ/mps-events), drop experiment scripts
…into the slots (safetensors#996), preadv fallback
…d eviction fix

`metal_router.py` computes the Qwen4 top-k router in one Metal dispatch on MPS:
the logits matmul, the full-softmax denominator, and the top-k selection all stay
on the GPU. Selection runs in the same threadgroup that produced the logits, so a
single-token decode step costs one dispatch instead of matmul + softmax + topk.
Logits are stored as bf16-rounded doubles, so top-k by logit equals top-k by
probability, and the full denominator is used because Qwen4 sets
`norm_topk_prob=None` (no renormalisation).

`Qwen4ExpTextTopKRouter.forward` takes the fast path for MPS, batch 1, eval,
`num_experts <= 512`, `hidden_dim % 4 == 0`, and no top-k renormalisation.
`DETERMINISTIC_TOPK=1` forces a stable argsort with id-ordered selected experts
instead, so repeated runs select the same experts in the same summation order.

`offload.py`: the read-ahead path could evict a resident expert of the layer it
was prefetching for, while the GPU was still in that layer's first pass. The
overwritten slot is then read as the wrong expert. The fix keeps every resident
expert of the target layer during install; the read-ahead sweep is now behind
`TRANSFORMERS_EXPERTS_OFFLOAD_PIN_AHEAD=1`.

Measured on Qwen3.8-Flash-Next 4-bit, 16 GiB expert cache, 128/128 greedy tokens
matching eager in every arm:

  router off            6.54 mean / 8.27 sustained tok/s
  router on             6.78 mean / 8.31 sustained tok/s
  router on + kernels   6.77 mean / 8.97 sustained tok/s

The router is a small but consistent win; the previous 6.05 vs 8.56 "regression"
was the read-ahead eviction bug, not the kernel. The kernel arm is correct and
8% faster on sustained decode.
…S knobs

The profiling harness that produced the per-component breakdown of the MPS
decode wall (host sync, read wait, GPU+python) and the token-dump used to
compare eager against compiled output.

- profile_decode.py: store.stats() deltas, host router spin/work, per-layer trace
- profile_cpu.py / profile_gpu_split.py: main-thread CPU share and the GPU split
- trace_syncs.py: attribute scalar syncs to their callers
- ablate_pass2.py: zero pass 2 to price the deferred miss pass
- verify_generate.py: NO_COMPILE=1 skips compile_config, DUMP_TOKENS writes the
  eager/compiled token streams for exactness diffs
…cess sweep

`MAX_AHEAD` was a plain class attribute, so `TRANSFORMERS_EXPERTS_OFFLOAD_MAX_AHEAD` (already
exported by the bench runners) silently did nothing and every run used 16.

`max_ahead_sweep.py` sets the field on the live router and walks the range inside one process,
which is what makes the sweep affordable: one checkpoint load instead of one per value. Measured
at 16 GiB / events on / LOOKAHEAD=0: the knee is 4 and 4..32 is flat (35.65-36.03 ms/token); a
cold 128-token verify at 32 with PIN_AHEAD=1 is exact but 17.48 sustained against 19.58, so 16
stays the default and is documented as a margin above the knee.
@tarekziade
tarekziade marked this pull request as draft November 25, 2026 07:46
ArthurZucker and others added 3 commits November 25, 2026 08:59
Instrumentation (all inert unless asked for):
- handshake submit -> routing landed, and the miss-install share of it
- guess accuracy priced against the next layer's real routing
- GUESS=1 decouples "predict the next layer" from the prefetch `lookahead`, which stays 0; the
  prediction had been dead in every arm because `next_layers()` is empty at lookahead 0
- ROUTE_TIMING=1 splits route() into copy / spin / put / flush
- profile_decode: TRACE_ITEM and TRACE_OPS attribute every scalar sync and op to its caller,
  DECODE_MARK lets a tracer attach to the measured window, NO_COMPILE=1 drops compile regions

Two changes, both measured neutral in the events-on window (63.56 vs 63.62 ms/token) and kept
only because they remove work with no regression:
- route() writes the three routing tensors with narrow copies instead of torch.cat
- the single-sequence rotary split skips lengths.tolist()

Measured, not guessed: wall 63.56 ms/token, main-thread CPU 62.79 (98.8%), route() copy 0.48 /
spin 27.31 / put 0.05 / flush 0.45, scalar syncs on the main thread 0.4 ms/token, and the guess
at 2.4% exact / 10% slower.
# Conflicts:
#	docs/source/en/quantization/finegrained_fp8.md
#	docs/source/en/quantization/mxfp4.md
#	docs/source/en/quantization/nvfp4.md
#	src/transformers/integrations/finegrained_fp8.py
#	src/transformers/integrations/hub_kernels.py
#	src/transformers/quantizers/quantizer_finegrained_fp8.py
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: deepseek_v4, ministral3, mistral4, qwen4_exp, aqlm_integration, bitnet_integration, eetq_integration, finegrained_fp8, higgs, metal, mxfp4, nvfp4, sinq

This branch has not been deployed

No deployments
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