Archived
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:
1 parent
8c547fe240
commit
47b7aae7a3
16 files changed
+320
-4
No files matched your search
@@ -551,7 +551,7 @@ struct Model {
|
||||
rad_weight ve_cinw = 0, ve_cinb = 0, ve_now = 0, ve_nob = 0, ve_coutw = 0, ve_coutb = 0;
|
||||
rad_weight ve_qw = 0, ve_qb = 0;
|
||||
rad_op op_ve_pix = 0, op_ve_pcast = 0, op_ve_cin = 0, op_ve_nout = 0, op_ve_cout = 0;
|
||||
rad_op op_ve_q = 0, op_ve_fold = 0;
|
||||
rad_op op_ve_q = 0, op_ve_fold = 0, op_eaug = 0;
|
||||
std::string ve_pix_mul, ve_pix_add, ve_fold_mul, ve_fold_add; /* pixel and latent norms */
|
||||
rad_buf b_epix32 = 0, b_epix = 0, b_eq48 = 0, b_eq2 = 0, b_egn = 0;
|
||||
rad_buf b_evc = 0; /* [kEncSlotsV * kEncSlotRowsV, kPatchVals] f32 PERSIST */
|
||||
@@ -2719,7 +2719,23 @@ static int declare_venc(RadBuilder* b, const RadModelMeta* meta, Model& m) {
|
||||
RAD_STR("dtype", "f32")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.b_eq2 }, { m.b_evc });
|
||||
if (!m.op_ve_nout || !m.op_ve_cout || !m.op_ve_q || !m.op_ve_fold) return RAD_E_INVAL;
|
||||
/* Condition-noise augmentation: the reference blends every visual condition's patchified
|
||||
* latents with fresh noise before they become condition rows, `r = a r + (1 - a) z` with
|
||||
* a = VISUAL_COND_TIMESTEP = 0.999 (`_cond_video_rows`, comfy/ldm/minimax/model.py); the
|
||||
* audio twin's aug defaults to 1.0 -- disabled -- so no audio-side port exists. `aug` is a
|
||||
* recipe constant, not a per-request value, hence a declared parameter and no staged sigma
|
||||
* tensor. z is the request seed's stream 2 (0 is randn's per-entry lanes, 1 the upscaler's
|
||||
* re-noise), restarted per media item at issue -- the reference's per-item restart. The
|
||||
* reference redraws the same blend every forward, so applying it once to the encoded rows
|
||||
* is exactly equivalent. */
|
||||
m.op_eaug = rw(b, RAD_OP(b, "cond_aug",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, kEncSlotRowsV), RAD_INT("n", kPatchVals),
|
||||
RAD_INT("stream", 2), RAD_F64("aug", 0.999),
|
||||
RAD_STR("dtype", "f32")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.b_evc }, { m.b_evc });
|
||||
if (!m.op_ve_nout || !m.op_ve_cout || !m.op_ve_q || !m.op_ve_fold || !m.op_eaug)
|
||||
return RAD_E_INVAL;
|
||||
/* Debug-only f32 shadow of the raw (pre-fold) quant_conv output, so the generator can read the
|
||||
* encoder's latent BEFORE the fixed-norm fold and tell whether a latent-scale gap enters above
|
||||
* the fold (encoder convs) or is introduced by the fold itself. A cast, not a math change. */
|
||||
@@ -4373,6 +4389,14 @@ static void step_venc(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
brow_slice(m.b_evc, frow, flat * ppc, kPatchVals));
|
||||
frow += flat * ppc;
|
||||
}
|
||||
/* The reference's condition-noise augmentation over this item's condition rows: the issue
|
||||
* spans [row0, frow) -- the whole item, its clips' fold outputs included -- so element e is
|
||||
* numbered from 0 at the item's first row, the reference's per-latent `manual_seed(seed)`
|
||||
* restart. b_evc is PERSIST and every kind issued here is a visual condition (the audio
|
||||
* path is step_aenc, whose reference aug defaults to off), so once per encode is exact. */
|
||||
if (frow > row0)
|
||||
RAD_ISSUE_N(c, m.op_eaug, frow - row0, praw(V->seed, RAD_U32, 2),
|
||||
brow_slice(m.b_evc, row0, frow - row0, kPatchVals));
|
||||
}
|
||||
|
||||
/* The audio VAE encoder over one media item: `enc_samples`, channel-major and zero-padded, each
|
||||
|
||||
@@ -1733,6 +1733,9 @@ const RadBatch* VideoDriver::build_encode(Job& j, int step) {
|
||||
if (enc_dbg_)
|
||||
enc_dbg_dump_file("encg_" + std::to_string((int)it.kind) + "_" +
|
||||
std::to_string((int)it.slot), gd, 25 * 4);
|
||||
/* The video encoder's condition-noise blend draws from the request seed (step_venc), and a
|
||||
* reused lane's R_SEED still holds the previous request's -- stage it like build_step does. */
|
||||
stage_seed(j);
|
||||
stage_geom(j, j.geom, j.rows, 0);
|
||||
if (upload() < 0) return nullptr;
|
||||
base_batches(j, j.geom, 0, step);
|
||||
|
||||
+23
@@ -1729,6 +1729,29 @@ Every op honours its activation operands' row pitches. `rd` is a rounding into t
|
||||
first, `stream` the parameter. `x` f32 `[M, n]` at its row pitch. MiniMax-H3: `n` 96 (the video's
|
||||
patch rows) and 32 (the soundtrack's), `stream` 1.
|
||||
|
||||
### Condition-noise augmentation
|
||||
|
||||
MiniMax-H3's reference blends every visual condition's patchified latents with fresh noise before
|
||||
they condition the transformer (`_cond_video_rows`, `comfy/ldm/minimax/model.py`: the blend weight
|
||||
is `VISUAL_COND_TIMESTEP` 0.999 and every condition intentionally restarts the same RNG stream).
|
||||
The arch applies the blend once, per media item, over the item's encoded rows after the VAE
|
||||
encoder's fold. Unlike `flow_renoise`'s staged `sigma` the weight is a declared parameter: it is a
|
||||
constant of the recipe, not a value the request carries. The audio twin's aug defaults to 1.0 —
|
||||
disabled — so no audio-side form exists.
|
||||
|
||||
| op | params | operands |
|
||||
|---|---|---|
|
||||
| `cond_aug` | `M`(range) `n` `stream` `aug`:f64 `dtype` | `seed`, `x`:inout |
|
||||
|
||||
- `cond_aug` blends condition rows with fresh noise, over `x`'s rows:
|
||||
`x[k, c] = f32(f32(a x[k, c]) + f32(oma z(k n + c)))`, `a` = `aug` in `[0, 1)`, `oma` the f32 of
|
||||
the f64 `1 - aug` computed in double (the reference subtracts that scalar in python),
|
||||
`z(e) = rad_philox_normal_s(seed, stream, e)` (`abi/rad_philox.h`), `seed` u32 `[2]` low word
|
||||
first, `stream` the parameter. `x` f32 `[M, n]` at its row pitch. The caller issues one media
|
||||
item's rows at a time with the same `(seed, stream)`, so every item's draws restart at `e = 0`
|
||||
as the reference's per-latent generator does. MiniMax-H3: `n` 96 (the video's patch rows),
|
||||
`stream` 2, `aug` 0.999.
|
||||
|
||||
## LTX-2
|
||||
|
||||
The ops LTX-2's audio-video transformer adds (`libref/ref_ltx.cpp`): its rotary over fractional
|
||||
|
||||
@@ -935,6 +935,10 @@ std::vector<Case> build_cases() {
|
||||
0, 2.0 * 27 * v.cin * v.cout * 27);
|
||||
add("flow_renoise", { HI("M", 9), HI("n", 96), HI("stream", 1), HS("dtype", "f32") });
|
||||
add("flow_renoise", { HI("M", 5), HI("n", 645), HI("stream", 7), HS("dtype", "f32") });
|
||||
add("cond_aug", { HI("M", 9), HI("n", 96), HI("stream", 2), HF("aug", 0.999),
|
||||
HS("dtype", "f32") });
|
||||
add("cond_aug", { HI("M", 5), HI("n", 645), HI("stream", 7), HF("aug", 0.5),
|
||||
HS("dtype", "f32") });
|
||||
|
||||
/* LTX-2: the video's 3-axis rotary (two padded pairs, a rank's second head), the cross
|
||||
* attentions' 1-axis one, the per-head gate and the guided step. */
|
||||
|
||||
+1
-1
@@ -127,7 +127,7 @@ int avx_par_force(int threads);
|
||||
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) \
|
||||
X(resize_bilinear) X(patch_fold) X(flow_renoise) X(cond_aug) \
|
||||
/* ---- LTX-2 */ \
|
||||
X(rope_split) X(head_gate) X(ltx_guide) X(pixel_norm) \
|
||||
X(d2s3d) X(s2d3d) X(pix_patch) \
|
||||
|
||||
@@ -671,6 +671,9 @@ static const RadOperandSpec oResize[] = { OPD("x"), OPD("geom"), OUT("y") };
|
||||
static const RadParamSpec pFlowRenoise[] = { P_INT("M"), P_INT("n"), P_INT("stream"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oFlowRenoise[] = { OPD("sigma"), OPD("seed"), INOUT("x") };
|
||||
static const RadParamSpec pCondAug[] = { P_INT("M"), P_INT("n"), P_INT("stream"), P_F64("aug"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oCondAug[] = { OPD("seed"), INOUT("x") };
|
||||
|
||||
/* ---- LTX-2 (avx_ltx.cpp) */
|
||||
static const RadParamSpec pRopeSplit[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
|
||||
@@ -1212,6 +1215,9 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "flow_renoise", ARR(pFlowRenoise), ARR(oFlowRenoise),
|
||||
"x = f32(f32(1 - s) x) + f32(s z), z the (seed, stream) normal stream at k n + c, bit for bit "
|
||||
"libref's." },
|
||||
{ "cond_aug", ARR(pCondAug), ARR(oCondAug),
|
||||
"x = f32(f32(a x) + f32(oma z)) with a the `aug` parameter and oma the f32 of the f64 1 - aug, "
|
||||
"z the (seed, stream) normal stream at k n + c, bit for bit libref's." },
|
||||
{ "rope_split", ARR(pRopeSplit), ARR(oRopeSplit),
|
||||
"LTX-2's split rotary over fractional coordinates, libref's arithmetic element by element." },
|
||||
{ "head_gate", ARR(pHeadGate), ARR(oHeadGate),
|
||||
@@ -1779,6 +1785,8 @@ SIMPLE("avx_patch_fold", "patch_fold", "elem", "voxel rows to normalised patch r
|
||||
L(patch_fold)),
|
||||
SIMPLE("avx_flow_renoise", "flow_renoise", "elem", "a clean latent re-noised to a sigma",
|
||||
DT_FLOAT, L(flow_renoise)),
|
||||
SIMPLE("avx_cond_aug", "cond_aug", "elem", "condition rows blended with fresh noise at a fixed aug",
|
||||
DT_FLOAT, L(cond_aug)),
|
||||
SIMPLE("avx_rope_split", "rope_split", "elem", "LTX-2's split rotary over coordinates", DT_FLOAT,
|
||||
L(rope_split)),
|
||||
SIMPLE("avx_head_gate", "head_gate", "elem", "each head times 2 sigmoid of its logit", DT_FLOAT,
|
||||
|
||||
@@ -833,3 +833,37 @@ AVX_KERNEL(flow_renoise) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ cond_aug */
|
||||
/* x = f32(f32(a x) + f32(oma z)) over x's rows, a the `aug` parameter, oma the f32 of the f64
|
||||
* 1 - aug, z the (seed, stream) normal stream at k n + c (libref's, bit for bit): a visual
|
||||
* condition's rows with the reference's condition-noise augmentation. */
|
||||
AVX_KERNEL(cond_aug) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *SD = &a->t[0], *X = &a->t[1];
|
||||
const int64_t n = p_int(a, "n", 0), sm = 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 || sm < 0 || !(aug >= 0.0 && aug < 1.0) || numel(SD) < 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)at(SD, 0) | ((uint64_t)(uint32_t)at(SD, 1) << 32);
|
||||
|
||||
AVX_PARALLEL_FOR_IF(rows * n * 8 >= AVX_PAR_MIN)
|
||||
for (int64_t k = 0; k < rows; ++k) {
|
||||
float* buf = scratch_f32((size_t)(3 * n + 48));
|
||||
float *xs = buf, *z = xs + n + 16, *o = z + n + 16;
|
||||
const float* x = in_row(X, rowoff(X, k, n), n, xs);
|
||||
philox_normals_s(sd, (uint64_t)sm, (uint64_t)(k * n), n, z);
|
||||
const vf va = vf_set1(av), vo = vf_set1(omv);
|
||||
int64_t c = 0;
|
||||
for (; c + VF_N <= n; c += VF_N)
|
||||
vf_storeu(o + c, vf_add(keep(vf_mul(va, vf_loadu(x + c))),
|
||||
keep(vf_mul(vo, vf_loadu(z + c)))));
|
||||
for (; c < n; ++c) o[c] = keep(av * x[c]) + keep(omv * z[c]);
|
||||
out_row(X, rowoff(X, k, n), n, o);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -270,6 +270,7 @@ int r4d_rad_tconv_dw (const RadArgs*, RadStream);
|
||||
int r4d_rad_resize_bilinear (const RadArgs*, RadStream);
|
||||
int r4d_rad_patch_fold (const RadArgs*, RadStream);
|
||||
int r4d_rad_flow_renoise (const RadArgs*, RadStream);
|
||||
int r4d_rad_cond_aug (const RadArgs*, RadStream);
|
||||
int r4d_rad_rope_split (const RadArgs*, RadStream);
|
||||
int r4d_rad_head_gate (const RadArgs*, RadStream);
|
||||
int r4d_rad_ltx_guide (const RadArgs*, RadStream);
|
||||
|
||||
@@ -2693,6 +2693,11 @@ static const RadParamSpec pFlowRenoise[] = { P_INT("M"), P_INT("n"), P_INT("stre
|
||||
static const RadOperandSpec oFlowRenoise[] = {
|
||||
{ "sigma", RAD_OPD_IN, 0 }, { "seed", RAD_OPD_IN, 0 }, { "x", RAD_OPD_INOUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pCondAug[] = { P_INT("M"), P_INT("n"), P_INT("stream"), P_F64("aug"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oCondAug[] = {
|
||||
{ "seed", RAD_OPD_IN, 0 }, { "x", RAD_OPD_INOUT, 0 },
|
||||
};
|
||||
|
||||
/* ---- LTX-2 (r4d_ltx.hip); the same specs as libref's. */
|
||||
static const RadParamSpec pRopeSplit[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
|
||||
@@ -3620,6 +3625,10 @@ static const RadOpSchema kSchemas[] = {
|
||||
SCHEMA("flow_renoise", pFlowRenoise, oFlowRenoise,
|
||||
"x = (1 - sigma) x + sigma z, z the (seed, stream) normal stream at element k n + c, "
|
||||
"each step one f32 rounding."),
|
||||
SCHEMA("cond_aug", pCondAug, oCondAug,
|
||||
"x = a x + oma z, a the `aug` parameter, oma the f32 of the f64 1 - aug, z the "
|
||||
"(seed, stream) normal stream at element k n + c, each step one f32 rounding: the "
|
||||
"reference's condition-noise augmentation."),
|
||||
SCHEMA("rope_split", pRopeSplit, oRopeSplit,
|
||||
"LTX-2's split rotary over fractional coordinates: the pairs of all heads on one "
|
||||
"ladder, the first `pad` unrotated, the axes interleaved; each head's halves turned "
|
||||
@@ -9368,6 +9377,13 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
ROW(cGenF32), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_flow_renoise },
|
||||
|
||||
{ "cond_aug_f32", "cond_aug", "elem",
|
||||
"condition rows blended with fresh noise at the declared aug: a thread a value",
|
||||
"any M and n, row pitches honoured", "f32 rows, u32 seed words",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cGenF32), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_cond_aug },
|
||||
|
||||
{ "rope_split", "rope_split", "elem",
|
||||
"LTX-2's rotary: a workgroup a (head, 64 rows), the head's ladder in LDS, sincosf a pair",
|
||||
"any M, row pitches honoured; head_dim / 2 dividing 256; up to four axes",
|
||||
|
||||
@@ -9709,6 +9709,52 @@ void flow_renoise_cases(const Plugin& pl) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ cond_aug */
|
||||
void cond_aug_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "cond_aug_f32");
|
||||
if (!r) { ok("cond_aug_f32", false, "row absent"); return; }
|
||||
struct C { const char* what; int64_t M, n, ld, extra; double aug; uint64_t seed; };
|
||||
for (const C& c : { C{ "the video's condition rows", 900, 96, 96, 0, 0.999, 0x123456789abcdefull },
|
||||
C{ "pitched rows, half kept", 1152, 32, 40, 3, 0.5, 42ull } }) {
|
||||
const std::string nm = fmt("cond_aug_f32 %s: %lld x %lld, aug %.3f", c.what,
|
||||
(long long)c.M, (long long)c.n, c.aug);
|
||||
const int64_t rows = c.M + c.extra;
|
||||
g_seed = 0x5eedull + (uint64_t)c.M;
|
||||
const std::vector<float> x = randv((size_t)(rows * c.ld), 1.5f);
|
||||
void* dx = up_f32(x);
|
||||
void* dk = dalloc(8);
|
||||
const uint32_t words[2] = { (uint32_t)c.seed, (uint32_t)(c.seed >> 32) };
|
||||
if (!dx || !dk) { ok(nm.c_str(), false, "hipMalloc failed"); continue; }
|
||||
st_memcpy(dk, words, 8, hipMemcpyHostToDevice);
|
||||
/* x is a view of the buffer's first M rows; the rows after it are not x's. */
|
||||
const int rc = launch(r, { T(dk, RAD_U32, { 2 }), pitched(dx, RAD_F32, c.M, c.n, c.ld) },
|
||||
{ PI("M", c.M), PI("n", c.n), PI("stream", 2), PF("aug", c.aug),
|
||||
PS("dtype", "f32") });
|
||||
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
|
||||
const std::vector<float> got = down_f32(dx, x.size());
|
||||
const float av = (float)c.aug, omv = (float)(1.0 - c.aug);
|
||||
double e = 0, q = 0, worst = 0;
|
||||
int64_t moved = 0;
|
||||
for (int64_t k = 0; k < rows; ++k)
|
||||
for (int64_t ch = 0; ch < c.ld; ++ch) {
|
||||
const size_t i = (size_t)(k * c.ld + ch);
|
||||
if (k >= c.M || ch >= c.n) { moved += got[i] != x[i]; continue; }
|
||||
const float z = rad_philox_normal_s(c.seed, 2, (uint64_t)(k * c.n + ch));
|
||||
const float a = av * x[i];
|
||||
const float bz = omv * z;
|
||||
const float want = a + bz;
|
||||
e += (double)(got[i] - want) * (got[i] - want);
|
||||
q += (double)want * want;
|
||||
worst = std::max(worst, (double)std::fabs(got[i] - want));
|
||||
}
|
||||
const double rl = std::sqrt(e / q);
|
||||
ok(fmt("%s (rel_l2 %.3g, worst %.3g; bounds 1e-6, 1e-5), the rest untouched", nm.c_str(),
|
||||
rl, worst).c_str(),
|
||||
rl <= 1e-6 && worst <= 1e-5 && moved == 0,
|
||||
fmt("%lld values outside written", (long long)moved));
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace ups */
|
||||
|
||||
/* ---- a video generator's f32 forms, its biased GEMMs and its rotary, against libref's arithmetic
|
||||
@@ -18534,6 +18580,11 @@ static void video_perf(const Plugin& pl, int iters, const char* only) {
|
||||
E{ "flow_renoise, 96-wide f32 rows", "flow_renoise_f32",
|
||||
{ T(dsig, RAD_F32, { 1 }), T(dseed, RAD_U32, { 2 }), T(dp, RAD_F32, { Ms, 96 }) },
|
||||
{ PI("M", Ms), PI("n", 96), PI("stream", 1), PS("dtype", "f32") },
|
||||
(double)Ms * 96 * 8 },
|
||||
E{ "cond_aug, 96-wide f32 rows", "cond_aug_f32",
|
||||
{ T(dseed, RAD_U32, { 2 }), T(dp, RAD_F32, { Ms, 96 }) },
|
||||
{ PI("M", Ms), PI("n", 96), PI("stream", 2), PF("aug", 0.999),
|
||||
PS("dtype", "f32") },
|
||||
(double)Ms * 96 * 8 } }) {
|
||||
const RadKernelInfo* r = find(pl, e.row);
|
||||
if (!r) { std::printf(" %s: row absent\n", e.row); continue; }
|
||||
@@ -27854,6 +27905,7 @@ static void device_phase(const Plugin& pl) {
|
||||
if (sel("resize_bilinear")) ups::resize_bilinear_cases(pl);
|
||||
if (sel("patch_fold_f32")) ups::patch_fold_cases(pl);
|
||||
if (sel("flow_renoise_f32")) ups::flow_renoise_cases(pl);
|
||||
if (sel("cond_aug_f32")) ups::cond_aug_cases(pl);
|
||||
/* The joint image generators' ops. */
|
||||
if (sel("table_add_bf16")) g3::table_add_cases(pl);
|
||||
if (sel("mix_rows_bf16")) g3::mix_rows_cases(pl);
|
||||
|
||||
@@ -774,6 +774,24 @@ __global__ __launch_bounds__(kT) void flow_renoise_kernel(
|
||||
*p = a + bz;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- cond_aug
|
||||
// x = a x + oma z, a the aug parameter, oma the f32 of the f64 1 - a, z element k n + c of the
|
||||
// (seed, stream) normal stream: a thread a value, each product and the sum one f32 rounding.
|
||||
__global__ __launch_bounds__(kT) void cond_aug_kernel(
|
||||
const uint32_t* __restrict__ seed, float* __restrict__ x, float a, float oma,
|
||||
int n, int ldx, long long total, unsigned long long stream) {
|
||||
const long long id = (long long)blockIdx.x * kT + threadIdx.x;
|
||||
if (id >= total) return;
|
||||
const long long k = id / n;
|
||||
const int c = (int)(id % n);
|
||||
const uint64_t sd = (uint64_t)seed[0] | ((uint64_t)seed[1] << 32);
|
||||
const float z = rad_philox_normal_s(sd, stream, (uint64_t)id);
|
||||
float* p = x + k * ldx + c;
|
||||
const float av = a * *p;
|
||||
const float bz = oma * z;
|
||||
*p = av + bz;
|
||||
}
|
||||
|
||||
inline bool is_bf16(const RadTensor* t) { return t && t->dtype == RAD_BF16; }
|
||||
inline bool is_f32(const RadTensor* t) { return t && t->dtype == RAD_F32; }
|
||||
inline bool is_i32(const RadTensor* t) { return t && t->dtype == RAD_I32; }
|
||||
@@ -1088,3 +1106,28 @@ extern "C" int r4d_rad_flow_renoise(const RadArgs* a, RadStream stream) {
|
||||
0, (const float*)r4d_p(sg), (const uint32_t*)r4d_p(seed), (float*)r4d_wp(x),
|
||||
(int)n, (int)ldx, total, (unsigned long long)sm);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ cond_aug */
|
||||
enum { CA_SEED = 0, CA_X };
|
||||
|
||||
extern "C" int r4d_rad_cond_aug(const RadArgs* a, RadStream stream) {
|
||||
const RadTensor *seed = r4d_opt(a, CA_SEED), *x = r4d_opt(a, CA_X);
|
||||
if (!seed || !x) return R4D_NO(RAD_E_INVAL);
|
||||
if (!is_f32(x) || (seed->dtype != RAD_U32 && seed->dtype != RAD_I32))
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
const long long n = r4d_geti_or(a, "n", 0), sm = r4d_geti_or(a, "stream", 0);
|
||||
const double aug = r4d_getf(a, "aug", -1.0);
|
||||
if (n <= 0 || sm < 0 || !(aug >= 0.0 && aug < 1.0) || r4d_numel(seed) < 2)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
long long rx = 0;
|
||||
const long long ldx = r4d_rows_pitch(x, n, &rx);
|
||||
if (ldx < 0 || !r4d_contig(seed)) return R4D_NO(RAD_E_STRIDE);
|
||||
/* Every row of x, as libref's; M is not read. */
|
||||
const long long M = rx;
|
||||
if (M <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long total = M * n;
|
||||
const float av = (float)aug, omv = (float)(1.0 - aug);
|
||||
return R4D_EMIT(stream, cond_aug_kernel, dim3((unsigned)((total + kT - 1) / kT)), dim3(kT),
|
||||
0, (const uint32_t*)r4d_p(seed), (float*)r4d_wp(x), av, omv,
|
||||
(int)n, (int)ldx, total, (unsigned long long)sm);
|
||||
}
|
||||
@@ -184,6 +184,7 @@ int ref_group_norm(const RadArgs*, RadStream);
|
||||
int ref_tconv_dw(const RadArgs*, RadStream);
|
||||
int ref_resize_bilinear(const RadArgs*, RadStream);
|
||||
int ref_flow_renoise(const RadArgs*, RadStream);
|
||||
int ref_cond_aug(const RadArgs*, RadStream);
|
||||
/* ref_ltx.cpp */
|
||||
int ref_rope_split(const RadArgs*, RadStream);
|
||||
int ref_head_gate(const RadArgs*, RadStream);
|
||||
@@ -437,6 +438,7 @@ REF_SHAPE_FN(ref_group_norm);
|
||||
REF_SHAPE_FN(ref_tconv_dw);
|
||||
REF_SHAPE_FN(ref_resize_bilinear);
|
||||
REF_SHAPE_FN(ref_flow_renoise);
|
||||
REF_SHAPE_FN(ref_cond_aug);
|
||||
REF_SHAPE_FN(ref_rope_split);
|
||||
REF_SHAPE_FN(ref_head_gate);
|
||||
REF_SHAPE_FN(ref_ltx_guide);
|
||||
|
||||
@@ -891,6 +891,11 @@ static const RadOperandSpec oResize[] = { OPD("x"), OPD("geom"), OUT("y") };
|
||||
static const RadParamSpec pFlowRenoise[] = { P_INT("M"), P_INT("n"), P_INT("stream"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oFlowRenoise[] = { OPD("sigma"), OPD("seed"), INOUT("x") };
|
||||
/* `aug` is the condition's kept fraction (the reference's VISUAL_COND_TIMESTEP, 0.999); unlike
|
||||
* `flow_renoise` the blend weight is a declared recipe constant, not a staged tensor. */
|
||||
static const RadParamSpec pCondAug[] = { P_INT("M"), P_INT("n"), P_INT("stream"), P_F64("aug"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oCondAug[] = { OPD("seed"), INOUT("x") };
|
||||
|
||||
/* ---- LTX-2 (ref_ltx.cpp) */
|
||||
static const RadParamSpec pRopeSplit[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
|
||||
@@ -1807,6 +1812,11 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "flow_renoise", ARR(pFlowRenoise), ARR(oFlowRenoise),
|
||||
"a clean latent re-noised to sigma s: x = f32(f32(f32(1 - s) x) + f32(s z)), z element "
|
||||
"k n + c of the normal stream (seed, `stream`) (abi/rad_philox.h)." },
|
||||
{ "cond_aug", ARR(pCondAug), ARR(oCondAug),
|
||||
"a visual condition's rows blended with fresh noise, the reference's condition augmentation: "
|
||||
"x = f32(f32(a x) + f32(oma z)) with a = `aug` and oma the f32 of the f64 1 - aug, z element "
|
||||
"k n + c of the normal stream (seed, `stream`); the caller issues one media item's rows so "
|
||||
"every item restarts the same stream, as the reference does." },
|
||||
{ "rope_split", ARR(pRopeSplit), ARR(oRopeSplit),
|
||||
"LTX-2's rotary over fractional coordinates: dim/2 pairs over all heads, the first `pad` "
|
||||
"unrotated, then freq[k] = f32(theta^linspace(0,1,steps)[k] pi/2) with the n_dims coordinates "
|
||||
@@ -2450,6 +2460,8 @@ SIMPLE("ref_patch_fold", "patch_fold", "elem", "voxel rows to normalised patch r
|
||||
ref_patch_fold),
|
||||
SIMPLE("ref_flow_renoise", "flow_renoise", "elem", "a clean latent re-noised to a sigma", DT_FLOAT,
|
||||
ref_flow_renoise),
|
||||
SIMPLE("ref_cond_aug", "cond_aug", "elem", "condition rows blended with fresh noise at a fixed aug",
|
||||
DT_FLOAT, ref_cond_aug),
|
||||
SIMPLE("ref_rope_split", "rope_split", "elem", "LTX-2's split rotary over coordinates", DT_FLOAT,
|
||||
ref_rope_split),
|
||||
SIMPLE("ref_head_gate", "head_gate", "elem", "each head times 2 sigmoid of its logit", DT_FLOAT,
|
||||
|
||||
@@ -3143,6 +3143,17 @@ int ref_flow_renoise_shape(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
return o.finish("flow_renoise", operand, out);
|
||||
}
|
||||
|
||||
/* aug in [0, 1); the condition rows f32. */
|
||||
int ref_cond_aug_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
const double aug = rad_param_getf(p, n_p, "aug", -1.0);
|
||||
if (M <= 0 || n <= 0 || !(aug >= 0.0 && aug < 1.0)) return RAD_E_UNSUPPORTED;
|
||||
Opd o;
|
||||
o.t_idx({ 2 }, (int64_t)1 << 30, -1, 0); /* seed: two words */
|
||||
o.t((uint32_t)RAD_F32, { M, n }); /* x, INOUT */
|
||||
return o.finish("cond_aug", operand, out);
|
||||
}
|
||||
|
||||
/* ================================================================== video sparse attention
|
||||
*
|
||||
* The tiles come from the values of `segs` and `geom`, which a description draws only as
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
+45
-1
@@ -1033,7 +1033,7 @@ TEST(op_vocabulary) {
|
||||
/* Its latent upscaler: the 3-D and temporal convolutions, the group norm, the resize,
|
||||
* the fold back into patch rows and the re-noising. */
|
||||
"conv3d_ndhwc", "group_norm", "tconv_dw", "resize_bilinear", "patch_fold",
|
||||
"flow_renoise",
|
||||
"flow_renoise", "cond_aug",
|
||||
/* The joint image generators' (Krea 2, FLUX.3): attention over separate key bounds, a
|
||||
* per-layer table added to every row, a learned mix of consecutive rows, and the VAE's
|
||||
* channel statistics. */
|
||||
@@ -7207,6 +7207,48 @@ TEST(flow_renoise_draws_its_own_stream_by_element) {
|
||||
CHECK(differs);
|
||||
}
|
||||
|
||||
/* cond_aug: x = f32(f32(a x) + f32(oma z(k n + c))) with a = 0.999 the f64 parameter and oma
|
||||
* the f32 of the f64 1 - a -- NOT the f32 difference 1.0f - 0.999f, whose low bits differ; the
|
||||
* reference subtracts that scalar in python double -- and z the (seed, 2) normal stream. x is
|
||||
* [2, 3] at a pitch of 5, last two columns not written. The second call is the caller's
|
||||
* per-item restart: item B issued alone with the same (seed, stream) draws from e = 0 again,
|
||||
* which a single joined call would not (its row 1 reads k n + c). aug 1 -- the audio twin's
|
||||
* disabled spelling -- and a missing aug are refused, not approximated. */
|
||||
TEST(cond_aug_blends_conditions_with_a_restarted_stream) {
|
||||
const uint64_t seed = 0x0000f00dbeef000dull;
|
||||
T sd(RAD_U32, {2}); sd.n({(int64_t)(seed & 0xffffffffu), (int64_t)(seed >> 32)});
|
||||
const float av = 0.999f, omv = (float)(1.0 - 0.999);
|
||||
const float xa[3] = { 1, -2, 3 }, xb[3] = { 0.5f, 0, -4 };
|
||||
T x(RAD_F32, {2, 5});
|
||||
x.f({ xa[0], xa[1], xa[2], -9, -9, xb[0], xb[1], xb[2], -9, -9 });
|
||||
x.t.shape[1] = 3;
|
||||
A a1; a1.t(sd).t(x).i("M", 2).i("n", 3).i("stream", 2).d("aug", 0.999).s("dtype", "f32");
|
||||
CHECK_OK(run("cond_aug", a1));
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
const float z = rad_philox_normal_s(seed, 2, (uint64_t)(k * 3 + c));
|
||||
CHECK_EQ(x.at(k * 5 + c), av * (k ? xb[c] : xa[c]) + omv * z);
|
||||
}
|
||||
CHECK_EQ(x.at(k * 5 + 3), -9.0f);
|
||||
CHECK_EQ(x.at(k * 5 + 4), -9.0f);
|
||||
}
|
||||
T y(RAD_F32, {1, 3});
|
||||
y.f({ xb[0], xb[1], xb[2] });
|
||||
A a2; a2.t(sd).t(y).i("M", 1).i("n", 3).i("stream", 2).d("aug", 0.999).s("dtype", "f32");
|
||||
CHECK_OK(run("cond_aug", a2));
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
const float restarted = av * xb[c] + omv * rad_philox_normal_s(seed, 2, (uint64_t)c);
|
||||
CHECK_EQ(y.at(c), restarted);
|
||||
CHECK(restarted != x.at(5 + c)); // joined, row 1 read element 3 + c instead
|
||||
}
|
||||
A a3; a3.t(sd).t(y).i("M", 1).i("n", 3).i("stream", 2).d("aug", 1.0).s("dtype", "f32");
|
||||
CHECK_EQ(run("cond_aug", a3), RAD_E_SHAPE);
|
||||
A a4; a4.t(sd).t(y).i("M", 1).i("n", 3).i("stream", 2).s("dtype", "f32");
|
||||
CHECK_EQ(run("cond_aug", a4), RAD_E_SHAPE);
|
||||
A a5; a5.t(y).i("M", 1).i("n", 3).i("stream", 2).d("aug", 0.999).s("dtype", "f32");
|
||||
CHECK_EQ(run("cond_aug", a5), RAD_E_INVAL);
|
||||
}
|
||||
|
||||
/* ================================================================== video sparse attention
|
||||
* docs/OPS.md "Video sparse attention": the tiles over a packed sequence of dense segments and a
|
||||
* video grid, vsa_select's top-k of pooled scores and its coarse branch, attn_vsa's block-sparse
|
||||
@@ -8920,6 +8962,8 @@ std::vector<Geo> geometries() {
|
||||
G("patch_fold", "M", 8, a.i("M", R).i("C", 3).i("pt", 1).i("ph", 2).i("pw", 2)
|
||||
.s("mul", "1 2 3").s("add", "0 1 2").s("dtype", "f32")),
|
||||
G("flow_renoise", "M", 3, a.i("M", R).i("n", 8).i("stream", 1).s("dtype", "f32")),
|
||||
G("cond_aug", "M", 3, a.i("M", R).i("n", 8).i("stream", 2).d("aug", 0.999)
|
||||
.s("dtype", "f32")),
|
||||
G("attn_cross", "M", 6, a.i("M", R).i("head_dim", 8).i("gqa", 2).i("causal", 0)
|
||||
.s("dtype", "f32").i("n_head", 2)),
|
||||
G("table_add", "M", 3, a.i("M", R).i("n", 8).i("L", 3).s("dtype", "f32")),
|
||||
|
||||
Reference in new issue
Block a user