Skip to content

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

Done

Description

@Qubitium

Summary

TorchLinear (and the TORCH-derived TRITON / SINQ kernels) and every AwqDecodeRangeMixin kernel (AWQ_TORCH, fused/aten/AVX512 AWQ) recompute the range-safe decode answers eagerly in clear_weight_cache(). That runs 4 full-tensor scale reductions per module (decode_dtype_for_scales + scales_fit_dtype for FP16/BF16/FP32), each ending in bool(...all()) (a host sync on an accelerator, or a small CPU reduction that oversubscribes OpenMP on a busy host). And clear_weight_cache() runs on every _apply (.to(), .cuda(), .cpu()), every train()/eval(), post_init() and state-dict load.

The scales do not change in any of these steps. A device move keeps their values (the forward-dtype contract K2 allows device moves only for "the same weights"). train()/eval() and post_init() keep the same tensor at the same version. So the answers are recomputed for unchanged inputs.

Path: BaseQuantLinear._apply / TorchLinear.train / TorchLinear.post_init → clear_weight_cache → TorchLinear._refresh_decode_dtypes → scales_fit_dtype ×4 (gptqmodel/nn_modules/qlinear/torch.py _refresh_decode_dtypes; AWQ: _awq_decode.py _refresh_decode_range_fits).

Impact (main eede258b3, RTX 5090, torch 2.15)

Llama-3.2-1B-Instruct GPTQ 4-bit (ModelCloud/Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v2.5, 112 quantized linears), BACKEND.TORCH:

scenario threads scales_fit_dtype calls time in it wall
GPTQModel.load(..., device="cuda:0") 16 1792 (16/module) 0.9 s 14.1 s
same default (213) 1792 18.3 s 23.7 s
placement="cpu>accel:0", 16-token greedy decode 16 14336 (896/token = 112 modules × 2 moves × 4) 14.4 s (51%) 28.5 s, 0.56 tok/s
same default 14336 46.4 s (76%) 60.7 s, 0.26 tok/s

Where the 16 calls per module come from during load (4 refreshes × 4 calls): create_quant_module .to(), the dispatch .to(cuda), post_init(), and model.eval(). Only the dispatch move sees new values.

Whole-model cpu>accel offload (#6592/#6594) moves every layer to the accelerator and back on each forward, so every decoded token pays 8 reductions per linear plus a sync.

RTN quantize (Qwen2.5-0.5B-Instruct, default disk offload) also pays this on the post-loop moves/eval() (1848 calls), but that is a minor part of its regression versus pre-#6473 (see the note below).

Expected

Compute the range answers once per distinct scale content. A device move or train()/eval() must reuse them, while keeping the selected decode dtype bit-identical. The answers still have to be precomputed eagerly for compiled / CUDA-graph forwards.

Note on RTN

cProfile of RTN quantize on main (16 threads) shows most of the post-#6473 slowdown in mmap.flush inside file_back_module/offload_to_disk (9.0 s of 13.8 s; 168 calls), not in scales_fit_dtype (0.16 s). That is tracked separately.

Activity

  1. Qubitium commented on Dec 1, 2026

    @Qubitium
    CollaboratorAuthor

    Correction on the RTN note: no separate issue was filed. The RTN quantize regression versus pre-#6473 (Qwen2.5-0.5B, 16 threads: 3.6 s before #6473, 11.7-29.6 s on main) is mostly synchronous mmap.flush() + fsync in weight_loader/filebacked.py write_safetensors, reached from file_back_module(evict=True) during offload_to_disk (9.0 s of 13.8 s in cProfile). Eviction needs clean pages, so this looks like a design trade-off of the disk-offload default rather than this issue. #6607 removes the scales_fit_dtype share (1848 → 168 reductions; 11.1 s → 1.2 s uncapped).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions