okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1// src/image/dit/dit_gemm.cu - see include/strata/image/dit_gemm.hpp. 2#include "strata/image/dit_gemm.hpp" 3 4#include "strata/image/te_dequant.hpp" 5#include "strata/kernels/f16_bits.hpp" 6 7#if defined(__HIP__) || defined(__HIPCC__) || defined(__HIP_PLATFORM_AMD__) 8#include "strata/hip_compat/cuda_runtime.h" 9#include "strata/hip_compat/cublas_v2.h" 10#else 11#include <cublas_v2.h> 12#include <cuda_runtime.h> 13#endif 14 15#include <mutex> 16 17namespace strata::image { 18namespace { 19 20// rows of f32 (stride ldx) -> packed f16 rows, round to nearest even (f16_bits.hpp) 21__global__ void rows_to_f16(const float* __restrict__ x, int64_t ldx, int64_t K, int64_t n, uint16_t* __restrict__ out) { 22 const int64_t total = n * K; 23 for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < total; i += (int64_t) gridDim.x * blockDim.x) { 24 const int64_t r = i / K, c = i % K; 25 out[i] = strata::kernels::f16_from_f32(x[r * ldx + c]); 26 } 27} 28 29struct State { 30 std::mutex mu; 31 int device = -1; 32 cublasHandle_t blas = nullptr; 33 uint16_t* w16 = nullptr; 34 size_t w_cap = 0; 35 uint16_t* x16 = nullptr; 36 size_t x_cap = 0; 37}; 38State& st() { 39 static State s; 40 return s; 41} 42 43std::string grow(uint16_t*& p, size_t& cap, size_t elems) { 44 if (elems <= cap) return std::string(); 45 if (p) cudaFree(p); 46 p = nullptr; 47 cap = 0; 48 const cudaError_t e = cudaMalloc((void**) &p, elems * sizeof(uint16_t)); 49 if (e != cudaSuccess) return std::string("dit_gemm: cudaMalloc: ") + cudaGetErrorString(e); 50 cap = elems; 51 return std::string(); 52} 53 54} // namespace 55 56namespace { 57 58// x (f32 rows, converted here) or x16 (f16 rows, used in place) 59std::string gemm_impl(void* stream, uint32_t type, const void* w, int64_t K, int64_t N, const float* x, 60 const uint16_t* x16, int64_t ldx, int64_t n, float* y, int64_t ldy) { 61 if (n <= 0) return std::string(); 62 State& s = st(); 63 std::lock_guard<std::mutex> lock(s.mu); 64 int dev = 0; 65 cudaGetDevice(&dev); 66 if (s.blas == nullptr || s.device != dev) { 67 if (s.blas) cublasDestroy(s.blas); 68 s.blas = nullptr; 69 if (cublasCreate(&s.blas) != CUBLAS_STATUS_SUCCESS) return "dit_gemm: cannot create the BLAS handle"; 70 s.device = dev; 71 } 72 std::string err; 73 if (type != TE_F16) err = grow(s.w16, s.w_cap, (size_t) N * (size_t) K); 74 if (err.empty() && x16 == nullptr) err = grow(s.x16, s.x_cap, (size_t) n * (size_t) K); 75 if (!err.empty()) return err; 76 cudaStream_t cs = static_cast<cudaStream_t>(stream); 77 const uint16_t* wa = static_cast<const uint16_t*>(w); 78 if (type != TE_F16) { 79 err = te_dequant_f16(type, w, K, N, s.w16, stream); 80 if (!err.empty()) return "dit_gemm: " + err; 81 wa = s.w16; 82 } 83 const uint16_t* xa = x16; 84 int64_t lda = ldx; 85 if (x16 == nullptr) { 86 const int64_t total = n * K; 87 const int64_t blocks = (total + 255) / 256; 88 rows_to_f16<<<(unsigned) (blocks < 65535 * 8 ? blocks : 65535 * 8), 256, 0, cs>>>(x, ldx, K, n, s.x16); 89 if (cudaGetLastError() != cudaSuccess) return "dit_gemm: the f16 conversion kernel failed"; 90 xa = s.x16; 91 lda = K; 92 } 93 if (cublasSetStream(s.blas, cs) != CUBLAS_STATUS_SUCCESS) return "dit_gemm: cublasSetStream failed"; 94 const float one = 1.f, zero = 0.f; 95 const auto r = cublasGemmEx(s.blas, CUBLAS_OP_T, CUBLAS_OP_N, (int) N, (int) n, (int) K, &one, wa, CUDA_R_16F, 96 (int) K, xa, CUDA_R_16F, (int) lda, &zero, y, CUDA_R_32F, (int) ldy, CUBLAS_COMPUTE_32F, 97 CUBLAS_GEMM_DEFAULT); 98 if (r != CUBLAS_STATUS_SUCCESS) return "dit_gemm: GemmEx status " + std::to_string((int) r); 99 return std::string(); 100} 101 102} // namespace 103 104std::string dit_gemm(void* stream, uint32_t type, const void* w, int64_t K, int64_t N, const float* x, int64_t ldx, 105 int64_t n, float* y, int64_t ldy) { 106 return gemm_impl(stream, type, w, K, N, x, nullptr, ldx, n, y, ldy); 107} 108 109std::string dit_gemm_x16(void* stream, uint32_t type, const void* w, int64_t K, int64_t N, const uint16_t* x16, 110 int64_t ldx, int64_t n, float* y, int64_t ldy) { 111 return gemm_impl(stream, type, w, K, N, nullptr, x16, ldx, n, y, ldy); 112} 113 114size_t dit_gemm_buffer_bytes() { 115 State& s = st(); 116 std::lock_guard<std::mutex> lock(s.mu); 117 return (s.w_cap + s.x_cap) * sizeof(uint16_t); 118} 119 120void dit_gemm_release() { 121 State& s = st(); 122 std::lock_guard<std::mutex> lock(s.mu); 123 if (s.w16) cudaFree(s.w16); 124 if (s.x16) cudaFree(s.x16); 125 if (s.blas) cublasDestroy(s.blas); 126 s.w16 = s.x16 = nullptr; 127 s.w_cap = s.x_cap = 0; 128 s.blas = nullptr; 129 s.device = -1; 130} 131 132} // namespace strata::image