Compare commits

...
8 Commits
Author SHA1 Message Date
pascallinxweilerandClaude Opus 5.5 c943bc65e3 libxe2: rmsnorm_quant_fp8 with 16-byte loads for bf16 rows -- 112 -> 44 us a call
Half the norm's time was its per-element loads of x, the residual and the gain. bf16 rows (gain
bf16 or f32) now load and store 8 values at a time and a 128-group's codes go out 8 bytes a lane;
the arithmetic and the ordered sum are the general form's. Bit-identical to libref (kbench rel_l2
0; 408/408 live oracle issues). 128 calls a speculative round: 17 -> 6.5 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 12:01:34 +02:00
pascallinxweilerandClaude Opus 5.5 8abcee5d35 libxe2: 8-row bf16 GEMMs and the E4M3 logits head on XMX
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
2026-10-24 12:01:34 +02:00
pascallinxweilerandClaude Opus 5.5 ad3b17a70d libxe2: the speculative round's selection ops -- w2a8 head on int8 DPAS, dflash_select a sub-group a sequence, row_topk in two stages
The DFlash2 drafter's 2-bit vocabulary head (N=248320) forms each group's integer sum on int8 DPAS
(8 x 32 by 32 x 16), the u2 codes unpacked straight into the packed B tile; a lane owns a column
and adds libref's group terms in order: 24.0 -> 4.2 ms a round, bit-identical to the libref-checked
sub-group form on 63 live calls. dflash_select scores the 16 candidates in parallel lanes, each in
libref's order, lane 0 taking the first strict maximum: 11.6 -> 0.5 ms. row_topk takes each of 128
chunks' top R, then the top R of the winners: 3.2 -> 0.95 ms. Both exact against libref (fixture,
engine oracle).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 12:01:34 +02:00
pascallinxweilerandClaude Opus 5.5 f49aee84f0 libxe2: attention value rows 16 bytes at a time at head_dim 256 -- 2.72 -> 0.88 s a 27B prefill
The lane-per-key attention read bf16 value rows with 16-byte loads only at head_dim 128; at 256
(Qwen3.8's full attention, 16 dims a lane) every element went through the generic loader. Two
16-byte loads a lane now. rad-kbench 138/138 on the fixture and 33/33 on the 27B (worst rel_l2
1.5e-4). SYCL_PROGRESS records the first 27B BetterBench run (5720a95): decode median 19.1 tok/s,
prefill 113.4 tok/s at 2000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 06:28:35 +02:00
pascallinxweilerandClaude Opus 5.5 68db6023fd docs: 27B KL against the exact reference and the prefill work
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 06:02:28 +02:00
pascallinxweilerandClaude Opus 5.5 5720a958e4 libxe2: givens_dense_quant on XMX for prefill rows -- 2.07 -> 0.70 s over a 1792-token prefill
For 17 or more rows the rotation is a batch of GEMMs, one a (part, 128-column group): the input
fp16(x / D) per part, the f16 rotation as B, a batched form of the tiled fp16 XMX kernel
(xmx_f16_batched) into f32, then libref's quantiser a (row, part, group). The input rounded once
to fp16 where libref sums in double: a fast kernel. KL against the exact reference: mean 0.0037,
top-1 97.2% (0.0035 / 95.6% before: within the noise of 250 positions). Up to 16 rows the float-pair
kernel stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 05:55:19 +02:00
pascallinxweilerandClaude Opus 5.5 57f02bc12d libxe2: a local-memory-tiled fp16 XMX GEMM for the m2xfp4 prefill -- 27B prefill 91 -> 105 tok/s
xmx_f16_tiled: a 128 x 128 work-group tile, 4 x 2 sub-groups of 32 x 64 (16 accumulators, the
large register file), K in steps of 32 with A and a VNNI B staged in local memory, double
buffered, each sub-group's A tiles loaded once a K16 step; the accumulators stored straight to an
f32 scratch and scaled into y by a second kernel (a generic store in the epilogue spilled them and
ran 3x slower than the old tiling). ~30 TFLOPS on the 27B's prefill shapes in a standalone probe
against ~18 / ~11 for the previous per-sub-group tiling (the card's measured fp16 DPAS peak: ~98).
gemm_nt_q over a 1792-token prefill 6.4 -> 3.7 s. KL against the exact reference unchanged (mean
0.0035, top-1 95.6%): the same products.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 05:49:41 +02:00
pascallinxweilerandClaude Opus 5.5 e0bfeb54f8 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
2026-10-24 05:33:08 +02:00
13 changed files with 1044 additions and 38 deletions

No files matched your search

+27 -1
View File
@@ -159,6 +159,7 @@ fonts (this host has none; the first post had no text) -- all unpacked without r
|---|---|---|---|---|---|---|---|---|
| 2026-10-23 | `874f8d2` | minicpm5-2b-bf16 | sycl, correctness-first kernels | `--num-speculative-tokens 0 --max-model-len 8192 --max-num-seqs 4` | 17.3 / 17.3 @2000 (8000+ skipped: over the 8192 context) | 4.2 / 3.5 | first run | `~/radiance-bench/runs/20261023-165102-minicpm5-2b-bf16-874f8d2/results.html`, posted with `report.jpg` |
| 2026-10-23 | `a869226` | minicpm5-2b-bf16 | sycl, Phase 4 step 1 (GEMV) | same | 17.5 / 17.4 @2000 | 25.3 / 16.2 | decode +504.5% | `~/radiance-bench/runs/20261023-194856-minicpm5-2b-bf16-a869226/results.html`, posted with `report.jpg` |
| 2026-10-24 | `5720a95` | qwen38-27b-m2xfp4 (Qwen3.8-27B-DFLASH2-PARO-M2XFP4) | sycl, fast m2xfp4 GEMV / DPAS verify / tiled XMX prefill, DFlash2 drafter on | `--max-model-len 8192 --max-num-seqs 4` | 113.4 / 113.3 @2000 | 19.1 / - | first run | `~/radiance-bench/runs/20261024-060226-qwen38-27b-m2xfp4-5720a95/results.html`, posted with `report.jpg` |
| 2026-10-23 | `b07da2a` | minicpm5-2b-bf16 | sycl, Phase 4 steps 2-6 (XMX prefill, split-KV attention, norms, GEMV split) | same | 1575.6 / 1547.1 @2000 | 51.8 / 41.4 | decode +104.7%, prefill 90x | `~/radiance-bench/runs/20261023-221354-minicpm5-2b-bf16-b07da2a/results.html`, posted with `report.jpg` |
## Models
@@ -542,7 +543,32 @@ DFlash2 drafter** (device time a token 78 / 71 ms). With the fast kernels the dr
departs from plain decoding at a near-tie (byte 246 of 256 tokens): the M = 8 verify and the M = 1
GEMV round differently; with the exact kernels the two were identical.
Probes in the scratch directory, not in the tree: `m2bench.cpp` (decode GEMV variants on synthetic
**Accuracy (KL).** libref cannot run a 27B forward on this host in reasonable time, so the
reference is the SYCL engine with `XE2_EXACT=1` (every kernel in its exact form; each exact form
matched libref bit for bit wherever checked), recorded over a 3-document, 250-position mini corpus
in 64-token chunks (the model's DeltaNet chunk). The fast set against it: **KLD mean 0.0035-0.0037,
median ~0.0025, p99 ~0.021, top-1 95.6-97.2%, PPL ratio 0.995-0.998** (the model's own quantisation
error against bf16 is 0.0211 per its README). The fast set's KL is dominated by the fp8 activation
quantiser in front of every linear re-rounding small upstream differences; moving the prefill GEMM
from bf16 to fp16 activations took it 0.0041 -> 0.0035.
**Prefill (1792 tokens).** 26.3 s (68 tok/s) -> **17.0 s (105 tok/s)**:
- gdn_chunk_scan chain-parallel (a work-group of 256 a (head, 32 value columns), every phase's
independent chains spread, still bit-identical): 11.2 -> 4.6 s
- m2xfp4 GEMM fp16 (weight exact in fp16 after its column's e_max, activation fp16(a / R)) on a new
local-memory-tiled XMX kernel (128 x 128 tiles, 32 x 64 a sub-group, ~30 TFLOPS against the
card's ~98 fp16 DPAS peak, measured): 6.4 -> 3.7 s
- givens_dense_quant as batched XMX GEMMs for rows >= 17: 2.07 -> 0.70 s
- attn_paged at head_dim 256: the value rows read 16 bytes at a time (they were scalar): 2.72 ->
0.88 s (rad-kbench 138/138 fixture, 33/33 on the 27B)
- tried and dropped: a chunk-parallel kkt_solve (exact but slower, 1.44 -> 2.14 s).
Remaining prefill (profiled): chunk scan 4.6 s, GEMMs 3.7, attention 2.7 (head_dim 256, the
lane-per-key kernel), kkt 1.4, the rest ~2.4. A 27B prefill at 1200 tok/s needs ~65 TFLOPS of
GEMM alone, two thirds of this card's DPAS peak.
Probes in the scratch directory, not in the tree: `gemm.cpp` (XMX GEMM tilings), `peak.cpp`
(DPAS peak), `scan.cpp` (the chunk scan's phases), `m2bench.cpp` (decode GEMV variants on synthetic
m2xfp4 data), `chain.cpp` (the ordered-sum floor), `jmap.cpp` (joint_matrix_apply coordinates on a
packed B -- lost the device; not used).
+7
View File
@@ -215,6 +215,13 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
const sycl::vec<uint16_t, 8> vv = *(const sycl::vec<uint16_t, 8>*)(
(const uint16_t*)cb + kbj + head_dim + lane * 8);
for (int e = 0; e < 8; ++e) acc[e] += wj * (rad_bf16_to_f32(vv[e]) * vds);
} else if (vec_kv && per == 16) {
/* head_dim 256 (Qwen3.8's full attention): two 16-byte loads a lane */
const uint16_t* vr = (const uint16_t*)cb + kbj + head_dim + lane * 16;
const sycl::vec<uint16_t, 8> v0 = *(const sycl::vec<uint16_t, 8>*)vr;
const sycl::vec<uint16_t, 8> v1 = *(const sycl::vec<uint16_t, 8>*)(vr + 8);
for (int e = 0; e < 8; ++e) acc[e] += wj * (rad_bf16_to_f32(v0[e]) * vds);
for (int e = 0; e < 8; ++e) acc[8 + e] += wj * (rad_bf16_to_f32(v1[e]) * vds);
} else {
for (int e = 0; e < per; ++e)
acc[e] += wj * (rad_load_f32(cb, cdt, kbj + (head_dim + lane * per + e) * sd) * vds);
+10
View File
@@ -54,6 +54,16 @@ sycl::queue* queue_of(RadStream s);
* allocation fails. */
float* workspace(sycl::queue& q, size_t floats, int slot = 0);
/* Batched fp16 XMX GEMM into f32 (xe2_gemm_xmx.cpp): C rows padded up to a multiple of 128
* are written, so C + b * c_bs must hold ceil(M / 128) * 128 rows of N. */
int xmx_f16_batched(sycl::queue& q, const sycl::half* ab, int64_t ars, int64_t a_bs, const sycl::half* bb,
int64_t brs, int64_t b_bs, float* cb, int64_t c_bs, int64_t M, int64_t N, int64_t K,
int64_t batch);
/* fp16 XMX GEMM with a per-row and per-column output factor (xe2_gemm_xmx.cpp). */
int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs,
char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N,
int64_t K, const float* rs, const float* cs);
/* Runs `submit`, turning a SYCL exception into RAD_E_DEVICE with its message. */
template <typename F>
int submit(F&& f) {
+134 -2
View File
@@ -6,19 +6,25 @@
* dflash_select the block drafter's greedy walk over its candidate lattice (ref_quant.cpp):
* score[l, c] = unary[l, c] + sum_r pred[pid][r] * hp[l][r] * succ[cand[l, c]][r],
* the best candidate (first strict maximum) becoming the next step's predecessor.
* Serial in l by nature, so one work-item a sequence, summing over r in libref's
* order; (pred * hp)[r] is recomputed per term rather than held, the same floats.
* Serial in l by nature, so one sub-group a sequence, a lane a candidate summing
* over r in libref's order (rank <= 1024; else one work-item a sequence).
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include <cmath>
#include <cstdlib>
#include <cstring>
namespace xe2 {
namespace {
constexpr size_t kRowWg = 1024;
constexpr int64_t kSelMaxR = 1024;
constexpr int64_t kTopChunks = 128; /* row_topk's first stage: work-groups a row */
constexpr size_t kTopWg = 256;
constexpr int64_t kTopMaxR = 64;
constexpr int64_t kTopMaxChunk = 4096; /* keys a chunk in local memory (32 KiB) */ /* dflash_select's sub-group form: R floats a row in local memory */
}
/* ------------------------------------------------------------------ row_topk */
@@ -41,6 +47,78 @@ int row_topk(const RadArgs* a, RadStream st) {
float* pairs = PAIRS ? (float*)PAIRS->data : nullptr;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
/* A long row in two stages: each of kTopChunks work-groups takes its chunk's top R by the
* same passes, and one work-group a row then takes the top R of those chunk winners. The keys
* are distinct (they carry the column), so the row's top R are among the winners and both
* stages pick exactly what one pass over the row would. One work-group walking 248320
* columns 32 times was 3.2 ms. */
const int64_t chunk = (N + kTopChunks - 1) / kTopChunks;
if (N >= 16 * (int64_t)kRowWg && R <= kTopMaxR && chunk <= kTopMaxChunk &&
!std::getenv("XE2_TOPK_SERIAL")) {
uint64_t* win = (uint64_t*)workspace(*q, (size_t)(M * kTopChunks * R * 2), 8);
if (!win) return RAD_E_DEVICE;
int rc = submit([&] {
q->submit([&](sycl::handler& h) {
sycl::local_accessor<uint64_t, 1> keys(sycl::range<1>((size_t)chunk), h);
h.parallel_for(sycl::nd_range<1>{(size_t)(M * kTopChunks) * kTopWg, kTopWg},
[=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0) / kTopChunks, ch = (int64_t)it.get_group(0) % kTopChunks;
const int lane = (int)it.get_local_id(0);
const int64_t i0 = ch * chunk, n = sycl::min(chunk, N - i0);
for (int64_t i = lane; i < n; i += (int64_t)kTopWg)
keys[i] = sel_key(rad_load_f32(x.base, x.dtype, m * x.pitch + i0 + i), i0 + i);
sycl::group_barrier(it.get_group());
uint64_t prev = ~(uint64_t)0;
for (int64_t r = 0; r < R; ++r) {
uint64_t best = 0;
if (prev != 0) { /* uniform */
for (int64_t i = lane; i < n; i += (int64_t)kTopWg)
if (keys[i] < prev) best = sycl::max(best, keys[i]);
best = sycl::reduce_over_group(it.get_group(), best, sycl::maximum<uint64_t>());
}
if (lane == 0) win[(m * kTopChunks + ch) * R + r] = best;
prev = best;
}
});
});
});
if (rc < 0) return rc;
const int64_t nw = kTopChunks * R;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kRowWg, kRowWg}, [=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0);
const int lane = (int)it.get_local_id(0);
const uint64_t* wk = win + m * nw;
uint64_t prev = ~(uint64_t)0;
bool more = true;
for (int64_t r = 0; r < R; ++r) {
uint64_t best = 0;
if (more) {
for (int64_t i = lane; i < nw; i += (int64_t)kRowWg)
if (wk[i] < prev) best = sycl::max(best, wk[i]);
best = sycl::reduce_over_group(it.get_group(), best, sycl::maximum<uint64_t>());
}
if (lane == 0) {
const bool ok = best != 0;
const int64_t id = ok ? key_index(best) + off : -1;
const float v = ok ? key_value(best) : -INFINITY;
const int64_t io = m * iv.pitch + r;
if (iv.dtype == RAD_I64) ((int64_t*)iv.base)[io] = id;
else ((int32_t*)iv.base)[io] = (int32_t)id;
rad_store_f32(vv.base, vv.dtype, m * vv.pitch + r, v);
if (pairs) {
float* pq = pairs + (m * R + r) * 2;
const int32_t i32 = (int32_t)id;
std::memcpy(pq, &i32, sizeof i32);
pq[1] = v;
}
}
if (best == 0) more = false;
else prev = best;
}
});
});
}
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kRowWg, kRowWg}, [=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0);
@@ -114,6 +192,60 @@ int dflash_select(const RadArgs* a, RadStream st) {
const Lin anc = Lin::of(ANC);
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
if (R <= kSelMaxR && !std::getenv("XE2_SELECT_SERIAL")) {
/* A sub-group a sequence: (pred * hp)[r] and the 16 candidates' succ rows staged in
* local memory by all lanes, lane c then sums candidate c's score over r in order (the
* same chain as below), and lane 0 takes the first strict maximum over the lanes' scores
* in candidate order. The serial form walks 16 rows of 256 one after another. */
const int64_t RP = R + 1; /* a padded row: lane c reads row c without bank conflicts */
return submit([&] {
q->submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> axl(sycl::range<1>((size_t)R), h);
sycl::local_accessor<float, 1> rows(sycl::range<1>((size_t)(kSub * RP)), h);
h.parallel_for(sycl::nd_range<1>{(size_t)M * kSub, (size_t)kSub},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int64_t m = (int64_t)it.get_group(0);
const int lane = (int)sg.get_local_linear_id();
int64_t pid = ld_index(anc.dtype, anc.base, anc.off(m * astr));
for (int64_t l = 0; l < steps; ++l) {
const int64_t r = m * steps + l;
const bool ok = pid >= 0 && pid < nv;
for (int64_t i = lane; i < R; i += kSub)
axl[i] = (ok ? rad_load_f32(pred.base, pred.dtype, pid * pred.pitch + i) : 0.0f) *
(has_hp ? rad_load_f32(hp.base, hp.dtype, r * hp.pitch + i) : 1.0f);
int64_t best_c = 0;
float best_s = -INFINITY;
for (int64_t c0 = 0; c0 < K; c0 += kSub) {
const int64_t c = c0 + lane;
const int64_t tok = c < K ? ld_index(cand.dtype, cand.base, r * cand.pitch + c) : -1;
for (int j = 0; j < kSub; ++j) {
const int64_t tj = sycl::select_from_group(sg, tok, j);
if (tj >= 0 && tj < nv) /* uniform */
for (int64_t i = lane; i < R; i += kSub)
rows[j * RP + i] = rad_load_f32(succ.base, succ.dtype, tj * succ.pitch + i);
}
sycl::group_barrier(sg);
float sc = c < K ? rad_load_f32(un.base, un.dtype, r * un.pitch + c) : -INFINITY;
if (c < K && tok >= 0 && tok < nv)
for (int64_t i = 0; i < R; ++i) sc += axl[i] * rows[lane * RP + i];
for (int j = 0; j < kSub; ++j) {
const float sj = sycl::select_from_group(sg, sc, j);
if (c0 + j < K && sj > best_s) { best_s = sj; best_c = c0 + j; }
}
sycl::group_barrier(sg);
}
pid = ld_index(cand.dtype, cand.base, r * cand.pitch + best_c);
if (lane == 0) {
const int64_t oo = m * out.pitch + l;
if (out.dtype == RAD_I64) ((int64_t*)out.base)[oo] = pid;
else ((int32_t*)out.base)[oo] = (int32_t)pid;
}
}
});
});
});
}
const size_t wg = 16;
const size_t n = (size_t)((M + (int64_t)wg - 1) / (int64_t)wg) * wg;
return submit([&] {
+148
View File
@@ -495,6 +495,154 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
const IV cuv = iv_of(cu);
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
if (K == 128 && V % 32 == 0 && chunk == 64 && !std::getenv("XE2_GDN_SCAN1")) {
/* A work-group of 256 a (sequence, v head, block of 32 value columns), the chunk's k, q,
* the block's slices of v, S, W, V' and the sA rows R in local memory (~124 KB). Each phase
* of a chunk is a set of independent chains -- W[j][e], V'[i][e], R[i][j], O[i][e], S[d][e]
* -- each run by one work-item in libref's order, so the scan is libref's; the parallelism
* is the chains' (thousands a phase), not one column a work-item. R and the decays depend
* only on q, k, g, so each column block forms them again. */
constexpr int CB = 32, CK = 64, KK = 128, WG = 256;
constexpr int KP = KK + 1; /* k and q rows padded: the R phase's lanes read different rows */
const int64_t nblk = V / CB;
/* e^{g_i - g_j} for every (row i, chunk position j <= i of i's chunk), libref's double exp
* rounded once, a (token, head) row of 64: once here instead of once a column block. */
float* dec = workspace(*qu, (size_t)(T * H * CK + 64), 5);
if (!dec) return RAD_E_DEVICE;
if (int rc0 = submit([&] {
qu->parallel_for(sycl::range<1>((size_t)(T * H)), [=](sycl::id<1> id) {
const int64_t th = (int64_t)id[0];
const int64_t tok = th / H, h = th % H;
/* the token's sequence and its chunk start */
int64_t n = 0;
while (n + 1 < N && cuv.at(n + 1) <= tok) ++n;
const int64_t bos = cuv.at(n), c0 = bos + (tok - bos) / CK * CK;
const float gi = gs.ld(at2(gv, tok, h));
for (int64_t j = 0; j < CK; ++j) {
const int64_t tj = c0 + j;
dec[th * CK + j] = tj <= tok ? rexp(gi - gs.ld(at2(gv, tj, h))) : 0.0f;
}
});
});
rc0 < 0)
return rc0;
return submit([&] {
qu->submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> lm(sycl::range<1>((size_t)(2 * CK * KP + 3 * CK * CB + KK * CB + CK * CK + 4 * CK)), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H * nblk) * WG, (size_t)WG}, [=](sycl::nd_item<1> it) {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t blk = gid % nblk, nh = gid / nblk;
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t e0 = blk * CB;
const int t = (int)it.get_local_id(0);
auto gw = it.get_group();
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
const int64_t sn = has_si ? sis.li(n * sw) : n;
if (sn < 0) return; /* uniform */
float* kc = &lm[0]; /* [CK][KP] */
float* qc = kc + CK * KP; /* [CK][KP] */
float* vc = qc + CK * KP; /* [CK][CB] */
float* Wm = vc + CK * CB; /* [CK][CB] */
float* Vp = Wm + CK * CB; /* [CK][CB] */
float* St = Vp + CK * CB; /* [KK][CB] */
float* R = St + KK * CB; /* [CK][CK] */
float* gc = R + CK * CK; /* [CK] */
float* bc = gc + CK;
float* ge = bc + CK; /* e^{g_j} */
float* gsj = ge + CK; /* e^{g_last - g_j} */
for (int idx = t; idx < KK * CB; idx += WG) {
const int d = idx / CB, e = idx % CB;
St[idx] = has_h0 ? h0s.ld(at4(h0v, sn, h, e0 + e, d)) : 0.0f;
}
for (int64_t c0 = 0; c0 < slen; c0 += CK) {
const int64_t L = (c0 + CK < slen) ? CK : slen - c0;
sycl::group_barrier(gw);
for (int idx = t; idx < (int)L * KK; idx += WG) {
const int j = idx / KK, d = idx % KK;
const int64_t tj = bos + c0 + j;
kc[j * KP + d] = ks.ld(at3(kv, tj, hk, d));
qc[j * KP + d] = qs.ld(at3(qv, tj, hk, d));
}
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int j = idx / CB, e = idx % CB;
vc[idx] = vs.ld(at3(vv, bos + c0 + j, h, e0 + e));
}
if (t < L) {
const int64_t tj = bos + c0 + t;
gc[t] = gs.ld(at2(gv, tj, h));
bc[t] = bs.ld(at2(bv, tj, h));
}
sycl::group_barrier(gw);
const float glast = gc[L - 1];
if (t < L) {
ge[t] = rexp(gc[t]);
gsj[t] = rexp(glast - gc[t]);
}
sycl::group_barrier(gw);
/* W[j][e] = beta_j (v[j][e] - e^{g_j} sum_d k[j][d] S[d][e]) */
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int j = idx / CB, e = idx % CB;
float acc = 0.0f;
for (int d = 0; d < KK; ++d) acc += kc[j * KP + d] * St[d * CB + e];
Wm[idx] = bc[j] * (vc[idx] - ge[j] * acc);
}
/* the chunk's rows of A, widened into the R buffer (R is formed after V') */
for (int idx = t; idx < (int)(L * L); idx += WG) {
const int i = idx / (int)L, j = idx % (int)L;
R[i * CK + j] = j <= i ? As.ld(at3(Av, bos + c0 + i, h, j)) : 0.0f;
}
sycl::group_barrier(gw);
/* V'[i][e] = sum_{j <= i} A[i][j] W[j][e] */
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int i = idx / CB, e = idx % CB;
float vp = 0.0f;
for (int j = 0; j <= i; ++j) vp += R[i * CK + j] * Wm[j * CB + e];
Vp[idx] = vp;
}
sycl::group_barrier(gw);
/* R[i][j] = scale e^{g_i - g_j} sum_d q[i][d] k[j][d], j <= i */
for (int idx = t; idx < (int)(L * L); idx += WG) {
const int i = idx / (int)L, j = idx % (int)L;
if (j > i) continue;
float rj = 0.0f;
for (int d = 0; d < KK; ++d) rj += qc[i * KP + d] * kc[j * KP + d];
R[i * CK + j] = scale * dec[((bos + c0 + i) * H + h) * CK + j] * rj;
}
sycl::group_barrier(gw);
/* O[i][e] = scale e^{g_i} sum_d q[i][d] S[d][e] + sum_{j <= i} R[i][j] V'[j][e] */
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int i = idx / CB, e = idx % CB;
float inr = 0.0f;
for (int d = 0; d < KK; ++d) inr += qc[i * KP + d] * St[d * CB + e];
float acc = (scale * ge[i]) * inr;
for (int j = 0; j <= i; ++j) acc += R[i * CK + j] * Vp[j * CB + e];
os.st(at3(ov, bos + c0 + i, h, e0 + e), acc);
}
sycl::group_barrier(gw);
/* k[j][d] *= e^{g_last - g_j}; S[d][e] = e^{g_last} S[d][e] + sum_j V'[j][e] k[j][d] */
for (int idx = t; idx < (int)L * KK; idx += WG) {
const int j = idx / KK, d = idx % KK;
kc[j * KP + d] = gsj[j] * kc[j * KP + d];
}
sycl::group_barrier(gw);
const float el = rexp(glast);
for (int idx = t; idx < KK * CB; idx += WG) {
const int d = idx / CB, e = idx % CB;
float sr = el * St[idx];
for (int j = 0; j < (int)L; ++j) sr += Vp[j * CB + e] * kc[j * KP + d];
St[idx] = sr;
}
}
sycl::group_barrier(gw);
if (has_ht)
for (int idx = t; idx < KK * CB; idx += WG) {
const int d = idx / CB, e = idx % CB;
hts.st(at4(htv, sn, h, e0 + e, d), St[idx]);
}
});
});
});
}
if (V % kSub == 0 && V <= 256 && chunk <= V && K * V <= 128 * 128) {
/* A work-group a (sequence, v head), a work-item a value column e (and, for the Q K^T
* rows, a chunk position j): the state in local memory, the chunk's widened operands in
+105 -3
View File
@@ -15,6 +15,7 @@
#include "rad_planes.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
namespace xe2 {
@@ -260,6 +261,97 @@ int head_gemv(sycl::queue& q, const sycl::half* a16, const float* rr, const char
});
}
/* M <= 8 on XMX (decode too: 3.4 ms against the SIMT form's 3.9 at M = 1): a sub-group 16 vocab columns, a lane one column's 64 codes a step decoded
* by the bits above into its slice of a packed fp16 B tile in local memory (the pairs (0, 2) and
* (1, 3) of each word are the activation's pair order already), four fp16 DPAS K16 against the
* activation; a scale block's sum is taken out of the accumulator and scaled in f32 per column,
* so the products are summed in f32 rather than half2. Needs the scale block a multiple of 64
* columns (or one block); else the SIMT form. */
int head_dpas(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) {
namespace jm = sycl::ext::oneapi::experimental::matrix;
using hf = sycl::half;
constexpr int kSgs = 16;
const int64_t tiles = (N + 15) / 16;
const size_t groups = (size_t)((tiles + kSgs - 1) / kSgs);
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>((size_t)kSgs * 32 * 16), h);
sycl::local_accessor<float, 1> Cs(sycl::range<1>((size_t)kSgs * 8 * 16), h);
h.parallel_for(sycl::nd_range<1>{groups * kSgs * kSub, (size_t)kSgs * kSub},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
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 n0 = ((int64_t)it.get_group(0) * kSgs + s) * 16;
if (n0 >= N) return; /* uniform */
const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale");
if (!cp.p || !sp.p) return;
const int64_t n = sycl::min(n0 + lane, N - 1);
const uint8_t* crow = (const uint8_t*)cp.p + n * cp.row_bytes;
if (cp.dt != RAD_F8E4M3 || !(sp.bc == 0 || sp.bc % 64 == 0) || cp.row_bytes % 16 ||
((uintptr_t)cp.p & 15)) {
/* not this form's head (the layout is only known here): a lane its column in
* f32, a16's permutation undone per element */
float accg[8];
for (int m = 0; m < 8; ++m) accg[m] = 0.0f;
for (int64_t k = 0; k < K; ++k) {
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 m = 0; m < 8; ++m) accg[m] += (float)a16[m * K + pos] * w;
}
if (n0 + lane < N)
for (int m = 0; m < mm; ++m)
rad_store_f32(yb, ydt, m * yrs + n * ycs, accg[m] * rr[m] * (1.0f / 256.0f));
return;
}
const int64_t blk = sp.bc > 0 ? sp.bc : K;
uint32_t* bt = &Bs[(size_t)s * 32 * 16];
float* ct = &Cs[(size_t)s * 8 * 16];
auto lc = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(ct);
jm::joint_matrix<sycl::sub_group, hf, jm::use::a, 8, 16, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, hf, jm::use::b, 16, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc;
float acc[8];
for (int m = 0; m < 8; ++m) acc[m] = 0.0f;
for (int64_t b0 = 0; b0 < K; b0 += blk) {
jm::joint_matrix_fill(sg, mc, 0.0f);
for (int64_t k0 = b0; k0 < b0 + blk; k0 += 64) {
uint32_t c[16];
for (int v = 0; v < 4; ++v) {
const sycl::vec<uint32_t, 4> c4 = *(const sycl::vec<uint32_t, 4>*)(crow + k0 + 16 * v);
for (int u = 0; u < 4; ++u) c[4 * v + u] = c4[u];
}
for (int j = 0; j < 16; ++j) {
const uint32_t x = c[j] & 0x00FF00FFu, y = (c[j] >> 8) & 0x00FF00FFu;
bt[(2 * j) * 16 + lane] = ((x & 0x007F007Fu) << 7) | ((x & 0x00800080u) << 8);
bt[(2 * j + 1) * 16 + lane] = ((y & 0x007F007Fu) << 7) | ((y & 0x00800080u) << 8);
}
sycl::group_barrier(sg);
for (int kk = 0; kk < 64; kk += 16) {
auto la = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(a16 + k0 + kk);
jm::joint_matrix_load(sg, ma, la, (size_t)K);
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>((hf*)&bt[(kk / 2) * 16]);
jm::joint_matrix_load(sg, mb, lb, 32);
jm::joint_matrix_mad(sg, mc, ma, mb, mc);
}
sycl::group_barrier(sg);
}
jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major);
sycl::group_barrier(sg);
const float sc = sp.cover(n, b0);
for (int m = 0; m < 8; ++m) acc[m] += ct[m * 16 + lane] * sc;
sycl::group_barrier(sg);
}
if (n0 + lane < N)
for (int m = 0; m < mm; ++m) rad_store_f32(yb, ydt, m * yrs + n * ycs, acc[m] * rr[m]);
});
});
});
}
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;
@@ -273,17 +365,26 @@ int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
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));
/* the XMX form reads 8 activation rows, the ones past M zero */
static const bool simt = std::getenv("XE2_HEAD_SIMT") != nullptr;
const bool dpas = M <= 8 && K % 64 == 0 && !simt;
const int64_t mrows = dpas ? 8 : M;
const int64_t abytes = (mrows * K * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)(abytes / 4 + mrows + 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) {
q->parallel_for(sycl::nd_range<1>{(size_t)mrows * 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);
if (m >= M) { /* uniform: the XMX form's padding rows */
for (int64_t k = t; k < K; k += 256) a16[m * K + k] = (sycl::half)0.0f;
if (t == 0) rr[m] = 1.0f;
return;
}
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>());
@@ -301,6 +402,7 @@ int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
const char* bb = (const char*)B->data;
char* yb = (char*)Y->data;
const int mm = (int)M;
if (dpas) return head_dpas(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
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);
+230
View File
@@ -17,6 +17,8 @@
#include "xe2_common.h"
#include "xe2_ops.h"
#include <cstdlib>
namespace xe2 {
namespace {
@@ -151,3 +153,231 @@ int logits_gemm_xmx(const RadArgs* a, RadStream s) {
}
} // namespace xe2
/* ------------------------------------------------------------------ the fp16 form, scaled */
/* The same tiling over fp16 operands with a per-row and per-column factor on the way out:
* y[m][n] = acc * rs[m] * cs[n]. For the m2xfp4 prefill (xe2_gemv_m2.cpp), whose weight is exact
* in fp16 once scaled by its column's largest exponent and whose activation is fp16(a / R): fp16's
* 2^-11 step beside bf16's 2^-8. A is [mpad, K] with mpad a multiple of 8 (padding rows zero);
* rows past M are not written. N a multiple of 64, K of 16. */
namespace xe2 {
/* The local-memory tiling: a 128 x 128 work-group tile (4 x 2 sub-groups of 32 x 64, 16
* accumulators each, the large register file), K in steps of 32 with A [128][32] and B VNNI
* [16 pairs][128] staged in local memory, double buffered; each sub-group's A tiles loaded once
* a K16 step. ~30 TFLOPS on the 27B's prefill shapes (gemm.cpp in scratch; the per-sub-group form
* below ~11-18, the card's DPAS peak ~98). N a multiple of 128, K of 32. */
int xmx_f16_tiled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs,
char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t N, int64_t K,
const float* rs, const float* cs) {
using hf = sycl::half;
constexpr int BM = 128, BN = 128, BK = 32, SGM = 32, SGN = 64;
const size_t gm = (size_t)((M + BM - 1) / BM), gn = (size_t)(N / BN);
namespace intelex = sycl::ext::intel::experimental;
syclex::properties props{intelex::grf_size<256>};
/* The accumulators go straight to an f32 [M, N] scratch (a generic store in this kernel's
* epilogue spills its 16 accumulators); a second kernel applies the factors and y's dtype. */
const int64_t mp = (int64_t)gm * BM;
float* cbuf = workspace(q, (size_t)(mp * N), 6);
if (!cbuf) return RAD_E_DEVICE;
int rc = submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<hf, 1> As(sycl::range<1>(2 * BM * BK), h);
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>(2 * (BK / 2) * BN), h);
h.parallel_for(sycl::nd_range<2>{{gm, gn * 128}, {1, 128}}, props,
[=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int s = (int)sg.get_group_linear_id(), t = (int)it.get_local_id(1);
const int sm = s / 2, sn = s % 2;
const int64_t mb0 = (int64_t)it.get_group(0) * BM, nb0 = (int64_t)it.get_group(1) * BN;
jm::joint_matrix<sycl::sub_group, hf, jm::use::a, 8, 16, jm::layout::row_major> ma[4];
jm::joint_matrix<sycl::sub_group, hf, jm::use::b, 16, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc[4][4];
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) jm::joint_matrix_fill(sg, mc[i][j], 0.0f);
auto stage_in = [&](int buf, int64_t k0) {
for (int r = 0; r < 4; ++r) {
const int idx = t + r * 128;
const int row = idx / 4, kc = (idx % 4) * 8;
const int64_t gm_ = mb0 + row;
sycl::vec<uint32_t, 4> v(0u, 0u, 0u, 0u);
if (gm_ < M) v = *(const sycl::vec<uint32_t, 4>*)(ab + gm_ * ars + k0 + kc);
*(sycl::vec<uint32_t, 4>*)&As[buf * BM * BK + row * BK + kc] = v;
}
for (int r = 0; r < 2; ++r) {
const int idx = t + r * 128;
const int n = idx / 2, kc = (idx % 2) * 16;
const sycl::vec<uint32_t, 8> v = *(const sycl::vec<uint32_t, 8>*)(bb + (nb0 + n) * brs + k0 + kc);
for (int u = 0; u < 8; ++u) Bs[buf * (BK / 2) * BN + (kc / 2 + u) * BN + n] = v[u];
}
};
stage_in(0, 0);
int buf = 0;
for (int64_t k0 = 0; k0 < K; k0 += BK) {
sycl::group_barrier(it.get_group());
if (k0 + BK < K) stage_in(buf ^ 1, k0 + BK);
for (int kk = 0; kk < BK; kk += 16) {
for (int i = 0; i < 4; ++i) {
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(&As[buf * BM * BK + (sm * SGM + i * 8) * BK + kk]);
jm::joint_matrix_load(sg, ma[i], la, BK);
}
for (int j = 0; j < 4; ++j) {
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(
(hf*)&Bs[buf * (BK / 2) * BN + (kk / 2) * BN] + (sn * SGN + j * 16) * 2);
jm::joint_matrix_load(sg, mb, lb, BN * 2);
for (int i = 0; i < 4; ++i) jm::joint_matrix_mad(sg, mc[i][j], ma[i], mb, mc[i][j]);
}
}
buf ^= 1;
}
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
const int64_t m0 = mb0 + sm * SGM + i * 8, n0 = nb0 + sn * SGN + j * 16;
auto gc = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(cbuf + m0 * N + n0);
jm::joint_matrix_store(sg, mc[i][j], gc, (size_t)N, jm::layout::row_major);
}
});
});
});
if (rc < 0) return rc;
return submit([&] {
q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> id) {
const int64_t m = (int64_t)id[0] / N, n = (int64_t)id[0] % N;
rad_store_f32(yb, ydt, m * yrs + n * ycs, cbuf[id[0]] * rs[m] * cs[n]);
});
});
}
/* The same tiling, batched and unscaled: batch b multiplies A + b * a_bs by B + b * b_bs into the
* f32 C + b * c_bs, [M, N] row-major (ldc = N). N a multiple of 128, K of 32. */
int xmx_f16_batched(sycl::queue& q, const sycl::half* ab, int64_t ars, int64_t a_bs, const sycl::half* bb,
int64_t brs, int64_t b_bs, float* cb, int64_t c_bs, int64_t M, int64_t N, int64_t K,
int64_t batch) {
using hf = sycl::half;
constexpr int BM = 128, BN = 128, BK = 32, SGM = 32, SGN = 64;
const size_t gm = (size_t)((M + BM - 1) / BM), gn = (size_t)(N / BN);
namespace intelex = sycl::ext::intel::experimental;
syclex::properties props{intelex::grf_size<256>};
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<hf, 1> As(sycl::range<1>(2 * BM * BK), h);
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>(2 * (BK / 2) * BN), h);
h.parallel_for(sycl::nd_range<3>{{(size_t)batch, gm, gn * 128}, {1, 1, 128}}, props,
[=](sycl::nd_item<3> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int s = (int)sg.get_group_linear_id(), t = (int)it.get_local_id(2);
const int sm = s / 2, sn = s % 2;
const int64_t bt = (int64_t)it.get_group(0);
const hf* a0 = ab + bt * a_bs;
const hf* b0 = bb + bt * b_bs;
float* c0 = cb + bt * c_bs;
const int64_t mb0 = (int64_t)it.get_group(1) * BM, nb0 = (int64_t)it.get_group(2) * BN;
jm::joint_matrix<sycl::sub_group, hf, jm::use::a, 8, 16, jm::layout::row_major> ma[4];
jm::joint_matrix<sycl::sub_group, hf, jm::use::b, 16, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc[4][4];
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) jm::joint_matrix_fill(sg, mc[i][j], 0.0f);
auto stage_in = [&](int buf, int64_t k0) {
for (int r = 0; r < 4; ++r) {
const int idx = t + r * 128;
const int row = idx / 4, kc = (idx % 4) * 8;
const int64_t gm_ = mb0 + row;
sycl::vec<uint32_t, 4> v(0u, 0u, 0u, 0u);
if (gm_ < M) v = *(const sycl::vec<uint32_t, 4>*)(a0 + gm_ * ars + k0 + kc);
*(sycl::vec<uint32_t, 4>*)&As[buf * BM * BK + row * BK + kc] = v;
}
for (int r = 0; r < 2; ++r) {
const int idx = t + r * 128;
const int n = idx / 2, kc = (idx % 2) * 16;
const sycl::vec<uint32_t, 8> v = *(const sycl::vec<uint32_t, 8>*)(b0 + (nb0 + n) * brs + k0 + kc);
for (int u = 0; u < 8; ++u) Bs[buf * (BK / 2) * BN + (kc / 2 + u) * BN + n] = v[u];
}
};
stage_in(0, 0);
int buf = 0;
for (int64_t k0 = 0; k0 < K; k0 += BK) {
sycl::group_barrier(it.get_group());
if (k0 + BK < K) stage_in(buf ^ 1, k0 + BK);
for (int kk = 0; kk < BK; kk += 16) {
for (int i = 0; i < 4; ++i) {
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(&As[buf * BM * BK + (sm * SGM + i * 8) * BK + kk]);
jm::joint_matrix_load(sg, ma[i], la, BK);
}
for (int j = 0; j < 4; ++j) {
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(
(hf*)&Bs[buf * (BK / 2) * BN + (kk / 2) * BN] + (sn * SGN + j * 16) * 2);
jm::joint_matrix_load(sg, mb, lb, BN * 2);
for (int i = 0; i < 4; ++i) jm::joint_matrix_mad(sg, mc[i][j], ma[i], mb, mc[i][j]);
}
}
buf ^= 1;
}
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
const int64_t m0 = mb0 + sm * SGM + i * 8, n0 = nb0 + sn * SGN + j * 16;
auto gc = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(c0 + m0 * N + n0);
jm::joint_matrix_store(sg, mc[i][j], gc, (size_t)N, jm::layout::row_major);
}
});
});
});
}
int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs,
char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N,
int64_t K, const float* rs, const float* cs) {
if (N % 128 == 0 && K % 32 == 0 && ars % 8 == 0 && brs % 16 == 0 && !std::getenv("XE2_XMX_PLAIN"))
return xmx_f16_tiled(q, ab, ars, bb, brs, yb, ydt, yrs, ycs, M, N, K, rs, cs);
using hf = sycl::half;
const size_t gm = (size_t)((mpad + kTm * kSgs - 1) / (kTm * kSgs));
const size_t gn = (size_t)(N / (kTn * kTiles));
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> stage(sycl::range<1>((size_t)kSgs * kTm * kTn), h);
h.parallel_for(sycl::nd_range<2>{{gm, gn * kWg}, {1, kWg}},
[=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int s = (int)sg.get_group_linear_id();
const int64_t m0 = ((int64_t)it.get_group(0) * kSgs + s) * kTm;
const int64_t n0 = (int64_t)it.get_group(1) * kTn * kTiles;
if (m0 >= mpad) return;
auto ga = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(ab + m0 * ars);
jm::joint_matrix<sycl::sub_group, hf, jm::use::a, kTm, kTk, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, hf, jm::use::b, kTk, kTn, jm::layout::col_major> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, kTm, kTn> mc[kTiles];
for (int t = 0; t < kTiles; ++t) jm::joint_matrix_fill(sg, mc[t], 0.0f);
for (int64_t k = 0; k < K; k += kTk) {
jm::joint_matrix_load(sg, ma, ga + k, (size_t)ars);
for (int t = 0; t < kTiles; ++t) {
auto gb = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(bb + (n0 + t * kTn) * brs + k);
jm::joint_matrix_load(sg, mb, gb, (size_t)brs);
jm::joint_matrix_mad(sg, mc[t], ma, mb, mc[t]);
}
}
float* mine = &stage[(size_t)s * kTm * kTn];
auto ls = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(mine);
const int lane = (int)sg.get_local_linear_id();
for (int t = 0; t < kTiles; ++t) {
jm::joint_matrix_store(sg, mc[t], ls, kTn, jm::layout::row_major);
sycl::group_barrier(sg);
for (int e = lane; e < kTm * kTn; e += kSub) {
const int64_t r = e / kTn, c = e % kTn, m = m0 + r, n = n0 + t * kTn + c;
if (m < M) rad_store_f32(yb, ydt, m * yrs + n * ycs, mine[e] * rs[m] * cs[n]);
}
sycl::group_barrier(sg);
}
});
});
});
}
} // namespace xe2
+114
View File
@@ -19,6 +19,7 @@
#include "rad_planes.h"
#include <cstdlib>
#include <cstring>
namespace xe2 {
@@ -287,6 +288,111 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
});
}
/* The same group terms with the integer sums on XMX: an int8 DPAS (8 x 32 by 32 x 16) forms each
* group's sum of a * code exactly in int32 for up to 8 rows and 16 columns at once. The codes
* unpack straight into the packed B operand: four codes along k are one dword, and the step
* order's swap of bits 2 and 3 of k moves whole dwords, so the activation is used as stored.
* A sub-group owns 16 columns and walks the groups in order, each lane adding its column's terms
* as the per-output form does -- libref's chain, at one read of the weight. */
constexpr int kZpDpasSgs = 16;
template <int CBITS>
inline uint32_t zp_dword(const uint8_t* crow, int64_t d) { /* the 4 codes of logical dword d */
if constexpr (CBITS == 2) {
const uint32_t b = crow[d];
return (b & 3u) | ((b >> 2) & 3u) << 8 | ((b >> 4) & 3u) << 16 | ((b >> 6) & 3u) << 24;
} else {
const uint32_t h = (uint32_t)crow[2 * d] | (uint32_t)crow[2 * d + 1] << 8;
return (h & 15u) | ((h >> 4) & 15u) << 8 | ((h >> 8) & 15u) << 16 | ((h >> 12) & 15u) << 24;
}
}
template <int CBITS>
int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint32_t sadt, int64_t sa_rs,
const char* sumb, uint32_t sumdt, int64_t asum_s, int64_t mpad, const uint8_t* bb, int64_t brow,
const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, int64_t N, int64_t K,
int64_t group, int mm) {
namespace jm = sycl::ext::oneapi::experimental::matrix;
const int64_t G = K / group;
const size_t wgs = (size_t)((N + 16 * kZpDpasSgs - 1) / (16 * kZpDpasSgs));
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<int8_t, 1> As(sycl::range<1>((size_t)(8 * K)), h);
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h);
sycl::local_accessor<int32_t, 1> Cs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h);
h.parallel_for(sycl::nd_range<1>{wgs * kZpDpasSgs * kSub, (size_t)kZpDpasSgs * kSub},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
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 int t = (int)it.get_local_id(0);
for (int64_t i = t; i < 2 * K; i += kZpDpasSgs * kSub) { /* 8 rows, 4 bytes at a time */
const int64_t m = i / (K / 4), c = (i % (K / 4)) * 4;
uint32_t v = 0u;
if (m < mm) v = *(const uint32_t*)(ab + m * ars + c);
*(uint32_t*)&As[m * K + c] = v;
}
sycl::group_barrier(it.get_group());
const int64_t n0 = ((int64_t)it.get_group(0) * kZpDpasSgs + s) * 16;
if (n0 >= N) return;
const DPlane sc = find_plane(bsb, "scale"), zp = find_plane(bsb, "zero");
if (!sc.p || !zp.p) return;
const int64_t n = n0 + lane;
const bool live = n < N;
const uint8_t* crow = bb + (live ? n : n0) * brow;
float sa[8], acc[8];
for (int m = 0; m < 8; ++m) {
sa[m] = m < mm ? rad_load_f32(sab, sadt, m * sa_rs) : 0.0f;
acc[m] = 0.0f;
}
uint32_t* bt = &Bs[s * 8 * 16];
int32_t* ct = &Cs[s * 8 * 16];
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>((int8_t*)bt);
auto lc = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(ct);
jm::joint_matrix<sycl::sub_group, int8_t, jm::use::a, 8, 32, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, int8_t, jm::use::b, 32, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, int32_t, jm::use::accumulator, 8, 16> mc;
for (int64_t g = 0; g < G; ++g) {
jm::joint_matrix_fill(sg, mc, 0);
for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 32) {
/* stored dword r of this step holds logical dword (r & ~3) | swap(r & 3) */
for (int r = 0; r < 8; ++r) {
const int ld = (r & ~3) | ((r & 1) << 1) | ((r & 2) >> 1);
bt[r * 16 + lane] = zp_dword<CBITS>(crow, k0 / 4 + ld);
}
sycl::group_barrier(sg);
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(&As[k0]);
jm::joint_matrix_load(sg, ma, la, (size_t)K);
jm::joint_matrix_load(sg, mb, lb, 64);
jm::joint_matrix_mad(sg, mc, ma, mb, mc);
sycl::group_barrier(sg);
}
jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major);
sycl::group_barrier(sg);
if (live) {
const float sb = sc.at(n, g);
const float z = (float)(int32_t)zp.at(n, g);
for (int m = 0; m < 8; ++m) {
if (m >= mm) break;
const float sub = (float)ct[m * 16 + lane] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s);
const float term = sub * sa[m] * sb;
acc[m] += term;
}
}
sycl::group_barrier(sg);
}
if (live)
for (int m = 0; m < 8; ++m) {
if (m >= mm) break;
rad_store_f32(yb, ydt, m * ys_r + n * ys_c, acc[m]);
}
});
});
});
}
} // namespace
int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) {
@@ -321,6 +427,14 @@ int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) {
const int64_t sa_rs = row_stride(AS, 1);
const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1;
const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1;
if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 4 == 0 &&
!std::getenv("XE2_ZP_GEMV")) {
auto dp = cbits == 2 ? zp_dpas<2> : zp_dpas<4>;
int rc = dp(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs,
(const char*)ASUM->data, ASUM->dtype, asum_s, mpad, (const uint8_t*)B->data, brow,
(const char*)BS->data, (char*)Y->data, Y->dtype, ys_r, ys_c, N, K, group, (int)M);
return rc;
}
auto go = [&](auto tag) {
constexpr int MM = decltype(tag)::value;
return zp_gemv<MM>(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs,
+68
View File
@@ -169,6 +169,71 @@ int launch_split(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* 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;
@@ -200,6 +265,9 @@ int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int
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);
}
+41 -27
View File
@@ -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;
}
+44
View File
@@ -60,6 +60,50 @@ int givens_dense_quant(const RadArgs* a, RadStream st) {
const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && rows >= 17 && !exact_only() &&
xcs == 1 && rad_tensor_is_contiguous(T)) {
/* Prefill: the rotation as a batch of GEMMs on XMX, one a (part, group):
* y[r][j] = sum_i T[p][gi][j][i] * fp16(x[r][gi*128 + i] / D[p][gi*128 + i]). The
* activation rounded once to fp16 (2^-11) where libref sums in double: a fast kernel, held
* to the tolerance and the KL bound. Then libref's quantiser a (row, part, group). */
const int64_t mp = (rows + 127) / 128 * 128;
const int64_t in_e = P * mp * n; /* fp16 inputs [P][mp][n] */
const int64_t inbytes = (in_e * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)(inbytes / 4 + P * ngrp * mp * kG + 64), 7);
if (!ws) return RAD_E_DEVICE;
sycl::half* in16 = (sycl::half*)ws;
float* yv = (float*)((char*)ws + inbytes); /* [P * ngrp][mp][128] */
int rc = submit([&] {
q->parallel_for(sycl::range<1>((size_t)in_e), [=](sycl::id<1> id) {
const int64_t c = (int64_t)id[0] % n, r = ((int64_t)id[0] / n) % mp, p = (int64_t)id[0] / (n * mp);
in16[id[0]] = r < rows ? (sycl::half)(rad_load_f32(xb, xdt, r * xrs + c) / rad_load_f32(db, ddt, p * n + c))
: (sycl::half)0.0f;
});
});
if (rc < 0) return rc;
/* batch b = p * ngrp + gi: A = in16[p] + gi * 128 (row stride n), B = T[p][gi] [128][128] */
for (int64_t p = 0; p < P && rc >= 0; ++p)
rc = xmx_f16_batched(*q, in16 + p * mp * n, n, kG, (const sycl::half*)tb + p * ngrp * kG * kG, kG,
kG * kG, yv + p * ngrp * mp * kG, mp * kG, rows, kG, kG, ngrp);
if (rc < 0) return rc;
return submit([&] {
q->parallel_for(sycl::nd_range<1>{(size_t)(rows * P * ngrp) * kG, kG}, [=](sycl::nd_item<1> it) {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t gi = gid % ngrp, p = (gid / ngrp) % P, r = gid / (ngrp * P);
const int j = (int)it.get_local_id(0);
const float y = yv[((p * ngrp + gi) * mp + r) * kG + j];
const float amax = sycl::reduce_over_group(it.get_group(), sycl::fabs(y), sycl::maximum<float>());
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
const float inv = 1.0f / scl;
float v = y * inv;
if (v > qmax) v = qmax;
if (v < -qmax) v = -qmax;
if (i8) v = sycl::rint(v);
rad_store_f32(qb, qdt, r * qrs + (p * n + gi * kG + j) * qcs, v);
if (j == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl);
});
});
}
if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16)) {
/* The fast form. A work-group a (part, group) and up to kRows rows; sub-group s takes the
* outputs j = 16 s .. 16 s + 15, and for each its lanes read row j of T in one coalesced
+115 -5
View File
@@ -248,6 +248,111 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
constexpr int kSubs = (int)(kWg / kSub);
auto a16 = [](const void* p) { return rad_ptr_aligned(p, 16); };
const bool vec = n <= kRowMax && n % g == 0 && g == 8 * kSub && x.dtype == RAD_BF16 && x.pitch % 8 == 0 &&
a16(x.base) && (gw.dtype == RAD_BF16 || gw.dtype == RAD_F32) && gw.stride == 1 && a16(gw.base) &&
(!has_res || (res.dtype == RAD_BF16 && res.pitch % 8 == 0 && a16(res.base))) &&
(!has_ob || (ob.dtype == RAD_BF16 && ob.pitch % 8 == 0 && a16(ob.base))) &&
Q->dtype == RAD_F8E4M3 && qv.pitch % 8 == 0 && rad_ptr_aligned(qv.base, 8);
if (vec) {
const char* gbase = gw.base;
const uint32_t gdt = gw.dtype;
/* The bf16 rows of the form below, 8 values a 16-byte load or store: x, the residual and
* the gain (bf16 or f32) in one read each, the sum and its squares formed as there; a group of 128 is
* a sub-group's, 8 contiguous values a lane, and its codes are stored 8 bytes a lane. The
* same arithmetic and order as the general form -- each value's, each group's amax and
* scale, the sum of squares -- without its per-element loads, which were half its time. */
return submit([&] {
q->submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> sv_(sycl::range<1>((size_t)n), h);
sycl::local_accessor<float, 1> sq(sycl::range<1>((size_t)n), h);
sycl::local_accessor<float, 1> wl(sycl::range<1>((size_t)n), h);
h.parallel_for(sycl::nd_range<1>{(size_t)rows * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
using V8 = sycl::vec<uint16_t, 8>;
const int64_t r = (int64_t)it.get_group(0);
const int t = (int)it.get_local_id(0);
auto grp = it.get_group();
constexpr int kPer = (int)(kRowMax / 8 / kWg);
const uint16_t* xr = (const uint16_t*)x.base + r * x.pitch;
uint16_t* rr_ = has_res ? (uint16_t*)res.base + r * res.pitch : nullptr;
const bool wf = gdt == RAD_F32;
V8 xv[kPer], rv[kPer];
sycl::vec<float, 8> wv[kPer];
#pragma unroll
for (int j = 0; j < kPer; ++j) {
const int64_t c = (t + (int64_t)j * kWg) * 8;
const int64_t cc = c < n ? c : 0;
xv[j] = *(const V8*)(xr + cc);
rv[j] = has_res ? *(const V8*)(rr_ + cc) : V8(0);
if (wf) {
const sycl::vec<float, 4> w0 = *(const sycl::vec<float, 4>*)((const float*)gbase + cc);
const sycl::vec<float, 4> w1 = *(const sycl::vec<float, 4>*)((const float*)gbase + cc + 4);
for (int u = 0; u < 4; ++u) { wv[j][u] = w0[u]; wv[j][u + 4] = w1[u]; }
} else {
const V8 w8 = *(const V8*)((const uint16_t*)gbase + cc);
for (int u = 0; u < 8; ++u) wv[j][u] = rad_bf16_to_f32(w8[u]);
}
}
#pragma unroll
for (int j = 0; j < kPer; ++j) {
const int64_t c = (t + (int64_t)j * kWg) * 8;
if (c >= n) continue;
V8 so;
for (int u = 0; u < 8; ++u) {
float v = rad_bf16_to_f32(xv[j][u]);
if (has_res) {
const float s0 = v + rad_bf16_to_f32(rv[j][u]);
so[u] = rad_f32_to_bf16(s0);
v = rad_bf16_to_f32(so[u]); /* what reading it back gives */
}
sv_[c + u] = v;
sq[c + u] = v * v;
wl[c + u] = wv[j][u] + wadd;
}
if (has_res) *(V8*)(rr_ + c) = so;
}
sycl::group_barrier(grp);
const sycl::sub_group sg = it.get_sub_group();
const int lane = (int)sg.get_local_linear_id();
float ss = 0.0f;
if (sg.get_group_linear_id() == 0) {
for (int64_t i = 0; i < n; i += kSub) {
const float v = sq[i + lane];
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
}
}
ss = sycl::group_broadcast(grp, ss, 0);
const float rs = 1.0f / sycl::sqrt(ss / (float)n + eps);
const int sgi = (int)sg.get_group_linear_id();
for (int64_t gi = sgi; gi < ngrp; gi += kSubs) {
const int64_t i0 = gi * g + lane * 8;
float c[8];
float amax = 0.0f;
for (int u = 0; u < 8; ++u) {
c[u] = rad_bf16_to_f32(rad_f32_to_bf16(sv_[i0 + u] * rs * wl[i0 + u]));
amax = sycl::fmax(amax, sycl::fabs(c[u]));
}
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
const float sc = amax > 0.0f ? amax * (1.0f / 448.0f) : 1.0f;
const float inv = 1.0f / sc;
sycl::vec<uint8_t, 8> qb;
V8 ov;
for (int u = 0; u < 8; ++u) {
float cc = c[u] * inv;
if (cc > 448.0f) cc = 448.0f;
if (cc < -448.0f) cc = -448.0f;
qb[u] = rad_f32_to_fp8e4m3(cc);
ov[u] = rad_f32_to_bf16(c[u]);
}
*(sycl::vec<uint8_t, 8>*)((uint8_t*)qv.base + r * qv.pitch + i0) = qb;
if (has_ob) *(V8*)((uint16_t*)ob.base + r * ob.pitch + i0) = ov;
if (lane == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
}
});
});
});
}
if (n <= kRowMax) {
/* A row up to kRowMax in local memory: the (narrowed) residual sum and its squares are
* formed once from one read of x and the residual, the sum stored once; sub-group 0 adds
@@ -269,16 +374,21 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
* knows), a memory round trip an element. */
constexpr int kPer = (int)(kRowMax / kWg);
float xv[kPer], rv[kPer], wv[kPer];
/* fully unrolled and predicated (no early exit), so every load is in flight
* before the first is used */
#pragma unroll
for (int j = 0; j < kPer; ++j) {
const int64_t i = t + (int64_t)j * kWg;
if (i >= n) break;
xv[j] = rad_load_f32(x.base, x.dtype, r * x.pitch + i);
rv[j] = has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + i) : 0.0f;
wv[j] = rad_load_f32(gw.base, gw.dtype, i * gw.stride) + wadd;
const bool ok = i < n;
const int64_t ii = ok ? i : 0;
xv[j] = rad_load_f32(x.base, x.dtype, r * x.pitch + ii);
rv[j] = has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + ii) : 0.0f;
wv[j] = rad_load_f32(gw.base, gw.dtype, ii * gw.stride) + wadd;
}
#pragma unroll
for (int j = 0; j < kPer; ++j) {
const int64_t i = t + (int64_t)j * kWg;
if (i >= n) break;
if (i >= n) continue;
float v = xv[j];
if (has_res) {
const float s0 = v + rv[j];
+1
View File
@@ -71,6 +71,7 @@ int gemm_nt_q_m2(const RadArgs* a, RadStream s);
int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream s);
int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream s);
int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream s);
int givens_dense_quant(const RadArgs* a, RadStream s);
int gemm_nt_bias(const RadArgs* a, RadStream s);
int dflash_conv(const RadArgs* a, RadStream s);