diff --git a/libxe2/xe2_attn_fast.cpp b/libxe2/xe2_attn_fast.cpp index f46a674b..573f94d9 100644 --- a/libxe2/xe2_attn_fast.cpp +++ b/libxe2/xe2_attn_fast.cpp @@ -46,7 +46,8 @@ namespace { constexpr int kSgs = 8; constexpr size_t kWg = kSgs * kSub; -constexpr int kMaxPer = 16; /* head_dim / 16, up to head_dim 256 */ +constexpr int kMaxPer = 32; /* head_dim / 16, up to head_dim 512; instantiated at 16 (head_dim <= 256, + the form every model up to 256 runs, unchanged) and at 32 (Gemma 4's 512) */ constexpr int64_t kScratch = (int64_t)64 << 20; /* pass 1's partial states */ constexpr int64_t kMaxParts = 32; /* a row's visible keys split this many ways at most */ constexpr int64_t kMinPart = 64; /* and into partitions of at least this many keys: at a verify's @@ -164,12 +165,13 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + auto launch = [&]() -> int { int rc = submit([&] { syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * kv_heads * P) * kWg, kWg}, [=](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( + 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(); @@ -214,13 +216,13 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { 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; - float qv[kMaxPer], acc[kMaxPer]; - for (int e = 0; e < kMaxPer; ++e) { + float qv[MP], acc[MP]; + for (int e = 0; e < MP; ++e) { 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; + float* qs = qloc + (size_t)s * kSub * MP; for (int e = 0; e < per; ++e) qs[lane * per + e] = qv[e]; sycl::group_barrier(sg); float m = -INFINITY, l = 0.0f; @@ -273,6 +275,13 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { const sycl::vec v1 = *(const sycl::vec*)(vr + 8); for (int e = 0; e < 8; ++e) acc[e] += wj * (rad_bf16_to_f32(v0[e]) * vds); for (int e = 0; e < 8; ++e) acc[8 + e] += wj * (rad_bf16_to_f32(v1[e]) * vds); + } else if (MP == 32 && vec_kv && per == 32) { + /* head_dim 512 (Gemma 4's global layers): four 16-byte loads a lane */ + const uint16_t* vr = (const uint16_t*)cb + kbj + head_dim + lane * 32; + for (int u = 0; u < 4; ++u) { + const sycl::vec vv = *(const sycl::vec*)(vr + u * 8); + for (int e = 0; e < 8; ++e) acc[u * 8 + 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); @@ -321,8 +330,8 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { if (lp > 0.0f) M = sycl::fmax(M, ps[p * state]); } const int64_t ooff = tok * o_rs + hq * o_hs; - float L = 0.0f, out[kMaxPer]; - for (int e = 0; e < kMaxPer; ++e) out[e] = 0.0f; + float L = 0.0f, out[MP]; + for (int e = 0; e < MP; ++e) out[e] = 0.0f; if (M != -INFINITY) for (int64_t p = 0; p < Pn; ++p) { const float* q0 = ps + p * state; @@ -336,6 +345,8 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { rad_store_f32(ob, odt, ooff + (lane * per + e) * o_ds, out[e] * inv); }); }); + }; + return per > 16 ? launch.template operator()<32>() : launch.template operator()<16>(); } } // namespace xe2