Repository navigation
fix(p2p): delegate native VMM peer enablement to PyTorch - #3815
Conversation
`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.
ZX-ModelCloud
left a comment
There was a problem hiding this comment.
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.
ZX-ModelCloud
left a comment
There was a problem hiding this comment.
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.
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:#cc0000The same flow and contracts are committed in Multi-GPU quantization, linked from README.
Before / after
_enable_onealways pre-enabled peers (gptqmodel/utils/p2p.py:654at merge base276605140)._allocator_forces_vmm(gptqmodel/utils/p2p.py:179atf9707a0eb) allowed raw copies when the last string omitted or disabled expandable segments, despite existing VMM tensors._pytorch_owns_peer_enablement(gptqmodel/utils/p2p.py:196atf9707a0eb) did not delegate unreadable CUDA backends._validate_peer_copy_pair(gptqmodel/utils/p2p.py:854at merge base) validated raw enqueues even where movers usedTensor.copy_._copy_payloads_like_moversvalidates and times the selected path. Tests retain allocator guards._timed_gbps(gptqmodel/utils/p2p.py:922atdcec732c0) ignored false enqueue results._ROCM_NESTED_PEER_DISABLED(gptqmodel/utils/p2p.py:1904atdcec732c0) used an environment switch.P2PConfig.use_rocm_nested_peer_copyis explicit and cannot bypass allocator guards.Validation
All runtime checks used
PYTHON_GIL=0.pytest tests/test_p2p.py tests/test_p2p_copy_utils.py(plustests/test_alloc_conf.pyon CUDA)gil0_api_probe.py --only gptqmodel.p2p.allocator_guardtest_mover_fallbacks_are_ordered_and_thread_safeThe 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 --checkpass.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.