Skip to content

fix(awq): stage and pack every module at its own width - #3563

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
Qubitium:fix/awq-per-module-bits
Nov 6, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
Qubitium:fix/awq-per-module-bits

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

AWQConfig accepts a per-module width through dynamic:

AWQConfig(bits=4, dynamic={"model.layers.0.mlp.down_proj": {"bits": 8}})

create_quant_module honours that override when it builds the module. But
pseudo_quantize_tensor, the clip search and the scale search all read
self.qcfg.bits directly, so a W8 module staged 4-bit codes and packed them
into an 8-bit container.

Demonstrated on upstream/main:

config says this module is W8: 8
widest staged code           : 7.0     # W8 allows 127
=> staged as W4, labelled W8

The result is a checkpoint whose config.json misdescribes its own tensors.

Second bug, same root

dynamic_get returns the raw override, which is False for an excluded module
(-model.layers.0.mlp.up_proj) and None for a rule that only sets another key
({"group_size": 64}). Treating these sentinel values as widths can select width 0 (False) or raise a type error (None).

Fix

Resolve the width once in _module_bits() and thread it through every stage:

  • pseudo_quantize_tensor / _pseudo_quantize_tensor_into — code range
  • _search_best_scale / _compute_best_scale — clip + scale search
  • the GSQ refine step
  • the packing create_quant_module call

_module_bits falls back to the global width for False or None.

A scaling group shares one activation scale, but each member is evaluated at its own configured width for every candidate scale. This keeps the reconstruction-loss objective consistent with the final mixed-width checkpoint.

For GEMM checkpoints, W8 modules route to the portable AwqTorchLinear; the CUDA GEMM kernel only supports W4. GEMV formats retain their existing W4 restriction.

Validation

  • tests/test_awq_per_module_bits.py: 82 passed on CPU and NVIDIA GB10, Torch 2.11.0+cu130.
  • tests/test_awq_weight_mean.py, tests/test_awq_clip_consistency.py, and tests/test_awq_gptq_lifecycle.py: 138 passed, 1 skipped, 1 pre-existing failure. The MoE policy test test_public_moe_model_policies_enable_pointwise_roots_and_children also fails on the untouched PR head 7fb3b5ee.
  • The new mixed-width scale-search oracle fails on 7fb3b5ee and passes with this fix. It covers W4/W8 in both orders, symmetric/asymmetric quantization, and chunked/eager activation replay on CPU and CUDA.
  • Independent quantization oracles cover exact half steps, their adjacent representable values, signed zero and endpoints in FP32, FP16 and BF16. Floating tolerance is rtol=atol=1e-6; discrete codes match exactly.
  • Real apply_quant → pack_module → state-dict reload → repeated forward tests cover W4/W8, symmetric/asymmetric quantization, FP16/BF16, and CPU/CUDA. All packed weight and zero-point words match the independent packing oracle exactly. Inference matches independently reconstructed weights at rtol=atol=2e-3.
  • Test shapes: scale search uses two [8, 16] weights and [9, 16] inputs; packing/inference uses [8, 32] weights and [3, 32] inputs; clipping uses [64, 32] weights and [11, 32] inputs.
  • Repeated portable forwards retain only the existing packed state: 148 bytes for the W4 fixture or 280 bytes for W8, with no additional dense/expanded weight state. The temporary dequantized [32, 8] FP16/BF16 weight occupies 512 bytes. No performance benchmark was run.
  • Ruff and git diff --check pass.

`AWQConfig` accepts a per-module width through `dynamic` (e.g. W8 on one
layer of a W4 run). `create_quant_module` applied that override when it
built the module, but the staging pass, the clip search and the scale
search all read `qcfg.bits` directly. A W8 module therefore staged 4-bit
codes and packed them into an 8-bit container.

Measured on upstream before this change: a module whose config says
`bits: 8` staged codes in the 4-bit range (widest code 7.0, where W8
allows 127), i.e. it was quantized as W4 and mislabelled as W8.

Resolve the width once in `_module_bits` (falling back to the global
width for an excluded module or a rule that only sets another key --
neither is a width, and coercing them to 0 was wrong too) and thread it
through:

  - `pseudo_quantize_tensor` / `_pseudo_quantize_tensor_into`
  - `_search_best_scale` / `_compute_best_scale` (clip + scale search)
  - the GSQ refine step
  - the packing `create_quant_module` call

A scaling group shares one activation scale, so its first member's width
drives the search; `apply_quant` re-quantizes each module at its own
width afterwards.

The CUDA GEMM/GEMV kernels unpack 4-bit codes only, so a non-4-bit
module now routes to the portable `AwqTorchLinear` (2-8 bits) instead of
being handed to a kernel that cannot represent it.
Resolve widths in scale search and use each linear\x27s quantizer for every candidate shared scale. Add independent scale and quantization oracles plus real packing, reload, dispatch and repeated-forward coverage.

Validation: 82 targeted tests passed on CPU and NVIDIA GB10 (Torch 2.11.0+cu130). AWQ weight-mean, clip and lifecycle suites: 138 passed, 1 skipped; the MoE policy failure reproduces on the untouched PR head. Ruff and git diff --check passed.
@Qubitium
Qubitium merged commit 4c7dacb into ModelCloud:main Nov 6, 2026
1 check passed
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