Repository navigation
Description
Perf audit of main @ 93d3ae7.
Test model: random Llama, hidden 4096, intermediate 11008, 8 layers, vocab 32k, bf16, quantized with the public API (4-bit, gs128, GPTQ v1), giving a 1.37 GB checkpoint. It was loaded on an RTX 5090. The host was loaded (load average 14–17 on 16 cores), so CPU-bound numbers are higher than on a quiet box; quiet/OMP_NUM_THREADS=1 numbers are given where they differ.
Every change below gives bitwise-identical tensors or byte-identical files.
1. qzeros pack/unpack helpers drop the device and loop in Python; this also makes GPU dequantize_model crash
gptqmodel/utils/model_dequant.py:796 unpack_cols, :813 pack_cols, :838 unpack_rows. The 2/4/8-bit path allocates result = torch.empty(...) with no device, then runs a Python loop of pack_factor strided writes.
These helpers are used by _correct_gptq_v1_qzeros / _revert_gptq_v1_qzeros_correction (:983, :987), which are called:
- on load, for v1 → v2 (
utils/model.py:930, reached vialoader.py:2526); - inside
pack_module, for v2 → v1 (utils/model.py:1049).
So for GPU-resident qzeros, every conversion goes GPU → CPU loop → GPU.
Correctness: dequantize_model(..., device="cuda") fails on main with RuntimeError: indices should be either on cpu or on the same device.... The unpacked zeros land on CPU while g_idx is on the GPU.
| main | vectorised, device-preserving | |
|---|---|---|
| v1→v2 convert, 56 modules, GPU load (Torch/Triton/ExLlamaV2 backends) | 5.6–14.6 s | 0.04 s |
(same, OMP_NUM_THREADS=1) |
0.125 s | 0.041 s |
whole GPTQModel.load (TORCH backend) |
17.5–20 s | 3.8–4.0 s |
_correct_gptq_v1_qzeros, one CUDA [32, 1376] tensor |
268 ms | 0.09 ms |
| GPU dequant of the model | crash | 2.7 s (CPU path: 36–40 s) |
Equivalence:
- 90 random cases (cpu/cuda × bits 2/4/8 × int8/16/32 × unpack/pack/shift ±1) are
torch.equal. - The loaded state_dict (243 tensors) and the logits are bitwise equal.
- The GPU dequant output has the same sha256 as main's CPU dequant.
tests/test_qzero_offsets.py,test_planar_bits_567.pyandtest_hf_packing_api.py: 98 passed, 3 skipped.
Fix:
shifts = torch.arange(pf, device=packed.device, dtype=torch.int64) * bits
out = ((packed_u64.unsqueeze(-1) >> shifts) & mask).to(torch.int32).reshape(rows, cols * pf) # unpack_cols
packed = (vals.view(r, c // pf, pf).to(torch.int64) << shifts).sum(-1).to(pack_dtype) # pack_colsunpack_rows is the same with unsqueeze(1). Optionally, run the per-module conversion loop under torch.set_num_threads(1): on CPU-device loads that took convert from 8–15 s to 0.12 s, bitwise equal (there is already a commented-out threadpool_limits(1) hint there).
At 70B (~560 modules) this saves roughly 1–2.5 min of load time on a busy host.
2. qweight is zero-filled in host RAM, then always replaced from the checkpoint
gptqmodel/nn_modules/qlinear/__init__.py:1175-1178 (_register_gptq_buffers) creates qweight with t.zeros. The loader builds the model on CPU (loader.py:2091 → make_quant :2266), and accelerate then swaps each buffer for a new device tensor, so the zero-filled pages are never read. The checkpoint must provide every qweight anyway (validate_checkpoint_qweights, utils/model.py:2578).
TORCH backend, GPU load:
- Peak RSS: 3560 → 2788 MB (−772 MB; the load delta drops by 30%).
make_quant: 1.4–2.3 s → 0.6–1.0 s.- state_dict and logits are bitwise equal.
Fix: use t.empty for qweight, at least on the from_quantized path; the pack paths always register a new qweight anyway. At 70B this avoids zero-filling ~35 GB of host RAM. Marlin is unaffected; for device="cpu" loads there is no peak gain.
3. Re-saving a loaded model rebuilds the whole model on CPU from disk, and replaces self.model
gptqmodel/models/writer.py:1861-1865 → get_model_with_quantize (:2140-2216) runs from_config, CPU make_quant (with the zero-filled buffers from item 2), then a full load_checkpoint_in_model into CPU fp16, only to write the same bytes back out.
| main | streaming prototype | |
|---|---|---|
| save time | 5.1 s (5.17 s rebuild + 0.31 s write) | 0.65 s |
| peak RSS delta | +2.6 GB (+1.3 GB still held afterwards) | +0.5 GB |
model.safetensors is byte-identical (same sha256).
Behaviour bug on the same path: writer.py:1862 assigns self.model = self.get_model_with_quantize(...). After save(), the user's model has been swapped for a CPU TorchCpuAvx512Linear model and the loaded GPU (e.g. Marlin) model has been freed: GPU allocation drops to ~0 and later inference silently runs on CPU. This path also casts all non-quantized tensors to fp16; that part is reported in #3976 item 1. The prototype keeps the cast so its output stays byte-equal to main.
Fix:
- Build a meta-device skeleton only for key order and the save plan.
- Point each
TensorSourceat anOffloadTensorRefinto the source safetensors: a raw byte stream viadata_offsets/_copy_file_streamwhen the dtype is unchanged, or lazyget_tensor().to(dtype)when it must be cast. - Then call the existing
streaming_state_dict_to_shards. - Leave
self.modeluntouched.
At 70B this avoids materialising the ~38 GB checkpoint plus ~35 GB of zeroed buffers in host RAM.
4. GPTQ dequantize_model builds a whole dense shard in memory before save_file
gptqmodel/utils/model_dequant.py:1585-1666 (convert_gptq_file) accumulates a dict for the whole shard, and on GPU it also loads every qweight in the shard onto the device at once. :1924 then calls save_file.
Streaming one module at a time (header computed up front from qweight shapes, written via _write_shard_file with lazy TensorSources):
- Peak RSS: +4.16 GB → +1.92 GB (−54%).
- GPU peak: 2224 → 753 MB (−66%).
- Time: unchanged (2.6 s on GPU).
- The output file is byte-identical.
- The GPU path needs item 1.
A 4 GB GPTQ shard expands to ~14 GB of bf16, which is held today along with save_file's own copy.
Unconfirmed follow-ups
- v2→v1 during pack: instead of pack → unpack → shift → repack, pack
(zeros - 1) & maxqdirectly. It is bitwise equal by construction but not measured.pack_modulesingle-threaded was 15–18× faster in isolation (525 → 33 ms per module), but end-to-endquantize()pack time only improved 3.1–4.3 s → 2.7–2.8 s on this noisy host. - Marlin startup:
_ensure_generated_marlin_kernelsspawns agenerate_kernels.py --checksubprocess on first use (~0.7 s per process); a fingerprint sentinel would skip it. g_idx:_register_gptq_buffersbuilds it with a Python list comprehension;torch.arange(in) // group_sizegives the same values.
🤖 Generated with Claude Code
Activity
docs(dequant): document the qzeros device-preserving conversion (#3985)
perf(dequant): vectorise and device-preserve qzeros pack/unpack (#3985)…
Qubitium commentedon Nov 14, 2026
Severity update for item 3: after
save(), the swapped-inm.modelreturns garbage (relative logit error 1.47), not just CPU inference.writer.py:1990still replacesself.model. See #4053 item 4.