Skip to content

fix(cpu): detach the AWQ int4 operands from the checkpoint mmap (#4785 follow-up) - #4849

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/awq-cpu-int4-mmap-detach
Nov 17, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/awq-cpu-int4-mmap-detach

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Follow-up to #4785 (which fixed the GPTQ CPU paths and is merged as #4842).

A safetensors load hands the CPU int4 modules zero-copy views of the shard mapping. The first-forward repack replaces qweight/qzeros with private tensors, and any other operand still pointing into the file pins the whole shard — every page the loader faulted in stays resident, so the model counts its quantized weights twice in RSS.

The AWQ siblings had the same gap:

  • TorchAtenAwqLinear.transform_cpu keeps scales at the checkpoint dtype and order by design and never touches bias, so both stay views of the mapping. Measured with the module's tensors bound to a real mmap: ['scales', 'bias'] are still inside it after the transform.
  • TorchCpuAvx512AwqLinear.transform_cpu reached for to(torch.float32).contiguous(), which returns the same tensor when the dtype already matches, so a checkpoint whose scales are already FP32 aliases as well. That variant does not reproduce with the FP16 scales these helpers build, so its test is a guard rather than a reproduction.

Both now route the operands they keep through the detach_cpu_operand helper added in #4842, and the AVX512 path detaches g_idx too. Values, dtype and order are unchanged.

Verification

test_cpu_awq_int4_mmap_detach_4785.py binds the module's checkpoint tensors to a real mmap, asserts the setup really did alias, runs transform_cpu, then asserts no parameter or buffer points inside the mapping and that scales is unchanged.

case parent commit (e19031750) this PR
TorchAtenAwqLinear (ckpt_scales) FAIL — still keeps ['scales', 'bias'] inside the checkpoint mapping PASS
TorchAtenAwqLinear (fp32_scales) FAIL — same PASS
TorchCpuAvx512AwqLinear (ckpt_scales) PASS PASS (guard)
TorchCpuAvx512AwqLinear (fp32_scales) PASS PASS (guard)

tests/kernels/test_awq_runtime_dtype_storage.py, whose helpers this file reuses, still passes.

CPU-only; run on the x86-64 AVX512-BF16 host.

🤖 Generated with Claude Code

…lCloud#4785 follow-up)

ModelCloud#4785 fixed the GPTQ CPU paths: a safetensors load hands the module zero-copy
views of the shard mapping, the first-forward repack replaces `qweight` and
`qzeros` with private tensors, and any other operand still pointing into the
file pins the whole shard, so every page the loader faulted in stays resident.

The AWQ siblings had the same gap:

- `TorchAtenAwqLinear.transform_cpu` keeps `scales` at the checkpoint dtype and
  order by design and never touches `bias`, so both stay views. Measured with
  the module's tensors bound to a real `mmap`: `['scales', 'bias']` are still
  inside the mapping after the transform.
- `TorchCpuAvx512AwqLinear.transform_cpu` reached for
  `to(torch.float32).contiguous()`, which returns the same tensor when the dtype
  already matches, so a checkpoint whose scales are already FP32 aliases too.
  That variant does not reproduce with the FP16 scales these helpers build, so
  its test is a guard rather than a reproduction.

Both now route the operands they keep through `detach_cpu_operand`, and the
AVX512 path detaches `g_idx` as well. Values, dtype and order are unchanged.

Tests
- `test_cpu_awq_int4_mmap_detach_4785.py` binds the module's checkpoint tensors
  to a real `mmap`, asserts the setup aliased, runs `transform_cpu`, then asserts
  no parameter or buffer points inside the mapping and that `scales` is
  unchanged. On the parent commit the two `TorchAtenAwqLinear` cases fail with
  `still keeps ['scales', 'bias'] inside the checkpoint mapping`; all four cases
  pass here.
- `test_awq_runtime_dtype_storage.py` (the suite these helpers come from) still
  passes.
@Qubitium
Qubitium merged commit 4f287eb into ModelCloud:main Nov 17, 2026
1 check passed
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.
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