Repository navigation
perf(qwen4): optimize Qwen 3.8 Flash Next prefill, decode, DFlash2 & prefix caching end-to-end - #465
Conversation
…ee verify & host pipeline
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…rounding tolerance
Summary
End-to-end Metal 4 / Apple Silicon inference optimization for Qwen 3.8 Flash Next (
qwen4_exp), bringing real 104 GiBUD-Q4_K_XL+ DFlash2 performance into Splash / TensorFold territory while preserving bit-exact greedy parity and persistent PagedAttention RAM + SSD prefix caching.flowchart LR subgraph Prefill["Prefill Overhaul (1.8x–5.2x faster TTFT)"] P1["In-place Tiled64 Q8_0 repack<br/>Zero TG/barriers INT8 NAX GEMM"] P2["AOT Unified MoE Prefill (BM=32)<br/>bfloat x half NAX, 8 KiB stage, shared fused"] P3["AOT Sparse QSA + GDN Prefill + PLE<br/>Fused block indexer Q/K & bank-coalesced PLE"] end subgraph Decode["Decode & Speculation (1.3x–3.6x faster)"] D1["Tiled64 M=1..2 col & M=3..64 mat<br/>Zero JIT CustomKernels, budget=0"] D2["Fused hc_down+hc_inject+2*sig(w/4)<br/>Fused QSA QKV prep+store & gated attn"] D3["DFlash2 M=8 + Metal Tree Verify (T<=16)<br/>Spanned vocab + Q4 head + kv_mode=4"] end subgraph Serving["Serving, Memory & Prefix Cache"] S1["INT8 GDN prefix states (110->31 MiB)<br/>Q4PS v5 + persistent SSD reload"] S2["1|4|24 flush schedule (0ms Bubble #2)<br/>Host-gate, KV growth cliff & 4-lane fairness"] endKey Changes
qwen4_tiled64_linear.metal,mlx_qwen4_tiled64.cpp,weights/): Load-time GPU repack of fixedq8_symmetric_128banks (N%64==0, K%64==0, excludingoutput.weightandffn_*_shexp) into Splash Tiled64 (plane0+meta, droppingbiasesfor ~5.6% runtime VRAM savings), powering zero-TG/zero-barrier INT8 NAX prefill (M>64), register-dequantmatmul2d(M=3..64), coalescedcol_m1/m2(M=1..2), and fusedhc_up + hc_mixwithbudget=0(eliminating all 209 JITCustomKernels in decode).qwen4_moe_prefill.metal,mlx_qwen4_moe_prefill.cpp): Replaces JITfast::metal_kernelprefill MoE (which ran FP32/TF32downin 23 KiB TG SRAM and serialized shared experts across 2,511sizeCB splits) with a single/32-budgeted AOT primitive usingbfloat×halfNAX, 8 KiB per-SG stage,simdgroup_barrieronly, 4-bucket LPT indirect tiles, and unified shared-expert + combine fusion.qwen4_sparse_qsa.metal,qwen4_gdn_prefill.metal,qwen4_ple.metal,paged_indexer.metal,paged_qsa_attention.metal,qwen4_decode_ops.metal,qwen4_lm_head.metal):hc_down + hc_inject + 2*sigmoid(w/4)(-384disps/step) and null-branch AOTinject_norm._gatedpaged QSA attention (sigmoid(gate)epilogue), 10× faster AOT radix11Qwen4PagedIndexerSelect, and AOTQwen4LmHeadQ8Argmax.qwen4_ple_gate_conv_bf16&qwen4_ple_gate_conv_tree_bf16.dflash2/,draft_cycle/{mod,cost_model,tree_metal}.rs,packages/lm/): Auto-discovery with soft residency fallback, fast Q4a4g64@t64draft head with spanned vocab[0..148_288) ∪ [248_000..248_320),M=8wave verify, Metal solo verify tree (T<=16) with exact sparse block-boundary top-k parity (HAS_PRE_TAIL_MASK) andkv_mode=4token-major Int8 KV compaction, plus fixedWaveCostModelslope calibration.state_codec.rs,prefix_disk.rs,hybrid_scheduler.rs,paged_kv_cache_adapter.rs,host.rs):110.3 -> 31.0 MiB/cell,STORED_STATES = 10,Q4PSv5 +MLX_QWEN4_PREFIX_STATE_FP32=1opt-out bound into persistent fingerprint).rusagecap fixes, KV growth-cliff fix (single-extent cap before reclaim + index-plane accounting), 4-lane coalescing & 100 ms layer-sliced resumable prefill,1|4|24decode flush schedule (zeroing Host Bubble Welcome to mlx-node Discussions! #2), asynccell_from_caches, and fallible cross-stream synchronization with fault latching.mlx_gpu_core_count(),device.get_architecture_gen(), andmetal::is_nax_available()).Live E2E Benchmarks (Real 104 GiB
Qwen3.8-Flash-Next-UD-Q4_K_XLon M5 Max 128 GB)Autoregressive (AR) & Prefill
title_450(load → 1st turn)6,325 ms(host-gate)230 ms/68.5 t/s(direct)chat_1.5kgreedy (T=0.0)4,522 ms(327 t/s)877 ms(1,687 t/s) /61.2 t/schat_inc_4.5k(prefix hit)5,388 ms/40.5 t/s1,599 ms(1,907 t/s) /58.3 t/swork_cold_12k(9.5K)11,522 ms(823 t/s)5,107 ms(1,857 t/s) /58.5 t/swork_cold_34k(38.7K)39.8 s/45.1 t/s22.3 s(1,735 t/s) /58.0 t/swork_cold_64k(75.8K)82.5 s/46.0 t/s46.5 s(1,632 t/s) /57.6 t/swork_inc_65k(79.5K)5.89 s/26.7 t/s(blk=5011)2.42 s/57.6 t/s(blk=2792)swarm_4L(165K, 4 concurrent)172.8 s(0hits,34mem drops)~94 s(3hits,9states,0drops)vision_chat_3k(2 JPGs)1,893 ms/57.7 t/sDFlash2 Speculative Decode (bit-exact with AR greedy)
chat_1.5kgreedy (T=0.0)2,283 ms/36.8 t/s852 ms/100.7 t/schat_1.5kT=1 k=0789 ms(1,874 t/s) /110.5 t/schat_1.5kDEF (T=1 k=20)1,875 ms/40.9 t/s789 ms/83.1 t/swork_cold_12k14.6 s/58.5 t/s5.26 s(1,804 t/s) /94.4 t/swork_cold_34k(38.7K)62.2 s/32.6 t/s22.8 s(1,697 t/s) /118.8 t/sswarm_4L(165K, 4 concurrent)263.2 s95.2 s(solo tail91.4 t/s)PagedAttention RAM + Persistent SSD Prefix Cache (
Q4PSv5)9,485tok,T=0.0)5,535 ms58.1 t/s3states (97.8 MB),refused=0138.2 ms60.3 t/sprefixRestored=31,837 → 375.9 ms58.2 t/s169.1 ms58.4 t/sadopted=5,diskRestored=1,droppedChain=0, bit-exact with RAM hitVerification
cargo fmt --check&cargo clippy -p mlx-paged-attn -p mlx-sys -p mlx-core --lib --tests -- -D warnings: 0 warningsbridge_metallib_names: 12/12 passed (all 152qwen4_*Metal kernels verified present + compiling live pipelines)mlx-paged-attn: 259 passed;kernels::qwen4: 18 passed (both Metal 4 NAX &MLX_QWEN4_FORCE_NON_NAX=1)models::qwen4_exp: 543 passed;engine::hybrid_scheduler: 120 passed;engine::scheduler: passedpackages/lm,inference-host,metallib-select): 11/11 files, 562/562 tests passedcrates/mlx-sys/mlxsubmodule verified 100% clean