hip: add experimental gfx1012 support and legacy BLAS compatibility

This commit is contained in:
David authored and Niko1221 committed 2026-10-03 23:33:45 +02:00
1 parent 9d638ffed6
commit e3e30eb131
8 files changed
+218 -9

No files matched your search

+41
View File
@@ -1,5 +1,45 @@
#pragma once
#include <hipblas/hipblas.h>
#include <hipblas/hipblas-version.h>
#if hipblasVersionMajor == 0
// Ubuntu's hipBLAS 0.54 has no workspace API or compute-type enum. Use its
// underlying rocBLAS API directly, including ownership of the real handle,
// so the engine's explicit workspace contract is preserved.
#include <rocblas/rocblas.h>
#define CUBLAS_COMPUTE_32F rocblas_datatype_f32_r
#define CUBLAS_DEFAULT_MATH 0
#define CUBLAS_GEMM_DEFAULT rocblas_gemm_algo_standard
#define CUBLAS_OP_N rocblas_operation_none
#define CUBLAS_OP_T rocblas_operation_transpose
#define CUBLAS_STATUS_SUCCESS rocblas_status_success
#define CUDA_R_16BF rocblas_datatype_bf16_r
#define CUDA_R_16F rocblas_datatype_f16_r
#define CUDA_R_32F rocblas_datatype_f32_r
#define cublasCreate rocblas_create_handle
#define cublasDestroy rocblas_destroy_handle
#define cublasHandle_t rocblas_handle
#define cublasSetStream rocblas_set_stream
#define cublasSetWorkspace rocblas_set_workspace
#define cublasStatus_t rocblas_status
inline rocblas_status cublasSetMathMode(rocblas_handle h, int mode) {
// This API has only its default arithmetic; GEMM below explicitly selects
// FP32 accumulation. Reject any request for a different math mode.
if (!h) return rocblas_status_invalid_handle;
return mode == CUBLAS_DEFAULT_MATH ? rocblas_status_success : rocblas_status_invalid_value;
}
inline rocblas_status cublasGemmEx(rocblas_handle h, rocblas_operation trans_a,
rocblas_operation trans_b, int m, int n, int k, const void* alpha,
const void* a, rocblas_datatype a_type, int lda,
const void* b, rocblas_datatype b_type, int ldb, const void* beta,
void* c, rocblas_datatype c_type, int ldc,
rocblas_datatype compute_type, rocblas_gemm_algo algo) {
return rocblas_gemm_ex(h, trans_a, trans_b, m, n, k, alpha,
a, a_type, lda, b, b_type, ldb, beta, c, c_type, ldc,
c, c_type, ldc, compute_type, algo, 0, 0);
}
#else
#define CUBLAS_COMPUTE_32F HIPBLAS_COMPUTE_32F
#define CUBLAS_DEFAULT_MATH HIPBLAS_DEFAULT_MATH
#define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
@@ -17,3 +57,4 @@
#define cublasSetStream hipblasSetStream
#define cublasSetWorkspace hipblasSetWorkspace
#define cublasStatus_t hipblasStatus_t
#endif
+11
View File
@@ -10,6 +10,7 @@
#endif
#endif
#include <hip/hip_runtime.h>
#include <hip/hip_version.h>
// Do not let HIP's legacy macro corrupt libstdc++ attribute names.
#ifdef __noinline__
#undef __noinline__
@@ -39,7 +40,11 @@
#define cudaEventSynchronize hipEventSynchronize
#define cudaEvent_t hipEvent_t
#define cudaFree hipFree
#if HIP_VERSION_MAJOR < 7
#define cudaFreeHost hipHostFree
#else
#define cudaFreeHost hipFreeHost
#endif
#define cudaFuncAttributeMaxDynamicSharedMemorySize hipFuncAttributeMaxDynamicSharedMemorySize
#define cudaGetDevice hipGetDevice
#define cudaGetDeviceCount hipGetDeviceCount
@@ -54,9 +59,15 @@
#define cudaGraphUpload hipGraphUpload
#define cudaGraph_t hipGraph_t
#define cudaHostAlloc hipHostMalloc
#if HIP_VERSION_MAJOR < 7
#define cudaHostAllocDefault hipHostMallocDefault
#define cudaHostAllocMapped hipHostMallocMapped
#define cudaHostAllocPortable hipHostMallocPortable
#else
#define cudaHostAllocDefault hipHostAllocDefault
#define cudaHostAllocMapped hipHostAllocMapped
#define cudaHostAllocPortable hipHostAllocPortable
#endif
#define cudaHostGetDevicePointer hipHostGetDevicePointer
#define cudaHostRegister hipHostRegister
#define cudaHostRegisterMapped hipHostRegisterMapped
+26 -1
View File
@@ -1,6 +1,6 @@
#pragma once
// CUDA device intrinsics used by Strata kernels that HIP does not provide on RDNA2 / RDNA3 / RDNA4 (wave32).
// CUDA device intrinsics used by Strata kernels on supported wave32 HIP targets.
// This header is included only from the HIP cuda_runtime compatibility shim.
#if defined(__HIPCC__)
@@ -25,6 +25,21 @@ __device__ __forceinline__ int dp4a(int a, int b, int c) {
// RDNA2 has no sudot4 (that is gfx11+), but it has the plain signed v_dot4_i32_i8 (dot1-insts): the same
// signed x signed byte products accumulated modulo 2^32, no clamp.
return __builtin_amdgcn_sdot4(a, b, c, false);
#elif defined(__gfx1012__) && !defined(STRATA_GFX1012_PORTABLE_DOT)
// RDNA1 has no native signed dot4. SDWA selects and sign-extends byte
// operands directly, avoiding separate shift/mask/sign-extension work.
// Adapted from pinned llama.cpp ggml-cuda/common.cuh (MIT; see
// third_party/ggml/LICENSE), same modulo-2^32 accumulation contract.
int lo, hi;
asm("v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0 src1_sel:BYTE_0\n"
"v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:BYTE_1\n"
"v_add3_u32 %0, %1, %2, %0\n"
"v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2 src1_sel:BYTE_2\n"
"v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_3 src1_sel:BYTE_3\n"
"v_add3_u32 %0, %1, %2, %0\n"
// c changes before the high-byte reads; it must not alias a or b.
: "+&v"(c), "=&v"(lo), "=&v"(hi) : "v"(a), "v"(b));
return c;
#else
const uint32_t ua = static_cast<uint32_t>(a);
const uint32_t ub = static_cast<uint32_t>(b);
@@ -84,6 +99,15 @@ __device__ __forceinline__ void require_full_wave_mask(uint32_t mask) {
if (mask != 0xffffffffu) __builtin_trap();
}
// Older HIP has no __syncwarp. A wave barrier alone does not order memory.
// Release/acquire fences cover the shared-memory exchange used by attention.
__device__ __forceinline__ void syncwarp(uint32_t mask = 0xffffffffu) {
require_full_wave_mask(mask);
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup");
__builtin_amdgcn_wave_barrier();
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "workgroup");
}
template <typename T>
__device__ __forceinline__ T shfl_xor_sync(uint32_t mask, T value, int lane_mask, int width = 32) {
require_full_wave_mask(mask);
@@ -125,6 +149,7 @@ __device__ __forceinline__ unsigned ballot_sync(uint32_t mask, int predicate) {
#define __shfl_up_sync(...) (::strata::hip_compat::shfl_up_sync(__VA_ARGS__))
#define __shfl_sync(...) (::strata::hip_compat::shfl_sync(__VA_ARGS__))
#define __ballot_sync(mask, predicate) (::strata::hip_compat::ballot_sync((mask), (predicate)))
#define __syncwarp(...) (::strata::hip_compat::syncwarp(__VA_ARGS__))
// AMD's sleep instruction accepts only 0..15; the synchronization loops use it as a
// backoff hint, so use its smallest portable delay independently of CUDA cycle counts.
#define __nanosleep(cycles) __builtin_amdgcn_s_sleep(1)
@@ -0,0 +1,4 @@
#pragma once
// CUDA spelling used by the attention kernels; included only in HIP builds.
#include <hip/hip_math_constants.h>
#define CUDART_INF_F HIP_INF_F