radiance 1.10.0

LoRA for image and video generators:
- civitai and Hugging Face adapter files as published: kohya, ComfyUI and diffusers/PEFT key names;
  LoRA, LoCon, LoHa, LoKr and full-weight diffs; DoRA, OFT and Tucker are refused by name
- stacked per request with a strength each, through the `loras` field or <lora:name:strength> tags
  in the prompt; --lora-dir serves every file in a directory, GET /v1/loras lists them, and
  --max-loras / --max-lora-rank size the slots on the card (generators default to rank 64)
- Krea 2, Qwen-Image 2.1, FLUX.3 [klein], MiniMax H3, LTX-2.5, Anima and SDXL

New models:
- Anima (base, aesthetic and turbo): Cosmos-Predict2 DiT with the Qwen3-0.6B text encoder and the
  Qwen-Image VAE, on one card
- Stable Diffusion XL and its single-file finetunes (Illustrious, NoobAI including V-Pred, Pony),
  read as civitai ships them, with the Euler, Euler a and DPM++ 2M samplers

Recipes:
- q38-27b-mxfp4-df2: any bf16 Qwen3.8-27B or finetune at MXFP4 with the DFlash2 drafter
- Krea 2 finetunes from civitai: scripts/krea2_native.py reads Krea's own key names, and
  krea-2-fp8-unscaled keeps a plain-fp8 file's codes exactly

Changes: --max-num-batched-tokens defaults to 2048 (was 8192). Plugin ABI 25: out-of-tree plugins
must be rebuilt.
This commit is contained in:
Deadcode committed 2026-10-20 21:05:16 +02:00
1 parent 685ec5bd85
commit 4246a1dd04
124 files changed
+14326 -387

No files matched your search

+23
View File
@@ -599,6 +599,22 @@ struct ImageGenInfo {
/* A reference picture is shrunk to `ref_area` only when larger, then centre-cropped to the
* size multiple; otherwise resized to `ref_area` at its aspect. */
bool ref_shrink_only = false;
/* A prompt of fewer tokens after the drop prefix is padded with `pad_token` up to this many
* (RadGeneratorDecl::min_tokens); 0: no minimum. */
int64_t min_tokens = 0;
int32_t pad_token = 0;
/* The prompt is also read through the model's second tokenizer (IImageGen::encode_aux), at
* most `aux_max_tokens` ids of it. */
bool aux_tokens = false;
int64_t aux_max_tokens = 0;
/* A discrete-time model (RadGeneratorDecl::train_steps): the samplers it serves (1 << id)
* and the sampler and scheduler a request gets when it names none. 0 samplers: flow
* matching, which takes neither field. */
uint32_t samplers = 0;
int32_t default_sampler = 0, default_scheduler = 0;
/* The prompt in windows (RadGeneratorDecl::chunk_tokens): tokens a window, its markers and
* its padding token; 0 for a prompt read whole. */
int32_t chunk_tokens = 0, chunk_bos = 0, chunk_eos = 0, chunk_pad = 0;
};
class IImageGen {
@@ -612,6 +628,13 @@ public:
virtual int picture(const mm::Frame& f, std::shared_ptr<mm::Item>* out, std::string* why) = 0;
virtual int64_t queued() const = 0;
virtual int64_t running() const = 0;
/* The user's prompt through the model's second tokenizer (ImageGenInfo::aux_tokens), with the
* special tokens that tokenizer adds; RAD_E_UNSUPPORTED for a model without one. */
virtual int encode_aux(const std::string& text, std::vector<int32_t>* out) const {
(void)text;
(void)out;
return RAD_E_UNSUPPORTED;
}
};
/* The audio generator (core/gen/audio_driver.h), for a model that makes music or other audio from
+111 -6
View File
@@ -21,6 +21,7 @@
* `.completed` event per image.
*/
#include "server.h"
#include "loras.h"
#include "oai.h"
#include "gen/gen.h"
@@ -58,6 +59,7 @@ struct ImgReq {
bool has_neg = false;
double cfg = 1.0;
int32_t w = 0, h = 0, n = 1, steps = 0, previews = 0;
int32_t sampler = 0, scheduler = 0;
bool seed_given = false;
uint64_t seed = 0;
std::string background = "auto", format = "png", quality = "auto";
@@ -89,6 +91,11 @@ json form_value(const std::string& key, const std::string& v) {
if (v == "true" || v == "1") return json(true);
if (v == "false" || v == "0") return json(false);
}
/* `loras` as JSON text, or one adapter's name. */
if ((key == "loras" || key == "lora") && !v.empty() && (v[0] == '[' || v[0] == '{')) {
json j = json::parse(v, nullptr, false);
if (!j.is_discarded()) return j;
}
return json(v);
}
@@ -235,7 +242,7 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
"output_compression", "response_format", "stream", "partial_images", "moderation",
"style", "user", "input_fidelity", "seed", "negative_prompt", "true_cfg_scale",
"guidance_scale",
"num_inference_steps", "width", "height",
"num_inference_steps", "width", "height", "loras", "lora", "sampler", "scheduler",
};
for (auto it = body.begin(); it != body.end(); ++it) {
bool known = false;
@@ -262,7 +269,20 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
return error_response(err_bad("prompt", "expected a string"), req.peer);
if (!body.contains("prompt"))
return error_response(err_bad("prompt", "the prompt is required"), req.peer);
if (r.prompt.empty()) r.prompt = " ";
/* Runtime LoRA adapters: `loras` and <lora:...> tags, which leave the prompt. */
std::vector<std::pair<std::string, double>> loras;
{
std::string field, why;
if (parse_loras(body, &r.prompt, &loras, &field, &why) < 0)
return error_response(err_bad(field.c_str(), why), req.peer);
if (!loras.empty() && !opt_.gen_lora_acquire)
return error_response(err_unsupported(field.c_str(), "this server was started without "
"adapters (--lora, --lora-dir)"),
req.peer);
}
/* An empty prompt becomes " ", as in the diffusers pipelines, unless the model pads a short
* prompt itself (ImageGenInfo::min_tokens), as ComfyUI's tokenizers do. */
if (r.prompt.empty() && info.min_tokens == 0) r.prompt = " ";
std::string rf;
if (str("response_format", &rf) < 0 || (!rf.empty() && rf != "b64_json" && rf != "url"))
return error_response(err_bad("response_format", "expected b64_json"), req.peer);
@@ -335,6 +355,39 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
else if (r.quality == "medium") steps = std::max<int64_t>(1, info.default_steps * 3 / 4);
}
r.steps = (int32_t)steps;
/* A discrete-time model's sampler and scheduler, by ComfyUI's names. */
{
std::string sname, kname;
if (str("sampler", &sname) < 0)
return error_response(err_bad("sampler", "expected a string"), req.peer);
if (str("scheduler", &kname) < 0)
return error_response(err_bad("scheduler", "expected a string"), req.peer);
if (!info.samplers && (!sname.empty() || !kname.empty()))
return error_response(err_unsupported(sname.empty() ? "scheduler" : "sampler",
"this model samples with flow-matching Euler "
"on its own schedule"), req.peer);
r.sampler = info.default_sampler;
r.scheduler = info.default_scheduler;
if (!sname.empty()) {
r.sampler = gen::sampler_id(sname);
if (r.sampler < 0 || !(info.samplers & (1u << r.sampler))) {
ApiError e = err_bad("sampler", "'" + sname + "' is not a sampler this model serves");
for (int32_t i = 0; i < RAD_GEN_SAMPLER_COUNT; ++i)
if (info.samplers & (1u << i)) e.supported.push_back(gen::sampler_name(i));
return error_response(e, req.peer);
}
}
if (!kname.empty()) {
r.scheduler = gen::scheduler_id(kname);
if (r.scheduler < 0) {
ApiError e = err_bad("scheduler", "'" + kname + "' is not a scheduler this model "
"serves");
for (int32_t i = 0; i < RAD_GEN_SCHED_COUNT; ++i)
e.supported.push_back(gen::scheduler_name(i));
return error_response(e, req.peer);
}
}
}
/* -1 asks for a random seed, as llama-server and the chat endpoints read it. */
if (body.contains("seed") && !body["seed"].is_null()) {
if (!body["seed"].is_number_integer())
@@ -365,12 +418,12 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
"..30"), req.peer);
}
/* Guidance needs a scale past its off value and a negative prompt: the request's (empty
* becomes " ", as in the reference pipeline), or the model's own. */
* becomes " " as the prompt does), or the model's own. */
const bool guided = r.cfg > cfg_off;
const bool named_neg = body.contains("negative_prompt") && !body["negative_prompt"].is_null();
r.has_neg = guided && (named_neg || info.has_default_negative);
if (r.has_neg && !named_neg) r.negative = info.default_negative;
else if (r.has_neg && r.negative.empty()) r.negative = " ";
else if (r.has_neg && r.negative.empty() && info.min_tokens == 0) r.negative = " ";
if (guided && !r.has_neg)
return error_response(err_bad(cfg_key, "guidance needs a negative_prompt"), req.peer);
@@ -470,6 +523,8 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
g->height = r.h;
g->n = r.n;
g->steps = r.steps;
g->sampler = r.sampler;
g->scheduler = r.scheduler;
g->seed = r.seed;
g->previews = r.previews;
/* The driver guides as uncond + cfg (cond - uncond); a card's cond + s (cond - uncond) is
@@ -585,6 +640,41 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
return RAD_E_STATE;
}
out->n_drop = (int32_t)drop.size();
/* The reference tokenizer's minimum length: an empty prompt is padding the model reads. */
while ((int64_t)out->tokens.size() - out->n_drop < info.min_tokens)
out->tokens.push_back(info.pad_token);
if (info.aux_tokens) {
if (deps_.images->encode_aux(prompt, &out->aux) < 0) {
*err = err_internal("the prompt could not be read by the model's second "
"tokenizer");
return RAD_E_STATE;
}
if ((int64_t)out->aux.size() > info.aux_max_tokens) {
*err = err_bad("prompt", "the prompt is " + std::to_string(out->aux.size()) +
" tokens to this model's second tokenizer, which "
"reads at most " +
std::to_string(info.aux_max_tokens));
return RAD_E_INVAL;
}
}
/* A prompt read in windows: runs of at most chunk_tokens after the prefix, each between
* its markers and padded to the window. */
if (info.chunk_tokens > 0) {
const size_t W = (size_t)info.chunk_tokens;
std::vector<int32_t> win(out->tokens.begin(), out->tokens.begin() + out->n_drop);
const size_t n_body = out->tokens.size() - (size_t)out->n_drop;
size_t at = 0;
do {
const size_t take = std::min(W, n_body - at);
win.push_back(info.chunk_bos);
win.insert(win.end(), out->tokens.begin() + out->n_drop + (std::ptrdiff_t)at,
out->tokens.begin() + out->n_drop + (std::ptrdiff_t)(at + take));
win.push_back(info.chunk_eos);
win.resize(win.size() + (W - take), info.chunk_pad);
at += take;
} while (at < n_body);
out->tokens = std::move(win);
}
if ((int64_t)out->tokens.size() > info.max_cond_tokens) {
*err = err_bad("prompt", "the prompt is " + std::to_string(out->tokens.size()) +
" tokens with its pictures; this deployment conditions on "
@@ -600,6 +690,19 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
return error_response(err, req.peer);
}
/* ---- the adapters' slot, held until the request is done */
std::string lora_note;
if (!loras.empty()) {
std::string why;
const int s = opt_.gen_lora_acquire(loras, &g->lora_slot, &g->lora_hold, &lora_note, &why);
if (s < 0) {
ApiError e = err_bad("loras", why);
if (s == RAD_E_NOTFOUND) { e.status = 404; e.type = "not_found_error"; }
else if (s != RAD_E_INVAL) { e.status = 503; e.type = "server_error"; }
return error_response(e, req.peer);
}
}
/* ---- admitted and queued */
if (auto no = admit(1, req.peer)) return *no;
std::shared_ptr<gen::Ticket> ticket = g->ticket;
@@ -613,9 +716,11 @@ HttpResponse Server::handle_images(const HttpRequest& req, bool edit) {
return error_response(e, req.peer);
}
}
RAD_INFO("images: %s %dx%d n=%d steps=%d seed=%llu%s%s%s", edit ? "edit" : "generation", r.w,
std::string lora_s;
for (const auto& [n, w] : loras) lora_s += (lora_s.empty() ? " loras=" : ",") + n + fmt(":%g", w);
RAD_INFO("images: %s %dx%d n=%d steps=%d seed=%llu%s%s%s%s", edit ? "edit" : "generation", r.w,
r.h, r.n, r.steps, (unsigned long long)r.seed, r.has_neg ? " guided" : "",
r.has_mask ? " masked" : "", r.stream ? " streamed" : "");
r.has_mask ? " masked" : "", r.stream ? " streamed" : "", lora_s.c_str());
/* ---- one finished picture to a file */
const std::string size_s = std::to_string(r.w) + "x" + std::to_string(r.h);
+117
View File
@@ -0,0 +1,117 @@
/* loras.cpp -- see loras.h. */
#include "loras.h"
#include "rad_internal.h"
#include <cmath>
#include <cstdlib>
namespace rad {
namespace server {
namespace {
/* "name" or "name:strength" (the last ':' splits, so a name may hold one). */
bool split_entry(const std::string& s, std::string* name, double* w) {
*w = 1.0;
const size_t c = s.rfind(':');
if (c != std::string::npos && c + 1 < s.size()) {
char* end = nullptr;
const double v = std::strtod(s.c_str() + c + 1, &end);
if (end && *end == '\0' && std::isfinite(v)) {
*name = s.substr(0, c);
*w = v;
return !name->empty();
}
}
*name = s;
return !name->empty();
}
} /* namespace */
int parse_loras(const nlohmann::json& body, std::string* prompt,
std::vector<std::pair<std::string, double>>* out, std::string* field,
std::string* why) {
out->clear();
const char* key = body.contains("loras") ? "loras" : "lora";
if (body.contains(key) && !body[key].is_null()) {
*field = key;
const nlohmann::json& v = body[key];
auto entry = [&](const nlohmann::json& e) -> bool {
std::string name;
double w = 1.0;
if (e.is_string()) {
if (!split_entry(e.get<std::string>(), &name, &w)) return false;
} else if (e.is_object()) {
if (!e.contains("name") || !e["name"].is_string()) return false;
name = e["name"].get<std::string>();
for (const char* k : { "weight", "strength", "scale" })
if (e.contains(k) && !e[k].is_null()) {
if (!e[k].is_number()) return false;
w = e[k].get<double>();
}
} else {
return false;
}
if (name.empty() || !std::isfinite(w)) return false;
out->push_back({ name, w });
return true;
};
if (v.is_array()) {
for (const nlohmann::json& e : v)
if (!entry(e)) {
*why = "each entry is a name, \"name:strength\" or {\"name\", \"weight\"}";
return RAD_E_INVAL;
}
} else if (v.is_object() && !v.contains("name")) {
for (auto it = v.begin(); it != v.end(); ++it) {
if (!it.value().is_number() || !std::isfinite(it.value().get<double>())) {
*why = "a {name: strength} object's strengths are numbers";
return RAD_E_INVAL;
}
out->push_back({ it.key(), it.value().get<double>() });
}
} else if (!entry(v)) {
*why = "expected a list of adapters (names, \"name:strength\" or {\"name\", \"weight\"})";
return RAD_E_INVAL;
}
}
/* <lora:name[:strength]> tags in the prompt. */
if (prompt) {
std::string p;
size_t at = 0;
for (;;) {
const size_t open = prompt->find("<lora:", at);
if (open == std::string::npos) { p += prompt->substr(at); break; }
const size_t close = prompt->find('>', open);
if (close == std::string::npos) { p += prompt->substr(at); break; }
std::string name;
double w = 1.0;
if (!split_entry(prompt->substr(open + 6, close - open - 6), &name, &w)) {
*field = "prompt";
*why = "a <lora:...> tag names no adapter";
return RAD_E_INVAL;
}
out->push_back({ name, w });
p += prompt->substr(at, open - at);
at = close + 1;
}
if (p.size() != prompt->size()) {
/* Tidy the gaps the tags leave: runs of spaces, and commas with nothing between. */
std::string t;
for (char c : p) {
if (c == ' ' && (t.empty() || t.back() == ' ')) continue;
t += c;
}
for (size_t k; (k = t.find(", ,")) != std::string::npos;) t.erase(k, 2);
for (size_t k; (k = t.find(",,")) != std::string::npos;) t.erase(k, 1);
while (!t.empty() && (t[0] == ' ' || t[0] == ',')) t.erase(0, 1);
while (!t.empty() && (t.back() == ' ' || t.back() == ',')) t.pop_back();
*prompt = t.empty() ? std::string(" ") : t;
}
}
return RAD_OK;
}
} /* namespace server */
} /* namespace rad */
+30
View File
@@ -0,0 +1,30 @@
/* loras.h -- which runtime LoRA adapters an image or video request asks for, and at what strength.
*
* Two spellings, merged:
*
* `loras` in the body: a list whose entries are a name, "name:strength", or an object
* {"name": ..., "weight" | "strength" | "scale": ...}; or an object {name: strength}.
* `lora` is the same field.
*
* <lora:name> or <lora:name:strength> in the prompt, as Stable Diffusion web UIs write it: the
* tag is taken out of the prompt, the text around it kept.
*
* A strength not given is 1. */
#pragma once
#include <nlohmann/json.hpp>
#include <string>
#include <utility>
#include <vector>
namespace rad {
namespace server {
/* RAD_OK with the adapters in `out` (possibly none) and their tags removed from *prompt;
* RAD_E_INVAL with `why` for a malformed entry. `field` receives the field the error is about. */
int parse_loras(const nlohmann::json& body, std::string* prompt,
std::vector<std::pair<std::string, double>>* out, std::string* field,
std::string* why);
} /* namespace server */
} /* namespace rad */
+17
View File
@@ -132,6 +132,7 @@ void Server::register_routes() {
return r;
});
router_.get ("/v1/models", [this](const HttpRequest& q) { return handle_models(q); });
router_.get ("/v1/loras", [this](const HttpRequest& q) { return handle_loras(q); });
router_.get ("/v1/models/*id", [this](const HttpRequest& q) { return handle_models(q); });
router_.post("/v1/load_lora_adapter", [this](const HttpRequest& q) { return handle_lora_admin(q, true); });
router_.post("/v1/unload_lora_adapter", [this](const HttpRequest& q) { return handle_lora_admin(q, false); });
@@ -1864,6 +1865,22 @@ HttpResponse Server::handle_models(const HttpRequest& req) {
/* Runtime LoRA admin, vLLM's routes and bodies: {"lora_name", "lora_path"} to load, {"lora_name"}
* to unload. The path is read on this machine, so without --api-key only a loopback client may
* ask, the rule /diagnostics follows. */
/* A generator's adapter files: what `loras` and <lora:...> tags may name. */
HttpResponse Server::handle_loras(const HttpRequest& req) {
using json = nlohmann::json;
if (!opt_.gen_lora_names) {
ApiError e;
e.status = 404;
e.type = "not_found_error";
e.message = "this server takes no adapters in image or video requests (--lora, --lora-dir)";
return error_response(e, req.peer);
}
json data = json::array();
for (const std::string& n : opt_.gen_lora_names()) data.push_back({ { "id", n }, { "object", "lora" } });
metrics_.observe_http(200);
return HttpResponse::json_body(200, json_dump(json{ { "object", "list" }, { "data", std::move(data) } }));
}
HttpResponse Server::handle_lora_admin(const HttpRequest& req, bool load) {
using json = nlohmann::json;
auto fail = [&](int status, const char* type, const std::string& msg) {
+7
View File
@@ -141,6 +141,12 @@ struct ServerOptions {
* of an adapter requests still use drains it: RAD_OK with *in_flight > 0 (Engine::lora_unload). */
std::function<int(const std::string&, const std::string&, std::string*)> lora_load;
std::function<int(const std::string&, std::string*, int*)> lora_unload;
/* A generator's adapters (Engine::gen_lora_acquire): the images and videos endpoints' `loras`
* and <lora:...> tags get a slot holding that combination, a hold on it and a note on what was
* left out; `gen_lora_names` is GET /v1/loras. Unset without adapters. */
std::function<int(const std::vector<std::pair<std::string, double>>&, int32_t*,
std::shared_ptr<void>*, std::string*, std::string*)> gen_lora_acquire;
std::function<std::vector<std::string>()> gen_lora_names;
std::string api_key;
bool cors = true;
@@ -240,6 +246,7 @@ private:
void realtime_run(RealtimeSession& s, WsConn& ws);
bool realtime_loop(RealtimeSession& s, WsConn& ws);
HttpResponse handle_models(const HttpRequest& req);
HttpResponse handle_loras(const HttpRequest& req);
HttpResponse handle_metrics(const HttpRequest& req);
std::optional<HttpResponse> refuse_text(const std::string& peer,
ApiShape shape = ApiShape::OpenAI);
+26 -3
View File
@@ -39,6 +39,7 @@
* special tokens and the prompt without, so a caller cannot write control tokens into it.
*/
#include "server.h"
#include "loras.h"
#include "oai.h"
#include "gen/video.h"
@@ -168,7 +169,9 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
return error_response(err_bad(p.name, "'" + p.name + "' takes text, not a file"),
req.peer);
json v = p.data;
if (p.name == "target" || p.name == "conditions") {
if (p.name == "target" || p.name == "conditions" ||
((p.name == "loras" || p.name == "lora") && !p.data.empty() &&
(p.data[0] == '[' || p.data[0] == '{'))) {
v = json::parse(p.data, nullptr, false);
if (v.is_discarded())
return error_response(err_bad(p.name, "expected JSON"), req.peer);
@@ -183,7 +186,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
static const char* kKnown[] = {
"model", "prompt", "seconds", "size", "input_reference", "seed", "num_inference_steps",
"task", "target", "conditions", "user", "latent_upscale", "upscale_refine_steps",
"negative_prompt", "decoder",
"negative_prompt", "decoder", "loras", "lora",
/* RadVideoGenBatch::guidance_h's order */
"guidance_scale", "stg_scale", "modality_scale", "guidance_rescale",
"audio_guidance_scale", "audio_stg_scale", "audio_modality_scale", "audio_guidance_rescale",
@@ -210,7 +213,17 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
if (!has(body, "prompt") || !body["prompt"].is_string() ||
body["prompt"].get<std::string>().find_first_not_of(" \t\r\n") == std::string::npos)
return bad("prompt", "the prompt is required, as a non-empty string");
const std::string prompt = body["prompt"].get<std::string>();
std::string prompt = body["prompt"].get<std::string>();
/* Runtime LoRA adapters: `loras` and <lora:...> tags, which leave the prompt. */
std::vector<std::pair<std::string, double>> loras;
{
std::string field, why;
if (parse_loras(body, &prompt, &loras, &field, &why) < 0) return bad(field.c_str(), why);
if (!loras.empty() && !opt_.gen_lora_acquire)
return error_response(err_unsupported(field.c_str(), "this server was started without "
"adapters (--lora, --lora-dir)"),
req.peer);
}
if (has(body, "input_reference")) {
/* JSON: a data URL, as a string or an image_url object. */
const json& v = body["input_reference"];
@@ -497,6 +510,16 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
}
/* ---- admitted and queued: the job holds its place until its file is written */
if (!loras.empty()) {
std::string why, note;
const int s = opt_.gen_lora_acquire(loras, &r->lora_slot, &r->lora_hold, &note, &why);
if (s < 0) {
ApiError e = err_bad("loras", why);
if (s == RAD_E_NOTFOUND) { e.status = 404; e.type = "not_found_error"; }
else if (s != RAD_E_INVAL) { e.status = 503; e.type = "server_error"; }
return error_response(e, req.peer);
}
}
if (auto no = admit(1, req.peer)) return *no;
auto j = std::make_shared<VideoJob>();
j->id = new_video_id();