Archived
libxe2: quad prefill attention scores as S^T = K Q^T, softmax vertical across key rows
The score tile is now keys by query rows: K is the A operand (plain 2D loads instead of the
transposed one), Q^T the B operand, staged once a work-group by a transposed load. With the 16
query rows across the lanes, a row's max is a vertical max over the page's 16 key rows instead of
32 horizontal reductions a thread a step, exp(S - base) one subtract a key row, and a row's sum is
taken in reduce<>'s own order (lanes i and i + 8, then i + 4, i + 2, i + 1 -- key rows here), so
every value is the old one: probe output bit-identical, 64 x 64 through 64k. P goes to the swap
area as [query][key], two keys a 32-bit scatter lane, the layout the P V step already reads.
XE2_FAQ_KA=0 at build time keeps the S = Q K^T form.
Probe, 2048 rows over 2k / 16k / 64k: 16.9 / 30.6 / 30.5 -> 18.8 / 32.9 / 32.5 TFLOPS. Fresh
prompts (mtp4fc, MTP 6), against 5e44973: ~2.1k 1425 -> 1427 tok/s, ~24k 1234 -> 1249, ~47k 1048 ->
1066. rad-kbench attn --all-rows 227/227, full replay 1410/1410, greedy 8/8.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5e44973a36
commit
fc5f00a169
1 file changed
+87
-4
@@ -36,7 +36,7 @@ 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, int QH, bool QSLM, int RG = 1>
|
||||
template <int HD, int QH, bool QSLM, int RG = 1, bool KA = false>
|
||||
int launch_quad(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,
|
||||
@@ -80,7 +80,17 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
{
|
||||
const int hh = qd & 1;
|
||||
const uint16_t* qh = qb + hq * q_hs;
|
||||
if (QSLM && qd < 2 && r0 + 8 * hh < rows) {
|
||||
if (KA) {
|
||||
/* Q^T a head-dim step: [8 u32 of dim pairs][16 rows], the B operand of S^T = K Q^T */
|
||||
if (qd == 0 && r0 < rows) {
|
||||
#pragma clang loop unroll(full)
|
||||
for (int k = 0; k < KS; ++k) {
|
||||
simd<uint32_t, 128> qt = load_2d<uint32_t, 8, 16, 1, true>(
|
||||
(const uint32_t*)qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k * 8, (int)r0);
|
||||
slm_block_store<uint32_t, 128>(qh0 + k * 512, qt);
|
||||
}
|
||||
}
|
||||
} else if (QSLM && qd < 2 && r0 + 8 * hh < rows) {
|
||||
#pragma clang loop unroll(full)
|
||||
for (int k2 = 0; k2 < KS / 2; ++k2) { /* pairs of head-dim steps */
|
||||
simd<uint16_t, 256> qq = load_2d<uint16_t, 16, 8, 2, false>(
|
||||
@@ -123,6 +133,41 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
/* this thread's page: rows 0-7 and 8-15, two chains on one key tile */
|
||||
const int64_t bo = blk[qd];
|
||||
simd<float, 128> S0 = 0.0f, S1 = 0.0f;
|
||||
simd<float, 16> mo;
|
||||
if constexpr (KA) {
|
||||
/* S^T: keys 0-7 (S0) and 8-15 (S1) of the page by the 16 query rows, K the A
|
||||
* operand (plain 2D loads), the row maxima vertical across the key rows */
|
||||
if (bo >= 0) {
|
||||
const uint16_t* pk = cb + bo * sb + kh * sh;
|
||||
#pragma clang loop unroll(full)
|
||||
for (int k = 0; k < KS; ++k) {
|
||||
simd<uint32_t, 128> qt = slm_block_load<uint32_t, 128>(qh0 + k * 512);
|
||||
simd<uint16_t, 128> k0 = load_2d<uint16_t, 16, 8, 1, false>(pk, pw, ph, pw, k * 16, 0);
|
||||
simd<uint16_t, 128> k1 = load_2d<uint16_t, 16, 8, 1, false>(pk, pw, ph, pw, k * 16, 8);
|
||||
S0 = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16,
|
||||
xmx::dpas_argument_type::bf16>(S0, qt, simd<uint32_t, 64>(k0.template bit_cast_view<uint32_t>()));
|
||||
S1 = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16,
|
||||
xmx::dpas_argument_type::bf16>(S1, qt, simd<uint32_t, 64>(k1.template bit_cast_view<uint32_t>()));
|
||||
}
|
||||
}
|
||||
S0 *= scale;
|
||||
S1 *= scale;
|
||||
const simd<int32_t, 16> qpos = lane + (int32_t)pos0;
|
||||
const simd_mask<16> qbad = lane >= nvalid;
|
||||
mo = -INFINITY;
|
||||
#pragma clang loop unroll(full)
|
||||
for (int kr = 0; kr < 8; ++kr) {
|
||||
const int32_t ja = (int32_t)(j0 + 16 * qd + kr), jb = ja + 8;
|
||||
simd_mask<16> h0 = (ja > qpos) | qbad, h1 = (jb > qpos) | qbad;
|
||||
if (bo < 0 || ja >= (int32_t)jhi) h0 = 1;
|
||||
if (bo < 0 || jb >= (int32_t)jhi) h1 = 1;
|
||||
S0.template select<16, 1>(kr * 16).merge(simd<float, 16>(-INFINITY), h0);
|
||||
S1.template select<16, 1>(kr * 16).merge(simd<float, 16>(-INFINITY), h1);
|
||||
mo = max(mo, simd<float, 16>(S0.template select<16, 1>(kr * 16)));
|
||||
mo = max(mo, simd<float, 16>(S1.template select<16, 1>(kr * 16)));
|
||||
}
|
||||
} else {
|
||||
|
||||
if (bo >= 0) {
|
||||
const uint32_t* pk = (const uint32_t*)(cb + bo * sb + kh * sh);
|
||||
#pragma clang loop unroll(full)
|
||||
@@ -150,7 +195,6 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
S0 *= scale;
|
||||
S1 *= scale;
|
||||
const simd<int32_t, 16> jj = lane + (int32_t)(j0 + 16 * qd);
|
||||
simd<float, 16> mo;
|
||||
#pragma clang loop unroll(full)
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
simd_mask<16> h0 = (jj > (int32_t)(pos0 + r)) | (jj >= (int32_t)jhi);
|
||||
@@ -162,6 +206,7 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
mo[r] = hmax<float>(simd<float, 16>(S0.template select<16, 1>(r * 16)));
|
||||
mo[8 + r] = hmax<float>(simd<float, 16>(S1.template select<16, 1>(r * 16)));
|
||||
}
|
||||
}
|
||||
slm_block_store<float, 16>(xmax + qd * 64, mo);
|
||||
barrier();
|
||||
/* the halves' maxima and bases, in turn: half 0 from m, half 1 from half 0's */
|
||||
@@ -182,6 +227,40 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
}
|
||||
const int myh = qd >> 1;
|
||||
simd<float, 16> ro = 0.0f;
|
||||
if constexpr (KA) {
|
||||
if (myh == 0 || two) {
|
||||
const simd<float, 16> bs = baseh[myh];
|
||||
#pragma clang loop unroll(full)
|
||||
for (int kr = 0; kr < 8; ++kr) {
|
||||
S0.template select<16, 1>(kr * 16) = exp(simd<float, 16>(S0.template select<16, 1>(kr * 16)) - bs);
|
||||
S1.template select<16, 1>(kr * 16) = exp(simd<float, 16>(S1.template select<16, 1>(kr * 16)) - bs);
|
||||
}
|
||||
/* a row's sum over its 16 keys in reduce<>'s order: lanes i and i + 8, then
|
||||
* i + 4, i + 2, i + 1 -- key rows here */
|
||||
simd<float, 16> t8[8];
|
||||
#pragma clang loop unroll(full)
|
||||
for (int i = 0; i < 8; ++i)
|
||||
t8[i] = simd<float, 16>(S0.template select<16, 1>(i * 16)) + simd<float, 16>(S1.template select<16, 1>(i * 16));
|
||||
#pragma clang loop unroll(full)
|
||||
for (int i = 0; i < 4; ++i) t8[i] = t8[i] + t8[i + 4];
|
||||
#pragma clang loop unroll(full)
|
||||
for (int i = 0; i < 2; ++i) t8[i] = t8[i] + t8[i + 2];
|
||||
ro = t8[0] + t8[1];
|
||||
}
|
||||
slm_block_store<float, 16>(xsum + qd * 64, ro);
|
||||
/* P to [query][key], two keys a 32-bit lane: the layout the P V step reads */
|
||||
const simd<uint32_t, 16> qo = simd<uint32_t, 16>(lane) * 32u + (uint32_t)(xp + qd * 512);
|
||||
#pragma clang loop unroll(full)
|
||||
for (int kp = 0; kp < 8; ++kp) {
|
||||
const int ka = 2 * kp, kb = ka + 1; /* key rows: 0-7 in S0, 8-15 in S1 */
|
||||
simd<float, 16> fa = ka < 8 ? simd<float, 16>(S0.template select<16, 1>(ka * 16)) : simd<float, 16>(S1.template select<16, 1>((ka - 8) * 16));
|
||||
simd<float, 16> fb = kb < 8 ? simd<float, 16>(S0.template select<16, 1>(kb * 16)) : simd<float, 16>(S1.template select<16, 1>((kb - 8) * 16));
|
||||
simd<uint32_t, 16> ua = fa.template bit_cast_view<uint32_t>(), ub = fb.template bit_cast_view<uint32_t>();
|
||||
ua = (ua + 0x7FFFu + ((ua >> 16) & 1u)) >> 16;
|
||||
ub = (ub + 0x7FFFu + ((ub >> 16) & 1u)) >> 16;
|
||||
slm_scatter<uint32_t, 16>(qo + (uint32_t)(ka * 2), ua | (ub << 16));
|
||||
}
|
||||
} else {
|
||||
if (myh == 0 || two) {
|
||||
const simd<float, 16> bs = baseh[myh];
|
||||
#pragma clang loop unroll(full)
|
||||
@@ -197,6 +276,7 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
slm_block_store<float, 16>(xsum + qd * 64, ro);
|
||||
slm_block_store<uint16_t, 128>(xp + qd * 512, bf16_of(S0));
|
||||
slm_block_store<uint16_t, 128>(xp + qd * 512 + 256, bf16_of(S1));
|
||||
}
|
||||
barrier();
|
||||
#pragma clang loop unroll(full)
|
||||
for (int h = 0; h < 2; ++h) {
|
||||
@@ -267,10 +347,13 @@ int attn_flash_quad(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64_
|
||||
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) {
|
||||
if (head_dim != 256 || so != 2 * 256 || gqa < 1 || gqa > 8 || rows < 16) return RAD_E_UNSUPPORTED;
|
||||
#ifndef XE2_FAQ_KA
|
||||
#define XE2_FAQ_KA 1
|
||||
#endif
|
||||
/* gqa <= 6: two 16-row groups a work-group, 48 threads walking the same pages (126 KiB of staged
|
||||
* queries and swap area; one group a work-group was 28.3 TFLOPS at 64k, two 30.3); gqa 7-8 one */
|
||||
if (gqa <= 6)
|
||||
return launch_quad<256, 6, true, 2>(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads,
|
||||
return launch_quad<256, 6, true, 2, XE2_FAQ_KA>(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, (int)gqa);
|
||||
return launch_quad<256, 8, true, 1>(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, (int)gqa);
|
||||
|
||||
Reference in new issue
Block a user