Archived
libxe2: DPAS flash attention at head_dim 64 and 128, not only 256
attn_flash_esimd (decode / verify and prefill) and attn_flash_quad (prefill) were written over HD but gated to head_dim 256 (Qwen3.8's full attention); every other model fell to the SIMT kernel. Both now dispatch 64, 128 and 256; the decode merge takes HD / 16 values a lane. MiniCPM5-2B bf16 (head_dim 128, gqa 8) on the Arc Pro B65, same build, XE2_ATTN_SIMT=1 as before: decode 69.3 -> 83.1 tok/s, prefill at ~2k tokens 1806 -> 8080 tok/s. rad-kbench attn_paged: fixture 207/207, MiniCPM5 160/160, Qwen3.8-27B 160/160. KL against XE2_EXACT=1 over the 12-document corpus: mean 0.00060, top-1 98.59% (the SIMT kernel: 0.00060, 98.59%). Qwen3.8-27B int4 unchanged (30.1 tok/s, 1361 tok/s prefill). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
989db71bfc
commit
3582900903
2 files changed
+91
-54
No files matched your search
@@ -259,61 +259,91 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
||||
});
|
||||
}
|
||||
|
||||
/* The decode / verify form at head_dim HD: the keys split kMaxP ways (partitions past the context exit at
|
||||
* once), the parts merged by a sub-group a (row, head), HD / 16 values a lane. */
|
||||
template <int HD>
|
||||
int flash_decode(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, int64_t gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
|
||||
int64_t o_hs, void* scratch, int64_t scratch_bytes) {
|
||||
constexpr int kMaxP = 32;
|
||||
constexpr int PL = HD / 16; /* values a lane in the merge */
|
||||
const int64_t q_heads = kv_heads * gqa, n = rows * q_heads * kMaxP;
|
||||
const int64_t need = n * (2 + HD) * (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 * HD;
|
||||
int rc = (gqa <= 8 ? launch<HD, true, 8> : launch<HD, true, kMaxGqa>)(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[PL];
|
||||
for (int e = 0; e < PL; ++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) * HD + lane * PL;
|
||||
for (int e = 0; e < PL; ++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 * PL;
|
||||
for (int e = 0; e < PL; ++e) orow[e] = rad_f32_to_bf16(acc[e] * inv);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* The prefill form at head_dim HD. */
|
||||
template <int HD>
|
||||
int flash_prefill(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, int64_t gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
|
||||
int64_t o_hs) {
|
||||
/* gqa <= 8: four 8-row blocks a work-group (their key and value loads shared through L1, 128 KiB of
|
||||
* staged queries at head_dim 256) and the next step's pages prefetched */
|
||||
if (gqa <= 8 && so == 2 * HD)
|
||||
return launch<HD, false, 8, 4, 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, 1, nullptr, nullptr);
|
||||
return (gqa <= 8 ? launch<HD, false, 8> : launch<HD, false, kMaxGqa>)(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, 1, nullptr, nullptr);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
/* attn_paged_fast's prefill form; RAD_E_UNSUPPORTED when the call is not one it serves (the caller
|
||||
* then runs the SIMT kernel). */
|
||||
/* attn_paged_fast's DPAS form at head_dim 64, 128 or 256; RAD_E_UNSUPPORTED when the call is not one
|
||||
* it serves (the caller then runs the SIMT kernel). */
|
||||
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, void* scratch, int64_t scratch_bytes) {
|
||||
if (head_dim != 256 || gqa < 1 || gqa > kMaxGqa) return RAD_E_UNSUPPORTED;
|
||||
if (gqa < 1 || gqa > kMaxGqa) return RAD_E_UNSUPPORTED;
|
||||
#define XE2_FLASH_ARGS q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, 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 = (gqa <= 8 ? launch<256, true, 8> : launch<256, true, kMaxGqa>)(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);
|
||||
});
|
||||
});
|
||||
switch (head_dim) {
|
||||
case 64: return flash_decode<64>(XE2_FLASH_ARGS, scratch, scratch_bytes);
|
||||
case 128: return flash_decode<128>(XE2_FLASH_ARGS, scratch, scratch_bytes);
|
||||
case 256: return flash_decode<256>(XE2_FLASH_ARGS, scratch, scratch_bytes);
|
||||
default: return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
}
|
||||
/* gqa <= 8: four 8-row blocks a work-group (their key and value loads shared through L1, 128 KiB of
|
||||
* staged queries) and the next step's pages prefetched */
|
||||
if (gqa <= 8 && so == 2 * 256)
|
||||
return launch<256, false, 8, 4, 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, 1, nullptr, nullptr);
|
||||
return (gqa <= 8 ? launch<256, false, 8> : launch<256, false, kMaxGqa>)(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, 1, nullptr, nullptr);
|
||||
switch (head_dim) {
|
||||
case 64: return flash_prefill<64>(XE2_FLASH_ARGS);
|
||||
case 128: return flash_prefill<128>(XE2_FLASH_ARGS);
|
||||
case 256: return flash_prefill<256>(XE2_FLASH_ARGS);
|
||||
default: return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
#undef XE2_FLASH_ARGS
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
@@ -346,17 +346,24 @@ int attn_flash_quad(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64_
|
||||
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) {
|
||||
if (head_dim != 256 || so != 2 * 256 || gqa < 1 || gqa > 8 || rows < 16) return RAD_E_UNSUPPORTED;
|
||||
if ((head_dim != 64 && head_dim != 128 && head_dim != 256) || so != 2 * head_dim || 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, 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);
|
||||
* queries and swap area at head_dim 256; one group a work-group was 28.3 TFLOPS at 64k, two 30.3);
|
||||
* gqa 7-8 one */
|
||||
#define XE2_QUAD(HD) \
|
||||
(gqa <= 6 ? launch_quad<HD, 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) \
|
||||
: launch_quad<HD, 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))
|
||||
if (head_dim == 64) return XE2_QUAD(64);
|
||||
if (head_dim == 128) return XE2_QUAD(128);
|
||||
return XE2_QUAD(256);
|
||||
#undef XE2_QUAD
|
||||
}
|
||||
|
||||
} // namespace xe2
|
||||
Reference in new issue
Block a user