okami.mom/strata

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

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
3.9 KiB104 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 "common.cuh"
4
5#define DPCT_PROFILING_ENABLED
6#include <sycl/sycl.hpp>
7#include <dpct/dpct.hpp>
8#include <cstdio>
9#include <cstdlib>
10#include <mutex>
11#include <vector>
12
13[[noreturn]] void ggml_cuda_error(const char * stmt, const char * func, const char * file, int line, const char * msg) {
14    std::fprintf(stderr, "ggml-cuda (strata mmq): %s: %s\n  in %s at %s:%d\n", msg, stmt, func, file, line);
15    std::abort();
16}
17
18int ggml_cuda_get_device() {
19    int id = 0;
20    CUDA_CHECK(DPCT_CHECK_ERROR(id = dpct::get_current_device_id()));
21    return id;
22}
23
24const ggml_cuda_device_info & ggml_cuda_info() {
25    static ggml_cuda_device_info info = [] {
26        ggml_cuda_device_info in = {};
27        int n = 0;
28        if (DPCT_CHECK_ERROR(n = dpct::device_count()) != 0) n = 0;
29        n = n > GGML_CUDA_MAX_DEVICES ? GGML_CUDA_MAX_DEVICES : n;
30        in.device_count = n;
31        in.physical_device_count = n;
32        for (int id = 0; id < n; ++id) {
33            dpct::device_info prop;
34            CUDA_CHECK(
35                DPCT_CHECK_ERROR(dpct::get_device(id).get_device_info(prop)));
36            auto & d = in.devices[id];
37            /*
38            DPCT1005: The SYCL device version is different from CUDA Compute
39            Compatibility. You may need to rewrite this code.
40            */
41            d.cc =
42                100 * prop.get_major_version() + 10 * prop.get_minor_version();
43            d.nsm(prop.set_max_compute_units);
44            /*
45            DPCT1019: local_mem_size in SYCL is not a complete equivalent of
46            sharedMemPerBlock in CUDA. You may need to adjust the code.
47            */
48            d.smpb(prop.set_local_mem_size);
49            /*
50            DPCT1019: local_mem_size in SYCL is not a complete equivalent of
51            sharedMemPerBlockOptin in CUDA. You may need to adjust the code.
52            */
53            d.smpbo(prop.set_local_mem_size);
54            /*
55            DPCT1006: SYCL does not provide a standard API to differentiate
56            between integrated and discrete GPU devices.
57            */
58            d.integrated = prop.get_integrated() != 0;
59            d.vmm = false;
60            d.total_vram(prop.set_global_mem_size);
61            d.warp_size(prop.set_max_sub_group_size);
62            d.supports_cooperative_launch = prop.cooperativeLaunch != 0;
63            d.physical_device = id;
64            d.physical_share_count = 1;
65            d.virtual_index = 0;
66        }
67        return in;
68    }();
69    return info;
70}
71
72namespace {
73// Buffers are kept and reused: MMQ asks for the same few sizes every launch (its stream-k fixup tiles).
74struct CachingPool : ggml_cuda_pool {
75    struct Buf { void * p; size_t size; bool used; };
76    std::vector<Buf> bufs;
77    std::mutex mu;
78    void * alloc(size_t size, size_t * actual_size) override {
79        std::lock_guard<std::mutex> lk(mu);
80        for (auto & b : bufs)
81            if (!b.used && b.size >= size) { b.used = true; *actual_size = b.size; return b.p; }
82        void * p = nullptr;
83        CUDA_CHECK(DPCT_CHECK_ERROR(
84            p = (void *)sycl::malloc_device(size, dpct::get_in_order_queue())));
85        bufs.push_back({p, size, true});
86        *actual_size = size;
87        return p;
88    }
89    void free(void * ptr, size_t) override {
90        std::lock_guard<std::mutex> lk(mu);
91        for (auto & b : bufs)
92            if (b.p == ptr) { b.used = false; return; }
93    }
94    ~CachingPool() override {
95        for (auto & b : bufs) cudaFree(b.p);
96    }
97};
98}  // namespace
99
100std::unique_ptr<ggml_cuda_pool> ggml_backend_cuda_context::new_pool_for_device(int, int) {
101    return std::unique_ptr<ggml_cuda_pool>(new CachingPool());
102}
103
104ggml_backend_cuda_context::~ggml_backend_cuda_context() {}