Repository navigation
fix(cpu): detach the operands the int4 repack keeps from the checkpoint mmap (#4785) - #4842
Merged
Merged
Conversation
…nt mmap (ModelCloud#4785) A CPU load keeps quantized tensors as zero-copy views of the shard's `safetensors` mapping. The first forward runs `transform_cpu`, which replaces `qweight`/`qzeros` with private tensors, but two per-column operands stayed views of that mapping: - `g_idx` on both the AVX512 and the ATen path; - `scales` on the ATen path, which deliberately keeps the checkpoint dtype and order (`to(float32).contiguous()` on the AVX512 path also returns the same tensor when the dtype already matches, so it aliased too). One surviving view pins the whole shard: every page the loader touched stays resident, so an 8B model reports about 1.6x its real working set in RSS (measured 15.6 GB vs 10.1 GB on Llama-3.1-8B-GPTQ-INT4). The pages are clean, so the kernel can reclaim them, but RSS-based monitoring and container limits see the difference. `detach_cpu_operand()` in `qlinear/__init__.py` clones such an operand once, at repack time, and both `transform_cpu` implementations now route the operands they keep through it. Values, dtype and order are unchanged, so numerics and a later save are unaffected; the copy is per-column metadata (4.75 MiB of `g_idx` on 8B, ~109 MiB of `scales` on ATen) against the shard pages it releases. Tests - `test_avx512_repack_detaches_every_checkpoint_view` and `test_aten_repack_detaches_every_checkpoint_view` bind the module's checkpoint tensors to a real `mmap` of a file, assert the setup really aliased a mapping, run `transform_cpu`, and then assert no parameter or buffer points inside those mappings. Values of the retained operands are compared too. On the parent commit they fail with exactly the operands the issue names: `TorchCpuAvx512Linear still keeps ['g_idx']` and `TorchAtenLinear still keeps ['scales', 'g_idx']`. - `test_resave_dense_parameter_dtype_4466.py`, `test_awq_runtime_dtype_storage.py` and the AVX512/ATen cases of `test_kernel_compile_levels_gptq.py` still pass (9 passed, 1 skipped; 2 passed).
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
…pping (#4876) (#4878) `detach_cpu_operand` (#4842/#4849) cloned unconditionally, which rebinds and duplicates tensors the module already owns. That regressed two #4395 int4pack snapshot assertions on main: tests/test_cpu_int4_snapshot_dedup_4395.py::test_int4pack_snapshot_drops_duplicate_operands[torch_aten] tests/test_cpu_int4_snapshot_dedup_4395.py::test_int4pack_snapshot_drops_duplicate_operands[awq_torch_aten] both at `assert module.scales is live_scales`. The whole point of #4395 item 1 was that `scales` stays the loader's buffer and is not copied; the clone also defeats the snapshot dedup accounting by adding a second private allocation. The distinction the helper actually needs is "does this storage alias the checkpoint mapping". A file-backed storage is non-resizable (that is how safetensors and torch.frombuffer over an mmap build it) while an ordinary allocation is resizable, so the helper now clones only the former. #4785 keeps its copy: the tensors it detaches are exactly the non-resizable ones. Non-CPU tensors, None and non-tensors still pass through unchanged, and views into a private allocation (the `qzeros` slice of `scales_and_zeros`) are left alone because they pin nothing. Tests: new `tests/test_cpu_operand_detach_identity.py` pins both directions of the discriminator. On unmodified main `test_private_tensor_keeps_identity` fails (`detach_cpu_operand(t) is not t`); with the fix all three pass. tests/test_cpu_int4_snapshot_dedup_4395.py tests/kernels/test_cpu_int4_mmap_detach_4785.py tests/test_cpu_operand_detach_identity.py tests/test_checkpoint_state_invariant.py tests/test_pinned_host_cache_4395.py -> 46 passed, 9 skipped, 4 failed (the 4 are the torch_cpu_avx512 nodes in test_checkpoint_state_invariant.py, identical on unmodified main) Closes #4876. Refs #4395, #4785.
Qubitium
added a commit
to Qubitium/GPTQModel
that referenced
this pull request
Nov 17, 2026
… 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
added a commit
that referenced
this pull request
Nov 17, 2026
… mapping (#4873) (#4918) `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, #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. `#4842`/`#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 (#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 #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.
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.
Fixes #4785.
A CPU load keeps quantized tensors as zero-copy views of the shard's
safetensorsmapping. The first forward runstransform_cpu, which replacesqweight/qzeroswith private tensors, but two per-column operands stayed views of that mapping:g_idxon both the AVX512 and the ATen path;scaleson the ATen path, which deliberately keeps the checkpoint dtype and order. On the AVX512 pathto(torch.float32).contiguous()also returns the same tensor when the dtype already matches, so it aliased too.One surviving view pins the whole shard: every page the loader touched stays resident, so an 8B model reports about 1.6x its real working set in RSS (the issue measured 15.6 GB vs 10.1 GB on Llama-3.1-8B-GPTQ-INT4). The pages are clean, so the kernel can reclaim them — but RSS-based monitoring and container limits see the difference.
Change
detach_cpu_operand()inqlinear/__init__.pyclones such an operand once, at repack time. Bothtransform_cpuimplementations now route the operands they keep through it:torch_cpu_avx512.py:g_idx, plusscales/biaswhen the dtype conversion returned the same tensor.torch_aten_kernel.py:scalesandg_idx, after the runtime layout is built.Values, dtype and order are unchanged, so numerics and a later save are unaffected. The copy is per-column metadata (4.75 MiB of
g_idxon 8B; ~109 MiB ofscaleson ATen) against the shard pages it releases.Verification
test_avx512_repack_detaches_every_checkpoint_viewandtest_aten_repack_detaches_every_checkpoint_viewbind the module's checkpoint tensors to a realmmapof a file, assert the setup really did alias a mapping, runtransform_cpu, then assert no parameter or buffer points inside those mappings. The retained operands' values are compared too, so detaching cannot change numerics.6fe07cb6d)TorchCpuAvx512Linear still keeps ['g_idx'] inside the checkpoint mappingTorchAtenLinear still keeps ['scales', 'g_idx'] inside the checkpoint mappingtest_resave_dense_parameter_dtype_4466.py,test_awq_runtime_dtype_storage.pytest_kernel_compile_levels_gptq.py -k "avx512 or aten"CPU-only; run on the x86-64 AVX512-BF16 host.
🤖 Generated with Claude Code