Archived
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:
1 parent
2420b2b383
commit
b466c91b26
17 files changed
+426
-2
No files matched your search
@@ -249,6 +249,73 @@ int ref_grid_embed_aa(const RadArgs* a, RadStream) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* grid_embed_cpe: RADIO's cropped position embedding (CPE). The table resampled as torch's
|
||||
* F.interpolate(mode="bilinear", align_corners=False) -- without antialias -- onto a square of side
|
||||
* d = max(grid height, grid width), of which the grid keeps the top-left h x w. Per axis two taps at
|
||||
* src = (i + 0.5) * side / d - 0.5, clamped at 0, the upper tap clamped into the table, and blended
|
||||
* along the row first as torch's bilinear kernel does. f32, rounded once before the add. */
|
||||
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];
|
||||
}
|
||||
|
||||
int ref_grid_embed_cpe(const RadArgs* a, RadStream) {
|
||||
if (!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;
|
||||
|
||||
REF_PAR_FOR(rows * n * 4)
|
||||
for (int64_t r = 0; r < rows; ++r) {
|
||||
int64_t g[4];
|
||||
for (int c = 0; c < 4; ++c) g[c] = ld_int(crd->dtype, crd->data, c * cs + r * ms);
|
||||
const int64_t xb0 = rowoff(x, r, n), xs0 = laststride(x);
|
||||
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]);
|
||||
for (int64_t i = 0; npre > 0 && i < n; ++i) {
|
||||
const float pe = round_dt(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_dt(x->dtype, wh[0] * r0 + wh[1] * r1);
|
||||
stt(x, xb + i * xs, ldt(x, xb + i * xs) + pe);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ add / mul */
|
||||
/* Elementwise over the output's extent. `b` may be a single row of exactly `n` elements, which
|
||||
* broadcasts along rows (a bias add); nothing else broadcasts, so a shape bug cannot pass as a
|
||||
|
||||
@@ -73,6 +73,7 @@ int ref_mtp_enter (const RadArgs*, RadStream);
|
||||
int ref_layernorm (const RadArgs*, RadStream);
|
||||
int ref_grid_embed (const RadArgs*, RadStream);
|
||||
int ref_grid_embed_aa (const RadArgs*, RadStream);
|
||||
int ref_grid_embed_cpe (const RadArgs*, RadStream);
|
||||
int ref_lili_cand (const RadArgs*, RadStream);
|
||||
int ref_lili_attn (const RadArgs*, RadStream);
|
||||
int ref_lili_factor (const RadArgs*, RadStream);
|
||||
@@ -364,6 +365,7 @@ REF_SHAPE_FN(ref_mtp_enter);
|
||||
REF_SHAPE_FN(ref_layernorm);
|
||||
REF_SHAPE_FN(ref_grid_embed);
|
||||
REF_SHAPE_FN(ref_grid_embed_aa);
|
||||
REF_SHAPE_FN(ref_grid_embed_cpe);
|
||||
REF_SHAPE_FN(ref_lili_cand);
|
||||
REF_SHAPE_FN(ref_lili_attn);
|
||||
REF_SHAPE_FN(ref_lili_factor);
|
||||
|
||||
@@ -172,6 +172,12 @@ static const RadOperandSpec oLayernorm[] = { OPD("x"), WGT("w"), WGT_O("b"), OUT
|
||||
/* A learned position table resampled to each row's grid and added in (a vision tower's). */
|
||||
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") };
|
||||
/* grid_embed_cpe adds the rows a segment carries ahead of its patches (n_prefix, `prefix`). */
|
||||
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 (ref_lili.cpp). */
|
||||
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"),
|
||||
@@ -1220,6 +1226,12 @@ static const RadOpSchema kSchemas[] = {
|
||||
"antialias=True) resamples it to the row's grid -- SigLIP2's 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." },
|
||||
{ "grid_embed_cpe", ARR(pGridEmbedCpe), ARR(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 (CPE). Per axis two taps at (i + 0.5) * side / "
|
||||
"d - 0.5, clamped at 0 and into the table, blended along the row first; f32, rounded once before "
|
||||
"the add. A row whose coordinate row is negative is the k-th of `n_prefix` learned rows ahead of its segment (RADIO's class and register tokens), k its column: it gets `prefix[k]` instead, rounded the same way." },
|
||||
{ "lili_cand", ARR(pLiliCand), ARR(oLiliCand),
|
||||
"LiLiCorr's candidates. `stats` [M, world * (2 + 2k)] f32 is each rank's sample_logprobs row, "
|
||||
"row-interleaved; the ranks merge to the log-normaliser and the k largest pairs by value, ties to "
|
||||
@@ -2363,6 +2375,9 @@ SIMPLE("ref_grid_embed", "grid_embed", "elem",
|
||||
SIMPLE("ref_grid_embed_aa", "grid_embed_aa", "elem",
|
||||
"antialiased half-pixel bilinear resample of a position grid, added in", DT_FLOAT,
|
||||
ref_grid_embed_aa),
|
||||
SIMPLE("ref_grid_embed_cpe", "grid_embed_cpe", "elem",
|
||||
"half-pixel bilinear resample of a position grid onto a square, cropped, added in", DT_FLOAT,
|
||||
ref_grid_embed_cpe),
|
||||
SIMPLE("ref_lili_cand", "lili_cand", "draft", "LiLiCorr candidates and their features",
|
||||
DT_FLOAT, ref_lili_cand),
|
||||
SIMPLE("ref_lili_attn", "lili_attn", "draft", "LiLiCorr lattice attention with slot biases",
|
||||
|
||||
@@ -509,6 +509,20 @@ int ref_grid_embed_aa_shape(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
return o.finish("grid_embed_aa", operand, out);
|
||||
}
|
||||
|
||||
/* grid_embed's operands plus the prefix rows; coordinates drawn over the table's side. */
|
||||
int ref_grid_embed_cpe_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), side = gi(p, n_p, "side", 0);
|
||||
const int64_t npre = gi(p, n_p, "n_prefix", 0);
|
||||
if (M <= 0 || n <= 0 || side <= 0) return RAD_E_UNSUPPORTED;
|
||||
Opd o;
|
||||
o.t(D, { M, n }); /* x, INOUT */
|
||||
o.t(D, { side * side, n }); /* table */
|
||||
o.t_idx({ 4, M }, side, -1, 0);
|
||||
o.t(D, { npre > 0 ? npre : 1, n }); /* prefix */
|
||||
return o.finish("grid_embed_cpe", operand, out);
|
||||
}
|
||||
|
||||
/* LiLiCorr's ops. lili_cand declines: its stats rows (sample_logprobs' sorted pairs and positive
|
||||
* sums) are no draw; avx_check_hand.cpp builds them. The others draw freely. */
|
||||
int ref_lili_cand_shape(const RadParam*, int, int, RadOpdDesc*) { return RAD_E_UNSUPPORTED; }
|
||||
|
||||
Reference in new issue
Block a user