Archived
The reference modulates by a per-token TAG: token_tags_from_embeds_info tags every image embed span (with one flanking token on each side) 0 -- video -- inside the prompt, while audio pads keep tag 1 (parallel PR #236, point 3). Our port modulated the whole prompt span as text, so every reference photo's pad tokens read the text ada projection where the reference reads the video one. The tags arrive in the prompt itself and nothing else carries them, so the denoiser could not know, and the look drifted with the reference count. The driver now writes each image item's token span into a tail of ref_tab -- one count, then {start, length} per run, audio items excluded (the reference tags images only). The tail rides inert through the pack and ref_parts, and is present exactly when the reference table is, so an i2va request keeps its old bytes. ref2va_mod_groups grows a runs argument: it cuts the prompt at the runs' edges and gives the pad pieces the VIDEO cell -- the video projection on the video-schedule block; the prompt's own timestep row never changes, only the projection. Cells stay few however many photos interleave, but groups no longer equal cells: two prompt pieces separated by a pad share one cell non-adjacently. So the merged-group output gains a per-group column map, and the modulated- norm kernels -- libr4d and libref alike -- accept an optional trailing colmap (slot 9 quant, 7 bf16, past every other arch's last operand) that redirects sequence -> table column, relaxing the row-count checks it binds. rad_arch_video_mod_segs exports boundaries then map, the plugin stages both only for a reference request -- the plain path still stages boundaries alone, byte-identical -- and the half-split keeps its single video row by taking the target's column instead of the last one. max_ref_segs now bounds the DISTINCT CELLS, its old meaning was the group count. ABI 28; no struct's shape moved, the layout table is re-stamped. Co-Authored-By: Claude <noreply@anthropic.com>
469 lines
24 KiB
C++
469 lines
24 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));
|
|
}
|
|
|
|
TEST(pack_mod_groups_images_collapse_to_four) {
|
|
/* Nine reference photos -- the reference-to-video workflow's request. Every photo is a
|
|
* video-pinned reference block sharing ONE modulation cell, so all nine merge into a single
|
|
* group: the sequence collapses to FOUR groups (prompt, all-photos, soundtrack, video) whatever
|
|
* the photo count. This merge is what lets a nine-photo request ride the fixed kMaxSegs-wide
|
|
* modulation table that its twelve fine segments would otherwise overrun. */
|
|
Ref2VAGeom g{ 2, 8, 8, 3 };
|
|
std::vector<Ref2VARef> 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<int>({ 1, 0, 2, 0 })); /* text, photos, audio, video */
|
|
/* Cells: text and the video target read the video schedule (r0 = 0); the photos read the
|
|
* video-PINNED block (2S); the soundtrack reads the audio schedule (S). */
|
|
CHECK(grp.r0 == std::vector<int64_t>({ 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's span is the whole run of reference rows: it starts where the prompt ends and
|
|
* ends where the soundtrack starts -- the two fine 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_invariants_over_the_goldens) {
|
|
/* For every golden pack: the merged boundaries span the sequence and are a strictly-increasing
|
|
* SUBSET of the fine segment boundaries (so no fine segment is ever split); every fine segment
|
|
* lies wholly inside one group whose modality is the segment's own tag; no two adjacent groups
|
|
* share a cell (the merge is maximal -- had they, they would have collapsed); and the merge is
|
|
* independent of the step count S -- the driver derives it at S = 1, the denoiser at the real S,
|
|
* so the boundaries must be identical for the two tables to 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 -- merging left no mergeable neighbours -- and two groups
|
|
* share a column EXACTLY when they read the same cell: a cond photo and a ref photo carry
|
|
* one cell, and separated by a soundtrack they stay two groups on one column, so the map
|
|
* is not the identity. Mirrors the pack's cell_of_kind at a's S. */
|
|
const int64_t S = 1;
|
|
auto cell = [S](int kind) -> std::pair<int, int64_t> {
|
|
switch (kind) {
|
|
case kR2VAudio: return { 2, S }; break; /* audio schedule */
|
|
case kR2VCondAudio:
|
|
case kR2VRefAudio: return { 2, 2 * 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<char> 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<int, int64_t> 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 -- no two share one -- so it
|
|
* merges to exactly the three fine groups and its boundaries equal the fine ones. That is why
|
|
* a driver with no mod-seg export can stage the fine table into the norm operand unchanged and
|
|
* reproduce the pre-merge output byte for byte. */
|
|
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<int>({ 1, 2, 0 })); /* text, audio, video */
|
|
CHECK(grp.r0 == std::vector<int64_t>({ 0, 32, 0 })); /* video sched, audio sched, video */
|
|
CHECK_EQ(grp.bound.size(), 4u);
|
|
CHECK(grp.bound == std::vector<int64_t>({ 0, 5, 13, 5 + 8 + 60 }));
|
|
}
|
|
|
|
TEST(pack_mod_groups_pad_run_cuts_the_prompt) {
|
|
/* One vision-pad run inside the prompt: the reference tags those rows VIDEO, so they get a
|
|
* group of their own reading the video ada projection on the VIDEO-SCHEDULE block (the prompt
|
|
* keeps its own timestep row -- only the projection changes). The cell deduplication is the
|
|
* whole point: the text pieces before and after the pad share ONE column, and the pad shares
|
|
* its column with the video target -- three distinct 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<int64_t>({ 0, 1, 3, 5, 13, 5 + 8 + 60 }));
|
|
CHECK(grp.col == std::vector<int>({ 0, 1, 0, 2, 1 })); /* text, pad, text, audio, video */
|
|
CHECK(grp.md == std::vector<int>({ 1, 0, 2 }));
|
|
CHECK(grp.r0 == std::vector<int64_t>({ 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<Ref2VARef> refs(9, Ref2VARef{ kR2VRefImage, 6, 6, 1, 0 });
|
|
Ref2VAPack p;
|
|
CHECK_EQ(ref2va_build(100, g, {}, refs, &p), 0);
|
|
std::vector<Ref2VATagRun> 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<int>({ 1, 0, 0, 2 })); /* text, pad, photo-pinned, audio */
|
|
CHECK(grp.r0 == std::vector<int64_t>({ 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<int>({ 0, 1, 2 }));
|
|
}
|
|
|
|
TEST(pack_mod_groups_pad_tag_audio_reads_the_audio_projection) {
|
|
/* The run struct carries its tag, so a tag-2 run (the audio modality) on the prompt's rows
|
|
* would open the (2, 0) cell -- a distinct column from both the text piece and the audio
|
|
* TARGET's (2, S). The reference tags only image pads (tag 0), so nothing emits this today;
|
|
* the plumbing is tag-generic on purpose. */
|
|
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<int64_t>({ 0, 5, 13, 73 }));
|
|
CHECK(grp.md == std::vector<int>({ 2, 2, 0 }));
|
|
CHECK(grp.r0 == std::vector<int64_t>({ 0, 16, 0 }));
|
|
CHECK(grp.col == std::vector<int>({ 0, 1, 2 }));
|
|
}
|
|
|
|
RAD_TEST_MAIN()
|