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

+59
View File
@@ -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 };
+1
View File
@@ -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);
+23
View File
@@ -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; "
+12
View File
@@ -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");