/* ref2va_pack_test.cpp -- the Ref2VA packing against golden vectors produced by the reference * implementation itself. * * arch/minimaxh3/rad_ref2va_pack.cpp is pure host arithmetic, so it is compiled straight in here -- * no rad_core, no device. tests/data/ref2va/golden.json is generated by gen_golden.py, which runs * the reference PackedLayout (from the MiniMax-H3 model file of a ComfyUI install) over the cases; * every expectation below is the reference's own output, not a re-derivation. The positions are * compared after a float64 -> float cast, bit for bit: both sides run the same IEEE double * operations, so an exact match is the contract -- a ULP drift would mean the port reordered the * arithmetic. */ #include "rad_test.h" #include "rad_ref2va_pack.h" #include "util/json.h" #include #include using Json = rad::Json; using namespace minimaxh3; namespace { Json& golden() { static Json g = [] { std::ifstream f(std::string(RAD_REF2VA_GOLDEN) + "/golden.json"); if (!f.is_open()) { fprintf(stderr, " FAIL golden.json missing -- regenerate with " "tests/data/ref2va/gen_golden.py\n"); std::exit(1); } return Json::parse(f); }(); return g; } /* The reference's names for the segment kinds. */ const char* kind_name(int kind) { static const char* const kNames[kR2VKinds] = { "text", "cond", "cond_audio", "ref_img", "ref_audio", "audio", "video" }; return kind >= 0 && kind < kR2VKinds ? kNames[kind] : "?"; } int ref_kind(const std::string& s) { if (s == "image") return kR2VRefImage; if (s == "audio") return kR2VRefAudioOnly; if (s == "video") return kR2VRefVideo; if (s == "video_audio") return kR2VRefVideoAudio; return -1; } /* The pack `name`'s case asks for, built. */ Ref2VAPack build_case(const Json& c) { Ref2VAGeom g{ c["latent_t"], c["latent_h"], c["latent_w"], c["audio_t"] }; std::vector kfs; for (const auto& k : c["keyframes"]) kfs.push_back({ k["frame"], k["video_t"], k["audio_t"] }); std::vector refs; for (const auto& r : c["refs"]) refs.push_back({ ref_kind(r["kind"].get()), r["latent_h"], r["latent_w"], r["latent_t"], r["ref_audio_t"] }); Ref2VAPack p; CHECK_EQ(ref2va_build(c["text_len"], g, kfs, refs, &p), 0); return p; } void check_case(const std::string& name) { const Json& c = golden()[name]; const Ref2VAPack p = build_case(c); CHECK_EQ(p.rows, c["seq_len"].get()); CHECK_EQ((int64_t)p.pos.size(), p.rows * 3); const auto& segs = c["segments"]; CHECK_EQ(p.segs.size(), segs.size()); for (size_t i = 0; i < p.segs.size() && i < segs.size(); ++i) { CHECK_EQ(p.segs[i].start, segs[i][0].get()); CHECK_EQ(p.segs[i].end, segs[i][1].get()); CHECK(std::string(kind_name(p.segs[i].kind)) == segs[i][2].get()); const std::string kind = segs[i][2].get(); CHECK_EQ(p.segs[i].update, kind == "audio" || kind == "video" ? 1 : 0); } /* Positions, bit for bit after the float cast: a coordinate drawn a different way in double * can round to the same f32 and still bend a rotary the next latent over. */ const auto& gp = c["positions"]; for (size_t i = 0; i < gp.size(); ++i) for (int j = 0; j < 3; ++j) { const float want = (float)gp[i][j].get(); if (p.pos[(int64_t)j * p.rows + i] != want) { ::radtest::fail(__FILE__, __LINE__, std::string(name) + ": row " + std::to_string(i) + " axis " + std::to_string(j) + " is " + std::to_string(p.pos[(int64_t)j * p.rows + i]) + ", want " + std::to_string(want)); return; } } /* The two latent spaces' row index lists and their update flags, rebuilt from the pack's * segments the way the sampler scatters them, against the reference's concatenated lists. */ std::vector img_pos, audio_pos; std::vector img_up, audio_up; for (const Ref2VASeg& s : p.segs) { for (int64_t r = s.start; r < s.end; ++r) { if (ref2va_is_img(s.kind)) { img_pos.push_back(r); img_up.push_back(s.update); } if (ref2va_is_audio(s.kind)) { audio_pos.push_back(r); audio_up.push_back(s.update); } } } CHECK(img_pos == c["img_pos"].get>()); CHECK(audio_pos == c["audio_pos"].get>()); std::vector giu, gau; for (const auto& b : c["img_update"]) giu.push_back(b.get()); for (const auto& b : c["audio_update"]) gau.push_back(b.get()); CHECK(img_up == giu); CHECK(audio_up == gau); } } /* namespace */ TEST(pack_golden_t2va) { check_case("t2va"); } TEST(pack_golden_fl2va) { check_case("fl2va"); } TEST(pack_golden_ref2va_all) { check_case("ref2va_all"); } TEST(pack_golden_chain) { check_case("chain"); } TEST(pack_golden_no_text) { check_case("no_text"); } TEST(pack_rejects_negative_dims) { Ref2VAPack p; Ref2VAGeom g{ 2, 4, 4, 1 }; CHECK_EQ(ref2va_build(-1, g, {}, {}, &p), -1); CHECK_EQ(ref2va_build(0, Ref2VAGeom{ -1, 4, 4, 1 }, {}, {}, &p), -1); CHECK_EQ(ref2va_build(0, g, { { -1, 1, 0 } }, {}, &p), -1); CHECK_EQ(ref2va_build(0, g, {}, { { kR2VRefImage, -4, 4, 0, 0 } }, &p), -1); CHECK_EQ(ref2va_build(0, g, {}, { { 7, 4, 4, 0, 0 } }, &p), -1); } TEST(pack_plain_matches_the_three_segment_order) { /* With no references the pack is the text-to-video sequence [text | audio | video], the * sampler owning the audio and video rows and nothing owning the prompt. */ Ref2VAPack p; Ref2VAGeom g{ 3, 8, 10, 4 }; CHECK_EQ(ref2va_build(5, g, {}, {}, &p), 0); CHECK_EQ(p.segs.size(), 3u); CHECK_EQ(p.segs[0].kind, kR2VText); CHECK_EQ(p.segs[1].kind, kR2VAudio); CHECK_EQ(p.segs[2].kind, kR2VVideo); CHECK_EQ(p.segs[0].update, 0); CHECK_EQ(p.segs[1].update, 1); CHECK_EQ(p.segs[2].update, 1); CHECK_EQ(p.segs[0].start, 0); CHECK_EQ(p.segs[0].end, 5); /* the prompt */ CHECK_EQ(p.segs[1].start, 5); /* 2 channels x 4 audio latents */ CHECK_EQ(p.segs[1].end, 5 + 8); CHECK_EQ(p.segs[2].start, 5 + 8); /* 3 latents x (8/2)(10/2) patches */ CHECK_EQ(p.segs[2].end, 5 + 8 + 60); CHECK_EQ(p.rows, 5 + 8 + 60); CHECK_EQ(p.pos.size(), (size_t)(3 * p.rows)); /* planar, the shape R_ROPE stages */ /* The rows' positions, axis by axis: the prompt counts up on time and ignores space; the * soundtrack runs channel-major (all of the left, then the right) at height 0 on the width * axis' two ends; every video row carries its frame's time. */ const int64_t rows = p.rows; for (int64_t i = 0; i < 5; ++i) { CHECK_EQ(p.pos[(size_t)i], (float)i); CHECK_EQ(p.pos[(size_t)rows + i], 0.0f); CHECK_EQ(p.pos[(size_t)(2 * rows) + i], 0.0f); } for (int ch = 0; ch < 2; ++ch) for (int k = 0; k < 4; ++k) { const size_t r = (size_t)(5 + ch * 4 + k); CHECK_EQ(p.pos[r], (float)(5 + k)); /* time counts per channel, not per row */ CHECK_EQ(p.pos[rows + r], 0.0f); } /* The channels sit at the opposite ends of the width axis, and they are different. */ CHECK(p.pos[2 * rows + 5] != p.pos[2 * rows + 9]); /* Video rows: the first frame's time is the timeline origin (text_len + all reference * spans -- none here); 20 rows (4x5 patches) per frame, h-major; and frame k+1's time is * frame k's plus 5/3 * kPattern[k % 5]. */ const size_t v0 = 5 + 8; const int64_t fpp = 4 * 5; CHECK_EQ(p.pos[v0], 5.0f); CHECK_EQ(p.pos[v0 + 1], 5.0f); /* still frame 0 */ CHECK_EQ(p.pos[v0 + fpp], (float)(5.0 + 5.0 / 3.0)); /* frame 1, span 1 ยท 5/3 */ CHECK_EQ(p.pos[(size_t)(rows + v0)], p.pos[(size_t)(rows + v0 + 1)]); /* same patch row */ CHECK(p.pos[(size_t)(rows + v0)] != p.pos[(size_t)(rows + v0 + 5)]); /* y = 1 begins */ } TEST(pack_ref_kinds_predicates) { CHECK_EQ(ref2va_seg_tag(kR2VText), 1); CHECK_EQ(ref2va_seg_tag(kR2VVideo), 0); CHECK_EQ(ref2va_seg_tag(kR2VAudio), 2); CHECK_EQ(ref2va_seg_tag(kR2VCond), 0); CHECK_EQ(ref2va_seg_tag(kR2VCondAudio), 2); CHECK_EQ(ref2va_is_img(kR2VText), 0); CHECK(ref2va_is_img(kR2VCond) && ref2va_is_img(kR2VRefImg) && ref2va_is_img(kR2VVideo)); CHECK(ref2va_is_audio(kR2VCondAudio) && ref2va_is_audio(kR2VRefAudio) && ref2va_is_audio(kR2VAudio)); CHECK(!ref2va_is_audio(kR2VRefImg) && !ref2va_is_img(kR2VRefAudio)); } TEST(pack_mod_groups_images_collapse_to_four) { /* Nine reference photos, the most a request carries. Every photo is a video-pinned block * reading the same modulation cell, so the nine merge into one group and the sequence has * four (prompt, photos, soundtrack, video) whatever the photo count. The merge is what fits * the request's twelve segments into the plugin's kMaxSegs modulation groups. */ Ref2VAGeom g{ 2, 8, 8, 3 }; std::vector refs(9, Ref2VARef{ kR2VRefImage, 6, 6, 1, 0 }); Ref2VAPack p; CHECK_EQ(ref2va_build(7, g, {}, refs, &p), 0); CHECK_EQ(p.segs.size(), 12u); /* text + 9 photos + audio + video */ const int64_t S = 24; Ref2VAModGroups grp; ref2va_mod_groups(p, S, {}, &grp); CHECK_EQ(grp.md.size(), 4u); CHECK(grp.md == std::vector({ 1, 0, 2, 0 })); /* text, photos, audio, video */ /* Cells: text and the video target read the video schedule (r0 = 0), the photos the * video-pinned block (2S), the soundtrack the audio schedule (S). */ CHECK(grp.r0 == std::vector({ 0, 2 * S, S, 0 })); CHECK_EQ(grp.bound.size(), 5u); CHECK_EQ(grp.bound.front(), 0); CHECK_EQ(grp.bound.back(), p.rows); /* The photo group spans the whole run of reference rows, from the prompt's end to the * soundtrack's start: the two segment boundaries that survive the merge. */ const int64_t text_end = p.segs[0].end, audio_start = p.segs[p.segs.size() - 2].start; CHECK_EQ(grp.bound[1], text_end); CHECK_EQ(grp.bound[2], audio_start); } TEST(pack_mod_groups_pin_each_stream_at_its_own_block) { /* A picture and a standalone soundtrack: each reads its own stream's pinned embeddings, the * video's at 2S and the soundtrack's at 3S. */ Ref2VAGeom g{ 2, 8, 8, 3 }; const std::vector refs = { { kR2VRefImage, 4, 4, 1, 0 }, { kR2VRefAudioOnly, 0, 0, 0, 2 } }; Ref2VAPack p; CHECK_EQ(ref2va_build(5, g, {}, refs, &p), 0); const int64_t S = 10; Ref2VAModGroups grp; ref2va_mod_groups(p, S, {}, &grp); CHECK(grp.md == std::vector({ 1, 0, 2, 2, 0 })); CHECK(grp.r0 == std::vector({ 0, 2 * S, 3 * S, S, 0 })); } TEST(pack_mod_groups_invariants_over_the_goldens) { /* For every golden pack: the group boundaries span the sequence and are a strictly increasing * subset of the segment boundaries, so no segment is split; every segment lies inside one * group of its own modality; no two adjacent groups share a cell, so the merge is maximal; and * the groups do not depend on the step count S. rad_arch_video_pack derives them at S = 1 and * the step at the model's S, and the two tables must line up. */ for (const char* name : { "t2va", "fl2va", "ref2va_all", "chain", "no_text" }) { const Ref2VAPack p = build_case(golden()[name]); Ref2VAModGroups a, b; ref2va_mod_groups(p, 1, {}, &a); ref2va_mod_groups(p, 64, {}, &b); CHECK(a.bound == b.bound); CHECK(!a.bound.empty()); CHECK_EQ(a.bound.front(), 0); CHECK_EQ(a.bound.back(), p.rows); for (size_t i = 0; i + 1 < a.bound.size(); ++i) CHECK(a.bound[i] < a.bound[i + 1]); /* every boundary is a fine boundary (a segment start, or the closing rows count) */ for (int64_t x : a.bound) { bool found = (x == p.rows); for (const Ref2VASeg& s : p.segs) if (s.start == x) found = true; CHECK(found); } /* every fine segment sits whole in a group carrying its own modality */ for (const Ref2VASeg& s : p.segs) { if (s.end <= s.start) continue; size_t g = 0; while (g + 1 < a.bound.size() && a.bound[g + 1] <= s.start) ++g; CHECK(a.bound[g] <= s.start && s.end <= a.bound[g + 1]); CHECK_EQ(a.md[a.col[g]], ref2va_seg_tag(s.kind)); } /* Adjacent groups differ in cell, and two groups share a column exactly when they read the * same cell: a cond photo and a ref photo separated by a soundtrack are two groups on one * column. Mirrors the pack's cell_of_kind at a's S. */ const int64_t S = 1; auto cell = [S](int kind) -> std::pair { switch (kind) { case kR2VAudio: return { 2, S }; break; /* audio schedule */ case kR2VCondAudio: case kR2VRefAudio: return { 2, 3 * S }; break; /* audio-pinned block */ case kR2VText: return { 1, 0 }; break; /* video schedule */ case kR2VCond: case kR2VRefImg: return { 0, 2 * S }; break; /* video-pinned block */ default: return { 0, 0 }; break; /* video target */ } }; auto kind_at = [&p](int64_t row) { for (const Ref2VASeg& s : p.segs) if (s.start <= row && row < s.end) return s.kind; return -1; }; for (size_t i = 0; i + 1 < a.col.size(); ++i) CHECK(a.col[i] != a.col[i + 1]); std::vector used(a.md.size(), 0); for (size_t i = 0; i < a.col.size(); ++i) { CHECK(a.col[i] >= 0 && (size_t)a.col[i] < a.md.size()); used[a.col[i]] = 1; const std::pair c = cell(kind_at(a.bound[i])); CHECK_EQ(a.md[a.col[i]], c.first); CHECK_EQ(a.r0[a.col[i]], c.second); for (size_t j = 0; j < i; ++j) CHECK_EQ((int)(a.col[i] == a.col[j]), (int)(cell(kind_at(a.bound[i])) == cell(kind_at(a.bound[j])))); } for (char u : used) CHECK(u); /* every column is addressed */ } } TEST(pack_mod_groups_plain_equals_the_fine_table) { /* A plain [text | audio | video] pack has three distinct cells, so its groups are its three * segments with the same boundaries: text-to-video modulates exactly as without the merge. */ Ref2VAPack p; Ref2VAGeom g{ 3, 8, 10, 4 }; CHECK_EQ(ref2va_build(5, g, {}, {}, &p), 0); Ref2VAModGroups grp; ref2va_mod_groups(p, 32, {}, &grp); CHECK_EQ(grp.md.size(), 3u); CHECK(grp.md == std::vector({ 1, 2, 0 })); /* text, audio, video */ CHECK(grp.r0 == std::vector({ 0, 32, 0 })); /* video sched, audio sched, video */ CHECK_EQ(grp.bound.size(), 4u); CHECK(grp.bound == std::vector({ 0, 5, 13, 5 + 8 + 60 })); } TEST(pack_mod_groups_pad_run_cuts_the_prompt) { /* One vision-pad run inside the prompt: ComfyUI tags those rows video, so they form a group * reading the video projection on the prompt's timestep block. The text pieces before and * after the pad share one cell, and the pad shares the video target's: three cells for five * groups. */ Ref2VAPack p; Ref2VAGeom g{ 3, 8, 10, 4 }; CHECK_EQ(ref2va_build(5, g, {}, {}, &p), 0); const int64_t S = 32; Ref2VAModGroups grp; ref2va_mod_groups(p, S, { Ref2VATagRun{ 1, 3, 0 } }, &grp); CHECK(grp.bound == std::vector({ 0, 1, 3, 5, 13, 5 + 8 + 60 })); CHECK(grp.col == std::vector({ 0, 1, 0, 2, 1 })); /* text, pad, text, audio, video */ CHECK(grp.md == std::vector({ 1, 0, 2 })); CHECK(grp.r0 == std::vector({ 0, 0, S })); /* pad on the video schedule */ } TEST(pack_mod_groups_many_pads_stay_few_cells) { /* Nine reference photos, each with its pad run and prompt text between: the groups multiply * (text and pad alternate), the DISTINCT CELLS do not -- four: text, pad/video, the photos' * pinned video cell, the soundtrack. This is the merge the fixed modulation table rides. */ Ref2VAGeom g{ 2, 8, 8, 3 }; std::vector refs(9, Ref2VARef{ kR2VRefImage, 6, 6, 1, 0 }); Ref2VAPack p; CHECK_EQ(ref2va_build(100, g, {}, refs, &p), 0); std::vector runs; for (int i = 0; i < 9; ++i) runs.push_back({ (int64_t)i * 10 + 1, (int64_t)i * 10 + 3, 0 }); const int64_t S = 24; Ref2VAModGroups grp; ref2va_mod_groups(p, S, runs, &grp); CHECK_EQ(grp.col.size(), 22u); /* 19 prompt pieces + photos + soundtrack + video */ CHECK_EQ(grp.md.size(), 4u); /* ... over four cells */ CHECK(grp.md == std::vector({ 1, 0, 0, 2 })); /* text, pad, photo-pinned, audio */ CHECK(grp.r0 == std::vector({ 0, 0, 2 * S, S })); /* The video target shares the pad's cell and column; the photo group shares the pad's * projection but not its block. */ CHECK_EQ(grp.col.front(), 0); CHECK_EQ(grp.col[1], 1); CHECK_EQ(grp.col.back(), 1); CHECK_EQ(grp.bound.front(), 0); CHECK_EQ(grp.bound.back(), p.rows); } TEST(pack_mod_groups_runs_are_normalised) { /* Runs arrive as the driver staged them: unsorted, partly outside the prompt, even empty. The * merge clamps each to the text segment, drops what falls outside, and the result is the same * cut as hand-placed in-range runs. A run covering the whole prompt leaves one pad group and * NO text group; an out-of-range run cuts nothing at all. */ Ref2VAPack p; Ref2VAGeom g{ 3, 8, 10, 4 }; CHECK_EQ(ref2va_build(5, g, {}, {}, &p), 0); const int64_t S = 32; Ref2VAModGroups one, a, b; ref2va_mod_groups(p, S, { Ref2VATagRun{ 1, 5, 0 } }, &one); /* Same cut: out-of-range tails, an empty run, a run starting before the prompt and one ending * at its start, in scrambled order. The two in-range fragments meet and merge into one pad * group [1,5), exactly the single-run table. */ ref2va_mod_groups(p, S, { Ref2VATagRun{ 4, 9, 0 }, Ref2VATagRun{ 3, 6, 0 }, Ref2VATagRun{ 1, 3, 0 }, Ref2VATagRun{ 2, 2, 0 }, Ref2VATagRun{ -4, 0, 0 } }, &a); CHECK(a.bound == one.bound); CHECK(a.col == one.col); ref2va_mod_groups(p, S, { Ref2VATagRun{ 5, 9, 0 } }, &b); /* starts at the segment end */ CHECK_EQ(b.col.size(), 3u); /* untouched: the plain three groups */ CHECK(b.col == std::vector({ 0, 1, 2 })); } TEST(pack_mod_groups_pad_tag_audio_reads_the_audio_projection) { /* A run carries its tag: a tag-2 (audio) run on the prompt opens the (2, 0) cell, distinct * from the text's and from the audio target's (2, S). The driver emits only tag 0 runs. */ Ref2VAPack p; Ref2VAGeom g{ 3, 8, 10, 4 }; CHECK_EQ(ref2va_build(5, g, {}, {}, &p), 0); Ref2VAModGroups grp; ref2va_mod_groups(p, 16, { Ref2VATagRun{ 0, 5, 2 } }, &grp); /* The run covers the whole prompt, so the text group is entirely pad: three groups over three * distinct cells -- (2,0) pad, (2,16) audio target, (0,0) video target. */ CHECK(grp.bound == std::vector({ 0, 5, 13, 73 })); CHECK(grp.md == std::vector({ 2, 2, 0 })); CHECK(grp.r0 == std::vector({ 0, 16, 0 })); CHECK(grp.col == std::vector({ 0, 1, 2 })); } RAD_TEST_MAIN()