Skip to content

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

Closed

Description

@Qubitium

detach_cpu_operand (gptqmodel/nn_modules/qlinear/__init__.py) was introduced by #4842/#4849 for #4785: a CPU load keeps quantized tensors as zero-copy views of the shard's safetensors mapping, and a single surviving view pins every page the loader touched, so the int4 repack detaches scales/g_idx/bias.

The helper clones unconditionally, which regressed two #4395 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 fail at assert module.scales is live_scales — the int4pack transform must not rebind or clone the checkpoint scales buffer, which is the whole point of #4395 item 1 (the byte-identical second copy was removed). The clone also defeats the module's own snapshot dedup accounting: scales becomes a second private allocation.

Not caused by #4785's fix — #4785 still needs a copy for a tensor that really aliases the checkpoint mapping.

Expected

detach_cpu_operand copies only a tensor that aliases the checkpoint, and returns a module-owned tensor unchanged. A file-backed torch storage (from safetensors or torch.frombuffer over an mmap) is non-resizable; an ordinary allocation is resizable, which is exactly the distinction that matters here.

Non-CPU tensors, None, and non-tensors pass through unchanged, as before.

Test that should have caught it

A unit test on the helper itself: a private tensor must keep its identity, and a tensor bound to a real mmap must be copied with equal values. The #4785 regression test (tests/kernels/test_cpu_int4_mmap_detach_4785.py) only covers the mmap side, and the #4395 test only covers the module side, so nothing pinned the discriminator.

Found while rebasing a stale #4785 branch; recorded on #4843.

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4876 is working on this issue now.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4876-detach is working on this issue now.

    Started 2026-11-17T17:53:32Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    claiming: make detach_cpu_operand copy only file-backed (non-resizable) storages so module-owned scales keep their identity

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released — already fixed.

    75feee538 (#4878) landed the same fix: detach_cpu_operand now copies only a non-resizable (checkpoint-backed) storage, so a module-owned tensor keeps its identity. Verified on main @ d8758c482:

    python -m pytest tests/test_cpu_int4_snapshot_dedup_4395.py -q      # 24 passed
    python -m pytest tests/kernels/test_cpu_int4_mmap_detach_4785.py -q # 2 passed
    

    Both test_int4pack_snapshot_drops_duplicate_operands[torch_aten] and [awq_torch_aten] are green, and the #4785 mmap detach contract still holds. My duplicate PR #4883 is closed.

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Fixed by #4878 (squash-merged). detach_cpu_operand now clones only a storage that aliases the checkpoint (not storage.resizable()); module-owned tensors keep their identity, so the #4395 scales is live_scales assertions are green again while #4785 still detaches the mmap-backed operands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions