Repository navigation
feat(qwen3,qwen3_moe): support Qwen3-MoE, Qwen3 dense GGUF/quant, Metal kernel reuse, and Splash MoE optimizations - #454
Conversation
…ernel reuse, and Splash MoE optimizations
…unt bounds, C++ move UB, split hybrid scan, scalar arch detect, QKV-only bias)
…config layer count check, strict split GGUF discovery parity)
…ore shared-MoE workspace guard, validate sidecar/overlay tie_word_embeddings and layer count)
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. |
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
… sym8 test loads on M5+/SM80
Closes #91
Overview
Adds full support for Qwen3-MoE (
qwen3_moe/ GGUFqwen3moe) and quantized / GGUF dense Qwen3 (qwen3/ GGUFqwen3), shares Metal/C++ and Rust kernel helpers across families, and optimizes both decode and prefill on Apple Silicon (M5 Max).flowchart TD subgraph Sources["Checkpoint & GGUF Sources"] SF["SafeTensors (BF16 / FP8 / MLX Q4-Q8 / Sym8 / MXFP / v5 3D)"] GGUF["GGUF (qwen3moe & dense qwen3, split + single)"] end subgraph Core["Shared Rust + Metal Reuse"] MH["mlx_metal_helpers.h (ensure_aligned_contiguous, 8 dispatchers)"] MC["common/moe_common.h (BF16 / Q5 / K-reduce across Qwen4 & Inkling2)"] SM["Splash MoE Routed (M=1 coalesced @t64 + persistent workspace + BF16 router)"] PD["Paged Decode ForceD128 (all 7 Qwen3 head geometries)"] AC["PagedAttentionCore (fused QKV/QK/KV + qk_norm_rope_offsets + MPP cache-hit prefill)"] end subgraph Models["Model Families"] Q3M["Qwen3MoeModel (Stage 0/1/2 + ChatSession + Tools)"] Q3D["Qwen3Model (Dense GRPO/SFT trainable + Optimized Inference backend)"] end SF --> Core GGUF --> Core Core --> Q3M Core --> Q3DKey Changes
1. Issue #91 Fixes (Qwen3-MoE + Chat Templates + Quantized Qwen3)
crates/mlx-core/src/models/qwen3_moe/): Full Stage 0/1/2 (ChatBackend,PagedBackend,HybridSchedulerBackend), HF per-expert / Transformers v5 fused 3D (.weight+ suffixless) / MLX quantized (4/8-bit with per-layermlp.gateoverrides, Sym8, MXFP, FP8) / directqwen3moeGGUF, mixed dense+MoE layers (mlp_only_layers,decoder_sparse_step), YaRN RoPE, QKV-only bias.crates/mlx-core/src/models/qwen3/): Dual-backendQwen3Innerkeeps BF16/F32 checkpoints 100% GRPO/SFT trainable (Qwen3Layers::Dense) while routing quantized & GGUF checkpoints throughQwen3Layers::InferencewithTiled64, fusedQ|K|V→Q|K→K|V, cross-layer residual Add+RMSNorm,qk_norm_rope_offsets,ForceD128,_swiglu/_resstore epilogues, and M5 Max INT8 prefill.crates/mlx-core/src/tokenizer.rs): Fixednone is iterable(cannot calculate length of value of type none) and missing map.keys()/.values()methods required byQwen3-Codertool-calling templates; added 4 byte-exact HFtransformers==5.5.0golden fixtures.convert.rs,utils/gguf.rs,utils/gguf_plan.rs,engine/checkpoint_source.rs): AddedQwen3MoeRecipe&Qwen3Recipe, legacy Qwen3.5 hybrid"qwen3"guard,block_countbounds checks, sidecar & prepared-overlay validation for layer count andtie_word_embeddingsvsoutput.weight, and K-quant embedding loading (load_embedding_quantized_or_bf16).2. Metal / C++ Kernel Reuse & Splash MoE Optimizations
crates/mlx-sys/src/metal/mlx_metal_helpers.h: Deduplicated 10 copies ofensure_aligned_contiguousacross 8 Metal dispatchers.crates/mlx-sys/src/metal/common/moe_common.h: Extracted shared bit-identical BF16/Q5/K-reduce device helpers acrossqwen4_moe.metalandinkling2_moe.metal.splash_moe/abi.h,moe.metal,mlx_splash_moe.cpp,moe/splash.rs):SPLASH_MOE_NO_SHARED,SPLASH_MOE_OUT_BF16,SPLASH_MOE_ROUTER_F32_MATH(eliminates 48 routerAsTypedispatches/token).@t64M=1 fast path (splash_moe_m1_gate_up+splash_moe_m1_down_combine): 59.9 µs/layer cold DRAM (~390 GB/s) / 46.5 µs/layer warm SLC — 1.68×–1.99× faster thansplash_columnand 33×–44× faster thanSwitchGLU.r_maxkey while keeping the legacy shared-MoE workspace guard intact for Qwen3.5-35B-A3B.mlx_paged_dispatch.cpp): WidenedForceD128allowlist to all 7 Qwen3 Dense/MoE head geometries and fixed hardcoded 32-head scratch budget inmlx_paged_grouped_d128_max_stripes.models/forward.rs): Slice last token beforefinal_norm+lm_headduring prefill.3. TypeScript & CLI Registration
qwen3_moeand denseqwen3GGUF acrosspackages/lm,packages/server,packages/cli, andpackages/dashboard, with split-GGUF discovery matching nativeresolve_main_parts+merge_split_headers.Verification
Real Unsloth GGUF Matrix (Apple M5 Max, 12/12 PASS)
Qwen3-0.6B-Q4_K_MQwen3-0.6B-UD-Q5_K_XLQwen3-1.7B-Q8_0Qwen3-1.7B-Q4_0Qwen3-1.7B-IQ4_XSQwen3-1.7B-UD-Q3_K_XLQwen3-1.7B-UD-IQ2_XXSQwen3-4B-UD-Q4_K_XLQwen3-8B-UD-Q4_K_XLQwen3-14B-UD-Q4_K_XL(40q/8kv)Qwen3-30B-A3B-Instruct-2507-UD-Q4_K_XLQwen3-Coder-30B-A3B-Instruct-IQ4_XSIssue #91 Repro & Automated Suites
Qwen3-0.6B+ sharded 4-bitqwen3_moewith 8-bitmlp.gateoverrides +Qwen3-Codertemplate + tool calling): PASSqwen3,qwen3_moe,gguf_plan,checkpoint_source,tokenizer,splash_moe_routed_parity,bridge_metallib_names): PASSyarn typecheck,yarn lint, 423/423 GGUF/registry/discovery/CLI/dashboard tests): PASS