Skip to content

fix(torch): route eval decode through the range-safe decoder (#5606) - #5670

Merged
Qubitium merged 1 commit into
mainfrom
fix/5606-gptq-eval-nan
Nov 20, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5606-gptq-eval-nan

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

TorchLinear.dequantize_weight() takes an eval-only fast path (_dequantize_weight_cached_248) that multiplied the packed codes by the stored FP16 scale before the caller could widen. A finite decoded weight of 122880 overflows FP16 to inf, so a BF16 linear returned NaN (0 * inf) and TorchQuantEmbeddings returned inf.

  • Decode the eval fast path in the dtype selected by the shared decode_dtype_for_scales helper, matching the base decoder.
  • Widen the embedding row-gather multiply; the whole-table lookup now inherits the fixed decode.
  • Keep the reduction in FP32 (via linear_wide_bias) only when the decoded weight does not fit the activation dtype, so the common finite path is unchanged.
  • Gate the CUDA streaming and fused GEMV/GEMM kernels on the same range check, since they stage decoded weights in the activation dtype.
  • Cache the range answers at clear_weight_cache() so a traced dequantize_weight reads Python constants instead of running a host reduction.

Tests

  • tests/test_gptq_torch_eval_decode_nan_5606.py: 3 passed (BF16/FP16 linear eval+train vs an FP64 oracle; embedding whole-table and row-gather routes).
  • tests/test_torch.py tests/test_inference_fp16_overflow.py tests/test_torch_kernel_accuracy.py: 129 passed, 1 skipped.
  • tests/test_torch_safety_gate.py tests/test_torch_weight_storage.py tests/test_torch_compile_fallback.py tests/test_q4_torch.py tests/test_torch_int8_tail_padding_4483.py tests/test_torch_aten_kernel_import_guard.py: 261 passed.
  • tests/test_quantized_embeddings.py tests/test_embed_output_dtype_5394.py tests/test_requant_embed_endpoint_5116.py tests/kernels/test_torch_wide_bias_fallback.py tests/kernels/test_inference_numeric_range.py: 580 passed.
  • Total: 973 passed, 1 skipped.
  • uvx [email protected] check gptqmodel tests: All checks passed!
  • git merge-tree --write-tree upstream/main HEAD: clean (exit 0).

Fixes #5606.

The GPTQ_TORCH eval fast path multiplied packed codes by the stored
FP16 scale, overflowing to inf before the caller could widen; a BF16
linear then returned NaN (0 * inf) and the embedding returned inf.

Decode in decode_dtype_for_scales' dtype, widen the embedding row
gather, and keep the reduction wide when the decoded weight does not
fit the activation dtype. Gate the streaming and fused kernels on the
same range check.
@Qubitium
Qubitium merged commit cbb6529 into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5606-gptq-eval-nan branch November 20, 2026 05:32
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.

[BUG] GPTQ_TORCH eval decode bypasses the range-safe base decoder: BF16 linear returns NaN and embedding returns inf

1 participant