Repository navigation
fix(cpu): copy a checkpoint operand only when it aliases the shard mapping (#4876) - #4878
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
detach_cpu_operand(#4842/#4849, the #4785 fix) cloned every operand it was given. Two #4395 assertions are red onmainbecause of it:both at
assert module.scales is live_scales. #4395 item 1 removed the byte-identical second copy ofscaleson 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
safetensorsmapping.What Changed
gptqmodel/nn_modules/qlinear/__init__.pydetach_cpu_operand: clone only when the storage is a checkpoint alias. A file-backed storage is non-resizable (that is howsafetensorsandtorch.frombufferover anmmapcreate it) while an ordinary allocation is resizable, sonot storage.resizable()is the discriminator. Module-owned tensors (including theqzerosview intoscales_and_zeros) are returned unchanged, so their identity and storage survive.Noneand non-tensors still pass through unchanged; a storage that raises onresizable()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 realmmapis 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
The four failures are
test_checkpoint_state_invariant.pytorch_cpu_avx512nodes; they fail identically on unmodifiedorigin/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:With the fix:
3 passed. The #4395 file goes from2 failed, 22 passedto green, andtest_cpu_int4_mmap_detach_4785.pystays green (25 passed, 1 skipped over the two files).ruff checkon both changed files: All checks passed.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
safetensors/frombufferstorages are non-resizable, which the new test asserts explicitly).Kernel Accuracy Requirements
Closes #4876. Refs #4395, #4785.