sycl: add experimental A770 router and mapped-transfer tuning

Port the fork's fast router with a bracketed reciprocal and serial fallback. Add bitwise comparisons against the existing router, including ties, NaN tails and the forced serial path. On the A770's 256-expert/top-10 shape, measured host submission plus execution fell from 395 to 119 us per call.

Pair activation payload stores and mapped result reads into 64-bit uncached transfers, preserving the checksum and unaligned/odd-tail fallback. Register router and payload probes with CTest. The two probes, elementwise_parity and router_top10_parity passed on the A770.

Add the fork's opt-in host-clock stage profiler for further investigation. Profiling remains experimental: enabled runs encountered a payload-arrival timeout, and review flagged concurrent atomic host writes versus non-atomic device clock reads as a synchronization concern. The profiler is not needed for normal inference.

With CPU-computed misses, cache borrowing and an 8.98 GiB VRAM cache, unprofiled Coder IQ1_M runs generated 128 tokens at 11.9-13.5 tok/s and returned final arithmetic answer 41. These runs retained 128K context capacity but used short prompts; the 20 tok/s goal has not been achieved.
This commit is contained in:
duncanmcqueen authored and Niko1221 committed 2026-10-09 15:17:50 +02:00
1 parent c2d008bc80
commit 174c74c07f
7 files changed
+395 -62

No files matched your search

+6
View File
@@ -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
+23
View File
@@ -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<uint32_t, sycl::memory_order::relaxed, sycl::memory_scope::system>;
// Payload pairs use 64-bit transactions; flags remain 32-bit. Keep the same
// cache-bypass contract while transferring two floats per PCIe transaction.
template<class T> 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::intel::experimental::cache_control<
sycl::ext::intel::experimental::cache_mode::uncached,
sycl::ext::oneapi::experimental::cache_level::L1,
sycl::ext::oneapi::experimental::cache_level::L3>>));
sycl::ext::oneapi::experimental::annotated_ptr<T, read_props> u(const_cast<T*>(p));
return u[0];
}
inline sycl::float4 load_mapped_float4(const sycl::float4* p) {
const uint64_t* words = reinterpret_cast<const uint64_t*>(p);
const auto lo = sycl::bit_cast<sycl::float2>(sys_load_mapped(words));
const auto hi = sycl::bit_cast<sycl::float2>(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
+61
View File
@@ -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 <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/elementwise.hpp"
#include <cstdio>
#include <cstring>
#include <vector>
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<float>(capacity, q);
auto* di = sycl::malloc_device<int32_t>(k, q);
auto* dw = sycl::malloc_device<float>(k, q);
auto* dr = sycl::malloc_device<int32_t>(experts, q);
std::vector<float> x(capacity), weights(k), copied(capacity);
std::vector<int32_t> 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<void*>(hx), static_cast<void*>(hi), static_cast<void*>(hw), static_cast<void*>(seq)})
strata::host_free_polled(p, q);
for (void* p : {static_cast<void*>(dx), static_cast<void*>(di), static_cast<void*>(dw), static_cast<void*>(dr)}) sycl::free(p, q);
std::printf("paired payload and mapped read: %d failures\n", failed);
return failed != 0;
}
+71
View File
@@ -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 <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/router_top10.hpp"
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <random>
#include <vector>
static int compare(int experts, int k, int rows) {
auto& q = *strata::q_of(nullptr);
std::mt19937 rng(17);
std::normal_distribution<float> normal(0, 2);
std::vector<float> 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<float>(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<float> weights[3]; std::vector<int> ids[3];
for (int v = 0; v < 3; ++v) {
dw[v] = sycl::malloc_device<float>(n, q);
di[v] = sycl::malloc_device<int>(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<double, std::micro>(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;
}
+38 -32
View File
@@ -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<const sycl::float4 *>(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<const sycl::float4 *>(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<uint32_t>(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<uint32_t>(x[j]);
const uint32_t b = sycl::bit_cast<uint32_t>(x[j + 1]);
strata::sys_store_mapped(reinterpret_cast<uint64_t*>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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<uint32_t>(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<class doorbell_publish_value_kernel_9bdb22>>(
@@ -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<class doorbell_publish_kernel_8b5bad>>(
@@ -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<class doorbell_publish_res_kernel_85b9b3>>(
@@ -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");
+139 -3
View File
@@ -55,9 +55,11 @@
#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/sycl_doorbell.hpp"
#include "strata/kernels/router_top10.hpp"
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
@@ -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 <bool FORCE_SERIAL>
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<double, 1> s_ex(sycl::range<1>(n_expert), h);
sycl::local_accessor<float, 1> s_red(sycl::range<1>(RT_MAX_THREADS / 32), h);
sycl::local_accessor<double, 1> s_dred(sycl::range<1>(RT_MAX_THREADS / 32), h);
sycl::local_accessor<float, 1> 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<false>(q, logits, n_tokens, n_expert, k, ids, weights);
else if (variant == 2) launch_sycl_fast<true>(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<uint8_t, 1> 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<sycl::access::decorated::no>().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<false>(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)
+57 -27
View File
@@ -15,6 +15,13 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <atomic>
#include <chrono>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <thread>
#include <vector>
#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<const sycl::float4 *>(src)[i];
: (pair ? strata::load_mapped_float4(src + i) : const_cast<const sycl::float4 *>(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::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
struct ProfileHostClock {
sycl::context context;
uint64_t* word;
std::atomic<bool> stop{false};
std::thread writer;
explicit ProfileHostClock(const sycl::queue& q) : context(q.get_context()),
word(sycl::malloc_host<uint64_t>(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<std::unique_ptr<ProfileHostClock>> clocks;
std::lock_guard<std::mutex> lock(mu);
for (const auto& clock : clocks) if (clock->context == q.get_context()) return clock->word;
clocks.push_back(std::make_unique<ProfileHostClock>(q));
return clocks.back()->word;
}
}
strata::q_of(stream)
->parallel_for<dpct_kernel_name<class gpu_stamp_kernel_76ad79>>(
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