ops: attn_dense at head_dim 80 and grid_embed_cpe, for the C-RADIO tower

attn_dense at head_dim 80 without GQA (C-RADIO's ViT heads), and grid_embed_cpe, RADIO's cropped
position embedding: the table resampled half-pixel bilinear onto a square and cropped to the
grid, with the class and register rows ahead of each segment. libref, libavx and libr4d; ref_test,
avx_check_hand and selftest cases; docs/OPS.md.
This commit is contained in:
keptn42 committed 2026-11-04 22:08:53 +01:00
1 parent 2420b2b383
commit b466c91b26
17 files changed
+426 -2

No files matched your search

+6 -1
View File
@@ -2,7 +2,7 @@
// an optional two-sided window, i - j < window behind and j - i < wright ahead (wright is
// `window_right`, or `window` for the symmetric |i - j| < window), or causal with an optional
// window behind (a codec's sliding layers, a text encoder's prompt). bf16 in and out, f16
// operands, f32 accumulate. Head widths 64, 256 and 512 are compiled; each (head_dim, gqa) pair
// operands, f32 accumulate. Head widths 64, 80, 256 and 512 are compiled; each (head_dim, gqa) pair
// is a row.
//
// Core from the vision kernel (r4d_attn_vit_h72_bf16.hip): S^T = K.Q^T, so the wave32
@@ -304,6 +304,7 @@ void r4d_attn_dense_kernel(const uint16_t* __restrict__ gq, const uint16_t* __re
// ---- launch ----------------------------------------------------------------------------------
// One instantiation per (head_dim, gqa), at a workgroup shape picked for it:
// h64 gqa1: 8 query blocks of 16, K/V tiles of 64 keys (the vision towers' heads)
// h80 gqa1: the same shape, five 16-wide k-tiles a head (C-RADIO's heads)
// h256 gqa1: 8 query blocks of 16, tiles of 32 (LDS: 37 KiB)
// h256 gqa2, 4, 8: 8, 4, 2 query blocks x the heads, tiles of 32 (LDS: 37 KiB)
// h512 gqa2, 4, 8: 4, 2, 1 query blocks x the heads x 2 head halves, tiles of 16 (LDS: 57 KiB)
@@ -353,6 +354,10 @@ extern "C" int r4d_attn_dense_bf16(const void* q, const void* k, const void* v,
return r4d_attn_dense_launch<64, 1, 8, 1, 64>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen,
n_head_kv, scale, window, wright, block,
cdiv, causal, q_ld, k_ld, v_ld, o_ld, s);
if (head_dim == 80 && gqa == 1)
return r4d_attn_dense_launch<80, 1, 8, 1, 64>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen,
n_head_kv, scale, window, wright, block,
cdiv, causal, q_ld, k_ld, v_ld, o_ld, s);
#define R4D_DENSE(HD, G, NQB, DS, T) \
return r4d_attn_dense_launch<HD, G, NQB, DS, T>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen, \
n_head_kv, scale, window, wright, block, \
+2
View File
@@ -902,6 +902,7 @@ int r4d_rad_rope_rows (const RadArgs*, RadStream);
int r4d_rad_layernorm (const RadArgs*, RadStream);
int r4d_rad_grid_embed (const RadArgs*, RadStream);
int r4d_rad_grid_embed_aa (const RadArgs*, RadStream);
int r4d_rad_grid_embed_cpe (const RadArgs*, RadStream);
int r4d_rad_gemm_nt_bias (const RadArgs*, RadStream);
/* gemm_nt_bias at f32 (r4d_gemm_f32_nt_bias.hip). */
int r4d_rad_gemm_f32_nt_bias(const RadArgs*, RadStream);
@@ -969,6 +970,7 @@ int r4d_shape_mul_weight (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_scatter_rows_bf16(const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_layernorm (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_grid_embed (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_grid_embed_cpe (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_gemm_nt_bias (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_unary (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_fill (const RadParam*, int, int operand, RadOpdDesc*);
+39
View File
@@ -2203,6 +2203,16 @@ static const RadOperandSpec oGridEmbed[] = {
{ "table", RAD_OPD_WEIGHT, 0 },
{ "coord", RAD_OPD_IN, 0 },
};
static const RadParamSpec pGridEmbedCpe[] = {
P_INT("M"), P_INT("n"), P_INT("side"), P_STR("dtype"),
{ "n_prefix", RAD_P_INT, RAD_OPTIONAL },
};
static const RadOperandSpec oGridEmbedCpe[] = {
{ "x", RAD_OPD_INOUT, 0 },
{ "table", RAD_OPD_WEIGHT, 0 },
{ "coord", RAD_OPD_IN, 0 },
{ "prefix", RAD_OPD_WEIGHT, 1 },
};
static const RadParamSpec pGemmBias[] = {
P_INT("M"), P_INT("N"), P_INT("K"), P_STR("dtype"),
{ "act", RAD_P_STR, RAD_OPTIONAL },
@@ -3291,6 +3301,12 @@ static const RadOpSchema kSchemas[] = {
"position embedding: per axis a triangle filter on half-pixel centres, widened by the "
"scale when shrinking, its in-table taps normalised. Separable, f32, rounded once "
"before the add; coordinates clamp into their grid."),
SCHEMA("grid_embed_cpe", pGridEmbedCpe, oGridEmbedCpe,
"x += the side x side table resampled as torch's F.interpolate(bilinear, "
"align_corners=False) -- no antialias -- onto a square of side max(grid height, grid "
"width), of which the grid keeps the top-left part: RADIO's cropped position embedding. "
"Two taps an axis, the row blended first; f32, rounded once before the add. A row "
"whose row coordinate is negative gets the learned prefix row its column names."),
SCHEMA("gemm_nt_bias", pGemmBias, oGemmBias,
"y = res + act(a @ b^T + bias), rounded to bf16 after the biased product, after the "
"activation and after the residual -- the unfused chain's three tensors."),
@@ -4681,6 +4697,10 @@ static const RadConstraint cCastNarrow[] = {
static const RadConstraint cAttnDenseH64[] = {
C_EQ("head_dim", 64), C_EQ("gqa", 1), C_IN("dtype", "bf16"),
};
/* head_dim 80 without GQA: C-RADIO's ViT heads (Nemotron-3.5-Bolt's vision tower), bidirectional. */
static const RadConstraint cAttnDenseH80[] = {
C_EQ("head_dim", 80), C_EQ("gqa", 1), C_IN("dtype", "bf16"),
};
/* head_dim 256 without GQA, sequences past the small kernel's. */
static const RadConstraint cAttnDenseH256G1[] = {
C_EQ("head_dim", 256), C_EQ("gqa", 1), C_GE("max_seqlen", 33), C_IN("dtype", "bf16"),
@@ -6428,6 +6448,15 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr, r4d_rad_grid_embed_aa, nullptr, nullptr, nullptr,
r4d_shape_grid_embed },
{ "grid_embed_cpe_bf16", "grid_embed_cpe", "elem",
"a learned position grid resampled half-pixel bilinear onto a square and cropped (RADIO's CPE)",
"any n and side; coord [4, M] dense; one workgroup a row",
"bf16 x, bf16 or f32 table, i32 coord, f32 taps",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cGridEmbed), NOTUNE,
nullptr, nullptr, r4d_rad_grid_embed_cpe, nullptr, nullptr, nullptr,
r4d_shape_grid_embed_cpe },
{ "gemm_bf16_nt_bias_tiled", "gemm_nt_bias", "gemm",
"biased GEMM C = res + act(A @ W^T + bias) over 64-deep K tiles, the K tail staged as zeros, "
"the activation and residual in the store: from 32 blocks of 128x128 the weight read straight "
@@ -7978,6 +8007,16 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
ROW(cAttnDenseH64), NOTUNE,
nullptr, nullptr, r4d_rad_attn_dense },
{ "attn_dense_h80_bf16", "attn_dense", "attn",
"dense varlen attention over cu_seqlens: bidirectional with an optional two-sided window or "
"blocks, or causal with an optional window behind",
"head_dim 80, multi-head (no GQA), q/k/v/o at their own token pitches (column slices of a "
"fused projection allowed)",
"bf16 query / key / value / out, f16 operands, fp32 accumulate",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cAttnDenseH80), NOTUNE,
nullptr, nullptr, r4d_rad_attn_dense },
{ "attn_dense_h256_bf16", "attn_dense", "attn",
"dense varlen attention over cu_seqlens: bidirectional with an optional two-sided window or "
"blocks, or causal with an optional window behind",
+35
View File
@@ -10108,6 +10108,40 @@ void attn_dense_gqa_cases(const Plugin& pl) {
}
}
/* attn_dense_h80_bf16: C-RADIO's tower, 16 heads of 80 over a fused [q|k|v] row, two images. */
void attn_dense_h80_cases(const Plugin& pl) {
const RadKernelInfo* r = find(pl, "attn_dense_h80_bf16");
if (!r) { ok("attn_dense_h80_bf16", false, "row absent"); return; }
const int64_t hd = 80, nh = 16, w = nh * hd, total = 333;
const std::vector<int32_t> cu = { 0, 197, 333 };
std::vector<float> qkv = randv((size_t)(total * 3 * w), 1.0f);
for (float& x : qkv) x = bfr(x);
void* d = up_bf16(qkv);
auto col = [&](int c) {
RadTensor t = T((char*)d + c * w * 2, RAD_BF16, { total, w });
t.stride[0] = 3 * w;
return t;
};
void* dout = dalloc((size_t)(total * w) * 2);
const int rc = launch(r, { col(0), col(1), col(2), T(up_i32(cu), RAD_I32, { 3 }),
T(dout, RAD_BF16, { total, w }) },
{ PI("M", total), PI("head_dim", hd), PI("gqa", 1), PI("causal", 0),
PS("dtype", "bf16"), PI("max_seqlen", 197) });
if (rc != RAD_OK) { ok("attn_dense_h80_bf16", false, "rc " + std::to_string(rc)); return; }
std::vector<float> q((size_t)(total * w)), k((size_t)(total * w)), v((size_t)(total * w));
for (int64_t i = 0; i < total; ++i)
for (int64_t t = 0; t < w; ++t) {
q[(size_t)(i * w + t)] = qkv[(size_t)(i * 3 * w + t)];
k[(size_t)(i * w + t)] = qkv[(size_t)(i * 3 * w + w + t)];
v[(size_t)(i * w + t)] = qkv[(size_t)(i * 3 * w + 2 * w + t)];
}
std::vector<float> want((size_t)(total * w));
for (int e = 0; e < 2; ++e)
host_attn(q, k, v, w, w, nh, nh, hd, cu[(size_t)e], cu[(size_t)e + 1], cu[(size_t)e],
cu[(size_t)e + 1], false, &want);
check("attn_dense_h80_bf16 16 heads over two images", down_bf16(dout, want.size()), want, 2e-2);
}
/* One wide head over every pixel, 384 and 512. */
void attn_wide_cases(const Plugin& pl) {
for (const int64_t hd : { 384, 512 }) {
@@ -32981,6 +33015,7 @@ static void device_phase(const Plugin& pl) {
if (sel("attn_cross_h128_bf16")) g3::attn_cross_cases(pl, 128);
if (sel("attn_cross_h64_bf16")) g3::attn_cross_cases(pl, 64);
if (sel("attn_dense_gqa")) g3::attn_dense_gqa_cases(pl);
if (sel("attn_dense_h80_bf16")) g3::attn_dense_h80_cases(pl);
if (sel("attn_dense_h384_bf16") || sel("attn_dense_h512_bf16")) g3::attn_wide_cases(pl);
if (sel("rms_mod_bf16") || sel("rms_mod_quant_fp8_bf16")) g3::rms_mod_forms(pl);
if (sel("timestep_embed_bf16")) g3::timestep_round_cases(pl);
+10
View File
@@ -372,6 +372,16 @@ extern "C" int r4d_shape_grid_embed(const RadParam* p, int n_p, int operand, Rad
R4D_PICK(l);
}
/* grid_embed's operands plus the prefix rows (RADIO's class and register tokens). */
extern "C" int r4d_shape_grid_embed_cpe(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), side = pi(p, n_p, "side");
const int64_t npre = rad_param_geti(p, n_p, "n_prefix", 1);
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, n), opd(D, side * side, n), opd_idx(4, M, side),
opd(D, npre > 0 ? npre : 1, n) };
R4D_PICK(l);
}
extern "C" int r4d_shape_gemm_nt_bias(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M"), N = pi(p, n_p, "N"), K = pi(p, n_p, "K");
const uint32_t D = dt_act(p, n_p, "dtype");
+83
View File
@@ -170,6 +170,54 @@ __global__ __launch_bounds__(kThr) void grid_embed_aa_kernel(
}
}
// ---------------------------------------------------------------------------- grid_embed_cpe
// ref_grid_embed_cpe: RADIO's cropped position embedding, torch's half-pixel bilinear without
// antialias onto a square of the grid's larger side, cropped to the grid. Two taps an axis, the
// row blended first as libref does; rounded once before the add.
__device__ __forceinline__ void cpe_taps(int at, int d, int side, int* t, float* w) {
d = d < 1 ? 1 : d;
at = at < 0 ? 0 : (at >= d ? d - 1 : at);
float src = (float)side / (float)d * ((float)at + 0.5f) - 0.5f;
src = src < 0.0f ? 0.0f : src;
int i0 = (int)src;
i0 = i0 > side - 1 ? side - 1 : i0;
t[0] = i0;
t[1] = i0 < side - 1 ? i0 + 1 : i0;
w[1] = src - (float)i0;
w[0] = 1.0f - w[1];
}
template <bool TF32>
__global__ __launch_bounds__(kThr) void grid_embed_cpe_kernel(
unsigned short* __restrict__ x, const void* __restrict__ tab, const int32_t* __restrict__ crd,
uint32_t n, int side, uint32_t x_ld, uint32_t plane, const unsigned short* __restrict__ pre,
int npre) {
const uint32_t m = blockIdx.x;
const int g[4] = { crd[m], crd[plane + m], crd[2 * plane + m], crd[3 * plane + m] };
unsigned short* __restrict__ xr = x + (size_t)m * x_ld;
if (g[0] < 0) {
// A row ahead of its segment's patches: the learned bf16 row its column names.
if (!pre || npre <= 0) return;
const int k = g[1] < 0 ? 0 : (g[1] >= npre ? npre - 1 : g[1]);
for (uint32_t i = threadIdx.x; i < n; i += kThr)
xr[i] = f32_to_bf16(bf2f(xr[i]) + bf2f(pre[(size_t)k * n + i]));
return;
}
const int d = g[2] > g[3] ? g[2] : g[3];
int th[2], tw[2];
float wh[2], ww[2];
cpe_taps(g[0], d, side, th, wh);
cpe_taps(g[1], d, side, tw, ww);
for (uint32_t i = threadIdx.x; i < n; i += kThr) {
const float r0 = ww[0] * ldw<TF32>(tab, (uint32_t)(th[0] * side + tw[0]) * n + i) +
ww[1] * ldw<TF32>(tab, (uint32_t)(th[0] * side + tw[1]) * n + i);
const float r1 = ww[0] * ldw<TF32>(tab, (uint32_t)(th[1] * side + tw[0]) * n + i) +
ww[1] * ldw<TF32>(tab, (uint32_t)(th[1] * side + tw[1]) * n + i);
const float pe = bf2f(f32_to_bf16(wh[0] * r0 + wh[1] * r1));
xr[i] = f32_to_bf16(bf2f(xr[i]) + pe);
}
}
// ---------------------------------------------------------------------------- gemm_nt_bias
#define VB_BK 64
#define VB_PITCH (VB_BK + 8) // 144-byte LDS rows, as in gemm_bf16_nt_tiled (bank spread)
@@ -621,6 +669,41 @@ extern "C" int r4d_rad_grid_embed_aa(const RadArgs* a, RadStream stream) {
(uint32_t)n, (int)side, (uint32_t)x_ld, plane);
}
/* grid_embed_cpe: grid_embed's operands and checks, RADIO's cropped resample. */
extern "C" int r4d_rad_grid_embed_cpe(const RadArgs* a, RadStream stream) {
const RadTensor* x = r4d_opt(a, GE_X);
const RadTensor* tab = r4d_opt(a, GE_TAB);
const RadTensor* crd = r4d_opt(a, GE_CRD);
if (!x || !tab || !crd) return R4D_NO(RAD_E_INVAL);
if (x->dtype != RAD_BF16 || crd->dtype != RAD_I32) return R4D_NO(RAD_E_DTYPE);
if (tab->dtype != RAD_BF16 && tab->dtype != RAD_F32) return R4D_NO(RAD_E_DTYPE);
if (!r4d_contig(tab)) return R4D_NO(RAD_E_STRIDE);
const long long n = r4d_geti_or(a, "n", r4d_dim(tab, (int)tab->rank - 1));
const long long side = r4d_geti_or(a, "side", 0);
if (n <= 0 || side <= 0 || r4d_numel(tab) < side * side * n) return R4D_NO(RAD_E_SHAPE);
long long M = 0;
const long long x_ld = r4d_rows_pitch(x, n, &M);
if (x_ld < 0) return R4D_NO(RAD_E_STRIDE);
if (crd->rank != 2 || crd->shape[0] < 4 || crd->shape[1] < M || !r4d_contig(crd))
return R4D_NO(RAD_E_SHAPE);
if (M <= 0) return R4D_NO(RAD_E_SHAPE);
const uint32_t plane = (uint32_t)crd->shape[1];
/* The prefix rows: bf16, contiguous, n_prefix rows of n. */
const long long npre = r4d_geti_or(a, "n_prefix", 0);
const RadTensor* pre = npre > 0 ? r4d_opt(a, 3) : nullptr;
if (npre > 0 && (!pre || pre->dtype != RAD_BF16 || !r4d_contig(pre) ||
r4d_numel(pre) < npre * n))
return R4D_NO(pre && pre->dtype != RAD_BF16 ? RAD_E_DTYPE : RAD_E_SHAPE);
const unsigned short* pp = pre ? (const unsigned short*)r4d_p(pre) : nullptr;
if (tab->dtype == RAD_F32)
return R4D_EMIT(stream, grid_embed_cpe_kernel<true>, dim3((uint32_t)M), dim3(kThr), 0,
(unsigned short*)r4d_wp(x), r4d_p(tab), (const int32_t*)r4d_p(crd),
(uint32_t)n, (int)side, (uint32_t)x_ld, plane, pp, (int)npre);
return R4D_EMIT(stream, grid_embed_cpe_kernel<false>, dim3((uint32_t)M), dim3(kThr), 0,
(unsigned short*)r4d_wp(x), r4d_p(tab), (const int32_t*)r4d_p(crd),
(uint32_t)n, (int)side, (uint32_t)x_ld, plane, pp, (int)npre);
}
/* ------------------------------------------------------------------ gemm_nt_bias */
enum { GB_A = 0, GB_W, GB_BIAS, GB_RES, GB_Y };