Archived
core/mm: the Radio image family and video tubelets
Nemotron-H Omni's processor: NVIDIA's size rule for one tile, torch's antialiased bicubic on float pixels, patches in merge-block order, and video in tubelets labelled as the reference labels them. A picture and a video frame share one patch row, so one embedding GEMM serves both. mm_test against golden rows.
This commit is contained in:
1 parent
eda0e92e8e
commit
cc46e3b714
6 files changed
+525
-5
No files matched your search
@@ -3046,6 +3046,18 @@ int Engine::run_server() {
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (cfg_.image_tokens) vc.max_image_tokens = *cfg_.image_tokens;
|
||||
} else if (vc.family == mm::Family::Radio) {
|
||||
/* NVIDIA's processor sizes a picture and a video frame by patch budgets
|
||||
* (min_num_patches..max_num_patches, video_target_num_patches), not pixel bands. */
|
||||
if (cfg_.image_min_pixels || cfg_.image_max_pixels || cfg_.image_tokens ||
|
||||
cfg_.video_min_pixels || cfg_.video_max_pixels || cfg_.video_max_frame_tokens) {
|
||||
RAD_ERR("media: this model's processor sizes images and video frames by its patch "
|
||||
"budgets (min_num_patches, max_num_patches, video_target_num_patches), so "
|
||||
"--image-min/max-pixels, --image-tokens, --video-min/max-pixels and "
|
||||
"--video-max-frame-tokens do nothing on it");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (cfg_.video_min_frames) vc.min_frames = *cfg_.video_min_frames;
|
||||
} else {
|
||||
if (cfg_.image_tokens) {
|
||||
RAD_ERR("media: --image-tokens is a token budget, and this model's processor "
|
||||
@@ -3146,6 +3158,16 @@ int Engine::run_server() {
|
||||
ranks_[0]->program.encoder_name.c_str(), mm_proc->config().image_soft_tokens,
|
||||
mm_proc->config().video_soft_tokens, mm_proc->config().fps,
|
||||
mm_proc->config().max_frames, (long long)encd.max_patches);
|
||||
else if (mm_proc->config().family == mm::Family::Radio)
|
||||
RAD_INFO("media: %s%s through '%s'; an image becomes one tile of %d..%d patches "
|
||||
"(a quarter as many tokens), a video frame about %d patches, %d frames a "
|
||||
"tubelet at %.3g frames a second, at most %d frames, no pruning; %lld "
|
||||
"patches a pass", o.allow_image ? "images" : "",
|
||||
o.allow_video ? " and video" : "", ranks_[0]->program.encoder_name.c_str(),
|
||||
mm_proc->config().radio_min_patches, mm_proc->config().radio_max_patches,
|
||||
mm_proc->config().radio_video_patches, mm_proc->config().radio_tubelet,
|
||||
mm_proc->config().fps, mm_proc->config().max_frames,
|
||||
(long long)encd.max_patches);
|
||||
else
|
||||
RAD_INFO("media: %s%s through '%s'; an image resizes to at most %lld pixels (%lld "
|
||||
"patches a pass), a video to %lld pixels over all its frames",
|
||||
|
||||
+10
-1
@@ -31,7 +31,16 @@ void Item::coords(int64_t first, int64_t count, int32_t* planes) const {
|
||||
while (s + 1 < n_seg && patch_at(s + 1) <= first) ++s;
|
||||
for (int64_t k = 0; k < count; ++k) {
|
||||
while (s + 1 < n_seg && patch_at(s + 1) <= first + k) ++s;
|
||||
const int64_t within = first + k - patch_at(s);
|
||||
int64_t within = first + k - patch_at(s);
|
||||
if (within < seg_prefix) {
|
||||
/* A row the tower fills itself: row -1, the column naming which. */
|
||||
planes[k] = -1;
|
||||
planes[count + k] = (int32_t)within;
|
||||
planes[2 * count + k] = seg_h(s);
|
||||
planes[3 * count + k] = seg_w(s);
|
||||
continue;
|
||||
}
|
||||
within -= seg_prefix;
|
||||
const int64_t bw = seg_w(s) / merge;
|
||||
int64_t row, col;
|
||||
if (patch_raster) {
|
||||
|
||||
@@ -244,6 +244,64 @@ int VisionConfig::from_meta(const RadModelMeta& m, std::string* why) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
/* NVIDIA's Nemotron-H Omni states its tile budget in the processor configs (preprocessor and
|
||||
* processor_config's image_processor carry the same keys) and its token ids in config.json,
|
||||
* except <img> and </img>, which the vocabulary names (Processor::init). */
|
||||
if (mt && !std::strcmp(mt, "radio")) {
|
||||
family = Family::Radio;
|
||||
auto geti = [&](const char* k, int64_t dflt) {
|
||||
const std::string a = std::string("processor.image_processor.") + k;
|
||||
const std::string b = std::string("preprocessor.") + k;
|
||||
return rad_meta_geti(&m, a.c_str(), rad_meta_geti(&m, b.c_str(), dflt));
|
||||
};
|
||||
patch = (int32_t)geti("patch_size", rad_meta_geti(&m, "vision_config.patch_size", 0));
|
||||
const double ratio = rad_meta_getf(&m, "downsample_ratio", 0.0);
|
||||
merge = ratio > 0.0 ? (int32_t)std::lround(1.0 / ratio) : 0;
|
||||
temporal = 1;
|
||||
radio_tubelet = (int32_t)rad_meta_geti(
|
||||
&m, "processor.video_temporal_patch_dim",
|
||||
rad_meta_geti(&m, "video_temporal_patch_size",
|
||||
rad_meta_geti(&m, "vision_config.video_temporal_patch_size", 0)));
|
||||
radio_prefix = (int32_t)(rad_meta_geti(&m, "vision_config.num_cls_tokens", 0) +
|
||||
rad_meta_geti(&m, "vision_config.num_registers", 0));
|
||||
tok_image = (int32_t)rad_meta_geti(&m, "img_context_token_id", -1);
|
||||
if (patch <= 0 || merge <= 0 || radio_tubelet <= 0 || radio_prefix <= 0 || tok_image < 0) {
|
||||
if (why) *why = "the container states no Nemotron-H Omni processor geometry "
|
||||
"(vision_config.patch_size, downsample_ratio, video_temporal_patch_size, "
|
||||
"num_cls_tokens + num_registers, img_context_token_id)";
|
||||
return RAD_E_NOTFOUND;
|
||||
}
|
||||
radio_min_patches = (int32_t)geti("min_num_patches", 1024);
|
||||
radio_max_patches = (int32_t)geti("max_num_patches", 13312);
|
||||
radio_max_len = (int32_t)geti("max_model_len", 16384);
|
||||
radio_video_patches = (int32_t)geti("video_target_num_patches", 1024);
|
||||
if (!read_triple(m, "processor.image_processor.norm_mean", mean) &&
|
||||
!read_triple(m, "preprocessor.norm_mean", mean))
|
||||
read_triple(m, "norm_mean", mean);
|
||||
if (!read_triple(m, "processor.image_processor.norm_std", stdv) &&
|
||||
!read_triple(m, "preprocessor.norm_std", stdv))
|
||||
read_triple(m, "norm_std", stdv);
|
||||
/* The model card's serving default: 2 frames a second, at most 128 (64 tubelets). */
|
||||
fps = 2.0;
|
||||
min_frames = radio_tubelet;
|
||||
max_frames = 128;
|
||||
max_frame_rows = 0;
|
||||
/* The pixel bands only have to be consistent for check(); the tile rule sizes by patches. */
|
||||
const int64_t p2 = (int64_t)patch * patch;
|
||||
image_min_pixels = (int64_t)radio_min_patches * p2;
|
||||
image_max_pixels = std::max<int64_t>(image_min_pixels, (int64_t)radio_max_patches * p2);
|
||||
video_min_pixels = video_max_pixels = (int64_t)radio_video_patches * p2;
|
||||
std::string bad;
|
||||
if (radio_min_patches <= 0 || radio_video_patches <= 0 || radio_max_len <= 0)
|
||||
bad = "min_num_patches, video_target_num_patches and max_model_len must be positive";
|
||||
if (bad.empty() && check(&bad) < 0) bad = "the container's Nemotron-H Omni processor "
|
||||
"configuration is inconsistent: " + bad;
|
||||
if (!bad.empty()) {
|
||||
if (why) *why = bad;
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
/* Gemma 4 12B ("unified") has no tower: its embedder takes a whole 48 x 48 pooling window as
|
||||
* one patch, so a patch is a row (merge 1), and its values are u / 255. The sizes are the
|
||||
* same: a window is 48 pixels a side either way. */
|
||||
@@ -977,6 +1035,134 @@ void resize_bicubic_torch(const Frame& in, int32_t w, int32_t h, Frame* out) {
|
||||
resize_bicubic_impl(in, w, h, out, true);
|
||||
}
|
||||
|
||||
/* Python's round(): the nearest integer, a tie to the even one. The default floating-point
|
||||
* environment rounds that way, which nearbyint follows. */
|
||||
static inline int64_t py_round(double x) { return (int64_t)std::nearbyint(x); }
|
||||
|
||||
void radio_size(int64_t w, int64_t h, const VisionConfig& c, int32_t* wp, int32_t* hp) {
|
||||
const int64_t div = c.merge, lo = c.radio_min_patches;
|
||||
const int64_t hi = c.radio_max_patches > 0 ? c.radio_max_patches : INT64_MAX;
|
||||
const int64_t budget = std::max<int64_t>((int64_t)(c.radio_max_len - 4) * div * div, lo);
|
||||
const int64_t avail = std::max<int64_t>(std::min<int64_t>(budget, hi), lo);
|
||||
/* round(x + 0.5): x + 1 rounded down for a fraction, and for a whole x, x or x + 1 by parity
|
||||
* (Python's tie to even), which the reference's comment does not say but its code does. */
|
||||
const int64_t ch = py_round((double)h / (double)c.patch + 0.5);
|
||||
const int64_t cw = py_round((double)w / (double)c.patch + 0.5);
|
||||
const double factor = std::min(std::sqrt((double)avail / (double)(ch * cw)), 1.0);
|
||||
int64_t th = (int64_t)std::floor(factor * (double)ch);
|
||||
int64_t tw = (int64_t)std::floor(factor * (double)cw);
|
||||
if (avail > lo && th * tw < lo) {
|
||||
const double up = std::sqrt((double)lo / (double)(th * tw));
|
||||
th = (int64_t)std::ceil(up * (double)th);
|
||||
tw = (int64_t)std::ceil(up * (double)tw);
|
||||
}
|
||||
if (const int64_t r = th % div) th = (th + div - r) * tw <= avail ? th + div - r
|
||||
: std::max(div, th - r);
|
||||
if (const int64_t r = tw % div) tw = th * (tw + div - r) <= avail ? tw + div - r
|
||||
: std::max(div, tw - r);
|
||||
*wp = (int32_t)tw;
|
||||
*hp = (int32_t)th;
|
||||
}
|
||||
|
||||
void radio_video_size(int64_t w, int64_t h, const VisionConfig& c, int32_t* wp, int32_t* hp) {
|
||||
const int64_t div = c.merge, target = c.radio_video_patches;
|
||||
const double aspect = (double)w / (double)std::max<int64_t>(h, 1);
|
||||
int64_t ph = std::max<int64_t>(py_round(std::sqrt((double)target / aspect)), 1);
|
||||
int64_t pw = std::max<int64_t>(py_round(std::sqrt((double)target * aspect)), 1);
|
||||
if (div > 1) {
|
||||
const int64_t rh = ph % div, rw = pw % div;
|
||||
const int64_t phu = ph + (rh ? div - rh : 0), pwu = pw + (rw ? div - rw : 0);
|
||||
if (phu * pwu <= target) {
|
||||
ph = phu;
|
||||
pw = pwu;
|
||||
} else {
|
||||
ph = std::max(div, ph - rh);
|
||||
pw = std::max(div, pw - rw);
|
||||
}
|
||||
}
|
||||
*wp = (int32_t)pw;
|
||||
*hp = (int32_t)ph;
|
||||
}
|
||||
|
||||
/* ATen's antialiased bicubic (Keys, a = -0.5) on float, one axis: output i reads inputs
|
||||
* [lo, lo + n) at the filter's normalised weights, the support widened by the scale when
|
||||
* shrinking. */
|
||||
namespace {
|
||||
struct AaTaps { std::vector<int32_t> lo, n; std::vector<float> w; int32_t width = 0; };
|
||||
inline float aa_cubic(float x) {
|
||||
const float a = -0.5f;
|
||||
x = std::fabs(x);
|
||||
if (x < 1.0f) return ((a + 2.0f) * x - (a + 3.0f)) * x * x + 1.0f;
|
||||
if (x < 2.0f) return ((a * x - 5.0f * a) * x + 8.0f * a) * x - 4.0f * a;
|
||||
return 0.0f;
|
||||
}
|
||||
AaTaps aa_taps_f32(int32_t in, int32_t out) {
|
||||
AaTaps t;
|
||||
const float scale = (float)in / (float)out;
|
||||
const float support = scale >= 1.0f ? 2.0f * scale : 2.0f;
|
||||
const float inv = scale >= 1.0f ? 1.0f / scale : 1.0f;
|
||||
t.width = (int32_t)std::ceil(support) * 2 + 1;
|
||||
t.lo.resize((size_t)out);
|
||||
t.n.resize((size_t)out);
|
||||
t.w.assign((size_t)out * t.width, 0.0f);
|
||||
for (int32_t i = 0; i < out; ++i) {
|
||||
const float center = scale * ((float)i + 0.5f);
|
||||
const int32_t lo = std::max((int32_t)(center - support + 0.5f), 0);
|
||||
const int32_t n = std::min((int32_t)(center + support + 0.5f), in) - lo;
|
||||
float* w = &t.w[(size_t)i * t.width];
|
||||
float total = 0.0f;
|
||||
for (int32_t j = 0; j < n && j < t.width; ++j) {
|
||||
w[j] = aa_cubic(((float)(j + lo) - center + 0.5f) * inv);
|
||||
total += w[j];
|
||||
}
|
||||
for (int32_t j = 0; j < n && j < t.width; ++j) w[j] = total != 0.0f ? w[j] / total : 0.0f;
|
||||
t.lo[(size_t)i] = lo;
|
||||
t.n[(size_t)i] = std::min(n, t.width);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
void resize_bicubic_aa_f32(const Frame& in, int32_t w, int32_t h, std::vector<float>* out) {
|
||||
std::vector<float> src((size_t)in.w * in.h * 3);
|
||||
for (size_t i = 0; i < src.size(); ++i) src[i] = (float)in.rgb[i];
|
||||
/* Width first, then height; a side that keeps its size is skipped (its taps are the identity). */
|
||||
std::vector<float> mid;
|
||||
const std::vector<float>* cur = &src;
|
||||
int32_t cw = in.w;
|
||||
if (w != in.w) {
|
||||
const AaTaps t = aa_taps_f32(in.w, w);
|
||||
mid.assign((size_t)w * in.h * 3, 0.0f);
|
||||
for (int32_t y = 0; y < in.h; ++y)
|
||||
for (int32_t x = 0; x < w; ++x)
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
float acc = 0.0f;
|
||||
const float* wt = &t.w[(size_t)x * t.width];
|
||||
for (int32_t j = 0; j < t.n[(size_t)x]; ++j)
|
||||
acc += src[((size_t)y * in.w + t.lo[(size_t)x] + j) * 3 + c] * wt[j];
|
||||
mid[((size_t)y * w + x) * 3 + c] = acc;
|
||||
}
|
||||
cur = ∣
|
||||
cw = w;
|
||||
}
|
||||
if (h != in.h) {
|
||||
const AaTaps t = aa_taps_f32(in.h, h);
|
||||
out->assign((size_t)cw * h * 3, 0.0f);
|
||||
for (int32_t y = 0; y < h; ++y) {
|
||||
const float* wt = &t.w[(size_t)y * t.width];
|
||||
for (int32_t x = 0; x < cw; ++x)
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
float acc = 0.0f;
|
||||
for (int32_t j = 0; j < t.n[(size_t)y]; ++j)
|
||||
acc += (*cur)[((size_t)(t.lo[(size_t)y] + j) * cw + x) * 3 + c] * wt[j];
|
||||
(*out)[((size_t)y * cw + x) * 3 + c] = acc;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
*out = *cur;
|
||||
}
|
||||
|
||||
void cap_area(const Frame& in, int64_t max_pixels, Frame* out) {
|
||||
if (max_pixels <= 0 || (int64_t)in.w * in.h <= max_pixels) { *out = in; return; }
|
||||
const double scale = std::sqrt((double)max_pixels / ((double)in.w * in.h));
|
||||
@@ -1108,6 +1294,55 @@ void patchify_gemma4(const VisionConfig& c, const std::vector<const Frame*>& fra
|
||||
}
|
||||
}
|
||||
|
||||
/* Radio's patch rows (Family::Radio), segment by segment: `n_prefix` zero rows, then the patches
|
||||
* in merge-block order. A row is [the tubelet's frames (frame, channel, y, x) | the picture's
|
||||
* (channel, y, x)]: a picture fills the second part, a video tubelet the first, the rest zero.
|
||||
* Each value is (u / 255 - mean) / std in f32 on the resized float pixels, rounded to bf16, as the
|
||||
* reference processor's tensor ops and the model's cast to bf16 give it. `frames` holds
|
||||
* grid_t * tubelet frames of [grid_h * patch, grid_w * patch, 3] floats for a video, one for a
|
||||
* picture. */
|
||||
void patchify_radio(const VisionConfig& c, const std::vector<const std::vector<float>*>& frames,
|
||||
bool video, Item* it) {
|
||||
const int P = c.patch, M = c.merge, T = c.radio_tubelet;
|
||||
const int gh = it->grid_h, gw = it->grid_w, W = gw * P;
|
||||
const int64_t dim = it->patch_dim, pp = 3LL * P * P;
|
||||
it->pixels.assign((size_t)it->n_patch * (size_t)dim, 0);
|
||||
uint16_t* dst = it->pixels.data();
|
||||
auto norm = [&](float u, int ch) {
|
||||
return rad_f32_to_bf16((u / 255.0f - c.mean[ch]) / c.stdv[ch]);
|
||||
};
|
||||
for (int g = 0; g < it->grid_t; ++g) {
|
||||
dst += (size_t)it->seg_prefix * dim;
|
||||
for (int bh = 0; bh < gh / M; ++bh)
|
||||
for (int bw = 0; bw < gw / M; ++bw)
|
||||
for (int mh = 0; mh < M; ++mh)
|
||||
for (int mw = 0; mw < M; ++mw) {
|
||||
const int py0 = (bh * M + mh) * P, px0 = (bw * M + mw) * P;
|
||||
const int nf = video ? T : 1;
|
||||
uint16_t* o = dst + (video ? 0 : (int64_t)T * pp);
|
||||
for (int t = 0; t < nf; ++t) {
|
||||
const std::vector<float>& f = *frames[(size_t)(g * nf + t)];
|
||||
for (int ch = 0; ch < 3; ++ch)
|
||||
for (int y = 0; y < P; ++y)
|
||||
for (int x = 0; x < P; ++x)
|
||||
*o++ = norm(f[((size_t)(py0 + y) * W + px0 + x) * 3 + ch], ch);
|
||||
}
|
||||
dst += dim;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Radio's item geometry: finish_item's, with the prefix rows counted in the patches. */
|
||||
void finish_item_radio(const VisionConfig& c, Item* it) {
|
||||
it->patch_dim = c.patch_dim();
|
||||
it->seg_prefix = c.radio_prefix;
|
||||
it->rows_h = it->grid_h / c.merge;
|
||||
it->rows_w = it->grid_w / c.merge;
|
||||
it->seg_rows = it->rows_h * it->rows_w;
|
||||
it->n_seg = it->grid_t;
|
||||
it->n_patch = (int64_t)it->grid_t * (c.radio_prefix + (int64_t)it->grid_h * it->grid_w);
|
||||
}
|
||||
|
||||
void finish_item(const VisionConfig& c, Item* it) {
|
||||
it->patch_dim = c.patch_dim();
|
||||
it->n_patch = (int64_t)it->grid_t * it->grid_h * it->grid_w;
|
||||
@@ -1161,6 +1396,26 @@ int Processor::init(const VisionConfig& cfg,
|
||||
encode_text_ = std::move(encode_text);
|
||||
return RAD_OK;
|
||||
}
|
||||
if (cfg.family == Family::Radio) {
|
||||
/* The markers around a run are vocabulary words, not config ids. A tile is one segment of
|
||||
* one pass with its prefix rows, so the picture budget is lowered to what a pass holds;
|
||||
* a video frame's target must fit as it is. */
|
||||
if (!token_id) return RAD_E_INVAL;
|
||||
cfg_.tok_start = token_id("<img>");
|
||||
cfg_.tok_end = token_id("</img>");
|
||||
if (cfg_.tok_start < 0 || cfg_.tok_end < 0) return RAD_E_NOTFOUND;
|
||||
const int64_t cap = cfg.max_patches - cfg.radio_prefix;
|
||||
if (cap < cfg.radio_min_patches || cap < cfg.radio_video_patches) return RAD_E_INVAL;
|
||||
if (cfg_.radio_max_patches > cap) cfg_.radio_max_patches = (int32_t)cap;
|
||||
/* A video's budget: max_frames frames in tubelets, each its prefix plus the frame target
|
||||
* (the video tile never exceeds it). */
|
||||
if (cfg_.max_video_patches <= 0)
|
||||
cfg_.max_video_patches = (int64_t)((cfg.max_frames + cfg.radio_tubelet - 1) /
|
||||
cfg.radio_tubelet) *
|
||||
(cfg.radio_prefix + cfg.radio_video_patches);
|
||||
encode_text_ = std::move(encode_text);
|
||||
return RAD_OK;
|
||||
}
|
||||
if (cfg.family == Family::Glm5Next) {
|
||||
/* A token is merge^2 patches and an image is one segment of one pass, so the budget is
|
||||
* lowered to what a pass holds. */
|
||||
@@ -1720,6 +1975,99 @@ int Processor::image_pixtral(const Frame& f, std::shared_ptr<Item>* out, std::st
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* NVIDIA's Nemotron-H Omni image processor: one tile, sized by radio_size, resized with torch's
|
||||
* antialiased bicubic on float pixels (none when the size already fits). */
|
||||
int Processor::image_radio(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const {
|
||||
const VisionConfig& c = cfg_;
|
||||
int32_t wp = 0, hp = 0;
|
||||
radio_size(f.w, f.h, c, &wp, &hp);
|
||||
std::vector<float> px;
|
||||
resize_bicubic_aa_f32(f, wp * c.patch, hp * c.patch, &px);
|
||||
auto it = std::make_shared<Item>();
|
||||
it->kind = Kind::Image;
|
||||
it->grid_t = 1;
|
||||
it->grid_h = hp;
|
||||
it->grid_w = wp;
|
||||
it->rope_1d = true;
|
||||
finish_item_radio(c, it.get());
|
||||
if (it->n_patch > c.max_patches) {
|
||||
if (why) *why = "the image resizes to more patches than one encoder pass holds";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
patchify_radio(c, { &px }, false, it.get());
|
||||
one_run(it.get(), c.tok_start, it->seg_rows, c.tok_image, c.tok_end);
|
||||
*out = std::move(it);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Its video path: every frame at the video tile (radio_video_size of the first), `tubelet`
|
||||
* frames a segment (the last repeated to fill one), each run labelled as the reference processor
|
||||
* labels a tubelet -- "Frame i sampled at T seconds and frame i+1 sampled at T' seconds: ", T the
|
||||
* frame's source index times int(1000 / fps) ms -- the runs joined by a newline. No pruning (EVS):
|
||||
* every tubelet keeps all its rows. */
|
||||
int Processor::video_radio(const Video& v, std::shared_ptr<Item>* out, std::string* why) const {
|
||||
const VisionConfig& c = cfg_;
|
||||
if (v.frames.empty()) { if (why) *why = "the video decoded to no frames"; return RAD_E_INVAL; }
|
||||
int32_t wp = 0, hp = 0;
|
||||
radio_video_size(v.frames[0].w, v.frames[0].h, c, &wp, &hp);
|
||||
const int32_t T = c.radio_tubelet;
|
||||
const int64_t nf = (int64_t)v.frames.size();
|
||||
auto it = std::make_shared<Item>();
|
||||
it->kind = Kind::Video;
|
||||
it->grid_t = (int32_t)((nf + T - 1) / T);
|
||||
it->grid_h = hp;
|
||||
it->grid_w = wp;
|
||||
it->rope_1d = true;
|
||||
finish_item_radio(c, it.get());
|
||||
if (c.radio_prefix + (int64_t)hp * wp > c.max_patches) {
|
||||
if (why) *why = "a video frame resizes to more patches than one encoder pass holds";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (it->n_patch > c.max_video_patches) {
|
||||
if (why) *why = "the video is over the server's patch budget for one video";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
std::vector<std::vector<float>> resized((size_t)nf);
|
||||
for (int64_t i = 0; i < nf; ++i)
|
||||
resize_bicubic_aa_f32(v.frames[(size_t)i], wp * c.patch, hp * c.patch, &resized[(size_t)i]);
|
||||
std::vector<const std::vector<float>*> frames;
|
||||
for (int64_t i = 0; i < (int64_t)it->grid_t * T; ++i)
|
||||
frames.push_back(&resized[(size_t)std::min(i, nf - 1)]);
|
||||
patchify_radio(c, frames, true, it.get());
|
||||
content_key(*it, &it->key_hi, &it->key_lo);
|
||||
|
||||
const int64_t frame_ms = v.info.fps > 0.0 ? (int64_t)(1000.0 / v.info.fps) : 0;
|
||||
for (int32_t g = 0; g < it->grid_t; ++g) {
|
||||
std::string label = g > 0 ? "\n" : "";
|
||||
for (int32_t j = 0; j < T; ++j) {
|
||||
const int64_t fi = (int64_t)g * T + j;
|
||||
if (fi >= nf) break;
|
||||
char part[96];
|
||||
if (frame_ms > 0)
|
||||
std::snprintf(part, sizeof part, "%s%s %lld sampled at %.2f seconds",
|
||||
j ? " and " : "", j ? "frame" : "Frame", (long long)(fi + 1),
|
||||
(double)(v.index[(size_t)fi] * frame_ms) / 1000.0);
|
||||
else
|
||||
std::snprintf(part, sizeof part, "%s%s %lld", j ? " and " : "",
|
||||
j ? "frame" : "Frame", (long long)(fi + 1));
|
||||
label += part;
|
||||
}
|
||||
label += ": ";
|
||||
const std::vector<int32_t> tt = encode_text_ ? encode_text_(label) : std::vector<int32_t>{};
|
||||
if (tt.empty()) {
|
||||
if (why) *why = "the tokenizer produced nothing for a video frame label";
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
it->expand.insert(it->expand.end(), tt.begin(), tt.end());
|
||||
it->expand.push_back(c.tok_start);
|
||||
it->seg_at.push_back((int32_t)it->expand.size());
|
||||
it->expand.insert(it->expand.end(), (size_t)it->seg_rows, c.tok_image);
|
||||
it->expand.push_back(c.tok_end);
|
||||
}
|
||||
*out = std::move(it);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Processor::image_from(const Frame& f, std::shared_ptr<Item>* out, std::string* why,
|
||||
const ImageMode& mode) const {
|
||||
const VisionConfig& c = cfg_;
|
||||
@@ -1731,6 +2079,7 @@ int Processor::image_from(const Frame& f, std::shared_ptr<Item>* out, std::strin
|
||||
if (c.family == Family::Pixtral) return image_pixtral(f, out, why);
|
||||
if (c.family == Family::Lfm2) return image_lfm2(f, out, why);
|
||||
if (c.family == Family::Glm5Next) return image_glm5next(f, out, why);
|
||||
if (c.family == Family::Radio) return image_radio(f, out, why);
|
||||
if (c.family == Family::UnlimitedOcr) return image_unlimitedocr(f, mode, out, why);
|
||||
int64_t h = 0, w = 0;
|
||||
RAD_TRY(smart_resize_image(f.h, f.w, c.factor(), c.image_min_pixels, c.image_max_pixels,
|
||||
@@ -1824,6 +2173,7 @@ int Processor::video_gemma4(const Video& v, std::shared_ptr<Item>* out, std::str
|
||||
int Processor::video_from(const Video& v, std::shared_ptr<Item>* out, std::string* why) const {
|
||||
const VisionConfig& c = cfg_;
|
||||
if (c.family == Family::Gemma4) return video_gemma4(v, out, why);
|
||||
if (c.family == Family::Radio) return video_radio(v, out, why);
|
||||
if (c.family == Family::Glm5Next || c.family == Family::Pixtral) {
|
||||
if (why) *why = "this model's processor takes pictures, not video";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
|
||||
+39
-4
@@ -81,8 +81,19 @@ enum class Kind { Image, Video, Audio };
|
||||
* Pixtral Nemotron 3.5's noncausal Mistral3 wrapper: longest-edge resize, then ceil each side
|
||||
* to patch * merge; PIL bicubic and CLIP normalization. Raster (channel, y, x) patches;
|
||||
* image placeholders per merged grid row, each followed by IMG_BREAK (last: IMG_END).
|
||||
* Every token has ordinary 1-D positions, and separators retain their token embedding. */
|
||||
enum class Family { QwenVL, Gemma4, Lfm2, UnlimitedOcr, Glm5Next, Pixtral };
|
||||
* Every token has ordinary 1-D positions, and separators retain their token embedding.
|
||||
* Radio NVIDIA's Nemotron-H Omni with C-RADIO: a picture becomes one tile whose patch grid
|
||||
* falls between min_num_patches and max_num_patches, aspect ratio kept and both sides
|
||||
* even (the pixel shuffle's factor), resized with torch's antialiased bicubic on float
|
||||
* pixels; a video frame the same at about video_target_num_patches. Values are
|
||||
* (u / 255 - mean) / std in f32, rounded to bf16. A patch row is the tubelet's frames
|
||||
* (frame, channel, y, x) and then the picture's (channel, y, x), the part not in use
|
||||
* zero, so one embedding GEMM serves both projections. Each segment carries `n_prefix`
|
||||
* zero rows ahead of its patches, which the tower fills with its class and register
|
||||
* tokens. Pictures are spelled <img> placeholders </img>; a video one such run a
|
||||
* tubelet, each after its "Frame i sampled at T seconds and frame i+1 ...: " label,
|
||||
* runs joined by a newline. Tokens at their positions (NoPE). */
|
||||
enum class Family { QwenVL, Gemma4, Lfm2, UnlimitedOcr, Glm5Next, Pixtral, Radio };
|
||||
|
||||
struct VisionConfig {
|
||||
Family family = Family::QwenVL;
|
||||
@@ -108,6 +119,11 @@ struct VisionConfig {
|
||||
/* Lfm2: the tiling (see Family). `max_segment_patches` is the tower's longest segment. */
|
||||
int32_t min_tiles = 0, max_tiles = 0, tile = 0;
|
||||
bool use_thumbnail = false;
|
||||
/* Radio: a picture's patch budget [radio_min_patches, radio_max_patches] (with the global cap
|
||||
* radio_max_len tokens), the per-frame target of a video, the frames a tubelet packs and the
|
||||
* learned rows (class and register tokens) a segment carries ahead of its patches. */
|
||||
int32_t radio_min_patches = 0, radio_max_patches = 0, radio_max_len = 0;
|
||||
int32_t radio_video_patches = 0, radio_tubelet = 0, radio_prefix = 0;
|
||||
double max_pixels_tolerance = 0.0;
|
||||
int64_t max_segment_patches = 0;
|
||||
|
||||
@@ -139,7 +155,11 @@ struct VisionConfig {
|
||||
* engine again after command-line overrides. */
|
||||
int check(std::string* why) const;
|
||||
|
||||
int64_t patch_dim() const { return 3LL * temporal * patch * patch; }
|
||||
int64_t patch_dim() const {
|
||||
/* Radio's row is a tubelet's frames and then the picture's patch (Family::Radio). */
|
||||
if (family == Family::Radio) return 3LL * (1 + radio_tubelet) * patch * patch;
|
||||
return 3LL * temporal * patch * patch;
|
||||
}
|
||||
int32_t factor() const { return patch * merge; }
|
||||
|
||||
/* Read from the container; refuses one with no vision tower. Processor-only keys the
|
||||
@@ -264,6 +284,10 @@ struct Item {
|
||||
/* Patches are in raster order (row by row), not merge-block order: the coordinate planes
|
||||
* invert the one or the other (Unlimited-OCR's tower reads a picture patch by patch). */
|
||||
bool patch_raster = false;
|
||||
/* Rows ahead of each segment's patches that the encoder fills itself (Family::Radio: C-RADIO's
|
||||
* class and register tokens): zero pixels, coordinate (-1, k), counted in patch_at and
|
||||
* n_patch but never in a segment's output rows. */
|
||||
int32_t seg_prefix = 0;
|
||||
|
||||
int32_t seg_h(int32_t s) const { return segments.empty() ? grid_h : segments[(size_t)s].h; }
|
||||
int32_t seg_w(int32_t s) const { return segments.empty() ? grid_w : segments[(size_t)s].w; }
|
||||
@@ -272,7 +296,7 @@ struct Item {
|
||||
bool seg_is_sep(int32_t s) const { return !segments.empty() && segments[(size_t)s].sep; }
|
||||
/* Segment s's first patch and first output row; s == n_seg is the end of the item. */
|
||||
int64_t patch_at(int32_t s) const {
|
||||
if (segments.empty()) return (int64_t)s * grid_h * grid_w;
|
||||
if (segments.empty()) return (int64_t)s * (seg_prefix + (int64_t)grid_h * grid_w);
|
||||
return s >= n_seg ? n_patch : segments[(size_t)s].patch0;
|
||||
}
|
||||
int64_t row_at(int32_t s) const {
|
||||
@@ -431,6 +455,8 @@ private:
|
||||
std::string* why) const;
|
||||
int image_glm5next(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const;
|
||||
int image_pixtral(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const;
|
||||
int image_radio(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const;
|
||||
int video_radio(const Video& v, std::shared_ptr<Item>* out, std::string* why) const;
|
||||
/* Lfm2's markers: <|img_thumbnail|>, and <|img_row_R_col_C|> at (R-1) * max_tiles + C-1. */
|
||||
int32_t tok_thumbnail_ = -1;
|
||||
std::vector<int32_t> tok_tiles_;
|
||||
@@ -468,6 +494,15 @@ std::vector<int64_t> sample_frames_gemma4(const VideoInfo& info, double fps, int
|
||||
* within `max_patches`, aspect ratio kept (get_aspect_ratio_preserving_size). */
|
||||
int gemma4_size(int64_t h, int64_t w, int32_t patch, int32_t pool, int64_t max_patches,
|
||||
int64_t* oh, int64_t* ow, std::string* why);
|
||||
/* Radio's tile for a picture of w x h pixels (NVIDIA's _compute_target_patches, Python's
|
||||
* round-half-even included) and for a video frame (_compute_target_patches_video), in patches. */
|
||||
void radio_size(int64_t w, int64_t h, const VisionConfig& c, int32_t* wp, int32_t* hp);
|
||||
void radio_video_size(int64_t w, int64_t h, const VisionConfig& c, int32_t* wp, int32_t* hp);
|
||||
/* torch's F.interpolate(mode="bicubic", align_corners=False, antialias=True) of packed RGB bytes
|
||||
* taken as float, without rounding or clamping: [h, w, 3] f32 in 0..255 give or take the
|
||||
* filter's overshoot. Separable, width first, as ATen's CPU kernel; a side that keeps its size is
|
||||
* the identity. */
|
||||
void resize_bicubic_aa_f32(const Frame& in, int32_t w, int32_t h, std::vector<float>* out);
|
||||
/* PIL's antialiased bicubic resample of packed RGB, bit for bit: fixed-point coefficients, a
|
||||
* horizontal pass into an 8-bit intermediate and then a vertical one. */
|
||||
void resize_bicubic(const Frame& in, int32_t w, int32_t h, Frame* out);
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "gemma4_golden.h"
|
||||
#include "gemma4u_golden.h"
|
||||
#include "glm5_golden.h"
|
||||
#include "radio_golden.h"
|
||||
#include "imaging_golden.h"
|
||||
#include "lfm2_golden.h"
|
||||
#include "mm/resample.h"
|
||||
@@ -585,6 +586,62 @@ std::vector<uint16_t> raster(const Item& it, int32_t s) {
|
||||
|
||||
} /* namespace */
|
||||
|
||||
namespace {
|
||||
VisionConfig radio_cfg() {
|
||||
VisionConfig c;
|
||||
c.family = Family::Radio;
|
||||
c.patch = kRadioPatch;
|
||||
c.merge = kRadioMerge;
|
||||
c.temporal = 1;
|
||||
c.radio_min_patches = kRadioMinP;
|
||||
c.radio_max_patches = kRadioMaxP;
|
||||
c.radio_max_len = kRadioMaxLen;
|
||||
c.radio_video_patches = kRadioVideoP;
|
||||
c.radio_tubelet = 2;
|
||||
c.radio_prefix = 10;
|
||||
return c;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
/* NVIDIA's tile rule, Python's round-half-even included (a side that is an odd multiple of 16
|
||||
* takes one patch more than an even one), for pictures and video frames. */
|
||||
TEST(radio_sizes_are_the_references) {
|
||||
const VisionConfig c = radio_cfg();
|
||||
for (const GoldRadioSize& g : kGoldRadioSize) {
|
||||
int32_t pw = 0, ph = 0, vw = 0, vh = 0;
|
||||
radio_size(g.w, g.h, c, &pw, &ph);
|
||||
radio_video_size(g.w, g.h, c, &vw, &vh);
|
||||
if (pw != g.pw || ph != g.ph || vw != g.vw || vh != g.vh)
|
||||
std::fprintf(stderr, " %lld x %lld: tile %d x %d, frame %d x %d; reference %d x %d, "
|
||||
"%d x %d\n", (long long)g.w, (long long)g.h, pw, ph, vw, vh, g.pw, g.ph,
|
||||
g.vw, g.vh);
|
||||
CHECK_EQ(pw, g.pw);
|
||||
CHECK_EQ(ph, g.ph);
|
||||
CHECK_EQ(vw, g.vw);
|
||||
CHECK_EQ(vh, g.vh);
|
||||
}
|
||||
}
|
||||
|
||||
/* torch's antialiased bicubic on float pixels, shrinking and growing, overshoot kept. */
|
||||
TEST(radio_resize_is_torchs_antialiased_bicubic_on_floats) {
|
||||
Frame f;
|
||||
f.w = 7;
|
||||
f.h = 5;
|
||||
f.rgb.resize(7 * 5 * 3);
|
||||
for (int y = 0; y < 5; ++y)
|
||||
for (int x = 0; x < 7; ++x)
|
||||
for (int c = 0; c < 3; ++c)
|
||||
f.rgb[((size_t)y * 7 + x) * 3 + c] = (uint8_t)((37 * x + 11 * y + 53 * c) % 256);
|
||||
struct Case { int w, h; const float* want; };
|
||||
const Case cases[] = { { 4, 3, kGoldRadioAa43 }, { 9, 6, kGoldRadioAa96 } };
|
||||
for (const Case& k : cases) {
|
||||
std::vector<float> out;
|
||||
resize_bicubic_aa_f32(f, k.w, k.h, &out);
|
||||
REQUIRE_EQ(out.size(), (size_t)(k.w * k.h * 3));
|
||||
for (size_t i = 0; i < out.size(); ++i) CHECK_NEAR(out[i], k.want[i], 2e-3);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(gemma4_sizes_are_the_references) {
|
||||
for (const GoldGemma4Size& g : kGoldGemma4Size) {
|
||||
int64_t oh = 0, ow = 0;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/* radio_golden.h -- from NVIDIA's image_processing_nemotron_h_omni.py (its _compute_target_patches
|
||||
* and _compute_target_patches_video, verbatim, at the checkpoint's preprocessor_config.json) and
|
||||
* torch 2.11's F.interpolate(bicubic, align_corners=False, antialias=True); do not edit. */
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
static const int32_t kRadioPatch = 16, kRadioMerge = 2, kRadioMinP = 1024, kRadioMaxP = 13312;
|
||||
static const int32_t kRadioMaxLen = 16384, kRadioVideoP = 1024;
|
||||
|
||||
/* A w x h picture's tile (pw x ph patches) and a video frame's (vw x vh). */
|
||||
struct GoldRadioSize { int64_t w, h; int32_t pw, ph, vw, vh; };
|
||||
static const GoldRadioSize kGoldRadioSize[] = {
|
||||
{ 1, 1, 32, 32, 32, 32 },
|
||||
{ 16, 16, 32, 32, 32, 32 },
|
||||
{ 48, 48, 32, 32, 32, 32 },
|
||||
{ 800, 400, 50, 26, 44, 22 },
|
||||
{ 768, 512, 48, 32, 38, 26 },
|
||||
{ 800, 560, 50, 36, 38, 26 },
|
||||
{ 896, 640, 56, 40, 38, 26 },
|
||||
{ 640, 720, 40, 46, 30, 34 },
|
||||
{ 1920, 1080, 120, 68, 42, 24 },
|
||||
{ 1080, 1920, 68, 120, 24, 42 },
|
||||
{ 3840, 2160, 154, 86, 42, 24 },
|
||||
{ 4000, 3000, 132, 100, 36, 28 },
|
||||
{ 512, 512, 32, 32, 32, 32 },
|
||||
{ 496, 496, 32, 32, 32, 32 },
|
||||
{ 500, 333, 40, 26, 38, 26 },
|
||||
{ 4096, 64, 256, 4, 256, 4 },
|
||||
{ 64, 4096, 4, 256, 4, 256 },
|
||||
{ 1000, 1000, 64, 64, 32, 32 },
|
||||
{ 1600, 48, 160, 8, 184, 6 },
|
||||
{ 33, 17, 40, 28, 44, 22 },
|
||||
{ 2048, 2048, 116, 114, 32, 32 },
|
||||
{ 1280, 720, 80, 46, 42, 24 },
|
||||
{ 720, 1280, 46, 80, 24, 42 },
|
||||
{ 240, 160, 42, 26, 38, 26 },
|
||||
{ 2560, 1440, 154, 86, 42, 24 },
|
||||
{ 1824, 1824, 114, 114, 32, 32 },
|
||||
{ 1840, 1840, 116, 114, 32, 32 },
|
||||
};
|
||||
|
||||
/* A 7 x 5 picture, channel c of pixel (x, y) = (37 x + 11 y + 53 c) mod 256, resized as float to
|
||||
* 4 x 3 and to 9 x 6: [h, w, 3]. Shrinking widens the filter; growing overshoots 0..255. */
|
||||
static const float kGoldRadioAa43[] = {
|
||||
18.68736f, 71.68737f, 124.68736f, 80.90739f, 133.72572f, 194.65350f, 148.87723f, 213.02129f, 152.70291f, 211.09726f, 126.56811f, 55.45514f, 36.79506f, 89.79505f, 142.43031f, 99.01508f, 156.68340f, 218.53284f, 166.63049f, 187.91020f, 85.37108f, 234.45689f, 48.12016f, 68.77328f, 54.90274f, 107.90273f, 166.63322f, 117.12276f, 176.20847f, 186.10544f, 190.66121f, 193.38731f, 58.40165f, 164.79666f, 38.00973f, 93.24097f };
|
||||
static const float kGoldRadioAa96[] = {
|
||||
-2.15752f, 50.84248f, 103.84248f, 22.05824f, 75.05824f, 128.05823f, 53.03841f, 106.03841f, 159.03842f, 81.81619f, 134.81619f, 187.63474f, 110.59396f, 163.59395f, 216.59395f, 139.37173f, 192.37170f, 271.01746f, 168.14951f, 238.70782f, 133.00426f, 199.12965f, 169.84392f, 30.16899f, 223.34544f, 8.22708f, 73.34544f, 6.05768f, 59.05768f, 112.05769f, 30.27345f, 83.27344f, 136.27345f, 61.25361f, 114.25362f, 167.25362f, 90.03139f, 143.03140f, 199.85938f, 118.80916f, 171.80916f, 224.80916f, 147.58691f, 199.40474f, 92.79315f, 176.36472f, 258.14157f, 39.83767f, 207.34486f, 191.24995f, 53.29689f, 231.56065f, 15.60972f, 81.56065f, 15.66519f, 68.66520f, 121.66520f, 39.88095f, 92.88096f, 145.88097f, 70.86112f, 123.86114f, 176.86115f, 99.63892f, 152.63893f, 210.80418f, 128.41669f, 181.41669f, 234.41670f, 157.19443f, 220.58044f, 40.21658f, 185.97224f, 157.96931f, 15.63081f, 216.95239f, 71.77780f, 67.87831f, 241.16818f, 33.36428f, 91.16816f, 24.83185f, 77.83186f, 130.83186f, 49.04762f, 102.04762f, 155.04762f, 80.02779f, 133.02780f, 185.31654f, 108.80557f, 161.80557f, 222.22914f, 137.58333f, 190.58334f, 256.54630f, 166.36110f, 237.78807f, 63.46646f, 194.24980f, 90.82895f, 30.38097f, 230.28572f, -8.77776f, 76.11905f, 263.91144f, 48.19390f, 100.33482f, 34.43936f, 87.43935f, 140.43936f, 58.65512f, 111.65513f, 164.65514f, 89.63529f, 142.63531f, 198.74486f, 118.41308f, 171.41309f, 218.36819f, 147.19084f, 200.19084f, 196.51909f, 175.96861f, 246.17574f, 59.60552f, 208.63335f, 112.01243f, 43.80931f, 217.51062f, 14.44085f, 85.72655f, 200.58788f, 56.94232f, 109.94233f, 42.65456f, 95.65456f, 148.65459f, 66.87033f, 119.87034f, 172.87033f, 97.85049f, 150.85052f, 218.41563f, 126.62828f, 179.62830f, 186.20245f, 155.40605f, 208.40605f, -4.04360f, 184.18382f, 254.39093f, 27.43979f, 231.16805f, 120.22764f, 63.48009f, 158.61864f, 22.65605f, 93.94176f, -9.85780f, 65.15753f, 118.15753f };
|
||||
Reference in new issue
Block a user