Skip to content

fix(cpu): decode int4pack fallbacks in a range-safe dtype (#5577) - #5596

Merged
Qubitium merged 1 commit into
mainfrom
fix/5577-int4pack-decode-dtype
Nov 20, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5577-int4pack-decode-dtype

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5577.

#5495 made the checkpoint-layout AWQ dense decode range-safe, but two decodes still ran in the FP16 scale dtype: the live int4pack runtime decode after an eval warm-up, and the GPTQ TorchFused/TorchAten dense fallback. A FP16 scale such as 8192 overflows to inf/NaN before the caller can widen the result, so the same layer returns 30720 without a grad and NaN with one.

What Changed

  • Added gptqmodel/nn_modules/qlinear/_decode_dtype.py with decode_dtype_for_scales() / scales_fit_dtype(). It selects the stored scale dtype while every decoded weight is finite and non-underflowing, and widens to FP32 (or FP64 for a pathological BF16 scale) only when needed. A wide activation dtype keeps its own dtype; under torch.compile the chooser reads only the dtype (a host reduction is not traceable) and keeps the historical narrow checkpoint-layout behavior.
  • runtime_int4pack_dense_weight() now decodes scales, zeros, and codes in that dtype; int4pack_decode_dtype() reuses an AWQ module's _decode_dtype_for_activation() when present.
  • PackableQuantLinear.dequantize_weight() / _dequantize_from_codes() accept an explicit decode dtype; the default is still the stored scale dtype.
  • TorchFusedLinear and TorchAtenLinear (and their AWQ variants) decode the checkpoint and runtime weights in the range-safe dtype, and promote a wide decode to FP32 for the call instead of narrowing it back to the activation dtype before the product.
  • Extended tests/test_inference_fp16_overflov.py with the no-grad warm-up regression for AWQ and cold/warm GPTQ cases (FP32 and FP16 activations, plus the FP32 grad check).

Tests

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.
GPTQ_BYPASS_SAFETY_CHECKS=1 PYTHONPATH=. /root/venv-tf523/bin/python -m pytest \
  tests/test_inference_fp16_overflov.py \
  -k "after_warmup_decodes_overfloing_fp16_scale or test_cpu_gptq_fallback_decodes_overfloing_fp16_scale" -q
# 6 passed, 33 deselected in 4.48s

The full file has 26 passed, 1 skipped, 12 failed on this host. Every failure is environmental: the AVX512 kernel classes are rejected on this non-AVX512 CPU, and the Marlin/AWQ-GEMM/ExllamaV2 tests require CUDA. They fail on main too and do not touch the changed paths.

Model Support / Module Plan

Not applicable: no model, alias, registry, or module-plan change.

Model test node IDs and results (or "not applicable"): not applicable.

AWQ onboarding safety

Not applicable: no model/alias or AWQ grouping/search/replay/fold change. The changed AWQ code is the existing dense decode fallback, and the new tests cover its range-safe decode directly.

AWQ evidence and per-variant policy (or "not applicable" with a reason): not applicable.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.

Diagram summary: not applicable. This is a dtype chooser at the existing decode point; no selection order, lifecycle, or ownership contract changes.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • For kernel-related changes, accuracy drift was measured against an applicable independent Torch FP32/FP64 oracle, or the reason this is not applicable is documented.

The new tests assert the exact decoded value (30720) and the exact input gradient against the FP16-scale oracle in both cold and warmed states. The narrow path is unchanged when the stored scales fit the activation dtype.

  • Fusion support changes preserve AUTO's native class and add actual-device FP16/BF16 tests where supported, native shape/workspace/descriptor checks, mutation, direct source calls, exact restoration and packed-storage checks.
  • The complete kernel inventory/devices report lists unavailable dependencies, architecture restrictions and unqualified formats; skips are not validation.

Not applicable: no fusion support, kernel inventory, or device-qualification change.

@Qubitium
Qubitium merged commit 845e3da into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5577-int4pack-decode-dtype branch November 20, 2026 01:57
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