Skip to content

fix(cpu): detach the Fused int4 operands from the checkpoint mapping (#4873) - #4918

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/4873-fused-detach
Nov 17, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/4873-fused-detach

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Three CPU int4 kernels repack qweight/qzeros into private runtime tensors but left their per-column operands 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:

kernel operands left mapped
TorchFusedLinear.transform_cpu scales (kept at checkpoint dtype by design, #4178), g_idx
TorchFusedAwqLinear.transform_cpu_awq scales, bias
ParoLinear._ensure_cpu_layout scales, theta, pairs, channel_scales

#4842/#4849 fixed this for the ATen and AVX512 siblings; these three were missed. All 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).

Partially addresses #4873 (see Scope).

Measurements (from the issue)

model / kernel before after
Meta-Llama-3.1-8B-Instruct-GPTQ-INT4, GPTQ_TORCH_FUSED 448 mapped tensors, 14.76 GB RSS 0 mapped, 9.41 GB
Qwen2.5-0.5B-Instruct Paro 4-bit g128 672 mapped, 1.99 GB 0 mapped, 1.54 GB

state_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 real mmap of a file, asserts the setup really aliased the mapping, runs the transform, and asserts no parameter or buffer points inside it afterwards.

python -m pytest tests/kernels/test_cpu_int4_mmap_detach_4785.py -q
# 5 passed
# parent commit: test_fused_repack_detaches_every_checkpoint_view,
#                test_fused_awq_repack_detaches_every_checkpoint_view and
#                test_paro_cpu_layout_detaches_checkpoint_views fail

Existing Paro coverage is unchanged: tests/kernels/test_paro.py tests/test_paro_capture_order.py -> 42 passed, 12 skipped.

ruff check on the three kernels and the test: All checks passed.

Scope

Fixed here: the Fused GPTQ, Fused AWQ and Paro detach.

Not taken, with reasons:

Logic Flow Documentation

Not applicable: detach_cpu_operand calls at the end of existing transforms.

Model Support / Module Plan

Not applicable: no definition or plan change.

… 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
Qubitium force-pushed the fix/4873-fused-detach branch from 26625fc to fd94565 Compare November 17, 2026 18:26
@Qubitium
Qubitium merged commit b218dce into ModelCloud:main Nov 17, 2026
1 check passed
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.
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.

1 participant