Repository navigation
perf(qlinear): compute scale range answers once per scale content (#6605) - #6607
Merged
Merged
Conversation
) TorchLinear and the AWQ Torch kernels recomputed the range-safe decode answers in clear_weight_cache(), which runs on every _apply (Module.to), train()/eval(), post_init() and state-dict load. Each refresh was four full-tensor scale reductions with a host sync, for scales whose values never changed. A whole-model cpu>accel offload moves every layer twice per forward, so decode spent 51-76% of its time there. - A device move or same-dtype to() copies the scale values, and a training transition does not touch them. Both now carry the answers over (carry_decode_ranges). A dtype cast, a meta round trip, a replaced tensor and an explicit clear_weight_cache() still recompute. - A real refresh reduces the scales once to [max |s|, min nonzero |s|]. Rounding is monotone, so these two values answer every dtype exactly, with one host transfer. - The AWQ eager path reuses the precomputed answers for the same scales. Decode dtypes, logits and generated tokens are bit-identical. 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
Fixes #6605
TorchLinear(GPTQ TORCH/TRITON/SINQ) and the AWQ Torch kernels precompute the range-safe decode answers eagerly so a compiled graph can read them as constants.clear_weight_cache()recomputed them on every_apply(eachModule.to),train()/eval(),post_init()and state-dict load. Each refresh ran four full-tensor scale reductions, each ending in a host sync, for scales whose values had not changed.to()copies the scale values exactly (contract K2: offload and device moves keep "the same weights"). A training transition does not touch the scales. Both now reuse the answers throughcarry_decode_ranges. The answers are carried only when they describe the pre-transition scales (sametensor_cache_key) and the resulting scales keep that dtype and shape. A dtype cast, a meta round trip, a replaced tensor,post_init(), a state-dict load and an explicitclear_weight_cache()still recompute.scale_magnitude_extremesreduces the scales once to[max |s|, min nonzero |s|]and makes one host transfer. Rounding to a float dtype is monotone, so these two values answer every dtype exactly as the old elementwise check did, NaN included. The test compares both against an independent elementwise oracle on boundary fixtures.Not changed: the decode dtype selection, the compiled/CUDA-graph behaviour, and the explicit-clear contract for in-place writes.
flowchart LR A["Module.to / train / eval"] --> B[clear_weight_cache] B --> C{"carry snapshot valid and\nsame dtype+shape?"} C -- yes --> D["reuse answers (0 reductions)"] C -- no --> E["scale_magnitude_extremes: 1 reduction, 1 sync"] --> F[answers for every dtype]Before, every arrow into
clear_weight_cacheran 4 full reductions and 4 syncs.Measurements
RTX 5090, torch 2.15, base
eede258b3vs this branch. Same scripts, run back to back; the load average was 40-180 throughout. "Reductions" counts full-tensor range reductions (scales_fit_dtypeon base,scale_magnitude_extremeshere).GPTQModel.loadLlama-3.2-1B GPTQ vortex-v2.5, TORCHGPTQModel.loadQwen2.5-0.5B-Instruct-AWQ, AWQ_TORCHplacement="cpu>accel:0"16-token greedy decode* RTN wall time is dominated by synchronous
mmap.flushin the disk offload writer (file_back_module), so it varies from run to run. This PR does not change that part; see the comment on #6605.ROCm (R9700 gfx1201, HIP device 3), 8-token
cpu>accel:0decode: 0.40 → 0.92 tok/s. Reductions per run: 7168 → 0. Tokens and logits are identical.Bitwise identity: every scenario gives the same generated tokens, the same logit sums and first logits (for example
500524.84375,498691.28125), and, for RTN, the same checkpoint tensor SHA-256 (8a4ce1c8cecd5824) before and after.Profile, decode, 16 threads, 8 tokens. Base top self-time:
Tensor.to5.63 s,isfinite2.51 s,scales_fit_dtype2.15 s (5.33 s cumulative),abs0.33 s. After the fix:Tensor.to5.23 s (the offload H2D/D2H copies themselves), then_apply0.74 s; no range work remains.Tests
test_range_check_matches_the_elementwise_oracle_apply+train/eval; answers and forward output bit-identical to an unmoved twintest_moves_and_mode_switches_reuse_the_range_answers[gptq_torch, awq_torch]inference_modetest_device_round_trips_reuse_the_range_answers[...].datawrite, tracked in-place write + move, dtype cast, meta round trip all recomputetest_transitions_that_change_the_answers_still_recompute,test_awq_explicit_clear_after_data_write_recomputesAffected families (16 threads, 5090 visible): 25 files, all pass:
kernels/test_awq_torch.py,test_awq_fp32_scale_wide_bias_5764.py,test_cpu_int4pack_scale_offsets_5372.py,test_gptq_bf16_fp16_scales_5740.py,test_inference_numeric_range.py(375),test_kernel_compile_levels_cpu.py(46 passed, 2 skipped),test_ret_idx_cache_invalidation.py,test_tritonv2_range_safe_decode_5738.py,test_torch.py,test_torch_weight_storage.py,test_inference_fp16_overflow.py,test_kernel_audit_regressions.py, paro lifecycle/cache tests,test_gptq_torch_eval_decode_nan_5606.py,test_awq_gemm_autocast_5753.py,test_cpu_offload_whole_model_residency_6592.py,test_awq_affine_range.py,test_qzeros_assign_load_4304.py,test_rehome_cpu_fallback_pairs.pyand others. The skips are pre-existing hardware/dependency skips.Lint:
uvx [email protected] check gptqmodel ... tests setup.pyreports "All checks passed".Not verified: AWQ fused/aten/AVX512 kernels only get the single-reduction refresh, not the carry, because their
_applyrewrites the layout. TTNN and other non-Torch kernels are untouched.🤖 Generated with Claude Code