/* xe2_gemm_m2.cpp -- gemm_nt_q at dtype m2xfp4 (the M2XFP4 grid of Qwen3.8-27B-PARO-M2XFP4). * Correctness first: libref's gemm_m2 (ref_gemm.cpp) per output -- * * w[k] = (e2m1(code[n][k]) * 2^(e[n][k/32] - 127)) * (1 + meta field / 4) * acc = acc + (a[m][k] * a_scale[m][k/128]) * w[k] for ascending k, f32 * * -- the meta byte's two-bit field [2s, 2s+2) over subgroups s of eight columns. The activation is * givens_dense_quant's E4M3 with a scale per 128 columns; the weight's Givens rotation is the * activation's, so the planes are read as they are stored. Parts (`pb1`, `pb2`, a fused projection * rotated per piece): weight rows [0, e1), [e1, e2), [e2, N) read copies 0, 1, 2 of the activation * and its scale, side by side, as libref's gemm_m2 and libr4d's mx_parts do. * * `b` reaches the kernel as the weight's codes, then its meta rows, `b_scale` as the E8M0 plane * [N, K/32]. The layout hooks are libref's m2 arm (ref_layout_gemm_q, ref_planes.h) -- codes rows * then meta rows, "m2.codes.meta" -- except where N % 16 == 0 and K % 512 == 0: there the codes and * meta bytes are stored tiled ("xe2.m2.tiled16", xe2_ops.h), the verify GEMM's registers one block * load a step (246 -> 229 us at gate_up's shape, probe m2d.cpp), and the exponent plane tiled alike * with each column's largest exponent after it ("xe2.m2.e8m0.t16"), which the kernels otherwise * scan for on every call. Every kernel here reads those forms. */ #include "xe2_common.h" #include "xe2_ops.h" #include "rad_planes.h" #include #include namespace xe2 { namespace { int64_t row_stride(const RadTensor* t, int64_t cols) { if (!t || t->rank < 2) return cols; int64_t span = 1; for (int i = (int)t->rank - 1; i >= 0; --i) { span *= t->shape[i]; if (span == cols) return (i > 0) ? t->stride[i - 1] : cols; if (span > cols) break; } return t->stride[t->rank - 2]; } int64_t last_stride(const RadTensor* t) { return t->rank ? t->stride[t->rank - 1] : 1; } bool sel_roles(const RadEncoding* enc, const int* sel, int n, const char* r0, const char* r1) { if (!enc || !sel || n != 2) return false; for (int i = 0; i < 2; ++i) if (sel[i] < 0 || sel[i] >= enc->n_planes) return false; return std::strncmp(enc->plane[sel[0]].role, r0, RAD_ENC_STR) == 0 && std::strncmp(enc->plane[sel[1]].role, r1, RAD_ENC_STR) == 0; } /* libref's m2_enc_planes: three tiled planes over 32-column groups, untransformed or under a * "givens" transform with its tables beside them. */ bool m2_enc_planes(const RadEncoding* enc) { if (!rad_enc_is(enc, "m2xfp4")) return false; if (enc->transform[0] && std::strncmp(enc->transform, "givens", RAD_ENC_STR) != 0) return false; const RadEncPlane* sc = rad_enc_plane(enc, "scale"); const RadEncPlane* mt = rad_enc_plane(enc, "meta"); const bool planes = enc->plane[0].kind == RAD_PLANE_TILED && enc->plane[0].dtype == RAD_FP4E2M1 && enc->plane[0].block[0] == 1 && enc->plane[0].block[1] == 1 && sc && mt && sc->kind == RAD_PLANE_TILED && sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == 32 && mt->kind == RAD_PLANE_TILED && mt->dtype == RAD_U8 && mt->block[0] == 1 && mt->block[1] == 32; if (!planes) return false; if (!enc->transform[0]) return enc->n_planes == 3; return enc->n_planes >= 6 && rad_enc_plane(enc, "t.pairs") && rad_enc_plane(enc, "t.angle") && rad_enc_plane(enc, "t.scale"); } constexpr const char* kTag = "m2.codes.meta"; constexpr const char* kTagT = "xe2.m2.tiled16"; /* codes and meta tiled (xe2_ops.h) */ constexpr const char* kTagE = "xe2.m2.e8m0.t16"; /* exponents tiled, then each column's max */ /* Moves the planes between their row-major form and the tiled one (xe2_ops.h): codes [N][K/2], a * 32-group plane (meta or exponents) [N][K/32]. */ void tile_codes(const uint8_t* src, uint8_t* dst, int64_t N, int64_t K, bool to_tiled) { const int64_t rb = K / 2; for (int64_t n = 0; n < N; ++n) for (int64_t b = 0; b < rb; b += 4) { const int64_t t = m2t_off(n, b, K), r = n * rb + b; std::memcpy(dst + (to_tiled ? t : r), src + (to_tiled ? r : t), 4); } } void tile_groups(const uint8_t* src, uint8_t* dst, int64_t N, int64_t kg, bool to_tiled) { for (int64_t n = 0; n < N; ++n) for (int64_t g = 0; g < kg; ++g) { const int64_t t = m2e_off(n, g, kg), r = n * kg + g; dst[to_tiled ? t : r] = src[to_tiled ? r : t]; } } } // namespace /* ------------------------------------------------------------------ the layout hooks */ int gemm_m2_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const RadTensor* planes, int n, RadLayout* out) { const char* dt = rad_param_gets(p, n_p, "dtype", nullptr); if (!dt || std::strcmp(dt, "m2xfp4") != 0) return RAD_E_DTYPE; const long long group = rad_param_geti(p, n_p, "group", 0); const long long N = rad_param_geti(p, n_p, "N", 0), K = rad_param_geti(p, n_p, "K", 0); if (!m2_enc_planes(enc) || group != 32 || N <= 0 || K <= 0 || K % 32) return RAD_E_DTYPE; if (operand == 2) { if (!sel_roles(enc, sel, n, "codes", "meta")) return RAD_E_DTYPE; if (planes[0].shape[0] != N || planes[0].shape[1] != K || planes[1].shape[0] != N || planes[1].shape[1] != K / 32) return RAD_E_SHAPE; out->tag = m2_tiled(N, K) ? kTagT : kTag; out->dtype = RAD_U8; out->rank = 1; out->shape[0] = N * (K / 2 + K / 32); out->align = 0; out->bytes = out->shape[0]; return RAD_OK; } if (operand == 3 || operand == 6) { if (n != 1 || sel[0] < 0 || sel[0] >= enc->n_planes || std::strncmp(enc->plane[sel[0]].role, "scale", RAD_ENC_STR) != 0) return RAD_E_DTYPE; if (operand == 6 || !m2_tiled(N, K)) return RAD_E_UNSUPPORTED; /* stored as it is */ if (planes[0].shape[0] != N || planes[0].shape[1] != K / 32) return RAD_E_SHAPE; out->tag = kTagE; out->dtype = RAD_E8M0; out->rank = 1; out->shape[0] = m2_emax_off(N, K / 32) + 4 * N; out->align = 0; out->bytes = out->shape[0]; return RAD_OK; } return RAD_E_UNSUPPORTED; } int gemm_m2_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const RadTensor* planes, int n, void* dst, int64_t dst_bytes) { RadLayout lay{}; const int st = gemm_m2_layout(p, n_p, operand, enc, sel, planes, n, &lay); if (st != RAD_OK) return st; if (!lay.tag || (std::strcmp(lay.tag, kTag) && std::strcmp(lay.tag, kTagT) && std::strcmp(lay.tag, kTagE))) return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes); if (dst_bytes != lay.bytes) return RAD_E_SHAPE; uint8_t* d = (uint8_t*)dst; const int64_t N = planes[0].shape[0]; if (std::strcmp(lay.tag, kTagE) == 0) { const int64_t kg = planes[0].shape[1]; const uint8_t* e = (const uint8_t*)planes[0].data; std::memset(d, 0, (size_t)dst_bytes); tile_groups(e, d, N, kg, true); int32_t* mx = (int32_t*)(d + m2_emax_off(N, kg)); for (int64_t r = 0; r < N; ++r) { int32_t m = 0; for (int64_t g = 0; g < kg; ++g) m = std::max(m, e[r * kg + g]); mx[r] = m; } return RAD_OK; } const bool tiled = std::strcmp(lay.tag, kTagT) == 0; for (int i = 0; i < n; ++i) { const int64_t b = planes[i].shape[0] * rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]); if (tiled && i == 0) tile_codes((const uint8_t*)planes[0].data, d, N, planes[0].shape[1], true); else if (tiled) tile_groups((const uint8_t*)planes[i].data, d, N, planes[i].shape[1], true); else std::memcpy(d, planes[i].data, (size_t)b); d += b; } return RAD_OK; } int gemm_m2_unrelayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const void* src, int64_t src_bytes, const RadTensor* planes, int n) { RadLayout lay{}; const int st = gemm_m2_layout(p, n_p, operand, enc, sel, planes, n, &lay); if (st != RAD_OK) return st; if (!lay.tag || (std::strcmp(lay.tag, kTag) && std::strcmp(lay.tag, kTagT) && std::strcmp(lay.tag, kTagE))) return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n); if (src_bytes != lay.bytes) return RAD_E_SHAPE; const uint8_t* s = (const uint8_t*)src; const int64_t N = planes[0].shape[0]; if (std::strcmp(lay.tag, kTagE) == 0) { /* the column maxima are derived: dropped */ tile_groups(s, (uint8_t*)planes[0].data, N, planes[0].shape[1], false); return RAD_OK; } const bool tiled = std::strcmp(lay.tag, kTagT) == 0; for (int i = 0; i < n; ++i) { const int64_t b = planes[i].shape[0] * rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]); if (tiled && i == 0) tile_codes(s, (uint8_t*)planes[0].data, N, planes[0].shape[1], false); else if (tiled) tile_groups(s, (uint8_t*)planes[i].data, N, planes[i].shape[1], false); else std::memcpy(planes[i].data, s, (size_t)b); s += b; } return RAD_OK; } /* ------------------------------------------------------------------ the kernel */ int gemm_nt_q_m2(const RadArgs* a, RadStream st) { if (!a || a->n_t < 5) return RAD_E_INVAL; const RadTensor *A = &a->t[0], *B = &a->t[2], *Y = &a->t[4]; const RadTensor *AS = rad_arg_in(a, 1), *BS = rad_arg_in(a, 3); if (!A->data || !B->data || !Y->data) return RAD_E_INVAL; if (rad_arg_in(a, 5)) return RAD_E_UNSUPPORTED; /* a_sum belongs to the asym grids */ if (A->dtype != RAD_F8E4M3 || B->dtype != RAD_U8 || !AS || !BS || !AS->data || !BS->data || BS->dtype != RAD_E8M0 || BS->rank < 2) return RAD_E_DTYPE; const int64_t K = rad_args_geti_or(a, "K", 0), N = rad_args_geti_or(a, "N", 0); if (K <= 0 || K % 32 || N <= 0) return RAD_E_SHAPE; const int64_t M = rad_tensor_numel(Y) / N; if (M <= 0) return RAD_E_SHAPE; const int64_t kb = K / 32, akb = (K + 127) / 128; const int64_t pb1 = rad_args_geti_or(a, "pb1", 0), pb2 = rad_args_geti_or(a, "pb2", 0); const bool parted = pb1 > 0 || pb2 > 0; const int64_t e1 = pb1 > 0 ? std::min(pb1, N) : N; const int64_t e2 = pb2 > 0 ? std::max(e1, std::min(pb2, N)) : N; const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1); if (parted && ((e1 % 16) || (e2 % 16))) return RAD_E_SHAPE; if (parted && e1 == e2 && e2 < N) return RAD_E_SHAPE; const int64_t acols = parts * K, scols = parts * akb; /* `b` is the stored codes-then-meta blob: a rank-1 u8 of its bytes (the shape hook's * description), or the weight's logical [N, K] u8 view of the same bytes (the engine's). */ const bool blob = (B->rank == 1 && rad_tensor_numel(B) == N * (K / 2 + kb)) || (B->rank == 2 && B->shape[0] == N && B->shape[1] == K); if (!blob) return RAD_E_SHAPE; if (rad_tensor_numel(A) < M * acols || rad_tensor_numel(BS) < N * kb || rad_tensor_numel(AS) < M * scols) return RAD_E_SHAPE; if (A->rank >= 2 && A->shape[A->rank - 1] != acols) return RAD_E_SHAPE; const int64_t ars = row_stride(A, acols), acs = last_stride(A); const int64_t srs = row_stride(AS, scols), scs = last_stride(AS); const int64_t brs = row_stride(BS, kb); const int64_t yrs = row_stride(Y, N), ycs = last_stride(Y); const char *ab = (const char*)A->data, *sb = (const char*)AS->data; const uint8_t* codes0 = (const uint8_t*)B->data; const uint8_t* meta0 = codes0 + N * (K / 2); const uint8_t* expe0 = (const uint8_t*)BS->data; char* yb = (char*)Y->data; const uint32_t adt = A->dtype, sdt = AS->dtype, ydt = Y->dtype; const bool tl = m2_tiled(N, K); /* the stored form gemm_m2_layout asked for */ if (tl && brs != kb) return RAD_E_SHAPE; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; const int64_t total = M * N; constexpr size_t wg = 128; const size_t groups = (size_t)((total + (int64_t)wg - 1) / (int64_t)wg); return submit([&] { syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) { const int64_t g = (int64_t)it.get_global_id(0); if (g >= total) return; const int64_t n = g % N, m = g / N; const int64_t part = n < e1 ? 0 : (n < e2 ? 1 : 2); const int64_t a0 = m * ars + part * K * acs, s0 = m * srs + part * akb * scs; constexpr float e2m1[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f }; float acc = 0.0f; for (int64_t k = 0; k < K; ++k) { const uint8_t cb = tl ? codes0[m2t_off(n, k >> 1, K)] : codes0[n * (K / 2) + (k >> 1)]; const uint8_t c = (uint8_t)((cb >> (4 * (k & 1))) & 0xF); const float cv = (c & 8) ? -e2m1[c & 7] : e2m1[c & 7]; const int64_t gk = k >> 5; const float ev = sycl::ldexp(1.0f, (int)expe0[tl ? m2e_off(n, gk, kb) : n * brs + gk] - 127); const uint8_t mt = meta0[tl ? m2e_off(n, gk, kb) : n * kb + gk]; const float km = 1.0f + 0.25f * (float)((mt >> (2 * ((k & 31) >> 3))) & 3); const float w = cv * ev * km; acc += rad_load_f32(ab, adt, a0 + k * acs) * rad_load_f32(sb, sdt, s0 + (k >> 7) * scs) * w; } rad_store_f32(yb, ydt, m * yrs + n * ycs, acc); }); }); } } // namespace xe2