Repository navigation
[Engine] qwen4: add opt-in --dense-requant with Q4_K quantizer and never-upsize guard - #501
Draft
NightRain233 wants to merge 1 commit into
Draft
NightRain233 wants to merge 1 commit into
NightRain233 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 Residualhc_*, shared expertsffn_*_shexp, andoutput.weight) remainQ5_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]):quantize_q4_K_ref(engine/include/strata/quant/act.hpp,engine/src/quant/act_ref.cpp), verified byte-identical (memcmp == 0) to ggml'squantize_row_q4_K_ref.eh_proj, and optionallyoutput.weightwith+head) during load with a strict never-upsize guard (out > 0 && out < bytes, falling back cleanly tokAnyMatwithout expanding native quantized tensors to BF16) and tied-embedding safety (only GPUoutput_is requantized; hosttok_embd_stays intact).--cache-biassupport onqwen4exp(verified active viaEngine::route_cached).Measured on real hardware (
Qwen4-35B-A3B-UD-IQ2_XS, 192 tokens, integrated R3 stackbfd5d1b):--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 hit93.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 hit93.5% -> 96.2%).--metal-memory balanced:19.12 tok/s(lossless) -> 21.48 tok/s (--dense-requant q4_k, +12.3%).--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).ORCHID-8814) 100% exact recall underq4_k+head, and Python code generation remains coherent and accurate.--no-draft:111.24 -> 116.89 tok/s(q4_k, +5.1%) / 118.88 tok/s (q4_k+head, +6.9%).155.75 -> 159.64 tok/s(q4_k+head, +2.5%).Related discussion
Tracking issue: #472. Strictly opt-in: when
--dense-requantis omitted (oroff/none),dense_type_ == 0,shexp_want() == kAnyMat, andhead_want() == kAnyMat, leaving the default path byte-identical.Approach
Engine::upload_as()(kGuessMatpath) with explicitif (want == kGuessMat) want = kAnyMat;fallback whenever the never-upsize guard skips requantization orn_inis unaligned.origin/main(7cef952) behavior including Looped Qwen4 slot accounting andlogits_dump.cppcompatibility.Validation
a6f15ceonorigin/main7cef952):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-layertiny-qwen4exp.ggufuntied & tied requant / never-upsize /--cache-bias/ CLI rejection tests all passed).logits_dump.cppsyntax check (/usr/bin/c++ -fsyntax-only -Wall -Wextra): passed (0 warnings/errors).python3 tools/check --only docs: passed (6/6).backends.yml/check.yml).Limits