diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt index 818571ff..5b92f355 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -300,6 +300,12 @@ if(STRATA_SYCL_PARITY) target_link_libraries(q6k_align_bench PRIVATE strata_kernels) add_executable(gr_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/gr_bench.cpp) # fused GR read timing target_link_libraries(gr_bench PRIVATE strata_kernels) + add_executable(sycl_router_fast ${CMAKE_CURRENT_SOURCE_DIR}/probe/router_fast.cpp) + target_link_libraries(sycl_router_fast PRIVATE strata_kernels) + add_test(NAME sycl_router_fast COMMAND sycl_router_fast) + add_executable(sycl_payload_pairs ${CMAKE_CURRENT_SOURCE_DIR}/probe/payload_pairs.cpp) + target_link_libraries(sycl_payload_pairs PRIVATE strata_kernels) + add_test(NAME sycl_payload_pairs COMMAND sycl_payload_pairs) add_executable(xmx_gemm_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/xmx_gemm_bench.cpp) # XMX quantized GEMM vs dequant+oneMKL target_link_libraries(xmx_gemm_bench PRIVATE strata_kernels strata_prefill) # -qmkl for the oneMKL reference add_executable(xmx_group_parity ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/xmx_group_parity.cpp) # A770: fused grouped XMX diff --git a/sycl/include/strata/sycl_doorbell.hpp b/sycl/include/strata/sycl_doorbell.hpp index ec93e210..34a4a5e0 100644 --- a/sycl/include/strata/sycl_doorbell.hpp +++ b/sycl/include/strata/sycl_doorbell.hpp @@ -18,8 +18,31 @@ #include "strata/sycl_queue.hpp" namespace strata { +// STRATA_SYCL_A770_FAST=1 (opt-in, experimental, PR #1710): the A770 router and 64-bit paired payload transfers. +inline bool a770_fast() { + static const bool on = [] { const char* v = std::getenv("STRATA_SYCL_A770_FAST"); return v != nullptr && v[0] == '1'; }(); + return on; +} using sys_atomic_u32 = sycl::atomic_ref; +// Payload pairs use 64-bit transactions; flags remain 32-bit. Keep the same +// cache-bypass contract while transferring two floats per PCIe transaction. +template inline T sys_load_mapped(const T* p) { + using read_props = decltype(sycl::ext::oneapi::experimental::properties( + sycl::ext::intel::experimental::read_hint>)); + sycl::ext::oneapi::experimental::annotated_ptr u(const_cast(p)); + return u[0]; +} +inline sycl::float4 load_mapped_float4(const sycl::float4* p) { + const uint64_t* words = reinterpret_cast(p); + const auto lo = sycl::bit_cast(sys_load_mapped(words)); + const auto hi = sycl::bit_cast(sys_load_mapped(words + 1)); + return sycl::float4(lo.x(), lo.y(), hi.x(), hi.y()); +} + // 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 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 diff --git a/sycl/probe/payload_pairs.cpp b/sycl/probe/payload_pairs.cpp new file mode 100644 index 00000000..b64c2383 --- /dev/null +++ b/sycl/probe/payload_pairs.cpp @@ -0,0 +1,61 @@ +// Test paired mapped transfers, odd tails, unaligned scalar fallback, and +// the checksum contracts of all three publish variants over repeated rings. +#include +#include +#include "strata/sycl_queue.hpp" +#include "strata/kernels/elementwise.hpp" +#include +#include +#include + +int main() { + auto& q = *strata::q_of(nullptr); + using namespace strata::kernels; + constexpr int capacity = 2560, k = 10, experts = 64; + auto* hx = (float*)strata::host_malloc_polled((capacity + 4)*sizeof(float), q); + auto* hi = (int32_t*)strata::host_malloc_polled(k*sizeof(int32_t), q); + auto* hw = (float*)strata::host_malloc_polled(k*sizeof(float), q); + auto* seq = (uint32_t*)strata::host_malloc_polled(64, q); + auto* dx = sycl::malloc_device(capacity, q); + auto* di = sycl::malloc_device(k, q); + auto* dw = sycl::malloc_device(k, q); + auto* dr = sycl::malloc_device(experts, q); + std::vector x(capacity), weights(k), copied(capacity); + std::vector ids(k); + for (int j = 0; j < capacity; ++j) x[j] = (j % 123 - 61)*0.25f; + for (int j = 0; j < k; ++j) { ids[j] = j*3; weights[j] = (j + 1)*0.03125f; } + q.memcpy(dx, x.data(), x.size()*sizeof(float)); + q.memcpy(di, ids.data(), k*sizeof(int32_t)); + q.memcpy(dw, weights.data(), k*sizeof(float)); + q.memset(dr, 0, experts*sizeof(int32_t)); + q.wait_and_throw(); + int failed = 0; + for (int n : {1, 3, 17, 2559, 2560}) + for (int offset : {0, 1}) + for (int variant = 0; variant < 3; ++variant) { + std::memset(seq, 0, 64); + for (uint32_t ring = 1; ring <= 12; ++ring) { + std::memset(hx, 0x7f, (capacity + 4)*sizeof(float)); + std::memset(hi, 0, k*sizeof(int32_t)); + std::memset(hw, 0, k*sizeof(float)); + if (variant == 0) doorbell_publish(dx, di, dw, n, k, hx+offset, hi, hw, seq, &q); + else if (variant == 1) doorbell_publish_res(dx, di, dr, experts, n, k, hx+offset, hi, seq, &q); + else doorbell_publish_value(dx, di, dw, n, k, hx+offset, hi, hw, seq, ring, &q); + q.wait_and_throw(); + bool ok = seq[0] == ring && !std::memcmp(hx+offset, x.data(), n*sizeof(float)) && + !std::memcmp(hi, ids.data(), k*sizeof(int32_t)) && + doorbell_payload_ready(seq, hx+offset, n, hi, hw, k, ring); + if (variant != 1) ok = ok && !std::memcmp(hw, weights.data(), k*sizeof(float)); + if (!ok) { ++failed; std::printf("FAIL n=%d offset=%d variant=%d ring=%u\n", n, offset, variant, ring); } + } + } + std::memcpy(hx, x.data(), x.size()*sizeof(float)); + copy_from_mapped(dx, hx, capacity, &q); + q.memcpy(copied.data(), dx, capacity*sizeof(float)).wait(); + if (std::memcmp(copied.data(), x.data(), capacity*sizeof(float))) ++failed; + for (void* p : {static_cast(hx), static_cast(hi), static_cast(hw), static_cast(seq)}) + strata::host_free_polled(p, q); + for (void* p : {static_cast(dx), static_cast(di), static_cast(dw), static_cast(dr)}) sycl::free(p, q); + std::printf("paired payload and mapped read: %d failures\n", failed); + return failed != 0; +} diff --git a/sycl/probe/router_fast.cpp b/sycl/probe/router_fast.cpp new file mode 100644 index 00000000..41699441 --- /dev/null +++ b/sycl/probe/router_fast.cpp @@ -0,0 +1,71 @@ +// Compare all output bits against the existing router, including ties, NaN +// tails and the fast kernel's forced serial-sum fallback. Also time decode calls. +#include +#include +#include "strata/sycl_queue.hpp" +#include "strata/kernels/router_top10.hpp" +#include +#include +#include +#include +#include +#include + +static int compare(int experts, int k, int rows) { + auto& q = *strata::q_of(nullptr); + std::mt19937 rng(17); + std::normal_distribution normal(0, 2); + std::vector logits((size_t)rows * experts); + for (size_t i = 0; i < logits.size(); ++i) { + const size_t r = i / experts; + float x = normal(rng) * (r % 7 == 5 ? 40 : 1); + if (r % 3 == 1) x = std::round(x * 4) / 4; + if (r % 3 == 2) x = std::round(x * 64) / 64; + if (r % 1000 == 999 && i % experts == 77) x = NAN; + if (r % 1000 == 998) x = 1; + logits[i] = x; + } + float* dl = sycl::malloc_device(logits.size(), q); + q.memcpy(dl, logits.data(), logits.size() * sizeof(float)); + const size_t n = (size_t)rows * k; + float* dw[3]; int* di[3]; + std::vector weights[3]; std::vector ids[3]; + for (int v = 0; v < 3; ++v) { + dw[v] = sycl::malloc_device(n, q); + di[v] = sycl::malloc_device(n, q); + q.memset(dw[v], 0x3c, n * sizeof(float)); + q.memset(di[v], 0x11, n * sizeof(int)); + if (!strata::kernels::router_top10_variant(dl, rows, experts, k, di[v], dw[v], &q, v)) return 2; + weights[v].resize(n); ids[v].resize(n); + q.memcpy(weights[v].data(), dw[v], n * sizeof(float)); + q.memcpy(ids[v].data(), di[v], n * sizeof(int)); + q.wait_and_throw(); + } + int fails = 0; + for (int v = 1; v < 3; ++v) { + int bad = 0; + for (int r = 0; r < rows; ++r) + if (std::memcmp(ids[0].data() + (size_t)r*k, ids[v].data() + (size_t)r*k, k*sizeof(int)) || + std::memcmp(weights[0].data() + (size_t)r*k, weights[v].data() + (size_t)r*k, k*sizeof(float))) ++bad; + std::printf("experts %d k %d rows %d variant %d: %d rows differ\n", experts, k, rows, v, bad); + fails += bad != 0; + } + for (int v = 0; v < 2; ++v) { + constexpr int reps = 300; + auto start = std::chrono::steady_clock::now(); + for (int r = 0; r < reps; ++r) + strata::kernels::router_top10_variant(dl + (r % 64)*experts, 1, experts, k, di[v], dw[v], &q, v); + q.wait_and_throw(); + auto us = std::chrono::duration(std::chrono::steady_clock::now()-start).count()/reps; + std::printf("variant %d single-token call: %.2f us (host submission + execution)\n", v, us); + } + for (int v = 0; v < 3; ++v) { sycl::free(dw[v], q); sycl::free(di[v], q); } + sycl::free(dl, q); + return fails; +} + +int main() { + int failures = compare(512, 10, 65536) + compare(256, 8, 8192) + compare(256, 10, 8192) + compare(512, 32, 1024); + std::printf("FAILURES: %d\n", failures); + return failures != 0; +} diff --git a/sycl/src/kernels/cuda/elementwise.dp.cpp b/sycl/src/kernels/cuda/elementwise.dp.cpp index 7bf4a7d4..1faf32ef 100644 --- a/sycl/src/kernels/cuda/elementwise.dp.cpp +++ b/sycl/src/kernels/cuda/elementwise.dp.cpp @@ -425,7 +425,7 @@ __dpct_inline__ void copy_from_mapped_kernel(sycl::float4 *__restrict__ dst, item_ct1.get_local_id(2); i < n4; i += (int64_t)item_ct1.get_group_range(2) * item_ct1.get_local_range(2)) { - const sycl::float4 v = const_cast(src)[i]; + const sycl::float4 v = strata::load_mapped_float4(src + i); dst[i] = v; } } @@ -468,7 +468,7 @@ __dpct_inline__ void copy_rows_from_mapped_kernel( #pragma unroll for (int64_t i = item_ct1.get_local_id(2); i < row4; i += item_ct1.get_local_range(2)) - d[i] = const_cast(sr)[i]; + d[i] = strata::load_mapped_float4(sr + i); } } namespace { @@ -588,6 +588,30 @@ inline uint32_t dbx_payload_sum(const float* x, int64_t n, const int32_t* ids, c for (int64_t j = 0; j < k; ++j) s += dbx_payload_mix(sycl::bit_cast(w[j]), (uint32_t) (n + k + j)); return s; } +// STRATA_SYCL_A770_FAST=1 (opt-in): pair the activation stores without changing the checksum's word/position +// order. Odd sizes or unaligned mapped pointers retain the scalar path. +__dpct_inline__ uint32_t dbx_publish_x(const float* x, float* out, int n, int lane, int threads, bool pair, bool plain) { + uint32_t part = 0; + if (pair && !plain && ((uintptr_t)out & 7u) == 0) { + for (int j = 2 * lane; j + 1 < n; j += 2 * threads) { + const uint32_t a = sycl::bit_cast(x[j]); + const uint32_t b = sycl::bit_cast(x[j + 1]); + strata::sys_store_mapped(reinterpret_cast(out + j), + (uint64_t)a | ((uint64_t)b << 32)); + part += dbx_payload_mix(a, (uint32_t)j) + dbx_payload_mix(b, (uint32_t)(j + 1)); + } + if ((n & 1) && lane == 0) { + strata::sys_store_mapped(plain, out + n - 1, x[n - 1]); + part += dbx_payload_mix(sycl::bit_cast(x[n - 1]), (uint32_t)(n - 1)); + } + } else { + for (int j = lane; j < n; j += threads) { + strata::sys_store_mapped(plain, out + j, x[j]); + part += dbx_payload_mix(sycl::bit_cast(x[j]), (uint32_t)j); + } + } + return part; +} // 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) { @@ -615,17 +639,11 @@ __dpct_inline__ void doorbell_publish_kernel(const float *__restrict__ x, const float *__restrict__ w, int n, int k, float *x_out, int32_t *ids_out, float *w_out, - uint32_t *seq, bool plain) { + uint32_t *seq, bool plain, bool pair) { 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 j = i; j < n; j += nt) { - const float v = x[j]; - strata::sys_store_mapped(plain, x_out + j, v); - part += dbx_payload_mix(sycl::bit_cast(v), (uint32_t) j); - } + uint32_t part = dbx_publish_x(x, x_out, n, i, nt, pair, plain); if (i < k) { const int32_t id = ids[i]; strata::sys_store_mapped(plain, ids_out + i, id); @@ -648,7 +666,7 @@ __dpct_inline__ void doorbell_publish_kernel(const float *__restrict__ x, __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, bool full, bool plain) { + int32_t *ids_out, uint32_t *seq, bool full, bool plain, bool pair) { // full (STRATA_DOORBELL_CHECK=1): the payload is published whole and the checksum covers x and the ids (this // variant publishes no weights). Otherwise d_res/n_expert decide whether x is copied, as before the checksum: // when every routed expert is resident the host never reads x, and an uncached copy of it costs the A750 ~10% @@ -675,13 +693,7 @@ __dpct_inline__ void doorbell_publish_res_kernel( } 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 j = i; j < n; j += nt) { - const float v = x[j]; - strata::sys_store_mapped(plain, x_out + j, v); - part += dbx_payload_mix(sycl::bit_cast(v), (uint32_t) j); - } + uint32_t part = dbx_publish_x(x, x_out, n, i, nt, pair, plain); if (i < k) { const int32_t id = ids[i]; strata::sys_store_mapped(plain, ids_out + i, id); @@ -701,17 +713,11 @@ __dpct_inline__ void doorbell_publish_res_kernel( __dpct_inline__ void doorbell_publish_value_kernel( const float *__restrict__ x, const int32_t *__restrict__ ids, const float *__restrict__ w, int n, int k, float *x_out, int32_t *ids_out, - float *w_out, uint32_t *seq, uint32_t value, bool plain) { + float *w_out, uint32_t *seq, uint32_t value, bool plain, bool pair) { 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 j = i; j < n; j += nt) { - const float v = x[j]; - strata::sys_store_mapped(plain, x_out + j, v); - part += dbx_payload_mix(sycl::bit_cast(v), (uint32_t) j); - } + uint32_t part = dbx_publish_x(x, x_out, n, i, nt, pair, plain); if (i < k) { const int32_t id = ids[i]; strata::sys_store_mapped(plain, ids_out + i, id); @@ -731,6 +737,7 @@ void doorbell_publish_value(const float* x, const int32_t* ids, const float* wei void* stream) { if (k > 1024) { std::fprintf(stderr, "doorbell_publish: k too large\n"); std::exit(1); } const bool plain = strata::doorbell_plain_payload() || g_plain_payload.load(std::memory_order_relaxed); + const bool pair = strata::a770_fast(); /* DPCT1049: The work-group size passed to the SYCL kernel may exceed the limit. To get the device limit, query info::device::max_work_group_size. @@ -740,7 +747,6 @@ void doorbell_publish_value(const float* x, const int32_t* ids, const float* wei auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; - const bool plain = g_plain_payload.load(std::memory_order_relaxed); strata::q_of(stream) ->parallel_for< dpct_kernel_name>( @@ -749,7 +755,7 @@ void doorbell_publish_value(const float* x, const int32_t* ids, const float* wei exp_props, [=](sycl::nd_item<3> item_ct1) { doorbell_publish_value_kernel(x, ids, weights, (int)n, (int)k, x_out, ids_out, - weights_out, d_seq, value, plain); + weights_out, d_seq, value, plain, pair); }); } } @@ -758,6 +764,7 @@ void doorbell_publish(const float* x, const int32_t* ids, const float* weights, int32_t* ids_out, float* weights_out, uint32_t* d_seq, void* stream) { if (k > 1024) { std::fprintf(stderr, "doorbell_publish: k too large\n"); std::exit(1); } const bool plain = strata::doorbell_plain_payload() || g_plain_payload.load(std::memory_order_relaxed); + const bool pair = strata::a770_fast(); /* DPCT1049: The work-group size passed to the SYCL kernel may exceed the limit. To get the device limit, query info::device::max_work_group_size. @@ -767,7 +774,6 @@ void doorbell_publish(const float* x, const int32_t* ids, const float* weights, auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; - const bool plain = g_plain_payload.load(std::memory_order_relaxed); strata::q_of(stream) ->parallel_for< dpct_kernel_name>( @@ -775,7 +781,7 @@ void doorbell_publish(const float* x, const int32_t* ids, const float* weights, sycl::range(1, 1, 1024)), exp_props, [=](sycl::nd_item<3> item_ct1) { doorbell_publish_kernel(x, ids, weights, (int)n, (int)k, - x_out, ids_out, weights_out, d_seq, plain); + x_out, ids_out, weights_out, d_seq, plain, pair); }); } check_launch("doorbell_publish"); @@ -787,6 +793,7 @@ void doorbell_publish_res(const float* x, const int32_t* ids, const int32_t* d_r static const bool full = [] { const char* v = std::getenv("STRATA_DOORBELL_CHECK"); return v != nullptr && v[0] == '1'; }(); const bool fl = full; const bool plain = strata::doorbell_plain_payload() || g_plain_payload.load(std::memory_order_relaxed); + const bool pair = strata::a770_fast(); /* DPCT1049: The work-group size passed to the SYCL kernel may exceed the limit. To get the device limit, query info::device::max_work_group_size. @@ -796,7 +803,6 @@ void doorbell_publish_res(const float* x, const int32_t* ids, const int32_t* d_r auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; - const bool plain = g_plain_payload.load(std::memory_order_relaxed); strata::q_of(stream) ->parallel_for< dpct_kernel_name>( @@ -804,7 +810,7 @@ void doorbell_publish_res(const float* x, const int32_t* ids, const int32_t* d_r sycl::range(1, 1, 1024)), exp_props, [=](sycl::nd_item<3> item_ct1) { doorbell_publish_res_kernel(x, ids, d_res, n_expert, (int)n, - (int)k, x_out, ids_out, d_seq, fl, plain); + (int)k, x_out, ids_out, d_seq, fl, plain, pair); }); } check_launch("doorbell_publish_res"); diff --git a/sycl/src/kernels/cuda/router_top10.dp.cpp b/sycl/src/kernels/cuda/router_top10.dp.cpp index 73d5b14a..21a3c1a8 100644 --- a/sycl/src/kernels/cuda/router_top10.dp.cpp +++ b/sycl/src/kernels/cuda/router_top10.dp.cpp @@ -55,9 +55,11 @@ #include #include #include "strata/sycl_queue.hpp" +#include "strata/sycl_doorbell.hpp" #include "strata/kernels/router_top10.hpp" #include +#include #include #include @@ -66,6 +68,116 @@ namespace { constexpr int RT_MAX_THREADS = 512; +// The fork's parity-tested SYCL implementation of the HIP fast router below. +// A tree sum brackets the serial reciprocal; ambiguous rounding falls back to +// the original ascending sum. Selection and renormalisation stay in subgroup 0. +template +void launch_sycl_fast(sycl::queue& q, const float* logits, int n_tokens, int n_expert, int k, + int* ids, float* weights) { + const int threads = (std::min(n_expert, RT_MAX_THREADS) + 31) & ~31; + q.submit([&](sycl::handler& h) { + sycl::local_accessor s_ex(sycl::range<1>(n_expert), h); + sycl::local_accessor s_red(sycl::range<1>(RT_MAX_THREADS / 32), h); + sycl::local_accessor s_dred(sycl::range<1>(RT_MAX_THREADS / 32), h); + sycl::local_accessor s_inv(sycl::range<1>(1), h); + h.parallel_for(sycl::nd_range<1>(sycl::range<1>((size_t)n_tokens * threads), + sycl::range<1>(threads)), + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(32)]] { + const auto sg = it.get_sub_group(); + const int t = it.get_group(0), tid = it.get_local_id(0); + const int lane = tid & 31, warp = tid >> 5; + const int nt = it.get_local_range(0), nw = (nt + 31) >> 5; + const float* l = logits + (size_t)t * n_expert; + auto bar = [&] { sycl::group_barrier(it.get_group()); }; + float mx = -INFINITY; + for (int e = tid; e < n_expert; e += nt) mx = sycl::fmax(mx, l[e]); + for (int off = 16; off > 0; off >>= 1) + mx = sycl::fmax(mx, sycl::shift_group_left(sg, mx, off)); + if (lane == 0) s_red[warp] = mx; + bar(); + if (tid < 32) { + float v = tid < nw ? s_red[tid] : -INFINITY; + for (int off = 16; off > 0; off >>= 1) + v = sycl::fmax(v, sycl::shift_group_left(sg, v, off)); + if (tid == 0) s_red[0] = v; + } + bar(); + mx = s_red[0]; + double part = 0.0; + for (int e = tid; e < n_expert; e += nt) { + const double x = sycl::exp((double)l[e] - (double)mx); + s_ex[e] = x; + part += x; + } + for (int off = 16; off > 0; off >>= 1) + part += sycl::permute_group_by_xor(sg, part, off); + if (lane == 0) s_dred[warp] = part; + bar(); + double tot = 0.0; + for (int w = 0; w < nw; ++w) tot += s_dred[w]; + const double lo = tot * (1.0 - 0x1p-40), hi = tot * (1.0 + 0x1p-40); + const float f_lo = (float)(1.0 / hi), f_hi = (float)(1.0 / lo); + float inv; + if (!FORCE_SERIAL && tot >= 1.0 && hi < 1e300 && f_lo == f_hi) { + inv = f_lo; + } else { + if (tid == 0) { + double sum = 0.0; + for (int e = 0; e < n_expert; ++e) sum += s_ex[e]; + s_inv[0] = (float)(1.0 / sum); + } + bar(); + inv = s_inv[0]; + } + if (warp != 0) return; + constexpr int PER = 16; + float pv[PER]; + unsigned live = 0; + for (int j = 0; j < PER; ++j) { + const int e = lane + 32 * j; + pv[j] = -INFINITY; + if (e < n_expert) { pv[j] = (float)(s_ex[e] * inv); live |= 1u << j; } + } + float my_w = 0.0f; + int my_id = 0, nsel = 0; + for (int i = 0; i < k; ++i) { + float bv = -INFINITY; + int bi = n_expert; + for (int j = 0; j < PER; ++j) + if (((live >> j) & 1u) && pv[j] > bv) { bv = pv[j]; bi = lane + 32 * j; } + for (int off = 16; off > 0; off >>= 1) { + const float ov = sycl::permute_group_by_xor(sg, bv, off); + const int oi = sycl::permute_group_by_xor(sg, bi, off); + if (ov > bv || (ov == bv && oi < bi)) { bv = ov; bi = oi; } + } + if (bi >= n_expert) break; + if ((bi & 31) == lane) live &= ~(1u << (bi >> 5)); + if (lane == i) { my_w = bv; my_id = bi; } + nsel = i + 1; + } + if (nsel == k) { + double s = 0.0; + for (int i = 0; i < k; ++i) s += (double)sycl::select_from_group(sg, my_w, i); + const double sc = sycl::fmax(s, 6.103515625e-05); + if (lane < k) { + ids[(size_t)t * k + lane] = my_id; + weights[(size_t)t * k + lane] = (float)((double)my_w / sc); + } + } else { + if (lane < nsel) { ids[(size_t)t * k + lane] = my_id; weights[(size_t)t * k + lane] = my_w; } + sycl::group_barrier(sg); + if (lane == 0) { + double s = 0.0; + for (int i = 0; i < k; ++i) s += (double)weights[(size_t)t * k + i]; + const double sc = sycl::fmax(s, 6.103515625e-05); + for (int i = 0; i < k; ++i) + weights[(size_t)t * k + i] = (float)((double)weights[(size_t)t * k + i] / sc); + } + } + }); + }); +} + /// One BLOCK per token, so the reductions have somewhere to happen. `n_tokens` is 1 in decode; the grid keeps /// the batch case working without a second code path. /* @@ -540,14 +652,38 @@ bool router_top10_variant(const float* logits, int n_tokens, int n_expert, int k launch_generic(logits, n_tokens, n_expert, k, ids, weights, stream, variant); return cudaGetLastError() == cudaSuccess; #else - (void) logits; (void) n_tokens; (void) n_expert; (void) k; (void) ids; (void) weights; (void) stream; - (void) variant; - return false; + if (n_tokens <= 0 || n_expert <= 64 || n_expert > 512 || k <= 0 || k > 32 || variant < 0 || variant > 2) + return false; + auto& q = *strata::q_of(stream); + if (variant == 1) launch_sycl_fast(q, logits, n_tokens, n_expert, k, ids, weights); + else if (variant == 2) launch_sycl_fast(q, logits, n_tokens, n_expert, k, ids, weights); + else { + const int threads = (std::min(n_expert, RT_MAX_THREADS) + 31) & ~31; + const size_t taken_bytes = ((size_t)n_expert + 15u) & ~size_t(15); + const size_t smem = taken_bytes + n_expert * (sizeof(double) + sizeof(float)); + q.submit([&](sycl::handler& h) { + sycl::local_accessor local(sycl::range<1>(smem), h); + h.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, (size_t)n_tokens * threads), + sycl::range<3>(1, 1, threads)), + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { + router_top10_kernel(logits, n_tokens, n_expert, k, ids, weights, + local.get_multi_ptr().get()); + }); + }); + } + if (stream == nullptr) q.wait_and_throw(); + return true; #endif } void router_top10(const float *logits, int n_tokens, int n_expert, int k, int *ids, float *weights, void *stream) try { + if (strata::a770_fast() && n_tokens > 0 && n_expert > 64 && n_expert <= 512 && k > 0 && k <= 32) { + auto& q = *strata::q_of(stream); + launch_sycl_fast(q, logits, n_tokens, n_expert, k, ids, weights); + if (stream == nullptr) q.wait_and_throw(); + return; + } #if defined(__HIPCC__) { #if defined(STRATA_HIP_GFX906) diff --git a/sycl/src/kernels/cuda/verify_kernels.dp.cpp b/sycl/src/kernels/cuda/verify_kernels.dp.cpp index 1a8c34e7..52c665ba 100644 --- a/sycl/src/kernels/cuda/verify_kernels.dp.cpp +++ b/sycl/src/kernels/cuda/verify_kernels.dp.cpp @@ -15,6 +15,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include #ifndef STRATA_PLAN_LOCAL #define STRATA_PLAN_LOCAL 1 // 0: the original one-thread plan kernel (A/B) @@ -2336,7 +2343,7 @@ code. */ __dpct_inline__ void copy_or_zero_kernel(sycl::float4 *__restrict__ dst, const sycl::float4 *src, long long n4, - const uint32_t *skip, uint32_t value) { + const uint32_t *skip, uint32_t value, bool pair) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); const bool zero = *skip == value; #pragma unroll @@ -2346,7 +2353,7 @@ __dpct_inline__ void copy_or_zero_kernel(sycl::float4 *__restrict__ dst, i < n4; i += (long long)item_ct1.get_group_range(2) * item_ct1.get_local_range(2)) dst[i] = zero ? sycl::float4(0.f, 0.f, 0.f, 0.f) - : const_cast(src)[i]; + : (pair ? strata::load_mapped_float4(src + i) : const_cast(src)[i]); } } // namespace @@ -2421,6 +2428,7 @@ void copy_or_zero_from_mapped(float* dst, const float* src, long long n, const u void* stream) { if (n <= 0) return; const long long n4 = n / 4; + const bool pair = strata::a770_fast(); const int blocks = (int) ((n4 + 255) / 256 < 64 ? (n4 + 255) / 256 : 64); { auto exp_props = sycl::ext::oneapi::experimental::properties{ @@ -2434,7 +2442,7 @@ void copy_or_zero_from_mapped(float* dst, const float* src, long long n, const u exp_props, [=](sycl::nd_item<3> item_ct1) { copy_or_zero_kernel((sycl::float4 *)dst, (const sycl::float4 *)src, n4, - skip, value); + skip, value, pair); }); } check("copy_or_zero_from_mapped"); @@ -2517,32 +2525,54 @@ void copy_indexed(float* dst, const float* src, int64_t stride, const int32_t* i check("copy_indexed"); } -// a GPU timestamp (ns, %globaltimer) into buf[i] - the verify window's stage profiler +// Opt-in GPU stage timestamps. DG2 has no comparable device-wide clock, so +// read a live host USM clock uncached (the fork's gpu_stamp implementation). +// The writer is joined before runtime teardown and exists only in profile mode. namespace { - __dpct_inline__ void gpu_stamp_kernel(unsigned long long *buf, int i) { - unsigned long long t; -#if defined(STRATA_HIP_GFX906) - t = wall_clock64() * 40ull; // gfx906: the wall clock runs at 25 MHz (hipDeviceAttributeWallClockRate) -> ns -#elif defined(__HIPCC__) - t = wall_clock64() * 10ull; // gfx10.3 / gfx11 / gfx12: a constant 100 MHz counter, in ns -#else - /* - DPCT1053: Migration of device assembly code is not supported. - */ - t = 0; // SYCL: no %globaltimer equivalent; the stage profiler is inert on this backend -#endif - buf[i] = t; -} } -void gpu_stamp(unsigned long long* buf, int i, void* stream) { - auto exp_props = sycl::ext::oneapi::experimental::properties{ - sycl::ext::oneapi::experimental::use_root_sync}; +uint64_t profile_now_ns() { + return std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count(); +} +struct ProfileHostClock { + sycl::context context; + uint64_t* word; + std::atomic stop{false}; + std::thread writer; + explicit ProfileHostClock(const sycl::queue& q) : context(q.get_context()), + word(sycl::malloc_host(8, context)) { + if (!word) throw std::runtime_error("gpu_stamp: host clock allocation failed"); + __atomic_store_n(word, profile_now_ns(), __ATOMIC_RELEASE); + writer = std::thread([this] { + while (!stop.load(std::memory_order_relaxed)) { + __atomic_store_n(word, profile_now_ns(), __ATOMIC_RELEASE); + for (int i = 0; i < 8; ++i) __builtin_ia32_pause(); + } + }); + } + ~ProfileHostClock() { + stop.store(true, std::memory_order_relaxed); + if (writer.joinable()) writer.join(); + // Keep the word alive until context teardown, as in the fork: a graph + // may still retain its pointer when process-level statics are destroyed. + } +}; +uint64_t* profile_clock_word(const sycl::queue& q) { + static std::mutex mu; + static std::vector> clocks; + std::lock_guard lock(mu); + for (const auto& clock : clocks) if (clock->context == q.get_context()) return clock->word; + clocks.push_back(std::make_unique(q)); + return clocks.back()->word; +} +} - strata::q_of(stream) - ->parallel_for>( - sycl::nd_range<3>(sycl::range(1, 1, 1), sycl::range(1, 1, 1)), - exp_props, [=](sycl::nd_item<3> item_ct1) { - gpu_stamp_kernel(buf, i); - }); +void gpu_stamp(unsigned long long* buf, int i, void* stream) { + auto& q = *strata::q_of(stream); + uint64_t* word = profile_clock_word(q); + q.single_task([=] { + sycl::atomic_fence(sycl::memory_order::acquire, sycl::memory_scope::system); + buf[i] = strata::sys_load_mapped(word); + }); } } // namespace strata::kernels