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

+18
View File
@@ -2154,6 +2154,24 @@ void video_cases(std::vector<Hand>& hs) {
hs.push_back(std::move(h));
}
/* ---- the second-order sampler: a bf16 velocity, f32 state AND estimate history, both
* INOUT at wider pitches; order=2 leans on the history, order=1 is the Euler limit -- */
for (float order : { 2.0f, 1.0f }) {
Hand h((std::string(order >= 1.5f ? "two-estimate" : "Euler limit") +
", bf16 velocity, f32 state and history" + sfx).c_str(),
"flow_res_multistep", 0.0);
h.dense(dt, { 7, 96 }, hfill(422, 2.0f))
.view(RAD_F32, { 7, 96 }, { 99, 1 }, 7 * 99, hfill(423), true) /* x0, INOUT */
.dense(RAD_F32, { 1 }, [](int64_t) { return 0.5625f; }) /* sigma */
.dense(RAD_F32, { 1 }, [](int64_t) { return 0.625f; }) /* r */
.dense(RAD_F32, { 1 }, [](int64_t) { return 0.24f; }) /* hb1 */
.dense(RAD_F32, { 1 }, [](int64_t) { return -0.005f; }) /* hb2 */
.dense(RAD_F32, { 1 }, [order](int64_t) { return order; }) /* order */
.view(RAD_F32, { 7, 96 }, { 101, 1 }, 7 * 101, hfill(424), true) /* x, INOUT */
.pi("M", 7).pi("n", 96).ps("dtype", dn);
hs.push_back(std::move(h));
}
/* ---- the decoder's geometry: 3 frames of 6 x 9 latents, tiles of 4 over 1 ------------ *
* Rows split 6 -> starts 0, 2 (overlap 2); columns 9 -> 0, 2, 5 (overlaps 2, 1). */
{
+51
View File
@@ -694,6 +694,57 @@ AVX_KERNEL(flow_x0_step) {
return RAD_OK;
}
/* x' = f32(f32(r x) + f32(hb1 d) + f32(hb2 x0)) when order >= 2, else the Euler form
* f32(f32(r x) + f32((1 - r) d)), with d = f32(x + f32(sigma v)): libref's step bit for bit, `keep`
* holding each product to its own rounding. x0 is the previous estimate (read here, d written back). */
AVX_KERNEL(flow_res_multistep) {
if (!rad_args_have(a, 8)) return RAD_E_INVAL;
const RadTensor *V = &a->t[0], *X0 = &a->t[1], *SG = &a->t[2], *RR = &a->t[3];
const RadTensor *HB1 = &a->t[4], *HB2 = &a->t[5], *ORD = &a->t[6], *X = &a->t[7];
const int64_t n = p_int(a, "n", 0);
if (n <= 0) return RAD_E_SHAPE;
const int64_t rows = rows_of(X, n);
if (rows <= 0 || rows_of(V, n) < rows || rows_of(X0, n) < rows || numel(SG) < 1 ||
numel(RR) < 1 || numel(HB1) < 1 || numel(HB2) < 1 || numel(ORD) < 1)
return RAD_E_SHAPE;
const float sg = ldt(SG, offlin(SG, 0));
const float r = ldt(RR, offlin(RR, 0));
const float b1 = ldt(HB1, offlin(HB1, 0));
const float b2 = ldt(HB2, offlin(HB2, 0));
const bool two = ldt(ORD, offlin(ORD, 0)) >= 1.5f;
const float omr = 1.0f - r;
AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN)
for (int64_t k = 0; k < rows; ++k) {
float* buf = scratch_f32((size_t)(5 * n + 64));
float *vs = buf, *xs = buf + n + 16, *h0s = xs + n + 16, *o = h0s + n + 16, *o0 = o + n + 16;
const float* v = in_row(V, rowoff(V, k, n), n, vs);
const float* x = in_row(X, rowoff(X, k, n), n, xs);
const float* h0 = in_row(X0, rowoff(X0, k, n), n, h0s);
const vf vsg = vf_set1(sg), vr = vf_set1(r), vb1 = vf_set1(b1), vb2 = vf_set1(b2),
vo = vf_set1(omr);
int64_t c = 0;
for (; c + VF_N <= n; c += VF_N) {
const vf xv = vf_loadu(x + c);
const vf d = vf_add(xv, keep(vf_mul(vsg, vf_loadu(v + c))));
const vf a = keep(vf_mul(vr, xv));
vf xo = two ? vf_add(vf_add(a, keep(vf_mul(vb1, d))), keep(vf_mul(vb2, vf_loadu(h0 + c))))
: vf_add(a, keep(vf_mul(vo, d)));
vf_storeu(o + c, xo);
vf_storeu(o0 + c, d);
}
for (; c < n; ++c) {
const float d = x[c] + keep(sg * v[c]);
const float a = keep(r * x[c]);
o[c] = two ? (a + keep(b1 * d)) + keep(b2 * h0[c]) : a + keep(omr * d);
o0[c] = d;
}
out_row(X, rowoff(X, k, n), n, o);
out_row(X0, rowoff(X0, k, n), n, o0);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ chan_affine */
/* A per-channel affine from two lists of n bf16 values: `fma` is bf16(bf16(x * mul) + add), `inv`
* is bf16(bf16(x - add) / mul), the inner rounding in y's dtype. */
+1
View File
@@ -111,6 +111,7 @@ int avx_par_force(int threads);
X(scatter_add_rows) X(attn_prefix_self) \
/* ---- video generation */ \
X(rms_mod) X(rms_mod_quant_fp8) X(flow_x0_step) X(patch_unfold) \
X(flow_res_multistep) \
X(vtile_gather) X(vtile_stitch) X(grid_pos3) X(vsa_select) \
X(attn_vsa) X(conv3d_ndhwc) X(group_norm) X(tconv_dw) \
X(resize_bilinear) X(patch_fold) X(flow_renoise) \
+7
View File
@@ -485,6 +485,8 @@ static const RadOperandSpec oRmsModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT
OUT_O("z") };
static const RadOperandSpec oFlowX0[] = { OPD("v"), OPD("t"), OPD("sigma"), OPD("sigma_next"),
INOUT("x") };
static const RadOperandSpec oFlowRM[] = { OPD("v"), INOUT("x0"), OPD("sigma"), OPD("r"),
OPD("hb1"), OPD("hb2"), OPD("order"), INOUT("x") };
static const RadParamSpec pTimestep[] = { P_INT("M"), P_INT("n"), P_F64("scale"), P_F64("max_period"),
P_INTO("round"), P_F64O("clamp_min"), P_STR("dtype") };
static const RadOperandSpec oTimestep[] = { OPD("t"), OUT("y") };
@@ -938,6 +940,9 @@ static const RadOpSchema kSchemas[] = {
{ "flow_x0_step", ARR(pMnD), ARR(oFlowX0),
"x = r x + (1 - r)(x + (1 - t) v), r = sigma_next / sigma, each step one f32 rounding, bit for "
"bit libref's." },
{ "flow_res_multistep", ARR(pMnD), ARR(oFlowRM),
"x = r x + hb1 (x + sigma v) + hb2 x0, r = sigma_next / sigma; order<2 is Euler "
"(x = r x + (1 - r)(x + sigma v)); bit for bit libref's." },
{ "patch_unfold", ARR(pPatchUnfold), ARR(oPatchUnfold),
"patch rows to denormalised voxel rows, f32(f32(x * mul) + add), bit for bit libref's." },
{ "vtile_gather", ARR(pVtileGather), ARR(oVtileGather),
@@ -1419,6 +1424,8 @@ SIMPLE("avx_rms_mod_quant_fp8", "rms_mod_quant_fp8", "norm",
L(rms_mod_quant_fp8)),
SIMPLE("avx_flow_x0_step", "flow_x0_step", "elem", "a sampler step toward the clean estimate",
DT_FLOAT, L(flow_x0_step)),
SIMPLE("avx_flow_res_multistep", "flow_res_multistep", "elem",
"a second-order sampler step on the clean estimate", DT_FLOAT, L(flow_res_multistep)),
SIMPLE("avx_patch_unfold", "patch_unfold", "elem", "patch rows to denormalised voxel rows",
DT_FLOAT, L(patch_unfold)),
SIMPLE("avx_vtile_gather", "vtile_gather", "elem", "a video decoder's tiles out of the latent",