// Gemm.i8: cuBLAS int8 GEMM on Bend arrays, in place. See cublas.bend. // Uses Bend runtime internals (blk_loc, e.mem, gpu_dev): rebuild and recheck with every Bend release. #include #if BEND_CUDA typedef int (*GemmI8Create)(void**); typedef int (*GemmI8Ex)(void*, int, int, int, int, int, const void*, const void*, int, int, const void*, int, int, const void*, void*, int, int, int, int); static struct { int state; // 0 untried, 1 ready, -1 unavailable void* handle; GemmI8Ex gemm; const char* why; } gemm_i8_lib; static void gemm_i8_open(void) { if (gemm_i8_lib.state != 0) { return; } gemm_i8_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_i8_lib.why = "libcublas.so not found"; return; } GemmI8Create create = (GemmI8Create)dlsym(h, "cublasCreate_v2"); gemm_i8_lib.gemm = (GemmI8Ex)dlsym(h, "cublasGemmEx"); if (create == NULL || gemm_i8_lib.gemm == NULL) { gemm_i8_lib.why = "cuBLAS symbols missing"; return; } CUcontext ctx; if (cuDevicePrimaryCtxRetain(&ctx, gpu_dev) != CUDA_SUCCESS || cuCtxSetCurrent(ctx) != CUDA_SUCCESS) { gemm_i8_lib.why = "no CUDA context"; return; } if (create(&gemm_i8_lib.handle) != 0) { gemm_i8_lib.why = "cublasCreate failed"; return; } gemm_i8_lib.state = 1; } // the array's cells, after checking it is a U32 block of at least `need` cells in CUDA managed memory static uint32_t* gemm_i8_cells(Env e, Term a, uint64_t need, const char* name) { if (term_tag(a) != TAG_BUF) { fprintf(stderr, "Gemm.i8: %s is not an Array\n", name); exit(1); } if ((1ull << blk_cls(a)) < need) { fprintf(stderr, "Gemm.i8: %s holds %llu cells, needs %llu\n", name, (unsigned long long)(1ull << blk_cls(a)), (unsigned long long)need); exit(1); } uint32_t* p = (uint32_t*)(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.i8: %s is not in CUDA managed memory (a CUDA build with a `!` call, not --gpu off)\n", name); exit(1); } return p; } #endif Term gemm_i8_run(Env e, Term* f, IoWork* w) { #if BEND_CUDA uint32_t n = (uint32_t)f[3], m = (uint32_t)f[4], k = (uint32_t)f[5]; if (k % 4 != 0) { fprintf(stderr, "Gemm.i8: k = %u is not a multiple of 4\n", k); exit(1); } gemm_i8_open(); if (gemm_i8_lib.state != 1) { fprintf(stderr, "Gemm.i8: %s\n", gemm_i8_lib.why); exit(1); } uint32_t* a = gemm_i8_cells(e, f[0], (uint64_t)n * k / 4, "A"); uint32_t* b = gemm_i8_cells(e, f[1], (uint64_t)m * k / 4, "B"); uint32_t* c = gemm_i8_cells(e, f[2], (uint64_t)n * m, "C"); // row-major C(n x m) = A(n x k) . B(m x k)^T is column-major C^T(m x n) = op_T(B) . A, cuBLAS's "TN" int8 layout int one = 1, zero = 0; int st = gemm_i8_lib.gemm(gemm_i8_lib.handle, 1 /* T */, 0 /* N */, (int)m, (int)n, (int)k, &one, b, 3 /* CUDA_R_8I */, (int)k, a, 3, (int)k, &zero, c, 10 /* CUDA_R_32I */, (int)m, 72 /* CUBLAS_COMPUTE_32I */, -1 /* CUBLAS_GEMM_DEFAULT */); if (st != 0) { fprintf(stderr, "Gemm.i8: cublasGemmEx status %d\n", st); exit(1); } if (cuCtxSynchronize() != CUDA_SUCCESS) { fprintf(stderr, "Gemm.i8: synchronize failed\n"); exit(1); } return io_tup(e, f[0], io_tup(e, f[1], f[2])); #else fprintf(stderr, "Gemm.i8 needs a CUDA build\n"); exit(1); #endif } static void __attribute__((constructor)) gemm_i8_use(void) { io_eff(CID(Gemm.i8), gemm_i8_run); }