radiance 1.8.1

- MiniMax-H3 and FastH3 no longer stop the server on a prompt longer than 16 tokens (FastH3) or
  128 tokens (MiniMax-H3)
- latent upscaling for the MiniMax-H3 containers: with LBH-123-AI's latent upscaler in the
  container, `latent_upscale` runs the first steps at a smaller size and refines the latent at the
  full size; `upscale_refine_steps` sets how many steps refine, and the video object reports both
- rad-convert --in-place extends a container whose weights the plugin has regrouped since
- plugin ABI 22: out-of-tree plugins must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-18 15:41:44 +02:00
1 parent 7a7707a955
commit 816e24bd46
43 files changed
+4458 -81

No files matched your search

+4
View File
@@ -611,6 +611,8 @@ ABI_FIELD(RadVideoGenBatch, n_chunks)
ABI_FIELD(RadVideoGenBatch, dec_rows)
ABI_FIELD(RadVideoGenBatch, n_frames_out)
ABI_FIELD(RadVideoGenBatch, n_samples)
ABI_FIELD(RadVideoGenBatch, geom_from)
ABI_FIELD(RadVideoGenBatch, upscale)
ABI_STRUCT(RadVideoGenDecl)
ABI_FIELD(RadVideoGenDecl, name)
ABI_FIELD(RadVideoGenDecl, tmpl)
@@ -647,6 +649,8 @@ ABI_FIELD(RadVideoGenDecl, frames_out)
ABI_FIELD(RadVideoGenDecl, audio_out)
ABI_FIELD(RadVideoGenDecl, n_rungs)
ABI_FIELD(RadVideoGenDecl, rungs)
ABI_FIELD(RadVideoGenDecl, upscale_max)
ABI_FIELD(RadVideoGenDecl, upscale_refine)
ABI_STRUCT(RadVideoGeom)
ABI_FIELD(RadVideoGeom, n_text)
ABI_FIELD(RadVideoGeom, frames)
+6 -2
View File
@@ -588,7 +588,7 @@
{ "RadTunable", "n_values", 16, 4 },
{ "RadTunable", "deflt", 24, 8 },
{ "RadTunable", "doc", 32, 8 },
{ "RadVideoGenBatch", "", 0, 160 },
{ "RadVideoGenBatch", "", 0, 192 },
{ "RadVideoGenBatch", "lane", 0, 4 },
{ "RadVideoGenBatch", "geom", 4, 24 },
{ "RadVideoGenBatch", "geom_d", 32, 8 },
@@ -608,7 +608,9 @@
{ "RadVideoGenBatch", "dec_rows", 136, 8 },
{ "RadVideoGenBatch", "n_frames_out", 144, 4 },
{ "RadVideoGenBatch", "n_samples", 152, 8 },
{ "RadVideoGenDecl", "", 0, 272 },
{ "RadVideoGenBatch", "geom_from", 160, 24 },
{ "RadVideoGenBatch", "upscale", 184, 8 },
{ "RadVideoGenDecl", "", 0, 288 },
{ "RadVideoGenDecl", "name", 0, 8 },
{ "RadVideoGenDecl", "tmpl", 8, 8 },
{ "RadVideoGenDecl", "max_text", 16, 8 },
@@ -644,6 +646,8 @@
{ "RadVideoGenDecl", "audio_out", 252, 4 },
{ "RadVideoGenDecl", "n_rungs", 256, 4 },
{ "RadVideoGenDecl", "rungs", 264, 8 },
{ "RadVideoGenDecl", "upscale_max", 272, 8 },
{ "RadVideoGenDecl", "upscale_refine", 280, 4 },
{ "RadVideoGeom", "", 0, 24 },
{ "RadVideoGeom", "n_text", 0, 4 },
{ "RadVideoGeom", "frames", 4, 4 },
+3 -3
View File
@@ -8,7 +8,7 @@
*
* Checked: a C translation unit (abi_c_test.c, strict C11) lays every struct out as C++ does; every
* struct's listed fields account for its size, so the list in abi_layout_rows.h is complete; and
* the layout agrees with the table below, taken at RAD_ABI_VERSION 21 and RAD_FORMAT_VER 2. Under
* the layout agrees with the table below, taken at RAD_ABI_VERSION 22 and RAD_FORMAT_VER 2. Under
* those versions a listed field may not move or change size, an ABI struct may only grow by fields
* past its old end, and an on-disk struct may not change at all. The table is a snapshot of the
* headers: nothing else states the layout. When it disagrees the test prints the live table.
@@ -57,8 +57,8 @@ std::vector<Row> cpp_rows() {
};
}
/* The layout at RAD_ABI_VERSION 21 / RAD_FORMAT_VER 2 (see the file header). */
constexpr uint32_t kTableAbi = 21, kTableFormat = 2;
/* The layout at RAD_ABI_VERSION 22 / RAD_FORMAT_VER 2 (see the file header). */
constexpr uint32_t kTableAbi = 22, kTableFormat = 2;
const AbiRow kTable[] = {
#include "abi_layout_table.h"
};
+36
View File
@@ -14,6 +14,7 @@
#include <sys/stat.h>
#include <cstddef>
#include <map>
using namespace rad;
@@ -360,6 +361,41 @@ TEST(in_place_keeps_every_weight_where_it_was_and_can_be_undone) {
CHECK(file == tiny::read_text(w.base));
}
/* A weight the container holds in another movement group than the plugin now declares (a plugin
* that regrouped it after the conversion) is kept in place, its entry taking the declared group:
* the group places no bytes of a dense weight. */
TEST(in_place_keeps_a_weight_the_plugin_has_regrouped) {
World& w = world();
REQUIRE(w.ok);
tiny::TempDir d("convert_ipg");
const std::string ip = d / "ip.rad";
REQUIRE(copy_file(w.base, ip));
RadFile a;
REQUIRE_EQ(a.open(w.base.c_str()), RAD_OK);
REQUIRE(a.entry_count() > 0);
const RadFileEntry& e0 = a.entry(0);
REQUIRE(e0.expert < 0);
const std::string name = a.str(e0.name);
const int32_t layer = e0.layer;
const uint64_t off = e0.offset;
{
std::string file = tiny::read_text(ip);
const size_t at = (size_t)a.header().dir_off + offsetof(RadFileEntry, layer);
const int32_t other = layer + 7;
std::memcpy(&file[at], &other, sizeof other);
REQUIRE(tiny::write_text(ip, file));
}
a.close();
std::string log;
REQUIRE_EQ(convert(w.ckpt, { "--recipe", w.recipe, "--reuse", ip, "--in-place" }, &log), 0);
RadFile b;
REQUIRE_EQ(b.open(ip.c_str()), RAD_OK);
const RadFileEntry* e = b.find(name.c_str());
REQUIRE(e != nullptr);
CHECK_EQ(e->layer, layer);
CHECK_EQ(e->offset, off);
}
/* In place, a weight cannot be replaced: a rule that would write one differently is refused and
* the file is left as it was. Without --in-place, -o may not name the --reuse container, whose
* weights the run reads while it truncates the output. */
+284
View File
@@ -309,6 +309,10 @@ TEST(op_vocabulary) {
/* Its step-distilled form's video sparse attention: the tile selection and the
* block-sparse attention over it. */
"vsa_select", "attn_vsa",
/* Its latent upscaler: the 3-D and temporal convolutions, the group norm, the resize,
* the fold back into patch rows and the re-noising. */
"conv3d_ndhwc", "group_norm", "tconv_dw", "resize_bilinear", "patch_fold",
"flow_renoise",
};
for (const char* op : kOps) {
if (!kern(op)) fprintf(stderr, " missing op '%s'\n", op);
@@ -5441,6 +5445,277 @@ TEST(grid_pos3_places_each_tile_token_and_zeroes_the_rest) {
CHECK_EQ(at3(2, 29), 1.5f);
}
/* ================================================================== the latent upscaler
* docs/OPS.md "The latent upscaler": voxel rows (t, i, j) of a T x H x W volume, channels
* innermost, the volume in `geom`. */
/* conv3d_ndhwc's `same` padding: an all-ones 3 x 3 x 3 kernel over a 3 x 3 x 3 volume of ones
* counts each voxel's in-volume neighbours, 2 on an axis at an edge and 3 inside, plus the bias:
* a corner 8.5, the centre 27.5, a face's middle 18.5, an edge's middle 12.5. Refused: an even
* kernel, and y short of the volume's rows. */
TEST(conv3d_ndhwc_pads_each_axis_with_zeros) {
T x(RAD_F32, {27, 1}); x.f(std::vector<float>(27, 1.0f));
T w(RAD_F32, {1, 1, 3, 3, 3}); w.f(std::vector<float>(27, 1.0f));
T b(RAD_F32, {1}); b.f({0.5f});
T geom(RAD_I32, {3}); geom.n({3, 3, 3});
T y(RAD_F32, {27, 1});
A a; a.t(x).t(w).t(b).t(geom).tnull().t(y).i("M", 27).i("cin", 1).i("cout", 1).i("k", 3)
.i("kt", 3).s("dtype", "f32");
CHECK_OK(run("conv3d_ndhwc", a));
CHECK_EQ(y.at(0), 8.5f); /* (0, 0, 0) */
CHECK_EQ(y.at(13), 27.5f); /* (1, 1, 1) */
CHECK_EQ(y.at(4), 18.5f); /* (0, 1, 1): the first frame's centre */
CHECK_EQ(y.at(10), 18.5f); /* (1, 0, 1): the middle frame's top edge */
CHECK_EQ(y.at(1), 12.5f); /* (0, 0, 1) */
CHECK_EQ(y.at(26), 8.5f); /* (2, 2, 2) */
A e; e.t(x).t(w).t(b).t(geom).tnull().t(y).i("M", 27).i("cin", 1).i("cout", 1).i("k", 2)
.i("kt", 3).s("dtype", "f32");
CHECK_EQ(run("conv3d_ndhwc", e), RAD_E_SHAPE);
T y2(RAD_F32, {26, 1});
A s; s.t(x).t(w).t(b).t(geom).tnull().t(y2).i("M", 26).i("cin", 1).i("cout", 1).i("k", 3)
.i("kt", 3).s("dtype", "f32");
CHECK_EQ(run("conv3d_ndhwc", s), RAD_E_SHAPE);
}
/* The weight is PyTorch's [cout, cin, kt, k, k] and tap (dt, kh, kw) reads voxel
* (t + dt - 1, i + kh - 1, j + kw - 1). Two frames of 1 x 3, two channels holding r + 1 at row r;
* only w[1, 0, 2, 1, 0] = 1 (frame t + 1, column j - 1) and w[1, 1, 0, 1, 2] = 10 (frame t - 1,
* column j + 1) are set, so output channel 1 at (t, 0, j) is x[(t + 1, j - 1), 0] +
* 10 x[(t - 1, j + 1), 1] + b[1] and channel 0 is its bias. */
TEST(conv3d_ndhwc_reads_the_weight_as_pytorch_lays_it_out) {
T x(RAD_F32, {6, 2}); x.f({1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6});
std::vector<float> wv(2 * 2 * 27, 0.0f);
wv[75] = 1.0f; /* ((1 * 2 + 0) * 3 + 2) * 9 + 1 * 3 + 0 */
wv[86] = 10.0f; /* ((1 * 2 + 1) * 3 + 0) * 9 + 1 * 3 + 2 */
T w(RAD_F32, {2, 2, 3, 3, 3}); w.f(wv);
T b(RAD_F32, {2}); b.f({0.5f, -1.0f});
T geom(RAD_I32, {3}); geom.n({2, 1, 3});
T y(RAD_F32, {6, 2});
A a; a.t(x).t(w).t(b).t(geom).tnull().t(y).i("M", 6).i("cin", 2).i("cout", 2).i("k", 3)
.i("kt", 3).s("dtype", "f32");
CHECK_OK(run("conv3d_ndhwc", a));
const float want[6] = { -1, 3, 4, 19, 29, -1 };
for (int r = 0; r < 6; ++r) {
CHECK_EQ(y.at(2 * r), 0.5f);
CHECK_EQ(y.at(2 * r + 1), want[r]);
}
}
/* With `res`, y = bf16(bf16(acc + b) + res): 1 + 0.0087890625 rounds to 1.0078125 first, and
* 1.0078125 + 2 = 3.0078125 is a tie that goes to 3, where the unrounded sum 3.0087890625 would
* give 3.015625. -1 + 0.0087890625 rounds to -0.9921875, plus 0.5 is -0.4921875. The residual may
* be y itself, and adds in place. */
TEST(conv3d_ndhwc_rounds_the_conv_before_the_residual_in_place) {
T x(RAD_BF16, {2, 1}); x.f({1.0f, -1.0f});
T w(RAD_BF16, {1, 1, 1, 1, 1}); w.f({1.0f});
T b(RAD_F32, {1}); b.f({0.0087890625f});
T geom(RAD_I32, {3}); geom.n({1, 1, 2});
T y(RAD_BF16, {2, 1}); y.f({2.0f, 0.5f});
A a; a.t(x).t(w).t(b).t(geom).t(y).t(y).i("M", 2).i("cin", 1).i("cout", 1).i("k", 1)
.i("kt", 1).s("dtype", "bf16");
CHECK_OK(run("conv3d_ndhwc", a));
CHECK_EQ(y.at(0), 3.0f);
CHECK_EQ(y.at(1), -0.4921875f);
T res(RAD_BF16, {2, 1}); res.f({2.0f, 0.5f});
T y2(RAD_BF16, {2, 1});
A c; c.t(x).t(w).t(b).t(geom).t(res).t(y2).i("M", 2).i("cin", 1).i("cout", 1).i("k", 1)
.i("kt", 1).s("dtype", "bf16");
CHECK_OK(run("conv3d_ndhwc", c));
CHECK(y2.mem == y.mem);
/* Without a residual, the one rounding: 1.0078125. */
A d; d.t(x).t(w).t(b).t(geom).tnull().t(y2).i("M", 2).i("cin", 1).i("cout", 1).i("k", 1)
.i("kt", 1).s("dtype", "bf16");
CHECK_OK(run("conv3d_ndhwc", d));
CHECK_EQ(y2.at(0), 1.0078125f);
}
/* group_norm's statistics are over every row of a group's channels. Two rows of four channels in
* two groups: group 0 (channels 0 and 1) holds 1, 1, 3, 3, so mean 2 and var 1 (a per-row mean
* would make row 0 constant); group 1 holds 8, 12, 12, 8, mean 10 and var 4, rstd 0.5 (eps 0).
* Normalised, row 0 is (-1, -1, -1, 1) and row 1 (1, 1, 1, -1); times w (1, 2, 3, 4) plus
* b (0, 0.5, -1, 0). stats gets (2, 1, 10, 0.5) and the rest of it is not written. */
TEST(group_norm_takes_each_group_over_all_rows) {
T x(RAD_F32, {2, 4}); x.f({1, 1, 8, 12, 3, 3, 12, 8});
T w(RAD_F32, {4}); w.f({1, 2, 3, 4});
T b(RAD_F32, {4}); b.f({0, 0.5f, -1, 0});
T st(RAD_F32, {2 * (2 + 3 * 1024)}); st.f(std::vector<float>(2 * (2 + 3 * 1024), -7.0f));
T y(RAD_F32, {2, 4});
A a; a.t(x).t(w).t(b).tnull().t(st).t(y).i("M", 2).i("n", 4).i("groups", 2).d("eps", 0.0)
.s("act", "none").s("dtype", "f32");
CHECK_OK(run("group_norm", a));
const float want[8] = { -1, -1.5f, -4, 4, 1, 2.5f, 2, -4 };
for (int i = 0; i < 8; ++i) CHECK_EQ(y.at(i), want[i]);
CHECK_EQ(st.at(0), 2.0f); CHECK_EQ(st.at(1), 1.0f);
CHECK_EQ(st.at(2), 10.0f); CHECK_EQ(st.at(3), 0.5f);
CHECK_EQ(st.at(4), -7.0f); CHECK_EQ(st.at(2 * (2 + 3 * 1024) - 1), -7.0f);
/* Modulated by one row [scale | shift], then silu, in place: v (1 + scale) + shift is
* (-2, -0.5, -2, 4.25) and (2, 3.5, 1, -3.75), and silu(v) = v / (1 + e^-v). */
T mod(RAD_F32, {8}); mod.f({1, 0, -0.5f, 0, 0, 1, 0, 0.25f});
A m; m.t(x).t(w).t(b).t(mod).t(st).t(x).i("M", 2).i("n", 4).i("groups", 2).d("eps", 0.0)
.s("act", "silu").s("dtype", "f32");
CHECK_OK(run("group_norm", m));
const double v[8] = { -2, -0.5, -2, 4.25, 2, 3.5, 1, -3.75 };
for (int i = 0; i < 8; ++i) CHECK_NEAR(x.at(i), v[i] / (1.0 + std::exp(-v[i])), 1e-5);
/* Refused: a stats short of groups (2 + 3 1024), groups not dividing n, an unknown act. */
T sm(RAD_F32, {2 * (2 + 3 * 1024) - 1});
A r1; r1.t(x).t(w).t(b).tnull().t(sm).t(y).i("M", 2).i("n", 4).i("groups", 2).d("eps", 0.0)
.s("act", "none").s("dtype", "f32");
CHECK_EQ(run("group_norm", r1), RAD_E_SHAPE);
A r2; r2.t(x).t(w).t(b).tnull().t(st).t(y).i("M", 2).i("n", 4).i("groups", 3).d("eps", 0.0)
.s("act", "none").s("dtype", "f32");
CHECK_EQ(run("group_norm", r2), RAD_E_SHAPE);
A r3; r3.t(x).t(w).t(b).tnull().t(st).t(y).i("M", 2).i("n", 4).i("groups", 2).d("eps", 0.0)
.s("act", "gelu").s("dtype", "f32");
CHECK_EQ(run("group_norm", r3), RAD_E_INVAL);
}
/* The variance is taken about the mean, not as E[x^2] - mean^2: 4095 and 4097 have mean 4096 and
* var 1, where an f32 sum of squares (4 4096^2 + 4 = 67108868, 8 apart from its neighbours in
* f32) cancels to 0. */
TEST(group_norm_keeps_a_small_variance_under_a_large_mean) {
T x(RAD_F32, {2, 2}); x.f({4095, 4097, 4097, 4095});
T w(RAD_F32, {2}); w.f({1, 1});
T b(RAD_F32, {2}); b.f({0, 0});
T st(RAD_F32, {2 + 3 * 1024});
T y(RAD_F32, {2, 2});
A a; a.t(x).t(w).t(b).tnull().t(st).t(y).i("M", 2).i("n", 2).i("groups", 1).d("eps", 0.0)
.s("act", "none").s("dtype", "f32");
CHECK_OK(run("group_norm", a));
const float want[4] = { -1, 1, 1, -1 };
for (int i = 0; i < 4; ++i) CHECK_EQ(y.at(i), want[i]);
CHECK_EQ(st.at(0), 4096.0f); CHECK_EQ(st.at(1), 1.0f);
}
/* tconv_dw along the frames: three frames of two rows, channel 0's taps (1, 10, 100) on frames
* t - 1, t, t + 1 (w[c k + j]) over x = 1..6, channel 1's (0, 1, 0) over -(1..6); frames outside
* the volume are zero, and each row reads its own position in the other frames. */
TEST(tconv_dw_convolves_each_position_across_frames) {
T x(RAD_F32, {6, 2}); x.f({1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6});
T w(RAD_F32, {2, 1, 3, 1, 1}); w.f({1, 10, 100, 0, 1, 0});
T b(RAD_F32, {2}); b.f({0.5f, 2});
T geom(RAD_I32, {3}); geom.n({3, 1, 2});
T y(RAD_F32, {6, 2});
A a; a.t(x).t(w).t(b).t(geom).t(y).i("M", 6).i("n", 2).i("k", 3).s("dtype", "f32");
CHECK_OK(run("tconv_dw", a));
const float c0[6] = { 310.5f, 420.5f, 531.5f, 642.5f, 53.5f, 64.5f };
for (int r = 0; r < 6; ++r) {
CHECK_EQ(y.at(2 * r), c0[r]);
CHECK_EQ(y.at(2 * r + 1), (float)(1 - r));
}
}
/* resize_bilinear as torch's align_corners=False at a size: 2 x 2 to 4 x 4 gives the familiar
* [[1, 1.25, 1.75, 2], [1.5, 1.75, 2.25, 2.5], [2.5, 2.75, 3.25, 3.5], [3, 3.25, 3.75, 4]]. The
* first row's source clamps to 0; the last's is 1.25, whose neighbour is clamped to row 1, so it
* is row 1. Channel 1 holds the negatives. */
TEST(resize_bilinear_matches_torch_and_clamps_at_the_border) {
T x(RAD_F32, {4, 2}); x.f({1, -1, 2, -2, 3, -3, 4, -4});
T geom(RAD_I32, {5}); geom.n({1, 2, 2, 4, 4});
T y(RAD_F32, {16, 2});
A a; a.t(x).t(geom).t(y).i("M", 16).i("n", 2).s("dtype", "f32");
CHECK_OK(run("resize_bilinear", a));
const float want[16] = { 1, 1.25f, 1.75f, 2, 1.5f, 1.75f, 2.25f, 2.5f,
2.5f, 2.75f, 3.25f, 3.5f, 3, 3.25f, 3.75f, 4 };
for (int r = 0; r < 16; ++r) {
CHECK_EQ(y.at(2 * r), want[r]);
CHECK_EQ(y.at(2 * r + 1), -want[r]);
}
}
/* Frames resize on their own, and shrinking averages: two frames of 4 x 1 to 2 x 1 read source
* rows 0.5 and 2.5. 3 to 5 at a ratio f32 cannot hold exactly: row 0's source clamps to 0, row
* 4's (2.2) has its neighbour clamped onto row 2, and rows 1 and 3 read 0.4 and 1.6. */
TEST(resize_bilinear_resizes_each_frame_and_shrinks) {
T x(RAD_F32, {8, 1}); x.f({1, 2, 4, 8, 10, 20, 40, 80});
T geom(RAD_I32, {5}); geom.n({2, 4, 1, 2, 1});
T y(RAD_F32, {4, 1});
A a; a.t(x).t(geom).t(y).i("M", 4).i("n", 1).s("dtype", "f32");
CHECK_OK(run("resize_bilinear", a));
CHECK_EQ(y.at(0), 1.5f); CHECK_EQ(y.at(1), 6.0f);
CHECK_EQ(y.at(2), 15.0f); CHECK_EQ(y.at(3), 60.0f);
T x3(RAD_F32, {3, 1}); x3.f({0, 10, 20});
T g3(RAD_I32, {5}); g3.n({1, 3, 1, 5, 1});
T y5(RAD_F32, {5, 1});
A c; c.t(x3).t(g3).t(y5).i("M", 5).i("n", 1).s("dtype", "f32");
CHECK_OK(run("resize_bilinear", c));
CHECK_EQ(y5.at(0), 0.0f);
CHECK_NEAR(y5.at(1), 4.0, 1e-5);
CHECK_NEAR(y5.at(3), 16.0, 1e-5);
CHECK_NEAR(y5.at(4), 20.0, 1e-5);
}
/* patch_fold deals voxel rows (yy, xx) of x [8, 2] (x[r, c] = 10 r + c) into the 1 x 2 x 2 patches
* side by side (geom (1, 1, 2)): patch j's column c 4 + b 2 + d is voxel (b, 2 j + d), times mul
* plus add, f32. Row 2 is past the patches and keeps its value. */
TEST(patch_fold_gathers_voxels_into_patches_and_normalises) {
std::vector<float> xv(16);
for (int r = 0; r < 8; ++r)
for (int c = 0; c < 2; ++c) xv[(size_t)(r * 2 + c)] = (float)(10 * r + c);
T x(RAD_F32, {8, 2}); x.f(xv);
T geom(RAD_I32, {3}); geom.n({1, 1, 2});
T y(RAD_F32, {3, 8}); y.f(std::vector<float>(24, -9.0f));
A a; a.t(x).t(geom).t(y).i("M", 3).i("C", 2).i("pt", 1).i("ph", 2).i("pw", 2)
.s("mul", "1 2").s("add", "0 0.5").s("dtype", "f32");
CHECK_OK(run("patch_fold", a));
const float want[16] = { 0, 10, 40, 50, 2.5f, 22.5f, 82.5f, 102.5f,
20, 30, 60, 70, 42.5f, 62.5f, 122.5f, 142.5f };
for (int i = 0; i < 16; ++i) CHECK_EQ(y.at(i), want[i]);
for (int i = 16; i < 24; ++i) CHECK_EQ(y.at(i), -9.0f);
}
/* patch_fold undoes patch_unfold: two frames of 1 x 2 patches of 2 x 1 x 2 with three channels,
* unfolded through (x 2 + 1, x 4 - 2, x 0.5 + 3) and folded back through their inverses, exact
* in f32 at these integers. */
TEST(patch_fold_inverts_patch_unfold) {
std::vector<float> pv(4 * 12);
for (size_t i = 0; i < pv.size(); ++i) pv[i] = (float)((int)i * 7 % 23 - 11);
T p(RAD_F32, {4, 12}); p.f(pv);
T geom(RAD_I32, {3}); geom.n({2, 1, 2});
T vox(RAD_F32, {16, 3});
A u; u.t(p).t(geom).t(vox).i("M", 16).i("C", 3).i("pt", 2).i("ph", 1).i("pw", 2)
.s("mul", "2 4 0.5").s("add", "1 -2 3").s("dtype", "f32");
CHECK_OK(run("patch_unfold", u));
T back(RAD_F32, {4, 12});
A f; f.t(vox).t(geom).t(back).i("M", 4).i("C", 3).i("pt", 2).i("ph", 1).i("pw", 2)
.s("mul", "0.5 0.25 2").s("add", "-0.5 0.5 -6").s("dtype", "f32");
CHECK_OK(run("patch_fold", f));
for (int i = 0; i < 48; ++i) CHECK_EQ(back.at(i), pv[(size_t)i]);
}
/* flow_renoise: x = f32(f32(0.75 x) + f32(0.25 z(k n + c))) with z the (seed, 1) normal stream,
* indexed by the row's position k n + c, not its pitch: x is [2, 3] at a pitch of 5, whose last
* two columns are not written. Both products are exact here, so the expectation has one
* rounding. Stream 0 (rad_philox_normal) is a different draw. */
TEST(flow_renoise_draws_its_own_stream_by_element) {
const uint64_t seed = 0x0123456789abcdefull;
T sg(RAD_F32, {1}); sg.f({0.25f});
T sd(RAD_U32, {2}); sd.n({(int64_t)(seed & 0xffffffffu), (int64_t)(seed >> 32)});
const float xv[6] = { 2, 4, -8, 6, 0, 10 };
T x(RAD_F32, {2, 5});
x.f({ xv[0], xv[1], xv[2], -9, -9, xv[3], xv[4], xv[5], -9, -9 });
x.t.shape[1] = 3;
A a; a.t(sg).t(sd).t(x).i("M", 2).i("n", 3).i("stream", 1).s("dtype", "f32");
CHECK_OK(run("flow_renoise", a));
bool differs = false;
for (int k = 0; k < 2; ++k) {
for (int c = 0; c < 3; ++c) {
const uint64_t e = (uint64_t)(k * 3 + c);
const float z = rad_philox_normal_s(seed, 1, e);
const float want = 0.75f * xv[k * 3 + c] + 0.25f * z;
CHECK_EQ(x.at(k * 5 + c), want);
differs = differs || want != 0.75f * xv[k * 3 + c] + 0.25f * rad_philox_normal(seed, e);
}
CHECK_EQ(x.at(k * 5 + 3), -9.0f);
CHECK_EQ(x.at(k * 5 + 4), -9.0f);
}
CHECK(differs);
}
/* ================================================================== video sparse attention
* docs/OPS.md "Video sparse attention": the tiles over a packed sequence of dense segments and a
* video grid, vsa_select's top-k of pooled scores and its coarse branch, attn_vsa's block-sparse
@@ -6798,6 +7073,15 @@ std::vector<Geo> geometries() {
G("attn_vsa", "M", 81, a.i("M", R).i("head_dim", 8).i("tile_t", 2).i("tile_h", 2)
.i("tile_w", 2).i("patch", 1).d("sparsity", 0.9)
.s("dtype", "f32")),
G("conv3d_ndhwc", "M", 27, a.i("M", R).i("cin", 3).i("cout", 5).i("k", 3).i("kt", 3)
.s("dtype", "f32")),
G("group_norm", "M", 4, a.i("M", R).i("n", 8).i("groups", 2).d("eps", 1e-5)
.s("act", "silu").s("dtype", "f32")),
G("tconv_dw", "M", 8, a.i("M", R).i("n", 8).i("k", 5).s("dtype", "f32")),
G("resize_bilinear", "M", 8, a.i("M", R).i("n", 8).s("dtype", "f32")),
G("patch_fold", "M", 8, a.i("M", R).i("C", 3).i("pt", 1).i("ph", 2).i("pw", 2)
.s("mul", "1 2 3").s("add", "0 1 2").s("dtype", "f32")),
G("flow_renoise", "M", 3, a.i("M", R).i("n", 8).i("stream", 1).s("dtype", "f32")),
};
}
#undef G
+128
View File
@@ -110,6 +110,7 @@ char kind_of(int gen) {
case RAD_GEN_PASS_VSTEP: return 'S';
case RAD_GEN_PASS_ADECODE: return 'A';
case RAD_GEN_PASS_VDECODE: return 'D';
case RAD_GEN_PASS_VUPSCALE: return 'U';
default: return '?';
}
}
@@ -279,6 +280,133 @@ TEST(a_second_request_is_conditioned_early_and_denoised_after_the_first) {
CHECK_EQ(kinds, std::string("CCSADSAD"));
}
/* Latent upscaling's small size: each side over the factor, rounded half up to the size multiple,
* at least one multiple and at most the side. */
TEST(the_small_size_is_each_side_over_the_factor_at_the_multiple) {
const RadVideoGenDecl d = decl_small();
int32_t w = 0, h = 0;
gen::video_small_size(d, 1344, 768, 2.0, &w, &h);
CHECK_EQ(w, 672);
CHECK_EQ(h, 384);
gen::video_small_size(d, 1344, 768, 4.0, &w, &h);
CHECK_EQ(w, 352); /* 336 is 10.5 multiples, rounded up */
CHECK_EQ(h, 192);
gen::video_small_size(d, 1280, 768, 4.0, &w, &h);
CHECK_EQ(w, 320);
CHECK_EQ(h, 192);
gen::video_small_size(d, 64, 32, 4.0, &w, &h);
CHECK_EQ(w, 32); /* never below one multiple */
CHECK_EQ(h, 32);
gen::video_small_size(d, 64, 64, 1.2, &w, &h);
CHECK_EQ(w, 64); /* 1.67 multiples round back to the side */
CHECK_EQ(h, 64);
}
/* An upscaled request: its first evaluations at the small size, the last of them stepping to the
* clean estimate, then the upscale pass with both geometries, the factor and the first full-size
* step's sigmas, then the full-size steps and the decodes at the full size. */
TEST(an_upscaled_request_runs_small_steps_then_the_upscale_then_full_steps) {
VideoFixture f;
f.build();
f.prog.video_gen.upscale_max = 4.0;
f.prog.video_gen.upscale_refine = 1;
CHECK_OK(f.start());
const gen::VideoSchedule sc = gen::video_schedule(f.prog.video_gen, 4);
REQUIRE_EQ(sc.steps, 3);
std::string why;
auto r = f.req(1, 4);
r->width = 64;
r->height = 64;
r->upscale = 2.0;
auto t = r->ticket;
CHECK_OK(f.drv.submit(std::move(r), &why));
CHECK_EQ(t->small_width, 32);
CHECK_EQ(t->small_height, 32);
CHECK_EQ(t->refine, 1);
CHECK_EQ(t->steps, 3);
/* 3 frames: 2 latent frames, 30 soundtrack latents a channel. Small: 1 x 1 patches a frame;
* full: 2 x 2. */
const int64_t small_rows = 3 + 60 + 2 * 1, full_rows = 3 + 60 + 2 * 4;
std::string kinds;
int steps_seen = 0;
for (int i = 0; i < 20; ++i) {
const RadBatch* b = f.drv.next(i);
if (!b) break;
const RadVideoGenBatch* V = b->vgen_b;
REQUIRE(V != nullptr);
kinds += kind_of(b->gen);
if (b->gen == RAD_GEN_PASS_VSTEP) {
const bool small = steps_seen < 2;
CHECK_EQ(b->n_tok, small ? small_rows : full_rows);
CHECK_EQ(V->geom.lat_h, small ? 2 : 4);
CHECK_EQ(V->geom.lat_w, small ? 2 : 4);
CHECK_EQ(V->geom_d[RAD_VGEOM_LAT_H], small ? 2 : 4);
CHECK_EQ(V->seg_cu[3], (int32_t)(small ? small_rows : full_rows));
CHECK_EQ(V->sched[1], sc.sig_v[(size_t)steps_seen]);
/* The last small step lands on the clean estimate. */
CHECK_EQ(V->sched[2], steps_seen == 1 ? 0.0f : sc.sig_v[(size_t)steps_seen + 1]);
CHECK_EQ(V->sched[5], steps_seen == 1 ? 0.0f : sc.sig_a[(size_t)steps_seen + 1]);
++steps_seen;
} else if (b->gen == RAD_GEN_PASS_VUPSCALE) {
CHECK_EQ(b->n_tok, (int64_t)0);
CHECK_EQ(V->geom.lat_h, 4);
CHECK_EQ(V->geom_from.lat_h, 2);
CHECK_EQ(V->geom_from.lat_w, 2);
CHECK_EQ(V->upscale[0], 2.0f);
CHECK_EQ(V->sched[1], sc.sig_v[2]);
CHECK_EQ(V->sched[4], sc.sig_a[2]);
const int32_t* g = V->geom_d;
CHECK(std::vector<int32_t>(g + RAD_VGEOM_UP_SRC, g + RAD_VGEOM_UP_SRC + 14) ==
std::vector<int32_t>({ 2, 2, 2, 4, 4, 2, 4, 4, 2, 1, 1, 2, 2, 2 }));
} else if (b->gen == RAD_GEN_PASS_VDECODE) {
CHECK_EQ(V->geom.lat_h, 4);
}
f.answer(0);
CHECK_OK(f.drv.done());
}
CHECK_EQ(kinds, std::string("CSSUSAD"));
CHECK(t->t_small_ns > 0 && t->t_upscaled_ns >= t->t_small_ns);
CHECK_EQ(t->status(), (int)RAD_OK);
}
/* Upscaling is refused when the model has none, past its largest factor, when the factor leaves
* the size as it is, and when the refinement leaves no small step or takes none. */
TEST(an_upscale_that_cannot_run_is_refused_at_submit) {
VideoFixture f;
f.build();
CHECK_OK(f.start());
std::string why;
auto r = f.req(1, 4);
r->upscale = 2.0;
CHECK_EQ(f.drv.submit(std::move(r), &why), (int)RAD_E_INVAL);
CHECK(has(why, "no latent upscaler"));
VideoFixture g;
g.build();
g.prog.video_gen.upscale_max = 4.0;
g.prog.video_gen.upscale_refine = 1;
CHECK_OK(g.start());
auto refused = [&](double factor, int32_t refine, const char* what) {
auto q = g.req(1, 4);
q->width = 64;
q->height = 64;
q->upscale = factor;
q->refine = refine;
why.clear();
CHECK_EQ(g.drv.submit(std::move(q), &why), (int)RAD_E_INVAL);
CHECK(has(why, what));
};
refused(5.0, 0, "at most 4");
refused(1.2, 0, "leaves 64x64");
refused(2.0, 3, "1..2");
auto ok = g.req(1, 4);
ok->width = 64;
ok->height = 64;
ok->upscale = 2.0;
ok->refine = 2;
CHECK_OK(g.drv.submit(std::move(ok), &why));
}
/* What can never run is refused at submit: an empty or long prompt, a size off the multiple,
* below the least side, over the area or the aspect, a length outside the chunks, steps outside
* 2..max. */
+46
View File
@@ -59,6 +59,11 @@ struct FakeVideoGen : IVideoGen {
t.sample_rate = 32000;
t.channels = 2;
t.fps = 24.0;
if (r->upscale > 1.0) {
t.small_width = (int32_t)(r->width / r->upscale) / 32 * 32;
t.small_height = (int32_t)(r->height / r->upscale) / 32 * 32;
t.refine = r->refine > 0 ? r->refine : (int32_t)inf.upscale_refine;
}
if (finish_status == RAD_OK) {
for (int s = 1; s <= 2; ++s) {
gen::VideoEvent e;
@@ -204,6 +209,47 @@ TEST(the_generator_gets_the_size_the_chunks_and_the_steps) {
v.wait(str_at(json::parse(r.body, nullptr, false), "id"));
}
/* Latent upscaling: refused by a model without an upscaler; with one, the factor and the refine
* steps reach the generator, a factor of 1 is none, and the refine steps need a factor. */
TEST(latent_upscale_reaches_the_generator_where_the_model_has_an_upscaler) {
Videos v;
CHECK_EQ(v.refused(Videos::body({ { "latent_upscale", 2 } })), std::string("latent_upscale"));
v.gen.inf.upscale_max = 4.0;
v.gen.inf.upscale_refine = 3;
CHECK_EQ(v.refused(Videos::body({ { "latent_upscale", 4.5 } })), std::string("latent_upscale"));
CHECK_EQ(v.refused(Videos::body({ { "latent_upscale", 0.5 } })), std::string("latent_upscale"));
CHECK_EQ(v.refused(Videos::body({ { "upscale_refine_steps", 2 } })),
std::string("upscale_refine_steps"));
CHECK_EQ(v.refused(Videos::body({ { "latent_upscale", 2 }, { "upscale_refine_steps", 1.5 } })),
std::string("upscale_refine_steps"));
HttpResponse r = v.create(Videos::body({ { "size", "512x512" }, { "latent_upscale", "2" },
{ "upscale_refine_steps", 4 } }));
REQUIRE_EQ(r.status, 200);
CHECK_EQ(v.gen.last().upscale, 2.0);
CHECK_EQ(v.gen.last().refine, 4);
/* The video object says how it was made, at creation and when it is fetched. */
json o = json::parse(r.body, nullptr, false);
CHECK_EQ(o["latent_upscale"].get<double>(), 2.0);
CHECK_EQ(o["upscale_refine_steps"].get<int>(), 4);
CHECK_EQ(str_at(o, "upscale_from"), std::string("256x256"));
json done = v.wait(str_at(o, "id"));
CHECK_EQ(str_at(done, "upscale_from"), std::string("256x256"));
/* Without refine steps named, the generator's default is what the object reports. */
r = v.create(Videos::body({ { "size", "512x512" }, { "latent_upscale", 2 } }));
REQUIRE_EQ(r.status, 200);
o = json::parse(r.body, nullptr, false);
CHECK_EQ(o["upscale_refine_steps"].get<int>(), 3);
v.wait(str_at(o, "id"));
r = v.create(Videos::body({ { "latent_upscale", 1 } }));
REQUIRE_EQ(r.status, 200);
CHECK_EQ(v.gen.last().upscale, 0.0);
CHECK_EQ(v.gen.last().refine, 0);
o = json::parse(r.body, nullptr, false);
CHECK(!o.contains("latent_upscale") && !o.contains("upscale_refine_steps") &&
!o.contains("upscale_from"));
v.wait(str_at(o, "id"));
}
/* A form is read as JSON's fields are (OpenAI's clients send the create call as one). */
TEST(a_form_creates_a_video_too) {
Videos v;