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
@@ -897,6 +897,22 @@ std::vector<Case> build_cases() {
|
||||
add("grid_pos3", { HI("M", 40), HI("T", 2), HI("H", 3), HI("W", 4), HI("period", 27),
|
||||
HF("scale", 6.283185307179586) });
|
||||
|
||||
/* The tiled encoder over the shape functions' 20 x 20 latent plane: the encoder's tile 16
|
||||
* with its 4 overlap (two tiles an axis), and a 3-overlap-1 split (ten tiles an axis, one of
|
||||
* slack round-robin) that puts a live blend on every interior row and column. crop is drawn
|
||||
* at ratio 1 -- the plane is 20 x 20 pixels, its tiles' sum 30 x 30 either way; join reads
|
||||
* the moments' 30 x 30 rows into the 400-row canvas. */
|
||||
add("vtile_plan", { HI("M", 4), HI("tile", 16), HI("overlap", 4), HI("ratio", 16) });
|
||||
add("vtile_plan", { HI("M", 100), HI("tile", 3), HI("overlap", 1), HI("ratio", 1) });
|
||||
add("vtile_crop", { HI("M", 1024), HI("tile", 16), HI("overlap", 4), HI("ratio", 1),
|
||||
HS("dtype", "f32") });
|
||||
add("vtile_crop", { HI("M", 900), HI("tile", 3), HI("overlap", 1), HI("ratio", 1),
|
||||
HS("dtype", "bf16") });
|
||||
add("vtile_join", { HI("M", 400), HI("tile", 16), HI("overlap", 4), HI("n", 48),
|
||||
HS("dtype", "bf16") });
|
||||
add("vtile_join", { HI("M", 400), HI("tile", 3), HI("overlap", 1), HI("n", 5),
|
||||
HS("dtype", "f32") });
|
||||
|
||||
/* The latent upscaler over libref's cube volumes (c x c x c, the largest M holds; every geom
|
||||
* entry c, so the resize is the identity here): the 3-D convolution at odd channel counts,
|
||||
* kernels 3 x 3 x 3 and 3 x 1 x 1, with the residual (a separate buffer), and at MiniMax-H3's
|
||||
|
||||
@@ -128,6 +128,7 @@ int avx_par_force(int threads);
|
||||
X(vtile_gather) X(vtile_stitch) X(grid_pos3) X(vsa_select) \
|
||||
X(attn_vsa) X(conv3d_ndhwc) X(group_norm) X(tconv_dw) \
|
||||
X(resize_bilinear) X(patch_fold) X(flow_renoise) X(cond_aug) \
|
||||
X(vtile_plan) X(vtile_crop) X(vtile_join) \
|
||||
/* ---- LTX-2 */ \
|
||||
X(rope_split) X(head_gate) X(ltx_guide) X(pixel_norm) \
|
||||
X(d2s3d) X(s2d3d) X(pix_patch) \
|
||||
|
||||
@@ -634,6 +634,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 (avx_video.cpp), as libref declares it: `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") };
|
||||
static const RadParamSpec pGridPos3[] = { P_INT("M"), P_INT("T"), P_INT("H"), P_INT("W"),
|
||||
P_INT("period"), P_F64("scale") };
|
||||
static const RadOperandSpec oGridPos3[] = { OUT("y") };
|
||||
@@ -1183,6 +1194,20 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "vtile_stitch", ARR(pVtileStitch), ARR(oVtileStitch),
|
||||
"decoded tiles blended into frames, the chunk overlap blended and kept, RGB bytes out; bit for "
|
||||
"bit libref's." },
|
||||
{ "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 its tiles, [3, >= M] at the operand's row pitch, bit "
|
||||
"for bit libref's." },
|
||||
@@ -1764,6 +1789,12 @@ SIMPLE("avx_vtile_gather", "vtile_gather", "elem", "a video decoder's tiles out
|
||||
SIMPLE("avx_vtile_stitch", "vtile_stitch", "elem",
|
||||
"decoded tiles blended into frames, the chunk overlap, bytes out", DT_FLOAT,
|
||||
L(vtile_stitch)),
|
||||
SIMPLE("avx_vtile_plan", "vtile_plan", "elem", "the tiled encoder's per-tile geometry table",
|
||||
DT_ANY, L(vtile_plan)),
|
||||
SIMPLE("avx_vtile_crop", "vtile_crop", "elem", "a frame's pixel plane cut into encode tiles",
|
||||
DT_FLOAT, L(vtile_crop)),
|
||||
SIMPLE("avx_vtile_join", "vtile_join", "elem",
|
||||
"per-tile encoder moments blended into the latent canvas", DT_FLOAT, L(vtile_join)),
|
||||
SIMPLE("avx_grid_pos3", "grid_pos3", "elem", "a video decoder's rotary positions over its tiles",
|
||||
DT_FLOAT, L(grid_pos3)),
|
||||
SIMPLE("avx_vsa_select", "vsa_select", "attn",
|
||||
|
||||
+165
-1
@@ -1,7 +1,9 @@
|
||||
/* avx_video.cpp -- the video generator's ops around its decoder (docs/OPS.md, "Video
|
||||
* generation"): the latent's patch rows unfolded to voxels, the tiles a video decoder runs over,
|
||||
* gathered from the latent and stitched back into frames, and the rotary positions of a tile's
|
||||
* tokens; and its latent upscaler's (docs/OPS.md, "The latent upscaler"): the 3-D convolution,
|
||||
* tokens; the encoder's plan, cut and blend for a picture too big for one volume (docs/OPS.md,
|
||||
* "Video generation"); and its latent upscaler's (docs/OPS.md, "The latent upscaler"): the 3-D
|
||||
* convolution,
|
||||
* the group norm, the temporal depthwise convolution, the per-frame resize, the fold back into
|
||||
* patch rows and the re-noising.
|
||||
*
|
||||
@@ -123,6 +125,19 @@ bool split_tiles(int64_t len, int64_t tile, int64_t overlap, std::vector<int64_t
|
||||
return true;
|
||||
}
|
||||
|
||||
/* libref's split_tiles_axis: the encoder's split over one axis, vae.py's `split_tiles` -- whose
|
||||
* single-tile branch answers ([0], [len]) 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);
|
||||
}
|
||||
|
||||
/* A row copied between operands, converted through f32 as libref's element loop is. Whole-byte
|
||||
* float rows of one dtype are copied as bytes, which is the same thing for every non-NaN value. */
|
||||
inline void copy_row(const RadTensor* src, int64_t sb, const RadTensor* dst, int64_t db, int64_t n,
|
||||
@@ -440,6 +455,155 @@ AVX_KERNEL(vtile_stitch) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ================================================================== the tiled encoder
|
||||
* A one-frame media bigger than the encoder's per-volume side is encoded the way vae.py's
|
||||
* `tiled_encode` encodes it (libref's section comment): the plan, the cut, and the blend; the
|
||||
* encoder itself is the existing per-level ops, issued per tile at the plan's geometries. */
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_plan */
|
||||
/* The plan one frame's encode runs over (libref): geom = (clip, H, W) the frame's extent in
|
||||
* pixels, split by split_tiles_axis over the spatial axes in latent units; row t of y (i32
|
||||
* [M, 22]) the tile's first pixel, its pixel extent, and the six level volumes. */
|
||||
AVX_KERNEL(vtile_plan) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *G = &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(G) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = at(G, 1), W = at(G, 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;
|
||||
const int64_t* psy = sy.data(), *psx = sx.data();
|
||||
AVX_PARALLEL_FOR_IF(M >= AVX_PAR_MIN)
|
||||
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 - psy[(size_t)i]) * ratio;
|
||||
const int64_t tw = std::min(tile, W / ratio - psx[(size_t)j]) * ratio;
|
||||
const int64_t yb = rowoff(Y, t, 22), ys = laststride(Y);
|
||||
const int64_t v[22] = { psy[(size_t)i] * ratio, psx[(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_dt(Y->dtype, Y->data, yb + c * ys, v[c]);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_crop */
|
||||
/* One frame's pixel plane cut into the plan's tiles (libref): y f32 [M, 3] holds the tiles'
|
||||
* pixels tile-major, M the tiles' pixel count. Only the row the tile stands on is read. */
|
||||
AVX_KERNEL(vtile_crop) {
|
||||
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *G = &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(G) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = at(G, 1), W = at(G, 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;
|
||||
const int64_t* psy = sy.data(), *psx = sx.data(), *poff = off.data();
|
||||
AVX_PARALLEL_FOR_IF(M * 3 >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < M; ++r) {
|
||||
const int64_t t =
|
||||
(int64_t)(std::upper_bound(poff, poff + nh * nw + 1, r) - poff) - 1;
|
||||
const int64_t i = t / nw, j = t % nw;
|
||||
const int64_t tw = std::min(tile, W / ratio - psx[(size_t)j]) * ratio;
|
||||
const int64_t k = r - poff[t];
|
||||
const int64_t src = (psy[(size_t)i] * ratio + k / tw) * W + psx[(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 (libref, vae.py's `tiled_encode`
|
||||
* blend): RAW neighbours, the top blend before the left one, every product and sum a pass
|
||||
* through x's dtype. */
|
||||
AVX_KERNEL(vtile_join) {
|
||||
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *G = &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(G) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = at(G, 1), W = at(G, 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 Yp = 0, i = 0; Yp < H; ++Yp) {
|
||||
while (i + 1 < nh && Yp >= sy[(size_t)(i + 1)]) ++i;
|
||||
own_y[(size_t)Yp] = i;
|
||||
}
|
||||
for (int64_t X0 = 0, j = 0; X0 < W; ++X0) {
|
||||
while (j + 1 < nw && X0 >= sx[(size_t)(j + 1)]) ++j;
|
||||
own_x[(size_t)X0] = j;
|
||||
}
|
||||
const uint32_t dt = X->dtype;
|
||||
auto rd = [dt](float v) { return narrow1(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)); };
|
||||
const int64_t* psy = sy.data(), *psx = sx.data(), *poff = off.data();
|
||||
const int64_t* pown_y = own_y.data(), *pown_x = own_x.data();
|
||||
const int64_t xs = laststride(X), ys = laststride(Y);
|
||||
auto T = [&](int64_t i, int64_t j, int64_t ly, int64_t lx, int64_t c) {
|
||||
const int64_t row = poff[i * nw + j] + ly * std::min(tile, W - psx[(size_t)j]) + lx;
|
||||
return ldt(X, rowoff(X, row, n) + c * xs);
|
||||
};
|
||||
AVX_PARALLEL_FOR_IF(M * n >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < M; ++r) {
|
||||
const int64_t Yp = r / W, X0 = r % W;
|
||||
const int64_t i = pown_y[Yp], j = pown_x[X0];
|
||||
const int64_t ly = Yp - psy[(size_t)i], lx = X0 - psx[(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;
|
||||
}
|
||||
|
||||
/* ================================================================== the latent upscaler
|
||||
* Voxel rows (t, i, j) of a T x H x W volume, channels innermost, the volume in `geom`. */
|
||||
|
||||
|
||||
Reference in new issue
Block a user