PIN ref2va: verified device differential + ref-count gate (revert anchor)

Ref2VA ref-image conditioning verified on-device: ref step runs all steps no
fault; output differs ref-present vs ref-absent (by log). 6-9 images rejected
cleanly at submit (no engine crash). Merged-cell modulation (<=9) tracked next.
NOTE: placeholder author -- re-author before any push. Do NOT push this branch.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-19 12:04:47 -05:00
1 parent 816e24bd46
commit 490d4c604a
73 files changed
+16484 -432

No files matched your search

+28 -11
View File
@@ -50,8 +50,12 @@ inline int64_t ldi(const RadTensor* t, int64_t i) {
* them), outside of which the input reads zero (each sequence is padded on its own). w is
* PyTorch's Conv1d weight [cout, cin, k], b [cout], `dil` the dilation and the padding
* dil (k - 1) / 2 on each side (k odd), so a sequence keeps its length; with `causal` 1 all of it,
* dil (k - 1), on the left (any k). `res`, when bound, is added after the bias; then `act` (none,
* tanh, or clamp to [-1, 1], a vocoder's output clamp). f32 accumulation, one rounding. */
* dil (k - 1), on the left (any k). With `stride` > 1 - bf16 only, the encoder's downsample - the
* op is one sequence [0, rows of x], `pad` explicit and any k, and output row m reads
* x[m * stride + tap * dil - pad]: PyTorch's zero-padded strided conv, whose output length
* (rows + 2 pad - dil (k - 1) - 1) / stride + 1 the y operand must have exactly. `res`, when
* bound, is added after the bias; then `act` (none, tanh, or clamp to [-1, 1], a vocoder's output
* clamp). f32 accumulation, one rounding. */
int ref_conv1d(const RadArgs* a, RadStream) {
if (!have(a, 3)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *w = &a->t[1], *cu = &a->t[2];
@@ -59,29 +63,42 @@ int ref_conv1d(const RadArgs* a, RadStream) {
if (!y->data) return RAD_E_INVAL;
const int64_t cin = p_int(a, "cin", 0), cout = p_int(a, "cout", 0), k = p_int(a, "k", 0);
const int64_t dil = p_int(a, "dil", 1), causal = p_int(a, "causal", 0);
const int64_t st = p_int(a, "stride", 1), pp = p_int(a, "pad", -1);
const char* act = p_str(a, "act", "none");
const bool tanh_out = act && !std::strcmp(act, "tanh");
const bool clamp_out = act && !std::strcmp(act, "clamp");
if (!tanh_out && !clamp_out && act && std::strcmp(act, "none")) return RAD_E_INVAL;
if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 ||
causal < 0 || causal > 1)
if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && st <= 1 && (k & 1) == 0) || dil <= 0 ||
causal < 0 || causal > 1 || st < 1 || (st > 1 && (causal || pp < 0)) ||
(st <= 1 && pp >= 0))
return RAD_E_SHAPE;
const int64_t n_seq = numel(cu) - 1;
if (n_seq <= 0) return RAD_E_SHAPE;
const int64_t r0 = cu_at(cu, 0), r1 = cu_at(cu, n_seq);
if (r0 < 0 || r1 <= r0 || rows_of(y, cout) < r1 || rows_of(x, cin) < r1) return RAD_E_SHAPE;
if (r0 < 0 || r1 <= r0 || rows_of(x, cin) < r1) return RAD_E_SHAPE;
if (numel(w) < cout * cin * k || (b && numel(b) < cout)) return RAD_E_SHAPE;
if (res && rows_of(res, cout) < r1) return RAD_E_SHAPE;
const int64_t pad = causal ? dil * (k - 1) : dil * (k - 1) / 2;
REF_PAR_FOR((r1 - r0) * cout * cin * k)
for (int64_t r = r0; r < r1; ++r) {
const int64_t e = seq_of(cu, n_seq, r);
/* The strided form: one full sequence, bf16, and y exactly the formula's rows. */
int64_t hi = r1;
if (st > 1) {
if (x->dtype != RAD_BF16) return RAD_E_DTYPE;
if (n_seq != 1 || r0 != 0 || r1 != rows_of(x, cin)) return RAD_E_SHAPE;
hi = (r1 + 2 * pp - dil * (k - 1) - 1) / st + 1;
if (hi <= 0 || rows_of(y, cout) != hi) return RAD_E_SHAPE;
} else if (rows_of(y, cout) < r1) {
return RAD_E_SHAPE;
}
if (res && rows_of(res, cout) < hi) return RAD_E_SHAPE;
const int64_t pad = pp >= 0 ? pp : (causal ? dil * (k - 1) : dil * (k - 1) / 2);
const int64_t lo = st > 1 ? 0 : r0; /* a strided op starts at row 0 of its one sequence */
REF_PAR_FOR((hi - lo) * cout * cin * k)
for (int64_t r = lo; r < hi; ++r) {
const int64_t e = seq_of(cu, n_seq, st > 1 ? r * st : r);
const int64_t s0 = cu_at(cu, e), s1 = cu_at(cu, e + 1);
const int64_t yb = rowoff(y, r, cout), ys = laststride(y);
for (int64_t co = 0; co < cout; ++co) {
float acc = 0.0f;
for (int64_t kk = 0; kk < k; ++kk) {
const int64_t ir = r + kk * dil - pad;
const int64_t ir = r * st + kk * dil - pad;
if (ir < s0 || ir >= s1) continue;
const int64_t xb = rowoff(x, ir, cin), xs = laststride(x);
for (int64_t ci = 0; ci < cin; ++ci)
+28
View File
@@ -460,6 +460,34 @@ int ref_pool_rows(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ pool_cols */
/* Column c of row m of y is the mean of x[m]'s columns [c*k, c*k + k), summed in f32 and rounded
* to y's dtype, then multiplied by `mult` and rounded again — pool_rows' dual: it pools the
* columns, what a torch adaptive_avg_pool1d over the feature axis of an encoder's posterior head
* computes. */
int ref_pool_cols(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *Y = &a->t[1];
const int64_t n = p_int(a, "n", 0), k = p_int(a, "k", 0);
const float mult = p_f32(a, "mult", 1.0f);
if (n <= 0 || k <= 0 || n % k) return RAD_E_SHAPE;
const int64_t M = rows_of(X, n), nw = n / k;
if (M <= 0 || numel(Y) < M * nw) return RAD_E_SHAPE;
const float inv = 1.0f / (float)k;
REF_PAR_FOR(M * nw * k)
for (int64_t r = 0; r < M; ++r) {
const int64_t yb = rowoff(Y, r, nw), ys = laststride(Y), xs = laststride(X);
for (int64_t c = 0; c < nw; ++c) {
float acc = 0.0f;
for (int64_t j = 0; j < k; ++j) acc += ldt(X, rowoff(X, r, n) + (c * k + j) * xs);
stt(Y, yb + c * ys, acc * inv);
stt(Y, yb + c * ys, ldt(Y, yb + c * ys) * mult);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ relu */
/* y = max(x, 0) over [M, n], each operand at its own row pitch. */
int ref_relu(const RadArgs* a, RadStream) {
+2 -1
View File
@@ -276,13 +276,14 @@ int ref_timestep_embed(const RadArgs* a, RadStream) {
const float scale = p_f32(a, "scale", 1000.0f);
const double mp = p_f32(a, "max_period", 10000.0f);
const int round = (int)p_int(a, "round", 0);
const float clamp = p_f32(a, "clamp_min", 0.0f);
if (n <= 0 || (n & 1)) return RAD_E_SHAPE;
const int64_t rows = rows_of(y, n);
if (rows <= 0 || numel(t) < rows) return RAD_E_SHAPE;
const int64_t h = n / 2;
const float coef = (float)(-std::log(mp));
for (int64_t m = 0; m < rows; ++m) {
const float tv = ldt(t, offlin(t, m));
const float tv = std::fmax(ldt(t, offlin(t, m)), clamp); /* ref pins are floors */
const float tt = round ? scale * rbf(rbf(tv * scale) / scale) : scale * tv;
const int64_t yb = rowoff(y, m, n), ys = laststride(y);
for (int64_t i = 0; i < h; ++i) {
+2
View File
@@ -73,6 +73,7 @@ int ref_act_mul (const RadArgs*, RadStream);
int ref_mul_weight (const RadArgs*, RadStream);
int ref_seg_mean (const RadArgs*, RadStream);
int ref_pool_rows (const RadArgs*, RadStream);
int ref_pool_cols (const RadArgs*, RadStream);
int ref_relu (const RadArgs*, RadStream);
int ref_clamp (const RadArgs*, RadStream);
int ref_conv2d (const RadArgs*, RadStream);
@@ -280,6 +281,7 @@ REF_SHAPE_FN(ref_act_mul);
REF_SHAPE_FN(ref_mul_weight);
REF_SHAPE_FN(ref_seg_mean);
REF_SHAPE_FN(ref_pool_rows);
REF_SHAPE_FN(ref_pool_cols);
REF_SHAPE_FN(ref_relu);
REF_SHAPE_FN(ref_clamp);
REF_SHAPE_FN(ref_conv2d);
+25 -8
View File
@@ -678,7 +678,7 @@ static const RadOperandSpec oRmsModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT
static const RadOperandSpec oFlowX0[] = { OPD("v"), OPD("t"), OPD("sigma"), OPD("sigma_next"),
INOUT("x") };
static const RadParamSpec pTimestep[] = { P_INT("M"), P_INT("n"), P_F64("scale"), P_F64("max_period"),
P_INTO("round"), P_STR("dtype") };
P_INTO("round"), P_F64O("clamp_min"), P_STR("dtype") };
static const RadOperandSpec oTimestep[] = { OPD("t"), OUT("y") };
static const RadOperandSpec oGather2[] = { OPD("a"), OPD("b"), OPD("idx"), OUT("y") };
static const RadParamSpec pLane[] = { P_INT("M"), P_INT("n"), P_INT("stride"), P_STR("dtype") };
@@ -753,8 +753,12 @@ static const RadOperandSpec oAttnVsa[] = { OPD("q"), OPD("k"), OPD("v"), OPD("se
/* ---- the latent upscaler (ref_video.cpp). `geom` is the request's volume, i32 on the device:
* (T, H, W) for conv3d_ndhwc and tconv_dw, (T, h, w, H, W) for resize_bilinear and (F, Hp, Wp)
* for patch_fold, which shares patch_unfold's parameters and operands. */
/* `stride`/`stride_t`/`causal`/`pad`/`reflect`: the video encoder's CausalConv3d forms; the
* defaults are the stride-1 `same` zero padding of everything shipped before them. */
static const RadParamSpec pConv3d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("kt"), P_STR("dtype") };
P_INT("kt"), P_STR("dtype"), P_INTO("stride"),
P_INTO("stride_t"), P_INTO("causal"), P_STRO("pad"),
P_INTO("reflect") };
static const RadOperandSpec oConv3d[] = { OPD("x"), WGT("w"), WGT("b"), OPD("geom"),
OPD_O("res"), OUT("y") };
/* `mod` is one row [scale | shift]; `stats` the per-group mean and rstd, then scratch. */
@@ -773,7 +777,7 @@ static const RadOperandSpec oFlowRenoise[] = { OPD("sigma"), OPD("seed"), INOUT(
/* ---- audio generation (ref_audio.cpp) */
static const RadParamSpec pConv1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("dil"), P_STRO("act"), P_STR("dtype"),
P_INTO("causal") };
P_INTO("causal"), P_INTO("stride"), P_INTO("pad") };
static const RadOperandSpec oConv1d[] = { OPD("x"), WGT("w"), OPD("cu_seqlens"), OPD_O("res"),
OUT("y"), WGT_O("b") };
static const RadParamSpec pConvT1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("stride"),
@@ -899,6 +903,10 @@ static const RadOpSchema kSchemas[] = {
{ "pool_rows", ARR(pPoolRows), ARR(oUnary),
"Row r of y is the mean of x's rows [r*k, r*k + k), summed in f32 and rounded to y's dtype, then "
"times `mult` (default 1) and rounded again. M, the input rows, is a multiple of k." },
{ "pool_cols", ARR(pPoolRows), ARR(oUnary),
"Column c of row m of y is the mean of x[m]'s columns [c*k, c*k + k), summed in f32 and rounded "
"to y's dtype, then times `mult` (default 1) and rounded again. n, the input width, is a "
"multiple of k." },
{ "relu", ARR(pMnD), ARR(oUnary), "y = max(x, 0), each operand at its own row pitch." },
{ "clamp", ARR(pMnD), ARR(oClamp),
"y = min(max(x, lo), hi), `lo` and `hi` WEIGHTS of one element each: a clipped linear's range." },
@@ -1463,10 +1471,15 @@ static const RadOpSchema kSchemas[] = {
"max subtracted. With `coarse` and `gate`: out = rd(rd(o) + coarse[h][tile] gate), rd the "
"output's dtype." },
{ "conv3d_ndhwc", ARR(pConv3d), ARR(oConv3d),
"a 3-D convolution over voxel rows [T H W, cin] -> [T H W, cout] (geom = (T, H, W)): "
"PyTorch's Conv3d weight [cout, cin, kt, k, k] and bias, stride 1, `same` zero padding (kt/2 "
"frames, k/2 rows and columns a side; k and kt odd), f32 accumulation, rd(acc + b); with `res`, "
"rd(rd(acc + b) + res), `res` possibly y itself." },
"a 3-D convolution over voxel rows of geom = (T, H, W) -> [rows out, cout]: "
"PyTorch's Conv3d weight [cout, cin, kt, k, k] and bias, f32 accumulation, rd(acc + b). "
"Defaults are stride 1 with `same` zero padding (kt/2 frames, k/2 rows and columns a side; "
"k and kt odd). `stride` (spatial) and `stride_t` > 1 pad then stride (3x3x3 only); `causal` "
"1 pads the time axis front-only with kt - 1 zeros and none behind (so one frame in gives "
"one frame out at any stride, the reference's T==1 tap truncation); `pad` `down` pads none "
"above and left and one below and right; `reflect` 1 mirrors the spatial padding (torch "
"mode `reflect`, a mirror with no repeated edge, so the padded dimension must exceed the "
"pad). With `res`, rd(rd(acc + b) + res), `res` possibly y itself." },
{ "group_norm", ARR(pGroupNorm), ARR(oGroupNorm),
"PyTorch's GroupNorm over all M rows: group g is channels [g n/groups, (g+1) n/groups), its "
"biased mean and variance in f64, rstd = 1 / sqrt(var + eps) in f32; v = (x - mean) rstd w + b, "
@@ -1492,7 +1505,9 @@ static const RadOpSchema kSchemas[] = {
"a 1-D convolution over the sequences `cu_seqlens` bounds, channels innermost: x [rows, cin], "
"y [rows, cout], PyTorch's [cout, cin, k] weight and an optional bias, dilation `dil` and padding "
"dil (k - 1) / 2 a side (k odd), or dil (k - 1) on the left with `causal` 1, zeros outside each "
"sequence; `res` added after the bias, then `act` none, tanh or clamp (to [-1, 1])." },
"sequence; `res` added after the bias, then `act` none, tanh or clamp (to [-1, 1]). bf16 only "
"with `stride` > 1: one sequence [0, rows of x], an explicit `pad` and any k, output row m "
"reading x[m * stride + tap * dil - pad] — PyTorch's zero-padded strided conv exactly." },
{ "conv_t1d", ARR(pConvT1d), ARR(oConvT1d),
"a transposed 1-D convolution of kernel `k` >= stride (2 stride by default), padding `pad` "
"((k - stride) / 2 by default, 0 the causal form): each sequence of x [rows, cin] grows "
@@ -1713,6 +1728,8 @@ SIMPLE("ref_seg_mean", "seg_mean", "elem", "mean of each cu_seqlens segment's ro
ref_seg_mean),
SIMPLE("ref_pool_rows", "pool_rows", "elem", "mean of every k consecutive rows, then a multiplier",
DT_FLOAT, ref_pool_rows),
SIMPLE("ref_pool_cols", "pool_cols", "elem", "mean of every k consecutive columns, then a "
"multiplier", DT_FLOAT, ref_pool_cols),
SIMPLE("ref_relu", "relu", "elem", "max(x, 0)", DT_FLOAT, ref_relu),
SIMPLE("ref_clamp", "clamp", "elem", "x clamped to two weight scalars", DT_FLOAT, ref_clamp),
SIMPLE("ref_conv2d", "conv2d", "elem", "segment-wise 2-D convolution, channels innermost",
+22 -4
View File
@@ -459,6 +459,16 @@ int ref_pool_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out
return o.finish("pool_rows", operand, out);
}
int ref_pool_cols_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t n = gi(p, n_p, "n", 0), k = gi(p, n_p, "k", 0), M = gi(p, n_p, "M", 0);
if (M <= 0 || n <= 0 || k <= 0 || n % k) return RAD_E_UNSUPPORTED;
Opd o;
o.t(D, { M, n }); /* x */
o.t(D, { M, n / k }); /* y */
return o.finish("pool_cols", operand, out);
}
int ref_relu_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);
@@ -2765,19 +2775,27 @@ int ref_attn_vsa_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
* REF_LANES lanes, as the image generator's are. */
static int64_t two_seqs(int64_t M) { return M >= 4 ? 2 : 1; }
/* `M` is the input rows; with `stride` > 1 (bf16, one sequence, explicit `pad`) the output is
* PyTorch's strided length, `M` being the rows of the one sequence `cu_seqlens` must cover. */
int ref_conv1d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
const int64_t k = gi(p, n_p, "k", 0), causal = gi(p, n_p, "causal", 0);
if (M <= 0 || cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0))
const int64_t st = gi(p, n_p, "stride", 1), pp = gi(p, n_p, "pad", -1);
const int64_t dil = gi(p, n_p, "dil", 1);
if (M <= 0 || cin <= 0 || cout <= 0 || k <= 0 || (!causal && st <= 1 && (k & 1) == 0) ||
st < 1 || (st > 1 && (causal || pp < 0)) || (st <= 1 && pp >= 0))
return RAD_E_UNSUPPORTED;
const uint32_t D = act_dt(p, n_p);
const int64_t S = two_seqs(M);
if (st > 1 && D != (uint32_t)RAD_BF16) return RAD_E_UNSUPPORTED;
const int64_t S = st > 1 ? 1 : two_seqs(M);
const int64_t O = st > 1 ? (M + 2 * pp - dil * (k - 1) - 1) / st + 1 : M;
if (O <= 0) return RAD_E_UNSUPPORTED;
Opd o;
o.t(D, { M, cin }); /* x */
o.t(D, { cout, cin, k }); /* w */
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
o.t(D, { M, cout }); /* res */
o.t(D, { M, cout }); /* y */
o.t(D, { O, cout }); /* res */
o.t(D, { O, cout }); /* y */
o.t(D, { cout }); /* b */
return o.finish("conv1d", operand, out);
}
+52 -14
View File
@@ -346,9 +346,10 @@ int ref_vtile_stitch(const RadArgs* a, RadStream) {
* `geom` carries the request's volume; an output holds at least the rows the volume writes. */
/* ------------------------------------------------------------------ conv3d_ndhwc */
/* A 3-D convolution, stride 1 with `same` zero padding: kt / 2 frames and k / 2 rows and columns
* on each side (kt and k odd). x [>= T H W, cin], y and `res` [>= T H W, cout], geom = (T, H, W);
* w is PyTorch's Conv3d weight [cout, cin, kt, k, k] and b [cout]:
/* A 3-D convolution over the volume geom = (T, H, W). The defaults are stride 1 with `same` zero
* padding: kt / 2 frames and k / 2 rows and columns on each side (kt and k odd); x
* [>= T H W, cin] and y and `res` [>= rows out, cout]; w is PyTorch's Conv3d weight
* [cout, cin, kt, k, k] and b [cout]:
*
* acc = sum over (dt, kh, kw, ci) of w[o, ci, dt, kh, kw] x[(t + dt - kt/2, i + kh - k/2,
* j + kw - k/2), ci]
@@ -357,7 +358,25 @@ int ref_vtile_stitch(const RadArgs* a, RadStream) {
* terms outside the volume zero. acc is f32, the taps in (dt, kh, kw) order and each tap's
* channels ascending; rd is y's dtype. With `res` the convolution's output is rounded before the
* residual is added, as a residual block's skip(x) + h adds two bf16 tensors. `res` may be y
* itself: each output element reads its own residual before its write, and no other. */
* itself: each output element reads its own residual before its write, and no other.
*
* The optional `stride` (spatial) and `stride_t` pad first and then stride, `causal` 1 takes the
* temporal padding front-only (kt - 1 zeros, none behind), `pad` `down` pads none above and left
* and one below and right, and `reflect` 1 mirrors the spatial padding instead of zeroing it;
* these CausalConv3d forms need k = kt = 3. Output (t, i, j) then reads
* x[(t stride_t + dt - ft, i stride + kh - pt, j stride + kw - pl), ci] over the lengths
*
* ((T + ft + bk - kt) / stride_t + 1) ((H + pt + pb - k) / stride + 1)
* ((W + pl + pr - k) / stride + 1) (floor divisions)
*
* with ft / bk and pt / pb / pl / pr the front / back and top / bottom / left / right pads. The
* time axis is always zero-padded; `reflect` mirrors each spatial axis once, torch's `reflect`
* (x[-n] = x[n], x[D + n] = x[D - 2 - n]), so each pad must be below the axis it pads. */
static int64_t idiv_floor(int64_t a, int64_t b) {
const int64_t q = a / b;
return (a % b && (a < 0) != (b < 0)) ? q - 1 : q;
}
int ref_conv3d_ndhwc(const RadArgs* a, RadStream) {
if (a->n_t < 6 || !have(a, 4) || !t_in(a, 5)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *w = &a->t[1], *b = &a->t[2], *geom = &a->t[3];
@@ -366,29 +385,48 @@ int ref_conv3d_ndhwc(const RadArgs* a, RadStream) {
const int64_t k = p_int(a, "k", 0), kt = p_int(a, "kt", 0);
if (cin <= 0 || cout <= 0 || k <= 0 || kt <= 0 || !(k & 1) || !(kt & 1) || numel(geom) < 3)
return RAD_E_SHAPE;
const int64_t sp = p_int(a, "stride", 1), st = p_int(a, "stride_t", 1);
const int64_t causal = p_int(a, "causal", 0), reflect = p_int(a, "reflect", 0);
const char* pads = p_str(a, "pad", "same");
const bool down = pads && !std::strcmp(pads, "down");
if (sp <= 0 || st <= 0 || (causal && causal != 1) || (reflect && reflect != 1) ||
!(!pads || !std::strcmp(pads, "same") || down))
return RAD_E_SHAPE;
const bool gen = sp != 1 || st != 1 || causal || down || reflect;
if (gen && (k != 3 || kt != 3)) return RAD_E_SHAPE;
const int64_t T = ldi(geom, 0), H = ldi(geom, 1), W = ldi(geom, 2);
if (T <= 0 || H <= 0 || W <= 0) return RAD_E_SHAPE;
const int64_t rows = T * H * W, taps = kt * k * k;
if (rows_of(y, cout) < rows || rows_of(x, cin) < rows || (res && rows_of(res, cout) < rows) ||
numel(w) < cout * cin * taps || numel(b) < cout)
const int64_t ft = causal ? kt - 1 : kt / 2, bk = causal ? 0 : kt / 2;
const int64_t pt = down ? 0 : k / 2, pb = down ? 1 : k / 2;
const int64_t pl = down ? 0 : k / 2, pr = down ? 1 : k / 2;
if (reflect && (std::max(pt, pb) >= H || std::max(pl, pr) >= W)) return RAD_E_SHAPE;
const int64_t T2 = idiv_floor(T + ft + bk - kt, st) + 1;
const int64_t H2 = idiv_floor(H + pt + pb - k, sp) + 1;
const int64_t W2 = idiv_floor(W + pl + pr - k, sp) + 1;
if (T2 <= 0 || H2 <= 0 || W2 <= 0) return RAD_E_SHAPE;
const int64_t rows = T * H * W, orows = T2 * H2 * W2, taps = kt * k * k;
if (rows_of(y, cout) < orows || rows_of(x, cin) < rows ||
(res && rows_of(res, cout) < orows) || numel(w) < cout * cin * taps || numel(b) < cout)
return RAD_E_SHAPE;
const int64_t pz = kt / 2, ps = k / 2, xs = laststride(x), ys = laststride(y);
const int64_t xs = laststride(x), ys = laststride(y);
const int64_t rs = res ? laststride(res) : 0;
const uint32_t dt = y->dtype;
REF_PAR_FOR(rows * cout * cin * taps)
for (int64_t r = 0; r < rows; ++r) {
const int64_t t = r / (H * W), i = (r / W) % H, j = r % W;
REF_PAR_FOR(orows * cout * cin * taps)
for (int64_t r = 0; r < orows; ++r) {
const int64_t t = r / (H2 * W2), i = (r / W2) % H2, j = r % W2;
const int64_t yb = rowoff(y, r, cout), rb = res ? rowoff(res, r, cout) : 0;
for (int64_t o = 0; o < cout; ++o) {
float acc = 0.0f;
for (int64_t dz = 0; dz < kt; ++dz) {
const int64_t tz = t + dz - pz;
const int64_t tz = t * st + dz - ft;
if (tz < 0 || tz >= T) continue;
for (int64_t kh = 0; kh < k; ++kh) {
const int64_t iy = i + kh - ps;
int64_t iy = i * sp + kh - pt;
if (reflect) iy = iy < 0 ? -iy : (iy >= H ? 2 * H - 2 - iy : iy);
if (iy < 0 || iy >= H) continue;
for (int64_t kw = 0; kw < k; ++kw) {
const int64_t ix = j + kw - ps;
int64_t ix = j * sp + kw - pl;
if (reflect) ix = ix < 0 ? -ix : (ix >= W ? 2 * W - 2 - ix : ix);
if (ix < 0 || ix >= W) continue;
const int64_t xb = rowoff(x, (tz * H + iy) * W + ix, cin);
const int64_t wb = o * cin * taps + (dz * k + kh) * k + kw;