Archived
libxe2: gdn_chunk_scan with typed loads for the engine's bf16/f32 operands -- 4.8 -> 2.7 s a 1882-token prefill
The chain-parallel scan staged each chunk's k, q, v, A, g and beta through the generic accessors (a dtype switch an element); bf16 q/k/v/A/o with f32 g, beta and states now take typed loads and stores. Same arithmetic: fixture 90/90 at rel_l2 0. 27B prefill 131 -> 153 tok/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
1 parent
006452c279
commit
d07494d1f2
1 file changed
+26
-9
+26
-9
@@ -577,6 +577,15 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
|
||||
});
|
||||
rc0 < 0)
|
||||
return rc0;
|
||||
/* bf16 q, k, v, A and o with f32 g, beta and states (the engine's operands) take typed
|
||||
* loads and stores: the generic accessors' dtype switch an element made this kernel ~3x
|
||||
* its probe. The same arithmetic either way. */
|
||||
const bool typed = qs.dt == RAD_BF16 && ks.dt == RAD_BF16 && vs.dt == RAD_BF16 && As.dt == RAD_BF16 &&
|
||||
os.dt == RAD_BF16 && gs.dt == RAD_F32 && bs.dt == RAD_F32 &&
|
||||
(!has_h0 || h0s.dt == RAD_F32) && (!has_ht || hts.dt == RAD_F32) &&
|
||||
!std::getenv("XE2_GDN_GENERIC");
|
||||
auto launch = [&](auto ty) {
|
||||
constexpr bool TY = decltype(ty)::value;
|
||||
return submit([&] {
|
||||
qu->submit([&](sycl::handler& hd) {
|
||||
sycl::local_accessor<float, 1> lm(sycl::range<1>((size_t)(2 * CK * KP + 3 * CK * CB + KK * CB + CK * CK + 4 * CK)), hd);
|
||||
@@ -587,6 +596,10 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
|
||||
const int64_t e0 = blk * CB;
|
||||
const int t = (int)it.get_local_id(0);
|
||||
auto gw = it.get_group();
|
||||
auto bfl = [](const DT& d, int64_t o) { return rad_bf16_to_f32(((const uint16_t*)d.p)[o]); };
|
||||
auto f3l = [](const DT& d, int64_t o) { return ((const float*)d.p)[o]; };
|
||||
auto LB = [&](const DT& d, int64_t o) -> float { if constexpr (TY) return bfl(d, o); else return d.ld(o); };
|
||||
auto LF = [&](const DT& d, int64_t o) -> float { if constexpr (TY) return f3l(d, o); else return d.ld(o); };
|
||||
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
|
||||
const int64_t sn = has_si ? sis.li(n * sw) : n;
|
||||
if (sn < 0) return; /* uniform */
|
||||
@@ -603,7 +616,7 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
|
||||
float* gsj = ge + CK; /* e^{g_last - g_j} */
|
||||
for (int idx = t; idx < KK * CB; idx += WG) {
|
||||
const int d = idx / CB, e = idx % CB;
|
||||
St[idx] = has_h0 ? h0s.ld(at4(h0v, sn, h, e0 + e, d)) : 0.0f;
|
||||
St[idx] = has_h0 ? LF(h0s, at4(h0v, sn, h, e0 + e, d)) : 0.0f;
|
||||
}
|
||||
for (int64_t c0 = 0; c0 < slen; c0 += CK) {
|
||||
const int64_t L = (c0 + CK < slen) ? CK : slen - c0;
|
||||
@@ -611,17 +624,17 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
|
||||
for (int idx = t; idx < (int)L * KK; idx += WG) {
|
||||
const int j = idx / KK, d = idx % KK;
|
||||
const int64_t tj = bos + c0 + j;
|
||||
kc[j * KP + d] = ks.ld(at3(kv, tj, hk, d));
|
||||
qc[j * KP + d] = qs.ld(at3(qv, tj, hk, d));
|
||||
kc[j * KP + d] = LB(ks, at3(kv, tj, hk, d));
|
||||
qc[j * KP + d] = LB(qs, at3(qv, tj, hk, d));
|
||||
}
|
||||
for (int idx = t; idx < (int)L * CB; idx += WG) {
|
||||
const int j = idx / CB, e = idx % CB;
|
||||
vc[idx] = vs.ld(at3(vv, bos + c0 + j, h, e0 + e));
|
||||
vc[idx] = LB(vs, at3(vv, bos + c0 + j, h, e0 + e));
|
||||
}
|
||||
if (t < L) {
|
||||
const int64_t tj = bos + c0 + t;
|
||||
gc[t] = gs.ld(at2(gv, tj, h));
|
||||
bc[t] = bs.ld(at2(bv, tj, h));
|
||||
gc[t] = LF(gs, at2(gv, tj, h));
|
||||
bc[t] = LF(bs, at2(bv, tj, h));
|
||||
}
|
||||
sycl::group_barrier(gw);
|
||||
const float glast = gc[L - 1];
|
||||
@@ -640,7 +653,7 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
|
||||
/* the chunk's rows of A, widened into the R buffer (R is formed after V') */
|
||||
for (int idx = t; idx < (int)(L * L); idx += WG) {
|
||||
const int i = idx / (int)L, j = idx % (int)L;
|
||||
R[i * CK + j] = j <= i ? As.ld(at3(Av, bos + c0 + i, h, j)) : 0.0f;
|
||||
R[i * CK + j] = j <= i ? LB(As, at3(Av, bos + c0 + i, h, j)) : 0.0f;
|
||||
}
|
||||
sycl::group_barrier(gw);
|
||||
/* V'[i][e] = sum_{j <= i} A[i][j] W[j][e] */
|
||||
@@ -667,7 +680,8 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
|
||||
for (int d = 0; d < KK; ++d) inr += qc[i * KP + d] * St[d * CB + e];
|
||||
float acc = (scale * ge[i]) * inr;
|
||||
for (int j = 0; j <= i; ++j) acc += R[i * CK + j] * Vp[j * CB + e];
|
||||
os.st(at3(ov, bos + c0 + i, h, e0 + e), acc);
|
||||
if constexpr (TY) ((uint16_t*)os.p)[at3(ov, bos + c0 + i, h, e0 + e)] = rad_f32_to_bf16(acc);
|
||||
else os.st(at3(ov, bos + c0 + i, h, e0 + e), acc);
|
||||
}
|
||||
sycl::group_barrier(gw);
|
||||
/* k[j][d] *= e^{g_last - g_j}; S[d][e] = e^{g_last} S[d][e] + sum_j V'[j][e] k[j][d] */
|
||||
@@ -688,11 +702,14 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
|
||||
if (has_ht)
|
||||
for (int idx = t; idx < KK * CB; idx += WG) {
|
||||
const int d = idx / CB, e = idx % CB;
|
||||
hts.st(at4(htv, sn, h, e0 + e, d), St[idx]);
|
||||
if constexpr (TY) ((float*)hts.p)[at4(htv, sn, h, e0 + e, d)] = St[idx];
|
||||
else hts.st(at4(htv, sn, h, e0 + e, d), St[idx]);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
return typed ? launch(std::true_type{}) : launch(std::false_type{});
|
||||
}
|
||||
if (V % kSub == 0 && V <= 256 && chunk <= V && K * V <= 128 * 128) {
|
||||
/* A work-group a (sequence, v head), a work-item a value column e (and, for the Q K^T
|
||||
|
||||
Reference in new issue
Block a user