Skip to content

feat(qwen4): add optional Looped-Qwen4 recurrent-depth sidecar support - #1270

Open
This-Is-NPC wants to merge 2 commits into
ROCm:mainfrom
This-Is-NPC:feat/qwen4-looped-depth
Open

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

Conversation

@This-Is-NPC

Copy link
Copy Markdown

Summary

Adds optional Open-Channel v4 recurrent-depth ("Looped-Qwen4") support to the qwen4_moe_npu (q4m) engine — enabling T = 2 or T = 3 recurrent passes over the middle 16 layers (L12..L27) using the pure-recurrence, zero-LoRA 22.8 MiB .safetensors sidecar from sliderforthewin/Looped-Qwen4-35B-A3B (MIT, blog).

  • Zero changes to model.q4nx or NPU xclbins: works directly on top of the official FastFlowLM/Qwen4-35B-A3B-NPU2 checkpoint, decode.xclbin, prefill_overlay.xclbin, vision tower, and causal_lm vtable ABI (_ZTV13qwen4_moe_npu == 144 bytes unchanged).
  • Zero overhead when inactive: if no sidecar is present (or FLM_QWEN4_LOOP_PASSES=1), loop_->active() is false, no extra KV/GDN pass caches are allocated, and model::forward executes the exact upstream single-pass loop.
  • Security-first loader: reads only raw binary tensors + JSON metadata via q4nx_file (zero pickle, zero script execution), strictly validating shapes, dtypes, and PLE_LAYER < core_start < core_end <= NL.

Key Changes

  • New isolated module (qwen4_moe_loop.hpp / qwen4_moe_loop.cpp):
    • Auto-discovers <model_dir>/looped.safetensors or <model_dir>/loop/loop_adapter_mlx.safetensors, or loads an explicit path via FLM_QWEN4_LOOP_ADAPTER.
    • Supports all published Looped-Qwen4-35B-A3B sidecar layouts (loop/, sushi/default_t{2,3}, and mlx/default_t{2,3} with *_tanh keys + delta_rms_match: true).
    • Implements bit-exact FP32 per-stream RMS-matched HCWorkspaceEntry, HCCrossPassMixer (L15, L19, L23, L27), and optional LoopCD-Hidden extrapolation (FLM_QWEN4_LOOP_CD_OMEGA, default 0.0) using BF16 AVX-512 low-rank projections (0.72 ms/token total overhead for all 5 mixers combined).
  • Per-pass recurrent state isolation (qwen4_moe_host.{hpp,cpp}, qwen4_moe_forward.cpp):
    • Allocates per-pass KV caches (4 core attention layers) and GDN recurrent states (12 core DeltaNet layers) only when looping is active.
    • Uses an exception-safe O(1) RAII pointer swap (pass_swap_guard) around core passes p = 1 .. T - 1 so attn_block and gdn_block stay untouched.
    • Saves and restores extra pass states in snapshot (model::save / load / load_rows) so prefix caching (flm_state_api) works seamlessly with looped inference.
  • Minor Linux GCC 14+/16 build compatibility: #include <climits> in npu_cmd.hpp and overridable CXX ?= g++-13 in Makefile.

Usage (Zero 42 GB Model Duplication)

# 1. Download ONLY the 22.8 MiB MIT-licensed sidecar into the official Qwen4 model folder:
hf download sliderforthewin/Looped-Qwen4-35B-A3B \
  loop/loop_adapter_mlx.safetensors loop/loop_adapter_mlx.json \
  --local-dir ~/.config/flm/models/Qwen4-35B-A3B-NPU2

# 2. Run Qwen4 with T=3 recurrent passes (or FLM_QWEN4_LOOP_PASSES=2, or =1 to disable):
FLM_QWEN4_LOOP_PASSES=3 flm run qwen4:35b-a3b

Verification

  1. Bit-for-bit Numerical Parity (vs pure-NumPy reference of SUSHI_QWEN4_35B.md / vllm_looped_qwen38 on real loop_adapter_mlx.safetensors, SHA-256 c589e2b9...):
    • workspace_entry(p=1): max_abs = 2.384e-07, rel_l2 = 1.034e-08
    • workspace_entry(p=2): max_abs = 2.384e-07, rel_l2 = 1.258e-08
    • cross_pass_mix(p=1, L15): max_abs = 1.192e-07, rel_l2 = 4.352e-08
    • cross_pass_mix(p=2, L27): max_abs = 1.192e-07, rel_l2 = 4.461e-08
    • apply_loop_cd(w=0.15): max_abs = 7.153e-07, rel_l2 = 7.528e-08
    • Single-token full-pass overhead (1 entry + 4 mixers, 4 decode threads): 0.719 ms
  2. ABI & Build:
    • Built libqwen4_moe_npu.so and src/build/flm cleanly with zero errors.
    • Verified readelf -sW _ZTV13qwen4_moe_npu == 144 bytes (exact ABI match).
  3. Live NPU End-to-End (/dev/accel/accel0, official FastFlowLM/Qwen4-35B-A3B-NPU2):
    • Ran FLM_QWEN4_LOOP_PASSES=3 (72 effective layers/token) across both prefill_overlay.xclbin (66 tokens, 7.69 tok/s, TTFT 8.72 s) and persistent decode.xclbin with clean output ("The ball costs $0.05 (since the bat costs $1.05, making the total $1.10 and the difference $1.00).").

This branch has not been deployed

No deployments
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.

1 participant