Skip to content

perf(awq): drop redundant entry NaN scan - #3402

Merged
Qubitium merged 1 commit into
mainfrom
perf/awq-single-nan-guard
Oct 20, 2026
Merged

Qubitium merged 1 commit into
mainfrom
perf/awq-single-nan-guard

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

pseudo_quantize_tensor scanned the full weight tensor for NaN on entry, then scanned both scales and dequantized weights on exit. The exit guard already catches NaN in w (and infinities propagate to NaN through the scale arithmetic), so the entry reduction is redundant.

Validation on AMD Instinct MI355X (gfx950, Torch 2.13.0+rocm10.0.0):

Shape Before After Speedup
1024x1024 112.27 us 81.21 us 1.38x
3584x3584 187.18 us 149.20 us 1.25x
  • tests/test_awq_weight_mean.py NaN-guard tests: 2 passed; the host-read test now asserts exactly one scalar read.
  • tests/test_awq_clip_consistency.py: 4 passed.
  • NaN and infinity remain rejected by the combined exit guard.

@Qubitium
Qubitium merged commit 6d2a2ba into main Oct 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/awq-single-nan-guard branch October 20, 2026 18:44
Qubitium added a commit that referenced this pull request Dec 1, 2026
…6633)

`test_awq_pseudo_quantize_nan_guard_uses_one_host_read` (#3402) has been
red whenever it runs without an earlier symmetric pseudo-quantize call in the
same process, for example alone, with -k, or next to an unrelated test. It
counted 4 scalar reads instead of 1.

Cause: #6002 added `_awq_symmetric_scale_limit`, an lru_cached helper that
steps the per-dtype limit down with `bool(torch.isfinite(...))` and returns
`float(limit)`. That is three `_local_scalar_dense` reads, but only on the
first call per `(decode_dtype, factor)` in a process. So the "one host read
per pseudo-quantize" contract held or failed depending on whether an earlier
test had already warmed this process-global cache. In the full file it passed,
which is why the failure looked flaky. The reads are on CPU constants, so no
GPU stream sync and no measurable runtime cost; the defect is that the
contract depended on cache state, against #6002's own "no host read" claim.

Fix at the cache owner: compute the limit branch-free (two `torch.where`
step-down rounds in place of the `bool()` loop) and cache the 0-d CPU tensor
instead of a Python float. Build it with inference mode disabled, so the cached
tensor is not an inference tensor for later autograd-enabled callers. The
value is unchanged: every decode dtype (fp16/bf16/fp32) x factor 1..255 gives
the same limit, and `torch.where` against fp16/bf16/fp32 scales is
bit-identical on CPU and CUDA. A tiny Granite hybrid sym AWQ quantize saves
bit-identical tensors before and after in bf16 and fp16.

The regression now clears the cache before counting and counts a cold and a
warm call, so it no longer depends on test order.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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