Archived
- 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
168 lines
6.1 KiB
C++
168 lines
6.1 KiB
C++
/* 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 */
|