Archived
libxe2: quad prefill attention two 16-row groups a work-group, loops fully unrolled
Fewer query heads a work-group (more work-groups a core) lost badly -- 28.3 -> 16.5 TFLOPS at 64k with two of six, 11.3 with one: the six heads' threads sharing each page through L1 matter more than occupancy. So the other way: for gqa <= 6 a work-group is two 16-row groups (48 threads, 126 KiB of staged queries and swap area), walking to its later group's end so every thread joins every barrier (the earlier group's extra keys are past its rows' positions: hidden, P = 0, the same output). Queries read from global instead of staged cost ~10% and were dropped; three row groups (72 threads) do not launch. #pragma unroll had left the score and value loops rolled (5 DPAS in the binary); clang loop unroll(full) gives the 64, worth ~1%. Probe, 2048 rows over 2k / 8k / 32k / 64k: 18.8 / 28.0 / 28.5 / 28.1 -> 18.4 / 30.0 / 30.5 / 30.5 TFLOPS, output bit-identical. Fresh prompts (mtp4fc, MTP 6): ~2.1k 1426 -> 1425 tok/s, ~24k 1221 -> 1234, ~47k 1025 -> 1048. 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
86955a62c5
commit
5e44973a36
1 file changed
+63
-39
@@ -36,44 +36,52 @@ 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 MAXG>
|
||||
template <int HD, int QH, bool QSLM, int RG = 1>
|
||||
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,
|
||||
int64_t o_hs) {
|
||||
int64_t o_hs, int qhn) {
|
||||
constexpr int KS = HD / 16; /* head-dim steps */
|
||||
constexpr int CQ = KS / 4; /* a thread's output column blocks */
|
||||
constexpr int VB = CQ; /* value loads a batch: the thread's four */
|
||||
constexpr int kQ8 = 8 * HD * 2; /* 8 staged query rows */
|
||||
constexpr int kQHead = 2 * kQ8; /* a head's 16 */
|
||||
constexpr uint32_t kX = MAXG * kQHead; /* the swap area: a head's [qd] maxima, sums, P */
|
||||
constexpr uint32_t kX = QSLM ? RG * QH * kQHead : 0; /* the swap area: a head's [qd] maxima, sums, P */
|
||||
constexpr int kXHead = 4 * (16 * 4 + 16 * 4 + 16 * 16 * 2);
|
||||
const int64_t nrg = (rows + 15) / 16;
|
||||
const int64_t nrg = (rows + 16 * RG - 1) / (16 * RG);
|
||||
const int ngrp = gqa / qhn; /* work-groups a (16 rows, kv head): qhn <= QH query heads each */
|
||||
return submit([&] {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(nrg * kv_heads * 4 * gqa), (size_t)(4 * gqa)},
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)(nrg * kv_heads * ngrp * 4 * qhn * RG), (size_t)(4 * qhn * RG)},
|
||||
[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||
using namespace esimd;
|
||||
slm_init<kX + MAXG * kXHead>();
|
||||
slm_init<kX + RG * QH * kXHead>();
|
||||
const int tl = (int)it.get_local_id(0);
|
||||
const int t = tl >> 2, qd = tl & 3;
|
||||
const int rgi = tl / (4 * qhn), tq = tl % (4 * qhn); /* the row group, the thread in it */
|
||||
const int t = tq >> 2, qd = tq & 3;
|
||||
const int64_t wg = (int64_t)it.get_group(0);
|
||||
const int64_t kh = wg % kv_heads, r0 = (wg / kv_heads) * 16;
|
||||
const int64_t hq = kh * gqa + t;
|
||||
const int64_t hg = wg % ngrp, wr = wg / ngrp;
|
||||
const int64_t kh = wr % kv_heads, rw0 = (wr / kv_heads) * 16 * RG, r0 = rw0 + 16 * rgi;
|
||||
const int64_t hq = kh * gqa + hg * qhn + t;
|
||||
const int64_t ctx = ld_index(sudt, sub, 0);
|
||||
const int64_t jend = ctx < jcap ? ctx : jcap;
|
||||
const int64_t pos0 = ctx - rows + r0;
|
||||
const int nvalid = (int)(rows - r0 < 16 ? rows - r0 : 16);
|
||||
const int nvalid = (int)(rows - r0 < 16 ? (rows - r0 > 0 ? rows - r0 : 0) : 16);
|
||||
const int64_t jhi0 = pos0 + nvalid;
|
||||
const int64_t jhi = jhi0 < jend ? jhi0 : jend;
|
||||
/* the work-group's walk: its last row group's keys, so every thread joins every barrier
|
||||
* (an earlier group's extra keys are past its rows' positions: hidden) */
|
||||
const int64_t rlast = (rw0 + 16 * RG < rows ? rw0 + 16 * RG : rows);
|
||||
const int64_t jl0 = ctx - rows + rlast;
|
||||
const int64_t jhw = jl0 < jend ? jl0 : jend;
|
||||
/* the head's 16 query rows to local memory, [half][step][row][16]: threads 0 and 1 of
|
||||
* the quad a half each (split four ways, by head-dim steps as well, the output was
|
||||
* wrong; not understood, and the staging is once a work-group) */
|
||||
const uint32_t qh0 = (uint32_t)(t * kQHead);
|
||||
const uint32_t qh0 = (uint32_t)((rgi * QH + t) * kQHead);
|
||||
{
|
||||
const int hh = qd & 1;
|
||||
const uint16_t* qh = qb + hq * q_hs;
|
||||
if (qd < 2 && r0 + 8 * hh < rows) {
|
||||
#pragma unroll
|
||||
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>(
|
||||
qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k2 * 32, (int)(r0 + 8 * hh));
|
||||
@@ -81,33 +89,33 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
}
|
||||
}
|
||||
}
|
||||
const uint32_t xb = kX + (uint32_t)(t * kXHead);
|
||||
const uint32_t xb = kX + (uint32_t)((rgi * QH + t) * kXHead);
|
||||
const uint32_t xmax = xb, xsum = xb + 4 * 64, xp = xb + 8 * 64; /* [qd][16] f32, [qd][16] f32, [qd][2][128] bf16 */
|
||||
barrier();
|
||||
simd<float, 128> O[2][CQ];
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int h = 0; h < 2; ++h)
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int c = 0; c < CQ; ++c) O[h][c] = 0.0f;
|
||||
simd<float, 16> m = -INFINITY, l = 0.0f;
|
||||
const simd<int32_t, 16> lane(0, 1);
|
||||
const unsigned pw = (unsigned)(so * 2 - 1), ph = 15u;
|
||||
for (int64_t j0 = 0; j0 < jhi; j0 += 64) {
|
||||
for (int64_t j0 = 0; j0 < jhw; j0 += 64) {
|
||||
/* the next step's four pages to L1, a page a thread of the first quad */
|
||||
if (tl < 4 && j0 + 64 + 16 * tl < jhi) {
|
||||
if (tl < 4 && j0 + 64 + 16 * tl < jhw) {
|
||||
const int64_t bn = ld_index(btdt, btb, ((j0 + 64 + 16 * tl) / 16) * bt_cs);
|
||||
if (bn >= 0) {
|
||||
const char* base = (const char*)(cb + bn * sb + kh * sh);
|
||||
const simd<uint32_t, 32> lo(0, 64);
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int i = 0; i < 16 * 2 * HD * 2 / 2048; ++i) /* so == 2 HD: the caller checks */
|
||||
xesimd::lsc_prefetch<uint32_t, 1, xesimd::lsc_data_size::default_size, xesimd::cache_hint::cached,
|
||||
xesimd::cache_hint::cached, 32>((const uint32_t*)(base + i * 2048), lo);
|
||||
}
|
||||
}
|
||||
const bool two = j0 + 32 < jhi; /* uniform: the step's second half has keys */
|
||||
const bool two = j0 + 32 < jhw; /* uniform: the step's second half has keys */
|
||||
int64_t blk[4];
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int p = 0; p < 4; ++p) {
|
||||
const int64_t jp = j0 + 16 * p;
|
||||
blk[p] = jp < jhi ? ld_index(btdt, btb, (jp / 16) * bt_cs) : -1;
|
||||
@@ -117,10 +125,21 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
simd<float, 128> S0 = 0.0f, S1 = 0.0f;
|
||||
if (bo >= 0) {
|
||||
const uint32_t* pk = (const uint32_t*)(cb + bo * sb + kh * sh);
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int k = 0; k < KS; ++k) {
|
||||
simd<uint32_t, 64> qa0 = slm_block_load<uint32_t, 64>(qh0 + k * 256);
|
||||
simd<uint32_t, 64> qa1 = slm_block_load<uint32_t, 64>(qh0 + kQ8 + k * 256);
|
||||
simd<uint32_t, 64> qa0, qa1;
|
||||
if constexpr (QSLM) {
|
||||
qa0 = slm_block_load<uint32_t, 64>(qh0 + k * 256);
|
||||
qa1 = slm_block_load<uint32_t, 64>(qh0 + kQ8 + k * 256);
|
||||
} else { /* from the query rows, through L1 */
|
||||
const uint16_t* qh = qb + hq * q_hs;
|
||||
simd<uint16_t, 128> u0 = load_2d<uint16_t, 16, 8, 1, false>(
|
||||
qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k * 16, (int)r0);
|
||||
simd<uint16_t, 128> u1 = load_2d<uint16_t, 16, 8, 1, false>(
|
||||
qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k * 16, (int)(r0 + 8));
|
||||
qa0 = u0.template bit_cast_view<uint32_t>();
|
||||
qa1 = u1.template bit_cast_view<uint32_t>();
|
||||
}
|
||||
simd<uint32_t, 128> kt = load_2d<uint32_t, 8, 16, 1, true>(pk, pw, ph, pw, k * 8, 0);
|
||||
S0 = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16,
|
||||
xmx::dpas_argument_type::bf16>(S0, kt, qa0);
|
||||
@@ -132,7 +151,7 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
S1 *= scale;
|
||||
const simd<int32_t, 16> jj = lane + (int32_t)(j0 + 16 * qd);
|
||||
simd<float, 16> mo;
|
||||
#pragma unroll
|
||||
#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);
|
||||
simd_mask<16> h1 = (jj > (int32_t)(pos0 + 8 + r)) | (jj >= (int32_t)jhi);
|
||||
@@ -148,7 +167,7 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
/* the halves' maxima and bases, in turn: half 0 from m, half 1 from half 0's */
|
||||
simd<float, 16> mh[2], baseh[2], corrh[2];
|
||||
simd<float, 16> mprev = m;
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int h = 0; h < 2; ++h) {
|
||||
if (h == 1 && !two) break;
|
||||
const simd<float, 16> a0 = slm_block_load<float, 16>(xmax + (2 * h) * 64);
|
||||
@@ -165,7 +184,7 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
simd<float, 16> ro = 0.0f;
|
||||
if (myh == 0 || two) {
|
||||
const simd<float, 16> bs = baseh[myh];
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
simd<float, 16> x = exp(S0.template select<16, 1>(r * 16) - (float)bs[r]);
|
||||
S0.template select<16, 1>(r * 16) = x;
|
||||
@@ -179,7 +198,7 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
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 unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int h = 0; h < 2; ++h) {
|
||||
if (h == 1 && !two) break;
|
||||
simd<float, 16> rs = 0.0f;
|
||||
@@ -188,27 +207,27 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
const simd<float, 16> corr = corrh[h];
|
||||
l = l * corr + rs;
|
||||
if ((corr != 1.0f).any()) {
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int c = 0; c < CQ; ++c)
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
O[0][c].template select<16, 1>(r * 16) *= (float)corr[r];
|
||||
O[1][c].template select<16, 1>(r * 16) *= (float)corr[8 + r];
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int p = 2 * h; p < 2 * h + 2; ++p) {
|
||||
if (blk[p] < 0) continue;
|
||||
const simd<uint16_t, 128> pa0 = slm_block_load<uint16_t, 128>(xp + p * 512);
|
||||
const simd<uint16_t, 128> pa1 = slm_block_load<uint16_t, 128>(xp + p * 512 + 256);
|
||||
const uint16_t* pg = cb + blk[p] * sb + kh * sh;
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int cv = 0; cv < CQ; cv += VB) {
|
||||
simd<uint16_t, 256> vb[VB];
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int b = 0; b < VB; ++b)
|
||||
vb[b] = load_2d<uint16_t, 16, 16, 1, false, true>(pg, pw, ph, pw, HD + (qd * CQ + cv + b) * 16, 0);
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int b = 0; b < VB; ++b) {
|
||||
O[0][cv + b] = xmx::dpas<8, 8, float, float, uint16_t, uint16_t, xmx::dpas_argument_type::bf16,
|
||||
xmx::dpas_argument_type::bf16>(O[0][cv + b], vb[b], pa0);
|
||||
@@ -222,13 +241,13 @@ int launch_quad(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
|
||||
}
|
||||
simd<float, 16> inv = 1.0f / l;
|
||||
inv.merge(simd<float, 16>(0.0f), l <= 0.0f);
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int h = 0; h < 2; ++h)
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
if (8 * h + r >= nvalid) break;
|
||||
simd<uint16_t, HD / 4> row;
|
||||
#pragma unroll
|
||||
#pragma clang loop unroll(full)
|
||||
for (int c = 0; c < CQ; ++c) {
|
||||
simd<float, 16> v = O[h][c].template select<16, 1>(r * 16) * (float)inv[8 * h + r];
|
||||
simd<uint32_t, 16> u = v.template bit_cast_view<uint32_t>();
|
||||
@@ -248,8 +267,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;
|
||||
return (gqa <= 6 ? launch_quad<256, 6> : launch_quad<256, 8>)(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);
|
||||
/* 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,
|
||||
(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);
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
Reference in new issue
Block a user