Repository navigation
[ROCm] Reland: Enable expandable segments (#173330) (#177974) (#3106) - #3179
Conversation
…77974) (ROCm#3106) Summary: Original pull request: pytorch#173330 Fixes pytorch#168737. Fixes pytorch#168736. The original diff enabled expandable segments for ROCm by adding `#ifdef USE_ROCM` guards throughout CUDACachingAllocator.cpp to use HIP APIs (hipMemAddressReserve, hipMemCreate, hipMemMap, etc.) instead of CUDA driver APIs when building for ROCm. Root cause: In HIP/ROCm 6.2.1, the field name for memory allocation properties is `requestedHandleType` (singular), not `requestedHandleTypes` (plural) as in CUDA. Additionally, `hipMemHandleTypeFabric` does not exist in HIP, so the `CU_MEM_HANDLE_TYPE_FABRIC` assignment must be skipped on ROCm. Fix applied on top of the original diff (from D96652342): - Use `prop.requestedHandleType = hipMemHandleTypePosixFileDescriptor` under `#ifdef USE_ROCM` (singular field name, HIP constant) - Use `prop.requestedHandleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR` for CUDA (plural field name, CUDA constant) - Skip the `CU_MEM_HANDLE_TYPE_FABRIC` assignment entirely on ROCm under `#ifndef USE_ROCM`, as `hipMemHandleTypeFabric` does not exist in HIP Co-authored-by: Prachi Gupta [email protected] Co-authored-by: Jeff Daily [email protected] Co-authored-by: moonshadow-25 [email protected] Co-authored-by: Vighanesh Sharma [email protected] Test Plan: ``` fbpkg build //aps_models/ads/ecosystem/eval/cogwheel_tests/amd:cogwheel_aps_ads_icvr_kd_eval_amd_test_harness --build-remote ``` https://www.internalfb.com/sandcastle/workflow/1049338713192153464 Differential Revision: D97211385 Pull Request resolved: pytorch#177974 Approved by: https://github.com/jeffdaily, https://github.com/echen4096 (cherry picked from commit 5792701) ## Motivation <!-- Explain the purpose of this PR and the goals it aims to achieve. --> ## Technical Details <!-- Explain the changes along with any relevant GitHub links. --> ## Test Plan <!-- Explain any relevant testing done to verify this PR. --> ## Test Result <!-- Briefly summarize test outcomes. --> ## Submission Checklist - [ ] Look over the contributing guidelines at https://github.com/ROCm/ROCm/blob/develop/CONTRIBUTING.md#pull-requests. Co-authored-by: Haoyu Zhang <[email protected]> (cherry picked from commit 2fea146)
There was a problem hiding this comment.
Pull request overview
This PR relands ROCm support for CUDA caching allocator “expandable segments” by switching the underlying VMM/IPC plumbing to HIP APIs and updating tests and Python bindings to accommodate ROCm behavior.
Changes:
- Add a new internal binding (
torch._C._accelerator_getAllocatorSettings) and mark it trace-safe for Dynamo. - Enable/adjust expandable-segment allocator logic for ROCm in
CUDACachingAllocator.cpp(HIP VMM APIs, handle type differences, Windows guards). - Update CUDA/Distributed tests to run (or selectively skip) expandable-segments coverage on ROCm.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| torch/csrc/DeviceAccelerator.cpp | Exposes allocator settings getter to Python via _accelerator_getAllocatorSettings. |
| torch/_dynamo/trace_rules.py | Allows Dynamo tracing to treat the new _accelerator_getAllocatorSettings symbol as safe. |
| torch/_C/init.pyi.in | Adds type stub for _accelerator_getAllocatorSettings() -> str. |
| test/test_cuda_expandable_segments.py | Removes ROCm exclusion so expandable-segments test runner can execute on ROCm. |
| test/test_cuda.py | Adds ROCm skip for allocator fuzz when expandable segments are enabled; enables mempool expandable test on ROCm. |
| test/distributed/test_cupy_as_tensor.py | Adds ROCm multiprocess skip decorator for expandable-segments IPC-related failure mode. |
| c10/cuda/CUDACachingAllocator.cpp | Implements ROCm HIP VMM API usage + guards for handle-type differences and Windows IPC limitations. |
| c10/cuda/CUDAAllocatorConfig.h | Treats ROCm as supported for expandable_segments() even without CUDA driver API support. |
| #else | ||
| auto status = | ||
| DriverAPI::get()->cuMemCreate_(&handle, segment_size_, &prop, 0); | ||
| #endif | ||
| if (status != CUDA_SUCCESS) { | ||
| if (status == CUDA_ERROR_OUT_OF_MEMORY) { | ||
| #ifdef USE_ROCM | ||
| // hipMemCreate above returned hipErrorOutOfMemory and treated it | ||
| // like a sticky runtime error. Which means we need to clear it. | ||
| // Unlike the corresponding CUDA Driver API. | ||
| (void)hipGetLastError(); | ||
| #endif | ||
| for (auto j : c10::irange(begin, i)) { | ||
| // NOLINTNEXTLINE(bugprone-unchecked-optional-access) | ||
| auto h = handles_.at(j).value(); | ||
| handles_.at(j) = std::nullopt; | ||
| #ifdef USE_ROCM | ||
| C10_CUDA_CHECK(hipMemRelease(h.handle)); | ||
| #else | ||
| C10_CUDA_DRIVER_CHECK(DriverAPI::get()->cuMemRelease_(h.handle)); | ||
| #endif | ||
| } | ||
| trimHandles(); | ||
| return rangeFromHandles(begin, begin); | ||
| #ifdef USE_ROCM | ||
| } else { | ||
| C10_CUDA_CHECK(status); | ||
| } | ||
| #else |
There was a problem hiding this comment.
In the ROCm path, status comes from hipMemCreate (HIP error type), but it’s being compared against CUDA driver error constants (CUDA_SUCCESS, CUDA_ERROR_OUT_OF_MEMORY). This relies on numeric equivalence between unrelated enums and can mis-handle errors on some HIP versions. Prefer checking status against the HIP constants (e.g., hipSuccess / hipErrorOutOfMemory) under #ifdef USE_ROCM and keep the CUDA comparisons in the CUDA branch.
| #else | |
| auto status = | |
| DriverAPI::get()->cuMemCreate_(&handle, segment_size_, &prop, 0); | |
| #endif | |
| if (status != CUDA_SUCCESS) { | |
| if (status == CUDA_ERROR_OUT_OF_MEMORY) { | |
| #ifdef USE_ROCM | |
| // hipMemCreate above returned hipErrorOutOfMemory and treated it | |
| // like a sticky runtime error. Which means we need to clear it. | |
| // Unlike the corresponding CUDA Driver API. | |
| (void)hipGetLastError(); | |
| #endif | |
| for (auto j : c10::irange(begin, i)) { | |
| // NOLINTNEXTLINE(bugprone-unchecked-optional-access) | |
| auto h = handles_.at(j).value(); | |
| handles_.at(j) = std::nullopt; | |
| #ifdef USE_ROCM | |
| C10_CUDA_CHECK(hipMemRelease(h.handle)); | |
| #else | |
| C10_CUDA_DRIVER_CHECK(DriverAPI::get()->cuMemRelease_(h.handle)); | |
| #endif | |
| } | |
| trimHandles(); | |
| return rangeFromHandles(begin, begin); | |
| #ifdef USE_ROCM | |
| } else { | |
| C10_CUDA_CHECK(status); | |
| } | |
| #else | |
| if (status != hipSuccess) { | |
| if (status == hipErrorOutOfMemory) { | |
| // hipMemCreate above returned hipErrorOutOfMemory and treated it | |
| // like a sticky runtime error. Which means we need to clear it. | |
| // Unlike the corresponding CUDA Driver API. | |
| (void)hipGetLastError(); | |
| for (auto j : c10::irange(begin, i)) { | |
| // NOLINTNEXTLINE(bugprone-unchecked-optional-access) | |
| auto h = handles_.at(j).value(); | |
| handles_.at(j) = std::nullopt; | |
| C10_CUDA_CHECK(hipMemRelease(h.handle)); | |
| } | |
| trimHandles(); | |
| return rangeFromHandles(begin, begin); | |
| } else { | |
| C10_CUDA_CHECK(status); | |
| } | |
| #else | |
| auto status = | |
| DriverAPI::get()->cuMemCreate_(&handle, segment_size_, &prop, 0); | |
| if (status != CUDA_SUCCESS) { | |
| if (status == CUDA_ERROR_OUT_OF_MEMORY) { | |
| for (auto j : c10::irange(begin, i)) { | |
| // NOLINTNEXTLINE(bugprone-unchecked-optional-access) | |
| auto h = handles_.at(j).value(); | |
| handles_.at(j) = std::nullopt; | |
| C10_CUDA_DRIVER_CHECK(DriverAPI::get()->cuMemRelease_(h.handle)); | |
| } | |
| trimHandles(); | |
| return rangeFromHandles(begin, begin); |
| #endif | ||
| if (h.shareable_handle) { | ||
| #ifndef _WIN32 | ||
| close(std::get<int>(*h.shareable_handle)); |
There was a problem hiding this comment.
h.shareable_handle is a variant<int, CUmemFabricHandle>, but this code unconditionally does std::get<int> and closes it. If the shareable handle is a fabric handle, this will throw/abort (bad_variant_access) at runtime. Only close the handle when the variant actually holds an int (FD), and skip closing for fabric handles.
| close(std::get<int>(*h.shareable_handle)); | |
| if (std::holds_alternative<int>(*h.shareable_handle)) { | |
| close(std::get<int>(*h.shareable_handle)); | |
| } |
| def device(self) -> torch.device: | ||
| return torch.device(device_type, self.rank) | ||
|
|
||
| @skip_if_rocm_multiprocess # RuntimeError: pidfd_getfd Operation not permitted" |
There was a problem hiding this comment.
The trailing quote in this comment looks accidental and makes the message confusing. Please remove the extra " at the end (or move it into the exception text if intended).
| @skip_if_rocm_multiprocess # RuntimeError: pidfd_getfd Operation not permitted" | |
| @skip_if_rocm_multiprocess # RuntimeError: pidfd_getfd Operation not permitted |
|
Jenkins build for 3b12ef3a6f4126c7553be18c3abf744e890c3c94 commit is in progress |
pruthvistony
left a comment
There was a problem hiding this comment.
Lets do sanity test and then merge this PR.
Summary:
Original pull request: pytorch#173330 Fixes pytorch#168737. Fixes pytorch#168736.
The original diff enabled expandable segments for ROCm by adding
#ifdef USE_ROCMguards throughout CUDACachingAllocator.cpp to use HIP APIs (hipMemAddressReserve, hipMemCreate, hipMemMap, etc.) instead of CUDA driver APIs when building for ROCm.Root cause: In HIP/ROCm 6.2.1, the field name for memory allocation properties is
requestedHandleType(singular), notrequestedHandleTypes(plural) as in CUDA. Additionally,hipMemHandleTypeFabricdoes not exist in HIP, so theCU_MEM_HANDLE_TYPE_FABRICassignment must be skipped on ROCm.Fix applied on top of the original diff (from D96652342):
prop.requestedHandleType = hipMemHandleTypePosixFileDescriptorunder#ifdef USE_ROCM(singular field name, HIP constant)prop.requestedHandleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTORfor CUDA (plural field name, CUDA constant)CU_MEM_HANDLE_TYPE_FABRICassignment entirely on ROCm under#ifndef USE_ROCM, ashipMemHandleTypeFabricdoes not exist in HIPCo-authored-by: Prachi Gupta [email protected]
Co-authored-by: Jeff Daily [email protected]
Co-authored-by: moonshadow-25 [email protected]
Co-authored-by: Vighanesh Sharma [email protected]
Test Plan:
https://www.internalfb.com/sandcastle/workflow/1049338713192153464
Differential Revision: D97211385
Pull Request resolved: pytorch#177974
Approved by: https://github.com/jeffdaily, https://github.com/echen4096
(cherry picked from commit 5792701)
Motivation
Technical Details
Test Plan
Test Result
Submission Checklist
(cherry picked from commit 2fea146)
Motivation
Technical Details
Test Plan
Test Result
Submission Checklist