Repository navigation
fix(awq): validate the plan path's sampled-row cache against the live host copy - #6479
Merged
Merged
Conversation
… host copy
AWQ on a routed MoE quantized differently from run to run on CUDA: on the
tiny qwen3_moe recovery fixture, layers.1.mlp.experts.3.{up,down}_proj
qweight/scales changed between identical runs, so
test_awq_checkpoint_recovery[*-qwen3_moe] failed intermittently.
Cause: `_record_input_feature` caches each host copy's plan-sampled rows
in `_capture_sampled_cache` under `_capture_share_key(host)`, which is
(batch, data_ptr, shape, stride, dtype, version, keep-mask). Calibration
tensors are inference tensors, so version is None and the key is just the
host address. Under ExpertsRoutingOverride every expert's down_proj input
has the same shape, and an expert's host copy is released as soon as the
next expert's capture replaces it in `_capture_host_cache`. When the host
allocator handed that address to a later expert's copy, the bare key hit
and the later down_proj was scaled and clipped on another expert's rows
(the up_proj change is the folded down_proj scale). Whether an address was
recycled depended on allocation timing, hence the nondeterminism. Traced
runs show the first divergence is exactly that capture: experts.3.down_proj
batch 0 bit-equal to experts.1.down_proj batch 0 in the failing runs, with
every other activation, x_mean/w_mean and replay identical.
Fix: store and check the entry through `_sibling_capture_get/_put`, the
weakref-validated helpers the replay-side caches already use (#5267), with
(share_key, quota) as the extra key. A hit now requires the entry to name
the same live host tensor, so q/k/v and gate/up still sample once while a
recycled address misses. The host cache already had this guard; the
sampled cache missed it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
On CUDA, AWQ quantization of a routed MoE gave different results on identical runs. With the tiny qwen3_moe recovery fixture (2 layers, hidden 64, 4 experts, fp16), three back-to-back runs of
tests/checkpoint_quantization_driver.py --mode baseline --device cuda:0 --method awqsaved differentmodel.layers.1.mlp.experts.3.{down,up}_projqweight/scales. Every other tensor matched. Because of this,test_awq_checkpoint_recovery[*-qwen3_moe]failed intermittently. CPU runs were bit-identical.Root cause. On the plan path,
AWQProcessor._record_input_featurecaches each host copy's sampled rows in_capture_sampled_cache. The key is(_capture_share_key(host), quota), and_capture_share_keyis(batch, data_ptr, shape, stride, dtype, version, keep-mask). Calibration tensors are inference tensors, soversionisNoneand the key comes down to the host address.Under
ExpertsRoutingOverride, every expert'sdown_projinput has the same shape. An expert's host copy is released as soon as the next expert's capture replaces it in_capture_host_cache. When the host allocator gave that address to a later expert's copy, the bare key matched, and the laterdown_projwas scaled and clipped on another expert's rows. Theup_projchange is just the foldeddown_projscale.Whether an address got recycled depended on allocation timing, which is why the result changed between runs.
_capture_host_cachealready checks that a hit names the same live tensor through a weakref (#5267 added the same guard to the replay-side caches). The sampled cache was the one cache without that check.Fix. Read and write the sampled cache through the existing
_sibling_capture_get/_sibling_capture_puthelpers, passing(share_key, quota)asextra. A hit now has to name the same live host tensor. q/k/v and gate/up siblings still sample once; a recycled address now misses. No new mechanism, config or env switch.Where two runs first diverged (evidence)
I traced the run with debug-only hooks in a scratch wrapper, not in the PR. The hooks hashed every
_AWQTokenRowOwner.submitcapture,_layer_input_featuresoutput,_compute_best_scaleinput (x,x_mean,w_mean, FP reference), every replay chunk and every clip result:layer 1 mlp.experts.3.down_proj, batch 0. It differed across the whole tensor (max |d| 0.09), not by rounding noise.mlp.experts.1.down_projbatch 0, i.e. another expert's rows. The run that differed showed no such alias.Routing, token order, reductions,
x_mean/w_mean, the scale-grid tie-break and the replay forward were all identical across runs once the capture was the same. So this is not GPU reduction or atomics nondeterminism.AWQ GEMM Triton logits. The separate observation that AWQ GEMM Triton logits vary between runs has a different cause. That inference kernel's split-K reduction uses
tl.atomic_addby design (see_single_split_requiredingptqmodel/quantization/awq/modules/triton/scheduler.py, which already switches to one split undertorch.use_deterministic_algorithms). AWQ quantization never runs that kernel, and the recovery matrix usesBACKEND.TORCH. Out of scope here.What Changed
gptqmodel/looper/awq_processor.py: the plan-path sampled-row cache is validated against the live host tensor (+18/−5).tests/test_awq_reservoir_sibling_rows_5267.py: new CPU unit regression in the sibling-cache family. It reuses one inference-mode buffer to recycle a host address deterministically, then checks that the second expert keeps its own sampled rows, both at capture and after_layer_input_features.tests/test_awq_moe_gpu_determinism.py(new,# GPU=1,cudamarker): quantizes a tiny 8-expert qwen3_moe twice in one process on CUDA and asserts every saved tensor is bit-identical. Skips cleanly without CUDA.Tests
test_plan_path_siblings_still_share_one_captureis the positive control: q/k/v still share one sampled capture object.Measured Impact
The fix adds one weakref per sampled-cache insert and one dereference per lookup, i.e. per unique host capture per batch. There's no measurable cost. Dense AWQ output is unchanged.
The MoE outputs that change are the ones that used to be corrupted: an aliased
down_projwas scaled and clipped on another expert's activations. The branch's output equals the alias-free main runs bit for bit.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 for onboarding: no grouping, search, replay or fold rule changed. This fixes which activation rows the existing capture hands to the scale and clip search. Evidence is above: the per-capture trace, the dense A/B, and the MoE recovery matrix.
Logic Flow Documentation
Diagram summary: not applicable. This is a local fix to one cache's hit check, and it reuses the existing weakref-validated helpers; no control flow, selection order or lifecycle changes.
Review Requirements
Kernel Accuracy Requirements
Not applicable: no kernel, dispatch or fusion change.
Notes
_materialize_host_featurereturns adetach()of the live activation instead of a fresh D2H allocation. The CPU unit test shows the defect without a GPU.test_awq_checkpoint_recovery[True-term-llama]because the resume refused the checkpoint withincompatible checkpoint identity fields: device_topology. A plausible but unverified cause:_gpu_serials_by_uuid(gptqmodel/looper/checkpoint_devices.py) returns{}whennvidia-smiexceeds its 5 s timeout, so the recorded serial can differ between the run and the resume. That case passed in all three branch runs.PYTHON_GIL=0runs; I only tested a single visible GPU.🤖 Generated with Claude Code