Files
Deadcode 3b13fd3266 radiance 2.3.0
New models. Thinking Machines' Inkling 2 Mini by gostar (#584-#586): its text backbone and loop, the
release's NVFP4 and MXFP8 checkpoint read by the converter, an int8 trunk, the vision tower, tensor
parallelism, tool calls (with Warzuponus) and an image processor that matches transformers bit for
bit (millaguie). Inkling's routed experts are served in IQR-V4's four-bit trellis (moe_gemm_iqr),
fitted by GPTQ against Hessians the plugin now records (`--calib-dir`), its token embedding in int8
(read in place by embed_lookup), and incoai's DFlash2 drafter in four bits in place of the release's
DSpark. That container, 14.4 GiB with the vision tower, decodes 190 / 427 / 658 tokens a second at 1
/ 4 / 8 streams at tp 2 on two R9700 with `draft-window: adaptive` (code 240 / 550 / 790,
step-by-step arithmetic 358 / 811 / 1116): 19-43% ahead of the same target with the release's DSpark
drafter and 45-83% ahead of the all-int8 container (26.1 GiB, 124 / 263 / 386), at a mean KL of
0.0066 against the release's weights (int8: 0.0042). DeepSeek-V4.1-Flash on 2.x by baKon (#583),
with its vision tower, its IQ1_S and IQ2_XXS containers and linears and experts served in
llama.cpp's own blocks; parts ported from mikoskinen/r9700-exact-serving by Dad_Vibes' fork.
GLM-5.3-Flash on 2.x with its IQR / IQ8R container, an int8 latent cache and four-card serving by
AnimusNull (#566). Gemma 4 dense on EXL3-V3 by keptn42 (#537), on the EXL3 dense rows and
qwen35_exl3 tensor parallelism by Hero422. NVIDIA's 2-bit Nemotron-3.5-Bolt-74B-A9B on one card by
keptn42 (#477). Wan 2.2 TI2V-5B text-to-video and first-frame video by rkcth (#535). LFM3-1.7B on
RDNA3 iGPUs by DokterSmol (#578). Qwen3.8-27B in bf16 with the DFlash2 drafter, and bf16x12, bf16
weights packed losslessly in 12.25 bits, by eddie168 (#577). Int8 experts for drama-3 by morevgalex
(#598).

Speculative decoding. Draft trees for Qwen3.8-27B's DFlash2 drafts by Kezi (#450, opt-in): one card,
+17% at one stream and +14% at two. --draft-max-lanes and --draft-min-accept, two gates on a block
drafter, and --draft-window-ckpt, its window KV kept in the checkpoint slots, by rkcth (#600-#602).
`draft-window: adaptive` verifies each sequence's drafts up to the first one unlikely to be kept. A
verify's rows share each K and V tile in Inkling's attention by gostar (#608): 93 to 118 tokens a
second at a 70k-token prompt on one card.

Generation. Krea 2 instruction edits with one or two references and registered outfill, LoRA on the
image generators with bias deltas and row-mixer adapters applied per request, and MiniMax-H3's
model-declared sparse attention profiles, by rkcth (#534, #542, #543, #605-#609). MiniMax-H3's audio
reference guide pinned from a frame by 1beb (#595). LTX-2's chunk ceiling raised to 40 by Itoash
(#570).

Fleets and memory. memory.queue-reserve, the hardware queues a card keeps ready, by kevinjd (#589).
memory.pinned-numa-node by Foundry (#580). memory.card-headroom-mib on single-model tensor-parallel
budgets by zihaomu (#624). Parked weights rebuilt on the CPU, memory ownership kept across a failed
handoff and quiescent callbacks serialised, by rkcth (#557-#561). A warm load may unload a parked
model (#592); the first card listed chooses the architecture (#458).

Kernels. libr3d's packed dense IQR GEMM and routed IQR expert rows on gfx1100, and libhalo's GEMV
decode for Qwen3.8-Flash-Next's MoE down, by zihaomu (#562, #620, #622). GGML IQ2_XXS and IQ3_XXS
packed on the card by baKon.

Dashboard. KV cache in tokens (in use, capacity now and with all flexible space), live figures
through long prefill steps, step / decode / prefill / acceptance lines without holes, and expert hit
rates only on tiered models. Seven tile and history defects fixed by Noblistic (#597), vision
encoder and per-card hardware figures by davetha (#574), per-LoRA attribution by dowjames (#552).

Fixed. return_tokens / return_token_ids on the OpenAI endpoints (#571). reasoning_effort "none" and
"minimal" reach a template that takes them as levels, by millaguie (#608). Misaligned loads in the
windowed paged attention over an int8 cache (#540). A retired 27B head refused by name (#541).
rad-convert's GPU trellis jobs kept fed, and --reuse-only, by gostar (#575). jinja's tojson takes
sort_keys by Warzuponus (#582). hfget measures file sizes with stat, by kepler_62f (#623). The
tested C++ compiler is stated in the build guide by lowell80 (#551). The codebase was cleaned up:
shared helpers instead of repeated blocks, and the int8 linear, activation and GEMM code no longer
named after the retired fp8 formats.

Inkling 2 Mini is served in one form: IQR-V4 experts with the DFlash2 drafter
(StillDeadcode/inkling-2-mini-iq4r-v4 on Hugging Face, or
data/recipes/inkling2-mini-iq4r-v4.recipe). A container with the release's DSpark drafter, or with
NVFP4, int8 or 4-bit experts, is refused by name and needs converting again.

Plugin ABI 44: out-of-tree plugins need to be rebuilt.

Thanks to @gostar, @baKon, @Dad_Vibes, @AnimusNull, @keptn42, @Hero422, @rkcth, @DokterSmol,
@eddie168, @morevgalex, @Kezi, @1beb, @Itoash, @kevinjd, @Foundry, @zihaomu, @Noblistic, @davetha,
@dowjames, @Warzuponus, @millaguie, @kepler_62f, @lowell80 and @Code000.
2026-11-26 15:28:37 +01:00

353 lines
18 KiB
C++

/* Nemotron's text/vision rotary, patch merger and materialized BF16 attention.
* Separate ops preserve existing rotary, attention and merger contracts. */
#include "ref_common.h"
#include "ref_ops.h"
#include "rad_yarn.h"
using namespace ref;
int ref_pixtral_rope(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *rc = &a->t[1];
// The issue's row views can be smaller than the resolved M capacity band.
const int64_t M = x->rank == 2 ? x->shape[0] : 0;
const int64_t D = p_int(a, "head_dim", 0), H = p_int(a, "n_head", 0);
const float theta = p_f32(a, "theta", 0);
if (M <= 0 || M > p_int(a, "M", 0) || D <= 0 || D % 4 || H <= 0 || !(theta > 0) ||
!std::isfinite(theta) || x->rank != 2 || x->shape[0] < M || x->shape[1] != 3 * H * D ||
rc->rank != 2 || rc->shape[0] != 2 || rc->shape[1] != M)
return RAD_E_SHAPE;
if (rc->dtype != RAD_I32 || (x->dtype != RAD_BF16 && x->dtype != RAD_F32)) return RAD_E_DTYPE;
for (int64_t r = 0; r < 2; ++r)
for (int64_t p = 0; p < M; ++p)
if (ld_int(rc->dtype, rc->data, r * rc->stride[0] + p * rc->stride[1]) < 0)
return RAD_E_INVAL;
REF_PAR_FOR(M * H * D)
for (int64_t p = 0; p < M; ++p) {
for (int64_t i = 0; i < D / 2; ++i) {
const int64_t axis = i / (D / 4), j = i % (D / 4);
const float pos =
(float)ld_int(rc->dtype, rc->data, axis * rc->stride[0] + p * rc->stride[1]);
const float inv = 1.0f / ref_pow(theta, (float)(4 * j + 2 * axis) / D);
const float angle = pos * inv;
const float c = round_dt(x->dtype, ref_cos(angle)),
s = round_dt(x->dtype, ref_sin(angle));
for (int64_t h = 0; h < 2 * H; ++h) {
const int64_t u = rowoff(x, p, 3 * H * D) + (h * D + i) * laststride(x);
const int64_t v = u + (D / 2) * laststride(x);
const float a0 = ldt(x, u), b0 = ldt(x, v);
/* Transformers narrows cos/sin and each product before adding BF16 tensors. */
stt(x, u, round_dt(x->dtype, a0 * c) - round_dt(x->dtype, b0 * s));
stt(x, v, round_dt(x->dtype, b0 * c) + round_dt(x->dtype, a0 * s));
}
}
}
return RAD_OK;
}
int ref_pixtral_merge(const RadArgs* a, RadStream) {
if (!have(a, 3)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *rc = &a->t[1], *y = &a->t[2];
const int64_t M = x->rank == 2 ? x->shape[0] : 0;
const int64_t N = p_int(a, "n", 0), K = p_int(a, "merge", 0);
if (M <= 0 || M > p_int(a, "M", 0) || N <= 0 || K <= 0 || K > M || M / K < K || M % (K * K) ||
x->rank != 2 || x->shape[0] != M || x->shape[1] != N || rc->rank != 2 ||
rc->shape[0] != 4 || rc->shape[1] != M || y->rank != 2 || y->shape[0] != M / (K * K) ||
y->shape[1] != N * K * K)
return RAD_E_SHAPE;
if (rc->dtype != RAD_I32 || x->dtype != y->dtype ||
(x->dtype != RAD_BF16 && x->dtype != RAD_F32))
return RAD_E_DTYPE;
const auto coord = [&](int axis, int64_t p) {
return ld_int(rc->dtype, rc->data, axis * rc->stride[0] + p * rc->stride[1]);
};
/* Verify full raster grids before any write, including variable image widths. The grid at
* each image's first patch defines its boundary; no image is merged with the next one. */
for (int64_t p = 0; p < M;) {
const int64_t h = coord(2, p), w = coord(3, p);
if (h <= 0 || w <= 0 || h % K || w % K || h > (M - p) / w) return RAD_E_INVAL;
for (int64_t j = 0; j < h * w; ++j)
if (coord(0, p + j) != j / w || coord(1, p + j) != j % w || coord(2, p + j) != h ||
coord(3, p + j) != w)
return RAD_E_INVAL;
p += h * w;
}
for (int64_t p = 0; p < M;) {
const int64_t h = coord(2, p), w = coord(3, p);
for (int64_t r = 0; r < h / K; ++r)
for (int64_t c = 0; c < w / K; ++c) {
const int64_t dst = p / (K * K) + r * (w / K) + c;
for (int64_t n = 0; n < N; ++n)
for (int64_t dy = 0; dy < K; ++dy)
for (int64_t dx = 0; dx < K; ++dx) {
const int64_t src = p + (r * K + dy) * w + c * K + dx;
const int64_t col = (n * K + dy) * K + dx;
stt(y, rowoff(y, dst, N * K * K) + col * laststride(y),
ldt(x, rowoff(x, src, N) + n * laststride(x)));
}
}
p += h * w;
}
return RAD_OK;
}
int ref_ministral_rope(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *pos = &a->t[1];
const int64_t M = x->rank == 2 ? x->shape[0] : 0;
const int64_t D = p_int(a, "head_dim", 0), H = p_int(a, "n_head", 0),
K = p_int(a, "n_head_kv", 0);
const float theta = p_f32(a, "theta", 0);
const RadYarn yarn = rad_yarn_args(a);
if (M <= 0 || M > p_int(a, "M", 0) || D <= 0 || D > 2048 || D % 2 || H <= 0 || H > 1024 ||
K <= 0 || K > H || H % K || x->shape[1] != (H + 2 * K) * D || pos->rank != 1 ||
numel(pos) != M || !(theta > 0) || !std::isfinite(theta) || !rad_yarn_valid(yarn))
return RAD_E_SHAPE;
if (pos->dtype != RAD_I32 || (x->dtype != RAD_BF16 && x->dtype != RAD_F32)) return RAD_E_DTYPE;
for (int64_t p = 0; p < M; ++p)
if (ld_int(pos->dtype, pos->data, offlin(pos, p)) < 0) return RAD_E_INVAL;
float freq[1024];
for (int64_t i = 0; i < D / 2; ++i)
freq[i] = yarn.frequency(1.0f / std::pow(theta, (float)(2 * i) / (float)D), (int)i);
REF_PAR_FOR(M * (H + K) * D)
for (int64_t p = 0; p < M; ++p) {
const float position = (float)(ld_int(pos->dtype, pos->data, offlin(pos, p)));
for (int64_t i = 0; i < D / 2; ++i) {
const float angle = position * freq[i];
const float c = round_dt(x->dtype, std::cos(angle) * yarn.magnitude);
const float s = round_dt(x->dtype, std::sin(angle) * yarn.magnitude);
for (int64_t h = 0; h < H + K; ++h) {
const int64_t u = rowoff(x, p, (H + 2 * K) * D) + (h * D + i) * laststride(x);
const int64_t v = u + (D / 2) * laststride(x);
const float a0 = ldt(x, u), b0 = ldt(x, v);
stt(x, u, round_dt(x->dtype, a0 * c) - round_dt(x->dtype, b0 * s));
stt(x, v, round_dt(x->dtype, b0 * c) + round_dt(x->dtype, a0 * s));
}
}
}
return RAD_OK;
}
/* The eager BF16 attention expression materializes QK, scaled logits and probabilities.
* Keep it separate from attn_dense's existing fused arithmetic. Only whole, noncausal segments
* are served. Stack storage is bounded by the model's 32768-token limit. */
int ref_embed_attn(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *cu = &a->t[3], *out = &a->t[4];
const int64_t M = q->rank == 2 ? q->shape[0] : 0;
const int64_t D = p_int(a, "head_dim", 0), G = p_int(a, "gqa", 1);
if (M <= 0 || M > p_int(a, "M", 0) || D <= 0 || D > 2048 || G <= 0 || q->shape[1] <= 0 ||
q->shape[1] % D || k->rank != 2 || v->rank != 2 || out->rank != 2 || cu->rank != 1 ||
numel(cu) < 2 || k->shape[0] != M || v->shape[0] != M || out->shape[0] != M)
return RAD_E_SHAPE;
const int64_t H = q->shape[1] / D, S = numel(cu) - 1;
if (H % G || k->shape[1] != H / G * D || v->shape[1] != H / G * D || out->shape[1] != H * D)
return RAD_E_SHAPE;
if (p_int(a, "causal", 0) != 0) return RAD_E_UNSUPPORTED;
if ((q->dtype != RAD_BF16 && q->dtype != RAD_F32) || k->dtype != q->dtype ||
v->dtype != q->dtype || out->dtype != q->dtype || cu->dtype != RAD_I32)
return RAD_E_DTYPE;
auto boundary = [&](int64_t s) { return ld_int(cu->dtype, cu->data, offlin(cu, s)); };
if (boundary(0) != 0 || boundary(S) != M) return RAD_E_INVAL;
for (int64_t s = 0; s < S; ++s)
if (boundary(s) >= boundary(s + 1) || boundary(s + 1) - boundary(s) > 32768)
return RAD_E_INVAL;
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)D));
if (!std::isfinite(scale) || !(scale > 0)) return RAD_E_INVAL;
REF_PAR_FOR(M * H * D)
for (int64_t sh = 0; sh < S * H; ++sh) {
const int64_t s = sh / H, h = sh % H, kh = h / G;
const int64_t begin = boundary(s), end = boundary(s + 1);
float scores[32768], accum[2048];
for (int64_t i = begin; i < end; ++i) {
const int64_t qb = rowoff(q, i, H * D) + h * D * laststride(q);
float maximum = -INFINITY;
for (int64_t j = begin; j < end; ++j) {
const int64_t kb = rowoff(k, j, H / G * D) + kh * D * laststride(k);
float dot = 0;
for (int64_t d = 0; d < D; ++d)
dot += ldt(q, qb + d * laststride(q)) * ldt(k, kb + d * laststride(k));
const float score = round_dt(q->dtype, round_dt(q->dtype, dot) * scale);
scores[j - begin] = score;
maximum = std::max(maximum, score);
}
float sum = 0;
for (int64_t j = begin; j < end; ++j) {
scores[j - begin] = std::exp(scores[j - begin] - maximum);
sum += scores[j - begin];
}
std::fill_n(accum, D, 0.0f);
for (int64_t j = begin; j < end; ++j) {
const float probability = round_dt(q->dtype, scores[j - begin] / sum);
const int64_t vb = rowoff(v, j, H / G * D) + kh * D * laststride(v);
for (int64_t d = 0; d < D; ++d)
accum[d] += probability * ldt(v, vb + d * laststride(v));
}
const int64_t ob = rowoff(out, i, H * D) + h * D * laststride(out);
for (int64_t d = 0; d < D; ++d)
stt(out, ob + d * laststride(out), accum[d]);
}
}
return RAD_OK;
}
/* Native masked mean: the sum is stored in BF16 before dividing, then stored in BF16 again.
* Keep this separate from seg_mean's one-rounding sentence-embedding contract. */
int ref_seg_mean_bf16_sum(const RadArgs* a, RadStream) {
if (!have(a, 3))
return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *cu = &a->t[1], *y = &a->t[2];
if (x->dtype != RAD_BF16 || y->dtype != RAD_BF16 || cu->dtype != RAD_I32)
return RAD_E_DTYPE;
if (!x->data || !cu->data || !y->data)
return RAD_E_INVAL;
const int64_t M = x->rank == 2 ? x->shape[0] : 0, N = p_int(a, "n", 0);
const int64_t S = cu->rank == 1 && cu->shape[0] > 1 ? cu->shape[0] - 1 : 0;
const auto matrix = [](const RadTensor& t, int64_t rows, int64_t cols) {
constexpr int64_t limit = INT64_MAX / 2;
if (t.rank != 2 || t.shape[0] != rows || t.shape[1] != cols || t.stride[0] <= 0 ||
t.stride[1] <= 0 || cols - 1 > limit / t.stride[1])
return false;
const int64_t columns = (cols - 1) * t.stride[1];
return t.stride[0] > columns && rows - 1 <= (limit - columns) / t.stride[0];
};
if (M <= 0 || M > INT32_MAX || M > p_int(a, "M", 0) || N <= 0 || S <= 0 || S > M ||
cu->stride[0] <= 0 || cu->stride[0] > (INT64_MAX / 4) / S || !matrix(*x, M, N) ||
!matrix(*y, S, N))
return RAD_E_SHAPE;
const auto at = [&](int64_t s) { return ld_int(cu->dtype, cu->data, s * cu->stride[0]); };
if (at(0) != 0 || at(S) != M)
return RAD_E_INVAL;
for (int64_t s = 0; s < S; ++s)
if (at(s) < 0 || at(s + 1) <= at(s) || at(s + 1) > M)
return RAD_E_INVAL;
for (int64_t s = 0; s < S; ++s)
for (int64_t c = 0; c < N; ++c) {
float sum = 0;
for (int64_t r = at(s); r < at(s + 1); ++r)
sum += ldt(x, r * x->stride[0] + c * x->stride[1]);
stt(y, s * y->stride[0] + c * y->stride[1],
round_dt(RAD_BF16, sum) / (float)(at(s + 1) - at(s)));
}
return RAD_OK;
}
/* ================================================================== DeepSeek V4.1's tower ops */
/* ds41_vit_rope2d: rotate-half on the Q and K halves of a fused qkv [M, 3*H*D]. Both axes ride
* ONE ladder, theta^(-2j/(D/2)); pixtral_rope interleaves a second ladder for the column, which is
* a different function of the same position. Half the rotary width tracks the patch row (the first
* quadrant of the rotary dims) and half the column. The products are f32 and rounded once to the
* stored dtype, as the reference's apply_rotary does (it rounds only on `.to(dtype)`). */
int ref_ds41_vit_rope2d(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *rc = &a->t[1];
const int64_t M = x->rank == 2 ? x->shape[0] : 0;
const int64_t D = p_int(a, "head_dim", 0), H = p_int(a, "n_head", 0);
const float theta = p_f32(a, "theta", 0);
if (M <= 0 || M > p_int(a, "M", 0) || D <= 0 || D % 4 || H <= 0 || !(theta > 0) ||
!std::isfinite(theta) || x->rank != 2 || x->shape[0] < M || x->shape[1] != 3 * H * D ||
rc->rank != 2 || rc->shape[0] != 2 || rc->shape[1] != M)
return RAD_E_SHAPE;
if (rc->dtype != RAD_I32 || (x->dtype != RAD_BF16 && x->dtype != RAD_F32)) return RAD_E_DTYPE;
const int64_t rot = D / 2, quad = D / 4;
for (int64_t r = 0; r < 2; ++r)
for (int64_t p = 0; p < M; ++p)
if (ld_int(rc->dtype, rc->data, r * rc->stride[0] + p * rc->stride[1]) < 0)
return RAD_E_INVAL;
REF_PAR_FOR(M * rot)
for (int64_t p = 0; p < M; ++p)
for (int64_t i = 0; i < rot; ++i) {
const int64_t axis = i / quad, j = i % quad;
const float pos = (float)ld_int(rc->dtype, rc->data,
axis * rc->stride[0] + p * rc->stride[1]);
const float inv = 1.0f / ref_pow(theta, (float)(2 * j) / (float)rot);
const float ang = pos * inv;
const float c = ref_cos(ang), s = ref_sin(ang);
/* q is heads [0, H), k is heads [H, 2H); v is untouched. */
for (int64_t h = 0; h < 2 * H; ++h) {
const int64_t u = rowoff(x, p, 3 * H * D) + (h * D + i) * laststride(x);
const int64_t v = u + rot * laststride(x);
const float x0 = ldt(x, u), x1 = ldt(x, v);
stt(x, u, round_dt(x->dtype, x0 * c - x1 * s));
stt(x, v, round_dt(x->dtype, x1 * c + x0 * s));
}
}
return RAD_OK;
}
/* ds41_pack3x3: the aligner's zero-padded 3x3 unfold. Complete raster grids (row, col, h, w per
* patch); a segment's grid is padded to a multiple of `merge` with zeros, and each cell of the
* padded grid becomes one row of merge*merge patch rows in (dy, dx) order, a cell's width whole.
* Padded cells are zeros -- the reference pads the value plane with zeros before unfold. */
int ref_ds41_pack3x3(const RadArgs* a, RadStream) {
if (!have(a, 3)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *rc = &a->t[1], *y = &a->t[2];
const int64_t M = x->rank == 2 ? x->shape[0] : 0;
const int64_t N = p_int(a, "n", 0), K = p_int(a, "merge", 0);
if (M <= 0 || M > p_int(a, "M", 0) || N <= 0 || K < 2 || x->rank != 2 || x->shape[0] != M ||
x->shape[1] != N || rc->rank != 2 || rc->shape[0] != 4 || rc->shape[1] != M ||
y->rank != 2 || y->shape[1] != N * K * K)
return RAD_E_SHAPE;
if (rc->dtype != RAD_I32 || x->dtype != y->dtype ||
(x->dtype != RAD_BF16 && x->dtype != RAD_F32))
return RAD_E_DTYPE;
const auto coord = [&](int axis, int64_t p) {
return ld_int(rc->dtype, rc->data, axis * rc->stride[0] + p * rc->stride[1]);
};
int64_t r0 = 0;
for (int64_t p = 0; p < M;) {
const int64_t h = coord(2, p), w = coord(3, p);
if (h <= 0 || w <= 0 || h * w > M - p) return RAD_E_INVAL;
const int64_t oh = (h + K - 1) / K, ow = (w + K - 1) / K;
if (r0 + oh * ow > y->shape[0]) return RAD_E_SHAPE;
for (int64_t oy = 0; oy < oh; ++oy)
for (int64_t ox = 0; ox < ow; ++ox) {
const int64_t r = r0 + oy * ow + ox;
/* `conv2d_unfold` column order: (channel, kernel row, kernel column), so the
* block's row is pixel-major over the K x K patches. */
for (int64_t j = 0; j < N; ++j)
for (int64_t dy = 0; dy < K; ++dy)
for (int64_t dx = 0; dx < K; ++dx) {
const int64_t yy = oy * K + dy, xx = ox * K + dx;
const int64_t dst =
rowoff(y, r, N * K * K) + (j * K * K + dy * K + dx) * laststride(y);
if (yy < h && xx < w) {
const int64_t src = rowoff(x, p + yy * w + xx, N);
stt(y, dst, ldt(x, src + j * laststride(x)));
} else {
stt(y, dst, 0.0f);
}
}
}
r0 += oh * ow;
p += h * w;
}
if (r0 != y->shape[0]) return RAD_E_SHAPE;
return RAD_OK;
}
/* ds41_span_rows: the three learned vectors an image span's typed rows take (start at row 0,
* newline at row 2, end at row 3; row 1 is the encoder's own rows and left untouched). A copy of
* each row weight into its slot: no arithmetic, so every byte reaches the table as one bf16 pair
* (the table is read by the core's span staging, not by a kernel). */
int ref_ds41_span_rows(const RadArgs* a, RadStream) {
if (!have(a, 4)) return RAD_E_INVAL;
const RadTensor *s = &a->t[0], *nl = &a->t[1], *e = &a->t[2], *y = &a->t[3];
const int64_t n = p_int(a, "n", 0);
if (n <= 0 || rad_tensor_numel(s) < n || rad_tensor_numel(nl) < n ||
rad_tensor_numel(e) < n || y->rank != 2 || y->shape[0] != 4 || y->shape[1] != n)
return RAD_E_SHAPE;
if (y->dtype != RAD_BF16 || s->dtype != y->dtype || nl->dtype != y->dtype ||
e->dtype != y->dtype)
return RAD_E_DTYPE;
const RadTensor* src[3] = { s, nl, e };
const int64_t row[3] = { 0, 2, 3 };
for (int k = 0; k < 3; ++k) {
const int64_t sb = src[k]->stride[0], yb = rowoff(y, row[k], n);
for (int64_t i = 0; i < n; ++i) stt(y, yb + i * laststride(y), ldt(src[k], i * sb));
}
return RAD_OK;
}