okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9adf4e01a
main
1// src/prefill/mmq_kq_lib.cu - strata_kq, the shared library with llama.cpp's MMQ prompt kernels for the K-quants 2// (Q4_K, Q5_K, Q5_1, Q6_K: the experts of Unsloth's UD-Q4/Q5/Q6_K_XL files). MODEL-UPDATE Phase 1: the engine loads 3// every kernel of its own binary when it starts (CUDA_MODULE_LOADING=EAGER, src/program/generate.cpp), so kernels in 4// the engine cost every model VRAM; these four instances were ~3.8 MB of sm_60 code (3 expert slots of IQ3_XXS on a 5// P100). Here they load only when moe_mmq.cu opens this library - for a model with these experts, before the expert 6// cache is sized - and never otherwise. The library has its own copy of ggml_cuda_host.cu's symbols and of the static 7// CUDA runtime; it works in the engine's context on the stream it is given. 8#include "common.cuh" 9#include "mmq.cuh" 10 11#include "strata/prefill/mmq_kq_abi.hpp" 12 13#include <mutex> 14 15#if defined(_WIN32) 16#define STRATA_KQ_EXPORT extern "C" __declspec(dllexport) 17#else 18#define STRATA_KQ_EXPORT extern "C" __attribute__((visibility("default"))) 19#endif 20 21namespace { 22std::mutex g_mu; 23ggml_backend_cuda_context* g_ctx[GGML_CUDA_MAX_DEVICES] = {}; 24 25ggml_backend_cuda_context* ctx_for(int dev) { 26 std::lock_guard<std::mutex> lk(g_mu); 27 if (g_ctx[dev] == nullptr) g_ctx[dev] = new ggml_backend_cuda_context(dev); 28 return g_ctx[dev]; 29} 30} // namespace 31 32// the interface version and the argument block's size: both sides are compiled from the same mmq.cuh 33STRATA_KQ_EXPORT int strata_kq_abi() { return STRATA_KQ_ABI_VERSION * 100000 + (int) sizeof(mmq_args); } 34 35// Starts this library's CUDA runtime in the engine's context on `device`: with CUDA_MODULE_LOADING=EAGER (the engine 36// sets it before its first CUDA call) that loads every kernel here now, while the VRAM for it is still free. 37// 0 = ready, else the cudaError_t. 38STRATA_KQ_EXPORT int strata_kq_init(int device) { 39 cudaError_t e = cudaSetDevice(device); 40 if (e == cudaSuccess) e = cudaFree(nullptr); 41 return (int) e; 42} 43 44// One MMQ product (moe_mmq.cu's Context::run for these types) on `device`, on the engine's `stream`. 0 = launched, -1 = 45// a type this library does not have, else the cudaError_t of the launch. 46STRATA_KQ_EXPORT int strata_kq_run(int device, int type, const void* args, void* stream) { 47 if (device < 0 || device >= GGML_CUDA_MAX_DEVICES) return -1; 48 if (cudaSetDevice(device) != cudaSuccess) return (int) cudaGetLastError(); 49 ggml_backend_cuda_context& ctx = *ctx_for(device); 50 const mmq_args& a = *(const mmq_args*) args; 51 const cudaStream_t s = (cudaStream_t) stream; 52 switch ((ggml_type) type) { 53 case GGML_TYPE_Q4_K: mul_mat_q_case<GGML_TYPE_Q4_K>(ctx, a, s); break; 54 case GGML_TYPE_Q5_K: mul_mat_q_case<GGML_TYPE_Q5_K>(ctx, a, s); break; 55 case GGML_TYPE_Q5_1: mul_mat_q_case<GGML_TYPE_Q5_1>(ctx, a, s); break; 56 case GGML_TYPE_Q6_K: mul_mat_q_case<GGML_TYPE_Q6_K>(ctx, a, s); break; 57 default: return -1; 58 } 59 return (int) cudaGetLastError(); 60}