Skip to content

[ROCm] Reland: Enable expandable segments (#173330) (#177974) (#3106) - #3179

Merged
pragupta merged 1 commit into
ROCm:release/2.10from
pragupta:pg-expandable-segments-2.10
Apr 25, 2026
Merged

pragupta merged 1 commit into
ROCm:release/2.10from
pragupta:pg-expandable-segments-2.10

Conversation

@pragupta

Copy link
Copy Markdown
Collaborator

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

Technical Details

Test Plan

Test Result

Submission Checklist

(cherry picked from commit 2fea146)

Motivation

Technical Details

Test Plan

Test Result

Submission Checklist

…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)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment on lines +479 to +507
#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

Copilot AI Apr 24, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
#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);

Copilot uses AI. Check for mistakes.
#endif
if (h.shareable_handle) {
#ifndef _WIN32
close(std::get<int>(*h.shareable_handle));

Copilot AI Apr 24, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
close(std::get<int>(*h.shareable_handle));
if (std::holds_alternative<int>(*h.shareable_handle)) {
close(std::get<int>(*h.shareable_handle));
}

Copilot uses AI. Check for mistakes.
def device(self) -> torch.device:
return torch.device(device_type, self.rank)

@skip_if_rocm_multiprocess # RuntimeError: pidfd_getfd Operation not permitted"

Copilot AI Apr 24, 2026

Copy link

Choose a reason for hiding this comment

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

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).

Suggested change
@skip_if_rocm_multiprocess # RuntimeError: pidfd_getfd Operation not permitted"
@skip_if_rocm_multiprocess # RuntimeError: pidfd_getfd Operation not permitted

Copilot uses AI. Check for mistakes.
@rocm-repo-management-api

Copy link
Copy Markdown

Jenkins build for 3b12ef3a6f4126c7553be18c3abf744e890c3c94 commit is in progress
Links: Pipeline Overview / Build artifacts

@pruthvistony pruthvistony left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lets do sanity test and then merge this PR.

@pragupta
pragupta merged commit 92fa102 into ROCm:release/2.10 Apr 25, 2026
7 of 8 checks passed
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.

3 participants