Archived
libxe2: chain-parallel gdn_chunk_scan and an fp16 m2xfp4 prefill -- 27B prefill 68 -> 91 tok/s
- gdn_chunk_scan for head_k = 128, chunk 64: a work-group of 256 a (sequence, head, block of 32
value columns), the chunk's k, q (rows padded), the block's slices of v, S, W, V' and the sA rows
in local memory (~124 KB); every phase of a chunk is thousands of independent chains -- W[j][e],
V'[i][e], R[i][j], O[i][e], S[d][e] -- each one work-item's, in libref's order, A staged in local
memory, the decays e^{g_i - g_j} (libref's double exp) once a call. Still bit-identical (rel_l2 0,
the 27B's geometry, 1..32 rows and multi-chunk). 233 -> 96 ms a call at 1792 tokens.
- m2xfp4 at M >= 17 now fp16 on XMX: the weight times 2^(127 - e_max(n)), exact in fp16; the
activation fp16(a * a_scale / R_m); y = acc * R_m * 2^(e_max - 127) in a new scaled fp16 XMX
kernel (xmx_f16_scaled). fp16's 2^-11 step instead of bf16's 2^-8.
KL on the 27B against an XE2_EXACT=1 reference (every kernel exact, each matched to libref bit for
bit; 250 positions, full vocabulary): mean 0.0041 -> 0.0035, p99 0.027 -> 0.021, top-1 96.8% ->
95.6%, PPL ratio 0.998. The fast set's KL is dominated by the model's own fp8 activation quantiser
re-rounding small upstream differences.
27B prefill of 1792 tokens: 26.3 -> 19.7 s (68 -> 91 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
0376adeb92
commit
e0bfeb54f8
6 files changed
+264
-32
No files matched your search
+41
-27
@@ -396,15 +396,23 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
|
||||
rad_tensor_numel(A) >= M * acols && rad_tensor_numel(AS) >= M * scols &&
|
||||
rad_tensor_numel(BS) >= N * kb &&
|
||||
(!(pb1 > 0 || pb2 > 0) || ((e1 % 16) == 0 && (e2 % 16) == 0 && !(e1 == e2 && e2 < N)));
|
||||
if (!ok) return gemm_nt_q_m2(a, st);
|
||||
if (!ok || K % 16 || e1 % 64 || e2 % 64 || N % 64) return gemm_nt_q_m2(a, st);
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
/* The workspace: the bf16 activation [M, parts * K], then the bf16 weight [N, K]. */
|
||||
const int64_t abytes = (M * acols * 2 + 255) / 256 * 256;
|
||||
float* ws = workspace(*q, (size_t)((abytes + N * K * 2) / 4 + 64));
|
||||
/* fp16 operands: the activation fp16(a * a_scale / R_m), R_m = 224 * the row's largest scale
|
||||
* (|a16| <= 2), rows padded to a multiple of 8 with zeros; the weight
|
||||
* e2m1 * (1 + k/4) * 2^(e - e_max(n)), exact in fp16 (a group more than 2^14 below its row's
|
||||
* largest goes subnormal); y = acc * R_m * 2^(e_max(n) - 127). */
|
||||
const int64_t mpad = (M + 7) / 8 * 8;
|
||||
const int64_t abytes = (mpad * acols * 2 + 255) / 256 * 256;
|
||||
const int64_t wbytes = (N * K * 2 + 255) / 256 * 256;
|
||||
float* ws = workspace(*q, (size_t)((abytes + wbytes) / 4 + mpad + 2 * N + 64));
|
||||
if (!ws) return RAD_E_DEVICE;
|
||||
uint16_t* a16 = (uint16_t*)ws;
|
||||
uint16_t* w16 = (uint16_t*)((char*)ws + abytes);
|
||||
half* a16 = (half*)ws;
|
||||
half* w16 = (half*)((char*)ws + abytes);
|
||||
float* rr = (float*)((char*)ws + abytes + wbytes);
|
||||
float* cs = rr + mpad;
|
||||
int32_t* emax = (int32_t*)(cs + N);
|
||||
const uint8_t* ab = (const uint8_t*)A->data;
|
||||
const char* sab = (const char*)AS->data;
|
||||
const uint32_t sadt = AS->dtype;
|
||||
@@ -412,11 +420,25 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
|
||||
const uint8_t* meta0 = codes0 + N * (K / 2);
|
||||
const uint8_t* expe0 = (const uint8_t*)BS->data;
|
||||
int rc = submit([&] {
|
||||
const int64_t na = M * acols;
|
||||
q->parallel_for(sycl::range<1>((size_t)na), [=](sycl::id<1> i) {
|
||||
const int64_t m = (int64_t)i[0] / acols, c = (int64_t)i[0] % acols;
|
||||
const float v = e4m3(ab[m * ars + c]) * rad_load_f32(sab, sadt, m * srs + c / 128);
|
||||
a16[m * acols + c] = rad_f32_to_bf16(v);
|
||||
q->parallel_for(sycl::nd_range<1>{(size_t)mpad * 256, 256}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t m = (int64_t)it.get_group(0), t = (int64_t)it.get_local_id(0);
|
||||
float mx = 0.0f;
|
||||
if (m < M)
|
||||
for (int64_t c = t; c < scols; c += 256) mx = sycl::fmax(mx, rad_load_f32(sab, sadt, m * srs + c));
|
||||
mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum<float>());
|
||||
const float R = mx > 0.0f ? 224.0f * mx : 1.0f;
|
||||
if (t == 0) rr[m] = R;
|
||||
const float f = 1.0f / R;
|
||||
for (int64_t c = t; c < acols; c += 256)
|
||||
a16[m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + c]) * rad_load_f32(sab, sadt, m * srs + c / 128) * f)
|
||||
: (half)0.0f;
|
||||
});
|
||||
q->parallel_for(sycl::range<1>((size_t)N), [=](sycl::id<1> i) {
|
||||
const uint8_t* er = expe0 + (int64_t)i[0] * brs;
|
||||
int32_t mx = 0;
|
||||
for (int64_t g = 0; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]);
|
||||
emax[i[0]] = mx;
|
||||
cs[i[0]] = sycl::ldexp(1.0f, mx - 127);
|
||||
});
|
||||
const int64_t nw = N * (K / 2); /* a work-item a code byte: two weights */
|
||||
q->parallel_for(sycl::range<1>((size_t)nw), [=](sycl::id<1> i) {
|
||||
@@ -424,11 +446,12 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
|
||||
const uint8_t byte = codes0[n * (K / 2) + j];
|
||||
constexpr float lut[16] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f,
|
||||
-0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f };
|
||||
for (int h = 0; h < 2; ++h) {
|
||||
const int64_t k = 2 * j + h;
|
||||
const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + (k >> 5)] - 127);
|
||||
const int mx = emax[n];
|
||||
for (int hh = 0; hh < 2; ++hh) {
|
||||
const int64_t k = 2 * j + hh;
|
||||
const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + (k >> 5)] - mx);
|
||||
const float km = 1.0f + 0.25f * (float)((meta0[n * kb + (k >> 5)] >> (2 * ((k & 31) >> 3))) & 3);
|
||||
w16[n * K + k] = rad_f32_to_bf16(lut[(byte >> (4 * h)) & 0xF] * ev * km);
|
||||
w16[n * K + k] = (half)(lut[(byte >> (4 * hh)) & 0xF] * km * ev);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -438,18 +461,9 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
|
||||
const int64_t n0 = bound[p], n1 = bound[p + 1];
|
||||
if (n1 <= n0) continue;
|
||||
const int64_t pi = p == 0 ? 0 : (p == 1 ? 1 : (parts == 3 ? 2 : 1));
|
||||
RadTensor t[3] = {};
|
||||
t[0].data = a16 + pi * K; t[0].dtype = RAD_BF16; t[0].rank = 2;
|
||||
t[0].shape[0] = M; t[0].shape[1] = K; t[0].stride[0] = acols; t[0].stride[1] = 1;
|
||||
t[1].data = w16 + n0 * K; t[1].dtype = RAD_BF16; t[1].rank = 2;
|
||||
t[1].shape[0] = n1 - n0; t[1].shape[1] = K; t[1].stride[0] = K; t[1].stride[1] = 1;
|
||||
t[2].data = (char*)Y->data + (n0 * ycs) * ((rad_dtype_bits(Y->dtype) + 7) / 8);
|
||||
t[2].dtype = Y->dtype; t[2].rank = 2;
|
||||
t[2].shape[0] = M; t[2].shape[1] = n1 - n0; t[2].stride[0] = yrs; t[2].stride[1] = ycs;
|
||||
RadParam pr[2] = { RAD_INT("N", n1 - n0), RAD_INT("K", K) };
|
||||
RadArgs b = *a;
|
||||
b.t = t; b.n_t = 3; b.p = pr; b.n_p = 2;
|
||||
rc = gemm_nt_xmx(&b, st);
|
||||
rc = xmx_f16_scaled(*q, a16 + pi * K, acols, w16 + n0 * K, K,
|
||||
(char*)Y->data + (n0 * ycs) * ((rad_dtype_bits(Y->dtype) + 7) / 8), Y->dtype,
|
||||
yrs, ycs, M, mpad, n1 - n0, K, rr, cs + n0);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user