// Gemm.f32: cublasSgemmStridedBatched on Bend arrays, in place. Shares gemm_i8.c's library loader when both are used. #include #if BEND_CUDA typedef int (*GemmF32Create)(void**); typedef int (*GemmF32Batched)(void*, int, int, int, int, int, const float*, const float*, int, long long, const float*, int, long long, const float*, float*, int, long long, int); typedef int (*GemmF32Math)(void*, int); static struct { int state; void* handle; GemmF32Batched gemm; GemmF32Math math; const char* why; } gemm_f32_lib; static void gemm_f32_open(void) { if (gemm_f32_lib.state != 0) return; gemm_f32_lib.state = -1; char path[512]; const char* home = getenv("CUDA_HOME"); void* h = NULL; if (home != NULL) { snprintf(path, sizeof path, "%s/lib64/libcublas.so", home); h = dlopen(path, RTLD_NOW | RTLD_GLOBAL); } if (h == NULL) h = dlopen("/usr/local/cuda/lib64/libcublas.so", RTLD_NOW | RTLD_GLOBAL); if (h == NULL) h = dlopen("libcublas.so", RTLD_NOW | RTLD_GLOBAL); if (h == NULL) { gemm_f32_lib.why = "libcublas.so not found"; return; } GemmF32Create create = (GemmF32Create)dlsym(h, "cublasCreate_v2"); gemm_f32_lib.gemm = (GemmF32Batched)dlsym(h, "cublasSgemmStridedBatched"); gemm_f32_lib.math = (GemmF32Math)dlsym(h, "cublasSetMathMode"); if (create == NULL || gemm_f32_lib.gemm == NULL || gemm_f32_lib.math == NULL) { gemm_f32_lib.why = "cuBLAS symbols missing"; return; } CUcontext ctx; if (cuDevicePrimaryCtxRetain(&ctx, gpu_dev) != CUDA_SUCCESS || cuCtxSetCurrent(ctx) != CUDA_SUCCESS) { gemm_f32_lib.why = "no CUDA context"; return; } if (create(&gemm_f32_lib.handle) != 0) { gemm_f32_lib.why = "cublasCreate failed"; return; } gemm_f32_lib.state = 1; } static float* gemm_f32_cells(Env e, Term a, uint64_t need, const char* name) { if (term_tag(a) != TAG_BUF) { fprintf(stderr, "Gemm.f32: %s is not a 32-bit array\n", name); exit(1); } if ((1ull << blk_cls(a)) < need) { fprintf(stderr, "Gemm.f32: %s holds %llu cells, needs %llu\n", name, (unsigned long long)(1ull << blk_cls(a)), (unsigned long long)need); exit(1); } float* p = (float*)(e.mem + blk_loc(e.mem, a)); unsigned int managed = 0; if (cuPointerGetAttribute(&managed, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)(uintptr_t)p) != CUDA_SUCCESS || !managed) { fprintf(stderr, "Gemm.f32: %s is not in CUDA managed memory (a CUDA build with a `!` call, not --gpu off)\n", name); exit(1); } return p; } // cells a column-major operand spans: the last column of the last batch, plus its rows static uint64_t gemm_f32_span(uint64_t rows, uint64_t cols, uint64_t ld, uint64_t stride, uint64_t batch) { return (batch - 1) * stride + (cols - 1) * ld + rows; } #endif // tf32: inputs rounded to TF32 (10-bit mantissa) on tensor cores, FP32 accumulation (CUBLAS_TF32_TENSOR_OP_MATH) static Term gemm_f32_any(Env e, Term* f, int offs, int tf32) { #if BEND_CUDA uint64_t oa = offs ? (uint32_t)f[15] : 0, ob = offs ? (uint32_t)f[16] : 0, oc = offs ? (uint32_t)f[17] : 0; uint32_t ta = (uint32_t)f[3], tb = (uint32_t)f[4], m = (uint32_t)f[5], n = (uint32_t)f[6], k = (uint32_t)f[7]; uint32_t lda = (uint32_t)f[8], ldb = (uint32_t)f[9], ldc = (uint32_t)f[10]; uint64_t sa = (uint32_t)f[11], sb = (uint32_t)f[12], sc = (uint32_t)f[13], batch = (uint32_t)f[14]; gemm_f32_open(); if (gemm_f32_lib.state != 1) { fprintf(stderr, "Gemm.f32: %s\n", gemm_f32_lib.why); exit(1); } float* a = gemm_f32_cells(e, f[0], oa + (ta ? gemm_f32_span(k, m, lda, sa, batch) : gemm_f32_span(m, k, lda, sa, batch)), "A") + oa; float* b = gemm_f32_cells(e, f[1], ob + (tb ? gemm_f32_span(n, k, ldb, sb, batch) : gemm_f32_span(k, n, ldb, sb, batch)), "B") + ob; float* c = gemm_f32_cells(e, f[2], oc + gemm_f32_span(m, n, ldc, sc, batch), "C") + oc; float one = 1.0f, zero = 0.0f; if (gemm_f32_lib.math(gemm_f32_lib.handle, tf32 ? 3 : 0) != 0) { fprintf(stderr, "Gemm.f32: cublasSetMathMode failed\n"); exit(1); } int st = gemm_f32_lib.gemm(gemm_f32_lib.handle, (int)ta, (int)tb, (int)m, (int)n, (int)k, &one, a, (int)lda, (long long)sa, b, (int)ldb, (long long)sb, &zero, c, (int)ldc, (long long)sc, (int)batch); if (st != 0) { fprintf(stderr, "Gemm.f32: cublasSgemmStridedBatched status %d\n", st); exit(1); } if (cuCtxSynchronize() != CUDA_SUCCESS) { fprintf(stderr, "Gemm.f32: synchronize failed\n"); exit(1); } return io_tup(e, f[0], io_tup(e, f[1], f[2])); #else fprintf(stderr, "Gemm.f32 needs a CUDA build\n"); exit(1); #endif } Term gemm_f32_run(Env e, Term* f, IoWork* w) { return gemm_f32_any(e, f, 0, 0); } Term gemm_f32o_run(Env e, Term* f, IoWork* w) { return gemm_f32_any(e, f, 1, 0); } Term gemm_tf32o_run(Env e, Term* f, IoWork* w) { return gemm_f32_any(e, f, 1, 1); } static void __attribute__((constructor)) gemm_f32_use(void) { #ifdef CID(Gemm.f32) io_eff(CID(Gemm.f32), gemm_f32_run); #endif #ifdef CID(Gemm.f32o) io_eff(CID(Gemm.f32o), gemm_f32o_run); #endif #ifdef CID(Gemm.tf32o) io_eff(CID(Gemm.tf32o), gemm_tf32o_run); #endif }