Archived
Ref2VA ref-image conditioning verified on-device: ref step runs all steps no fault; output differs ref-present vs ref-absent (by log). 6-9 images rejected cleanly at submit (no engine crash). Merged-cell modulation (<=9) tracked next. NOTE: placeholder author -- re-author before any push. Do NOT push this branch. Co-Authored-By: Claude <noreply@anthropic.com>
266 lines
13 KiB
C++
266 lines
13 KiB
C++
/* 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 <nlohmann/json.hpp>
|
|
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
using namespace minimaxh3;
|
|
|
|
namespace {
|
|
|
|
nlohmann::json& golden() {
|
|
static nlohmann::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 nlohmann::json::parse(f);
|
|
}();
|
|
return g;
|
|
}
|
|
|
|
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 nlohmann::json& c) {
|
|
Ref2VAGeom g{ c["latent_t"], c["latent_h"], c["latent_w"], c["audio_t"] };
|
|
std::vector<Ref2VAKeyframe> kfs;
|
|
for (const auto& k : c["keyframes"])
|
|
kfs.push_back({ k["frame"], k["video_t"], k["audio_t"] });
|
|
std::vector<Ref2VARef> refs;
|
|
for (const auto& r : c["refs"])
|
|
refs.push_back({ ref_kind(r["kind"].get<std::string>()), 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 nlohmann::json& c = golden()[name];
|
|
const Ref2VAPack p = build_case(c);
|
|
|
|
CHECK_EQ(p.rows, c["seq_len"].get<int64_t>());
|
|
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<int64_t>());
|
|
CHECK_EQ(p.segs[i].end, segs[i][1].get<int64_t>());
|
|
CHECK(std::string(ref2va_kind_name(p.segs[i].kind)) == segs[i][2].get<std::string>());
|
|
const std::string kind = segs[i][2].get<std::string>();
|
|
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<double>();
|
|
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<int64_t> img_pos, audio_pos;
|
|
std::vector<int> 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<std::vector<int64_t>>());
|
|
CHECK(audio_pos == c["audio_pos"].get<std::vector<int64_t>>());
|
|
std::vector<int> giu, gau;
|
|
for (const auto& b : c["img_update"]) giu.push_back(b.get<int>());
|
|
for (const auto& b : c["audio_update"]) gau.push_back(b.get<int>());
|
|
CHECK(img_up == giu);
|
|
CHECK(audio_up == gau);
|
|
|
|
/* Modulation rows at probe timesteps -- including the probes past both pins, where the pins
|
|
* collapse onto the streams' own labels and the row numbering must collapse with them. */
|
|
for (const auto& m : c["mod"]) {
|
|
std::vector<int> rows;
|
|
CHECK_EQ(ref2va_mod_rows(p, m["t_v"].get<double>(), m["t_a"].get<double>(), &rows), 0);
|
|
CHECK(rows == m["rows"].get<std::vector<int>>());
|
|
|
|
/* The same cells as step_ada will fill them: (md, trow). md must equal the reference
|
|
* encoding's tag (its row's low digits), and the timestep row the cell reads must carry
|
|
* exactly the label ref2va_mod_rows compacted -- i.e. the schedule row when the reference
|
|
* label IS the stream's current timestep, and a pin otherwise. Checked pin-value-free. */
|
|
std::vector<int> md, trow;
|
|
ref2va_ada_cell(p, m["t_v"].get<double>(), m["t_a"].get<double>(), &md, &trow);
|
|
CHECK_EQ(md.size(), p.segs.size());
|
|
for (size_t i = 0; i < p.segs.size(); ++i) {
|
|
CHECK_EQ(md[i], rows[i] % 3); /* the modality column agrees */
|
|
if (p.segs[i].end <= p.segs[i].start) continue; /* empty cell, never read */
|
|
const double tv = m["t_v"].get<double>(), ta = m["t_a"].get<double>();
|
|
const double lbl = ref2va_t_label(p.segs[i].kind, tv, ta);
|
|
/* The modality picks the schedule row; the label decides schedule vs pin -- decided
|
|
* by modality, not by the bare label, so it holds even if a probe sets tv == ta. */
|
|
if (md[i] == 2) CHECK_EQ(trow[i], (lbl == ta) ? kR2VTAudio : kR2VTPinAudio);
|
|
else CHECK_EQ(trow[i], (lbl == tv) ? kR2VTVideo : kR2VTPinVideo);
|
|
}
|
|
}
|
|
}
|
|
|
|
} /* 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 must reduce to the engine's historical [text | audio | video]
|
|
* -- exactly the seg_cu table the driver stages today -- with the sampler owning the audio
|
|
* and video rows and nobody else 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 row contract positions() used to compute by hand, 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_ada_cell_plain_is_the_shipped_three_cell_plan) {
|
|
/* No references -> the cell plan must be byte-identical to the modulation step_ada builds
|
|
* today: md == the kSegMod column {text 1, audio 2, video 0}, and every cell reads a schedule
|
|
* row (the plain path has no pins), i.e. the row base r0 = e == audio ? S : 0. This holds at
|
|
* every probe, because a plain segment's label is always its stream's own timestep. */
|
|
Ref2VAPack p;
|
|
Ref2VAGeom g{ 3, 8, 10, 4 };
|
|
CHECK_EQ(ref2va_build(5, g, {}, {}, &p), 0);
|
|
for (double tv : { 0.9995, 0.5, 0.001, 0.999, 1.0 }) {
|
|
for (double ta : { 1.0, 0.5, 0.001, 0.999 }) {
|
|
std::vector<int> md, trow;
|
|
ref2va_ada_cell(p, tv, ta, &md, &trow);
|
|
const std::vector<int> want_md{ 1, 2, 0 }; /* == kSegMod */
|
|
const std::vector<int> want_tr{ kR2VTVideo, kR2VTAudio, kR2VTVideo };
|
|
CHECK(md == want_md);
|
|
CHECK(trow == want_tr);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST(pack_ada_cell_pinned_rows_collapse_onto_the_schedule) {
|
|
/* One reference photo and one standalone audio, over a video+audio target. Well under both
|
|
* pins the reference rows read the pinned embedding rows; at or above a pin (the sampler's
|
|
* early, near-clean steps) the pin is the stream's own timestep and the row collapses onto it,
|
|
* exactly as ref2va_mod_rows' row numbering collapses. Pins: visual 0.999, audio 1.0. */
|
|
Ref2VAPack p;
|
|
Ref2VAGeom g{ 3, 8, 10, 4 };
|
|
std::vector<Ref2VARef> refs = { { kR2VRefImage, 4, 4, 0, 0 }, { kR2VRefAudioOnly, 0, 0, 0, 2 } };
|
|
CHECK_EQ(ref2va_build(5, g, {}, refs, &p), 0);
|
|
/* [text, ref_img, ref_audio, audio(target), video(target)] */
|
|
CHECK_EQ(p.segs.size(), 5u);
|
|
|
|
std::vector<int> md, trow;
|
|
ref2va_ada_cell(p, 0.5, 0.5, &md, &trow);
|
|
const std::vector<int> want_md{ 1, 0, 2, 2, 0 }; /* text, ref_img, ref_audio, audio, video */
|
|
const std::vector<int> want_tr{ kR2VTVideo, kR2VTPinVideo, kR2VTPinAudio, kR2VTAudio,
|
|
kR2VTVideo };
|
|
CHECK(md == want_md);
|
|
CHECK(trow == want_tr);
|
|
|
|
ref2va_ada_cell(p, 0.9995, 1.0, &md, &trow); /* schedule at/above both pins */
|
|
const std::vector<int> collapse{ kR2VTVideo, kR2VTVideo, kR2VTAudio, kR2VTAudio, kR2VTVideo };
|
|
CHECK(trow == collapse);
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
RAD_TEST_MAIN()
|