Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

SYCL: the prompt path's expert copies run on the GPU's copy engine (an immediate command list on the copy-only queue group, wrapped as a SYCL queue); the Level Zero v2 adapter ran them on the compute engine, ~13% of a B70 prompt. STRATA_COPY_ENGINE=0 turns it off

Niko122101fe68ddab92
diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt
index e9a6186f4..9c63332b8 100644
--- a/sycl/CMakeLists.txt
+++ b/sycl/CMakeLists.txt
@@ -224,7 +224,8 @@ strata_resolve(_engine src/core/layer.cpp src/core/session.cpp src/core/expert_s
src/core/arch_defaults.cpp src/core/vmm.cpp # vmm.cpp is the port's stub: no driver VMM on Level Zero
src/core/conversation_file.cpp
src/core/peer_experts.cpp src/core/remote_expert_opt_stub.cpp) # 0.1.36+: --peer-device (a stub in the port: no second GPU tier yet)
-add_library(strata_engine STATIC ${_engine} ${CMAKE_CURRENT_SOURCE_DIR}/src/core/gguf_expert_source.cpp)
+add_library(strata_engine STATIC ${_engine} ${CMAKE_CURRENT_SOURCE_DIR}/src/core/gguf_expert_source.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/core/copy_queue.cpp) # 0.1.43: a queue on the copy engine
target_include_directories(strata_engine PRIVATE ${STRATA_ROOT}/src/core) # conversation_checked.hpp (#include "...")
target_link_libraries(strata_engine PUBLIC strata_core strata_kernels strata_kernels_cpu)
diff --git a/sycl/include/strata/copy_queue.hpp b/sycl/include/strata/copy_queue.hpp
new file mode 100644
index 000000000..9223053b0
--- /dev/null
+++ b/sycl/include/strata/copy_queue.hpp
@@ -0,0 +1,24 @@
+// include/strata/copy_queue.hpp - SYCL port, 0.1.43: a queue on the GPU's dedicated copy (blitter) engine.
+//
+// WHY. With the Level Zero "v2" adapter (the default on Xe2 / Battlemage and newer), every SYCL queue, including a queue
+// that only holds host-to-device copies, runs on the compute engine: a 2 MB expert copy (~80 us at 25 GB/s) blocks the
+// kernels behind it for that long. A prompt chunk streams ~10,000 experts: ~0.8 s of the 3.8 s of a 4K prompt on an Arc
+// Pro B70 is the compute engine waiting for its own copies (unitrace: every H2D copy sits on "L0 Compute Engine<0,0>"
+// and none overlaps a kernel). The device does expose a copy-only queue group (zeDeviceGetCommandQueueGroupProperties:
+// group 1, flags COPY, 1 queue); the v2 adapter just does not use it for in-order queues.
+//
+// WHAT. make_copy_engine_queue() creates an immediate command list on that group and wraps it with
+// sycl::make_queue<ext_oneapi_level_zero>, so everything built on SYCL queues (memcpy, events, ext_oneapi_submit_barrier
+// across queues) keeps working, only the engine changes. It returns nullptr (the caller keeps an ordinary in-order queue)
+// when the backend is not Level Zero, the device has no copy-only group, the loader or the call fails, or
+// STRATA_COPY_ENGINE=0. Same bytes, same kernels: the tokens do not change.
+#pragma once
+#include <sycl/sycl.hpp>
+
+namespace strata {
+/// `what` names the first user in the one log line. One queue per process (an in-order queue, the same one for every
+/// caller: copies are serial on one engine anyway); never freed, so a caller must not delete it.
+sycl::queue* make_copy_engine_queue(const char* what);
+/// true once a copy-engine queue has been made in this process
+bool copy_engine_active();
+} // namespace strata
diff --git a/sycl/src/core/copy_queue.cpp b/sycl/src/core/copy_queue.cpp
new file mode 100644
index 000000000..e292996da
--- /dev/null
+++ b/sycl/src/core/copy_queue.cpp
@@ -0,0 +1,121 @@
+// src/core/copy_queue.cpp - see include/strata/copy_queue.hpp.
+#include "strata/copy_queue.hpp"
+
+#include <dpct/dpct.hpp>
+#include <sycl/ext/oneapi/backend/level_zero.hpp>
+
+#if defined(__has_include)
+#if __has_include(<level_zero/ze_api.h>)
+#include <level_zero/ze_api.h>
+#define STRATA_HAVE_ZE_API 1
+#endif
+#endif
+
+#include <atomic>
+#include <mutex>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <vector>
+
+#ifdef STRATA_HAVE_ZE_API
+#if defined(_WIN32)
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
+#endif
+
+namespace strata {
+namespace {
+std::atomic<bool> g_active{false};
+
+#ifdef STRATA_HAVE_ZE_API
+struct ZeFns {
+ decltype(&zeDeviceGetCommandQueueGroupProperties) get_groups = nullptr;
+ decltype(&zeCommandListCreateImmediate) create_imm = nullptr;
+};
+bool load_ze(ZeFns& f) {
+#if defined(_WIN32)
+ HMODULE h = LoadLibraryA("ze_loader.dll");
+ if (!h) return false;
+ auto sym = [&](const char* n) { return (void*) GetProcAddress(h, n); };
+#else
+ void* h = dlopen("libze_loader.so.1", RTLD_NOW | RTLD_NOLOAD); // already loaded by the SYCL runtime
+ if (!h) h = dlopen("libze_loader.so.1", RTLD_NOW);
+ if (!h) return false;
+ auto sym = [&](const char* n) { return dlsym(h, n); };
+#endif
+ f.get_groups = (decltype(f.get_groups)) sym("zeDeviceGetCommandQueueGroupProperties");
+ f.create_imm = (decltype(f.create_imm)) sym("zeCommandListCreateImmediate");
+ return f.get_groups && f.create_imm;
+}
+#endif
+} // namespace
+
+bool copy_engine_active() { return g_active.load(); }
+
+sycl::queue* make_copy_engine_queue(const char* what) {
+ static const bool off = [] { const char* v = std::getenv("STRATA_COPY_ENGINE"); return v && v[0] == '0'; }();
+ if (off) return nullptr;
+#ifndef STRATA_HAVE_ZE_API
+ (void) what;
+ return nullptr;
+#else
+ static std::mutex mu;
+ static sycl::queue* made = nullptr;
+ static bool tried = false;
+ std::lock_guard<std::mutex> lk(mu);
+ if (tried) return made;
+ tried = true;
+ try {
+ sycl::device dev = dpct::get_current_device();
+ if (dev.get_backend() != sycl::backend::ext_oneapi_level_zero) return nullptr;
+ // The legacy Level Zero adapter already puts memcpy on the copy engine (and its kernel launches are slower); only
+ // the v2 adapter needs this. SYCL_UR_USE_LEVEL_ZERO_V2=0 selects the legacy one.
+ const char* v2 = std::getenv("SYCL_UR_USE_LEVEL_ZERO_V2");
+ if (v2 && v2[0] == '0') return nullptr;
+ ZeFns zf;
+ if (!load_ze(zf)) return nullptr;
+ sycl::context ctx = dpct::get_default_context();
+ auto zctx = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(ctx);
+ auto zdev = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(dev);
+ uint32_t ng = 0;
+ if (zf.get_groups(zdev, &ng, nullptr) != ZE_RESULT_SUCCESS || ng == 0) return nullptr;
+ std::vector<ze_command_queue_group_properties_t> g(ng);
+ for (auto& x : g) { std::memset(&x, 0, sizeof(x)); x.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_GROUP_PROPERTIES; }
+ if (zf.get_groups(zdev, &ng, g.data()) != ZE_RESULT_SUCCESS) return nullptr;
+ int ord = -1;
+ for (uint32_t i = 0; i < ng; ++i)
+ if ((g[i].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COPY) &&
+ !(g[i].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COMPUTE) && g[i].numQueues > 0) { ord = (int) i; break; }
+ if (ord < 0) return nullptr;
+ ze_command_queue_desc_t d;
+ std::memset(&d, 0, sizeof(d));
+ d.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
+ d.ordinal = (uint32_t) ord;
+ d.index = 0;
+ d.flags = ZE_COMMAND_QUEUE_FLAG_IN_ORDER;
+ d.mode = ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS;
+ d.priority = ZE_COMMAND_QUEUE_PRIORITY_NORMAL;
+ ze_command_list_handle_t cl = nullptr;
+ if (zf.create_imm(zctx, zdev, &d, &cl) != ZE_RESULT_SUCCESS || cl == nullptr) return nullptr;
+ sycl::backend_input_t<sycl::backend::ext_oneapi_level_zero, sycl::queue> in(
+ cl, dev, sycl::ext::oneapi::level_zero::ownership::keep,
+ sycl::property_list{sycl::property::queue::in_order{}});
+ auto* q = new sycl::queue(sycl::make_queue<sycl::backend::ext_oneapi_level_zero>(in, ctx));
+ made = q;
+ g_active.store(true);
+ static std::atomic<bool> said{false};
+ if (!said.exchange(true))
+ std::fprintf(stderr, "strata: copy engine: %s copies run on the GPU's copy engine (queue group %d), not the "
+ "compute engine (STRATA_COPY_ENGINE=0 turns it off)\n", what, ord);
+ return q;
+ } catch (const std::exception& e) {
+ std::fprintf(stderr, "strata: copy engine queue not available (%s): ordinary queue\n", e.what());
+ return nullptr;
+ }
+#endif
+}
+
+} // namespace strata
diff --git a/sycl/src/prefill/prefill.cpp b/sycl/src/prefill/prefill.cpp
index 1e35e68d3..87e9e0258 100644
--- a/sycl/src/prefill/prefill.cpp
+++ b/sycl/src/prefill/prefill.cpp
@@ -3,6 +3,7 @@
#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
+#include "strata/copy_queue.hpp"
#include "strata/prefill/xmx_moe.hpp"
#include "strata/prefill/prefill.hpp"
#include "strata/core/gguf_expert_source.hpp"
@@ -971,8 +972,12 @@ bool Prefill::init(const core::WeightTable &wt, const core::ModelGeometry &g,
DPCT1025: The SYCL queue is created ignoring the flag and priority
options.
*/
- if (DPCT_CHECK_ERROR(
- m.copy = dpct::get_current_device().create_queue(true)) != 0) {
+ // 0.1.43: the expert copies on the GPU's copy engine when the device has one the SYCL runtime does not use itself
+ // (the Level Zero v2 adapter runs every queue on the compute engine: ~0.8 s of a 4K prompt on a B70 is the compute
+ // engine waiting for its own DMA). nullptr: the ordinary in-order queue. STRATA_COPY_ENGINE=0 turns it off.
+ m.copy = strata::make_copy_engine_queue("the prompt path's expert");
+ if (m.copy == nullptr &&
+ DPCT_CHECK_ERROR(m.copy = dpct::get_current_device().create_queue(true)) != 0) {
err = "prefill: copy stream"; return false;
}
const size_t T = (size_t) chunk;