Skip to content

perf(cpu): release the checkpoint mmap after the int4 repack (#4785) - #4843

Closed
Qubitium wants to merge 1 commit into
mainfrom
root/cpu-int4-views-4785
Closed

Qubitium wants to merge 1 commit into
mainfrom
root/cpu-int4-views-4785

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

perf(cpu): release the checkpoint mmap after the int4 repack (#4785)

A safetensors load hands the CPU int4 modules zero-copy views of the
shard mapping. The first-forward repack replaces qweight/qzeros with
anonymous runtime tensors but kept g_idx, and on the ATen/fused kernels
the checkpoint-dtype scales and bias, as views of the file. Each
surviving view pins the whole shard, so every page the loader faulted in
stays resident and the quantized weights are counted twice in RSS.

Detach every module tensor still backed by a checkpoint file mapping at
the end of the CPU int4 transform. The sweep copies only the small
operands that survive the repack byte-identically; the packed weight is
still rebuilt in place and never copied a second time. It runs outside
inference_mode so a later load_state_dict can write the copied buffer.
Non-Linux and anonymous tensors are left untouched.

Measured on a synthetic 2048x8192 GPTQ layer loaded through safe_open
(TorchAtenLinear, 8.3 MiB shard):

  • before: 2137 resident shard pages (8.3 MiB), scales + g_idx mapped
  • after: 0 resident shard pages, no module tensor in the mapping
    The same layer on unmodified main keeps all 2137 pages and both views.

The regression test loads a synthetic checkpoint through safe_open, runs
one forward, asserts the shard disappears from /proc/self/maps and no
module tensor storage lies in a file mapping, and compares the output to
the pre-repack dense result and to a repack of anonymous copies
(bitwise). It fails on main for TorchAtenLinear and TorchFusedLinear and
passes with this change. TorchCpuAvx512Linear is skipped on hosts
without AVX512-BF16.

Tests: new test 4 passed / 2 skipped; 229 passed / 111 skipped across
the CPU kernel and checkpoint-state suites; the 4 AVX512-BF16
checkpoint-state failures reproduce identically on main (host lacks
AVX512-BF16).

A safetensors load hands the CPU int4 modules zero-copy views of the
shard mapping. The first-forward repack replaces qweight/qzeros with
anonymous runtime tensors but kept g_idx, and on the ATen/fused kernels
the checkpoint-dtype scales and bias, as views of the file. Each
surviving view pins the whole shard, so every page the loader faulted in
stays resident and the quantized weights are counted twice in RSS.

Detach every module tensor still backed by a checkpoint file mapping at
the end of the CPU int4 transform. The sweep copies only the small
operands that survive the repack byte-identically; the packed weight is
still rebuilt in place and never copied a second time. It runs outside
inference_mode so a later load_state_dict can write the copied buffer.
Non-Linux and anonymous tensors are left untouched.

Measured on a synthetic 2048x8192 GPTQ layer loaded through safe_open
(TorchAtenLinear, 8.3 MiB shard):
- before: 2137 resident shard pages (8.3 MiB), scales + g_idx mapped
- after:  0 resident shard pages, no module tensor in the mapping
The same layer on unmodified main keeps all 2137 pages and both views.

The regression test loads a synthetic checkpoint through safe_open, runs
one forward, asserts the shard disappears from /proc/self/maps and no
module tensor storage lies in a file mapping, and compares the output to
the pre-repack dense result and to a repack of anonymous copies
(bitwise). It fails on main for TorchAtenLinear and TorchFusedLinear and
passes with this change. TorchCpuAvx512Linear is skipped on hosts
without AVX512-BF16.

Tests: new test 4 passed / 2 skipped; 229 passed / 111 skipped across
the CPU kernel and checkpoint-state suites; the 4 AVX512-BF16
checkpoint-state failures reproduce identically on main (host lacks
AVX512-BF16).
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Closing as superseded — #4785 was fixed by #4842 (9cd6f78b5) at 16:45Z, before this PR was opened, and this branch has been conflicting since.

The GPTQ CPU paths from #4785 are in main (detach_cpu_operand in torch_cpu_avx512.py / torch_aten_kernel.py), with tests/kernels/test_cpu_int4_mmap_detach_4785.py covering them.

The part of this PR's idea that the merged fix did not cover — a generic sweep over every tensor still backed by a checkpoint mapping — turned out to matter for the AWQ siblings, which still pinned scales/bias. I reproduced that and landed it as #4849, which reuses the same detach_cpu_operand helper on torch_aten_kernel_awq.py and torch_cpu_avx512_awq.py, with a test that fails on main for TorchAtenAwqLinear (still keeps ['scales', 'bias'] inside the checkpoint mapping).

If you want to pursue the generic sweep (detect file-backed storage instead of naming operands), it is worth its own PR against #4849's test file — that would also cover torch_fused.py / torch_fused_awq.py, which I have not measured.

@Qubitium Qubitium closed this Nov 17, 2026
@Qubitium

Copy link
Copy Markdown
Collaborator Author

One residual gap for a separate follow-up, verified on main @ 6ec93e632 (not filed, since #4785 is closed):

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