Archived
libxe2: decode / verify attention on DPAS, the keys split 32 ways
attn_paged_fast's single-sequence calls under 16 rows took the SIMT kernel (a lane a key, full 256-wide dots: ~137 us a verify call at ~400 tokens). The flash kernel now has a split form: a work-group a (row block, kv head, partition), partitions past the context exit at once, each partition's unnormalised (m, l, O) to scratch and a merge pass a sub-group a (row, head). XE2_ATTN_DEC_SIMT=1 keeps the SIMT kernel. rad-kbench -m mtp4 --op attn_paged: 240/240; greedy against SIMT 5/8 identical, the others diverge late at near-ties. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
726e532114
commit
2d2356ad4b
2 files changed
+87
-13
No files matched your search
@@ -34,7 +34,7 @@ namespace xe2 {
|
||||
int attn_flash_esimd(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb,
|
||||
int64_t sb, int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub,
|
||||
uint32_t sudt, int64_t rows, int64_t kv_heads, int64_t gqa, float scale, int64_t jcap,
|
||||
uint16_t* ob, int64_t o_rs, int64_t o_hs);
|
||||
uint16_t* ob, int64_t o_rs, int64_t o_hs, void* scratch, int64_t scratch_bytes);
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -113,7 +113,7 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
|
||||
|
||||
/* A single sequence's prefill on DPAS (xe2_attn_flash_esimd.cpp): bf16 throughout, pages of 16,
|
||||
* causal without a window, every 2D load's base and pitch on its alignment. */
|
||||
if (n_seq == 1 && rows >= 16 && causal && window == 0 && !KD && !VD && Q->dtype == RAD_BF16 &&
|
||||
if (n_seq == 1 && (rows >= 16 || !std::getenv("XE2_ATTN_DEC_SIMT")) && causal && window == 0 && !KD && !VD && Q->dtype == RAD_BF16 &&
|
||||
KV->dtype == RAD_BF16 && O->dtype == RAD_BF16 && q_ds == 1 && o_ds == 1 && sd == 1 && block_size == 16 &&
|
||||
rad_ptr_aligned(Q->data, 64) && rad_ptr_aligned(KV->data, 64) && rad_ptr_aligned(O->data, 64) &&
|
||||
(q_rs * 2) % 64 == 0 && (q_hs * 2) % 64 == 0 && (so * 2) % 64 == 0 && (sb * 2) % 64 == 0 &&
|
||||
@@ -124,7 +124,7 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
|
||||
const int rc = attn_flash_esimd(*fq, head_dim, (const uint16_t*)Q->data, q_rs, q_hs, (const uint16_t*)KV->data,
|
||||
sb, sh, so, (const char*)BT->data, BT->dtype, bt_cs, (const char*)SU->data,
|
||||
SU->dtype, rows, kv_heads, gqa, fscale, max_blocks * block_size,
|
||||
(uint16_t*)O->data, o_rs, o_hs);
|
||||
(uint16_t*)O->data, o_rs, o_hs, a->scratch, a->scratch_bytes);
|
||||
if (rc != RAD_E_UNSUPPORTED) return rc;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,23 +37,29 @@ inline esimd::simd<uint16_t, 128> bf16_of(esimd::simd<float, 128> v) SYCL_ESIMD_
|
||||
return simd<uint16_t, 128>((u + 0x7FFFu + ((u >> 16) & 1u)) >> 16);
|
||||
}
|
||||
|
||||
template <int HD>
|
||||
/* SPLIT (the decode / verify form, a few rows): the keys are also split P ways, a work-group a
|
||||
* (row block, kv head, partition) -- one 8-row block alone is kv_heads work-groups of gqa threads,
|
||||
* each walking the whole context. A partition's unnormalised state (m, l, O) goes to scratch,
|
||||
* [row][head][P] of (m, l) in `ml` and of HD floats in `op`, and merge_parts adds them. */
|
||||
template <int HD, bool SPLIT>
|
||||
int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb, int64_t sb,
|
||||
int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub, uint32_t sudt,
|
||||
int64_t rows, int64_t kv_heads, int gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
|
||||
int64_t o_hs) {
|
||||
int64_t o_hs, int NP = 1, float* ml = nullptr, float* op = nullptr) {
|
||||
constexpr int KS = HD / 16; /* head-dim steps */
|
||||
constexpr int kQBytes = 8 * HD * 2; /* a thread's staged query rows */
|
||||
const int64_t nqb = (rows + 7) / 8;
|
||||
namespace intelex = sycl::ext::intel::experimental;
|
||||
syclex::properties props{intelex::grf_size<256>};
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(nqb * kv_heads * gqa), (size_t)gqa}, props,
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(nqb * kv_heads * NP * gqa), (size_t)gqa}, props,
|
||||
[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||
using namespace esimd;
|
||||
slm_init<kMaxGqa * kQBytes>();
|
||||
const int t = (int)it.get_local_id(0);
|
||||
const int64_t wg = (int64_t)it.get_group(0);
|
||||
const int64_t wg0 = (int64_t)it.get_group(0);
|
||||
const int part = (int)(wg0 % NP);
|
||||
const int64_t wg = wg0 / NP;
|
||||
const int64_t kh = wg % kv_heads, r0 = (wg / kv_heads) * 8;
|
||||
const int64_t hq = kh * gqa + t;
|
||||
const int64_t ctx = ld_index(sudt, sub, 0);
|
||||
@@ -61,7 +67,22 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
||||
const int64_t pos0 = ctx - rows + r0;
|
||||
const int nvalid = (int)(rows - r0 < 8 ? rows - r0 : 8);
|
||||
const int64_t jhi0 = pos0 + nvalid; /* causal: keys below the last row's position + 1 */
|
||||
const int64_t jhi = jhi0 < jend ? jhi0 : jend;
|
||||
int64_t jhi = jhi0 < jend ? jhi0 : jend;
|
||||
int64_t jbeg = 0;
|
||||
if constexpr (SPLIT) { /* this partition's whole 32-key steps */
|
||||
const int64_t nst = (jhi + 31) / 32, per = (nst + NP - 1) / NP;
|
||||
jbeg = part * per * 32;
|
||||
const int64_t je = jbeg + per * 32;
|
||||
if (je < jhi) jhi = je;
|
||||
if (jbeg >= jhi) { /* uniform over the work-group: no keys here */
|
||||
for (int r = 0; r < nvalid; ++r) {
|
||||
const int64_t pi = ((r0 + r) * kv_heads * gqa + hq) * NP + part;
|
||||
ml[pi * 2] = -INFINITY;
|
||||
ml[pi * 2 + 1] = 0.0f;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* the 8 query rows of head hq to local memory, [step][row][16]: a step's A operand one load */
|
||||
const uint32_t qsl = (uint32_t)(t * kQBytes);
|
||||
const uint16_t* qh = qb + hq * q_hs;
|
||||
@@ -78,7 +99,7 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
||||
simd<float, 8> m = -INFINITY, l = 0.0f;
|
||||
const simd<int32_t, 16> lane(0, 1);
|
||||
const unsigned pw = (unsigned)(so * 2 - 1), ph = 15u; /* a page: 16 rows of so elements */
|
||||
for (int64_t j0 = 0; j0 < jhi; j0 += 32) {
|
||||
for (int64_t j0 = jbeg; j0 < jhi; j0 += 32) {
|
||||
simd<float, 128> S[2];
|
||||
int64_t blk[2];
|
||||
#pragma unroll
|
||||
@@ -105,7 +126,7 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
||||
const simd<int32_t, 16> jj = lane + (int32_t)(j0 + 16 * p);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
simd_mask<16> hide = (jj > (int32_t)(pos0 + r)) | (jj >= (int32_t)jend);
|
||||
simd_mask<16> hide = (jj > (int32_t)(pos0 + r)) | (jj >= (int32_t)jhi);
|
||||
if (blk[p] < 0 || r >= nvalid) hide = 1;
|
||||
S[p].template select<16, 1>(r * 16).merge(simd<float, 16>(-INFINITY), hide);
|
||||
}
|
||||
@@ -151,6 +172,20 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
||||
}
|
||||
m = mn;
|
||||
}
|
||||
if constexpr (SPLIT) {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
if (r >= nvalid) break;
|
||||
const int64_t pi = ((r0 + r) * kv_heads * gqa + hq) * NP + part;
|
||||
ml[pi * 2] = m[r];
|
||||
ml[pi * 2 + 1] = l[r];
|
||||
simd<float, HD> row;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < KS; ++c) row.template select<16, 1>(c * 16) = O[c].template select<16, 1>(r * 16);
|
||||
block_store<float, HD>(op + pi * HD, row);
|
||||
}
|
||||
return;
|
||||
}
|
||||
simd<float, 8> inv = 1.0f / l;
|
||||
inv.merge(simd<float, 8>(0.0f), l <= 0.0f); /* no visible key: a zero row, as libref */
|
||||
#pragma unroll
|
||||
@@ -176,10 +211,49 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
||||
int attn_flash_esimd(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb,
|
||||
int64_t sb, int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub,
|
||||
uint32_t sudt, int64_t rows, int64_t kv_heads, int64_t gqa, float scale, int64_t jcap,
|
||||
uint16_t* ob, int64_t o_rs, int64_t o_hs) {
|
||||
uint16_t* ob, int64_t o_rs, int64_t o_hs, void* scratch, int64_t scratch_bytes) {
|
||||
if (head_dim != 256 || gqa < 1 || gqa > kMaxGqa) return RAD_E_UNSUPPORTED;
|
||||
return launch<256>(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, (int)gqa, scale,
|
||||
jcap, ob, o_rs, o_hs);
|
||||
if (rows < 16) {
|
||||
/* The decode / verify form: the keys split kMaxP ways (partitions past the context exit at
|
||||
* once), the parts merged by a sub-group a (row, head). */
|
||||
constexpr int kMaxP = 32;
|
||||
const int64_t q_heads = kv_heads * gqa, n = rows * q_heads * kMaxP;
|
||||
const int64_t need = n * (2 + 256) * (int64_t)sizeof(float);
|
||||
if (!scratch || scratch_bytes < need || !rad_ptr_aligned(scratch, 64)) return RAD_E_UNSUPPORTED;
|
||||
float* op = (float*)scratch;
|
||||
float* ml = op + n * 256;
|
||||
int rc = launch<256, true>(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads,
|
||||
(int)gqa, scale, jcap, ob, o_rs, o_hs, kMaxP, ml, op);
|
||||
if (rc != RAD_OK) return rc;
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(rows * q_heads) * 16, 16},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(16)]] {
|
||||
const int64_t g = (int64_t)it.get_group(0); /* row * q_heads + head */
|
||||
const int lane = (int)it.get_local_id(0);
|
||||
const float* mlg = ml + g * kMaxP * 2;
|
||||
float M = -INFINITY;
|
||||
for (int p = 0; p < kMaxP; ++p)
|
||||
if (mlg[p * 2 + 1] > 0.0f) M = sycl::fmax(M, mlg[p * 2]);
|
||||
float L = 0.0f, acc[16];
|
||||
for (int e = 0; e < 16; ++e) acc[e] = 0.0f;
|
||||
if (M != -INFINITY)
|
||||
for (int p = 0; p < kMaxP; ++p) {
|
||||
const float lp = mlg[p * 2 + 1];
|
||||
if (!(lp > 0.0f)) continue;
|
||||
const float f = sycl::exp(mlg[p * 2] - M);
|
||||
L += lp * f;
|
||||
const float* o = op + (g * kMaxP + p) * 256 + lane * 16;
|
||||
for (int e = 0; e < 16; ++e) acc[e] += o[e] * f;
|
||||
}
|
||||
const float inv = L > 0.0f ? 1.0f / L : 0.0f;
|
||||
const int64_t row = g / q_heads, hq = g % q_heads;
|
||||
uint16_t* orow = ob + row * o_rs + hq * o_hs + lane * 16;
|
||||
for (int e = 0; e < 16; ++e) orow[e] = rad_f32_to_bf16(acc[e] * inv);
|
||||
});
|
||||
});
|
||||
}
|
||||
return launch<256, false>(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, (int)gqa,
|
||||
scale, jcap, ob, o_rs, o_hs);
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
Reference in new issue
Block a user