sampler: res_multistep (DPM++2M data-pred) as a per-request field; ABI 24

Radiance's video denoising had only first-order Euler. The DaSiWa reference
workflow samples MiniMax-H3 with res_multistep + simple, so add it as a second
order option, exposed as a `sampler` field on /v1/videos (default euler).

New op `flow_res_multistep` across all three kernel tiers (device==avx==libref),
closed form reusing the driver's per-step scalars:
  x' = r*x + hb1*den + hb2*x0,  den = x + sigma*v,  r = sigma_next/sigma
  order 2: h=ln(sg/sgn), h_last=ln(sg_prev/sg), r_s=h_last/h,
           hb1=(1-r)(1+1/(2 r_s)), hb2=-(1-r)/(2 r_s)
hb1+hb2 == 1-r, so it collapses to Euler when x0==den. Order 1 (Euler) is forced
at the first step, the first full-size step after a latent upscale (stale
history), and the last step (sigma_next==0); order 2 otherwise. The previous
denoised estimate persists in new PERSIST buffers b_vx0/b_ax0. Verified equal to
k-diffusion sample_dpmpp_2m to 5.6e-17 on three sigma grids.

- ABI 23->24: trailing int32_t sampler on RadVideoGenBatch; RAD_SAMPLER_EULER=0
  / RAD_SAMPLER_RES_MULTISTEP=1 in rad_runtime.h.
- driver: R_SCHED region widened 6->16 f32 ([0..5] Euler, [6..10] video coeffs,
  [11..15] audio); step_dit branches on V->sampler.
- server: `sampler` string field ("euler"/empty, "res_multistep", else
  invalid_request_error with the supported list); echoed on the job object;
  capabilities advertise video_samplers.

Tests: rad-avx-check 816 + 451 hand-built, 0 fail (left-assoc grouping matches
ref/device at tol 0); r4d_selftest --case flow_res_multistep_f32 f32|bf16 x
order {1,2} exact, both INOUT pitch gaps untouched.

Device-verified (TP2, fp8 container, 256^2/2s/10-step, seed 1234 identical):
both samplers complete clean 73-frame h264; outputs differ (branch genuinely
consumed, not ignored); bad value rejected; field omitted -> euler. res_multistep
renders warmer/higher-contrast than euler -- a tone lever, not an over-exposure
fix. The euler default path is unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-20 18:28:49 -05:00
1 parent c8c8f93789
commit 8785774fc0
18 files changed
+391 -12

No files matched your search

+3
View File
@@ -119,6 +119,9 @@ struct VideoRequest {
int32_t width = 0, height = 0; /* pixels, within the declaration's limits */
int32_t chunks = 0; /* the length in chunks (RadVideoGenDecl) */
int32_t steps = 0; /* sigma grid points; 0: the model's default */
/* The sampler: a RAD_SAMPLER_* value (abi/rad_runtime.h). 0 = Euler, the default; 1 =
* res_multistep, the second-order step on the clean estimate. */
int32_t sampler = 0;
/* Latent upscaling (abi/rad_builder.h, RadVideoGenDecl): the factor, 0 for none, and the
* evaluations at the full size after it, 0 for the model's default. */
double upscale = 0.0;
+43 -4
View File
@@ -165,7 +165,8 @@ int64_t carve(const RadVideoGenDecl& d, int64_t max_tok, int64_t ep, int64_t pd,
2 * 4, /* R_CU */
3 * max_tok * 4, /* R_ROPE: a step's rotary positions, a conditioning's [3,T] M-RoPE */
RAD_VGEOM_N * 4, /* R_GEOM */
6 * 4, /* R_SCHED */
16 * 4, /* R_SCHED: the Euler scalars [0..5], then the res_multistep
* coeffs -- video [6..10], audio [11..15] */
d.max_steps * 4, /* R_TV */
d.max_steps * 4, /* R_TA */
4 * 4, /* R_SEED */
@@ -886,6 +887,7 @@ void VideoDriver::base_batches(const Job& j, const RadVideoGeom& g, int64_t n_to
V.t_video = dptr<float>(ri, R_TV);
V.t_audio = dptr<float>(ri, R_TA);
V.step = j.step;
V.sampler = j.req->sampler;
V.sched = dptr<float>(ri, R_SCHED);
V.pos = dptr<float>(ri, R_ROPE);
V.seed = dptr<uint32_t>(ri, R_SEED);
@@ -1052,8 +1054,21 @@ const RadBatch* VideoDriver::build_cond(Job& j, int step) {
return &out_[0];
}
/* Step i's timesteps and sigmas, the video's then the soundtrack's (RadVideoGenBatch::sched);
* with `to_clean` the next sigmas are 0, so the step lands on its clean estimate. */
/* Step i's timesteps, sigmas and sampler coefficients (RadVideoGenBatch::sched). With `to_clean` the
* next sigmas are 0, so the step lands on its clean estimate. Both samplers' scalars are staged
* whichever runs, so the plugin reads the slots its sampler names:
* [0..5] Euler: video (t, sigma, sigma_next) then soundtrack.
* [6..10] res_multistep, video: sigma (= the velocity's weight), r = sigma_next/sigma, hb1, hb2,
* order (1 or 2).
* [11..15] res_multistep, soundtrack: the same five.
* The coefficients are DPM-Solver++2M data-prediction / k-diffusion res_multistep at eta=0: with
* h = log(sigma/sigma_next), h_last = log(sigma_prev/sigma) and rs = h_last/h,
* hb1 = (1 - r)(1 + 1/(2 rs)), hb2 = -(1 - r)/(2 rs), order = 2.
* They are used only on a step that has a previous estimate on the SAME latent grid, so `order` is 1
* (plain Euler, hb unused) on the first step, on the first full-size step after a latent upscale
* (i == split, whose grid and rows differ from the small run's stale history) and on a step whose
* next sigma is 0 (the last, or the last small step). hb1 + hb2 == 1 - r on the two-estimate steps,
* so every branch shares the Euler limit. */
void VideoDriver::stage_sched(const Job& j, int32_t i, bool to_clean) {
float* s = hptr<float>(R_SCHED);
s[0] = 1.0f - j.sched.sig_v[(size_t)i];
@@ -1062,7 +1077,31 @@ void VideoDriver::stage_sched(const Job& j, int32_t i, bool to_clean) {
s[3] = 1.0f - j.sched.sig_a[(size_t)i];
s[4] = j.sched.sig_a[(size_t)i];
s[5] = to_clean ? 0.0f : j.sched.sig_a[(size_t)i + 1];
reg_[R_SCHED].used = 6 * 4;
/* A stream's res_multistep coefficients, written to out[0..4]. `prev` is the step before this
* one's sigma (>= i so i > 0), and the restart step (i == split) forces Euler. */
auto coeffs = [&](const std::vector<float>& sig, float* out) {
const float sg = sig[(size_t)i];
const float sgn = to_clean ? 0.0f : sig[(size_t)i + 1];
const float r = sg > 0.0f ? sgn / sg : 0.0f;
float hb1 = 1.0f - r, hb2 = 0.0f, order = 1.0f;
const bool two = sgn > 0.0f && i > 0 && !(j.split > 0 && i == j.split);
if (two) {
const float h = std::log(sg / sgn);
const float h_last = std::log(sig[(size_t)i - 1] / sg);
const float rs = h_last / h;
hb1 = (1.0f - r) * (1.0f + 1.0f / (2.0f * rs));
hb2 = -(1.0f - r) / (2.0f * rs);
order = 2.0f;
}
out[0] = sg;
out[1] = r;
out[2] = hb1;
out[3] = hb2;
out[4] = order;
};
coeffs(j.sched.sig_v, s + 6);
coeffs(j.sched.sig_a, s + 11);
reg_[R_SCHED].used = 16 * 4;
}
/* The video's noise seed is the request's; the soundtrack's the same with its top bit flipped, so
+18
View File
@@ -59,6 +59,7 @@ struct Server::VideoJob {
std::string id, model, prompt, seconds, size;
int64_t created_at = 0;
uint64_t seed = 0;
int32_t sampler = 0; /* the request's RAD_SAMPLER_* */
std::shared_ptr<gen::VideoTicket> ticket;
std::thread worker;
/* Under Server::videos_m_ from here on. */
@@ -271,6 +272,8 @@ void Server::video_caps(json& caps, const IVideoGen* gen) {
/* The containers `format` accepts; mp4 is the default and always available, the webm codecs
* only when this build's libavcodec has the encoder and muxer for them. */
caps["video_formats"] = json::array({ "mp4", "webm", "webm-av1" });
/* The samplers `sampler` accepts; euler is the default and always available. */
caps["video_samplers"] = json::array({ "euler", "res_multistep" });
}
HttpResponse Server::handle_video_create(const HttpRequest& req) {
@@ -343,6 +346,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", "format",
"sampler",
};
for (auto it = body.begin(); it != body.end(); ++it) {
bool known = false;
@@ -553,6 +557,18 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
" (sigma grid points, the last one 0)");
r->steps = (int32_t)v;
}
if (has(body, "sampler")) {
const std::string sm = body["sampler"].is_string() ? body["sampler"].get<std::string>() : "";
if (sm.empty() || sm == "euler") r->sampler = RAD_SAMPLER_EULER;
else if (sm == "res_multistep") r->sampler = RAD_SAMPLER_RES_MULTISTEP;
else {
ApiError e = err_unsupported("sampler", "the sampler is euler (first order) or "
"res_multistep (second order on the clean "
"estimate)");
e.supported = { "euler", "res_multistep" };
return error_response(e, req.peer);
}
}
if (has(body, "latent_upscale")) {
double v = 0;
if (!(info.upscale_max > 0.0))
@@ -770,6 +786,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
j->size = std::to_string(W) + "x" + std::to_string(H);
j->created_at = unix_now();
j->seed = r->seed;
j->sampler = r->sampler;
j->fmt = fmt;
for (const gen::VideoRefMedia& m : r->refs) j->refs.push_back(m.name);
j->ticket = std::make_shared<gen::VideoTicket>();
@@ -955,6 +972,7 @@ json Server::video_json(const VideoJob& j) {
o["size"] = j.size;
o["seed"] = j.seed;
o["format"] = format_name(j.fmt); /* what /content will return */
o["sampler"] = j.sampler == RAD_SAMPLER_RES_MULTISTEP ? "res_multistep" : "euler";
if (j.refine > 0) {
o["latent_upscale"] = j.upscale;
o["upscale_refine_steps"] = j.refine;