Skip to content

fix(cpu): copy a checkpoint-backed operand only, not every module buffer (#4876) - #4883

Closed
Qubitium wants to merge 1 commit into
ModelCloud:mainfrom
Qubitium:fix/4876-detach-resizable
Closed

Qubitium wants to merge 1 commit into
ModelCloud:mainfrom
Qubitium:fix/4876-detach-resizable

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

detach_cpu_operand (gptqmodel/nn_modules/qlinear/__init__.py), added by #4842/#4849 for #4785, cloned unconditionally. That regressed two #4395 assertions: the int4pack transform must not rebind or clone the checkpoint scales buffer, because removing that byte-identical second copy is the whole point of #4395 item 1. Closes #4876.

The helper now copies only a tensor that really aliases the checkpoint:

storage resizable() result
safetensors / mmap / torch.frombuffer over a buffer False private clone (the #4785 contract)
ordinary allocation, or a view of one True returned unchanged, identity kept (the #4395 contract)
non-CPU tensor, None, non-tensor n/a unchanged, as before

A backend without resizable() keeps the previous fail-safe behaviour (clone), so no platform loses the #4785 protection.

Tests

New file tests/test_detach_cpu_operand_4876.py pins both sides of the discriminator — the #4785 test only covered the mmap side and the #4395 test only covered the module side, so nothing pinned the choice:

python -m pytest tests/test_detach_cpu_operand_4876.py -q
# 7 passed
# parent commit: test_module_owned_tensor_keeps_its_identity and
#                test_module_owned_view_keeps_its_identity fail

The two failures named in the issue are green again:

python -m pytest tests/test_cpu_int4_snapshot_dedup_4395.py -q
# 24 passed   (parent commit: test_int4pack_snapshot_drops_duplicate_operands[torch_aten]
#              and [awq_torch_aten] fail at `module.scales is live_scales`)

python -m pytest tests/kernels/test_cpu_int4_mmap_detach_4785.py -q
# 2 passed    (the mmap detach contract is preserved)

python -m pytest tests/test_cpu_int4pack_dispatch_contracts.py tests/test_torch_cpu_avx512.py \
    tests/test_torch_cpu_avx512_awq.py -q
# 100 passed

ruff check on the helper and the new test: All checks passed.

Logic Flow Documentation

Not applicable: one predicate added to an existing helper.

Model Support / Module Plan

Not applicable: no definition, plan, or checkpoint-layout change.

…fer (ModelCloud#4876)

`detach_cpu_operand` was added for ModelCloud#4785 so a CPU int4 repack does not keep a
`safetensors` mapping pinned through `g_idx`/`scales` views, but it cloned
unconditionally. That duplicated buffers the module already owns and broke the
ModelCloud#4395 item-1 identity contract: the int4pack snapshot must not rebind `scales`,
because the byte-identical second copy was the thing ModelCloud#4395 removed.

A `safetensors` (or `torch.frombuffer`) storage is file-backed and therefore not
resizable; an ordinary allocation is. Copy only when the storage is not
resizable, and return everything else unchanged. Non-CPU tensors, `None` and
non-tensors keep their previous behaviour.

Regression tests (red on the parent commit): `tests/test_detach_cpu_operand_4876.py`
pins the discriminator on both sides — a module-owned tensor and a view of one
keep their identity, while an `mmap` and a non-owning `bytearray` buffer are
copied. The two `test_int4pack_snapshot_drops_duplicate_operands` cases from
ModelCloud#4395 that this broke are green again.
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Superseded by #4878, which landed the same discriminator on main as 75feee538 (clone only a non-resizable, i.e. checkpoint-backed, storage) with its own regression test. Closing rather than re-merging the duplicate.

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