sycl: fix the GEMM staging scratch lifetime and unregister freed allocations

Follow-up to the >4 GiB GEMM staging.

- gemm_stage freed its scratch as soon as a larger operand arrived, but the
  in-order queue is asynchronous: a previous staged GEMM could still read the
  buffer.  Wait for the queue before the free.
- The staging used dpct::get_in_order_queue() while the GEMM runs on the
  descriptor's queue (strata::q_of(stream_)); pass the descriptor's queue so
  the copy is ordered before the GEMM for a non-default stream, and so two
  Gemm objects on different queues do not share the thread_local scratch.
- Staging that could not allocate fell silently back to the direct (wrong)
  GEMM; it now says so.
- device_alloc_map had no unregister, so a freed cache base left a stale
  entry that device_offset_end could mis-attribute after the VA was reused.
  Add device_alloc_unregister and call it where the expert cache frees base_.
This commit is contained in:
duncanmcqueen authored and Niko1221 committed 2026-10-09 03:06:31 +02:00
1 parent edff903d7a
commit 0b063ce804
3 files changed
+23 -6

No files matched your search

+7
View File
@@ -188,6 +188,13 @@ inline void device_alloc_register(const void* p, size_t bytes) {
std::lock_guard<std::mutex> lk(device_alloc_mutex());
device_alloc_map()[p] = bytes;
}
// Drop a base when its allocation is freed, so a later allocation that reuses the address is not mis-attributed by
// device_offset_end.
inline void device_alloc_unregister(const void* p) {
if (p == nullptr) return;
std::lock_guard<std::mutex> lk(device_alloc_mutex());
device_alloc_map().erase(p);
}
// 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
+3
View File
@@ -459,6 +459,7 @@ void ExpertCache::release_segmented() {
seg_size_.clear();
mapped_segs_ = 0;
reserved_ = 0;
strata::device_alloc_unregister(base_);
base_ = nullptr;
}
@@ -748,8 +749,10 @@ void ExpertCache::close() {
release_segmented();
} else if (vmm_) {
vmm_.reset(); // unmaps and frees every chunk it still holds
strata::device_alloc_unregister(base_);
base_ = nullptr;
} else if (base_ != nullptr) {
strata::device_alloc_unregister(base_);
sycl::free(base_, dpct::get_in_order_queue());
base_ = nullptr;
}
+13 -6
View File
@@ -697,13 +697,16 @@ 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);
if (p != nullptr) {
q.wait(); // the queue is in-order but asynchronous: a previous staged GEMM may still read p
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,
inline bool staged_gemm(sycl::queue& q, 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
@@ -714,9 +717,13 @@ inline bool staged_gemm(void* handle, const uint16_t* W, int lda, const uint16_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
if (s == nullptr) {
std::fprintf(stderr, "strata: WARNING: a GEMM operand lies past 4 GiB and the %zu MiB staging buffer could not be "
"allocated; the GEMM runs directly and may be wrong on this card\n",
(w_bytes + x_bytes) >> 20);
return false;
}
uint8_t* sw = s;
uint8_t* sx = s + w_bytes;
if (far_w) q.memcpy(sw, W, w_bytes);
@@ -804,7 +811,7 @@ 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,
if (staged_gemm(*strata::q_of(stream_), 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].
@@ -845,7 +852,7 @@ 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))
if (staged_gemm(*strata::q_of(stream_), 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,