Spaces:
Runtime error
Runtime error
| // for rocblas_initialize() | |
| // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication | |
| // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant | |
| // for large computational tasks. the drawback is that this requires some extra amount of VRAM: | |
| // - 7B quantum model: +100-200 MB | |
| // - 13B quantum model: +200-400 MB | |
| // | |
| //#define GGML_CUDA_FORCE_MMQ | |
| // TODO: improve this to be correct for more hardware | |
| // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores | |
| [[noreturn]] | |
| void ggml_cuda_error(const char * stmt, const char * func, const char * file, int line, const char * msg); | |
| static const char * cublas_get_error_str(const cublasStatus_t err) { | |
| return cublasGetStatusString(err); | |
| } | |
| static const char * cublas_get_error_str(const cublasStatus_t err) { | |
| switch (err) { | |
| case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS"; | |
| case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED"; | |
| case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED"; | |
| case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE"; | |
| case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH"; | |
| case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR"; | |
| case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED"; | |
| case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR"; | |
| case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED"; | |
| default: return "unknown error"; | |
| } | |
| } | |
| static const char * cu_get_error_str(CUresult err) { | |
| const char * err_str; | |
| cuGetErrorString(err, &err_str); | |
| return err_str; | |
| } | |
| typedef half dfloat; // dequantize float | |
| typedef half2 dfloat2; | |
| typedef float dfloat; // dequantize float | |
| typedef float2 dfloat2; | |
| [[noreturn]] | |
| static __device__ void no_device_code( | |
| const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) { | |
| printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n", | |
| file_name, line, function_name, arch); | |
| GGML_UNUSED(arch_list); | |
| printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n", | |
| file_name, line, function_name, arch, arch_list); | |
| __trap(); | |
| GGML_UNUSED(no_device_code); // suppress unused function warning | |
| } | |
| static __device__ __forceinline__ float warp_reduce_sum(float x) { | |
| for (int mask = 16; mask > 0; mask >>= 1) { | |
| x += __shfl_xor_sync(0xffffffff, x, mask, 32); | |
| } | |
| return x; | |
| } | |
| static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) { | |
| for (int mask = 16; mask > 0; mask >>= 1) { | |
| a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32); | |
| a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32); | |
| } | |
| return a; | |
| } | |
| static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) { | |
| for (int mask = 16; mask > 0; mask >>= 1) { | |
| a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32)); | |
| } | |
| return a; | |
| GGML_UNUSED(a); | |
| NO_DEVICE_CODE; | |
| } | |
| static __device__ __forceinline__ float warp_reduce_max(float x) { | |
| for (int mask = 16; mask > 0; mask >>= 1) { | |
| x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32)); | |
| } | |
| return x; | |
| } | |
| static __device__ __forceinline__ half ggml_cuda_hmax(const half a, const half b) { | |
| return __hmax(a, b); | |
| return __half2float(a) > __half2float(b) ? a : b; | |
| GGML_UNUSED(a); | |
| GGML_UNUSED(b); | |
| NO_DEVICE_CODE; | |
| } | |
| static __device__ __forceinline__ half2 ggml_cuda_hmax2(const half2 a, const half2 b) { | |
| return __hmax2(a, b); | |
| half2 ret; | |
| reinterpret_cast<half&>(ret.x) = __low2float(a) > __low2float(b) ? __low2half(a) : __low2half(b); | |
| reinterpret_cast<half&>(ret.y) = __high2float(a) > __high2float(b) ? __high2half(a) : __high2half(b); | |
| return ret; | |
| GGML_UNUSED(a); | |
| GGML_UNUSED(b); | |
| NO_DEVICE_CODE; | |
| } | |
| static __device__ __forceinline__ half2 warp_reduce_max(half2 x) { | |
| for (int mask = 16; mask > 0; mask >>= 1) { | |
| x = ggml_cuda_hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32)); | |
| } | |
| return x; | |
| GGML_UNUSED(x); | |
| NO_DEVICE_CODE; | |
| } | |
| static __device__ __forceinline__ uint32_t __hgt2_mask(const half2 a, const half2 b) { | |
| const uint32_t mask_low = 0x0000FFFF * (float( __low2half(a)) > float( __low2half(b))); | |
| const uint32_t mask_high = 0xFFFF0000 * (float(__high2half(a)) > float(__high2half(b))); | |
| return mask_low | mask_high; | |
| } | |
| typedef int8_t int8x4_t __attribute__((ext_vector_type(4))); | |
| typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4))); | |
| static __device__ __forceinline__ int __vsubss4(const int a, const int b) { | |
| const int8x4_t va = reinterpret_cast<const int8x4_t&>(a); | |
| const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b); | |
| const int8x4_t c = __builtin_elementwise_sub_sat(va, vb); | |
| return reinterpret_cast<const int &>(c); | |
| int8x4_t c; | |
| int16_t tmp; | |
| for (int i = 0; i < 4; i++) { | |
| tmp = va[i] - vb[i]; | |
| if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max(); | |
| if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min(); | |
| c[i] = tmp; | |
| } | |
| return reinterpret_cast<int &>(c); | |
| } | |
| static __device__ __forceinline__ int __vsub4(const int a, const int b) { | |
| return __vsubss4(a, b); | |
| } | |
| static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) { | |
| const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a); | |
| const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b); | |
| unsigned int c; | |
| uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c); | |
| for (int i = 0; i < 4; ++i) { | |
| vc[i] = va[i] == vb[i] ? 0xff : 0x00; | |
| } | |
| return c; | |
| } | |
| static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) { | |
| c = __builtin_amdgcn_sdot4(a, b, c, false); | |
| c = __builtin_amdgcn_sudot4( true, a, true, b, c, false); | |
| int tmp1; | |
| int tmp2; | |
| asm("\n \ | |
| 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 \ | |
| " | |
| : "+v"(c), "=&v"(tmp1), "=&v"(tmp2) | |
| : "v"(a), "v"(b) | |
| ); | |
| const int8x4_t va = reinterpret_cast<const int8x4_t&>(a); | |
| const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b); | |
| c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3]; | |
| return c; | |
| } | |
| // TODO: move to ggml-common.h | |
| static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113}; | |
| typedef void (*dequantize_kernel_t)(const void * vx, const int64_t ib, const int iqs, dfloat2 & v); | |
| ////////////////////// | |
| struct ggml_cuda_device_info { | |
| int device_count; | |
| struct cuda_device_info { | |
| int cc; // compute capability | |
| int nsm; // number of streaming multiprocessors | |
| size_t smpb; // max. shared memory per block | |
| bool vmm; // virtual memory support | |
| size_t vmm_granularity; // granularity of virtual memory | |
| size_t total_vram; | |
| }; | |
| cuda_device_info devices[GGML_CUDA_MAX_DEVICES] = {}; | |
| std::array<float, GGML_CUDA_MAX_DEVICES> default_tensor_split = {}; | |
| }; | |
| const ggml_cuda_device_info & ggml_cuda_info(); | |
| void ggml_cuda_set_device(int device); | |
| int ggml_cuda_get_device(); | |
| struct ggml_cuda_pool { | |
| virtual ~ggml_cuda_pool() = default; | |
| virtual void * alloc(size_t size, size_t * actual_size) = 0; | |
| virtual void free(void * ptr, size_t size) = 0; | |
| }; | |
| template<typename T> | |
| struct ggml_cuda_pool_alloc { | |
| ggml_cuda_pool * pool = nullptr; | |
| T * ptr = nullptr; | |
| size_t actual_size = 0; | |
| ggml_cuda_pool_alloc() = default; | |
| explicit ggml_cuda_pool_alloc(ggml_cuda_pool & pool) : pool(&pool) { | |
| } | |
| ggml_cuda_pool_alloc(ggml_cuda_pool & pool, size_t size) : pool(&pool) { | |
| alloc(size); | |
| } | |
| ~ggml_cuda_pool_alloc() { | |
| if (ptr != nullptr) { | |
| pool->free(ptr, actual_size); | |
| } | |
| } | |
| // size is in number of elements | |
| T * alloc(size_t size) { | |
| GGML_ASSERT(pool != nullptr); | |
| GGML_ASSERT(ptr == nullptr); | |
| ptr = (T *) pool->alloc(size * sizeof(T), &this->actual_size); | |
| return ptr; | |
| } | |
| T * alloc(ggml_cuda_pool & pool, size_t size) { | |
| this->pool = &pool; | |
| return alloc(size); | |
| } | |
| T * get() { | |
| return ptr; | |
| } | |
| ggml_cuda_pool_alloc(const ggml_cuda_pool_alloc &) = delete; | |
| ggml_cuda_pool_alloc(ggml_cuda_pool_alloc &&) = delete; | |
| ggml_cuda_pool_alloc& operator=(const ggml_cuda_pool_alloc &) = delete; | |
| ggml_cuda_pool_alloc& operator=(ggml_cuda_pool_alloc &&) = delete; | |
| }; | |
| // backend interface | |
| struct ggml_tensor_extra_gpu { | |
| void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors | |
| cudaEvent_t events[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS]; // events for synchronizing multiple GPUs | |
| }; | |
| struct ggml_backend_cuda_context { | |
| int device; | |
| std::string name; | |
| cudaEvent_t copy_event = nullptr; | |
| cudaStream_t streams[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS] = { { nullptr } }; | |
| cublasHandle_t cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr}; | |
| explicit ggml_backend_cuda_context(int device) : | |
| device(device), | |
| name(GGML_CUDA_NAME + std::to_string(device)) { | |
| } | |
| ~ggml_backend_cuda_context() { | |
| if (copy_event != nullptr) { | |
| CUDA_CHECK(cudaEventDestroy(copy_event)); | |
| } | |
| for (int i = 0; i < GGML_CUDA_MAX_DEVICES; ++i) { | |
| for (int j = 0; j < GGML_CUDA_MAX_STREAMS; ++j) { | |
| if (streams[i][j] != nullptr) { | |
| CUDA_CHECK(cudaStreamDestroy(streams[i][j])); | |
| } | |
| } | |
| if (cublas_handles[i] != nullptr) { | |
| CUBLAS_CHECK(cublasDestroy(cublas_handles[i])); | |
| } | |
| } | |
| } | |
| cudaStream_t stream(int device, int stream) { | |
| if (streams[device][stream] == nullptr) { | |
| ggml_cuda_set_device(device); | |
| CUDA_CHECK(cudaStreamCreateWithFlags(&streams[device][stream], cudaStreamNonBlocking)); | |
| } | |
| return streams[device][stream]; | |
| } | |
| cudaStream_t stream() { | |
| return stream(device, 0); | |
| } | |
| cublasHandle_t cublas_handle(int device) { | |
| if (cublas_handles[device] == nullptr) { | |
| ggml_cuda_set_device(device); | |
| CUBLAS_CHECK(cublasCreate(&cublas_handles[device])); | |
| CUBLAS_CHECK(cublasSetMathMode(cublas_handles[device], CUBLAS_TF32_TENSOR_OP_MATH)); | |
| } | |
| return cublas_handles[device]; | |
| } | |
| cublasHandle_t cublas_handle() { | |
| return cublas_handle(device); | |
| } | |
| // pool | |
| std::unique_ptr<ggml_cuda_pool> pools[GGML_CUDA_MAX_DEVICES]; | |
| static std::unique_ptr<ggml_cuda_pool> new_pool_for_device(int device); | |
| ggml_cuda_pool & pool(int device) { | |
| if (pools[device] == nullptr) { | |
| pools[device] = new_pool_for_device(device); | |
| } | |
| return *pools[device]; | |
| } | |
| ggml_cuda_pool & pool() { | |
| return pool(device); | |
| } | |
| }; | |