Archived
radiance 2.1.0
New models. Recursive Flint (39.8B, about 2.5B active) by everplays (#422): `arch/flint`, as an IQ8R container with rotated int8 experts or a four-bit one, with a sink-aware WMMA prefill attention and the fused attention prologue at `rotary_dim` 0. Meta Muse Glimmer 30B by launch80_assets (#423): bf16, an int4 trunk or IQ8R, its windowed vision tower, its DFlash drafter, the ATEM tool dialect and context extension. Ling-3.0-tiny by RikshaDriver (#441): served from its GGUF releases or as IQ8R on `bailing_hybrid`, with six more libr3d kernels. Block FP8, MXFP4 and NVFP4 conversions of the three are not served. IQ8R containers of all three (round to nearest, no calibration) are on Hugging Face under StillDeadcode: flint-iq8r, muse-glimmer-30b-iq8r-dflash and ling-3.0-tiny-iq8r. They were checked for sane answers on two R9700; nothing was benchmarked, and Muse Glimmer's pictures, video and tool calls were not tried. Speech. Breeze-TTS-2 clones a voice from a reference recording and its transcript, with an optional delivery direction, by farcryjohn (#436). The published Breeze container needs converting again with the updated recipe before it can. Tools. A model entry with `diagnose: DIR` runs a fixed workload on the engine instead of serving and writes a report: step times, tokens a step, draft acceptance, TTFT and prefill rates by category, concurrency and prompt length, and with `diagnose-baseline` a token-for-token comparison against an earlier report (docs/DIAGNOSE.md), by Code000 (#418). Scheduler. `mixed-step-target-ms` holds a mixed prefill and decode step to a service time, off by default, by tnhnyzc (#413). `scheduler.stuck-exit` (60s by default, 0 never) ends the process when the device has advanced nothing for that long, so a restart policy brings a hung server back (#409, #415, #428). Fixed. Draft trees with the int8 KV cache, which stopped Gemma 4 31B with DFlash2 on its first request (#454, diagnosed by aelfwyne). The expert loan held at zero by a few leftover cache blocks after long sessions (#455, diagnosed by thekk1). The mapped tier's pages kept in the page cache across a load and a restore, by C_Tamas (#421). Replayed tool calls with blank arguments, and arguments that are not JSON refused naming the call, by dschmitz (#444). The dashboard's card temperature reads the junction sensor, by dschmitz (#453). The VRAM shortfall report says what the KL mode needs, by baKon (#448). The out-of-tree plugin test builds its plugins in parallel and takes its toolchain from the tree. Plugin ABI 40: out-of-tree plugins need to be rebuilt. Thanks to @everplays, @launch80_assets, @RikshaDriver, @farcryjohn, @Code000, @tnhnyzc, @C_Tamas, @dschmitz, @baKon, @aelfwyne and @thekk1.
This commit is contained in:
1 parent
ddcb9c69bb
commit
2a2b756371
166 files changed
+20749
-327
No files matched your search
@@ -104,6 +104,8 @@ set(R4D_UNITS
|
||||
# Vision tower ops: layer norm, the learned-grid position embedding, and the biased GEMM with
|
||||
# activation and residual fused into its store.
|
||||
"r4d_vit_bf16|"
|
||||
# Muse Glimmer's vision-tower coordinate ops: grid_embed_pad, window_order.
|
||||
"r4d_vit_muse|"
|
||||
# The same biased GEMM over f32 operands (a diffusion transformer's patch projections and heads,
|
||||
# a video decoder's 1x1 projections), on the f32 FMA tile r4d_audio's f32 convolutions share.
|
||||
"r4d_gemm_f32_nt_bias|"
|
||||
|
||||
@@ -358,6 +358,12 @@ extern "C" int r4d_attn_dense_bf16(const void* q, const void* k, const void* v,
|
||||
return r4d_attn_dense_launch<80, 1, 8, 1, 64>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen,
|
||||
n_head_kv, scale, window, wright, block,
|
||||
cdiv, causal, q_ld, k_ld, v_ld, o_ld, s);
|
||||
/* h96 gqa1: Muse Glimmer's vision tower, 16 heads of 96, bidirectional over segments. The
|
||||
* h80 shape (8 query blocks of 16, K/V tiles of 64) with six 16-wide k-tiles a head. */
|
||||
if (head_dim == 96 && gqa == 1)
|
||||
return r4d_attn_dense_launch<96, 1, 8, 1, 64>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen,
|
||||
n_head_kv, scale, window, wright, block,
|
||||
cdiv, causal, q_ld, k_ld, v_ld, o_ld, s);
|
||||
#define R4D_DENSE(HD, G, NQB, DS, T) \
|
||||
return r4d_attn_dense_launch<HD, G, NQB, DS, T>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen, \
|
||||
n_head_kv, scale, window, wright, block, \
|
||||
|
||||
@@ -30,8 +30,8 @@
|
||||
* k_descale / v_descale per (sequence, kv head), absent = 1
|
||||
* attn_paged_sink: `sinks` [query heads] f32, one logit a head in the scaled-score domain, in
|
||||
* the softmax's maximum and denominator and with no value (LiLiCorr's drafter). Folded in where
|
||||
* a row is normalised: the unsplit kernel's store and the merge. The WMMA kernel takes none,
|
||||
* so a sink launch stays scalar.
|
||||
* a row is normalised: the unsplit kernel's store, the merge, and the WMMA prefill kernel's
|
||||
* running max and denominator. A decode-sized sink launch stays scalar.
|
||||
*
|
||||
* Paged layout: kv_cache[n_blocks, kv_heads, 16, 2*head_dim], K then V per slot, so one block of
|
||||
* one KV head is a contiguous run of 16 * 2 * head_dim elements; a key tile is one or two runs,
|
||||
@@ -732,7 +732,12 @@ __global__ __launch_bounds__(kWThreads) void attn_wmma_kernel(AttnArgs A) {
|
||||
for (int mc = 0; mc < kDQ / 16; ++mc)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) oacc[mc][e] = 0.0f;
|
||||
float m = -__builtin_inff(), l = 0.0f;
|
||||
/* The learned sink is a key with no value: it joins the running max and the denominator
|
||||
* (log2 domain). Keys are split across lane and lane^16, so only lane h=0 seeds the sink's
|
||||
* weight of one; the final lt2 adds the two lanes. */
|
||||
const float sink = A.sinks ? A.sinks[kh * GQA + g] * 1.4426950408889634f
|
||||
: -__builtin_inff();
|
||||
float m = sink, l = (A.sinks && h == 0) ? 1.0f : 0.0f;
|
||||
|
||||
/* ---- the key tiles, one paged block each, prefetched one ahead through registers */
|
||||
uint4 pk[KPT], pv[VPT][2];
|
||||
@@ -1249,7 +1254,7 @@ static int launch_attn(const RadArgs* a, RadStream stream, bool sink = false) {
|
||||
rows >= n_seq * (kWmmaMinQ / 2);
|
||||
const bool aligned = rad_ptr_aligned(q->data, 16) && rad_ptr_aligned(o->data, 16) &&
|
||||
(q_rs | q_hs | o_rs | o_hs) % 8 == 0;
|
||||
if (shaped && aligned && !sk) {
|
||||
if (shaped && aligned) {
|
||||
constexpr int BQW = WGeo<HD>::R / GQA;
|
||||
int64_t ntw = 0, tpsw = 0;
|
||||
if (!cu) {
|
||||
@@ -1282,6 +1287,7 @@ static int launch_attn(const RadArgs* a, RadStream stream, bool sink = false) {
|
||||
W.window = (int)window;
|
||||
W.shared_window = SHARED;
|
||||
W.ahead = FWD ? 1 : 0;
|
||||
W.sinks = sk ? (const float*)sk->data : nullptr;
|
||||
W.n_split = 1;
|
||||
R4D_KLAUNCH(R4D_KNAME(attn_wmma_kernel<HD, GQA, KV8>), dim3((unsigned)ntw, (unsigned)kvh, 1u),
|
||||
dim3(kWThreads), (hipStream_t)stream, W);
|
||||
@@ -1304,7 +1310,6 @@ static int launch_attn(const RadArgs* a, RadStream stream, bool sink = false) {
|
||||
!rad_ptr_aligned(a->scratch, 16)))
|
||||
ns = 1; /* never wrong, only slower: the unsplit kernel writes the rows itself */
|
||||
}
|
||||
|
||||
AttnArgs A{};
|
||||
A.q = (const uint16_t*)q->data; A.q_rs = q_rs; A.q_hs = q_hs;
|
||||
A.kv = (const unsigned char*)kv->data; A.kv_sb = kv->stride[0]; A.kv_sh = kv->stride[1];
|
||||
@@ -1365,7 +1370,9 @@ extern "C" {
|
||||
int r4d_rad_attn_window_h##HD##_gqa##G##_i8(const RadArgs* a, RadStream s) { \
|
||||
return r4d_win::launch_attn<HD, G, 2>(a, s); \
|
||||
}
|
||||
R4D_WIN_DEF(128, 1)
|
||||
/* h128 gqa16: Muse Glimmer's sliding layers, 32 query heads over 2 KV heads; the same
|
||||
* launch_attn the sink variant below already runs at this geometry. */
|
||||
R4D_WIN_DEF(128, 1) R4D_WIN_DEF(128, 16)
|
||||
R4D_WIN_DEF(256, 1) R4D_WIN_DEF(256, 2) R4D_WIN_DEF(256, 4) R4D_WIN_DEF(256, 8)
|
||||
R4D_WIN_DEF(512, 1) R4D_WIN_DEF(512, 2) R4D_WIN_DEF(512, 4) R4D_WIN_DEF(512, 8) R4D_WIN_DEF(512, 16)
|
||||
#undef R4D_WIN_DEF
|
||||
|
||||
+247
-2
@@ -21,8 +21,139 @@
|
||||
|
||||
namespace {
|
||||
|
||||
__global__ void elu_f32_kernel(const float* x, float* y, int rows, int n, int xp, int yp) {
|
||||
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i >= rows * n) return;
|
||||
const float v = x[(size_t)(i / n) * xp + i % n];
|
||||
y[(size_t)(i / n) * yp + i % n] = v >= 0 ? v : expm1f(v);
|
||||
}
|
||||
|
||||
/* One workgroup a frame, one thread a candidate at a time. Candidate ties reduce to the
|
||||
* lower index. The projected residual is only updated after every candidate has read it. */
|
||||
__global__ void vq_euclidean_kernel(float* x, const float* cb, int* codes, int n, int bins,
|
||||
int xp, int cp, int cs) {
|
||||
const int r = blockIdx.x, t = threadIdx.x;
|
||||
__shared__ float dist[256];
|
||||
__shared__ int pick[256];
|
||||
float best = INFINITY;
|
||||
int id = 0;
|
||||
for (int k = t; k < bins; k += 256) {
|
||||
float d = 0;
|
||||
for (int j = 0; j < n; ++j) {
|
||||
const float delta = x[(size_t)r * xp + j] - cb[(size_t)k * cp + j];
|
||||
d += delta * delta;
|
||||
}
|
||||
if (d < best) { best = d; id = k; }
|
||||
}
|
||||
dist[t] = best; pick[t] = id;
|
||||
__syncthreads();
|
||||
for (int s = 128; s; s /= 2) {
|
||||
if (t < s && (dist[t + s] < dist[t] ||
|
||||
(dist[t + s] == dist[t] && pick[t + s] < pick[t]))) {
|
||||
dist[t] = dist[t + s]; pick[t] = pick[t + s];
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
if (!t) codes[(size_t)r * cs] = pick[0];
|
||||
for (int j = t; j < n; j += 256)
|
||||
x[(size_t)r * xp + j] -= cb[(size_t)pick[0] * cp + j];
|
||||
}
|
||||
|
||||
constexpr int kT = 256;
|
||||
|
||||
/* Small f32 encoder primitives. The generator remains bf16; these keep the released
|
||||
* reference tokenizer in f32 instead of routing its transformer through a host fallback. */
|
||||
__device__ float encoder_sum(float v, float* buf) {
|
||||
const int t = threadIdx.x;
|
||||
buf[t] = v;
|
||||
__syncthreads();
|
||||
for (int s = 128; s; s /= 2) {
|
||||
if (t < s) buf[t] += buf[t + s];
|
||||
__syncthreads();
|
||||
}
|
||||
return buf[0];
|
||||
}
|
||||
|
||||
__global__ void encoder_layernorm(const float* x, const float* w, const float* bias,
|
||||
float* y, int n, int xp, int yp, float eps) {
|
||||
__shared__ float buf[256];
|
||||
const int r = blockIdx.x, t = threadIdx.x;
|
||||
float v = 0;
|
||||
for (int j = t; j < n; j += 256) v += x[(size_t)r * xp + j];
|
||||
const float mean = encoder_sum(v, buf) / n;
|
||||
__syncthreads();
|
||||
v = 0;
|
||||
for (int j = t; j < n; j += 256) {
|
||||
const float d = x[(size_t)r * xp + j] - mean;
|
||||
v += d * d;
|
||||
}
|
||||
const float inv = rsqrtf(encoder_sum(v, buf) / n + eps);
|
||||
__syncthreads();
|
||||
for (int j = t; j < n; j += 256)
|
||||
y[(size_t)r * yp + j] = (x[(size_t)r * xp + j] - mean) * inv * w[j] + (bias ? bias[j] : 0);
|
||||
}
|
||||
|
||||
__global__ void encoder_gelu(const float* x, float* y, int rows, int n, int xp, int yp) {
|
||||
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i >= rows * n) return;
|
||||
const float v = x[(size_t)(i / n) * xp + i % n];
|
||||
y[(size_t)(i / n) * yp + i % n] = 0.5f * v * (1.0f + erff(v * 0.7071067811865475f));
|
||||
}
|
||||
|
||||
__global__ void encoder_rope(float* x, const int* positions, int heads, int pitch,
|
||||
float theta, float scale) {
|
||||
const int r = blockIdx.x;
|
||||
for (int k = threadIdx.x; k < heads * 32; k += 256) {
|
||||
const int h = k / 32, j = k % 32;
|
||||
float* row = x + (size_t)r * pitch + h * 64;
|
||||
const float angle = positions[r] / scale / powf(theta, (2.0f * j) / 64.0f);
|
||||
float sn, cs;
|
||||
sincosf(angle, &sn, &cs);
|
||||
const float a = row[j], b = row[j + 32];
|
||||
row[j] = a * cs - b * sn; row[j + 32] = a * sn + b * cs;
|
||||
}
|
||||
}
|
||||
|
||||
/* One workgroup a (query, head), <=512 keys. Only the reference encoder uses this f32
|
||||
* attention path; its sequence is at most 250 rows, so no quadratic global score plane. */
|
||||
__global__ void encoder_attn(const float* q, const float* k, const float* v, const int* cu,
|
||||
float* out, int heads, int qp, int kp, int vp, int op,
|
||||
int nseq, int div, int window, float scale) {
|
||||
__shared__ float score[512], reduce[256];
|
||||
const int r = blockIdx.x, h = blockIdx.y, t = threadIdx.x;
|
||||
int seq = 0;
|
||||
while (seq + 1 < nseq && cu[seq + 1] <= r * div) ++seq;
|
||||
const int first = max(cu[seq] / div, window > 0 ? r - window + 1 : 0);
|
||||
const int last = min(cu[seq + 1] / div, r + 1), count = last - first;
|
||||
if (count <= 0 || count > 512) return;
|
||||
float best = -INFINITY;
|
||||
for (int j = t; j < count; j += 256) {
|
||||
float dot = 0;
|
||||
for (int d = 0; d < 64; ++d)
|
||||
dot += q[(size_t)r * qp + h * 64 + d] * k[(size_t)(first + j) * kp + h * 64 + d];
|
||||
score[j] = dot * scale;
|
||||
best = fmaxf(best, score[j]);
|
||||
}
|
||||
reduce[t] = best;
|
||||
__syncthreads();
|
||||
for (int s = 128; s; s /= 2) {
|
||||
if (t < s) reduce[t] = fmaxf(reduce[t], reduce[t + s]);
|
||||
__syncthreads();
|
||||
}
|
||||
best = reduce[0];
|
||||
__syncthreads();
|
||||
float sum = 0;
|
||||
for (int j = t; j < count; j += 256) { score[j] = expf(score[j] - best); sum += score[j]; }
|
||||
const float denom = encoder_sum(sum, reduce);
|
||||
__syncthreads();
|
||||
if (t < 64) {
|
||||
float result = 0;
|
||||
for (int j = 0; j < count; ++j)
|
||||
result += score[j] / denom * v[(size_t)(first + j) * vp + h * 64 + t];
|
||||
out[(size_t)r * op + h * 64 + t] = result;
|
||||
}
|
||||
}
|
||||
|
||||
// A window entry's flags (RadAudioGenBatch, RAD_AGEN_F_*), as libref restates them.
|
||||
enum { kFirst = 1, kLast = 8, kJoin = 16 };
|
||||
|
||||
@@ -1334,6 +1465,119 @@ extern "C" int r4d_rad_conv_t1d(const RadArgs* a, RadStream s) {
|
||||
/* The f32 rows. The weight is the _f32 layout hooks' (tap-major for conv1d, phase-major with
|
||||
* ceil(k / stride) taps for conv_t1d). The column tile is the narrowest of 128, 64 and 32 that
|
||||
* covers cout in as few workgroups; fewer than 16 channels take the direct conv1d kernel. */
|
||||
extern "C" int r4d_rad_encoder_layernorm(const RadArgs* a, RadStream s) {
|
||||
const auto *x = r4d_opt(a, 0), *w = r4d_opt(a, 1), *b = r4d_opt(a, 2), *y = r4d_opt(a, 3);
|
||||
if (!x || !w || !y || !is_f32(x) || !is_f32(w) || !is_f32(y) || (b && !is_f32(b)))
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
const long long n = r4d_geti_or(a, "n", 0);
|
||||
if (n < 1 || n > 4096 || r4d_numel(w) != n || (b && r4d_numel(b) != n)) return R4D_NO(RAD_E_SHAPE);
|
||||
long long rx = 0, ry = 0;
|
||||
const auto xp = r4d_rows_pitch(x, n, &rx), yp = r4d_rows_pitch(y, n, &ry);
|
||||
if (xp < 0 || yp < 0 || !r4d_contig(w) || (b && !r4d_contig(b))) return R4D_NO(RAD_E_STRIDE);
|
||||
const float eps = r4d_getf(a, "eps", 1e-5);
|
||||
if (ry < 1 || ry > 65535 || rx < ry || xp > 0x7fffffffll || yp > 0x7fffffffll || !(eps > 0))
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL(encoder_layernorm, dim3((unsigned)ry), dim3(kT), 0, (hipStream_t)s,
|
||||
(const float*)x->data, (const float*)w->data, b ? (const float*)b->data : nullptr,
|
||||
(float*)y->data, (int)n, (int)xp, (int)yp, eps);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_encoder_gelu(const RadArgs* a, RadStream s) {
|
||||
const auto *x = r4d_opt(a, 0), *y = r4d_opt(a, 1);
|
||||
if (!x || !y || !is_f32(x) || !is_f32(y)) return R4D_NO(RAD_E_DTYPE);
|
||||
const long long n = r4d_geti_or(a, "n", 0);
|
||||
if (n < 1 || n > 0x7fffffffll) return R4D_NO(RAD_E_SHAPE);
|
||||
long long rx = 0, ry = 0;
|
||||
const auto xp = r4d_rows_pitch(x, n, &rx), yp = r4d_rows_pitch(y, n, &ry);
|
||||
if (xp < 0 || yp < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (ry < 1 || rx < ry || ry > 0x7fffffffll / n || xp > 0x7fffffffll || yp > 0x7fffffffll)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL(encoder_gelu, dim3(blocks_for(ry * n)), dim3(kT), 0, (hipStream_t)s,
|
||||
(const float*)x->data, (float*)y->data, (int)ry, (int)n, (int)xp, (int)yp);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_encoder_rope(const RadArgs* a, RadStream s) {
|
||||
const auto *x = r4d_opt(a, 0), *p = r4d_opt(a, 1);
|
||||
if (!x || !p || !is_f32(x) || !is_i32(p)) return R4D_NO(RAD_E_DTYPE);
|
||||
const long long nh = r4d_geti_or(a, "n_head", 0), nk = r4d_geti_or(a, "n_head_kv", 0);
|
||||
const char* mode = r4d_gets(a, "mode");
|
||||
if (r4d_geti_or(a, "head_dim", 0) != 64 || r4d_geti_or(a, "rotary_dim", 64) != 64 ||
|
||||
nh < 1 || nh > 64 || nk != nh || !mode || std::strcmp(mode, "neox") || p->rank != 1 ||
|
||||
r4d_gets(a, "sections") || r4d_getf(a, "yarn_factor", 0) != 0 ||
|
||||
r4d_getf(a, "llama3_factor", 0) != 0) return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
long long rx = 0;
|
||||
const auto xp = r4d_rows_pitch(x, 3 * nh * 64, &rx), rows = r4d_numel(p);
|
||||
if (xp < 0 || !r4d_contig(p)) return R4D_NO(RAD_E_STRIDE);
|
||||
const float theta = r4d_getf(a, "theta", 10000), scale = r4d_getf(a, "scale", 1);
|
||||
if (rows < 1 || rows > 65535 || rx < rows || xp > 0x7fffffffll || !(theta > 0) || !(scale > 0))
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL(encoder_rope, dim3((unsigned)rows), dim3(kT), 0, (hipStream_t)s,
|
||||
(float*)x->data, (const int*)p->data, (int)(nh + nk), (int)xp, theta, scale);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_encoder_attn(const RadArgs* a, RadStream s) {
|
||||
const auto *q = r4d_opt(a, 0), *k = r4d_opt(a, 1), *v = r4d_opt(a, 2),
|
||||
*cu = r4d_opt(a, 3), *y = r4d_opt(a, 4);
|
||||
if (!q || !k || !v || !cu || !y || !is_f32(q) || !is_f32(k) || !is_f32(v) ||
|
||||
!is_f32(y) || !is_i32(cu)) return R4D_NO(RAD_E_DTYPE);
|
||||
if (r4d_geti_or(a, "head_dim", 0) != 64 || r4d_geti_or(a, "gqa", 1) != 1 ||
|
||||
r4d_geti_or(a, "causal", 0) != 1) return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
const long long n = r4d_dim(q, (int)q->rank - 1), div = r4d_geti_or(a, "cu_div", 1);
|
||||
if (n < 64 || n % 64 || n > 4096 || div < 1 || div > 0x7fffffffll) return R4D_NO(RAD_E_SHAPE);
|
||||
long long rq = 0, rk = 0, rv = 0, ry = 0;
|
||||
const auto qp = r4d_rows_pitch(q, n, &rq), kp = r4d_rows_pitch(k, n, &rk),
|
||||
vp = r4d_rows_pitch(v, n, &rv), yp = r4d_rows_pitch(y, n, &ry);
|
||||
if (qp < 0 || kp < 0 || vp < 0 || yp < 0 || !r4d_contig(cu)) return R4D_NO(RAD_E_STRIDE);
|
||||
const auto ns = r4d_numel(cu) - 1, window = r4d_geti_or(a, "window", 0);
|
||||
if (ry < 1 || ry > 512 || rq < ry || rk < ry || rv < ry || ns < 1 || ns > 512 ||
|
||||
qp > 0x7fffffffll || kp > 0x7fffffffll || vp > 0x7fffffffll || yp > 0x7fffffffll ||
|
||||
window < 0 || window > 0x7fffffffll) return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL(encoder_attn, dim3((unsigned)ry, (unsigned)(n / 64)), dim3(kT), 0,
|
||||
(hipStream_t)s, (const float*)q->data, (const float*)k->data, (const float*)v->data,
|
||||
(const int*)cu->data, (float*)y->data, (int)(n / 64), (int)qp, (int)kp, (int)vp, (int)yp,
|
||||
(int)ns, (int)div, (int)window, (float)r4d_getf(a, "scale", 0.125));
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_elu(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *x = r4d_opt(a, 0), *y = r4d_opt(a, 1);
|
||||
if (!x || !y || !is_f32(x) || !is_f32(y)) return R4D_NO(RAD_E_DTYPE);
|
||||
const long long n = r4d_geti_or(a, "n", 0);
|
||||
long long rx = 0, ry = 0;
|
||||
if (n <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long xp = r4d_rows_pitch(x, n, &rx), yp = r4d_rows_pitch(y, n, &ry);
|
||||
if (xp < 0 || yp < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (ry <= 0 || rx < ry || n > 0x7fffffffll || ry > 0x7fffffffll / n ||
|
||||
xp > 0x7fffffffll || yp > 0x7fffffffll) return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL(elu_f32_kernel, dim3(blocks_for(ry * n)), dim3(kT), 0,
|
||||
(hipStream_t)s, (const float*)x->data, (float*)y->data,
|
||||
(int)ry, (int)n, (int)xp, (int)yp);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_vq_euclidean(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *x = r4d_opt(a, 0), *cb = r4d_opt(a, 1), *c = r4d_opt(a, 2);
|
||||
if (!x || !cb || !c || !is_f32(x) || !is_f32(cb) || !is_i32(c))
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
const long long n = r4d_geti_or(a, "n", 0), bins = r4d_geti_or(a, "bins", 0);
|
||||
if (n <= 0 || bins <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
long long rx = 0, rc = 0;
|
||||
const long long xp = r4d_rows_pitch(x, n, &rx), cp = r4d_rows_pitch(cb, n, &rc);
|
||||
if (xp < 0 || cp < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (rx < 1 || rc != bins || r4d_numel(c) < rx || rx > 65535 || n > 4096 ||
|
||||
bins > 65536 || xp > 0x7fffffffll || cp > 0x7fffffffll)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
const long long cs = c->rank ? c->stride[c->rank - 1] : 1;
|
||||
if (cs < 1 || cs > 0x7fffffffll) return R4D_NO(RAD_E_STRIDE);
|
||||
hipLaunchKernelGGL(vq_euclidean_kernel, dim3((unsigned)rx), dim3(kT), 0,
|
||||
(hipStream_t)s, (float*)x->data, (const float*)cb->data,
|
||||
(int*)c->data, (int)n, (int)bins, (int)xp, (int)cp, (int)cs);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *x = r4d_opt(a, 0), *w = r4d_opt(a, 1), *cu = r4d_opt(a, 2);
|
||||
const RadTensor *res = r4d_opt(a, 3), *y = r4d_opt(a, 4), *b = r4d_opt(a, 5);
|
||||
@@ -1348,8 +1592,9 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) {
|
||||
const long long cd = r4d_geti_or(a, "cu_div", 1);
|
||||
const int act = conv1d_act(r4d_gets(a, "act"));
|
||||
if (act < 0) return R4D_NO(RAD_E_INVAL);
|
||||
/* The strided forms are bf16 only. */
|
||||
if (r4d_geti_or(a, "stride", 1) != 1 || r4d_geti_or(a, "pad", -1) >= 0)
|
||||
/* Explicit symmetric padding is still the bf16-only form. Causal strides share the
|
||||
* same loaders as stride 1 and retain each clip's own cu_div bounds. */
|
||||
if (r4d_geti_or(a, "pad", -1) >= 0)
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 || causal < 0 ||
|
||||
causal > 1 || st < 1 || (st > 1 && (!causal || dil * (k - 1) + 1 < st)) || cd < 1)
|
||||
|
||||
@@ -68,7 +68,7 @@ __global__ __launch_bounds__(r4d_f32::kThr) void gemm_f32_bias_kernel(
|
||||
for (int j = 0; j < TN; ++j) {
|
||||
const int n = n0 + (j / CW) * 16 * CW + tx * CW + j % CW;
|
||||
const int nc = n < N ? n : N - 1;
|
||||
bc[j] = bias_f32 ? ((const float*)bias)[nc] : bf16_to_f32(((const uint16_t*)bias)[nc]);
|
||||
bc[j] = !bias ? 0.0f : bias_f32 ? ((const float*)bias)[nc] : bf16_to_f32(((const uint16_t*)bias)[nc]);
|
||||
}
|
||||
auto ep = [&](int r, int c, int cnt, float4 q, int g) {
|
||||
const int m = m0 + r, n = n0 + c;
|
||||
@@ -99,23 +99,23 @@ __global__ __launch_bounds__(r4d_f32::kThr) void gemm_f32_bias_kernel(
|
||||
// ============================================================== the plugin ABI entry point
|
||||
enum { GF_A = 0, GF_W, GF_BIAS, GF_RES, GF_Y };
|
||||
|
||||
extern "C" int r4d_rad_gemm_f32_nt_bias(const RadArgs* a, RadStream stream) {
|
||||
static int gemm_f32_emit(const RadArgs* a, RadStream stream, bool plain) {
|
||||
const RadTensor* A = r4d_opt(a, GF_A);
|
||||
const RadTensor* W = r4d_opt(a, GF_W);
|
||||
const RadTensor* B = r4d_opt(a, GF_BIAS);
|
||||
const RadTensor* R = r4d_opt(a, GF_RES);
|
||||
const RadTensor* Y = r4d_opt(a, GF_Y);
|
||||
if (!A || !W || !B || !Y) return R4D_NO(RAD_E_INVAL);
|
||||
if (!A || !W || !Y || (!B && !plain)) return R4D_NO(RAD_E_INVAL);
|
||||
/* f32 A and W; C f32 or bf16, the residual C's dtype; the bias f32 or bf16. */
|
||||
if (A->dtype != RAD_F32 || W->dtype != RAD_F32) return R4D_NO(RAD_E_DTYPE);
|
||||
if (Y->dtype != RAD_F32 && Y->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
|
||||
if (B->dtype != RAD_F32 && B->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
|
||||
if (B && B->dtype != RAD_F32 && B->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
|
||||
if (R && R->dtype != Y->dtype) return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(W) || !r4d_contig(B)) return R4D_NO(RAD_E_STRIDE);
|
||||
if (!r4d_contig(W) || (B && !r4d_contig(B))) return R4D_NO(RAD_E_STRIDE);
|
||||
|
||||
const long long K = r4d_geti_or(a, "K", r4d_dim(W, (int)W->rank - 1));
|
||||
const long long N = r4d_geti_or(a, "N", r4d_numel(W) / (K > 0 ? K : 1));
|
||||
if (K <= 0 || N <= 0 || r4d_numel(W) < N * K || r4d_numel(B) < N) return R4D_NO(RAD_E_SHAPE);
|
||||
if (K <= 0 || N <= 0 || r4d_numel(W) < N * K || (B && r4d_numel(B) < N)) return R4D_NO(RAD_E_SHAPE);
|
||||
long long M = 0, Ma = 0, Mr = 0;
|
||||
const long long ldc = r4d_rows_pitch(Y, N, &M);
|
||||
const long long lda = r4d_rows_pitch(A, K, &Ma);
|
||||
@@ -135,7 +135,7 @@ extern "C" int r4d_rad_gemm_f32_nt_bias(const RadArgs* a, RadStream stream) {
|
||||
else if (!std::strcmp(act_s, "gelu_tanh")) act = 2;
|
||||
else if (std::strcmp(act_s, "none")) return R4D_NO(RAD_E_SCHEMA);
|
||||
}
|
||||
const int bias_f32 = B->dtype == RAD_F32;
|
||||
const int bias_f32 = B && B->dtype == RAD_F32;
|
||||
const bool f32 = Y->dtype == RAD_F32;
|
||||
/* Whole-run stores where every row starts aligned to a run of four. */
|
||||
const unsigned run_bytes = f32 ? 16u : 8u;
|
||||
@@ -161,3 +161,14 @@ extern "C" int r4d_rad_gemm_f32_nt_bias(const RadArgs* a, RadStream stream) {
|
||||
GF_L(2, uint16_t);
|
||||
#undef GF_L
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_gemm_f32_nt_bias(const RadArgs* a, RadStream stream) {
|
||||
return gemm_f32_emit(a, stream, false);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_gemm_f32_nt(const RadArgs* a, RadStream stream) {
|
||||
if (!a || a->n_t < 3) return R4D_NO(RAD_E_INVAL);
|
||||
const RadTensor t[] = {a->t[0], a->t[1], {}, {}, a->t[2]};
|
||||
RadArgs b = *a; b.t = t; b.n_t = 5;
|
||||
return gemm_f32_emit(&b, stream, true);
|
||||
}
|
||||
@@ -35,6 +35,9 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
/* The f32 reference encoder shares rope's schema but keeps its activations in f32. */
|
||||
extern "C" int r4d_rad_encoder_rope(const RadArgs*, RadStream);
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t kWave = 32;
|
||||
@@ -2391,9 +2394,13 @@ static int rope_emit(const RadArgs* a, RadStream s, bool rows_form) {
|
||||
const RadTensor* qkv = r4d_opt(a, RP_QKV);
|
||||
const RadTensor* pos = r4d_opt(a, RP_POS);
|
||||
if (!qkv || !pos) return R4D_NO(RAD_E_INVAL);
|
||||
if (qkv->dtype == RAD_F32) return r4d_rad_encoder_rope(a, s);
|
||||
R4D_NEED(dt_bf16(qkv));
|
||||
R4D_NEED(dt_i32(pos));
|
||||
if (!r4d_contig(pos)) return R4D_NO(RAD_E_STRIDE);
|
||||
/* A component list is read at its own pitch: a [C, M] view of a [C, P] buffer (a tower's
|
||||
* per-pass rope positions) is row-contiguous with a leading stride wider than M. */
|
||||
if (pos->rank == 2 ? pos->stride[1] != 1 : pos->stride[0] != 1)
|
||||
return R4D_NO(RAD_E_STRIDE);
|
||||
|
||||
const long long head_dim = r4d_geti_or(a, "head_dim", 0);
|
||||
const long long n_head = r4d_geti_or(a, "n_head", 0);
|
||||
@@ -2452,7 +2459,7 @@ static int rope_emit(const RadArgs* a, RadStream s, bool rows_form) {
|
||||
if (mc != 3 && [&] { int t = 0; for (int c = 0; c < n_sec; ++c) t += sec[c];
|
||||
return t != (int)(rot / 2); }())
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
plane = (int)pos->shape[1];
|
||||
plane = (int)(pos->stride[0] >= pos->shape[1] ? pos->stride[0] : pos->shape[1]);
|
||||
}
|
||||
|
||||
const float theta = (float)r4d_getf(a, "theta", 10000.0);
|
||||
|
||||
@@ -161,6 +161,9 @@ int64_t r4d_rad_attn_qrow_h128_gqa8_scratch(const RadArgs*);
|
||||
* row pitch, so they serve both attn_paged and attn_paged_qrow. */
|
||||
int r4d_rad_attn_window_h128_gqa1_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_window_h128_gqa1_i8 (const RadArgs*, RadStream);
|
||||
/* h128 gqa16: Muse Glimmer's 32 query heads over 2 KV heads. */
|
||||
int r4d_rad_attn_window_h128_gqa16_bf16(const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_window_h128_gqa16_i8 (const RadArgs*, RadStream);
|
||||
/* The same kernels under attn_paged_sink (a sink logit a query head). */
|
||||
int r4d_rad_attn_window_sink_h128_gqa8_bf16 (const RadArgs*, RadStream);
|
||||
int r4d_rad_attn_window_sink_h128_gqa8_i8 (const RadArgs*, RadStream);
|
||||
@@ -292,6 +295,14 @@ int r4d_rad_snake (const RadArgs*, RadStream);
|
||||
int r4d_rad_cfg_topk_sample (const RadArgs*, RadStream);
|
||||
int r4d_rad_rvq_embed (const RadArgs*, RadStream);
|
||||
int r4d_rad_vq_encode (const RadArgs*, RadStream);
|
||||
int r4d_rad_vq_euclidean (const RadArgs*, RadStream);
|
||||
int r4d_rad_elu (const RadArgs*, RadStream);
|
||||
int r4d_rad_encoder_layernorm (const RadArgs*, RadStream);
|
||||
int r4d_rad_encoder_gelu (const RadArgs*, RadStream);
|
||||
int r4d_rad_encoder_rope (const RadArgs*, RadStream);
|
||||
int r4d_rad_encoder_attn (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_f32_nt (const RadArgs*, RadStream);
|
||||
int r4d_shape_vq_euclidean (const RadParam*, int, int, RadOpdDesc*);
|
||||
int r4d_rad_frame_mix_store (const RadArgs*, RadStream);
|
||||
int r4d_rad_end_status (const RadArgs*, RadStream);
|
||||
int r4d_rad_ras_pick (const RadArgs*, RadStream);
|
||||
@@ -882,6 +893,9 @@ int r4d_rad_layernorm (const RadArgs*, RadStream);
|
||||
int r4d_rad_grid_embed (const RadArgs*, RadStream);
|
||||
int r4d_rad_grid_embed_aa (const RadArgs*, RadStream);
|
||||
int r4d_rad_grid_embed_cpe (const RadArgs*, RadStream);
|
||||
/* Muse's vision-tower coordinate ops (r4d_vit_muse.hip). */
|
||||
int r4d_rad_grid_embed_pad (const RadArgs*, RadStream);
|
||||
int r4d_rad_window_order (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_nt_bias (const RadArgs*, RadStream);
|
||||
/* gemm_nt_bias at f32 (r4d_gemm_f32_nt_bias.hip). */
|
||||
int r4d_rad_gemm_f32_nt_bias(const RadArgs*, RadStream);
|
||||
@@ -948,6 +962,9 @@ int r4d_shape_scatter_rows_bf16(const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_layernorm (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_grid_embed (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_grid_embed_cpe (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
/* Muse's vision-tower coordinate ops. */
|
||||
int r4d_shape_grid_embed_pad (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_window_order (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_gemm_nt_bias (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_unary (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_fill (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
|
||||
@@ -263,7 +263,9 @@ static int qkv_norm_rope_launch(const RadArgs* a, RadStream stream, bool store)
|
||||
const long long vn = r4d_geti_or(a, "v_norm", 1);
|
||||
if (hd != 128 && hd != 256 && hd != 512) return R4D_NO(RAD_E_SHAPE);
|
||||
if (vk && !vn) return R4D_NO(RAD_E_INVAL); // v from k is v's norm
|
||||
if (nq < 0 || nkv <= 0 || rot <= 0 || rot > hd || (rot & 1)) return R4D_NO(RAD_E_SHAPE);
|
||||
/* rot 0 is the NoPE layer: the norm still runs, and `rotate` with half = 0 only copies the
|
||||
* unrotated tail, which is the whole head. */
|
||||
if (nq < 0 || nkv <= 0 || rot < 0 || rot > hd || (rot & 1)) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r4d_numel(qw) != hd || r4d_numel(kw) != hd) return R4D_NO(RAD_E_SHAPE);
|
||||
const char* mode = r4d_gets(a, "mode");
|
||||
if (mode && std::strcmp(mode, "neox")) return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
|
||||
+108
-4
@@ -2185,6 +2185,15 @@ static const RadOperandSpec oGridEmbed[] = {
|
||||
{ "table", RAD_OPD_WEIGHT, 0 },
|
||||
{ "coord", RAD_OPD_IN, 0 },
|
||||
};
|
||||
static const RadParamSpec pWindowOrder[] = { P_INT("M"), P_INT("window") };
|
||||
static const RadOperandSpec oWindowOrder[] = {
|
||||
{ "crd", RAD_OPD_IN, 0 },
|
||||
{ "cu", RAD_OPD_IN, 1 },
|
||||
{ "idx", RAD_OPD_OUT, 0 },
|
||||
{ "inv", RAD_OPD_OUT, 0 },
|
||||
{ "pos", RAD_OPD_OUT, 0 },
|
||||
{ "cuw", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pGridEmbedCpe[] = {
|
||||
P_INT("M"), P_INT("n"), P_INT("side"), P_STR("dtype"),
|
||||
{ "n_prefix", RAD_P_INT, RAD_OPTIONAL },
|
||||
@@ -2846,7 +2855,11 @@ static const RadOperandSpec oVqEncode[] = {
|
||||
{ "codes", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pFrameMix[] = { P_INT("M"), P_INT("n_embd"), P_INT("L"), P_INT("stride"),
|
||||
P_INTO("rep"), P_STR("dtype") };
|
||||
P_INTO("rep"), P_STR("dtype") };
|
||||
static const RadParamSpec pVqEuclidean[] = { P_INT("M"), P_INT("n"), P_INT("bins"), P_STR("dtype") };
|
||||
static const RadOperandSpec oVqEuclidean[] = {
|
||||
{ "x", RAD_OPD_INOUT, 0 }, { "cb", RAD_OPD_WEIGHT, 0 }, { "codes", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadOperandSpec oFrameMix[] = {
|
||||
{ "hid", RAD_OPD_IN, 0 }, { "logits", RAD_OPD_WEIGHT, 0 }, { "scale", RAD_OPD_WEIGHT, 0 },
|
||||
{ "lane", RAD_OPD_IN, 0 }, { "frame", RAD_OPD_IN, 0 }, { "status", RAD_OPD_IN, 0 },
|
||||
@@ -3294,6 +3307,25 @@ static const RadOpSchema kSchemas[] = {
|
||||
"x += a side x side table resampled bilinearly (corners aligned, taps clamped) to each "
|
||||
"row's grid; coord is [4, M] row, column, height, width. The taps are summed in f32 "
|
||||
"and rounded before the add."),
|
||||
SCHEMA("grid_embed_pad", pGridEmbed, oGridEmbed,
|
||||
"x += the side x side table resampled at each row's grid in the half-pixel-centre form "
|
||||
"(align_corners=False) with grid_sample's padding_mode=\"zeros\": per axis taps at "
|
||||
"floor(src) and floor(src) + 1 of src = (i + 0.5) * side / size - 0.5, each carrying "
|
||||
"the hat weight (1 - |src - tap|)+, and a tap whose UNCLAMPED index falls outside the "
|
||||
"table contributes zero rather than reading an edge. Same operands and tap order as "
|
||||
"`grid_embed`; equal to it whenever the grid fits the table, different only when a "
|
||||
"grid upsamples past it (Muse's case)."),
|
||||
SCHEMA("window_order", pWindowOrder, oWindowOrder,
|
||||
"The window-attention permutation of one encoder pass (HF's get_vision_window_index) "
|
||||
"computed from the coordinates alone: `crd` [4, P] i32 is row, column, segment height, "
|
||||
"segment width per patch and `cu` [S+1] i32 the per-frame segment boundaries. Windows "
|
||||
"of `window` patches partition each segment, window-row-major, ragged at the segment's "
|
||||
"right and bottom edges, patches raster within a window and windows never across "
|
||||
"segments. Outputs i32: `idx` [P], slot -> source patch (the gather index); `inv` [P], "
|
||||
"the inverse; `pos` [2, P], each slot's rope positions (col + 1, row + 1), "
|
||||
"component-major like the planes; `cuw` [P+1], the cumulative window boundaries in the "
|
||||
"permuted order, n_windows + 1 entries used. `M` bands the pass; slots past M are the "
|
||||
"identity. `window` <= 0 is the identity permutation, cuw = cu."),
|
||||
SCHEMA("grid_embed_aa", pGridEmbed, oGridEmbed,
|
||||
"x += the side x side table resampled as torch's F.interpolate(bilinear, "
|
||||
"align_corners=False, antialias=True) resamples it to the row's grid -- SigLIP2's "
|
||||
@@ -4033,6 +4065,9 @@ static const RadOpSchema kSchemas[] = {
|
||||
"(the lower index on a tie), then x -= out_w cb[code] + out_b."),
|
||||
SCHEMA("frame_mix_store", pFrameMix, oFrameMix,
|
||||
"a frame's conditioning row (softmax-weighted planes times scale) into its lane."),
|
||||
SCHEMA("vq_euclidean", pVqEuclidean, oVqEuclidean,
|
||||
"nearest Euclidean centroid, codes written and centroid subtracted from x."),
|
||||
SCHEMA("elu", pGenMn, oGenUnary, "y = x >= 0 ? x : expm1(x), alpha 1."),
|
||||
SCHEMA("end_status", pEndStatus, oEndStatus,
|
||||
"status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]."),
|
||||
SCHEMA("ras_pick", pRasPick, oRasPick,
|
||||
@@ -4617,6 +4652,10 @@ static const RadConstraint cAttnDenseH64[] = {
|
||||
static const RadConstraint cAttnDenseH80[] = {
|
||||
C_EQ("head_dim", 80), C_EQ("gqa", 1), C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* head_dim 96 without GQA: Muse Glimmer's vision tower heads (16 of 96). */
|
||||
static const RadConstraint cAttnDenseH96[] = {
|
||||
C_EQ("head_dim", 96), C_EQ("gqa", 1), C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* head_dim 256 without GQA, sequences past the small kernel's. */
|
||||
static const RadConstraint cAttnDenseH256G1[] = {
|
||||
C_EQ("head_dim", 256), C_EQ("gqa", 1), C_GE("max_seqlen", 33), C_IN("dtype", "bf16"),
|
||||
@@ -5847,6 +5886,9 @@ static const RadTunable tRmsQ8[] = {
|
||||
* power-of-two padding. */
|
||||
static const RadConstraint cBf16Row[] = { C_IN("dtype", "bf16") };
|
||||
static const RadConstraint cF32Row[] = { C_IN("dtype", "f32") };
|
||||
static const RadConstraint cEncoderNorm[] = { C_IN("dtype", "f32"), C_GE("n", 1), C_LE("n", 4096) };
|
||||
static const RadConstraint cEncoderAttn[] = { C_IN("dtype", "f32"), C_EQ("head_dim", 64),
|
||||
C_EQ("gqa", 1), C_EQ("causal", 1), C_GE("M", 1), C_LE("M", 512) };
|
||||
/* bf16 rows over the byte copy, which moves four bytes a thread: an even width. */
|
||||
static const RadConstraint cBf16Row2[] = { C_IN("dtype", "bf16"), C_DIV("n", 2) };
|
||||
/* Opaque byte rows, four at a time: the vectorised copy's alignment, stated so the selector
|
||||
@@ -5891,6 +5933,8 @@ static const RadConstraint cRopeRow[] = { C_GE("head_dim", 1),
|
||||
* the position embedding take any width. */
|
||||
static const RadConstraint cLayernorm[] = { C_GE("n", 1), C_IN("dtype", "bf16") };
|
||||
static const RadConstraint cGridEmbed[] = { C_GE("n", 1), C_GE("side", 1), C_IN("dtype", "bf16") };
|
||||
/* window_order has no dtype of its own: its operands are i32 by schema, `M` the only band. */
|
||||
static const RadConstraint cWindowOrder[] = { C_GE("M", 1) };
|
||||
static const RadConstraint cGemmBias[] = { C_GE("M", 1), C_DIV("K", 8), C_IN("dtype", "bf16") };
|
||||
/* The f32 GEMM reads its rows a float at a time: any K. */
|
||||
static const RadConstraint cGemmBiasF32[] = { C_GE("M", 1), C_IN("dtype", "f32") };
|
||||
@@ -6456,6 +6500,26 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_grid_embed_cpe, nullptr, nullptr, nullptr,
|
||||
r4d_shape_grid_embed_cpe },
|
||||
|
||||
{ "grid_embed_pad_bf16", "grid_embed_pad", "elem",
|
||||
"the position grid resampled half-pixel bilinear with zero-padded off-table taps and added in "
|
||||
"(Muse's)",
|
||||
"any n and side; coord [4, M] dense; one workgroup a row",
|
||||
"bf16 x, bf16 or f32 table, i32 coord, f32 taps",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cGridEmbed), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_grid_embed_pad, nullptr, nullptr, nullptr,
|
||||
r4d_shape_grid_embed_pad },
|
||||
|
||||
{ "window_order_i32", "window_order", "elem",
|
||||
"the window-attention permutation of one pass from its coordinate planes: idx, its inverse, "
|
||||
"the swapped one-offset rope positions and the cumulative window boundaries",
|
||||
"M in [1, P); one workgroup the pass",
|
||||
"i32 coord [4, P] and cu [S+1]; i32 idx, inv, pos [2, P] and cuw [P+1]",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cWindowOrder), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_window_order, nullptr, nullptr, nullptr,
|
||||
r4d_shape_window_order },
|
||||
|
||||
{ "gemm_bf16_nt_bias_tiled", "gemm_nt_bias", "gemm",
|
||||
"biased GEMM C = res + act(A @ W^T + bias) over 64-deep K tiles, the K tail staged as zeros, "
|
||||
"the activation and residual in the store: from 32 blocks of 128x128 the weight read straight "
|
||||
@@ -6476,7 +6540,27 @@ static const RadKernelInfo kKernels[] = {
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cGemmBiasF32), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_gemm_f32_nt_bias, nullptr, nullptr, nullptr,
|
||||
r4d_shape_gemm_nt_bias },
|
||||
r4d_shape_gemm_nt_bias },
|
||||
{ "gemm_f32_nt", "gemm_nt", "gemm",
|
||||
"unbiased f32 GEMM on the shared FMA tile", "pitched A and Y, contiguous W", "f32",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cF32Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_gemm_f32_nt, nullptr, nullptr, nullptr, r4d_shape_gemm_nt },
|
||||
{ "layernorm_f32", "layernorm", "norm",
|
||||
"two-pass f32 layer norm", "n <= 4096, pitched rows", "f32 rows, gain and bias",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cEncoderNorm), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_encoder_layernorm, nullptr, nullptr, nullptr, r4d_shape_layernorm },
|
||||
{ "gelu_f32", "gelu", "elem",
|
||||
"exact erf GELU", "pitched rows", "f32",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cF32Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_encoder_gelu, nullptr, nullptr, nullptr, r4d_shape_unary },
|
||||
{ "attn_dense_h64_f32", "attn_dense", "attn",
|
||||
"f32 causal encoder attention without a global score plane", "M <= 512, head_dim 64, gqa 1", "f32",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cEncoderAttn), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_encoder_attn },
|
||||
|
||||
{ "add_bf16", "add", "elem",
|
||||
"elementwise sum, with the one row-broadcast form; `div` divides the rounded sum",
|
||||
@@ -7349,7 +7433,7 @@ static const RadKernelInfo kKernels[] = {
|
||||
#define R4D_WIN_PAIR(HD, GQ) \
|
||||
R4D_WIN_ROW(HD, GQ, bf16kv, bf16, cWinH##HD##G##GQ##Bf16, "bf16") \
|
||||
R4D_WIN_ROW(HD, GQ, i8kv, i8, cWinH##HD##G##GQ##I8, "int8 (an f16 scale of K and of V per token and head)")
|
||||
R4D_WIN_PAIR(128, 1)
|
||||
R4D_WIN_PAIR(128, 1) R4D_WIN_PAIR(128, 16) /* Muse Glimmer's 32/2-head sliding layers */
|
||||
R4D_WIN_PAIR(256, 1) R4D_WIN_PAIR(256, 2) R4D_WIN_PAIR(256, 4) R4D_WIN_PAIR(256, 8)
|
||||
R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, 8)
|
||||
R4D_WIN_PAIR(512, 16)
|
||||
@@ -7867,6 +7951,16 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
|
||||
ROW(cAttnDenseH80), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_dense },
|
||||
|
||||
{ "attn_dense_h96_bf16", "attn_dense", "attn",
|
||||
"dense varlen attention over cu_seqlens: bidirectional with an optional two-sided window or "
|
||||
"blocks, or causal with an optional window behind",
|
||||
"head_dim 96, multi-head (no GQA), q/k/v/o at their own token pitches (column slices of a "
|
||||
"fused projection allowed)",
|
||||
"bf16 query / key / value / out, f16 operands, fp32 accumulate",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cAttnDenseH96), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_attn_dense },
|
||||
|
||||
{ "attn_dense_h256_bf16", "attn_dense", "attn",
|
||||
"dense varlen attention over cu_seqlens: bidirectional with an optional two-sided window or "
|
||||
"blocks, or causal with an optional window behind",
|
||||
@@ -10296,7 +10390,17 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
|
||||
"bf16 or f32 rows and weights, f32 arithmetic, the row rounded once",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cVqEncode), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_vq_encode },
|
||||
nullptr, nullptr, r4d_rad_vq_encode },
|
||||
{ "vq_euclidean_f32", "vq_euclidean", "elem",
|
||||
"nearest Euclidean centroid", "one workgroup a row", "f32 rows and centroids, i32 codes",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cF32Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_vq_euclidean, nullptr, nullptr, nullptr, r4d_shape_vq_euclidean },
|
||||
{ "elu_f32", "elu", "elem",
|
||||
"ELU, alpha 1", "row pitches honoured", "f32 rows",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cF32Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_elu, nullptr, nullptr, nullptr, r4d_shape_unary },
|
||||
|
||||
{ "frame_mix_store_bf16", "frame_mix_store", "elem",
|
||||
"a frame's conditioning row into its lane", "a workgroup a lane, L <= 16",
|
||||
|
||||
+194
-9
@@ -8498,6 +8498,10 @@ static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K
|
||||
* libref function (libref/ref_audio.cpp, ref_elementwise.cpp, ref_attn.cpp) over the same bf16
|
||||
* inputs. Picks, ids and the history are compared exactly; a sum the kernel orders differently is
|
||||
* held to a relative L2 against the answer rounded to bf16. */
|
||||
namespace vg {
|
||||
void* stored_weight_f32(const RadKernelInfo*, const std::vector<RadParam>&,
|
||||
const std::vector<float>&, int64_t, int64_t, const char*);
|
||||
}
|
||||
namespace au {
|
||||
|
||||
uint64_t g_seed = 0xa0d10ull;
|
||||
@@ -8719,8 +8723,8 @@ void conv1d_cases(const Plugin& pl) {
|
||||
}
|
||||
check(nm + " numerics", down_bf16(dy, want.size()), want, 1e-2);
|
||||
}
|
||||
/* An even kernel is causal only; a stride needs its pad and its one sequence; and the
|
||||
* strided form refuses the f32 rows. */
|
||||
/* An even kernel is causal only; the explicit padded stride needs one sequence and
|
||||
* remains bf16-only (the causal strided form also supports f32). */
|
||||
std::vector<RadParam> ps = { PI("M", 4), PI("cin", 8), PI("cout", 16), PI("k", 4), PI("dil", 1),
|
||||
PS("dtype", "bf16") };
|
||||
const int rc = launch(r, { TA(RAD_BF16, { 4, 8 }), TA(RAD_BF16, { 16, 8, 4 }),
|
||||
@@ -8744,14 +8748,14 @@ void conv1d_cases(const Plugin& pl) {
|
||||
ok("conv1d_bf16 refuses a stride over two sequences", rt == RAD_E_SHAPE,
|
||||
"rc " + std::to_string(rt));
|
||||
if (const RadKernelInfo* f = find(pl, "conv1d_f32")) {
|
||||
/* Odd k so the refusal comes from the stride, not from the even-kernel rule. */
|
||||
/* Odd k so the refusal comes from explicit padding, not the even-kernel rule. */
|
||||
const std::vector<RadParam> f32 = { PI("M", 8), PI("cin", 8), PI("cout", 16), PI("k", 3),
|
||||
PI("dil", 1), PS("dtype", "f32"), PI("stride", 2),
|
||||
PI("pad", 1) };
|
||||
const int rf = launch(f, { TA(RAD_F32, { 8, 8 }), TA(RAD_F32, { 16, 8, 3 }),
|
||||
TI32({ 2 }, { 0, 8 }), TA(RAD_F32, { 4, 16 }),
|
||||
TA(RAD_F32, { 4, 16 }), TA(RAD_F32, { 16 }) }, f32);
|
||||
ok("conv1d_f32 refuses a stride", rf == RAD_E_DTYPE, "rc " + std::to_string(rf));
|
||||
ok("conv1d_f32 refuses explicit symmetric stride padding", rf == RAD_E_DTYPE, "rc " + std::to_string(rf));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9169,6 +9173,179 @@ void rvq_embed_cases(const Plugin& pl) {
|
||||
* codes a dense row and a strided column: each code against the host's cosine argmax in double (a
|
||||
* pick within 1e-5 of the best score is a near tie, not a miss), the row after against
|
||||
* x - out_w cb[code] - out_b for the code the kernel took. */
|
||||
void breeze_reference_cases(const Plugin& pl) {
|
||||
const auto* q = find(pl, "vq_euclidean_f32");
|
||||
const auto* e = find(pl, "elu_f32");
|
||||
const auto* cv = find(pl, "conv1d_f32");
|
||||
if (!q || !e || !cv) { ok("breeze reference rows present", false); return; }
|
||||
constexpr int n = 256, bins = 2048, xp = 259, cp = 261, cs = 3;
|
||||
std::vector<float> x(3 * xp, 0), cb(bins * cp, 0);
|
||||
x[0] = 4; x[xp] = 2; x[2 * xp] = -1;
|
||||
cb[0] = 1; cb[cp] = 3; cb[2 * cp] = -1;
|
||||
for (int k = 3; k < bins; ++k) cb[k * cp] = 100 + k;
|
||||
for (int r = 0; r < 3; ++r) for (int j = n; j < xp; ++j) x[r * xp + j] = 77;
|
||||
void* dx = up_f32(x);
|
||||
void* dc = up_i32(std::vector<int32_t>(3 * cs, -7));
|
||||
RadTensor tx = T(dx, RAD_F32, {3, n}); tx.stride[0] = xp;
|
||||
RadTensor tc = T(up_f32(cb), RAD_F32, {bins, n}); tc.stride[0] = cp;
|
||||
RadTensor codes = T(dc, RAD_I32, {3}); codes.stride[0] = cs;
|
||||
const int rc = launch(q, {tx, tc, codes}, {PI("M", 3), PI("n", n), PI("bins", bins), PS("dtype", "f32")});
|
||||
ok("breeze Euclidean RVQ launch", rc == RAD_OK, "rc " + std::to_string(rc));
|
||||
if (rc == RAD_OK) {
|
||||
const auto got = down_i32(dc, 3 * cs);
|
||||
const auto residual = down_f32(dx, x.size());
|
||||
bool valid = got[0] == 1 && got[cs] == 0 && got[2 * cs] == 2;
|
||||
for (int i = 0; i < 3 * cs; ++i) if (i % cs) valid &= got[i] == -7;
|
||||
for (int r = 0; r < 3; ++r) for (int j = 0; j < xp; ++j) {
|
||||
const float want = j == 0 ? (r == 2 ? 0 : 1) : j < n ? 0 : 77;
|
||||
valid &= residual[r * xp + j] == want;
|
||||
}
|
||||
ok("breeze Euclidean distance, tie order, residual and pitched guards", valid);
|
||||
}
|
||||
std::vector<float> v = {-1, 0, 2, -100};
|
||||
void* dv = up_f32(v);
|
||||
const int re = launch(e, {T(dv, RAD_F32, {4, 1}), T(dv, RAD_F32, {4, 1})},
|
||||
{PI("M", 4), PI("n", 1), PS("dtype", "f32")});
|
||||
ok("breeze ELU in place launch", re == RAD_OK);
|
||||
if (re == RAD_OK) {
|
||||
const auto g = down_f32(dv, 4);
|
||||
ok("breeze ELU in place values", std::abs(g[0] + 0.6321205588f) < 1e-7f &&
|
||||
g[1] == 0 && g[2] == 2 && g[3] == -1);
|
||||
}
|
||||
if (const auto* g = find(pl, "gemm_f32_nt")) {
|
||||
void* out = dalloc(6 * sizeof(float));
|
||||
const int s = launch(g, {T(up_f32({1, 2, 3, 4, -1, 2}), RAD_F32, {3, 2}),
|
||||
T(up_f32({2, 0, 0, 3}), RAD_F32, {2, 2}), T(out, RAD_F32, {3, 2})},
|
||||
{PI("M", 3), PI("N", 2), PI("K", 2), PS("dtype", "f32")});
|
||||
ok("breeze unbiased f32 GEMM", s == RAD_OK && down_f32(out, 6) == std::vector<float>({2, 6, 6, 12, -2, 6}));
|
||||
} else ok("breeze unbiased f32 GEMM present", false);
|
||||
if (const auto* ln = find(pl, "layernorm_f32")) {
|
||||
void* out = dalloc(4 * sizeof(float));
|
||||
const int s = launch(ln, {T(up_f32({-1, 1, 3, 5}), RAD_F32, {2, 2}),
|
||||
T(up_f32({2, 2}), RAD_F32, {2}), T(up_f32({1, 1}), RAD_F32, {2}), T(out, RAD_F32, {2, 2})},
|
||||
{PI("M", 2), PI("n", 2), PF("eps", 1e-5), PS("dtype", "f32")});
|
||||
bool valid = s == RAD_OK;
|
||||
if (valid) {
|
||||
const auto got = down_f32(out, 4);
|
||||
const float inv = 1 / std::sqrt(1.00001f);
|
||||
for (int i = 0; i < 4; ++i) valid &= std::abs(got[i] - (1 + (i % 2 ? 2 : -2) * inv)) < 1e-6f;
|
||||
}
|
||||
ok("breeze f32 layer norm mean, variance, gain and bias", valid);
|
||||
} else ok("breeze f32 layer norm present", false);
|
||||
if (const auto* gl = find(pl, "gelu_f32")) {
|
||||
void* out = dalloc(4 * sizeof(float));
|
||||
const int s = launch(gl, {T(up_f32({-1, 0, 1, 2}), RAD_F32, {2, 2}), T(out, RAD_F32, {2, 2})},
|
||||
{PI("M", 2), PI("n", 2), PS("dtype", "f32")});
|
||||
bool valid = s == RAD_OK;
|
||||
if (valid) {
|
||||
const auto got = down_f32(out, 4);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const float v = i - 1;
|
||||
valid &= std::abs(got[i] - 0.5f * v * (1 + std::erf(v / std::sqrt(2.f)))) < 1e-6f;
|
||||
}
|
||||
}
|
||||
ok("breeze f32 exact GELU", valid);
|
||||
} else ok("breeze f32 GELU present", false);
|
||||
if (const auto* rope = find(pl, "rope_bf16")) {
|
||||
std::vector<float> x(2 * 192, 0);
|
||||
for (int r = 0; r < 2; ++r) {
|
||||
x[r * 192] = 1; x[r * 192 + 32] = 2;
|
||||
x[r * 192 + 64] = 3; x[r * 192 + 96] = 4;
|
||||
for (int d = 128; d < 192; ++d) x[r * 192 + d] = 9;
|
||||
}
|
||||
void* dx = up_f32(x);
|
||||
const int s = launch(rope, {T(dx, RAD_F32, {2, 192}), TI32({2}, {0, 1})},
|
||||
{PI("M", 2), PI("head_dim", 64), PI("n_head", 1), PI("n_head_kv", 1),
|
||||
PF("theta", 10000), PF("scale", 1), PS("mode", "neox")});
|
||||
bool valid = s == RAD_OK;
|
||||
if (valid) {
|
||||
const auto got = down_f32(dx, x.size());
|
||||
for (int i = 0; i < 192; ++i) valid &= got[i] == x[i];
|
||||
valid &= std::abs(got[192] - (std::cos(1.f) - 2 * std::sin(1.f))) < 1e-6f;
|
||||
valid &= std::abs(got[224] - (std::sin(1.f) + 2 * std::cos(1.f))) < 1e-6f;
|
||||
valid &= std::abs(got[256] - (3 * std::cos(1.f) - 4 * std::sin(1.f))) < 1e-6f;
|
||||
for (int i = 320; i < 384; ++i) valid &= got[i] == 9;
|
||||
}
|
||||
ok("breeze f32 rotary turns Q/K and preserves V", valid);
|
||||
}
|
||||
if (const auto* attn = find(pl, "attn_dense_h64_f32")) {
|
||||
constexpr int N = 128;
|
||||
std::vector<float> zeros(4 * N, 0), values(4 * N);
|
||||
for (int r = 0; r < 4; ++r) for (int d = 0; d < N; ++d) values[r * N + d] = r + 1 + 10 * (d / 64);
|
||||
void* out = dalloc(values.size() * sizeof(float));
|
||||
const int s = launch(attn, {T(up_f32(zeros), RAD_F32, {4, N}), T(up_f32(zeros), RAD_F32, {4, N}),
|
||||
T(up_f32(values), RAD_F32, {4, N}), TI32({3}, {0, 4, 8}), T(out, RAD_F32, {4, N})},
|
||||
{PI("M", 4), PI("head_dim", 64), PI("gqa", 1), PI("causal", 1), PI("window", 2),
|
||||
PI("max_seqlen", 4), PI("cu_div", 2), PF("scale", 0.125), PS("dtype", "f32")});
|
||||
bool valid = s == RAD_OK;
|
||||
if (valid) {
|
||||
const auto got = down_f32(out, values.size());
|
||||
const float mean[] = {1, 1.5, 3, 3.5};
|
||||
for (int r = 0; r < 4; ++r) for (int d = 0; d < N; ++d)
|
||||
valid &= std::abs(got[r * N + d] - (mean[r] + 10 * (d / 64))) < 1e-6f;
|
||||
}
|
||||
ok("breeze f32 attention isolates sequences, heads and causal windows", valid);
|
||||
constexpr int R = 7, H = 2;
|
||||
const auto q = randv(R * N, 0.2f), k = randv(R * N, 0.2f), v = randv(R * N, 1.0f);
|
||||
void* dy = dalloc(R * N * sizeof(float));
|
||||
const int rc = launch(attn, {T(up_f32(q), RAD_F32, {R, N}), T(up_f32(k), RAD_F32, {R, N}),
|
||||
T(up_f32(v), RAD_F32, {R, N}), TI32({3}, {0, 3, R}), T(dy, RAD_F32, {R, N})},
|
||||
{PI("M", R), PI("head_dim", 64), PI("gqa", 1), PI("causal", 1), PI("window", 3),
|
||||
PI("max_seqlen", R), PF("scale", 0.125), PS("dtype", "f32")});
|
||||
bool matches = rc == RAD_OK;
|
||||
if (matches) {
|
||||
const auto got = down_f32(dy, R * N);
|
||||
for (int r = 0; r < R; ++r) for (int h = 0; h < H; ++h) {
|
||||
const int first = std::max(r < 3 ? 0 : 3, r - 2);
|
||||
std::vector<double> scores;
|
||||
double sum = 0;
|
||||
for (int j = first; j <= r; ++j) {
|
||||
double dot = 0;
|
||||
for (int d = 0; d < 64; ++d) dot += (double)q[r * N + h * 64 + d] * k[j * N + h * 64 + d];
|
||||
scores.push_back(std::exp(dot * 0.125)); sum += scores.back();
|
||||
}
|
||||
for (int d = 0; d < 64; ++d) {
|
||||
double want = 0;
|
||||
for (int j = first; j <= r; ++j) want += scores[j - first] / sum * v[j * N + h * 64 + d];
|
||||
matches &= std::abs(got[r * N + h * 64 + d] - want) < 5e-6;
|
||||
}
|
||||
}
|
||||
}
|
||||
ok("breeze f32 attention agrees with double softmax/dot oracle", matches);
|
||||
} else ok("breeze f32 attention present", false);
|
||||
/* Exercise both the direct and tiled f32 causal strided conv, with two clips whose bounds
|
||||
* are expressed in samples at a different rate. */
|
||||
for (int channels : {1, 32}) {
|
||||
constexpr int R = 12, O = 6, K = 4;
|
||||
std::vector<float> cx(R * channels, 0), w(channels * channels * K, 0);
|
||||
for (int r = 0; r < R; ++r) for (int c = 0; c < channels; ++c) cx[r * channels + c] = r % 8 + 1;
|
||||
for (int c = 0; c < channels; ++c) for (int k = 0; k < K; ++k) w[(c * channels + c) * K + k] = 1;
|
||||
std::vector<RadParam> ps = {PI("M", R), PI("cin", channels), PI("cout", channels), PI("k", K),
|
||||
PI("dil", 1), PI("causal", 1), PI("stride", 2), PI("cu_div", 4), PS("dtype", "f32")};
|
||||
const std::string nm = fmt("breeze f32 causal stride, %d channels", channels);
|
||||
void* dw = vg::stored_weight_f32(cv, ps, w, channels, channels * K, nm.c_str());
|
||||
if (!dw) continue;
|
||||
void* dy = dalloc(O * channels * sizeof(float));
|
||||
RadTensor nores = TA(RAD_F32, {O, channels}), nobias = TA(RAD_F32, {channels});
|
||||
absent(nores); absent(nobias);
|
||||
const int s = launch(cv, {T(up_f32(cx), RAD_F32, {R, channels}), T(dw, RAD_F32, {channels, channels, K}),
|
||||
TI32({3}, {0, 32, 48}), nores, T(dy, RAD_F32, {O, channels}), nobias}, ps);
|
||||
bool valid = s == RAD_OK;
|
||||
if (valid) {
|
||||
const auto got = down_f32(dy, O * channels);
|
||||
for (int r = 0; r < O; ++r) for (int c = 0; c < channels; ++c) {
|
||||
float want = 0;
|
||||
for (int k = 0; k < K; ++k) {
|
||||
const int ir = 2 * r + k - 2, begin = r < 4 ? 0 : 8, end = r < 4 ? 8 : 12;
|
||||
if (ir >= begin && ir < end) want += cx[ir * channels + c];
|
||||
}
|
||||
valid &= got[r * channels + c] == want;
|
||||
}
|
||||
}
|
||||
ok(nm.c_str(), valid, "rc " + std::to_string(s));
|
||||
}
|
||||
}
|
||||
|
||||
void vq_encode_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "vq_encode");
|
||||
if (!r) { ok("vq_encode", false, "row absent"); return; }
|
||||
@@ -15027,7 +15204,9 @@ static void gelu_mul_quant_i8_numeric(const Plugin& pl, bool grp = false) {
|
||||
* every width r4d_act_threads answers, including 256 at head_dim 512 (two channels per thread).
|
||||
* Gemma 3's class (head_dim 128, 16 q over 8 kv heads at one rank of two) has no value norm
|
||||
* (v_norm 0), so its chain is four ops and v must come back untouched. HYV3 adds 32 q / 4 kv
|
||||
* heads, F32 norm gains widened from BF16 and positions near the 65K serving bound. */
|
||||
* heads, F32 norm gains widened from BF16 and positions near the 65K serving bound. Flint's full
|
||||
* layers are the same heads with no positional encoding (`rotary_dim` 0): the chain is the two
|
||||
* norms alone, and the one dispatch copies each normed head through its rotary stage. */
|
||||
static void qkv_norm_rope_numeric(const Plugin& pl) {
|
||||
const RadKernelInfo* np = find(pl, "rmsnorm_p_bf16");
|
||||
const RadKernelInfo* nrm = find(pl, "rmsnorm_bf16");
|
||||
@@ -15045,7 +15224,8 @@ static void qkv_norm_rope_numeric(const Plugin& pl) {
|
||||
Cls{ 128, 16, 8, 128, "rotary", 10000.0, false, false },
|
||||
Cls{ 128, 32, 4, 128, "rotary", 11158840.0, false, false, true },
|
||||
Cls{ 256, 8, 4, 256, "rotary", 10000.0, false, false },
|
||||
Cls{ 256, 0, 4, 256, "rotary", 10000.0, false, false } }) {
|
||||
Cls{ 256, 0, 4, 256, "rotary", 10000.0, false, false },
|
||||
Cls{ 128, 32, 4, 0, "rotary", 10000.0, false, false, true } }) {
|
||||
/* 8 rows of the head-256 class are 8 x 32 workgroups of 8 wavefronts, a full card: the
|
||||
* launch adds an idle row past the cliff (r4d_cliff_rows), which must not change a byte. */
|
||||
for (const int64_t M : { (int64_t)1, (int64_t)4, (int64_t)8, (int64_t)40 }) {
|
||||
@@ -15099,7 +15279,7 @@ static void qkv_norm_rope_numeric(const Plugin& pl) {
|
||||
if (rc == RAD_OK)
|
||||
rc = launch_row(nrm, { T(dk[0], RAD_BF16, { M * cl.nkv, cl.hd }), T(dkw, wdt, { cl.hd }), T(dk[0], RAD_BF16, { M * cl.nkv, cl.hd }) },
|
||||
{ PI("M", M * cl.nkv), PI("n", cl.hd), PF("eps", eps), PS("dtype", "bf16"), PF("wadd", wadd) });
|
||||
for (int j = cl.nq ? 0 : 1; j < 2 && rc == RAD_OK; ++j)
|
||||
for (int j = cl.nq ? 0 : 1; j < 2 && rc == RAD_OK && cl.rot; ++j)
|
||||
rc = launch_row(rp, { T(j ? dk[0] : dq[0], RAD_BF16, { M, (j ? cl.nkv : cl.nq) * cl.hd }), T(dp, RAD_I32, { M }) },
|
||||
{ PI("M", M), PI("head_dim", cl.hd), PI("n_head", j ? cl.nkv : cl.nq), PI("n_head_kv", 0),
|
||||
PF("theta", cl.theta), PF("scale", 1.0), PS("mode", "neox"), PI("rotary_dim", cl.rot),
|
||||
@@ -15114,6 +15294,7 @@ static void qkv_norm_rope_numeric(const Plugin& pl) {
|
||||
PI("v_from_k", cl.vk ? 1 : 0), PI("v_norm", cl.vn ? 1 : 0), PS("dtype", "bf16") });
|
||||
const std::string what = std::string("qkv_norm_rope hd=") + std::to_string(cl.hd) +
|
||||
(cl.nq ? "" : " n_head=0") + (cl.vn ? "" : " v_norm=0") +
|
||||
(cl.rot ? "" : " rotary_dim=0") +
|
||||
(cl.w32 ? " f32 gains" : "") + " M=" + std::to_string(M);
|
||||
if (rc != RAD_OK) { ok((what + " vs its five").c_str(), false, "rc=" + std::to_string(rc)); continue; }
|
||||
int nd = 0, moved = 0;
|
||||
@@ -15126,8 +15307,9 @@ static void qkv_norm_rope_numeric(const Plugin& pl) {
|
||||
const std::vector<uint16_t>& h0 = t == 0 ? hq : (t == 1 ? hk : hv);
|
||||
for (size_t i = 0; i < n; ++i) { nd += a[i] != b[i]; moved += b[i] != h0[i]; }
|
||||
}
|
||||
ok((what + (cl.nq ? " is byte-identical to rmsnorm_p + rmsnorm x2 + rope x2"
|
||||
: " is byte-identical to rmsnorm + rope over k")).c_str(),
|
||||
ok((what + (!cl.rot ? " is byte-identical to its norms alone"
|
||||
: cl.nq ? " is byte-identical to rmsnorm_p + rmsnorm x2 + rope x2"
|
||||
: " is byte-identical to rmsnorm + rope over k")).c_str(),
|
||||
nd == 0 && moved > 0,
|
||||
"differs " + std::to_string(nd) + ", changed from the input " + std::to_string(moved));
|
||||
|
||||
@@ -32287,6 +32469,9 @@ static void device_phase(const Plugin& pl) {
|
||||
if (sel("ras_pick")) au::ras_pick_cases(pl);
|
||||
if (sel("rvq_embed_bf16")) au::rvq_embed_cases(pl);
|
||||
if (sel("vq_encode")) au::vq_encode_cases(pl);
|
||||
if (sel("breeze_reference") || sel("vq_euclidean_f32") || sel("elu_f32") ||
|
||||
sel("gemm_f32_nt") || sel("layernorm_f32") || sel("gelu_f32") || sel("attn_dense_h64_f32"))
|
||||
au::breeze_reference_cases(pl);
|
||||
if (sel("frame_store")) au::frame_store_cases(pl);
|
||||
if (sel("iota")) au::iota_cases(pl);
|
||||
if (sel("sub_i32")) au::sub_i32_cases(pl);
|
||||
|
||||
@@ -382,6 +382,20 @@ extern "C" int r4d_shape_grid_embed_cpe(const RadParam* p, int n_p, int operand,
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
/* grid_embed_pad's: the same operands and drawing as grid_embed's. */
|
||||
extern "C" int r4d_shape_grid_embed_pad(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), side = pi(p, n_p, "side");
|
||||
const uint32_t D = dt_act(p, n_p, "dtype");
|
||||
const RadOpdDesc l[] = { opd(D, M, n), opd(D, side * side, n), opd_idx(4, M, side) };
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
/* window_order declines: its `crd` rows, segment grids and `cu` boundaries must describe whole
|
||||
* raster grids, and an index draw cannot; the emu and unit tests build them by hand. */
|
||||
extern "C" int r4d_shape_window_order(const RadParam*, int, int, RadOpdDesc*) {
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_gemm_nt_bias(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = pi(p, n_p, "M"), N = pi(p, n_p, "N"), K = pi(p, n_p, "K");
|
||||
const uint32_t D = dt_act(p, n_p, "dtype");
|
||||
@@ -605,6 +619,13 @@ extern "C" int r4d_shape_unary(const RadParam* p, int n_p, int operand,
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_vq_euclidean(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = pi(p, n_p, "M"), n = pi(p, n_p, "n"), bins = pi(p, n_p, "bins");
|
||||
if (M <= 0 || n <= 0 || bins <= 0) return RAD_E_UNSUPPORTED;
|
||||
const RadOpdDesc l[] = {opd(RAD_F32, M, n), opd(RAD_F32, bins, n), opd(RAD_I32, 1, M)};
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_silu_mul(const RadParam* p, int n_p, int operand,
|
||||
RadOpdDesc* out) {
|
||||
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
// r4d_vit_muse.hip -- Muse Glimmer's vision-tower coordinate ops: the zero-padded position-table
|
||||
// resample (grid_embed_pad) and the window-attention permutation (window_order).
|
||||
//
|
||||
// Arithmetic matches libref (ref_elementwise.cpp): grid_embed_pad is grid_embed with the
|
||||
// half-pixel-centre resample (align_corners=False) and grid_sample's padding_mode="zeros" -- a tap
|
||||
// whose UNCLAMPED index falls outside [0, side) contributes nothing rather than reading an edge --
|
||||
// the four taps summed in f32 in libref's order, rounded to bf16, added and rounded again.
|
||||
// window_order derives the whole permutation on the device from the coordinate planes: one
|
||||
// workgroup walks the segments' windows, writes the cumulative boundaries, then scatters the
|
||||
// gather index, its inverse and the swapped one-offset rope positions.
|
||||
#include "r4d.h"
|
||||
#include "r4d_args.h"
|
||||
#include "r4d_emit.h"
|
||||
#include "r4d_common.h"
|
||||
#include "r4d_gdn_wmma.h" // bf2f / f32_to_bf16
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kThr = 256;
|
||||
|
||||
__device__ __forceinline__ int imin2(int a, int b) { return a < b ? a : b; }
|
||||
|
||||
// ldw: a table channel as f32, the weight stored bf16 or f32 (as grid_embed's kernel reads it).
|
||||
template <bool F32>
|
||||
__device__ __forceinline__ float ldw(const void* p, uint32_t i) {
|
||||
return F32 ? ((const float*)p)[i] : bf2f(((const unsigned short*)p)[i]);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- grid_embed_pad
|
||||
// ref_grid_embed_pad: grid_embed's operands, half-pixel-centre taps, off-table taps weighted zero.
|
||||
// `crd` is [4, planes]: row, column, segment height, segment width.
|
||||
template <bool TF32>
|
||||
__global__ __launch_bounds__(kThr) void grid_embed_pad_kernel(
|
||||
unsigned short* __restrict__ x, const void* __restrict__ tab, const int32_t* __restrict__ crd,
|
||||
uint32_t n, int side, uint32_t x_ld, uint32_t plane) {
|
||||
const uint32_t m = blockIdx.x;
|
||||
const int g[4] = { crd[m], crd[plane + m], crd[2 * plane + m], crd[3 * plane + m] };
|
||||
int th[2], tw[2];
|
||||
float wh[2], ww[2];
|
||||
auto axis = [&](int idx, int size, int* t, float* wt) {
|
||||
if (size < 1) size = 1;
|
||||
const float src = (float)side / (float)size * ((float)idx + 0.5f) - 0.5f;
|
||||
const float fl = floorf(src);
|
||||
#pragma unroll
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
const int raw = (int)fl + k;
|
||||
t[k] = raw < 0 ? 0 : (raw > side - 1 ? side - 1 : raw);
|
||||
const float d = fabsf(src - fl - (float)k);
|
||||
const float u = 1.0f - d > 0.0f ? 1.0f - d : 0.0f;
|
||||
wt[k] = raw >= 0 && raw <= side - 1 ? u : 0.0f;
|
||||
}
|
||||
};
|
||||
axis(g[0], g[2], th, wh);
|
||||
axis(g[1], g[3], tw, ww);
|
||||
unsigned short* __restrict__ xr = x + (size_t)m * x_ld;
|
||||
for (uint32_t i = threadIdx.x; i < n; i += kThr) {
|
||||
float acc = 0.0f;
|
||||
#pragma unroll
|
||||
for (int ki = 0; ki < 2; ++ki)
|
||||
#pragma unroll
|
||||
for (int kj = 0; kj < 2; ++kj)
|
||||
acc += ldw<TF32>(tab, (uint32_t)(th[ki] * side + tw[kj]) * n + i) *
|
||||
(wh[ki] * ww[kj]);
|
||||
const float pe = bf2f(f32_to_bf16(acc));
|
||||
xr[i] = f32_to_bf16(bf2f(xr[i]) + pe);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- window_order
|
||||
// ref_window_order: HF's get_vision_window_index as a gather-index computation, entirely from the
|
||||
// coordinate planes. One workgroup for the pass, three phases over global memory (P is a tower
|
||||
// pass's patches, at most a few thousand):
|
||||
// 1. every thread lays down the identity over [0, P);
|
||||
// 2. thread 0 walks the segments' windows in permuted order, writing each window's base slot as
|
||||
// cuw's boundary -- the prefix the caller's windowed attention and the scatter both read;
|
||||
// 3. the threads re-derive each patch's window from the boundaries and scatter idx, inv and pos.
|
||||
// `window` <= 0 is the identity permutation with cuw = cu.
|
||||
__global__ __launch_bounds__(kThr) void window_order_kernel(
|
||||
const int32_t* __restrict__ crd, const int32_t* __restrict__ cu, int32_t* __restrict__ idx,
|
||||
int32_t* __restrict__ inv, int32_t* __restrict__ pos, int32_t* __restrict__ cuw,
|
||||
uint32_t P, uint32_t M, uint32_t plane, uint32_t pos_plane, uint32_t cu_len,
|
||||
uint32_t cuw_len, int window) {
|
||||
// 1. the identity everywhere, so slots the pass does not cover are defined; the covered slots
|
||||
// already get their rope positions (col + 1, row + 1), identity or not.
|
||||
for (uint32_t p = threadIdx.x; p < P; p += kThr) {
|
||||
idx[p] = (int32_t)p;
|
||||
inv[p] = (int32_t)p;
|
||||
if (p < M) {
|
||||
pos[p] = crd[plane + p] + 1;
|
||||
pos[pos_plane + p] = crd[p] + 1;
|
||||
} else {
|
||||
pos[p] = 1;
|
||||
pos[pos_plane + p] = 1;
|
||||
}
|
||||
}
|
||||
if (window <= 0) {
|
||||
if (threadIdx.x == 0) {
|
||||
/* The caller narrows cuw to the pass's window count + 1, so every write below is
|
||||
* bounded by its length rather than by P. */
|
||||
const uint32_t k = cu_len < cuw_len ? cu_len : cuw_len;
|
||||
for (uint32_t i = 0; i < k; ++i) cuw[i] = cu[i];
|
||||
const int32_t last = k ? cuw[k - 1] : 0;
|
||||
for (uint32_t i = k; i < cuw_len; ++i) cuw[i] = last;
|
||||
}
|
||||
return;
|
||||
}
|
||||
__syncthreads();
|
||||
// 2. the windows of each segment in permuted order; each boundary is a window's base slot.
|
||||
if (threadIdx.x == 0) {
|
||||
int64_t slot = 0;
|
||||
uint32_t nwin = 0;
|
||||
for (uint32_t s = 0; s + 1 < cu_len && cu[s] < (int32_t)M; ++s) {
|
||||
const int start = cu[s] < 0 ? 0 : cu[s];
|
||||
int end = cu[s + 1];
|
||||
if (end > (int32_t)M) end = (int32_t)M;
|
||||
const int gh = start < end ? crd[2 * plane + start] : 1;
|
||||
const int gw = start < end ? crd[3 * plane + start] : 1;
|
||||
const int nh = (gh + window - 1) / window, nw = (gw + window - 1) / window;
|
||||
for (int wr = 0; wr < nh; ++wr)
|
||||
for (int wc = 0; wc < nw; ++wc) {
|
||||
const int rows_in = imin2(window, gh - wr * window);
|
||||
const int cols_in = imin2(window, gw - wc * window);
|
||||
if (nwin < cuw_len) cuw[nwin] = (int32_t)slot;
|
||||
++nwin;
|
||||
slot += (int64_t)rows_in * cols_in;
|
||||
}
|
||||
}
|
||||
if (nwin < cuw_len) cuw[nwin] = (int32_t)slot;
|
||||
for (uint32_t i = nwin + 1; i < cuw_len; ++i) cuw[i] = (int32_t)slot;
|
||||
}
|
||||
__syncthreads();
|
||||
// 3. each patch's slot: its segment, window id and rank in the window, off the boundaries.
|
||||
for (uint32_t p = threadIdx.x; p < M; p += kThr) {
|
||||
const int r = crd[p], c = crd[plane + p], gw = crd[3 * plane + p];
|
||||
int wr = r / window, wc = c / window;
|
||||
if (wr < 0) wr = 0;
|
||||
if (wc < 0) wc = 0;
|
||||
const int cols_in = imin2(window, gw - wc * window);
|
||||
// The patch's segment: the last boundary at or before p; the windows before it, replayed.
|
||||
uint32_t s = 0;
|
||||
int64_t wbefore = 0;
|
||||
for (; s + 1 < cu_len && cu[s + 1] <= (int32_t)p; ++s) {
|
||||
const int tstart = cu[s] < 0 ? 0 : cu[s];
|
||||
int tend = cu[s + 1];
|
||||
if (tend > (int32_t)M) tend = (int32_t)M;
|
||||
const int th = tstart < tend ? crd[2 * plane + tstart] : 1;
|
||||
const int tw_ = tstart < tend ? crd[3 * plane + tstart] : 1;
|
||||
wbefore += (int64_t)((th + window - 1) / window) * ((tw_ + window - 1) / window);
|
||||
}
|
||||
const int start = cu[s] < 0 ? 0 : cu[s];
|
||||
const int sgw = start < (int)M ? crd[3 * plane + start] : 1;
|
||||
const int nw = (sgw + window - 1) / window;
|
||||
const int64_t wid = wbefore + (int64_t)wr * nw + wc;
|
||||
const int64_t at = cuw[wid] + (int64_t)(r - wr * window) * cols_in + (c - wc * window);
|
||||
idx[at] = (int32_t)p;
|
||||
inv[p] = (int32_t)at;
|
||||
pos[at] = c + 1;
|
||||
pos[pos_plane + at] = r + 1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
/* ------------------------------------------------------------------ grid_embed_pad */
|
||||
enum { GEP_X = 0, GEP_TAB, GEP_CRD };
|
||||
|
||||
extern "C" int r4d_rad_grid_embed_pad(const RadArgs* a, RadStream stream) {
|
||||
const RadTensor* x = r4d_opt(a, GEP_X);
|
||||
const RadTensor* tab = r4d_opt(a, GEP_TAB);
|
||||
const RadTensor* crd = r4d_opt(a, GEP_CRD);
|
||||
if (!x || !tab || !crd) return R4D_NO(RAD_E_INVAL);
|
||||
if (x->dtype != RAD_BF16 || crd->dtype != RAD_I32) return R4D_NO(RAD_E_DTYPE);
|
||||
if (tab->dtype != RAD_BF16 && tab->dtype != RAD_F32) return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(tab)) return R4D_NO(RAD_E_STRIDE);
|
||||
const long long n = r4d_geti_or(a, "n", r4d_dim(tab, (int)tab->rank - 1));
|
||||
const long long side = r4d_geti_or(a, "side", 0);
|
||||
if (n <= 0 || side <= 0 || r4d_numel(tab) < side * side * n) return R4D_NO(RAD_E_SHAPE);
|
||||
long long M = 0;
|
||||
const long long x_ld = r4d_rows_pitch(x, n, &M);
|
||||
if (x_ld < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (crd->rank != 2 || crd->shape[0] < 4 || crd->shape[1] < M || crd->stride[1] != 1)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
if (M <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
/* crd is a [4, pass] view of the full-pass coord buffer: the plane pitch is its leading
|
||||
* stride when that exceeds the shape. */
|
||||
const uint32_t plane = (uint32_t)(crd->stride[0] >= crd->shape[1] ? crd->stride[0]
|
||||
: crd->shape[1]);
|
||||
if (tab->dtype == RAD_F32)
|
||||
return R4D_EMIT(stream, grid_embed_pad_kernel<true>, dim3((uint32_t)M), dim3(kThr), 0,
|
||||
(unsigned short*)r4d_wp(x), r4d_p(tab), (const int32_t*)r4d_p(crd),
|
||||
(uint32_t)n, (int)side, (uint32_t)x_ld, plane);
|
||||
return R4D_EMIT(stream, grid_embed_pad_kernel<false>, dim3((uint32_t)M), dim3(kThr), 0,
|
||||
(unsigned short*)r4d_wp(x), r4d_p(tab), (const int32_t*)r4d_p(crd),
|
||||
(uint32_t)n, (int)side, (uint32_t)x_ld, plane);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ window_order */
|
||||
enum { WO_CRD = 0, WO_CU, WO_IDX, WO_INV, WO_POS, WO_CUW };
|
||||
|
||||
extern "C" int r4d_rad_window_order(const RadArgs* a, RadStream stream) {
|
||||
const RadTensor* crd = r4d_opt(a, WO_CRD);
|
||||
const RadTensor* cu = r4d_opt(a, WO_CU);
|
||||
const RadTensor* idx = r4d_opt(a, WO_IDX);
|
||||
const RadTensor* inv = r4d_opt(a, WO_INV);
|
||||
const RadTensor* pos = r4d_opt(a, WO_POS);
|
||||
const RadTensor* cuw = r4d_opt(a, WO_CUW);
|
||||
if (!crd || !cu || !idx || !inv || !pos || !cuw) return R4D_NO(RAD_E_INVAL);
|
||||
if (crd->dtype != RAD_I32 || cu->dtype != RAD_I32 || idx->dtype != RAD_I32 ||
|
||||
inv->dtype != RAD_I32 || pos->dtype != RAD_I32 || cuw->dtype != RAD_I32)
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(cu) || !r4d_contig(idx) || !r4d_contig(inv) || !r4d_contig(cuw))
|
||||
return R4D_NO(RAD_E_STRIDE);
|
||||
/* `M` is a ranged parameter: the params carry the band's bound (16384), while this issue's
|
||||
* patches are the coord view's columns. cuw is narrowed to the pass's windows + 1. */
|
||||
const long long M = crd ? crd->shape[crd->rank - 1] : 0;
|
||||
const long long window = r4d_geti_or(a, "window", 0);
|
||||
if (M <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
/* crd and pos are plane views of full-pass buffers ([4, P] and [2, P] declared) read at this
|
||||
* pass's M rows; their plane pitch is the leading stride when it exceeds the shape. */
|
||||
if (crd->rank != 2 || crd->shape[0] < 4 || crd->shape[1] < M || crd->stride[1] != 1)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
/* `pos` is the two rope planes either as [2, M] (or a pitched [2, P] view) or as one
|
||||
* [2 * M] row: both describe the same bytes, so the pitch is the row length in the first form
|
||||
* and M in the second. */
|
||||
long long pplane = 0;
|
||||
if (pos->rank == 1) {
|
||||
if (pos->stride[0] != 1 || r4d_numel(pos) < 2 * M) return R4D_NO(RAD_E_SHAPE);
|
||||
pplane = M;
|
||||
} else if (pos->rank == 2) {
|
||||
if (pos->shape[0] < 2 || pos->shape[1] < M || pos->stride[1] != 1)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
pplane = pos->stride[0] >= pos->shape[1] ? pos->stride[0] : pos->shape[1];
|
||||
} else {
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
}
|
||||
/* P is THIS pass's patches (M), not the coord buffer's declared width: the caller narrows
|
||||
* idx/inv/pos/cuw to the pass, so a slot lies in [0, M). */
|
||||
const long long P = M;
|
||||
const long long cplane = crd->stride[0] >= crd->shape[1] ? crd->stride[0] : crd->shape[1];
|
||||
if (r4d_numel(cu) < 2 || r4d_numel(idx) < M || r4d_numel(inv) < M || r4d_numel(pos) < 2 * M ||
|
||||
r4d_numel(cuw) < 2)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
return R4D_EMIT(stream, window_order_kernel, dim3(1), dim3(kThr), 0,
|
||||
(const int32_t*)r4d_p(crd), (const int32_t*)r4d_p(cu),
|
||||
(int32_t*)r4d_wp(idx), (int32_t*)r4d_wp(inv), (int32_t*)r4d_wp(pos),
|
||||
(int32_t*)r4d_wp(cuw), (uint32_t)P, (uint32_t)M, (uint32_t)cplane,
|
||||
(uint32_t)pplane, (uint32_t)r4d_numel(cu), (uint32_t)r4d_numel(cuw),
|
||||
(int)window);
|
||||
}
|
||||
Reference in new issue
Block a user