diff --git a/docs/SYCL_PROGRESS.md b/docs/SYCL_PROGRESS.md index 1ab22f57..bebf5ef9 100644 --- a/docs/SYCL_PROGRESS.md +++ b/docs/SYCL_PROGRESS.md @@ -441,6 +441,41 @@ and every value computed twice, a double exp each). 1.62 s = 1109 tok/s (was 4.61 s).** Device time a 1.8k-context step 23.4 ms (profiled): gemm_nt 9.7, attn 4.7, rmsnorm_quant_fp8 3.8. +## Phase 4, step 6 (2026-10-23): the decode GEMV, and what GRIMOIRE suggested + +Per-shape times from `rad-kbench -m ... --timing-only --op gemm_nt --report FILE` (cold weights), +M = 1, MiniCPM5-2B: gate_up N=12288 K=2048 93.3 us (540 GB/s), down N=2048 K=6144 59.7 (420), +qkv N=2560 K=2048 21.0, o N=2048 K=2048 18.1. A plain read kernel reaches ~600 GB/s with +incompressible data (1042 GB/s on a memset buffer: the card compresses). + +- Two columns a sub-group (commit 7a58b31): gemm_nt 9.1 -> 8.5 ms a step at 1.8k. +- **down_proj splits K two ways inside the work-group** (partials added in chunk order through + local memory, deterministic): 59.7 -> 52.2 us. 4/8/16 ways no better; splitting the K=2048 shapes + made them slower (18.1 -> 21.6 us), so only K >= 4096 splits. +- Tried and dropped: four columns a sub-group (10.9 ms a step, fewer sub-groups), K unrolled 2/4/8 + loads ahead (9.2 / 9.4 / 12.3 against 8.8 ms). +- rad-kbench 361/361, 350/350; KL (chunk 16) mean 6.0e-4; greedy against libref 7/8, the eighth a + near-tie at token 14 (" and" vs " Neptune", 0.025 nats apart here, 0.048 in libref). +- **Decode 57.3 tok/s short, 51.4 tok/s after a 1797-token prompt.** + +`rad_memcpy_ranges_async` read its range list on the host (commit 10455f2); device.h has the kernel +read it, and rad-kbench's timer keeps it on the card, so `--bench` / `--timing-only` crashed. Fixed: +the list is read by a kernel. rad-kbench's timer is slow on this backend (a 4-op `-m` run 7 min: +each unit restores its inputs with a small host copy). + +**GRIMOIRE** (github.com/doopeworld/GRIMOIRE, a SYCL engine for the Arc Pro B70) was read on the +user's suggestion. It is GPL-3.0 and radiance is Apache-2.0, so no code comes from it; only measured +ideas were tried: +- wide loads with several in flight, sub-group-contiguous ("vec16x4", 626 GB/s there): our GEMV + already reads 16 B a lane contiguously; more loads in flight lost (above). +- decode split-K scaled with context (their attention went 3650 -> 1197 us a layer): ours already + splits each row's visible keys 32 ways (step 5). +- low-N decode GEMVs underfilling the card: the down_proj split above. +- the decode step as a replayed SYCL command graph: not taken yet. Profiled device time (21.3 ms a + step at short context, inflated by the timing events) already exceeds wall time (17.5 ms), so the + card is not waiting on the host; a graph would only trim launch gaps. Radiance's tape hooks + (`tape_begin` / `tape_play` in device.h) are where it would go. + ## Next steps 1. ~~Milestone B, part 2: `minicpm5-2b-fp8.rad`~~ **Dropped by the user (2026-10-23): "instead of the diff --git a/libxe2/xe2_gemv.cpp b/libxe2/xe2_gemv.cpp index 4f994944..b0a99084 100644 --- a/libxe2/xe2_gemv.cpp +++ b/libxe2/xe2_gemv.cpp @@ -38,6 +38,7 @@ namespace { constexpr int kMaxM = 16; constexpr int kCols = 2; /* output columns a sub-group at decode: two weight rows in flight */ constexpr size_t kWg = 256; /* 16 sub-groups */ +constexpr int64_t kFill = 8192; /* sub-groups that fill the B65 (it runs ~10k threads) */ int64_t row_stride(const RadTensor* t, int64_t cols) { if (!t || t->rank < 2) return cols; @@ -113,6 +114,61 @@ int launch(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* yb, uin }); } +/* Decode (M = 1) with K split S ways inside a work-group: sub-group g takes columns + * (group * 16/S + g / S) * NC + c and the K chunk g % S (whole 128-element steps), its lanes reduce + * their partials, and the chunk partials are added in chunk order through local memory -- the same + * order every launch. Without the split a decode GEMV is N / NC sub-groups, which for N = 2048 is a + * tenth of the card's threads; at a long K each of them then walks K alone. */ +template +int launch_split(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* yb, uint32_t ydt, + int64_t N, int64_t K, int64_t brs, int64_t ycs, int S) { + const int cg = 16 / S; /* column groups a work-group */ + const int64_t per_wg = (int64_t)cg * NC; + const size_t groups = (size_t)((N + per_wg - 1) / per_wg); + const int64_t steps = (K + 8 * kSub - 1) / (8 * kSub); + const int64_t chunk = (steps + S - 1) / S * (8 * kSub); /* K elements a split */ + return submit([&] { + q.submit([&](sycl::handler& h) { + sycl::local_accessor part(sycl::range<1>((size_t)16 * NC), h); + h.parallel_for(sycl::nd_range<1>{groups * kWg, kWg}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int g = (int)sg.get_group_linear_id(); + const int sp = g % S; + const int64_t n0 = ((int64_t)it.get_group(0) * cg + g / S) * NC; + const int nc = (int)sycl::max((int64_t)0, sycl::min((int64_t)NC, N - n0)); + const int lane = (int)sg.get_local_linear_id(); + float acc[NC]; + for (int c = 0; c < NC; ++c) acc[c] = 0.0f; + const int64_t klo = sp * chunk, khi = sycl::min(K, klo + chunk); + using V8 = sycl::vec; + for (int64_t k = klo + (int64_t)lane * 8; k < khi; k += 8 * kSub) { + const V8 x = *(const V8*)(ab + k); + float xf[8]; + for (int j = 0; j < 8; ++j) xf[j] = bf(x[j]); + for (int c = 0; c < NC; ++c) { + if (c >= nc) break; + const V8 w = *(const V8*)(bb + (n0 + c) * brs + k); + float s = 0.0f; + for (int j = 0; j < 8; ++j) s += xf[j] * bf(w[j]); + acc[c] += s; + } + } + for (int c = 0; c < NC; ++c) { + const float v = sycl::reduce_over_group(sg, acc[c], sycl::plus()); + if (lane == 0) part[g * NC + c] = v; + } + sycl::group_barrier(it.get_group()); + if (sp == 0 && lane < nc) { + float y = 0.0f; + for (int t = 0; t < S; ++t) y += part[(g + t) * NC + lane]; + rad_store_f32(yb, ydt, (n0 + lane) * ycs, y); + } + }); + }); + }); +} + int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int (*exact)(const RadArgs*, RadStream)) { if (exact_only()) return exact(a, st); if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL; @@ -135,7 +191,15 @@ int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int /* Decode (M = 1) takes the lean form with kCols columns a sub-group; M up to 16 one column a * sub-group with a partial sum a row. Sizing the accumulators by the band keeps them in * registers (four columns at sixteen rows spilled and ran slower than one). */ - if (mm == 1) return launch(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, 1); + if (mm == 1) { + /* A long K on too few sub-groups to fill the card splits two ways. Measured on the B65 + * (MiniCPM5-2B, cold weights): down_proj N=2048 K=6144 59.7 -> 52.2 us; 4, 8 or 16 ways no + * better; the K=2048 shapes slower split (18.1 -> 21.6 us at N=2048), so they keep one. */ + const int64_t sgs = (N + kCols - 1) / kCols; + const int S = (K >= 4096 && sgs < kFill) ? 2 : 1; + if (S > 1) return launch_split(*q, ab, bb, yb, ydt, N, K, brs, ycs, S); + return launch(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, 1); + } return launch<1, kMaxM>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm); }