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: stage oneMKL GEMM operands that lie more than 4 GiB into their allocation

duncanmcqueen56efb70939df
diff --git a/sycl/include/strata/sycl_queue.hpp b/sycl/include/strata/sycl_queue.hpp
index 5728e8264..594ae8d15 100644
--- a/sycl/include/strata/sycl_queue.hpp
+++ b/sycl/include/strata/sycl_queue.hpp
@@ -170,13 +170,49 @@ inline bool arena_alias_guard_enabled() {
} // namespace strata
#include <vector>
+#include <map>
+#include <mutex>
namespace strata {
+// Every big device allocation (base -> size), so device_offset_end can find the allocation that holds an address. The
+// CPU expert pool's handshake and oneMKL's GEMM both care where, within its allocation, an operand lies.
+inline std::map<const void*, size_t>& device_alloc_map() {
+ static std::map<const void*, size_t> m;
+ return m;
+}
+inline std::mutex& device_alloc_mutex() {
+ static std::mutex m;
+ return m;
+}
+inline void device_alloc_register(const void* p, size_t bytes) {
+ if (p == nullptr || bytes == 0) return;
+ std::lock_guard<std::mutex> lk(device_alloc_mutex());
+ device_alloc_map()[p] = bytes;
+}
+// How far into its device allocation the byte range [p, p + bytes) ends; 0 when p is not in one we know. oneMKL's GEMM
+// reads the last, partial tile of an operand from the wrong place when it lies more than 4 GiB in (Arc A770, oneMKL
+// 2026.1), so the GEMM stages any operand this reports > 4 GiB for. An operand in an allocation we did not register
+// (small cudaMalloc'd buffers) is reported 0: those can never reach 4 GiB.
+inline size_t device_offset_end(const void* p, size_t bytes) {
+ if (p == nullptr) return 0;
+ std::lock_guard<std::mutex> lk(device_alloc_mutex());
+ auto& m = device_alloc_map();
+ auto it = m.upper_bound(p);
+ if (it == m.begin()) return 0;
+ --it;
+ const uint8_t* base = (const uint8_t*) it->first;
+ const uint8_t* end = (const uint8_t*) p + bytes;
+ if (end > base + it->second) return 0;
+ return (size_t) (end - base);
+}
// sycl::malloc_device for a big allocation, checked for aliased pages (arena_alias_check) and, when it has some, allocated
// again behind a growing spacer that moves it. Small allocations and STRATA_ARENA_ALIAS_CHECK=0 go straight to
// sycl::malloc_device. The memory comes back uninitialised, as from sycl::malloc_device.
inline void* malloc_device_guarded(size_t bytes, sycl::queue& q, const char* what = "device buffer") {
void* p = sycl::malloc_device(bytes, q);
- if (p == nullptr || bytes < ((size_t) 32 << 20) || !arena_alias_guard_enabled()) return p;
+ if (p == nullptr || bytes < ((size_t) 32 << 20) || !arena_alias_guard_enabled()) {
+ device_alloc_register(p, bytes);
+ return p;
+ }
std::vector<void*> spacers;
bool clean = false;
for (int attempt = 0; attempt < 8 && p != nullptr; ++attempt) {
@@ -199,6 +235,7 @@ inline void* malloc_device_guarded(size_t bytes, sycl::queue& q, const char* wha
for (void* sp : spacers) if (sp != nullptr) sycl::free(sp, q);
if (p != nullptr && !clean)
std::fprintf(stderr, "strata: WARNING: the %s still has aliased pages after 8 tries (STRATA_ARENA_ALIAS_CHECK=0 skips the check)\n", what);
+ device_alloc_register(p, bytes);
return p;
}
} // namespace strata
diff --git a/sycl/src/core/expert_cache.cpp b/sycl/src/core/expert_cache.cpp
index cc3a7809e..bdfede035 100644
--- a/sycl/src/core/expert_cache.cpp
+++ b/sycl/src/core/expert_cache.cpp
@@ -430,6 +430,9 @@ bool ExpertCache::open_segmented(uint64_t want, std::string &err) try {
}
mapped_segs_ = (int64_t) i + 1;
}
+ // the reserved VA, so device_offset_end can place a borrowed prompt buffer or a resident expert inside it (the GEMM
+ // stages an operand that lies more than 4 GiB in; oneMKL misreads such an operand's last partial tile)
+ strata::device_alloc_register(base_, (size_t) reserved_);
return true;
#endif
}
diff --git a/sycl/src/prefill/gemm.dp.cpp b/sycl/src/prefill/gemm.dp.cpp
index 110a5f90d..8673cb503 100644
--- a/sycl/src/prefill/gemm.dp.cpp
+++ b/sycl/src/prefill/gemm.dp.cpp
@@ -686,6 +686,52 @@ bool Gemm::bf16_hcd_exact(const uint16_t* X, int64_t ldx, const uint16_t* W, flo
#endif
}
+namespace {
+// oneMKL's GEMM reads the last, partial tile of an operand from the wrong place when it lies more than 4 GiB into its
+// allocation (Arc A770, oneMKL 2026.1: a BF16 B placed past 4 GiB of a 10.5 GiB allocation gave its last 21 columns
+// wrong, and none below 4 GiB). The prompt path borrows the top of the ~10 GiB expert cache for its buffers, so its
+// GEMMs met this and the prompt came out non-finite / garbage. An operand past 4 GiB is copied into a scratch buffer
+// of the queue's own - a fresh allocation, so at offset 0 - and the GEMM runs on that. Returns false, so the caller
+// runs the direct GEMM, when neither operand needs staging (the common, correct case).
+uint8_t* gemm_stage(size_t need, sycl::queue& q) {
+ static thread_local uint8_t* p = nullptr;
+ static thread_local size_t cap = 0;
+ if (need > cap) {
+ if (p != nullptr) sycl::free(p, q);
+ p = (uint8_t*) sycl::malloc_device(need, q);
+ cap = p != nullptr ? need : 0;
+ }
+ return p;
+}
+inline bool staged_gemm(void* handle, const uint16_t* W, int lda, const uint16_t* X, int ldb,
+ dpct::library_data_t dt, int64_t N, int64_t T, int64_t K, float alpha, float beta,
+ float* Y, int64_t ldy) {
+ static const bool no_stage = std::getenv("STRATA_SYCL_GEMM_NO_STAGE") != nullptr; // tests' negative control
+ constexpr size_t k4G = 4ull << 30;
+ const size_t es = 2;
+ const size_t w_bytes = (size_t) lda * (size_t) N * es; // W stored K x N, col-major (lda = K)
+ const size_t x_bytes = (size_t) ldb * (size_t) T * es; // X stored ldb x T
+ const bool far_w = strata::device_offset_end(W, w_bytes) > k4G;
+ const bool far_x = strata::device_offset_end(X, x_bytes) > k4G;
+ if (no_stage || (!far_w && !far_x)) return false;
+ sycl::queue& q = dpct::get_in_order_queue();
+ uint8_t* s = gemm_stage(w_bytes + x_bytes, q);
+ if (s == nullptr) return false; // no room to stage: fall back to the direct GEMM rather than drop the operand
+ uint8_t* sw = s;
+ uint8_t* sx = s + w_bytes;
+ if (far_w) q.memcpy(sw, W, w_bytes);
+ if (far_x) q.memcpy(sx, X, x_bytes);
+ const uint16_t* Wu = far_w ? (const uint16_t*) sw : W;
+ const uint16_t* Xu = far_x ? (const uint16_t*) sx : X;
+ ck(DPCT_CHECK_ERROR(dpct::blas::gemm(
+ (dpct::blas::descriptor_ptr) handle, oneapi::mkl::transpose::trans, oneapi::mkl::transpose::nontrans,
+ (int) N, (int) T, (int) K, &alpha, Wu, dt, lda, Xu, dt, ldb, &beta, Y,
+ dpct::library_data_t::real_float, (int) ldy, dpct::compute_type::f32)),
+ "cublasGemmEx (staged)");
+ return true;
+}
+} // namespace
+
void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy,
float beta, int64_t ldx) {
if (T <= 0 || N <= 0) return;
@@ -758,6 +804,9 @@ void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64
}
#endif
#endif
+ if (staged_gemm(handle_, W, (int) K, X, (int) (ldx ? ldx : K), dpct::library_data_t::real_bfloat16, N, T, K,
+ alpha, beta, Y, ldy))
+ return;
// Column-major view: Y^T[N, T] = W[N, K] (stored K x N col-major, transposed) . X^T[K, T].
ck(DPCT_CHECK_ERROR(dpct::blas::gemm(
(dpct::blas::descriptor_ptr)handle_, oneapi::mkl::transpose::trans,
@@ -796,6 +845,8 @@ void Gemm::f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_
return;
}
#endif
+ if (staged_gemm(handle_, W, (int) K, X, (int) K, dpct::library_data_t::real_half, N, T, K, alpha, beta, Y, ldy))
+ return;
ck(DPCT_CHECK_ERROR(dpct::blas::gemm(
(dpct::blas::descriptor_ptr)handle_, oneapi::mkl::transpose::trans,
oneapi::mkl::transpose::nontrans, (int)N, (int)T, (int)K, &alpha, W,