Archived
libxe2: prefill flash attention's softmax and rescale a row at a time against scalar factors
The 128-wide broadcast copies of the row base and correction overflowed the register file; per-row selects compute the same values (bit-identical output) and the 2048-token call drops from 7.8 to 6.6 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
175f0bbd33
commit
d63b985161
1 file changed
+13
-12
@@ -143,27 +143,28 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
||||
simd<float, 8> base = mn;
|
||||
base.merge(simd<float, 8>(0.0f), mn == -INFINITY); /* a row with nothing visible yet */
|
||||
const simd<float, 8> corr = exp(m - base);
|
||||
simd<float, 128> bb;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r) bb.template select<16, 1>(r * 16) = (float)base[r];
|
||||
simd<float, 128> P[2];
|
||||
/* P = exp(S - base) in place over S, and the accumulator's rescale, a row at a time
|
||||
* against scalar factors: the same values without 128-wide broadcast copies (the
|
||||
* register file overflowed: 4 KiB of spill in the step loop) */
|
||||
simd<float, 8> rs = 0.0f;
|
||||
#pragma unroll
|
||||
for (int p = 0; p < 2; ++p) {
|
||||
P[p] = exp(S[p] - bb);
|
||||
for (int p = 0; p < 2; ++p)
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r) rs[r] += reduce<float>(simd<float, 16>(P[p].template select<16, 1>(r * 16)), std::plus<>());
|
||||
}
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
simd<float, 16> x = S[p].template select<16, 1>(r * 16) - (float)base[r];
|
||||
x = exp(x);
|
||||
S[p].template select<16, 1>(r * 16) = x;
|
||||
rs[r] += reduce<float>(x, std::plus<>());
|
||||
}
|
||||
l = l * corr + rs;
|
||||
simd<float, 128> cb8;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r) cb8.template select<16, 1>(r * 16) = (float)corr[r];
|
||||
for (int c = 0; c < KS; ++c)
|
||||
#pragma unroll
|
||||
for (int c = 0; c < KS; ++c) O[c] *= cb8;
|
||||
for (int r = 0; r < 8; ++r) O[c].template select<16, 1>(r * 16) *= (float)corr[r];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < 2; ++p) {
|
||||
if (blk[p] < 0) continue;
|
||||
const simd<uint16_t, 128> pa = bf16_of(P[p]);
|
||||
const simd<uint16_t, 128> pa = bf16_of(S[p]);
|
||||
const uint16_t* pg = cb + blk[p] * sb + kh * sh;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < KS; ++c) {
|
||||
|
||||
Reference in new issue
Block a user