diff --git a/abi/rad_abi_layout_rows.h b/abi/rad_abi_layout_rows.h index 4aa87338..ea2aa138 100644 --- a/abi/rad_abi_layout_rows.h +++ b/abi/rad_abi_layout_rows.h @@ -134,6 +134,7 @@ ABI_FIELD(RadBuildCtx, calib_layer_hi) ABI_FIELD(RadBuildCtx, calib_tokens) ABI_FIELD(RadBuildCtx, gen_max_pixels) ABI_FIELD(RadBuildCtx, gen_max_refs) +ABI_FIELD(RadBuildCtx, plain_norm) ABI_STRUCT(RadAudioGenBatch) ABI_FIELD(RadAudioGenBatch, n_lane) ABI_FIELD(RadAudioGenBatch, lane) diff --git a/abi/rad_builder.h b/abi/rad_builder.h index 08ae1c62..8df163db 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -295,6 +295,16 @@ typedef struct RadBuildCtx { * fixed buffers ignores them, and the core says so at startup. */ int64_t gen_max_pixels; int64_t gen_max_refs; + /* ---- appended in the SYCL fork (the plain block norm) */ + /* Nonzero: where no reader of a block norm's output takes its E4M3 code pair (every reader a + * 4-bit or plain bf16 projection, which reads the bf16 plane), the plugin may run the norm as + * `add` (the residual stored first, as `rmsnorm_quant_fp8` stores it) then `rmsnorm`: the same + * bf16 values, without codes nobody reads. The engine sets it on the SYCL backend, whose + * quantising norm is bound by libref's ordered sum of squares while its `rmsnorm` is a fast + * row (docs/SYCL_PROGRESS.md); 0 elsewhere, so other backends declare what they did. + * + * Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */ + int plain_norm; } RadBuildCtx; /* ================================================================== control vectors */ diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 6d68e746..78fc0d46 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -705,6 +705,9 @@ struct Geom { /* RAD_ATTN_FP8_* bits: the attention products accepted in e4m3 (RadBuildCtx describes it). * decl_attn passes it on as `attn_paged`'s `prefill_fp8`. */ int attn_prefill_fp8 = 0; + /* RadBuildCtx::plain_norm: a block norm none of whose readers takes codes may run as `add` + + * `rmsnorm` (rad_fp8.h, NormQuantFP8::x_only). */ + int plain_norm = 0; float eps = 1e-6f; /* GemmaRMSNorm's `1 +`. Qwen3.5, Qwen3-Next and Gemma normalise with `x_hat * (1 + w)` and * store `w` near 0.1, so reading it as a plain gain shrinks every normed activation 5-10x. An @@ -977,6 +980,7 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c, g.wire_exact= c->tp_wire_lossy ? 0 : 1; g.wire_min_bytes = c->tp_wire_min_bytes; g.attn_prefill_fp8 = c->attn_prefill_fp8; + g.plain_norm = c->plain_norm; g.eps = m->rms_eps; g.theta = m->rope_theta; g.rope_scale= 1.0f; /* yarn_from reads the model's rotary scaling */ @@ -1447,6 +1451,22 @@ inline const RadKVGroupBatch* kv_batch(const RadBatch* b, rad_kvgroup group) { } /* ================================================================== calibration in slices */ + +/* The draft head's rows: the vocabulary's first RADIANCE_MTP_DRAFT_VOCAB ids (a prefix shortlist, + * rounded down to whole 16-row tiles) or all of it. Byte-level BPE vocabularies number their + * merges by frequency, so a prefix keeps the likely drafts and the head reads that much less of + * its weight a draft round; a draft outside it is never proposed, which costs acceptance only -- + * the verify still runs the full head. */ +inline int64_t mtp_draft_rows(int64_t n_vocab_all) { + static const long long want = [] { + const char* s = std::getenv("RADIANCE_MTP_DRAFT_VOCAB"); + return s ? std::atoll(s) : 0LL; + }(); + if (want <= 0 || want >= n_vocab_all) return n_vocab_all; + const int64_t r = (int64_t)want / 16 * 16; + return r > 0 ? r : n_vocab_all; +} + /* The layers [lo, hi) of n a calibration taps: the environment variable `var` as "lo:hi", else * all of them. A large model's Grams do not fit beside its weights at once, so it is calibrated * in slices of layers, the corpus pushed through once a slice. */ diff --git a/arch/common/rad_block_attn_gated_fp8.h b/arch/common/rad_block_attn_gated_fp8.h index ca9365d4..a3e28966 100644 --- a/arch/common/rad_block_attn_gated_fp8.h +++ b/arch/common/rad_block_attn_gated_fp8.h @@ -260,7 +260,15 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int w_k_norm = decl_w(b, nm.f("blk.%d.attn_k_norm.weight", l), RAD_F32, {g.head_dim}, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); - /* One norm+quantise op feeds all three input projections (rad_fp8.h: NormQuantFP8). */ + /* One norm+quantise op feeds all three input projections (rad_fp8.h: NormQuantFP8). Where + * none of them reads the code pair, the plain form (NormQuantFP8::x_only): the projections are + * probed for it first, and their declare() does not map them again. */ + if (!ext_in && g.plain_norm && ar_in == kArNone && !g.attn_zero) { + RAD_ARCH_TRY(qg.probe(b, nm, g, nm.f("blk.%d.attn_qg", l), { src.qg })); + RAD_ARCH_TRY(kp.probe(b, nm, g, nm.f("blk.%d.attn_k", l), { src.k })); + RAD_ARCH_TRY(vp.probe(b, nm, g, nm.f("blk.%d.attn_v", l), { src.v })); + nq_h.x_only = qg.codes_free && kp.codes_free && vp.codes_free; + } if (!ext_in) RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd, fold ? w.x : 0, 0, ar_in)); diff --git a/arch/common/rad_block_gdn_fp8.h b/arch/common/rad_block_gdn_fp8.h index 5fa76c15..7ed4d9f5 100644 --- a/arch/common/rad_block_gdn_fp8.h +++ b/arch/common/rad_block_gdn_fp8.h @@ -233,7 +233,12 @@ inline int GdnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); /* Norm and quantise in one op (rad_fp8.h: NormQuantFP8); the qkv|z and a|b projections read - * it. */ + * it. a|b is a plain bf16 GEMM over the bf16 plane, so where qkv|z does not read the code pair + * the norm takes its plain form (NormQuantFP8::x_only; qkv|z probed first). */ + if (!ext_in && g.plain_norm && ar_in == kArNone) { + RAD_ARCH_TRY(in.probe(b, nm, g, nm.f("blk.%d.ssm_inz", l), { s.in_qkv, s.in_z })); + nq_h.x_only = in.codes_free; + } if (!ext_in) RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd, fold ? w.x : 0, 0, ar_in)); diff --git a/arch/common/rad_block_mlp_fp8.h b/arch/common/rad_block_mlp_fp8.h index 8c7b4659..82ad42a9 100644 --- a/arch/common/rad_block_mlp_fp8.h +++ b/arch/common/rad_block_mlp_fp8.h @@ -106,7 +106,12 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l)); /* Norm, quantise and residual add in one op (rad_fp8.h: NormQuantFP8). Folded, it adds the - * previous block's delta from `h` into the stream; unfolded it reads the stream directly. */ + * previous block's delta from `h` into the stream; unfolded it reads the stream directly. The + * plain form where gate|up does not read the code pair (NormQuantFP8::x_only; probed first). */ + if (g.plain_norm && ar_in == kArNone) { + RAD_ARCH_TRY(gate_up.probe(b, nm, g, nm.f("blk.%d.ffn_gate_up", l), { src.gate, src.up })); + nq_h.x_only = gate_up.codes_free; + } RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd, fold ? w.x : 0, 0, ar_in)); RAD_ARCH_TRY(gate_up.declare(b, nm, g, nm.f("blk.%d.ffn_gate_up", l), diff --git a/arch/common/rad_block_mtp_fp8.h b/arch/common/rad_block_mtp_fp8.h index c571d451..3082a47a 100644 --- a/arch/common/rad_block_mtp_fp8.h +++ b/arch/common/rad_block_mtp_fp8.h @@ -329,14 +329,14 @@ inline int MtpBlockFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, op_dlogits = rw(b, RAD_OP(b, "gemm_nt_q", RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), - RAD_INT("N", g.n_vocab_all), RAD_INT("K", g.n_embd), + RAD_INT("N", mtp_draft_rows(g.n_vocab_all)), RAD_INT("K", g.n_embd), RAD_INT("group", MTP_W2_GROUP), RAD_STR("dtype", "w2a8")), RAD_WEIGHTS(w_dh, w_dhs)), {w.dq, w.ds, w.dsum}, {w.dlog}); op_dtopk = rw(b, RAD_OP(b, "row_topk", RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), - RAD_INT("N", g.n_vocab_all), RAD_INT("R", 1), + RAD_INT("N", mtp_draft_rows(g.n_vocab_all)), RAD_INT("R", 1), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), {w.dlog}, {w.didx, w.dval}); diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index bf470232..78590a6a 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -382,6 +382,13 @@ struct NormQuantFP8 { rad_op op_arn6 = 0; /* The ops write had_quant_act_i8g's rotated int8 codes into the int8 pair (ActFP8::q8_rot). */ bool hi8 = false; + /* Set by the block before declare when Geom::plain_norm is on, no all-reduce is fused into + * this norm, and no reader takes `a`'s code pair (LinearFP8::codes_free): the norm runs as + * `add` (`op_add`, the residual stored before the variance reads it, as rmsnorm_quant_fp8 + * stores it) then `rmsnorm` into `a.x` -- rmsnorm_quant_fp8's residual and bf16 output, + * without the codes. */ + bool x_only = false; + rad_op op_add = 0; Geom gm{}; /* The row width, for turning a row slice into element offsets. */ int64_t n = 0; @@ -418,6 +425,24 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain, "linears are bf16; its blocks take a prepared input instead\n"); return RAD_E_UNSUPPORTED; } + if (x_only) { + if (fuse_ar != kArNone || !src || !a.x || !w || n <= 0) return RAD_E_INVAL; + const int64_t rows = max_rows > 0 ? max_rows : g.max_tok; + if (res) { + op_add = rw(b, RAD_OP(b, "add", + RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n), + RAD_STR("dtype", g.dtype)), + RAD_NOWEIGHTS), + { src, res }, { res }); + if (!op_add) return RAD_E_INVAL; + } + op = rw(b, RAD_OP(b, "rmsnorm", + RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n), RAD_F64("eps", g.eps), + RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w)), + { res ? res : src }, { a.x }); + return op ? RAD_OK : RAD_E_INVAL; + } /* Rotated int8 codes (ActFP8::q8_rot) come from the `_had_quant_i8g` twins, into the int8 * pair; the model checked they are declared before feeding. Otherwise E4M3, as always. */ hi8 = a.q8_fed && a.q8_rot; @@ -493,6 +518,14 @@ inline void NormQuantFP8::step_plain(RadCtx* c, int64_t T, int64_t r0, int64_t r * different summation order would run. */ inline void NormQuantFP8::step(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const { if (rows <= 0) rows = T - r0; + if (x_only) { + if (op_add) + RAD_ISSUE_N(c, op_add, rows, brow_slice(src, r0, rows, n), brow_slice(res, r0, rows, n), + brow_slice(res, r0, rows, n)); + RAD_ISSUE_N(c, op, rows, brow_slice(res ? res : src, r0, rows, n), RAD_W(w), + brow_slice(a.x, r0, rows, n)); + return; + } RAD_ISSUE_N(c, pick(rows), rows, brow_slice(src, r0, rows, n), res ? brow_slice(res, r0, rows, n) : RAD_NONE, RAD_W(w), brow_slice(hi8 ? a.q8 : a.q, r0, rows, n), @@ -777,6 +810,11 @@ struct LinearFP8 { * declared. A producer writes the bf16 plane only when asked, so the block must ask for it when * this is set; otherwise the linear reads unwritten arena bytes. */ bool reads_x = false; + /* Whether it reads the bf16 plane alone and never the producer's code pair: a 4-bit weight + * (cast or quantised from the plane by its own op) or a plain bf16 one. Known from probe(); + * a block norm all of whose readers are code-free may skip the quantiser + * (NormQuantFP8::x_only). */ + bool codes_free = false; bool mapped = false; /* probe() made the name map; declare() does not make it again */ /* The stored weight this linear's views take when it is not `.weight`: a second linear * over one container entry, e.g. a whole projection beside its tensor-parallel slice. Set @@ -1082,6 +1120,7 @@ inline int LinearFP8::probe(RadBuilder* b, Names& nm, const Geom& g, const char* const bool rotated = known && (linear_is_paro_mxfp4(enc) || linear_is_paro_w5(enc) || linear_is_paro_m2xfp4(enc)); reads_x = g.w_bf16 || plain || probed_i8 || rotated || (known && linear_is_w4(enc)); + codes_free = plain || (known && linear_is_w4(enc)); return RAD_OK; } diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index b9d80009..da6d1fcc 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -1166,7 +1166,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c m.b_mtp_dq = decl_b(b, nm.f("mtp_dq"), RAD_I8, {g.max_logit_rows, g.n_embd}); m.b_mtp_ds = decl_b(b, nm.f("mtp_ds"), RAD_F32, {g.max_logit_rows, 1}); m.b_mtp_dsum = decl_b(b, nm.f("mtp_dsum"), RAD_F32, {g.n_embd / 128, dmpad}); - m.b_mtp_dlog = decl_b(b, nm.f("mtp_dlog"), g.act_dtype, {g.max_logit_rows, g.n_vocab_all}); + m.b_mtp_dlog = decl_b(b, nm.f("mtp_dlog"), g.act_dtype, {g.max_logit_rows, mtp_draft_rows(g.n_vocab_all)}); /* A draft round's proposal comes out of row_topk here, never through the sampler's * token buffer. */ m.b_mtp_didx = decl_b(b, nm.f("mtp_didx"), RAD_I32, {g.max_logit_rows, 1}); diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index b16ea4ae..5b2db9b2 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -996,6 +996,9 @@ RadBuildCtx Engine::build_ctx(int rank, int64_t max_tok) const { bc.gen_max_refs = cfg_.image_gen_max_refs; bc.state_dtype = cfg_.state_cache_dtype == "f16" ? RAD_F16 : 0; bc.draft_coupling = cfg_.draft_coupling ? 1 : 0; + /* The SYCL backend's quantising norm runs libref's ordered sum of squares; its plain norm is a + * fast row (RadBuildCtx::plain_norm). */ + bc.plain_norm = std::strcmp(device_backend_name(), "sycl") == 0 ? 1 : 0; bc.tp_wire_min_bytes = cfg_.tp_wire_min_kb * 1024; bc.scope = ""; bc.max_enc_patches = cfg_.mm_max_patches > 0 ? cfg_.mm_max_patches : 0; diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index c95d67bf..77e1355c 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -143,6 +143,9 @@ int gdn_chunk_scan_dpas(sycl::queue& q, const uint16_t* qp, int64_t qs0, int64_t int64_t bs1, uint16_t* op, int64_t os0, int64_t os1, const float* h0, const int64_t* h0s, float* ht, const int64_t* hts, const RadTensor* cu, const char* si, uint32_t sidt, int64_t sw, int64_t T, int64_t N, int64_t H, int64_t Hg, float scale); +int gdn_kkt_solve_dpas(sycl::queue& q, const uint16_t* kp, int64_t ks0, int64_t ks1, const float* g, int64_t gs0, + int64_t gs1, const float* beta, int64_t bs0, int64_t bs1, uint16_t* Ap, int64_t As0, + int64_t As1, const RadTensor* cu, int64_t T, int64_t N, int64_t H, int64_t Hg); /* ------------------------------------------------------------------ gdn_conv_prep */ int gdn_conv_prep(const RadArgs* a, RadStream st) { @@ -478,6 +481,15 @@ int gdn_kkt_solve(const RadArgs* a, RadStream st) { const IV cuv = iv_of(cu); sycl::queue* qu = queue_of(st); if (!qu) return RAD_E_DEVICE; + /* The DPAS form (xe2_gdn_esimd.cpp) for the engine's operands: tolerance-held, so not under + * XE2_EXACT=1; it refuses a layout its 2D loads cannot take, and the exact kernels run. */ + if (K == 128 && chunk == 64 && !exact_only() && !std::getenv("XE2_KKT_SERIAL") && ks.dt == RAD_BF16 && + bs.dt == RAD_F32 && gs.dt == RAD_F32 && As.dt == RAD_BF16 && kv.s2 == 1 && Av.s2 == 1) { + const int rc = gdn_kkt_solve_dpas(*qu, (const uint16_t*)ks.p, kv.s0, kv.s1, (const float*)gs.p, gv.s0, gv.s1, + (const float*)bs.p, bv.s0, bv.s1, (uint16_t*)As.p, Av.s0, Av.s1, cu, T, N, + H, Hg); + if (rc != RAD_E_UNSUPPORTED) return rc; + } if (chunk == 64 && K <= 128 && !std::getenv("XE2_KKT_SERIAL")) { /* A work-group a (sequence, v head, chunk): chunks are independent here. The chunk's k rows * (bf16, exact) in local memory; first every row term beta_i (k_i . k_j) e^{g_i - g_j}, diff --git a/libxe2/xe2_gdn_esimd.cpp b/libxe2/xe2_gdn_esimd.cpp index 86210bf6..f9275a3a 100644 --- a/libxe2/xe2_gdn_esimd.cpp +++ b/libxe2/xe2_gdn_esimd.cpp @@ -423,6 +423,183 @@ int launch_scan(sycl::queue& q, const ScanArgs& a, const char* ws) { }); } +/* ---------------------------------------------------------------- gdn_kkt_solve on DPAS */ +struct KktArgs { + const uint16_t* k; + int64_t ks0, ks1; + const float *g, *beta; + int64_t gs0, gs1, bs0, bs1; + uint16_t* A; + int64_t As0, As1; + const char* cu; /* base and element stride, as ScanArgs's */ + uint32_t cudt; + int64_t cus; + int64_t T, N, H, Hg; + int64_t cu_at(int64_t i) const { return ld_index(cudt, cu, i * cus); } +}; + +/* C = A B over 16 x 16 f32 blocks, row-major, each row's sum over l in order. */ +inline esimd::simd mm16(esimd::simd A, esimd::simd B) SYCL_ESIMD_FUNCTION { + using namespace esimd; + simd C; +#pragma unroll + for (int i = 0; i < 16; ++i) { + simd acc = 0.0f; +#pragma unroll + for (int l = 0; l < 16; ++l) acc += (float)A[16 * i + l] * B.template select<16, 1>(16 * l); + C.template select<16, 1>(16 * i) = acc; + } + return C; +} + +/* The local-memory offset of X block (I, J), J <= I: 1 KiB blocks in row order. */ +inline uint32_t xoff(int I, int J) { return (uint32_t)((I * (I + 1) / 2 + J) * 1024); } + +/* A work-group of 4 threads a (sequence, chunk, v head), thread t the rows 16t..16t+15: + * M[i][j] = (beta_i (k_i . k_j)) e^{g_i - g_j}, j < i: k_i . k_j on DPAS (bf16 k, exact + * products, f32 sums), the factors in f32 + * X = (I + M)^{-1} by 16 x 16 blocks: the diagonal block by forward substitution in registers, + * then X_tJ = -X_tt sum_{K=J}^{t-1} M_tK X_KJ, a level (t - J) a barrier, the blocks + * passed through local memory -- all in f32 + * Not libref's order (it substitutes row by row over the whole chunk): held to gdn_kkt_solve's + * tolerance; XE2_EXACT=1 keeps the exact kernel. */ +int launch_kkt(sycl::queue& q, const KktArgs& a, int64_t C) { + syclex::properties props{ intelex::grf_size<256> }; + /* local memory: the X blocks (I, J), J <= I, at I (I + 1) / 2 + J, 1 KiB each */ + constexpr uint32_t kBlkB = 16 * 16 * 4; + return submit([&] { + q.parallel_for(sycl::nd_range<1>{ (size_t)(a.N * C * a.H) * 4, 4 }, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { + using namespace esimd; + slm_init<10 * kBlkB>(); + const int t = (int)it.get_local_id(0); + const int64_t wg = (int64_t)it.get_group(0); + const int64_t h = wg % a.H, c = (wg / a.H) % C, n = wg / (a.H * C); + const int64_t bos = a.cu_at(n), slen = a.cu_at(n + 1) - bos; + const int64_t c0 = c * CK; + if (c0 >= slen) return; /* uniform over the work-group */ + const int L = (int)(slen - c0 < CK ? slen - c0 : CK); + const int64_t tok0 = bos + c0; + const int64_t hk = h / (a.H / a.Hg); + const uint16_t* kh = a.k + hk * a.ks1; + const unsigned TH = (unsigned)(a.T - 1), kp = (unsigned)(a.ks0 * 2 - 1); + const int r0 = 16 * t; + const simd lane(0, 1); + + /* g at the chunk's positions (past L, g_last), beta at the thread's rows */ + const float* gp = a.g + tok0 * a.gs0 + h * a.gs1; + const float* bp = a.beta + tok0 * a.bs0 + h * a.bs1; + const float glast = gp[(int64_t)(L - 1) * a.gs0]; + simd gj; +#pragma unroll + for (int s = 0; s < 4; ++s) { + const simd jj = lane + 16 * s; + simd gv = gather(gp, simd(jj) * (uint32_t)(a.gs0 * 4), jj < L); + gv.merge(simd(glast), jj >= L); + gj.template select<16, 1>(16 * s) = gv; + } + simd gi, bi; + { + const simd ii = lane + r0; + gi = gather(gp, simd(ii) * (uint32_t)(a.gs0 * 4), ii < L); + bi = gather(bp, simd(ii) * (uint32_t)(a.bs0 * 4), ii < L); + gi.merge(simd(glast), ii >= L); + bi.merge(simd(0.0f), ii >= L); + } + + /* M's blocks of the thread's rows, Mb[J] for J <= t: k_i . k_j on DPAS */ + simd Cc[4][2]; +#pragma unroll + for (int J = 0; J < 4; ++J) Cc[J][0] = Cc[J][1] = 0.0f; +#pragma unroll 1 + for (int ks = 0; ks < KK / 16; ++ks) { + simd a0 = load_2d(kh, 255u, TH, kp, ks * 16, (int)(tok0 + r0)); + simd a1 = load_2d(kh, 255u, TH, kp, ks * 16, (int)(tok0 + r0 + 8)); +#pragma unroll + for (int J = 0; J < 4; ++J) { + if (J > t) continue; + simd kb = load_2d((const uint32_t*)kh, 255u, TH, kp, ks * 8, + (int)(tok0 + 16 * J)); + Cc[J][0] = mma(Cc[J][0], kb, a0); + Cc[J][1] = mma(Cc[J][1], kb, a1); + } + } + simd Mb[4], Md; +#pragma unroll + for (int J = 0; J < 4; ++J) { + if (J > t) continue; + simd m; + m.template select<128, 1>(0) = Cc[J][0]; + m.template select<128, 1>(128) = Cc[J][1]; + const simd gjJ = gj.template select<16, 1>(16 * J); + const simd jc = lane + 16 * J; +#pragma unroll + for (int r = 0; r < 16; ++r) { + const int i = r0 + r; + const simd_mask<16> hide = (jc >= i) | (i >= L); + simd dg = (float)gi[r] - gjJ; + dg.merge(simd(0.0f), hide); /* keep exp finite where hidden */ + simd v = ((float)bi[r] * m.template select<16, 1>(16 * r)) * exp(dg); + v.merge(simd(0.0f), hide); + m.template select<16, 1>(16 * r) = v; + } + Mb[J] = m; + if (J == t) Md = m; + } + /* the diagonal block's inverse: row r = e_r - sum_{l < r} M[r][l] X[l] */ + simd Xd = 0.0f; +#pragma unroll + for (int r = 0; r < 16; ++r) { + simd row = 0.0f; + row[r] = 1.0f; +#pragma unroll + for (int l = 0; l < r; ++l) row -= (float)Md[16 * r + l] * Xd.template select<16, 1>(16 * l); + Xd.template select<16, 1>(16 * r) = row; + } + slm_block_store(xoff(t, t), Xd); + /* the off-diagonal blocks, a level a barrier */ +#pragma unroll + for (int lvl = 1; lvl < 4; ++lvl) { + barrier(); + if (t >= lvl) { + const int J = t - lvl; + simd S = 0.0f; +#pragma unroll + for (int K = 0; K < 4; ++K) { + if (K < J || K >= t) continue; + simd xkj = slm_block_load(xoff(K, J)); + S += mm16(Mb[K], xkj); + } + slm_block_store(xoff(t, J), mm16(Xd, S) * -1.0f); + } + } + barrier(); + /* the thread's rows of A: X_tJ for J <= t, zero right of the diagonal block */ + uint16_t* Ah = a.A + h * a.As1; + const unsigned Ap = (unsigned)(a.As0 * 2 - 1); +#pragma unroll + for (int J = 0; J < 4; ++J) { + simd x = 0.0f; + if (J <= t) x = slm_block_load(xoff(t, J)); + simd xb = bf16_of<256>(x); +#pragma unroll + for (int m = 0; m < 2; ++m) { + const int rb = r0 + 8 * m; + if (rb + 8 <= L) { + store_2d(Ah, 127u, TH, Ap, 16 * J, (int)(tok0 + rb), + simd(xb.template select<128, 1>(128 * m))); + } else { +#pragma unroll + for (int r = 0; r < 8; ++r) + if (rb + r < L) + block_store(Ah + (tok0 + rb + r) * a.As0 + 16 * J, + simd(xb.template select<16, 1>(128 * m + 16 * r))); + } + } + } + }); + }); +} + bool al64(const void* p) { return ((uintptr_t)p & 63u) == 0; } } // namespace @@ -468,4 +645,23 @@ int gdn_chunk_scan_dpas(sycl::queue& q, const uint16_t* qp, int64_t qs0, int64_t return launch_scan(q, a, ws); } +/* gdn_kkt_solve's DPAS form; RAD_E_UNSUPPORTED when the call is not one it serves. bf16 k + * ([T, Hg, 128]) and A ([T, H, 64]) with unit last strides, f32 g and beta ([T, H]). */ +int gdn_kkt_solve_dpas(sycl::queue& q, const uint16_t* kp, int64_t ks0, int64_t ks1, const float* g, int64_t gs0, + int64_t gs1, const float* beta, int64_t bs0, int64_t bs1, uint16_t* Ap, int64_t As0, + int64_t As1, const RadTensor* cu, int64_t T, int64_t N, int64_t H, int64_t Hg) { + if (!al64(kp) || (ks1 * 2) % 64 != 0 || (ks0 * 2) % 16 != 0 || ks0 * 2 < 256) return RAD_E_UNSUPPORTED; + if (!al64(Ap) || (As1 * 2) % 64 != 0 || (As0 * 2) % 16 != 0 || As0 * 2 < 128) return RAD_E_UNSUPPORTED; + if (T >= (int64_t)1 << 24 || H % Hg != 0) return RAD_E_UNSUPPORTED; + KktArgs a{}; + a.k = kp; a.ks0 = ks0; a.ks1 = ks1; + a.g = g; a.gs0 = gs0; a.gs1 = gs1; a.beta = beta; a.bs0 = bs0; a.bs1 = bs1; + a.A = Ap; a.As0 = As0; a.As1 = As1; + const Lin cl = Lin::of(cu); + if (!cl.contig && cl.rank != 1) return RAD_E_UNSUPPORTED; + a.cu = cl.base; a.cudt = cl.dtype; a.cus = cl.contig ? 1 : cl.stride[0]; + a.T = T; a.N = N; a.H = H; a.Hg = Hg; + return launch_kkt(q, a, (T + CK - 1) / CK); +} + } // namespace xe2 diff --git a/libxe2/xe2_gemm.cpp b/libxe2/xe2_gemm.cpp index 2c619f48..db26363d 100644 --- a/libxe2/xe2_gemm.cpp +++ b/libxe2/xe2_gemm.cpp @@ -370,10 +370,11 @@ 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; - /* the XMX form reads 8 activation rows, the ones past M zero */ + /* the XMX form reads 8 activation rows, the ones past M zero; 9-16 rows (a verify step past + * depth 7) run it once a block of 8 -- the SIMT kernel took ~100 ms a call there */ 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 bool dpas = M <= 16 && K % 64 == 0 && !simt; + const int64_t mrows = dpas ? (M <= 8 ? 8 : 16) : 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; @@ -410,9 +411,17 @@ int logits_gemm_head_gemv(const RadArgs* a, RadStream st) { if (dpas) { /* head_dpas serves the E4M3 head and every form but the int8 one, which it leaves to the * ESIMD kernel (each reads the planes on the card; a form not its own returns at once) */ - if (int r = head_dpas(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm); r != RAD_OK) return r; - return head_i8_esimd(*q, mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : 8)), a16, rr, bb, yb, Y->dtype, N, K, yrs, - ycs, mm); + for (int b0 = 0; b0 < mm; b0 += 8) { + const int mb = mm - b0 < 8 ? mm - b0 : 8; + const sycl::half* ab0 = a16 + (int64_t)b0 * K; + char* yb0 = yb + rad_dtype_bytes(Y->dtype, (int64_t)b0 * yrs); + if (int r = head_dpas(*q, ab0, rr + b0, bb, yb0, Y->dtype, N, K, yrs, ycs, mb); r != RAD_OK) return r; + if (int r = head_i8_esimd(*q, mb == 1 ? 1 : (mb <= 2 ? 2 : (mb <= 4 ? 4 : 8)), ab0, rr + b0, bb, yb0, + Y->dtype, N, K, yrs, ycs, mb); + r != RAD_OK) + return r; + } + return RAD_OK; } 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); diff --git a/libxe2/xe2_gemm_w4.cpp b/libxe2/xe2_gemm_w4.cpp index 9b5a1dde..164f3b60 100644 --- a/libxe2/xe2_gemm_w4.cpp +++ b/libxe2/xe2_gemm_w4.cpp @@ -40,7 +40,7 @@ namespace xe2 { /* xe2_gemm_w4_esimd.cpp */ -int w4a16_esimd(sycl::queue& q, int rc, const sycl::half* a, int64_t ars, int mm, const uint32_t* ct, +int w4a16_esimd(sycl::queue& q, int rc, const void* a, bool a_bf16, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz, int64_t N, int64_t K, int splits, char* yb, uint32_t ydt, int64_t yrs); int w4a8i8_esimd(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad, const uint32_t* ct, const uint32_t* sz, int64_t M, int64_t N, int64_t K, char* yb, uint32_t ydt, @@ -394,8 +394,8 @@ int gemm_nt_q_w4a16(const RadArgs* a, RadStream st) { W4Call c; if (int rc = w4_parse(a, &c); rc != RAD_OK) return rc; /* A bf16 activation (arch/common since upstream 492d33c, which dropped the separate cast) is - * narrowed to f16 round-to-nearest-even into a workspace first: the cast it replaced, bit for - * bit. TODO: narrow in the kernel's activation load instead. */ + * narrowed to f16 round-to-nearest-even at the kernel's activation load: the cast it replaced, + * bit for bit. */ const bool a_bf16 = c.adt == RAD_BF16; if (c.kind != W4::F16 || !c.tl || (c.adt != RAD_F16 && !a_bf16) || c.M > 64 || c.ys_c != 1 || (c.ydt != RAD_BF16 && c.ydt != RAD_F32) || (c.ars * 2) % 16 || !rad_ptr_aligned(c.ab, 64) || @@ -403,29 +403,13 @@ int gemm_nt_q_w4a16(const RadArgs* a, RadStream st) { return gemm_nt_q_w4(a, st); sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; - const sycl::half* a16 = (const sycl::half*)c.ab; - int64_t ars = c.ars; - if (a_bf16) { - const int64_t M = c.M, K = c.K, src_rs = c.ars; - ars = (K + 31) / 32 * 32; - sycl::half* buf = (sycl::half*)workspace(*q, (size_t)((M * ars + 1) / 2), 9); - if (!buf) return RAD_E_DEVICE; - const uint16_t* src = (const uint16_t*)c.ab; - const int rc = submit([&] { - q->parallel_for(sycl::range<1>((size_t)(M * K)), [=](sycl::id<1> id) { - const int64_t i = (int64_t)id[0], m = i / K, k = i % K; - const uint32_t bits = (uint32_t)src[m * src_rs + k] << 16; - buf[m * ars + k] = sycl::half(sycl::bit_cast(bits)); - }); - }); - if (rc != RAD_OK) return rc; - a16 = buf; - } + const uint16_t* a16 = (const uint16_t*)c.ab; + const int64_t ars = c.ars; const int splits = w4_splits(c.G); for (int64_t m0 = 0; m0 < c.M; m0 += 8) { const int mm = (int)std::min(8, c.M - m0); const int rcount = mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : 8)); - const int rc = w4a16_esimd(*q, rcount, a16 + m0 * ars, ars, mm, (const uint32_t*)c.bb, (const uint32_t*)c.bsb, + const int rc = w4a16_esimd(*q, rcount, a16 + m0 * ars, a_bf16, ars, mm, (const uint32_t*)c.bb, (const uint32_t*)c.bsb, c.N, c.K, splits, c.yb + m0 * c.ys_r * (c.ydt == RAD_F32 ? 4 : 2), c.ydt, c.ys_r); if (rc != RAD_OK) return rc; } diff --git a/libxe2/xe2_gemm_w4_esimd.cpp b/libxe2/xe2_gemm_w4_esimd.cpp index a3106bea..759235b6 100644 --- a/libxe2/xe2_gemm_w4_esimd.cpp +++ b/libxe2/xe2_gemm_w4_esimd.cpp @@ -46,8 +46,17 @@ inline esimd::simd to_bf16(esimd::simd v) SYCL_ESIMD_FU * K 6144 35 (+ the reduction) -> 32, N 1024 10 (+) -> 7.4: the weight's bytes at the card's ~560 GB/s. */ constexpr int kMaxSplits = 32; -template -int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz, int64_t N, +/* bf16 codes to fp16, through f32 and rounded to nearest even: sycl::half(float), bit for bit */ +template +inline esimd::simd bf16_to_half(esimd::simd v) SYCL_ESIMD_FUNCTION { + using namespace esimd; + simd u = simd(v) << 16; + return simd(u.template bit_cast_view().read()); +} + +/* ABF: the activation is bf16, narrowed to fp16 at its load (the cast upstream dropped, fused). */ +template +int launch16(sycl::queue& q, const void* av, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz, int64_t N, int64_t K, int S, char* yb, uint32_t ydt, int64_t yrs) { const int64_t ntile = N / 16, G = K / 128; const int64_t per = G / S; @@ -61,6 +70,8 @@ int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t* const uint32_t* cb = ct + tile * (K / 8) * 16; const uint32_t* sb = sz + tile * G * 16; const unsigned sw = (unsigned)(K * 2 - 1), sh = (unsigned)(mm - 1), spt = (unsigned)(ars * 2 - 1); + const half* a = (const half*)av; + const uint16_t* ab = (const uint16_t*)av; simd tot = 0.0f; for (int64_t g = g0; g < g1; ++g) { simd cw; @@ -70,13 +81,23 @@ int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t* simd Ar; /* A2D: [pair of steps][step][row][16]; else [row][128] */ if constexpr (A2D) { #pragma unroll - for (int j2 = 0; j2 < 4; ++j2) - Ar.template select(j2 * RC * 32) = - load_2d(a, sw, sh, spt, (int)(g * 128 + j2 * 32), 0); + for (int j2 = 0; j2 < 4; ++j2) { + if constexpr (ABF) + Ar.template select(j2 * RC * 32) = bf16_to_half( + load_2d(ab, sw, sh, spt, (int)(g * 128 + j2 * 32), 0)); + else + Ar.template select(j2 * RC * 32) = + load_2d(a, sw, sh, spt, (int)(g * 128 + j2 * 32), 0); + } } else { #pragma unroll for (int m = 0; m < RC; ++m) { - if (m < mm) Ar.template select<128, 1>(m * 128) = block_load(a + m * ars + g * 128); + if (m < mm) { + if constexpr (ABF) + Ar.template select<128, 1>(m * 128) = bf16_to_half<128>(block_load(ab + m * ars + g * 128)); + else + Ar.template select<128, 1>(m * 128) = block_load(a + m * ars + g * 128); + } else Ar.template select<128, 1>(m * 128) = 0; } } @@ -244,15 +265,19 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo } // namespace -int w4a16_esimd(sycl::queue& q, int rc, const half* a, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz, - int64_t N, int64_t K, int splits, char* yb, uint32_t ydt, int64_t yrs) { +int w4a16_esimd(sycl::queue& q, int rc, const void* a, bool a_bf16, int64_t ars, int mm, const uint32_t* ct, + const uint32_t* sz, int64_t N, int64_t K, int splits, char* yb, uint32_t ydt, int64_t yrs) { if (splits < 1 || splits > kMaxSplits || (K / 128) % splits) return RAD_E_UNSUPPORTED; - switch (rc) { - case 1: return launch16<1, false>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); - case 2: return launch16<2, false>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); - case 4: return launch16<4, false>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); - default: return launch16<8, true>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); +#define XE2_W4_RC(ABF) \ + switch (rc) { \ + case 1: return launch16<1, false, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ + case 2: return launch16<2, false, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ + case 4: return launch16<4, false, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ + default: return launch16<8, true, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ } + if (a_bf16) XE2_W4_RC(true) + XE2_W4_RC(false) +#undef XE2_W4_RC } int w4a8i8_esimd(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad, diff --git a/libxe2/xe2_gemm_zp.cpp b/libxe2/xe2_gemm_zp.cpp index 1c7df28d..3526112c 100644 --- a/libxe2/xe2_gemm_zp.cpp +++ b/libxe2/xe2_gemm_zp.cpp @@ -92,14 +92,18 @@ int gemm_zp_layout(const RadParam* p, int n_p, int operand, const RadEncoding* e return RAD_E_UNSUPPORTED; const long long N = rad_param_geti(p, n_p, "N", 0), K = rad_param_geti(p, n_p, "K", 0); const long long group = rad_param_geti(p, n_p, "group", 0); - if (!zp_tiled(2, N, K, group)) return RAD_E_UNSUPPORTED; - if (planes[0].shape[0] != N || planes[0].shape[1] != K) return RAD_E_SHAPE; + /* An op may read a prefix of the rows (the MTP draft head's shortlist, rad_arch.h + * mtp_draft_rows): the stored form is the whole weight's, tile-major, so a prefix of + * whole tiles is a prefix of it. */ + const long long Nw = planes[0].shape[0]; + if (!zp_tiled(2, N, K, group) || !zp_tiled(2, Nw, K, group)) return RAD_E_UNSUPPORTED; + if (N > Nw || planes[0].shape[1] != K) return RAD_E_SHAPE; out->tag = kTagU2; out->dtype = RAD_U2; out->rank = 1; - out->shape[0] = N * K; + out->shape[0] = Nw * K; out->align = 0; - out->bytes = N * K / 4; + out->bytes = Nw * K / 4; return RAD_OK; } if (n < 2 || operand != 3) return RAD_E_UNSUPPORTED; diff --git a/libxe2/xe2_norm.cpp b/libxe2/xe2_norm.cpp index 2183cc3f..036ebe8b 100644 --- a/libxe2/xe2_norm.cpp +++ b/libxe2/xe2_norm.cpp @@ -162,6 +162,96 @@ int rmsnorm(const RadArgs* a, RadStream st) { if (rows <= 0 || y.rows < rows) return RAD_E_SHAPE; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + /* The fast rows (D13): each work-item's squares added in index order, the partials by + * reduce_over_group's tree -- not libref's order, held to rmsnorm's tolerance (bf16 4e-3, f32 + * 1e-6). libref's one chain is ~13 cycles an element on one work-item (~31 us of a 5120-wide + * decode norm's ~40); XE2_EXACT=1 keeps it (below). Rows up to 512 wide (the per-head q / k + * norms) take a sub-group each, wider ones a work-group. */ + if (!exact_only()) { + if (n <= 512) { + constexpr int64_t kRowsWg = (int64_t)kWg / kSub; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)((rows + kRowsWg - 1) / kRowsWg) * kWg, kWg}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int64_t r = (int64_t)it.get_group(0) * kRowsWg + (int64_t)sg.get_group_linear_id(); + if (r >= rows) return; /* uniform over the sub-group */ + const int l = (int)sg.get_local_linear_id(); + float ss = 0.0f; + for (int64_t i = l; i < n; i += kSub) { + const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i); + ss += v * v; + } + ss = sycl::reduce_over_group(sg, ss, sycl::plus()); + const float s = 1.0f / sycl::sqrt(ss / (float)n + eps); + for (int64_t i = l; i < n; i += kSub) { + const float gw = g.base ? rad_load_f32(g.base, g.dtype, i * g.stride) + wadd : 1.0f; + const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i); + rad_store_f32(y.base, y.dtype, r * y.pitch + i, v * s * gw * mult); + } + }); + }); + } + /* bf16 rows up to 8192 wide: a work-item eight elements by one 16-byte load, the row in + * registers once (the generic form below walks 20 dependent loads a work-item at n = 5120, + * ~25 us for six rows; this is one load a work-item). */ + const int64_t nv = n / 8, wgv = (nv + kSub - 1) / kSub * kSub; + if (x.dtype == RAD_BF16 && y.dtype == RAD_BF16 && n % 8 == 0 && wgv <= 1024 && x.pitch % 8 == 0 && + y.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(y.base, 16) && + (!g.base || ((g.dtype == RAD_F32 || g.dtype == RAD_BF16) && g.stride == 1))) { + using V8 = sycl::vec; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wgv), (size_t)wgv}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const int64_t r = (int64_t)it.get_group(0); + const int64_t t = (int64_t)it.get_local_id(0); + const bool live = t < nv; + float v[8]; + float ss = 0.0f; + if (live) { + const V8 u = *(const V8*)((const uint16_t*)x.base + r * x.pitch + t * 8); +#pragma unroll + for (int j = 0; j < 8; ++j) { + v[j] = sycl::bit_cast((uint32_t)u[j] << 16); + ss += v[j] * v[j]; + } + } + ss = sycl::reduce_over_group(it.get_group(), ss, sycl::plus()); + if (!live) return; + const float s = 1.0f / sycl::sqrt(ss / (float)n + eps); + V8 o; +#pragma unroll + for (int j = 0; j < 8; ++j) { + const int64_t i = t * 8 + j; + const float gw = !g.base ? 1.0f + : (g.dtype == RAD_F32 ? ((const float*)g.base)[i] + : sycl::bit_cast((uint32_t)((const uint16_t*)g.base)[i] << 16)) + wadd; + o[j] = rad_f32_to_bf16(v[j] * s * gw * mult); + } + *(V8*)((uint16_t*)y.base + r * y.pitch + t * 8) = o; + }); + }); + } + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const int64_t r = (int64_t)it.get_group(0); + const int t = (int)it.get_local_id(0); + float ss = 0.0f; + for (int64_t i = t; i < n; i += (int64_t)kWg) { + const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i); + ss += v * v; + } + ss = sycl::reduce_over_group(it.get_group(), ss, sycl::plus()); + const float s = 1.0f / sycl::sqrt(ss / (float)n + eps); + for (int64_t i = t; i < n; i += (int64_t)kWg) { + const float gw = g.base ? rad_load_f32(g.base, g.dtype, i * g.stride) + wadd : 1.0f; + const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i); + rad_store_f32(y.base, y.dtype, r * y.pitch + i, v * s * gw * mult); + } + }); + }); + } return submit([&] { syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { diff --git a/libxe2/xe2_rows.cpp b/libxe2/xe2_rows.cpp index cb03ee64..9a08fcbb 100644 --- a/libxe2/xe2_rows.cpp +++ b/libxe2/xe2_rows.cpp @@ -74,7 +74,9 @@ const RadKernelInfo kKernels[] = { .domain = RAD_DOMAIN_DEVICE, .launch = xe2::grid_embed }, { .name = "xe2_rmsnorm", .op = "rmsnorm", .family = "norm", - .computes = "RMS norm, one work-group a row, f32 work-group reduction", + .computes = "RMS norm, a sub-group a row up to 512 wide, else a work-group, squares by a tree " + "reduction (not libref's order: rmsnorm's tolerance). XE2_EXACT=1 runs libref's " + "ordered sum", .shape = "any n; rows may be padded", .dtypes = "float x, gain and y (bf16 in the recipes), f32 arithmetic", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::rmsnorm }, @@ -393,12 +395,17 @@ const RadKernelInfo kKernels[] = { .dtypes = "any float dtype; integer cu, slots and counts", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gdn_conv_update }, { .name = "xe2_gdn_kkt_solve", .op = "gdn_kkt_solve", .family = "gdn", - .computes = "(I + strict_lower(beta K K^T e^{g_i-g_j}))^-1 per chunk and v head, forward substitution; libref's arithmetic", + .computes = "(I + strict_lower(beta K K^T e^{g_i-g_j}))^-1 per chunk and v head: K K^T on DPAS, the " + "inverse by 16-row blocks in f32 (not libref's order: its tolerance) for head_k 128, " + "bf16 k and A; else forward substitution, libref's arithmetic. XE2_EXACT=1 runs the " + "exact kernel", .shape = "q, k, v as [T, heads, width]; conv width <= 17", .dtypes = "any float dtype; integer cu, slots and counts", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gdn_kkt_solve }, { .name = "xe2_gdn_chunk_scan", .op = "gdn_chunk_scan", .family = "gdn", - .computes = "the chunked delta-rule scan over a prefill chunk, state in and out; libref's arithmetic", + .computes = "the chunked delta-rule scan over a prefill chunk, state in and out: on DPAS with bf16 " + "operands for head_k = head_v = 128, chunk 64 and bf16/f32 operands (not libref's " + "order: its tolerance); else libref's arithmetic. XE2_EXACT=1 runs the exact kernel", .shape = "q, k, v as [T, heads, width]; conv width <= 17", .dtypes = "any float dtype; integer cu, slots and counts", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gdn_chunk_scan },