okami.mom/strata

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

Niko1221Merge f-143-intel: B70 prompt and decode speed (copy engine, wide kv_append, no GEMM staging on bmg, lend mirror, fused dequant, lean verify graph, the xe expert ranking); same output tokens, B70 4K prompt 8.5 -> 3.3 s (128K config), decode +14-21% on prompts outside the ranking's corpus2eba71548

main
5.9 KiB142 linesraw
1// src/core/copy_queue.cpp - see include/strata/copy_queue.hpp.
2#include "strata/copy_queue.hpp"
3#include "strata/sycl_queue.hpp"
4
5#include <dpct/dpct.hpp>
6#include <sycl/ext/oneapi/backend/level_zero.hpp>
7
8#if defined(__has_include)
9#if __has_include(<level_zero/ze_api.h>)
10#include <level_zero/ze_api.h>
11#define STRATA_HAVE_ZE_API 1
12#endif
13#endif
14
15#include <atomic>
16#include <mutex>
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <vector>
21
22#ifdef STRATA_HAVE_ZE_API
23#if defined(_WIN32)
24#include <windows.h>
25#else
26#include <dlfcn.h>
27#endif
28#endif
29
30namespace strata {
31namespace {
32std::atomic<bool> g_active{false};
33
34#ifdef STRATA_HAVE_ZE_API
35struct ZeFns {
36    decltype(&zeDeviceGetCommandQueueGroupProperties) get_groups = nullptr;
37    decltype(&zeCommandListCreateImmediate) create_imm = nullptr;
38};
39bool load_ze(ZeFns& f) {
40#if defined(_WIN32)
41    HMODULE h = LoadLibraryA("ze_loader.dll");
42    if (!h) return false;
43    auto sym = [&](const char* n) { return (void*) GetProcAddress(h, n); };
44#else
45    void* h = dlopen("libze_loader.so.1", RTLD_NOW | RTLD_NOLOAD);   // already loaded by the SYCL runtime
46    if (!h) h = dlopen("libze_loader.so.1", RTLD_NOW);
47    if (!h) return false;
48    auto sym = [&](const char* n) { return dlsym(h, n); };
49#endif
50    f.get_groups = (decltype(f.get_groups)) sym("zeDeviceGetCommandQueueGroupProperties");
51    f.create_imm = (decltype(f.create_imm)) sym("zeCommandListCreateImmediate");
52    return f.get_groups && f.create_imm;
53}
54#endif
55}  // namespace
56
57bool copy_engine_active() { return g_active.load(); }
58
59namespace {
60// one copy-engine queue per device (a layer split calls this once per card): device -> queue; a device that has none maps to nullptr
61struct DevQ { sycl::device dev; sycl::queue* q; };
62std::vector<DevQ>& dev_queues() { static std::vector<DevQ> v; return v; }
63std::mutex& dev_mu() { static std::mutex m; return m; }
64}  // namespace
65bool is_copy_engine_queue(const sycl::queue* q) {
66    if (q == nullptr) return false;
67    std::lock_guard<std::mutex> lk(dev_mu());
68    for (const DevQ& d : dev_queues()) if (d.q == q) return true;
69    return false;
70}
71
72
73sycl::queue* make_copy_engine_queue(const char* what) {
74    static const bool off = [] { const char* v = std::getenv("STRATA_COPY_ENGINE"); return v && v[0] == '0'; }();
75    if (off) return nullptr;
76#ifndef STRATA_HAVE_ZE_API
77    (void) what;
78    return nullptr;
79#else
80    std::lock_guard<std::mutex> lk(dev_mu());
81    sycl::device dev = dpct::get_current_device();
82    for (const DevQ& d : dev_queues()) if (d.dev == dev) return d.q;   // asked before for this card (nullptr: it has none)
83    dev_queues().push_back({dev, nullptr});
84    const size_t slot = dev_queues().size() - 1;
85    try {
86
87        if (dev.get_backend() != sycl::backend::ext_oneapi_level_zero) return nullptr;
88        // Battlemage only unless STRATA_COPY_ENGINE=1: on an Arc A750 (i915, JIT) the prompt path hung with it once the stager's
89        // completion flag became a copy-engine memcpy (4K and 600-token prompts, 10+ minutes, GPU not hung), and the measured gain
90        // there was 1.4% (a prompt there is compute bound).
91        {
92            const char* force = std::getenv("STRATA_COPY_ENGINE");
93            if (!(force && force[0] == '1') && strata::intel_gpu_gen(dev) != strata::IntelGpuGen::Battlemage) return nullptr;
94        }
95        // The legacy Level Zero adapter already puts memcpy on the copy engine (and its kernel launches are slower); only
96        // the v2 adapter needs this.  SYCL_UR_USE_LEVEL_ZERO_V2=0 selects the legacy one.
97        const char* v2 = std::getenv("SYCL_UR_USE_LEVEL_ZERO_V2");
98        if (v2 && v2[0] == '0') return nullptr;
99        ZeFns zf;
100        if (!load_ze(zf)) return nullptr;
101        sycl::context ctx = dpct::get_default_context();
102        auto zctx = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(ctx);
103        auto zdev = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(dev);
104        uint32_t ng = 0;
105        if (zf.get_groups(zdev, &ng, nullptr) != ZE_RESULT_SUCCESS || ng == 0) return nullptr;
106        std::vector<ze_command_queue_group_properties_t> g(ng);
107        for (auto& x : g) { std::memset(&x, 0, sizeof(x)); x.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_GROUP_PROPERTIES; }
108        if (zf.get_groups(zdev, &ng, g.data()) != ZE_RESULT_SUCCESS) return nullptr;
109        int ord = -1;
110        for (uint32_t i = 0; i < ng; ++i)
111            if ((g[i].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COPY) &&
112                !(g[i].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COMPUTE) && g[i].numQueues > 0) { ord = (int) i; break; }
113        if (ord < 0) return nullptr;
114        ze_command_queue_desc_t d;
115        std::memset(&d, 0, sizeof(d));
116        d.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
117        d.ordinal = (uint32_t) ord;
118        d.index = 0;
119        d.flags = ZE_COMMAND_QUEUE_FLAG_IN_ORDER;
120        d.mode = ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS;
121        d.priority = ZE_COMMAND_QUEUE_PRIORITY_NORMAL;
122        ze_command_list_handle_t cl = nullptr;
123        if (zf.create_imm(zctx, zdev, &d, &cl) != ZE_RESULT_SUCCESS || cl == nullptr) return nullptr;
124        sycl::backend_input_t<sycl::backend::ext_oneapi_level_zero, sycl::queue> in(
125            cl, dev, sycl::ext::oneapi::level_zero::ownership::keep,
126            sycl::property_list{sycl::property::queue::in_order{}});
127        auto* q = new sycl::queue(sycl::make_queue<sycl::backend::ext_oneapi_level_zero>(in, ctx));
128        dev_queues()[slot].q = q;
129        g_active.store(true);
130        static std::atomic<bool> said{false};
131        if (!said.exchange(true))
132            std::fprintf(stderr, "strata: copy engine: %s copies run on the GPU's copy engine (queue group %d), not the "
133                                 "compute engine (STRATA_COPY_ENGINE=0 turns it off)\n", what, ord);
134        return q;
135    } catch (const std::exception& e) {
136        std::fprintf(stderr, "strata: copy engine queue not available (%s): ordinary queue\n", e.what());
137        return nullptr;
138    }
139#endif
140}
141
142}  // namespace strata