Archived
radiance 1.13.0
New models: - Tencent Hy-Memory (HYV3) from its BF16, FP8 and GGUF checkpoints, with its native tool calls - M2XFP4: metadata-augmented MXFP4 weights for Qwen3.8-27B and its DFlash2 drafter (top-1 89.1% -> 92.4% against plain MXFP4), its quantiser and recipe, and ParoQuant's M2XFP4 checkpoints - Gemma 4 E2B/E4B audio input; NVIDIA-Nemotron-3.5-Bolt-E4B in int8 and 4-bit, on two cards by default; Ornith-1.5-122B compose files Speed: - Weight reads beside the decode all-reduces (qwen35_fp8, gemma3; --weight-prefetch, on by default): the 27B MXFP4 with DFlash2 61.2 -> 67.9 tok/s at c1, TranslateGemma 27B +13% - Head-256 split-KV decode attention at 256 workgroups and without bank conflicts (27B: +3-5% a step at 25-41K context), and the prefill KV split for small launches (a short turn over a long context decodes the other sequences 27% faster in that step) - MXFP4 decode gate_up, activation and fp8 quantiser in one launch (27B, Gemma 3 up to 64 rows: TranslateGemma +2-5% at 2-8 streams) - ParoQuant int4/int5 tiled GEMMs 6-9% faster - Speculation: a step's tokens are sent before the drafter's passes end, no step waits behind one that ends its requests, and the MTP head's first draft no longer collapses after short chats in the prefix cache Features: - --kv-resident N: KV streaming for a context bigger than VRAM (qwen4exp_fp8, GLM-5.3) - MiniMax-H3: reference media and keyframe conditioning with the VAE encoders - Krea 2 rebalance presets balanced, detail and subtle - The dashboard's request history; a stall watch that reports a frozen step's thread and stack - rad-tune at the served configuration, collectives measured as the exchange; Gemma 4 E2B/E4B's per-layer embeddings read ahead off the step path - A GPTQ calibration tap and recipe for Gemma 4 31B's w4a8 container Fixes: - The two-rank all-reduce checks that a peer's push has landed before reducing it - z-lab's DFlash drafter on Gemma 4 31B keeps drafting after a prefix-cache hit - MiniMax-H3's modulation rows Changes: Plugin ABI 31: out-of-tree plugins must be rebuilt. RADIANCE_PREFETCH_BESIDE is --weight-prefetch now; RADIANCE_NO_HUGE_PAGES, RADIANCE_IDS_EARLY, RADIANCE_ATTN_BAND_SPLIT, R4D_PREFILL_SPLITS, RAD_PLE_TIMING and RAD_PLE_CACHE_ROWS are gone.
This commit is contained in:
commit
6645337f69
198 files changed
+20904
-3240
No files matched your search
+279
-18
@@ -63,6 +63,20 @@ bool split_tiles(int64_t len, int64_t tile, int64_t overlap, std::vector<int64_t
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The encoder's split over one axis (comfy/ldm/minimax/vae.py, `split_tiles`): split_tiles,
|
||||
* except that an axis no longer than a tile is one tile, short if need be, where the decoder's
|
||||
* split demands an exact fit. */
|
||||
bool split_tiles_axis(int64_t len, int64_t tile, int64_t overlap, std::vector<int64_t>* start,
|
||||
std::vector<int64_t>* ov) {
|
||||
if (tile >= len) {
|
||||
start->clear();
|
||||
ov->clear();
|
||||
start->push_back(0);
|
||||
return len > 0 && tile > 0 && overlap >= 0 && overlap < tile;
|
||||
}
|
||||
return split_tiles(len, tile, overlap, start, ov);
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* ------------------------------------------------------------------ patch_unfold */
|
||||
@@ -346,9 +360,10 @@ int ref_vtile_stitch(const RadArgs* a, RadStream) {
|
||||
* `geom` carries the request's volume; an output holds at least the rows the volume writes. */
|
||||
|
||||
/* ------------------------------------------------------------------ conv3d_ndhwc */
|
||||
/* A 3-D convolution, stride 1 with `same` zero padding: kt / 2 frames and k / 2 rows and columns
|
||||
* on each side (kt and k odd). x [>= T H W, cin], y and `res` [>= T H W, cout], geom = (T, H, W);
|
||||
* w is PyTorch's Conv3d weight [cout, cin, kt, k, k] and b [cout]:
|
||||
/* A 3-D convolution over the volume geom = (T, H, W). The defaults are stride 1 with `same` zero
|
||||
* padding: kt / 2 frames and k / 2 rows and columns on each side (kt and k odd); x
|
||||
* [>= T H W, cin] and y and `res` [>= rows out, cout]; w is PyTorch's Conv3d weight
|
||||
* [cout, cin, kt, k, k] and b [cout]:
|
||||
*
|
||||
* acc = sum over (dt, kh, kw, ci) of w[o, ci, dt, kh, kw] x[(t + dt - kt/2, i + kh - k/2,
|
||||
* j + kw - k/2), ci]
|
||||
@@ -359,11 +374,33 @@ int ref_vtile_stitch(const RadArgs* a, RadStream) {
|
||||
* residual is added, as a residual block's skip(x) + h adds two bf16 tensors. `res` may be y
|
||||
* itself: each output element reads its own residual before its write, and no other.
|
||||
*
|
||||
* The optional `stride` (spatial) and `stride_t` pad first and then stride, `causal` 1 takes the
|
||||
* temporal padding front-only (kt - 1 zeros, none behind), `pad` `down` pads none above and left
|
||||
* and one below and right, and `reflect` 1 mirrors the spatial padding instead of zeroing it;
|
||||
* these CausalConv3d forms need k = kt = 3. Output (t, i, j) then reads
|
||||
* x[(t stride_t + dt - ft, i stride + kh - pt, j stride + kw - pl), ci] over the lengths
|
||||
*
|
||||
* ((T + ft + bk - kt) / stride_t + 1) ((H + pt + pb - k) / stride + 1)
|
||||
* ((W + pl + pr - k) / stride + 1) (floor divisions)
|
||||
*
|
||||
* with ft / bk and pt / pb / pl / pr the front / back and top / bottom / left / right pads. The
|
||||
* time axis is zero-padded unless `tpad` says otherwise (below); `reflect` mirrors each spatial
|
||||
* axis once, torch's `reflect` (x[-n] = x[n], x[D + n] = x[D - 2 - n]), so each pad must be below
|
||||
* the axis it pads.
|
||||
*
|
||||
* `tpad` sets the frames' padding (rows and columns stay zero-padded): `zero` (the default),
|
||||
* `replicate` (the edge frame repeated, kt/2 a side: LTX-2's decoder) or `causal` (the first
|
||||
* frame repeated kt - 1 times in front, nothing after: LTX-2's encoder). With `causal`, `tpre`
|
||||
* more copies of the first frame stand in front of those, and y has T + tpre frames (LTX-2's
|
||||
* temporal downsampler, which prepends its first frame before its convolution). */
|
||||
* temporal downsampler, which prepends its first frame before its convolution). It folds into
|
||||
* the same grid math: replicate keeps ft = kt / 2 and clamps the time reads to the volume;
|
||||
* causal-`tpre` takes ft = kt - 1 + tpre and bk = 0, clamping below. `tpad` `causal` repeats
|
||||
* where `causal` 1 zeroes, and the two are refused together. */
|
||||
static int64_t idiv_floor(int64_t a, int64_t b) {
|
||||
const int64_t q = a / b;
|
||||
return (a % b && (a < 0) != (b < 0)) ? q - 1 : q;
|
||||
}
|
||||
|
||||
int ref_conv3d_ndhwc(const RadArgs* a, RadStream) {
|
||||
if (a->n_t < 6 || !have(a, 4) || !t_in(a, 5)) return RAD_E_INVAL;
|
||||
const RadTensor *x = &a->t[0], *w = &a->t[1], *b = &a->t[2], *geom = &a->t[3];
|
||||
@@ -376,33 +413,52 @@ int ref_conv3d_ndhwc(const RadArgs* a, RadStream) {
|
||||
if (cin <= 0 || cout <= 0 || k <= 0 || kt <= 0 || !(k & 1) || !(kt & 1) || tpre < 0 ||
|
||||
numel(geom) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t sp = p_int(a, "stride", 1), st = p_int(a, "stride_t", 1);
|
||||
const int64_t causal = p_int(a, "causal", 0), reflect = p_int(a, "reflect", 0);
|
||||
const char* pads = p_str(a, "pad", "same");
|
||||
const bool down = pads && !std::strcmp(pads, "down");
|
||||
if (sp <= 0 || st <= 0 || (causal && causal != 1) || (reflect && reflect != 1) ||
|
||||
(causal && tpad) /* zero-causal and replicated-causal contradict */ ||
|
||||
!(!pads || !std::strcmp(pads, "same") || down))
|
||||
return RAD_E_SHAPE;
|
||||
const bool gen = sp != 1 || st != 1 || causal || down || reflect;
|
||||
if (gen && (k != 3 || kt != 3)) return RAD_E_SHAPE;
|
||||
const int64_t T = ldi(geom, 0), H = ldi(geom, 1), W = ldi(geom, 2);
|
||||
if (T <= 0 || H <= 0 || W <= 0) return RAD_E_SHAPE;
|
||||
const int64_t rows = (T + tpre) * H * W, taps = kt * k * k;
|
||||
if (rows_of(y, cout) < rows || rows_of(x, cin) < T * H * W ||
|
||||
(res && rows_of(res, cout) < rows) || numel(w) < cout * cin * taps || numel(b) < cout)
|
||||
const int64_t ft = causal ? kt - 1 : tpad == 2 ? kt - 1 + tpre : kt / 2;
|
||||
const int64_t bk = (causal || tpad == 2) ? 0 : kt / 2;
|
||||
const int64_t pt = down ? 0 : k / 2, pb = down ? 1 : k / 2;
|
||||
const int64_t pl = down ? 0 : k / 2, pr = down ? 1 : k / 2;
|
||||
if (reflect && (std::max(pt, pb) >= H || std::max(pl, pr) >= W)) return RAD_E_SHAPE;
|
||||
const int64_t T2 = idiv_floor(T + ft + bk - kt, st) + 1;
|
||||
const int64_t H2 = idiv_floor(H + pt + pb - k, sp) + 1;
|
||||
const int64_t W2 = idiv_floor(W + pl + pr - k, sp) + 1;
|
||||
if (T2 <= 0 || H2 <= 0 || W2 <= 0) return RAD_E_SHAPE;
|
||||
const int64_t rows = T * H * W, orows = T2 * H2 * W2, taps = kt * k * k;
|
||||
if (rows_of(y, cout) < orows || rows_of(x, cin) < rows ||
|
||||
(res && rows_of(res, cout) < orows) || numel(w) < cout * cin * taps || numel(b) < cout)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t pz = kt / 2, ps = k / 2, xs = laststride(x), ys = laststride(y);
|
||||
const int64_t xs = laststride(x), ys = laststride(y);
|
||||
const int64_t rs = res ? laststride(res) : 0;
|
||||
const uint32_t dt = y->dtype;
|
||||
REF_PAR_FOR(rows * cout * cin * taps)
|
||||
for (int64_t r = 0; r < rows; ++r) {
|
||||
const int64_t t = r / (H * W), i = (r / W) % H, j = r % W;
|
||||
REF_PAR_FOR(orows * cout * cin * taps)
|
||||
for (int64_t r = 0; r < orows; ++r) {
|
||||
const int64_t t = r / (H2 * W2), i = (r / W2) % H2, j = r % W2;
|
||||
const int64_t yb = rowoff(y, r, cout), rb = res ? rowoff(res, r, cout) : 0;
|
||||
for (int64_t o = 0; o < cout; ++o) {
|
||||
float acc = 0.0f;
|
||||
for (int64_t dz = 0; dz < kt; ++dz) {
|
||||
/* The frame tap dz reads: zero pads, the edge frame repeated, or causal (the first
|
||||
* frame repeated in front, `tpre` more copies of it before those). */
|
||||
int64_t tz = t + dz - pz;
|
||||
if (tpad == 1) tz = std::min<int64_t>(std::max<int64_t>(tz, 0), T - 1);
|
||||
else if (tpad == 2) tz = std::max<int64_t>(t + dz - (kt - 1) - tpre, 0);
|
||||
int64_t tz = t * st + dz - ft;
|
||||
if (tpad) /* replicate and causal-tpre clamp; `zero`/`causal` pad */
|
||||
tz = std::min<int64_t>(std::max<int64_t>(tz, 0), T - 1);
|
||||
if (tz < 0 || tz >= T) continue;
|
||||
for (int64_t kh = 0; kh < k; ++kh) {
|
||||
const int64_t iy = i + kh - ps;
|
||||
int64_t iy = i * sp + kh - pt;
|
||||
if (reflect) iy = iy < 0 ? -iy : (iy >= H ? 2 * H - 2 - iy : iy);
|
||||
if (iy < 0 || iy >= H) continue;
|
||||
for (int64_t kw = 0; kw < k; ++kw) {
|
||||
const int64_t ix = j + kw - ps;
|
||||
int64_t ix = j * sp + kw - pl;
|
||||
if (reflect) ix = ix < 0 ? -ix : (ix >= W ? 2 * W - 2 - ix : ix);
|
||||
if (ix < 0 || ix >= W) continue;
|
||||
const int64_t xb = rowoff(x, (tz * H + iy) * W + ix, cin);
|
||||
const int64_t wb = o * cin * taps + (dz * k + kh) * k + kw;
|
||||
@@ -614,3 +670,208 @@ int ref_flow_renoise(const RadArgs* a, RadStream) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ cond_aug */
|
||||
/* A visual condition's rows blended with noise, as ComfyUI's `_cond_video_rows`
|
||||
* (comfy/ldm/minimax/model.py) does before they become condition rows. Element e = k n + c:
|
||||
*
|
||||
* x[k, c] = f32(f32(a x[k, c]) + f32(oma z(e)))
|
||||
*
|
||||
* a = `aug` (VISUAL_COND_TIMESTEP, 0.999), oma the f32 of the f64 1 - aug (ComfyUI subtracts in
|
||||
* double), z(e) = rad_philox_normal_s(seed, stream, e) (abi/rad_philox.h), `seed` the request's
|
||||
* two u32 words, low first. x is f32 [M, n] at its row pitch. ComfyUI restarts the generator for
|
||||
* every condition and every forward, so the blend is a fixed function of the request and is
|
||||
* applied once, per media item. */
|
||||
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;
|
||||
}
|
||||
|
||||
/* ================================================================== the tiled encoder
|
||||
* A one-frame media larger than a tile (a reference picture or a keyframe) is encoded as vae.py's
|
||||
* `tiled_encode` does: the pixel plane is cut into split_tiles_axis' tiles, each goes through the
|
||||
* encoder alone, and the tiles' moments are blended into one latent plane. These ops are the plan,
|
||||
* the cut and the blend; the encoder is the per-level ops, issued per tile. Tile starts, extents
|
||||
* and counts come from `geom`, the request's (clip, H, W) on the device, never from the declared
|
||||
* parameters. */
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_plan */
|
||||
/* The tiles of one frame of `geom` = (clip, H, W) pixels: split_tiles_axis over each spatial axis
|
||||
* in latent units (`tile` a side, `overlap` the least blend, `ratio` pixels a latent), numbered
|
||||
* row-major. Row t of y (i32 [M, 22]):
|
||||
*
|
||||
* [0] [1] the tile's first pixel (y0, x0)
|
||||
* [2] [3] its pixel extent (th, tw): `tile` x ratio, except a lone short tile
|
||||
* 3 L + 4..6 level L's volume (1, th / 2^min(L, 4), tw / 2^min(L, 4)), L = 0..5: venc_geom's
|
||||
* level layout for one frame (levels 4 and 5 share a size)
|
||||
*
|
||||
* `M` is the band's bound, a ceiling only (band_rows); y must hold the tiles. */
|
||||
int ref_vtile_plan(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *geom = &a->t[0], *y = &a->t[1];
|
||||
const int64_t band = p_int(a, "M", 0), tile = p_int(a, "tile", 0);
|
||||
const int64_t overlap = p_int(a, "overlap", 0), ratio = p_int(a, "ratio", 0);
|
||||
if (tile <= 0 || overlap < 0 || overlap >= tile || ratio <= 0 || numel(geom) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = ldi(geom, 1), W = ldi(geom, 2);
|
||||
std::vector<int64_t> sy, sx, oy, ox;
|
||||
if (H <= 0 || W <= 0 || H % ratio || W % ratio ||
|
||||
!split_tiles_axis(H / ratio, tile, overlap, &sy, &oy) ||
|
||||
!split_tiles_axis(W / ratio, tile, overlap, &sx, &ox))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
const int64_t M = band_rows(nh * nw, band);
|
||||
if (M <= 0 || rows_of(y, 22) < M) return RAD_E_SHAPE;
|
||||
REF_PAR_FOR(M)
|
||||
for (int64_t t = 0; t < M; ++t) {
|
||||
const int64_t i = t / nw, j = t % nw;
|
||||
const int64_t th = std::min(tile, H / ratio - sy[(size_t)i]) * ratio;
|
||||
const int64_t tw = std::min(tile, W / ratio - sx[(size_t)j]) * ratio;
|
||||
const int64_t yb = rowoff(y, t, 22), ys = laststride(y);
|
||||
const int64_t v[22] = { sy[(size_t)i] * ratio, sx[(size_t)j] * ratio, th, tw,
|
||||
1, th, tw, 1, th / 2, tw / 2, 1, th / 4, tw / 4,
|
||||
1, th / 8, tw / 8, 1, th / 16, tw / 16, 1, th / 16, tw / 16 };
|
||||
for (int64_t c = 0; c < 22; ++c) st_int(y->dtype, y->data, yb + c * ys, v[c]);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_crop */
|
||||
/* One frame's pixel plane cut into vtile_plan's tiles: x f32 [>= H W, 3] is the plane (pixel
|
||||
* (Y, X) at row Y W + X), geom = (clip, H, W) in pixels; y f32 [M, 3] holds the tiles' pixels
|
||||
* tile-major, each tile's rows in plane order. The tiles overlap, so y is larger than x and a
|
||||
* buffer of its own. `M` is a ceiling only (band_rows). */
|
||||
int ref_vtile_crop(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *x = &a->t[0], *geom = &a->t[1], *y = &a->t[2];
|
||||
const int64_t band = p_int(a, "M", 0), tile = p_int(a, "tile", 0);
|
||||
const int64_t overlap = p_int(a, "overlap", 0), ratio = p_int(a, "ratio", 0);
|
||||
if (tile <= 0 || overlap < 0 || overlap >= tile || ratio <= 0 || numel(geom) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = ldi(geom, 1), W = ldi(geom, 2);
|
||||
std::vector<int64_t> sy, sx, oy, ox;
|
||||
if (H <= 0 || W <= 0 || H % ratio || W % ratio ||
|
||||
!split_tiles_axis(H / ratio, tile, overlap, &sy, &oy) ||
|
||||
!split_tiles_axis(W / ratio, tile, overlap, &sx, &ox))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
std::vector<int64_t> off((size_t)(nh * nw + 1), 0);
|
||||
for (int64_t i = 0; i < nh; ++i) {
|
||||
const int64_t th = std::min(tile, H / ratio - sy[(size_t)i]) * ratio;
|
||||
for (int64_t j = 0; j < nw; ++j)
|
||||
off[(size_t)(i * nw + j + 1)] =
|
||||
off[(size_t)(i * nw + j)] +
|
||||
th * std::min(tile, W / ratio - sx[(size_t)j]) * ratio;
|
||||
}
|
||||
const int64_t M = band_rows(off.back(), band);
|
||||
if (M <= 0 || rows_of(x, 3) < H * W || rows_of(y, 3) < M) return RAD_E_SHAPE;
|
||||
REF_PAR_FOR(M * 3)
|
||||
for (int64_t r = 0; r < M; ++r) {
|
||||
const int64_t t = (int64_t)(std::upper_bound(off.begin(), off.end(), r) - off.begin()) - 1;
|
||||
const int64_t i = t / nw, j = t % nw;
|
||||
const int64_t tw = std::min(tile, W / ratio - sx[(size_t)j]) * ratio;
|
||||
const int64_t k = r - off[(size_t)t];
|
||||
const int64_t src = (sy[(size_t)i] * ratio + k / tw) * W + sx[(size_t)j] * ratio + k % tw;
|
||||
const int64_t xb = rowoff(x, src, 3), yb = rowoff(y, r, 3), xs = laststride(x);
|
||||
const int64_t ys = laststride(y);
|
||||
for (int64_t c = 0; c < 3; ++c) stt(y, yb + c * ys, ldt(x, xb + c * xs));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ vtile_join */
|
||||
/* The tiles' moments blended into one latent plane, as vae.py's `tiled_encode` does: x holds the
|
||||
* tiles' n-channel moments tile-major (vtile_crop's layout at latent extent), geom = (clip, lat_h,
|
||||
* lat_w) in latents, and y f32 or bf16 [M, n] is the canvas, M = lat_h lat_w. Position (Y, X)
|
||||
* belongs to the tile (i, j) whose range [start, next start) holds it, at (ly, lx) within it;
|
||||
* T(i, j, ly, lx) is that tile's value. The neighbours are read unblended and the top blend comes
|
||||
* before the left one; every product and sum rounds to x's dtype (`rd`), with
|
||||
* wa(p, e) = rd(1 - rd(p / e)) and wb(p, e) = rd(p / e):
|
||||
*
|
||||
* v = T(i, j, ly, lx)
|
||||
* v = rd(rd(T(i - 1, j, ly + tile - ev) * wa(ly, ev)) + rd(v * wb(ly, ev))) ev = ov_y[i - 1]
|
||||
* v = rd(rd(T(i, j - 1, lx + tile - eh) * wa(lx, eh)) + rd(v * wb(lx, eh))) eh = ov_x[j - 1]
|
||||
*
|
||||
* the first when i > 0 and ly < ev, the second when j > 0 and lx < eh. Only a lone tile is short,
|
||||
* and it has no neighbour, so a neighbour is always `tile` long on the blended axis. `M` is a
|
||||
* ceiling only (band_rows). */
|
||||
int ref_vtile_join(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *x = &a->t[0], *geom = &a->t[1], *y = &a->t[2];
|
||||
const int64_t band = p_int(a, "M", 0), tile = p_int(a, "tile", 0);
|
||||
const int64_t overlap = p_int(a, "overlap", 0), n = p_int(a, "n", 0);
|
||||
if (tile <= 0 || overlap < 0 || overlap >= tile || n <= 0 || numel(geom) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t H = ldi(geom, 1), W = ldi(geom, 2);
|
||||
std::vector<int64_t> sy, sx, oy, ox;
|
||||
if (H <= 0 || W <= 0 || !split_tiles_axis(H, tile, overlap, &sy, &oy) ||
|
||||
!split_tiles_axis(W, tile, overlap, &sx, &ox))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t nh = (int64_t)sy.size(), nw = (int64_t)sx.size();
|
||||
const int64_t M = band_rows(H * W, band);
|
||||
std::vector<int64_t> off((size_t)(nh * nw + 1), 0);
|
||||
for (int64_t i = 0; i < nh; ++i) {
|
||||
const int64_t th = std::min(tile, H - sy[(size_t)i]);
|
||||
for (int64_t j = 0; j < nw; ++j)
|
||||
off[(size_t)(i * nw + j + 1)] =
|
||||
off[(size_t)(i * nw + j)] + th * std::min(tile, W - sx[(size_t)j]);
|
||||
}
|
||||
if (M <= 0 || rows_of(x, n) < off.back() || rows_of(y, n) < M) return RAD_E_SHAPE;
|
||||
/* The owner tile of every canvas row and column. */
|
||||
std::vector<int64_t> own_y((size_t)H), own_x((size_t)W);
|
||||
for (int64_t Y = 0, i = 0; Y < H; ++Y) {
|
||||
while (i + 1 < nh && Y >= sy[(size_t)(i + 1)]) ++i;
|
||||
own_y[(size_t)Y] = i;
|
||||
}
|
||||
for (int64_t X = 0, j = 0; X < W; ++X) {
|
||||
while (j + 1 < nw && X >= sx[(size_t)(j + 1)]) ++j;
|
||||
own_x[(size_t)X] = j;
|
||||
}
|
||||
const uint32_t dt = x->dtype;
|
||||
auto rd = [dt](float v) { return round_dt(dt, v); };
|
||||
auto wb = [&](int64_t p, int64_t e) { return rd((float)p / (float)e); };
|
||||
auto wa = [&](int64_t p, int64_t e) { return rd(1.0f - rd((float)p / (float)e)); };
|
||||
auto T = [&](int64_t i, int64_t j, int64_t ly, int64_t lx, int64_t c) {
|
||||
const int64_t row = off[(size_t)(i * nw + j)] + ly * std::min(tile, W - sx[(size_t)j]) + lx;
|
||||
return ldt(x, rowoff(x, row, n) + c * laststride(x));
|
||||
};
|
||||
const int64_t ys = laststride(y);
|
||||
REF_PAR_FOR(M * n)
|
||||
for (int64_t r = 0; r < M; ++r) {
|
||||
const int64_t Y = r / W, X = r % W;
|
||||
const int64_t i = own_y[(size_t)Y], j = own_x[(size_t)X];
|
||||
const int64_t ly = Y - sy[(size_t)i], lx = X - sx[(size_t)j];
|
||||
const int64_t ev = i > 0 ? oy[(size_t)(i - 1)] : 0, eh = j > 0 ? ox[(size_t)(j - 1)] : 0;
|
||||
const int64_t yb = rowoff(y, r, n);
|
||||
for (int64_t c = 0; c < n; ++c) {
|
||||
float v = T(i, j, ly, lx, c);
|
||||
if (ly < ev)
|
||||
v = rd(rd(T(i - 1, j, ly + tile - ev, lx, c) * wa(ly, ev)) +
|
||||
rd(v * wb(ly, ev)));
|
||||
if (lx < eh)
|
||||
v = rd(rd(T(i, j - 1, ly, lx + tile - eh, c) * wa(lx, eh)) +
|
||||
rd(v * wb(lx, eh)));
|
||||
stt(y, yb + c * ys, v);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
Reference in new issue
Block a user