// Wt.load and Gemm.i8w (see cublas.bend). Uses Bend runtime internals (e.mem, blk_loc, gpu_dev). #include #if BEND_CUDA #define WT_MAX 4096 static struct { CUdeviceptr ptr; uint64_t bytes; int f32; } wt_tab[WT_MAX]; static uint32_t wt_count; typedef int (*WtCreate)(void**); typedef int (*WtSgemm)(void*, int, int, int, int, int, const float*, const float*, int, const float*, int, const float*, float*, int); typedef int (*WtGemmEx)(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; void* handle; WtGemmEx gemm; WtSgemm sgemm; const char* why; } wt_lib; static void wt_ctx(void) { CUcontext ctx; if (cuDevicePrimaryCtxRetain(&ctx, gpu_dev) != CUDA_SUCCESS || cuCtxSetCurrent(ctx) != CUDA_SUCCESS) { fprintf(stderr, "Wt: no CUDA context\n"); exit(1); } } static void wt_open(void) { if (wt_lib.state != 0) return; wt_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) { wt_lib.why = "libcublas.so not found"; return; } WtCreate create = (WtCreate)dlsym(h, "cublasCreate_v2"); wt_lib.gemm = (WtGemmEx)dlsym(h, "cublasGemmEx"); wt_lib.sgemm = (WtSgemm)dlsym(h, "cublasSgemm_v2"); if (create == NULL || wt_lib.gemm == NULL || wt_lib.sgemm == NULL) { wt_lib.why = "cuBLAS symbols missing"; return; } wt_ctx(); if (create(&wt_lib.handle) != 0) { wt_lib.why = "cublasCreate failed"; return; } wt_lib.state = 1; } static void* wt_cells(Env e, Term a, uint64_t need, const char* name) { if (term_tag(a) != TAG_BUF) { fprintf(stderr, "Gemm.i8w: %s is not a 32-bit array\n", name); exit(1); } if ((1ull << blk_cls(a)) < need) { fprintf(stderr, "Gemm.i8w: %s holds %llu cells, needs %llu\n", name, (unsigned long long)(1ull << blk_cls(a)), (unsigned long long)need); exit(1); } void* p = (void*)(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.i8w: %s is not in CUDA managed memory\n", name); exit(1); } return p; } #endif // mode 0: int8 as is; 1: bf16 -> F32; 2: fp16 -> F32 static Term wt_load_any(Env e, Term* f, int mode) { int bf16 = mode != 0; #if BEND_CUDA uint64_t len; char* path = io_cstr(e, f[0], &len); uint64_t off = ((uint64_t)(uint32_t)f[1] << 32) | (uint32_t)f[2]; uint64_t bytes = (uint32_t)f[3]; if (wt_count >= WT_MAX) { fprintf(stderr, "Wt.load: more than %d buffers\n", WT_MAX); exit(1); } wt_ctx(); FILE* fp = fopen(path, "rb"); if (fp == NULL) { fprintf(stderr, "Wt.load: cannot open %s\n", path); exit(1); } uint64_t dbytes = bf16 ? bytes * 2 : bytes; if (bf16 && bytes % 2 != 0) { fprintf(stderr, "Wt.load_bf16: odd byte count\n"); exit(1); } CUdeviceptr d; if (cuMemAlloc(&d, dbytes) != CUDA_SUCCESS) { fprintf(stderr, "Wt.load: cannot allocate %llu bytes on the GPU\n", (unsigned long long)dbytes); exit(1); } const uint64_t chunk = 64ull << 20; char* buf = malloc(chunk); uint32_t* wide = bf16 ? malloc(chunk * 2) : NULL; for (uint64_t at = 0; at < bytes; at += chunk) { uint64_t n = bytes - at < chunk ? bytes - at : chunk; if (fseeko(fp, (off_t)(off + at), SEEK_SET) != 0 || fread(buf, 1, n, fp) != n) { fprintf(stderr, "Wt.load: short read of %s at %llu\n", path, (unsigned long long)(off + at)); exit(1); } if (bf16) { if (mode == 1) { for (uint64_t i = 0; i < n / 2; i++) wide[i] = (uint32_t)((uint16_t*)buf)[i] << 16; } else { for (uint64_t i = 0; i < n / 2; i++) { float v = (float)((_Float16*)buf)[i]; memcpy(&wide[i], &v, 4); } } if (cuMemcpyHtoD(d + at * 2, wide, n * 2) != CUDA_SUCCESS) { fprintf(stderr, "Wt.load: copy failed\n"); exit(1); } } else if (cuMemcpyHtoD(d + at, buf, n) != CUDA_SUCCESS) { fprintf(stderr, "Wt.load: copy failed\n"); exit(1); } } free(buf); free(wide); fclose(fp); free(path); wt_tab[wt_count].ptr = d; wt_tab[wt_count].bytes = dbytes; wt_tab[wt_count].f32 = bf16; return (Term)(wt_count++); #else fprintf(stderr, "Wt.load needs a CUDA build\n"); exit(1); #endif } Term wt_load_run(Env e, Term* f, IoWork* w) { return wt_load_any(e, f, 0); } Term wt_load_bf16_run(Env e, Term* f, IoWork* w) { return wt_load_any(e, f, 1); } Term wt_load_f16_run(Env e, Term* f, IoWork* w) { return wt_load_any(e, f, 2); } Term wt_free_run(Env e, Term* f, IoWork* w) { #if BEND_CUDA uint32_t id = (uint32_t)f[0]; if (id >= wt_count || wt_tab[id].ptr == 0) { fprintf(stderr, "Wt.free: no weight buffer %u\n", id); exit(1); } wt_ctx(); cuMemFree(wt_tab[id].ptr); wt_tab[id].ptr = 0; wt_tab[id].bytes = 0; #endif return term_pak(CID(Unit), 0); } Term gemm_f32w_run(Env e, Term* f, IoWork* w) { #if BEND_CUDA uint32_t id = (uint32_t)f[1], n = (uint32_t)f[3], m = (uint32_t)f[4], k = (uint32_t)f[5]; if (id >= wt_count || wt_tab[id].ptr == 0 || !wt_tab[id].f32) { fprintf(stderr, "Gemm.f32w: no F32 weight buffer %u\n", id); exit(1); } if (wt_tab[id].bytes != (uint64_t)m * k * 4) { fprintf(stderr, "Gemm.f32w: buffer %u holds %llu bytes, %u x %u needs %llu\n", id, (unsigned long long)wt_tab[id].bytes, m, k, (unsigned long long)m * k * 4); exit(1); } wt_open(); if (wt_lib.state != 1) { fprintf(stderr, "Gemm.f32w: %s\n", wt_lib.why); exit(1); } float* a = wt_cells(e, f[0], (uint64_t)n * k, "A"); float* c = wt_cells(e, f[2], (uint64_t)n * m, "C"); float one = 1.0f, zero = 0.0f; int st = wt_lib.sgemm(wt_lib.handle, 1, 0, (int)m, (int)n, (int)k, &one, (const float*)(uintptr_t)wt_tab[id].ptr, (int)k, a, (int)k, &zero, c, (int)m); if (st != 0) { fprintf(stderr, "Gemm.f32w: cublasSgemm status %d\n", st); exit(1); } if (cuCtxSynchronize() != CUDA_SUCCESS) { fprintf(stderr, "Gemm.f32w: synchronize failed\n"); exit(1); } return io_tup(e, f[0], f[2]); #else fprintf(stderr, "Gemm.f32w needs a CUDA build\n"); exit(1); #endif } Term gemm_i8w_run(Env e, Term* f, IoWork* w) { #if BEND_CUDA uint32_t id = (uint32_t)f[1], n = (uint32_t)f[3], m = (uint32_t)f[4], k = (uint32_t)f[5]; if (id >= wt_count || wt_tab[id].ptr == 0 || wt_tab[id].f32) { fprintf(stderr, "Gemm.i8w: no int8 weight buffer %u\n", id); exit(1); } if (wt_tab[id].bytes != (uint64_t)m * k) { fprintf(stderr, "Gemm.i8w: buffer %u holds %llu bytes, %u x %u needs %llu\n", id, (unsigned long long)wt_tab[id].bytes, m, k, (unsigned long long)m * k); exit(1); } if (k % 4 != 0) { fprintf(stderr, "Gemm.i8w: k = %u is not a multiple of 4\n", k); exit(1); } wt_open(); if (wt_lib.state != 1) { fprintf(stderr, "Gemm.i8w: %s\n", wt_lib.why); exit(1); } void* a = wt_cells(e, f[0], (uint64_t)n * k / 4, "A"); void* c = wt_cells(e, f[2], (uint64_t)n * m, "C"); int one = 1, zero = 0; int st = wt_lib.gemm(wt_lib.handle, 1, 0, (int)m, (int)n, (int)k, &one, (void*)(uintptr_t)wt_tab[id].ptr, 3, (int)k, a, 3, (int)k, &zero, c, 10, (int)m, 72, -1); if (st != 0) { fprintf(stderr, "Gemm.i8w: cublasGemmEx status %d\n", st); exit(1); } if (cuCtxSynchronize() != CUDA_SUCCESS) { fprintf(stderr, "Gemm.i8w: synchronize failed\n"); exit(1); } return io_tup(e, f[0], f[2]); #else fprintf(stderr, "Gemm.i8w needs a CUDA build\n"); exit(1); #endif } static void __attribute__((constructor)) wt_use(void) { #ifdef CID(Wt.load) io_eff(CID(Wt.load), wt_load_run); #endif #ifdef CID(Gemm.i8w) io_eff(CID(Gemm.i8w), gemm_i8w_run); #endif #ifdef CID(Wt.load_bf16) io_eff(CID(Wt.load_bf16), wt_load_bf16_run); #endif #ifdef CID(Wt.free) io_eff(CID(Wt.free), wt_free_run); #endif #ifdef CID(Wt.load_f16) io_eff(CID(Wt.load_f16), wt_load_f16_run); #endif #ifdef CID(Gemm.f32w) io_eff(CID(Gemm.f32w), gemm_f32w_run); #endif }