ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / vendors / hip.h
| 1 | #pragma once |
| 2 | |
| 3 | #define HIP_DISABLE_WARP_SYNC_BUILTINS 1 |
| 4 | #include <hip/hip_runtime.h> |
| 5 | #include <hipblas/hipblas.h> |
| 6 | #include <hip/hip_fp16.h> |
| 7 | #include <hip/hip_bf16.h> |
| 8 | |
| 9 | #ifdef GGML_USE_NCCL |
| 10 | #include <rccl/rccl.h> |
| 11 | #endif // GGML_USE_NCCL |
| 12 | |
| 13 | |
| 14 | #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT |
| 15 | #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT |
| 16 | #define CUBLAS_OP_N HIPBLAS_OP_N |
| 17 | #define CUBLAS_OP_T HIPBLAS_OP_T |
| 18 | #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS |
| 19 | #define CUBLAS_TF32_TENSOR_OP_MATH 0 |
| 20 | #define CUDA_R_16F HIPBLAS_R_16F |
| 21 | #define CUDA_R_16BF HIPBLAS_R_16B |
| 22 | #define CUDA_R_32F HIPBLAS_R_32F |
| 23 | #define CUBLAS_SIDE_RIGHT HIPBLAS_SIDE_RIGHT |
| 24 | #define CUBLAS_FILL_MODE_UPPER HIPBLAS_FILL_MODE_UPPER |
| 25 | #define CUBLAS_DIAG_NON_UNIT HIPBLAS_DIAG_NON_UNIT |
| 26 | #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED hipDeviceAttributeVirtualMemoryManagementSupported |
| 27 | #define CU_MEM_ALLOC_GRANULARITY_RECOMMENDED hipMemAllocationGranularityRecommended |
| 28 | #define CU_MEM_ALLOCATION_TYPE_PINNED hipMemAllocationTypePinned |
| 29 | #define CU_MEM_LOCATION_TYPE_DEVICE hipMemLocationTypeDevice |
| 30 | #define CU_MEM_ACCESS_FLAGS_PROT_READWRITE hipMemAccessFlagsProtReadWrite |
| 31 | #define CU_CHECK(fn) {hipError_t err = fn; if(err != hipSuccess) { GGML_ABORT("HipVMM Failure: %s\n", hipGetErrorString(err)); }} |
| 32 | #define __shfl_sync(mask, var, laneMask, width) __shfl(var, laneMask, width) |
| 33 | #define __shfl_up_sync(mask, var, laneMask, width) __shfl_up(var, laneMask, width) |
| 34 | #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width) |
| 35 | #define __all_sync(mask, var) __all(var) |
| 36 | #define __any_sync(mask, var) __any(var) |
| 37 | #define cublasStrsmBatched hipblasStrsmBatched |
| 38 | #define cublasCreate hipblasCreate |
| 39 | #define cublasDestroy hipblasDestroy |
| 40 | #define cublasGemmEx hipblasGemmEx |
| 41 | #define cublasGemmBatchedEx hipblasGemmBatchedEx |
| 42 | #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx |
| 43 | #define cublasHandle_t hipblasHandle_t |
| 44 | #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS |
| 45 | #define cublasSetStream hipblasSetStream |
| 46 | #define cublasSgemm hipblasSgemm |
| 47 | #define cublasSgemmBatched hipblasSgemmBatched |
| 48 | #define cublasSgemmStridedBatched hipblasSgemmStridedBatched |
| 49 | #define cublasStatus_t hipblasStatus_t |
| 50 | #define cublasOperation_t hipblasOperation_t |
| 51 | #define cudaDevAttrCooperativeLaunch hipDeviceAttributeCooperativeLaunch |
| 52 | #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer |
| 53 | #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess |
| 54 | #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess |
| 55 | #define cudaDeviceGetAttribute hipDeviceGetAttribute |
| 56 | #define cudaDeviceGetPCIBusId hipDeviceGetPCIBusId |
| 57 | #define cudaDeviceProp hipDeviceProp_t |
| 58 | #define cudaDeviceSynchronize hipDeviceSynchronize |
| 59 | #define cudaError_t hipError_t |
| 60 | #define cudaErrorMemoryAllocation hipErrorOutOfMemory |
| 61 | #define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled |
| 62 | #define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled |
| 63 | #define cudaEventCreateWithFlags hipEventCreateWithFlags |
| 64 | #define cudaEventDisableTiming hipEventDisableTiming |
| 65 | #define cudaEventRecord hipEventRecord |
| 66 | #define cudaEventSynchronize hipEventSynchronize |
| 67 | #define cudaEvent_t hipEvent_t |
| 68 | #define cudaEventDestroy hipEventDestroy |
| 69 | #define cudaFree hipFree |
| 70 | #define cudaFreeHost hipHostFree |
| 71 | #define cudaGetDevice hipGetDevice |
| 72 | #define cudaGetDeviceCount hipGetDeviceCount |
| 73 | #define cudaGetDeviceProperties hipGetDeviceProperties |
| 74 | #define cudaGetErrorString hipGetErrorString |
| 75 | #define cudaGetLastError hipGetLastError |
| 76 | #define cudaHostAlloc hipHostMalloc |
| 77 | #define cudaHostAllocPortable hipHostMallocPortable |
| 78 | #define cudaHostAllocMapped hipHostMallocMapped |
| 79 | #define cudaHostGetDevicePointer hipHostGetDevicePointer |
| 80 | #define cudaHostRegister hipHostRegister |
| 81 | #define cudaHostRegisterPortable hipHostRegisterPortable |
| 82 | #define cudaHostRegisterReadOnly hipHostRegisterReadOnly |
| 83 | #define cudaHostUnregister hipHostUnregister |
| 84 | #define cudaLaunchCooperativeKernel hipLaunchCooperativeKernel |
| 85 | #define cudaLaunchHostFunc hipLaunchHostFunc |
| 86 | #define cudaMalloc hipMalloc |
| 87 | #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault) |
| 88 | #define cudaMallocManaged hipMallocManaged |
| 89 | #define cudaMemAdvise hipMemAdvise |
| 90 | #define cudaMemcpy hipMemcpy |
| 91 | #define cudaMemcpyAsync hipMemcpyAsync |
| 92 | #define cudaMemcpyPeerAsync hipMemcpyPeerAsync |
| 93 | #define cudaMemcpy2DAsync hipMemcpy2DAsync |
| 94 | #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice |
| 95 | #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost |
| 96 | #define cudaMemcpyHostToDevice hipMemcpyHostToDevice |
| 97 | #define cudaMemcpyKind hipMemcpyKind |
| 98 | #define cudaMemset hipMemset |
| 99 | #define cudaMemsetAsync hipMemsetAsync |
| 100 | #define cudaMemGetInfo hipMemGetInfo |
| 101 | #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize |
| 102 | #define cudaSetDevice hipSetDevice |
| 103 | #define cuDeviceGet hipDeviceGet |
| 104 | #define CUdevice hipDevice_t |
| 105 | #define CUdeviceptr hipDeviceptr_t |
| 106 | #define cuMemUnmap hipMemUnmap |
| 107 | #define CUmemAccessDesc hipMemAccessDesc |
| 108 | #define cuMemAddressFree hipMemAddressFree |
| 109 | #define cuMemRelease hipMemRelease |
| 110 | #define CUmemGenericAllocationHandle hipMemGenericAllocationHandle_t |
| 111 | #define cuMemCreate hipMemCreate |
| 112 | #define cuMemAddressReserve hipMemAddressReserve |
| 113 | #define cuMemMap hipMemMap |
| 114 | #define cuMemSetAccess hipMemSetAccess |
| 115 | #define cuMemGetAllocationGranularity hipMemGetAllocationGranularity |
| 116 | #define CUmemAllocationProp hipMemAllocationProp |
| 117 | #define cuDeviceGetAttribute hipDeviceGetAttribute |
| 118 | #define cudaStreamCreateWithFlags hipStreamCreateWithFlags |
| 119 | #define cudaStreamDestroy hipStreamDestroy |
| 120 | #define cudaStreamFireAndForget hipStreamFireAndForget |
| 121 | #define cudaStreamNonBlocking hipStreamNonBlocking |
| 122 | #define cudaStreamPerThread hipStreamPerThread |
| 123 | #define cudaStreamSynchronize hipStreamSynchronize |
| 124 | #define cudaStreamWaitEvent hipStreamWaitEvent |
| 125 | #define cudaGraphExec_t hipGraphExec_t |
| 126 | #define cudaGraphNode_t hipGraphNode_t |
| 127 | #define cudaKernelNodeParams hipKernelNodeParams |
| 128 | #define cudaKernelNodeParams hipKernelNodeParams |
| 129 | #define cudaGraphExecDestroy hipGraphExecDestroy |
| 130 | #define cudaGraphLaunch hipGraphLaunch |
| 131 | #define cudaErrorGraphExecUpdateFailure hipErrorGraphExecUpdateFailure |
| 132 | #define cudaGraphExecUpdateResult hipGraphExecUpdateResult |
| 133 | #define cudaGraphNodeType hipGraphNodeType |
| 134 | #define cudaGraphNodeTypeKernel hipGraphNodeTypeKernel |
| 135 | #define cudaGraphInstantiate hipGraphInstantiate |
| 136 | #define cudaStreamEndCapture hipStreamEndCapture |
| 137 | #define cudaGraphDestroy hipGraphDestroy |
| 138 | #define cudaGraphKernelNodeSetParams hipGraphKernelNodeSetParams |
| 139 | #define cudaErrorInvalidDeviceFunction hipErrorInvalidDeviceFunction |
| 140 | #define cudaGraphKernelNodeGetParams hipGraphKernelNodeGetParams |
| 141 | #define cudaGraphNodeGetType hipGraphNodeGetType |
| 142 | #define cudaGraphGetNodes hipGraphGetNodes |
| 143 | #define cudaGraphExecUpdate hipGraphExecUpdate |
| 144 | #define cudaStreamCaptureModeRelaxed hipStreamCaptureModeRelaxed |
| 145 | #define cudaStreamBeginCapture hipStreamBeginCapture |
| 146 | #define cudaGraph_t hipGraph_t |
| 147 | #define cudaStream_t hipStream_t |
| 148 | #define cudaSuccess hipSuccess |
| 149 | #define cudaOccupancyMaxActiveBlocksPerMultiprocessor hipOccupancyMaxActiveBlocksPerMultiprocessor |
| 150 | #define cudaFuncSetAttribute hipFuncSetAttribute |
| 151 | #define cudaFuncAttributeMaxDynamicSharedMemorySize hipFuncAttributeMaxDynamicSharedMemorySize |
| 152 | #define __trap() do { abort(); __builtin_unreachable(); } while(0) |
| 153 | #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS |
| 154 | #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED |
| 155 | #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED |
| 156 | #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE |
| 157 | #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH |
| 158 | #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR |
| 159 | #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED |
| 160 | #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR |
| 161 | #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED |
| 162 | |
| 163 | #if HIP_VERSION >= 60500000 |
| 164 | #define CUBLAS_COMPUTE_16F HIPBLAS_COMPUTE_16F |
| 165 | #define CUBLAS_COMPUTE_32F HIPBLAS_COMPUTE_32F |
| 166 | #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_COMPUTE_32F_FAST_16F |
| 167 | #define cublasComputeType_t hipblasComputeType_t |
| 168 | #define cudaDataType_t hipDataType |
| 169 | #else |
| 170 | #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F |
| 171 | #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F |
| 172 | #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F |
| 173 | #define cublasComputeType_t hipblasDatatype_t |
| 174 | #define cudaDataType_t hipblasDatatype_t |
| 175 | #endif // HIP_VERSION >= 6050000 |
| 176 | |
| 177 | #if !defined(__HIP_PLATFORM_AMD__) |
| 178 | #error "The HIP backend supports only AMD targets" |
| 179 | #endif // !defined(__HIP_PLATFORM_AMD__) |
| 180 | |
| 181 | #define __CUDA_ARCH__ 1300 |
| 182 | |
| 183 | #if defined(__gfx900__) || defined(__gfx906__) || defined(__gfx909__) || defined(__gfx90c__) |
| 184 | #define GCN5 |
| 185 | #endif // defined(__gfx900__) || defined(__gfx906__) || defined(__gfx909__) || defined(__gfx90c__) |
| 186 | |
| 187 | #if defined(__gfx803__) |
| 188 | #define GCN4 |
| 189 | #endif // defined(__gfx803__) |
| 190 | |
| 191 | #if defined(GCN5) || defined(GCN4) |
| 192 | #define GCN |
| 193 | #endif // defined(GCN5) || defined(GCN4) |
| 194 | |
| 195 | #if defined(__gfx950__) |
| 196 | #define CDNA4 |
| 197 | #endif // defined(__gfx950__) |
| 198 | |
| 199 | #if defined(__gfx942__) |
| 200 | #define CDNA3 |
| 201 | #endif // defined(__gfx942__) |
| 202 | |
| 203 | #if defined(__gfx90a__) |
| 204 | #define CDNA2 |
| 205 | #endif // defined(__gfx90a__) |
| 206 | |
| 207 | #if defined(__gfx908__) |
| 208 | #define CDNA1 |
| 209 | #endif // defined(__gfx908__) |
| 210 | |
| 211 | #if defined(CDNA4) || defined(CDNA3) || defined(CDNA2) || defined(CDNA1) |
| 212 | #define CDNA // For the entire family |
| 213 | #endif // defined(CDNA4) || defined(CDNA3) || defined(CDNA2) || defined(CDNA1) |
| 214 | |
| 215 | #if defined(__GFX12__) |
| 216 | #define RDNA4 |
| 217 | #endif // defined(__GFX12__) |
| 218 | |
| 219 | #if defined(__gfx1170__) || defined(__gfx1171__) |
| 220 | #define RDNA4_M |
| 221 | #endif // defined(__gfx1170__) || defined(__gfx1171__) |
| 222 | |
| 223 | #if defined(__GFX11__) && !defined(RDNA4_M) |
| 224 | #define RDNA3 |
| 225 | #endif // defined(__GFX11__) && !defined(RDNA4_M) |
| 226 | |
| 227 | #if defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__) || defined(__gfx1153__) |
| 228 | #define RDNA3_5 |
| 229 | #endif // defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__) || defined(__gfx1153__) |
| 230 | |
| 231 | #if defined(RDNA3) && !defined(RDNA3_5) |
| 232 | #define RDNA3_0 |
| 233 | #endif // defined(RDNA3) && !defined(RDNA3_5) |
| 234 | |
| 235 | #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \ |
| 236 | defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__) |
| 237 | #define RDNA2 |
| 238 | #endif |
| 239 | |
| 240 | #if defined(__gfx1010__) || defined(__gfx1012__) |
| 241 | #define RDNA1 |
| 242 | #endif // defined(__gfx1010__) || defined(__gfx1012__) |
| 243 | |
| 244 | #if defined(RDNA4) || defined(RDNA4_M) || defined(RDNA3) || defined(RDNA2) || defined(RDNA1) |
| 245 | #define RDNA // For the entire family |
| 246 | #endif // defined(RDNA4) || defined(RDNA4_M) || defined(RDNA3) || defined(RDNA2) || defined(RDNA1) |
| 247 | |
| 248 | #ifndef __has_builtin |
| 249 | #define __has_builtin(x) 0 |
| 250 | #endif |
| 251 | |
| 252 | typedef __hip_bfloat16 nv_bfloat16; |
| 253 | typedef __hip_bfloat162 nv_bfloat162; |
| 254 | |
| 255 | #if HIP_VERSION >= 60300000 |
| 256 | #include <hip/hip_fp8.h> |
| 257 | typedef __hip_fp8_e4m3 __nv_fp8_e4m3; |
| 258 | #define FP8_AVAILABLE |
| 259 | #endif // HIP_VERSION >= 60300000 |
| 260 | |
| 261 | #if HIP_VERSION < 70000000 |
| 262 | #define __ballot_sync(mask, var) __ballot(var) |
| 263 | #endif //HIP_VERSION < 70000000 |
| 264 | |
| 265 | typedef int8_t int8x4_t __attribute__((ext_vector_type(4))); |
| 266 | typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4))); |
| 267 | static __device__ __forceinline__ int __vsubss4(const int a, const int b) { |
| 268 | const int8x4_t va = reinterpret_cast<const int8x4_t&>(a); |
| 269 | const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b); |
| 270 | #if __has_builtin(__builtin_elementwise_sub_sat) |
| 271 | const int8x4_t c = __builtin_elementwise_sub_sat(va, vb); |
| 272 | return reinterpret_cast<const int &>(c); |
| 273 | #else |
| 274 | int8x4_t c; |
| 275 | int16_t tmp; |
| 276 | #pragma unroll |
| 277 | for (int i = 0; i < 4; i++) { |
| 278 | tmp = va[i] - vb[i]; |
| 279 | if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max(); |
| 280 | if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min(); |
| 281 | c[i] = tmp; |
| 282 | } |
| 283 | return reinterpret_cast<int &>(c); |
| 284 | #endif // __has_builtin(__builtin_elementwise_sub_sat) |
| 285 | } |
| 286 | |
| 287 | static __device__ __forceinline__ int __vsub4(const int a, const int b) { |
| 288 | // do some small modifications to a and b to make the subtraction not underflow |
| 289 | const unsigned int a_large = a | 0x80808080; |
| 290 | const unsigned int b_small = b & 0x7f7f7f7f; |
| 291 | const unsigned int result_low_7bits = a_large - b_small; |
| 292 | |
| 293 | // if two ops share the same high bit, we should flip the high bit of the result |
| 294 | const unsigned int should_flip_high_1bit = (a ^ ~b) & 0x80808080; |
| 295 | |
| 296 | return result_low_7bits ^ should_flip_high_1bit; |
| 297 | } |
| 298 | |
| 299 | static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) { |
| 300 | const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a); |
| 301 | const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b); |
| 302 | unsigned int c; |
| 303 | uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c); |
| 304 | #pragma unroll |
| 305 | for (int i = 0; i < 4; ++i) { |
| 306 | vc[i] = va[i] == vb[i] ? 0xff : 0x00; |
| 307 | } |
| 308 | return c; |
| 309 | } |
| 310 | |
| 311 | static __device__ __forceinline__ unsigned int __vcmpne4(unsigned int a, unsigned int b) { |
| 312 | const unsigned int x = a ^ b; |
| 313 | |
| 314 | // any non-equal bit in a byte will set the high bit of that byte here |
| 315 | // the addition will not overflow in the byte as op1 and op2 are both less than 0x80 |
| 316 | const unsigned int ne_low_7bits = ((x & 0x7f7f7f7f) + 0x7f7f7f7f) & 0x80808080; |
| 317 | const unsigned int ne_high_1bit = x & 0x80808080; |
| 318 | const unsigned int ne_any_bit = ne_low_7bits | ne_high_1bit; |
| 319 | |
| 320 | return (ne_any_bit >> 7) * 0xff; |
| 321 | } |
| 322 | |
| 323 | // v_perm_b32 with selector bytes in [0, 7]: 0-3 pick bytes of lo, 4-7 pick bytes of hi. |
| 324 | static __device__ __forceinline__ uint32_t ggml_hip_perm(const uint32_t hi, const uint32_t lo, const uint32_t sel) { |
| 325 | #if __has_builtin(__builtin_amdgcn_is_invocable) |
| 326 | if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_perm)) { |
| 327 | return __builtin_amdgcn_perm(hi, lo, sel); |
| 328 | } |
| 329 | const uint64_t src = ((uint64_t) hi << 32) | lo; |
| 330 | uint32_t res = 0; |
| 331 | #pragma unroll |
| 332 | for (int i = 0; i < 4; ++i) { |
| 333 | const uint32_t byte_sel = (sel >> (8 * i)) & 0x07; |
| 334 | res |= (uint32_t) ((src >> (8 * byte_sel)) & 0xFF) << (8 * i); |
| 335 | } |
| 336 | return res; |
| 337 | #else |
| 338 | return __builtin_amdgcn_perm(hi, lo, sel); |
| 339 | #endif // __has_builtin(__builtin_amdgcn_is_invocable) |
| 340 | } |