Skip to content

[Engine] qwen4: add opt-in --dense-requant with Q4_K quantizer and never-upsize guard - #501

Draft
NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-dense-requant
Draft

NightRain233 wants to merge 1 commit into
simplexai-labs:mainfrom
NightRain233:feat/qwen4-dense-requant

Conversation

@NightRain233

Copy link
Copy Markdown
Contributor

Problem and result

In low-bit MoE GGUF packs like Qwen4-35B-A3B-UD-IQ2_XS, routed experts are compressed to ~2.5 bpw (IQ2_XS/IQ2_S/IQ3_XXS), while dense projections (attn_*, ssm_*, Gated Residual hc_*, shared experts ffn_*_shexp, and output.weight) remain Q5_K/Q6_K/Q8_0 (~1.87 GiB, accounting for 81% of per-token decode memory traffic on 16 GB Apple M4 and consuming VRAM that could otherwise cache hundreds more routed experts).

This PR adds a strictly opt-in load-time dense weight re-quantization flag for Qwen4 (--dense-requant <q4_k|iq4_nl|q4_0|q5_0|q5_k>[+head]):

  1. Adds quantize_q4_K_ref (engine/include/strata/quant/act.hpp, engine/src/quant/act_ref.cpp), verified byte-identical (memcmp == 0) to ggml's quantize_row_q4_K_ref.
  2. Re-quantizes Qwen4 dense projections (GR, GDN/Attention, shared experts, MTP eh_proj, and optionally output.weight with +head) during load with a strict never-upsize guard (out > 0 && out < bytes, falling back cleanly to kAnyMat without expanding native quantized tensors to BF16) and tied-embedding safety (only GPU output_ is requantized; host tok_embd_ stays intact).
  3. Also documents --cache-bias support on qwen4exp (verified active via Engine::route_cached).

Measured on real hardware (Qwen4-35B-A3B-UD-IQ2_XS, 192 tokens, integrated R3 stack bfd5d1b):

  • VRAM savings & extra experts:
    • --dense-requant q4_k: requantizes 382 tensors, saves 403.4 MiB VRAM -> +429 extra routed experts in VRAM on 16 GB M4 (4,084 -> 4,513, VRAM hit 93.5% -> 95.8%).
    • --dense-requant q4_k+head: requantizes 383 tensors, saves 528.4 MiB VRAM -> +555 extra routed experts in VRAM on 16 GB M4 (4,084 -> 4,639, VRAM hit 93.5% -> 96.2%).
  • Apple M4 16 GB:
    • Out-of-the-box --metal-memory balanced: 19.12 tok/s (lossless) -> 21.48 tok/s (--dense-requant q4_k, +12.3%).
    • Frozen profile --no-draft: 13.95 tok/s (R2) / 17.54 tok/s (R3 lossless) -> 20.23 tok/s (R3 + q4_k+head, +15.3% over R3 lossless / +45.0% over R2).
    • Quality check: 32K NIAH (ORCHID-8814) 100% exact recall under q4_k+head, and Python code generation remains coherent and accurate.
  • Apple M5 Ultra 256 GB (100% full VRAM):
    • --no-draft: 111.24 -> 116.89 tok/s (q4_k, +5.1%) / 118.88 tok/s (q4_k+head, +6.9%).
    • Embedded MTP: 155.75 -> 159.64 tok/s (q4_k+head, +2.5%).

Related discussion

Tracking issue: #472. Strictly opt-in: when --dense-requant is omitted (or off/none), dense_type_ == 0, shexp_want() == kAnyMat, and head_want() == kAnyMat, leaving the default path byte-identical.

Approach

  • Reuses Engine::upload_as() (kGuessMat path) with explicit if (want == kGuessMat) want = kAnyMat; fallback whenever the never-upsize guard skips requantization or n_in is unaligned.
  • Preserves all origin/main (7cef952) behavior including Looped Qwen4 slot accounting and logits_dump.cpp compatibility.

Validation

  • Passed (Apple M4 16 GB, macOS, commit a6f15ce on origin/main 7cef952):
    • Metal Release configure + build (strata-poly, poly_memory_test, poly_memory_growth, poly_qwen4_parity, poly_tiny_model_fixture): 0 errors, 0 warnings.
    • ctest --test-dir engine/build-metal -R '^(poly_memory_test|poly_metal_defaults|poly_memory_growth|poly_qwen4_parity)$' --output-on-failure: 10/10 passed (quantize_q4_K_ref: vs_ggml_max_diff=0, RMSE=0.007585; synthetic 2-layer tiny-qwen4exp.gguf untied & tied requant / never-upsize / --cache-bias / CLI rejection tests all passed).
    • logits_dump.cpp syntax check (/usr/bin/c++ -fsyntax-only -Wall -Wextra): passed (0 warnings/errors).
    • python3 tools/check --only docs: passed (6/6).
  • Failed: None observed.
  • Not run: CUDA, HIP, SYCL, Linux, and Windows builds were not run locally on this Apple Silicon host. Kept as Draft pending cross-backend CI (backends.yml / check.yml).

Limits

  • Adds ~15–20 seconds of one-time CPU requantization overhead during model load (trade-off for ~403–528 MiB permanent VRAM savings and faster decode; opt-in only).
  • Cross-backend compilation on CUDA/HIP/SYCL/MSVC must be confirmed via CI before marking Ready for Review.

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