Archived
At M <= 8 the bf16 GEMV re-read the activation for every column; it now stages 8 rows in local memory and runs bf16 DPAS against the weight, K split to fill the card: N=96 164 -> 20 us. The joined E4M3 head decodes its codes by bits into a packed fp16 tile and runs fp16 DPAS, a scale block's sum scaled per column in f32: 9.7 -> 3.5 ms at M = 8, and 3.9 -> 3.4 ms at M = 1, so decode takes it too. Held to the gemm tolerance (rel_l2 1.6e-3, as before). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
282 lines
15 KiB
C++
282 lines
15 KiB
C++
/* xe2_gemv.cpp -- Phase 4's first fast kernel: the skinny bf16 GEMM a decode step is made of
|
|
* (gemm_nt and logits_gemm at M <= 16), bandwidth-bound by construction.
|
|
*
|
|
* At decode (M = 1) one 16-lane sub-group takes two output columns: each lane reads 8 bf16 of each
|
|
* weight row at a time (16-byte loads, the sub-group 256 contiguous bytes a row), multiplies them
|
|
* against the same 8 columns of the activation, and keeps a partial sum a column; the sub-group then
|
|
* reduces and lane 0 stores. Measured on the B65 at a 1.8k context: one column 9.1 ms a step of
|
|
* gemm_nt, two 8.5 (~465 GB/s of the card's ~600), four 10.9 (fewer sub-groups, more registers).
|
|
* For 2 <= M <= 16, one column a sub-group with a partial sum a row. A weight byte is read once per step whatever M is, so the cost is the weight's
|
|
* bytes over the card's bandwidth.
|
|
*
|
|
* Not libref's order: each lane sums its own strided slice and the slices are reduced as a tree, so
|
|
* the result differs from the reference's ascending sum in the last bits. It is checked against
|
|
* libref at rad-kbench's gemm tolerance, the standard libr4d's split-K WMMA GEMMs are held to, and
|
|
* by the engine oracle at that tolerance. XE2_EXACT=1 sends every launch to the correctness-first
|
|
* kernel instead (xe2_gemm.cpp), for bisecting a difference.
|
|
*
|
|
* Takes: bf16 activation and weight, rows 16-byte aligned with K a multiple of 8 and a unit column
|
|
* stride; anything else falls back to the correctness-first kernel, which serves every layout.
|
|
*/
|
|
#include "xe2_common.h"
|
|
#include "xe2_ops.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
namespace xe2 {
|
|
|
|
bool exact_only() {
|
|
static const bool on = [] {
|
|
const char* e = std::getenv("XE2_EXACT");
|
|
return e && e[0] == '1';
|
|
}();
|
|
return on;
|
|
}
|
|
|
|
namespace {
|
|
|
|
constexpr int kMaxM = 16;
|
|
constexpr int kCols = 2; /* output columns a sub-group at decode: two weight rows in flight */
|
|
constexpr size_t kWg = 256; /* 16 sub-groups */
|
|
constexpr int64_t kFill = 8192; /* sub-groups that fill the B65 (it runs ~10k threads) */
|
|
|
|
int64_t row_stride(const RadTensor* t, int64_t cols) {
|
|
if (!t || t->rank < 2) return cols;
|
|
int64_t span = 1;
|
|
for (int i = (int)t->rank - 1; i >= 0; --i) {
|
|
span *= t->shape[i];
|
|
if (span == cols) return (i > 0) ? t->stride[i - 1] : cols;
|
|
if (span > cols) break;
|
|
}
|
|
return t->stride[t->rank - 2];
|
|
}
|
|
|
|
inline float bf(uint16_t h) { return rad_bf16_to_f32(h); }
|
|
|
|
/* Whether the fast kernel can read this launch's operands; otherwise the exact one runs. */
|
|
bool gemv_ok(const RadTensor* A, const RadTensor* B, const RadTensor* Y, int64_t M, int64_t K,
|
|
int64_t ars, int64_t brs) {
|
|
if (A->dtype != RAD_BF16 || B->dtype != RAD_BF16) return false;
|
|
if (Y->dtype != RAD_BF16 && Y->dtype != RAD_F32) return false;
|
|
if (M < 1 || M > kMaxM || K % 8) return false;
|
|
if ((A->rank >= 2 && A->stride[A->rank - 1] != 1) || (B->rank >= 2 && B->stride[B->rank - 1] != 1))
|
|
return false;
|
|
if (ars % 8 || brs % 8) return false;
|
|
return rad_ptr_aligned(A->data, 16) && rad_ptr_aligned(B->data, 16);
|
|
}
|
|
|
|
/* The kernel: NC output columns a sub-group, up to MM activation rows. */
|
|
template <int NC, int MM>
|
|
int launch(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* yb, uint32_t ydt, int64_t N,
|
|
int64_t K, int64_t ars, int64_t brs, int64_t yrs, int64_t ycs, int mm) {
|
|
const int64_t per_wg = 16 * NC;
|
|
const size_t groups = (size_t)((N + per_wg - 1) / per_wg);
|
|
return submit([&] {
|
|
syclex::nd_launch(q, sycl::nd_range<1>{groups * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int64_t n0 = ((int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id()) * NC;
|
|
if (n0 >= N) return; /* uniform across the sub-group */
|
|
const int nc = (int)sycl::min((int64_t)NC, N - n0);
|
|
const int lane = (int)sg.get_local_linear_id();
|
|
float acc[NC][MM];
|
|
for (int c = 0; c < NC; ++c)
|
|
for (int r = 0; r < MM; ++r) acc[c][r] = 0.0f;
|
|
for (int64_t k = (int64_t)lane * 8; k < K; k += 8 * kSub) {
|
|
float wf[NC][8];
|
|
for (int c = 0; c < NC; ++c) {
|
|
if (c >= nc) break;
|
|
const sycl::vec<uint16_t, 8> w = *(const sycl::vec<uint16_t, 8>*)(bb + (n0 + c) * brs + k);
|
|
for (int j = 0; j < 8; ++j) wf[c][j] = bf(w[j]);
|
|
}
|
|
for (int r = 0; r < MM; ++r) {
|
|
if (r >= mm) break;
|
|
const sycl::vec<uint16_t, 8> x = *(const sycl::vec<uint16_t, 8>*)(ab + r * ars + k);
|
|
float xf[8];
|
|
for (int j = 0; j < 8; ++j) xf[j] = bf(x[j]);
|
|
for (int c = 0; c < NC; ++c) {
|
|
if (c >= nc) break;
|
|
float s = 0.0f;
|
|
for (int j = 0; j < 8; ++j) s += xf[j] * wf[c][j];
|
|
acc[c][r] += s;
|
|
}
|
|
}
|
|
}
|
|
for (int c = 0; c < NC; ++c) {
|
|
if (c >= nc) break;
|
|
for (int r = 0; r < MM; ++r) {
|
|
if (r >= mm) break;
|
|
const float v = sycl::reduce_over_group(sg, acc[c][r], sycl::plus<float>());
|
|
if (lane == 0) rad_store_f32(yb, ydt, r * yrs + (n0 + c) * ycs, v);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/* Decode (M = 1) with K split S ways inside a work-group: sub-group g takes columns
|
|
* (group * 16/S + g / S) * NC + c and the K chunk g % S (whole 128-element steps), its lanes reduce
|
|
* their partials, and the chunk partials are added in chunk order through local memory -- the same
|
|
* order every launch. Without the split a decode GEMV is N / NC sub-groups, which for N = 2048 is a
|
|
* tenth of the card's threads; at a long K each of them then walks K alone. */
|
|
template <int NC>
|
|
int launch_split(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* yb, uint32_t ydt,
|
|
int64_t N, int64_t K, int64_t brs, int64_t ycs, int S) {
|
|
const int cg = 16 / S; /* column groups a work-group */
|
|
const int64_t per_wg = (int64_t)cg * NC;
|
|
const size_t groups = (size_t)((N + per_wg - 1) / per_wg);
|
|
const int64_t steps = (K + 8 * kSub - 1) / (8 * kSub);
|
|
const int64_t chunk = (steps + S - 1) / S * (8 * kSub); /* K elements a split */
|
|
return submit([&] {
|
|
q.submit([&](sycl::handler& h) {
|
|
sycl::local_accessor<float, 1> part(sycl::range<1>((size_t)16 * NC), h);
|
|
h.parallel_for(sycl::nd_range<1>{groups * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int g = (int)sg.get_group_linear_id();
|
|
const int sp = g % S;
|
|
const int64_t n0 = ((int64_t)it.get_group(0) * cg + g / S) * NC;
|
|
const int nc = (int)sycl::max((int64_t)0, sycl::min((int64_t)NC, N - n0));
|
|
const int lane = (int)sg.get_local_linear_id();
|
|
float acc[NC];
|
|
for (int c = 0; c < NC; ++c) acc[c] = 0.0f;
|
|
const int64_t klo = sp * chunk, khi = sycl::min(K, klo + chunk);
|
|
using V8 = sycl::vec<uint16_t, 8>;
|
|
for (int64_t k = klo + (int64_t)lane * 8; k < khi; k += 8 * kSub) {
|
|
const V8 x = *(const V8*)(ab + k);
|
|
float xf[8];
|
|
for (int j = 0; j < 8; ++j) xf[j] = bf(x[j]);
|
|
for (int c = 0; c < NC; ++c) {
|
|
if (c >= nc) break;
|
|
const V8 w = *(const V8*)(bb + (n0 + c) * brs + k);
|
|
float s = 0.0f;
|
|
for (int j = 0; j < 8; ++j) s += xf[j] * bf(w[j]);
|
|
acc[c] += s;
|
|
}
|
|
}
|
|
for (int c = 0; c < NC; ++c) {
|
|
const float v = sycl::reduce_over_group(sg, acc[c], sycl::plus<float>());
|
|
if (lane == 0) part[g * NC + c] = v;
|
|
}
|
|
sycl::group_barrier(it.get_group());
|
|
if (sp == 0 && lane < nc) {
|
|
float y = 0.0f;
|
|
for (int t = 0; t < S; ++t) y += part[(g + t) * NC + lane];
|
|
rad_store_f32(yb, ydt, (n0 + lane) * ycs, y);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
/* 2 <= M <= 8 (a speculative verify) on XMX: the activation staged in local memory as 8 rows
|
|
* (the missing ones zero), a sub-group 16 columns and a K split of the work-group's 16
|
|
* sub-groups, bf16 DPAS 8 x 16 x 16 against B read column-major from the weight; the splits'
|
|
* partials are added in split order. The SIMT form above re-reads the activation for every
|
|
* column, which at 8 rows costs more than the weight. */
|
|
int launch_dpas8(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* yb, uint32_t ydt, int64_t N,
|
|
int64_t K, int64_t ars, int64_t brs, int64_t yrs, int64_t ycs, int mm) {
|
|
namespace jm = sycl::ext::oneapi::experimental::matrix;
|
|
using bf16 = sycl::ext::oneapi::bfloat16;
|
|
const int64_t tiles = N / 16;
|
|
int S = 1; /* K splits: enough sub-groups to fill the card, whole 16-element steps each */
|
|
while (S < 16 && tiles * S < kFill) S *= 2;
|
|
const int ct = 16 / S;
|
|
const size_t groups = (size_t)((tiles + ct - 1) / ct);
|
|
const int64_t ksteps = K / 16, kper = (ksteps + S - 1) / S;
|
|
return submit([&] {
|
|
q.submit([&](sycl::handler& h) {
|
|
sycl::local_accessor<uint16_t, 1> As(sycl::range<1>((size_t)(8 * K)), h);
|
|
sycl::local_accessor<float, 1> Cs(sycl::range<1>((size_t)16 * 8 * 16), h);
|
|
h.parallel_for(sycl::nd_range<1>{groups * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int g = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
|
|
const int t = (int)it.get_local_id(0);
|
|
for (int64_t i = t; i < K; i += kWg) { /* 8 bf16 at a time */
|
|
const int64_t m = i / (K / 8), c = (i % (K / 8)) * 8;
|
|
sycl::vec<uint16_t, 8> v(0);
|
|
if (m < mm) v = *(const sycl::vec<uint16_t, 8>*)(ab + m * ars + c);
|
|
*(sycl::vec<uint16_t, 8>*)&As[m * K + c] = v;
|
|
}
|
|
sycl::group_barrier(it.get_group());
|
|
const int sp = g % S;
|
|
const int64_t tile = (int64_t)it.get_group(0) * ct + g / S;
|
|
const bool live = tile < tiles;
|
|
const int64_t n0 = (live ? tile : 0) * 16;
|
|
jm::joint_matrix<sycl::sub_group, bf16, jm::use::a, 8, 16, jm::layout::row_major> ma;
|
|
jm::joint_matrix<sycl::sub_group, bf16, jm::use::b, 16, 16, jm::layout::col_major> mb;
|
|
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc;
|
|
jm::joint_matrix_fill(sg, mc, 0.0f);
|
|
const int64_t klo = sp * kper * 16, khi = sycl::min(K, klo + kper * 16);
|
|
if (live)
|
|
for (int64_t k = klo; k < khi; k += 16) {
|
|
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
|
|
sycl::access::decorated::no>((bf16*)&As[k]);
|
|
jm::joint_matrix_load(sg, ma, la, (size_t)K);
|
|
auto gb = sycl::address_space_cast<sycl::access::address_space::global_space,
|
|
sycl::access::decorated::no>((const bf16*)bb + n0 * brs + k);
|
|
jm::joint_matrix_load(sg, mb, gb, (size_t)brs);
|
|
jm::joint_matrix_mad(sg, mc, ma, mb, mc);
|
|
}
|
|
auto lc = sycl::address_space_cast<sycl::access::address_space::local_space,
|
|
sycl::access::decorated::no>(&Cs[(size_t)g * 128]);
|
|
jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major);
|
|
sycl::group_barrier(it.get_group());
|
|
if (live && sp == 0)
|
|
for (int m = 0; m < mm; ++m) {
|
|
float y = 0.0f;
|
|
for (int u = 0; u < S; ++u) y += Cs[(size_t)(g + u) * 128 + m * 16 + lane];
|
|
rad_store_f32(yb, ydt, m * yrs + (n0 + lane) * ycs, y);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int (*exact)(const RadArgs*, RadStream)) {
|
|
if (exact_only()) return exact(a, st);
|
|
if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL;
|
|
const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[2];
|
|
const int64_t K = rad_args_geti_or(a, kkey, B->rank >= 2 ? B->shape[B->rank - 1] : 0);
|
|
const int64_t N = rad_args_geti_or(a, nkey, B->rank >= 2 ? B->shape[B->rank - 2] : 0);
|
|
if (K <= 0 || N <= 0) return RAD_E_SHAPE;
|
|
const int64_t M = rad_tensor_numel(Y) / N;
|
|
if (M <= 0 || rad_tensor_numel(A) < M * K || rad_tensor_numel(B) < N * K) return RAD_E_SHAPE;
|
|
const int64_t ars = row_stride(A, K), brs = row_stride(B, K);
|
|
if (!gemv_ok(A, B, Y, M, K, ars, brs)) return exact(a, st);
|
|
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
|
|
const uint16_t* ab = (const uint16_t*)A->data;
|
|
const uint16_t* bb = (const uint16_t*)B->data;
|
|
char* yb = (char*)Y->data;
|
|
const uint32_t ydt = Y->dtype;
|
|
const int mm = (int)M;
|
|
sycl::queue* q = queue_of(st);
|
|
if (!q) return RAD_E_DEVICE;
|
|
/* Decode (M = 1) takes the lean form with kCols columns a sub-group; M up to 16 one column a
|
|
* sub-group with a partial sum a row. Sizing the accumulators by the band keeps them in
|
|
* registers (four columns at sixteen rows spilled and ran slower than one). */
|
|
if (mm == 1) {
|
|
/* A long K on too few sub-groups to fill the card splits two ways. Measured on the B65
|
|
* (MiniCPM5-2B, cold weights): down_proj N=2048 K=6144 59.7 -> 52.2 us; 4, 8 or 16 ways no
|
|
* better; the K=2048 shapes slower split (18.1 -> 21.6 us at N=2048), so they keep one. */
|
|
const int64_t sgs = (N + kCols - 1) / kCols;
|
|
const int S = (K >= 4096 && sgs < kFill) ? 2 : 1;
|
|
if (S > 1) return launch_split<kCols>(*q, ab, bb, yb, ydt, N, K, brs, ycs, S);
|
|
return launch<kCols, 1>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, 1);
|
|
}
|
|
static const bool simt = std::getenv("XE2_GEMV_SIMT") != nullptr;
|
|
if (mm <= 8 && N % 16 == 0 && K % 16 == 0 && K <= 6144 && !simt)
|
|
return launch_dpas8(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm);
|
|
return launch<1, kMaxM>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int gemm_nt_gemv(const RadArgs* a, RadStream s) { return gemv(a, s, "N", "K", gemm_nt); }
|
|
int logits_gemm_gemv(const RadArgs* a, RadStream s) {
|
|
return gemv(a, s, "n_vocab", "n_embd", logits_gemm);
|
|
}
|
|
|
|
} // namespace xe2
|