Skip to content

feat(qwen3,qwen3_moe): support Qwen3-MoE, Qwen3 dense GGUF/quant, Metal kernel reuse, and Splash MoE optimizations - #454

Merged
Brooooooklyn merged 6 commits into
mainfrom
feat/qwen3-moe
Dec 1, 2026
Merged

Brooooooklyn merged 6 commits into
mainfrom
feat/qwen3-moe

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Closes #91

Overview

Adds full support for Qwen3-MoE (qwen3_moe / GGUF qwen3moe) and quantized / GGUF dense Qwen3 (qwen3 / GGUF qwen3), 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 --> Q3D
Loading

Key Changes

1. Issue #91 Fixes (Qwen3-MoE + Chat Templates + Quantized Qwen3)

  • Qwen3-MoE (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-layer mlp.gate overrides, Sym8, MXFP, FP8) / direct qwen3moe GGUF, mixed dense+MoE layers (mlp_only_layers, decoder_sparse_step), YaRN RoPE, QKV-only bias.
  • Dense Qwen3 Quantized & GGUF (crates/mlx-core/src/models/qwen3/): Dual-backend Qwen3Inner keeps BF16/F32 checkpoints 100% GRPO/SFT trainable (Qwen3Layers::Dense) while routing quantized & GGUF checkpoints through Qwen3Layers::Inference with Tiled64, fused Q|K|V → Q|K → K|V, cross-layer residual Add+RMSNorm, qk_norm_rope_offsets, ForceD128, _swiglu/_res store epilogues, and M5 Max INT8 prefill.
  • Tokenizer / MiniJinja (crates/mlx-core/src/tokenizer.rs): Fixed none is iterable (cannot calculate length of value of type none) and missing map .keys() / .values() methods required by Qwen3-Coder tool-calling templates; added 4 byte-exact HF transformers==5.5.0 golden fixtures.
  • Converter & GGUF Planning (convert.rs, utils/gguf.rs, utils/gguf_plan.rs, engine/checkpoint_source.rs): Added Qwen3MoeRecipe & Qwen3Recipe, legacy Qwen3.5 hybrid "qwen3" guard, block_count bounds checks, sidecar & prepared-overlay validation for layer count and tie_word_embeddings vs output.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 of ensure_aligned_contiguous across 8 Metal dispatchers.
  • crates/mlx-sys/src/metal/common/moe_common.h: Extracted shared bit-identical BF16/Q5/K-reduce device helpers across qwen4_moe.metal and inkling2_moe.metal.
  • Routed-only Splash MoE (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 router AsType dispatches/token).
    • Coalesced @t64 M=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 than splash_column and 33×–44× faster than SwitchGLU.
    • Generalized routed-only persistent workspace with unified r_max key while keeping the legacy shared-MoE workspace guard intact for Qwen3.5-35B-A3B.
  • Grouped Striped Paged Decode (mlx_paged_dispatch.cpp): Widened ForceD128 allowlist to all 7 Qwen3 Dense/MoE head geometries and fixed hardcoded 32-head scratch budget in mlx_paged_grouped_d128_max_stripes.
  • Last-token logit projection (models/forward.rs): Slice last token before final_norm + lm_head during prefill.

3. TypeScript & CLI Registration

  • Registered qwen3_moe and dense qwen3 GGUF across packages/lm, packages/server, packages/cli, and packages/dashboard, with split-GGUF discovery matching native resolve_main_parts + merge_split_headers.

Verification

Real Unsloth GGUF Matrix (Apple M5 Max, 12/12 PASS)

Checkpoint Decode ~2K Prefill Tools / Concurrent
Qwen3-0.6B-Q4_K_M 339.9 tok/s 25,458 tok/s OK
Qwen3-0.6B-UD-Q5_K_XL 324.0 tok/s 11,286 tok/s OK
Qwen3-1.7B-Q8_0 129.7 tok/s — OK
Qwen3-1.7B-Q4_0 224.0 tok/s — OK
Qwen3-1.7B-IQ4_XS 185.2 tok/s — OK
Qwen3-1.7B-UD-Q3_K_XL 197.0 tok/s — OK
Qwen3-1.7B-UD-IQ2_XXS 176.9 tok/s — OK
Qwen3-4B-UD-Q4_K_XL 91.9 tok/s 4,736 tok/s Tools OK
Qwen3-8B-UD-Q4_K_XL 77.9 tok/s 3,478 tok/s Tools OK, Concurrent exact
Qwen3-14B-UD-Q4_K_XL (40q/8kv) 30.4 tok/s 643 tok/s Tools OK, Concurrent exact
Qwen3-30B-A3B-Instruct-2507-UD-Q4_K_XL 102.4 tok/s 4,944 tok/s Tools OK, Concurrent exact (+17.3% M1 fast path)
Qwen3-Coder-30B-A3B-Instruct-IQ4_XS 68.4 tok/s 2,730 tok/s XML Tools OK, Concurrent exact

Issue #91 Repro & Automated Suites

  • Exact Issue model_type "qwen3_moe" is rejected by loadModel() #91 repro (Q4-converted dense Qwen3-0.6B + sharded 4-bit qwen3_moe with 8-bit mlp.gate overrides + Qwen3-Coder template + tool calling): PASS
  • Rust suites (qwen3, qwen3_moe, gguf_plan, checkpoint_source, tokenizer, splash_moe_routed_parity, bridge_metallib_names): PASS
  • TypeScript checks (yarn typecheck, yarn lint, 423/423 GGUF/registry/discovery/CLI/dashboard tests): PASS
  • Completed 5 rounds of Codex adversarial review and fixed all in-scope defects.

…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)
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T18:39:57.066026Z f59f4cc PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8b973821-dea8-4d94-b0d2-740628d1f59e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@Brooooooklyn
Brooooooklyn merged commit 05be3df into main Dec 1, 2026
22 checks passed
@Brooooooklyn
Brooooooklyn deleted the feat/qwen3-moe branch December 1, 2026 02:06
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.

model_type "qwen3_moe" is rejected by loadModel()

1 participant