Archived
merge origin/main: the upstream LoRA-runtime refactor beneath the ref2va port
Merges upstream 1.10.0 (152 commits: the LoRA acquire/runtime refactor, the
form-1 LTX-2 scheduler family, guidance terms + negative prompts, decoder
selection, input_reference, new architectures) into the ref2va port branch.
Resolution rule throughout: keep BOTH feature sets; ABI numbering follows
upstream (RAD_ABI_VERSION 26, RAD_FORMAT_VER 2), with the port's video fields
unioned into RadVideoGenBatch/RadVideoGenDecl and abi_layout_table.h
regenerated from the live table.
Notable reconciliations:
* videos.cpp: the task grammars are disjoint by capability -- keyframe tasks
(i2va/l2va/flash-attn l2va/fl2va) are recognized only when decl.encodes,
so upstream models keep refusing them as unknown tasks; input_reference,
the LoRA tag/field pipeline, and the guidance/negative_prompt rendering
coexist with refs/ref2va; the prompt put() helper hoisted to block scope
for the media branch.
* video_schedule(): upstream's (steps, video_tokens, refine) keeps its
positional slots; the port's shift_video/shift_audio overrides move to the
tail so upstream's schedule tests call unchanged.
* r4d_selftest conv3d cases: the port's sp/st/causal/down/refl geometry and
upstream's tpad/tpre fold into one reference (ft = causal ? 2 : tpad==2 ?
2+tpre : 1); rms_mod rows keep the port's i8/fp8 quant modes alongside
upstream's wadd/norm_once/norm parameters.
* tests/oot_plugins.sh: the per-plugin cmake --install gains a --prefix
staging dir -- plugin .so files still land in the absolute
RAD_PLUGIN_INSTALL_HOME, but libr4d now also installs r4d_selftest to a
relative bin/, which needs no root.
Host ctest: 112/113 (mm_test's webp gap pre-exists this merge); r4d_selftest,
engine_e2e, oot_plugins, abi_layout, all LoRA tests green.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
a1a75da7e9
415 files changed
+78837
-5310
No files matched your search
+21
-4
@@ -22,6 +22,12 @@ double sinc(double x) {
|
||||
|
||||
double lanczos(double x) { return (-3.0 <= x && x < 3.0) ? sinc(x) * sinc(x / 3.0) : 0.0; }
|
||||
|
||||
/* PIL's BILINEAR filter: a triangle of support 1. */
|
||||
double triangle(double x) {
|
||||
if (x < 0.0) x = -x;
|
||||
return x < 1.0 ? 1.0 - x : 0.0;
|
||||
}
|
||||
|
||||
/* One axis: for each output index its first input index, its tap count, and `ksize` fixed-point
|
||||
* coefficients. */
|
||||
struct Axis {
|
||||
@@ -30,11 +36,11 @@ struct Axis {
|
||||
std::vector<int> kk; /* [out, ksize] */
|
||||
};
|
||||
|
||||
Axis coeffs(int in_size, int out_size) {
|
||||
Axis coeffs(int in_size, int out_size, bool bilinear) {
|
||||
Axis a;
|
||||
const double scale = (double)in_size / (double)out_size;
|
||||
double filterscale = scale < 1.0 ? 1.0 : scale;
|
||||
const double support = 3.0 * filterscale;
|
||||
const double support = (bilinear ? 1.0 : 3.0) * filterscale;
|
||||
a.ksize = (int)std::ceil(support) * 2 + 1;
|
||||
a.bounds.assign((size_t)out_size * 2, 0);
|
||||
a.kk.assign((size_t)out_size * a.ksize, 0);
|
||||
@@ -50,7 +56,8 @@ Axis coeffs(int in_size, int out_size) {
|
||||
xmax -= xmin;
|
||||
int x = 0;
|
||||
for (; x < xmax; ++x) {
|
||||
const double w = lanczos((x + xmin - center + 0.5) * ss);
|
||||
const double t = (x + xmin - center + 0.5) * ss;
|
||||
const double w = bilinear ? triangle(t) : lanczos(t);
|
||||
k[(size_t)x] = w;
|
||||
ww += w;
|
||||
}
|
||||
@@ -80,7 +87,17 @@ inline unsigned muldiv255(unsigned a, unsigned b) {
|
||||
|
||||
} /* namespace */
|
||||
|
||||
static void resize_rgba(const FrameRGBA& src, int32_t w, int32_t h, bool bilinear, FrameRGBA* out);
|
||||
|
||||
void resize_lanczos_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA* out) {
|
||||
resize_rgba(src, w, h, false, out);
|
||||
}
|
||||
|
||||
void resize_bilinear_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA* out) {
|
||||
resize_rgba(src, w, h, true, out);
|
||||
}
|
||||
|
||||
static void resize_rgba(const FrameRGBA& src, int32_t w, int32_t h, bool bilinear, FrameRGBA* out) {
|
||||
const int iw = src.w, ih = src.h;
|
||||
/* Image.resize returns a copy at the same size. The premultiplied round trip below is not
|
||||
* lossless where alpha is below 255. */
|
||||
@@ -96,7 +113,7 @@ void resize_lanczos_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA*
|
||||
pre[i * 4 + 3] = (uint8_t)al;
|
||||
}
|
||||
const bool horiz = w != iw, vert = h != ih;
|
||||
const Axis ax = coeffs(iw, w), ay = coeffs(ih, h);
|
||||
const Axis ax = coeffs(iw, w, bilinear), ay = coeffs(ih, h, bilinear);
|
||||
/* the rows the vertical pass reads */
|
||||
const int y_first = ay.bounds[0];
|
||||
const int y_last = ay.bounds[(size_t)(h - 1) * 2] + ay.bounds[(size_t)(h - 1) * 2 + 1];
|
||||
|
||||
@@ -22,6 +22,8 @@ void resize_lanczos_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA*
|
||||
* 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);
|
||||
/* The same with PIL's BILINEAR filter (a triangle of support 1, widened when shrinking). */
|
||||
void resize_bilinear_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA* out);
|
||||
/* `src` over an opaque white ground. */
|
||||
void composite_white(const FrameRGBA& src, Frame* out);
|
||||
|
||||
|
||||
@@ -46,6 +46,8 @@ int decode_video(const uint8_t*, size_t, SampleFn, const void*, int64_t, Video*,
|
||||
std::string* why) {
|
||||
return no_decoder(why);
|
||||
}
|
||||
int h264_roundtrip(const Frame&, int32_t, Frame*, std::string* why) { return no_decoder(why); }
|
||||
bool h264_roundtrip_available() { return false; }
|
||||
int decode_audio(const uint8_t*, size_t, int32_t, int64_t, std::vector<float>*,
|
||||
std::string* why) {
|
||||
return no_decoder(why);
|
||||
@@ -677,6 +679,113 @@ int decode_audio(const uint8_t* data, size_t n, int32_t rate, int64_t max_sample
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
bool h264_roundtrip_available() {
|
||||
return avcodec_find_encoder_by_name("libx264") && avcodec_find_decoder(AV_CODEC_ID_H264);
|
||||
}
|
||||
|
||||
/* PyAV's single-frame round trip: VideoFrame.from_ndarray(rgb24).reformat("yuv420p") (plain
|
||||
* SWS_BILINEAR, BT.601 coefficients, limited-range output), libx264 with the given crf, then the
|
||||
* decoded frame's to_ndarray("rgb24"), which to_rgb reproduces. */
|
||||
int h264_roundtrip(const Frame& in, int32_t crf, Frame* out, std::string* why) {
|
||||
std::string w;
|
||||
if (!why) why = &w;
|
||||
const int W = in.w & ~1, H = in.h & ~1;
|
||||
if (W <= 0 || H <= 0) { *why = "the picture is smaller than 2x2"; return RAD_E_INVAL; }
|
||||
const AVCodec* enc = avcodec_find_encoder_by_name("libx264");
|
||||
const AVCodec* dcd = avcodec_find_decoder(AV_CODEC_ID_H264);
|
||||
if (!enc || !dcd) { *why = "this libavcodec has no libx264 encoder or H.264 decoder"; return RAD_E_UNSUPPORTED; }
|
||||
AVCodecContext* ec = avcodec_alloc_context3(enc);
|
||||
AVCodecContext* dc = avcodec_alloc_context3(dcd);
|
||||
AVFrame* yuv = av_frame_alloc();
|
||||
AVFrame* dec = av_frame_alloc();
|
||||
AVPacket* pkt = av_packet_alloc();
|
||||
SwsContext* sws = nullptr;
|
||||
int st = RAD_OK;
|
||||
auto fail = [&](const char* what, int e) {
|
||||
*why = std::string(what) + (e < 0 ? " (" + averr(e) + ")" : std::string());
|
||||
st = e == AVERROR(ENOMEM) ? RAD_E_NOMEM : RAD_E_INVAL;
|
||||
};
|
||||
bool got = false;
|
||||
if (!ec || !dc || !yuv || !dec || !pkt) {
|
||||
*why = "out of memory";
|
||||
st = RAD_E_NOMEM;
|
||||
} else {
|
||||
ec->width = W;
|
||||
ec->height = H;
|
||||
ec->pix_fmt = AV_PIX_FMT_YUV420P;
|
||||
ec->time_base = AVRational{ 1, 1 };
|
||||
ec->framerate = AVRational{ 1, 1 };
|
||||
/* PyAV's codec defaults: as many threads as the machine has, splitting each frame into
|
||||
* that many slices, which shapes the bitstream. */
|
||||
ec->thread_count = 0;
|
||||
ec->thread_type = FF_THREAD_SLICE;
|
||||
av_opt_set(ec->priv_data, "preset", "veryfast", 0);
|
||||
av_opt_set(ec->priv_data, "crf", std::to_string(crf).c_str(), 0);
|
||||
int e = avcodec_open2(ec, enc, nullptr);
|
||||
if (e < 0) fail("opening libx264", e);
|
||||
if (st == RAD_OK && (e = avcodec_open2(dc, dcd, nullptr)) < 0) fail("opening the H.264 decoder", e);
|
||||
if (st == RAD_OK) {
|
||||
yuv->format = AV_PIX_FMT_YUV420P;
|
||||
yuv->width = W;
|
||||
yuv->height = H;
|
||||
if ((e = av_frame_get_buffer(yuv, 0)) < 0) fail("allocating a frame", e);
|
||||
}
|
||||
if (st == RAD_OK) {
|
||||
sws = sws_getContext(W, H, AV_PIX_FMT_RGB24, W, H, AV_PIX_FMT_YUV420P, SWS_BILINEAR,
|
||||
nullptr, nullptr, nullptr);
|
||||
if (!sws) fail("no RGB to yuv420p conversion", 0);
|
||||
}
|
||||
if (st == RAD_OK) {
|
||||
/* The source rows into a padded RGB frame: the scaler reads whole vectors past a tight
|
||||
* row's end. */
|
||||
AVFrame* rgb = av_frame_alloc();
|
||||
if (!rgb) fail("out of memory", AVERROR(ENOMEM));
|
||||
else {
|
||||
rgb->format = AV_PIX_FMT_RGB24;
|
||||
rgb->width = W;
|
||||
rgb->height = H;
|
||||
if ((e = av_frame_get_buffer(rgb, 0)) < 0) fail("allocating a frame", e);
|
||||
else {
|
||||
for (int y = 0; y < H; ++y)
|
||||
std::memcpy(rgb->data[0] + (ptrdiff_t)y * rgb->linesize[0],
|
||||
in.rgb.data() + (size_t)y * in.w * 3, (size_t)W * 3);
|
||||
if (sws_scale(sws, rgb->data, rgb->linesize, 0, H, yuv->data, yuv->linesize) != H)
|
||||
fail("colour conversion failed", 0);
|
||||
}
|
||||
av_frame_free(&rgb);
|
||||
}
|
||||
}
|
||||
/* Encode the frame and flush; every packet goes straight to the decoder. */
|
||||
for (int pass = 0; st == RAD_OK && pass < 2; ++pass) {
|
||||
yuv->pts = 0;
|
||||
if ((e = avcodec_send_frame(ec, pass == 0 ? yuv : nullptr)) < 0) { fail("encoding", e); break; }
|
||||
while (st == RAD_OK && (e = avcodec_receive_packet(ec, pkt)) >= 0) {
|
||||
if ((e = avcodec_send_packet(dc, pkt)) < 0) fail("decoding", e);
|
||||
av_packet_unref(pkt);
|
||||
}
|
||||
if (st == RAD_OK && e != AVERROR(EAGAIN) && e != AVERROR_EOF) fail("encoding", e);
|
||||
}
|
||||
if (st == RAD_OK) avcodec_send_packet(dc, nullptr);
|
||||
while (st == RAD_OK && (e = avcodec_receive_frame(dc, dec)) >= 0) {
|
||||
if (!got) {
|
||||
SwsContext* back = nullptr;
|
||||
st = to_rgb(dec, &back, out, why);
|
||||
sws_freeContext(back);
|
||||
got = st == RAD_OK;
|
||||
}
|
||||
av_frame_unref(dec);
|
||||
}
|
||||
if (st == RAD_OK && !got) fail("the round trip decoded to no frame", 0);
|
||||
}
|
||||
sws_freeContext(sws);
|
||||
av_packet_free(&pkt);
|
||||
av_frame_free(&yuv);
|
||||
av_frame_free(&dec);
|
||||
avcodec_free_context(&ec);
|
||||
avcodec_free_context(&dc);
|
||||
return st;
|
||||
}
|
||||
|
||||
#endif /* RAD_HAVE_FFMPEG */
|
||||
|
||||
} /* namespace mm */
|
||||
|
||||
@@ -77,5 +77,13 @@ int decode_video(const uint8_t* data, size_t n, SampleFn sample, const void* use
|
||||
int decode_audio(const uint8_t* data, size_t n, int32_t rate, int64_t max_samples,
|
||||
std::vector<float>* out, std::string* why);
|
||||
|
||||
/* `in` encoded as a one-frame H.264 stream at constant quality `crf` (libx264, preset veryfast,
|
||||
* yuv420p) and decoded back, as a video model's reference pipeline does to a conditioning
|
||||
* picture so it carries the compression of the footage the model was trained on. An odd width
|
||||
* or height loses its last column or row first. Needs libx264. */
|
||||
int h264_roundtrip(const Frame& in, int32_t crf, Frame* out, std::string* why);
|
||||
/* Whether this build can run h264_roundtrip: its libavcodec has libx264. */
|
||||
bool h264_roundtrip_available();
|
||||
|
||||
} /* namespace mm */
|
||||
} /* namespace rad */
|
||||
+166
-1
@@ -91,6 +91,65 @@ int VisionConfig::from_meta(const RadModelMeta& m, std::string* why) {
|
||||
return RAD_OK;
|
||||
}
|
||||
const char* mt = rad_meta_gets(&m, "vision_config.model_type", "");
|
||||
/* GLM-5.3 states its processor only in processor_config.json's image_processor, with a token
|
||||
* budget and no pixel band; the tower's geometry is the fallback, as the processor class
|
||||
* defaults to it. */
|
||||
if (mt && !std::strcmp(mt, "glm5_next_vision")) {
|
||||
family = Family::Glm5Next;
|
||||
auto geti = [&](const char* k, const char* tower, int64_t dflt) {
|
||||
const std::string key = std::string("processor.image_processor.") + k;
|
||||
return rad_meta_geti(&m, key.c_str(), rad_meta_geti(&m, tower, dflt));
|
||||
};
|
||||
patch = (int32_t)geti("patch_size", "vision_config.patch_size", 0);
|
||||
temporal = (int32_t)geti("temporal_patch_size", "vision_config.temporal_patch_size", 0);
|
||||
merge = (int32_t)geti("merge_size", "vision_config.spatial_merge_size", 0);
|
||||
if (patch <= 0 || temporal <= 0 || merge <= 0) {
|
||||
if (why) *why = "the container states no vision tower geometry (vision_config."
|
||||
"patch_size, temporal_patch_size, spatial_merge_size)";
|
||||
return RAD_E_NOTFOUND;
|
||||
}
|
||||
/* The expand factor multiplies the canvas's alignment without changing the patches; the
|
||||
* checkpoints ship 1, and another one is a canvas this processor does not make. */
|
||||
const int64_t expand =
|
||||
rad_meta_geti(&m, "processor.image_processor.patch_expand_factor", 1);
|
||||
if (expand != 1) {
|
||||
if (why) *why = "processor.image_processor.patch_expand_factor is " +
|
||||
std::to_string(expand) + "; this processor serves 1";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
tok_image = (int32_t)rad_meta_geti(&m, "image_token_id", -1);
|
||||
tok_video = (int32_t)rad_meta_geti(&m, "video_token_id", -1);
|
||||
tok_start = (int32_t)rad_meta_geti(&m, "image_start_token_id", -1);
|
||||
tok_end = (int32_t)rad_meta_geti(&m, "image_end_token_id", -1);
|
||||
if (tok_image < 0 || tok_start < 0 || tok_end < 0) {
|
||||
if (why) *why = "the container states no media token ids (image_token_id, "
|
||||
"image_start_token_id, image_end_token_id)";
|
||||
return RAD_E_NOTFOUND;
|
||||
}
|
||||
min_image_tokens = (int32_t)rad_meta_geti(&m, "processor.image_processor.min_image_tokens",
|
||||
16);
|
||||
max_image_tokens = (int32_t)rad_meta_geti(&m, "processor.image_processor.max_image_tokens",
|
||||
8000);
|
||||
/* OpenAI CLIP's, the processor class's default. */
|
||||
const float clip_mean[3] = { 0.48145466f, 0.4578275f, 0.40821073f };
|
||||
const float clip_std[3] = { 0.26862954f, 0.26130258f, 0.27577711f };
|
||||
std::copy(clip_mean, clip_mean + 3, mean);
|
||||
std::copy(clip_std, clip_std + 3, stdv);
|
||||
read_triple(m, "processor.image_processor.image_mean", mean);
|
||||
read_triple(m, "processor.image_processor.image_std", stdv);
|
||||
/* The pixel bands are QwenVL's; here they only have to be consistent for check(). */
|
||||
const int64_t f2 = (int64_t)factor() * factor();
|
||||
image_min_pixels = video_min_pixels = std::max<int64_t>(1, min_image_tokens) * f2;
|
||||
image_max_pixels = video_max_pixels =
|
||||
std::max<int64_t>(image_min_pixels, (int64_t)max_image_tokens * f2);
|
||||
std::string bad;
|
||||
if (check(&bad) < 0) {
|
||||
if (why)
|
||||
*why = "the container's GLM-5.3 processor configuration is inconsistent: " + bad;
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
if (mt && !std::strcmp(mt, "gemma4_vision")) {
|
||||
family = Family::Gemma4;
|
||||
patch = (int32_t)rad_meta_geti(&m, "vision_config.patch_size", 0);
|
||||
@@ -201,6 +260,9 @@ int VisionConfig::check(std::string* why) const {
|
||||
if (!(max_pixels_tolerance > 0.0) || max_segment_patches <= 0)
|
||||
return no("max_pixels_tolerance and max_num_patches must be positive");
|
||||
}
|
||||
if (family == Family::Glm5Next && (min_image_tokens < 1 || max_image_tokens < min_image_tokens))
|
||||
return no("the image token band [" + std::to_string(min_image_tokens) + ", " +
|
||||
std::to_string(max_image_tokens) + "] is empty");
|
||||
if (family == Family::Gemma4) {
|
||||
if (!soft_budget_ok(image_soft_tokens))
|
||||
return no("the image soft-token budget " + std::to_string(image_soft_tokens) +
|
||||
@@ -324,6 +386,51 @@ int gemma4_size(int64_t h, int64_t w, int32_t patch, int32_t pool, int64_t max_p
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Glm5NextImageProcessor's smart_resize and the size its resize() gives the picture, in Python's
|
||||
* doubles. A token is `temporal` frames of factor x factor pixels, and a picture counts as
|
||||
* `temporal` frames. The canvas's sides are rounded up to whole factors; under the floor the
|
||||
* picture is scaled up by the area ratio and rounded up again; over the budget a binary search over
|
||||
* the picture's height finds the largest canvas inside it. The picture then scales by the smaller
|
||||
* side ratio, never up while it reaches the floor, truncated. */
|
||||
int glm5next_size(int64_t h, int64_t w, int32_t temporal, int32_t factor, int64_t min_tok,
|
||||
int64_t max_tok, int64_t* oh, int64_t* ow, int64_t* ch, int64_t* cw,
|
||||
std::string* why) {
|
||||
if (h <= 0 || w <= 0) { if (why) *why = "the picture has no size"; return RAD_E_INVAL; }
|
||||
const int64_t per_tok = (int64_t)temporal * factor * factor;
|
||||
const int64_t min_px = min_tok * per_tok, max_px = max_tok * per_tok;
|
||||
if (max_px < per_tok) {
|
||||
if (why) *why = "the token budget is less than one merged patch";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
auto align = [factor](int64_t v) { return (v + factor - 1) / factor * factor; };
|
||||
int64_t ah = align(h), aw = align(w);
|
||||
if (temporal * ah * aw < min_px) {
|
||||
const double scale = std::sqrt((double)min_px / ((double)temporal * (double)h * (double)w));
|
||||
ah = align(std::max<int64_t>(1, (int64_t)std::ceil((double)h * scale)));
|
||||
aw = align(std::max<int64_t>(1, (int64_t)std::ceil((double)w * scale)));
|
||||
}
|
||||
if (temporal * ah * aw > max_px) {
|
||||
int64_t lo = 1, hi = h, bh = factor, bw = factor;
|
||||
while (lo <= hi) {
|
||||
const int64_t mid = (lo + hi) / 2;
|
||||
const int64_t wid = std::max<int64_t>(1, (int64_t)std::floor((double)(w * mid) /
|
||||
(double)h));
|
||||
const int64_t th = align(mid), tw = align(wid);
|
||||
if (temporal * th * tw <= max_px) { bh = th; bw = tw; lo = mid + 1; }
|
||||
else hi = mid - 1;
|
||||
}
|
||||
ah = bh;
|
||||
aw = bw;
|
||||
}
|
||||
double scale = std::min((double)ah / (double)h, (double)aw / (double)w);
|
||||
if (temporal * h * w >= per_tok * min_tok) scale = std::min(1.0, scale);
|
||||
*oh = ah;
|
||||
*ow = aw;
|
||||
*ch = std::max<int64_t>(1, std::min<int64_t>(ah, (int64_t)std::floor((double)h * scale)));
|
||||
*cw = std::max<int64_t>(1, std::min<int64_t>(aw, (int64_t)std::floor((double)w * scale)));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* The reference's (EmbeddingGemma2VideoProcessor.sample_frames): the stream's duration at `fps`,
|
||||
* one frame each 1/fps seconds as int(i * native_fps / fps), then `max_frames` of those chosen by
|
||||
* numpy's linspace truncated to integers. */
|
||||
@@ -671,6 +778,15 @@ int Processor::init(const VisionConfig& cfg,
|
||||
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. */
|
||||
const int64_t pass_tok = cfg.max_patches / ((int64_t)cfg.merge * cfg.merge);
|
||||
if (pass_tok < cfg.min_image_tokens) return RAD_E_INVAL;
|
||||
if (cfg_.max_image_tokens > pass_tok) cfg_.max_image_tokens = (int32_t)pass_tok;
|
||||
encode_text_ = std::move(encode_text);
|
||||
return RAD_OK;
|
||||
}
|
||||
if (cfg.family == Family::Gemma4) {
|
||||
/* A budget is a patch count, and an image is one segment of one pass. */
|
||||
if ((int64_t)cfg.image_soft_tokens * cfg.merge * cfg.merge > cfg.max_patches ||
|
||||
@@ -805,10 +921,50 @@ int Processor::image_lfm2(const Frame& f, std::shared_ptr<Item>* out, std::strin
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* GLM-5.3's image processor (transformers' Glm5NextImageProcessor, torchvision backend): the
|
||||
* picture, resized only when its size changes, in the top left of a canvas of zero bytes, which
|
||||
* the normalisation turns into each channel's -mean / std as it does the reference's padding. */
|
||||
int Processor::image_glm5next(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const {
|
||||
const VisionConfig& c = cfg_;
|
||||
int64_t h = 0, w = 0, ch = 0, cw = 0;
|
||||
RAD_TRY(glm5next_size(f.h, f.w, c.temporal, c.factor(), c.min_image_tokens,
|
||||
c.max_image_tokens, &h, &w, &ch, &cw, why));
|
||||
Frame r;
|
||||
const Frame* src = &f;
|
||||
if (ch != f.h || cw != f.w) {
|
||||
resize_bicubic_torch(f, (int32_t)cw, (int32_t)ch, &r);
|
||||
src = &r;
|
||||
}
|
||||
Frame canvas;
|
||||
canvas.w = (int32_t)w;
|
||||
canvas.h = (int32_t)h;
|
||||
canvas.rgb.assign((size_t)w * h * 3, 0);
|
||||
for (int64_t y = 0; y < ch; ++y)
|
||||
std::memcpy(canvas.rgb.data() + (size_t)y * w * 3, src->rgb.data() + (size_t)y * cw * 3,
|
||||
(size_t)cw * 3);
|
||||
auto it = std::make_shared<Item>();
|
||||
it->kind = Kind::Image;
|
||||
it->grid_t = 1;
|
||||
it->grid_h = (int32_t)(h / c.patch);
|
||||
it->grid_w = (int32_t)(w / c.patch);
|
||||
it->rope_1d = true;
|
||||
finish_item(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;
|
||||
}
|
||||
std::vector<const Frame*> frames((size_t)c.temporal, &canvas);
|
||||
patchify(c, frames, 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;
|
||||
}
|
||||
|
||||
int Processor::image_from(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const {
|
||||
const VisionConfig& c = cfg_;
|
||||
if (c.family == Family::Gemma4) return image_gemma4(f, c.image_soft_tokens, out, why);
|
||||
if (c.family == Family::Lfm2) return image_lfm2(f, out, why);
|
||||
if (c.family == Family::Glm5Next) return image_glm5next(f, 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,
|
||||
&h, &w, why));
|
||||
@@ -901,6 +1057,10 @@ 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::Glm5Next) {
|
||||
if (why) *why = "this model's processor takes pictures, not video";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (v.frames.empty()) { if (why) *why = "the video decoded to no frames"; return RAD_E_INVAL; }
|
||||
/* Pad the frames and their indices to whole temporal groups by repeating the last; a clip
|
||||
* shorter than one group becomes one group. */
|
||||
@@ -982,6 +1142,11 @@ int Processor::video_from(const Video& v, std::shared_ptr<Item>* out, std::strin
|
||||
int Processor::image(const uint8_t* bytes, size_t n, int32_t image_tokens,
|
||||
std::shared_ptr<Item>* out, std::string* why, int64_t max_pixels) const {
|
||||
if (image_tokens != 0) {
|
||||
if (cfg_.family == Family::Glm5Next) {
|
||||
if (why) *why = "this model's image token budget is the server's (--image-tokens), "
|
||||
"not a request's";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (cfg_.family != Family::Gemma4) {
|
||||
if (why) *why = "this model's processor sizes an image by its pixels, not by a token "
|
||||
"budget";
|
||||
@@ -1153,7 +1318,7 @@ std::vector<int64_t> sample_cb(const VideoInfo& info, const void* user) {
|
||||
|
||||
int Processor::video(const uint8_t* bytes, size_t n, std::shared_ptr<Item>* out,
|
||||
std::string* why) const {
|
||||
if (cfg_.family == Family::Lfm2) {
|
||||
if (cfg_.family == Family::Lfm2 || cfg_.family == Family::Glm5Next) {
|
||||
if (why) *why = "this model's processor takes pictures, not video";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
+19
-2
@@ -55,8 +55,16 @@ enum class Kind { Image, Video, Audio };
|
||||
* is cut into tile x tile crops, min_tiles..max_tiles of them in the ratio nearest its
|
||||
* own, followed by the whole picture at the budgeted size as a thumbnail. Patches
|
||||
* (y, x, channel) normalised by the mean and deviation, resized with torchvision's
|
||||
* fixed-point bicubic; images only, and the tokens rotate at their positions. */
|
||||
enum class Family { QwenVL, Gemma4, Lfm2 };
|
||||
* fixed-point bicubic; images only, and the tokens rotate at their positions.
|
||||
* Glm5Next GLM-5.3's: a canvas whose sides are multiples of patch * merge and whose tokens fall
|
||||
* in [min_image_tokens, max_image_tokens] (the sides rounded up, a picture under the
|
||||
* floor grown, one over the budget fitted by the reference's search); the picture keeps
|
||||
* its aspect ratio, is never enlarged above the floor, and is resized with torchvision's
|
||||
* fixed-point bicubic only when its size changes; the rest of the canvas, right and
|
||||
* bottom, is zero bytes. Patches as QwenVL's. Images only, spelled <begin_of_image>
|
||||
* placeholders <end_of_image>, the tokens at their positions (the language model has no
|
||||
* rotary). */
|
||||
enum class Family { QwenVL, Gemma4, Lfm2, Glm5Next };
|
||||
|
||||
struct VisionConfig {
|
||||
Family family = Family::QwenVL;
|
||||
@@ -66,6 +74,8 @@ struct VisionConfig {
|
||||
int32_t image_soft_tokens = 0, video_soft_tokens = 0;
|
||||
int64_t image_min_pixels = 0, image_max_pixels = 0;
|
||||
int64_t video_min_pixels = 0, video_max_pixels = 0;
|
||||
/* Glm5Next: the band an image's canvas falls in, in output rows (tokens). */
|
||||
int32_t min_image_tokens = 0, max_image_tokens = 0;
|
||||
double fps = 2.0;
|
||||
int32_t min_frames = 4, max_frames = 768;
|
||||
/* Per-frame ceiling on a video's resolution, in output rows (the reference processor's
|
||||
@@ -371,6 +381,7 @@ private:
|
||||
int image_gemma4(const Frame& f, int32_t soft, std::shared_ptr<Item>* out,
|
||||
std::string* why) const;
|
||||
int image_lfm2(const Frame& f, std::shared_ptr<Item>* out, std::string* why) const;
|
||||
int image_glm5next(const Frame& f, 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_;
|
||||
@@ -412,6 +423,12 @@ void resize_bicubic(const Frame& in, int32_t w, int32_t h, Frame* out);
|
||||
/* ...and torchvision's on a uint8 tensor (ATen's CPU kernel), which is the same filter with the
|
||||
* coefficients at the most precision 16 bits hold, chosen per axis, rather than PIL's 22. */
|
||||
void resize_bicubic_torch(const Frame& in, int32_t w, int32_t h, Frame* out);
|
||||
/* GLM-5.3's smart_resize: the (oh, ow) canvas, multiples of `factor`, for an h x w picture whose
|
||||
* `temporal` frames take [min_tok, max_tok] tokens of temporal * factor^2 pixels each; and the
|
||||
* (ch, cw) the picture is resized to inside it, top left, aspect ratio kept. */
|
||||
int glm5next_size(int64_t h, int64_t w, int32_t temporal, int32_t factor, int64_t min_tok,
|
||||
int64_t max_tok, int64_t* oh, int64_t* ow, int64_t* ch, int64_t* cw,
|
||||
std::string* why);
|
||||
/* LFM2-VL's smart_resize: (height, width) to multiples of `factor`, at least one each, pixels
|
||||
* within [min_px, max_px] where the rounding left them outside. */
|
||||
void lfm2_size(int64_t h, int64_t w, int32_t factor, int64_t min_px, int64_t max_px,
|
||||
|
||||
Reference in new issue
Block a user