okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-intel-quants into f-144-rel9adf4e01a

main
3.9 KiB124 linesraw
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 {
30    bool ok = false;
31    strata_kq_run_fn run = nullptr;
32};
33
34std::string own_folder() {
35#if defined(_WIN32)
36    char p[MAX_PATH] = {};
37    const DWORD n = GetModuleFileNameA(nullptr, p, MAX_PATH);
38    std::string s(p, n);
39    const size_t k = s.find_last_of("\\/");
40    return k == std::string::npos ? std::string() : s.substr(0, k + 1);
41#else
42    char p[4096] = {};
43    const ssize_t n = readlink("/proc/self/exe", p, sizeof(p) - 1);
44    std::string s(p, n > 0 ? (size_t) n : 0);
45    const size_t k = s.find_last_of('/');
46    return 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)
52    return reinterpret_cast<void*>(GetProcAddress((HMODULE) h, name));
53#else
54    return dlsym(h, name);
55#endif
56}
57
58Lib open_lib() {
59    Lib L;
60    const std::string path = own_folder() + STRATA_KQ_LIB_NAME;
61#if defined(_WIN32)
62    void* h = (void*) LoadLibraryA(path.c_str());
63#else
64    void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
65#endif
66    if (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);
69        return L;
70    }
71    const auto abi = (strata_kq_abi_fn) sym(h, "strata_kq_abi");
72    const auto init = (strata_kq_init_fn) sym(h, "strata_kq_init");
73    L.run = (strata_kq_run_fn) sym(h, "strata_kq_run");
74    if (abi == nullptr || init == nullptr || L.run == nullptr || abi() / 100000 != STRATA_KQ_ABI_VERSION ||
75        abi() % 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());
78        L.run = nullptr;
79        return L;
80    }
81    // every GPU this engine sees: each context loads the library's kernels now, while the VRAM is still free
82    int n = 0, cur = 0;
83    cudaGetDevice(&cur);
84    if (cudaGetDeviceCount(&n) != cudaSuccess) n = 0;
85    for (int d = 0; d < n; ++d) {
86        const int e = init(d);
87        if (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);
90            L.run = nullptr;
91            cudaSetDevice(cur);
92            return L;
93        }
94    }
95    cudaSetDevice(cur);
96    std::fprintf(stderr, "strata: K-quant prompt kernels from %s (STRATA_MMQ_KQ=1)\n", path.c_str());
97    L.ok = true;
98    return L;
99}
100
101const Lib& lib() {
102    static const Lib L = open_lib();
103    return L;
104}
105
106}  // namespace
107
108bool wanted() {
109    static const bool on = [] {
110        const char* v = std::getenv("STRATA_MMQ_KQ");
111        return v != nullptr && v[0] == '1';
112    }();
113    return on;
114}
115
116bool available() { return wanted() && lib().ok; }
117
118int run(int type, const void* args, void* stream) {
119    int dev = 0;
120    cudaGetDevice(&dev);
121    return lib().ok ? lib().run(dev, type, args, stream) : -1;
122}
123
124}  // namespace strata::prefill::mmq::kq