mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 23:38:47 +00:00
sycl: a GPU wait on the host that runs out fails its window instead of using stale expert rows (#5160, #5992, #6016)
Every device wait on a host flag (the plan, the PCIe share, the CPU rows; the single-token doorbell) is bounded so an xe card is never wedged, but a wait that ran out simply returned: the GPU went on with the previous layer's plan and rows (a wrong token, "!!!!" from then on, #5160) and rang the next step over a payload the CPU pool was still reading (#6016's crash at 262144). The window still counted as good and was committed. Now a wait that runs out writes a record, and the record is the window's own: - strata/core/window_wait.hpp: the wait (one template, run by the kernels with uncached system loads / stores and by the host fixture with atomics) and a 4-word record: site, step, epoch, count. The first expiry keeps its site and step. - One record per Verifier (mapped next to its flags) and per token graph, never per device: pipelined parity verifiers, layer-split stages and batch windows each read only their own. stage_inputs() / stage_batch() arm it with a new epoch where they reset the flags, i.e. after the previous window completed. - The host reads it after every CPU step (before flag M) and after the window's sync, in run(), run_slot_rows(), batch_poll(), service() / pl_finish() and session_run_token(). A written record fails the window: its tokens are never returned or committed; after a mid-window find the host raises the window's flags and drains its queues (5 s; a verifier that does not drain takes no further windows, as after #267). The engine log line names the wait, the layer and group, the window, its position and size, the device and layer range, how long the CPU took and the bound. The token graph stops computing once its record is written and only answers the doorbell. - No warn-only mode. The bounds are unchanged here (the xe bound commit comes before this one). - Test build only (-DSTRATA_SYCL_TEST_HOOKS=ON): STRATA_TEST_POOL_DELAY=W:K:MS delays one CPU step, for the card's fault-injection check; released engines are built without it. - sycl/src/core/window_wait_test.cpp (host only, CTest): on time, a delayed worker past the bound, within it, cancel and slot reuse, a late expiry after the host's check, a slow and a dead host, three windows at once with their own sites and steps, the record helpers. --legacy replays the old protocol and must fail (8 checks fail there).
This commit is contained in:
1 parent
6ba86f9618
commit
bba1a0e92a
9 files changed
+556
-19
No files matched your search
@@ -56,6 +56,12 @@ endif()
|
||||
# time (20,000 reads on a B-series card, 1,000,000 when the CPU serves experts and the host is waited for, 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)")
|
||||
# A test build only: STRATA_TEST_POOL_DELAY=W:K:MS delays one CPU step of one verify window (verify.cpp, test_pool_delay),
|
||||
# for the fault-injection check of #5160. Released engines are built without it and have no such switch.
|
||||
option(STRATA_SYCL_TEST_HOOKS "compile the fault-injection test hooks (never in a release build)" OFF)
|
||||
if(STRATA_SYCL_TEST_HOOKS)
|
||||
add_compile_definitions(STRATA_SYCL_TEST_HOOKS=1)
|
||||
endif()
|
||||
if(STRATA_SYCL_SPIN_MAX)
|
||||
add_compile_definitions(STRATA_SYCL_SPIN_MAX=${STRATA_SYCL_SPIN_MAX}u)
|
||||
endif()
|
||||
@@ -320,6 +326,15 @@ if(STRATA_SYCL_PARITY)
|
||||
add_test(NAME window_jobs_test COMMAND window_jobs_test)
|
||||
add_test(NAME window_jobs_test_legacy COMMAND window_jobs_test --legacy) # the 756242554 loop: must fail
|
||||
set_tests_properties(window_jobs_test_legacy PROPERTIES WILL_FAIL TRUE)
|
||||
# Host-only: a GPU wait on the host that runs out fails its window (window_wait.hpp; #5160). --legacy replays the old
|
||||
# protocol (a wait that runs out just returns) and must fail.
|
||||
find_package(Threads REQUIRED)
|
||||
add_executable(window_wait_test ${CMAKE_CURRENT_SOURCE_DIR}/src/core/window_wait_test.cpp)
|
||||
target_include_directories(window_wait_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
target_link_libraries(window_wait_test PRIVATE Threads::Threads)
|
||||
add_test(NAME window_wait_test COMMAND window_wait_test)
|
||||
add_test(NAME window_wait_test_legacy COMMAND window_wait_test --legacy)
|
||||
set_tests_properties(window_wait_test_legacy PROPERTIES WILL_FAIL TRUE)
|
||||
add_executable(spec_admission_test ${STRATA_ROOT}/src/core/spec_admission_test.cpp ${STRATA_ROOT}/src/spec/draft_policy.cpp)
|
||||
target_include_directories(spec_admission_test PRIVATE ${STRATA_ROOT}/include)
|
||||
add_test(NAME spec_admission_default COMMAND ${CMAKE_COMMAND} -E env --unset=STRATA_SPEC_PROB_GATE $<TARGET_FILE:spec_admission_test>)
|
||||
|
||||
@@ -412,6 +412,11 @@ struct TokenGraph {
|
||||
double ms_wait = 0; ///< host waiting for rings (the GPU is running)
|
||||
double ms_pool = 0; ///< host inside the pool
|
||||
int64_t flushes = 0; ///< cudaStreamQuery calls made because a ring was slow to appear
|
||||
/// #5160: the token's wait record (strata/core/window_wait.hpp), 4 words of mapped host memory, written by the graph's
|
||||
/// doorbell waits when one runs out; armed before every launch. A written record fails the token.
|
||||
uint32_t* wrec = nullptr;
|
||||
uint32_t wepoch = 0;
|
||||
int64_t waits_expired = 0;
|
||||
};
|
||||
|
||||
/// Plan v0.3 P4: the VRAM expert tier inside the token graph. Residency is STATIC during a token (a
|
||||
|
||||
@@ -277,6 +277,7 @@ public:
|
||||
|
||||
double ms_wait = 0, ms_pool = 0, ms_host = 0, ms_commit = 0;
|
||||
int64_t windows = 0;
|
||||
int64_t waits_expired = 0; ///< windows discarded because a GPU wait on the host ran out (#5160)
|
||||
/// STRATA_VERIFY_PROFILE=1 - GPU stage times of the windows since the last call (ms per
|
||||
/// window), as one line; empty when off.
|
||||
std::string profile_report();
|
||||
@@ -369,6 +370,17 @@ private:
|
||||
bool ar_on() const { return all_resident_ && !ar_off_; }
|
||||
void refresh_ar();
|
||||
uint32_t* h_plan_err_ = nullptr; uint32_t* m_plan_err_ = nullptr; // set by resident_plan on a -1 (all-resident graph)
|
||||
/// #5160 / #6016: this verifier's wait record (strata/core/window_wait.hpp), 4 words of mapped host memory. Its window
|
||||
/// kernels write it when a wait on the host runs out; stage_inputs() arms it with the window's epoch; wait_failed() reads it.
|
||||
uint32_t* h_wrec_ = nullptr; uint32_t* m_wrec_ = nullptr;
|
||||
uint32_t wepoch_ = 0;
|
||||
double wpool_ms_ = 0; ///< the slowest CPU step of the current window (ms), for the message
|
||||
/// A wait of this window ran out: say so in `err` (which wait, which layer, how long the CPU took) and return true. With
|
||||
/// `drain` the window's flags are raised and its queues drained first (the window is given up; the verifier stays usable
|
||||
/// when they finish within 5 s, otherwise it refuses every later window as release_gpu_waits does).
|
||||
bool wait_failed(int64_t pos0, int T, int G, bool drain, std::string& err);
|
||||
/// The window's flags to UINT32_MAX and its queues drained (bounded); unlike release_gpu_waits it does not retire the verifier.
|
||||
bool drain_failed_window(int timeout_ms);
|
||||
const int32_t* h_res_ = nullptr; ///< the host residency table (VerifyHits::h_res)
|
||||
bool device_plan_ = false; ///< E-6: resident-only layers planned on the device (STRATA_VERIFY_DEVICE_PLAN)
|
||||
uint32_t* skip_ = nullptr; ///< E-6: per group, the ring whose plan the device built (0: the host's)
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
// sycl/include/strata/core/window_wait.hpp - a GPU wait on a host flag that runs out is an error of its window (#5160, #5992, #6016).
|
||||
//
|
||||
// THE CONTRACT. In a verify window (and the single-token session loop) the GPU rings a step, writes that step's payload (activations,
|
||||
// expert ids, routing weights) into mapped host memory and then waits for the host: the plan (flag A), the PCIe share (flag B) and the
|
||||
// CPU's rows (flag M). The payload belongs to the host from the ring until it raises flag M; the GPU writes that region again only after
|
||||
// it passed flag M. Every device wait is bounded (sycl_doorbell.hpp, spin_max) because an unbounded spin wedges an xe card. Before this
|
||||
// header a wait that ran out simply returned: the GPU used the previous layer's plan and rows (a wrong token, "!!!!" from then on,
|
||||
// #5160) and rang the next step over a payload the CPU pool was still reading (#6016's crash at 262144).
|
||||
//
|
||||
// So a wait that runs out writes a RECORD, and the host treats a written record as the failure of the window:
|
||||
// - one record per verifier (and per session), never per device: two windows in flight on one card (pipelined parity verifiers,
|
||||
// --split-device 0, a batch window beside a single one) each read only their own;
|
||||
// - only that verifier's wait kernels write it, one at a time on its in-order queue, so plain uncached loads and stores are enough;
|
||||
// the first expiry keeps its site and step, every expiry bumps the count;
|
||||
// - the host arms it (all zero, a new epoch) where it resets the flags for a new window, i.e. after the previous window completed;
|
||||
// - the host reads it after every CPU step (before it raises flag M) and after the window's final sync; a written record fails the
|
||||
// window: its tokens are never returned or committed.
|
||||
//
|
||||
// The wait itself is a template on its loads and stores so the device kernels (uncached system loads / stores) and the CPU fixture
|
||||
// (window_wait_test.cpp, std::atomic) run the same code.
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace strata::core {
|
||||
|
||||
/// Which wait ran out (WaitRecord::site). 0 = none.
|
||||
enum WaitSite : uint32_t {
|
||||
kWaitNone = 0,
|
||||
kWaitPlan = 1, ///< flag A: the host's plan for the GPU's share of this step
|
||||
kWaitPcie = 2, ///< flag B: the PCIe share staged
|
||||
kWaitRows = 3, ///< flag M: the CPU's rows in the mapped buffer
|
||||
kWaitDevicePlan = 4, ///< a device-plan wait (wait_flag_ge_or) on any of the three
|
||||
kWaitFirst = 5, ///< flag 1 of an all-resident window (the PLE rows)
|
||||
kWaitDoorbell = 6, ///< the single-token session loop's doorbell
|
||||
};
|
||||
|
||||
/// Words of the record, in mapped host memory (4 x uint32). The device writes STEP before SITE, so a host that sees a site sees its step.
|
||||
enum WaitWord : int { kWordSite = 0, kWordStep = 1, kWordEpoch = 2, kWordCount = 3, kWaitWords = 4 };
|
||||
|
||||
inline const char* wait_site_name(uint32_t site) {
|
||||
switch (site) {
|
||||
case kWaitPlan: return "the plan for its GPU share (flag A)";
|
||||
case kWaitPcie: return "the PCIe share (flag B)";
|
||||
case kWaitRows: return "the CPU experts' rows (flag M)";
|
||||
case kWaitDevicePlan: return "a device-plan wait";
|
||||
case kWaitFirst: return "the window's first flag (PLE rows)";
|
||||
case kWaitDoorbell: return "the CPU experts (doorbell)";
|
||||
default: return "an unknown wait";
|
||||
}
|
||||
}
|
||||
|
||||
/// THE WAIT, device and fixture. `ready(v)` says whether the flag value v lets the GPU go on (>= value, or == want for the doorbell).
|
||||
/// Spins at most `spin_max` reads. Returns true when the flag arrived; otherwise writes the record (if any) and returns false. The
|
||||
/// caller goes on either way (a bounded wait never hangs the card): a written record is what makes the host discard the window.
|
||||
template <class Load, class Store, class Pause, class Ready>
|
||||
inline bool bounded_wait(const volatile uint32_t* flag, uint32_t spin_max, volatile uint32_t* rec, uint32_t site, uint32_t step,
|
||||
Load&& load, Store&& store, Pause&& pause, Ready&& ready) {
|
||||
for (uint32_t spin = 0; spin < spin_max; ++spin) {
|
||||
if (ready(load(flag))) return true;
|
||||
pause();
|
||||
}
|
||||
if (ready(load(flag))) return true; // the last read after the bound: arrived just in time
|
||||
if (rec != nullptr) {
|
||||
if (load(rec + kWordSite) == 0u) { // the first expiry of this window keeps its place
|
||||
store(rec + kWordStep, step);
|
||||
store(rec + kWordSite, site);
|
||||
}
|
||||
store(rec + kWordCount, load(rec + kWordCount) + 1u);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Host side: what a record says.
|
||||
struct WaitExpiry {
|
||||
uint32_t site = 0, step = 0, epoch = 0, count = 0;
|
||||
bool expired() const { return site != 0; }
|
||||
};
|
||||
|
||||
/// Host side: arm the record for a new window (all zero, the window's epoch). Only when no kernel of the previous window can still
|
||||
/// write it: the caller has synchronised that window or drained it.
|
||||
inline void wait_record_arm(volatile uint32_t* rec, uint32_t epoch) {
|
||||
if (rec == nullptr) return;
|
||||
rec[kWordSite] = 0u;
|
||||
rec[kWordStep] = 0u;
|
||||
rec[kWordCount] = 0u;
|
||||
rec[kWordEpoch] = epoch;
|
||||
__atomic_thread_fence(__ATOMIC_SEQ_CST);
|
||||
}
|
||||
|
||||
/// Host side: read the record (site first; the device wrote it last).
|
||||
inline WaitExpiry wait_record_read(const volatile uint32_t* rec) {
|
||||
WaitExpiry x;
|
||||
if (rec == nullptr) return x;
|
||||
x.site = rec[kWordSite];
|
||||
__atomic_thread_fence(__ATOMIC_ACQUIRE);
|
||||
x.step = rec[kWordStep];
|
||||
x.epoch = rec[kWordEpoch];
|
||||
x.count = rec[kWordCount];
|
||||
return x;
|
||||
}
|
||||
|
||||
} // namespace strata::core
|
||||
|
||||
namespace strata::kernels {
|
||||
// The launchers with a record (sycl/src/kernels/cuda/*.dp.cpp). The three-argument forms of the shared headers stay and pass none.
|
||||
void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream, uint32_t* rec, uint32_t site);
|
||||
void wait_flag_ge_or(const uint32_t* flag, uint32_t value, const uint32_t* skip, void* stream, uint32_t* rec, uint32_t site);
|
||||
void doorbell_wait(const uint32_t* d_flag, const uint32_t* d_seq, void* stream, uint32_t* rec);
|
||||
} // namespace strata::kernels
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "strata/kernels/qsa.hpp"
|
||||
#include "strata/kernels/elementwise.hpp"
|
||||
#include "strata/core/window_wait.hpp"
|
||||
#include "strata/sycl_doorbell.hpp"
|
||||
#include "strata/kernels/quantize_act.hpp"
|
||||
#include "strata/kernels/s2_expert_grouped.hpp"
|
||||
#include "strata/kernels/cpu/pool.hpp"
|
||||
@@ -1233,6 +1235,11 @@ bool session_capture_token(const WeightTable &tables, const ModelGeometry &g,
|
||||
err = "session_capture_token: the parts staging is not mapped pinned memory";
|
||||
return false;
|
||||
}
|
||||
if (tg.wrec == nullptr) {
|
||||
tg.wrec = (uint32_t*) strata::host_malloc_polled(64, dpct::get_in_order_queue());
|
||||
if (tg.wrec == nullptr) { err = "session_capture_token: the wait record could not be allocated"; return false; }
|
||||
wait_record_arm(tg.wrec, 0);
|
||||
}
|
||||
dpct::queue_ptr cs = &dpct::get_in_order_queue();
|
||||
if (DPCT_CHECK_ERROR(cs = dpct::get_current_device().create_queue(true)) !=
|
||||
0) {
|
||||
@@ -1262,7 +1269,7 @@ bool session_capture_token(const WeightTable &tables, const ModelGeometry &g,
|
||||
hits->blob, hits->x_q8, hits->scratch, hits->hit_out, (void*) cs,
|
||||
hits->x_scale);
|
||||
}
|
||||
strata::kernels::doorbell_wait(s.db->d_flag, s.db->d_seq, (void*) cs);
|
||||
strata::kernels::doorbell_wait(s.db->d_flag, s.db->d_seq, (void*) cs, tg.wrec);
|
||||
// A kernel, not a memcpy node: a copy-engine node splits the WDDM submission (measured 67 flushes/token).
|
||||
strata::kernels::copy_from_mapped(parts_dev, y_dev, (int64_t) (parts_bytes / sizeof(float)), (void*) cs);
|
||||
if (hits != nullptr)
|
||||
@@ -1336,6 +1343,7 @@ bool session_run_token(const ModelGeometry &g, int64_t pos, int32_t pos_base,
|
||||
++tg.calls;
|
||||
stage_token(g, pos, pos_base, s);
|
||||
doorbell_reset(*s.db);
|
||||
wait_record_arm(tg.wrec, ++tg.wepoch); // #5160: the previous token has completed (synced below)
|
||||
const dpct::err0 le = DPCT_CHECK_ERROR(cs->ext_oneapi_graph(*tg.exec));
|
||||
/*
|
||||
DPCT1009: SYCL reports errors using exceptions and does not use error
|
||||
@@ -1363,6 +1371,7 @@ bool session_run_token(const ModelGeometry &g, int64_t pos, int32_t pos_base,
|
||||
volatile uint32_t* const seq = s.db->h_seq;
|
||||
volatile uint32_t* const flag = s.db->h_flag;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
double tg_pool_max = 0; // the slowest CPU step of this token (ms), for the message
|
||||
for (int64_t l = 0; l < g.n_layers; ++l) {
|
||||
const uint32_t want = (uint32_t) (l + 1);
|
||||
const auto t0 = Clock::now();
|
||||
@@ -1404,7 +1413,11 @@ bool session_run_token(const ModelGeometry &g, int64_t pos, int32_t pos_base,
|
||||
}
|
||||
const auto t1 = Clock::now();
|
||||
progress_at("token: the CPU experts of layer", l);
|
||||
if (pool != nullptr) pool(user, s.db->h_x_f, s.db->h_ids, s.db->h_weights, g.n_embd, s.k, y_miss_host);
|
||||
// #5160: once a wait of this token has run out the GPU no longer waits for this payload, so the host stops
|
||||
// computing and only answers the doorbell (the graph then finishes at once); the check after the sync fails it
|
||||
if (pool != nullptr && !wait_record_read(tg.wrec).expired())
|
||||
pool(user, s.db->h_x_f, s.db->h_ids, s.db->h_weights, g.n_embd, s.k, y_miss_host);
|
||||
tg_pool_max = std::max(tg_pool_max, std::chrono::duration<double, std::milli>(Clock::now() - t1).count());
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
_mm_sfence();
|
||||
*flag = want;
|
||||
@@ -1431,6 +1444,17 @@ bool session_run_token(const ModelGeometry &g, int64_t pos, int32_t pos_base,
|
||||
dpct::get_error_string_dummy(se);
|
||||
return false;
|
||||
}
|
||||
if (const WaitExpiry x = wait_record_read(tg.wrec); x.expired()) { // #5160: the token was computed without the CPU's rows
|
||||
++tg.waits_expired;
|
||||
char buf[400];
|
||||
std::snprintf(buf, sizeof buf,
|
||||
"session_run_token: the GPU stopped waiting for %s of layer %u (token at position %lld): the CPU took %.0f ms, "
|
||||
"longer than the GPU's wait of %u reads; the token was discarded",
|
||||
wait_site_name(x.site), x.step > 0 ? x.step - 1 : 0u, (long long) pos, tg_pool_max,
|
||||
(unsigned) strata::spin_max(*cs));
|
||||
err = buf;
|
||||
return false;
|
||||
}
|
||||
progress_at("decode");
|
||||
progress_beat();
|
||||
return true;
|
||||
@@ -1443,6 +1467,7 @@ catch (sycl::exception const &exc) {
|
||||
|
||||
void token_graph_free(TokenGraph& tg) {
|
||||
if (tg.exec) delete (tg.exec);
|
||||
if (tg.wrec) strata::host_free_polled(tg.wrec, dpct::get_in_order_queue());
|
||||
tg = TokenGraph{};
|
||||
}
|
||||
|
||||
|
||||
+101
-8
@@ -4,6 +4,7 @@
|
||||
#include <sycl/sycl.hpp>
|
||||
#include <dpct/dpct.hpp>
|
||||
#include "strata/sycl_doorbell.hpp"
|
||||
#include "strata/core/window_wait.hpp"
|
||||
#include "strata/sycl_queue.hpp"
|
||||
#include "strata/kprof.hpp"
|
||||
#include "strata/core/verify.hpp"
|
||||
@@ -399,6 +400,21 @@ const int64_t g_test_stall = [] {
|
||||
const char* e = std::getenv("STRATA_TEST_VERIFY_STALL");
|
||||
return e != nullptr ? (int64_t) std::atoll(e) : (int64_t) 0;
|
||||
}();
|
||||
// #5160 test hook, compiled only into a test build (cmake -DSTRATA_SYCL_TEST_HOOKS=ON; released engines have no such switch):
|
||||
// STRATA_TEST_POOL_DELAY=W:K:MS sleeps MS ms after the CPU step K of window W (0-based), so the GPU's wait on that step
|
||||
// runs out with a small STRATA_SPIN_MAX. Without the build option this is an empty function.
|
||||
void test_pool_delay(int64_t window, int64_t step) {
|
||||
#ifdef STRATA_SYCL_TEST_HOOKS
|
||||
static long long tw = -1, tk = -1, tms = 0;
|
||||
static const bool on = [] {
|
||||
const char* e = std::getenv("STRATA_TEST_POOL_DELAY");
|
||||
return e != nullptr && std::sscanf(e, "%lld:%lld:%lld", &tw, &tk, &tms) == 3 && tms > 0;
|
||||
}();
|
||||
if (on && window == tw && step == tk) std::this_thread::sleep_for(std::chrono::milliseconds(tms));
|
||||
#else
|
||||
(void) window; (void) step;
|
||||
#endif
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool Verifier::release_gpu_waits(int timeout_ms) try {
|
||||
@@ -430,6 +446,58 @@ bool Verifier::release_gpu_waits(int timeout_ms) try {
|
||||
return false; // the runtime may already be tearing down (the watchdog's path ends the engine)
|
||||
}
|
||||
|
||||
bool Verifier::drain_failed_window(int timeout_ms) try {
|
||||
trace_ev("DRAIN", -1, -1, timeout_ms);
|
||||
// as release_gpu_waits: every flag past every ring, so the window's remaining waits pass at once (the host has stopped
|
||||
// serving it); then the queues, bounded. Drained: the next stage_inputs() resets the flags and re-arms the record.
|
||||
for (uint32_t* p : {h_flag_, h_flagA_, h_flagB_})
|
||||
if (p != nullptr) *(volatile uint32_t*) p = UINT32_MAX;
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
_mm_sfence();
|
||||
const OnDevice on_device(device_);
|
||||
const Clock::time_point t0 = Clock::now();
|
||||
for (dpct::queue_ptr q : {cs_, copy_}) {
|
||||
if (q == nullptr) continue;
|
||||
while (!q->ext_oneapi_empty()) {
|
||||
if (ms_since(t0) > timeout_ms) {
|
||||
released_.store(true); // still running: no later window may share its buffers
|
||||
trace_ev("DRAIN-NOT-DRAINED", -1, -1, (int64_t) ms_since(t0));
|
||||
return false;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
}
|
||||
trace_ev("DRAINED", -1, -1, (int64_t) ms_since(t0));
|
||||
return true;
|
||||
} catch (...) {
|
||||
released_.store(true);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Verifier::wait_failed(int64_t pos0, int T, int G, bool drain, std::string& err) {
|
||||
const WaitExpiry x = wait_record_read(h_wrec_);
|
||||
if (!x.expired()) return false;
|
||||
const bool drained = drain ? drain_failed_window(5000) : true;
|
||||
++waits_expired;
|
||||
const int g = G > 0 ? G : 1;
|
||||
const int64_t step = x.step > 0 ? (int64_t) x.step - 1 : 0;
|
||||
const int64_t layer = (x.site == kWaitFirst) ? 1 : lb_ + step / g;
|
||||
const int grp = (int) (step % g);
|
||||
char buf[640];
|
||||
std::snprintf(buf, sizeof buf,
|
||||
"verify: the GPU stopped waiting for %s of layer %lld (group %d, window %lld at position %lld, %d tokens, "
|
||||
"device %d layers [%lld,%lld)): the CPU took %.0f ms, longer than the GPU's wait of %u reads; the window was "
|
||||
"discarded%s%s%s",
|
||||
wait_site_name(x.site), (long long) layer, grp, (long long) windows, (long long) pos0, T, device_,
|
||||
(long long) lb_, (long long) le_, wpool_ms_, (unsigned) strata::spin_max(*cs_),
|
||||
x.count > 1 ? (" (" + std::to_string(x.count) + " waits ran out)").c_str() : "",
|
||||
x.epoch != wepoch_ ? " (the record carries another window's epoch)" : "",
|
||||
drained ? "" : "; the GPU did not finish within 5 s, so this verifier takes no further windows");
|
||||
err = buf;
|
||||
trace_ev("WAIT-EXPIRED", (int64_t) x.step, layer, (int64_t) x.site);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Verifier::trace_ev(const char* what, int64_t step, int64_t layer, int64_t aux) const {
|
||||
if (!g_trace) return;
|
||||
auto rd = [](const uint32_t* p) { return p ? *(const volatile uint32_t*) p : 0u; };
|
||||
@@ -546,7 +614,7 @@ Verifier::~Verifier() try {
|
||||
if (tap_dev_) sycl::free(tap_dev_, dpct::get_in_order_queue());
|
||||
if (arena_) sycl::free(arena_, dpct::get_in_order_queue());
|
||||
void* hosts[] = {h_tok_, h_step_, h_pos_, h_commit_, h_ple_, h_out_, h_x_, h_ids_, h_w_, h_seq_, h_flag_, h_ymiss_,
|
||||
h_flagA_, h_plan_, h_flagB_, h_plan_err_};
|
||||
h_flagA_, h_plan_, h_flagB_, h_plan_err_, h_wrec_};
|
||||
for (void* h : hosts)
|
||||
if (h) strata::host_free_polled(h, dpct::get_in_order_queue());
|
||||
} catch (...) {
|
||||
@@ -635,6 +703,7 @@ bool Verifier::init(const WeightTable &wt, const ModelGeometry &g,
|
||||
mapped(64, (void**) &h_flagA_, (void**) &m_flagA_) &&
|
||||
mapped(64, (void**) &h_flagB_, (void**) &m_flagB_) &&
|
||||
mapped(64, (void**) &h_plan_err_, (void**) &m_plan_err_) &&
|
||||
mapped(64, (void**) &h_wrec_, (void**) &m_wrec_) &&
|
||||
mapped(T * K * N * 4, (void**) &h_ymiss_, (void**) &m_ymiss_);
|
||||
if (!ok) { err = "verify: mapped staging allocation failed"; return false; }
|
||||
// DFlash2 taps (set_taps): off, and nothing is allocated or recorded, otherwise
|
||||
@@ -1067,7 +1136,7 @@ bool Verifier::record_window(int T, dpct::queue_ptr cs, std::string &err) {
|
||||
bool pending = l > 0 && !cvec().covers(l - 1);
|
||||
if (l == 1 && ple_on) {
|
||||
if (grp == 0) {
|
||||
if (ar_on()) wait_flag_ge(m_flag_, 1, cs);
|
||||
if (ar_on()) wait_flag_ge(m_flag_, 1, cs, m_wrec_, kWaitFirst);
|
||||
copy_from_mapped(ple_, m_ple_, (int64_t) T * N, cs);
|
||||
}
|
||||
float* normalized = (float*) ((uint8_t*) ss.ple.scratch + ple_block_scratch_bytes());
|
||||
@@ -1690,10 +1759,10 @@ bool Verifier::record_window(int T, dpct::queue_ptr cs, std::string &err) {
|
||||
stamp(l, 20, grp);
|
||||
} else {
|
||||
if (device_plan_) { // E-6: skipped when the device planned this group (all its experts resident)
|
||||
if (!lean) wait_flag_ge_or(m_flagA_, ring, skip_ + grp, cs);
|
||||
if (!lean) wait_flag_ge_or(m_flagA_, ring, skip_ + grp, cs, m_wrec_, kWaitDevicePlan);
|
||||
if (!lean) copy_i32_from_mapped_unless(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, skip_ + grp, ring, cs);
|
||||
} else {
|
||||
wait_flag_ge(m_flagA_, ring, cs); // the pool published this group's GPU plan
|
||||
wait_flag_ge(m_flagA_, ring, cs, m_wrec_, kWaitPlan); // the pool published this group's GPU plan
|
||||
copy_i32_from_mapped(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
|
||||
}
|
||||
stamp(l, 19, grp);
|
||||
@@ -1701,8 +1770,8 @@ bool Verifier::record_window(int T, dpct::queue_ptr cs, std::string &err) {
|
||||
stamp(l, 20, grp);
|
||||
// stepped mode: the plan copy + VRAM groups end a segment, so they run while the CPU computes its rows
|
||||
if (step_hook2_ && !step_hook2_(l, grp)) { if (err.empty()) err = "verify: stepped split failed"; return false; }
|
||||
if (device_plan_) { if (!lean) wait_flag_ge_or(m_flagB_, ring, skip_ + grp, cs); }
|
||||
else wait_flag_ge(m_flagB_, ring, cs); // the PCIe share is in staging (DMA) or mapped
|
||||
if (device_plan_) { if (!lean) wait_flag_ge_or(m_flagB_, ring, skip_ + grp, cs, m_wrec_, kWaitDevicePlan); }
|
||||
else wait_flag_ge(m_flagB_, ring, cs, m_wrec_, kWaitPcie); // the PCIe share is in staging (DMA) or mapped
|
||||
if (!lean && sink_.pcie_mode == 2) { // stage it with a copy kernel, then point at staging
|
||||
const int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;
|
||||
uint8_t* stage = staging_ + (size_t) (grp * per) * lay.max_blob;
|
||||
@@ -1715,11 +1784,11 @@ bool Verifier::record_window(int T, dpct::queue_ptr cs, std::string &err) {
|
||||
if (!lean) grouped(p_ptr2, p_start2, p_counts + 2, kPcieGroupRows, hit_out); // (lean: the device plan leaves it empty)
|
||||
stamp(l, 22, grp);
|
||||
if (device_plan_) { // no CPU share when the device planned the group: its rows are zeros
|
||||
if (!lean) wait_flag_ge_or(m_flag_, ring, skip_ + grp, cs);
|
||||
if (!lean) wait_flag_ge_or(m_flag_, ring, skip_ + grp, cs, m_wrec_, kWaitDevicePlan);
|
||||
copy_or_zero_from_mapped(parts_out, m_ymiss_ + (size_t) tb * K * N, (long long) n * K * N,
|
||||
skip_ + grp, ring, cs);
|
||||
} else {
|
||||
wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
|
||||
wait_flag_ge(m_flag_, ring, cs, m_wrec_, kWaitRows); // the CPU's share is in the mapped rows
|
||||
stamp(l, 23, grp);
|
||||
if (remote_opt_) // #578: the helper's rows come back reduced; skip them as well
|
||||
remote_opt_->copy_rows(parts_out, m_ymiss_ + (size_t) tb * K * N, tb, n, p_dst, p_counts + 1, cs);
|
||||
@@ -2220,6 +2289,8 @@ void Verifier::stage_inputs(int T, const int32_t* tokens, int64_t pos0) {
|
||||
*(volatile uint32_t*) h_flag_ = 0;
|
||||
*(volatile uint32_t*) h_flagA_ = 0;
|
||||
*(volatile uint32_t*) h_flagB_ = 0;
|
||||
wait_record_arm(h_wrec_, ++wepoch_); // #5160: this window's wait record (the previous window has completed)
|
||||
wpool_ms_ = 0;
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
last_t_ = T;
|
||||
last_pos0_ = pos0;
|
||||
@@ -2599,6 +2670,9 @@ bool Verifier::run(int T, const int32_t *tokens, int64_t pos0, PoolMultiFn pool,
|
||||
pool(user, h_x_ + (size_t) tb * g.n_embd, h_ids_ + (size_t) tb * ss.k, n, ss.k,
|
||||
h_ymiss_ + (size_t) tb * ss.k * g.n_embd, l);
|
||||
if (remote_opt_) remote_opt_->end();
|
||||
test_pool_delay(windows, k);
|
||||
wpool_ms_ = std::max(wpool_ms_, ms_since(b));
|
||||
if (wait_failed(pos0, T, G, true, err)) return false; // #5160: the GPU went on without this step: give the window up
|
||||
if (std::FILE* lf = layer_hash_file()) { // debug only (STRATA_LAYER_HASH_FILE)
|
||||
std::fprintf(lf, "%lld %lld %d %016llx %016llx %016llx %016llx\n", (long long) windows, (long long) l, n,
|
||||
(unsigned long long) layer_hash_fnv(h_x_ + (size_t) tb * g.n_embd, (size_t) n * g.n_embd * 4),
|
||||
@@ -2669,6 +2743,7 @@ bool Verifier::run(int T, const int32_t *tokens, int64_t pos0, PoolMultiFn pool,
|
||||
err = std::string("verify: ") + dpct::get_error_string_dummy(se);
|
||||
return false;
|
||||
}
|
||||
if (wait_failed(pos0, T, G, false, err)) return false; // #5160: a wait ran out: the window's tokens are not used
|
||||
if (expert_usage_counts_d_) {
|
||||
if (!expert_usage_counter_ || expert_usage_counter_->size() != expert_usage_counts_h_.size())
|
||||
throw std::runtime_error("mirror adaptive expert usage is unbound");
|
||||
@@ -3533,6 +3608,8 @@ bool Verifier::stage_batch(const int *rows, int S, int hbase,
|
||||
*(volatile uint32_t*) h_flag_ = 0;
|
||||
*(volatile uint32_t*) h_flagA_ = 0;
|
||||
*(volatile uint32_t*) h_flagB_ = 0;
|
||||
wait_record_arm(h_wrec_, ++wepoch_); // #5160: this window's wait record (the previous window has completed)
|
||||
wpool_ms_ = 0;
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
last_t_ = S;
|
||||
for (int t = 0; t < S; ++t) last_rows_[t] = rows[t];
|
||||
@@ -3615,6 +3692,7 @@ bool Verifier::run_slot_rows(const int *rows, int S, const int32_t *tokens,
|
||||
return false;
|
||||
}
|
||||
copy_->wait();
|
||||
if (wait_failed(pos[0], S, 1, false, err)) return false; // #5160
|
||||
if (prof_on_) collect_profile();
|
||||
++windows;
|
||||
if (le_ < g.n_layers) return next_ == nullptr || next_->run_slot_rows(rows, S, tokens, pos, pool, next_user_, out, err);
|
||||
@@ -3664,6 +3742,9 @@ bool Verifier::run_slot_rows(const int *rows, int S, const int32_t *tokens,
|
||||
set_plan_slot(0);
|
||||
progress_at("verify batch: the CPU experts of layer", l);
|
||||
if (pool != nullptr) pool(user, h_x_, h_ids_, S, ss_->k, h_ymiss_, l);
|
||||
test_pool_delay(windows, k);
|
||||
wpool_ms_ = std::max(wpool_ms_, ms_since(b));
|
||||
if (wait_failed(pos[0], S, 1, true, err)) return false; // #5160
|
||||
progress_tick();
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
_mm_sfence();
|
||||
@@ -3698,6 +3779,7 @@ bool Verifier::run_slot_rows(const int *rows, int S, const int32_t *tokens,
|
||||
return false;
|
||||
}
|
||||
copy_->wait();
|
||||
if (wait_failed(pos[0], S, 1, false, err)) return false; // #5160
|
||||
if (prof_on_) collect_profile();
|
||||
++windows;
|
||||
if (le_ < g.n_layers) return next_ == nullptr || next_->run_slot_rows(rows, S, tokens, pos, pool, next_user_, out, err);
|
||||
@@ -3935,6 +4017,9 @@ int Verifier::batch_poll(PoolMultiFn pool, void *user, std::string &err) try {
|
||||
cur_layer_ = want - 1;
|
||||
set_plan_slot(0);
|
||||
if (pool != nullptr) pool(user, h_x_, h_ids_, S, ss_->k, h_ymiss_, lb_ + b_k_);
|
||||
test_pool_delay(windows, b_k_);
|
||||
wpool_ms_ = std::max(wpool_ms_, ms_since(b));
|
||||
if (wait_failed(last_pos_b_[0], S, 1, true, err)) { b_running_ = false; return -1; } // #5160
|
||||
progress_tick();
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
_mm_sfence();
|
||||
@@ -3989,6 +4074,7 @@ int Verifier::batch_poll(PoolMultiFn pool, void *user, std::string &err) try {
|
||||
to rewrite this code.
|
||||
*/
|
||||
if (qc == 1) return 0;
|
||||
if (wait_failed(last_pos_b_[0], S, 1, false, err)) { b_running_ = false; return -1; } // #5160
|
||||
if (prof_on_) collect_profile();
|
||||
if (last_stage()) {
|
||||
if (!sample_rows(S, err)) { b_running_ = false; return -1; }
|
||||
@@ -4265,6 +4351,9 @@ int Verifier::service(PoolMultiFn pool, void *user, std::string &err) try {
|
||||
if (pool != nullptr)
|
||||
pool(user, h_x_ + (size_t) tb * g.n_embd, h_ids_ + (size_t) tb * ss.k, n, ss.k,
|
||||
h_ymiss_ + (size_t) tb * ss.k * g.n_embd, l);
|
||||
test_pool_delay(windows, fl_k_);
|
||||
wpool_ms_ = std::max(wpool_ms_, ms_since(b));
|
||||
if (wait_failed(last_pos0_, T, G, true, err)) { fl_active_ = false; return -1; } // #5160
|
||||
if (g_trace) trace_ev(*(volatile uint32_t*) h_flagA_ == want ? "SERVED" : "SERVED-NO-PLAN-YET", fl_k_, l,
|
||||
(int64_t) ms_since(b));
|
||||
progress_tick();
|
||||
@@ -4353,6 +4442,10 @@ bool Verifier::pl_finish(int32_t *out, std::string &err) try {
|
||||
const OnDevice on_device(device_);
|
||||
const ModelGeometry& g = *g_;
|
||||
fl_active_ = false;
|
||||
{ // #5160: a wait of this window ran out: its tokens are not used
|
||||
const int Gp = groups_[fl_T_] > 0 ? groups_[fl_T_] : 1;
|
||||
if (wait_failed(last_pos0_, fl_T_, Gp, false, err)) return false;
|
||||
}
|
||||
if (fl_prof_) accumulate_profile(prof_pin_);
|
||||
++windows;
|
||||
trace_ev("DONE (pipelined)", -1, -1, (int64_t) (now_ms() - fl_launch_ms_));
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
// sycl/src/core/window_wait_test.cpp - a GPU wait on the host that runs out fails its window (strata/core/window_wait.hpp), no GPU, no model.
|
||||
//
|
||||
// A thread plays the GPU of one verify window: per step it writes the step's payload (expert ids) into "mapped" memory, rings,
|
||||
// waits for the host's plan (flag A) and the CPU's rows (flag M) with the SAME bounded_wait the device kernels run, copies the
|
||||
// rows and goes on. The test thread plays the host: it waits for the ring, takes one copy of the ids, computes the rows,
|
||||
// checks the record, raises the flags, and at the end accepts the window's rows only when the record is clean.
|
||||
//
|
||||
// window_wait_test the protocol of this build: every case must pass
|
||||
// window_wait_test --legacy the protocol of 756242554 (a wait that runs out just returns, the host never checks):
|
||||
// cases 2, 5, 6 and 7 accept stale rows, so this run must FAIL (CTest: WILL_FAIL)
|
||||
//
|
||||
// Each wait spins `kSpin` times with a 50 us pause, so it gives up after at least 0.2 s; a delayed worker sleeps 1.5 s.
|
||||
#include "strata/core/window_wait.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using namespace strata::core;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
static int g_fail = 0;
|
||||
static bool g_legacy = false;
|
||||
static void check(bool ok, const char* what) { std::printf(" %-112s %s\n", what, ok ? "ok" : "FAIL"); if (!ok) ++g_fail; std::fflush(stdout); }
|
||||
|
||||
constexpr uint32_t kSpin = 4000;
|
||||
constexpr int kK = 4; // entries per step
|
||||
constexpr auto kLate = std::chrono::milliseconds(1500);
|
||||
|
||||
struct Load { uint32_t operator()(const volatile uint32_t* p) const { return __atomic_load_n(p, __ATOMIC_ACQUIRE); } };
|
||||
struct Store { void operator()(volatile uint32_t* p, uint32_t v) const { __atomic_store_n(p, v, __ATOMIC_RELEASE); } };
|
||||
struct Pause { void operator()() const { std::this_thread::sleep_for(std::chrono::microseconds(50)); } };
|
||||
|
||||
static int32_t router(int win, int step, int i) { return (int32_t) ((win * 131 + step * 17 + i * 5) % 512); } // the "layer's" experts
|
||||
static float expert_row(int32_t e) { return 1.0f + (float) e; } // the CPU's answer
|
||||
|
||||
/// One verifier's mapped memory: seq, flags, the payload, the CPU rows and the wait record.
|
||||
struct Mapped {
|
||||
volatile uint32_t seq = 0, flagA = 0, flagM = 0;
|
||||
volatile int32_t ids[kK] = {};
|
||||
volatile float rows[kK] = {};
|
||||
volatile uint32_t rec[kWaitWords] = {};
|
||||
uint32_t epoch = 0;
|
||||
void stage() { // stage_inputs(): flags to zero, the record armed (the previous window has completed)
|
||||
Store{}(&seq, 0); Store{}(&flagA, 0); Store{}(&flagM, 0);
|
||||
if (!g_legacy) wait_record_arm(rec, ++epoch);
|
||||
}
|
||||
};
|
||||
|
||||
/// The GPU of one window of `steps` steps. out[s] = the rows it used at step s (what the window's tokens are made of).
|
||||
struct Gpu {
|
||||
Mapped& m;
|
||||
int win, steps;
|
||||
std::vector<float> out;
|
||||
std::atomic<bool> done{false};
|
||||
std::thread th;
|
||||
Gpu(Mapped& mm, int w, int s) : m(mm), win(w), steps(s), out((size_t) s * kK, -1.0f) {}
|
||||
void start() {
|
||||
th = std::thread([this] {
|
||||
volatile uint32_t* rec = g_legacy ? nullptr : m.rec;
|
||||
for (int s = 0; s < steps; ++s) {
|
||||
const uint32_t ring = (uint32_t) s + 1;
|
||||
for (int i = 0; i < kK; ++i) m.ids[i] = router(win, s, i); // the payload of step s (the next step overwrites it)
|
||||
Store{}(&m.seq, ring);
|
||||
bounded_wait(&m.flagA, kSpin, rec, kWaitPlan, ring, Load{}, Store{}, Pause{}, [ring](uint32_t v) { return v >= ring; });
|
||||
bounded_wait(&m.flagM, kSpin, rec, kWaitRows, ring, Load{}, Store{}, Pause{}, [ring](uint32_t v) { return v >= ring; });
|
||||
for (int i = 0; i < kK; ++i) out[(size_t) s * kK + i] = m.rows[i];
|
||||
}
|
||||
done = true;
|
||||
});
|
||||
}
|
||||
bool join_within(std::chrono::milliseconds ms) {
|
||||
const auto t0 = Clock::now();
|
||||
while (!done.load() && Clock::now() - t0 < ms) std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
if (!done.load()) return false;
|
||||
th.join();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
enum class Outcome { Accepted, Failed, HostTimeout, Cancelled };
|
||||
struct Result {
|
||||
Outcome outcome = Outcome::Accepted;
|
||||
WaitExpiry x;
|
||||
bool rows_right = false; // out == the reference for every step
|
||||
bool drained = true;
|
||||
double ms = 0;
|
||||
};
|
||||
|
||||
/// The host's plan for one window: how late each step is, and where.
|
||||
struct HostPlan {
|
||||
int late_before_plan = -1; // sleep kLate before raising flag A at this step (inside the "pool")
|
||||
int late_before_rows = -1; // raise flag A on time, sleep kLate before flag M
|
||||
int late_after_check = -1; // check the record (clean), THEN sleep kLate, then raise A and M (the late expiry)
|
||||
bool late_every_step = false; // every step is late
|
||||
int delay_ms_every = 0; // every step this many ms late (within the bound)
|
||||
bool dead = false; // never serves
|
||||
int cancel_after = -1; // stop serving after this step
|
||||
};
|
||||
|
||||
static Result run_window(Mapped& m, int win, int steps, const HostPlan& hp) {
|
||||
Result r;
|
||||
const auto t0 = Clock::now();
|
||||
m.stage();
|
||||
Gpu gpu(m, win, steps);
|
||||
gpu.start();
|
||||
bool failed = false, cancelled = false;
|
||||
auto drain = [&] { // drain_failed_window(): every flag past every ring, then the "queue", bounded
|
||||
Store{}(&m.flagA, UINT32_MAX);
|
||||
Store{}(&m.flagM, UINT32_MAX);
|
||||
r.drained = gpu.join_within(std::chrono::milliseconds(5000));
|
||||
};
|
||||
for (int s = 0; s < steps && !failed && !cancelled; ++s) {
|
||||
const uint32_t want = (uint32_t) s + 1;
|
||||
const auto a = Clock::now();
|
||||
if (hp.dead) { // the host never answers: its own ring timeout (here 4 s instead of 20) ends the wait
|
||||
while (Clock::now() - a < std::chrono::seconds(4)) std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
r.outcome = Outcome::HostTimeout;
|
||||
drain();
|
||||
r.x = wait_record_read(m.rec);
|
||||
r.ms = std::chrono::duration<double, std::milli>(Clock::now() - t0).count();
|
||||
return r;
|
||||
}
|
||||
while (Load{}(&m.seq) < want) {
|
||||
if (Clock::now() - a > std::chrono::seconds(20)) { r.outcome = Outcome::HostTimeout; drain(); return r; }
|
||||
std::this_thread::yield();
|
||||
}
|
||||
int32_t ids[kK];
|
||||
for (int i = 0; i < kK; ++i) ids[i] = m.ids[i]; // ONE copy of the payload, read once (#6016)
|
||||
float rows[kK];
|
||||
for (int i = 0; i < kK; ++i) rows[i] = expert_row(ids[i]);
|
||||
if (hp.late_every_step || s == hp.late_before_plan) std::this_thread::sleep_for(kLate);
|
||||
if (hp.delay_ms_every > 0) std::this_thread::sleep_for(std::chrono::milliseconds(hp.delay_ms_every));
|
||||
if (!g_legacy && wait_record_read(m.rec).expired()) { failed = true; drain(); break; } // after the CPU step
|
||||
if (s == hp.late_after_check) std::this_thread::sleep_for(kLate);
|
||||
for (int i = 0; i < kK; ++i) m.rows[i] = rows[i];
|
||||
Store{}(&m.flagA, want);
|
||||
if (s == hp.late_before_rows) {
|
||||
std::this_thread::sleep_for(kLate);
|
||||
if (!g_legacy && wait_record_read(m.rec).expired()) { failed = true; drain(); break; }
|
||||
}
|
||||
Store{}(&m.flagM, want);
|
||||
if (s == hp.cancel_after) { cancelled = true; drain(); }
|
||||
}
|
||||
if (!failed && !cancelled) r.drained = gpu.join_within(std::chrono::milliseconds(20000)); // the window's sync
|
||||
if (!g_legacy && wait_record_read(m.rec).expired()) failed = true; // after the sync
|
||||
r.x = wait_record_read(m.rec);
|
||||
r.outcome = cancelled ? Outcome::Cancelled : failed ? Outcome::Failed : Outcome::Accepted;
|
||||
r.rows_right = true;
|
||||
for (int s = 0; s < steps; ++s)
|
||||
for (int i = 0; i < kK; ++i)
|
||||
if (gpu.out[(size_t) s * kK + i] != expert_row(router(win, s, i))) r.rows_right = false;
|
||||
r.ms = std::chrono::duration<double, std::milli>(Clock::now() - t0).count();
|
||||
return r;
|
||||
}
|
||||
|
||||
/// The fail-closed rule every case must keep: a window is accepted only with every row right.
|
||||
static bool fail_closed(const Result& r) { return r.outcome != Outcome::Accepted || r.rows_right; }
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
g_legacy = argc > 1 && std::strcmp(argv[1], "--legacy") == 0;
|
||||
std::printf("window_wait_test%s\n", g_legacy ? " --legacy (the 756242554 protocol: must fail)" : "");
|
||||
// 1. on time
|
||||
{
|
||||
Mapped m;
|
||||
const Result r = run_window(m, 1, 6, {});
|
||||
check(r.outcome == Outcome::Accepted && r.rows_right && !r.x.expired(), "1. host on time: window accepted, every row right, record clean");
|
||||
}
|
||||
// 2. a delayed worker past the bound at step 2 (before the plan): explicit failure, the stale rows are not accepted
|
||||
{
|
||||
Mapped m;
|
||||
HostPlan hp; hp.late_before_plan = 2;
|
||||
const Result r = run_window(m, 2, 6, hp);
|
||||
check(fail_closed(r), "2a. worker 1.5 s late at step 2: the window is not accepted with wrong rows");
|
||||
check(r.outcome == Outcome::Failed && r.x.site == kWaitPlan && r.x.step == 3, "2b. ... it fails with its own site (plan) and step (3 = ring of step 2)");
|
||||
check(r.drained && r.ms < 8000, "2c. ... and the GPU finishes once released (bounded)");
|
||||
}
|
||||
// 3. a delayed worker within the bound
|
||||
{
|
||||
Mapped m;
|
||||
HostPlan hp; hp.delay_ms_every = 40;
|
||||
const Result r = run_window(m, 3, 4, hp);
|
||||
check(r.outcome == Outcome::Accepted && r.rows_right, "3. worker 40 ms late at every step (the bound is 0.2 s or more): accepted, rows right");
|
||||
}
|
||||
// 4. cancel mid-window, then the same record serves the next window (slot reuse)
|
||||
{
|
||||
Mapped m;
|
||||
HostPlan hp; hp.cancel_after = 1;
|
||||
const Result r = run_window(m, 4, 6, hp);
|
||||
check(r.outcome == Outcome::Cancelled && r.drained, "4a. cancelled after step 1: the GPU drains within the bound");
|
||||
const Result r2 = run_window(m, 5, 6, {});
|
||||
check(r2.outcome == Outcome::Accepted && r2.rows_right && !r2.x.expired() && (g_legacy || r2.x.epoch == m.epoch),
|
||||
"4b. the next window on the same record: clean, its own epoch, rows right");
|
||||
}
|
||||
// 5. late expiry: the host checked a clean record, THEN was late; the check after the sync fails the window; next one clean
|
||||
{
|
||||
Mapped m;
|
||||
HostPlan hp; hp.late_after_check = 3;
|
||||
const Result r = run_window(m, 6, 5, hp);
|
||||
check(fail_closed(r), "5a. late after the host's check: the window is not accepted with wrong rows");
|
||||
check(r.outcome == Outcome::Failed && r.x.step == 4, "5b. ... it fails at a later check, naming step 4");
|
||||
const Result r2 = run_window(m, 7, 5, {});
|
||||
check(r2.outcome == Outcome::Accepted && r2.rows_right && !r2.x.expired(), "5c. the next window starts with a clean record and is right");
|
||||
}
|
||||
// 6. slow host at every step, and a dead host: bounded, explicit, no hang
|
||||
{
|
||||
Mapped m;
|
||||
HostPlan hp; hp.late_every_step = true;
|
||||
const Result r = run_window(m, 8, 4, hp);
|
||||
check(fail_closed(r) && r.outcome != Outcome::Accepted && r.ms < 12000, "6a. slow host at every step: explicit failure within a bounded time");
|
||||
Mapped d;
|
||||
HostPlan dead; dead.dead = true;
|
||||
const Result rd = run_window(d, 9, 4, dead);
|
||||
check(rd.outcome == Outcome::HostTimeout && rd.drained && (g_legacy || (rd.x.site == kWaitPlan && rd.x.step == 1)),
|
||||
"6b. dead host: the host's timeout ends it, the GPU finishes, the record names step 1 (plan)");
|
||||
}
|
||||
// 7. simultaneous queues: three windows at once on three records; two fail at different steps and sites, one is clean
|
||||
{
|
||||
Mapped a, b, c;
|
||||
Result ra, rb, rc;
|
||||
HostPlan pa; pa.late_before_plan = 1;
|
||||
HostPlan pb; pb.late_before_rows = 4;
|
||||
std::thread ta([&] { ra = run_window(a, 10, 6, pa); });
|
||||
std::thread tb([&] { rb = run_window(b, 11, 6, pb); });
|
||||
std::thread tc([&] { rc = run_window(c, 12, 6, {}); });
|
||||
ta.join(); tb.join(); tc.join();
|
||||
check(fail_closed(ra) && fail_closed(rb) && fail_closed(rc), "7a. three windows at once: none is accepted with wrong rows");
|
||||
check(ra.outcome == Outcome::Failed && ra.x.site == kWaitPlan && ra.x.step == 2, "7b. window A fails with its own site (plan) and step 2");
|
||||
check(rb.outcome == Outcome::Failed && rb.x.site == kWaitRows && rb.x.step == 5, "7c. window B fails with its own site (rows) and step 5");
|
||||
check(rc.outcome == Outcome::Accepted && rc.rows_right && !rc.x.expired(), "7d. window C beside them is clean and right");
|
||||
}
|
||||
// 8. the record helpers: arm clears, the first expiry keeps its place, the count goes on
|
||||
{
|
||||
volatile uint32_t rec[kWaitWords] = {7, 7, 7, 7};
|
||||
wait_record_arm(rec, 42);
|
||||
const WaitExpiry z = wait_record_read(rec);
|
||||
volatile uint32_t never = 0;
|
||||
bounded_wait(&never, 3, rec, kWaitRows, 9, Load{}, Store{}, [] {}, [](uint32_t v) { return v >= 1; });
|
||||
bounded_wait(&never, 3, rec, kWaitPlan, 11, Load{}, Store{}, [] {}, [](uint32_t v) { return v >= 1; });
|
||||
const WaitExpiry x = wait_record_read(rec);
|
||||
volatile uint32_t ready = 5;
|
||||
const bool ok_ready = bounded_wait(&ready, 0, rec, kWaitPcie, 12, Load{}, Store{}, [] {}, [](uint32_t v) { return v >= 5; });
|
||||
check(!z.expired() && z.epoch == 42 && z.count == 0 && x.site == kWaitRows && x.step == 9 && x.count == 2 && ok_ready &&
|
||||
wait_record_read(rec).count == 2,
|
||||
"8. arm clears with the epoch; first expiry keeps site/step; count 2; a flag already up passes without a write");
|
||||
}
|
||||
std::printf(g_fail ? "window_wait_test: %d FAILED\n" : "window_wait_test: all ok\n", g_fail);
|
||||
return g_fail ? 1 : 0;
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "strata/sycl_queue.hpp"
|
||||
#include "strata/sycl_doorbell.hpp"
|
||||
#include "strata/kernels/elementwise.hpp"
|
||||
#include "strata/core/window_wait.hpp"
|
||||
#include "strata/kernels/dp4a.hpp"
|
||||
|
||||
#include "strata/kernels/bf16_bits.hpp"
|
||||
@@ -383,10 +384,15 @@ __dpct_inline__ void doorbell_ring_kernel(uint32_t *seq) {
|
||||
#endif
|
||||
}
|
||||
|
||||
struct DoorbellLoad { uint32_t operator()(const volatile uint32_t* p) const { return strata::sys_load(p); } };
|
||||
struct DoorbellStore { void operator()(volatile uint32_t* p, uint32_t v) const { strata::sys_store(p, v); } };
|
||||
struct DoorbellPause { void operator()() const { strata_spin_pause(); } };
|
||||
__dpct_inline__ void doorbell_wait_kernel(const volatile uint32_t *flag,
|
||||
const volatile uint32_t *seq, uint32_t spin_max) {
|
||||
const volatile uint32_t *seq, uint32_t spin_max, volatile uint32_t *rec) {
|
||||
const uint32_t want = strata::sys_load(seq);
|
||||
for (uint32_t spin = 0; spin < spin_max && strata::sys_load(flag) != want; ++spin) strata_spin_pause();
|
||||
// a wait that runs out writes the session's record (strata/core/window_wait.hpp): the host then fails the token
|
||||
strata::core::bounded_wait(flag, spin_max, rec, strata::core::kWaitDoorbell, want, DoorbellLoad{}, DoorbellStore{},
|
||||
DoorbellPause{}, [want](uint32_t v) { return v == want; });
|
||||
/*
|
||||
DPCT1078: Consider replacing memory_order::acq_rel with
|
||||
memory_order::seq_cst for correctness if strong memory order restrictions
|
||||
@@ -396,6 +402,9 @@ __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) {
|
||||
doorbell_wait(d_flag, d_seq, stream, nullptr);
|
||||
}
|
||||
void doorbell_wait(const uint32_t* d_flag, const uint32_t* d_seq, void* stream, uint32_t* rec) {
|
||||
if (d_flag == nullptr || d_seq == nullptr) return;
|
||||
const uint32_t spin_max = strata::spin_max(*strata::q_of(stream));
|
||||
{
|
||||
@@ -406,7 +415,7 @@ void doorbell_wait(const uint32_t* d_flag, const uint32_t* d_seq, void* stream)
|
||||
->template 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, spin_max);
|
||||
doorbell_wait_kernel(d_flag, d_seq, spin_max, rec);
|
||||
});
|
||||
}
|
||||
check_launch("doorbell_wait");
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "strata/sycl_queue.hpp"
|
||||
#include "strata/sycl_doorbell.hpp"
|
||||
#include "strata/kernels/verify_kernels.hpp"
|
||||
#include "strata/core/window_wait.hpp"
|
||||
#include "strata/kernels/resident_plan_mirror.hpp"
|
||||
#include "strata/kernels/dp4a.hpp"
|
||||
|
||||
@@ -2461,9 +2462,15 @@ void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const
|
||||
}
|
||||
|
||||
namespace {
|
||||
// The device side of strata/core/window_wait.hpp: uncached system loads and stores of mapped host memory.
|
||||
struct WaitLoad { uint32_t operator()(const volatile uint32_t* p) const { return strata::sys_load(p); } };
|
||||
struct WaitStore { void operator()(volatile uint32_t* p, uint32_t v) const { strata::sys_store(p, v); } };
|
||||
struct WaitPause { void operator()() const { strata_spin_pause(); } };
|
||||
__dpct_inline__ void wait_flag_ge_kernel(const volatile uint32_t *flag,
|
||||
uint32_t value, uint32_t spin_max) {
|
||||
for (uint32_t spin = 0; spin < spin_max && strata::sys_load(flag) < value; ++spin) strata_spin_pause();
|
||||
uint32_t value, uint32_t spin_max, volatile uint32_t *rec, uint32_t site) {
|
||||
// a wait that runs out writes this verifier's record (window_wait.hpp): the host then discards the window
|
||||
strata::core::bounded_wait(flag, spin_max, rec, site, value, WaitLoad{}, WaitStore{}, WaitPause{},
|
||||
[value](uint32_t v) { return v >= value; });
|
||||
/*
|
||||
DPCT1078: Consider replacing memory_order::acq_rel with
|
||||
memory_order::seq_cst for correctness if strong memory order restrictions
|
||||
@@ -2732,9 +2739,11 @@ 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, uint32_t spin_max) {
|
||||
const volatile uint32_t *skip, uint32_t spin_max,
|
||||
volatile uint32_t *rec, uint32_t site) {
|
||||
if (strata::sys_load(skip) == value) return;
|
||||
for (uint32_t spin = 0; spin < spin_max && strata::sys_load(flag) < value; ++spin) strata_spin_pause();
|
||||
strata::core::bounded_wait(flag, spin_max, rec, site, value, WaitLoad{}, WaitStore{}, WaitPause{},
|
||||
[value](uint32_t v) { return v >= value; });
|
||||
/*
|
||||
DPCT1078: Consider replacing memory_order::acq_rel with
|
||||
memory_order::seq_cst for correctness if strong memory order restrictions
|
||||
@@ -2818,6 +2827,9 @@ 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) {
|
||||
wait_flag_ge_or(flag, value, skip, stream, nullptr, 0);
|
||||
}
|
||||
void wait_flag_ge_or(const uint32_t* flag, uint32_t value, const uint32_t* skip, void* stream, uint32_t* rec, uint32_t site) {
|
||||
const uint32_t spin_max = strata::spin_max(*strata::q_of(stream));
|
||||
{
|
||||
auto exp_props = sycl::ext::oneapi::experimental::properties{
|
||||
@@ -2828,7 +2840,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, spin_max);
|
||||
wait_flag_ge_or_kernel(flag, value, skip, spin_max, rec, site);
|
||||
});
|
||||
}
|
||||
check("wait_flag_ge_or");
|
||||
@@ -2877,6 +2889,9 @@ 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) {
|
||||
wait_flag_ge(flag, value, stream, nullptr, 0);
|
||||
}
|
||||
void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream, uint32_t* rec, uint32_t site) {
|
||||
const uint32_t spin_max = strata::spin_max(*strata::q_of(stream));
|
||||
{
|
||||
auto exp_props = sycl::ext::oneapi::experimental::properties{
|
||||
@@ -2886,7 +2901,7 @@ void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream) {
|
||||
->template 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, spin_max);
|
||||
wait_flag_ge_kernel(flag, value, spin_max, rec, site);
|
||||
});
|
||||
}
|
||||
check("wait_flag_ge");
|
||||
|
||||
Reference in new issue
Block a user