Skip to content

fix(gptaq,foem,gguf): unblock MoE experts and warn on K-quant aliases (#4067) - #4080

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4067-gptaqmoe
Nov 15, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4067-gptaqmoe

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4067

TL;DR

  • GPTAQ and FOEM no longer pair native and quantized routed-expert inputs by call order. Routed experts are marked from the model's ModulePlan and quantize with dXXT = 0 (plain GPTQ update for GPTAQ, FOEM beta term retained); dense and shared experts keep the native delta.
  • GPTAQ and FOEM now take GPTQ's data-independent RTN fallback when a module receives zero calibration rows.
  • GGUF q*_k_s / q*_k_m / q*_k_l now warn loudly that they store a uniform K-quant, and q5_k_s reports 5.5 bpw (matching the Q5_K blocks actually written).
  • Hessian staging never narrows a bf16 input to fp16, so staging_dtype=float16 no longer produces an inf/NaN Hessian.
  • New reference page: docs/quantizer-moe.md, linked from README.md.

Net: 11 files, +584/-27.

1. The problem

1.1 GPTAQ/FOEM on MoE

NativeProcessor records 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) and FOEM.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:#842029
Loading

A second path: an expert that receives no tokens still reached GPTAQ.quantize, which had no zero-sample fallback and read self.H.device (gptqmodel/quantization/gptaq.py:161) while H was None. FOEM.quantize had the same gap.

1.2 GGUF K-quant quality aliases

_GGUF_BITS_ALIAS_TO_TENSOR_QTYPE (gptqmodel/nn_modules/qlinear/gguf.py:103) maps q4_k, q4_k_s and q4_k_m to Q4_K, and likewise for q2_k_*, q3_k_*, q5_k_*. The parsed quality suffix was never used outside config.py. llama.cpp's _M mixture uses Q6_K for attn_v/ffn_down in use_more_bits layers, so the saved checkpoint was lower quality than its name implied. The bpw table also reported q5_k_s as 5.0 while it writes Q5_K (5.5 bpw).

1.3 fp16 Hessian staging overflow

preferred_staging_dtype (gptqmodel/quantization/gptq.py:842) returned torch.float16 for a bf16 input when staging_dtype=float16. A bf16 activation outside float16's range became inf before the fp32 matmul, so the Hessian became inf/NaN.

2. The new flow

ModuleLooper.create_named_modules resolves the layer-relative plan once and marks routed experts with state["is_routed_expert"] (gptqmodel/looper/module_looper.py:2148). NativeProcessor skips capturing native inputs for those modules (gptqmodel/looper/native_processor.py:109). GPTAQ and FOEM read 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:#0f5132
Loading
Situation Before After
Routed expert, row counts differ RuntimeError in gptaq.py:124 / foem.py:208 dXXT = 0; plain GPTQ update
Routed expert, row counts happen to match silent dXXT corruption dXXT = 0; no native pairing
Routed expert, zero calibration rows AttributeError on self.H.device RTN fallback
Dense projection native delta unchanged
Shared expert native delta unchanged

The 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:#664d03
Loading

Hessian 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:#0f5132
Loading

3. Change inventory

File Change
gptqmodel/looper/module_looper.py Mark routed experts from ModulePlan.is_expert
gptqmodel/looper/native_processor.py Skip native capture for routed experts; keep state shape
gptqmodel/quantization/gptaq.py Skip native delta for routed experts; zero-sample RTN fallback; P=None path
gptqmodel/quantization/foem.py Same routed-expert and zero-sample handling; beta retained
gptqmodel/quantization/gptq.py Never stage bf16 input to fp16
gptqmodel/quantization/config.py Loud K-quality alias warning; q5_k_s bpw 5.5
tests/test_moe_gptaq_foem.py New unit + tiny Qwen2-MoE end-to-end regression
tests/test_gptq_hessian_pipeline.py bf16 + fp16 staging regression
tests/test_weight_only.py GGUF alias warning + bpw regression
docs/quantizer-moe.md, README.md Flow reference page and link

4. Before / after

Tiny random Qwen2-MoE (3 layers, 16 experts, top-2), 8 synthetic calibration samples, bs=1.

Before (CPU, fp32):

GPTAQ -> RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 1
         at gptqmodel/quantization/gptaq.py:124
FOEM  -> RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 1
         at gptqmodel/quantization/foem.py:208

After (CPU, fp32; same script):

QUANT OK method=gptaq device=cpu dtype=float32 nonfinite=0
QUANT OK method=foem  device=cpu dtype=float32 nonfinite=0

Quality on CUDA (RTX 4090, fp16, same fixture, held-out text):

PPL gptq: 133.4265 | gptaq: 133.4338 | foem(alpha=0.25, beta=0.2): 132.4535

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):

before: q4_k_m and q4_k_s export identical Q4_K bytes
after : GGUFConfig warns `q4_k_m aliases to uniform Q4_K`; q5_k_s reports 5.5 bpw

Item 3 (CPU, bf16 input with channel 3 at 1e5, chunk_size=64):

before: staging fp16 -> Hessian inf/NaN
after : staging bf16 -> finite, normalized error 6.9e-09 vs float64 oracle

5. Tests run

CUDA (/opt/venvs/cu130t, 4090 GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42):

pytest tests/test_moe_gptaq_foem.py tests/test_gptq_hessian_pipeline.py tests/test_moe_padding_rows.py tests/test_weight_only.py \
  -k "moe or bf16_activations or k_quality_aliases" -q
-> 13 passed, 96 deselected

pytest tests/test_gptq.py -q
-> 61 passed, 12 skipped

pytest tests/test_moe_batch_equivalence.py tests/test_tiny_moe_quant_smoke.py -q
-> 3 passed

pytest tests/test_gguf_qlinear_llama.py -q
-> 2 passed

pytest tests/test_weight_only_config.py tests/protocol/test_protocol.py -q
-> 32 passed

ROCm (/opt/venvs/rocm101t, HIP_VISIBLE_DEVICES=1, R9700):

pytest tests/test_moe_gptaq_foem.py tests/test_gptq_hessian_pipeline.py tests/test_weight_only.py \
  -k "moe or bf16_activations or k_quality_aliases" -q
-> 7 passed, 96 deselected

Lint:

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

6. Reviewer notes

  • The routed-expert alpha correction is not implemented; the issue explicitly accepts dXXT = 0 for experts. A follow-up can gather native hidden states with the quantized-path top-k indices and restore the alpha term.
  • The GGUF _s/_m/_l mixture 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.
  • GPTAQ/FOEM still do not share GPTQ's GAR and threshold-fallback loop; this PR only adds the mandatory zero-sample fallback. Unifying the loops is left out to keep the numerical change scoped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant