Skip to content

feat(qwen4-moe): add Qwen4-35B-A3B AIE2P NPU backend, MTP speculative decoding & 16GB imatrix quantization - #1267

Closed
This-Is-NPC wants to merge 2 commits into
ROCm:mainfrom
This-Is-NPC:feat/qwen4-moe-npu
Closed

This-Is-NPC wants to merge 2 commits into
ROCm:mainfrom
This-Is-NPC:feat/qwen4-moe-npu

Conversation

@This-Is-NPC

Copy link
Copy Markdown

Summary

Adds full Qwen4-35B-A3B support for AMD XDNA2 (AIE2P) NPUs in FastFlowLM, including:

  1. Native AIE2P NPU Backend (qwen4_moe_npu):
    • Hybrid 40-layer architecture (30 Gated DeltaNet + 10 QSA Indexed Full Attention), Hyper-Connections (4-stream INT4/INT8/BF16 AVX-512), mmap-backed PLE n-gram table (ple_ngram.q4nx), and 256-expert sparse MoE (top-8).
    • Stateless 8-token Q4_K NPU GEMM (q4k_mm8.xclbin) + Q4_K LM head (lm_head_q4k.xclbin) + prefill/decode xclbins in src/xclbins/Qwen4-35B-A3B-NPU2/.
  2. Native MTP (blk.40) Speculative Decoding:
    • Async NPU × CPU verify overlap (overlapping lin_qkv_gate / sh_ug / sh_down on NPU with alpha/beta_proj, router, and AVX512-VNNI routed MoE on CPU).
    • Multilingual frequency-ordered top-64K draft LM head inside mtp.q4nx (100% Latin/accented coverage, 0% vocab loss via full 248K verify).
    • Confidence-based adaptive draft_k and exact Leviathan et al. (2023) rejection sampling for temperature > 0 (top_k / top_p).
  3. Quality-Preserving imatrix Mixed-Precision Quantization (Q3_A64 & Q2_A32_256) + Offline Pre-Quantizer:
    • Keeps 100% of sensitive layers (all dense/attn/DeltaNet/shared/routers/HC/PLE/lm_head/MTP-dense) in lossless Q4_K/BF16 (~1.3 GiB pinned NPU BOs on 16 GB profiles!).
    • Native streaming AVX-512 offline quantizer (qwen4_q4nx_quantizer.cpp + quantize_qwen4_q4nx.py) with live NPU imatrix calibration (FLM_QWEN4_DUMP_IMATRIX), pre-quantized .q4nx direct load (zero env vars), and automatic RAM-aware precision selection.

Published Hugging Face Model Packages (4 Variants)

(Maintainers are welcome to mirror these repositories under FastFlowLM/* and update URLs in src/model_list.json as desired.)

FLM Tag Hugging Face Repo Quantization model.q4nx Host RSS (--ctx-len 4096) Total RAM (incl. NPU BOs) Startup Load MTP Acceptance Throughput
qwen4-moe:35b-a3b This-Is-NPC/Qwen4-35B-A3B-NPU2 Lossless Q4_K (+ auto-RAM fallback) 21.0 GiB — ~19.8 GiB 40.8 s 62.9% (2.31 tok/cyc) Up to 20.2 tok/s peak
qwen4-moe:35b-a3b-q3 This-Is-NPC/Qwen4-35B-A3B-NPU2-Q3 Mixed Q3_A64 (4..35) + imatrix 16.93 GiB 11.91 GiB ~16.4 GiB 68.6 s 60.3%–61.8% (2.28) 14.62 tok/s sustained / 22.4 peak
qwen4-moe:35b-a3b-16gb This-Is-NPC/Qwen4-35B-A3B-NPU2-16GB Q2_HQ (down=Q3, up/gate=Q2_A32) + imatrix 14.33 GiB 12.83 GiB ~14.1 GiB (~1.3 GiB pinned) 28.8 s 62.2% (2.22) 13.3+ tok/s (0 swap on 16 GB PCs)
qwen4-moe:35b-a3b-16gb-lite This-Is-NPC/Qwen4-35B-A3B-NPU2-16GB-Lite Q2_A32_256 (4..35) + imatrix 13.46 GiB 11.95 GiB ~13.2 GiB (~1.3 GiB pinned) 32.7 s 63.0% (2.20) 14.25 tok/s sustained (0 swap on 16 GB PCs)

(Measured live on AMD Ryzen AI 5 340 Krackan Point XDNA2, 33.17 GB/s STREAM DRAM bandwidth — non-speculative 1-token Q4_K baseline: 9.9 tok/s; chat mode temp=0.7 with exact rejection sampling: 14.00 tok/s sustained / 27.12 tok/s peak.)


Verification

  • Unit & Parity Tests (make -C flm-dev-kit/FLM_DLL/detail/qwen4_moe_npu test): 13/13 PASS
    • End-to-end tiny Qwen4 HF PyTorch parity: cosine=0.999998, max_abs_diff=0 (prefill vs token-by-token)
    • Q8_0 -> Q4_K LM head + HC Q4 (lm_cos=0.997505)
    • Q3_A64 AVX512-VNNI (cos=0.984744) & Q2_A32_256 AVX512-VNNI (cos=0.9356)
  • Live NPU Hardware Tests (/dev/accel/accel0):
    • All 4 variants tested end-to-end with zero environment variables (flm run qwen4-moe:35b-a3b* --ctx-len 4096), verifying accurate multilingual reasoning, arithmetic, and Python code generation with zero swap on 16 GB RAM budget.

@tawei-amd
tawei-amd requested a review from zaneni6 December 5, 2026 20:08
@tawei-amd

Copy link
Copy Markdown
Collaborator

qwen4 35b has been merged. how about some optimization work on it?

This is great contribution; we really appreciate it 🙏

@This-Is-NPC

Copy link
Copy Markdown
Author

Hi @tawei-amd, thanks a lot for the kind note and congrats on getting #1266 merged!

I have rebased and ported the two main optimizations cleanly onto the merged qwen4_moe_npu engine in a fresh, focused PR: #1269.

Specifically, #1269 adds (100% backward-compatible with the official FastFlowLM/Qwen4-35B-A3B-NPU2 package, vision tower, prefix cache, and causal_lm vtable ABI):

  1. 16 GB / 24 GB mixed-precision imatrix Host-MoE (Q3_A64 / Q2_A32_256 AVX-512BW+VNNI) + async NPU×CPU overlap on the existing persistent decode.xclbin (cutting pinned NPU BOs from 21.35 GB → 2.73 GB and boosting real-world throughput on 18 GiB-available laptops from 0.53 → 7.25 tok/s).
  2. Optional native MTP (blk.40) speculative decoding with O(1) recurrent-state rollback and exact Leviathan et al. stochastic rejection sampling (60.4% acceptance / 2.21 tok/cycle at Qwen4's default temp=1.0, top_p=0.95, top_k=20, ~16–20 ms draft, zero extra BOs/xclbins).

Closing this PR in favor of #1269 — would love your thoughts there!

@This-Is-NPC This-Is-NPC closed this Dec 5, 2026
@This-Is-NPC
This-Is-NPC deleted the feat/qwen4-moe-npu branch December 5, 2026 22:29
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.

2 participants