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

+4
View File
@@ -674,6 +674,10 @@ std::vector<Case> build_cases() {
* avx_check's hand-built case. */
add("grid_embed_aa", { HI("M", 20), HI("n", 72), HI("side", 16), HS("dtype", dt) });
add("grid_embed_aa", { HI("M", 40), HI("n", 72), HI("side", 4), HS("dtype", dt) });
add("grid_embed_cpe", { HI("M", 20), HI("n", 72), HI("side", 16), HS("dtype", dt) });
add("grid_embed_cpe", { HI("M", 40), HI("n", 72), HI("side", 4), HS("dtype", dt) });
add("grid_embed_cpe", { HI("M", 24), HI("n", 72), HI("side", 8), HI("n_prefix", 3),
HS("dtype", dt) });
}
/* LiLiCorr's lattice ops, at its geometry (7 positions of 8 candidates, 8 heads) narrowed in
+12
View File
@@ -544,6 +544,18 @@ void shape_cases(std::vector<Hand>& hs) {
.pi("M", 6).pi("n", N).pi("side", 4).ps("dtype", "f32");
hs.push_back(std::move(ga));
/* RADIO's cropped resample: two prefix rows (row -1, the column naming which), three of a
* 3 x 5 grid upsampling the 4 x 4 table and one of a 2 x 2 grid shrinking it. */
static const int32_t kCpeCrd[4][6] = { { -1, -1, 0, 2, 1, 0 }, { 0, 2, 0, 4, 3, 1 },
{ 3, 3, 3, 3, 3, 2 }, { 5, 5, 5, 5, 5, 2 } };
Hand gc("prefix rows and a 4 x 4 table onto 3 x 5 and 2 x 2 grids", "grid_embed_cpe", 1e-6);
gc.dense(RAD_F32, { 6, N }, hfill(61), true)
.dense(RAD_F32, { 16, N }, hfill(62))
.dense(RAD_I32, { 4, 6 }, [](int64_t i) { return (float)kCpeCrd[i / 6][i % 6]; })
.dense(RAD_F32, { 3, N }, hfill(63))
.pi("M", 6).pi("n", N).pi("side", 4).pi("n_prefix", 3).ps("dtype", "f32");
hs.push_back(std::move(gc));
/* LiLiCorr's candidates over two ranks' sample_logprobs rows: a merge that interleaves the
* ranks, an equal value on both (the lower id first), a rank with an unfilled slot. Rows
* of (max, sum, 4 (id, logit) pairs descending) a rank, k = 4, steps 3, n = 24. */
+1
View File
@@ -85,6 +85,7 @@ int avx_par_force(int threads);
X(mla_attn) \
X(mla_attn_paged) \
X(add) X(grid_embed) X(grid_embed_aa) X(causal_conv1d) \
X(grid_embed_cpe) \
X(lili_cand) X(lili_attn) X(lili_factor) X(lili_walk) \
X(mul) X(silu_mul) X(gelu_mul) X(gelu) \
X(silu) X(sigmoid) X(logit_softcap) X(gather_rows) \
+67
View File
@@ -352,3 +352,70 @@ AVX_KERNEL(grid_embed_aa) {
}
return RAD_OK;
}
/* ------------------------------------------------------------------ grid_embed_cpe */
/* Adds the table resampled as torch's F.interpolate(bilinear, align_corners=False) does without
* antialias, onto a square of the grid's larger side and cropped to the grid (RADIO's cropped
* position embedding); matches libref's grid_embed_cpe operation for operation. */
static void cpe_taps(int64_t at, int64_t d, int64_t side, int64_t t[2], float w[2]) {
if (d < 1) d = 1;
at = at < 0 ? 0 : (at >= d ? d - 1 : at);
float src = (float)side / (float)d * ((float)at + 0.5f) - 0.5f;
if (src < 0.0f) src = 0.0f;
int64_t i0 = (int64_t)src;
if (i0 > side - 1) i0 = side - 1;
t[0] = i0;
t[1] = i0 < side - 1 ? i0 + 1 : i0;
w[1] = src - (float)i0;
w[0] = 1.0f - w[1];
}
AVX_KERNEL(grid_embed_cpe) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *tab = &a->t[1], *crd = &a->t[2];
const int64_t n = p_int(a, "n", tab->rank ? tab->shape[tab->rank - 1] : 0);
const int64_t side = p_int(a, "side", 0);
if (n <= 0 || side <= 0 || numel(tab) < side * side * n) return RAD_E_SHAPE;
const int64_t rows = rows_of(x, n);
if (rows <= 0 || crd->rank != 2 || crd->shape[0] < 4 || crd->shape[1] < rows)
return RAD_E_SHAPE;
const int64_t cs = crd->stride[0], ms = crd->stride[1];
const int64_t ts = laststride(tab);
/* The prefix rows, when n_prefix names some; without it the operand is not read. */
const int64_t npre = p_int(a, "n_prefix", 0);
const RadTensor* pre = npre > 0 && a->n_t > 3 && a->t[3].data ? &a->t[3] : nullptr;
if (npre > 0 && (!pre || numel(pre) < npre * n)) return RAD_E_SHAPE;
AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
int64_t g[4];
for (int c = 0; c < 4; ++c) g[c] = ldi(crd, c * cs + r * ms);
if (g[0] < 0) {
/* A row ahead of its segment's patches: the learned row its column names. */
const int64_t k = g[1] < 0 ? 0 : (g[1] >= npre ? npre - 1 : g[1]);
const int64_t xb0 = rowoff(x, r, n), xs0 = laststride(x);
for (int64_t i = 0; npre > 0 && i < n; ++i) {
const float pe = round_act(x->dtype, ldt(pre, rowoff(pre, k, n) + i * laststride(pre)));
stt(x, xb0 + i * xs0, ldt(x, xb0 + i * xs0) + pe);
}
continue;
}
const int64_t d = g[2] > g[3] ? g[2] : g[3];
int64_t 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);
const int64_t xb = rowoff(x, r, n), xs = laststride(x);
auto at = [&](int64_t y, int64_t c, int64_t i) {
return ldt(tab, rowoff(tab, y * side + c, n) + i * ts);
};
for (int64_t i = 0; i < n; ++i) {
const float r0 = ww[0] * at(th[0], tw[0], i) + ww[1] * at(th[0], tw[1], i);
const float r1 = ww[0] * at(th[1], tw[0], i) + ww[1] * at(th[1], tw[1], i);
const float pe = round_act(x->dtype, wh[0] * r0 + wh[1] * r1);
stt(x, xb + i * xs, ldt(x, xb + i * xs) + pe);
}
}
return RAD_OK;
}
+10
View File
@@ -144,6 +144,9 @@ static const RadParamSpec pLayernorm[] = { P_INT("M"), P_INT("n"), P_F64("eps"
static const RadOperandSpec oLayernorm[] = { OPD("x"), WGT("w"), WGT_O("b"), OUT("y") };
static const RadParamSpec pGridEmbed[] = { P_INT("M"), P_INT("n"), P_INT("side"), P_STR("dtype") };
static const RadOperandSpec oGridEmbed[] = { INOUT("x"), WGT("table"), OPD("coord") };
static const RadParamSpec pGridEmbedCpe[] = { P_INT("M"), P_INT("n"), P_INT("side"), P_STR("dtype"),
P_INTO("n_prefix") };
static const RadOperandSpec oGridEmbedCpe[] = { INOUT("x"), WGT("table"), OPD("coord"), WGT_O("prefix") };
/* LiLiCorr's correlator (avx_lili.cpp; libref/ref_lili.cpp has the contracts). */
static const RadParamSpec pLiliCand[] = { P_INT("M"), P_INT("k"), P_INT("world"), P_INT("steps"),
P_INT("n"), P_STR("dtype"), P_F64O("eps"),
@@ -1016,6 +1019,10 @@ static const RadOpSchema kSchemas[] = {
"[h | a | h * a] a lattice row, a its sequence's anchor." },
{ "lili_walk", ARR(pLiliWalk), ARR(oLiliWalk),
"LiLiCorr's normalised edge scores and their greedy walk." },
{ "grid_embed_cpe", ARR(pGridEmbedCpe), ARR(oGridEmbedCpe),
"x += the table resampled half-pixel bilinear (no antialias) onto a square of the grid's larger "
"side and cropped to the grid: RADIO's cropped position embedding. A row with a negative row "
"coordinate gets the learned prefix row its column names." },
{ "add", ARR(pAdd), ARR(oBinary),
"y = a + b; a `b` of exactly n elements against a taller output broadcasts along rows. With "
"`div`, y = rd(rd(a + b) / div), bit for bit libref's." },
@@ -1681,6 +1688,9 @@ SIMPLE("avx_grid_embed", "grid_embed", "elem",
SIMPLE("avx_grid_embed_aa", "grid_embed_aa", "elem",
"antialiased half-pixel bilinear resample of a position grid, added in", DT_FLOAT,
L(grid_embed_aa)),
SIMPLE("avx_grid_embed_cpe", "grid_embed_cpe", "elem",
"half-pixel bilinear resample of a position grid onto a square, cropped, added in", DT_FLOAT,
L(grid_embed_cpe)),
SIMPLE("avx_lili_cand", "lili_cand", "draft", "LiLiCorr candidates and their features", DT_FLOAT,
L(lili_cand)),
SIMPLE("avx_lili_attn", "lili_attn", "draft", "LiLiCorr lattice attention with slot biases",