diff --git a/libxe2/xe2_attn_flash_esimd.cpp b/libxe2/xe2_attn_flash_esimd.cpp index 1e3280b2..4fdab71f 100644 --- a/libxe2/xe2_attn_flash_esimd.cpp +++ b/libxe2/xe2_attn_flash_esimd.cpp @@ -143,27 +143,28 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const simd base = mn; base.merge(simd(0.0f), mn == -INFINITY); /* a row with nothing visible yet */ const simd corr = exp(m - base); - simd bb; -#pragma unroll - for (int r = 0; r < 8; ++r) bb.template select<16, 1>(r * 16) = (float)base[r]; - simd 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 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(simd(P[p].template select<16, 1>(r * 16)), std::plus<>()); - } + for (int r = 0; r < 8; ++r) { + simd 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(x, std::plus<>()); + } l = l * corr + rs; - simd 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 pa = bf16_of(P[p]); + const simd pa = bf16_of(S[p]); const uint16_t* pg = cb + blk[p] * sb + kh * sh; #pragma unroll for (int c = 0; c < KS; ++c) {