/* xe2_norm.cpp -- the RMS norms and the block-scaled fp8 activation quantisers. * * One work-group a row. The sum of squares is libref's: f32, in ascending index order, by one * work-item and broadcast to the group. A tree reduction rounds differently, and on MiniCPM5-2B's * real weights that difference moved enough normed values across a bf16 tie, and from there the * E4M3 codes, to fail rmsnorm_quant_fp8's tolerance at M=1 (rel 7.7e-3 against 4e-3). The serial * sum is n multiply-adds, small beside the row's loads; a faster ordered form is Phase 4's. * A quantisation group belongs to one sub-group, whose lanes stride over it for the amax and the * codes. * * The arithmetic is libref's, step for step (ref_elementwise.cpp, ref_quant.cpp): * rmsnorm y = ((x * s) * (w + wadd)) * mult, s = 1 / sqrt(mean(x^2) + eps) * quant_act_fp8 per group: scale = amax * (1/448) (1 for an all-zero group), * code = clamp(x * (1/scale), +-448), stored RNE * rmsnorm_quant_fp8 the residual sum is stored first and read back (so the norm sees the * narrowed sum), the normed row is narrowed to bf16, and the codes are * taken over those bf16 values; an int8 `q` takes amax / 127 instead * gated_quant_fp8 silu(gate) * up or sigmoid(gate) * up over a packed [M, 2n] gate_up, * narrowed to bf16, then quantised as above */ #include "xe2_common.h" #include "xe2_ops.h" #include namespace xe2 { namespace { constexpr size_t kWg = 256; /* work-items a row */ /* sigmoid exactly as libref's act_sigmoid: exp in double, rounded once to f32 (ref_exp). An f32 * exp is within an ulp, inside rad-kbench's tolerance, but an ulp in silu now and then moves the * bf16 narrowing and then an E4M3 code; the engine-level oracle (RADIANCE_OP_ORACLE) caught one * such code in some of MiniCPM5-2B's 42 layers, enough to flip a greedy near-tie later. */ inline float sigmoidf(float x) { return 1.0f / (1.0f + (float)sycl::exp((double)-x)); } /* The sum of src(i)^2 over i < n in ascending order, f32, as libref sums it, computed by work-item * 0 and returned to every work-item of the group. */ template inline float ordered_sumsq(const sycl::nd_item<1>& it, const F& src, int64_t n) { float ss = 0.0f; if (it.get_local_id(0) == 0) for (int64_t i = 0; i < n; ++i) { const float v = src(i); ss += v * v; } return sycl::group_broadcast(it.get_group(), ss, 0); } /* One group's codes and scale, by the sub-group `sg` over values v(k0 + i), i < len. * Returns the scale (the same in every lane). */ template inline float quant_group(const sycl::sub_group& sg, const V& v, int64_t k0, int64_t len, float top, char* qbase, uint32_t qdt, int64_t qoff) { const int lane = (int)sg.get_local_linear_id(); float amax = 0.0f; for (int64_t i = lane; i < len; i += kSub) amax = sycl::fmax(amax, sycl::fabs(v(k0 + i))); amax = sycl::reduce_over_group(sg, amax, sycl::maximum()); const float sc = amax > 0.0f ? amax * (1.0f / top) : 1.0f; const float inv = 1.0f / sc; for (int64_t i = lane; i < len; i += kSub) { float c = v(k0 + i) * inv; if (c > top) c = top; if (c < -top) c = -top; rad_store_f32(qbase, qdt, qoff + k0 + i, c); } return sc; } /* The scale plane, one value a group, rows of `ngrp`. */ int scale_view(const RadTensor* s, int64_t rows, int64_t ngrp, Rows* out, const char* what) { if (rad_tensor_numel(s) < rows * ngrp) return RAD_E_SHAPE; return rows_view(s, ngrp, out, what); } /* The gain, read as one row of n, or absent. */ struct Gain { const char* base = nullptr; uint32_t dtype = 0; int64_t stride = 1; }; int gain_view(const RadTensor* w, int64_t n, Gain* out, const char* what) { if (!w) return RAD_OK; if (rad_tensor_numel(w) < n) return RAD_E_SHAPE; out->base = (const char*)w->data; out->dtype = w->dtype; out->stride = w->rank ? w->stride[w->rank - 1] : 1; (void)what; return RAD_OK; } } // namespace /* ------------------------------------------------------------------ rmsnorm */ int rmsnorm(const RadArgs* a, RadStream st) { if (!a || a->n_t < 3 || !a->t[0].data || !a->t[2].data) return RAD_E_INVAL; const RadTensor* w = rad_arg_in(a, 1); const float wadd = (float)rad_args_getf_or(a, "wadd", 0.0); if (!w && wadd != 0.0f) return RAD_E_INVAL; const int64_t n = rad_args_geti_or(a, "n", w && w->rank ? w->shape[w->rank - 1] : 0); const float eps = (float)rad_args_getf_or(a, "eps", 1e-6); const float mult = (float)rad_args_getf_or(a, "mult", 1.0); if (n <= 0) return RAD_E_SHAPE; Rows x, y; Gain g; if (int rc = rows_view(&a->t[0], n, &x, "rmsnorm x"); rc < 0) return rc; if (int rc = rows_view(&a->t[2], n, &y, "rmsnorm y"); rc < 0) return rc; if (int rc = gain_view(w, n, &g, "rmsnorm w"); rc < 0) return rc; const int64_t rows = x.rows; if (rows <= 0 || y.rows < rows) return RAD_E_SHAPE; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; 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); const float ss = ordered_sumsq( it, [&](int64_t i) { return rad_load_f32(x.base, x.dtype, r * x.pitch + i); }, n); const float s = 1.0f / sycl::sqrt(ss / (float)n + eps); for (int64_t i = t; i < n; i += 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); } }); }); } /* ------------------------------------------------------------------ quant_act_fp8 */ int quant_act_fp8(const RadArgs* a, RadStream st) { if (!rad_args_have(a, 3)) return RAD_E_INVAL; const int64_t n = rad_args_geti_or(a, "n", 0); if (n <= 0) return RAD_E_SHAPE; int64_t g = rad_args_geti_or(a, "group", 0); if (g <= 0 || g > n) g = n; const int64_t ngrp = (n + g - 1) / g; Rows x, qv, sv; if (int rc = rows_view(&a->t[0], n, &x, "quant_act_fp8 x"); rc < 0) return rc; const int64_t rows = x.rows; if (rows <= 0 || rad_tensor_numel(&a->t[1]) < rows * n) return RAD_E_SHAPE; if (int rc = rows_view(&a->t[1], n, &qv, "quant_act_fp8 q"); rc < 0) return rc; if (int rc = scale_view(&a->t[2], rows, ngrp, &sv, "quant_act_fp8 scale"); rc < 0) return rc; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; constexpr int kSubs = (int)(kWg / kSub); 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 sycl::sub_group sg = it.get_sub_group(); const int sgi = (int)sg.get_group_linear_id(); auto v = [&](int64_t i) { return rad_load_f32(x.base, x.dtype, r * x.pitch + i); }; for (int64_t gi = sgi; gi < ngrp; gi += kSubs) { const int64_t k0 = gi * g, len = sycl::min(g, n - k0); const float sc = quant_group(sg, v, k0, len, 448.0f, qv.base, qv.dtype, r * qv.pitch); if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc); } }); }); } /* ------------------------------------------------------------------ rmsnorm_quant_fp8 */ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) { if (!a || a->n_t < 5) return RAD_E_INVAL; const RadTensor *X = &a->t[0], *W = &a->t[2], *Q = &a->t[3], *S = &a->t[4]; const RadTensor* R = rad_arg_in(a, 1); const RadTensor* O = rad_arg_in(a, 5); if (!X->data || !W->data || !Q->data || !S->data) return RAD_E_INVAL; const int64_t n = rad_args_geti_or(a, "n", W->rank ? W->shape[W->rank - 1] : 0); const float eps = (float)rad_args_getf_or(a, "eps", 1e-6); const float wadd = (float)rad_args_getf_or(a, "wadd", 0.0); if (n <= 0) return RAD_E_SHAPE; int64_t g = rad_args_geti_or(a, "group", 0); if (g <= 0 || g > n) g = n; const int64_t ngrp = (n + g - 1) / g; Rows x, qv, sv, res, ob; Gain gw; if (int rc = rows_view(X, n, &x, "rmsnorm_quant_fp8 x"); rc < 0) return rc; const int64_t rows = x.rows; if (rows <= 0 || rad_tensor_numel(Q) < rows * n) return RAD_E_SHAPE; if (int rc = rows_view(Q, n, &qv, "rmsnorm_quant_fp8 q"); rc < 0) return rc; if (int rc = scale_view(S, rows, ngrp, &sv, "rmsnorm_quant_fp8 scale"); rc < 0) return rc; if (int rc = gain_view(W, n, &gw, "rmsnorm_quant_fp8 w"); rc < 0) return rc; const bool has_res = R != nullptr, has_ob = O != nullptr; if (has_res) { if (rad_tensor_numel(R) < rows * n) return RAD_E_SHAPE; if (int rc = rows_view(R, n, &res, "rmsnorm_quant_fp8 residual"); rc < 0) return rc; } if (has_ob) { if (rad_tensor_numel(O) < rows * n) return RAD_E_SHAPE; if (int rc = rows_view(O, n, &ob, "rmsnorm_quant_fp8 out_bf16"); rc < 0) return rc; } const float top = Q->dtype == RAD_I8 ? 127.0f : 448.0f; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; constexpr int kSubs = (int)(kWg / kSub); 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); /* The residual sum, stored first; the rest reads it back narrowed. Other work-items * read these elements below, hence the barrier. */ if (has_res) { for (int64_t i = t; i < n; i += kWg) { const float s = rad_load_f32(x.base, x.dtype, r * x.pitch + i) + rad_load_f32(res.base, res.dtype, r * res.pitch + i); rad_store_f32(res.base, res.dtype, r * res.pitch + i, s); } sycl::group_barrier(it.get_group()); } auto src = [&](int64_t i) { return has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + i) : rad_load_f32(x.base, x.dtype, r * x.pitch + i); }; const float ss = ordered_sumsq(it, src, n); const float rs = 1.0f / sycl::sqrt(ss / (float)n + eps); /* The normed value narrowed to bf16, as the unfused pair would write it. */ auto v = [&](int64_t i) { const float w = rad_load_f32(gw.base, gw.dtype, i * gw.stride) + wadd; return rad_bf16_to_f32(rad_f32_to_bf16(src(i) * rs * w)); }; if (has_ob) for (int64_t i = t; i < n; i += kWg) rad_store_f32(ob.base, ob.dtype, r * ob.pitch + i, v(i)); const sycl::sub_group sg = it.get_sub_group(); const int sgi = (int)sg.get_group_linear_id(); for (int64_t gi = sgi; gi < ngrp; gi += kSubs) { const int64_t k0 = gi * g, len = sycl::min(g, n - k0); const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch); if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc); } }); }); } /* ------------------------------------------------------------------ gated_quant_fp8 */ int gated_quant_fp8(const RadArgs* a, RadStream st) { if (!rad_args_have(a, 3)) return RAD_E_INVAL; const RadTensor *GU = &a->t[0], *Q = &a->t[1], *S = &a->t[2]; const RadTensor* O = rad_arg_in(a, 3); const int64_t n = rad_args_geti_or(a, "n", 0); if (n <= 0) return RAD_E_SHAPE; const char* act_s = rad_args_gets_or(a, "act", nullptr); int act = (int)rad_args_geti_or(a, "mode", 0); if (act_s) act = !std::strcmp(act_s, "silu") ? 0 : !std::strcmp(act_s, "sigmoid") ? 1 : -1; if (act != 0 && act != 1) return RAD_E_INVAL; int64_t g = rad_args_geti_or(a, "group", 0); if (g <= 0 || g > n) g = n; const int64_t ngrp = (n + g - 1) / g; Rows gu, qv, sv, ob; if (int rc = rows_view(GU, 2 * n, &gu, "gated_quant_fp8 gate_up"); rc < 0) return rc; const int64_t rows = gu.rows; if (rows <= 0 || rad_tensor_numel(Q) < rows * n) return RAD_E_SHAPE; if (int rc = rows_view(Q, n, &qv, "gated_quant_fp8 q"); rc < 0) return rc; if (int rc = scale_view(S, rows, ngrp, &sv, "gated_quant_fp8 scale"); rc < 0) return rc; const bool has_ob = O != nullptr; if (has_ob) { if (rad_tensor_numel(O) < rows * n) return RAD_E_SHAPE; if (int rc = rows_view(O, n, &ob, "gated_quant_fp8 out_bf16"); rc < 0) return rc; } const float top = Q->dtype == RAD_I8 ? 127.0f : 448.0f; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; constexpr int kSubs = (int)(kWg / kSub); 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); auto v = [&](int64_t i) { const float gt = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + i); const float up = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + n + i); const float sg = sigmoidf(gt); return rad_bf16_to_f32(rad_f32_to_bf16((act == 0 ? gt * sg : sg) * up)); }; if (has_ob) for (int64_t i = t; i < n; i += kWg) rad_store_f32(ob.base, ob.dtype, r * ob.pitch + i, v(i)); const sycl::sub_group sg = it.get_sub_group(); const int sgi = (int)sg.get_group_linear_id(); for (int64_t gi = sgi; gi < ngrp; gi += kSubs) { const int64_t k0 = gi * g, len = sycl::min(g, n - k0); const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch); if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc); } }); }); } } // namespace xe2