Repository navigation
fix(gptaq,foem,gguf): unblock MoE experts and warn on K-quant aliases (#4067) - #4080
Merged
Merged
Conversation
This was referenced Nov 15, 2026
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.
Fixes #4067
TL;DR
ModulePlanand quantize withdXXT = 0(plain GPTQ update for GPTAQ, FOEM beta term retained); dense and shared experts keep the native delta.q*_k_s/q*_k_m/q*_k_lnow warn loudly that they store a uniform K-quant, andq5_k_sreports 5.5 bpw (matching the Q5_K blocks actually written).staging_dtype=float16no longer produces an inf/NaN Hessian.README.md.Net: 11 files, +584/-27.
1. The problem
1.1 GPTAQ/FOEM on MoE
NativeProcessorrecords each module input during one full-precision forward (gptqmodel/looper/native_processor.py:118). During the quantized replay of the same layer, attention is already quantized, so the router sends a different token subset to each routed expert.GPTAQ.process_batch(gptqmodel/quantization/gptaq.py:79) andFOEM.process_batch(gptqmodel/quantization/foem.py:160) popped the recorded native entries by call order:flowchart TD A["full-precision forward"] --> B["NativeProcessor records expert inputs per sample"] C["quantized replay of the same layer"] --> D["attention is already quantized"] D --> E["router selects a different token subset"] E --> F["GPTAQ/FOEM pop native entry by call order"] F --> G{"row counts match"} G -- no --> H["RuntimeError in dXXT matmul"] G -- yes --> I["rows from different tokens"] I --> J["silent dXXT corruption"] style H fill:#f8d7da,stroke:#b02a37,color:#842029 style J fill:#f8d7da,stroke:#b02a37,color:#842029A second path: an expert that receives no tokens still reached
GPTAQ.quantize, which had no zero-sample fallback and readself.H.device(gptqmodel/quantization/gptaq.py:161) whileHwasNone.FOEM.quantizehad the same gap.1.2 GGUF K-quant quality aliases
_GGUF_BITS_ALIAS_TO_TENSOR_QTYPE(gptqmodel/nn_modules/qlinear/gguf.py:103) mapsq4_k,q4_k_sandq4_k_mtoQ4_K, and likewise forq2_k_*,q3_k_*,q5_k_*. The parsedqualitysuffix was never used outsideconfig.py. llama.cpp's_Mmixture uses Q6_K forattn_v/ffn_downinuse_more_bitslayers, so the saved checkpoint was lower quality than its name implied. The bpw table also reportedq5_k_sas 5.0 while it writes Q5_K (5.5 bpw).1.3 fp16 Hessian staging overflow
preferred_staging_dtype(gptqmodel/quantization/gptq.py:842) returnedtorch.float16for a bf16 input whenstaging_dtype=float16. A bf16 activation outside float16's range becameinfbefore the fp32 matmul, so the Hessian becameinf/NaN.2. The new flow
ModuleLooper.create_named_modulesresolves the layer-relative plan once and marks routed experts withstate["is_routed_expert"](gptqmodel/looper/module_looper.py:2148).NativeProcessorskips capturing native inputs for those modules (gptqmodel/looper/native_processor.py:109).GPTAQandFOEMread the mark and disable the native delta.flowchart TD A["create_named_modules"] --> B{"ModulePlan.is_expert(name)"} B -- yes --> C["NamedModule state is_routed_expert = True"] B -- no --> D["dense or shared expert"] C --> E["GPTAQ: use_native_delta = False"] C --> F["FOEM: gptaq = False, beta retained"] E --> G["H from quantized-path rows only"] F --> G G --> H{"nsamples == 0"} H -- yes --> I["RTN fallback"] H -- no --> J["GPTQ-style update with dXXT = 0"] D --> K["native delta retained"] style I fill:#d1e7dd,stroke:#0f5132,color:#0f5132 style J fill:#d1e7dd,stroke:#0f5132,color:#0f5132RuntimeErroringptaq.py:124/foem.py:208dXXT = 0; plain GPTQ updatedXXTcorruptiondXXT = 0; no native pairingAttributeErroronself.H.deviceThe alpha correction for routed experts is intentionally disabled until the native rows can be gathered with the quantized-path routing. This PR takes the issue's sanctioned fallback and states the limitation rather than leaving silent corruption.
GGUF aliases now warn once per
GGUFConfig(and per dynamic layer override):flowchart TD A["GGUFConfig bits = q4_k_m"] --> B["_warn_gguf_quality_alias"] B --> C["warn: aliases to uniform Q4_K"] C --> D["saved checkpoint still uniform Q4_K"] style C fill:#fff3cd,stroke:#997404,color:#664d03Hessian staging is range-safe:
flowchart TD A["preferred_staging_dtype input_dtype, device"] --> B{"staging_dtype == float16"} B -- no --> C["existing float32 / bfloat16 path"] B -- yes --> D{"input_dtype == bfloat16"} D -- yes --> E["bfloat16 if supported, else float32"] D -- no --> F["float16"] style E fill:#d1e7dd,stroke:#0f5132,color:#0f51323. Change inventory
gptqmodel/looper/module_looper.pyModulePlan.is_expertgptqmodel/looper/native_processor.pystateshapegptqmodel/quantization/gptaq.pygptqmodel/quantization/foem.pygptqmodel/quantization/gptq.pygptqmodel/quantization/config.pyq5_k_sbpw 5.5tests/test_moe_gptaq_foem.pytests/test_gptq_hessian_pipeline.pytests/test_weight_only.pydocs/quantizer-moe.md,README.md4. Before / after
Tiny random Qwen2-MoE (3 layers, 16 experts, top-2), 8 synthetic calibration samples,
bs=1.Before (CPU, fp32):
After (CPU, fp32; same script):
Quality on CUDA (RTX 4090, fp16, same fixture, held-out text):
All dequantized expert weights are finite. Routed experts are intentionally quantized without the GPTAQ alpha delta, so their weights differ from the plain-GPTQ control by code flips from the method's own loop (GPTAQ/FOEM do not use GPTQ's GAR); the PPL band stays within 0.8% (FOEM's beta term intentionally changes the weights).
Item 2 (CPU):
Item 3 (CPU, bf16 input with channel 3 at 1e5, chunk_size=64):
5. Tests run
CUDA (
/opt/venvs/cu130t, 4090GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42):ROCm (
/opt/venvs/rocm101t,HIP_VISIBLE_DEVICES=1, R9700):Lint:
6. Reviewer notes
dXXT = 0for experts. A follow-up can gather native hidden states with the quantized-path top-k indices and restore the alpha term._s/_m/_lmixture is not implemented; the issue accepts a loud warning as the minimum. The warning names the stored uniform type and states that the checkpoint is lower quality than its name.