sycl: Prefill::release destroys only the queues the prompt path created, never the default in-order queue (#5405)

The prompt path holds two queues that start as the device's default in-order queue, which dpct owns: the K/V copy queue
(kv_copy; the code that would give it its own queue never runs, the pointer is never null) and the expert copy queue (copy,
replaced only partway through init()). release() destroyed both, which freed dpct's _q_in_order: every later
get_in_order_queue() ran on a freed queue - the frees in release() itself and at exit, and on a small card the whole
engine after init_prompt_paths() failed an allocation and retried with a smaller chunk (generate.cpp: sp.reset() on a
prompt path whose init() had not reached the copy queue yet).

Each queue now carries who owns it: kv_copy_owned / copy_owned are set only where the prompt path creates the queue, and
release() destroys only those, then clears both pointers. The card's copy-engine queue (make_copy_engine_queue, one per
device) is never destroyed here. Nothing else changes: the same queues do the same work.

PR #5405 (Andy Slater) found the kv_copy case and compared the pointer with the current device's default queue; this keeps
the ownership instead, which also covers copy and a layer-split stage released while another card is current.

Test: prefill_release_test (CTest; one SYCL device): reset() and destroy a Prefill, then make 8 queues (a freed default
queue's block would come back at its address) and copy and run a kernel on the default queue. Arc A750 (p1006,
p1006-20261116-1616-deb7): with the old release() it segfaults in urQueueFinish (exit 139), with this one it passes.
BASE-05 identity on the A750 from the shared setup: 9 of 9 prompts identical to the 0.1.59 baseline and to 756242554
(p1006-20261116-1616-deb7 + -1629-adbe).
This commit is contained in:
Niko1221 committed 2026-11-16 16:54:43 +01:00
1 parent 497ee681ce
commit f59c0da970
3 files changed
+102 -10

No files matched your search

+5
View File
@@ -494,6 +494,11 @@ if(STRATA_SYCL_PARITY)
add_executable(verify_refusal_test ${CMAKE_CURRENT_SOURCE_DIR}/src/core/verify_refusal_test.cpp) # a refused start (no VRAM expert tier) ends cleanly: the verifiers leave the default queue alone
target_link_libraries(verify_refusal_test PRIVATE strata_engine strata_prefill strata_spec)
add_test(NAME verify_refusal_test COMMAND verify_refusal_test) # needs one SYCL GPU
# #5405: release() leaves the device's default in-order queue alone (needs one SYCL device)
add_executable(prefill_release_test ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/prefill_release_test.cpp)
target_link_libraries(prefill_release_test PRIVATE strata_engine strata_prefill strata_spec)
add_test(NAME prefill_release_test COMMAND prefill_release_test)
set_tests_properties(prefill_release_test PROPERTIES SKIP_RETURN_CODE 77)
add_executable(dflash2_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/dflash2_bench.cpp) # DF2-SYCL: the DFlash2 Q4_0 drafter round on an Arc, kernel checks
target_link_libraries(dflash2_bench PRIVATE strata_kernels)
add_executable(dense_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/dense_bench.cpp) # the decode window's dense GEMVs, graph, hash
+29 -10
View File
@@ -769,6 +769,11 @@ struct Prefill::Impl {
dpct::queue_ptr cs = &dpct::get_in_order_queue(),
copy = &dpct::get_in_order_queue();
std::unique_ptr<sycl::queue> copy_without_profiling;
// #5405: `copy` is borrowed as well until init() sets it: the default queue above, or the device's copy-engine queue
// (strata::make_copy_engine_queue keeps that one for every prompt path of the card). release() destroys it only when
// init() created it (copy_owned); a prompt path whose init() failed before that point (the smaller-chunk retry,
// generate.cpp: sp.reset()) used to destroy the default queue here.
bool copy_owned = false;
Gemm gemm;
std::vector<void*> owned;
// chunk buffers
@@ -850,7 +855,12 @@ struct Prefill::Impl {
// KV streaming: one layer's whole K/V, staged from the host copy per layer and chunk (identity layout)
strata::kernels::KvHostPools stage;
int32_t* ident_table = nullptr;
// #5405: BORROWED until the prompt path makes a queue of its own (kv_copy_owned): it starts as the device's default
// in-order queue, which dpct owns. release() destroys only a queue this object created: destroying the default one freed
// dpct's _q_in_order, and every later get_in_order_queue() (the frees in release() itself, the next prompt path after
// a reset(), the frees at exit) ran on a freed queue.
dpct::queue_ptr kv_copy = &dpct::get_in_order_queue();
bool kv_copy_owned = false;
dpct::event_ptr kv_released = nullptr, kv_ready = nullptr;
// layer split: the device, and the hand-off to the next stage (two pinned chunk buffers, used in turn)
int device = -1;
@@ -1144,9 +1154,12 @@ void Prefill::release() {
if (impl_->copy_without_profiling) {
impl_->copy_without_profiling.reset();
impl_->copy = nullptr;
} else if (impl_->copy) dpct::get_current_device().destroy_queue(impl_->copy);
if (impl_->kv_copy) dpct::get_current_device().destroy_queue(
impl_->kv_copy);
} else if (impl_->copy && impl_->copy_owned) dpct::get_current_device().destroy_queue(impl_->copy); // #5405: never a borrowed one
impl_->copy = nullptr;
impl_->copy_owned = false;
if (impl_->kv_copy && impl_->kv_copy_owned) dpct::get_current_device().destroy_queue(impl_->kv_copy); // #5405: never a borrowed one
impl_->kv_copy = nullptr;
impl_->kv_copy_owned = false;
if (impl_->kv_released) dpct::destroy_event(impl_->kv_released);
if (impl_->kv_ready) dpct::destroy_event(impl_->kv_ready);
if (impl_->grp_host)
@@ -1352,9 +1365,11 @@ bool Prefill::init(const core::WeightTable &wt, const core::ModelGeometry &g,
m.copy_without_profiling = strata::make_prefill_copy_queue(*m.cs);
m.copy = m.copy_without_profiling.get();
}
if (m.copy == nullptr &&
DPCT_CHECK_ERROR(m.copy = dpct::get_current_device().create_queue(true)) != 0) {
err = "prefill: copy stream"; return false;
if (m.copy == nullptr) { // a queue of its own: release() destroys it (#5405)
if (DPCT_CHECK_ERROR(m.copy = dpct::get_current_device().create_queue(true)) != 0 || m.copy == nullptr) {
err = "prefill: copy stream"; return false;
}
m.copy_owned = true;
}
const size_t T = (size_t) chunk;
m.T_max = chunk;
@@ -2496,10 +2511,14 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,
DPCT1025: The SYCL queue is created ignoring the flag and priority
options.
*/
if ((!m.kv_copy &&
DPCT_CHECK_ERROR(
m.kv_copy = dpct::get_current_device().create_queue(true)) !=
0) ||
if (!m.kv_copy) { // a queue of its own: release() destroys it (#5405)
if (DPCT_CHECK_ERROR(m.kv_copy = dpct::get_current_device().create_queue(true)) != 0 || m.kv_copy == nullptr) {
err = "prefill: KV prefetch stream/events";
return false;
}
m.kv_copy_owned = true;
}
if (
(!m.kv_released &&
DPCT_CHECK_ERROR(m.kv_released = new sycl::event()) != 0) ||
(!m.kv_ready &&
+68
View File
@@ -0,0 +1,68 @@
// sycl/src/prefill/prefill_release_test.cpp - #5405: Prefill::release() must not destroy the device's default in-order queue.
//
// The prompt path's K/V copy queue starts as dpct's default in-order queue (borrowed). release() destroyed it, which freed
// dpct's _q_in_order, so every later get_in_order_queue() ran on a freed queue: the frees at exit, and on a small card the
// whole engine after init_prompt_paths() retried with a smaller chunk (generate.cpp: sp.reset()). This test builds a Prefill,
// resets it (release() on a fresh object) and destroys it (release() again), then:
// 1. makes 8 new dpct queues: a freed default queue's memory comes back as one of them at the same address (glibc hands a
// just-freed block of the same size out again), which is the use-after-free itself; a live default queue cannot be reused;
// 2. copies and runs a kernel on the default queue.
// Needs one SYCL device (the Arc, or a CPU device); exits 77 (skipped) without one.
#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include "strata/prefill/prefill.hpp"
#include <cstdio>
#include <vector>
int main() {
std::setvbuf(stdout, nullptr, _IONBF, 0);
try {
(void) dpct::get_current_device();
} catch (const std::exception& e) {
std::printf("prefill_release_test: no SYCL device (%s): skipped\n", e.what());
return 77;
}
sycl::queue* before = &dpct::get_in_order_queue();
std::printf("device: %s; default in-order queue at %p\n", before->get_device().get_info<sycl::info::device::name>().c_str(),
(void*) before);
{
strata::prefill::Prefill p;
std::printf("Prefill made; reset()\n");
p.reset(); // release() on a fresh prompt path, then a new one
std::printf("reset() done; destroying it\n");
} // and release() again in the destructor
std::printf("destroyed\n");
int fail = 0;
std::vector<sycl::queue*> fresh;
for (int i = 0; i < 8; ++i) fresh.push_back(dpct::get_current_device().create_queue(true));
for (sycl::queue* q : fresh)
if (q == before) {
std::printf(" a new queue took the default queue's address %p: release() freed the default queue\n", (void*) q);
++fail;
}
for (sycl::queue*& q : fresh) dpct::get_current_device().destroy_queue(q);
if (fail) { // the default queue is gone: do not use it
std::printf("prefill_release_test: FAILED\n");
return 1;
}
sycl::queue& q = dpct::get_in_order_queue();
if (&q != before) { std::printf(" the default queue moved (%p -> %p)\n", (void*) before, (void*) &q); ++fail; }
try {
const size_t n = 4096;
std::vector<int> host(n), back(n, -1);
for (size_t i = 0; i < n; ++i) host[i] = (int) (i * 7 + 3);
int* d = sycl::malloc_device<int>(n, q);
q.memcpy(d, host.data(), n * sizeof(int)).wait();
q.parallel_for(sycl::range<1>(n), [=](sycl::id<1> i) { d[i] += 1; }).wait();
q.memcpy(back.data(), d, n * sizeof(int)).wait();
sycl::free(d, q);
for (size_t i = 0; i < n; ++i)
if (back[i] != host[i] + 1) { ++fail; std::printf(" wrong value at %zu: %d\n", i, back[i]); break; }
} catch (const std::exception& e) {
std::printf(" the default queue failed after release(): %s\n", e.what());
++fail;
}
std::printf("prefill_release_test: %s\n", fail ? "FAILED" : "the default in-order queue is alive and works after release(): ok");
return fail ? 1 : 0;
}