OpenFlowLM-Next icon indicating copy to clipboard operation
OpenFlowLM-Next copied to clipboard

Open XDNA2 kernels for Qwen3.6-MoE decode

Open Cyronius opened this issue 2 months ago • 3 comments

Continuing the open-kernels thread from ROCm/FastFlowLM#690 — @Atomic-Germ asked (2026-09-04, in that issue) for this to land as an issue referenced from a PR, so here it is, with a PR attached rather than a proposal: Qwen3.6-MoE (35B-A3B: hybrid gated-DeltaNet / full-attention / 256-expert MoE) decoding end to end on hand-written IRON/mlir-aie kernels, with the closed qwen3_6_moe_npu engine no longer in the loop.

PR: (opening alongside this issue)

What's in it

  • 21 kernel designs (open_kernels/designs/) covering every op in a decode step — q4_1 GEMV with dequant fused into the integer matrix unit, q8 lm_head, gated DeltaNet, the conv1d/SiLU/qk-norm glue, full attention with a dynamically-patched KV window, the MoE router with on-device top-8 — plus fused whole-layer designs (layer_x/) that run either layer type as two dispatches on one xclbin context.
  • A host harness (open_kernels/harness/) that drives them from a 6-line .cfg language over the same XRT route npu_matmul.cpp already uses, plus three instruction-patching directives (moeroute2, attnpos) that let one compiled program serve every layer and every token — no per-step recompilation, ~0.04ms per patch.
  • An open weight-container reader + packer (open_kernels/model/, src/open_qwen36/) for FastFlowLM's .q4nx format, replacing q4_npu_eXpress.dll on this path, plus an independent fp64 CPU implementation of the whole decode step used as the correctness oracle.
  • A resident engine (src/open_qwen36/) implementing the app's own causal_lm interface, so the existing tokenizer/sampler/chat-template/ server code drives it exactly as it drives the closed engine. Qwen3_6_MOE::load_model picks it automatically whenever the kernels are installed for the model; FLM_QWEN36_ENGINE=open|closed forces either way.

Verified

  • All 40 layers of stock Qwen3.6-35B-A3B-NPU2 against the fp64 oracle: logits corr 0.999997, same argmax and top-5, every layer's residual ≥ 0.99992. The on-device router agreed with the oracle's own top-8 in 38 of 40 layers; the two exceptions are 8th-of-8 ties with an expert-probability gap of ~8e-5, below bf16 resolution at that magnitude.
  • Through the real app, built from scratch on a machine with none of the toolchain installed (src/build-windows-vcpkg.cmd): flm serve qwen3.6-moe:35b-a3b selects the open engine and answers chat completions correctly and deterministically at 5–7 tok/s decode, 1.4–2.2s TTFT — in the same range as the closed engine on the same hardware.

Full numbers, per-kernel timing and the traps hit along the way are in the PR and in src/open_qwen36/README.md / open_kernels/README.md once it lands.

Two things worth deciding here rather than in review

  1. Toolchain: this pins mlir-aie 1.4.2. 1.3.4, as this repo currently configures it, can't even import aie.iron (a namespace collision with eudsl-python-extras), and its IRON runtime API is different enough (rt.task_group() vs free-standing TaskGroup(), rt.fill(fifo, ...) vs fifo.fill(...)) that none of these designs run on it. The one existing design on the old API (npu_offload/matmul/matmul_whole_array.py) is ported in the first commit; all 12 embedding matmul shapes still rebuild bit-identical to the shipped binaries. If there's a reason to stay on 1.3.4 I'd want to hear it before more designs pile onto 1.4.2-only APIs.
  2. Weight format. This still reads FastFlowLM's .q4nx container (@Atomic-Germ noted in #690 that q4nx isn't the intended long-term format). The packer's transform is a byte permutation plus fp16→bf16 narrowing — nothing is dequantized or requantized — which is what would let it read GGUF Q4_1/Q8_0 directly instead; open_kernels/q4_1_pack.py is a first cut at that direction, unused by the engine yet. Worth agreeing on GGUF as the target before it's load-bearing in more places.

Not yet done

  • Batched prefill. Prompt tokens go through the decode step one at a time (exact for this architecture, since each layer's state update only ever sees one token) at ~124ms/token on the full model. The closed engine has real batch-prefill kernels; nobody has written open ones.
  • Vision. The model is a VLM; the open engine has no encoder and defers to the closed one for image inputs.
  • Attention cost at long context is untuned (~24µs per cached row per attention layer) — not a hard limit, just not looked at yet.

Credit

The kernel package is vendored from my own prior work (phlegm, tools/open-kernels/), full provenance and commit pin in open_kernels/PROVENANCE.md. The GEMV tile arithmetic and the rot13 smoke test build on @vegah's LLMNpuTest (Apache-2.0, notice preserved in the relevant file headers) — same lineage as the embedding kernels already in this repo.

Cyronius avatar Sep 05 '26 15:09 Cyronius

obviously that is an AI slop comment, so here's the human one: my goal for this was merely to port my work to the openflowlm-next repo. I have deliberately left out support for my 30 layer pruned qwen since than is unlikely to provide value for other people. If it is wanted, though, it would not take much to port it. Just let me know.

There are a lot of interesting places we can take this next that I have not done in this already massive PR.

  • I think support for different quants in gguf would be good
  • a rust rewrite of the CLI (which I have in the phelgm repo
  • ability to control from CLI which parts happen on NPU, which on GPU (high complexity work but there is a lot of value in using NPU for prefill only)

probably the highest value, though, would be to figure out how we can take these open pieces and reassemble them into optimized kernels either on the fly or through configuration. Otherwise we will have to continue creating custom kernels in order to achieve good performance, so we will always be behind on model releases. There may be an interim phase where we can at least make it an easy compile step from config to add support for other models. I'll probably investigate both of these next, if we like that direction.

Cyronius avatar Sep 05 '26 15:09 Cyronius

Great work! I squashed it, maybe I should've used ordinary merge instead - and maybe I should have waited. I just wanted to "stabilize" the repo a little. Sorry if I took too much freedom, it was in the name of progress! With everything depending on other stuff, I felt it was the right thing to do, but I will ask next time :)

vegah avatar Sep 05 '26 21:09 vegah

Closing because merged

Atomic-Germ avatar Sep 05 '26 23:09 Atomic-Germ