libxe2: the DFlash2 path fast -- exact draft head, DPAS verify GEMV, parallel rerank

- gemm_nt_q over the zero-point grids at M <= 16 (the drafter's 2-bit vocabulary head, N=248320):
  a sub-group a column, the group sums exact integers in f32, libref's group terms added in group
  order through lane broadcasts -- bit-identical to the exact kernel on the live head (checked in
  the engine: 0 differing bytes of 3.5 MB, every round). 252 ms -> well under 1 ms a call.
- m2xfp4 at 2 <= M <= 16 (the verify width) on XMX: a lane a column decodes its codes a 64-byte line
  at a time into its sub-group's VNNI slice of local memory (subnormal-bit fp16 pairs times
  (1 + k/4) * 2^(e - e_max + 14)), eight K16 DPAS a step against the permuted fp16 activation,
  K split for narrow N, partials added in order. On the vector ALUs M = 8 cost five times M = 1;
  gate_up at M = 8 1159 -> ~490 us. rad-kbench on the 27B: 50/50, rel_l2 ~8.5e-4 (tol 4e-2).
- logit_rerank: a work-group a row, a sub-group a candidate along K, libref's double sum as a float
  pair (two-product, two-sum, a fixed lane tree), libref's selection; 136 ms -> negligible a call.
- the gated delta net's gate, beta and decay (libref's double exp / log1p) once a (token, head)
  in a prepass instead of once a column block; still bit-identical (10/10 at rel_l2 0).
- profile_decode.sh: KEEP=DIR keeps the per-geometry tables.

27B on the B65, greedy: 12.1 tok/s plain, 13.2 tok/s with the drafter (device time a token
217 -> 76 ms under speculation). The drafter's text now departs from plain decoding at a
near-tie (byte 246 of 256 tokens): the verify forward runs the M = 8 DPAS path and plain decoding
the M = 1 GEMV, whose roundings differ (~1e-3); with the exact kernels the two were identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-24 04:37:16 +02:00
1 parent 07cdc546a3
commit 55a350f0c6
7 files changed
+399 -7

No files matched your search

+156
View File
@@ -126,6 +126,157 @@ int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* code
});
}
/* The verify band (2 <= M <= 16) on XMX. A lane per output column: it reads its column's codes a
* 64-byte line (four 32-groups) at a time and writes the decoded fp16 pairs -- (u, u + 4) of each
* word, the subnormal-bits form times (1 + k/4) * 2^(e - e_max + 14) by one half2 multiply,
* exactly e2m1 * (1 + k/4) * 2^(e - e_max) -- into its sub-group's VNNI slice of local memory,
* consecutive words across the lanes; then the sub-group runs eight K16 DPAS against the fp16
* activation fp16(a * a_scale / R) laid out in the same permuted order. The vector ALUs keep the
* decode, DPAS takes the rows: on the ALUs alone M = 8 costs five times M = 1 (m2bench in scratch).
* K is split when the columns alone give too few work-groups; the partials are added in split
* order, then y = acc * R_m * 2^(e_max - 127). Products in f32 (DPAS), the activation rounded once
* to fp16: held to the gemm tolerance and the KL bound like the rest. */
namespace jm = sycl::ext::oneapi::experimental::matrix;
constexpr int kKs = 4; /* 32-groups a step: one 64-byte line of a column's codes */
template <int MT>
int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* emax,
const uint8_t* codes0, const uint8_t* meta0, const uint8_t* expe0, int64_t brs,
float* pbuf, int64_t N, int64_t K, int64_t e1, int64_t e2, int64_t mpad, int splits) {
const int64_t ntile = (N + 127) / 128;
const size_t groups = (size_t)(ntile * splits);
const int64_t kg = K / 32;
const int64_t per = ((kg + splits - 1) / splits + kKs - 1) / kKs * kKs;
const int64_t ldp = ntile * 128;
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<uint32_t, 1> bt(sycl::range<1>((size_t)8 * kKs * 16 * 16), h);
h.parallel_for(sycl::nd_range<1>{groups * 128, 128}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
using H2 = sycl::vec<half, 2>;
const sycl::sub_group sg = it.get_sub_group();
const int s = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
const int64_t tile = (int64_t)it.get_group(0) % ntile, sp = (int64_t)it.get_group(0) / ntile;
const int64_t g0s = sp * per, g1s = sycl::min(kg, g0s + per);
const int64_t n0 = tile * 128 + s * 16;
if (n0 >= N) return; /* uniform */
const int64_t n = sycl::min(n0 + lane, N - 1);
const int64_t part = n0 < e1 ? 0 : (n0 < e2 ? 1 : 2);
const half* abase = a16 + part * K;
const uint8_t* crow = codes0 + n * (K / 2);
const int emx = emax[n];
uint32_t* mine = &bt[(size_t)s * kKs * 16 * 16];
jm::joint_matrix<sycl::sub_group, half, jm::use::a, 8, 16, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, half, jm::use::b, 16, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc[MT];
for (int i = 0; i < MT; ++i) jm::joint_matrix_fill(sg, mc[i], 0.0f);
for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) {
const sycl::vec<uint32_t, 16> line = *(const sycl::vec<uint32_t, 16>*)(crow + g0 * 16);
const uint32_t e4 = *(const uint32_t*)(expe0 + n * brs + g0);
const uint32_t m4 = *(const uint32_t*)(meta0 + n * kg + g0);
for (int gg = 0; gg < kKs; ++gg) {
const int d = (int)((e4 >> (8 * gg)) & 0xFF) - emx;
const float sc = d < -30 ? 0.0f : sycl::ldexp(16384.0f, d);
const uint32_t mt = (m4 >> (8 * gg)) & 0xFF;
for (int j = 0; j < 4; ++j) {
const uint32_t word = line[gg * 4 + j];
const half kh = (half)((1.0f + 0.25f * (float)((mt >> (2 * j)) & 3)) * sc);
const H2 k2(kh, kh);
for (int u = 0; u < 4; ++u) {
const uint32_t mb2 = u < 3 ? (word << (9 - 4 * u)) & 0x0E000E00u : (word >> 3) & 0x0E000E00u;
const uint32_t sb2 = (word << (12 - 4 * u)) & 0x80008000u;
mine[(gg * 16 + j * 4 + u) * 16 + lane] = sycl::bit_cast<uint32_t>(sycl::bit_cast<H2>(mb2 | sb2) * k2);
}
}
}
sycl::group_barrier(sg);
for (int kk = 0; kk < kKs * 32; kk += 16) {
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>((half*)mine + (size_t)(kk / 2) * 32);
jm::joint_matrix_load(sg, mb, lb, 32);
for (int i = 0; i < MT; ++i) {
auto ga = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(abase + (int64_t)i * 8 * lda + g0 * 32 + kk);
jm::joint_matrix_load(sg, ma, ga, (size_t)lda);
jm::joint_matrix_mad(sg, mc[i], ma, mb, mc[i]);
}
}
sycl::group_barrier(sg);
}
for (int i = 0; i < MT; ++i) {
auto gp = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(pbuf + (sp * mpad + (int64_t)i * 8) * ldp + n0);
jm::joint_matrix_store(sg, mc[i], gp, (size_t)ldp, jm::layout::row_major);
}
});
});
});
}
int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8_t* codes0,
const uint8_t* meta0, const uint8_t* expe0, int64_t brs, const RadTensor* Y, int64_t M,
int64_t N, int64_t K, int64_t acols, int64_t scols, int64_t ars, int64_t srs, int64_t yrs,
int64_t ycs, int64_t e1, int64_t e2) {
const int mt = (int)((M + 7) / 8);
const int64_t mpad = (int64_t)mt * 8, kg = K / 32, kb = kg;
const int64_t ntile = (N + 127) / 128, ldp = ntile * 128;
constexpr int64_t kFill = 160;
const int splits = (int)std::min<int64_t>(std::max<int64_t>(1, (kFill + ntile - 1) / ntile), std::max<int64_t>(1, kg / kKs));
const int64_t abytes = (mpad * acols * 2 + 255) / 256 * 256, rbytes = (mpad * 4 + 255) / 256 * 256;
float* ws = workspace(q, (size_t)((abytes + rbytes + N * 4) / 4 + 64));
float* pbuf = workspace(q, (size_t)(splits * mpad * ldp), 2);
if (!ws || !pbuf) return RAD_E_DEVICE;
half* a16 = (half*)ws;
float* rr = (float*)((char*)ws + abytes);
int32_t* emax = (int32_t*)((char*)ws + abytes + rbytes);
const uint8_t* ab = (const uint8_t*)A->data;
const char* sab = (const char*)AS->data;
const uint32_t sadt = AS->dtype;
int rc = submit([&] {
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) {
const int64_t p = c & 7, src = (c & ~(int64_t)7) + (p >> 1) + 4 * (p & 1);
a16[m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + src]) * rad_load_f32(sab, sadt, m * srs + src / 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;
int64_t g = 0;
if ((kb & 15) == 0 && ((uintptr_t)er & 15) == 0)
for (; g < kb; g += 16) {
const sycl::vec<uint8_t, 16> ev = *(const sycl::vec<uint8_t, 16>*)(er + g);
for (int u = 0; u < 16; ++u) mx = sycl::max(mx, (int32_t)ev[u]);
}
for (; g < kb; ++g) mx = sycl::max(mx, (int32_t)er[g]);
emax[i[0]] = mx;
});
});
if (rc < 0) return rc;
rc = mt == 1 ? launch_dpas<1>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits)
: launch_dpas<2>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits);
if (rc < 0) return rc;
char* yb = (char*)Y->data;
const uint32_t ydt = Y->dtype;
return submit([&] {
q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> i) {
const int64_t m = (int64_t)i[0] / N, c = (int64_t)i[0] % N;
float acc = 0.0f;
for (int sp = 0; sp < splits; ++sp) acc += pbuf[(sp * mpad + m) * ldp + c];
rad_store_f32(yb, ydt, m * yrs + c * ycs, acc * rr[m] * sycl::ldexp(1.0f, emax[c] - 127));
});
});
}
} // namespace
int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
@@ -161,6 +312,11 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
const uint8_t* codes0 = (const uint8_t*)B->data;
const uint8_t* meta0 = codes0 + N * (K / 2);
const int mm = (int)M;
/* the verify band on XMX, where its line loads fit: K a multiple of 128, aligned rows */
if (mm >= 2 && K % 128 == 0 && brs % 4 == 0 && rad_ptr_aligned(BS->data, 4) &&
((uintptr_t)meta0 & 3) == 0 && (K / 2) % 64 == 0 && rad_ptr_aligned(B->data, 64))
return m2_dpas(*q, A, AS, codes0, meta0, (const uint8_t*)BS->data, brs, Y, M, N, K, acols, scols,
ars, srs, yrs, ycs, e1, e2);
/* The activation once as fp16 in the weight pairs' order, with its row factor R. */
const int64_t abytes = (M * acols * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)(abytes / 4 + M + 64));