radiance 1.6.0

- Qwen-Image 2.1 and 2.1 Turbo: text to image and image edits over OpenAI's images API,
  /v1/images/generations and /v1/images/edits. An edit takes up to ten pictures and an optional
  mask, outside which the picture comes back exactly as it was; `background: transparent`, png,
  jpeg and webp output, `n` pictures from one prompt, streamed previews (`partial_images`), and
  vLLM-Omni's seed, negative_prompt with true_cfg_scale, num_inference_steps, width and height.
  arch/qwenimage21 serves the diffusers pipeline directory, converted with
  data/recipes/qwen-image-2.1-fp8.recipe (block fp8) or qwen-image-2.1-bf16.recipe.
  deploy/compose/qwen-image-2.1.yaml and qwen-image-2.1-turbo.yaml serve the fp8 containers on
  two cards. Two R9700s make a 2048x2048 picture in 61 s at the base model's 40 steps and in
  13 s with Turbo (56 and 12 s with --tp-wire wht6), a 1024x1024 one in 6.0 and 2.6 s. The
  models are under Qwen's research license, which is non-commercial
- an image generator is a program kind of its own (core/gen): an architecture declares its
  conditioning, denoising and decoding passes, and the driver runs the schedule, the pictures of
  a batch and the conditioning cache. An image model's /v1/models entry is its generator's name
  (qwen-image-2.1, qwen-image-2.1-turbo), and its capabilities and startup banner list only the
  images endpoints
- the tiled prefill GEMMs run their tiles in groups of activation bands, 16 for fp8 and 8 for
  bf16, so an operand larger than the 64 MB cache is read once a group instead of once a band. A
  bf16 GEMM with more than 64 MB of weight runs up to twice as fast
- fixed: a second lane opened in the middle of a step started beside work lane 0 had not
  finished. Gemma 4 26B-A4B opens its lane that way, so its first step after startup could read
  rows not yet written
- startup no longer warns about --checkpoint-slots when it was not passed, nor about an expert
  floor on a model with no experts
- the image's FFmpeg writes png, jpeg and webp (libwebp)
- for out-of-tree plugins: RAD_ABI_VERSION is 16. The op vocabulary gains the image generator's
  ops (conv2d_nhwc, ln_mod, ln_mod_quant_fp8, flow_euler, attn_prefix_self and others; docs/OPS.md,
  "Image generation"), and all_reduce takes an optional `overlap`. Plugins built against 1.5.0
  must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-14 14:03:59 +02:00
1 parent b93b531f63
commit b068190326
66 files changed
+9035 -87

No files matched your search

+167
View File
@@ -0,0 +1,167 @@
/* imaging.cpp -- PIL's Lanczos resize and alpha paste (imaging.h). The constants and the order of
* every operation are Pillow's (src/libImaging/Resample.c, Convert.c, Paste.c). */
#include "mm/imaging.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
namespace rad {
namespace mm {
namespace {
constexpr int kPrecision = 32 - 8 - 2;
double sinc(double x) {
if (x == 0.0) return 1.0;
x *= M_PI;
return std::sin(x) / x;
}
double lanczos(double x) { return (-3.0 <= x && x < 3.0) ? sinc(x) * sinc(x / 3.0) : 0.0; }
/* One axis: for each output index its first input index, its tap count, and `ksize` fixed-point
* coefficients. */
struct Axis {
int ksize = 0;
std::vector<int> bounds; /* [out, 2]: xmin, count */
std::vector<int> kk; /* [out, ksize] */
};
Axis coeffs(int in_size, int out_size) {
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;
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);
std::vector<double> k((size_t)a.ksize);
for (int xx = 0; xx < out_size; ++xx) {
const double center = (xx + 0.5) * scale;
double ww = 0.0;
const double ss = 1.0 / filterscale;
int xmin = (int)(center - support + 0.5);
if (xmin < 0) xmin = 0;
int xmax = (int)(center + support + 0.5);
if (xmax > in_size) xmax = in_size;
xmax -= xmin;
int x = 0;
for (; x < xmax; ++x) {
const double w = lanczos((x + xmin - center + 0.5) * ss);
k[(size_t)x] = w;
ww += w;
}
for (x = 0; x < xmax; ++x)
if (ww != 0.0) k[(size_t)x] /= ww;
for (; x < a.ksize; ++x) k[(size_t)x] = 0.0;
a.bounds[(size_t)xx * 2] = xmin;
a.bounds[(size_t)xx * 2 + 1] = xmax;
for (x = 0; x < a.ksize; ++x) {
const double v = k[(size_t)x] * (double)(1 << kPrecision);
a.kk[(size_t)xx * a.ksize + x] = v < 0 ? (int)(-0.5 + v) : (int)(0.5 + v);
}
}
return a;
}
inline uint8_t clip8(int v) {
if (v >= (1 << kPrecision << 8)) return 255;
if (v <= 0) return 0;
return (uint8_t)(v >> kPrecision);
}
inline unsigned muldiv255(unsigned a, unsigned b) {
const unsigned t = a * b + 128;
return ((t >> 8) + t) >> 8;
}
} /* namespace */
void resize_lanczos_rgba(const FrameRGBA& src, int32_t w, int32_t h, FrameRGBA* out) {
const int iw = src.w, ih = src.h;
/* RGBA -> RGBa: the colour premultiplied by the alpha */
std::vector<uint8_t> pre((size_t)iw * ih * 4);
for (size_t i = 0; i < (size_t)iw * ih; ++i) {
const unsigned al = src.rgba[i * 4 + 3];
for (int c = 0; c < 3; ++c) pre[i * 4 + c] = (uint8_t)muldiv255(src.rgba[i * 4 + c], al);
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);
/* 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];
std::vector<uint8_t> tmp;
const uint8_t* rows = pre.data();
int rw = iw, row0 = 0;
if (horiz) {
const int th = vert ? y_last - y_first : ih;
const int off = vert ? y_first : 0;
tmp.assign((size_t)w * th * 4, 0);
for (int yy = 0; yy < th; ++yy)
for (int xx = 0; xx < w; ++xx) {
const int xmin = ax.bounds[(size_t)xx * 2], xn = ax.bounds[(size_t)xx * 2 + 1];
const int* k = &ax.kk[(size_t)xx * ax.ksize];
int s[4] = { 1 << (kPrecision - 1), 1 << (kPrecision - 1), 1 << (kPrecision - 1),
1 << (kPrecision - 1) };
const uint8_t* p = pre.data() + ((size_t)(yy + off) * iw + xmin) * 4;
for (int x = 0; x < xn; ++x)
for (int c = 0; c < 4; ++c) s[c] += (int)p[(size_t)x * 4 + c] * k[x];
for (int c = 0; c < 4; ++c) tmp[((size_t)yy * w + xx) * 4 + c] = clip8(s[c]);
}
rows = tmp.data();
rw = w;
row0 = vert ? y_first : 0;
}
std::vector<uint8_t> res;
if (vert) {
res.assign((size_t)rw * h * 4, 0);
for (int yy = 0; yy < h; ++yy) {
const int ymin = ay.bounds[(size_t)yy * 2] - row0, yn = ay.bounds[(size_t)yy * 2 + 1];
const int* k = &ay.kk[(size_t)yy * ay.ksize];
for (int xx = 0; xx < rw; ++xx) {
int s[4] = { 1 << (kPrecision - 1), 1 << (kPrecision - 1), 1 << (kPrecision - 1),
1 << (kPrecision - 1) };
for (int y = 0; y < yn; ++y) {
const uint8_t* p = rows + ((size_t)(y + ymin) * rw + xx) * 4;
for (int c = 0; c < 4; ++c) s[c] += (int)p[c] * k[y];
}
for (int c = 0; c < 4; ++c) res[((size_t)yy * rw + xx) * 4 + c] = clip8(s[c]);
}
}
} else {
res.assign(rows, rows + (size_t)rw * (horiz ? h : ih) * 4);
}
/* RGBa -> RGBA */
out->w = w;
out->h = h;
out->rgba.resize((size_t)w * h * 4);
for (size_t i = 0; i < (size_t)w * h; ++i) {
const unsigned al = res[i * 4 + 3];
for (int c = 0; c < 3; ++c) {
const unsigned v = res[i * 4 + c];
out->rgba[i * 4 + c] =
(al == 255 || al == 0) ? (uint8_t)v : (uint8_t)std::min(255u, (255u * v) / al);
}
out->rgba[i * 4 + 3] = (uint8_t)al;
}
}
void composite_white(const FrameRGBA& src, Frame* out) {
out->w = src.w;
out->h = src.h;
out->rgb.resize((size_t)src.w * src.h * 3);
for (size_t i = 0; i < (size_t)src.w * src.h; ++i) {
const unsigned a = src.rgba[i * 4 + 3];
for (int c = 0; c < 3; ++c) {
const unsigned t = src.rgba[i * 4 + c] * a + 255u * (255u - a) + 128u;
out->rgb[i * 3 + c] = (uint8_t)(((t >> 8) + t) >> 8);
}
}
}
} /* namespace mm */
} /* namespace rad */
+25
View File
@@ -0,0 +1,25 @@
/* imaging.h -- the picture arithmetic an image generator's reference pipeline does in PIL, done the
* way PIL does it, so a picture reaches the model as the reference's would.
*
* THE RESIZE IS PIL's Image.resize WITH LANCZOS, bit for bit: per-axis filter coefficients
* computed in double (support 3 times the downscale, normalised to sum one), turned into 22-bit
* fixed point, a horizontal pass then a vertical one over 8-bit values, each output rounded and
* clipped -- and an RGBA picture premultiplied by its alpha first and divided back after, which is
* what PIL's resize does to RGBA. Diffusers' VaeImageProcessor resizes a picture exactly this way.
*
* THE COMPOSITE is Image.paste(picture, mask=alpha) onto white: each channel
* (c * a + 255 * (255 - a)) / 255 in PIL's rounding -- how the reference flattens a transparent
* picture for a text encoder that reads RGB. */
#pragma once
#include "mm/media.h"
namespace rad {
namespace mm {
/* `src` resized to w x h. */
void resize_lanczos_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);
} /* namespace mm */
} /* namespace rad */
+179
View File
@@ -34,6 +34,13 @@ static int no_decoder(std::string* why) {
int decode_image(const uint8_t*, size_t, int64_t, Frame*, std::string* why) {
return no_decoder(why);
}
int decode_image_rgba(const uint8_t*, size_t, int64_t, FrameRGBA*, std::string* why) {
return no_decoder(why);
}
int encode_image(const uint8_t*, int32_t, int32_t, int32_t, const std::string&, int32_t,
std::vector<uint8_t>*, std::string* why) {
return no_decoder(why);
}
int decode_video(const uint8_t*, size_t, SampleFn, const void*, int64_t, Video*,
std::string* why) {
return no_decoder(why);
@@ -211,6 +218,46 @@ int to_rgb(const AVFrame* f, SwsContext** sws, Frame* out, std::string* why) {
return RAD_OK;
}
/* The same conversion to packed RGBA, for a picture whose alpha is kept. */
int to_rgba(const AVFrame* f, SwsContext** sws, FrameRGBA* out, std::string* why) {
const AVPixelFormat src = (AVPixelFormat)f->format;
*sws = sws_getCachedContext(*sws, f->width, f->height, src, f->width, f->height,
AV_PIX_FMT_RGBA, SWS_BILINEAR, nullptr, nullptr, nullptr);
if (!*sws) { *why = "no colour conversion from this pixel format"; return RAD_E_UNSUPPORTED; }
int cs = f->colorspace;
if (cs == AVCOL_SPC_UNSPECIFIED || cs == AVCOL_SPC_RGB) cs = AVCOL_SPC_BT470BG;
const int src_full = f->color_range == AVCOL_RANGE_JPEG ||
src == AV_PIX_FMT_YUVJ420P || src == AV_PIX_FMT_YUVJ422P ||
src == AV_PIX_FMT_YUVJ444P || src == AV_PIX_FMT_YUVJ440P;
sws_setColorspaceDetails(*sws, sws_getCoefficients(cs), src_full,
sws_getCoefficients(SWS_CS_DEFAULT), 1, 0, 1 << 16, 1 << 16);
AVFrame* dst = av_frame_alloc();
if (!dst) { *why = "out of memory"; return RAD_E_NOMEM; }
dst->format = AV_PIX_FMT_RGBA;
dst->width = f->width;
dst->height = f->height;
if (av_frame_get_buffer(dst, 0) < 0) {
av_frame_free(&dst);
*why = "out of memory";
return RAD_E_NOMEM;
}
const int e = sws_scale(*sws, f->data, f->linesize, 0, f->height, dst->data, dst->linesize);
if (e != f->height) {
av_frame_free(&dst);
*why = "colour conversion failed";
return RAD_E_INVAL;
}
out->w = f->width;
out->h = f->height;
const size_t row = (size_t)f->width * 4;
out->rgba.resize(row * (size_t)f->height);
for (int y = 0; y < f->height; ++y)
std::memcpy(out->rgba.data() + (size_t)y * row, dst->data[0] + (ptrdiff_t)y * dst->linesize[0],
row);
av_frame_free(&dst);
return RAD_OK;
}
/* THE DECODE LOOP, for both kinds: every frame in presentation order, `keep(i, frame)` told the
* index of each and whether to stop.
*
@@ -313,6 +360,138 @@ int decode_image(const uint8_t* data, size_t n, int64_t max_pixels, Frame* out,
return RAD_OK;
}
int decode_image_rgba(const uint8_t* data, size_t n, int64_t max_pixels, FrameRGBA* out,
std::string* why) {
std::string w;
if (!why) why = &w;
if (!data || n == 0) { *why = "empty"; return RAD_E_INVAL; }
Input in;
RAD_TRY(in.open(data, n, true, why));
const AVCodecParameters* cp = in.fmt->streams[in.stream]->codecpar;
if (cp->width <= 0 || cp->height <= 0) { *why = "the image has no size"; return RAD_E_INVAL; }
if ((int64_t)cp->width * cp->height > max_pixels) {
*why = "the image is " + std::to_string(cp->width) + "x" + std::to_string(cp->height) +
", over the server's " + std::to_string(max_pixels) + "-pixel limit for a source";
return RAD_E_INVAL;
}
SwsContext* sws = nullptr;
bool got = false;
const int s = decode_frames(in, [&](int64_t, const AVFrame* f) -> int {
const int c = to_rgba(f, &sws, out, why);
if (c < 0) return c;
got = true;
return 1;
}, why);
sws_freeContext(sws);
if (s < 0) return s;
if (!got) { *why = "the image decoded to no frame"; return RAD_E_INVAL; }
return RAD_OK;
}
/* ONE PICTURE THROUGH ONE ENCODER. The pixels are converted to the pixel format the encoder takes
* (swscale, full-range BT.601 for jpeg's YUV, as PIL writes it), sent as one frame, and the one
* packet that comes back is the file: png, mjpeg and libwebp all write a complete image file a
* packet. */
int encode_image(const uint8_t* px, int32_t w, int32_t h, int32_t ch, const std::string& format,
int32_t quality, std::vector<uint8_t>* out, std::string* why) {
std::string wy;
if (!why) why = &wy;
if (!px || w <= 0 || h <= 0 || (ch != 3 && ch != 4) || !out) {
*why = "no picture to encode";
return RAD_E_INVAL;
}
const AVCodec* codec = nullptr;
AVPixelFormat pf = AV_PIX_FMT_NONE;
if (format == "png") {
codec = avcodec_find_encoder(AV_CODEC_ID_PNG);
pf = ch == 4 ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB24;
} else if (format == "jpeg") {
if (ch == 4) { *why = "jpeg carries no alpha"; return RAD_E_INVAL; }
codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
pf = AV_PIX_FMT_YUVJ420P;
} else if (format == "webp") {
codec = avcodec_find_encoder_by_name("libwebp");
pf = AV_PIX_FMT_BGRA;
} else {
*why = "'" + format + "' is not an image format this server writes (png, jpeg, webp)";
return RAD_E_INVAL;
}
if (!codec) {
*why = "this build has no " + format + " encoder";
return RAD_E_UNSUPPORTED;
}
AVCodecContext* cx = avcodec_alloc_context3(codec);
AVFrame* fr = av_frame_alloc();
AVPacket* pk = av_packet_alloc();
SwsContext* sws = nullptr;
int st = RAD_OK;
auto fail = [&](const std::string& m, int code) { *why = m; st = code; };
do {
if (!cx || !fr || !pk) { fail("out of memory", RAD_E_NOMEM); break; }
cx->width = w;
cx->height = h;
cx->pix_fmt = pf;
cx->time_base = AVRational{ 1, 25 };
cx->color_range = format == "jpeg" ? AVCOL_RANGE_JPEG : AVCOL_RANGE_UNSPECIFIED;
if (format == "jpeg") {
const int q = quality > 0 && quality <= 100 ? quality : 90;
/* qscale 2 (best) .. 31 (worst), linear in the quality */
const int qs = 2 + (100 - q) * 29 / 100;
cx->flags |= AV_CODEC_FLAG_QSCALE;
cx->global_quality = FF_QP2LAMBDA * qs;
}
/* PNG AT zlib LEVEL 1 WITH PER-ROW PREDICTION: a 2048^2 picture in 0.16 s and 5.9 MB, where
* the encoder's default (level 6) takes 1.0 s for 5.3 MB -- a second of a few-step image
* model's request, on one core, for a tenth of the bytes. */
if (format == "png") {
cx->compression_level = 1;
av_opt_set(cx->priv_data, "pred", "mixed", 0);
}
if (format == "webp") {
const int q = quality > 0 && quality <= 100 ? quality : 90;
if (q >= 100) av_opt_set_int(cx->priv_data, "lossless", 1, 0);
else av_opt_set_double(cx->priv_data, "quality", (double)q, 0);
}
int r = avcodec_open2(cx, codec, nullptr);
if (r < 0) { fail("the " + format + " encoder would not open (" + averr(r) + ")", RAD_E_INVAL); break; }
fr->format = pf;
fr->width = w;
fr->height = h;
if (av_frame_get_buffer(fr, 0) < 0) { fail("out of memory", RAD_E_NOMEM); break; }
const AVPixelFormat src = ch == 4 ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB24;
sws = sws_getContext(w, h, src, w, h, pf, SWS_BILINEAR | SWS_ACCURATE_RND, nullptr,
nullptr, nullptr);
if (!sws) { fail("no colour conversion to the encoder's format", RAD_E_UNSUPPORTED); break; }
if (format == "jpeg")
sws_setColorspaceDetails(sws, sws_getCoefficients(SWS_CS_DEFAULT), 1,
sws_getCoefficients(SWS_CS_ITU601), 1, 0, 1 << 16, 1 << 16);
const uint8_t* sp[1] = { px };
const int sl[1] = { w * ch };
if (sws_scale(sws, sp, sl, 0, h, fr->data, fr->linesize) != h) {
fail("colour conversion failed", RAD_E_INVAL);
break;
}
fr->pts = 0;
r = avcodec_send_frame(cx, fr);
if (r >= 0) r = avcodec_send_frame(cx, nullptr);
if (r < 0) { fail("encoding failed (" + averr(r) + ")", RAD_E_INVAL); break; }
out->clear();
for (;;) {
r = avcodec_receive_packet(cx, pk);
if (r == AVERROR_EOF || r == AVERROR(EAGAIN)) break;
if (r < 0) { fail("encoding failed (" + averr(r) + ")", RAD_E_INVAL); break; }
out->insert(out->end(), pk->data, pk->data + pk->size);
av_packet_unref(pk);
}
if (st == RAD_OK && out->empty()) fail("the encoder wrote nothing", RAD_E_INVAL);
} while (false);
sws_freeContext(sws);
av_packet_free(&pk);
av_frame_free(&fr);
avcodec_free_context(&cx);
return st;
}
int decode_video(const uint8_t* data, size_t n, SampleFn sample, const void* user,
int64_t max_pixels, Video* out, std::string* why) {
std::string w;
+17
View File
@@ -27,6 +27,13 @@ struct Frame {
std::vector<uint8_t> rgb; /* [h, w, 3] */
};
/* A picture with its alpha: packed 8-bit RGBA, row-major, no padding -- what an image generator
* reads and writes. A source without alpha comes back opaque. */
struct FrameRGBA {
int32_t w = 0, h = 0;
std::vector<uint8_t> rgba; /* [h, w, 4] */
};
/* Is there a decoder in this build. */
bool decoder_available();
@@ -34,6 +41,16 @@ bool decoder_available();
* `max_pixels` in the SOURCE, which is what bounds the memory a request can make the server
* allocate before any resize happens. */
int decode_image(const uint8_t* data, size_t n, int64_t max_pixels, Frame* out, std::string* why);
/* The same, keeping the alpha channel. */
int decode_image_rgba(const uint8_t* data, size_t n, int64_t max_pixels, FrameRGBA* out,
std::string* why);
/* A PICTURE TO A FILE: `px` is [h, w, ch] 8-bit, ch 3 (RGB) or 4 (RGBA), and `format` one of png,
* jpeg and webp. `quality` 0..100 is jpeg's and lossy webp's (0 or above 100: the format's own
* default); png is lossless and webp is lossless at 100. jpeg has no alpha and is refused one.
* RAD_E_UNSUPPORTED, with the reason, for a format this build has no encoder for. */
int encode_image(const uint8_t* px, int32_t w, int32_t h, int32_t ch, const std::string& format,
int32_t quality, std::vector<uint8_t>* out, std::string* why);
/* HOW A VIDEO IS SAMPLED, and it happens INSIDE the decode rather than after it: a minute of 1080p
* is 10 GB of RGB, so frames that are not sampled are never converted or kept. The choice of which