Repository navigation
[ROCm] Facilitate shfl_xor usage in AMD platform #3664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
yiakwy-xpu-ml-framework-team
wants to merge
5
commits into
sgl-project:use__shfl_xor_sync_style_reduce
from
yiakwy-xpu-ml-framework-team:enable_shfl_xor_sync_style_reduce_on_amd
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
595d180
facilitate compiling in AMD platform
yiakwy-xpu-ml-framework-team d996df5
remove redundant static_cast<float>
yiakwy-xpu-ml-framework-team e053898
add castToFloat symbol for CUDA platform
yiakwy-xpu-ml-framework-team 1d75eaf
remove guard
yiakwy-xpu-ml-framework-team f5514fc
update setup_rocm.py
yiakwy-xpu-ml-framework-team File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #pragma once | ||
|
|
||
| #if defined(__HIP_PLATFORM_AMD__) | ||
|
|
||
| #include <hip/hip_bf16.h> | ||
| #include <hip/hip_common.h> | ||
| #include <hip/hip_fp16.h> | ||
| #include <hip/hip_fp8.h> | ||
|
|
||
| // this is adoption from flashinfer-rocm [PR#491](https://github.com/flashinfer-ai/flashinfer/pull/491) | ||
|
|
||
| namespace amdgpu { | ||
|
|
||
| template <typename T> | ||
| __forceinline__ __device__ T shfl_xor_sync(unsigned mask, T var, int laneMask, int width = warpSize); | ||
|
|
||
| template <typename srcDtype, typename destDtype> | ||
| __forceinline__ __device__ destDtype cast(srcDtype val); | ||
|
|
||
| // specialization | ||
| template <> | ||
| __forceinline__ __device__ float shfl_xor_sync(unsigned mask, float var, int laneMask, int width) { | ||
| return __shfl_xor(var, laneMask, width); | ||
| } | ||
|
|
||
| template <> | ||
| __forceinline__ __device__ int shfl_xor_sync(unsigned mask, int var, int laneMask, int width) { | ||
| return __shfl_xor(var, laneMask, width); | ||
| } | ||
|
|
||
| template <> | ||
| __forceinline__ __device__ float cast(float val) { | ||
| return val; | ||
| } | ||
|
|
||
| template <> | ||
| __forceinline__ __device__ float cast(__half val) { | ||
| return __half2float(val); | ||
| } | ||
|
|
||
| template <> | ||
| __forceinline__ __device__ float cast(__hip_bfloat16 val) { | ||
| return __bfloat162float(val); | ||
| } | ||
|
|
||
| } // namespace amdgpu | ||
|
|
||
| template <typename T> | ||
| __forceinline__ __device__ float __shfl_xor_sync(unsigned mask, T var, int laneMask, int width = warpSize) { | ||
| return amdgpu::shfl_xor_sync(var, laneMask, width); | ||
| } | ||
|
|
||
| template <typename srcDtype> | ||
| __device__ __forceinline__ float castToFloat(srcDtype val) { | ||
| return amdgpu::cast<srcDtype, float>(val); | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,21 @@ limitations under the License. | |
| #include <cuda_runtime.h> | ||
| #ifndef USE_ROCM | ||
| #include <pytorch_extension_utils.h> | ||
| #else | ||
|
|
||
| // adding dispatch utils adpated from flashinfer | ||
| #define _DISPATCH_CASE_F16(c_type, ...) \ | ||
| case at::ScalarType::Half: { \ | ||
| using c_type = __half; \ | ||
| return __VA_ARGS__(); \ | ||
| } | ||
|
|
||
| #define _DISPATCH_CASE_BF16(c_type, ...) \ | ||
| case at::ScalarType::BFloat16: { \ | ||
| using c_type = __hip_bfloat16; \ | ||
| return __VA_ARGS__(); \ | ||
| } | ||
|
|
||
| #endif | ||
| #include <torch/extension.h> | ||
|
|
||
|
|
@@ -55,6 +70,8 @@ struct cuda_error : public std::runtime_error { | |
| CHECK_IS_CUDA(x); \ | ||
| CHECK_IS_CONTIGUOUS(x) | ||
|
|
||
| #define CHECK_CUDA_EQ(x, y) TORCH_CHECK(x == y, #x " must be" #y) | ||
|
|
||
| inline int getSMVersion() { | ||
| int device{-1}; | ||
| CHECK_CUDA_SUCCESS(cudaGetDevice(&device)); | ||
|
|
@@ -65,7 +82,18 @@ inline int getSMVersion() { | |
| return sm_major * 10 + sm_minor; | ||
| } | ||
|
|
||
| #ifndef USE_ROCM | ||
| #ifdef USE_ROCM | ||
|
yiakwy-xpu-ml-framework-team marked this conversation as resolved.
Resolved
|
||
|
|
||
| #define CHECK_INPUT(x) \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aten has already defined |
||
| \ | ||
| CHECK_CUDA_INPUT(x); | ||
|
|
||
| #define CHECK_EQ(x, y) \ | ||
| \ | ||
| CHECK_CUDA_EQ(x, y); | ||
|
|
||
| #endif | ||
|
|
||
| #define DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FLOAT_FP16(pytorch_dtype, c_type, ...) \ | ||
| [&]() -> bool { \ | ||
| switch (pytorch_dtype) { \ | ||
|
|
@@ -82,7 +110,6 @@ inline int getSMVersion() { | |
| return false; \ | ||
| } \ | ||
| }() | ||
| #endif | ||
|
|
||
| #define DISPATCH_CASE_INTEGRAL_TYPES(...) \ | ||
| AT_DISPATCH_CASE(at::ScalarType::Byte, __VA_ARGS__) \ | ||
|
|
@@ -95,3 +122,15 @@ inline int getSMVersion() { | |
| AT_DISPATCH_SWITCH(TYPE, NAME, DISPATCH_CASE_INTEGRAL_TYPES(__VA_ARGS__)) | ||
|
|
||
| #define CEILDIV(x, y) (((x) + (y)-1) / (y)) | ||
|
|
||
| #ifndef USE_ROCM | ||
| #define WARP_SIZE 32 | ||
| #else | ||
| #define WARP_SIZE warpSize // 64 | ||
|
yiakwy-xpu-ml-framework-team marked this conversation as resolved.
Resolved
|
||
| #endif | ||
|
|
||
| #if defined(__HIP_PLATFORM_AMD__) | ||
|
|
||
| #include "hip_math_def.h" | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.