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
@@ -260,6 +260,9 @@ int r4d_rad_flow_res_multistep (const RadArgs*, RadStream);
|
||||
int r4d_rad_patch_unfold (const RadArgs*, RadStream);
|
||||
int r4d_rad_vtile_gather (const RadArgs*, RadStream);
|
||||
int r4d_rad_vtile_stitch (const RadArgs*, RadStream);
|
||||
int r4d_rad_vtile_plan (const RadArgs*, RadStream);
|
||||
int r4d_rad_vtile_crop (const RadArgs*, RadStream);
|
||||
int r4d_rad_vtile_join (const RadArgs*, RadStream);
|
||||
int r4d_rad_grid_pos3 (const RadArgs*, RadStream);
|
||||
int r4d_rad_snake_aa (const RadArgs*, RadStream);
|
||||
/* r4d_upscaler.hip: a video latent upscaler's 3D convolution, group norm, temporal depthwise
|
||||
|
||||
@@ -2660,6 +2660,23 @@ static const RadOperandSpec oVtileStitch[] = {
|
||||
{ "x", RAD_OPD_IN, 0 }, { "geom", RAD_OPD_IN, 0 }, { "prev", RAD_OPD_INOUT, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* The tiled encoder (r4d_video.hip): 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[] = {
|
||||
{ "geom", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pVtileCrop[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
|
||||
P_INT("ratio"), P_STR("dtype") };
|
||||
static const RadOperandSpec oVtileCrop[] = {
|
||||
{ "x", RAD_OPD_IN, 0 }, { "geom", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pVtileJoin[] = { P_INT("M"), P_INT("tile"), P_INT("overlap"),
|
||||
P_INT("n"), P_STR("dtype") };
|
||||
static const RadOperandSpec oVtileJoin[] = {
|
||||
{ "x", RAD_OPD_IN, 0 }, { "geom", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
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[] = { { "y", RAD_OPD_OUT, 0 } };
|
||||
@@ -3600,6 +3617,19 @@ static const RadOpSchema kSchemas[] = {
|
||||
SCHEMA("vtile_stitch", pVtileStitch, oVtileStitch,
|
||||
"a decoded chunk's tiles stitched into RGB bytes, blended with the previous chunk's "
|
||||
"tail."),
|
||||
SCHEMA("vtile_plan", pVtilePlan, oVtilePlan,
|
||||
"the tiled encoder's plan for one frame of geom = (clip, H, W) pixels: the encoder's "
|
||||
"tile split over the spatial axes in latent units, tiles row-major; row t of the "
|
||||
"i32 [M, 22] y is the tile's first pixel, its pixel extent and the six level "
|
||||
"volumes (1, th / 2^min(L, 4), tw / 2^min(L, 4)); M is the tile count."),
|
||||
SCHEMA("vtile_crop", pVtileCrop, 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."),
|
||||
SCHEMA("vtile_join", pVtileJoin, oVtileJoin,
|
||||
"the tiled encoder's per-tile moments blended into the canvas y [M, n] of "
|
||||
"geom = (clip, lat_h, lat_w): each position blended with the tile above and then "
|
||||
"the tile to the left, both RAW, the products and sums rounding to x's dtype."),
|
||||
SCHEMA("grid_pos3", pGridPos3, oGridPos3,
|
||||
"a video decoder's rotary positions over tiles of `period` rows, f32 [3, M]."),
|
||||
SCHEMA("conv3d_ndhwc", pConv3d, oConv3d,
|
||||
@@ -9313,6 +9343,27 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
nullptr, 0, NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_vtile_stitch },
|
||||
|
||||
{ "vtile_plan", "vtile_plan", "elem",
|
||||
"the tiled encoder's per-tile geometry table: a thread a (tile, entry)",
|
||||
"any M; the split reads geom on the device", "i32 table, i32 geom",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
nullptr, 0, NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_vtile_plan },
|
||||
|
||||
{ "vtile_crop", "vtile_crop", "elem",
|
||||
"a frame's pixel plane cut into the encode tiles: a thread a tile pixel row",
|
||||
"row pitches honoured", "f32 or bf16 plane and tiles",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
nullptr, 0, NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_vtile_crop },
|
||||
|
||||
{ "vtile_join", "vtile_join", "elem",
|
||||
"the encode tiles' moments blended into the latent canvas: a thread a canvas row",
|
||||
"row pitches honoured", "f32 or bf16 moments and canvas",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
nullptr, 0, NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_vtile_join },
|
||||
|
||||
{ "grid_pos3", "grid_pos3", "elem",
|
||||
"a video decoder's rotary positions over its tiles", "a [3, M] view at its own pitch", "f32",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
|
||||
@@ -7493,6 +7493,234 @@ void vtile_stitch_cases(const Plugin& pl) {
|
||||
}
|
||||
}
|
||||
|
||||
/* libref's split_tiles_axis: the ENCODER's split, vae.py's `split_tiles` -- like `split` above,
|
||||
* but a single axis no longer than the tile is one short tile rather than a refusal. */
|
||||
bool split_axis(int64_t len, int64_t tile, int64_t overlap, std::vector<int64_t>* st,
|
||||
std::vector<int64_t>* ov) {
|
||||
if (len > 0 && tile >= len) {
|
||||
st->clear();
|
||||
ov->clear();
|
||||
st->push_back(0);
|
||||
return overlap >= 0 && overlap < tile;
|
||||
}
|
||||
return split(len, tile, overlap, st, ov);
|
||||
}
|
||||
|
||||
/* The tiles' tile-major row offsets and per-band extents over a latent plane (libref's
|
||||
* ref_vtile_join prefix sum; ref_vtile_crop's is the same with each extent ratio-squared). */
|
||||
void tile_offsets(const std::vector<int64_t>& sy, const std::vector<int64_t>& sx, int64_t H,
|
||||
int64_t W, int64_t tile, std::vector<int64_t>* off) {
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
off->assign((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]);
|
||||
}
|
||||
}
|
||||
|
||||
/* vtile_plan: real H3 canvases and their encoder tiling -- 640 x 320 (2 x 2 ... 6 full tiles),
|
||||
* a 128 x 96 lone short tile, 96 x 768 (one short row band, four columns whose overlaps 6, 5, 5
|
||||
* come out of the round-robin), and 320 x 560 (rows at 0, 4 with overlap 12, columns at 0, 9, 19
|
||||
* with overlaps 7 and 6). */
|
||||
void vtile_plan_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "vtile_plan");
|
||||
if (!r) { ok("vtile_plan", false, "row absent"); return; }
|
||||
const int64_t gs[][2] = { { 640, 320 }, { 128, 96 }, { 96, 768 }, { 320, 560 } };
|
||||
for (const auto& g : gs) {
|
||||
const int64_t H = g[0], W = g[1];
|
||||
std::vector<int64_t> sy, sx, oy, ox;
|
||||
split_axis(H / 16, 16, 4, &sy, &oy);
|
||||
split_axis(W / 16, 16, 4, &sx, &ox);
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size(), M = nh * nw;
|
||||
void* dy = dalloc((size_t)(M * 22) * 4);
|
||||
const std::string nm = fmt("vtile_plan %lld x %lld in %lld x %lld tiles", (long long)H,
|
||||
(long long)W, (long long)nh, (long long)nw);
|
||||
const int rc = launch(r, { TI32({ 3 }, { 1, (int32_t)H, (int32_t)W }),
|
||||
T(dy, RAD_I32, { M, 22 }) },
|
||||
{ PI("M", M), PI("tile", 16), PI("overlap", 4), PI("ratio", 16) });
|
||||
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
|
||||
std::vector<int32_t> want((size_t)(M * 22));
|
||||
for (int64_t i = 0; i < nh; ++i)
|
||||
for (int64_t j = 0; j < nw; ++j) {
|
||||
const int64_t th = std::min((int64_t)16, H / 16 - sy[(size_t)i]) * 16;
|
||||
const int64_t tw = std::min((int64_t)16, W / 16 - sx[(size_t)j]) * 16;
|
||||
int32_t* w = want.data() + (size_t)(i * nw + j) * 22;
|
||||
w[0] = (int32_t)(sy[(size_t)i] * 16);
|
||||
w[1] = (int32_t)(sx[(size_t)j] * 16);
|
||||
w[2] = (int32_t)th;
|
||||
w[3] = (int32_t)tw;
|
||||
for (int L = 0; L < 6; ++L) {
|
||||
const int64_t d = (int64_t)1 << std::min(L, 4);
|
||||
w[4 + 3 * L] = 1;
|
||||
w[5 + 3 * L] = (int32_t)(th / d);
|
||||
w[6 + 3 * L] = (int32_t)(tw / d);
|
||||
}
|
||||
}
|
||||
const int64_t bad = differ(down_i32(dy, want.size()), want);
|
||||
ok((nm + " exact").c_str(), bad == 0, fmt("%lld ints differ", (long long)bad));
|
||||
}
|
||||
}
|
||||
|
||||
/* vtile_crop: the plane cut tile-major over ratio-4 test shapes and the encoder's real ratio 16
|
||||
* -- a 96 x 768 short-row plane (the overlap column cut twice) and 320 x 560 -- in f32 and bf16.
|
||||
* The crop is a copy, so both dtypes compare bit for bit. */
|
||||
void vtile_crop_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "vtile_crop");
|
||||
if (!r) { ok("vtile_crop", false, "row absent"); return; }
|
||||
const int64_t gs[][4] = { { 20, 20, 3, 1 }, { 8, 20, 3, 1 }, { 96, 768, 16, 4 },
|
||||
{ 320, 560, 16, 4 } };
|
||||
for (const auto& g : gs) {
|
||||
const int64_t H = g[0], W = g[1], tile = g[2], ov = g[3];
|
||||
const int64_t ratio = tile > 4 ? 16 : 4;
|
||||
std::vector<int64_t> sy, sx, oy, ox, off;
|
||||
split_axis(H / ratio, tile, ov, &sy, &oy);
|
||||
split_axis(W / ratio, tile, ov, &sx, &ox);
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
off.assign((size_t)(nh * nw + 1), 0); /* pixel counts: tile t covers th tw pixels */
|
||||
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;
|
||||
}
|
||||
const int64_t M = off.back();
|
||||
for (const bool bf : { false, true }) {
|
||||
std::vector<float> x = randv((size_t)(H * W * 3), 2.0f);
|
||||
void* dx = bf ? up_bf16(x) : up_f32(x);
|
||||
const uint32_t dt = bf ? RAD_BF16 : RAD_F32;
|
||||
void* dy = dalloc((size_t)(M * 3) * (bf ? 2 : 4));
|
||||
const std::string nm = fmt("vtile_crop %lld x %lld %s tile %lld", (long long)H,
|
||||
(long long)W, bf ? "bf16" : "f32", (long long)tile);
|
||||
const int rc = launch(r, { T(dx, dt, { H * W, 3 }), TI32({ 3 }, { 1, (int32_t)H, (int32_t)W }),
|
||||
T(dy, dt, { M, 3 }) },
|
||||
{ PI("M", M), PI("tile", tile), PI("overlap", ov),
|
||||
PI("ratio", ratio), PS("dtype", bf ? "bf16" : "f32") });
|
||||
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
|
||||
std::vector<float> want((size_t)(M * 3));
|
||||
for (int64_t rr = 0; rr < M; ++rr) {
|
||||
int64_t t = 0;
|
||||
while (t + 1 < (int64_t)off.size() && rr >= off[(size_t)(t + 1)]) ++t;
|
||||
const int64_t k = rr - off[(size_t)t], i = t / nw, j = t % nw;
|
||||
const int64_t tw = std::min(tile, W / ratio - sx[(size_t)j]) * ratio;
|
||||
const int64_t src = (sy[(size_t)i] * ratio + k / tw) * W + sx[(size_t)j] * ratio + k % tw;
|
||||
for (int64_t c = 0; c < 3; ++c)
|
||||
want[(size_t)(rr * 3 + c)] = x[(size_t)(src * 3 + c)];
|
||||
}
|
||||
const std::vector<float> got = bf ? down_bf16(dy, want.size()) : down_f32(dy, want.size());
|
||||
const int64_t bad = differ(got, want);
|
||||
ok((nm + " exact").c_str(), bad == 0, fmt("%lld values differ", (long long)bad));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* vtile_join: the per-tile moments blended back, top then left, against RAW neighbours. bf16 over
|
||||
* random values (the tile walks, the uneven 6 x 9 and 20 x 35 splits, a short lone row and a
|
||||
* lone short tile), and one f32 geometry -- 32 x 32 in 3 x 3 tiles split at 0, 8, 16, where every
|
||||
* weight is an eighth and every blend is exact to the last bit. */
|
||||
void vtile_join_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "vtile_join");
|
||||
if (!r) { ok("vtile_join", false, "row absent"); return; }
|
||||
const int64_t gs[][5] = { { 6, 9, 4, 1, 5 }, { 3, 3, 4, 2, 1 }, { 1, 20, 16, 4, 3 },
|
||||
{ 20, 35, 16, 4, 48 } };
|
||||
for (const auto& g : gs) {
|
||||
const int64_t H = g[0], W = g[1], tile = g[2], ov = g[3], n = g[4];
|
||||
std::vector<int64_t> sy, sx, oy, ox, off;
|
||||
split_axis(H, tile, ov, &sy, &oy);
|
||||
split_axis(W, tile, ov, &sx, &ox);
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
tile_offsets(sy, sx, H, W, tile, &off);
|
||||
const int64_t Sm = off.back(), M = H * W;
|
||||
std::vector<float> x = randv((size_t)(Sm * n), 2.0f);
|
||||
void* dx = up_bf16(x);
|
||||
void* dy = dalloc((size_t)(M * n) * 2);
|
||||
const std::string nm = fmt("vtile_join %lld x %lld bf16 tile %lld", (long long)H,
|
||||
(long long)W, (long long)tile);
|
||||
const int rc = launch(r, { T(dx, RAD_BF16, { Sm, n }), TI32({ 3 }, { 1, (int32_t)H, (int32_t)W }),
|
||||
T(dy, RAD_BF16, { M, n }) },
|
||||
{ PI("M", M), PI("tile", tile), PI("overlap", ov), PI("n", n),
|
||||
PS("dtype", "bf16") });
|
||||
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
|
||||
/* The blends round to bf16 at every step, as the kernel's rdv does. */
|
||||
auto rd = [](float v) { return bfr(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 Tv = [&](int64_t i, int64_t j, int64_t ly, int64_t lx, int64_t c) {
|
||||
return x[(size_t)((off[(size_t)(i * nw + j)] +
|
||||
ly * std::min(tile, W - sx[(size_t)j]) + lx) * n + c)];
|
||||
};
|
||||
std::vector<float> want((size_t)(M * n));
|
||||
for (int64_t rr = 0; rr < M; ++rr) {
|
||||
const int64_t Y = rr / W, X = rr % W;
|
||||
int64_t i = 0, j = 0;
|
||||
while (i + 1 < nh && Y >= sy[(size_t)(i + 1)]) ++i;
|
||||
while (j + 1 < nw && X >= sx[(size_t)(j + 1)]) ++j;
|
||||
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;
|
||||
const int64_t eh = j > 0 ? ox[(size_t)(j - 1)] : 0;
|
||||
for (int64_t c = 0; c < n; ++c) {
|
||||
float v = Tv(i, j, ly, lx, c);
|
||||
if (ly < ev)
|
||||
v = rd(rd(Tv(i - 1, j, ly + tile - ev, lx, c) * wa(ly, ev)) +
|
||||
rd(v * wb(ly, ev)));
|
||||
if (lx < eh)
|
||||
v = rd(rd(Tv(i, j - 1, ly, lx + tile - eh, c) * wa(lx, eh)) +
|
||||
rd(v * wb(lx, eh)));
|
||||
want[(size_t)(rr * n + c)] = v;
|
||||
}
|
||||
}
|
||||
const int64_t bad = differ(down_bf16(dy, want.size()), want);
|
||||
ok((nm + " exact").c_str(), bad == 0, fmt("%lld values differ", (long long)bad));
|
||||
}
|
||||
/* 32 x 32 in 3 x 3 tiles split at 0, 8, 16 (the slack of 8 round-robin makes both overlaps
|
||||
* 8): every weight is an eighth and every blend of these integer values is exact to the
|
||||
* last bit, so f32 device and host must agree bit for bit. */
|
||||
{
|
||||
const int64_t H = 32, W = 32, tile = 16, ov = 4, n = 3;
|
||||
std::vector<float> x((size_t)(9 * tile * tile * n));
|
||||
for (size_t i = 0; i < x.size(); ++i) x[i] = (float)(i % 5 + 1);
|
||||
void* dx = up_f32(x);
|
||||
void* dy = dalloc((size_t)(H * W * n) * 4);
|
||||
const int rc = launch(r, { T(dx, RAD_F32, { 9 * tile * tile, n }),
|
||||
TI32({ 3 }, { 1, (int32_t)H, (int32_t)W }),
|
||||
T(dy, RAD_F32, { H * W, n }) },
|
||||
{ PI("M", H * W), PI("tile", tile), PI("overlap", ov), PI("n", n),
|
||||
PS("dtype", "f32") });
|
||||
if (rc != RAD_OK) { ok("vtile_join 32 x 32 exact f32", false, "rc " + std::to_string(rc)); }
|
||||
else {
|
||||
const int64_t st[3] = { 0, 8, 16 };
|
||||
auto Tv = [&](int64_t i, int64_t j, int64_t ly, int64_t lx, int64_t c) {
|
||||
return x[(size_t)(((i * 3 + j) * tile * tile + ly * tile + lx) * n + c)];
|
||||
};
|
||||
std::vector<float> want((size_t)(H * W * n));
|
||||
for (int64_t Y = 0; Y < H; ++Y)
|
||||
for (int64_t X = 0; X < W; ++X) {
|
||||
int64_t i = 0, j = 0;
|
||||
while (i + 1 < 3 && Y >= st[i + 1]) ++i;
|
||||
while (j + 1 < 3 && X >= st[j + 1]) ++j;
|
||||
const int64_t ly = Y - st[i], lx = X - st[j];
|
||||
const int64_t ev = i > 0 ? 8 : 0, eh = j > 0 ? 8 : 0;
|
||||
for (int64_t c = 0; c < n; ++c) {
|
||||
float v = Tv(i, j, ly, lx, c);
|
||||
if (ly < ev) {
|
||||
const float wa = 1.0f - (float)ly / 8.0f, wb = (float)ly / 8.0f;
|
||||
v = (Tv(i - 1, j, ly + 16 - 8, lx, c) * wa) + (v * wb);
|
||||
}
|
||||
if (lx < eh) {
|
||||
const float wa = 1.0f - (float)lx / 8.0f, wb = (float)lx / 8.0f;
|
||||
v = (Tv(i, j - 1, ly, lx + 16 - 8, c) * wa) + (v * wb);
|
||||
}
|
||||
want[(size_t)((Y * W + X) * n + c)] = v;
|
||||
}
|
||||
}
|
||||
const int64_t bad = differ(down_f32(dy, want.size()), want);
|
||||
ok("vtile_join 32 x 32 exact f32", bad == 0, fmt("%lld values differ", (long long)bad));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* rms_mod and rms_mod_quant_fp8 at the DiT's width over three segments, the table and the gate a
|
||||
* column slice of a wider row as the plugin reads them, gated and not. The residual is exact; the
|
||||
@@ -27892,6 +28120,9 @@ static void device_phase(const Plugin& pl) {
|
||||
if (sel("patch_unfold_f32")) vid::patch_unfold_cases(pl);
|
||||
if (sel("vtile_gather")) vid::vtile_gather_cases(pl);
|
||||
if (sel("vtile_stitch")) vid::vtile_stitch_cases(pl);
|
||||
if (sel("vtile_plan")) vid::vtile_plan_cases(pl);
|
||||
if (sel("vtile_crop")) vid::vtile_crop_cases(pl);
|
||||
if (sel("vtile_join")) vid::vtile_join_cases(pl);
|
||||
if (sel("rms_mod_bf16")) vid::rms_mod_cases(pl, false);
|
||||
if (sel("rms_mod_quant_fp8_bf16")) vid::rms_mod_cases(pl, true);
|
||||
if (sel("flow_x0_step_f32")) vid::flow_x0_cases(pl);
|
||||
|
||||
+197
-2
@@ -1,7 +1,8 @@
|
||||
// R4D -- the video generator's ops (docs/OPS.md, "Video generation"): a diffusion transformer's
|
||||
// modulated RMS norm and its sampler step, the video decoder's patch unfold, the tiles it runs
|
||||
// over, their stitch back into frames and their rotary positions; and its soundtrack decoder's
|
||||
// alias-free SnakeBeta (docs/OPS.md, "Audio generation").
|
||||
// over, their stitch back into frames and their rotary positions; the video encoder's plan, cut
|
||||
// and blend for a picture bigger than one volume; and its soundtrack decoder's alias-free
|
||||
// SnakeBeta (docs/OPS.md, "Audio generation").
|
||||
//
|
||||
// Each kernel rounds where its libref function does (libref/ref_gen.cpp, libref/ref_video.cpp,
|
||||
// libref/ref_audio.cpp), under the library's -ffp-contract=off. The norm's sum of squares is a
|
||||
@@ -277,6 +278,21 @@ __device__ __forceinline__ Tiles split_tiles(int len, int tile, int overlap) {
|
||||
return t;
|
||||
}
|
||||
|
||||
// The encoder's split over one axis (libref's split_tiles_axis, vae.py's `split_tiles`): 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 above demands an exact fit. A lone tile reads no overlap and starts at 0, which the closed
|
||||
// form above already gives at n = 1.
|
||||
__device__ __forceinline__ Tiles split_tiles_axis(int len, int tile, int overlap) {
|
||||
if (tile >= len && len > 0 && tile > 0 && overlap >= 0 && overlap < tile) {
|
||||
Tiles t;
|
||||
t.n = 1;
|
||||
t.tile = tile;
|
||||
t.overlap = overlap;
|
||||
return t;
|
||||
}
|
||||
return split_tiles(len, tile, overlap);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- vtile_gather
|
||||
// A workgroup a tile row: its voxel from x, a register token from `reg`, or zeros.
|
||||
template <typename TX, typename TR, typename TY>
|
||||
@@ -383,6 +399,119 @@ __global__ __launch_bounds__(kT) void vtile_stitch_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- 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: the plan, the cut and the blend (libref's ref_vtile_plan/crop/join);
|
||||
// the encoder itself is the existing per-level ops, issued per tile at the plan's geometries.
|
||||
|
||||
// A thread a (tile, entry): the tile's first pixel, its pixel extent and the six level volumes,
|
||||
// as ref_vtile_plan's row of 22 i32.
|
||||
__global__ __launch_bounds__(kT) void vtile_plan_kernel(
|
||||
const int* __restrict__ geom, int* __restrict__ y, int tile, int overlap, int ratio, int y_ld,
|
||||
int64_t M, int64_t y_rows) {
|
||||
const int64_t id = (int64_t)blockIdx.x * kT + threadIdx.x;
|
||||
const int H = geom[1], W = geom[2];
|
||||
if (id >= M * 22 || id >= y_rows * 22 || H <= 0 || W <= 0 || H % ratio || W % ratio) return;
|
||||
const Tiles ty = split_tiles_axis(H / ratio, tile, overlap);
|
||||
const Tiles tx = split_tiles_axis(W / ratio, tile, overlap);
|
||||
if (ty.n == 0 || tx.n == 0) return;
|
||||
const int64_t t = id / 22;
|
||||
const int c = (int)(id % 22);
|
||||
const int nh = ty.n, nw = tx.n;
|
||||
if (t >= (int64_t)nh * nw) return;
|
||||
const int i = (int)(t / nw), j = (int)(t % nw);
|
||||
const int th = min(tile, H / ratio - ty.start(i)) * ratio;
|
||||
const int tw = min(tile, W / ratio - tx.start(j)) * ratio;
|
||||
int v;
|
||||
if (c < 4) {
|
||||
v = c == 0 ? ty.start(i) * ratio : c == 1 ? tx.start(j) * ratio : c == 2 ? th : tw;
|
||||
} else {
|
||||
const int L = (c - 4) / 3, k = (c - 4) % 3, d = 1 << min(L, 4);
|
||||
v = k == 0 ? 1 : k == 1 ? th / d : tw / d;
|
||||
}
|
||||
y[t * y_ld + c] = v;
|
||||
}
|
||||
|
||||
// A thread a tile pixel row: its plane row copied. The tile is found by walking the tiles' sizes,
|
||||
// at most a hundred of them (libref's upper_bound over the same prefix sums).
|
||||
template <typename T>
|
||||
__global__ __launch_bounds__(kT) void vtile_crop_kernel(
|
||||
const T* __restrict__ x, const int* __restrict__ geom, T* __restrict__ y, int tile,
|
||||
int overlap, int ratio, int x_ld, int y_ld, int64_t M, int64_t x_rows, int64_t y_rows) {
|
||||
const int64_t r = (int64_t)blockIdx.x * kT + threadIdx.x;
|
||||
const int H = geom[1], W = geom[2];
|
||||
if (r >= M || r >= y_rows || H <= 0 || W <= 0 || H % ratio || W % ratio) return;
|
||||
const Tiles ty = split_tiles_axis(H / ratio, tile, overlap);
|
||||
const Tiles tx = split_tiles_axis(W / ratio, tile, overlap);
|
||||
if (ty.n == 0 || tx.n == 0 || (int64_t)H * W > x_rows) return;
|
||||
const int nh = ty.n, nw = tx.n;
|
||||
int t = -1;
|
||||
int64_t acc = 0, k = 0;
|
||||
for (int tt = 0; tt < nh * nw; ++tt) {
|
||||
const int64_t sz = (int64_t)min(tile, H / ratio - ty.start(tt / nw)) * ratio *
|
||||
min(tile, W / ratio - tx.start(tt % nw)) * ratio;
|
||||
if (r < acc + sz) { t = tt; k = r - acc; break; }
|
||||
acc += sz;
|
||||
}
|
||||
if (t < 0) return; // r past the tiles' sum: M was not it
|
||||
const int tw = min(tile, W / ratio - tx.start(t % nw)) * ratio;
|
||||
const int64_t src = ((int64_t)ty.start(t / nw) * ratio + k / tw) * W +
|
||||
(int64_t)tx.start(t % nw) * ratio + k % tw;
|
||||
for (int c = 0; c < 3; ++c) stv(y + r * (int64_t)y_ld, c, ldv(x + src * (int64_t)x_ld, c));
|
||||
}
|
||||
|
||||
// A thread a canvas row: its three tiles' moments blended, top first then left, RAW neighbours,
|
||||
// every product and sum rounded to x's dtype -- ref_vtile_join's rule, vae.py's `tiled_encode`
|
||||
// blend. A thread per (row, channel) would recompute the tile walk n times.
|
||||
template <typename T>
|
||||
__global__ __launch_bounds__(kT) void vtile_join_kernel(
|
||||
const T* __restrict__ x, const int* __restrict__ geom, T* __restrict__ y, int tile,
|
||||
int overlap, int n, int x_ld, int y_ld, int64_t M, int64_t x_rows, int64_t y_rows) {
|
||||
const int64_t r = (int64_t)blockIdx.x * kT + threadIdx.x;
|
||||
const int H = geom[1], W = geom[2];
|
||||
if (r >= M || r >= y_rows || r >= (int64_t)H * W || H <= 0 || W <= 0) return;
|
||||
const Tiles ty = split_tiles_axis(H, tile, overlap), tx = split_tiles_axis(W, tile, overlap);
|
||||
if (ty.n == 0 || tx.n == 0) return;
|
||||
const int nh = ty.n, nw = tx.n;
|
||||
// The tiles' row extents and their prefix sums, in tile-major order.
|
||||
const int64_t ext_y = nh == 1 ? H : (int64_t)tile * nh, ext_x = nw == 1 ? W : (int64_t)tile * nw;
|
||||
if (ext_y * ext_x > x_rows) return;
|
||||
auto sz = [&](int ii, int jj) {
|
||||
return (int64_t)min(tile, H - ty.start(ii)) * min(tile, W - tx.start(jj));
|
||||
};
|
||||
auto off = [&](int ii, int jj) {
|
||||
int64_t a = 0;
|
||||
for (int t = 0; t < ii * nw + jj; ++t) a += sz(t / nw, t % nw);
|
||||
return a;
|
||||
};
|
||||
const int Y = (int)(r / W), X = (int)(r % W);
|
||||
int i = 0;
|
||||
while (i + 1 < nh && Y >= ty.start(i + 1)) ++i;
|
||||
int j = 0;
|
||||
while (j + 1 < nw && X >= tx.start(j + 1)) ++j;
|
||||
const int ly = Y - ty.start(i), lx = X - tx.start(j);
|
||||
const int ev = i > 0 ? ty.ov(i - 1) : 0, eh = j > 0 ? tx.ov(j - 1) : 0;
|
||||
const int64_t own = off(i, j), up = i > 0 ? off(i - 1, j) : 0, left = j > 0 ? off(i, j - 1) : 0;
|
||||
// Each tile's own width: own and up share this tile's column (`own_w`, short only in the
|
||||
// last column); the left neighbour sits in column j-1, never the last, so its width is the
|
||||
// full `tile`. libref indexes every tile by its own column's width, and these are those.
|
||||
const int own_w = min(tile, W - tx.start(j));
|
||||
const auto fetch = [&](int64_t base, int w, int ay, int ax, int c) {
|
||||
return ldv(x, (base + (int64_t)ay * w + ax) * x_ld + c);
|
||||
};
|
||||
auto rd = [&](float v) { return rdv(x, v); };
|
||||
auto wb = [&](int p, int e) { return rd((float)p / (float)e); };
|
||||
auto wa = [&](int p, int e) { return rd(1.0f - rd((float)p / (float)e)); };
|
||||
for (int c = 0; c < n; ++c) {
|
||||
float v = fetch(own, own_w, ly, lx, c);
|
||||
if (ly < ev)
|
||||
v = rd(rd(fetch(up, own_w, ly + tile - ev, lx, c) * wa(ly, ev)) + rd(v * wb(ly, ev)));
|
||||
if (lx < eh)
|
||||
v = rd(rd(fetch(left, tile, ly, lx + tile - eh, c) * wa(lx, eh)) + rd(v * wb(lx, eh)));
|
||||
stv(y + r * (int64_t)y_ld, c, v);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- grid_pos3
|
||||
// A thread a row: its token's three centred, scaled coordinates, or zeros past the tile's tokens.
|
||||
__global__ __launch_bounds__(kT) void grid_pos3_kernel(
|
||||
@@ -722,6 +851,72 @@ extern "C" int r4d_rad_vtile_stitch(const RadArgs* a, RadStream s) {
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_vtile_plan(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *geom = r4d_opt(a, 0), *y = r4d_opt(a, 1);
|
||||
if (!geom || !y) return R4D_NO(RAD_E_INVAL);
|
||||
if (!is_i32(geom) || !is_i32(y)) return R4D_NO(RAD_E_DTYPE);
|
||||
const long long M = r4d_geti_or(a, "M", 0), tile = r4d_geti_or(a, "tile", 0);
|
||||
const long long overlap = r4d_geti_or(a, "overlap", 0), ratio = r4d_geti_or(a, "ratio", 0);
|
||||
if (M <= 0 || tile <= 0 || overlap < 0 || overlap >= tile || ratio <= 0 ||
|
||||
r4d_numel(geom) < 3)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
long long ry = 0;
|
||||
const long long yl = r4d_rows_pitch(y, 22, &ry);
|
||||
if (yl < 0 || !r4d_contig(geom) || ry < M) return R4D_NO(RAD_E_STRIDE);
|
||||
const int64_t total = (int64_t)M * 22;
|
||||
hipLaunchKernelGGL(vtile_plan_kernel, dim3(blocks_for(total)), dim3(kT), 0, (hipStream_t)s,
|
||||
(const int*)geom->data, (int*)y->data, (int)tile, (int)overlap, (int)ratio,
|
||||
(int)yl, M, ry);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_vtile_crop(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *x = r4d_opt(a, 0), *geom = r4d_opt(a, 1), *y = r4d_opt(a, 2);
|
||||
if (!x || !geom || !y) return R4D_NO(RAD_E_INVAL);
|
||||
if ((!is_f32(x) && !is_bf16(x)) || x->dtype != y->dtype || !is_i32(geom))
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
const long long M = r4d_geti_or(a, "M", 0), tile = r4d_geti_or(a, "tile", 0);
|
||||
const long long overlap = r4d_geti_or(a, "overlap", 0), ratio = r4d_geti_or(a, "ratio", 0);
|
||||
if (M <= 0 || tile <= 0 || overlap < 0 || overlap >= tile || ratio <= 0 ||
|
||||
r4d_numel(geom) < 3)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
long long rx = 0, ry = 0;
|
||||
const long long xl = r4d_rows_pitch(x, 3, &rx), yl = r4d_rows_pitch(y, 3, &ry);
|
||||
if (xl < 0 || yl < 0 || !r4d_contig(geom)) return R4D_NO(RAD_E_STRIDE);
|
||||
if (ry < M) return R4D_NO(RAD_E_SHAPE);
|
||||
#define R4D_VTC(T) \
|
||||
hipLaunchKernelGGL((vtile_crop_kernel<T>), dim3(blocks_for(M)), dim3(kT), 0, (hipStream_t)s, \
|
||||
(const T*)x->data, (const int*)geom->data, (T*)y->data, (int)tile, \
|
||||
(int)overlap, (int)ratio, (int)xl, (int)yl, M, rx, ry)
|
||||
if (is_f32(x)) R4D_VTC(float);
|
||||
else R4D_VTC(uint16_t);
|
||||
#undef R4D_VTC
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_vtile_join(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *x = r4d_opt(a, 0), *geom = r4d_opt(a, 1), *y = r4d_opt(a, 2);
|
||||
if (!x || !geom || !y) return R4D_NO(RAD_E_INVAL);
|
||||
if ((!is_f32(x) && !is_bf16(x)) || x->dtype != y->dtype || !is_i32(geom))
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
const long long M = r4d_geti_or(a, "M", 0), tile = r4d_geti_or(a, "tile", 0);
|
||||
const long long overlap = r4d_geti_or(a, "overlap", 0), n = r4d_geti_or(a, "n", 0);
|
||||
if (M <= 0 || tile <= 0 || overlap < 0 || overlap >= tile || n <= 0 || r4d_numel(geom) < 3)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
long long rx = 0, ry = 0;
|
||||
const long long xl = r4d_rows_pitch(x, n, &rx), yl = r4d_rows_pitch(y, n, &ry);
|
||||
if (xl < 0 || yl < 0 || !r4d_contig(geom)) return R4D_NO(RAD_E_STRIDE);
|
||||
if (ry < M) return R4D_NO(RAD_E_SHAPE);
|
||||
#define R4D_VTJ(T) \
|
||||
hipLaunchKernelGGL((vtile_join_kernel<T>), dim3(blocks_for(M)), dim3(kT), 0, (hipStream_t)s, \
|
||||
(const T*)x->data, (const int*)geom->data, (T*)y->data, (int)tile, \
|
||||
(int)overlap, (int)n, (int)xl, (int)yl, M, rx, ry)
|
||||
if (is_f32(x)) R4D_VTJ(float);
|
||||
else R4D_VTJ(uint16_t);
|
||||
#undef R4D_VTJ
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_grid_pos3(const RadArgs* a, RadStream s) {
|
||||
const RadTensor* y = r4d_opt(a, 0);
|
||||
if (!y) return R4D_NO(RAD_E_INVAL);
|
||||
|
||||
Reference in new issue
Block a user