okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
508e6999a
main
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(__GFX11__ ) 220#define RDNA3 221#endif // defined(__GFX11__) 222 223#if defined(__gfx1150__ )|| defined(__gfx1151__ )|| defined(__gfx1152__ )|| defined(__gfx1153__ ) 224#define RDNA3_5 225#endif // defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__) || defined(__gfx1153__) 226 227#if defined(RDNA3 )&& !defined(RDNA3_5 ) 228#define RDNA3_0 229#endif // defined(RDNA3) && !defined(RDNA3_5) 230 231#if defined(__gfx1030__ )|| defined(__gfx1031__ )|| defined(__gfx1032__ )|| defined(__gfx1033__ )|| \ 232 defined(__gfx1034__ )|| defined(__gfx1035__ )|| defined(__gfx1036__ )|| defined(__gfx1037__ ) 233#define RDNA2 234#endif 235 236#if defined(__gfx1010__ )|| defined(__gfx1012__ ) 237#define RDNA1 238#endif // defined(__gfx1010__) || defined(__gfx1012__) 239 240#if defined(RDNA4 )|| defined(RDNA3 )|| defined(RDNA2 )|| defined(RDNA1 ) 241#define RDNA // For the entire family 242#endif // defined(RDNA4) || defined(RDNA3) || defined(RDNA2) || defined(RDNA1) 243 244#ifndef __has_builtin 245#define __has_builtin (x ) 0 246#endif 247 248typedef __hip_bfloat16 nv_bfloat16 ; 249typedef __hip_bfloat162 nv_bfloat162 ; 250 251#if HIP_VERSION >=60200000 252#include <hip/hip_fp8.h> 253typedef __hip_fp8_e4m3 __nv_fp8_e4m3 ; 254#define FP8_AVAILABLE 255#endif // HIP_VERSION >= 60200000 256 257typedef int8_t int8x4_t __attribute__((ext_vector_type (4 ))); 258typedef uint8_t uint8x4_t __attribute__((ext_vector_type (4 ))); 259static __device__ __forceinline__ int __vsubss4 (const int a ,const int b ) { 260const int8x4_t va = reinterpret_cast < const int8x4_t &> (a ); 261const int8x4_t vb = reinterpret_cast < const int8x4_t &> (b ); 262#if __has_builtin (__builtin_elementwise_sub_sat ) 263const int8x4_t c = __builtin_elementwise_sub_sat (va ,vb ); 264return reinterpret_cast < const int &> (c ); 265#else 266int8x4_t c ; 267int16_t tmp ; 268#pragma unroll 269for (int i = 0 ;i < 4 ;i ++ ) { 270tmp = va [i ]- vb [i ]; 271if (tmp > std ::numeric_limits < int8_t > ::max ())tmp = std ::numeric_limits < int8_t > ::max (); 272if (tmp < std ::numeric_limits < int8_t > ::min ())tmp = std ::numeric_limits < int8_t > ::min (); 273c [i ]= tmp ; 274 } 275return reinterpret_cast < int &> (c ); 276#endif // __has_builtin(__builtin_elementwise_sub_sat) 277} 278 279static __device__ __forceinline__ int __vsub4 (const int a ,const int b ) { 280return __vsubss4 (a ,b ); 281} 282 283static __device__ __forceinline__ unsigned int __vcmpeq4 (unsigned int a ,unsigned int b ) { 284const uint8x4_t & va = reinterpret_cast < const uint8x4_t &> (a ); 285const uint8x4_t & vb = reinterpret_cast < const uint8x4_t &> (b ); 286unsigned int c ; 287uint8x4_t & vc = reinterpret_cast < uint8x4_t &> (c ); 288#pragma unroll 289for (int i = 0 ;i < 4 ;++ i ) { 290vc [i ]= va [i ]== vb [i ] ?0xff :0x00 ; 291 } 292return c ; 293} 294 295static __device__ __forceinline__ unsigned int __vcmpne4 (unsigned int a ,unsigned int b ) { 296const uint8x4_t & va = reinterpret_cast < const uint8x4_t &> (a ); 297const uint8x4_t & vb = reinterpret_cast < const uint8x4_t &> (b ); 298unsigned int c ; 299uint8x4_t & vc = reinterpret_cast < uint8x4_t &> (c ); 300#pragma unroll 301for (int i = 0 ;i < 4 ;++ i ) { 302vc [i ]= va [i ]== vb [i ] ?0x00 :0xff ; 303 } 304return c ; 305}