/* xe2_givens.cpp -- givens_dense_quant: ParoQuant's activation side for the M2XFP4 linears. * Correctness first, libref's ref_givens_dense_quant (ref_quant.cpp) step for step: * * per (row, part p, 128-column group gi): * in[i] = x[gi*128 + i] / D[p*n + gi*128 + i] f32 * y[j] = (float) sum over ascending i of (double)T[p][gi][j][i] * (double)in[i] * scale = amax(|y|) / qmax (1 for an all-zero group), code = clamp(y / scale, +-qmax) * * qmax 448 for an E4M3 `q`, 127 (rounded to nearest even) for an i8 one; `s` holds a scale per * (row, part, group). Two forms: the fast one below (a float-pair sum; the card's f64 is slow) and * the exact one (XE2_EXACT=1, or a rotation that is not f16): a work-item an output j, its dot in * double in libref's order, so the codes and scales are libref's. */ #include "xe2_common.h" #include "xe2_ops.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; } constexpr int kG = 128; } // namespace int givens_dense_quant(const RadArgs* a, RadStream st) { if (!rad_args_have(a, 5)) return RAD_E_INVAL; const RadTensor *X = &a->t[0], *T = &a->t[1], *D = &a->t[2], *Q = &a->t[3], *S = &a->t[4]; const int64_t n = rad_args_geti_or(a, "n", 0), P = rad_args_geti_or(a, "parts", 1); const int64_t g = rad_args_geti_or(a, "group", 0); if (n <= 0 || P <= 0 || g != kG || n % g != 0) return RAD_E_SHAPE; const bool i8 = Q->dtype == RAD_I8; if (!i8 && Q->dtype != RAD_F8E4M3) return RAD_E_DTYPE; if (std::strcmp(rad_args_gets_or(a, "out", ""), i8 ? "i8" : "fp8") != 0) return RAD_E_DTYPE; const int64_t rows = rad_tensor_numel(X) / n, ngrp = n / g; if (rows <= 0 || rad_tensor_numel(T) < P * n * g || rad_tensor_numel(D) < P * n || rad_tensor_numel(Q) < rows * P * n || rad_tensor_numel(S) < rows * P * ngrp) return RAD_E_SHAPE; if (!rad_tensor_is_contiguous(T) || !rad_tensor_is_contiguous(D)) return refuse("givens_dense_quant: tables not contiguous"); const float qmax = i8 ? 127.0f : 448.0f; const int64_t xrs = row_stride(X, n), xcs = last_stride(X); const int64_t qrs = row_stride(Q, P * n), qcs = last_stride(Q); const int64_t srs = row_stride(S, P * ngrp), scs = last_stride(S); const char *xb = (const char*)X->data, *tb = (const char*)T->data, *db = (const char*)D->data; char *qb = (char*)Q->data, *sb = (char*)S->data; const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; /* XE2_GIVENS_BATCHED=1: the prefill's three-pass batched form; XE2_GIVENS_PAIR=1: the float-pair * form at decode sizes */ static const bool pair_s = std::getenv("XE2_GIVENS_PAIR") != nullptr; static const bool batched_s = std::getenv("XE2_GIVENS_BATCHED") != nullptr; if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && rows >= 17 && !exact_only() && xcs == 1 && rad_tensor_is_contiguous(T) && batched_s) { /* Prefill: the rotation as a batch of GEMMs on XMX, one a (part, group): * y[r][j] = sum_i T[p][gi][j][i] * fp16(x[r][gi*128 + i] / D[p][gi*128 + i]). The * activation rounded once to fp16 (2^-11) where libref sums in double: a fast kernel, held * to the tolerance and the KL bound. Then libref's quantiser a (row, part, group). */ const int64_t mp = (rows + 127) / 128 * 128; const int64_t in_e = P * mp * n; /* fp16 inputs [P][mp][n] */ const int64_t inbytes = (in_e * 2 + 255) / 256 * 256; float* ws = workspace(*q, (size_t)(inbytes / 4 + P * ngrp * mp * kG + 64), 7); if (!ws) return RAD_E_DEVICE; sycl::half* in16 = (sycl::half*)ws; float* yv = (float*)((char*)ws + inbytes); /* [P * ngrp][mp][128] */ int rc = submit([&] { q->parallel_for(sycl::range<1>((size_t)in_e), [=](sycl::id<1> id) { const int64_t c = (int64_t)id[0] % n, r = ((int64_t)id[0] / n) % mp, p = (int64_t)id[0] / (n * mp); in16[id[0]] = r < rows ? (sycl::half)(rad_load_f32(xb, xdt, r * xrs + c) / rad_load_f32(db, ddt, p * n + c)) : (sycl::half)0.0f; }); }); if (rc < 0) return rc; /* batch b = p * ngrp + gi: A = in16[p] + gi * 128 (row stride n), B = T[p][gi] [128][128] */ for (int64_t p = 0; p < P && rc >= 0; ++p) rc = xmx_f16_batched(*q, in16 + p * mp * n, n, kG, (const sycl::half*)tb + p * ngrp * kG * kG, kG, kG * kG, yv + p * ngrp * mp * kG, mp * kG, rows, kG, kG, ngrp); if (rc < 0) return rc; return submit([&] { q->parallel_for(sycl::nd_range<1>{(size_t)(rows * P * ngrp) * kG, kG}, [=](sycl::nd_item<1> it) { const int64_t gid = (int64_t)it.get_group(0); const int64_t gi = gid % ngrp, p = (gid / ngrp) % P, r = gid / (ngrp * P); const int j = (int)it.get_local_id(0); const float y = yv[((p * ngrp + gi) * mp + r) * kG + j]; const float amax = sycl::reduce_over_group(it.get_group(), sycl::fabs(y), sycl::maximum()); const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f; const float inv = 1.0f / scl; float v = y * inv; if (v > qmax) v = qmax; if (v < -qmax) v = -qmax; if (i8) v = sycl::rint(v); rad_store_f32(qb, qdt, r * qrs + (p * n + gi * kG + j) * qcs, v); if (j == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl); }); }); } if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && !exact_only() && !pair_s && (rows <= 16 || !batched_s)) { /* Decode and the verify (rows <= 16): a (part, group)'s rotation as one small GEMM on * register DPAS (dpas_f16), y[r][j] = sum_i T[j][i] fp16(x[r][i] / D[i]) -- the prefill * form's arithmetic (activation rounded once to fp16, f32 sums), held to the same * tolerance and the KL bound. A work-group a (part, group): the rows' inputs in local * memory as A columns, sub-group s the outputs 16 s .. 16 s + 15, a lane's B column its * output's row of T (32 contiguous bytes a K16 step), so T is read once for all rows; then * libref's quantiser a (row, part, group). The float-pair form (XE2_GIVENS_PAIR=1) walked * the rows in turn on 80 work-groups at n = 5120. */ /* any row count: a work-group a (part, group, 16-row block), T re-read from the cache for * each block (the prefill's fp16 copy, strided batch GEMM and quantiser passes, 1.4 ms a * 941-row call at n = 5120, two parts) */ const int mt = rows <= 8 ? 1 : 2; const int64_t mp = mt * 8; const int64_t rblk = (rows + 15) / 16; return submit([&] { q->submit([&](sycl::handler& h) { sycl::local_accessor at(sycl::range<1>((size_t)(kG * 16)), h); sycl::local_accessor red(sycl::range<1>((size_t)(8 * 16)), h); h.parallel_for(sycl::nd_range<1>{(size_t)(P * ngrp * rblk) * kG, kG}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { const int64_t gid0 = (int64_t)it.get_group(0); const int64_t rb = gid0 / (P * ngrp), gid = gid0 % (P * ngrp); const int64_t r0 = rb * 16; const int64_t gi = gid % ngrp, p = gid / ngrp; const int t = (int)it.get_local_id(0); const sycl::sub_group sg = it.get_sub_group(); const int sgi = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id(); const int64_t c = gi * kG + t; const float dsc = rad_load_f32(db, ddt, p * n + c); for (int r = 0; r < mp; ++r) { /* at[i][r]: lane k's A column is 16 bytes */ const float v = r0 + r < rows ? rad_load_f32(xb, xdt, (r0 + r) * xrs + c * xcs) / dsc : 0.0f; at[t * mp + r] = sycl::bit_cast((sycl::half)v); } sycl::group_barrier(it.get_group()); const int j = sgi * 16 + lane; const uint16_t* trow = (const uint16_t*)tb + ((p * ngrp + gi) * kG + j) * kG; xe2_float8 acc[2] = { xe2_float8(0.0f), xe2_float8(0.0f) }; for (int k0 = 0; k0 < kG; k0 += 16) { const sycl::vec b0 = *(const sycl::vec*)(trow + k0); const sycl::vec b1 = *(const sycl::vec*)(trow + k0 + 8); xe2_int8 b; for (int u = 0; u < 4; ++u) { b[u] = b0[u]; b[u + 4] = b1[u]; } for (int i = 0; i < mt; ++i) { const sycl::vec av = *(const sycl::vec*)&at[(k0 + lane) * mp + i * 8]; xe2_short8 a; for (int m = 0; m < 8; ++m) a[m] = (short)av[m]; acc[i] = dpas_f16(a, b, acc[i]); } } /* each row's amax over the group's 128 outputs: the sub-groups' maxima in * local memory, read by all */ for (int i = 0; i < mt; ++i) { for (int m = 0; m < 8; ++m) { const float am = sycl::reduce_over_group(sg, sycl::fabs(acc[i][m]), sycl::maximum()); if (lane == 0) red[sgi * 16 + i * 8 + m] = am; } } sycl::group_barrier(it.get_group()); for (int i = 0; i < mt; ++i) for (int m = 0; m < 8; ++m) { const int64_t r = r0 + i * 8 + m; if (r >= rows) break; float amax = 0.0f; for (int u = 0; u < 8; ++u) amax = sycl::fmax(amax, red[u * 16 + i * 8 + m]); const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f; const float inv = 1.0f / scl; float v = acc[i][m] * inv; if (v > qmax) v = qmax; if (v < -qmax) v = -qmax; if (i8) v = sycl::rint(v); rad_store_f32(qb, qdt, r * qrs + (p * n + gi * kG + j) * qcs, v); if (t == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl); } }); }); }); } if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16)) { /* The fast form. A work-group a (part, group) and up to kRows rows; sub-group s takes the * outputs j = 16 s .. 16 s + 15, and for each its lanes read row j of T in one coalesced * 256-byte step (8 f16 a lane). libref's double sum becomes a float pair: each lane's eight * products split exactly (two-product by fma) and added by two-sum, then the sixteen pairs * combined in a fixed tree, also by two-sum -- ~48 significant bits, rounded once to f32: * libref's y unless the exact sum sits within ~2^-24 ulp of an f32 rounding boundary. So * this is a fast kernel, held to rad-kbench's tolerance and the KL bound; XE2_EXACT=1 takes * the double form below. */ constexpr int64_t kRows = 16; const int64_t rblocks = (rows + kRows - 1) / kRows; const size_t groups = (size_t)(P * ngrp * rblocks); if (!exact_only()) return submit([&] { q->submit([&](sycl::handler& h) { sycl::local_accessor in(sycl::range<1>(kG), h); sycl::local_accessor yv(sycl::range<1>(kG), h); h.parallel_for(sycl::nd_range<1>{groups * kG, kG}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { const int64_t gid = (int64_t)it.get_group(0); const int64_t rb = gid % rblocks, pg = gid / rblocks; const int64_t gi = pg % ngrp, p = pg / ngrp; const int t = (int)it.get_local_id(0); auto grp = it.get_group(); const sycl::sub_group sg = it.get_sub_group(); const int sgi = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id(); const uint16_t* t0 = (const uint16_t*)tb + (p * ngrp + gi) * kG * kG; const int64_t c = gi * kG + t; const float dsc = rad_load_f32(db, ddt, p * n + c); auto two_sum = [](float a, float b, float& s, float& e) { s = a + b; const float bb = s - a; e = (a - (s - bb)) + (b - bb); }; for (int64_t r = rb * kRows; r < sycl::min(rows, rb * kRows + kRows); ++r) { sycl::group_barrier(grp); in[t] = rad_load_f32(xb, xdt, r * xrs + c * xcs) / dsc; sycl::group_barrier(grp); float xin[8]; for (int u = 0; u < 8; ++u) xin[u] = in[lane * 8 + u]; for (int jj = 0; jj < 16; ++jj) { const int j = sgi * 16 + jj; const sycl::vec tv = *(const sycl::vec*)(t0 + j * kG + lane * 8); float hi = 0.0f, lo = 0.0f; for (int u = 0; u < 8; ++u) { const float tw = (float)sycl::bit_cast(tv[u]); const float pr = tw * xin[u]; const float pe = sycl::fma(tw, xin[u], -pr); float sv, se; two_sum(hi, pr, sv, se); hi = sv; lo += se + pe; } for (int off = 8; off > 0; off >>= 1) { const float h2 = sycl::shift_group_left(sg, hi, off); const float l2 = sycl::shift_group_left(sg, lo, off); float sv, se; two_sum(hi, h2, sv, se); hi = sv; lo = lo + l2 + se; } if (lane == 0) yv[j] = hi + lo; } sycl::group_barrier(grp); const float y = yv[t]; const float amax = sycl::reduce_over_group(grp, sycl::fabs(y), sycl::maximum()); const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f; const float inv = 1.0f / scl; float v = y * inv; if (v > qmax) v = qmax; if (v < -qmax) v = -qmax; if (i8) v = sycl::rint(v); rad_store_f32(qb, qdt, r * qrs + (p * n + c) * qcs, v); if (t == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl); } }); }); }); } /* The exact form, libref's double sum in libref's order: a work-group a (row, part, group). */ const size_t groups = (size_t)(rows * P * ngrp); return submit([&] { q->submit([&](sycl::handler& h) { sycl::local_accessor in(sycl::range<1>(kG), h); h.parallel_for(sycl::nd_range<1>{groups * kG, kG}, [=](sycl::nd_item<1> it) { const int64_t gid = (int64_t)it.get_group(0); const int64_t gi = gid % ngrp, p = (gid / ngrp) % P, r = gid / (ngrp * P); const int j = (int)it.get_local_id(0); const int64_t c = gi * kG + j; in[j] = rad_load_f32(xb, xdt, r * xrs + c * xcs) / rad_load_f32(db, ddt, p * n + c); sycl::group_barrier(it.get_group()); const int64_t t0 = ((p * ngrp + gi) * kG + j) * kG; double acc = 0.0; for (int i = 0; i < kG; ++i) acc += (double)rad_load_f32(tb, tdt, t0 + i) * (double)in[i]; const float y = (float)acc; const float amax = sycl::reduce_over_group(it.get_group(), sycl::fabs(y), sycl::maximum()); const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f; const float inv = 1.0f / scl; float v = y * inv; if (v > qmax) v = qmax; if (v < -qmax) v = -qmax; if (i8) v = sycl::rint(v); rad_store_f32(qb, qdt, r * qrs + (p * n + c) * qcs, v); if (j == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl); }); }); }); } } // namespace xe2