Skip to content

[BUG] Quantizer algorithms: GPTAQ/FOEM crash on every MoE model (native vs quantized expert inputs paired by call order), GGUF q*_k_m/_s/_l silently identical uniform K-quant, fp16 Hessian staging overflows bf16 #4067

Closed

Description

@Qubitium

Audit of main @ ad0525c, CUDA (RTX 4090). Quantizer algorithms were checked against independent references at layer level.

1. GPTAQ and FOEM (alpha>0) crash on every MoE model: native and quantized expert inputs are paired by call order (high)

NativeProcessor (looper/native_processor.py:168) records each routed expert's inputs during the full-precision forward. GPTAQ.process_batch (quantization/gptaq.py:77-124, native_inps.pop(0)) and FOEM (quantization/foem.py:~160-208) pair them, by call order, with the inputs of the quantized-path forward. Earlier modules in the same layer (attention) are already quantized at that point, so the router sends different tokens to each expert. Expert calls therefore get native and quantized tensors with different token counts, or, if the counts happen to match, rows from different tokens, which silently corrupts dXXᵀ.

tiny random Qwen2-MoE (3 layers, 16 experts, top-2, fp16), 8 C4 samples, bs=1
GPTAQConfig            -> gptaq.py:124  RuntimeError: The size of tensor a (31) must match the size of tensor b (32) at non-singleton dimension 1
FOEMConfig(alpha=0.25) -> foem.py:208   RuntimeError: mm: mat1 and mat2 shapes cannot be multiplied (256x2 and 1x256)
GPTQConfig (control)   -> QUANT OK;   GPTAQ on a dense tiny Llama -> QUANT OK

There is also a second path: an expert that receives no tokens still reaches GPTAQ.quantize. That function has no zero-sample fallback (unlike GPTQ) and uses self.H.device (gptaq.py:161) while H is None.

Fix:

  • For routed experts, either compute the native inputs with the quantized-path routing (gather the native hidden states with the same top-k indices), or fall back to dXXᵀ = 0 (plain GPTQ) for experts.
  • Add the GPTQ zero-sample/RTN fallback to GPTAQ.quantize and FOEM.quantize. They also re-implement the GPTQ loop without act_group_aware (GAR), the fallback thresholds or _tp_pad_cols truncation; worth unifying.
  • Add a GPTAQ/FOEM MoE test.

2. GGUF q*_k_m / q*_k_s / q*_k_l are all the same uniform K-quant (medium, silent accuracy loss)

_GGUF_BITS_ALIAS_TO_TENSOR_QTYPE (nn_modules/qlinear/gguf.py:~103-125) maps q4_k, q4_k_s and q4_k_m to Q4_K, and likewise q2_k_*, q3_k_* and q5_k_*. The parsed quality suffix is stored in the config but never used outside config.py. llama.cpp's _M mixture uses Q6_K for attn_v / ffn_down in the use_more_bits layers. The README example names its output …-GGUF-Q4_K_M, so users get a different (lower-quality) mixture than the name says.

packed bytes: q4_k_m == q4_k_s: True   q5_k_m == q5_k_s: True
Qwen2.5-0.5B, all decoder linears fake-quantized, wikitext 40x512:
  fp16 17.91 | uniform Q4_K (= what q4_k_m produces) 20.05 | Q4_K_M-style mixture 19.44

Fix:

  • Implement the per-tensor _s/_m/_l mixture rule, for example through per-module dynamic qtype overrides, or reject these names / warn that they alias to the uniform type.
  • Fix the bpw table too: q5_k_s is listed as 5.0 but is stored as Q5_K (5.5).
  • tests/test_gguf_qlinear_llama.py and tests/test_mlx_gguf_quantization.py parametrize q4_k_m but only compare it with native Q4_K.

3. HessianConfig(staging_dtype="float16") overflows bf16 activations to an inf/NaN Hessian (low, opt-in)

quantization/gptq.py:842-944 (preferred_staging_dtype / borrow_materialized_chunk_fp32): when chunking is active, bf16 inputs are cast to fp16 before fp32, so |x| > 65504 becomes inf. The staging buffer also doesn't save memory, because an fp32 workspace is allocated anyway.

bf16 input, one channel x1e5:  staging fp32 chunk 64 -> relerr 6.5e-9 vs fp64 | staging fp16 chunk 64 -> NaN | staging bf16 -> 6.5e-9

Fix: never stage to a narrower-range dtype than the input, or drop the option.

Checked, no defect found

  • GGUF dequant: bit-exact against llama.cpp dequantize_rows for all 25 types.
  • GGUF quantize: Q4_0/Q8_0/Q1_0/Q2_0/TQ1_0/TQ2_0/MXFP4 are byte-identical to llama.cpp.
  • NF12 codec: lossless, including NaN/inf/−0/denormals.
  • QQQ/VPTQ hooks: receive unpadded rows at bs 1 and 4.
  • GPTQ embedding quant: consistent with GAR, desc_act and static groups.
  • :in= Hessian adoption: equals independent Hessians.
  • FOEM block-boundary fix (fix(fp8,foem): preserve packed save payloads and fix capture/block-boundary paths (#4037) #4046): present.
  • Dense GPTAQ: matches upstream GPTQv2.

Plausible, not verified

  • ParoQuant opt_scope="layer"/group: F.smooth_l1_loss (paroquant_processor.py:1729, 2025, 2196) runs over the full layer outputs, so pad positions enter the loss at bs>1.
  • GPTAQ/FOEM Conv2d use nn.Unfold(padding=self.module.padding) directly, which fails for padding="same". GPTQ uses conv_padding_pairs.

🤖 Generated with Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions