Archived
- libxe2 serves `add_rmsnorm` (device-only; libxe2 now publishes its schema, libr4d's spelling): x += a then rmsnorm of the x written in one pass, at rmsnorm's own bf16 partition, so both outputs are byte-identical to the pair here. NormQuantFP8's plain form takes it where it resolves (one launch instead of `add` + `rmsnorm`, ~150 a round). - AttnGatedFP8: under the plain norm an out projection that reads no codes skips the quantiser after the gate's sigmoid + mul. Greedy output 8/8 identical to the previous commit; decode proxy 81.2 -> 82.5 tok/s; declared ops 2046 -> 1902. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
812 lines
44 KiB
C++
812 lines
44 KiB
C++
/* 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 <cstring>
|
|
|
|
namespace xe2 {
|
|
|
|
namespace {
|
|
|
|
constexpr size_t kWg = 256; /* work-items a row */
|
|
constexpr int64_t kRowMax = 8192; /* a row rmsnorm_quant_fp8 keeps in local memory */
|
|
|
|
/* 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, returned to every
|
|
* work-item of the group. The squares are formed in parallel into local memory a chunk at a time
|
|
* and work-item 0 adds them in index order: the same additions as a serial loop, bit for bit, but
|
|
* reading local memory instead of waiting on one global load after another (that serial walk was
|
|
* 13% of a MiniCPM5 decode step, 360 us a call). */
|
|
constexpr int64_t kSqChunk = 2048;
|
|
|
|
template <class F>
|
|
inline float ordered_sumsq(const sycl::nd_item<1>& it, const F& src, int64_t n) {
|
|
auto grp = it.get_group();
|
|
float* sq = *sycl::ext::oneapi::group_local_memory_for_overwrite<float[kSqChunk]>(grp);
|
|
const int64_t t = (int64_t)it.get_local_id(0), wg = (int64_t)it.get_local_range(0);
|
|
float ss = 0.0f;
|
|
for (int64_t c0 = 0; c0 < n; c0 += kSqChunk) {
|
|
const int64_t cn = sycl::min(kSqChunk, n - c0);
|
|
for (int64_t i = t; i < cn; i += wg) {
|
|
const float v = src(c0 + i);
|
|
sq[i] = v * v;
|
|
}
|
|
sycl::group_barrier(grp);
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
if (sg.get_group_linear_id() == 0) {
|
|
/* Sub-group 0 takes the squares sixteen at a time, one a lane, and every lane adds them
|
|
* in index order from the lanes' registers: the same chain of additions as libref's
|
|
* serial loop, without a local-memory load per addition. */
|
|
const int lane = (int)sg.get_local_linear_id();
|
|
int64_t i = 0;
|
|
for (; i + kSub <= cn; i += kSub) {
|
|
const float v = sq[i + lane];
|
|
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
|
|
}
|
|
for (; i < cn; ++i) ss += sq[i];
|
|
}
|
|
sycl::group_barrier(grp);
|
|
}
|
|
return sycl::group_broadcast(grp, ss, 0);
|
|
}
|
|
|
|
/* One group's codes and scale, by the sub-group `sg` over values v(k0 + i), i < len; with
|
|
* `obase`, each value is also stored there (out_bf16). A group of up to kCached * 16 values is
|
|
* computed once into registers, so the amax pass and the codes read the same floats without
|
|
* evaluating v twice (for gated_quant_fp8 that is a double exp an element); a wider group
|
|
* evaluates v in each pass, which gives the same floats. Returns the scale (the same in every
|
|
* lane). */
|
|
constexpr int kCached = 8;
|
|
|
|
template <class V>
|
|
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, char* obase = nullptr,
|
|
uint32_t odt = 0, int64_t ooff = 0) {
|
|
const int lane = (int)sg.get_local_linear_id();
|
|
auto code = [&](int64_t i, float x, float inv) {
|
|
float c = x * inv;
|
|
if (c > top) c = top;
|
|
if (c < -top) c = -top;
|
|
rad_store_f32(qbase, qdt, qoff + k0 + i, c);
|
|
if (obase) rad_store_f32(obase, odt, ooff + k0 + i, x);
|
|
};
|
|
float amax = 0.0f;
|
|
if (len <= (int64_t)kCached * kSub) {
|
|
float c[kCached];
|
|
for (int j = 0; j < kCached; ++j) {
|
|
const int64_t i = lane + (int64_t)j * kSub;
|
|
c[j] = i < len ? v(k0 + i) : 0.0f;
|
|
amax = sycl::fmax(amax, sycl::fabs(c[j]));
|
|
}
|
|
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
|
|
const float sc = amax > 0.0f ? amax * (1.0f / top) : 1.0f;
|
|
const float inv = 1.0f / sc;
|
|
for (int j = 0; j < kCached; ++j) {
|
|
const int64_t i = lane + (int64_t)j * kSub;
|
|
if (i < len) code(i, c[j], inv);
|
|
}
|
|
return sc;
|
|
}
|
|
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<float>());
|
|
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) code(i, v(k0 + i), inv);
|
|
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;
|
|
/* The fast rows (D13): each work-item's squares added in index order, the partials by
|
|
* reduce_over_group's tree -- not libref's order, held to rmsnorm's tolerance (bf16 4e-3, f32
|
|
* 1e-6). libref's one chain is ~13 cycles an element on one work-item (~31 us of a 5120-wide
|
|
* decode norm's ~40); XE2_EXACT=1 keeps it (below). Rows up to 512 wide (the per-head q / k
|
|
* norms) take a sub-group each, wider ones a work-group. */
|
|
if (!exact_only()) {
|
|
/* bf16 head rows (n = 128 .. 512, a multiple of 128): a lane its n / 16 contiguous
|
|
* elements by 16-byte loads, a sub-group a row (the generic form below issues a dependent
|
|
* load an element: ~16 us for Qwen3.8's q / k norms at a verify step). */
|
|
if (n <= 512 && n % 128 == 0 && x.dtype == RAD_BF16 && y.dtype == RAD_BF16 && x.pitch % 8 == 0 &&
|
|
y.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(y.base, 16) &&
|
|
(!g.base || ((g.dtype == RAD_F32 || g.dtype == RAD_BF16) && g.stride == 1))) {
|
|
using V8 = sycl::vec<uint16_t, 8>;
|
|
constexpr int64_t kRowsWg = (int64_t)kWg / kSub;
|
|
const int nv = (int)(n / 128); /* 16-byte vectors a lane: 1 .. 4 */
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)((rows + kRowsWg - 1) / kRowsWg) * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int64_t r = (int64_t)it.get_group(0) * kRowsWg + (int64_t)sg.get_group_linear_id();
|
|
if (r >= rows) return; /* uniform over the sub-group */
|
|
const int64_t l = (int64_t)sg.get_local_linear_id();
|
|
float v[32];
|
|
float ss = 0.0f;
|
|
#pragma unroll
|
|
for (int u = 0; u < 4; ++u) {
|
|
if (u >= nv) break;
|
|
const V8 w = *(const V8*)((const uint16_t*)x.base + r * x.pitch + (l * nv + u) * 8);
|
|
#pragma unroll
|
|
for (int j = 0; j < 8; ++j) {
|
|
v[u * 8 + j] = sycl::bit_cast<float>((uint32_t)w[j] << 16);
|
|
ss += v[u * 8 + j] * v[u * 8 + j];
|
|
}
|
|
}
|
|
ss = sycl::reduce_over_group(sg, ss, sycl::plus<float>());
|
|
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
|
#pragma unroll
|
|
for (int u = 0; u < 4; ++u) {
|
|
if (u >= nv) break;
|
|
V8 o;
|
|
#pragma unroll
|
|
for (int j = 0; j < 8; ++j) {
|
|
const int64_t i = (l * nv + u) * 8 + j;
|
|
const float gw = !g.base ? 1.0f
|
|
: (g.dtype == RAD_F32 ? ((const float*)g.base)[i]
|
|
: sycl::bit_cast<float>((uint32_t)((const uint16_t*)g.base)[i] << 16)) + wadd;
|
|
o[j] = rad_f32_to_bf16(v[u * 8 + j] * s * gw * mult);
|
|
}
|
|
*(V8*)((uint16_t*)y.base + r * y.pitch + (l * nv + u) * 8) = o;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
if (n <= 512) {
|
|
constexpr int64_t kRowsWg = (int64_t)kWg / kSub;
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)((rows + kRowsWg - 1) / kRowsWg) * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int64_t r = (int64_t)it.get_group(0) * kRowsWg + (int64_t)sg.get_group_linear_id();
|
|
if (r >= rows) return; /* uniform over the sub-group */
|
|
const int l = (int)sg.get_local_linear_id();
|
|
float ss = 0.0f;
|
|
for (int64_t i = l; i < n; i += kSub) {
|
|
const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i);
|
|
ss += v * v;
|
|
}
|
|
ss = sycl::reduce_over_group(sg, ss, sycl::plus<float>());
|
|
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
|
for (int64_t i = l; i < n; i += kSub) {
|
|
const float gw = g.base ? rad_load_f32(g.base, g.dtype, i * g.stride) + wadd : 1.0f;
|
|
const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i);
|
|
rad_store_f32(y.base, y.dtype, r * y.pitch + i, v * s * gw * mult);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
/* bf16 rows up to 8192 wide: a work-item eight elements by one 16-byte load, the row in
|
|
* registers once (the generic form below walks 20 dependent loads a work-item at n = 5120,
|
|
* ~25 us for six rows; this is one load a work-item). */
|
|
const int64_t nv = n / 8, wgv = (nv + kSub - 1) / kSub * kSub;
|
|
if (x.dtype == RAD_BF16 && y.dtype == RAD_BF16 && n % 8 == 0 && wgv <= 1024 && x.pitch % 8 == 0 &&
|
|
y.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(y.base, 16) &&
|
|
(!g.base || ((g.dtype == RAD_F32 || g.dtype == RAD_BF16) && g.stride == 1))) {
|
|
using V8 = sycl::vec<uint16_t, 8>;
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wgv), (size_t)wgv},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const int64_t r = (int64_t)it.get_group(0);
|
|
const int64_t t = (int64_t)it.get_local_id(0);
|
|
const bool live = t < nv;
|
|
float v[8];
|
|
float ss = 0.0f;
|
|
if (live) {
|
|
const V8 u = *(const V8*)((const uint16_t*)x.base + r * x.pitch + t * 8);
|
|
#pragma unroll
|
|
for (int j = 0; j < 8; ++j) {
|
|
v[j] = sycl::bit_cast<float>((uint32_t)u[j] << 16);
|
|
ss += v[j] * v[j];
|
|
}
|
|
}
|
|
ss = sycl::reduce_over_group(it.get_group(), ss, sycl::plus<float>());
|
|
if (!live) return;
|
|
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
|
V8 o;
|
|
#pragma unroll
|
|
for (int j = 0; j < 8; ++j) {
|
|
const int64_t i = t * 8 + j;
|
|
const float gw = !g.base ? 1.0f
|
|
: (g.dtype == RAD_F32 ? ((const float*)g.base)[i]
|
|
: sycl::bit_cast<float>((uint32_t)((const uint16_t*)g.base)[i] << 16)) + wadd;
|
|
o[j] = rad_f32_to_bf16(v[j] * s * gw * mult);
|
|
}
|
|
*(V8*)((uint16_t*)y.base + r * y.pitch + t * 8) = o;
|
|
});
|
|
});
|
|
}
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const int64_t r = (int64_t)it.get_group(0);
|
|
const int t = (int)it.get_local_id(0);
|
|
float ss = 0.0f;
|
|
for (int64_t i = t; i < n; i += (int64_t)kWg) {
|
|
const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i);
|
|
ss += v * v;
|
|
}
|
|
ss = sycl::reduce_over_group(it.get_group(), ss, sycl::plus<float>());
|
|
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
|
for (int64_t i = t; i < n; i += (int64_t)kWg) {
|
|
const float gw = g.base ? rad_load_f32(g.base, g.dtype, i * g.stride) + wadd : 1.0f;
|
|
const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i);
|
|
rad_store_f32(y.base, y.dtype, r * y.pitch + i, v * s * gw * mult);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
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, quant_act_i8g */
|
|
namespace {
|
|
|
|
/* Codes and a scale amax / top per group: quant_act_fp8 (top 448, E4M3 codes) and quant_act_i8g
|
|
* (top 127, int8 codes rounded to nearest even and saturating) -- libref's q8_group either way. */
|
|
int quant_act_groups(const RadArgs* a, RadStream st, float top, const char* what) {
|
|
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, what); 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, what); rc < 0) return rc;
|
|
if (int rc = scale_view(&a->t[2], rows, ngrp, &sv, what); rc < 0) return rc;
|
|
sycl::queue* q = queue_of(st);
|
|
if (!q) return RAD_E_DEVICE;
|
|
constexpr int kSubs = (int)(kWg / kSub);
|
|
/* a sub-group a (row, group): the groups are independent, and a row's work-group walking its
|
|
* groups in turn ran ~5 us a group (48 us at n = 17408) */
|
|
const int64_t wpr = (ngrp + kSubs - 1) / kSubs; /* work-groups a row */
|
|
/* bf16 in, int8 out, groups of 128 (the int4 release's prefill quantiser): a lane's 8 values one
|
|
* 16-byte load and its 8 codes one 8-byte store, typed -- the generic accessors per element
|
|
* ran 677 us a call at 2048 rows of 5120. The same floats, amax, scale and rounding (round to
|
|
* nearest even and saturate, NaN to 0, rad_store_f32's rule). */
|
|
if (top == 127.0f && x.dtype == RAD_BF16 && qv.dtype == RAD_I8 && g == 128 && n % 128 == 0 &&
|
|
x.pitch % 8 == 0 && qv.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(qv.base, 8)) {
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wpr) * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const int64_t r = (int64_t)it.get_group(0) / wpr;
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int64_t gi = ((int64_t)it.get_group(0) % wpr) * kSubs + (int64_t)sg.get_group_linear_id();
|
|
if (gi >= ngrp) return; /* uniform over the sub-group */
|
|
const int lane = (int)sg.get_local_linear_id();
|
|
const int64_t k0 = gi * 128 + lane * 8;
|
|
const sycl::vec<uint16_t, 8> w = *(const sycl::vec<uint16_t, 8>*)((const uint16_t*)x.base + r * x.pitch + k0);
|
|
float c[8];
|
|
float amax = 0.0f;
|
|
for (int j = 0; j < 8; ++j) {
|
|
c[j] = rad_bf16_to_f32(w[j]);
|
|
amax = sycl::fmax(amax, sycl::fabs(c[j]));
|
|
}
|
|
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
|
|
const float sc = amax > 0.0f ? amax * (1.0f / 127.0f) : 1.0f;
|
|
const float inv = 1.0f / sc;
|
|
sycl::vec<int8_t, 8> o;
|
|
for (int j = 0; j < 8; ++j) {
|
|
float v = c[j] * inv;
|
|
if (v > 127.0f) v = 127.0f;
|
|
if (v < -127.0f) v = -127.0f;
|
|
o[j] = v != v ? (int8_t)0 : (int8_t)sycl::rint(v);
|
|
}
|
|
*(sycl::vec<int8_t, 8>*)((int8_t*)qv.base + r * qv.pitch + k0) = o;
|
|
if (lane == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
|
|
});
|
|
});
|
|
}
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wpr) * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const int64_t r = (int64_t)it.get_group(0) / wpr;
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int64_t gi = ((int64_t)it.get_group(0) % wpr) * kSubs + (int64_t)sg.get_group_linear_id();
|
|
if (gi >= ngrp) return; /* uniform over the sub-group */
|
|
auto v = [&](int64_t i) { return rad_load_f32(x.base, x.dtype, r * x.pitch + i); };
|
|
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
|
|
|
|
int quant_act_fp8(const RadArgs* a, RadStream st) { return quant_act_groups(a, st, 448.0f, "quant_act_fp8"); }
|
|
|
|
int quant_act_i8g(const RadArgs* a, RadStream st) {
|
|
if (rad_args_have(a, 3) && a->t[1].dtype != RAD_I8)
|
|
return refuse("quant_act_i8g: codes are int8, not dtype %u", a->t[1].dtype);
|
|
return quant_act_groups(a, st, 127.0f, "quant_act_i8g");
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ add_rmsnorm */
|
|
/* x += a (bf16, rounded as `add` rounds it), then y = rmsnorm(x) over the x written, at rmsnorm's
|
|
* own partition (the bf16 rows above: a work-item eight elements, the same per-item order and the
|
|
* same group reduction), so both outputs are byte-identical to `add` then `rmsnorm` here. Other
|
|
* shapes or dtypes: the two kernels in order. */
|
|
int add_rmsnorm(const RadArgs* a, RadStream st) {
|
|
if (!a || a->n_t < 4 || !a->t[0].data || !a->t[1].data || !a->t[3].data) return RAD_E_INVAL;
|
|
const RadTensor* w = rad_arg_in(a, 2);
|
|
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, ad, y;
|
|
Gain g;
|
|
if (int rc = rows_view(&a->t[0], n, &x, "add_rmsnorm x"); rc < 0) return rc;
|
|
if (int rc = rows_view(&a->t[1], n, &ad, "add_rmsnorm a"); rc < 0) return rc;
|
|
if (int rc = rows_view(&a->t[3], n, &y, "add_rmsnorm y"); rc < 0) return rc;
|
|
if (int rc = gain_view(w, n, &g, "add_rmsnorm w"); rc < 0) return rc;
|
|
const int64_t rows = x.rows;
|
|
if (rows <= 0 || y.rows < rows || ad.rows < rows) return RAD_E_SHAPE;
|
|
sycl::queue* q = queue_of(st);
|
|
if (!q) return RAD_E_DEVICE;
|
|
const int64_t nv = n / 8, wgv = (nv + kSub - 1) / kSub * kSub;
|
|
const bool fast = !exact_only() && x.dtype == RAD_BF16 && ad.dtype == RAD_BF16 && y.dtype == RAD_BF16 &&
|
|
n % 8 == 0 && n > 512 && wgv <= 1024 && x.pitch % 8 == 0 && ad.pitch % 8 == 0 &&
|
|
y.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(ad.base, 16) &&
|
|
rad_ptr_aligned(y.base, 16) &&
|
|
(!g.base || ((g.dtype == RAD_F32 || g.dtype == RAD_BF16) && g.stride == 1));
|
|
if (!fast) {
|
|
/* the pair: x += a elementwise, then rmsnorm's own launch over x */
|
|
int rc = submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg}, [=](sycl::nd_item<1> it) {
|
|
const int64_t r = (int64_t)it.get_group(0);
|
|
for (int64_t i = (int64_t)it.get_local_id(0); i < n; i += (int64_t)kWg) {
|
|
const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i) +
|
|
rad_load_f32(ad.base, ad.dtype, r * ad.pitch + i);
|
|
rad_store_f32(x.base, x.dtype, r * x.pitch + i, v);
|
|
}
|
|
});
|
|
});
|
|
if (rc != RAD_OK) return rc;
|
|
RadArgs b2 = *a;
|
|
const RadTensor tt[3] = { a->t[0], a->t[2], a->t[3] };
|
|
b2.t = tt;
|
|
b2.n_t = 3;
|
|
return rmsnorm(&b2, st);
|
|
}
|
|
using V8 = sycl::vec<uint16_t, 8>;
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wgv), (size_t)wgv},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const int64_t r = (int64_t)it.get_group(0);
|
|
const int64_t t = (int64_t)it.get_local_id(0);
|
|
const bool live = t < nv;
|
|
float v[8];
|
|
float ss = 0.0f;
|
|
if (live) {
|
|
uint16_t* xr = (uint16_t*)x.base + r * x.pitch + t * 8;
|
|
const V8 u = *(const V8*)xr;
|
|
const V8 d = *(const V8*)((const uint16_t*)ad.base + r * ad.pitch + t * 8);
|
|
V8 o;
|
|
#pragma unroll
|
|
for (int j = 0; j < 8; ++j) {
|
|
const float sum = sycl::bit_cast<float>((uint32_t)u[j] << 16) + sycl::bit_cast<float>((uint32_t)d[j] << 16);
|
|
o[j] = rad_f32_to_bf16(sum);
|
|
v[j] = sycl::bit_cast<float>((uint32_t)o[j] << 16);
|
|
ss += v[j] * v[j];
|
|
}
|
|
*(V8*)xr = o;
|
|
}
|
|
ss = sycl::reduce_over_group(it.get_group(), ss, sycl::plus<float>());
|
|
if (!live) return;
|
|
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
|
V8 o;
|
|
#pragma unroll
|
|
for (int j = 0; j < 8; ++j) {
|
|
const int64_t i = t * 8 + j;
|
|
const float gw = !g.base ? 1.0f
|
|
: (g.dtype == RAD_F32 ? ((const float*)g.base)[i]
|
|
: sycl::bit_cast<float>((uint32_t)((const uint16_t*)g.base)[i] << 16)) + wadd;
|
|
o[j] = rad_f32_to_bf16(v[j] * s * gw * mult);
|
|
}
|
|
*(V8*)((uint16_t*)y.base + r * y.pitch + t * 8) = o;
|
|
});
|
|
});
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ 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);
|
|
auto a16 = [](const void* p) { return rad_ptr_aligned(p, 16); };
|
|
const bool vec = n <= kRowMax && n % g == 0 && g == 8 * kSub && x.dtype == RAD_BF16 && x.pitch % 8 == 0 &&
|
|
a16(x.base) && (gw.dtype == RAD_BF16 || gw.dtype == RAD_F32) && gw.stride == 1 && a16(gw.base) &&
|
|
(!has_res || (res.dtype == RAD_BF16 && res.pitch % 8 == 0 && a16(res.base))) &&
|
|
(!has_ob || (ob.dtype == RAD_BF16 && ob.pitch % 8 == 0 && a16(ob.base))) &&
|
|
Q->dtype == RAD_F8E4M3 && qv.pitch % 8 == 0 && rad_ptr_aligned(qv.base, 8);
|
|
if (vec) {
|
|
const char* gbase = gw.base;
|
|
const uint32_t gdt = gw.dtype;
|
|
/* The bf16 rows of the form below, 8 values a 16-byte load or store: x, the residual and
|
|
* the gain (bf16 or f32) in one read each, the sum and its squares formed as there; a group of 128 is
|
|
* a sub-group's, 8 contiguous values a lane, and its codes are stored 8 bytes a lane. The
|
|
* same arithmetic and order as the general form -- each value's, each group's amax and
|
|
* scale, the sum of squares -- without its per-element loads, which were half its time. */
|
|
/* Sub-group s loads the groups s, s + 16, ... it later quantises (lane l the 8 values at
|
|
* 8 l of each), so the normed inputs and the gain stay in its registers between the sum and
|
|
* the codes; only the squares go to local memory (20 KB at n = 5120, so several rows run on
|
|
* a Xe core at once -- three n-wide arrays allowed two). */
|
|
constexpr int kGrpMax = (int)(kRowMax / (8 * kSub) / kSubs); /* groups a sub-group */
|
|
return submit([&] {
|
|
q->submit([&](sycl::handler& h) {
|
|
sycl::local_accessor<float, 1> sq(sycl::range<1>((size_t)n), h);
|
|
h.parallel_for(sycl::nd_range<1>{(size_t)rows * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
using V8 = sycl::vec<uint16_t, 8>;
|
|
const int64_t r = (int64_t)it.get_group(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* xr = (const uint16_t*)x.base + r * x.pitch;
|
|
uint16_t* rr_ = has_res ? (uint16_t*)res.base + r * res.pitch : nullptr;
|
|
const bool wf = gdt == RAD_F32;
|
|
V8 xv[kGrpMax], rv[kGrpMax];
|
|
sycl::vec<float, 8> wv[kGrpMax];
|
|
#pragma unroll
|
|
for (int j = 0; j < kGrpMax; ++j) {
|
|
const int64_t gi = sgi + (int64_t)j * kSubs;
|
|
const int64_t c = gi * g + lane * 8;
|
|
const int64_t cc = gi < ngrp ? c : 0;
|
|
xv[j] = *(const V8*)(xr + cc);
|
|
rv[j] = has_res ? *(const V8*)(rr_ + cc) : V8(0);
|
|
if (wf) {
|
|
const sycl::vec<float, 4> w0 = *(const sycl::vec<float, 4>*)((const float*)gbase + cc);
|
|
const sycl::vec<float, 4> w1 = *(const sycl::vec<float, 4>*)((const float*)gbase + cc + 4);
|
|
for (int u = 0; u < 4; ++u) { wv[j][u] = w0[u]; wv[j][u + 4] = w1[u]; }
|
|
} else {
|
|
const V8 w8 = *(const V8*)((const uint16_t*)gbase + cc);
|
|
for (int u = 0; u < 8; ++u) wv[j][u] = rad_bf16_to_f32(w8[u]);
|
|
}
|
|
}
|
|
float vv[kGrpMax][8];
|
|
#pragma unroll
|
|
for (int j = 0; j < kGrpMax; ++j) {
|
|
const int64_t gi = sgi + (int64_t)j * kSubs;
|
|
if (gi >= ngrp) continue;
|
|
const int64_t c = gi * g + lane * 8;
|
|
V8 so;
|
|
for (int u = 0; u < 8; ++u) {
|
|
float v = rad_bf16_to_f32(xv[j][u]);
|
|
if (has_res) {
|
|
const float s0 = v + rad_bf16_to_f32(rv[j][u]);
|
|
so[u] = rad_f32_to_bf16(s0);
|
|
v = rad_bf16_to_f32(so[u]); /* what reading it back gives */
|
|
}
|
|
vv[j][u] = v;
|
|
sq[c + u] = v * v;
|
|
wv[j][u] += wadd;
|
|
}
|
|
if (has_res) *(V8*)(rr_ + c) = so;
|
|
}
|
|
sycl::group_barrier(grp);
|
|
float ss = 0.0f;
|
|
if (sgi == 0) {
|
|
for (int64_t i = 0; i < n; i += kSub) {
|
|
const float v = sq[i + lane];
|
|
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
|
|
}
|
|
}
|
|
ss = sycl::group_broadcast(grp, ss, 0);
|
|
const float rs = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
|
#pragma unroll
|
|
for (int j = 0; j < kGrpMax; ++j) {
|
|
const int64_t gi = sgi + (int64_t)j * kSubs;
|
|
if (gi >= ngrp) continue; /* uniform */
|
|
const int64_t i0 = gi * g + lane * 8;
|
|
float c[8];
|
|
float amax = 0.0f;
|
|
for (int u = 0; u < 8; ++u) {
|
|
c[u] = rad_bf16_to_f32(rad_f32_to_bf16(vv[j][u] * rs * wv[j][u]));
|
|
amax = sycl::fmax(amax, sycl::fabs(c[u]));
|
|
}
|
|
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
|
|
const float sc = amax > 0.0f ? amax * (1.0f / 448.0f) : 1.0f;
|
|
const float inv = 1.0f / sc;
|
|
sycl::vec<uint8_t, 8> qb;
|
|
V8 ov;
|
|
for (int u = 0; u < 8; ++u) {
|
|
float cc = c[u] * inv;
|
|
if (cc > 448.0f) cc = 448.0f;
|
|
if (cc < -448.0f) cc = -448.0f;
|
|
qb[u] = rad_f32_to_fp8e4m3(cc);
|
|
ov[u] = rad_f32_to_bf16(c[u]);
|
|
}
|
|
*(sycl::vec<uint8_t, 8>*)((uint8_t*)qv.base + r * qv.pitch + i0) = qb;
|
|
if (has_ob) *(V8*)((uint16_t*)ob.base + r * ob.pitch + i0) = ov;
|
|
if (lane == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
if (n <= kRowMax) {
|
|
/* A row up to kRowMax in local memory: the (narrowed) residual sum and its squares are
|
|
* formed once from one read of x and the residual, the sum stored once; sub-group 0 adds
|
|
* the squares in index order through register broadcasts (libref's chain); the groups
|
|
* read their values from local memory. The same arithmetic as the general form below,
|
|
* without re-reading what the work-group just stored. */
|
|
return submit([&] {
|
|
q->submit([&](sycl::handler& h) {
|
|
sycl::local_accessor<float, 1> sv_(sycl::range<1>((size_t)n), h);
|
|
sycl::local_accessor<float, 1> sq(sycl::range<1>((size_t)n), h);
|
|
sycl::local_accessor<float, 1> wl(sycl::range<1>((size_t)n), h);
|
|
h.parallel_for(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 grp = it.get_group();
|
|
/* Every load of the work-item first, then the stores: interleaved, each load would
|
|
* wait for the store before it (the residual may alias x as far as the compiler
|
|
* knows), a memory round trip an element. */
|
|
constexpr int kPer = (int)(kRowMax / kWg);
|
|
float xv[kPer], rv[kPer], wv[kPer];
|
|
/* fully unrolled and predicated (no early exit), so every load is in flight
|
|
* before the first is used */
|
|
#pragma unroll
|
|
for (int j = 0; j < kPer; ++j) {
|
|
const int64_t i = t + (int64_t)j * kWg;
|
|
const bool ok = i < n;
|
|
const int64_t ii = ok ? i : 0;
|
|
xv[j] = rad_load_f32(x.base, x.dtype, r * x.pitch + ii);
|
|
rv[j] = has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + ii) : 0.0f;
|
|
wv[j] = rad_load_f32(gw.base, gw.dtype, ii * gw.stride) + wadd;
|
|
}
|
|
#pragma unroll
|
|
for (int j = 0; j < kPer; ++j) {
|
|
const int64_t i = t + (int64_t)j * kWg;
|
|
if (i >= n) continue;
|
|
float v = xv[j];
|
|
if (has_res) {
|
|
const float s0 = v + rv[j];
|
|
rad_store_f32(res.base, res.dtype, r * res.pitch + i, s0);
|
|
v = round_dt(res.dtype, s0); /* what reading it back gives */
|
|
}
|
|
sv_[i] = v;
|
|
sq[i] = v * v;
|
|
wl[i] = wv[j];
|
|
}
|
|
sycl::group_barrier(grp);
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
float ss = 0.0f;
|
|
if (sg.get_group_linear_id() == 0) {
|
|
const int lane = (int)sg.get_local_linear_id();
|
|
int64_t i = 0;
|
|
for (; i + kSub <= n; i += kSub) {
|
|
const float v = sq[i + lane];
|
|
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
|
|
}
|
|
for (; i < n; ++i) ss += sq[i];
|
|
}
|
|
ss = sycl::group_broadcast(grp, ss, 0);
|
|
const float rs = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
|
auto v = [&](int64_t i) { return rad_bf16_to_f32(rad_f32_to_bf16(sv_[i] * rs * wl[i])); };
|
|
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,
|
|
has_ob ? ob.base : nullptr, ob.dtype, r * ob.pitch);
|
|
if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
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));
|
|
};
|
|
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,
|
|
has_ob ? ob.base : nullptr, ob.dtype, r * ob.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;
|
|
/* The groups are independent, so a row spreads over ceil(ngrp / 16) work-groups, one group a
|
|
* sub-group: at decode (one row of 48 groups) three work-groups share the row, not one. */
|
|
constexpr int kSubs = (int)(kWg / kSub);
|
|
const int64_t per_row = (ngrp + kSubs - 1) / kSubs;
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * per_row) * kWg, kWg},
|
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
|
const int64_t r = (int64_t)it.get_group(0) / per_row;
|
|
const sycl::sub_group sg = it.get_sub_group();
|
|
const int64_t gi = ((int64_t)it.get_group(0) % per_row) * kSubs + (int64_t)sg.get_group_linear_id();
|
|
if (gi >= ngrp) return; /* uniform across the sub-group */
|
|
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);
|
|
/* a float exp, libref's double one only near a bf16 tie: the same bf16 (xe2_common.h) */
|
|
const float sf = sigmoid_fexp(gt);
|
|
float y = (act == 0 ? gt * sf : sf) * up;
|
|
if (near_bf16_tie(y)) {
|
|
const float sgm = sigmoidf(gt);
|
|
y = (act == 0 ? gt * sgm : sgm) * up;
|
|
}
|
|
return rad_bf16_to_f32(rad_f32_to_bf16(y));
|
|
};
|
|
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,
|
|
has_ob ? ob.base : nullptr, ob.dtype, r * ob.pitch);
|
|
if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
|
|
});
|
|
});
|
|
}
|
|
|
|
} // namespace xe2
|