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 doorbell spin bound chosen per device at run time (#1397)

Niko1221a79e7225af66
diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt
index 4d79be350..0ed82cced 100644
--- a/sycl/CMakeLists.txt
+++ b/sycl/CMakeLists.txt
@@ -29,17 +29,13 @@ endif()
add_compile_definitions(STRATA_VERSION="${PROJECT_VERSION}")
set(STRATA_SYCL_AOT "" CACHE STRING "AOT device for the GPU code, e.g. bmg-g31 (empty: SPIR-V, JIT at run time)")
-# The device spins on host flags are bounded (include/strata/sycl_doorbell.hpp). The B-series keeps 20,000 reads; any
-# other build - the A-series JIT build - waits up to 2,000,000, which the host's per-layer CPU expert work needs there.
-set(STRATA_SYCL_SPIN_MAX "" CACHE STRING "bound of the device's spins on host flags, in reads (empty: 20000 for a bmg AOT build, else 2000000)")
-if(NOT STRATA_SYCL_SPIN_MAX)
- if(STRATA_SYCL_AOT MATCHES "^bmg")
- set(STRATA_SYCL_SPIN_MAX 20000)
- else()
- set(STRATA_SYCL_SPIN_MAX 2000000)
- endif()
+# The device spins on host flags are bounded (include/strata/sycl_doorbell.hpp). The bound is chosen per device at run
+# time (20,000 reads on a B-series card, 2,000,000 elsewhere; STRATA_SPIN_MAX overrides). STRATA_SYCL_SPIN_MAX fixes one
+# bound for every device in this build.
+set(STRATA_SYCL_SPIN_MAX "" CACHE STRING "fixed bound of the device's spins on host flags, in reads (empty: chosen per device at run time)")
+if(STRATA_SYCL_SPIN_MAX)
+ add_compile_definitions(STRATA_SYCL_SPIN_MAX=${STRATA_SYCL_SPIN_MAX}u)
endif()
-add_compile_definitions(STRATA_SYCL_SPIN_MAX=${STRATA_SYCL_SPIN_MAX}u)
set(_sycl_flags -fsycl)
if(STRATA_SYCL_AOT)
list(APPEND _sycl_flags -fsycl-targets=spir64_gen)
diff --git a/sycl/include/strata/sycl_doorbell.hpp b/sycl/include/strata/sycl_doorbell.hpp
index 574e3017a..3ad9f1c89 100644
--- a/sycl/include/strata/sycl_doorbell.hpp
+++ b/sycl/include/strata/sycl_doorbell.hpp
@@ -10,12 +10,17 @@
#include <sycl/sycl.hpp>
#include <sycl/ext/intel/experimental/cache_control_properties.hpp>
#include <cstdint>
+#include <cstdlib>
+#include <unordered_map>
+#include <mutex>
+#include <string>
+#include "strata/sycl_queue.hpp"
namespace strata {
using sys_atomic_u32 = sycl::atomic_ref<uint32_t, sycl::memory_order::relaxed, sycl::memory_scope::system>;
// A system-scope atomic load of host USM is still served from the GPU's cache on an Arc Pro B60 (NEO 26.31,
-// oneAPI 2026.1.1): a device spin never sees the host's store and runs to kSpinMax every time (bounded host/GPU
+// oneAPI 2026.1.1): a device spin never sees the host's store and runs to the spin bound every time (bounded host/GPU
// ping-pong: 263 ms per round trip, 168 of 200 waits hit the bound). An explicit uncached L1+L3 read hint goes to
// memory every time: 2.4 us per round trip, 0 of 200. The acquire fence keeps the compiler from hoisting the load
// out of a spin loop. -DSTRATA_DOORBELL_ATOMIC_LOAD restores the atomic load.
@@ -43,14 +48,30 @@ inline void sys_store(volatile uint32_t* p, uint32_t v) {
// xe driver times the queue out, resets the GT node by node (a window graph has 2,366 of them), and the card
// stays wedged until a reboot - measured twice. With a bound the failure is a wrong window instead, which the
// verifier's checks catch. ~2 M host-memory reads is a few seconds at PCIe latency.
-// The bound is the build's (CMake STRATA_SYCL_SPIN_MAX): 20,000 reads for the B-series (bmg) AOT builds, whose
-// device-plan windows only spin in a failure and where a longer spin is what makes the xe driver reset the GT; 2,000,000
-// (a few seconds) elsewhere. On an Arc A-series (i915) the CPU computes the experts the card does not hold and the GPU
-// waits for it at every layer: 20,000 reads is a few tens of milliseconds, the first request after a start (cold pages,
-// slow CPU layers) is slower than that, the GPU gave up, went on with the experts' outputs missing, and the answer was
-// token 0 ("!!!!!") or the engine crashed in the CPU pool on the garbage routing it read next.
-#ifndef STRATA_SYCL_SPIN_MAX
-#define STRATA_SYCL_SPIN_MAX 20000u
+// The bound is per device, chosen at run time (spin_max below): 20,000 reads on a card under the xe driver (the B-series
+// on Linux), whose device-plan windows only spin in a failure and where a longer spin is what makes the driver reset the
+// GT; 2,000,000 (a few seconds) elsewhere, Windows included (a JIT/OpenCL B-series build measured faster with the long
+// bound, #1397). On an Arc A-series (i915) the CPU computes the experts the card does not hold and the GPU waits for it at
+// every layer: 20,000 reads is a few tens of milliseconds, the first request after a start (cold pages, slow CPU
+// layers) is slower than that, the GPU gave up, went on with the experts' outputs missing, and the answer was token 0
+// ("!!!!!") or the engine crashed in the CPU pool on the garbage routing it read next.
+// STRATA_SPIN_MAX=<reads> overrides both; a build's -DSTRATA_SYCL_SPIN_MAX=<reads> (CMake) fixes one bound for every
+// device. The device functions take the bound as an argument: the launchers pass spin_max(queue).
+inline uint32_t spin_max(const sycl::queue& q) {
+ static std::mutex mu;
+ static std::unordered_map<sycl::device, uint32_t> cache;
+ const sycl::device d = q.get_device();
+ std::lock_guard<std::mutex> lk(mu);
+ auto it = cache.find(d);
+ if (it != cache.end()) return it->second;
+ uint32_t v;
+ if (const char* e = std::getenv("STRATA_SPIN_MAX"); e && std::atol(e) > 0) v = (uint32_t) std::atol(e);
+#ifdef STRATA_SYCL_SPIN_MAX
+ else v = STRATA_SYCL_SPIN_MAX;
+#else
+ else v = intel_gpu_driver() == "xe" ? 20000u : 2000000u;
#endif
-inline constexpr uint32_t kSpinMax = STRATA_SYCL_SPIN_MAX;
+ cache.emplace(d, v);
+ return v;
+}
} // namespace strata
diff --git a/sycl/src/kernels/cuda/elementwise.dp.cpp b/sycl/src/kernels/cuda/elementwise.dp.cpp
index 38ef29ef4..44815f33b 100644
--- a/sycl/src/kernels/cuda/elementwise.dp.cpp
+++ b/sycl/src/kernels/cuda/elementwise.dp.cpp
@@ -381,9 +381,9 @@ __dpct_inline__ void doorbell_ring_kernel(uint32_t *seq) {
}
__dpct_inline__ void doorbell_wait_kernel(const volatile uint32_t *flag,
- const volatile uint32_t *seq) {
+ const volatile uint32_t *seq, uint32_t spin_max) {
const uint32_t want = strata::sys_load(seq);
- for (uint32_t spin = 0; spin < strata::kSpinMax && strata::sys_load(flag) != want; ++spin) strata_spin_pause();
+ for (uint32_t spin = 0; spin < spin_max && strata::sys_load(flag) != want; ++spin) strata_spin_pause();
/*
DPCT1078: Consider replacing memory_order::acq_rel with
memory_order::seq_cst for correctness if strong memory order restrictions
@@ -394,6 +394,7 @@ __dpct_inline__ void doorbell_wait_kernel(const volatile uint32_t *flag,
void doorbell_wait(const uint32_t* d_flag, const uint32_t* d_seq, void* stream) {
if (d_flag == nullptr || d_seq == nullptr) return;
+ const uint32_t spin_max = strata::spin_max(*strata::q_of(stream));
{
auto exp_props = sycl::ext::oneapi::experimental::properties{
sycl::ext::oneapi::experimental::use_root_sync};
@@ -402,7 +403,7 @@ void doorbell_wait(const uint32_t* d_flag, const uint32_t* d_seq, void* stream)
->parallel_for<dpct_kernel_name<class doorbell_wait_kernel_47b360>>(
sycl::nd_range<3>(sycl::range(1, 1, 1), sycl::range(1, 1, 1)),
exp_props, [=](sycl::nd_item<3> item_ct1) {
- doorbell_wait_kernel(d_flag, d_seq);
+ doorbell_wait_kernel(d_flag, d_seq, spin_max);
});
}
check_launch("doorbell_wait");
diff --git a/sycl/src/kernels/cuda/verify_kernels.dp.cpp b/sycl/src/kernels/cuda/verify_kernels.dp.cpp
index 957b8d0e8..1a8c34e7c 100644
--- a/sycl/src/kernels/cuda/verify_kernels.dp.cpp
+++ b/sycl/src/kernels/cuda/verify_kernels.dp.cpp
@@ -2045,8 +2045,8 @@ void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const
namespace {
__dpct_inline__ void wait_flag_ge_kernel(const volatile uint32_t *flag,
- uint32_t value) {
- for (uint32_t spin = 0; spin < strata::kSpinMax && strata::sys_load(flag) < value; ++spin) strata_spin_pause();
+ uint32_t value, uint32_t spin_max) {
+ for (uint32_t spin = 0; spin < spin_max && strata::sys_load(flag) < value; ++spin) strata_spin_pause();
/*
DPCT1078: Consider replacing memory_order::acq_rel with
memory_order::seq_cst for correctness if strong memory order restrictions
@@ -2309,9 +2309,9 @@ auto &s_id = *sycl::ext::oneapi::group_local_memory_for_overwrite<int32_t[128]>(
}
__dpct_inline__ void wait_flag_ge_or_kernel(const volatile uint32_t *flag,
uint32_t value,
- const volatile uint32_t *skip) {
+ const volatile uint32_t *skip, uint32_t spin_max) {
if (strata::sys_load(skip) == value) return;
- for (uint32_t spin = 0; spin < strata::kSpinMax && strata::sys_load(flag) < value; ++spin) strata_spin_pause();
+ for (uint32_t spin = 0; spin < spin_max && strata::sys_load(flag) < value; ++spin) strata_spin_pause();
/*
DPCT1078: Consider replacing memory_order::acq_rel with
memory_order::seq_cst for correctness if strong memory order restrictions
@@ -2383,6 +2383,7 @@ void resident_plan(const int32_t* ids, int n_entries, int k, const int32_t* res_
check("resident_plan");
}
void wait_flag_ge_or(const uint32_t* flag, uint32_t value, const uint32_t* skip, void* stream) {
+ const uint32_t spin_max = strata::spin_max(*strata::q_of(stream));
{
auto exp_props = sycl::ext::oneapi::experimental::properties{
sycl::ext::oneapi::experimental::use_root_sync};
@@ -2392,7 +2393,7 @@ void wait_flag_ge_or(const uint32_t* flag, uint32_t value, const uint32_t* skip,
dpct_kernel_name<class wait_flag_ge_or_kernel_2b2de3>>(
sycl::nd_range<3>(sycl::range(1, 1, 1), sycl::range(1, 1, 1)),
exp_props, [=](sycl::nd_item<3> item_ct1) {
- wait_flag_ge_or_kernel(flag, value, skip);
+ wait_flag_ge_or_kernel(flag, value, skip, spin_max);
});
}
check("wait_flag_ge_or");
@@ -2440,6 +2441,7 @@ void copy_or_zero_from_mapped(float* dst, const float* src, long long n, const u
}
void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream) {
+ const uint32_t spin_max = strata::spin_max(*strata::q_of(stream));
{
auto exp_props = sycl::ext::oneapi::experimental::properties{
sycl::ext::oneapi::experimental::use_root_sync};
@@ -2448,7 +2450,7 @@ void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream) {
->parallel_for<dpct_kernel_name<class wait_flag_ge_kernel_d7debf>>(
sycl::nd_range<3>(sycl::range(1, 1, 1), sycl::range(1, 1, 1)),
exp_props, [=](sycl::nd_item<3> item_ct1) {
- wait_flag_ge_kernel(flag, value);
+ wait_flag_ge_kernel(flag, value, spin_max);
});
}
check("wait_flag_ge");