Skip to content

perf(qlinear): compute scale range answers once per scale content (#6605) - #6607

Merged
Qubitium merged 1 commit into
mainfrom
perf/6605-scales-range-once
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
perf/6605-scales-range-once

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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 (each Module.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.

  • Carry across value-preserving transitions. A device move or same-dtype 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 through carry_decode_ranges. The answers are carried only when they describe the pre-transition scales (same tensor_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 explicit clear_weight_cache() still recompute.
  • One reduction per real refresh. scale_magnitude_extremes reduces 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.
  • AWQ eager path reuses the precomputed per-dtype answers when they describe the live scales.

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]
Loading

Before, every arrow into clear_weight_cache ran 4 full reductions and 4 syncs.

Measurements

RTX 5090, torch 2.15, base eede258b3 vs this branch. Same scripts, run back to back; the load average was 40-180 throughout. "Reductions" counts full-tensor range reductions (scales_fit_dtype on base, scale_magnitude_extremes here).

scenario threads reductions before → after time in them before → after wall before → after
GPTQModel.load Llama-3.2-1B GPTQ vortex-v2.5, TORCH 16 1792 → 336 0.87 s → 0.19 s 12.5 s → 4.7 s
same default (213) 1792 → 336 12.6 s → 4.3 s 19.5 s → 10.9 s
GPTQModel.load Qwen2.5-0.5B-Instruct-AWQ, AWQ_TORCH 16 – 0.27 s → 0.16 s 5.1 s → 4.1 s
same default – 11.0 s → 4.5 s 15.5 s → 10.6 s
placement="cpu>accel:0" 16-token greedy decode 16 14336 → 0 15.1 s (51%) → 0 0.54 → 1.25 tok/s
same default 14336 → 0 31.5 s (69%) → 0 0.35 → 1.19 tok/s
RTN quantize Qwen2.5-0.5B-Instruct (default disk offload) 16 1848 → 168 0.20 s → 0.01 s 11.7 / 29.6 s → 11.9 / 11.7 s*
same default 1848 → 168 11.1 s → 1.2 s 32.1 s → 31.2 s

* RTN wall time is dominated by synchronous mmap.flush in 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:0 decode: 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.to 5.63 s, isfinite 2.51 s, scales_fit_dtype 2.15 s (5.33 s cumulative), abs 0.33 s. After the fix: Tensor.to 5.23 s (the offload H2D/D2H copies themselves), then _apply 0.74 s; no range work remains.

Tests

invariant / trigger node red on main green
range check == independent elementwise oracle (fp16/bf16/fp32 scales × bits 2/3/4/8, boundary ± ulps) test_range_check_matches_the_elementwise_oracle passes (control) 12 passed
no scale reduction across 3 value-preserving _apply + train/eval; answers and forward output bit-identical to an unmoved twin test_moves_and_mode_switches_reuse_the_range_answers[gptq_torch, awq_torch] FAILED: 108 / 88 reductions passed
same over real cpu↔cuda round trips under inference_mode test_device_round_trips_reuse_the_range_answers[...] FAILED: 32 / 28 reductions passed (5090)
explicit clear after .data write, tracked in-place write + move, dtype cast, meta round trip all recompute test_transitions_that_change_the_answers_still_recompute, test_awq_explicit_clear_after_data_write_recomputes passes (control) passed

Affected 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.py and others. The skips are pre-existing hardware/dependency skips.

Lint: uvx [email protected] check gptqmodel ... tests setup.py reports "All checks passed".

Not verified: AWQ fused/aten/AVX512 kernels only get the single-reduction refresh, not the carry, because their _apply rewrites the layout. TTNN and other non-Torch kernels are untouched.

🤖 Generated with Claude Code

)

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]>
@Qubitium
Qubitium marked this pull request as ready for review December 1, 2026 06:32
@Qubitium
Qubitium merged commit c0b4cb3 into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/6605-scales-range-once branch December 1, 2026 06: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.

perf: TorchLinear/AWQ torch kernels recompute scales_fit_dtype range reductions on every .to()/train()/eval() (slow load, cpu>accel decode)

1 participant