From 90aed054ffe4b3c8d59737d447079e1b9c024a0a Mon Sep 17 00:00:00 2001 From: maxfridbe Date: Wed, 30 Sep 2026 22:46:56 -0500 Subject: [PATCH] sycl: decode 54 -> 62.6 tok/s: parallel resident plan, untiled GR down, wide Q4_K/Q5_K/IQ4_XS - resident_plan groups the entries with one thread per entry (thread 0 alone took 73 us per layer); the plan is the host loop's exactly - the GR down kernel reads xn from global memory instead of a 60 KB SLM tile per work-group (occupancy); outputs bitwise equal (gr_bench) - 16-byte-load wide kernels for Q4_K, Q5_K and IQ4_XS (IQ4 codebook in registers): IQ4_XS 107 -> 323 GB/s, Q4_K/Q5_K 1.3-1.6x Output tokens identical in every engine run. --- docs/INTEL.md | 15 ++ sycl/CMakeLists.txt | 2 + sycl/src/kernels/cuda/fused_gr.dp.cpp | 59 +++++++- sycl/src/kernels/cuda/native_mmvq.dp.cpp | 150 ++++++++++++++++++++ sycl/src/kernels/cuda/verify_kernels.dp.cpp | 62 +++++++- sycl/src/kernels/gr_bench.cpp | 57 ++++++++ 6 files changed, 341 insertions(+), 4 deletions(-) create mode 100644 sycl/src/kernels/gr_bench.cpp diff --git a/docs/INTEL.md b/docs/INTEL.md index 14093b67..c7b1aa5d 100644 --- a/docs/INTEL.md +++ b/docs/INTEL.md @@ -384,6 +384,21 @@ runtime's teardown began); it now exits directly once its requests are done. Still misaligned: IQ4_XS (136 B, 8-aligned, ~100 GB/s), IQ4_NL (18 B), Q8_0 (34 B); Q4_K/Q5_K are aligned but use 4-byte loads in the multi kernel (230-280 GB/s). +**Decode round 2 (2026-09-30), from a unitrace of 256 decoded tokens.** Each step measured on its own, 19-token prompt, +256 tokens, two runs each, output tokens identical throughout: + +| | decode | +|---|---| +| after the Q6_K alignment fix | 53.9 tok/s | +| + `resident_plan` grouping in parallel (thread 0 alone took 73 us per layer, ~3.5 ms a round) | 57.1 | +| + the GR down kernel reads the activations directly (the 60 KB SLM tile per group capped occupancy) | 58.7 | +| + wide 16-byte-load kernels for Q4_K, Q5_K, IQ4_XS (codebook in registers) | **62.6** | + +Kernel level: IQ4_XS 107 -> 323 GB/s at 2 columns (61 -> 212 at 6), Q4_K/Q5_K 1.3-1.6x; the GR read 135 -> 110 us at +6 tokens (bitwise equal). Switches to the old paths: `STRATA_PLAN_PARALLEL=0`, `STRATA_GR_DOWN_DIRECT=0`, +`STRATA_MMVQ_WIDE_K=0`. (`iq_parity` reports 10 "missing fixture" failures with and without the change: the oracle +fixtures are not in the port's tree.) + **Two-speed runs, explained (2026-09-30).** Identical greedy runs decode at either ~45 or ~39 tok/s. A per-gather trace of the PLE reader (`STRATA_PLE_TRACE=1`) shows the slow runs pay one 226 ms PLE read stall in the first decode round, after the window graphs are captured; every other read and round matches the fast runs. Prompt time diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt index 949aa17b..11be21ca 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -197,6 +197,8 @@ if(STRATA_SYCL_PARITY) target_link_libraries(mmvq_sg_bench PRIVATE strata_kernels) add_executable(q6k_align_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/q6k_align_bench.cpp) # Q6_K block alignment 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(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 # one native expert three ways on real GGUF rows (ggml reference, CPU path, GPU native_expert_grouped): diff --git a/sycl/src/kernels/cuda/fused_gr.dp.cpp b/sycl/src/kernels/cuda/fused_gr.dp.cpp index 27b912c6..8e6daf38 100644 --- a/sycl/src/kernels/cuda/fused_gr.dp.cpp +++ b/sycl/src/kernels/cuda/fused_gr.dp.cpp @@ -401,6 +401,58 @@ __dpct_inline__ void gr_down_multi_kernel(GrMulti m, uint8_t *dpct_local) { } } +// SYCL port: gr_down_multi_kernel without the shared-memory tile. The tiled kernel stages T x 2560 floats per +// work-group (60 KB at T = 6) for 8 rows (40 KB of weights): the copy outweighs the weights and the SLM caps the +// B70 at ~2 groups per Xe core (unitrace: 64 us per call, ~100 GB/s). Here each lane reads its 8 xn floats per +// token straight from global memory (T x 40 KB in all, L1/L2-resident, shared by every group). Same per-lane +// order of the sums, so the outputs are bitwise the tiled kernel's. +__dpct_inline__ void gr_down_multi_direct_kernel(GrMulti m) { + auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); + const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; + const int T = m.T; + const int gid = (int) item_ct1.get_group(2); + const bool inject_block = gid == DOWN_BLOCKS * DOWN_SPLIT; + const int split = inject_block ? 0 : gid % DOWN_SPLIT, rb = inject_block ? 0 : gid / DOWN_SPLIT; + const int row = inject_block ? warp : rb * WARPS + warp; + const bool active = !(inject_block && (m.a[0].w_inject == nullptr || warp >= HC)); + if (!active) return; + const uint16_t* wrow = (inject_block ? m.a[0].w_inject : m.a[0].w_down) + (size_t) row * D; + const sycl::uint4* w4 = reinterpret_cast(wrow); + float acc[kFusedGrMaxT]; +#pragma unroll + for (int k = 0; k < kFusedGrMaxT; ++k) acc[k] = 0.0f; + for (int base = inject_block ? 0 : split * TILE; base < D; base += inject_block ? TILE : TILE * DOWN_SPLIT) { + sycl::uint4 wv[TQ]; +#pragma unroll + for (int q = 0; q < TQ; ++q) wv[q] = *(w4 + base / 8 + lane + 32 * q); +#pragma unroll + for (int q = 0; q < TQ; ++q) { + const int j = lane + 32 * q; +#pragma unroll + for (int k = 0; k < kFusedGrMaxT; ++k) + if (k < T) { + const sycl::float4* x4 = reinterpret_cast(m.xn + (size_t) k * D + base + j * 8); + const sycl::float4 a0 = x4[0], a1 = x4[1]; + const float xv[8] = {a0.x(), a0.y(), a0.z(), a0.w(), a1.x(), a1.y(), a1.z(), a1.w()}; + acc[k] += dot8(wv[q], xv); + } + } + } + float s[kFusedGrMaxT]; +#pragma unroll + for (int k = 0; k < kFusedGrMaxT; ++k) s[k] = k < T ? warp_sum(acc[k]) : 0.0f; +#pragma unroll + for (int k = 0; k < kFusedGrMaxT; ++k) { + if (k >= T || lane != k) continue; + if (inject_block) m.a[k].inject_out[row] = s[k]; + else m.part[((size_t) split * kFusedGrMaxT + k) * LR + row] = s[k]; + } +} +bool gr_down_direct() { // default; STRATA_GR_DOWN_DIRECT=0: the tiled kernel (gr_bench: 13-19% slower at 3-6 tokens) + static const bool v = std::getenv("STRATA_GR_DOWN_DIRECT") == nullptr || std::atoi(std::getenv("STRATA_GR_DOWN_DIRECT")) != 0; + return v; +} + constexpr int UPM_COLS = 16; // columns per block (x 4 streams = 64 rows, 8 per warp) constexpr int UPM_BLOCKS = N / UPM_COLS; // 160 @@ -588,7 +640,12 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi attr[dev] = true; } const int chunk_tok = (dev >= 0 && dev < 64 && chunk[dev]) ? chunk[dev] : kFusedGrMaxT; - if (chunk_tok >= n_tok) { + if (gr_down_direct()) { + st->parallel_for>( + sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS * DOWN_SPLIT + 1) * sycl::range(1, 1, THREADS), + sycl::range(1, 1, THREADS)), + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_direct_kernel(m); }); + } else if (chunk_tok >= n_tok) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; diff --git a/sycl/src/kernels/cuda/native_mmvq.dp.cpp b/sycl/src/kernels/cuda/native_mmvq.dp.cpp index f6d4fe6a..82ea1411 100644 --- a/sycl/src/kernels/cuda/native_mmvq.dp.cpp +++ b/sycl/src/kernels/cuda/native_mmvq.dp.cpp @@ -1551,6 +1551,153 @@ void launch_q6k_wide(const void* weights, const void* x_q8_1, float* y, int n_in sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_q6k_wide_kernel(w, x, y, n_in, n_out); }); } +// ---------------------------------------------------------------- SYCL port: wide kernels for Q4_K, Q5_K, IQ4_XS +// +// The Q6_K wide kernel's shape for the other 256-element formats: 8 lanes share a block, each lane's weights are one +// or two 16-byte loads, a 32-lane sub-group has four blocks in flight, one row per sub-group. The multi kernel these +// replace does 4-byte loads (Q4_K/Q5_K, 230-280 GB/s) or global-memory codebook lookups (IQ4_XS, ~100 GB/s). +// Lane g of a block: Q4_K/Q5_K take qs bytes [16g, 16g+16) = sub-blocks 2(g/2) (low nibbles) and 2(g/2)+1 (high +// nibbles) at positions 16(g%2)..+15; IQ4_XS takes sub-block g (16 bytes, low nibbles = positions 0-15). +__dpct_inline__ sycl::int4 ld_q8_16(const Q81Block* b, int half) { // Q8_1 qs is 4-byte aligned: four int loads + const int* q = reinterpret_cast(b->qs) + 4 * half; + return sycl::int4(q[0], q[1], q[2], q[3]); +} +__dpct_inline__ int dp4a4(const sycl::int4 a, const sycl::int4 b, int acc) { + acc = strata::dp4a(a.x(), b.x(), acc); acc = strata::dp4a(a.y(), b.y(), acc); + acc = strata::dp4a(a.z(), b.z(), acc); return strata::dp4a(a.w(), b.w(), acc); +} +__dpct_inline__ void k4_scale_min(const uint8_t* sc, int j, float& s, float& m) { // ggml get_scale_min_k4 + if (j < 4) { s = (float) (sc[j] & 63); m = (float) (sc[j + 4] & 63); } + else { s = (float) ((sc[j + 4] & 0xF) | ((sc[j - 4] >> 6) << 4)); m = (float) ((sc[j + 4] >> 4) | ((sc[j] >> 6) << 4)); } +} +struct WideQ4K { + using Block = Q4KBlock; + struct W { sycl::int4 lo, hi; float dl, dh, ml, mh; int sb, half; }; + static W load(const Block* b, int g) { + W r; r.sb = 2 * (g >> 1); r.half = g & 1; + const sycl::int4 q = reinterpret_cast(b->qs)[g]; // 16-byte aligned (144 = 9 x 16) + r.lo = sycl::int4(q.x() & 0x0f0f0f0f, q.y() & 0x0f0f0f0f, q.z() & 0x0f0f0f0f, q.w() & 0x0f0f0f0f); + r.hi = sycl::int4((q.x() >> 4) & 0x0f0f0f0f, (q.y() >> 4) & 0x0f0f0f0f, (q.z() >> 4) & 0x0f0f0f0f, (q.w() >> 4) & 0x0f0f0f0f); + const sycl::float2 dm = b->dm.convert(); + float s0, m0, s1, m1; k4_scale_min(b->scales, r.sb, s0, m0); k4_scale_min(b->scales, r.sb + 1, s1, m1); + r.dl = dm.x() * s0; r.ml = dm.y() * m0; r.dh = dm.x() * s1; r.mh = dm.y() * m1; + return r; + } + static float apply(const W& r, const Q81Block* xb) { // xb = this column's Q8_1 blocks of the 256-block + const Q81Block* a = xb + r.sb; const Q81Block* c = xb + r.sb + 1; + const sycl::int4 ua = ld_q8_16(a, r.half), uc = ld_q8_16(c, r.half); + const sycl::int4 ones(0x01010101, 0x01010101, 0x01010101, 0x01010101); + const float da = (float) a->ds[0], dc = (float) c->ds[0]; + return da * (r.dl * (float) dp4a4(r.lo, ua, 0) - r.ml * (float) dp4a4(ones, ua, 0)) + + dc * (r.dh * (float) dp4a4(r.hi, uc, 0) - r.mh * (float) dp4a4(ones, uc, 0)); + } +}; +struct WideQ5K { + using Block = Q5KBlock; + using W = WideQ4K::W; + static W load(const Block* b, int g) { + W r; r.sb = 2 * (g >> 1); r.half = g & 1; + const sycl::int4 q = reinterpret_cast(b->qs)[g]; // qs at 48, 176 = 11 x 16 + const sycl::int4 h = reinterpret_cast(b->qh)[r.half]; // qh bytes of positions 16h..+15 + const int sl = r.sb, sh = r.sb + 1; + auto hb = [](int v, int bit) { return ((v >> bit) & 0x01010101) << 4; }; + r.lo = sycl::int4((q.x() & 0x0f0f0f0f) | hb(h.x(), sl), (q.y() & 0x0f0f0f0f) | hb(h.y(), sl), + (q.z() & 0x0f0f0f0f) | hb(h.z(), sl), (q.w() & 0x0f0f0f0f) | hb(h.w(), sl)); + r.hi = sycl::int4(((q.x() >> 4) & 0x0f0f0f0f) | hb(h.x(), sh), ((q.y() >> 4) & 0x0f0f0f0f) | hb(h.y(), sh), + ((q.z() >> 4) & 0x0f0f0f0f) | hb(h.z(), sh), ((q.w() >> 4) & 0x0f0f0f0f) | hb(h.w(), sh)); + const sycl::float2 dm = b->dm.convert(); + float s0, m0, s1, m1; k4_scale_min(b->scales, r.sb, s0, m0); k4_scale_min(b->scales, r.sb + 1, s1, m1); + r.dl = dm.x() * s0; r.ml = dm.y() * m0; r.dh = dm.x() * s1; r.mh = dm.y() * m1; + return r; + } + static float apply(const W& r, const Q81Block* xb) { return WideQ4K::apply(r, xb); } +}; +__dpct_inline__ uint32_t iq4_lut4(uint32_t q4) { // four nibbles -> four codebook bytes, codebook in registers + constexpr uint32_t t0 = 0xBFAD9881u, t1 = 0xF6EADDCFu, t2 = 0x26190D01u, t3 = 0x71594535u; + uint32_t r = 0; +#pragma unroll + for (int i = 0; i < 4; ++i) { + const uint32_t n = (q4 >> (8 * i)) & 0xF; + const uint32_t t = n < 4 ? t0 : n < 8 ? t1 : n < 12 ? t2 : t3; + r |= ((t >> (8 * (n & 3))) & 0xFF) << (8 * i); + } + return r; +} +struct WideIQ4XS { + using Block = IQ4XSBlock; + struct W { sycl::int4 lo, hi; float d; int sb; }; + static W load(const Block* b, int g) { + W r; r.sb = g; + const sycl::int2* q2 = reinterpret_cast(b->qs + 16 * g); // 8-byte aligned (136 = 17 x 8) + const sycl::int2 qa = q2[0], qb = q2[1]; + const uint32_t v[4] = {(uint32_t) qa.x(), (uint32_t) qa.y(), (uint32_t) qb.x(), (uint32_t) qb.y()}; + int lo[4], hi[4]; +#pragma unroll + for (int i = 0; i < 4; ++i) { lo[i] = (int) iq4_lut4(v[i] & 0x0f0f0f0f); hi[i] = (int) iq4_lut4((v[i] >> 4) & 0x0f0f0f0f); } + r.lo = sycl::int4(lo[0], lo[1], lo[2], lo[3]); r.hi = sycl::int4(hi[0], hi[1], hi[2], hi[3]); + const int ls = ((b->scales_l[g / 2] >> (4 * (g & 1))) & 0x0f) | (((b->scales_h >> (2 * g)) & 0x03) << 4); + r.d = (float) b->d * (float) (ls - 32); + return r; + } + static float apply(const W& r, const Q81Block* xb) { + const Q81Block* a = xb + r.sb; + return r.d * (float) a->ds[0] * (float) dp4a4(r.hi, ld_q8_16(a, 1), dp4a4(r.lo, ld_q8_16(a, 0), 0)); + } +}; +template +void native_mmvq_wide_kernel(const typename F::Block* __restrict__ w, const Q81Block* __restrict__ x, + float* __restrict__ y, int n_in, int n_out) { + auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); + const int lane = int(item.get_local_id(2)), warp = int(item.get_local_id(1)); + const int row = int(item.get_group(2)) * WARPS + warp; + if (row >= n_out) return; + const int blocks_per_row = n_in / QK, x_stride = n_in / Q8K; + const int g = lane & 7, sub = lane >> 3; + const typename F::Block* wr = w + std::size_t(row) * blocks_per_row; + float acc[NCOLS] = {}; + for (int kbx = sub; kbx < blocks_per_row; kbx += 4) { + const typename F::W wv = F::load(wr + kbx, g); +#pragma unroll + for (int j = 0; j < NCOLS; ++j) acc[j] += F::apply(wv, x + std::size_t(j) * x_stride + kbx * (QK / Q8K)); + } + auto sg = item.get_sub_group(); +#pragma unroll + for (int j = 0; j < NCOLS; ++j) { + float v = acc[j]; +#pragma unroll + for (int o = WARP / 2; o > 0; o >>= 1) v += sycl::permute_group_by_xor(sg, v, o); + if (lane == 0) y[std::size_t(j) * n_out + row] = v; + } +} +template +void launch_wide(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, dpct::queue_ptr s) { + const auto* w = static_cast(weights); + const auto* x = static_cast(x_q8_1); + const unsigned blocks = unsigned((std::size_t(n_out) + WARPS - 1) / WARPS); + s->parallel_for>>( + sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)), + [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_wide_kernel(w, x, y, n_in, n_out); }); +} +inline bool wide_k() { // STRATA_MMVQ_WIDE_K=0: Q4_K/Q5_K/IQ4_XS through the multi kernel (the old path) + static const bool v = std::getenv("STRATA_MMVQ_WIDE_K") == nullptr || std::atoi(std::getenv("STRATA_MMVQ_WIDE_K")) != 0; + return v; +} +template +bool try_wide(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, int ncols, void* stream) { + if (!wide_k() || ncols < 1 || ncols > 8 || n_in % QK != 0) return false; + const auto s = strata::q_of(stream); + switch (ncols) { + case 1: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + case 2: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + case 3: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + case 4: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + case 5: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + case 6: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + case 7: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + default: launch_wide(weights, x_q8_1, y, n_in, n_out, s); return true; + } +} + // SYCL port: alignment experiment - Q6_K blocks repacked at a 224-byte stride (every block 16-byte aligned). template void launch_q6k_wide224(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, dpct::queue_ptr s) { @@ -1884,6 +2031,7 @@ void native_q5_k_mmvq(const void* weights, const void* x_q8_1, float* y, validate_pointer(x_q8_1); validate_pointer(y); validate_stream(stream); + if (try_wide(weights, x_q8_1, y, n_in, n_out, ncols, stream)) { launch_check(); return; } if (ncols > 1) { launch_multi(weights, x_q8_1, y, n_in, n_out, ncols, stream); launch_check(); @@ -2103,6 +2251,7 @@ void native_iq4_xs_mmvq(const void* weights, const void* x_q8_1, float* y, validate_pointer(x_q8_1); validate_pointer(y); validate_stream(stream); + if (try_wide(weights, x_q8_1, y, n_in, n_out, ncols, stream)) { launch_check(); return; } if (ncols > 1) { launch_multi(weights, x_q8_1, y, n_in, n_out, ncols, stream); launch_check(); @@ -2192,6 +2341,7 @@ void native_q4_k_mmvq(const void* weights, const void* x_q8_1, float* y, validate_pointer(x_q8_1); validate_pointer(y); validate_stream(stream); + if (try_wide(weights, x_q8_1, y, n_in, n_out, ncols, stream)) { launch_check(); return; } if (ncols > 1) { launch_multi(weights, x_q8_1, y, n_in, n_out, ncols, stream); launch_check(); diff --git a/sycl/src/kernels/cuda/verify_kernels.dp.cpp b/sycl/src/kernels/cuda/verify_kernels.dp.cpp index 9d51ecee..25f05990 100644 --- a/sycl/src/kernels/cuda/verify_kernels.dp.cpp +++ b/sycl/src/kernels/cuda/verify_kernels.dp.cpp @@ -927,12 +927,16 @@ __dpct_inline__ void wait_flag_ge_kernel(const volatile uint32_t *flag, } // namespace namespace { +inline bool plan_parallel() { // STRATA_PLAN_PARALLEL=0: thread 0 groups the entries alone (the old way) + static const bool v = std::getenv("STRATA_PLAN_PARALLEL") == nullptr || std::atoi(std::getenv("STRATA_PLAN_PARALLEL")) != 0; + return v; +} __dpct_inline__ void resident_plan_kernel( const int32_t *__restrict__ ids, int n, int k, const int32_t *__restrict__ res, int n_expert, const uint8_t *cache_base, const unsigned long long *slot_off, long long blob, int32_t *__restrict__ pl, long long capx, uint32_t *skip, uint32_t ring, - const unsigned long long *__restrict__ mir) { + const unsigned long long *__restrict__ mir, bool par) { #if STRATA_PLAN_LOCAL // SYCL port: the host's exact loop, but over a local copy of the ids and their slots. One thread reading global // memory for every compare (n^2 of them) took 87 us per layer on the B70 - 4% of a decode round; a work-group @@ -956,8 +960,59 @@ __dpct_inline__ void resident_plan_kernel( if (!valid || (sl < 0 && ma == 0)) s_bad = 1; } item.barrier(sycl::access::fence_space::local_space); + if (s_bad || n > 64) { if (tid == 0) *skip = 0; return; } // n > 64 never happens (kVerifyMaxT * 10 = 60); refuse rather than read past + if (par) { + // SYCL port: the grouping in parallel, one thread per entry (unitrace: thread 0 alone took 73 us per layer, + // ~3.5 ms of a decode round). The plan is the host loop's exactly: groups in order of first occurrence, + // entries in a group in index order. Entry i: L = its expert's first index; its group = the number of + // first occurrences before L; the group starts at the number of entries whose leader is before L; its + // place in the group = the number of earlier entries with the same leader. + auto &s_lead = *sycl::ext::oneapi::group_local_memory_for_overwrite(item.get_group()); + if (tid < n) { + int L = tid; + for (int j = 0; j < tid; ++j) if (s_ids[j] == s_ids[tid]) { L = j; break; } + s_lead[tid] = L; + } + item.barrier(sycl::access::fence_space::local_space); + int32_t* counts = pl; + int32_t* start = pl + 4; + int32_t* dst = start + capx + 1; + int32_t* tok = dst + capx; + const long long ptr_off = ((4 + (capx + 1) + 2 * capx) + 1) & ~1ll; + unsigned long long* ptr = (unsigned long long*) (pl + ptr_off); + int32_t* start2 = pl + ptr_off + 4 * capx; + if (tid < n) { + const int L = s_lead[tid]; + int g = 0, gstart = 0, pos = 0; + for (int j = 0; j < n; ++j) { + const int Lj = s_lead[j]; + if (j < L && Lj == j) ++g; // first occurrences before L + if (Lj < L) ++gstart; // entries of earlier groups + if (j < tid && Lj == L) ++pos; // earlier entries of this group + } + dst[gstart + pos] = tid; + tok[gstart + pos] = tid / k; + if (L == tid) { + const int32_t slot = s_res[tid]; + ptr[g] = slot >= 0 ? (unsigned long long) (cache_base + (slot_off ? (size_t) slot_off[slot] : (size_t) slot * (size_t) blob)) + : s_mir[tid]; + start[g] = gstart; + } + } + item.barrier(sycl::access::fence_space::global_and_local); + if (tid != 0) return; + int groups = 0; + for (int i = 0; i < n; ++i) groups += s_lead[i] == i; + start[groups] = n; + start2[0] = n; + counts[0] = groups; + counts[1] = n; + counts[2] = 0; + sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::device); + strata::sys_store(skip, ring); + return; + } if (tid != 0) return; - if (s_bad || n > 64) { *skip = 0; return; } // n > 64 never happens (kVerifyMaxT * 10 = 60); refuse rather than read past #else // the original: one thread, the host's exact loop over global memory for (int i = 0; i < n; ++i) { @@ -1083,13 +1138,14 @@ void resident_plan(const int32_t* ids, int n_entries, int k, const int32_t* res_ auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; + const bool par = plan_parallel(); strata::q_of(stream) ->parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, STRATA_PLAN_LOCAL ? 64 : 1), sycl::range(1, 1, STRATA_PLAN_LOCAL ? 64 : 1)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { resident_plan_kernel(ids, n_entries, k, res_layer, n_expert, cache_base, slot_off, blob, plan, capx, - skip, ring, mir); + skip, ring, mir, par); }); } check("resident_plan"); diff --git a/sycl/src/kernels/gr_bench.cpp b/sycl/src/kernels/gr_bench.cpp new file mode 100644 index 00000000..7abeda91 --- /dev/null +++ b/sycl/src/kernels/gr_bench.cpp @@ -0,0 +1,57 @@ +// sycl/src/kernels/gr_bench.cpp - SYCL port: time fused_gr_read_multi (norm + down + up) for 1-6 tokens on random +// weights at the Coder's shapes; prints a checksum of every output so two builds/switches can be compared bitwise. +#include "strata/kernels/fused_gr.hpp" +#include +#include +#include +#include +#include +#include +int main() { + using namespace strata::kernels; + sycl::queue* s = &dpct::get_in_order_queue(); + constexpr int N = 2560, HC = 4, D = N * HC, LR = 320, TM = kFusedGrMaxT; + auto bf16 = [](float f) { uint32_t u; std::memcpy(&u, &f, 4); return (uint16_t) (u >> 16); }; + std::vector wd((size_t) LR * D), wu((size_t) D * LR), wi((size_t) HC * D); + uint32_t r = 12345; + auto rnd = [&] { r = r * 1664525u + 1013904223u; return ((r >> 8) & 0xffff) / 65536.0f - 0.5f; }; + for (auto& v : wd) v = bf16(rnd() * 0.05f); + for (auto& v : wu) v = bf16(rnd() * 0.05f); + for (auto& v : wi) v = bf16(rnd() * 0.05f); + std::vector hR((size_t) TM * D), hn(D); + for (auto& v : hR) v = rnd(); + for (auto& v : hn) v = 1.0f + 0.1f * rnd(); + auto dev = [&](const void* h, size_t b) { void* p = sycl::malloc_device(b, *s); s->memcpy(p, h, b).wait(); return p; }; + auto* d_wd = (uint16_t*) dev(wd.data(), wd.size() * 2); + auto* d_wu = (uint16_t*) dev(wu.data(), wu.size() * 2); + auto* d_wi = (uint16_t*) dev(wi.data(), wi.size() * 2); + auto* d_R = (float*) dev(hR.data(), hR.size() * 4); + auto* d_n = (float*) dev(hn.data(), hn.size() * 4); + float* xn = sycl::malloc_device((size_t) TM * D, *s); + float* out = sycl::malloc_device((size_t) TM * (LR + HC + HC + N), *s); + FusedGrArgs a[TM]; + for (int t = 0; t < TM; ++t) { + float* o = out + (size_t) t * (LR + HC + HC + N); + a[t].R = d_R + (size_t) t * D; a[t].w_norm = d_n; a[t].w_down = d_wd; a[t].w_up = d_wu; a[t].w_inject = d_wi; + a[t].lo = o; a[t].rs = o + LR; a[t].inject_out = o + LR + HC; a[t].mixed = o + LR + 2 * HC; + } + for (int T = 1; T <= 6; ++T) { + const auto w0 = std::chrono::steady_clock::now(); + while (std::chrono::duration(std::chrono::steady_clock::now() - w0).count() < 150) { + for (int i = 0; i < 20; ++i) fused_gr_read_multi(a, T, xn, s); + s->wait(); + } + const int it = 500; + const auto t0 = std::chrono::steady_clock::now(); + for (int i = 0; i < it; ++i) fused_gr_read_multi(a, T, xn, s); + s->wait(); + const double us = std::chrono::duration(std::chrono::steady_clock::now() - t0).count() / it; + std::vector h((size_t) T * (LR + HC + HC + N)); + s->memcpy(h.data(), out, h.size() * 4).wait(); + uint64_t cs = 1469598103934665603ull; + for (float v : h) { uint32_t u; std::memcpy(&u, &v, 4); cs = (cs ^ u) * 1099511628211ull; } + std::printf("fused GR read, %d tokens: %7.1f us (%.0f GB/s of weights) checksum %016llx\n", T, us, + (wd.size() + wu.size() + wi.size()) * 2.0 / us / 1e3, (unsigned long long) cs); + } + return 0; +}