|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <musa_runtime.h> |
| 4 | +#include <musa.h> |
| 5 | +#include <mublas.h> |
| 6 | +#include <musa_fp16.h> |
| 7 | +#define CUBLAS_COMPUTE_16F CUDA_R_16F |
| 8 | +#define CUBLAS_COMPUTE_32F CUDA_R_32F |
| 9 | +#define CUBLAS_COMPUTE_32F_FAST_16F MUBLAS_COMPUTE_32F_FAST_16F |
| 10 | +#define CUBLAS_GEMM_DEFAULT MUBLAS_GEMM_DEFAULT |
| 11 | +#define CUBLAS_GEMM_DEFAULT_TENSOR_OP MUBLAS_GEMM_DEFAULT |
| 12 | +#define CUBLAS_OP_N MUBLAS_OP_N |
| 13 | +#define CUBLAS_OP_T MUBLAS_OP_T |
| 14 | +#define CUBLAS_STATUS_SUCCESS MUBLAS_STATUS_SUCCESS |
| 15 | +#define CUBLAS_TF32_TENSOR_OP_MATH MUBLAS_MATH_MODE_DEFAULT |
| 16 | +#define CUDA_R_16F MUSA_R_16F |
| 17 | +#define CUDA_R_32F MUSA_R_32F |
| 18 | +#define cublasComputeType_t cudaDataType_t |
| 19 | +#define cublasCreate mublasCreate |
| 20 | +#define cublasDestroy mublasDestroy |
| 21 | +#define cublasGemmEx mublasGemmEx |
| 22 | +#define cublasGemmBatchedEx mublasGemmBatchedEx |
| 23 | +#define cublasGemmStridedBatchedEx mublasGemmStridedBatchedEx |
| 24 | +#define cublasHandle_t mublasHandle_t |
| 25 | +#define cublasSetMathMode mublasSetMathMode |
| 26 | +#define cublasSetStream mublasSetStream |
| 27 | +#define cublasSgemm mublasSgemm |
| 28 | +#define cublasStatus_t mublasStatus_t |
| 29 | +#define cublasGetStatusString mublasStatus_to_string |
| 30 | +#define cudaDataType_t musaDataType_t |
| 31 | +#define cudaDeviceCanAccessPeer musaDeviceCanAccessPeer |
| 32 | +#define cudaDeviceDisablePeerAccess musaDeviceDisablePeerAccess |
| 33 | +#define cudaDeviceEnablePeerAccess musaDeviceEnablePeerAccess |
| 34 | +#define cudaDeviceProp musaDeviceProp |
| 35 | +#define cudaDeviceSynchronize musaDeviceSynchronize |
| 36 | +#define cudaError_t musaError_t |
| 37 | +#define cudaErrorPeerAccessAlreadyEnabled musaErrorPeerAccessAlreadyEnabled |
| 38 | +#define cudaErrorPeerAccessNotEnabled musaErrorPeerAccessNotEnabled |
| 39 | +#define cudaEventCreateWithFlags musaEventCreateWithFlags |
| 40 | +#define cudaEventDisableTiming musaEventDisableTiming |
| 41 | +#define cudaEventRecord musaEventRecord |
| 42 | +#define cudaEventSynchronize musaEventSynchronize |
| 43 | +#define cudaEvent_t musaEvent_t |
| 44 | +#define cudaEventDestroy musaEventDestroy |
| 45 | +#define cudaFree musaFree |
| 46 | +#define cudaFreeHost musaFreeHost |
| 47 | +#define cudaGetDevice musaGetDevice |
| 48 | +#define cudaGetDeviceCount musaGetDeviceCount |
| 49 | +#define cudaGetDeviceProperties musaGetDeviceProperties |
| 50 | +#define cudaGetErrorString musaGetErrorString |
| 51 | +#define cudaGetLastError musaGetLastError |
| 52 | +#define cudaHostRegister musaHostRegister |
| 53 | +#define cudaHostRegisterPortable musaHostRegisterPortable |
| 54 | +#define cudaHostRegisterReadOnly musaHostRegisterReadOnly |
| 55 | +#define cudaHostUnregister musaHostUnregister |
| 56 | +#define cudaLaunchHostFunc musaLaunchHostFunc |
| 57 | +#define cudaMalloc musaMalloc |
| 58 | +#define cudaMallocHost musaMallocHost |
| 59 | +#define cudaMemcpy musaMemcpy |
| 60 | +#define cudaMemcpyAsync musaMemcpyAsync |
| 61 | +#define cudaMemcpyPeerAsync musaMemcpyPeerAsync |
| 62 | +#define cudaMemcpy2DAsync musaMemcpy2DAsync |
| 63 | +#define cudaMemcpyDeviceToDevice musaMemcpyDeviceToDevice |
| 64 | +#define cudaMemcpyDeviceToHost musaMemcpyDeviceToHost |
| 65 | +#define cudaMemcpyHostToDevice musaMemcpyHostToDevice |
| 66 | +#define cudaMemcpyKind musaMemcpyKind |
| 67 | +#define cudaMemset musaMemset |
| 68 | +#define cudaMemsetAsync musaMemsetAsync |
| 69 | +#define cudaMemGetInfo musaMemGetInfo |
| 70 | +#define cudaOccupancyMaxPotentialBlockSize musaOccupancyMaxPotentialBlockSize |
| 71 | +#define cudaSetDevice musaSetDevice |
| 72 | +#define cudaStreamCreateWithFlags musaStreamCreateWithFlags |
| 73 | +#define cudaStreamDestroy musaStreamDestroy |
| 74 | +#define cudaStreamFireAndForget musaStreamFireAndForget |
| 75 | +#define cudaStreamNonBlocking musaStreamNonBlocking |
| 76 | +#define cudaStreamPerThread musaStreamPerThread |
| 77 | +#define cudaStreamSynchronize musaStreamSynchronize |
| 78 | +#define cudaStreamWaitEvent musaStreamWaitEvent |
| 79 | +#define cudaStream_t musaStream_t |
| 80 | +#define cudaSuccess musaSuccess |
| 81 | + |
| 82 | +// Additional mappings for MUSA virtual memory pool |
| 83 | +#define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED MU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED |
| 84 | +#define CU_MEM_ACCESS_FLAGS_PROT_READWRITE MU_MEM_ACCESS_FLAGS_PROT_READWRITE |
| 85 | +#define CU_MEM_ALLOC_GRANULARITY_RECOMMENDED MU_MEM_ALLOC_GRANULARITY_RECOMMENDED |
| 86 | +#define CU_MEM_ALLOCATION_TYPE_PINNED MU_MEM_ALLOCATION_TYPE_PINNED |
| 87 | +#define CU_MEM_LOCATION_TYPE_DEVICE MU_MEM_LOCATION_TYPE_DEVICE |
| 88 | +#define CUdevice MUdevice |
| 89 | +#define CUdeviceptr MUdeviceptr |
| 90 | +#define CUmemAccessDesc MUmemAccessDesc |
| 91 | +#define CUmemAllocationProp MUmemAllocationProp |
| 92 | +#define CUmemGenericAllocationHandle MUmemGenericAllocationHandle |
| 93 | +#define cuDeviceGet muDeviceGet |
| 94 | +#define cuDeviceGetAttribute muDeviceGetAttribute |
| 95 | +#define cuMemAddressFree muMemAddressFree |
| 96 | +#define cuMemAddressReserve muMemAddressReserve |
| 97 | +#define cuMemCreate muMemCreate |
| 98 | +#define cuMemGetAllocationGranularity muMemGetAllocationGranularity |
| 99 | +#define cuMemMap muMemMap |
| 100 | +#define cuMemRelease muMemRelease |
| 101 | +#define cuMemSetAccess muMemSetAccess |
| 102 | +#define cuMemUnmap muMemUnmap |
| 103 | +#define cudaFuncAttributeMaxDynamicSharedMemorySize musaFuncAttributeMaxDynamicSharedMemorySize |
| 104 | +#define cudaFuncSetAttribute musaFuncSetAttribute |
| 105 | +#define cudaMemcpy3DPeerParms musaMemcpy3DPeerParms |
| 106 | +#define make_cudaExtent make_musaExtent |
| 107 | +#define make_cudaPitchedPtr make_musaPitchedPtr |
| 108 | + |
| 109 | +// Additional mappings for MUSA graphs |
| 110 | +#define CUDA_SUCCESS MUSA_SUCCESS |
| 111 | +#define CUresult MUresult |
| 112 | +#define cuGetErrorString muGetErrorString |
| 113 | +#define cudaErrorGraphExecUpdateFailure musaErrorGraphExecUpdateFailure |
| 114 | +#define cudaErrorInvalidDeviceFunction musaErrorInvalidDeviceFunction |
| 115 | +#define cudaGraphDestroy musaGraphDestroy |
| 116 | +#define cudaGraphExecDestroy musaGraphExecDestroy |
| 117 | +#define cudaGraphExec_t musaGraphExec_t |
| 118 | +#define cudaGraphExecUpdate musaGraphExecUpdate |
| 119 | +#define cudaGraphExecUpdateResultInfo musaGraphExecUpdateResult |
| 120 | +#define cudaGraphGetNodes musaGraphGetNodes |
| 121 | +#define cudaGraphInstantiate musaGraphInstantiate |
| 122 | +#define cudaGraphKernelNodeGetParams musaGraphKernelNodeGetParams |
| 123 | +#define cudaGraphKernelNodeSetParams musaGraphKernelNodeSetParams |
| 124 | +#define cudaGraphLaunch musaGraphLaunch |
| 125 | +#define cudaGraphNodeGetType musaGraphNodeGetType |
| 126 | +#define cudaGraphNode_t musaGraphNode_t |
| 127 | +#define cudaGraphNodeType musaGraphNodeType |
| 128 | +#define cudaGraphNodeTypeKernel musaGraphNodeTypeKernel |
| 129 | +#define cudaGraph_t musaGraph_t |
| 130 | +#define cudaKernelNodeParams musaKernelNodeParams |
| 131 | +#define cudaStreamCaptureModeRelaxed musaStreamCaptureModeRelaxed |
| 132 | +#define cudaStreamEndCapture musaStreamEndCapture |
| 133 | + |
| 134 | +// XXX: Clang builtins mapping |
| 135 | +#define __vsub4 __vsub4_musa |
| 136 | +#define __vcmpeq4 __vcmpeq4_musa |
| 137 | +#define __vcmpne4 __vcmpne4_musa |
| 138 | + |
| 139 | +#ifndef __has_builtin |
| 140 | + #define __has_builtin(x) 0 |
| 141 | +#endif |
| 142 | + |
| 143 | +typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4))); |
| 144 | + |
| 145 | +static __device__ __forceinline__ int __vsub4_musa(const int a, const int b) { |
| 146 | + return __vsubss4(a, b); |
| 147 | +} |
| 148 | + |
| 149 | +static __device__ __forceinline__ unsigned int __vcmpeq4_musa(unsigned int a, unsigned int b) { |
| 150 | + const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a); |
| 151 | + const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b); |
| 152 | + unsigned int c; |
| 153 | + uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c); |
| 154 | +#pragma unroll |
| 155 | + for (int i = 0; i < 4; ++i) { |
| 156 | + vc[i] = va[i] == vb[i] ? 0xff : 0x00; |
| 157 | + } |
| 158 | + return c; |
| 159 | +} |
| 160 | + |
| 161 | +static __device__ __forceinline__ unsigned int __vcmpne4_musa(unsigned int a, unsigned int b) { |
| 162 | + const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a); |
| 163 | + const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b); |
| 164 | + unsigned int c; |
| 165 | + uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c); |
| 166 | +#pragma unroll |
| 167 | + for (int i = 0; i < 4; ++i) { |
| 168 | + vc[i] = va[i] == vb[i] ? 0x00 : 0xff; |
| 169 | + } |
| 170 | + return c; |
| 171 | +} |
0 commit comments