diff --git a/include/strata/kernels/elementwise.hpp b/include/strata/kernels/elementwise.hpp index 39a2560a..7c92dcb4 100644 --- a/include/strata/kernels/elementwise.hpp +++ b/include/strata/kernels/elementwise.hpp @@ -121,6 +121,21 @@ void doorbell_publish_value(const float* x, const int32_t* ids, const float* wei void doorbell_publish_res(const float* x, const int32_t* ids, const int32_t* d_res, int n_expert, int64_t n, int64_t k, float* x_out, int32_t* ids_out, uint32_t* d_seq, void* stream); +/// The ring can reach the host before its payload on some GPUs: the payload stores and the ring store are not ordered +/// by the fences alone (measured on an Arc A770: 14 of 2,832 rings in one run were stale, and the CPU pool then +/// computed on a partly stale payload). So the publish kernels also write, next to the sequence number, the ring it +/// belongs to and a checksum of the payload (the wrapping sum of every 32-bit word mixed with its position, murmur3's +/// finalizer - a plain sum missed a reordering, [3,7] -> [7,3], of the expert ids) in a per-ring slot modulo 4: +/// seq[1 + 2 (r % 4)] is the tag, seq[2 + 2 (r % 4)] the checksum (a split verify publishes two rings before the host +/// reads the first). The sequence word therefore needs 9 words of mapped host memory. The two publish shapes +/// differ (doorbell_publish / doorbell_publish_value publish the routing weights, doorbell_publish_res does not), +/// so the wait accepts either. CUDA/HIP: always true (their ordering holds), so callers may skip the wait there. +bool doorbell_payload_ready(const uint32_t* h_seq, const float* h_x, int64_t n, const int32_t* h_ids, + const float* h_weights, int64_t k, uint32_t want); +/// Spins on doorbell_payload_ready for up to `timeout_ms`; false on timeout. +bool doorbell_wait_payload(const uint32_t* h_seq, const float* h_x, int64_t n, const int32_t* h_ids, + const float* h_weights, int64_t k, uint32_t want, int timeout_ms = 2000); + /// Plan v0.3 P3: copy `n` int32 from mapped pinned host memory into device memory with a kernel (the QSA /// per-token step and positions), instead of a host-to-device memcpy node in the middle of a layer. void copy_i32_from_mapped(int32_t* dst, const int32_t* src, int64_t n, void* stream); diff --git a/sycl/include/strata/sycl_doorbell.hpp b/sycl/include/strata/sycl_doorbell.hpp index 6522d0ed..36f71704 100644 --- a/sycl/include/strata/sycl_doorbell.hpp +++ b/sycl/include/strata/sycl_doorbell.hpp @@ -57,11 +57,21 @@ inline void sys_store(volatile uint32_t* p, uint32_t v) { // experts' ids the host reads after the ring) after the ring, the same contract the atomic store had. sycl::atomic_fence(sycl::memory_order::release, sycl::memory_scope::system); } +// The doorbell's PAYLOAD (activations, expert ids, routing weights) is the other half of the handshake: a plain store +// to host USM reaches the host only when the kernel ends on an Arc (xe), so the host would spin on the ring and then +// read a stale payload. Same uncached L1+L3 hint as sys_store, for float/int32. +template inline void sys_store_mapped(T* p, T v) { + sycl::ext::oneapi::experimental::annotated_ptr u(p); + u[0] = v; +} #else inline void sys_store(volatile uint32_t* p, uint32_t v) { sys_atomic_u32(*const_cast(p)).store(v); sycl::atomic_fence(sycl::memory_order::release, sycl::memory_scope::system); } +template inline void sys_store_mapped(T* p, T v) { + sycl::atomic_ref(*p).store(v); +} #endif // Every device spin is bounded. An unbounded spin that never sees its flag is not a hang of one process: the diff --git a/sycl/src/core/layer.cpp b/sycl/src/core/layer.cpp index 32a02253..5cb981f8 100644 --- a/sycl/src/core/layer.cpp +++ b/sycl/src/core/layer.cpp @@ -1376,7 +1376,7 @@ catch (sycl::exception const &exc) { << ", line:" << __LINE__ << std::endl; std::exit(1); } -}; if (!alloc((size_t) g.n_embd * 4, (void**) &db.h_x_f, (void**) &db.d_x_f, "x_f")) return 0; if (!alloc((size_t) k * 4, (void**) &db.h_ids, (void**) &db.d_ids, "ids")) return 0; if (!alloc((size_t) k * 4, (void**) &db.h_weights, (void**) &db.d_weights, "weights")) return 0; if (!alloc(4, (void**) &db.h_seq, (void**) &db.d_seq, "seq")) return 0; if (!alloc(4, (void**) &db.h_flag, (void**) &db.d_flag, "flag")) return 0; return bytes;} +}; if (!alloc((size_t) g.n_embd * 4, (void**) &db.h_x_f, (void**) &db.d_x_f, "x_f")) return 0; if (!alloc((size_t) k * 4, (void**) &db.h_ids, (void**) &db.d_ids, "ids")) return 0; if (!alloc((size_t) k * 4, (void**) &db.h_weights, (void**) &db.d_weights, "weights")) return 0; if (!alloc(64, (void**) &db.h_seq, (void**) &db.d_seq, "seq")) return 0; /* + the payload tag and checksum */ if (!alloc(4, (void**) &db.h_flag, (void**) &db.d_flag, "flag")) return 0; return bytes;} void doorbell_free(Doorbell &db) { if (db.h_x_f) sycl::free(db.h_x_f, dpct::get_in_order_queue()); if (db.h_ids) sycl::free(db.h_ids, dpct::get_in_order_queue()); @@ -1386,7 +1386,7 @@ void doorbell_free(Doorbell &db) { db = Doorbell{}; } void doorbell_reset(const Doorbell& db) { - if (db.h_seq) *db.h_seq = 0; + if (db.h_seq) for (int i = 0; i <= 8; ++i) ((volatile uint32_t*) db.h_seq)[i] = 0; /* + the payload tags */ if (db.h_flag) *(volatile uint32_t*) db.h_flag = 0; } // ================================ THE TWO ENDS OF A TOKEN ================================ diff --git a/sycl/src/core/session.cpp b/sycl/src/core/session.cpp index 59fc6c72..9edd8082 100644 --- a/sycl/src/core/session.cpp +++ b/sycl/src/core/session.cpp @@ -1002,6 +1002,14 @@ bool session_loop(const ModelGeometry &g, int64_t pos, int32_t pos_base, return false; } + // the ring can arrive before its payload on some GPUs (elementwise.hpp): wait until it is whole, or the + // pool reads a partly stale payload and the answer is garbage (the A770's "!!!!") + if (!strata::kernels::doorbell_wait_payload(s.db->h_seq, s.db->h_x_f, g.n_embd, s.db->h_ids, + s.db->h_weights, k, (uint32_t) want)) { + err = "session_loop: layer " + std::to_string(l) + " rang but its payload never arrived whole"; + return false; + } + // ---- **THE GPU'S HALF GOES FIRST, SO IT RUNS WHILE THE CPU DOES ITS HALF.** `Launch` only enqueues: // the quantize and the grouped expert kernel land on `main_cs` and the GPU starts on them immediately, // while the host is still inside `pool` below. Nothing here waits. diff --git a/sycl/src/core/verify.cpp b/sycl/src/core/verify.cpp index bae8ecb3..8339b9da 100644 --- a/sycl/src/core/verify.cpp +++ b/sycl/src/core/verify.cpp @@ -1810,6 +1810,7 @@ void Verifier::stage_inputs(int T, const int32_t* tokens, int64_t pos0) { for (int64_t h = 0; h < g.idx_q_heads; ++h) pi[t * g.idx_q_heads + h] = pos_t; } *(volatile uint32_t*) h_seq_ = 0; + for (int _i = 1; _i <= 8; ++_i) ((volatile uint32_t*) h_seq_)[_i] = 0; /* + the payload tags/checksums */ *(volatile uint32_t*) h_flag_ = 0; *(volatile uint32_t*) h_flagA_ = 0; *(volatile uint32_t*) h_flagB_ = 0; @@ -2009,6 +2010,13 @@ bool Verifier::run(int T, const int32_t *tokens, int64_t pos0, PoolMultiFn pool, cur_layer_ = want - 1; set_plan_slot(grp); const int tb = gtb[grp], n = gte[grp] - gtb[grp]; + // the ring can arrive before its payload (elementwise.hpp): the pool must read a whole one + if (!strata::kernels::doorbell_wait_payload(h_seq_, h_x_ + (size_t) tb * g.n_embd, (int64_t) n * g.n_embd, + h_ids_ + (size_t) tb * ss.k, h_w_ + (size_t) tb * ss.k, + (int64_t) n * ss.k, want)) { + err = "verify: layer " + std::to_string(l) + " rang but its payload never arrived whole"; + return false; + } progress_at("verify window: the CPU experts of layer", l); if (remote_opt_) remote_opt_->begin(h_w_ + (size_t) tb * ss.k, tb, n); if (pool != nullptr) @@ -2823,6 +2831,7 @@ bool Verifier::stage_batch(const int *rows, int S, int hbase, c[2 + j] = j < t - first ? (int32_t) pos[first + j] : -1; } *(volatile uint32_t*) h_seq_ = 0; + for (int _i = 1; _i <= 8; ++_i) ((volatile uint32_t*) h_seq_)[_i] = 0; /* + the payload tags/checksums */ *(volatile uint32_t*) h_flag_ = 0; *(volatile uint32_t*) h_flagA_ = 0; *(volatile uint32_t*) h_flagB_ = 0; @@ -3521,6 +3530,13 @@ int Verifier::service(PoolMultiFn pool, void *user, std::string &err) try { cur_layer_ = want - 1; set_plan_slot(grp); const int tb = gtb[grp], n = gte[grp] - gtb[grp]; + // the ring can arrive before its payload (elementwise.hpp): the pool must read a whole one + if (!strata::kernels::doorbell_wait_payload(h_seq_, h_x_ + (size_t) tb * g.n_embd, (int64_t) n * g.n_embd, + h_ids_ + (size_t) tb * ss.k, h_w_ + (size_t) tb * ss.k, + (int64_t) n * ss.k, want)) { + err = "verify: layer " + std::to_string(l) + " rang but its payload never arrived whole"; + return -1; + } progress_at("verify window (pipelined): the CPU experts of layer", l); if (pool != nullptr) pool(user, h_x_ + (size_t) tb * g.n_embd, h_ids_ + (size_t) tb * ss.k, n, ss.k, diff --git a/sycl/src/kernels/cuda/elementwise.dp.cpp b/sycl/src/kernels/cuda/elementwise.dp.cpp index 44815f33..88719a15 100644 --- a/sycl/src/kernels/cuda/elementwise.dp.cpp +++ b/sycl/src/kernels/cuda/elementwise.dp.cpp @@ -13,6 +13,8 @@ #include #include #include +#include +#include namespace strata::kernels { namespace { @@ -566,6 +568,41 @@ void copy_from_mapped(float* dst, const float* src, int64_t n, void* stream) { check_launch("copy_from_mapped"); } +namespace { +// The published payload's checksum: the wrapping sum of every 32-bit word mixed with its position (murmur3's +// finalizer). A plain sum of the words missed a reordering ([3, 7] -> [7, 3] in the expert ids) and cancelling +// changes; mixed, a stale payload passes only with a chance near 2^-32 per read. It detects; it does not order - +// the host keeps reading until the payload it sees matches (doorbell_payload_ready). Positions: x 0..n-1, +// ids n..n+k-1, weights n+k..n+2k-1. +inline uint32_t dbx_payload_mix(uint32_t word, uint32_t pos) { + uint32_t h = word ^ (pos * 0x9E3779B9u); + h ^= h >> 16; h *= 0x85EBCA6Bu; h ^= h >> 13; h *= 0xC2B2AE35u; h ^= h >> 16; + return h; +} +inline uint32_t dbx_payload_sum(const float* x, int64_t n, const int32_t* ids, const float* w, int64_t k, + bool has_weights) { + uint32_t s = 0; + for (int64_t j = 0; j < n; ++j) s += dbx_payload_mix(sycl::bit_cast(x[j]), (uint32_t) j); + for (int64_t j = 0; j < k; ++j) s += dbx_payload_mix((uint32_t) ids[j], (uint32_t) (n + j)); + if (has_weights) + for (int64_t j = 0; j < k; ++j) s += dbx_payload_mix(sycl::bit_cast(w[j]), (uint32_t) (n + k + j)); + return s; +} +// The ring, its tag and its checksum, in slot r % 4 of the sequence word (elementwise.hpp). Written last, after a +// system fence over the uncached payload stores. +__dpct_inline__ void dbx_ring_commit(uint32_t* seq, uint32_t part, bool group_ok, uint32_t ring) { +#if defined(__HIPCC__) // #697: HIP only; on RDNA4 the volatile store alone can sit in L2 until the stream syncs + (void) group_ok; +#endif + if (group_ok) { + strata::sys_store(seq + 2 + 2 * (ring % 4), part); + strata::sys_store(seq + 1 + 2 * (ring % 4), ring); + sycl::atomic_fence(sycl::memory_order::seq_cst, sycl::memory_scope::system); + strata::sys_store(seq, ring); + } +} +} // namespace + __dpct_inline__ void doorbell_publish_kernel(const float *__restrict__ x, const int32_t *__restrict__ ids, const float *__restrict__ w, int n, @@ -573,31 +610,31 @@ __dpct_inline__ void doorbell_publish_kernel(const float *__restrict__ x, int32_t *ids_out, float *w_out, uint32_t *seq) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); + const int i = (int) item_ct1.get_local_id(2); + const int nt = (int) item_ct1.get_local_range(2); + uint32_t part = 0; #pragma unroll - for (int i = item_ct1.get_local_id(2); i < n; - i += item_ct1.get_local_range(2)) x_out[i] = x[i]; - if ((int)item_ct1.get_local_id(2) < k) { - ids_out[item_ct1.get_local_id(2)] = ids[item_ct1.get_local_id(2)]; - if (w_out != nullptr) w_out[item_ct1.get_local_id(2)] = - w[item_ct1.get_local_id(2)]; + for (int j = i; j < n; j += nt) { + const float v = x[j]; + strata::sys_store_mapped(x_out + j, v); + part += dbx_payload_mix(sycl::bit_cast(v), (uint32_t) j); } - /* - DPCT1078: Consider replacing memory_order::acq_rel with - memory_order::seq_cst for correctness if strong memory order restrictions - are needed. - */ - sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::system); - /* - DPCT1065: Consider replacing sycl::nd_item::barrier() with - sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better - performance if there is no access to global memory. - */ + if (i < k) { + const int32_t id = ids[i]; + strata::sys_store_mapped(ids_out + i, id); + part += dbx_payload_mix((uint32_t) id, (uint32_t) (n + i)); + if (w_out != nullptr) { + const float wv = w[i]; + strata::sys_store_mapped(w_out + i, wv); + part += dbx_payload_mix(sycl::bit_cast(wv), (uint32_t) (n + k + i)); + } + } + const uint32_t sum = sycl::reduce_over_group(item_ct1.get_group(), part, sycl::plus()); + sycl::atomic_fence(sycl::memory_order::seq_cst, sycl::memory_scope::system); item_ct1.barrier(); - if (item_ct1.get_local_id(2) == 0) { - strata::sys_store(seq, strata::sys_load(seq) + 1u); -#if defined(__HIPCC__) // #697: HIP only; on RDNA4 the volatile store alone can sit in L2 until the stream syncs - __threadfence_system(); -#endif + if (i == 0) { + const uint32_t next = strata::sys_load(seq) + 1u; + dbx_ring_commit(seq, sum, true, next); } } @@ -605,52 +642,31 @@ __dpct_inline__ void doorbell_publish_res_kernel( const float *__restrict__ x, const int32_t *__restrict__ ids, const int32_t *__restrict__ d_res, int n_expert, int n, int k, float *x_out, int32_t *ids_out, uint32_t *seq) { + // d_res/n_expert decided whether x was copied; the payload is now published whole and the checksum covers x and + // the ids (this variant publishes no weights), so the two are unused. + (void) d_res; + (void) n_expert; auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); - int any_miss = 0; - if ((int)item_ct1.get_local_id(2) < k) { - const int32_t id = ids[item_ct1.get_local_id(2)]; - ids_out[item_ct1.get_local_id(2)] = id; - if (d_res == nullptr || id < 0 || id >= n_expert || d_res[id] < 0) any_miss = 1; - } - /* - DPCT1065: Consider replacing sycl::nd_item::barrier() with - sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better - performance if there is no access to global memory. - */ - if ((item_ct1.barrier(), - sycl::any_of_group( - sycl::ext::oneapi::this_work_item::get_work_group<3>(), - any_miss))) { + const int i = (int) item_ct1.get_local_id(2); + const int nt = (int) item_ct1.get_local_range(2); + uint32_t part = 0; #pragma unroll - for (int i = item_ct1.get_local_id(2); i < n; - i += item_ct1.get_local_range(2)) x_out[i] = x[i]; - /* - DPCT1078: Consider replacing memory_order::acq_rel with - memory_order::seq_cst for correctness if strong memory order - restrictions are needed. - */ - sycl::atomic_fence(sycl::memory_order::acq_rel, - sycl::memory_scope::system); - } else if ((int)item_ct1.get_local_id(2) < k) { - /* - DPCT1078: Consider replacing memory_order::acq_rel with - memory_order::seq_cst for correctness if strong memory order - restrictions are needed. - */ - sycl::atomic_fence(sycl::memory_order::acq_rel, - sycl::memory_scope::system); + for (int j = i; j < n; j += nt) { + const float v = x[j]; + strata::sys_store_mapped(x_out + j, v); + part += dbx_payload_mix(sycl::bit_cast(v), (uint32_t) j); } - /* - DPCT1065: Consider replacing sycl::nd_item::barrier() with - sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better - performance if there is no access to global memory. - */ + if (i < k) { + const int32_t id = ids[i]; + strata::sys_store_mapped(ids_out + i, id); + part += dbx_payload_mix((uint32_t) id, (uint32_t) (n + i)); + } + const uint32_t sum = sycl::reduce_over_group(item_ct1.get_group(), part, sycl::plus()); + sycl::atomic_fence(sycl::memory_order::seq_cst, sycl::memory_scope::system); item_ct1.barrier(); - if (item_ct1.get_local_id(2) == 0) { - strata::sys_store(seq, strata::sys_load(seq) + 1u); -#if defined(__HIPCC__) // #697: HIP only; on RDNA4 the volatile store alone can sit in L2 until the stream syncs - __threadfence_system(); -#endif + if (i == 0) { + const uint32_t next = strata::sys_load(seq) + 1u; + dbx_ring_commit(seq, sum, true, next); } } @@ -661,37 +677,27 @@ __dpct_inline__ void doorbell_publish_value_kernel( const float *__restrict__ w, int n, int k, float *x_out, int32_t *ids_out, float *w_out, uint32_t *seq, uint32_t value) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); + const int i = (int) item_ct1.get_local_id(2); + const int nt = (int) item_ct1.get_local_range(2); + uint32_t part = 0; #pragma unroll - for (int i = item_ct1.get_local_id(2); i < n; - i += item_ct1.get_local_range(2)) x_out[i] = x[i]; - if ((int)item_ct1.get_local_id(2) < k) { - ids_out[item_ct1.get_local_id(2)] = ids[item_ct1.get_local_id(2)]; - w_out[item_ct1.get_local_id(2)] = w[item_ct1.get_local_id(2)]; + for (int j = i; j < n; j += nt) { + const float v = x[j]; + strata::sys_store_mapped(x_out + j, v); + part += dbx_payload_mix(sycl::bit_cast(v), (uint32_t) j); } - /* - DPCT1078: Consider replacing memory_order::acq_rel with - memory_order::seq_cst for correctness if strong memory order restrictions - are needed. - */ - sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::system); - item_ct1.barrier(sycl::access::fence_space::local_space); - if (item_ct1.get_local_id(2) == 0) { - /* - DPCT1078: Consider replacing memory_order::acq_rel with - memory_order::seq_cst for correctness if strong memory order - restrictions are needed. - */ - sycl::atomic_fence(sycl::memory_order::acq_rel, - sycl::memory_scope::system); - *(volatile uint32_t*) seq = value; - /* - DPCT1078: Consider replacing memory_order::acq_rel with - memory_order::seq_cst for correctness if strong memory order - restrictions are needed. - */ - sycl::atomic_fence(sycl::memory_order::acq_rel, - sycl::memory_scope::system); + if (i < k) { + const int32_t id = ids[i]; + strata::sys_store_mapped(ids_out + i, id); + part += dbx_payload_mix((uint32_t) id, (uint32_t) (n + i)); + const float wv = w[i]; + strata::sys_store_mapped(w_out + i, wv); + part += dbx_payload_mix(sycl::bit_cast(wv), (uint32_t) (n + k + i)); } + const uint32_t sum = sycl::reduce_over_group(item_ct1.get_group(), part, sycl::plus()); + sycl::atomic_fence(sycl::memory_order::seq_cst, sycl::memory_scope::system); + item_ct1.barrier(); + if (i == 0) dbx_ring_commit(seq, sum, true, value); } void doorbell_publish_value(const float* x, const int32_t* ids, const float* weights, int64_t n, int64_t k, @@ -770,6 +776,27 @@ void doorbell_publish_res(const float* x, const int32_t* ids, const int32_t* d_r check_launch("doorbell_publish_res"); } +bool doorbell_payload_ready(const uint32_t* h_seq, const float* h_x, int64_t n, const int32_t* h_ids, + const float* h_weights, int64_t k, uint32_t want) { + const volatile uint32_t* w = h_seq + 1 + 2 * (want % 4); // this ring's tag and checksum (elementwise.hpp) + if (w[0] != want) return false; + std::atomic_thread_fence(std::memory_order_acquire); + // the two publish shapes: with the routing weights (doorbell_publish / _value) or without (doorbell_publish_res) + return dbx_payload_sum(h_x, n, h_ids, h_weights, k, true) == w[1] || + dbx_payload_sum(h_x, n, h_ids, h_weights, k, false) == w[1]; +} + +bool doorbell_wait_payload(const uint32_t* h_seq, const float* h_x, int64_t n, const int32_t* h_ids, + const float* h_weights, int64_t k, uint32_t want, int timeout_ms) { + if (doorbell_payload_ready(h_seq, h_x, n, h_ids, h_weights, k, want)) return true; + const auto t0 = std::chrono::steady_clock::now(); + while (!doorbell_payload_ready(h_seq, h_x, n, h_ids, h_weights, k, want)) { + for (int i = 0; i < 64; ++i) __builtin_ia32_pause(); + if (std::chrono::steady_clock::now() - t0 > std::chrono::milliseconds(timeout_ms)) return false; + } + return true; +} + __dpct_inline__ void copy_i32_from_mapped_kernel(int32_t *__restrict__ dst, const volatile int32_t *src, int n) {