Repository navigation
fix(awq): keep the cached symmetric scale limit free of host reads - #6633
Merged
Merged
Conversation
`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]>
Qubitium
marked this pull request as ready for review
December 1, 2026 09:48
This was referenced Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tests/test_awq_weight_mean.py::test_awq_pseudo_quantize_nan_guard_uses_one_host_read(#3402) fails whenever no symmetric pseudo-quantize call has run earlier in the same process. That covers running it alone, with-k, or next to an unrelated test such as the AWQ hybrid test. It counts4 == 1scalar reads. In the full file it passes, which is why it looked flaky.Root cause is test-order state leakage, and the state is owned by the product. #6002 (
dedeb2924) added thefunctools.lru_cached_awq_symmetric_scale_limit. Its cold path does three_local_scalar_densereads: twobool(torch.isfinite(limit * factor))step-down checks and a finalfloat(limit). A warm cache does none. So the "pseudo-quantize does exactly one host read" contract held or failed depending on whether an earlier test had filled this process-global cache.dedeb2924~1passes the test alone;dedeb2924fails it.Impact is limited: the reads are on CPU constants and happen once per
(decode_dtype, factor)per process, so there is no GPU stream sync and no measurable runtime cost. The defect is that the contract depended on cache state, which contradicts #6002's own docstring ("no host read is needed").What Changed
_awq_symmetric_scale_limitnow computes the limit branch-free, with twotorch.where(isfinite(limit * factor), limit, nextafter(limit, 0))rounds replacing thebool()loop. It caches the resulting 0-d CPU tensor instead of a Pythonfloat. The tensor is built withtorch.inference_mode(False), so the cached tensor is never an inference tensor handed to a later autograd-enabled caller.torch.where(overflowing, limit, scales)consumes it exactly as it consumed the float._awq_symmetric_scale_limit.cache_clear()before counting. It counts a cold and a warm call (each must be exactly 1 read) and asserts one cache miss, so it no longer depends on test order.test_awq_hybrid_recurrent_mask_4671.py::test_awq_batch_size_one_quantizes_a_mamba_hybrid) has an unrelated cause (a fixture tokenizer with colliding ids) and gets its own PR.Tests
Measured Impact
Value equivalence, old Python-float limit vs new cached tensor:
_awq_symmetric_scale_limit(dd, factor)is equal for every decode dtypeddin {fp16, bf16, fp32} and everyfactorin 1..255. For fp16/7 that is 9352.0.torch.where(mask, limit, scales)is bit-identical for every scale dtype in {fp16, bf16, fp32}: 2295 combos on CPU and 1784 on CUDA.End to end: a tiny random-init Granite-MoE-Hybrid (the fixture from
test_awq_hybrid_recurrent_mask_4671.py) quantized with sym AWQ GEMM (bits=4, group_size=32) on CPU saves 117/117 bit-identical tensors on origin/main vs this branch, in both bf16 and fp16.No runtime change is expected or claimed. The removed reads were CPU-only, once per process.
Model Support / Module Plan
Not applicable: no model, alias, definition or module plan change.
Model test node IDs and results (or "not applicable"): not applicable.
AWQ onboarding safety
Not applicable: no grouping, search, replay or fold change. The symmetric scale-limit value is proven identical above, and quantized output is bit-identical.
Logic Flow Documentation
Diagram summary: not applicable. The same limit is computed with tensor ops instead of a host-read branch; no control flow, selection order or lifecycle changes.
Review Requirements
Kernel Accuracy Requirements
Notes
The leaked state stays owned by the cache, and the cache now has nothing observable in its cold path. The test does not depend on warm-up order, and the assertion is unchanged (exactly one read per call). It now also covers the cold path.
🤖 Generated with Claude Code