ops+minimaxh3+driver: the condition-noise augmentation the reference adds to visual cond rows

The reference's _cond_video_rows runs every visual conditioning latent through
x = aug x + (1 - aug) z at VISUAL_COND_TIMESTEP 0.999, its generator restarted
per item, while audio conditioning is untouched (its aug is 1.0, a no-op). We
staged the clean latent instead, so every image-, keyframe- and reference-
conditioned render started from un-augmented conditioning.

New op cond_aug: x = f32(f32(a x) + f32(oma z)), a the aug parameter and oma
the f32 of the double 1 - aug -- the reference subtracts in double and rounds
once -- with z the (seed, stream) normal stream at element k n + c on stream
2, the reference's noise never sharing the generator that fills x. Seed words
0-1 of the video's seed, per-item restart by per-row offset. Issued once at
the end of step_venc over the conditioning rows: staging once at encode-build
time is equivalent to the reference's per-call restart because each item's
rows are exactly its own, and the driver's build_encode now re-stages the
seed, where a stale lane's words used to ride through.

Reference RNG is MT19937 and ours Philox, so the noise is distributionally
faithful, not bit-exact. ABI unchanged at 28. Device: r4d_selftest 0 failures,
cond_aug rows 1.13e-9/4.42e-8 rel_l2 against libref.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-25 08:45:29 -05:00
1 parent 8c547fe240
commit 47b7aae7a3
16 files changed
+320 -4

No files matched your search

+39
View File
@@ -656,3 +656,42 @@ int ref_flow_renoise(const RadArgs* a, RadStream) {
}
return RAD_OK;
}
/* ------------------------------------------------------------------ cond_aug */
/* A visual condition's patchified rows with the reference's condition-noise augmentation, its
* `_cond_video_rows` (comfy/ldm/minimax/model.py): every condition latent is blended with fresh
* noise before it becomes a condition row, and every condition intentionally restarts the same
* RNG stream, so element e = k n + c of row k is
*
* x[k, c] = f32(f32(a x[k, c]) + f32(oma z(k n + c)))
*
* a = `aug` (the reference's VISUAL_COND_TIMESTEP, 0.999), oma the one cast to f32 of the f64
* 1 - aug (the reference subtracts that scalar in python double and casts per tensor op),
* z(e) = rad_philox_normal_s(seed, stream, e) (abi/rad_philox.h), `seed` two u32 words low first
* -- the request's seed -- and `stream` the parameter. x is f32 [M, n] at its row pitch.
* The reference redraws the same seed every forward, so the blend is a fixed function of the
* request; the arch applies it once to the encoded condition rows, per media item, which is
* exactly the reference's per-item restart. */
int ref_cond_aug(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *seed = &a->t[0], *x = &a->t[1];
const int64_t n = p_int(a, "n", 0), stream = p_int(a, "stream", 0);
const RadParam* ap = p_find(a, "aug");
if (!ap || ap->kind != RAD_P_F64) return RAD_E_SHAPE;
const double aug = ap->dval;
if (n <= 0 || stream < 0 || !(aug >= 0.0 && aug < 1.0) || numel(seed) < 2) return RAD_E_SHAPE;
const int64_t rows = rows_of(x, n);
if (rows <= 0) return RAD_E_SHAPE;
const float av = (float)aug, omv = (float)(1.0 - aug);
const uint64_t sd = (uint64_t)(uint32_t)ldi(seed, 0) | ((uint64_t)(uint32_t)ldi(seed, 1) << 32);
const int64_t xs = laststride(x);
REF_PAR_FOR(rows * n * 8)
for (int64_t k = 0; k < rows; ++k) {
const int64_t xb = rowoff(x, k, n);
for (int64_t c = 0; c < n; ++c) {
const float z = rad_philox_normal_s(sd, (uint64_t)stream, (uint64_t)(k * n + c));
stt(x, xb + c * xs, av * ldt(x, xb + c * xs) + omv * z);
}
}
return RAD_OK;
}