Skip to content

fix(p2p): delegate native VMM peer enablement to PyTorch - #3815

Merged
Qubitium merged 4 commits into
mainfrom
fix/rocm-p2p-probe-guard
Nov 13, 2026
Merged

Qubitium merged 4 commits into
mainfrom
fix/rocm-p2p-probe-guard

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Native allocators must own peer mappings for existing and future VMM reservations. Delegate peer enablement and direct copies to PyTorch on CUDA and ROCm. Non-native or unreadable ROCm allocators fail closed to host staging.

Allocator settings strings cannot establish copy safety: partial or empty updates can leave expandable segments enabled, and disabling them leaves existing VMM tensors alive. Native allocators now always delegate, including after rejected updates; unknown CUDA backends also withhold raw copies. Runtime policy stays on P2PConfig. The PR changes 7 files (+920/-26).

Flow and failure paths

flowchart TD
    A["_runtime_allocator_backend()<br/>active backend only"] --> B{"_rocm_peer_link_unusable()<br/>ROCm and backend not native?"}
    B -- "yes, including unreadable backend" --> C["enable_peer_access: every pair degraded<br/>reason_code = REASON_ROCM_VMM"]
    C --> D["moves staged through pinned host memory"]
    B -- "no" --> F{"_pytorch_owns_peer_enablement()<br/>native or backend unknown?"}
    F -- "no" --> G["_enable_one: runtime enablement<br/>existing raw-copy validation"]
    F -- "yes, CUDA or ROCm" --> H["_enable_one returns REASON_PYTORCH_ENABLEMENT<br/>no direct runtime enablement"]
    H --> I["_raw_peer_copy_unsafe() = True<br/>raw enqueue withheld"]
    I --> J["_copy_payloads_like_movers<br/>Tensor.copy_ validates the direct path"]
    G --> O["_validate_peer_copy_pair<br/>_verify_peer_payloads"]
    J --> O
    O --> P{"payload bytes match?"}
    P -- "no" --> M["pair degraded"]
    P -- "yes" --> K["_timed_gbps"]
    K --> L{"copy returns False?"}
    L -- "yes, measurement unavailable" --> M
    L -- "no" --> N["classify measured direct path"]
    style C fill:#ffdddd,stroke:#cc0000
    style M fill:#ffdddd,stroke:#cc0000
Loading

The same flow and contracts are committed in Multi-GPU quantization, linked from README.

Before / after

Mechanism Before After
Native enablement _enable_one always pre-enabled peers (gptqmodel/utils/p2p.py:654 at merge base 276605140). Native allocators delegate enablement and direct copies to PyTorch on both CUDA and ROCm.
Settings as allocation evidence _allocator_forces_vmm (gptqmodel/utils/p2p.py:179 at f9707a0eb) allowed raw copies when the last string omitted or disabled expandable segments, despite existing VMM tensors. Remove settings-string authorization. Native allocators always delegate, preserving ownership across settings changes.
Unknown backend _pytorch_owns_peer_enablement (gptqmodel/utils/p2p.py:196 at f9707a0eb) did not delegate unreadable CUDA backends. Unknown backends withhold raw copies; CUDA delegates and ROCm stages.
Copy validation _validate_peer_copy_pair (gptqmodel/utils/p2p.py:854 at merge base) validated raw enqueues even where movers used Tensor.copy_. _copy_payloads_like_movers validates and times the selected path. Tests retain allocator guards.
Failed timing step _timed_gbps (gptqmodel/utils/p2p.py:922 at dcec732c0) ignored false enqueue results. Failed warm-up or measured copies return no bandwidth; AUTO degrades the pair.
Nested ROCm policy _ROCM_NESTED_PEER_DISABLED (gptqmodel/utils/p2p.py:1904 at dcec732c0) used an environment switch. P2PConfig.use_rocm_nested_peer_copy is explicit and cannot bypass allocator guards.

Validation

All runtime checks used PYTHON_GIL=0.

Check CUDA ROCm
pytest tests/test_p2p.py tests/test_p2p_copy_utils.py (plus tests/test_alloc_conf.py on CUDA) 96 passed, 14 skipped 87 passed, 15 skipped
P2P suite after disabling expandable segments through the Torch API 88 passed, 14 skipped 88 passed, 14 skipped
gil0_api_probe.py --only gptqmodel.p2p.allocator_guard 3/3 independent runs passed 3/3 independent runs passed
test_mover_fallbacks_are_ordered_and_thread_safe 3/3 independent runs passed 3/3 independent runs passed

The four allocator-update regressions failed before the correction and pass afterward. They retain real VMM tensors across partial, empty, explicit-disable and rejected settings updates, then verify exact copies in both directions. The separate mover stress uses 8 workers and 32 tasks per process with distinct streams. The concurrency ledger records the removal of settings-string reads from the threaded path. Ruff and git diff --check pass.

Native raw-copy tests intentionally skip, even with expandable segments disabled; actual mover fallbacks remain covered. Existing non-native CUDA raw paths were not exercised in this validation. No speedup is claimed for the changed non-expandable path. This change covers helper-managed transfers; Accelerate hook routing retains its existing behavior.

`hipMemcpyPeerAsync` is not sound against ROCm memory served from a VMM
reservation, and GPT-QModel installs `expandable_segments:True` by default.
Measured on gfx1201 with ROCm 10.1 and a 4 MiB payload: after peer access was
enabled, a raw runtime copy returned 0 and left the destination untouched, and
the accompanying illegal-address fault poisoned the HIP context so the next
unrelated `torch.cuda` call failed. `enable_peer_access` issues exactly such a
copy from `_validate_peer_copy_pair`, so any multi-GPU ROCm run (quantize,
subset, checkpoint recovery) could damage the context during startup probing.

The failure mode is the allocator's memory class, not the topology, and it is
not limited to expandable segments: `backend:cudaMallocAsync` showed the same
non-copy and intermittent fault. Detect it before any raw copy is issued:

* `_allocator_forces_vmm` reads the allocator settings Torch actually parsed
  (`torch._C._accelerator_getAllocatorSettings`) and the live backend, falling
  back to the environment for builds that do not expose them. The parsed value
  wins because a later environment change is invisible to the allocator.
* `_rocm_raw_peer_copy_unsafe` gates `_validate_peer_copy_pair`,
  `enable_peer_access`, `can_transfer_directly`, `_can_peer_copy` and
  `_peer_copy_batch_enqueue`, so no probe, enablement or data copy reaches the
  runtime call. Every directed pair is reported `degraded` and moves use the
  existing pinned-host staging path.
* `PeerStatus.reason_code = REASON_ROCM_VMM` keeps the one-time warning on the
  allocator instead of the motherboard/BIOS wording meant for topology
  problems.
* `peer_copy_batch` rejects a mixed-device group before probing the pair.

Also add a `gptqmodel.p2p.allocator_guard` GIL=0 probe: the guard runs from
worker threads on every cross-device move, so its metadata reads are recorded
in the compatibility ledger.

Document the flow, the measured matrix, and the repair (native allocator
without `expandable_segments`).
ROCm peer copies fail on a VMM-backed allocator, and the original diagnosis was
too broad. The defect is the ctypes `hipDeviceEnablePeerAccess` call, not the
allocator: it grants access to the expandable segments that exist at that
moment, and segments the allocator reserves afterwards stay unreachable. A copy
into one of those returns success while moving nothing.

Measured on gfx1201 with ROCm 10.1, 4 MiB payload, source buffer allocated
first, peer access pre-enabled with ctypes: `Tensor.copy_` copied nothing in 40
of 80 rounds (3 of 3 processes), and the raw `hipMemcpyPeerAsync` behaved the
same. Letting PyTorch own enablement -- its copy path calls `hipMemSetAccess`
per segment, `ExpandableSegment::addPeer` -- copied 80 of 80 rounds
byte-exactly in 3 of 3 processes with the same `expandable_segments:True`
setting. That is the GPT-QModel default, so the link is kept rather than
degraded: 28.7 GB/s direct against 22.7 GB/s ordered host staging.

Separate the two failure modes instead of treating them as one:

* `_allocator_is_native` resolves the backend from the parsed allocator
  settings, then the live backend query, then the environment, and fails closed
  when none can be read.
* `_rocm_pytorch_owns_peer_enablement` is the native + `expandable_segments`
  case: `_enable_one` returns without calling `hipDeviceEnablePeerAccess`, and
  the raw ctypes copy is withheld while the pair stays `enabled` and direct.
* `_rocm_peer_link_unusable` is the non-native backend case: `cudaMallocAsync`
  returned mismatched bytes even from a plain same-device copy, so every pair is
  reported `degraded`, `reason_code` is `REASON_ROCM_VMM`, and moves stage
  through pinned host memory.

The probe must validate what the movers run, otherwise it demotes a healthy
link for a copy nothing depends on: `_copy_payloads_like_movers` selects
`Tensor.copy_` whenever the raw enqueue is withheld, and both the correctness
check and the timing loop go through it.

Also stop the degraded warning from blaming the motherboard/BIOS when the cause
is the allocator, and extend the GIL=0 probe to assert all three verdicts are
thread-stable.

Document the corrected diagnosis, the two branches, and the measured matrix.
Comment thread gptqmodel/utils/p2p.py Dismissed
Comment thread gptqmodel/utils/p2p.py Fixed
@ZX-ModelCloud ZX-ModelCloud changed the title fix(p2p): delegate ROCm peer enablement instead of pre-enabling it fix(p2p): delegate native VMM peer enablement to PyTorch Nov 13, 2026

@ZX-ModelCloud ZX-ModelCloud left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and fixed allocator detection, failed-copy bandwidth handling, and hardware tests that bypassed safety guards. Native expandable allocators now delegate peer enablement/copies to PyTorch on CUDA and ROCm; nested-copy policy is explicit in P2PConfig. Targeted regressions and three independent GIL-disabled guard/mover runs per backend pass.

Comment thread gptqmodel/utils/p2p.py Fixed

@ZX-ModelCloud ZX-ModelCloud left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed a remaining allocator-safety gap in c15043a: partial, empty, disabled, or rejected settings updates could re-enable raw peer copies while VMM tensors remained alive. Native allocators now always retain PyTorch ownership; unknown backends also withhold raw copies. All four regressions now pass, along with CUDA/ROCm P2P suites and three independent GIL-disabled guard/mover runs per backend. Flow documentation and the concurrency ledger are updated.

Comment thread gptqmodel/utils/p2p.py Dismissed
@Qubitium
Qubitium merged commit af11862 into main Nov 13, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/rocm-p2p-probe-guard branch November 13, 2026 08:06
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.

2 participants