PIN ref2va: verified device differential + ref-count gate (revert anchor)

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>
This commit is contained in:
gearwave00001andClaude committed 2026-10-19 12:04:47 -05:00
1 parent 816e24bd46
commit 490d4c604a
73 files changed
+16484 -432

No files matched your search

+20
View File
@@ -156,6 +156,26 @@ void resize_lanczos_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA*
}
}
void resize_lanczos_rgb(const Frame& src, int32_t w, int32_t h, Frame* out) {
/* Wrapping opaque and running the RGBA kernel is exactly PIL's LANCZOS on the RGB image: at
* alpha 255 the premultiply is the identity and the divide-back takes the al == 255 branch. */
FrameRGBA pre;
pre.w = src.w;
pre.h = src.h;
pre.rgba.resize((size_t)src.w * src.h * 4);
for (size_t i = 0; i < (size_t)src.w * src.h; ++i) {
for (int c = 0; c < 3; ++c) pre.rgba[i * 4 + c] = src.rgb[i * 3 + c];
pre.rgba[i * 4 + 3] = 255;
}
FrameRGBA post;
resize_lanczos_rgba(pre, w, h, &post);
out->w = post.w;
out->h = post.h;
out->rgb.resize((size_t)post.w * post.h * 3);
for (size_t i = 0; i < (size_t)post.w * post.h; ++i)
for (int c = 0; c < 3; ++c) out->rgb[i * 3 + c] = post.rgba[i * 4 + c];
}
void composite_white(const FrameRGBA& src, Frame* out) {
out->w = src.w;
out->h = src.h;
+4
View File
@@ -18,6 +18,10 @@ namespace mm {
/* `src` resized to w x h. */
void resize_lanczos_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA* out);
/* As resize_lanczos_rgba for an opaque picture: wrapped with alpha 255 and resized, so the RGB
* channels are exactly PIL's LANCZOS on the RGB image (premultiply and its inverse are the
* identity at full alpha). The video VAE's reference preprocessing resizes RGB this way. */
void resize_lanczos_rgb(const Frame& src, int32_t w, int32_t h, Frame* out);
/* `src` over an opaque white ground. */
void composite_white(const FrameRGBA& src, Frame* out);
+34
View File
@@ -3,6 +3,8 @@
* metadata reader, the real device layer. */
#include "mm/processor.h"
#include <algorithm>
namespace rad {
namespace mm {
@@ -43,5 +45,37 @@ void Item::coords(int64_t first, int64_t count, int32_t* planes) const {
}
}
/* The rotary layout of one prompt: one run per segment, the text in front of a run counting up
* and a grid advancing the rotary by its longer side. Pure computation over the items, like the
* accessors above, so it lives here rather than in the processor. */
void PromptMedia::layout() {
rope.runs.clear();
int64_t pos = 0, tok = 0;
for (size_t pi = 0; pi < items.size(); ++pi) {
const Placed& p = items[pi];
const Item& it = *p.item;
const int32_t m = it.rows_h > 0 ? it.grid_h / it.rows_h : 1;
for (int32_t s = 0; s < it.n_seg; ++s) {
const int64_t run_tok = (int64_t)p.tok + it.seg_at[(size_t)s];
pos += run_tok - tok; /* the text in front of the run counts up */
const int32_t rh = it.segments.empty() ? it.rows_h : it.seg_h(s) / m;
const int32_t rw = it.segments.empty() ? it.rows_w : it.seg_w(s) / m;
RopeLayout::Run r;
r.tok = (int32_t)run_tok;
r.n = (int32_t)(it.row_at(s + 1) - it.row_at(s));
r.w = rw;
r.base = (int32_t)pos;
r.flat = it.rope_1d;
/* A grid advances the rotary by its longer side; a 1-D run by its length. */
pos += it.rope_1d ? r.n : std::max(rh, rw);
tok = run_tok + r.n;
r.shift = (int32_t)(pos - tok);
r.item = (int32_t)pi;
r.row0 = (int32_t)it.row_at(s);
rope.runs.push_back(r);
}
}
}
} /* namespace mm */
} /* namespace rad */
+35 -31
View File
@@ -1,5 +1,6 @@
/* processor.cpp -- see processor.h. */
#include "mm/processor.h"
#include "mm/imaging.h"
#include "rad_internal.h"
#include "rad_plugin.h"
@@ -1006,6 +1007,40 @@ int Processor::image(const uint8_t* bytes, size_t n, int32_t image_tokens,
return image_from(f, out, why);
}
int Processor::encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side,
Frame* out, std::string* why) const {
if (ratio < 1 || max_side < ratio) {
if (why) *why = "the encoder's side ratio and maximum are not usable";
return RAD_E_INVAL;
}
/* The same decode guard as image(): a reference the text encoder accepts must reach the VAE
* too, so the two never disagree on what a request may carry. */
Frame f;
RAD_TRY(decode_image(bytes, n, cfg_.max_source_pixels, &f, why));
/* Snap both sides down to a multiple of the encoder's ratio, neither above max_side, aspect
* kept as close as the grid allows: scale the longest side to fit, then floor each side to the
* grid. Flooring (never rounding up) is what keeps a side within max_side; a side under one
* grid step is raised to it, the smallest volume the encoder accepts. Our encoder runs one
* volume rather than tiling at 256, so a reference larger than the bound is shrunk here — the
* one place the reference and this build differ on an oversized input. */
auto snap = [&](int64_t s) {
int64_t v = (s / ratio) * ratio;
if (v < ratio) v = ratio;
if (v > max_side) v = max_side; /* max_side is itself a multiple of ratio */
return (int32_t)v;
};
const int32_t longest = std::max(f.w, f.h);
const double scale = longest > max_side ? (double)max_side / (double)longest : 1.0;
const int32_t w = snap((int64_t)std::floor(f.w * scale));
const int32_t h = snap((int64_t)std::floor(f.h * scale));
if (w == f.w && h == f.h) {
*out = std::move(f);
return RAD_OK;
}
resize_lanczos_rgb(f, w, h, out);
return RAD_OK;
}
namespace {
struct SampleArgs { double fps; int32_t lo, hi; bool gemma4; };
std::vector<int64_t> sample_cb(const VideoInfo& info, const void* user) {
@@ -1528,36 +1563,5 @@ int Processor::audio(const uint8_t* bytes, size_t n, std::shared_ptr<Item>* out,
return audio_from(s, out, why);
}
/* ================================================================== rotary layout */
void PromptMedia::layout() {
rope.runs.clear();
int64_t pos = 0, tok = 0;
for (size_t pi = 0; pi < items.size(); ++pi) {
const Placed& p = items[pi];
const Item& it = *p.item;
const int32_t m = it.rows_h > 0 ? it.grid_h / it.rows_h : 1;
for (int32_t s = 0; s < it.n_seg; ++s) {
const int64_t run_tok = (int64_t)p.tok + it.seg_at[(size_t)s];
pos += run_tok - tok; /* the text in front of the run counts up */
const int32_t rh = it.segments.empty() ? it.rows_h : it.seg_h(s) / m;
const int32_t rw = it.segments.empty() ? it.rows_w : it.seg_w(s) / m;
RopeLayout::Run r;
r.tok = (int32_t)run_tok;
r.n = (int32_t)(it.row_at(s + 1) - it.row_at(s));
r.w = rw;
r.base = (int32_t)pos;
r.flat = it.rope_1d;
/* A grid advances the rotary by its longer side; a 1-D run by its length. */
pos += it.rope_1d ? r.n : std::max(rh, rw);
tok = run_tok + r.n;
r.shift = (int32_t)(pos - tok);
r.item = (int32_t)pi;
r.row0 = (int32_t)it.row_at(s);
rope.runs.push_back(r);
}
}
}
} /* namespace mm */
} /* namespace rad */
+9
View File
@@ -318,6 +318,15 @@ public:
int image_from(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const;
int video_from(const Video& v, std::shared_ptr<Item>* out, std::string* why) const;
/* A picture in the video VAE's own form: decoded under the same `max_source_pixels` guard as
* `image` (so any reference the text encoder accepts the encoder accepts too), then scaled —
* aspect kept — so both sides are multiples of `ratio` and neither exceeds `max_side`, the
* single-volume encoder's square bound. Returns the resized RGB; the caller hands it to the
* encoder un-normalised (the VAE applies its own mean/std). `ratio`/`max_side` come from the
* generator's declaration (RadVideoGenDecl enc_ratio / max_enc_side). */
int encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side, Frame* out,
std::string* why) const;
/* Audio, when the program declares an audio encoder; `merge` and `max_frames` are the
* encoder's. */
int init_audio(const AudioConfig& cfg, std::string* why = nullptr);