Repository navigation
fix(cpu): detach the Fused int4 operands from the checkpoint mapping (#4873) - #4918
Merged
Merged
Conversation
… mapping (ModelCloud#4873) `TorchFusedLinear.transform_cpu` and `TorchFusedAwqLinear.transform_cpu_awq` repack `qweight`/`qzeros` into private runtime tensors, but left `scales` (which keeps the checkpoint dtype by design, ModelCloud#4178) and `g_idx` as zero-copy views of the shard's `safetensors` mapping. One surviving view pins every page the loader touched, so the replaced checkpoint pages stay resident. `ParoLinear._ensure_cpu_layout` has the same shape: it rebinds `qweight`/`qzeros` but leaves `scales`, `theta`, `pairs` and `channel_scales` mapped. `ModelCloud#4842`/`ModelCloud#4849` fixed this for the ATen and AVX512 siblings; the Fused pair and Paro were missed. All three now route those operands through `detach_cpu_operand`, which copies only a tensor that really aliases the mapping and returns a module-owned tensor unchanged (ModelCloud#4876). Measured by the issue on `Meta-Llama-3.1-8B-Instruct-GPTQ-INT4` with `GPTQ_TORCH_FUSED`: 448 module tensors (g_idx + scales) inside the mapping, 14.76 GB RSS; after detaching, 9.41 GB (-5.35 GB). Paro on Qwen2.5-0.5B-Instruct: 672 mapped tensors, 1.99 -> 1.54 GB. The `state_dict()` stays bitwise equal to the checkpoint in both cases, so only residency changes. Regression tests: three new cases in `tests/kernels/test_cpu_int4_mmap_detach_4785.py` bind the checkpoint tensors to a real `mmap` and assert no parameter or buffer points inside it after the transform, for `TorchFusedLinear`, `TorchFusedAwqLinear` and `ParoLinear`. All three fail on the parent commit. Not in this change (still open on ModelCloud#4873): the AWQ AVX512 `qweight` snapshot needs a verified inverse of the AWQ packer, and the `desc_act` ATen/Fused snapshot needs a permuted rebuild. Both are separate, larger changes.
Qubitium
force-pushed
the
fix/4873-fused-detach
branch
from
November 17, 2026 18:26
26625fc to
fd94565
Compare
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
…5013) (#5033) #4918 (fix of #4873) removed the fused/Paro operands that pinned the safetensors mapping, but two CPU int4 layouts still kept a full checkpoint-layout copy of `qweight` (`_fused_train_state_qweight`) for the life of the model, even though a bitwise inverse exists. 1. GPTQ `desc_act` on `TorchAtenLinear`/`TorchFusedLinear`: `_can_rebuild_checkpoint_qweight` returned False for every non-identity `ret_idx`, on the grounds that the ATen transform reorders the input rows "without keeping the gather". The module does keep `ret_idx` (it is `argsort(g_idx)`, a permutation of `[0, in_features)`), so `index_copy_` with that index is the exact inverse of `weight.index_select(0, ret_idx)`. `AUTO` with `dtype=torch.float16` on CPU selects this kernel, so the duplicate was not opt-in. 2. `TorchCpuAvx512AwqLinear` declared `_FUSED_TRAIN_STATE_REBUILD = None` ("no verified inverse") for the extension's `pack_awq_int4`. That op is a pure nibble permutation: each runtime byte pairs output column `group * 32 + row` (low nibble) with `group * 32 + row + 16` (high nibble) for one input feature, reading the checkpoint word through the AWQ reverse order. `rebuild_avx512_awq_qweight` undoes it with the reverse read plus `pack_awq_columns(..., order=AWQ_ORDER)`, in pure Torch, so it needs neither the extension nor AVX512-BF16 at save time. `TorchCpuAvx512AwqLinear.transform_cpu` now ends with `finalize_fused_train_state`, like the other CPU int4 transforms. Both inverses are only trusted after `finalize_fused_train_state` compares the rebuilt words bitwise with the clone captured before the transform; a mismatch keeps the clone, so an unverified layout can never reach a saved checkpoint. Measured on Qwen2.5-0.5B-Instruct, 4-bit g128 `desc_act`, CPU fp16 (`TorchAtenLinear`, `AUTO`'s choice), unique tensor storages before/after one forward: | tree | snapshot buffers | after forward | snapshot bytes | | --- | ---: | ---: | ---: | | `origin/main` | 168 | 620.3 MiB | 172.0 MiB | | this branch | 0 | 449.6 MiB | 1.3 MiB | `state_dict()` still emits the checkpoint `qweight` bitwise unchanged.
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
Three CPU int4 kernels repack
qweight/qzerosinto private runtime tensors but left their per-column operands as zero-copy views of the shard'ssafetensorsmapping. One surviving view pins every page the loader touched, so the replaced checkpoint pages stay resident:TorchFusedLinear.transform_cpuscales(kept at checkpoint dtype by design, #4178),g_idxTorchFusedAwqLinear.transform_cpu_awqscales,biasParoLinear._ensure_cpu_layoutscales,theta,pairs,channel_scales#4842/#4849fixed this for the ATen and AVX512 siblings; these three were missed. All now route those operands throughdetach_cpu_operand, which copies only a tensor that really aliases the mapping and returns a module-owned tensor unchanged (#4876).Partially addresses #4873 (see Scope).
Measurements (from the issue)
Meta-Llama-3.1-8B-Instruct-GPTQ-INT4,GPTQ_TORCH_FUSEDQwen2.5-0.5B-InstructParo 4-bit g128state_dict()is bitwise equal to the checkpoint in both cases — only residency changes.Tests
Three new cases in
tests/kernels/test_cpu_int4_mmap_detach_4785.py. Each binds the module's checkpoint tensors to a realmmapof a file, asserts the setup really aliased the mapping, runs the transform, and asserts no parameter or buffer points inside it afterwards.Existing Paro coverage is unchanged:
tests/kernels/test_paro.py tests/test_paro_capture_order.py-> 42 passed, 12 skipped.ruff checkon the three kernels and the test: All checks passed.Scope
Fixed here: the Fused GPTQ, Fused AWQ and Paro detach.
Not taken, with reasons:
qweightsnapshot (item 1): needs_FUSED_TRAIN_STATE_REBUILDbuilt onunpack_hf_int4+ the AWQ nibble order +pack_awq; the issue's own comment notes the verify cost is shared with [PERF] Regression from #4845 (fix of #4783), also #4832: verify-by-full-rebuild of every CPU int4 qweight makes the AVX512 AUTO first forward 10x slower (8B: 1.97 -> 23.5 s) and every state_dict() 72x slower (0.32 -> 22.9 s) #4884, so the two should land together.desc_actATen/Fused snapshot (item 2): needs a permuted rebuild in_can_rebuild_checkpoint_qweight; the AVX512 kind was allowed in fix(cpu): rebuild the AVX512 checkpoint qweight instead of cloning it (#4783) #4845, the ATen/Fused kinds are a separate change.Logic Flow Documentation
Not applicable:
detach_cpu_operandcalls at the end of existing transforms.Model Support / Module Plan
Not applicable: no definition or plan change.