From c0497e040ec3fcdbce393d321aac5e8c2a9b7966 Mon Sep 17 00:00:00 2001 From: claude Date: Fri, 23 Oct 2026 20:58:52 +0200 Subject: [PATCH] libxe2: attention scores a key a lane with 16-byte loads -- decode 32 -> 55 tok/s Each sub-group now takes sixteen keys a step, one a lane: the lane scores its key in full against the query in local memory, one max and one sum update the flash state, and the value rows are added with each key's weight taken from its lane. bf16 cache rows are read 16 bytes at a time, and each row's own visible span is split evenly 32 ways on the device instead of the context limit (which left most partitions empty at a short context). rad-kbench 138/138 and 132/132; KL against libref mean 5.7e-4, p99 3.1e-3, top-1 98.6%. MiniCPM5-2B on the B65: decode 55 tok/s at a short context and 49 tok/s after a 1797-token prompt (was 32 and 30); that prompt's prefill 1.62 s, 1109 tok/s (was 4.61 s). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- docs/SYCL_PROGRESS.md | 14 +++++ libxe2/xe2_attn_fast.cpp | 107 +++++++++++++++++++++++++++++---------- 2 files changed, 93 insertions(+), 28 deletions(-) diff --git a/docs/SYCL_PROGRESS.md b/docs/SYCL_PROGRESS.md index bce631a1..1ab22f57 100644 --- a/docs/SYCL_PROGRESS.md +++ b/docs/SYCL_PROGRESS.md @@ -427,6 +427,20 @@ and every value computed twice, a double exp each). - After: **26.6 ms/step** -- gemm_nt 9.62, attn_paged 9.50, rmsnorm_quant_fp8 3.75, logits_gemm 1.02, gated_quant_fp8 0.87. +## Phase 4, step 5 (2026-10-23): attention, lane-per-key with vector loads + +- 16 keys a step, one a lane: a lane scores its key in full against q in local memory; one max and + one sum a step; value rows accumulated with each key's weight taken from its lane. Alone: 9.5 -> + 8.8 ms/step. +- Partitions split each row's *own* visible span on the device (P = 32), not the context limit. + Alone it was slower (11.5 ms: fewer active work-groups), which showed the real cost. +- bf16 cache rows read 16 bytes at a time (keys for the lane's dot, values for the lane's 8 dims): + **4.67 ms/step**. P = 64: 4.81, P = 128: 7.46 -- kept 32. +- rad-kbench 138/138, 132/132; KL mean 5.7e-4, p99 3.1e-3, top-1 98.6%. +- **Decode 55 tok/s short context, 49.2 tok/s after a 1797-token prompt (was 29.8); that prefill + 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. + ## 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_attn_fast.cpp b/libxe2/xe2_attn_fast.cpp index 07e6013a..a36363ee 100644 --- a/libxe2/xe2_attn_fast.cpp +++ b/libxe2/xe2_attn_fast.cpp @@ -2,15 +2,18 @@ * decoding"), a kv head's keys read once for all its query heads. * * Pass 1: a work-group a (query row, kv head, key partition). Its sub-groups are the kv head's - * query heads (sub-group s takes heads s, s + 8, ...); each walks the partition's visible keys in - * order, its lanes splitting head_dim, so a key's score is a sub-group reduction of - * q . (k * k_descale), scaled, and the sub-group keeps the flash state: max m, sum l and its lanes' - * slice of the weighted value sum, rescaled by exp(m_old - m_new) as the max grows. The eight query - * heads read the same key and value rows, so the bytes come from memory once a work-group. + * query heads (sub-group s takes heads s, s + 8, ...); each walks the partition's visible keys + * sixteen at a time, one key a lane: the lane scores its key in full against the query in local + * memory (q . (k * k_descale), scaled), one max and one sum over the sub-group update the flash + * state (max m, sum l), and the lanes -- which split head_dim for the value rows -- add each key's + * value row weighted by that key's exp(s - m), taken from its lane. Per key that is no reduction and + * no exp on the lanes that do not own it (the first version reduced and exponentiated once a key on + * every lane, 27% of a 1.8k-context decode step). The eight query heads read the same key and value + * rows, so the bytes come from memory once a work-group. * * The partition count P is chosen at launch from the rows: a decode step (one row, a few heads) - * splits the keys into partitions of 32 or more so the whole card works on one token; a - * prefill step has parallelism in its rows and takes P = 1. With P > 1 each partition's state goes + * splits each row's visible keys evenly 32 ways, on the device from the row's own context, so the + * whole card works on one token; a prefill step has parallelism in its rows and takes P = 1. With P > 1 each partition's state goes * to scratch (64 MiB, the `scratch` hook) and pass 2 -- a sub-group a (row, query head) -- merges * them; with P = 1 pass 1 writes the normalised output itself. * @@ -32,8 +35,7 @@ constexpr int kSgs = 8; constexpr size_t kWg = kSgs * kSub; constexpr int kMaxPer = 16; /* head_dim / 16, up to head_dim 256 */ constexpr int64_t kScratch = (int64_t)64 << 20; /* pass 1's partial states */ -constexpr int64_t kPartKeys = 32; /* the finest partition */ -constexpr int64_t kMaxParts = 1024; +constexpr int64_t kMaxParts = 32; /* a row's visible keys split this many ways at most */ int64_t row_stride(const RadTensor* t, int64_t cols) { if (!t || t->rank < 2) return cols; @@ -95,16 +97,21 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { const float scale = (float)rad_args_getf_or(a, "scale", (double)(1.0f / std::sqrt((float)head_dim))); const int64_t jcap = max_blocks * block_size; const int per = (int)(head_dim / kSub); + /* bf16 cache rows read 16 bytes at a time when every row start is 16-byte aligned. */ + const bool vec_kv = KV->dtype == RAD_BF16 && sd == 1 && head_dim % 8 == 0 && sb % 8 == 0 && + sh % 8 == 0 && so % 8 == 0 && rad_ptr_aligned(KV->data, 16); - /* The partitions: as many as the scratch holds for these rows, none finer than kPartKeys. */ + /* The partitions: as many as the scratch holds for these rows, at most kMaxParts. Each row's + * own visible span is split evenly on the device, so every partition has keys whatever the + * context limit (splitting the limit instead left most partitions empty at a short context and + * the merge walking all of them). */ const int64_t state = 2 + head_dim; /* floats a partial state: m, l, acc[head_dim] */ int64_t P = 1; if (a->scratch && a->scratch_bytes > 0) { const int64_t fit = a->scratch_bytes / (rows * q_heads * state * (int64_t)sizeof(float)); - P = sycl::min(sycl::min(fit, kMaxParts), (jcap + kPartKeys - 1) / kPartKeys); + P = sycl::min(fit, kMaxParts); if (P < 1) P = 1; } - const int64_t chunk = (jcap + P - 1) / P; float* part = (float*)a->scratch; const char *qb = (const char*)Q->data, *cb = (const char*)KV->data, *btb = (const char*)BT->data; @@ -122,6 +129,8 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { const int64_t g = (int64_t)it.get_group(0); const int64_t p = g % P, kh = (g / P) % kv_heads, tok = g / (P * kv_heads); + float* qloc = *sycl::ext::oneapi::group_local_memory_for_overwrite( + it.get_group()); const sycl::sub_group sg = it.get_sub_group(); const int s = (int)sg.get_group_linear_id(); const int lane = (int)sg.get_local_linear_id(); @@ -145,8 +154,12 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { jlo = sycl::max((int64_t)0, pos - window + 1); if (!causal) jhi = sycl::min(jend, pos + window); } - jlo = sycl::max(jlo, p * chunk); - jhi = sycl::min(jhi, (p + 1) * chunk); + if (P > 1) { + const int64_t span = sycl::max(jhi - jlo, (int64_t)0); + const int64_t ch = (span + P - 1) / P; + jlo += p * ch; + jhi = sycl::min(jhi, jlo + ch); + } for (int64_t hl = s; hl < gqa; hl += kSgs) { const int64_t hq = kh * gqa + hl; const int64_t qoff = tok * q_rs + hq * q_hs; @@ -155,23 +168,61 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { qv[e] = e < per ? rad_load_f32(qb, qdt, qoff + (lane * per + e) * q_ds) : 0.0f; acc[e] = 0.0f; } + /* The query in local memory, so a lane can score a whole key by itself. */ + float* qs = qloc + (size_t)s * kSub * kMaxPer; + for (int e = 0; e < per; ++e) qs[lane * per + e] = qv[e]; + sycl::group_barrier(sg); float m = -INFINITY, l = 0.0f; - for (int64_t j = jlo; j < jhi; ++j) { - const int64_t blk = ld_index(btdt, btb, sq * bt_rs + (j / block_size) * bt_cs); - if (blk < 0) continue; - const int64_t kb = blk * sb + kh * sh + (j % block_size) * so; - float pp = 0.0f; - for (int e = 0; e < per; ++e) - pp += qv[e] * (rad_load_f32(cb, cdt, kb + (lane * per + e) * sd) * kds); - const float sc = sycl::reduce_over_group(sg, pp, sycl::plus()) * scale; - const float mn = sycl::fmax(m, sc); - const float corr = sycl::exp(m - mn), w = sycl::exp(sc - mn); - l = l * corr + w; - for (int e = 0; e < per; ++e) - acc[e] = acc[e] * corr + - w * (rad_load_f32(cb, cdt, kb + (head_dim + lane * per + e) * sd) * vds); + /* Sixteen keys a step, one a lane: the lane's own dot, then one max and one sum + * over the sub-group, and the value rows accumulated with each key's weight taken + * from its lane. */ + for (int64_t j0 = jlo; j0 < jhi; j0 += kSub) { + const int64_t j = j0 + lane; + int64_t kb = -1; + float sc = -INFINITY; + if (j < jhi) { + const int64_t blk = ld_index(btdt, btb, sq * bt_rs + (j / block_size) * bt_cs); + if (blk >= 0) { + kb = blk * sb + kh * sh + (j % block_size) * so; + float dot = 0.0f; + if (vec_kv) { + /* A bf16 key row, 16 bytes at a time. */ + const uint16_t* kr = (const uint16_t*)cb + kb; + for (int64_t d = 0; d < head_dim; d += 8) { + const sycl::vec kk = *(const sycl::vec*)(kr + d); + for (int u = 0; u < 8; ++u) + dot += qs[d + u] * (rad_bf16_to_f32(kk[u]) * kds); + } + } else { + for (int64_t d = 0; d < head_dim; ++d) + dot += qs[d] * (rad_load_f32(cb, cdt, kb + d * sd) * kds); + } + sc = dot * scale; + } + } + const float bm = sycl::reduce_over_group(sg, sc, sycl::maximum()); + if (bm == -INFINITY) continue; /* uniform: no visible key in this step */ + const float mn = sycl::fmax(m, bm); + const float corr = sycl::exp(m - mn); + const float w = kb >= 0 ? sycl::exp(sc - mn) : 0.0f; + l = l * corr + sycl::reduce_over_group(sg, w, sycl::plus()); + for (int e = 0; e < per; ++e) acc[e] *= corr; + for (int jj = 0; jj < kSub; ++jj) { + const int64_t kbj = sycl::select_from_group(sg, kb, jj); + if (kbj < 0) continue; /* uniform */ + const float wj = sycl::select_from_group(sg, w, jj); + if (vec_kv && per == 8) { + const sycl::vec vv = *(const sycl::vec*)( + (const uint16_t*)cb + kbj + head_dim + lane * 8); + for (int e = 0; e < 8; ++e) acc[e] += wj * (rad_bf16_to_f32(vv[e]) * vds); + } else { + for (int e = 0; e < per; ++e) + acc[e] += wj * (rad_load_f32(cb, cdt, kbj + (head_dim + lane * per + e) * sd) * vds); + } + } m = mn; } + sycl::group_barrier(sg); if (P == 1) { const int64_t ooff = tok * o_rs + hq * o_hs; const float inv = l > 0.0f ? 1.0f / l : 0.0f; /* no visible key: zero row */