/* xe2_rows.cpp -- libxe2's plugin exports: identity and the kernel rows the engine selects from. * * libxe2 is radiance's kernel library for Intel Xe2 GPUs (the Arc Pro B65, Battlemage G31) through * SYCL. It defines no op schemas of its own: every op it serves is in the conventional vocabulary, * whose schemas libref fixes (docs/OPS.md). All rows are device-domain; an op libxe2 lacks has no * row here, and on the SYCL backend the engine stops at startup naming it (docs/SYCL_PORTING.md * ยง4.4). */ #include #include #include #include #include "rad_abi.h" #include "rad_abi_layout.h" #include "rad_plugin.h" #include "xe2_ops.h" namespace { const RadConstraint cRopeModes[] = { RAD_CIN("mode", "neox gptj mrope imrope axial") }; /* The GEMMs here read float weights only; a quantised dtype key is gemm_nt_q's. */ const RadConstraint cFloatDtype[] = { RAD_CIN("dtype", "bf16 f16 fp16 f32 fp32") }; /* bf16 operands, any M (xe2_gemm_nt_bias picks its GEMM by M). */ const RadConstraint cBf16[] = { RAD_CIN("dtype", "bf16") }; /* the fused attention prologue: bf16 heads of 128, 256 or 512 */ const RadConstraint cQnr[] = { RAD_CIN("dtype", "bf16"), RAD_CDIV("head_dim", 128), RAD_CLE("head_dim", 512) }; /* the gated prologue: the same heads, its dtype key is q_dtype */ const RadConstraint cQkg[] = { RAD_CIN("q_dtype", "bf16"), RAD_CDIV("head_dim", 128), RAD_CLE("head_dim", 512) }; /* The decode band of the skinny bf16 GEMV (xe2_gemv.cpp). */ const RadConstraint cGemvBf16[] = { RAD_CIN("dtype", "bf16"), RAD_CLE("M", 16) }; /* The prefill band of the bf16 XMX GEMM (xe2_gemm_xmx.cpp). */ const RadConstraint cXmxBf16[] = { RAD_CIN("dtype", "bf16"), RAD_CGE("M", 17) }; /* The block-scaled pairs xe2_gemm_q.cpp serves; the zero-point grids (w2a8, w4*_asym) and mxfp4 are * other rows. */ const RadConstraint cFp8Dtype[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16") }; const RadConstraint cFp8Gated[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16"), RAD_CLE("N", 57344) }; /* The zero-point grids over quant_act_i8's activation (xe2_gemm_zp.cpp). */ const RadConstraint cZpI8Dtype[] = { RAD_CIN("dtype", "w2a8 w4a8_asym") }; const RadConstraint cZpGemv[] = { RAD_CIN("dtype", "w2a8 w4a8_asym"), RAD_CLE("M", 16) }; /* The 4-bit zero-point grids of an int4 W4A16 release (xe2_gemm_w4.cpp): one stored form, so every * row of either dtype shares gemm_w4_layout. */ const RadConstraint cW4Dtype[] = { RAD_CIN("dtype", "w4a16_asym w4a8i8_asym") }; const RadConstraint cW4a16[] = { RAD_CIN("dtype", "w4a16_asym"), RAD_CLE("M", 64) }; const RadConstraint cW4a8i8[] = { RAD_CIN("dtype", "w4a8i8_asym") }; /* The M2XFP4 grid over givens_dense_quant's fp8 activation (xe2_gemm_m2.cpp). */ const RadConstraint cM2Dtype[] = { RAD_CIN("dtype", "m2xfp4") }; /* The fp8 head's decode band. */ const RadConstraint cHeadGemv[] = { RAD_CLE("M", 16) }; /* Its decode / verify band (xe2_gemv_m2.cpp). */ const RadConstraint cM2Gemv[] = { RAD_CIN("dtype", "m2xfp4"), RAD_CLE("M", 16) }; /* Its prefill band (bf16 XMX, xe2_gemv_m2.cpp). */ const RadConstraint cM2Xmx[] = { RAD_CIN("dtype", "m2xfp4"), RAD_CGE("M", 17) }; /* Operand descriptions for the gated prologue, for rad-kbench (libr4d's r4d_shapes.cpp: the same * shapes, so both libraries are drawn alike). A non-positive extent declines the whole list. */ RadOpdDesc opd(uint32_t dt, int64_t d0, int64_t d1 = 0, int64_t d2 = 0, int64_t d3 = 0) { RadOpdDesc d{}; d.dtype = dt; d.fill = RAD_FILL_NORMAL; d.idx_const = -1; const int64_t e[4] = { d0, d1, d2, d3 }; for (int i = 0; i < 4 && e[i] != 0; ++i) d.shape[d.rank++] = e[i]; return d; } RadOpdDesc opd_idx(int64_t d0, int64_t idx_max, uint32_t flags = 0) { RadOpdDesc d = opd((uint32_t)RAD_I32, d0); d.fill = RAD_FILL_INDEX; d.idx_max = idx_max; d.flags = flags; return d; } RadOpdDesc opd_none() { RadOpdDesc d{}; d.idx_const = -1; d.flags = RAD_OPD_F_ABSENT; return d; } template int pick(RadOpdDesc* out, int operand, const RadOpdDesc (&list)[N]) { if (!out) return RAD_E_INVAL; for (const RadOpdDesc& d : list) { if (d.flags & RAD_OPD_F_ABSENT) continue; if (d.rank == 0 || d.rank > RAD_MAX_RANK) return RAD_E_UNSUPPORTED; for (uint32_t i = 0; i < d.rank; ++i) if (d.shape[i] <= 0) return RAD_E_UNSUPPORTED; } if (operand < 0 || operand >= N) return RAD_E_SHAPE; *out = list[operand]; return RAD_OK; } uint32_t dt_act(const RadParam* p, int n_p, const char* key) { const char* s = nullptr; for (int i = 0; i < n_p; ++i) if (p[i].kind == RAD_P_STR && p[i].key && !std::strcmp(p[i].key, key)) s = p[i].sval; const uint32_t dt = s ? rad_dtype_parse(s) : (uint32_t)RAD_DT_INVALID; return dt == (uint32_t)RAD_DT_INVALID ? (uint32_t)RAD_BF16 : dt; } /* [q|gate] interleaved per head, k, the [rows, rot] cos/sin table (a position is a lookup into it, * so the draw is bounded by its rows), positions, f32 gains as every arch declares them, q_out, * k_out; no gate_out, as the gated attention block passes none. */ int shape_qk_norm_rope_gate(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { const int64_t M = rad_param_getdim(p, n_p, "M", 0), hd = rad_param_getdim(p, n_p, "head_dim", 0); const int64_t nq = rad_param_getdim(p, n_p, "n_head", 0), nkv = rad_param_getdim(p, n_p, "n_head_kv", 0); const int64_t rot = rad_param_getdim(p, n_p, "rot", 0); const uint32_t Q = dt_act(p, n_p, "q_dtype"); const uint32_t CS = rad_param_getdim(p, n_p, "cs_f32", 0) ? (uint32_t)RAD_F32 : (uint32_t)RAD_BF16; const RadOpdDesc l[] = { nq ? opd(Q, M, nq * 2 * hd) : opd_none(), opd(Q, M, nkv * hd), opd(CS, M, rot), opd_idx(M, M), nq ? opd((uint32_t)RAD_F32, hd) : opd_none(), opd((uint32_t)RAD_F32, hd), nq ? opd(Q, M, nq * hd) : opd_none(), opd(Q, M, nkv * hd), opd_none(), }; return pick(out, operand, l); } /* then kv_store's v, unique slots and the paged cache at kv_store's own shapes */ int shape_qk_norm_rope_gate_kv_store(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { if (operand < 9) return shape_qk_norm_rope_gate(p, n_p, operand, out); const int64_t M = rad_param_getdim(p, n_p, "M", 0), hd = rad_param_getdim(p, n_p, "head_dim", 0); const int64_t kvh = rad_param_getdim(p, n_p, "n_head_kv", 0), bs = rad_param_getdim(p, n_p, "block_size", 0); const uint32_t KV = dt_act(p, n_p, "kv_dtype"); if (M <= 0 || bs <= 0) return RAD_E_UNSUPPORTED; const int64_t nb = (M + bs - 1) / bs + 2; const RadOpdDesc l[] = { opd_none(), opd_none(), opd_none(), opd_none(), opd_none(), opd_none(), opd_none(), opd_none(), opd_none(), opd((uint32_t)RAD_BF16, M, kvh, hd), opd_idx(M, nb * bs, RAD_OPD_F_IDX_UNIQUE), opd(KV, nb, kvh, bs, 2 * hd), }; return pick(out, operand, l); } /* The chain xe2_qk_norm_rope_gate_kv_store replaces (libr4d's r4d_fuse.cpp, the same wiring): * qk_norm_rope_gate, then kv_store of its k_out and v. rad-kbench's fusion oracle runs both steps * on libxe2's own rows and compares the bytes. The plain prologue declares no chain, as libr4d's * does not: its q norm is an rmsnorm over T * n_head strided rows, which no step can express. * * Operands: [0] q_gate? [1] k [2] cos_sin [3] positions [4] q_w? [5] k_w [6] q_out? * [7] k_out [8] gate_out? [9] v [10] slot_mapping [11] kv_cache */ int fuse_qk_norm_rope_gate_kv_store(const RadParam* p, int n_p, uint64_t present, int step, RadFuseStep* out) { if (!out) return RAD_E_INVAL; *out = RadFuseStep{}; bool ok = true; auto mirror = [&](const char* key, bool req) { const RadParam* f = rad_param_find(p, n_p, key); if (!f) { ok = ok && !req; return; } if (out->n_p >= RAD_MAX_FUSE_PARAMS) { ok = false; return; } out->p[out->n_p++] = *f; }; auto opt = [present](int i) { return RAD_FUSE_HAS(present, i) ? (int16_t)i : (int16_t)RAD_FUSE_NONE; }; auto wire = [&](std::initializer_list from) { if (!ok) return (int)RAD_E_UNSUPPORTED; for (int16_t v : from) out->from[out->n_from++] = v; return (int)RAD_OK; }; switch (step) { case 0: out->op = "qk_norm_rope_gate"; for (const char* k : { "M", "head_dim", "n_head", "n_head_kv", "rot", "q_dtype" }) mirror(k, true); for (const char* k : { "eps", "wadd", "cs_f32", "pos_i64" }) mirror(k, false); return wire({ opt(0), 1, 2, 3, opt(4), 5, opt(6), 7, opt(8) }); case 1: out->op = "kv_store"; for (const char* k : { "M", "head_dim", "n_head_kv", "block_size", "kv_dtype" }) mirror(k, true); for (const char* k : { "k_scale", "v_scale" }) mirror(k, false); return wire({ 7, 9, 10, 11 }); /* k (k_out), v, slot_mapping, kv_cache */ default: return RAD_E_NOTFOUND; } } /* Elementwise rows of [M, n] (libr4d's r4d_shape_unary / r4d_shape_binary): the form callers * issue, and what the fusion oracle sizes a chain's temporaries from. */ int shape_unary(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { const int64_t M = rad_param_getdim(p, n_p, "M", 0), n = rad_param_getdim(p, n_p, "n", 0); const uint32_t D = dt_act(p, n_p, "dtype"); const RadOpdDesc l[] = { opd(D, M, n), opd(D, M, n) }; return pick(out, operand, l); } int shape_binary(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { const int64_t M = rad_param_getdim(p, n_p, "M", 0), n = rad_param_getdim(p, n_p, "n", 0); const uint32_t D = dt_act(p, n_p, "dtype"); const RadOpdDesc l[] = { opd(D, M, n), opd(D, M, n), opd(D, M, n) }; return pick(out, operand, l); } /* gate_quant_fp8: bf16 gate and x, whole 128-column scale groups (the out projection's grid). */ const RadConstraint cGateQuant[] = { RAD_CIN("dtype", "bf16"), RAD_CEQ("group", 128) }; /* gate, x, codes, the [M, n / group] scale plane and out_bf16, flat as libr4d draws them. */ int shape_gate_quant_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { const int64_t M = rad_param_getdim(p, n_p, "M", 0), n = rad_param_getdim(p, n_p, "n", 0); const int64_t g = rad_param_getdim(p, n_p, "group", 0); const uint32_t D = dt_act(p, n_p, "dtype"); if (g <= 0) return RAD_E_UNSUPPORTED; const RadOpdDesc l[] = { opd(D, M, n), opd(D, M, n), opd((uint32_t)RAD_F8E4M3, M, n), opd((uint32_t)RAD_F32, M, (n + g - 1) / g), opd(D, M, n), }; return pick(out, operand, l); } /* The chain xe2_gate_quant_fp8 replaces (libr4d's r4d_fuse_gate_quant_fp8): sigmoid into a bf16 * temporary, mul, quant_act_fp8 over the product; at act none, mul and quant_act_fp8. The product * goes to out_bf16 when passed, else a second temporary. * * Operands: [0] gate [1] x [2] q [3] scale [4] out_bf16? */ int fuse_gate_quant_fp8(const RadParam* p, int n_p, uint64_t present, int step, RadFuseStep* out) { if (!out) return RAD_E_INVAL; *out = RadFuseStep{}; const RadParam* a = rad_param_find(p, n_p, "act"); const char* act = a && a->kind == RAD_P_STR && a->sval ? a->sval : "sigmoid"; const bool none = !std::strcmp(act, "none"); if (!none && std::strcmp(act, "sigmoid")) return RAD_E_UNSUPPORTED; bool ok = true; auto mirror = [&](const char* key) { const RadParam* f = rad_param_find(p, n_p, key); if (!f || out->n_p >= RAD_MAX_FUSE_PARAMS) { ok = false; return; } out->p[out->n_p++] = *f; }; auto wire = [&](std::initializer_list from) { if (!ok) return (int)RAD_E_UNSUPPORTED; for (int16_t v : from) out->from[out->n_from++] = v; return (int)RAD_OK; }; const int16_t mid = RAD_FUSE_HAS(present, 4) ? (int16_t)4 : (int16_t)RAD_FUSE_TMP(1); const int s = none ? step + 1 : step; switch (s) { case 0: out->op = "sigmoid"; for (const char* k : { "M", "n", "dtype" }) mirror(k); return wire({ 0, RAD_FUSE_TMP(0) }); /* x, y */ case 1: out->op = "mul"; for (const char* k : { "M", "n", "dtype" }) mirror(k); return wire({ none ? (int16_t)0 : (int16_t)RAD_FUSE_TMP(0), 1, mid }); /* a, b, y */ case 2: out->op = "quant_act_fp8"; for (const char* k : { "M", "n", "group", "dtype" }) mirror(k); return wire({ mid, 2, 3 }); /* x, q, scale */ default: return RAD_E_NOTFOUND; } } /* Rows by field name (C++20 designated initialisers must follow declaration order, so the compiler * checks it), unlike a positional table where a shifted field can still compile. */ const RadKernelInfo kKernels[] = { { .name = "xe2_add", .op = "add", .family = "elem", .computes = "elementwise sum, b broadcast along rows when it is one row; optional div", .shape = "any n and row count; rows may be padded, the last axis dense", .dtypes = "any float dtype in and out (bf16, f16, f32, fp8), f32 arithmetic, rounded as libref", .domain = RAD_DOMAIN_DEVICE, .priority = 0, .launch = xe2::add }, { .name = "xe2_mul", .op = "mul", .family = "elem", .computes = "elementwise product, b broadcast along rows when it is one row", .shape = "any n and row count; rows may be padded, the last axis dense", .dtypes = "any float dtype in and out, f32 arithmetic, rounded as libref", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::mul, .opd_shape = shape_binary }, { .name = "xe2_sigmoid", .op = "sigmoid", .family = "elem", .computes = "1 / (1 + exp(-x)), the exp in double rounded once as libref's", .shape = "any element count; any strides", .dtypes = "any float dtype in and out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sigmoid, .opd_shape = shape_unary }, { .name = "xe2_gelu", .op = "gelu", .family = "elem", .computes = "0.5x(1 + erf(x / sqrt 2)), erf in double rounded once, as libref's", .shape = "any element count; any strides", .dtypes = "any float dtype in and out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gelu }, { .name = "xe2_silu", .op = "silu", .family = "elem", .computes = "x sigmoid(x), exp in double (a bf16 result: a float exp, libref's chain near a tie)", .shape = "any element count; any strides", .dtypes = "any float dtype in and out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::silu }, { .name = "xe2_gelu_tanh", .op = "gelu_tanh", .family = "elem", .computes = "the tanh gelu, tanh in double rounded once, as libref's", .shape = "any element count; any strides", .dtypes = "any float dtype in and out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gelu_tanh }, { .name = "xe2_scale", .op = "scale", .family = "elem", .computes = "y = x * s, s a one-element weight", .shape = "any element count; any strides", .dtypes = "any float x, s and y", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::scale }, { .name = "xe2_mul_weight", .op = "mul_weight", .family = "elem", .computes = "y = x * w, w one element or one row broadcast down the rows; f32 product rounded once", .shape = "rows of n, uniform row pitch", .dtypes = "any float x, w and y", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::mul_weight }, { .name = "xe2_clamp", .op = "clamp", .family = "elem", .computes = "y = min(max(x, lo), hi), lo and hi one-element weights", .shape = "rows of n, uniform row pitch", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::clamp }, { .name = "xe2_fill", .op = "fill", .family = "elem", .computes = "y = value over [M, n], rounded once to y's dtype", .shape = "rows of n, uniform row pitch", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::fill }, { .name = "xe2_iota", .op = "iota", .family = "elem", .computes = "y[i] = start + (period ? i % period : i) * step", .shape = "any element count; any strides", .dtypes = "i32, u32, i64, i16, i8, u8", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::iota }, { .name = "xe2_act_mul", .op = "act_mul", .family = "elem", .computes = "y = rd(act(a)) * b; silu, gelu, gelu_tanh, sigmoid, quick_gelu, relu in libref's chain", .shape = "rows of n, uniform row pitch (column slices of one fused row)", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::act_mul }, { .name = "xe2_gelu_mul", .op = "gelu_mul", .family = "elem", .computes = "y = gelu_tanh(gate_up[i]) * gate_up[n + i], the gate first, tanh in double", .shape = "rows of 2n in, n out", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gelu_mul }, { .name = "xe2_rmsnorm_p", .op = "rmsnorm_p", .family = "elem", .computes = "rmsnorm with no gain: xe2_rmsnorm with its gain operand absent", .shape = "as xe2_rmsnorm", .dtypes = "as xe2_rmsnorm", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::rmsnorm_p }, { .name = "xe2_relu", .op = "relu", .family = "elem", .computes = "y = max(x, 0)", .shape = "rows of n, uniform row pitch", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::relu }, { .name = "xe2_logit_softcap", .op = "logit_softcap", .family = "elem", .computes = "y = cap tanh(x / cap), tanh in double rounded once", .shape = "any element count; any strides", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::logit_softcap }, { .name = "xe2_rel_sinusoid", .op = "rel_sinusoid", .family = "elem", .computes = "row d: sin then cos of d inv[i], exp, sin and cos in double rounded once, as libref's", .shape = "rows of n, n even", .dtypes = "any float y", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::rel_sinusoid }, { .name = "xe2_pool_rows", .op = "pool_rows", .family = "elem", .computes = "the mean of k consecutive rows, f32 in order, rounded, times mult, rounded", .shape = "rows of n, M a multiple of k", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::pool_rows }, { .name = "xe2_qk_norm_rope", .op = "qk_norm_rope", .family = "rope", .computes = "libref's per-head RMS norm (gain + wadd, or unit), rounded, then rotation: neox, gptj, blocks, gptj_blocks, imrope, axial; v_norm", .shape = "head_dim, rotary_dim <= 512; a work-item a (row, head)", .dtypes = "any float qkv, gains", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::qk_norm_rope }, { .name = "xe2_qkv_norm_rope", .op = "qkv_norm_rope", .family = "rope", .computes = "rmsnorm_p (v, or v from the raw k), the q and k rmsnorms and the two NeoX rotaries in one " "launch, byte-identical to libxe2's five (its bf16 head-row norm; XE2_EXACT=1 libref's order)", .shape = "head_dim 128, 256 or 512; q, k, v rows 16-byte aligned; positions [M]", .dtypes = "bf16 q, k, v; bf16 or f32 gains", .domain = RAD_DOMAIN_DEVICE, .constraints = cQnr, .n_constraints = 3, .launch = xe2::qkv_norm_rope }, { .name = "xe2_qkv_norm_rope_kv_store", .op = "qkv_norm_rope_kv_store", .family = "rope", .computes = "xe2_qkv_norm_rope, then xe2_kv_store of the finished k and v heads from the same launch", .shape = "as xe2_qkv_norm_rope; any paged cache xe2_kv_store takes", .dtypes = "bf16 q, k, v; a bf16 or fp8 cache", .domain = RAD_DOMAIN_DEVICE, .constraints = cQnr, .n_constraints = 3, .launch = xe2::qkv_norm_rope_kv_store }, { .name = "xe2_qk_norm_rope_gate", .op = "qk_norm_rope_gate", .family = "rope", .computes = "the [q|gate] split, the q and k rmsnorms, partial NeoX rotaries from the cos/sin table and the " "gate copy in one launch, byte-identical to libxe2's rmsnorm, rope_table + rope (its bf16 head-row " "norm; XE2_EXACT=1 libref's order)", .shape = "head_dim 128, 256 or 512; rot even, <= head_dim; rows 16-byte aligned; n_head 0 is the k-only form", .dtypes = "bf16 q_gate, k, outputs; bf16 or f32 gains; f32 or bf16 cos/sin", .domain = RAD_DOMAIN_DEVICE, .constraints = cQkg, .n_constraints = 3, .launch = xe2::qk_norm_rope_gate, .opd_shape = shape_qk_norm_rope_gate }, { .name = "xe2_qk_norm_rope_gate_kv_store", .op = "qk_norm_rope_gate_kv_store", .family = "rope", .computes = "xe2_qk_norm_rope_gate, then xe2_kv_store of the rotated k and the v heads from the same launch", .shape = "as xe2_qk_norm_rope_gate; any paged cache xe2_kv_store takes", .dtypes = "bf16 q_gate, k, v; a bf16 or fp8 cache", .domain = RAD_DOMAIN_DEVICE, .constraints = cQkg, .n_constraints = 3, .launch = xe2::qk_norm_rope_gate_kv_store, .opd_shape = shape_qk_norm_rope_gate_kv_store, .replaces = fuse_qk_norm_rope_gate_kv_store }, { .name = "xe2_conv1d_dw", .op = "conv1d_dw", .family = "conv", .computes = "causal depthwise conv over time within cu segments, f32 in tap order, + bias, one rounding", .shape = "rows of n, k taps", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::conv1d_dw }, { .name = "xe2_conv2d", .op = "conv2d", .family = "conv", .computes = "libref's segmented 2-D conv over (time, width), zero pad, frame mask, bias, gelu on the rounded value", .shape = "channels innermost, any stride / pad", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::conv2d }, { .name = "xe2_attn_local_rel", .op = "attn_local_rel", .family = "attn", .computes = "Gemma 4 audio local attention with the relative term and tanh softcap, f32, transcendentals in double", .shape = "head_dim <= 256, left <= 63, right 0", .dtypes = "any float q, k, v, rel_k, o", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::attn_local_rel }, { .name = "xe2_scatter_rows", .op = "scatter_rows", .family = "gather", .computes = "row m of v to row idx[m] of x; a negative index skipped, one past x refused", .shape = "any n and row counts; rows may be padded, the last axis dense", .dtypes = "any float dtype; integer indices", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::scatter_rows }, { .name = "xe2_layernorm", .op = "layernorm", .family = "norm", .computes = "layer norm with a gain and an optional bias, libref's serial sums", .shape = "any n and row count; rows may be padded, the last axis dense", .dtypes = "any float dtype in and out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::layernorm }, { .name = "xe2_grid_embed", .op = "grid_embed", .family = "elem", .computes = "x += a square position table bilinearly resampled at each row's grid cell", .shape = "any n, rows and side", .dtypes = "any float dtype; integer coordinates", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::grid_embed }, { .name = "xe2_rmsnorm", .op = "rmsnorm", .family = "norm", .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 }, { .name = "xe2_add_rmsnorm", .op = "add_rmsnorm", .family = "norm", .computes = "x += a (bf16, as add rounds it), then rmsnorm of the x written at rmsnorm's own " "partition: byte-identical to add then rmsnorm here", .shape = "any n; bf16 rows wider than 512, 8-aligned, in one pass, else the pair", .dtypes = "float x, a, gain and y (bf16 in the recipes), f32 arithmetic", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::add_rmsnorm }, { .name = "xe2_mtp_couple_propose", .op = "mtp_couple_propose", .family = "draft", .computes = "x~Q over the final top8, FP64 T1 softmax: spechub's row helper, a work-item a row", .shape = "M,n_vocab,round,max_rounds<=8", .dtypes = "f32,i32,u32", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::mtp_couple_propose, .opd_shape = rad_mtp_couple_propose_shape }, { .name = "xe2_mtp_couple_verify", .op = "mtp_couple_verify", .family = "draft", .computes = "maximal coupling over the surviving target candidates, FP64 P: spechub's row " "helper, a work-item a row", .shape = "M,n_cand,n_vocab", .dtypes = "f32,i32,u32", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::mtp_couple_verify, .opd_shape = rad_mtp_couple_verify_shape }, { .name = "xe2_rmsnorm_quant_fp8", .op = "rmsnorm_quant_fp8", .family = "norm", .computes = "residual add (optional), RMS norm, bf16 narrowing, E4M3 codes and an f32 scale " "per group; int8 codes at amax/127 for an int8 q", .shape = "any n and group; one sub-group a quantisation group", .dtypes = "bf16 in, E4M3 (or i8) codes, f32 scales, optional bf16 out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::rmsnorm_quant_fp8 }, { .name = "xe2_quant_act_fp8", .op = "quant_act_fp8", .family = "quant", .computes = "E4M3 codes and an f32 scale amax/448 per group", .shape = "any n and group", .dtypes = "float in, E4M3 codes, f32 scales", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_fp8 }, { .name = "xe2_gate_quant_fp8", .op = "gate_quant_fp8", .family = "quant", .computes = "the gated attention's output gate: bf16(sigmoid(gate)) * x (or gate * x at act none), " "bf16 narrowing, E4M3 (or i8) codes and an f32 scale per group, one launch; " "byte-identical to xe2_sigmoid, xe2_mul and xe2_quant_act_fp8", .shape = "rows of n (a (token, head) pair), groups of 128; out_bf16 may be x", .dtypes = "bf16 gate and x, E4M3 (or i8) codes, f32 scales, optional bf16 out", .domain = RAD_DOMAIN_DEVICE, .constraints = cGateQuant, .n_constraints = 2, .launch = xe2::gate_quant_fp8, .opd_shape = shape_gate_quant_fp8, .replaces = fuse_gate_quant_fp8 }, { .name = "xe2_quant_act_i8g", .op = "quant_act_i8g", .family = "quant", .computes = "row-major int8 codes and an f32 scale amax/127 per group, libref's q8_group", .shape = "any n and group", .dtypes = "float in, i8 codes, f32 scales", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_i8g }, { .name = "xe2_gated_quant_fp8", .op = "gated_quant_fp8", .family = "quant", .computes = "silu(gate)*up or sigmoid(gate)*up over packed gate_up, bf16 narrowing, E4M3 " "codes and an f32 scale per group", .shape = "any n and group", .dtypes = "bf16 gate_up, E4M3 (or i8) codes, f32 scales, optional bf16 out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gated_quant_fp8 }, { .name = "xe2_rope", .op = "rope", .family = "rope", .computes = "rotary embedding in place over q and k heads; frequency ladder (YaRN, llama3) " "on the host as libref builds it, cos/sin in double", .shape = "neox, gptj, mrope, imrope, axial; [M] or [C, M] positions, rotary_dim <= 512", .dtypes = "bf16 qkv (any float), integer or float positions", .domain = RAD_DOMAIN_DEVICE, .constraints = cRopeModes, .n_constraints = 1, .launch = xe2::rope }, { .name = "xe2_rope_table", .op = "rope_table", .family = "rope", .computes = "cos and sin rows per position on libref's ladder (YaRN), in double", .shape = "rot <= 256", .dtypes = "integer positions, float table", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::rope_table }, { .name = "xe2_embed_lookup", .op = "embed_lookup", .family = "vocab", .computes = "embedding gather times mult; an id outside the table writes a zero row", .shape = "any n_embd and n_vocab, vocab_offset", .dtypes = "integer ids, float table and output", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::embed_lookup }, { .name = "xe2_gather_rows", .op = "gather_rows", .family = "elem", .computes = "row gather; a negative or out-of-range index writes a zero row", .shape = "any n", .dtypes = "integer indices, float rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gather_rows }, { .name = "xe2_cast", .op = "cast", .family = "elem", .computes = "dtype conversion; integer to integer exact", .shape = "any strides (libref's offlin order)", .dtypes = "any float or integer pair", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::cast }, { .name = "xe2_gemm_nt_gemv", .op = "gemm_nt", .family = "gemm", .computes = "skinny Y = A B^T for decode: a sub-group an output column, 16-byte coalesced " "weight loads, a tree reduction (not libref's order; checked at the gemm " "tolerance). XE2_EXACT=1 runs xe2_gemm_nt_ref instead", .shape = "M <= 16; bf16 rows 16-byte aligned, K a multiple of 8 (else the exact kernel)", .dtypes = "bf16 A and W, bf16 or f32 Y, f32 accumulate", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cGemvBf16, .n_constraints = 2, .launch = xe2::gemm_nt_gemv }, { .name = "xe2_logits_gemm_gemv", .op = "logits_gemm", .family = "gemm", .computes = "logits at decode, as xe2_gemm_nt_gemv", .shape = "M <= 16", .dtypes = "bf16 x and head, f32 logits", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cGemvBf16, .n_constraints = 2, .launch = xe2::logits_gemm_gemv }, { .name = "xe2_gemm_nt_xmx", .op = "gemm_nt", .family = "gemm", .computes = "Y = A B^T on XMX (joint_matrix M8 N16 K16), B read column-major from the " "[N, K] weight; tail rows by the GEMV. XE2_EXACT=1 runs xe2_gemm_nt_ref instead", .shape = "M >= 17; N a multiple of 64, K of 16, bf16 rows 16-byte aligned (else the exact " "kernel)", .dtypes = "bf16 A and W, bf16 or f32 Y, f32 accumulate", .domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cXmxBf16, .n_constraints = 2, .launch = xe2::gemm_nt_xmx }, { .name = "xe2_logits_gemm_xmx", .op = "logits_gemm", .family = "gemm", .computes = "logits at prefill sizes, as xe2_gemm_nt_xmx", .shape = "M >= 17", .dtypes = "bf16 x and head, f32 logits", .domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cXmxBf16, .n_constraints = 2, .launch = xe2::logits_gemm_xmx }, { .name = "xe2_gemm_nt_ref", .op = "gemm_nt", .family = "gemm", .computes = "Y = A B^T, one work-item an output, f32 over ascending k (libref's order); the " "correctness-first form, not the fast one", .shape = "any M, N, K; strided A", .dtypes = "float A and W (bf16 in the recipes), float Y", .domain = RAD_DOMAIN_DEVICE, .constraints = cFloatDtype, .n_constraints = 1, .launch = xe2::gemm_nt }, { .name = "xe2_logits_gemm_ref", .op = "logits_gemm", .family = "gemm", .computes = "logits = x lm_head^T, as xe2_gemm_nt_ref", .shape = "any M, n_vocab, n_embd", .dtypes = "float x and head, f32 logits", .domain = RAD_DOMAIN_DEVICE, .constraints = cFloatDtype, .n_constraints = 1, .launch = xe2::logits_gemm }, { .name = "xe2_kv_store", .op = "kv_store", .family = "attn", .computes = "this step's K and V into the paged cache at slot_mapping; fp8 stores k/k_scale", .shape = "any head_dim, kv heads and block size; a negative or out-of-range slot is skipped", .dtypes = "bf16 k and v, bf16 or E4M3 cache", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::kv_store }, { .name = "xe2_attn_paged", .op = "attn_paged", .family = "attn", .computes = "split-KV paged attention with an online softmax: a work-group a (row, kv head, " "key partition), its sub-groups the kv head's query heads, partitions merged from " "scratch (not libref's order). XE2_EXACT=1 runs xe2_attn_paged_ref instead", .shape = "head_dim a multiple of 16 up to 512 (else the exact kernel); any gqa, causal or " "not, sliding window, ragged batches", .dtypes = "bf16 query, bf16 or E4M3 cache with per-(seq, head) descales, bf16 out", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .launch = xe2::attn_paged_fast, .scratch = xe2::attn_paged_scratch }, { .name = "xe2_attn_paged_ref", .op = "attn_paged", .family = "attn", .computes = "paged attention, one work-group a (row, head), libref's order: serial dots, " "keys summed in order; the correctness-first form", .shape = "head_dim <= 512, any gqa, causal or not, sliding window, ragged batches", .dtypes = "bf16 query, bf16 or E4M3 cache with per-(seq, head) descales, bf16 out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::attn_paged }, { .name = "xe2_sample_penalties", .op = "sample_penalties", .family = "sample", .computes = "repetition, frequency and presence penalties over the row's history", .shape = "one work-item a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_penalties }, { .name = "xe2_sample_dry", .op = "sample_dry", .family = "sample", .computes = "DRY penalty over the row's history", .shape = "one work-item a row; quadratic in the window, as libref", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_dry }, { .name = "xe2_sample_ngram_ban", .op = "sample_ngram_ban", .family = "sample", .computes = "windowed no-repeat-ngram ban, optional whitelist", .shape = "one work-item a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_ngram_ban }, { .name = "xe2_sample_temp", .op = "sample_temp", .family = "sample", .computes = "logits divided by the row's temperature", .shape = "one work-item an element", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_temp }, { .name = "xe2_sample_mask", .op = "sample_mask", .family = "sample", .computes = "grammar bitmask: a cleared bit sets -inf", .shape = "one work-item an element", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_mask }, { .name = "xe2_sample_topk", .op = "sample_topk", .family = "sample", .computes = "the row's top_k candidates, descending, ties to the lower id", .shape = "one work-group a row, k reduction rounds", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_topk }, { .name = "xe2_sample_topp", .op = "sample_topp", .family = "sample", .computes = "top-p over the candidate set", .shape = "one work-item a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_topp }, { .name = "xe2_sample_minp", .op = "sample_minp", .family = "sample", .computes = "min-p over the candidate set", .shape = "one work-item a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_minp }, { .name = "xe2_sample_typical", .op = "sample_typical", .family = "sample", .computes = "locally typical sampling over the candidate set", .shape = "one work-item a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_typical }, { .name = "xe2_sample_xtc", .op = "sample_xtc", .family = "sample", .computes = "exclude top choices, seeded coin", .shape = "one work-item a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_xtc }, { .name = "xe2_sample_pick", .op = "sample_pick", .family = "sample", .computes = "inverse-CDF draw over the candidates, seeded per row", .shape = "one work-item a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_pick }, { .name = "xe2_sample_argmax", .op = "sample_argmax", .family = "sample", .computes = "greedy token, ties to the lower id", .shape = "one work-group a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_argmax }, { .name = "xe2_sample_logprobs", .op = "sample_logprobs", .family = "sample", .computes = "max, double normaliser and the top k (id, logit) pairs", .shape = "one work-group a row", .dtypes = "f32 logits and candidates, RadSampleParams rows", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_logprobs }, { .name = "xe2_gemm_nt_q_fp8_ref", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over E4M3 codes with group and block scales, libref's quant_dot per " "output; the correctness-first form", .shape = "any M, N, K; 128x128 block or per-row group scale grids", .dtypes = "fp8a8, fp8a8f32, fp8a16: E4M3 weights, E4M3 or bf16 activations", .domain = RAD_DOMAIN_DEVICE, .constraints = cFp8Dtype, .n_constraints = 1, .launch = xe2::gemm_nt_q }, { .name = "xe2_gemm_nt_q_gated_fp8_ref", .op = "gemm_nt_q_gated", .family = "gemm", .computes = "gemm_nt_q over a gate_up plane with gated_quant_fp8 in the epilogue, one " "work-group a row", .shape = "N <= 57344 (n gated values in local memory)", .dtypes = "fp8a8, fp8a8f32, fp8a16; E4M3 (or i8) codes and f32 scales out", .domain = RAD_DOMAIN_DEVICE, .constraints = cFp8Gated, .n_constraints = 2, .launch = xe2::gemm_nt_q_gated }, { .name = "xe2_silu_mul", .op = "silu_mul", .family = "elem", .computes = "silu(gate) * up over a packed gate_up", .shape = "any n", .dtypes = "bf16 (any float)", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::silu_mul }, { .name = "xe2_quant_act_i8", .op = "quant_act_i8", .family = "quant", .computes = "int8 codes with one scale a row, in the int8 WMMA step order; optional group sums", .shape = "n a multiple of 16", .dtypes = "float in, i8 codes, f32 scale", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_i8 }, { .name = "xe2_row_topk", .op = "row_topk", .family = "sample", .computes = "exact per-row top-R, descending, ties to the lower column; one work-group a row", .shape = "any N and R", .dtypes = "float x, integer ids and float values out", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::row_topk }, { .name = "xe2_dflash_select", .op = "dflash_select", .family = "draft", .computes = "the block drafter's greedy walk over its candidate lattice, one work-item a " "sequence", .shape = "any steps, top_k and rank", .dtypes = "integer candidates, float scores and factors", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::dflash_select }, { .name = "xe2_gemm_nt_q_zp_gemv", .op = "gemm_nt_q", .family = "gemm", .computes = "the zero-point grids at M <= 16: a sub-group a column, exact integer group sums, " "libref's group terms added in order through lane broadcasts -- libref's result", .shape = "M <= 16; group a multiple of 16 up to 512, at most 64 groups", .dtypes = "w2a8, w4a8_asym: u2/u4 codes, f16 scale and unsigned zero per (row, group), i8 " "activation in step order with row scale and group sums", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cZpGemv, .n_constraints = 2, .launch = xe2::gemm_nt_q_zp_gemv, .layout = xe2::gemm_zp_layout, .relayout = xe2::gemm_zp_relayout, .unrelayout = xe2::gemm_zp_unrelayout }, { .name = "xe2_gemm_nt_q_zp_ref", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over an unsigned zero-point grid against quant_act_i8's activation, " "libref's gemm_zp per output; the correctness-first form", .shape = "any M, N; K a multiple of 16 and of group", .dtypes = "w2a8, w4a8_asym: u2/u4 codes, f16 scale and unsigned zero per (row, group), i8 " "activation in step order with row scale and group sums", .domain = RAD_DOMAIN_DEVICE, .constraints = cZpI8Dtype, .n_constraints = 1, .launch = xe2::gemm_nt_q_zp, .layout = xe2::gemm_zp_layout, .relayout = xe2::gemm_zp_relayout, .unrelayout = xe2::gemm_zp_unrelayout }, { .name = "xe2_gemm_nt_q_w4a16", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over u4 codes with an f16 scale and zero per (row, 128): codes decoded to " "code - zero in fp16 registers, fp16 DPAS a group into f32, scaled once a group", .shape = "M <= 64 (blocks of 8 rows); N a multiple of 16, K of 128, group 128 (else the exact form)", .dtypes = "w4a16_asym: u4 codes, f16 scale and u4/u8 zero per (row, 128 of K), f16 activation", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cW4a16, .n_constraints = 2, .launch = xe2::gemm_nt_q_w4a16, .layout = xe2::gemm_w4_layout, .relayout = xe2::gemm_w4_relayout, .unrelayout = xe2::gemm_w4_unrelayout }, { .name = "xe2_gemm_nt_q_w4a8i8", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over u4 codes against an int8 activation: int8 DPAS with the codes as its " "u4 B operand, exact group sums, libref's group terms in order -- libref's result", .shape = "any M; N a multiple of 32, K of 128, group 128 (else the exact form)", .dtypes = "w4a8i8_asym: u4 codes, f16 scale and u4/u8 zero per (row, 128 of K), row-major int8 " "activation with an f32 scale per (row, 128)", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cW4a8i8, .n_constraints = 1, .launch = xe2::gemm_nt_q_w4a8i8, .layout = xe2::gemm_w4_layout, .relayout = xe2::gemm_w4_relayout, .unrelayout = xe2::gemm_w4_unrelayout }, { .name = "xe2_gemm_nt_q_w4_ref", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over the 4-bit zero-point grids, libref's gemm_zp per output; the " "correctness-first form", .shape = "any M, N; K a multiple of group", .dtypes = "w4a16_asym (f16 activation), w4a8i8_asym (int8 activation, f32 scale per (row, " "group)): u4 codes, f16 scale and u4/u8 zero per (row, group)", .domain = RAD_DOMAIN_DEVICE, .constraints = cW4Dtype, .n_constraints = 1, .launch = xe2::gemm_nt_q_w4, .layout = xe2::gemm_w4_layout, .relayout = xe2::gemm_w4_relayout, .unrelayout = xe2::gemm_w4_unrelayout }, { .name = "xe2_gemm_nt_q_m2_gemv", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over the M2XFP4 grid for M <= 16: e2m1 to fp16 by bits, fp16 activation " "and subgroup sums, f32 group sums; libref's products regrouped", .shape = "M <= 16; K a multiple of 32, group 32", .dtypes = "m2xfp4 weight, E4M3 activation with a scale per 128 columns", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cM2Gemv, .n_constraints = 2, .launch = xe2::gemm_nt_q_m2_gemv, .layout = xe2::gemm_m2_layout, .relayout = xe2::gemm_m2_relayout, .unrelayout = xe2::gemm_m2_unrelayout }, { .name = "xe2_gemm_nt_q_m2_xmx", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over the M2XFP4 grid for M >= 17: the weight decoded to bf16 (exact), " "the activation to bf16(e4m3 * scale), then the bf16 XMX GEMM per part", .shape = "M >= 17; K a multiple of 32, group 32", .dtypes = "m2xfp4 weight, E4M3 activation with a scale per 128 columns", .domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cM2Xmx, .n_constraints = 2, .launch = xe2::gemm_nt_q_m2_xmx, .layout = xe2::gemm_m2_layout, .relayout = xe2::gemm_m2_relayout, .unrelayout = xe2::gemm_m2_unrelayout }, { .name = "xe2_gemm_nt_q_m2_ref", .op = "gemm_nt_q", .family = "gemm", .computes = "Y = A B^T over the M2XFP4 grid against givens_dense_quant's fp8 activation, " "libref's gemm_m2 per output; the correctness-first form", .shape = "any M, N; K a multiple of 32, group 32", .dtypes = "m2xfp4: e2m1 codes and u8 meta concatenated, e8m0 scale per (row, 32); E4M3 " "activation with a scale per 128 columns", .domain = RAD_DOMAIN_DEVICE, .constraints = cM2Dtype, .n_constraints = 1, .launch = xe2::gemm_nt_q_m2, .layout = xe2::gemm_m2_layout, .relayout = xe2::gemm_m2_relayout, .unrelayout = xe2::gemm_m2_unrelayout }, { .name = "xe2_givens_dense_quant", .op = "givens_dense_quant", .family = "quant", .computes = "ParoQuant's activation quantiser: x / D through a dense 128x128 rotation per " "(part, group) in double, then the fp8 or i8 group quantiser; libref's form", .shape = "any rows; n a multiple of 128, group 128, any parts", .dtypes = "float x, f16 rotation, float channel scales; E4M3 or i8 codes, f32 scales", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::givens_dense_quant }, { .name = "xe2_attn_dense", .op = "attn_dense", .family = "attn", .computes = "unpaged dense attention over cu_seqlens segments (causal, window, blocks); " "libref's two passes, exp in double", .shape = "head_dim <= 256, any heads, gqa, segments", .dtypes = "any float q, k, v, o", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::attn_dense }, { .name = "xe2_dflash_conv", .op = "dflash_conv", .family = "draft", .computes = "DFlash2's per-block causal tap conv with per-row tap deltas; libref's order", .shape = "any T, taps, groups, block size", .dtypes = "any float dtype", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::dflash_conv }, { .name = "xe2_logit_rerank", .op = "logit_rerank", .family = "draft", .computes = "R candidates a row rescored against a float head in double, row_topk's order", .shape = "any rows and R", .dtypes = "float x and head; integer ids", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::logit_rerank }, { .name = "xe2_logit_rerank_head", .op = "logit_rerank", .family = "draft", .computes = "R candidates a row rescored against a head of codes and a scale plane, joined", .shape = "any rows and R", .dtypes = "float x; E4M3 or i8 codes with a float scale", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::logit_rerank_head, .layout = xe2::head_layout, .relayout = xe2::head_relayout, .unrelayout = xe2::head_unrelayout }, { .name = "xe2_gemm_nt_bias", .op = "gemm_nt_bias", .family = "gemm", .computes = "Y = A B^T + bias, then gelu / gelu_tanh and a residual: the product on the fast " "GEMMs (the GEMV at M <= 16, XMX above) into f32, the epilogue in libref's order. " "XE2_EXACT=1 runs xe2_gemm_nt_bias_ref instead", .shape = "any M, N, K (each GEMM falls back to the exact kernel where its layout is refused)", .dtypes = "bf16 A and W, float bias, residual and Y", .domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cBf16, .n_constraints = 1, .launch = xe2::gemm_nt_bias_fast }, { .name = "xe2_gemm_nt_bias_ref", .op = "gemm_nt_bias", .family = "gemm", .computes = "Y = A B^T + bias, then gelu / gelu_tanh and a residual; libref's order", .shape = "any M, N, K", .dtypes = "float A and B", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::gemm_nt_bias }, { .name = "xe2_logits_gemm_head_gemv", .op = "logits_gemm", .family = "vocab", .computes = "the lm_head over a joined E4M3 / int8 head at M <= 16: a sub-group a column, " "16 codes a lane step inside a scale block", .shape = "M <= 16, n_embd a multiple of 128", .dtypes = "float A; E4M3 codes, float block scale", .domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cHeadGemv, .n_constraints = 1, .launch = xe2::logits_gemm_head_gemv, .layout = xe2::head_layout, .relayout = xe2::head_relayout, .unrelayout = xe2::head_unrelayout }, { .name = "xe2_logits_gemm_head_ref", .op = "logits_gemm", .family = "vocab", .computes = "the lm_head over a head of E4M3 / int8 codes with a scale plane, joined; each " "weight decoded to code * scale, then libref's plain product", .shape = "any M, n_vocab, n_embd", .dtypes = "float A; E4M3 or i8 codes, float block scale", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::logits_gemm_head, .layout = xe2::head_layout, .relayout = xe2::head_relayout, .unrelayout = xe2::head_unrelayout }, { .name = "xe2_gdn_conv_prep", .op = "gdn_conv_prep", .family = "gdn", .computes = "the causal conv with silu and its state, the q/k l2 norm, the chunked gate cumsum and beta; libref's arithmetic", .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_conv_prep }, { .name = "xe2_gdn_conv_update", .op = "gdn_conv_update", .family = "gdn", .computes = "the decode step's causal conv against the rolling window, shifted by one; libref's arithmetic", .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_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: 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: 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 }, { .name = "xe2_gdn_recurrent_update", .op = "gdn_recurrent_update", .family = "gdn", .computes = "the decode delta-rule step against the paged state, gated RMS norm folded in; libref's arithmetic", .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_recurrent_update }, { .name = "xe2_gdn_gated_rmsnorm", .op = "gdn_gated_rmsnorm", .family = "gdn", .computes = "x rsqrt(mean(x^2) + eps) w act(z) per (token, head); libref's arithmetic", .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_gated_rmsnorm }, { .name = "xe2_gdn_conv_recurrent_update", .op = "gdn_conv_recurrent_update", .family = "gdn", .computes = "gdn_conv_update then gdn_recurrent_update, libref's definition of the pair; libref's arithmetic", .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_conv_recurrent_update }, }; /* Schemas of the device-only ops libxe2 serves that libref does not declare (docs/OPS.md, libr4d's * spellings field for field). */ const RadParamSpec pAddRmsNorm[] = { { "M", RAD_P_INT, 1, 0 }, { "n", RAD_P_INT, 1, 0 }, { "eps", RAD_P_F64, 1, 0 }, { "dtype", RAD_P_STR, 1, 0 }, { "wadd", RAD_P_F64, 0, 0 }, { "mult", RAD_P_F64, 0, 0 }, }; const RadOperandSpec oAddRmsNorm[] = { { "x", RAD_OPD_INOUT, 0 }, { "a", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 0 }, { "y", RAD_OPD_OUT, 0 }, }; /* libr4d's schemas for the fused attention prologue (r4d_rows.cpp), which libxe2 now also declares */ const RadParamSpec pQkvNormRope[] = { { "M", RAD_P_INT, 1 }, { "head_dim", RAD_P_INT, 1 }, { "n_head", RAD_P_INT, 1 }, { "n_head_kv", RAD_P_INT, 1 }, { "theta", RAD_P_F64, 1 }, { "eps", RAD_P_F64, 1 }, { "scale", RAD_P_F64, 0 }, { "mode", RAD_P_STR, 0 }, { "rotary_dim", RAD_P_INT, 0 }, { "ladder", RAD_P_STR, 0 }, { "wadd", RAD_P_F64, 0 }, { "v_from_k", RAD_P_INT, 0 }, /* 0 leaves v unnormed (Gemma 3; a Qwen3-shaped layer); absent, 1 */ { "v_norm", RAD_P_INT, 0 }, { "dtype", RAD_P_STR, 1 }, }; const RadOperandSpec oQkvNormRope[] = { { "q", RAD_OPD_INOUT, 0 }, { "k", RAD_OPD_INOUT, 0 }, { "v", RAD_OPD_INOUT, 0 }, /* written from the raw k under v_from_k */ { "positions", RAD_OPD_IN, 0 }, { "q_w", RAD_OPD_WEIGHT, 0 }, { "k_w", RAD_OPD_WEIGHT, 0 }, }; /* `qkv_norm_rope_kv_store` is qkv_norm_rope followed by kv_store in one launch: the prologue's * operands, then kv_store's slot mapping and paged cache; every parameter is one of the two ops'. * Byte-identical to the pair (docs/OPS.md). */ const RadParamSpec pQkvNormRopeKv[] = { { "M", RAD_P_INT, 1 }, { "head_dim", RAD_P_INT, 1 }, { "n_head", RAD_P_INT, 1 }, { "n_head_kv", RAD_P_INT, 1 }, { "theta", RAD_P_F64, 1 }, { "eps", RAD_P_F64, 1 }, { "scale", RAD_P_F64, 0 }, { "mode", RAD_P_STR, 0 }, { "rotary_dim", RAD_P_INT, 0 }, { "ladder", RAD_P_STR, 0 }, { "wadd", RAD_P_F64, 0 }, { "v_from_k", RAD_P_INT, 0 }, { "dtype", RAD_P_STR, 1 }, { "v_norm", RAD_P_INT, 0 }, { "block_size", RAD_P_INT, 1 }, { "kv_dtype", RAD_P_STR, 1 }, { "k_scale", RAD_P_F64, 0 }, { "v_scale", RAD_P_F64, 0 }, }; const RadOperandSpec oQkvNormRopeKv[] = { { "q", RAD_OPD_INOUT, 0 }, { "k", RAD_OPD_INOUT, 0 }, { "v", RAD_OPD_INOUT, 0 }, { "positions", RAD_OPD_IN, 0 }, { "q_w", RAD_OPD_WEIGHT, 0 }, { "k_w", RAD_OPD_WEIGHT, 0 }, { "slot_mapping", RAD_OPD_IN, 0 }, { "kv_cache", RAD_OPD_INOUT, 0 }, }; /* libr4d's schemas for the gated prologue (r4d_rows.cpp), which the loader requires to agree. Not * docs/OPS.md's qk_norm_rope: [q|gate] split per head, three destinations, and a precomputed * cos/sin table in place of `theta`. */ const RadParamSpec pQkNormRopeGate[] = { { "M", RAD_P_INT, RAD_REQUIRED }, { "head_dim", RAD_P_INT, RAD_REQUIRED }, { "n_head", RAD_P_INT, RAD_REQUIRED }, { "n_head_kv", RAD_P_INT, RAD_REQUIRED }, { "rot", RAD_P_INT, RAD_REQUIRED }, { "q_dtype", RAD_P_STR, RAD_REQUIRED }, { "eps", RAD_P_F64, RAD_OPTIONAL }, { "wadd", RAD_P_F64, RAD_OPTIONAL }, { "cs_f32", RAD_P_INT, RAD_OPTIONAL }, { "pos_i64", RAD_P_INT, RAD_OPTIONAL }, }; const RadOperandSpec oQkNormRopeGate[] = { { "q_gate", RAD_OPD_IN, 1 }, { "k", RAD_OPD_IN, 0 }, { "cos_sin", RAD_OPD_IN, 0 }, { "positions", RAD_OPD_IN, 0 }, { "q_w", RAD_OPD_WEIGHT, 1 }, { "k_w", RAD_OPD_WEIGHT, 0 }, { "q_out", RAD_OPD_OUT, 1 }, { "k_out", RAD_OPD_OUT, 0 }, { "gate_out", RAD_OPD_OUT, 1 }, }; const RadParamSpec pQkNormRopeGateKv[] = { { "M", RAD_P_INT, RAD_REQUIRED }, { "head_dim", RAD_P_INT, RAD_REQUIRED }, { "n_head", RAD_P_INT, RAD_REQUIRED }, { "n_head_kv", RAD_P_INT, RAD_REQUIRED }, { "rot", RAD_P_INT, RAD_REQUIRED }, { "q_dtype", RAD_P_STR, RAD_REQUIRED }, { "eps", RAD_P_F64, RAD_OPTIONAL }, { "wadd", RAD_P_F64, RAD_OPTIONAL }, { "cs_f32", RAD_P_INT, RAD_OPTIONAL }, { "pos_i64", RAD_P_INT, RAD_OPTIONAL }, { "block_size", RAD_P_INT, RAD_REQUIRED }, { "kv_dtype", RAD_P_STR, RAD_REQUIRED }, { "k_scale", RAD_P_F64, RAD_OPTIONAL }, { "v_scale", RAD_P_F64, RAD_OPTIONAL }, }; const RadOperandSpec oQkNormRopeGateKv[] = { { "q_gate", RAD_OPD_IN, 1 }, { "k", RAD_OPD_IN, 0 }, { "cos_sin", RAD_OPD_IN, 0 }, { "positions", RAD_OPD_IN, 0 }, { "q_w", RAD_OPD_WEIGHT, 1 }, { "k_w", RAD_OPD_WEIGHT, 0 }, { "q_out", RAD_OPD_OUT, 1 }, { "k_out", RAD_OPD_OUT, 0 }, { "gate_out", RAD_OPD_OUT, 1 }, { "v", RAD_OPD_IN, 0 }, { "slot_mapping", RAD_OPD_IN, 0 }, { "kv_cache", RAD_OPD_INOUT, 0 }, }; /* gate_quant_fp8 (libr4d's pGateQuantFp8 / oGateQuantFp8): the gate in its own operand. */ const RadParamSpec pGateQuantFp8[] = { { "M", RAD_P_INT, RAD_REQUIRED }, { "n", RAD_P_INT, RAD_REQUIRED }, { "group", RAD_P_INT, RAD_REQUIRED }, { "dtype", RAD_P_STR, RAD_REQUIRED }, { "act", RAD_P_STR, RAD_OPTIONAL }, }; const RadOperandSpec oGateQuantFp8[] = { { "gate", RAD_OPD_IN, 0 }, { "x", RAD_OPD_IN, 0 }, { "q", RAD_OPD_OUT, 0 }, { "scale", RAD_OPD_OUT, 0 }, { "out_bf16", RAD_OPD_OUT, 1 }, }; const RadOpSchema kSchemas[] = { { "add_rmsnorm", pAddRmsNorm, (int)(sizeof pAddRmsNorm / sizeof pAddRmsNorm[0]), oAddRmsNorm, (int)(sizeof oAddRmsNorm / sizeof oAddRmsNorm[0]), "x = x + a, then y = rmsnorm(x) * (w + wadd) * mult over the x written: a residual add and the " "next norm in one pass, byte-identical to add then rmsnorm." }, { "qkv_norm_rope", pQkvNormRope, (int)(sizeof pQkvNormRope / sizeof pQkvNormRope[0]), oQkvNormRope, (int)(sizeof oQkvNormRope / sizeof oQkvNormRope[0]), "per-head q and k RMS norms, the weightless v norm and the rotary of q and k over separate " "q, k and v projections, in one dispatch; v from the raw k under v_from_k. n_head 0 " "leaves q unread (the k and v heads alone)." }, { "qkv_norm_rope_kv_store", pQkvNormRopeKv, (int)(sizeof pQkvNormRopeKv / sizeof pQkvNormRopeKv[0]), oQkvNormRopeKv, (int)(sizeof oQkvNormRopeKv / sizeof oQkvNormRopeKv[0]), "qkv_norm_rope, then kv_store of the finished k and v heads into the paged cache, in one " "launch." }, { "qk_norm_rope_gate", pQkNormRopeGate, (int)(sizeof pQkNormRopeGate / sizeof pQkNormRopeGate[0]), oQkNormRopeGate, (int)(sizeof oQkNormRopeGate / sizeof oQkNormRopeGate[0]), "split [q|gate] per head, QK RMS norm over the head, partial NeoX RoPE and the gate " "copy, in one pass. Not docs/OPS.md's qk_norm_rope: different operands, and a " "precomputed cos/sin table instead of theta. `n_head` 0 is the K-ONLY FORM -- no " "`q_gate`, no `q_w`, no `q_out` -- for an input that is already one plain head per " "`head_dim` columns with nothing interleaved. The QSA indexer's query heads are that " "shape, and it is how its `heads` norms plus a rope become one launch." }, { "qk_norm_rope_gate_kv_store", pQkNormRopeGateKv, (int)(sizeof pQkNormRopeGateKv / sizeof pQkNormRopeGateKv[0]), oQkNormRopeGateKv, (int)(sizeof oQkNormRopeGateKv / sizeof oQkNormRopeGateKv[0]), "qk_norm_rope_gate followed by kv_store, in one launch: each k head's block also writes " "its rotated k and its v into the paged cache at the token's slot. The `k` kv_store " "reads is `k_out`, so the tail adds only `v`, `slot_mapping` and `kv_cache`." }, { "gate_quant_fp8", pGateQuantFp8, (int)(sizeof pGateQuantFp8 / sizeof pGateQuantFp8[0]), oGateQuantFp8, (int)(sizeof oGateQuantFp8 / sizeof oGateQuantFp8[0]), "act(gate) * x rounded to bf16 (act sigmoid, its result rounded to bf16 first, or none), " "then E4M3 codes and an f32 scale per (row, group) over the rounded product; out_bf16 " "receives the product. A row is a (token, head) pair of a gated attention." }, }; const RadPluginInfo kInfo = { RAD_PLUGIN_KERNEL, "libxe2", "0.1", "Intel Xe2 (Battlemage) kernels in SYCL, checked against libref", "intel_gpu_bmg_g31", }; /* XE2_NO_FUSED_PROLOGUE=1 leaves the fused attention prologues (plain and gated) undeclared, so an architecture takes * its unfused chain: the A/B for its byte-identity, and an escape hatch. */ bool hidden(const char* op) { static const bool off = std::getenv("XE2_NO_FUSED_PROLOGUE") != nullptr; /* XE2_NO_FUSED_GATE=1 the same for gate_quant_fp8: the gated block runs sigmoid and mul. */ static const bool gate_off = std::getenv("XE2_NO_FUSED_GATE") != nullptr; if (gate_off && !std::strcmp(op, "gate_quant_fp8")) return true; return off && (!std::strcmp(op, "qkv_norm_rope") || !std::strcmp(op, "qkv_norm_rope_kv_store") || !std::strcmp(op, "qk_norm_rope_gate") || !std::strcmp(op, "qk_norm_rope_gate_kv_store")); } const std::vector& kernels() { static const std::vector v = [] { std::vector r; for (const RadKernelInfo& k : kKernels) if (!hidden(k.op)) r.push_back(&k); return r; }(); return v; } const std::vector& schemas() { static const std::vector v = [] { std::vector r; for (const RadOpSchema& o : kSchemas) if (!hidden(o.op)) r.push_back(&o); return r; }(); return v; } } // namespace extern "C" { __attribute__((visibility("default"))) uint32_t rad_plugin_abi_version(void) { return RAD_ABI_VERSION; } __attribute__((visibility("default"))) uint64_t rad_plugin_abi_layout(void) { return rad_abi_layout(); } __attribute__((visibility("default"))) const RadPluginInfo* rad_plugin_info(void) { return &kInfo; } __attribute__((visibility("default"))) int rad_kernel_schema_count(void) { return (int)schemas().size(); } __attribute__((visibility("default"))) const RadOpSchema* rad_kernel_schema_at(int i) { return i >= 0 && i < rad_kernel_schema_count() ? schemas()[(size_t)i] : nullptr; } __attribute__((visibility("default"))) int rad_kernel_count(void) { return (int)kernels().size(); } __attribute__((visibility("default"))) const RadKernelInfo* rad_kernel_at(int i) { return i >= 0 && i < rad_kernel_count() ? kernels()[(size_t)i] : nullptr; } } // extern "C"