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
@@ -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 "
|
||||
|
||||
Reference in new issue
Block a user