From 2a44f965df853c1ed9baed7d74fb4ff358d4d7eb Mon Sep 17 00:00:00 2001 From: Nara Prasetya Date: Wed, 8 Jul 2026 16:24:56 +0000 Subject: [PATCH 1/3] [ROCm] derive the hipcub compatible cccl version from 'HIPCUB_CCCL_VERSION' (#188072) Recreated https://github.com/pytorch/pytorch/pull/184552 from personal fork. hipCUB is targeting to be compatible with CCCL 3.0 (https://github.com/ROCm/rocm-libraries/pull/4079). This requires updating some of the CUB and hipCUB compatibility glue. To help this transition hipCUB will be exposing `HIPCUB_CCCL_VERSION` in one of the following ROCm releases (>=7.14) (https://github.com/ROCm/rocm-libraries/pull/7632), which will be bumped once hipCUB reaches parity with CCCL 3.0 and beyond. This PR is created in anticipation for this future changes. - Introduce dependency on [`libhipcxx`](https://github.com/ROCm/libhipcxx). - Guard `FpLimits` for `c10::BFloat16` backport to pre CCCL 3.x. Numeric limits are handled by libhipcxx. - Extend `CUB_VERSION` derivation for hipCUB. Keep old fallback for backwards compatibility. Only enable CUB V3 API once hipCUB reaches parity with CCCL 3.x. Pull Request resolved: https://github.com/pytorch/pytorch/pull/188072 Approved by: https://github.com/jeffdaily (cherry picked from commit c8e24736809f13a950d6bb825e395b1f7eb81cc8) --- aten/src/ATen/cuda/cub.cuh | 8 ++++++++ aten/src/ATen/cuda/cub_definitions.cuh | 28 ++++++++++++++++++++++---- cmake/public/LoadHIP.cmake | 3 +++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/aten/src/ATen/cuda/cub.cuh b/aten/src/ATen/cuda/cub.cuh index 7828c3917fc46..5e85d3eced0d4 100644 --- a/aten/src/ATen/cuda/cub.cuh +++ b/aten/src/ATen/cuda/cub.cuh @@ -70,11 +70,19 @@ #define ATEN_CUB_MAXIMUM() NO_ROCM(at_cuda_detail)ROCM_HIPCUB(::cub)::Max() #endif +<<<<<<< HEAD #if (!defined(USE_ROCM) && !CUB_SUPPORTS_NV_BFLOAT16()) || defined(USE_ROCM) #if !defined(USE_ROCM) namespace at_cuda_detail { #endif +======= +// Pre ROCm 8.0 we require backporting of FpLimits specialization +// for 'c10::BFloat16'. However, ROCm 8.0 lifts the compatible +// CUB version to 3.x which uses libhipcxx and derives numerical +// limits from there. +#if defined(USE_ROCM) && CUB_VERSION < 300000 +>>>>>>> c8e24736809 ([ROCm] derive the hipcub compatible cccl version from 'HIPCUB_CCCL_VERSION' (#188072)) // backport https://github.com/NVIDIA/cub/pull/306 for c10::BFloat16 diff --git a/aten/src/ATen/cuda/cub_definitions.cuh b/aten/src/ATen/cuda/cub_definitions.cuh index 0d76ae6e8dcfc..650baae320ebd 100644 --- a/aten/src/ATen/cuda/cub_definitions.cuh +++ b/aten/src/ATen/cuda/cub_definitions.cuh @@ -1,14 +1,20 @@ #pragma once #if !defined(USE_ROCM) -#include // for CUDA_VERSION -#endif - -#if !defined(USE_ROCM) +#include // for CUDA_VERSION #include #else +// Check if we can find HIPCUB_CCCL_VERSION. +#include +// Older versions of hipCUB do not support the CUB V3 API. +#if defined(HIPCUB_CCCL_VERSION) && HIPCUB_CCCL_VERSION >= 300000 +#define CUB_VERSION HIPCUB_CCCL_VERSION +#else +// If we cannot find a compatible CCCL version, fallback to +// a very old version. #define CUB_VERSION 200001 #endif +#endif // cub sort support for __nv_bfloat16 is added to cub 1.13 in: // https://github.com/NVIDIA/cub/pull/306 @@ -37,9 +43,23 @@ #endif // There were many bc-breaking changes in major version release of CCCL v3.0.0 +<<<<<<< HEAD // Please see https://nvidia.github.io/cccl/cccl/3.0_migration_guide.html #if CUB_VERSION >= 200800 #define CUB_V3_PLUS() true #else +======= +// Please see https://github.com/NVIDIA/cccl/blob/main/docs/cccl/3.0_migration_guide.rst +#if CUB_VERSION >= 300400 +#define CUB_V3_4_PLUS() true +#define CUB_V3_PLUS() false +#elif CUB_VERSION >= 200800 +// CCCL 2.8 introduced new CUB v3 API. +#define CUB_V3_4_PLUS() false +#define CUB_V3_PLUS() true +#else +// Pre CCCL 2.8. +#define CUB_V3_4_PLUS() false +>>>>>>> c8e24736809 ([ROCm] derive the hipcub compatible cccl version from 'HIPCUB_CCCL_VERSION' (#188072)) #define CUB_V3_PLUS() false #endif diff --git a/cmake/public/LoadHIP.cmake b/cmake/public/LoadHIP.cmake index 538a2fe1982c5..c4030328940c1 100644 --- a/cmake/public/LoadHIP.cmake +++ b/cmake/public/LoadHIP.cmake @@ -189,6 +189,9 @@ if(HIP_FOUND) # Optional components. find_package_and_print_version(hipsparselt) # Will be required when ready. + # ROCm 8.0 and later requires libhipcxx! This should be marked as + # 'REQUIRED' once minimal ROCm version is bumped to 8.0 or later. + find_package_and_print_version(libhipcxx) list(REMOVE_DUPLICATES ROCM_INCLUDE_DIRS) From 69205f3666daa724b4fe5f295d2b770a24aa6886 Mon Sep 17 00:00:00 2001 From: Stanley Tsang Date: Thu, 16 Jul 2026 14:51:50 -0600 Subject: [PATCH 2/3] resolve merge conflict (#24) --- aten/src/ATen/cuda/cub_definitions.cuh | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/aten/src/ATen/cuda/cub_definitions.cuh b/aten/src/ATen/cuda/cub_definitions.cuh index 650baae320ebd..b68fee5114e96 100644 --- a/aten/src/ATen/cuda/cub_definitions.cuh +++ b/aten/src/ATen/cuda/cub_definitions.cuh @@ -43,23 +43,9 @@ #endif // There were many bc-breaking changes in major version release of CCCL v3.0.0 -<<<<<<< HEAD // Please see https://nvidia.github.io/cccl/cccl/3.0_migration_guide.html #if CUB_VERSION >= 200800 #define CUB_V3_PLUS() true #else -======= -// Please see https://github.com/NVIDIA/cccl/blob/main/docs/cccl/3.0_migration_guide.rst -#if CUB_VERSION >= 300400 -#define CUB_V3_4_PLUS() true -#define CUB_V3_PLUS() false -#elif CUB_VERSION >= 200800 -// CCCL 2.8 introduced new CUB v3 API. -#define CUB_V3_4_PLUS() false -#define CUB_V3_PLUS() true -#else -// Pre CCCL 2.8. -#define CUB_V3_4_PLUS() false ->>>>>>> c8e24736809 ([ROCm] derive the hipcub compatible cccl version from 'HIPCUB_CCCL_VERSION' (#188072)) #define CUB_V3_PLUS() false #endif From 842cb552526053e9fffb15cb185788e48ca19f4e Mon Sep 17 00:00:00 2001 From: Jithun Nair <37884920+jithunnair-amd@users.noreply.github.com> Date: Thu, 16 Jul 2026 15:58:31 -0500 Subject: [PATCH 3/3] Resolve conflicts in cub.cuh --- aten/src/ATen/cuda/cub.cuh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/aten/src/ATen/cuda/cub.cuh b/aten/src/ATen/cuda/cub.cuh index 5e85d3eced0d4..a332e3e63e24f 100644 --- a/aten/src/ATen/cuda/cub.cuh +++ b/aten/src/ATen/cuda/cub.cuh @@ -70,19 +70,16 @@ #define ATEN_CUB_MAXIMUM() NO_ROCM(at_cuda_detail)ROCM_HIPCUB(::cub)::Max() #endif -<<<<<<< HEAD -#if (!defined(USE_ROCM) && !CUB_SUPPORTS_NV_BFLOAT16()) || defined(USE_ROCM) +#if (!defined(USE_ROCM) && !CUB_SUPPORTS_NV_BFLOAT16()) || (defined(USE_ROCM) && CUB_VERSION < 300000) #if !defined(USE_ROCM) namespace at_cuda_detail { #endif -======= + // Pre ROCm 8.0 we require backporting of FpLimits specialization // for 'c10::BFloat16'. However, ROCm 8.0 lifts the compatible // CUB version to 3.x which uses libhipcxx and derives numerical // limits from there. -#if defined(USE_ROCM) && CUB_VERSION < 300000 ->>>>>>> c8e24736809 ([ROCm] derive the hipcub compatible cccl version from 'HIPCUB_CCCL_VERSION' (#188072)) // backport https://github.com/NVIDIA/cub/pull/306 for c10::BFloat16