Archived
ops: seg_softmax_pool, each segment's rows summed under the softmax of their scores
This commit is contained in:
1 parent
5a2732f063
commit
555fbf62f5
14 files changed
+224
No files matched your search
@@ -211,6 +211,7 @@ at once. Run it before adding a kernel plugin; the loader names only the first c
|
||||
| `mul_weight` | `M`(range) `n` `dtype` `scalar`/opt | `x`, `w`w → `y` (w one element or one row) |
|
||||
| `act_mul` | `M`(range) `n` `act` `dtype` | `a`, `b` → `y` (act(a)·b, two operands) |
|
||||
| `seg_mean` | `M`(range) `n` `dtype` | `x`, `cu_seqlens` → `y` (one mean row a segment) |
|
||||
| `seg_softmax_pool` | `M`(range) `n` `dtype` | `score`, `v`, `cu_seqlens` → `y` (one softmax-weighted row a segment) |
|
||||
| `pool_rows` | `M`(range) `n` `k` `mult`:f64/opt `dtype` | `x` → `y` (one mean row a window of k rows) |
|
||||
| `pool_cols` | `M`(range) `n` `k` `mult`:f64/opt `dtype` | `x` → `y` (one mean column a window of k columns) |
|
||||
| `relu` | `M`(range) `n` `dtype` | `x` → `y` |
|
||||
@@ -289,6 +290,13 @@ rounded to `y`'s dtype before the product, as the unfused pair stores it.
|
||||
`[cu[s], cu[s+1])`, summed in double and rounded once. `M` is the step's token count and bands the
|
||||
op; the segment count is `cu_seqlens`' length less one. An empty segment is a zero row.
|
||||
|
||||
**`seg_softmax_pool`** is `seg_mean` with learned weights: row `s` of `y` is `v`'s rows
|
||||
`[cu[s], cu[s+1])` summed with the softmax of their `score`s (one a row of `v`, f32, the maximum
|
||||
subtracted) as the weights, the sum in f32 and rounded once. It is a query reading its few
|
||||
candidates: clef's joint schema head routes each question over its options this way, the scores
|
||||
the options' dot products with the question. `M` bands the op as `seg_mean`'s does, and an empty
|
||||
segment is a zero row.
|
||||
|
||||
**`pool_rows`** is a vision pooler over windows laid out consecutively: row `r` of `y` is the mean
|
||||
of `x`'s rows `[r*k, r*k + k)`, summed in f32 and rounded to `y`'s dtype, then times `mult` and
|
||||
rounded again (the unfused pair stores the mean). `M` is the input row count, a multiple of `k`.
|
||||
|
||||
@@ -65,6 +65,7 @@ std::vector<Case> build_cases() {
|
||||
add("mul_weight", { HI("M", 9), HI("n", 645), HS("dtype", dt), HI("scalar", 1) },
|
||||
EB(9, 645, w, 2));
|
||||
add("seg_mean", { HI("M", 23), HI("n", 645), HS("dtype", dt) }, EB(23, 645, w, 1));
|
||||
add("seg_softmax_pool", { HI("M", 23), HI("n", 645), HS("dtype", dt) }, EB(23, 645, w, 2));
|
||||
add("relu", { HI("M", 7), HI("n", 645), HS("dtype", dt) }, EB(7, 645, w, 2));
|
||||
add("clamp", { HI("M", 7), HI("n", 645), HS("dtype", dt) }, EB(7, 645, w, 2));
|
||||
add("rmsnorm", { HI("M", 5), HI("n", 645), HF("eps", 1e-6), HS("dtype", dt),
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
#include "avx_vec.h"
|
||||
#include "avx_common.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
using namespace avx;
|
||||
|
||||
/* Round a row through an operand's storage dtype and back, as an unfused store-and-reload would.
|
||||
@@ -534,6 +537,40 @@ AVX_KERNEL(seg_mean) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ seg_softmax_pool */
|
||||
/* One output row per cu_seqlens segment: its rows summed with the softmax of their scores as
|
||||
* weights, the softmax in f32 with the maximum subtracted, the sum in f32, rounded once, as in
|
||||
* libref. A segment is a question's few options, so the scores are read plainly. */
|
||||
AVX_KERNEL(seg_softmax_pool) {
|
||||
if (!rad_args_have(a, 4)) return RAD_E_INVAL;
|
||||
const RadTensor *Sc = &a->t[0], *V = &a->t[1], *cu = &a->t[2], *Y = &a->t[3];
|
||||
const int64_t n = p_int(a, "n", 0);
|
||||
const int64_t S = numel(cu) - 1;
|
||||
if (n <= 0 || S <= 0 || numel(Y) < S * n) return RAD_E_SHAPE;
|
||||
const int64_t total = ldi(cu, offlin(cu, S));
|
||||
if (total <= 0 || numel(V) < total * n || numel(Sc) < total) return RAD_E_SHAPE;
|
||||
|
||||
AVX_PARALLEL_FOR_IF(S >= 2)
|
||||
for (int64_t s = 0; s < S; ++s) {
|
||||
const int64_t b0 = ldi(cu, offlin(cu, s)), b1 = ldi(cu, offlin(cu, s + 1));
|
||||
float* acc = scratch_f32_b((size_t)n);
|
||||
float* sy = scratch_f32_c((size_t)n);
|
||||
float one = 0.0f;
|
||||
float m = -INFINITY, z = 0.0f;
|
||||
for (int64_t r = b0; r < b1; ++r) m = std::max(m, in_row(Sc, rowoff(Sc, r, 1), 1, &one)[0]);
|
||||
for (int64_t r = b0; r < b1; ++r) z += std::exp(in_row(Sc, rowoff(Sc, r, 1), 1, &one)[0] - m);
|
||||
for (int64_t i = 0; i < n; ++i) acc[i] = 0.0f;
|
||||
for (int64_t r = b0; r < b1; ++r) {
|
||||
const float w = std::exp(in_row(Sc, rowoff(Sc, r, 1), 1, &one)[0] - m) / z;
|
||||
const float* x = in_row(V, rowoff(V, r, n), n, sy);
|
||||
for (int64_t i = 0; i < n; ++i) acc[i] += w * x[i];
|
||||
}
|
||||
for (int64_t i = 0; i < n; ++i) sy[i] = b1 > b0 ? acc[i] : 0.0f;
|
||||
out_row(Y, rowoff(Y, s, n), n, sy);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ pool_rows */
|
||||
/* As libref: the mean of each k consecutive rows, summed in f32 and rounded to y's dtype, then the
|
||||
* multiplier and a second rounding (the unfused pair). Parallel over output rows. */
|
||||
|
||||
@@ -88,6 +88,7 @@ int avx_par_force(int threads);
|
||||
X(ple_gate) X(ple_conv) X(cast) X(softmax) \
|
||||
X(scale_rows) X(scale) X(fill) X(act_mul) \
|
||||
X(seg_mean) X(mul_weight) X(pool_rows) X(pool_cols) \
|
||||
X(seg_softmax_pool) \
|
||||
X(relu) X(clamp) X(conv2d) X(conv1d_dw) \
|
||||
X(rel_sinusoid) X(seg_sinusoid) X(conv2d_unfold) \
|
||||
/* ---- quantisation and rotation */ \
|
||||
|
||||
@@ -161,6 +161,8 @@ static const RadOperandSpec oMulWeight[] = { OPD("x"), WGT("w"), OUT("y") };
|
||||
static const RadParamSpec pActMul[] = { P_INT("M"), P_INT("n"), P_STR("act"), P_STR("dtype") };
|
||||
static const RadParamSpec pSegMean[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
|
||||
static const RadOperandSpec oSegMean[] = { OPD("x"), OPD("cu_seqlens"), OUT("y") };
|
||||
static const RadParamSpec pSegPool[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
|
||||
static const RadOperandSpec oSegPool[] = { OPD("score"), OPD("v"), OPD("cu_seqlens"), OUT("y") };
|
||||
static const RadParamSpec pPoolRows[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_F64O("mult"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oGateUp[] = { OPD("gate_up"), OUT("y") };
|
||||
@@ -934,6 +936,9 @@ static const RadOpSchema kSchemas[] = {
|
||||
"y = act(a) * b, act silu / gelu / gelu_tanh, the activation rounded to y's dtype first." },
|
||||
{ "seg_mean", ARR(pSegMean), ARR(oSegMean),
|
||||
"y[s] = the mean of x's rows in cu_seqlens segment s, summed in double, rounded once." },
|
||||
{ "seg_softmax_pool", ARR(pSegPool), ARR(oSegPool),
|
||||
"y[s] = v's rows in cu_seqlens segment s summed with the softmax of their scores as weights, "
|
||||
"in f32, rounded once." },
|
||||
{ "relu", ARR(pMnD), ARR(oUnary), "y = max(x, 0)." },
|
||||
{ "clamp", ARR(pMnD), ARR(oClamp), "y = min(max(x, lo), hi), lo and hi one-element weights." },
|
||||
{ "conv2d", ARR(pConv2d), ARR(oConv2d),
|
||||
@@ -1533,6 +1538,9 @@ SIMPLE("avx_mul_weight", "mul_weight", "elem", "x * a scalar or one-row weight",
|
||||
L(mul_weight)),
|
||||
SIMPLE("avx_seg_mean", "seg_mean", "elem", "mean of each cu_seqlens segment's rows", DT_FLOAT,
|
||||
L(seg_mean)),
|
||||
SIMPLE("avx_seg_softmax_pool", "seg_softmax_pool", "elem",
|
||||
"each cu_seqlens segment's rows summed with the softmax of their scores", DT_FLOAT,
|
||||
L(seg_softmax_pool)),
|
||||
SIMPLE("avx_pool_rows", "pool_rows", "elem", "mean of every k consecutive rows, then a multiplier",
|
||||
DT_FLOAT, L(pool_rows)),
|
||||
SIMPLE("avx_pool_cols", "pool_cols", "elem", "mean of every k consecutive columns, then a "
|
||||
|
||||
@@ -786,6 +786,41 @@ extern "C" int r4d_seg_mean_bf16(long x, long cu, long y, int S, int n, int x_ld
|
||||
return hipGetLastError() == hipSuccess ? 0 : -4;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- seg_softmax_pool
|
||||
// ref_seg_softmax_pool: row s of y is v's rows [cu[s], cu[s+1]) summed with the softmax of their
|
||||
// scores as weights, the softmax in f32 with the maximum subtracted, the sum in f32, one rounding.
|
||||
// A workgroup is 256 columns of one segment, and every thread forms the segment's softmax itself:
|
||||
// a segment is one question's few options (clef's routing), so the repeat is cheaper than a
|
||||
// reduction. An empty segment writes zeros.
|
||||
__global__ __launch_bounds__(kThreads) void seg_softmax_pool_kernel(
|
||||
const uint16_t* __restrict__ sc, const uint16_t* __restrict__ v, const int* __restrict__ cu,
|
||||
uint16_t* __restrict__ y, uint32_t n, uint32_t v_ld, uint32_t y_ld) {
|
||||
const uint32_t s = blockIdx.y;
|
||||
const uint32_t i = blockIdx.x * kThreads + threadIdx.x;
|
||||
if (i >= n) return;
|
||||
const int b0 = cu[s], b1 = cu[s + 1];
|
||||
float m = -INFINITY;
|
||||
for (int r = b0; r < b1; ++r) m = fmaxf(m, bf2f(sc[r]));
|
||||
float z = 0.0f;
|
||||
for (int r = b0; r < b1; ++r) z += expf(bf2f(sc[r]) - m);
|
||||
float acc = 0.0f;
|
||||
for (int r = b0; r < b1; ++r)
|
||||
acc += expf(bf2f(sc[r]) - m) / z * bf2f(v[(size_t)r * v_ld + i]);
|
||||
y[(size_t)s * y_ld + i] = f32_to_bf16(b1 > b0 ? acc : 0.0f);
|
||||
}
|
||||
|
||||
extern "C" int r4d_seg_softmax_pool_bf16(long sc, long v, long cu, long y, int S, int n, int v_ld,
|
||||
int y_ld, long stream) {
|
||||
if (!sc || !v || !cu || !y) return -1;
|
||||
if (S <= 0 || n <= 0) return -2;
|
||||
hipLaunchKernelGGL(seg_softmax_pool_kernel,
|
||||
dim3((uint32_t)((n + kThreads - 1) / kThreads), (uint32_t)S),
|
||||
dim3(kThreads), 0, (hipStream_t)stream, (const uint16_t*)sc,
|
||||
(const uint16_t*)v, (const int*)cu, (uint16_t*)y, (uint32_t)n,
|
||||
(uint32_t)v_ld, (uint32_t)y_ld);
|
||||
return hipGetLastError() == hipSuccess ? 0 : -4;
|
||||
}
|
||||
|
||||
// mode: 0 neox, 1 gptj, 2 blocks, 3 gptj_blocks, 4 imrope, 5 axial (`blk` and the position plane
|
||||
// stride `pplane` used only by the last four). A null gain is a unit gain; `rot` 0 is the norm
|
||||
// alone, and then `pos` may be null. `pos_f32` reads f32 positions as the values they hold.
|
||||
@@ -1099,6 +1134,30 @@ extern "C" int r4d_rad_seg_mean(const RadArgs* a, RadStream s) {
|
||||
(int)x_ld, (int)y_ld, (long)(uintptr_t)s));
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_seg_softmax_pool(const RadArgs* a, RadStream s) {
|
||||
const RadTensor* Sc = r4d_opt(a, 0);
|
||||
const RadTensor* V = r4d_opt(a, 1);
|
||||
const RadTensor* cu = r4d_opt(a, 2);
|
||||
const RadTensor* Y = r4d_opt(a, 3);
|
||||
if (!Sc || !V || !cu || !Y) return R4D_NO(RAD_E_INVAL);
|
||||
R4D_NEED(dt_bf16(Sc));
|
||||
R4D_NEED(dt_bf16(V));
|
||||
R4D_NEED(dt_bf16(Y));
|
||||
if (!r4d_contig(Sc)) return R4D_NO(RAD_E_STRIDE);
|
||||
if (cu->dtype != RAD_I32 || !r4d_contig(cu) || r4d_numel(cu) < 2) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long n = r4d_geti_or(a, "n", r4d_dim(Y, (int)Y->rank - 1));
|
||||
if (n <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long S = r4d_numel(cu) - 1;
|
||||
long long Mv = 0, My = 0;
|
||||
const long long v_ld = r4d_rows_pitch(V, n, &Mv);
|
||||
const long long y_ld = r4d_rows_pitch(Y, n, &My);
|
||||
if (v_ld < 0 || y_ld < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (My < S || r4d_numel(Sc) < Mv) return R4D_NO(RAD_E_SHAPE);
|
||||
return enc_status(r4d_seg_softmax_pool_bf16(r4d_addr(Sc), r4d_addr(V), r4d_addr(cu),
|
||||
r4d_addr(Y), (int)S, (int)n, (int)v_ld,
|
||||
(int)y_ld, (long)(uintptr_t)s));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ qk_norm_rope */
|
||||
enum { QN_QKV = 0, QN_POS, QN_QW, QN_KW };
|
||||
|
||||
|
||||
@@ -734,6 +734,7 @@ int r4d_rad_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_mul_weight (const RadArgs*, RadStream);
|
||||
int r4d_rad_act_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_seg_mean (const RadArgs*, RadStream);
|
||||
int r4d_rad_seg_softmax_pool(const RadArgs*, RadStream);
|
||||
int r4d_rad_qk_norm_rope (const RadArgs*, RadStream);
|
||||
int r4d_rad_cast_widen (const RadArgs*, RadStream);
|
||||
int r4d_rad_cast_f16 (const RadArgs*, RadStream);
|
||||
|
||||
@@ -1886,6 +1886,15 @@ static const RadOperandSpec oSegMean[] = {
|
||||
{ "cu_seqlens", RAD_OPD_IN, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pSegPool[] = {
|
||||
P_INT("M"), P_INT("n"), P_STR("dtype"),
|
||||
};
|
||||
static const RadOperandSpec oSegPool[] = {
|
||||
{ "score", RAD_OPD_IN, 0 },
|
||||
{ "v", RAD_OPD_IN, 0 },
|
||||
{ "cu_seqlens", RAD_OPD_IN, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* docs/OPS.md's `qk_norm_rope`: a fused [q | k | v] row in place, theta and not a table. */
|
||||
static const RadParamSpec pQkNormRope[] = {
|
||||
P_INT("M"), P_INT("head_dim"), P_INT("n_head"), P_INT("n_head_kv"), P_F64("theta"),
|
||||
@@ -2930,6 +2939,10 @@ static const RadOpSchema kSchemas[] = {
|
||||
SCHEMA("seg_mean", pSegMean, oSegMean,
|
||||
"Row s of y is the mean of x's rows [cu_seqlens[s], cu_seqlens[s+1]), summed in f64 "
|
||||
"and rounded once; an empty segment is zeros."),
|
||||
SCHEMA("seg_softmax_pool", pSegPool, oSegPool,
|
||||
"Row s of y is v's rows [cu_seqlens[s], cu_seqlens[s+1]) summed with the softmax of "
|
||||
"their scores (one a row, f32, the maximum subtracted) as weights, in f32 and rounded "
|
||||
"once; an empty segment is zeros."),
|
||||
SCHEMA("qk_norm_rope", pQkNormRope, oQkNormRope,
|
||||
"In place over a fused [q | k | v] row: each q and k head RMS-normed against its gain "
|
||||
"(plus wadd) and rounded, then rotated (neox or gptj, rotary_dim of head_dim); with "
|
||||
@@ -5648,6 +5661,16 @@ static const RadKernelInfo kKernels[] = {
|
||||
ROW(cBf16Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_seg_mean },
|
||||
|
||||
{ "seg_softmax_pool_bf16", "seg_softmax_pool", "elem",
|
||||
"a segment's rows summed with the softmax of their scores as weights: one row a cu_seqlens "
|
||||
"segment",
|
||||
"any n; a thread an output column, one workgroup row a segment, the softmax formed in f32 by "
|
||||
"every thread",
|
||||
"bf16 scores, rows and out, f32 weights and sum, one rounding",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cBf16Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_seg_softmax_pool },
|
||||
|
||||
{ "qk_norm_rope_bf16", "qk_norm_rope", "attn",
|
||||
"per-head RMS norm of q and k (and optionally v, unit gain) with rotary on q and k, in place",
|
||||
"head_dim up to 1024, a multiple of 32 for neox (rotary_dim / 2 a multiple of 32) and gptj; "
|
||||
|
||||
@@ -28531,6 +28531,18 @@ static void device_phase(const Plugin& pl) {
|
||||
run_case(r, ts, ps);
|
||||
}
|
||||
}
|
||||
if (sel("seg_softmax_pool_bf16")) {
|
||||
const RadKernelInfo* r = find(pl, "seg_softmax_pool_bf16");
|
||||
if (!r) ok("seg_softmax_pool_bf16", false, "row absent");
|
||||
else {
|
||||
std::vector<RadTensor> ts = { T(nullptr, RAD_BF16, { 23 }),
|
||||
T(nullptr, RAD_BF16, { 23, 645 }),
|
||||
T(nullptr, RAD_I32, { 4 }),
|
||||
T(nullptr, RAD_BF16, { 3, 645 }) };
|
||||
std::vector<RadParam> ps = { PI("M", 23), PI("n", 645), PS("dtype", "bf16") };
|
||||
run_case(r, ts, ps);
|
||||
}
|
||||
}
|
||||
if (sel("qk_norm_rope_bf16")) {
|
||||
const RadKernelInfo* r = find(pl, "qk_norm_rope_bf16");
|
||||
if (!r) ok("qk_norm_rope_bf16", false, "row absent");
|
||||
|
||||
@@ -436,6 +436,39 @@ int ref_seg_mean(const RadArgs* a, RadStream) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ seg_softmax_pool */
|
||||
/* y[s, :] = sum over the segment's rows r of softmax(score)[r] * v[r, :]: one row a cu_seqlens
|
||||
* segment, the weights a softmax over the segment's own scores (f32, the maximum subtracted), the
|
||||
* sum in f32 and rounded once. A query reading its few candidates with learned scores (clef's
|
||||
* routing over a question's options). An empty segment writes zeros. */
|
||||
int ref_seg_softmax_pool(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 4)) return RAD_E_INVAL;
|
||||
const RadTensor *Sc = &a->t[0], *V = &a->t[1], *cu = &a->t[2], *Y = &a->t[3];
|
||||
const int64_t n = p_int(a, "n", 0);
|
||||
const int64_t S = numel(cu) - 1;
|
||||
if (n <= 0 || S <= 0 || numel(Y) < S * n) return RAD_E_SHAPE;
|
||||
const int64_t total = cu_at(cu, S);
|
||||
if (total <= 0 || numel(V) < total * n || numel(Sc) < total) return RAD_E_SHAPE;
|
||||
|
||||
REF_PAR_FOR((total + S) * n)
|
||||
for (int64_t s = 0; s < S; ++s) {
|
||||
const int64_t b0 = cu_at(cu, s);
|
||||
const int64_t b1 = cu_at(cu, s + 1);
|
||||
const int64_t yb = rowoff(Y, s, n), ys = laststride(Y), vs = laststride(V);
|
||||
float m = -INFINITY, z = 0.0f;
|
||||
for (int64_t r = b0; r < b1; ++r) m = std::max(m, ldt(Sc, rowoff(Sc, r, 1)));
|
||||
for (int64_t r = b0; r < b1; ++r) z += std::exp(ldt(Sc, rowoff(Sc, r, 1)) - m);
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
float acc = 0.0f;
|
||||
for (int64_t r = b0; r < b1; ++r)
|
||||
acc += std::exp(ldt(Sc, rowoff(Sc, r, 1)) - m) / z *
|
||||
ldt(V, rowoff(V, r, n) + i * vs);
|
||||
stt(Y, yb + i * ys, b1 > b0 ? acc : 0.0f);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ pool_rows */
|
||||
/* Row r of y is the mean of x's rows [r*k, r*k + k), summed in f32 and rounded to y's dtype, then
|
||||
* multiplied by `mult` and rounded again, as the unfused pair stores the mean. For vision poolers
|
||||
|
||||
@@ -87,6 +87,7 @@ int ref_gelu_mul (const RadArgs*, RadStream);
|
||||
int ref_act_mul (const RadArgs*, RadStream);
|
||||
int ref_mul_weight (const RadArgs*, RadStream);
|
||||
int ref_seg_mean (const RadArgs*, RadStream);
|
||||
int ref_seg_softmax_pool (const RadArgs*, RadStream);
|
||||
int ref_pool_rows (const RadArgs*, RadStream);
|
||||
int ref_pool_cols (const RadArgs*, RadStream);
|
||||
int ref_relu (const RadArgs*, RadStream);
|
||||
@@ -355,6 +356,7 @@ REF_SHAPE_FN(ref_gelu_mul);
|
||||
REF_SHAPE_FN(ref_act_mul);
|
||||
REF_SHAPE_FN(ref_mul_weight);
|
||||
REF_SHAPE_FN(ref_seg_mean);
|
||||
REF_SHAPE_FN(ref_seg_softmax_pool);
|
||||
REF_SHAPE_FN(ref_pool_rows);
|
||||
REF_SHAPE_FN(ref_pool_cols);
|
||||
REF_SHAPE_FN(ref_relu);
|
||||
|
||||
@@ -194,6 +194,8 @@ static const RadParamSpec pActMul[] = { P_INT("M"), P_INT("n"), P_STR("act"), P_
|
||||
/* `M` is the step's token count and bands the op; the segment count is cu_seqlens' length. */
|
||||
static const RadParamSpec pSegMean[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
|
||||
static const RadOperandSpec oSegMean[] = { OPD("x"), OPD("cu_seqlens"), OUT("y") };
|
||||
static const RadParamSpec pSegPool[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
|
||||
static const RadOperandSpec oSegPool[] = { OPD("score"), OPD("v"), OPD("cu_seqlens"), OUT("y") };
|
||||
/* `M` is the input row count, a multiple of `k`; y has M / k rows. */
|
||||
static const RadParamSpec pPoolRows[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_F64O("mult"),
|
||||
P_STR("dtype") };
|
||||
@@ -1153,6 +1155,10 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "seg_mean", ARR(pSegMean), ARR(oSegMean),
|
||||
"y[s] = mean of x's rows [cu[s], cu[s+1]): one row a segment, summed in double and rounded "
|
||||
"once to y's dtype. An empty segment is a zero row." },
|
||||
{ "seg_softmax_pool", ARR(pSegPool), ARR(oSegPool),
|
||||
"y[s] = v's rows [cu[s], cu[s+1]) summed with the softmax of their scores (one a row, f32, the "
|
||||
"maximum subtracted) as weights, in f32 and rounded once to y's dtype. An empty segment is a "
|
||||
"zero row." },
|
||||
{ "pool_rows", ARR(pPoolRows), ARR(oUnary),
|
||||
"Row r of y is the mean of x's rows [r*k, r*k + k), summed in f32 and rounded to y's dtype, then "
|
||||
"times `mult` (default 1) and rounded again. M, the input rows, is a multiple of k." },
|
||||
@@ -2207,6 +2213,9 @@ SIMPLE("ref_chan_stats", "chan_stats", "elem", "per-channel statistics held as w
|
||||
DT_FLOAT, ref_chan_stats),
|
||||
SIMPLE("ref_seg_mean", "seg_mean", "elem", "mean of each cu_seqlens segment's rows", DT_FLOAT,
|
||||
ref_seg_mean),
|
||||
SIMPLE("ref_seg_softmax_pool", "seg_softmax_pool", "elem",
|
||||
"each cu_seqlens segment's rows summed with the softmax of their scores", DT_FLOAT,
|
||||
ref_seg_softmax_pool),
|
||||
SIMPLE("ref_pool_rows", "pool_rows", "elem", "mean of every k consecutive rows, then a multiplier",
|
||||
DT_FLOAT, ref_pool_rows),
|
||||
SIMPLE("ref_pool_cols", "pool_cols", "elem", "mean of every k consecutive columns, then a "
|
||||
|
||||
@@ -546,6 +546,19 @@ int ref_seg_mean_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
return o.finish("seg_mean", operand, out);
|
||||
}
|
||||
|
||||
int ref_seg_softmax_pool_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
||||
const int64_t S = n_seq_of(p, n_p);
|
||||
Opd o;
|
||||
o.t(D, { M }); /* score, one a row of v */
|
||||
o.t(D, { M, n }); /* v */
|
||||
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
||||
o.t(D, { S, n }); /* y */
|
||||
return o.finish("seg_softmax_pool", operand, out);
|
||||
}
|
||||
|
||||
int ref_pool_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t n = gi(p, n_p, "n", 0), k = gi(p, n_p, "k", 0);
|
||||
|
||||
@@ -968,6 +968,7 @@ TEST(op_vocabulary) {
|
||||
"mhc_pre", "mhc_pre_partial", "mhc_pre_finish", "mhc_post", "mhc_contract", "swiglu_clamped", "router_sigmoid_topk", "dsa_pool", "dsa_query", "dsa_score", "mla_attn", "mla_project", "gemm_nt_f32",
|
||||
"kda_update",
|
||||
"layernorm", "add", "mul", "mul_weight", "silu_mul", "gelu_mul", "act_mul", "seg_mean",
|
||||
"seg_softmax_pool",
|
||||
"pool_rows", "pool_cols", "relu", "clamp", "conv2d", "conv2d_unfold", "conv1d_dw",
|
||||
"rel_sinusoid",
|
||||
"seg_sinusoid", "attn_local_rel", "gelu", "silu", "sigmoid", "logit_softcap", "scale",
|
||||
@@ -3260,6 +3261,21 @@ TEST(op_seg_mean) {
|
||||
CHECK_NEAR(y.at(4), 7.0f, 1e-6); CHECK_NEAR(y.at(5), 9.0f, 1e-6);
|
||||
}
|
||||
|
||||
/* seg_softmax_pool: a segment's rows summed with the softmax of their scores as weights. Scores 0
|
||||
* and ln 3 weigh two rows 1/4 and 3/4; an empty segment is a zero row. */
|
||||
TEST(op_seg_softmax_pool) {
|
||||
T sc(RAD_F32, {4}); sc.f({ 0.0f, 1.0986123f, 5.0f, 2.0f });
|
||||
T v(RAD_F32, {4, 2}); v.f({ 1, 2, 5, 6, 7, 9, 3, 3 });
|
||||
T cu(RAD_I32, {4}); cu.n({0, 2, 2, 4});
|
||||
T y(RAD_F32, {3, 2});
|
||||
A a; a.t(sc).t(v).t(cu).t(y).i("M", 4).i("n", 2).s("dtype", "f32");
|
||||
CHECK_OK(run("seg_softmax_pool", a));
|
||||
CHECK_NEAR(y.at(0), 4.0f, 1e-5); CHECK_NEAR(y.at(1), 5.0f, 1e-5);
|
||||
CHECK_EQ(y.at(2), 0.0f); CHECK_EQ(y.at(3), 0.0f);
|
||||
/* Scores 5 and 2: weights 1 / (1 + e^-3) and the rest. */
|
||||
CHECK_NEAR(y.at(4), 6.8103f, 1e-3); CHECK_NEAR(y.at(5), 8.7155f, 1e-3);
|
||||
}
|
||||
|
||||
/* relu and clamp: the plain element rules. */
|
||||
TEST(op_relu_clamp) {
|
||||
T x(RAD_F32, {1, 3}); x.f({ -3.0f, 0.5f, 4.0f });
|
||||
@@ -9617,6 +9633,7 @@ std::vector<Geo> geometries() {
|
||||
G("mul_weight", "M", 3, a.i("M", R).i("n", 8).s("dtype", "f32")),
|
||||
G("act_mul", "M", 3, a.i("M", R).i("n", 8).s("act", "silu").s("dtype", "f32")),
|
||||
G("seg_mean", "M", 6, a.i("M", R).i("n", 8).s("dtype", "f32").i("n_seq", 2)),
|
||||
G("seg_softmax_pool", "M", 6, a.i("M", R).i("n", 8).s("dtype", "f32").i("n_seq", 2)),
|
||||
G("pool_rows", "M", 6, a.i("M", R).i("n", 8).i("k", 2).s("dtype", "f32")),
|
||||
G("pool_cols", "M", 6, a.i("M", R).i("n", 8).i("k", 2).s("dtype", "f32")),
|
||||
G("relu", "M", 3, a.i("M", R).i("n", 8).s("dtype", "f32")),
|
||||
|
||||
Reference in new issue
Block a user