Skip to content

fix(cpu): copy a checkpoint operand only when it aliases the shard mapping (#4876) - #4878

Merged
Qubitium merged 1 commit into
mainfrom
root/fix-4395-detach
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/fix-4395-detach

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

detach_cpu_operand (#4842/#4849, the #4785 fix) cloned every operand it was given. Two #4395 assertions are red on main because of it:

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. #4395 item 1 removed the byte-identical second copy of scales on purpose; the unconditional clone put a copy back and also defeats the snapshot dedup accounting.

#4785 still needs the copy — but only for a tensor that really aliases the shard's safetensors mapping.

What Changed

  • gptqmodel/nn_modules/qlinear/__init__.py detach_cpu_operand: clone only when the storage is a checkpoint alias. A file-backed storage is non-resizable (that is how safetensors and torch.frombuffer over an mmap create it) while an ordinary allocation is resizable, so not storage.resizable() is the discriminator. Module-owned tensors (including the qzeros view into scales_and_zeros) are returned unchanged, so their identity and storage survive.
  • Non-CPU tensors, None and non-tensors still pass through unchanged; a storage that raises on resizable() falls back to the old pass-through (never to a clone).
  • tests/test_cpu_operand_detach_identity.py (new): pins both directions — a private tensor keeps identity, a tensor bound to a real mmap is copied with equal values and a resizable storage.

No numerics change: the values are byte-identical in both paths, and the #4785 copy still happens for exactly the tensors that pinned the mapping.

Tests

cd <worktree>
export CUDA_VISIBLE_DEVICES="" PYTHONPATH=<worktree> GPTQ_BYPASS_SAFETY_CHECKS=1 LOGBAR_ANIMATION=0
python -m pytest \
  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 -q
# 46 passed, 9 skipped, 4 failed in 9.27s

The four failures are test_checkpoint_state_invariant.py torch_cpu_avx512 nodes; they fail identically on unmodified origin/main (4 failed, 17 passed, 6 skipped) on this EPYC host, which has no AVX512-BF16.

Red / green for the new test on unmodified origin/main @ 6ec93e632:

tests/test_cpu_operand_detach_identity.py
# 1 failed, 2 passed  (test_private_tensor_keeps_identity)
#   assert detach_cpu_operand(tensor) is tensor  ->  is not

With the fix: 3 passed. The #4395 file goes from 2 failed, 22 passed to green, and test_cpu_int4_mmap_detach_4785.py stays green (25 passed, 1 skipped over the two files).

ruff check on both changed files: All checks passed.

  • Unit tests executed and passed; the four env-only failures are reproduced on unmodified main.
  • New targeted regression test added.

Model Support / Module Plan

Not applicable: no registry, definition or module-plan change.

Logic Flow Documentation

Not applicable: no control-flow change. The helper gains one predicate (storage.resizable()) on the same path; the mechanism is documented in its docstring and in the new test header.

Review Requirements

Kernel Accuracy Requirements

Closes #4876. Refs #4395, #4785.

…pping (#4876)

`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.
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.

[BUG] detach_cpu_operand clones every operand: #4842/#4849 regressed two #4395 int4pack snapshot assertions (scales must keep its identity)

1 participant