radiance 1.8.1

- MiniMax-H3 and FastH3 no longer stop the server on a prompt longer than 16 tokens (FastH3) or
  128 tokens (MiniMax-H3)
- latent upscaling for the MiniMax-H3 containers: with LBH-123-AI's latent upscaler in the
  container, `latent_upscale` runs the first steps at a smaller size and refines the latent at the
  full size; `upscale_refine_steps` sets how many steps refine, and the video object reports both
- rad-convert --in-place extends a container whose weights the plugin has regrouped since
- plugin ABI 22: out-of-tree plugins must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-18 15:41:44 +02:00
1 parent 7a7707a955
commit 816e24bd46
43 files changed
+4458 -81

No files matched your search

+16 -2
View File
@@ -3,7 +3,7 @@
The conventional op vocabulary in hand-written x86 SIMD, dispatched at run time by CPU feature.
libavx is a kernel plugin beside `libref` and `libr4d`. libref is the only other host
implementation, and it is a slow reference. libavx implements all 150 of libref's ops with the
implementation, and it is a slow reference. libavx implements all 161 of libref's ops with the
same schemas and the same answers to a tolerance, the image generator's included (below).
cmake -S libavx -B build-avx -DCMAKE_PREFIX_PATH=<radiance prefix> && cmake --build build-avx -j4
@@ -639,6 +639,19 @@ per (head, query tile), 64 keys of a visible tile packed at a time for broadcast
online softmax, as `attn_prefix_self`'s f32 form; a prefix query tile sees every tile, so the tasks
are scheduled dynamically.
Its latent upscaler adds six (`avx_video.cpp`). `conv3d_ndhwc` is GEMMs over bands of patch rows, as
`conv2d_nhwc`: the weight re-laid once a launch as `[cout][dt][kh][kw][cin]`, an output voxel's
patch its 27 input voxels' channel runs (zeros outside the volume), each band one `gemm_nt_into`
into f32 rows; a row is then rounded once, or with `res` rounded, its residual added and rounded
again, the residual read before the row is written so it may be y. `group_norm` sums its
statistics in f64 over fixed 64-row blocks combined in block order, so its bytes do not depend on
the thread count, and keeps those sums in its own memory: only `stats[0, 2 groups)` is written.
Both are held to the tolerance table. `tconv_dw` (each tap's product rounded before its add, in
libref's order), `resize_bilinear` (the source rows and weights taken once a launch in libref's
f32 steps), `patch_fold` and `flow_renoise` (`rad_philox_normal_s` a Philox block at a time) are
libref's bit for bit and held to zero error by the hand-built cases. The convolution has not been
tuned: MiniMax-H3 is not served on the CPU.
## Design choices
Each choice below was measured on the platform above. The variants that did not pay are recorded
@@ -701,7 +714,8 @@ and spills a 16-register file, costing more than the FMA saves, so the AVX and A
avx_{elementwise,norm,hc,quant,gemm,attn,qsa,moe,gdn,sample,misc}.cpp the kernels; compiled 4x
avx_gen.cpp the image generator's norms, sampler, lanes and decoder ops; compiled 4x
avx_gen_attn.cpp attn_prefix_self, the generator's attention; compiled 4x
avx_video.cpp the video decoder's patch unfold, tiles, stitch and positions; compiled 4x
avx_video.cpp the video decoder's patch unfold, tiles, stitch and positions, and the
latent upscaler's convolutions, norm, resize, fold and re-noise; compiled 4x
avx_vsa.cpp video sparse attention: the tile selection and the attention; compiled 4x
avx_audio.cpp the audio generator's vocoder, sampler, embeddings and window ops; compiled 4x
avx_harness.h plugin loading, row selection, the operand draw, guarded buffers, the
+36
View File
@@ -748,6 +748,42 @@ std::vector<Case> build_cases() {
add("grid_pos3", { HI("M", 40), HI("T", 2), HI("H", 3), HI("W", 4), HI("period", 27),
HF("scale", 6.283185307179586) });
/* The latent upscaler over libref's cube volumes (c x c x c, the largest M holds; every geom
* entry c, so the resize is the identity here): the 3-D convolution at odd channel counts,
* kernels 3 x 3 x 3 and 3 x 1 x 1, with the residual (a separate buffer), and at MiniMax-H3's
* widths 24 -> 512, 512 -> 512 and 512 -> 24 in bf16; the modulated group norm over a few rows
* and over three of avx's 64-row blocks; the temporal convolution with taps past both ends.
* Real resizes, an in-place residual, an unmodulated norm over y = x, uneven volumes and views
* of wider planes are avx_check_hand.cpp's. */
for (const char* dt : { "f32", "bf16" }) {
struct Conv3 { int M, cin, cout, k, kt; };
static const Conv3 kConv3[] = { { 27, 5, 13, 3, 3 }, { 64, 20, 52, 3, 3 },
{ 27, 24, 16, 1, 3 } };
for (const Conv3& v : kConv3)
add("conv3d_ndhwc", { HI("M", v.M), HI("cin", v.cin), HI("cout", v.cout),
HI("k", v.k), HI("kt", v.kt), HS("dtype", dt) },
0, 2.0 * v.M * v.cout * v.cin * v.k * v.k * v.kt);
for (const char* act : { "silu", "none" })
add("group_norm", { HI("M", 7), HI("n", 640), HI("groups", 32), HF("eps", 1e-5),
HS("act", act), HS("dtype", dt) });
add("group_norm", { HI("M", 150), HI("n", 96), HI("groups", 4), HF("eps", 1e-5),
HS("act", "silu"), HS("dtype", dt) });
add("tconv_dw", { HI("M", 27), HI("n", 645), HI("k", 5), HS("dtype", dt) });
add("tconv_dw", { HI("M", 64), HI("n", 37), HI("k", 3), HS("dtype", dt) });
add("resize_bilinear", { HI("M", 27), HI("n", 645), HS("dtype", dt) });
for (int pt : { 1, 4 })
add("patch_fold", { HI("M", 8), HI("C", 24), HI("pt", pt), HI("ph", 2), HI("pw", 2),
HS("mul", kStd24.c_str()), HS("add", kMean24.c_str()),
HS("dtype", dt) });
}
struct Wide { int cin, cout; };
for (const Wide v : { Wide{ 24, 512 }, Wide{ 512, 512 }, Wide{ 512, 24 } })
add("conv3d_ndhwc", { HI("M", 27), HI("cin", v.cin), HI("cout", v.cout), HI("k", 3),
HI("kt", 3), HS("dtype", "bf16") },
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") });
/* Video sparse attention over the cubes libref describes: a 3 x 3 x 3 grid in boxes of 2
* after two dense segments of as many rows (P 8, V 8; sparsity 0.8 keeps 2), with and without
* the coarse operands; and MiniMax-H3's boxes of 4 at patch 2 over a 10 x 5 x 5 grid after
+145
View File
@@ -2211,6 +2211,150 @@ void video_cases(std::vector<Hand>& hs) {
}
}
/* ================================================================== the latent upscaler */
/* Forms libref's cube descriptions do not draw: volumes of a different extent on every axis, an
* activation at a wider row pitch, the residual in place (res = y), a group norm in place (y = x)
* without its modulation and with a modulation row of the other dtype, statistics over several
* of avx's 64-row blocks and a part of one, kernels of other shapes, real resizes (MiniMax-H3's
* x2 and 4.2 x 4 ratios, and shrinking), output rows past the volume, the soundtrack's and the
* video's re-noise widths. tconv_dw, resize_bilinear, patch_fold and flow_renoise are held to
* zero error, libref's arithmetic bit for bit; the convolution's GEMM and the norm's statistics
* sum in another order and are held to the tolerance table. */
void upscaler_cases(std::vector<Hand>& hs) {
const auto zero = [](int64_t) { return 0.0f; };
for (const uint32_t dt : { (uint32_t)RAD_F32, (uint32_t)RAD_BF16 }) {
const char* dn = dt == RAD_F32 ? "f32" : "bf16";
const uint32_t other = dt == RAD_F32 ? (uint32_t)RAD_BF16 : (uint32_t)RAD_F32;
const std::string sfx = std::string(", ") + dn;
const double tol = dt == RAD_F32 ? 1e-5 : 6e-3;
/* ---- conv3d_ndhwc ------------------------------------------------------------------ */
{
const int64_t T = 2, H = 3, W = 4, R = T * H * W, ci = 7, co = 11;
Hand h(("2 x 3 x 4, x at a pitch, the residual in place, f32 bias" + sfx).c_str(),
"conv3d_ndhwc", tol);
h.view(dt, { R, ci }, { ci + 3, 1 }, R * (ci + 3), hfill(450))
.dense(dt, { co, ci, 3, 3, 3 }, hfill(451, 0.3f))
.dense(RAD_F32, { co }, hfill(452, 0.5f))
.idx({ T, H, W })
.dense(dt, { R, co }, hfill(453), true)
.alias(4)
.pi("M", R).pi("cin", ci).pi("cout", co).pi("k", 3).pi("kt", 3).ps("dtype", dn);
hs.push_back(std::move(h));
/* A temporal-only kernel over six frames of 2 x 3, two output rows past the volume. */
const int64_t R2 = 6 * 2 * 3, ci2 = 9, co2 = 50;
Hand g(("6 x 2 x 3 at kernel 5 x 1 x 1, rows past the volume" + sfx).c_str(),
"conv3d_ndhwc", tol);
g.dense(dt, { R2, ci2 }, hfill(454))
.dense(dt, { co2, ci2, 5, 1, 1 }, hfill(455, 0.3f))
.dense(dt, { co2 }, hfill(456, 0.5f))
.idx({ 6, 2, 3 })
.none()
.dense(dt, { R2 + 2, co2 }, hfill(457), true)
.pi("M", R2 + 2).pi("cin", ci2).pi("cout", co2).pi("k", 1).pi("kt", 5)
.ps("dtype", dn);
hs.push_back(std::move(g));
Hand e(("an even kernel is refused" + sfx).c_str(), "conv3d_ndhwc", tol);
e.dense(dt, { R, ci }, hfill(458))
.dense(dt, { co, ci, 3, 2, 2 }, hfill(459))
.dense(dt, { co }, hfill(459))
.idx({ T, H, W })
.none()
.dense(dt, { R, co }, zero, true)
.pi("M", R).pi("cin", ci).pi("cout", co).pi("k", 2).pi("kt", 3).ps("dtype", dn)
.refuses(RAD_E_SHAPE);
hs.push_back(std::move(e));
}
/* ---- group_norm: stats f32 of exactly groups (2 + 3 1024) values --------------------- */
{
const int64_t R = 200, n = 48, G = 6;
const auto offset = [](int64_t i) { return 2.5f + 3.0f * hfill(460)(i); };
Hand h(("200 rows in 6 groups, y = x at a pitch, unmodulated" + sfx).c_str(),
"group_norm", tol);
h.view(dt, { R, n }, { n + 5, 1 }, R * (n + 5), offset)
.dense(dt, { n }, hfill(461))
.dense(dt, { n }, hfill(462))
.none()
.dense(RAD_F32, { G * (2 + 3 * 1024) }, zero, true)
.alias(0)
.pi("M", R).pi("n", n).pi("groups", G).pf("eps", 1e-5).ps("act", "none")
.ps("dtype", dn);
hs.push_back(std::move(h));
const int64_t R2 = 150, n2 = 96, G2 = 32;
Hand g(("150 rows in 32 groups of 3, a modulation row of the other dtype, silu" +
sfx).c_str(), "group_norm", tol);
g.dense(dt, { R2, n2 }, hfill(463, 2.0f))
.dense(dt, { n2 }, hfill(464))
.dense(dt, { n2 }, hfill(465))
.dense(other, { 2 * n2 }, hfill(466, 0.5f))
.dense(RAD_F32, { G2 * (2 + 3 * 1024) }, zero, true)
.dense(dt, { R2, n2 }, zero, true)
.pi("M", R2).pi("n", n2).pi("groups", G2).pf("eps", 1e-5).ps("act", "silu")
.ps("dtype", dn);
hs.push_back(std::move(g));
}
/* ---- tconv_dw: seven frames of 2 x 3, x at a pitch, rows past the volume ------------ */
{
const int64_t R = 7 * 2 * 3, n = 37;
Hand h(("7 x 2 x 3 at k 5, x at a pitch, rows past the volume" + sfx).c_str(),
"tconv_dw", 0.0);
h.view(dt, { R, n }, { n + 3, 1 }, R * (n + 3), hfill(470))
.dense(dt, { n, 1, 5, 1, 1 }, hfill(471, 0.5f))
.dense(RAD_F32, { n }, hfill(472, 0.5f))
.idx({ 7, 2, 3 })
.dense(dt, { R + 2, n }, hfill(473), true)
.pi("M", R + 2).pi("n", n).pi("k", 5).ps("dtype", dn);
hs.push_back(std::move(h));
}
/* ---- resize_bilinear: up and down at once, H3's ratios, shrinking ------------------- */
{
struct Rs { int64_t T, h, w, H, W, n; const char* what; };
static const Rs kRs[] = { { 2, 3, 5, 7, 4, 37, "3 x 5 to 7 x 4" },
{ 2, 5, 3, 10, 6, 16, "5 x 3 to 10 x 6 (x2)" },
{ 1, 5, 3, 21, 12, 16, "5 x 3 to 21 x 12 (4.2 x 4)" },
{ 3, 6, 7, 4, 2, 20, "6 x 7 to 4 x 2" } };
for (const Rs& v : kRs) {
const int64_t rin = v.T * v.h * v.w, rout = v.T * v.H * v.W;
Hand h((std::string(v.what) + ", x at a pitch, rows past" + sfx).c_str(),
"resize_bilinear", 0.0);
h.view(dt, { rin, v.n }, { v.n + 2, 1 }, rin * (v.n + 2), hfill(480 + v.h))
.idx({ v.T, v.h, v.w, v.H, v.W })
.dense(dt, { rout + 3, v.n }, hfill(481), true)
.pi("M", rout + 3).pi("n", v.n).ps("dtype", dn);
hs.push_back(std::move(h));
}
}
/* ---- patch_fold: 2 x 3 x 2 patches of 2 x 2 x 2, x at a pitch, rows past ------------ */
{
Hand h(("2 x 3 x 2 patches of 2 x 2 x 2, x at a pitch, rows past" + sfx).c_str(),
"patch_fold", 0.0);
h.view(dt, { 96, 5 }, { 8, 1 }, 96 * 8, hfill(490, 3.0f))
.idx({ 2, 3, 2 })
.dense(RAD_F32, { 14, 40 }, hfill(491), true)
.pi("M", 14).pi("C", 5).pi("pt", 2).pi("ph", 2).pi("pw", 2)
.ps("mul", "0.5 1.25 -2 0.3 1.7").ps("add", "0.1 -0.2 0.3 0 2.5").ps("dtype", dn);
hs.push_back(std::move(h));
}
}
/* ---- flow_renoise: the video's 96 and the soundtrack's 32, rows at a wider pitch --------- */
for (const int64_t n : { 96, 32 }) {
Hand h(("n " + std::to_string(n) + ", rows at a wider pitch").c_str(), "flow_renoise", 0.0);
h.dense(RAD_F32, { 1 }, [](int64_t) { return 0.37f; })
.idx({ 12345, 81 })
.view(RAD_F32, { 7, n }, { n + 5, 1 }, 7 * (n + 5), hfill(495, 2.0f), true)
.pi("M", 7).pi("n", n).pi("stream", 1).ps("dtype", "f32");
hs.push_back(std::move(h));
}
}
/* ================================================================== video sparse attention
* What libref's descriptions cannot draw: uneven dense segments (13 and 14 rows: a tile each in
* boxes of 4, two each in boxes of 2) before a 6 x 5 x 7 grid (a 10 x 14 latent at patch 2) with
@@ -3892,6 +4036,7 @@ bool hand_cases(const Check& ck) {
packed_cases(hs);
gen_cases(hs);
video_cases(hs);
upscaler_cases(hs);
vsa_cases(hs);
qsa_cases(hs);
encoder_cases(hs);
+2 -1
View File
@@ -111,7 +111,8 @@ int avx_par_force(int threads);
/* ---- video generation */ \
X(rms_mod) X(rms_mod_quant_fp8) X(flow_x0_step) X(patch_unfold) \
X(vtile_gather) X(vtile_stitch) X(grid_pos3) X(vsa_select) \
X(attn_vsa) \
X(attn_vsa) X(conv3d_ndhwc) X(group_norm) X(tconv_dw) \
X(resize_bilinear) X(patch_fold) X(flow_renoise) \
/* ---- audio generation */ \
X(conv1d) X(conv_t1d) X(snake) X(cfg_topk_sample) \
X(rvq_embed) X(frame_mix_store) X(end_status) X(frame_store) \
+47
View File
@@ -551,6 +551,22 @@ static const RadOperandSpec oAttnVsa[] = { OPD("q"), OPD("k"), OPD("v"), OPD("se
OPD("geom"), OPD("sel"), OPD_O("coarse"),
OPD_O("gate"), OUT("out"), OPD_O("k_i8"),
OPD_O("k_scale"), OPD_O("v_scale") };
/* ---- the latent upscaler (avx_video.cpp), as libref declares it; patch_fold takes
* patch_unfold's parameters and operands ------------------------------------------------------ */
static const RadParamSpec pConv3d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("kt"), P_STR("dtype") };
static const RadOperandSpec oConv3d[] = { OPD("x"), WGT("w"), WGT("b"), OPD("geom"),
OPD_O("res"), OUT("y") };
static const RadParamSpec pGroupNorm[] = { P_INT("M"), P_INT("n"), P_INT("groups"), P_F64("eps"),
P_STR("act"), P_STR("dtype") };
static const RadOperandSpec oGroupNorm[] = { OPD("x"), WGT("w"), WGT("b"), OPD_O("mod"),
INOUT("stats"), OUT("y") };
static const RadParamSpec pTconvDw[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_STR("dtype") };
static const RadOperandSpec oTconvDw[] = { OPD("x"), WGT("w"), WGT("b"), OPD("geom"), OUT("y") };
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") };
/* ---- audio generation (avx_audio.cpp), as libref declares it ------------------------------ */
static const RadParamSpec pConv1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
@@ -935,6 +951,24 @@ static const RadOpSchema kSchemas[] = {
"video sparse attention over libref's tiles: prefix queries dense, video queries the prefix "
"and their `sel` tiles, blocked online softmax; the gated coarse term on the output rounded "
"to its dtype." },
{ "conv3d_ndhwc", ARR(pConv3d), ARR(oConv3d),
"a 3-D convolution over voxel rows, `same` zero padding, PyTorch's [cout, cin, kt, k, k] "
"weight, as GEMMs over bands of patch rows; with `res` (possibly y) rd(rd(acc + b) + res)." },
{ "group_norm", ARR(pGroupNorm), ARR(oGroupNorm),
"a group norm over all rows, its statistics in f64 over fixed row blocks combined in order, "
"then the optional [scale | shift] row and silu, one rounding; stats[0, 2 groups) the mean "
"and rstd." },
{ "tconv_dw", ARR(pTconvDw), ARR(oTconvDw),
"a depthwise convolution along a volume's frames, PyTorch's [n, 1, k, 1, 1] weight, zero "
"frames outside the volume." },
{ "resize_bilinear", ARR(pMnD), ARR(oResize),
"each frame of a volume resized as torch's align_corners=False bilinear at a size, bit for bit "
"libref's." },
{ "patch_fold", ARR(pPatchUnfold), ARR(oPatchUnfold),
"voxel rows to normalised patch rows, f32(f32(x * mul) + add), bit for bit libref's." },
{ "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." },
{ "conv1d", ARR(pConv1d), ARR(oConv1d),
"a 1-D convolution over the sequences `cu_seqlens` bounds, channels innermost, PyTorch's "
"[cout, cin, k] weight, dilation `dil`, centred or (`causal`) all on the left, zeros outside "
@@ -1390,6 +1424,19 @@ SIMPLE("avx_vsa_select", "vsa_select", "attn",
SIMPLE("avx_attn_vsa", "attn_vsa", "attn",
"block-sparse attention over the prefix and selected video tiles, the gated coarse term",
"float q, k, v, coarse, gate and out; i32 segs, geom and sel", L(attn_vsa)),
SIMPLE("avx_conv3d_ndhwc", "conv3d_ndhwc", "conv",
"a 3-D convolution over voxel rows, as GEMMs over patch rows", DT_FLOAT, L(conv3d_ndhwc)),
SIMPLE("avx_group_norm", "group_norm", "norm",
"a group norm over all rows, optionally modulated, then an activation", DT_FLOAT,
L(group_norm)),
SIMPLE("avx_tconv_dw", "tconv_dw", "conv", "a depthwise convolution along a volume's frames",
DT_FLOAT, L(tconv_dw)),
SIMPLE("avx_resize_bilinear", "resize_bilinear", "elem",
"each frame of a volume resized bilinearly", DT_FLOAT, L(resize_bilinear)),
SIMPLE("avx_patch_fold", "patch_fold", "elem", "voxel rows to normalised patch rows", DT_FLOAT,
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_conv1d", "conv1d", "conv", "a 1-D convolution over sequences, as GEMMs over patch rows",
DT_FLOAT, L(conv1d)),
+443 -7
View File
@@ -1,22 +1,36 @@
/* avx_video.cpp -- the video generator's ops around its decoder (docs/OPS.md, "Video
* generation"): the latent's patch rows unfolded to voxels, the tiles a video decoder runs over,
* gathered from the latent and stitched back into frames, and the rotary positions of a tile's
* tokens.
* tokens; and its latent upscaler's (docs/OPS.md, "The latent upscaler"): the 3-D convolution,
* the group norm, the temporal depthwise convolution, the per-frame resize, the fold back into
* patch rows and the re-noising.
*
* Bit-exact with libref (libref/ref_video.cpp): they are copies and roundings, and a seed must
* give the same video bytes on both host libraries. Where libref multiplies and then adds in two
* roundings, `keep` stops the compiler from fusing the pair (this plugin is built with
* -ffp-contract=fast, libref without contraction); the stitch's bf16 blends are passes of whole
* rows through x's dtype (`narrow_row`), so a product is rounded before the sum reads it.
* Bit-exact with libref (libref/ref_video.cpp) but for conv3d_ndhwc and group_norm: they are
* copies, roundings and short sums in libref's order, and a seed must give the same video bytes
* on both host libraries. Where libref multiplies and then adds in two roundings, `keep` stops the
* compiler from fusing the pair (this plugin is built with -ffp-contract=fast, libref without
* contraction); the stitch's bf16 blends are passes of whole rows through x's dtype
* (`narrow_row`), so a product is rounded before the sum reads it. conv3d_ndhwc is GEMMs over
* patch rows and group_norm sums its statistics over fixed row blocks; both are held to the
* tolerance table.
*/
#include "avx_vec.h"
#include "avx_common.h"
#include "rad_philox.h"
#include <algorithm>
#include <vector>
using namespace avx;
namespace avx {
inline namespace AVX_SUFFIX {
/* gemm_nt's product over operands assembled by a composite kernel (avx_gemm.cpp). */
int gemm_nt_into(const RadTensor* A, const RadTensor* B, const RadTensor* bias, const RadTensor* Y,
int64_t M, int64_t N, int64_t K);
} /* inline namespace AVX_SUFFIX */
} /* namespace avx */
namespace {
inline int64_t at(const RadTensor* t, int64_t i) { return ldi(t, offlin(t, i)); }
@@ -29,15 +43,51 @@ inline void narrow_row(uint32_t dt, float* v, int64_t n, float* tmp) {
row_to_f32(dt, tmp, v, n);
}
/* A value the compiler may not fuse into the next add. */
/* A value the compiler may not fuse into the next add. At the scalar level a vector is a float. */
#if AVX_LEVEL == 0
inline float keep(float x) { __asm__("" : "+x"(x)); return x; }
#elif AVX_LEVEL == 3
inline float keep(float x) { __asm__("" : "+v"(x)); return x; }
inline vf keep(vf x) { __asm__("" : "+v"(x)); return x; }
#else
inline float keep(float x) { __asm__("" : "+x"(x)); return x; }
inline vf keep(vf x) { __asm__("" : "+x"(x)); return x; }
#endif
/* A dense rank-2 view of `rows` x `cols` elements of dtype `dt` at `data`. */
inline RadTensor mat_view(void* data, uint32_t dt, int64_t rows, int64_t cols) {
RadTensor t{};
t.data = data;
t.dtype = dt;
t.rank = 2;
t.shape[0] = rows; t.shape[1] = cols;
t.stride[0] = cols; t.stride[1] = 1;
return t;
}
/* Elements [e0, e0 + cnt) of the normal stream (seed, stream) into out (abi/rad_philox.h): the same
* arithmetic as rad_philox_normal_s, with one Philox block and one log per element pair instead of
* one each per element (avx_audio.cpp's). */
void philox_normals_s(uint64_t seed, uint64_t stream, uint64_t e0, int64_t cnt, float* out) {
uint64_t e = e0;
int64_t i = 0;
while (i < cnt) {
const uint64_t g = e >> 2;
uint32_t c[4] = { (uint32_t)g, (uint32_t)(g >> 32), (uint32_t)stream,
(uint32_t)(stream >> 32) };
rad_philox4x32_10(c, (uint32_t)seed, (uint32_t)(seed >> 32));
for (int pair = (int)((e & 3u) >> 1); pair < 2 && i < cnt; ++pair) {
const uint32_t wa = c[pair * 2], wb = c[pair * 2 + 1];
const float ua = ((float)(wa >> 8) + 0.5f) * (1.0f / 16777216.0f);
const float ub = ((float)(wb >> 8) + 0.5f) * (1.0f / 16777216.0f);
const float r = sqrtf(-2.0f * logf(ua));
const float t = 6.28318530717958647692f * ub;
if ((e & 1u) == 0) { out[i++] = r * cosf(t); ++e; }
if (i < cnt) { out[i++] = r * sinf(t); ++e; }
}
}
}
/* The f32 values of a space-separated list (libref's parse_f32_list): up to `cap` into out, the
* count returned whatever it is. */
int64_t parse_f32(const char* s, float* out, int64_t cap) {
@@ -143,6 +193,44 @@ AVX_KERNEL(patch_unfold) {
return RAD_OK;
}
/* ------------------------------------------------------------------ patch_fold */
/* Voxel rows folded into normalised patch rows, f32(f32(x * mul[c]) + add[c]) (libref): a task
* is a patch, its pt ph pw voxel rows read once each and dealt into its row. */
AVX_KERNEL(patch_fold) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *G = &a->t[1], *Y = &a->t[2];
const int64_t C = p_int(a, "C", 0), pt = p_int(a, "pt", 0);
const int64_t ph = p_int(a, "ph", 0), pw = p_int(a, "pw", 0);
const char* ms = p_str(a, "mul", ""), *as = p_str(a, "add", "");
/* Each number takes at least two characters but the last, so this bounds the count. */
const int64_t cap = (int64_t)((std::strlen(ms) + std::strlen(as)) / 2 + 2);
float* lists = (float*)scratch_raw((size_t)(2 * cap + 16) * sizeof(float));
float *mul = lists, *add = lists + cap;
if (C <= 0 || pt <= 0 || ph <= 0 || pw <= 0 || parse_f32(ms, mul, cap) != C ||
parse_f32(as, add, cap) != C || numel(G) < 3)
return RAD_E_SHAPE;
const int64_t F = at(G, 0), Hp = at(G, 1), Wp = at(G, 2);
if (F <= 0 || Hp <= 0 || Wp <= 0) return RAD_E_SHAPE;
const int64_t vol = pt * ph * pw, Hv = Hp * ph, Wv = Wp * pw, K = C * vol;
const int64_t rows = F * Hp * Wp;
if (rows_of(Y, K) < rows || rows_of(X, C) < F * pt * Hv * Wv) return RAD_E_SHAPE;
AVX_PARALLEL_FOR_IF(rows * K >= AVX_PAR_MIN)
for (int64_t pi = 0; pi < rows; ++pi) {
float* buf = scratch_f32((size_t)(K + C + 32));
float *row = buf, *xs = buf + K + 16;
const int64_t f = pi / (Hp * Wp), i = (pi / Wp) % Hp, j = pi % Wp;
for (int64_t sub = 0; sub < vol; ++sub) {
const int64_t ta = sub / (ph * pw), tb = (sub / pw) % ph, td = sub % pw;
const int64_t r = ((f * pt + ta) * Hv + i * ph + tb) * Wv + j * pw + td;
const float* xr = in_row(X, rowoff(X, r, C), C, xs);
for (int64_t c = 0; c < C; ++c) row[c * vol + sub] = keep(xr[c] * mul[c]) + add[c];
}
out_row(Y, rowoff(Y, pi, K), K, row);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ grid_pos3 */
/* A tile's rotary positions, f32(scale) g(c, n), g(c, n) = f32(f32(2 f32((c + 0.5) / n)) - 1)
* (libref): one value an axis index, taken once a launch. */
@@ -351,3 +439,351 @@ AVX_KERNEL(vtile_stitch) {
}
return RAD_OK;
}
/* ================================================================== the latent upscaler
* Voxel rows (t, i, j) of a T x H x W volume, channels innermost, the volume in `geom`. */
/* ------------------------------------------------------------------ conv3d_ndhwc */
/* A 3-D convolution as GEMMs over bands of patch rows, as conv2d_nhwc and conv1d take theirs. The
* weight [cout, cin, kt, k, k] is re-laid once a launch as [cout][dt][kh][kw][cin] (bf16 copies
* when it is bf16, f32 otherwise), so an output voxel's patch row is its kt k k input voxels'
* channel runs back to back, zeros outside the volume: bf16 copies when x is bf16 with dense
* rows, f32 otherwise. A band is one gemm_nt_into into f32 rows, the bias added there; each row
* is then rounded once into y, or with `res` rounded to y's dtype, its residual row added and
* rounded again. A band's residual rows are read before its rows of y are written, row by row,
* so `res` may be y. The patches and the weight live in thread_local vectors that keep their
* high-water size, leaving the GEMM's scratch arenas to it. */
AVX_KERNEL(conv3d_ndhwc) {
if (a->n_t < 6 || !rad_args_have(a, 4) || !rad_arg_in(a, 5)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *W = &a->t[1], *B = &a->t[2], *G = &a->t[3];
const RadTensor *R = rad_arg_in(a, 4), *Y = &a->t[5];
const int64_t cin = p_int(a, "cin", 0), cout = p_int(a, "cout", 0);
const int64_t k = p_int(a, "k", 0), kt = p_int(a, "kt", 0);
if (cin <= 0 || cout <= 0 || k <= 0 || kt <= 0 || !(k & 1) || !(kt & 1) || numel(G) < 3)
return RAD_E_SHAPE;
const int64_t T = at(G, 0), H = at(G, 1), Wd = at(G, 2);
if (T <= 0 || H <= 0 || Wd <= 0) return RAD_E_SHAPE;
const int64_t rows = T * H * Wd, taps = kt * k * k, KK = taps * cin;
if (rows_of(Y, cout) < rows || rows_of(X, cin) < rows || (R && rows_of(R, cout) < rows) ||
numel(W) < cout * KK || numel(B) < cout)
return RAD_E_SHAPE;
static thread_local std::vector<uint8_t> wbuf, pbuf;
static thread_local std::vector<float> ybuf;
/* ---- the weight, [cout][dt][kh][kw][cin] */
const bool w16 = W->dtype == RAD_BF16;
wbuf.resize((size_t)(cout * KK) * (w16 ? 2 : 4) + 64);
uint8_t* wp = wbuf.data();
AVX_PARALLEL_FOR_IF(cout * KK >= AVX_PAR_MIN)
for (int64_t co = 0; co < cout; ++co)
for (int64_t ci = 0; ci < cin; ++ci)
for (int64_t tp = 0; tp < taps; ++tp) {
const int64_t src = (co * cin + ci) * taps + tp, dst = co * KK + tp * cin + ci;
if (w16) ((uint16_t*)wp)[dst] = ((const uint16_t*)W->data)[src];
else ((float*)wp)[dst] = ld_elem(W->data, W->dtype, src);
}
const RadTensor wt = mat_view(wp, w16 ? (uint32_t)RAD_BF16 : (uint32_t)RAD_F32, cout, KK);
/* ---- bands of patch rows: at least 8 Mi elements and eight times the weight, so the GEMM's
* per-call weight pack is amortised, at most 32 Mi (64 MiB of bf16 kept after the call) */
const bool x16 = X->dtype == RAD_BF16 && laststride(X) == 1;
const size_t ael = x16 ? 2 : 4;
const int64_t want = std::min<int64_t>((int64_t)1 << 25,
std::max<int64_t>((int64_t)1 << 23, 8 * cout * KK));
const int64_t band = std::max<int64_t>(1, std::min(rows, want / KK));
pbuf.resize((size_t)(band * KK) * ael + 64);
ybuf.resize((size_t)(band * cout) + 16);
char* pp = (char*)pbuf.data();
float* yp = ybuf.data();
const int64_t pz = kt / 2, ps = k / 2;
for (int64_t r0 = 0; r0 < rows; r0 += band) {
const int64_t nb = std::min(band, rows - r0);
AVX_PARALLEL_FOR_IF(nb * KK >= AVX_PAR_MIN)
for (int64_t pr = 0; pr < nb; ++pr) {
const int64_t r = r0 + pr, t = r / (H * Wd), i = (r / Wd) % H, j = r % Wd;
char* d = pp + (size_t)(pr * KK) * ael;
for (int64_t dz = 0; dz < kt; ++dz)
for (int64_t kh = 0; kh < k; ++kh)
for (int64_t kw = 0; kw < k; ++kw, d += (size_t)cin * ael) {
const int64_t tz = t + dz - pz, iy = i + kh - ps, ix = j + kw - ps;
if (tz < 0 || tz >= T || iy < 0 || iy >= H || ix < 0 || ix >= Wd) {
std::memset(d, 0, (size_t)cin * ael);
continue;
}
const int64_t xo = rowoff(X, (tz * H + iy) * Wd + ix, cin);
if (x16) {
std::memcpy(d, (const uint16_t*)X->data + xo, (size_t)cin * 2);
} else {
const float* s = in_row(X, xo, cin, (float*)d);
if (s != (const float*)d) std::memcpy(d, s, (size_t)cin * 4);
}
}
}
const RadTensor pt = mat_view(pp, x16 ? (uint32_t)RAD_BF16 : (uint32_t)RAD_F32, nb, KK);
const RadTensor yt = mat_view(yp, RAD_F32, nb, cout);
const int st = gemm_nt_into(&pt, &wt, B, &yt, nb, cout, KK);
if (st < 0) return st;
AVX_PARALLEL_FOR_IF(nb * cout >= AVX_PAR_MIN)
for (int64_t pr = 0; pr < nb; ++pr) {
float* row = yp + pr * cout;
if (R) {
float* buf = scratch_f32((size_t)(2 * cout + 32));
float *tmp = buf, *rs = buf + cout + 16;
narrow_row(Y->dtype, row, cout, tmp);
const float* rr = in_row(R, rowoff(R, r0 + pr, cout), cout, rs);
for (int64_t c = 0; c < cout; ++c) row[c] += rr[c];
}
out_row(Y, rowoff(Y, r0 + pr, cout), cout, row);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ group_norm */
/* A group norm over all rows (libref's definition). The statistics are f64 sums over fixed blocks
* of 64 rows, each block's per-group sums in row-major order and the blocks combined in block
* order, so the bytes do not depend on the thread count; two passes, the variance about the f64
* mean. The rows are then normalised in f32 from per-channel tables (the group's mean and rstd,
* w, b, 1 + scale and shift), each product kept apart from the add after it as libref rounds
* them, and rounded once. stats[0, 2 groups) gets the mean and rstd; the rest of `stats` is not
* touched, the block sums being kept in this plugin's own memory. */
AVX_KERNEL(group_norm) {
if (a->n_t < 6 || !rad_args_have(a, 3) || !rad_arg_in(a, 4) || !rad_arg_in(a, 5))
return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *W = &a->t[1], *B = &a->t[2], *MD = rad_arg_in(a, 3);
const RadTensor *ST = &a->t[4], *Y = &a->t[5];
const int64_t n = p_int(a, "n", 0), G = p_int(a, "groups", 0);
const float eps = p_f32(a, "eps", 1e-5f);
static const char* const kActs[] = { "none", "silu" };
const int act = p_enum(a, "act", kActs, 2, 0);
if (act < 0 || act > 1) return RAD_E_INVAL;
if (n <= 0 || G <= 0 || n % G || numel(W) < n || numel(B) < n ||
(MD && numel(MD) < 2 * n) || numel(ST) < G * (2 + 3 * 1024))
return RAD_E_SHAPE;
const int64_t rows = rows_of(Y, n), cg = n / G;
if (rows <= 0 || rows_of(X, n) < rows) return RAD_E_SHAPE;
constexpr int64_t kBlock = 64;
const int64_t nblk = (rows + kBlock - 1) / kBlock;
const double cnt = (double)rows * (double)cg;
static thread_local std::vector<double> part;
part.assign((size_t)(nblk * G + 2 * G), 0.0);
double *ps = part.data(), *mu = ps + nblk * G, *var = mu + G;
/* Block bk's per-group sums of x, or with `dev` of (x - mu)^2, into ps[bk G, (bk + 1) G). */
for (const bool dev : { false, true }) {
AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN)
for (int64_t bk = 0; bk < nblk; ++bk) {
float* xs = scratch_f32((size_t)n + 16);
double* s = ps + bk * G;
const int64_t r1 = std::min(rows, (bk + 1) * kBlock);
for (int64_t r = bk * kBlock; r < r1; ++r) {
const float* x = in_row(X, rowoff(X, r, n), n, xs);
for (int64_t g = 0; g < G; ++g) {
const float* xg = x + g * cg;
double acc = s[g];
if (dev) {
for (int64_t c = 0; c < cg; ++c) {
const double d = (double)xg[c] - mu[g];
acc += d * d;
}
} else {
for (int64_t c = 0; c < cg; ++c) acc += (double)xg[c];
}
s[g] = acc;
}
}
}
for (int64_t g = 0; g < G; ++g) {
double tot = 0.0;
for (int64_t bk = 0; bk < nblk; ++bk) {
tot += ps[bk * G + g];
ps[bk * G + g] = 0.0;
}
(dev ? var : mu)[g] = tot / cnt;
}
}
/* Per-channel tables: mean, rstd, w, b, 1 + scale and shift. */
float* tab = (float*)scratch_raw((size_t)(6 * n + 16) * sizeof(float));
float *mc = tab, *rc = mc + n, *wc = rc + n, *bc = wc + n, *sc = bc + n, *sh = sc + n;
for (int64_t g = 0; g < G; ++g) {
const float m = (float)mu[g], rs = 1.0f / std::sqrt((float)var[g] + eps);
stt(ST, offlin(ST, 2 * g), m);
stt(ST, offlin(ST, 2 * g + 1), rs);
for (int64_t c = g * cg; c < (g + 1) * cg; ++c) { mc[c] = m; rc[c] = rs; }
}
for (int64_t c = 0; c < n; ++c) {
wc[c] = ld_elem(W->data, W->dtype, c);
bc[c] = ld_elem(B->data, B->dtype, c);
sc[c] = MD ? 1.0f + ldt(MD, offlin(MD, c)) : 1.0f;
sh[c] = MD ? ldt(MD, offlin(MD, n + c)) : 0.0f;
}
const bool modded = MD != nullptr;
AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
float* buf = scratch_f32((size_t)(2 * n + 32));
float *xs = buf, *v = buf + n + 16;
const float* x = in_row(X, rowoff(X, r, n), n, xs);
const vf one = vf_set1(1.0f);
int64_t c = 0;
for (; c + VF_N <= n; c += VF_N) {
vf t = vf_mul(vf_sub(vf_loadu(x + c), vf_loadu(mc + c)), vf_loadu(rc + c));
t = vf_add(keep(vf_mul(t, vf_loadu(wc + c))), vf_loadu(bc + c));
if (modded) t = vf_add(keep(vf_mul(t, vf_loadu(sc + c))), vf_loadu(sh + c));
if (act) t = vf_div(t, vf_add(one, vf_exp(vf_sub(vf_zero(), t))));
vf_storeu(v + c, t);
}
for (; c < n; ++c) {
float t = (x[c] - mc[c]) * rc[c];
t = keep(t * wc[c]) + bc[c];
if (modded) t = keep(t * sc[c]) + sh[c];
if (act) t = t / (1.0f + sc_exp(-t));
v[c] = t;
}
out_row(Y, rowoff(Y, r, n), n, v);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ tconv_dw */
/* A depthwise convolution along the frames, libref's sum bit for bit: each output row adds its
* in-volume taps in ascending order, every product rounded before its add, then the bias, a
* vector across the channels. The taps are re-laid once a launch as [k][n] f32 beside the bias. */
AVX_KERNEL(tconv_dw) {
if (!rad_args_have(a, 5)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *W = &a->t[1], *B = &a->t[2], *G = &a->t[3], *Y = &a->t[4];
const int64_t n = p_int(a, "n", 0), k = p_int(a, "k", 0);
if (n <= 0 || k <= 0 || !(k & 1) || numel(G) < 3 || numel(W) < n * k || numel(B) < n)
return RAD_E_SHAPE;
const int64_t T = at(G, 0), H = at(G, 1), Wd = at(G, 2);
if (T <= 0 || H <= 0 || Wd <= 0) return RAD_E_SHAPE;
const int64_t P = H * Wd, rows = T * P;
if (rows_of(Y, n) < rows || rows_of(X, n) < rows) return RAD_E_SHAPE;
float* tab = (float*)scratch_raw((size_t)((k + 1) * n + 16) * sizeof(float));
for (int64_t c = 0; c < n; ++c) {
for (int64_t j = 0; j < k; ++j) tab[j * n + c] = ld_elem(W->data, W->dtype, c * k + j);
tab[k * n + c] = ld_elem(B->data, B->dtype, c);
}
const float* bias = tab + k * n;
AVX_PARALLEL_FOR_IF(rows * n * k >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
float* buf = scratch_f32((size_t)(2 * n + 32));
float *xs = buf, *acc = buf + n + 16;
const int64_t t = r / P, p = r % P;
for (int64_t c = 0; c < n; ++c) acc[c] = 0.0f;
for (int64_t j = 0; j < k; ++j) {
const int64_t tz = t + j - k / 2;
if (tz < 0 || tz >= T) continue;
const float* x = in_row(X, rowoff(X, tz * P + p, n), n, xs);
const float* w = tab + j * n;
int64_t c = 0;
for (; c + VF_N <= n; c += VF_N)
vf_storeu(acc + c, vf_add(vf_loadu(acc + c),
keep(vf_mul(vf_loadu(w + c), vf_loadu(x + c)))));
for (; c < n; ++c) acc[c] = acc[c] + keep(w[c] * x[c]);
}
int64_t c = 0;
for (; c + VF_N <= n; c += VF_N)
vf_storeu(acc + c, vf_add(vf_loadu(acc + c), vf_loadu(bias + c)));
for (; c < n; ++c) acc[c] = acc[c] + bias[c];
out_row(Y, rowoff(Y, r, n), n, acc);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ resize_bilinear */
/* Each frame resized as torch's align_corners=False bilinear at a size, libref's arithmetic bit
* for bit: the source rows, columns and weights of every output row and column are taken once a
* launch (libref's f32 steps, the product before the subtraction kept apart), and an output row
* is its four source rows blended, a vector across the channels, each product kept apart from
* the add after it. */
AVX_KERNEL(resize_bilinear) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *G = &a->t[1], *Y = &a->t[2];
const int64_t n = p_int(a, "n", 0);
if (n <= 0 || numel(G) < 5) return RAD_E_SHAPE;
const int64_t T = at(G, 0), h = at(G, 1), w = at(G, 2), H = at(G, 3), Wd = at(G, 4);
if (T <= 0 || h <= 0 || w <= 0 || H <= 0 || Wd <= 0) return RAD_E_SHAPE;
const int64_t rows = T * H * Wd;
if (rows_of(Y, n) < rows || rows_of(X, n) < T * h * w) return RAD_E_SHAPE;
/* Per output index of each axis: two source indices, then two weights. */
const int64_t E = H + Wd;
int64_t* idx =
(int64_t*)scratch_raw((size_t)E * (2 * sizeof(int64_t) + 2 * sizeof(float)) + 64);
float* lam = (float*)(idx + 2 * E);
auto axis = [&](int64_t e, int64_t o, int64_t in, int64_t out) {
const float s = (float)in / (float)out;
float src = keep(s * ((float)o + 0.5f)) - 0.5f;
if (src < 0.0f) src = 0.0f;
const int64_t i0 = (int64_t)src;
idx[2 * e] = i0;
idx[2 * e + 1] = i0 + (i0 < in - 1 ? 1 : 0);
lam[2 * e + 1] = src - (float)i0;
lam[2 * e] = 1.0f - lam[2 * e + 1];
};
for (int64_t i = 0; i < H; ++i) axis(i, i, h, H);
for (int64_t j = 0; j < Wd; ++j) axis(H + j, j, w, Wd);
AVX_PARALLEL_FOR_IF(rows * n * 4 >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
const int64_t t = r / (H * Wd), i = (r / Wd) % H, j = r % Wd;
const int64_t i0 = idx[2 * i], i1 = idx[2 * i + 1];
const int64_t j0 = idx[2 * (H + j)], j1 = idx[2 * (H + j) + 1];
const float l0y = lam[2 * i], l1y = lam[2 * i + 1];
const float l0x = lam[2 * (H + j)], l1x = lam[2 * (H + j) + 1];
float* buf = scratch_f32((size_t)(5 * n + 80));
float *s0 = buf, *s1 = s0 + n + 16, *s2 = s1 + n + 16, *s3 = s2 + n + 16, *v = s3 + n + 16;
const float* x00 = in_row(X, rowoff(X, (t * h + i0) * w + j0, n), n, s0);
const float* x01 = in_row(X, rowoff(X, (t * h + i0) * w + j1, n), n, s1);
const float* x10 = in_row(X, rowoff(X, (t * h + i1) * w + j0, n), n, s2);
const float* x11 = in_row(X, rowoff(X, (t * h + i1) * w + j1, n), n, s3);
const vf a0 = vf_set1(l0x), a1 = vf_set1(l1x), b0 = vf_set1(l0y), b1 = vf_set1(l1y);
int64_t c = 0;
for (; c + VF_N <= n; c += VF_N) {
const vf top = vf_add(keep(vf_mul(a0, vf_loadu(x00 + c))),
keep(vf_mul(a1, vf_loadu(x01 + c))));
const vf bot = vf_add(keep(vf_mul(a0, vf_loadu(x10 + c))),
keep(vf_mul(a1, vf_loadu(x11 + c))));
vf_storeu(v + c, vf_add(keep(vf_mul(b0, top)), keep(vf_mul(b1, bot))));
}
for (; c < n; ++c) {
const float top = keep(l0x * x00[c]) + keep(l1x * x01[c]);
const float bot = keep(l0x * x10[c]) + keep(l1x * x11[c]);
v[c] = keep(l0y * top) + keep(l1y * bot);
}
out_row(Y, rowoff(Y, r, n), n, v);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ flow_renoise */
/* x = f32(f32(f32(1 - s) x) + f32(s z)) over x's rows, z the (seed, stream) normal stream at
* k n + c (libref's, bit for bit): a row's draws come a Philox block at a time. */
AVX_KERNEL(flow_renoise) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *SG = &a->t[0], *SD = &a->t[1], *X = &a->t[2];
const int64_t n = p_int(a, "n", 0), sm = p_int(a, "stream", 0);
if (n <= 0 || sm < 0 || numel(SG) < 1 || numel(SD) < 2) return RAD_E_SHAPE;
const int64_t rows = rows_of(X, n);
if (rows <= 0) return RAD_E_SHAPE;
const float s = ldt(SG, offlin(SG, 0)), oms = 1.0f - s;
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 vo = vf_set1(oms), vs = vf_set1(s);
int64_t c = 0;
for (; c + VF_N <= n; c += VF_N)
vf_storeu(o + c, vf_add(keep(vf_mul(vo, vf_loadu(x + c))),
keep(vf_mul(vs, vf_loadu(z + c)))));
for (; c < n; ++c) o[c] = keep(oms * x[c]) + keep(s * z[c]);
out_row(X, rowoff(X, k, n), n, o);
}
return RAD_OK;
}