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
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user