Archived
libxe2: fast decode kernels for Qwen3.8-27B-PARO-M2XFP4 -- 0.1 -> 12.1 tok/s on the B65
- m2xfp4 GEMV (M <= 16): e2m1 decoded to fp16 by bits alone ((mag << 9) | sign << 15 is exactly e2m1 * 2^-14; the card keeps fp16 subnormals), K permuted (u, u + 4) inside each 8 so a code pair is one shift and mask, the activation as fp16(a * a_scale / R * 2^14) in that order, half2 subgroup and group sums, f32 across groups; four columns a sub-group at decode. The scalar form was ALU-bound (~10 ops a weight, 80 ms a token); this one runs at 415-525 GB/s on the 27B's shapes in a standalone probe. rad-kbench against libref on the container: rel_l2 ~5e-5 to ~3e-3, tolerance 4e-2. - m2xfp4 prefill (M >= 17): the weight decoded to bf16 (exact) and the activation to bf16(e4m3 * scale), then the bf16 XMX GEMM per part. - givens_dense_quant: a sub-group an output, the rotation row read coalesced, libref's double sum as a float pair (two-product and two-sum, ~48 bits); the double form stays for XE2_EXACT=1. 146 -> 5.4 ms a token. - the joined fp8 head (logits_gemm, M <= 16): E4M3 to fp16 by bits ((b & 0x7F) << 7 is E4M3 * 2^-8), bytes (0, 2), (1, 3) as half pairs; an i8 head or a narrow scale block element by element. 43 -> 3.9 ms. - gated delta net, still bit-identical to libref: the decode step split over 16-column blocks (384 work-groups, not 48) with the gated norm in a second kernel and the anchor replay its own launch (block 0 rewrites the factors other blocks read); the chunked scan and the kkt solve work-group parallel; persistent staging (workspace slots) instead of a malloc and a wait a call. - rmsnorm_quant_fp8: the row in local memory, loads issued before stores, the ordered sum fed by sub-group broadcasts -- still libref's chain (a probe puts that chain's floor at ~32 us for 5120). - round_dt converts the float dtypes directly. rad-kbench: 1410/1410 on the fixture; the 27B container's M <= 32 bands 77/77. Decode (greedy, no speculation) 12.1 tok/s, coherent text; with the DFlash2 drafter 5.3 tok/s and the same text. 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
4921641578
commit
07cdc546a3
9 files changed
+1030
-35
No files matched your search
@@ -174,6 +174,138 @@ int logits_gemm_head(const RadArgs* a, RadStream s) {
|
||||
return gemm_plain(a, s, "n_vocab", "n_embd", "logits_gemm", false, true);
|
||||
}
|
||||
|
||||
/* The joined fp8 head at M <= 16 (decode, verify): a sub-group a vocab column, each lane 16 E4M3
|
||||
* codes a step (one 16-byte load) inside one scale block, summed against the f32 activation and
|
||||
* scaled once: libref's products code * scale * a regrouped, held to the gemm tolerance and the
|
||||
* KL bound like the other fast GEMVs. The activation is widened to f32 once into the workspace. */
|
||||
inline float e4m3f(uint8_t b) {
|
||||
const uint32_t sgn = (uint32_t)(b & 0x80) << 24;
|
||||
const uint32_t e = (b >> 3) & 0xF, m = b & 7;
|
||||
if ((b & 0x7F) == 0x7F) return sycl::bit_cast<float>(sgn | 0x7FC00000u);
|
||||
if (e == 0) {
|
||||
const float v = (float)m * (1.0f / 512.0f);
|
||||
return sgn ? -v : v;
|
||||
}
|
||||
return sycl::bit_cast<float>(sgn | ((e + 120) << 23) | (m << 20));
|
||||
}
|
||||
|
||||
/* E4M3 codes as fp16 by bits: ((b & 0x7F) << 7) | ((b & 0x80) << 8) is exactly E4M3(b) * 2^-8
|
||||
* (its subnormals land on fp16 subnormals), so bytes (0, 2) and (1, 3) of a word are each a half
|
||||
* pair after two masks and shifts. The activation is laid out in that order, as
|
||||
* fp16(x / R * 2^8), R = max|x| / 2 of the row (|a16| <= 512); a lane's sixteen products sum in
|
||||
* half2, scaled once by the block's scale in f32. An i8 head, or a scale block narrower than 16
|
||||
* columns, takes the f32 form. */
|
||||
template <int MM>
|
||||
int head_gemv(sycl::queue& q, const sycl::half* a16, const float* rr, const char* bb, char* yb,
|
||||
uint32_t ydt, int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm) {
|
||||
constexpr size_t wg = 256;
|
||||
const size_t groups = (size_t)((N + 15) / 16);
|
||||
return submit([&] {
|
||||
syclex::nd_launch(q, sycl::nd_range<1>{groups * wg, wg},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
const int64_t n = (int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id();
|
||||
if (n >= N) return;
|
||||
const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale");
|
||||
if (!cp.p || !sp.p) return; /* the layout hook guarantees a joined head */
|
||||
const int lane = (int)sg.get_local_linear_id();
|
||||
if (cp.dt != RAD_F8E4M3 || !(sp.bc == 0 || sp.bc % 16 == 0)) {
|
||||
/* an i8 head or a narrow scale block: element by element in f32 (a16 carries
|
||||
* 2^8 / R, its permutation undone per element) */
|
||||
float accg[MM];
|
||||
for (int r = 0; r < MM; ++r) accg[r] = 0.0f;
|
||||
const char* crow = cp.p + n * cp.row_bytes;
|
||||
for (int64_t k = lane; k < K; k += kSub) {
|
||||
const float w = rad_load_f32(crow, cp.dt, k) * sp.cover(n, k);
|
||||
const int64_t pos = (k & ~(int64_t)3) + ((k & 3) == 1 ? 2 : (k & 3) == 2 ? 1 : (k & 3));
|
||||
for (int r = 0; r < MM; ++r) {
|
||||
if (r >= mm) break;
|
||||
accg[r] += (float)a16[r * K + pos] * w;
|
||||
}
|
||||
}
|
||||
for (int r = 0; r < MM; ++r) {
|
||||
if (r >= mm) break;
|
||||
const float v = sycl::reduce_over_group(sg, accg[r], sycl::plus<float>());
|
||||
if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v * rr[r] * (1.0f / 256.0f));
|
||||
}
|
||||
return;
|
||||
}
|
||||
const uint8_t* crow = (const uint8_t*)cp.p + n * cp.row_bytes;
|
||||
using H2 = sycl::vec<sycl::half, 2>;
|
||||
float acc[MM];
|
||||
for (int r = 0; r < MM; ++r) acc[r] = 0.0f;
|
||||
for (int64_t k = (int64_t)lane * 16; k < K; k += 16 * kSub) {
|
||||
const sycl::vec<uint32_t, 4> c = *(const sycl::vec<uint32_t, 4>*)(crow + k);
|
||||
H2 w2[8];
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const uint32_t x = c[j] & 0x00FF00FFu, y = (c[j] >> 8) & 0x00FF00FFu;
|
||||
w2[2 * j] = sycl::bit_cast<H2>(((x & 0x007F007Fu) << 7) | ((x & 0x00800080u) << 8));
|
||||
w2[2 * j + 1] = sycl::bit_cast<H2>(((y & 0x007F007Fu) << 7) | ((y & 0x00800080u) << 8));
|
||||
}
|
||||
const float sc = sp.cover(n, k);
|
||||
for (int r = 0; r < MM; ++r) {
|
||||
if (r >= mm) break;
|
||||
const sycl::vec<uint32_t, 8> aw = *(const sycl::vec<uint32_t, 8>*)(a16 + r * K + k);
|
||||
H2 p2(0.0f, 0.0f);
|
||||
for (int u = 0; u < 8; ++u) p2 = sycl::fma(sycl::bit_cast<H2>(aw[u]), w2[u], p2);
|
||||
acc[r] += ((float)p2[0] + (float)p2[1]) * sc;
|
||||
}
|
||||
}
|
||||
for (int r = 0; r < MM; ++r) {
|
||||
if (r >= mm) break;
|
||||
const float v = sycl::reduce_over_group(sg, acc[r], sycl::plus<float>());
|
||||
if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v * rr[r]);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
|
||||
if (exact_only()) return logits_gemm_head(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, "n_embd", 0), N = rad_args_geti_or(a, "n_vocab", 0);
|
||||
if (K <= 0 || N <= 0) return RAD_E_SHAPE;
|
||||
const int64_t M = rad_tensor_numel(Y) / N;
|
||||
if (M < 1 || M > 16 || K % 128 || !is_float(A->dtype) || !rad_ptr_aligned(B->data, 16))
|
||||
return logits_gemm_head(a, st);
|
||||
const int64_t ars = row_stride(A, K), acs = A->rank >= 2 ? A->stride[A->rank - 1] : 1;
|
||||
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
const int64_t abytes = (M * K * 2 + 255) / 256 * 256;
|
||||
float* ws = workspace(*q, (size_t)(abytes / 4 + M + 64));
|
||||
if (!ws) return RAD_E_DEVICE;
|
||||
sycl::half* a16 = (sycl::half*)ws;
|
||||
float* rr = (float*)((char*)ws + abytes);
|
||||
const char* ab = (const char*)A->data;
|
||||
const uint32_t adt = A->dtype;
|
||||
int rc = submit([&] {
|
||||
q->parallel_for(sycl::nd_range<1>{(size_t)M * 256, 256}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t m = (int64_t)it.get_group(0);
|
||||
const int64_t t = (int64_t)it.get_local_id(0);
|
||||
float mx = 0.0f;
|
||||
for (int64_t k = t; k < K; k += 256) mx = sycl::fmax(mx, sycl::fabs(rad_load_f32(ab, adt, m * ars + k * acs)));
|
||||
mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum<float>());
|
||||
const float R = mx > 0.0f ? mx * 0.5f : 1.0f;
|
||||
if (t == 0) rr[m] = R;
|
||||
const float f = 256.0f / R;
|
||||
/* position p of each 4 holds element (p % 2) * 2 + (p / 2): bytes (0, 2), (1, 3) */
|
||||
for (int64_t k = t; k < K; k += 256) {
|
||||
const int64_t p = k & 3, src = (k & ~(int64_t)3) + (p & 1) * 2 + (p >> 1);
|
||||
a16[m * K + k] = (sycl::half)(rad_load_f32(ab, adt, m * ars + src * acs) * f);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (rc < 0) return rc;
|
||||
const char* bb = (const char*)B->data;
|
||||
char* yb = (char*)Y->data;
|
||||
const int mm = (int)M;
|
||||
if (mm == 1) return head_gemv<1>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
|
||||
if (mm <= 8) return head_gemv<8>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
|
||||
return head_gemv<16>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
|
||||
}
|
||||
|
||||
/* libref's ref_layout_head: operand 1 (the head) of codes and one scale, joined. */
|
||||
int head_layout(const RadParam*, int, int operand, const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* planes, int n, RadLayout* out) {
|
||||
|
||||
Reference in new issue
Block a user