11/* ************************************************************************
2+ * This file was modified for portability to AMDGPU
3+ * Copyright (c) 2026, Advanced Micro Devices, Inc. All rights reserved.
24 * Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
35 *
46 * See LICENSE for license information.
1719#include " common/util/cuda_runtime.h"
1820#include " common/util/ptx.cuh"
1921#include " common/utils.cuh"
22+ #ifdef __HIP_PLATFORM_AMD__
23+ #include " common/util/rocm_device_utils.cuh"
24+ #endif
2025
2126#if (!defined(__CUDA_MINIMUM_ARCH__) && __CUDA_ARCH__ >= 900) || \
2227 (defined (__CUDA_MINIMUM_ARCH__) && __CUDA_MINIMUM_ARCH__ >= 900 )
@@ -28,7 +33,11 @@ namespace {
2833
2934// const values configuration
3035
36+ #if defined(__HIP_PLATFORM_AMD__) && !defined(__gfx1250__)
37+ constexpr size_t kThreadsPerWarp = 64 ;
38+ #else
3139constexpr size_t kThreadsPerWarp = 32 ;
40+ #endif
3241#ifdef TMA_HW_SUPPORTED
3342constexpr size_t BLOCK_TILE_DIM = 128 ;
3443constexpr size_t WARP_TILE_DIM_X = 32 ;
@@ -40,8 +49,12 @@ constexpr size_t BLOCK_TILE_DIM = 128;
4049constexpr size_t WARP_TILE_DIM_X = 64 ;
4150constexpr size_t WARP_TILE_DIM_Y = 32 ;
4251constexpr size_t THREAD_TILE_DIM_X = 8 ;
52+ #if defined(__HIP_PLATFORM_AMD__) && !defined(__gfx1250__)
53+ constexpr size_t THREAD_TILE_DIM_Y = 4 ;
54+ #else
4355constexpr size_t THREAD_TILE_DIM_Y = 8 ;
4456#endif
57+ #endif
4558
4659#ifdef TMA_HW_SUPPORTED
4760constexpr size_t NUM_BYTES_PER_BANK = 4 ;
@@ -62,6 +75,7 @@ constexpr size_t NUM_THREADS_Y_IN_WARP = kThreadsPerWarp / NUM_THREADS_X_IN_WARP
6275
6376#define MIN (a, b ) (a < b ? a : b)
6477
78+ #ifndef __HIP_PLATFORM_AMD__
6579template <bool kReturnTranspose , typename CType, typename IType, typename OType>
6680__global__ void __launch_bounds__ (THREADS_PER_BLOCK )
6781 block_scaled_cast_transpose_kernel(const IType* const input, OType* const output_c,
@@ -247,6 +261,7 @@ __global__ void __launch_bounds__(THREADS_PER_BLOCK)
247261#endif
248262 }
249263}
264+ #endif // __HIP_PLATFORM_AMD__
250265
251266template <bool kReturnTranspose , typename CType, typename IType, typename OType>
252267__global__ void __launch_bounds__ (THREADS_PER_BLOCK ) block_scaled_cast_transpose_kernel_notaligned(
@@ -357,10 +372,14 @@ __global__ void __launch_bounds__(THREADS_PER_BLOCK) block_scaled_cast_transpose
357372 }
358373 }
359374 // Reduce amax in the warp (32x32 tile)
375+ #ifdef __HIP_PLATFORM_AMD__
376+ warp_tile_amax = rocm_subwarp_allreduce<kThreadsPerWarp >(amax, rocm_op::max{});
377+ #else
360378 warp_tile_amax = warp_reduce_max<kThreadsPerWarp >(amax);
361379 // broadcast the amax to all threads in a warp from the lane 0
362380 constexpr int lane_zero = 0 ;
363381 warp_tile_amax = __shfl_sync (0xFFFFFFFF , warp_tile_amax, lane_zero);
382+ #endif
364383
365384 // reduce warp_tile_amax across multiple warps in a thread block using shared mem
366385 if (tid_in_warp == 0 ) {
@@ -456,6 +475,7 @@ __global__ void __launch_bounds__(THREADS_PER_BLOCK) block_scaled_cast_transpose
456475 }
457476}
458477
478+ #ifndef __HIP_PLATFORM_AMD__
459479template <typename OutputType>
460480CUtensorMap get_tensor_map (const SimpleTensor& tensor, size_t global_dim_x, size_t global_dim_y) {
461481 CUtensorMapDataType dataType;
@@ -473,6 +493,7 @@ CUtensorMap get_tensor_map(const SimpleTensor& tensor, size_t global_dim_x, size
473493 /* stride_elems=*/ global_dim_x, /* offset_elems=*/ 0 , sizeof (OutputType) * 8 );
474494 return tensor_map_output_trans;
475495}
496+ #endif // __HIP_PLATFORM_AMD__
476497
477498} // namespace
478499} // namespace transformer_engine
@@ -543,6 +564,7 @@ void quantize_transpose_square_blockwise(const SimpleTensor& input, SimpleTensor
543564 return_transpose, kReturnTranspose ,
544565
545566 dim3 grid (num_blocks_x, num_blocks_y, 1 );
567+ #ifndef __HIP_PLATFORM_AMD__
546568 const bool full_tile =
547569 row_length % BLOCK_TILE_DIM == 0 && num_rows % BLOCK_TILE_DIM == 0 ;
548570
@@ -573,6 +595,20 @@ void quantize_transpose_square_blockwise(const SimpleTensor& input, SimpleTensor
573595 scale_stride_x, scale_stride_y, scale_t_stride_x, scale_t_stride_y, epsilon,
574596 pow_2_scale, noop_ptr);
575597 } // full-tile
598+ #else
599+ const size_t threads_per_block =
600+ (transformer_engine::cuda::sm_arch (transformer_engine::cuda::current_device ()) == 125 ) ? 256 : 512 ;
601+ block_scaled_cast_transpose_kernel_notaligned<kReturnTranspose , float , InputType,
602+ OutputType>
603+ <<<grid, threads_per_block, 0 , stream>>> (
604+ reinterpret_cast <const InputType*>(input.dptr ),
605+ reinterpret_cast <OutputType*>(output.dptr ),
606+ reinterpret_cast <OutputType*>(output_t .dptr ),
607+ reinterpret_cast <float *>(scale_inv.dptr ),
608+ reinterpret_cast <float *>(scale_inv_t .dptr ), row_length, num_rows,
609+ scale_stride_x, scale_stride_y, scale_t_stride_x, scale_t_stride_y, epsilon,
610+ pow_2_scale, noop_ptr);
611+ #endif // __HIP_PLATFORM_AMD__
576612 ) // return_transpose
577613 ) // OutputType
578614 ) // InputType
0 commit comments