ops: seg_softmax_pool, each segment's rows summed under the softmax of their scores

This commit is contained in:
Brandon Bertelsen committed 2026-10-28 00:33:55 -04:00
1 parent 5a2732f063
commit 555fbf62f5
14 files changed
+224

No files matched your search

+33
View File
@@ -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
+2
View File
@@ -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);
+9
View File
@@ -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 "
+13
View File
@@ -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);