okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9adf4e01a
main
1// src/prefill/mmq_kq_loader.cpp - opens strata_kq (src/prefill/mmq_kq_lib.cu), the library with the K-quant MMQ prompt 2// kernels, from the engine's own folder - once, for the first model format that asks for it (moe_mmq.cu's supported()), 3// which the engine does before it sizes the expert cache (src/program/generate.cpp). Without the library, or with an 4// interface of another version, the K-quants keep the FP16 prompt path and the engine says so once. 5#include "strata/prefill/mmq_kq_abi.hpp" 6 7#include <cuda_runtime.h> 8 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <mutex> 13#include <string> 14 15#if defined(_WIN32 ) 16#ifndef NOMINMAX 17#define NOMINMAX 18#endif 19#define WIN32_LEAN_AND_MEAN 20#include <windows.h> 21#else 22#include <dlfcn.h> 23#include <unistd.h> 24#endif 25 26namespace strata::prefill::mmq::kq { 27namespace { 28 29struct Lib { 30bool ok = false; 31strata_kq_run_fn run = nullptr ; 32}; 33 34std::string own_folder () { 35#if defined(_WIN32 ) 36char p [MAX_PATH ]= {}; 37const DWORD n = GetModuleFileNameA (nullptr ,p ,MAX_PATH ); 38 std::string s (p ,n ); 39const size_t k = s .find_last_of ("\\/" ); 40return k == std::string::npos ? std::string () :s .substr (0 ,k + 1 ); 41#else 42char p [4096 ]= {}; 43const ssize_t n = readlink ("/proc/self/exe" ,p ,sizeof (p )- 1 ); 44 std::string s (p ,n > 0 ? (size_t )n :0 ); 45const size_t k = s .find_last_of ('/' ); 46return k == std::string::npos ? std::string () :s .substr (0 ,k + 1 ); 47#endif 48} 49 50void * sym (void * h ,const char * name ) { 51#if defined(_WIN32 ) 52return reinterpret_cast < void *> (GetProcAddress ((HMODULE )h ,name )); 53#else 54return dlsym (h ,name ); 55#endif 56} 57 58Lib open_lib () { 59Lib L ; 60const std::string path = own_folder ()+ STRATA_KQ_LIB_NAME ; 61#if defined(_WIN32 ) 62void * h = (void * )LoadLibraryA (path .c_str ()); 63#else 64void * h = dlopen (path .c_str (),RTLD_NOW |RTLD_LOCAL ); 65#endif 66if (h == nullptr ) { 67 std::fprintf (stderr ,"strata: K-quant prompt kernels (STRATA_MMQ_KQ=1): %s not found next to the engine - " 68"these experts take the FP16 prompt path\n" ,STRATA_KQ_LIB_NAME ); 69return L ; 70 } 71const auto abi = (strata_kq_abi_fn )sym (h ,"strata_kq_abi" ); 72const auto init = (strata_kq_init_fn )sym (h ,"strata_kq_init" ); 73L .run = (strata_kq_run_fn )sym (h ,"strata_kq_run" ); 74if (abi == nullptr || init == nullptr || L .run == nullptr || abi () /100000 != STRATA_KQ_ABI_VERSION || 75abi () %100000 != abi_args_size ()) { 76 std::fprintf (stderr ,"strata: K-quant prompt kernels: %s is from another engine version - these experts take " 77"the FP16 prompt path\n" ,path .c_str ()); 78L .run = nullptr ; 79return L ; 80 } 81// every GPU this engine sees: each context loads the library's kernels now, while the VRAM is still free 82int n = 0 ,cur = 0 ; 83cudaGetDevice (& cur ); 84if (cudaGetDeviceCount (& n )!= cudaSuccess )n = 0 ; 85for (int d = 0 ;d < n ;++ d ) { 86const int e = init (d ); 87if (e != 0 ) { 88 std::fprintf (stderr ,"strata: K-quant prompt kernels: %s did not start on GPU %d (CUDA error %d) - these " 89"experts take the FP16 prompt path\n" ,path .c_str (),d ,e ); 90L .run = nullptr ; 91cudaSetDevice (cur ); 92return L ; 93 } 94 } 95cudaSetDevice (cur ); 96 std::fprintf (stderr ,"strata: K-quant prompt kernels from %s (STRATA_MMQ_KQ=1)\n" ,path .c_str ()); 97L .ok = true; 98return L ; 99} 100 101const Lib & lib () { 102static const Lib L = open_lib (); 103return L ; 104} 105 106}// namespace 107 108bool wanted () { 109static const bool on = [] { 110const char * v = std::getenv ("STRATA_MMQ_KQ" ); 111return v != nullptr && v [0 ]== '1' ; 112 }(); 113return on ; 114} 115 116bool available () {return wanted ()&& lib ().ok ; } 117 118int run (int type ,const void * args ,void * stream ) { 119int dev = 0 ; 120cudaGetDevice (& dev ); 121return lib ().ok ?lib ().run (dev ,type ,args ,stream ) :-1 ; 122} 123 124}// namespace strata::prefill::mmq::kq