Archived
minimaxh3+ops: a picture too big for one volume encodes tile-by-tile, as the reference does
vae.py's tiled_encode cuts a picture bigger than tile_size (256 px, 16
latents) into overlapping tiles, encodes each tile alone, and blends every
tile's RAW moments with its raw top and left neighbours -- top first, then
left, never an already-blended neighbour -- into the whole latent canvas.
GroupNorm normalises inside the volume it sees, so the tile seams are
observable: our port ran the whole canvas through the trunk at once, and a
photo or keyframe over 256 px a side drifted from the reference's look.
Three ops, libref/avx/r4d alike. vtile_plan lays each tile's geometry rows
the way vae.py splits -- round-robin slack, and one short tile on an axis
no longer than a tile. vtile_crop cuts the picture's pixels tile-major.
vtile_join blends the tile-major moments into the plain pass's b_eq2 with
vae.py's weights, every product and sum rounding through x's dtype. The
split carries one structural gift: with the slack fully distributed,
start[last] = len - tile, so every band is exactly a tile, and a short
band exists only in the single-band branch -- whose axis has no neighbour
to blend against. The join can therefore fetch the left neighbour always
at the full tile width while own and top fetch at the owner's own column
width, and vae.py's min() clamp inside blend is provably dead code.
step_venc_tiled runs the plain trunk once a tile: pixels from b_vtile,
moments out to one slice of b_eqt, the tile's six level geometries read
back off b_tplan (PERSIST) through raw pointers, exactly as every clip's
geometries come off enc_geom_d. The arena packs by declared lifetime, so
op_ve_q moved ahead of the trunk and the tiled ops sit last: b_vtile must
span op_ve_pix's first read to op_vt_crop at the tail and b_eqt must span
op_ve_q to op_vt_join, bracketing every trunk op the tiled pass re-runs.
b_vtile and b_eqt size tile-major at the declared tile cap; a picture
splitting past the cap step-fails by name. Clips never tile -- their
volume forces each side back to kEncMaxSide. OPS.md documents all three.
Verified: ref_test 264 cases (plan pins the round-robin overlaps {6,5,5}
and a short lone row with column neighbours; crop walks a short band at
ratio 4; join checks a 6x9 canvas with uneven round-robin splits point by
point, and a 32x32 eighths-exact f32 blend that must land bit-identical);
rad-avx-check scalar/avx/avx2 on all three ops; device r4d_selftest plan
4 / crop 8 (f32+bf16) / join 5 cases ok. Full ctest 123/123. ABI
unchanged at 28.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
47b7aae7a3
commit
69087ce773
15 files changed
+1407
-14
No files matched your search
@@ -185,6 +185,9 @@ int ref_tconv_dw(const RadArgs*, RadStream);
|
||||
int ref_resize_bilinear(const RadArgs*, RadStream);
|
||||
int ref_flow_renoise(const RadArgs*, RadStream);
|
||||
int ref_cond_aug(const RadArgs*, RadStream);
|
||||
int ref_vtile_plan(const RadArgs*, RadStream);
|
||||
int ref_vtile_crop(const RadArgs*, RadStream);
|
||||
int ref_vtile_join(const RadArgs*, RadStream);
|
||||
/* ref_ltx.cpp */
|
||||
int ref_rope_split(const RadArgs*, RadStream);
|
||||
int ref_head_gate(const RadArgs*, RadStream);
|
||||
@@ -439,6 +442,9 @@ REF_SHAPE_FN(ref_tconv_dw);
|
||||
REF_SHAPE_FN(ref_resize_bilinear);
|
||||
REF_SHAPE_FN(ref_flow_renoise);
|
||||
REF_SHAPE_FN(ref_cond_aug);
|
||||
REF_SHAPE_FN(ref_vtile_plan);
|
||||
REF_SHAPE_FN(ref_vtile_crop);
|
||||
REF_SHAPE_FN(ref_vtile_join);
|
||||
REF_SHAPE_FN(ref_rope_split);
|
||||
REF_SHAPE_FN(ref_head_gate);
|
||||
REF_SHAPE_FN(ref_ltx_guide);
|
||||
|
||||
@@ -845,6 +845,17 @@ static const RadParamSpec pVtileStitch[] = { P_INT("M"), P_INT("tile"), P_INT("o
|
||||
P_INT("keep"), P_INT("blend"), P_INT("period"),
|
||||
P_STR("mean"), P_STR("std") };
|
||||
static const RadOperandSpec oVtileStitch[] = { OPD("x"), OPD("geom"), INOUT("prev"), OUT("y") };
|
||||
/* The tiled encoder (ref_video.cpp): the plan, the cut and the blend. `geom` is the request's
|
||||
* (clip, H, W); plan and crop take it in pixels, join in latent units. */
|
||||
static const RadParamSpec pVtilePlan[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
|
||||
P_INT("ratio") };
|
||||
static const RadOperandSpec oVtilePlan[] = { OPD("geom"), OUT("y") };
|
||||
static const RadParamSpec pVtileCrop[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
|
||||
P_INT("ratio"), P_STR("dtype") };
|
||||
static const RadOperandSpec oVtileCrop[] = { OPD("x"), OPD("geom"), OUT("y") };
|
||||
static const RadParamSpec pVtileJoin[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
|
||||
P_INT("n"), P_STR("dtype") };
|
||||
static const RadOperandSpec oVtileJoin[] = { OPD("x"), OPD("geom"), OUT("y") };
|
||||
/* `y` is [3, >= M], component-major at its own row pitch. */
|
||||
static const RadParamSpec pGridPos3[] = { P_INT("M"), P_INT("T"), P_INT("H"), P_INT("W"),
|
||||
P_INT("period"), P_F64("scale") };
|
||||
@@ -1764,6 +1775,20 @@ static const RadOpSchema kSchemas[] = {
|
||||
"with `prev` unless the chunk is first, prev refilled from the chunk's tail (also emitted "
|
||||
"when it is last), then bytes round(255 clamp(v std + mean, 0, 1)); geom = (frames, H, W, "
|
||||
"first, last), u8 out." },
|
||||
{ "vtile_plan", ARR(pVtilePlan), ARR(oVtilePlan),
|
||||
"the tiled encoder's plan for one frame of geom = (clip, H, W) pixels: split_tiles_axis over "
|
||||
"the spatial axes in latent units (tile, overlap, pixels per latent ratio), tiles row-major; "
|
||||
"row t of the i32 [M, 22] y is the tile's first pixel (y0, x0), its pixel extent (th, tw) and "
|
||||
"level L's volume (1, th / 2^min(L, 4), tw / 2^min(L, 4)) for L = 0..5; M is the tile count." },
|
||||
{ "vtile_crop", ARR(pVtileCrop), ARR(oVtileCrop),
|
||||
"one frame's pixel plane [>= H W, 3] (geom = (clip, H, W) pixels) cut into vtile_plan's tiles: "
|
||||
"y [M, 3] the tiles' pixels tile-major, M the tiles' pixel count (sum of th tw)." },
|
||||
{ "vtile_join", ARR(pVtileJoin), ARR(oVtileJoin),
|
||||
"the tiled encoder's per-tile moments (vtile_crop's row layout at latent extent) blended into "
|
||||
"the canvas y [M, n], M = geom's (clip, lat_h, lat_w) lat_h lat_w: a canvas position's tile "
|
||||
"kept range, blended with the tile above and then the tile to the left, both RAW (never "
|
||||
"already blended), the products and sums rounding to x's dtype as the reference's bf16 blends "
|
||||
"do." },
|
||||
{ "grid_pos3", ARR(pGridPos3), ARR(oGridPos3),
|
||||
"a video decoder's rotary positions over tiles of `period` rows: token (t, i, j) of a tile's "
|
||||
"T x H x W gets f32(scale) * (2 (c + 0.5) / n - 1) on each axis, in f32 as torch makes it, "
|
||||
@@ -2439,6 +2464,12 @@ SIMPLE("ref_vtile_gather", "vtile_gather", "elem", "a video decoder's tiles out
|
||||
SIMPLE("ref_vtile_stitch", "vtile_stitch", "elem",
|
||||
"decoded tiles blended into frames, the chunk overlap, bytes out", DT_FLOAT,
|
||||
ref_vtile_stitch),
|
||||
SIMPLE("ref_vtile_plan", "vtile_plan", "elem", "the tiled encoder's per-tile geometry table",
|
||||
DT_ANY, ref_vtile_plan),
|
||||
SIMPLE("ref_vtile_crop", "vtile_crop", "elem", "a frame's pixel plane cut into encode tiles",
|
||||
DT_FLOAT, ref_vtile_crop),
|
||||
SIMPLE("ref_vtile_join", "vtile_join", "elem",
|
||||
"per-tile encoder moments blended into the latent canvas", DT_FLOAT, ref_vtile_join),
|
||||
SIMPLE("ref_grid_pos3", "grid_pos3", "elem", "a video decoder's rotary positions over its tiles",
|
||||
DT_FLOAT, ref_grid_pos3),
|
||||
SIMPLE("ref_vsa_select", "vsa_select", "attn",
|
||||
|
||||
@@ -2991,6 +2991,22 @@ static int64_t tile_count(int64_t len, int64_t tile, int64_t overlap) {
|
||||
return nt;
|
||||
}
|
||||
|
||||
/* The encoder's split (ref_video.cpp split_tiles_axis) over one axis: the tile count and the
|
||||
* tiles' extent sum along it, a lone tile short of `tile` if the axis is. -1 where the split
|
||||
* refuses. The drawn encode planes are `axis_len` long, so the shape fns can size the tile
|
||||
* operands exactly. */
|
||||
static int64_t axis_split(int64_t len, int64_t tile, int64_t overlap, int64_t* ext) {
|
||||
if (len <= 0 || tile <= 0 || overlap < 0 || overlap >= tile) return -1;
|
||||
if (tile >= len) {
|
||||
*ext = len;
|
||||
return 1;
|
||||
}
|
||||
const int64_t nt = tile_count(len, tile, overlap);
|
||||
/* The extents telescope: sum of the tiles = len + sum of the overlaps = `tile` nt. */
|
||||
*ext = tile * nt;
|
||||
return nt;
|
||||
}
|
||||
|
||||
/* A 2 x 2 x 2 grid of patches. */
|
||||
int ref_patch_unfold_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "C", 0);
|
||||
@@ -3154,6 +3170,50 @@ int ref_cond_aug_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
return o.finish("cond_aug", operand, out);
|
||||
}
|
||||
|
||||
/* ---- the tiled encoder. The drawn frame is 20 latent units a side; with the sweep's tile of
|
||||
* 16 and overlap of 4 the split lays 2 x 2 full tiles over it, so the crops, the plan's level
|
||||
* triples and the blends all draw non-trivial. The kernels size by the geom; these only draw a
|
||||
* plane that one gives them. */
|
||||
int ref_vtile_plan_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
||||
const int64_t ov = gi(p, n_p, "overlap", 0), ratio = gi(p, n_p, "ratio", 0);
|
||||
int64_t e = 0;
|
||||
const int64_t nt = axis_split(20, tile, ov, &e);
|
||||
if (M <= 0 || nt < 0 || ratio <= 0) return RAD_E_UNSUPPORTED;
|
||||
Opd o;
|
||||
o.t_idx({ 3 }, 0, 20 * ratio, 0); /* geom: (_, H, W) pixels */
|
||||
o.t((uint32_t)RAD_I32, { M, 22 }); /* y: one row a tile */
|
||||
return o.finish("vtile_plan", operand, out);
|
||||
}
|
||||
|
||||
int ref_vtile_crop_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
||||
const int64_t ov = gi(p, n_p, "overlap", 0), ratio = gi(p, n_p, "ratio", 0);
|
||||
int64_t e = 0;
|
||||
const int64_t nt = axis_split(20, tile, ov, &e);
|
||||
if (M <= 0 || nt < 0 || ratio <= 0) return RAD_E_UNSUPPORTED;
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
Opd o;
|
||||
o.t(D, { 20 * ratio * (20 * ratio), 3 }); /* x: the pixel plane */
|
||||
o.t_idx({ 3 }, 0, 20 * ratio, 0); /* geom: (_, H, W) pixels */
|
||||
o.t(D, { M, 3 }); /* y: the tiles' pixels */
|
||||
return o.finish("vtile_crop", operand, out);
|
||||
}
|
||||
|
||||
int ref_vtile_join_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
||||
const int64_t ov = gi(p, n_p, "overlap", 0), n = gi(p, n_p, "n", 0);
|
||||
int64_t e = 0;
|
||||
const int64_t nt = axis_split(20, tile, ov, &e);
|
||||
if (M <= 0 || nt < 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
Opd o;
|
||||
o.t(D, { e * e, n }); /* x: the tiles' moments */
|
||||
o.t_idx({ 3 }, 0, 20, 0); /* geom: (_, lat_h, lat_w) */
|
||||
o.t(D, { M, n }); /* y: the canvas */
|
||||
return o.finish("vtile_join", operand, out);
|
||||
}
|
||||
|
||||
/* ================================================================== video sparse attention
|
||||
*
|
||||
* The tiles come from the values of `segs` and `geom`, which a description draws only as
|
||||
|
||||
@@ -63,6 +63,21 @@ bool split_tiles(int64_t len, int64_t tile, int64_t overlap, std::vector<int64_t
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The encoder's split over one axis -- vae.py's `split_tiles` (comfy/ldm/minimax/vae.py), whose
|
||||
* multi-tile branch is `split_tiles`' exactly, but whose single-tile branch answers ([0], [len]):
|
||||
* a tile at least as long as the axis is ONE tile over the whole axis, short if need be, where
|
||||
* the decoder's split demands an exact fit. */
|
||||
bool split_tiles_axis(int64_t len, int64_t tile, int64_t overlap, std::vector<int64_t>* start,
|
||||
std::vector<int64_t>* ov) {
|
||||
if (tile >= len) {
|
||||
start->clear();
|
||||
ov->clear();
|
||||
start->push_back(0);
|
||||
return len > 0 && tile > 0 && overlap >= 0 && overlap < tile;
|
||||
}
|
||||
return split_tiles(len, tile, overlap, start, ov);
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* ------------------------------------------------------------------ patch_unfold */
|
||||
@@ -695,3 +710,178 @@ int ref_cond_aug(const RadArgs* a, RadStream) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ================================================================== the tiled encoder
|
||||
* A one-frame media bigger than the encoder's per-volume side (a reference picture or an
|
||||
* endpoint keyframe) is encoded the way vae.py's `tiled_encode` encodes it: the pixel plane is
|
||||
* cut into `split_tiles_axis`' tiles, every tile goes through the whole encoder on its own, and
|
||||
* the tiles' moments are blended back into one latent plane. The three ops below are the cut,
|
||||
* the plan the cut and the per-tile volumes read, and the blend; the encoder itself is the
|
||||
* existing per-level ops, issued per tile at the plan's geometries. `geom` is always the
|
||||
* request's (clip, H, W) read off the device -- tile starts, extents and counts come from the
|
||||
* split, never from the parameters, which are fixed at declare. */
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_plan */
|
||||
/* The plan one frame's encode runs over: `geom` = (clip, H, W) the frame's extent in pixels,
|
||||
* split by split_tiles_axis over the spatial axes in latent units -- `tile` a side, `overlap`
|
||||
* its minimum blend, `ratio` the VAE's pixels per latent -- and the tiles numbered row-major
|
||||
* over (row band, column band). Row t of y (i32 [M, 22]):
|
||||
*
|
||||
* [0] [1] the tile's first pixel (y0, x0)
|
||||
* [2] [3] the tile's pixel extent (th, tw): `tile` ratio, save the lone short tile
|
||||
* 3 L + 4..6 level L's volume (1, th / 2^min(L, 4), tw / 2^min(L, 4)) for L = 0..5 -- the
|
||||
* encoder's five levels and the deepest one's repeat, which shares it (no
|
||||
* downsampler between them), exactly venc_geom's level layout for one frame.
|
||||
*
|
||||
* M is the tile count; any other count is refused. */
|
||||
int ref_vtile_plan(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *geom = &a->t[0], *y = &a->t[1];
|
||||
const int64_t M = p_int(a, "M", 0), tile = p_int(a, "tile", 0);
|
||||
const int64_t overlap = p_int(a, "overlap", 0), ratio = p_int(a, "ratio", 0);
|
||||
if (M <= 0 || tile <= 0 || overlap < 0 || overlap >= tile || ratio <= 0 || numel(geom) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = ldi(geom, 1), W = ldi(geom, 2);
|
||||
std::vector<int64_t> sy, sx, oy, ox;
|
||||
if (H <= 0 || W <= 0 || H % ratio || W % ratio ||
|
||||
!split_tiles_axis(H / ratio, tile, overlap, &sy, &oy) ||
|
||||
!split_tiles_axis(W / ratio, tile, overlap, &sx, &ox))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
if (M != nh * nw || rows_of(y, 22) < M) return RAD_E_SHAPE;
|
||||
REF_PAR_FOR(M)
|
||||
for (int64_t t = 0; t < M; ++t) {
|
||||
const int64_t i = t / nw, j = t % nw;
|
||||
const int64_t th = std::min(tile, H / ratio - sy[(size_t)i]) * ratio;
|
||||
const int64_t tw = std::min(tile, W / ratio - sx[(size_t)j]) * ratio;
|
||||
const int64_t yb = rowoff(y, t, 22), ys = laststride(y);
|
||||
const int64_t v[22] = { sy[(size_t)i] * ratio, sx[(size_t)j] * ratio, th, tw,
|
||||
1, th, tw, 1, th / 2, tw / 2, 1, th / 4, tw / 4,
|
||||
1, th / 8, tw / 8, 1, th / 16, tw / 16, 1, th / 16, tw / 16 };
|
||||
for (int64_t c = 0; c < 22; ++c) st_int(y->dtype, y->data, yb + c * ys, v[c]);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_crop */
|
||||
/* One frame's pixel plane cut into vtile_plan's tiles: x f32 [>= H W, 3] is the plane (pixel
|
||||
* (Y, X) at row Y W + X) of geom = (clip, H, W) in pixels; y f32 [M, 3] holds the tiles' pixels
|
||||
* tile-major, tile t's (th, tw) rows in plane order at their offset in the sum. M is the tiles'
|
||||
* pixel count, sum of th tw over the plan. Only the row the tile stands on is read; the tiles'
|
||||
* union covers the plane, but their sum may exceed it -- that is why y is its own buffer. */
|
||||
int ref_vtile_crop(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *x = &a->t[0], *geom = &a->t[1], *y = &a->t[2];
|
||||
const int64_t M = p_int(a, "M", 0), tile = p_int(a, "tile", 0);
|
||||
const int64_t overlap = p_int(a, "overlap", 0), ratio = p_int(a, "ratio", 0);
|
||||
if (M <= 0 || tile <= 0 || overlap < 0 || overlap >= tile || ratio <= 0 || numel(geom) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = ldi(geom, 1), W = ldi(geom, 2);
|
||||
std::vector<int64_t> sy, sx, oy, ox;
|
||||
if (H <= 0 || W <= 0 || H % ratio || W % ratio ||
|
||||
!split_tiles_axis(H / ratio, tile, overlap, &sy, &oy) ||
|
||||
!split_tiles_axis(W / ratio, tile, overlap, &sx, &ox))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
std::vector<int64_t> off((size_t)(nh * nw + 1), 0);
|
||||
for (int64_t i = 0; i < nh; ++i) {
|
||||
const int64_t th = std::min(tile, H / ratio - sy[(size_t)i]) * ratio;
|
||||
for (int64_t j = 0; j < nw; ++j)
|
||||
off[(size_t)(i * nw + j + 1)] =
|
||||
off[(size_t)(i * nw + j)] +
|
||||
th * std::min(tile, W / ratio - sx[(size_t)j]) * ratio;
|
||||
}
|
||||
if (M != off.back() || rows_of(x, 3) < H * W || rows_of(y, 3) < M) return RAD_E_SHAPE;
|
||||
REF_PAR_FOR(M * 3)
|
||||
for (int64_t r = 0; r < M; ++r) {
|
||||
const int64_t t = (int64_t)(std::upper_bound(off.begin(), off.end(), r) - off.begin()) - 1;
|
||||
const int64_t i = t / nw, j = t % nw;
|
||||
const int64_t tw = std::min(tile, W / ratio - sx[(size_t)j]) * ratio;
|
||||
const int64_t k = r - off[(size_t)t];
|
||||
const int64_t src = (sy[(size_t)i] * ratio + k / tw) * W + sx[(size_t)j] * ratio + k % tw;
|
||||
const int64_t xb = rowoff(x, src, 3), yb = rowoff(y, r, 3), xs = laststride(x);
|
||||
const int64_t ys = laststride(y);
|
||||
for (int64_t c = 0; c < 3; ++c) stt(y, yb + c * ys, ldt(x, xb + c * xs));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_join */
|
||||
/* The per-tile moments blended back into one latent plane, vae.py's `tiled_encode` blend: x
|
||||
* holds the tiles' n-channel moments tile-major (vtile_crop's row layout at latent extent),
|
||||
* geom = (clip, lat_h, lat_w) in latent units, and y f32-or-bf16 [M, n] is the canvas, M
|
||||
* lat_h lat_w. Canvas position (Y, X) belongs to the tile (i, j) whose kept range holds it,
|
||||
* [start, next start) (the last band to the end), at (ly, lx) within it; T(i, j, ly, lx) is
|
||||
* that tile's stored value. As in vae.py the neighbours are RAW (as the per-tile encoder left
|
||||
* them, never already blended) and the top blend runs before the left one; every product and
|
||||
* sum rounds to x's dtype (`rd`), as its bf16 blends round, with wa(p, e) = rd(1 - rd(p / e))
|
||||
* and wb(p, e) = rd(p / e):
|
||||
*
|
||||
* v = T(i, j, ly, lx)
|
||||
* v = rd(rd(T(i - 1, j, ly + tile - ev) * wa(ly, ev)) + rd(v * wb(ly, ev))) ev = ov_y[i - 1]
|
||||
* v = rd(rd(T(i, j - 1, lx + tile - eh) * wa(lx, eh)) + rd(v * wb(lx, eh))) eh = ov_x[j - 1]
|
||||
*
|
||||
* the first when i > 0 and ly < ev, the second when j > 0 and lx < eh. Only a lone tile is
|
||||
* short, and it has no neighbour, so a neighbour's extent along the blended axis is always
|
||||
* `tile`. */
|
||||
int ref_vtile_join(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *x = &a->t[0], *geom = &a->t[1], *y = &a->t[2];
|
||||
const int64_t M = p_int(a, "M", 0), tile = p_int(a, "tile", 0);
|
||||
const int64_t overlap = p_int(a, "overlap", 0), n = p_int(a, "n", 0);
|
||||
if (M <= 0 || tile <= 0 || overlap < 0 || overlap >= tile || n <= 0 || numel(geom) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = ldi(geom, 1), W = ldi(geom, 2);
|
||||
std::vector<int64_t> sy, sx, oy, ox;
|
||||
if (H <= 0 || W <= 0 || !split_tiles_axis(H, tile, overlap, &sy, &oy) ||
|
||||
!split_tiles_axis(W, tile, overlap, &sx, &ox))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
if (M != H * W) return RAD_E_SHAPE;
|
||||
std::vector<int64_t> off((size_t)(nh * nw + 1), 0);
|
||||
for (int64_t i = 0; i < nh; ++i) {
|
||||
const int64_t th = std::min(tile, H - sy[(size_t)i]);
|
||||
for (int64_t j = 0; j < nw; ++j)
|
||||
off[(size_t)(i * nw + j + 1)] =
|
||||
off[(size_t)(i * nw + j)] + th * std::min(tile, W - sx[(size_t)j]);
|
||||
}
|
||||
if (rows_of(x, n) < off.back() || rows_of(y, n) < M) return RAD_E_SHAPE;
|
||||
/* The owner tile of every canvas row and column. */
|
||||
std::vector<int64_t> own_y((size_t)H), own_x((size_t)W);
|
||||
for (int64_t Y = 0, i = 0; Y < H; ++Y) {
|
||||
while (i + 1 < nh && Y >= sy[(size_t)(i + 1)]) ++i;
|
||||
own_y[(size_t)Y] = i;
|
||||
}
|
||||
for (int64_t X = 0, j = 0; X < W; ++X) {
|
||||
while (j + 1 < nw && X >= sx[(size_t)(j + 1)]) ++j;
|
||||
own_x[(size_t)X] = j;
|
||||
}
|
||||
const uint32_t dt = x->dtype;
|
||||
auto rd = [dt](float v) { return round_dt(dt, v); };
|
||||
auto wb = [&](int64_t p, int64_t e) { return rd((float)p / (float)e); };
|
||||
auto wa = [&](int64_t p, int64_t e) { return rd(1.0f - rd((float)p / (float)e)); };
|
||||
auto T = [&](int64_t i, int64_t j, int64_t ly, int64_t lx, int64_t c) {
|
||||
const int64_t row = off[(size_t)(i * nw + j)] + ly * std::min(tile, W - sx[(size_t)j]) + lx;
|
||||
return ldt(x, rowoff(x, row, n) + c * laststride(x));
|
||||
};
|
||||
const int64_t ys = laststride(y);
|
||||
REF_PAR_FOR(M * n)
|
||||
for (int64_t r = 0; r < M; ++r) {
|
||||
const int64_t Y = r / W, X = r % W;
|
||||
const int64_t i = own_y[(size_t)Y], j = own_x[(size_t)X];
|
||||
const int64_t ly = Y - sy[(size_t)i], lx = X - sx[(size_t)j];
|
||||
const int64_t ev = i > 0 ? oy[(size_t)(i - 1)] : 0, eh = j > 0 ? ox[(size_t)(j - 1)] : 0;
|
||||
const int64_t yb = rowoff(y, r, n);
|
||||
for (int64_t c = 0; c < n; ++c) {
|
||||
float v = T(i, j, ly, lx, c);
|
||||
if (ly < ev)
|
||||
v = rd(rd(T(i - 1, j, ly + tile - ev, lx, c) * wa(ly, ev)) +
|
||||
rd(v * wb(ly, ev)));
|
||||
if (lx < eh)
|
||||
v = rd(rd(T(i, j - 1, ly, lx + tile - eh, c) * wa(lx, eh)) +
|
||||
rd(v * wb(lx, eh)));
|
||||
stt(y, yb + c * ys, v);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
Reference in new issue
Block a user