Skip to content

[ROCm] Use native HIP support from CMake - #172775

Closed
GZGavinZhao wants to merge 3 commits into
pytorch:mainfrom
GZGavinZhao:gzgz/cmake-native-hip
Closed

GZGavinZhao wants to merge 3 commits into
pytorch:mainfrom
GZGavinZhao:gzgz/cmake-native-hip

Conversation

@GZGavinZhao

@GZGavinZhao GZGavinZhao commented Jan 19, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for using native HIP CMake support:

  1. It allows easy separation of the C++ compiler and HIP compiler. This is often desirable for distributions because we want to provide both CPU optimized and GPU optimized PyTorch in one package, and oftentimes this means we don't want to use the specific LLVM verison/configuration that ROCm's LLVM uses. Even without this use case, in general I don't think it's good to tie the C++ compiler to the HIP compiler whenever one needs to compile with ROCm support.

    Note that technically it's possible to disentangle C++ and HIP compilers, but it is undocumented and requires some CMake modifications. When using CMake's native HIP support, it's as easy as setting CMAKE_HIP_COMPILER.

  2. It's the officially recommended way to use HIP: https://rocm.docs.amd.com/en/latest/conceptual/cmake-packages.html#using-hip-in-cmake

  3. It's the same method that CUDA uses, so this makes the use of two languages consistent.

I haven't been able to test it on Windows yet. Tested on Windows too (this is not part of the CI).

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang

@pytorch-bot

pytorch-bot Bot commented Jan 19, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/172775

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 1 Unrelated Failure

As of commit cbbb86f with merge base ef0353f (image):

NEW FAILURE - The following job has failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added the module: rocm AMD GPU support for Pytorch label Jan 19, 2026
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Jan 19, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jan 19, 2026 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@pytorch-bot pytorch-bot Bot removed the oncall: jit Add this issue/PR to JIT oncall triage queue label Jan 19, 2026
@pytorch-bot

pytorch-bot Bot commented Jan 19, 2026

Copy link
Copy Markdown

The label oncall: jit is only applicable to issues and has been removed. Please only use this label on issues.

@GZGavinZhao
GZGavinZhao force-pushed the gzgz/cmake-native-hip branch from 8058b31 to 0fc6d2d Compare January 19, 2026 15:27
@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@m-gallus

Copy link
Copy Markdown
Contributor

Back in the day I intended to create a PR for the HIP language support in PyTorch, however it turned out that there is no support for HIP Language in CMake on Windows. Has that been fixed recently?

@jeffdaily

Copy link
Copy Markdown
Collaborator

Back in the day I intended to create a PR for the HIP language support in PyTorch, however it turned out that there is no support for HIP Language in CMake on Windows. Has that been fixed recently?

@ScottTodd can you comment here?

Comment thread cmake/public/LoadHIP.cmake Resolved
@GZGavinZhao
GZGavinZhao force-pushed the gzgz/cmake-native-hip branch 2 times, most recently from 8a29231 to 285f0d9 Compare January 22, 2026 06:26
pytorchmergebot pushed a commit that referenced this pull request Jan 22, 2026
…#172799)

The HasSameArgTypes test references `at::native::modern::detail::has_same_arg_types`, which was defined in `aten/src/ATen/native/cuda/ROCmLoops.cuh`. This file was deleted in c11bd72 (#120101) when ROCm switched to using the hipified version of `CUDALoops.cuh` instead.

My understanding is that this test should be removed since it's a legacy compile-time check for a code path that no longer exists.

Causes failure in #172775 in [this run](https://github.com/pytorch/pytorch/actions/runs/21153824053/job/60835278003?pr=172775).

Pull Request resolved: #172799
Approved by: https://github.com/m-gallus, https://github.com/jeffdaily
@jeffdaily

jeffdaily commented Jan 23, 2026 •

Copy link
Copy Markdown
Collaborator

@GZGavinZhao we have another PR that needs to set enable_language(HIP). For now perhaps you can just protect this statement in CMake with a check for WIN32? It's a bandaid until you can dig into the flags issues.

Aside: would be nice if we had a ROCm Windows build as part of CI.

@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

I'll investigate it once again today and see if I can figure out a way to make enable_language(HIP) compile on Windows.

If this doesn't work, I'll restore the hip_add_* code and figure out a concise way to use CMake's native HIP support for Linux and the original HIP CMake on Windows.

@GZGavinZhao
GZGavinZhao force-pushed the gzgz/cmake-native-hip branch from 285f0d9 to 69f224b Compare January 30, 2026 07:25
@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

Some good progress! The current version went pass enable_language(HIP) and was able to compile until the following error appears complaining about no matching constructor for initialization of const __half coming from rocPRIM. The current branch was just rebased on main so I need to investigate whether that's still a compiler problem or some legitimate programming error.

Details
In file included from C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\aten\src\ATen\hip\cub-RadixSortKeys.hip:4:
In file included from C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\aten\src\ATen/hip\cub.cuh:32:
In file included from C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hipcub/hipcub.hpp:34:
In file included from C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hipcub\backend/rocprim/hipcub.hpp:61:
In file included from C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hipcub\backend/rocprim\device/device_radix_sort.hpp:38:
In file included from C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device/device_radix_sort.hpp:41:
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device\detail/device_radix_sort.hpp(636,13): error: no matching constructor for initialization of 'const __half'
  636 |     const T zero{0};
      |             ^   ~~~
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device\detail/device_radix_sort.hpp(678,16): note: in instantiation of function template specialization 'rocprim::detail::compare_nan_sensitive<__half>' requested here
  678 |         return compare_nan_sensitive<T>(a, b);
      |                ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/block\detail\../../detail/merge_path.hpp(83,13): note: in instantiation of member function 'rocprim::detail::radix_merge_compare<true, false, __half>::operator()' requested here
   83 |         if(!compare_function(input_b, input_a))
      |             ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device/device_merge_sort.hpp(201,46): note: in instantiation of function template specialization 'rocprim::detail::merge_path<__half *, __half *, unsigned int, rocprim::detail::radix_merge_compare<true, false, __half>>' requested here
  201 |                         = ::rocprim::detail::merge_path(keys_input_ + keys1_beg,
      |                                              ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device/config_types.hpp(580,32): note: in instantiation of function template specialization 'rocprim::detail::trampoline_kernel<rocprim::default_config, rocprim::detail::merge_sort_block_merge_config_selector<__half, rocprim::empty_type>, (lambda at C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim\device\device_merge_sort.hpp:161:70), rocprim::detail::comp_target<rocprim::detail::gen::rdna3, rocprim::detail::target_arch::gfx1100, rocprim::detail::gpu::rx7900, rocprim::detail::rep::amdgcn>, rocprim::detail::merge_mergepath_partition_config_static_selector>' requested here
  580 |                 tuned_kernel = trampoline_kernel<Config,
      |                                ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device/config_types.hpp(598,30): note: in instantiation of function template specialization 'rocprim::detail::make_launch_plan<rocprim::default_config, rocprim::detail::merge_sort_block_merge_config_selector<__half, rocprim::empty_type>, rocprim::detail::merge_mergepath_partition_config_static_selector, (lambda at C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim\device\device_merge_sort.hpp:161:70)>' requested here
  598 |     const auto launch_plan = make_launch_plan<Config, ConfigSelector, LaunchSelector>(t, kernel);
      |                              ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device/device_merge_sort.hpp(213,21): note: (skipping 5 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
  213 |                     execute_launch_plan<Config,
      |                     ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device/device_radix_sort.hpp(667,16): note: in instantiation of function template specialization 'rocprim::detail::radix_sort_merge_impl<rocprim::default_config, true, const __half *, __half *, rocprim::empty_type *, rocprim::empty_type *, rocprim::identity_decomposer>' requested here
  667 |         return radix_sort_merge_impl<merge_sort_config, Descending>(temporary_storage,
      |                ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\rocprim/device/device_radix_sort.hpp(1609,20): note: in instantiation of function template specialization 'rocprim::detail::radix_sort_impl<rocprim::default_config, true, const __half *, __half *, rocprim::empty_type *, rocprim::empty_type *, long long, rocprim::identity_decomposer>' requested here
 1609 |     return detail::radix_sort_impl<Config, true>(temporary_storage,
      |                    ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hipcub\backend/rocprim\device/device_radix_sort.hpp(645,27): note: in instantiation of function template specialization 'rocprim::radix_sort_keys_desc<rocprim::default_config, const __half *, __half *, long long, __half>' requested here
  645 |         return ::rocprim::radix_sort_keys_desc(d_temp_storage,
      |                           ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\aten\src\ATen\hip\cub-RadixSortKeys.hip(26,59): note: in instantiation of function template specialization 'hipcub::DeviceRadixSort::SortKeysDescending<__half, long long>' requested here
   26 |         NO_ROCM(at_cuda_detail)::hipcub::DeviceRadixSort::SortKeysDescending,
      |                                                           ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\aten\src\ATen/hip\cub.cuh(46,17): note: expanded from macro 'CUB_WRAPPER'
   46 |   AT_CUDA_CHECK(func(nullptr, temp_storage_bytes, __VA_ARGS__));          \
      |                 ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\aten\src\ATen/hip\Exceptions.h(176,43): note: expanded from macro 'AT_CUDA_CHECK'
  176 | #define AT_CUDA_CHECK(EXPR) C10_HIP_CHECK(EXPR)
      |                                           ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\c10/hip/HIPException.h(30,30): note: expanded from macro 'C10_HIP_CHECK'
   30 |     const hipError_t __err = EXPR;                                 \
      |                              ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\aten\src\ATen\hip\cub-RadixSortKeys.hip(54,51): note: in instantiation of function template specialization 'at::cuda::cub::radix_sort_keys<c10::Half>' requested here
   54 | AT_FORALL_SCALAR_TYPES_AND3(Bool, BFloat16, Half, AT_INSTATIATE_CUB_TEMPLATES)
      |                                                   ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\torch/headeronly/core/ScalarType.h(208,3): note: expanded from macro 'AT_FORALL_SCALAR_TYPES_AND3'
  208 |   _(c10::impl::ScalarTypeToCPPTypeT<c10::ScalarType::SCALARTYPE3>, SCALARTYPE3)
      |   ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\pytorch\aten\src\ATen\hip\cub-RadixSortKeys.hip(46,17): note: expanded from macro 'AT_INSTATIATE_CUB_TEMPLATES'
   46 |   template void radix_sort_keys(                          \
      |                 ^
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hip/amd_detail/amd_hip_fp16.h(102,29): note: candidate constructor not viable: no known conversion from 'int' to 'const __half_raw' for 1st argument
  102 |   __HOST_DEVICE__ constexpr __half(const __half_raw& x) : __x{x.x} {}
      |                             ^      ~~~~~~~~~~~~~~~~~~~
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hip/amd_detail/amd_hip_fp16.h(110,3): note: candidate constructor not viable: no known conversion from 'int' to 'const __half' for 1st argument
  110 |   __half(const __half&) = default;
      |   ^      ~~~~~~~~~~~~~
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hip/amd_detail/amd_hip_fp16.h(112,3): note: candidate constructor not viable: no known conversion from 'int' to '__half' for 1st argument
  112 |   __half(__half&&) = default;
      |   ^      ~~~~~~~~
C:\Users\GavinZhao\CS\TheRock\external-builds\pytorch\.venv\Lib\site-packages\_rocm_sdk_devel\include\hip/amd_detail/amd_hip_fp16.h(101,3): note: candidate constructor not viable: requires 0 arguments, but 1 was provided
  101 |   __half() = default;
      |   ^

@ScottTodd

ScottTodd commented Jan 30, 2026 •

Copy link
Copy Markdown
Contributor

until the following error appears complaining about no matching constructor for initialization of const __half coming from rocPRIM.

I suspect this is ROCm/TheRock#3042. ROCm nightly releases starting after 7.12.0a20260128 should be safe to build with. Releases between 7.12.0a20260122 and 7.12.0a20260127 are affected by that issue.

Edit: I just put that information at the top of that issue.

@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

Thanks! I'll test again tonight with newer releases, hopefully this time everything will just work.

@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

Not ready yet, trying to figure out why it's not building after rebase.

@ScottTodd

Copy link
Copy Markdown
Contributor

Not ready yet, trying to figure out why it's not building after rebase.

You might encounter ROCm/TheRock#3283 (another build break for Windows ROCm we detected last night)

@GZGavinZhao
GZGavinZhao force-pushed the gzgz/cmake-native-hip branch 4 times, most recently from e2411e2 to 345068d Compare February 8, 2026 20:50
@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

Found a solution to ROCm/TheRock#3283. After rebasing changes in this PR on top of that, this PR now builds on Windows as well.

@GZGavinZhao
GZGavinZhao force-pushed the gzgz/cmake-native-hip branch 2 times, most recently from d075acb to c8c4e4f Compare February 11, 2026 02:14
@jeffdaily
jeffdaily self-requested a review February 11, 2026 17:22
message("HIP_VERSION_MINOR: ${HIP_VERSION_MINOR}")
message("TORCH_HIP_VERSION: ${TORCH_HIP_VERSION}")

# Backup variables before searching to workaround FindHIP.cmake overriding HIP variables.

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.

@GZGavinZhao do we absolutely need find_package(HIP), or can we rely solely on enable_language(HIP) instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't need find_package(HIP) and just having enable_language(HIP) is enough. The comment here though is talking about a dependency (rocRAND) using find_package(HIP) which is out of our control.

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.

Understood. I agree then with your approach. You'll need to make backward-compatible changes that can work with dependencies that do the wrong thing today, but will be fixed similarly in the future. CC @ScottTodd

Comment thread c10/cuda/CMakeLists.txt

if(NOT BUILD_LIBTORCHLESS)
torch_cuda_based_add_library(c10_cuda ${C10_CUDA_SRCS} ${C10_CUDA_HEADERS})
add_library(c10_cuda ${C10_CUDA_SRCS} ${C10_CUDA_HEADERS})

@jeffdaily jeffdaily Feb 11, 2026 •

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.

Be careful making changes to code paths that are shared with CUDA.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've checked to make sure no code uses torch_cuda_based_add_library. Can you run the CI again?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also, the rationale for removing torch_cuda_based_add_library is that it should be superceded by add_library since we don't use hip_add_library anymore:

macro(torch_cuda_based_add_library cuda_target)
  if(USE_ROCM)
    hip_add_library(${cuda_target} ${ARGN})
  elseif(USE_CUDA)
    add_library(${cuda_target} ${ARGN})
  else()
  endif()
endmacro()

So replacing torch_cuda_based_add_library with add_library is guaranteed to have no effect on CUDA code paths.

@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

Gentle ping requesting a new CI run <3

@GZGavinZhao
GZGavinZhao force-pushed the gzgz/cmake-native-hip branch from bfd1479 to 7d7c31c Compare February 17, 2026 01:45
@GZGavinZhao

Copy link
Copy Markdown
Contributor Author

New CI run please, some PYTORCH_ROCM_ARCH was not properly escaped causing the previous run's error.

@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

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

Labels

module: rocm AMD GPU support for Pytorch open source Stale topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants