okami.mom/strata

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

Niko1221Merge branch 'f-138-fixB' into f-138-prs09817da39

main
5.7 KiB147 linesraw
1// src/prefill/ggml_cuda_host.cu - prompt-speed plan step 2b: the host-side symbols of llama.cpp's ggml-cuda that its MMQ
2// and quantize code reference, for the MMQ kernels compiled into strata_mmq without the rest of ggml-cuda.cu.
3#include "strata/core/emulate.hpp"
4#include "common.cuh"
5
6#include <cstdio>
7#include <cstdlib>
8#include <mutex>
9#include <string>
10#include <vector>
11
12[[noreturn]] void ggml_cuda_error(const char * stmt, const char * func, const char * file, int line, const char * msg) {
13    std::fprintf(stderr, "ggml-cuda (strata mmq): %s: %s\n  in %s at %s:%d\n", msg, stmt, func, file, line);
14    std::abort();
15}
16
17int ggml_cuda_get_device() {
18    int id = 0;
19    CUDA_CHECK(cudaGetDevice(&id));
20    return id;
21}
22
23#if defined(GGML_USE_HIP)
24namespace {
25
26unsigned parse_hex(const std::string& value) {
27    if (value.empty()) return 0;
28    char* end = nullptr;
29    const unsigned long parsed = std::strtoul(value.c_str(), &end, 16);
30    return end == value.c_str() || *end != '\0' ? 0 : static_cast<unsigned>(parsed);
31}
32
33// Match GGML's AMD cc encoding: OFFSET_AMD + gfx major * 0x100 + minor/stepping.
34// For example, gfx1100 maps to GGML_CUDA_CC_RDNA3 (0x01001100).
35int ggml_cuda_parse_amd_id(const char* device_name) {
36    if (device_name == nullptr) return GGML_CUDA_CC_OFFSET_AMD;
37
38    std::string arch(device_name);
39    if (arch.compare(0, 3, "gfx") == 0) arch.erase(0, 3);
40    const size_t suffix = arch.find(':');
41    if (suffix != std::string::npos) arch.resize(suffix);
42
43    const size_t generic = arch.rfind("-generic");
44    if (generic != std::string::npos && generic + 8 == arch.size()) arch.resize(generic);
45
46    unsigned major = 0;
47    unsigned minor = 0;
48    const size_t separator = arch.find('-');
49    if (separator != std::string::npos) {
50        major = parse_hex(arch.substr(0, separator));
51        minor = parse_hex(arch.substr(separator + 1)) * 0x10;
52    } else if (arch.size() >= 3) {
53        minor = parse_hex(arch.substr(arch.size() - 2));
54        major = parse_hex(arch.substr(0, arch.size() - 2));
55    }
56    return GGML_CUDA_CC_OFFSET_AMD + static_cast<int>(major * 0x100 + minor);
57}
58
59}  // namespace
60#endif
61
62const ggml_cuda_device_info & ggml_cuda_info() {
63    static ggml_cuda_device_info info = [] {
64        ggml_cuda_device_info in = {};
65        int n = 0;
66        if (cudaGetDeviceCount(&n) != cudaSuccess) n = 0;
67        n = n > GGML_CUDA_MAX_DEVICES ? GGML_CUDA_MAX_DEVICES : n;
68        in.device_count = n;
69        in.physical_device_count = n;
70        for (int id = 0; id < n; ++id) {
71            cudaDeviceProp prop;
72            CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
73            auto & d = in.devices[id];
74#if defined(GGML_USE_HIP)
75            d.cc = ggml_cuda_parse_amd_id(prop.gcnArchName);
76            if ((d.cc & 0xff00) == 0) {
77                // Fall back to HIP's device version fields if its architecture name is unavailable.
78                d.cc = GGML_CUDA_CC_OFFSET_AMD + prop.major * 0x100 + prop.minor * 0x10;
79            }
80            d.smpbo = prop.sharedMemPerBlock;
81            d.integrated = false;
82            d.supports_cooperative_launch = false;
83            d.nsm = prop.multiProcessorCount;
84            d.smpb = prop.sharedMemPerBlock;
85            d.warp_size = prop.warpSize;
86#else
87            // #542: the fields MMQ's choices read come from cudaDeviceGetAttribute, which is ABI-stable: a
88            // cudaDeviceProp filled by an older libcudart than the headers (a CUDA 13 build that links a CUDA 12
89            // libcudart.so) is shifted - sharedMemPerBlockOptin read 1 there, and every i-quant lost MMQ (#420's
90            // fits()).  In a matched build both give the same values.
91            auto attr = [id](cudaDeviceAttr a) {
92                int v = 0;
93                CUDA_CHECK(cudaDeviceGetAttribute(&v, a, id));
94                return v;
95            };
96            d.cc = 100 * strata::cc_major_of(attr(cudaDevAttrComputeCapabilityMajor)) +
97                   10 * strata::cc_minor_of(attr(cudaDevAttrComputeCapabilityMinor));   // STRATA_EMULATE_CC
98            d.smpbo = strata::smem_optin_of(attr(cudaDevAttrMaxSharedMemoryPerBlockOptin));
99            d.integrated = attr(cudaDevAttrIntegrated) != 0;
100            d.supports_cooperative_launch = attr(cudaDevAttrCooperativeLaunch) != 0;
101            d.nsm = attr(cudaDevAttrMultiProcessorCount);
102            d.smpb = (size_t) attr(cudaDevAttrMaxSharedMemoryPerBlock);
103            d.warp_size = attr(cudaDevAttrWarpSize);
104#endif
105            d.vmm = false;
106            d.total_vram = prop.totalGlobalMem;
107            d.physical_device = id;
108            d.physical_share_count = 1;
109            d.virtual_index = 0;
110        }
111        return in;
112    }();
113    return info;
114}
115
116namespace {
117// Buffers are kept and reused: MMQ asks for the same few sizes every launch (its stream-k fixup tiles).
118struct CachingPool : ggml_cuda_pool {
119    struct Buf { void * p; size_t size; bool used; };
120    std::vector<Buf> bufs;
121    std::mutex mu;
122    void * alloc(size_t size, size_t * actual_size) override {
123        std::lock_guard<std::mutex> lk(mu);
124        for (auto & b : bufs)
125            if (!b.used && b.size >= size) { b.used = true; *actual_size = b.size; return b.p; }
126        void * p = nullptr;
127        CUDA_CHECK(cudaMalloc(&p, size));
128        bufs.push_back({p, size, true});
129        *actual_size = size;
130        return p;
131    }
132    void free(void * ptr, size_t) override {
133        std::lock_guard<std::mutex> lk(mu);
134        for (auto & b : bufs)
135            if (b.p == ptr) { b.used = false; return; }
136    }
137    ~CachingPool() override {
138        for (auto & b : bufs) cudaFree(b.p);
139    }
140};
141}  // namespace
142
143std::unique_ptr<ggml_cuda_pool> ggml_backend_cuda_context::new_pool_for_device(int, int) {
144    return std::unique_ptr<ggml_cuda_pool>(new CachingPool());
145}
146
147ggml_backend_cuda_context::~ggml_backend_cuda_context() {}