Archived
nemotron 3.5 implementation
This commit is contained in:
1 parent
33108e488b
commit
94c90b17ad
59 files changed
+6046
-76
No files matched your search
@@ -75,6 +75,7 @@ set(R4D_UNITS
|
||||
# Unlimited-OCR's SAM tower: windowed attention with decomposed relative position.
|
||||
"r4d_sam_attn_bf16|"
|
||||
"r4d_encoder_bf16|"
|
||||
"r4d_nemotron_bf16|"
|
||||
# The image generator: attention over a paged prefix plus the pass's own rows, the causal
|
||||
# head_dim-128 dense form and the 1152-wide decoder head; plus the sampler, adaptive norm,
|
||||
# implicit-GEMM convolution and pixel ops.
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/* r4d_bf16_gemm_rows.h -- host-side row chunks for the BF16 WMMA GEMMs.
|
||||
*
|
||||
* The operands' dimensions use the kernels' signed-int ABI. Each launch uses 32-bit element
|
||||
* offsets for A and W, but successive launches rebase A and Y with 64-bit pointers. Thus M*K
|
||||
* need not fit in 32 bits. W is reused whole and still must fit; a wider weight needs a different
|
||||
* addressing implementation. This is a matrix-shape constraint, not a deployment token budget.
|
||||
*/
|
||||
#ifndef R4D_BF16_GEMM_ROWS_H
|
||||
#define R4D_BF16_GEMM_ROWS_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
|
||||
static inline int r4d_bf16_gemm_chunk_rows(int64_t M, int64_t N, int64_t K, int row_tile,
|
||||
int col_tile) {
|
||||
if (M < 1 || M > INT_MAX || N < 1 || N > INT_MAX || K < 16 || K > INT_MAX || K % 16 ||
|
||||
row_tile < 1 || col_tile < 1)
|
||||
return 0;
|
||||
constexpr int64_t offsets = UINT32_MAX;
|
||||
if (N > offsets / K) return 0;
|
||||
const int64_t gx = (N + col_tile - 1) / col_tile;
|
||||
/* Bound each launch's row grid, not the full matrix. The tiled kernel flattens block IDs
|
||||
* in unsigned arithmetic, so the product must fit too. No input is rejected for exceeding
|
||||
* this workgroup budget: it becomes another launch on the same stream. */
|
||||
const int64_t gy = std::min<int64_t>(65535, offsets / gx);
|
||||
int64_t rows = std::min(offsets / K, gy * row_tile);
|
||||
if (rows >= row_tile) rows -= rows % row_tile;
|
||||
return (int)std::min(M, rows);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// C[M,N] = A[M,K] @ W[N,K]^T, bf16 in and out, fp32 accumulate.
|
||||
//
|
||||
// General in M up to R4D_GEMM64_MAX_M: MT row tiles per block, grid.y covers the rest. The 64 is
|
||||
// General in M: MT row tiles per block, bounded grids with rebased row launches. The 64 is
|
||||
// the registry band, not a kernel limit; the M <= 16 row forwards here too.
|
||||
//
|
||||
// A scalar kernel (one wave per output column, M accumulators) costs one weight load and M
|
||||
@@ -18,13 +18,13 @@
|
||||
// results are never stored.
|
||||
#include "r4d.h"
|
||||
#include "r4d_gemm_args.h"
|
||||
#include "r4d_bf16_gemm_rows.h"
|
||||
#include "r4d_gdn_wmma.h"
|
||||
#include "r4d_chain.h"
|
||||
#include <hip/hip_bf16.h>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
#define R4D_GEMM64_MAX_M 65536
|
||||
#define WAVE32 32
|
||||
|
||||
// A fragment from a base pointer plus a compile-time k step. With fragGo's 32-bit element offset,
|
||||
@@ -155,30 +155,55 @@ void r4d_gemm_bf16_nt_m64(long a, long w, long c, int M, int K, int N, int WV, i
|
||||
/* `chain`: the chained form (r4d_rad_gemm_bf16_m64 passes the row's parameter; M <= 64 always). */
|
||||
static void gemm_bf16_m64_launch(long a, long w, long c, int M, int K, int N, int WV, int SK,
|
||||
int MB, long stream, bool chain) {
|
||||
if (M < 1 || M > R4D_GEMM64_MAX_M) throw std::runtime_error("r4d gemm_bf16_nt_m64: M out of range");
|
||||
if (K % (SK * 16)) throw std::runtime_error("r4d gemm_bf16_nt_m64: K must be divisible by SK*16");
|
||||
if (WV * SK * WAVE32 > 1024) throw std::runtime_error("r4d gemm_bf16_nt_m64: threads/block > 1024");
|
||||
const int ntiles = (N + 15) / 16, mtiles = (M + 15) / 16;
|
||||
if (WV < 1 || SK < 1 || (int64_t)WV * SK > 1024 / WAVE32)
|
||||
throw std::runtime_error("r4d gemm_bf16_nt_m64: invalid threads/block");
|
||||
if (K % ((int64_t)SK * 16))
|
||||
throw std::runtime_error("r4d gemm_bf16_nt_m64: K must be divisible by SK*16");
|
||||
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_bf16_nt_m64: MB must be 1..4");
|
||||
dim3 grid((ntiles + WV - 1) / WV, (mtiles + MB - 1) / MB), block(WV * SK * WAVE32);
|
||||
const int chunk = r4d_bf16_gemm_chunk_rows(M, N, K, MB * 16, WV * 16);
|
||||
if (!chunk) throw std::runtime_error("r4d gemm_bf16_nt_m64: invalid dimensions or weight span");
|
||||
// A chained op must publish one completion, not one per rebased launch. Existing chained
|
||||
// decode shapes (M <= 64) fit one launch; do not silently change their synchronization law.
|
||||
if (chain && chunk < M)
|
||||
throw std::runtime_error("r4d gemm_bf16_nt_m64: chained rows need one launch");
|
||||
const int ntiles = (N + 15) / 16;
|
||||
dim3 block(WV * SK * WAVE32);
|
||||
size_t smem = (size_t)WV * SK * 256 * sizeof(float);
|
||||
auto Ap = (const unsigned short*)a; auto Wp = (const unsigned short*)w;
|
||||
auto Cp = (__hip_bfloat16*)c;
|
||||
auto Wp = (const unsigned short*)w;
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
#define LAUNCH(MT) hipLaunchKernelGGL((chain ? r4dchain_gemm_bf16_nt_m64<MT> \
|
||||
: r4d_gemm_bf16_nt_m64_kernel<MT>), \
|
||||
grid, block, smem, st, Ap, Wp, Cp, M, K, N, WV, SK)
|
||||
switch (MB) {
|
||||
case 1: LAUNCH(1); break;
|
||||
case 2: LAUNCH(2); break;
|
||||
case 3: LAUNCH(3); break;
|
||||
case 4: LAUNCH(4); break;
|
||||
default: throw std::runtime_error("r4d gemm_bf16_nt_m64: MB unsupported");
|
||||
#define LAUNCH(MT) \
|
||||
hipLaunchKernelGGL((chain ? r4dchain_gemm_bf16_nt_m64<MT> : r4d_gemm_bf16_nt_m64_kernel<MT>), \
|
||||
grid, block, smem, st, Ap, Wp, Cp, mm, K, N, WV, SK)
|
||||
for (int64_t m0 = 0; m0 < M; m0 += chunk) {
|
||||
const int mm = (int)std::min<int64_t>(chunk, M - m0), mtiles = (mm + 15) / 16;
|
||||
dim3 grid((ntiles + WV - 1) / WV, (mtiles + MB - 1) / MB);
|
||||
auto Ap = (const unsigned short*)a + m0 * K;
|
||||
auto Cp = (__hip_bfloat16*)c + m0 * N;
|
||||
switch (MB) {
|
||||
case 1:
|
||||
LAUNCH(1);
|
||||
break;
|
||||
case 2:
|
||||
LAUNCH(2);
|
||||
break;
|
||||
case 3:
|
||||
LAUNCH(3);
|
||||
break;
|
||||
case 4:
|
||||
LAUNCH(4);
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("r4d gemm_bf16_nt_m64: MB unsupported");
|
||||
}
|
||||
const hipError_t e = hipGetLastError();
|
||||
if (e != hipSuccess) throw std::runtime_error(hipGetErrorString(e));
|
||||
}
|
||||
#undef LAUNCH
|
||||
}
|
||||
|
||||
int r4d_gemm_bf16_nt_m64_max_m(void) { return R4D_GEMM64_MAX_M; }
|
||||
int r4d_gemm_bf16_nt_m64_max_m(void) {
|
||||
return INT_MAX;
|
||||
}
|
||||
|
||||
// ============================================================== the plugin ABI entry points
|
||||
//
|
||||
@@ -187,14 +212,16 @@ extern "C" int r4d_rad_gemm_bf16_m64(const RadArgs* a, RadStream s) {
|
||||
GemmOps g;
|
||||
int rc = gemm_ops(a, &g, 0);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (g.M > r4d_gemm_bf16_nt_m64_max_m()) return R4D_NO(RAD_E_SHAPE);
|
||||
if (!r4d_bf16_gemm_chunk_rows(g.M, g.N, g.K, 16, 16)) return R4D_NO(RAD_E_SHAPE);
|
||||
Cfg c;
|
||||
rc = cfg_for(a, g.K, g.M, 16, BR_FLAT, &c, (g.N + 15) / 16); /* "K divisible by SK*16" */
|
||||
if (rc != RAD_OK) return rc;
|
||||
const bool chain = r4d_geti_or(a, "chain", 0) != 0;
|
||||
if (chain && r4d_bf16_gemm_chunk_rows(g.M, g.N, g.K, c.mb * 16, c.wv * 16) < g.M)
|
||||
return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
R4D_TRY("gemm_bf16_nt_m64",
|
||||
gemm_bf16_m64_launch(r4d_addr(g.a), r4d_addr(g.b), r4d_addr(g.y),
|
||||
(int)g.M, (int)g.K, (int)g.N, c.wv, c.sk, c.mb,
|
||||
(long)(uintptr_t)s, r4d_geti_or(a, "chain", 0) != 0));
|
||||
gemm_bf16_m64_launch(r4d_addr(g.a), r4d_addr(g.b), r4d_addr(g.y), (int)g.M, (int)g.K,
|
||||
(int)g.N, c.wv, c.sk, c.mb, (long)(uintptr_t)s, chain));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
// and their results are never stored.
|
||||
#include "r4d.h"
|
||||
#include "r4d_gemm_args.h"
|
||||
#include "r4d_bf16_gemm_rows.h"
|
||||
#include "r4d_common.h"
|
||||
#include "r4d_gdn_wmma.h"
|
||||
#include <hip/hip_bf16.h>
|
||||
@@ -173,7 +174,7 @@ __global__ __launch_bounds__(256) void r4d_gemm_bf16_nt_tiled_kernel(
|
||||
enum { kBtLargeGrid = 32 };
|
||||
|
||||
int r4d_gemm_bf16_nt_tiled_arm(int M, int N) {
|
||||
const long long g = (long long)((N + 127) / 128) * ((M + 127) / 128);
|
||||
const long long g = ((N + 127ll) / 128) * ((M + 127ll) / 128);
|
||||
return g >= kBtLargeGrid ? 1 : 2;
|
||||
}
|
||||
|
||||
@@ -181,23 +182,40 @@ int r4d_gemm_bf16_nt_tiled_arm(int M, int N) {
|
||||
int r4d_gemm_bf16_nt_tiled(long a, long w, long c, int M, int K, int N, int SK, int tile,
|
||||
long stream) {
|
||||
if (M < 1 || N < 1 || K < BT_BK || K % BT_BK) return -1;
|
||||
if (SK < 1 || K % (SK * 16)) return -1;
|
||||
if (SK < 1 || K % ((int64_t)SK * 16)) return -1;
|
||||
if (tile != 1 && tile != 2) return -1;
|
||||
// 32-bit element offsets: the last row of either operand has to be addressable.
|
||||
if ((long long)M * K >= (1ll << 32) || (long long)N * K >= (1ll << 32)) return -1;
|
||||
const int spk = K / SK / 16;
|
||||
const int bt = tile == 1 ? 128 : 64;
|
||||
dim3 grid((N + bt - 1) / bt, (M + bt - 1) / bt), block(256);
|
||||
auto Ap = (const unsigned short*)a; auto Wp = (const unsigned short*)w;
|
||||
auto Cp = (__hip_bfloat16*)c;
|
||||
const int chunk = r4d_bf16_gemm_chunk_rows(M, N, K, bt, bt);
|
||||
if (!chunk) return -1;
|
||||
dim3 block(256);
|
||||
auto Wp = (const unsigned short*)w;
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
#define LAUNCH(TM, TN, S) hipLaunchKernelGGL((r4d_gemm_bf16_nt_tiled_kernel<TM, TN, S>), grid, \
|
||||
block, 0, st, Ap, Wp, Cp, M, K, N, spk)
|
||||
#define LAUNCH(TM, TN, S) \
|
||||
hipLaunchKernelGGL((r4d_gemm_bf16_nt_tiled_kernel<TM, TN, S>), grid, block, 0, st, Ap, Wp, Cp, \
|
||||
mm, K, N, spk)
|
||||
const bool split = SK > 1;
|
||||
if (tile == 1) { if (split) LAUNCH(4, 2, true); else LAUNCH(4, 2, false); }
|
||||
else { if (split) LAUNCH(2, 1, true); else LAUNCH(2, 1, false); }
|
||||
for (int64_t m0 = 0; m0 < M; m0 += chunk) {
|
||||
const int mm = (int)std::min<int64_t>(chunk, M - m0);
|
||||
dim3 grid((N + bt - 1) / bt, (mm + bt - 1) / bt);
|
||||
auto Ap = (const unsigned short*)a + m0 * K;
|
||||
auto Cp = (__hip_bfloat16*)c + m0 * N;
|
||||
if (tile == 1) {
|
||||
if (split)
|
||||
LAUNCH(4, 2, true);
|
||||
else
|
||||
LAUNCH(4, 2, false);
|
||||
} else {
|
||||
if (split)
|
||||
LAUNCH(2, 1, true);
|
||||
else
|
||||
LAUNCH(2, 1, false);
|
||||
}
|
||||
const hipError_t e = hipGetLastError();
|
||||
if (e != hipSuccess) return (int)e;
|
||||
}
|
||||
#undef LAUNCH
|
||||
return (int)hipGetLastError();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ============================================================== the plugin ABI entry point
|
||||
@@ -205,6 +223,7 @@ extern "C" int r4d_rad_gemm_bf16_tiled(const RadArgs* a, RadStream s) {
|
||||
GemmOps g;
|
||||
int rc = gemm_ops(a, &g, 0);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (!r4d_bf16_gemm_chunk_rows(g.M, g.N, g.K, 64, 64)) return R4D_NO(RAD_E_SHAPE);
|
||||
// The skinny row's split rule with the same arguments keeps the two rows' outputs identical.
|
||||
Cfg c;
|
||||
rc = cfg_for(a, g.K, g.M, 16, BR_FLAT, &c, (g.N + 15) / 16);
|
||||
|
||||
@@ -0,0 +1,398 @@
|
||||
// Model-local BF16 seams. Attention recomputes QK in three bounded passes: maximum, f32
|
||||
// denominator, then BF16 probabilities times V. Online rescaling would round unnormalised P
|
||||
// and change this op's eager contract. No quadratic score buffer, allocation or host barrier.
|
||||
#include "r4d.h"
|
||||
#include "r4d_args.h"
|
||||
#include "r4d_common.h"
|
||||
#include "r4d_dt16.h"
|
||||
#include "rad_yarn.h"
|
||||
#include <climits>
|
||||
#include <algorithm>
|
||||
#include <new>
|
||||
|
||||
namespace {
|
||||
constexpr int NT = 256;
|
||||
struct RotaryFrequency {
|
||||
float f[64] = {};
|
||||
};
|
||||
__device__ float bf2f(uint16_t x) {
|
||||
return __builtin_bit_cast(float, (uint32_t)x << 16);
|
||||
}
|
||||
__device__ float narrow(float x) {
|
||||
return bf2f(f32_to_bf16(x));
|
||||
}
|
||||
|
||||
// Metadata is device resident. Validate on the same queue before any data writes. A malformed
|
||||
// index faults the pass (asynchronously), rather than returning a successful partial embedding.
|
||||
__global__ void validate(const int* x, int64_t stride, int64_t plane, int M, int mode, int extra,
|
||||
int limit, int* valid) {
|
||||
if (threadIdx.x || blockIdx.x) return;
|
||||
*valid = 0;
|
||||
bool ok = true;
|
||||
if (mode == 0 || mode == 1) {
|
||||
for (int r = 0; r < M; ++r) {
|
||||
ok = ok && x[(int64_t)r * stride] >= 0;
|
||||
if (mode == 1) ok = ok && x[plane + (int64_t)r * stride] >= 0;
|
||||
}
|
||||
} else if (mode == 2) {
|
||||
ok = x[0] == 0 && x[(int64_t)extra * stride] == M;
|
||||
for (int s = 0; s < extra; ++s) {
|
||||
const int64_t b = x[(int64_t)s * stride], e = x[(int64_t)(s + 1) * stride];
|
||||
ok = ok && b >= 0 && e > b && e <= M && e - b <= limit;
|
||||
}
|
||||
} else {
|
||||
for (int64_t p = 0; ok && p < M;) {
|
||||
const int64_t h = x[2 * plane + p * stride], w = x[3 * plane + p * stride];
|
||||
ok = h > 0 && w > 0 && h % extra == 0 && w % extra == 0 && h <= (M - p) / w;
|
||||
for (int64_t j = 0; ok && j < h * w; ++j)
|
||||
ok = x[(p + j) * stride] == j / w && x[plane + (p + j) * stride] == j % w &&
|
||||
x[2 * plane + (p + j) * stride] == h && x[3 * plane + (p + j) * stride] == w;
|
||||
if (ok) p += h * w;
|
||||
}
|
||||
}
|
||||
if (!ok) __builtin_trap();
|
||||
*valid = 1;
|
||||
}
|
||||
|
||||
__global__ void scale(uint16_t* x, int64_t ld, int64_t cs, const int* pos, int64_t ps, int M, int n,
|
||||
int original, float beta, const int* valid) {
|
||||
if (*valid != 1) return;
|
||||
for (int64_t r = blockIdx.y; r < M; r += gridDim.y) {
|
||||
const int64_t c = (int64_t)blockIdx.x * NT + threadIdx.x;
|
||||
if (c < n) {
|
||||
const float g = narrow(1.f + beta * logf(1.f + (float)(pos[r * ps] / original)));
|
||||
x[r * ld + c * cs] = f32_to_bf16(bf2f(x[r * ld + c * cs]) * g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void rope(uint16_t* x, int64_t ld, int64_t cs, const int* pos, int64_t ps, int64_t plane,
|
||||
int M, int D, int H, int K, RotaryFrequency frequency, float magnitude,
|
||||
bool pixtral, const int* valid) {
|
||||
if (*valid != 1) return;
|
||||
const int i = threadIdx.x % (D / 2), h = threadIdx.x / (D / 2) + blockIdx.x * (NT / (D / 2));
|
||||
if (h >= H + K) return;
|
||||
for (int64_t r = blockIdx.y; r < M; r += gridDim.y) {
|
||||
const int axis = pixtral ? i / (D / 4) : 0;
|
||||
const float angle = (float)pos[axis * plane + r * ps] * frequency.f[i];
|
||||
const float c = narrow(cosf(angle) * magnitude), s = narrow(sinf(angle) * magnitude);
|
||||
const int64_t u = r * ld + (h * D + i) * cs, v = u + (D / 2) * cs;
|
||||
const float a = bf2f(x[u]), b = bf2f(x[v]);
|
||||
x[u] = f32_to_bf16(narrow(a * c) - narrow(b * s));
|
||||
x[v] = f32_to_bf16(narrow(b * c) + narrow(a * s));
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void merge(const uint16_t* x, int64_t ld, int64_t cs, const int* rc, int64_t rs,
|
||||
int64_t ps, uint16_t* y, int64_t yl, int64_t ys, int M, int N, int K,
|
||||
const int* valid) {
|
||||
if (*valid != 1) return;
|
||||
for (int64_t p = blockIdx.y; p < M; p += gridDim.y) {
|
||||
const int row = rc[p * ps], col = rc[rs + p * ps], w = rc[3 * rs + p * ps];
|
||||
if (row % K || col % K) continue;
|
||||
const int64_t base = p - (int64_t)row * w - col;
|
||||
const int64_t dst = base / (K * K) + (row / K) * (w / K) + col / K;
|
||||
for (int64_t n = (int64_t)blockIdx.x * NT + threadIdx.x; n < N; n += (int64_t)gridDim.x * NT)
|
||||
for (int dy = 0; dy < K; ++dy)
|
||||
for (int dx = 0; dx < K; ++dx)
|
||||
y[dst * yl + ((int64_t)n * K * K + dy * K + dx) * ys] =
|
||||
x[(p + (int64_t)dy * w + dx) * ld + (int64_t)n * cs];
|
||||
}
|
||||
}
|
||||
|
||||
template <int D>
|
||||
__global__ __launch_bounds__(32) void attention(const uint16_t* q, const uint16_t* k,
|
||||
const uint16_t* v, uint16_t* out, const int* cu,
|
||||
int64_t cus, int M, int H, int G, int64_t ql,
|
||||
int64_t kl, int64_t vl, int64_t ol, float scale,
|
||||
const int* valid) {
|
||||
if (*valid != 1) return;
|
||||
using BF = DT16<0>;
|
||||
const int lane = threadIdx.x, c = lane & 15, h = lane >> 4;
|
||||
const int begin = cu[(int64_t)blockIdx.z * cus], end = cu[(int64_t)(blockIdx.z + 1) * cus];
|
||||
const int qi = blockIdx.x * 16 + c, head = blockIdx.y, kh = head / G;
|
||||
if ((int)blockIdx.x * 16 >= end - begin) return;
|
||||
const int qr = begin + min(qi, end - begin - 1);
|
||||
typename BF::frag qf[D / 16];
|
||||
for (int t = 0; t < D / 16; ++t) {
|
||||
typename BF::frag f;
|
||||
for (int e = 0; e < 8; ++e)
|
||||
f[e] = __builtin_bit_cast(__bf16, q[(int64_t)qr * ql + head * D + t * 16 + h * 8 + e]);
|
||||
qf[t] = f;
|
||||
}
|
||||
__shared__ uint16_t sv[D][24];
|
||||
__shared__ float sp[16][16];
|
||||
v8f acc[D / 16];
|
||||
for (int t = 0; t < D / 16; ++t)
|
||||
acc[t] = (v8f){0, 0, 0, 0, 0, 0, 0, 0};
|
||||
float mx = -INFINITY, denom = 0;
|
||||
for (int pass = 0; pass < 3; ++pass) {
|
||||
for (int kb = begin; kb < end; kb += 16) {
|
||||
v8f s = (v8f){0, 0, 0, 0, 0, 0, 0, 0};
|
||||
if constexpr (D == 64) {
|
||||
// Pixtral's small heads: preserve the reference dot order, including very small
|
||||
// components beside the learned large coordinates. BF16 midpoint crossings
|
||||
// otherwise compound over the vision tower before the text encoder sees it.
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int kr = min(kb + h * 8 + e, end - 1);
|
||||
float dot = 0;
|
||||
for (int d = 0; d < D; ++d)
|
||||
dot +=
|
||||
bf2f(q[(int64_t)qr * ql + head * D + d]) * bf2f(k[(int64_t)kr * kl + kh * D + d]);
|
||||
s[e] = dot;
|
||||
}
|
||||
} else {
|
||||
for (int t = 0; t < D / 16; ++t) {
|
||||
typename BF::frag f;
|
||||
const int kr = min(kb + c, end - 1);
|
||||
for (int e = 0; e < 8; ++e)
|
||||
f[e] = __builtin_bit_cast(__bf16, k[(int64_t)kr * kl + kh * D + t * 16 + h * 8 + e]);
|
||||
s = BF::wmma(f, qf[t], s);
|
||||
}
|
||||
}
|
||||
float p[8];
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const float score = narrow(narrow(s[e]) * scale);
|
||||
p[e] = kb + h * 8 + e < end ? score : -INFINITY;
|
||||
if (pass == 0)
|
||||
mx = fmaxf(mx, p[e]);
|
||||
else
|
||||
p[e] = expf(p[e] - mx);
|
||||
if (pass == 1 && D != 64) denom += p[e];
|
||||
}
|
||||
if constexpr (D == 64) {
|
||||
if (pass == 1) {
|
||||
for (int e = 0; e < 8; ++e)
|
||||
denom += __shfl(p[e], c);
|
||||
for (int e = 0; e < 8; ++e)
|
||||
denom += __shfl(p[e], c + 16);
|
||||
}
|
||||
}
|
||||
if (pass != 2) continue;
|
||||
if constexpr (D == 64) {
|
||||
for (int e = 0; e < 8; ++e)
|
||||
sp[c][h * 8 + e] = narrow(p[e] / denom);
|
||||
__syncthreads();
|
||||
for (int j = 0; j < 16 && kb + j < end; ++j)
|
||||
for (int t = 0; t < D / 16; ++t)
|
||||
for (int e = 0; e < 8; ++e)
|
||||
acc[t][e] += sp[c][j] * bf2f(v[(int64_t)(kb + j) * vl + kh * D + t * 16 + h * 8 + e]);
|
||||
__syncthreads();
|
||||
continue;
|
||||
}
|
||||
for (int z = lane; z < D * 16; z += 32) {
|
||||
const int d = z / 16, key = z % 16;
|
||||
sv[d][key] = kb + key < end ? v[(int64_t)(kb + key) * vl + kh * D + d] : 0;
|
||||
}
|
||||
__syncthreads();
|
||||
typename BF::frag pf;
|
||||
for (int e = 0; e < 8; ++e)
|
||||
pf[e] = __builtin_bit_cast(__bf16, f32_to_bf16(p[e] / denom));
|
||||
for (int t = 0; t < D / 16; ++t) {
|
||||
typename BF::frag vf;
|
||||
for (int e = 0; e < 8; ++e)
|
||||
vf[e] = __builtin_bit_cast(__bf16, sv[t * 16 + c][h * 8 + e]);
|
||||
acc[t] = BF::wmma(vf, pf, acc[t]);
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
if (pass == 0) mx = fmaxf(mx, __shfl_xor(mx, 16));
|
||||
if (pass == 1 && D != 64) denom += __shfl_xor(denom, 16);
|
||||
}
|
||||
if (qi >= end - begin) return;
|
||||
for (int t = 0; t < D / 16; ++t)
|
||||
for (int e = 0; e < 8; ++e)
|
||||
out[(int64_t)(begin + qi) * ol + head * D + t * 16 + h * 8 + e] = f32_to_bf16(acc[t][e]);
|
||||
}
|
||||
|
||||
// Pixtral's RGB 14x14 patch projection has K=588, outside the existing K%16 GEMMs.
|
||||
// Keep the oracle's ordered f32 dot for this small stem: a WMMA reduction can cross a BF16
|
||||
// rounding midpoint. No existing GEMM row accepts this geometry, so no old row is replaced.
|
||||
__global__ __launch_bounds__(NT) void patch_projection(const uint16_t* a, const uint16_t* w,
|
||||
uint16_t* y, int M, int N, int64_t al,
|
||||
int64_t wl, int64_t yl) {
|
||||
const int n = blockIdx.x * NT + threadIdx.x;
|
||||
if (n >= N) return;
|
||||
for (int64_t r = blockIdx.y; r < M; r += gridDim.y) {
|
||||
float sum = 0;
|
||||
for (int d = 0; d < 588; ++d)
|
||||
sum += bf2f(a[r * al + d]) * bf2f(w[(int64_t)n * wl + d]);
|
||||
y[r * yl + n] = f32_to_bf16(sum);
|
||||
}
|
||||
}
|
||||
|
||||
bool matrix(const RadTensor& t, int64_t m, int64_t n) {
|
||||
constexpr int64_t max_elements = INT64_MAX / 2;
|
||||
if (!t.data || t.dtype != RAD_BF16 || t.rank != 2 || t.shape[0] != m || t.shape[1] != n ||
|
||||
m <= 0 || n <= 0 || t.stride[1] <= 0 || t.stride[0] <= 0 || n > max_elements / t.stride[1])
|
||||
return false;
|
||||
const int64_t columns = (n - 1) * t.stride[1];
|
||||
return t.stride[0] >= columns + 1 && (m - 1) <= (max_elements - columns) / t.stride[0];
|
||||
}
|
||||
bool indices(const RadTensor& t, int64_t n, int planes = 1) {
|
||||
constexpr int64_t max_elements = INT64_MAX / 4;
|
||||
if (!t.data || t.dtype != RAD_I32 || t.rank != (planes == 1 ? 1u : 2u) || n <= 0 ||
|
||||
t.stride[t.rank - 1] <= 0 || n > max_elements / t.stride[t.rank - 1])
|
||||
return false;
|
||||
if (planes == 1) return t.shape[0] == n;
|
||||
const int64_t columns = (n - 1) * t.stride[1];
|
||||
return t.shape[0] == planes && t.shape[1] == n && t.stride[0] >= columns + 1 &&
|
||||
t.stride[0] <= (max_elements - columns) / (planes - 1);
|
||||
}
|
||||
int prepare(const RadArgs* a, RadStream stream, const RadTensor& idx, int M, int mode,
|
||||
int extra = 0, int limit = 32768) {
|
||||
if (!a->scratch || a->scratch_bytes < 4) return RAD_E_SCRATCH;
|
||||
hipLaunchKernelGGL(validate, dim3(1), dim3(1), 0, (hipStream_t)stream, (const int*)idx.data,
|
||||
idx.stride[idx.rank - 1], idx.rank == 2 ? idx.stride[0] : 0, M, mode, extra,
|
||||
limit, (int*)a->scratch);
|
||||
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
int finish() {
|
||||
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
}
|
||||
|
||||
// Frequencies are shape constants. Resolve them once with the host oracle's powf, rather than
|
||||
// let a device pow approximation perturb long-position phases before BF16 narrowing.
|
||||
static int rotary_init(const RadParam* p, int np, void** out, bool pix) {
|
||||
if (!out) return RAD_E_INVAL;
|
||||
RadArgs a{};
|
||||
a.p = p;
|
||||
a.n_p = np;
|
||||
const int64_t D = r4d_geti_or(&a, "head_dim", 0);
|
||||
const float theta = r4d_getf(&a, "theta", 0);
|
||||
const RadYarn yarn = rad_yarn_args(&a);
|
||||
if ((D != 64 && D != 128) || !std::isfinite(theta) || theta <= 0 || !rad_yarn_valid(yarn))
|
||||
return RAD_E_SHAPE;
|
||||
auto* f = new (std::nothrow) RotaryFrequency;
|
||||
if (!f) return RAD_E_NOMEM;
|
||||
for (int i = 0; i < D / 2; ++i) {
|
||||
const int axis = pix ? i / (D / 4) : 0, j = pix ? i % (D / 4) : i;
|
||||
const float inv = 1.f / std::pow(theta, (float)(pix ? 4 * j + 2 * axis : 2 * i) / (float)D);
|
||||
f->f[i] = pix ? inv : yarn.frequency(inv, i);
|
||||
}
|
||||
*out = f;
|
||||
return RAD_OK;
|
||||
}
|
||||
extern "C" int r4d_ministral_init(const RadParam* p, int n, int, int, void** out) {
|
||||
return rotary_init(p, n, out, false);
|
||||
}
|
||||
extern "C" int r4d_pixtral_init(const RadParam* p, int n, int, int, void** out) {
|
||||
return rotary_init(p, n, out, true);
|
||||
}
|
||||
extern "C" void r4d_rotary_fini(void* p) {
|
||||
delete (RotaryFrequency*)p;
|
||||
}
|
||||
extern "C" int64_t r4d_nemotron_scratch(const RadArgs*) {
|
||||
return 4;
|
||||
}
|
||||
extern "C" int r4d_rad_position_scale(const RadArgs* a, RadStream stream) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const auto &x = a->t[0], &p = a->t[1];
|
||||
const int64_t M = x.rank == 2 ? x.shape[0] : 0, n = r4d_geti_or(a, "n", 0),
|
||||
orig = r4d_geti_or(a, "original", 0);
|
||||
const float beta = r4d_getf(a, "beta", -1);
|
||||
if (M <= 0 || M > INT_MAX || M > r4d_geti_or(a, "M", 0) || n <= 0 || n > INT_MAX || orig <= 0 ||
|
||||
orig > INT_MAX || !std::isfinite(beta) || beta < 0 || !matrix(x, M, n) || !indices(p, M))
|
||||
return RAD_E_SHAPE;
|
||||
int rc = prepare(a, stream, p, (int)M, 0);
|
||||
if (rc < 0) return rc;
|
||||
hipLaunchKernelGGL(scale, dim3((n + NT - 1) / NT, std::min<int64_t>(M, 65535)), dim3(NT), 0,
|
||||
(hipStream_t)stream, (uint16_t*)x.data, x.stride[0], x.stride[1],
|
||||
(const int*)p.data, p.stride[0], (int)M, (int)n, (int)orig, beta,
|
||||
(const int*)a->scratch);
|
||||
return finish();
|
||||
}
|
||||
static int rotary(const RadArgs* a, RadStream stream, bool pix) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const auto &x = a->t[0], &p = a->t[1];
|
||||
const int64_t M = x.rank == 2 ? x.shape[0] : 0, D = r4d_geti_or(a, "head_dim", 0),
|
||||
H = r4d_geti_or(a, "n_head", 0), K = pix ? H : r4d_geti_or(a, "n_head_kv", 0);
|
||||
const float theta = r4d_getf(a, "theta", 0);
|
||||
const RadYarn yarn = rad_yarn_args(a);
|
||||
if (M <= 0 || M > INT_MAX || M > r4d_geti_or(a, "M", 0) || (D != 64 && D != 128) || H <= 0 ||
|
||||
H > 1024 || K <= 0 || K > H || H % K || !std::isfinite(theta) || theta <= 0 ||
|
||||
!rad_yarn_valid(yarn) || !matrix(x, M, (H + 2 * K) * D) || !indices(p, M, pix ? 2 : 1))
|
||||
return RAD_E_SHAPE;
|
||||
if (!a->instance) return RAD_E_STATE;
|
||||
int rc = prepare(a, stream, p, (int)M, pix ? 1 : 0);
|
||||
if (rc < 0) return rc;
|
||||
hipLaunchKernelGGL(rope, dim3(((H + K) * (D / 2) + NT - 1) / NT, std::min<int64_t>(M, 65535)),
|
||||
dim3(NT), 0, (hipStream_t)stream, (uint16_t*)x.data, x.stride[0], x.stride[1],
|
||||
(const int*)p.data, p.stride[p.rank - 1], pix ? p.stride[0] : 0, (int)M,
|
||||
(int)D, (int)H, (int)K, *(const RotaryFrequency*)a->instance, yarn.magnitude,
|
||||
pix, (const int*)a->scratch);
|
||||
return finish();
|
||||
}
|
||||
extern "C" int r4d_rad_ministral_rope(const RadArgs* a, RadStream s) {
|
||||
return rotary(a, s, false);
|
||||
}
|
||||
extern "C" int r4d_rad_pixtral_rope(const RadArgs* a, RadStream s) {
|
||||
return rotary(a, s, true);
|
||||
}
|
||||
extern "C" int r4d_rad_pixtral_merge(const RadArgs* a, RadStream stream) {
|
||||
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
|
||||
const auto &x = a->t[0], &p = a->t[1], &y = a->t[2];
|
||||
const int64_t M = x.rank == 2 ? x.shape[0] : 0, N = r4d_geti_or(a, "n", 0),
|
||||
K = r4d_geti_or(a, "merge", 0);
|
||||
if (M <= 0 || M > INT_MAX || M > r4d_geti_or(a, "M", 0) || N <= 0 || N > INT_MAX || K != 2 ||
|
||||
M % 4 || !matrix(x, M, N) || !indices(p, M, 4) || !matrix(y, M / 4, N * 4))
|
||||
return RAD_E_SHAPE;
|
||||
int rc = prepare(a, stream, p, (int)M, 3, 2);
|
||||
if (rc < 0) return rc;
|
||||
hipLaunchKernelGGL(merge, dim3((N + NT - 1) / NT, std::min<int64_t>(M, 65535)), dim3(NT), 0,
|
||||
(hipStream_t)stream, (const uint16_t*)x.data, x.stride[0], x.stride[1],
|
||||
(const int*)p.data, p.stride[0], p.stride[1], (uint16_t*)y.data, y.stride[0],
|
||||
y.stride[1], (int)M, (int)N, (int)K, (const int*)a->scratch);
|
||||
return finish();
|
||||
}
|
||||
extern "C" int r4d_rad_embed_attn(const RadArgs* a, RadStream stream) {
|
||||
if (!rad_args_have(a, 5)) return RAD_E_INVAL;
|
||||
const auto &q = a->t[0], &k = a->t[1], &v = a->t[2], &cu = a->t[3], &o = a->t[4];
|
||||
const int64_t M = q.rank == 2 ? q.shape[0] : 0, D = r4d_geti_or(a, "head_dim", 0),
|
||||
G = r4d_geti_or(a, "gqa", 0), H = D > 0 ? q.shape[1] / D : 0,
|
||||
S = cu.rank == 1 ? cu.shape[0] - 1 : 0;
|
||||
if (M <= 0 || M > INT_MAX || M > r4d_geti_or(a, "M", 0) || (D != 64 && D != 128) || G <= 0 ||
|
||||
H <= 0 || H > 65535 || H % G || S <= 0 || S > 65535 || r4d_geti_or(a, "causal", 0) != 0 ||
|
||||
!matrix(q, M, H * D) || !matrix(k, M, H / G * D) || !matrix(v, M, H / G * D) ||
|
||||
!matrix(o, M, H * D) || !indices(cu, S + 1) || q.stride[1] != 1 || k.stride[1] != 1 ||
|
||||
v.stride[1] != 1 || o.stride[1] != 1)
|
||||
return RAD_E_SHAPE;
|
||||
const float sc = r4d_getf(a, "scale", 1.0 / std::sqrt((float)D));
|
||||
if (!std::isfinite(sc) || sc <= 0) return RAD_E_INVAL;
|
||||
const int64_t limit = std::min<int64_t>({M, 32768, r4d_geti_or(a, "max_seqlen", 32768)});
|
||||
if (limit <= 0) return RAD_E_SHAPE;
|
||||
int rc = prepare(a, stream, cu, (int)M, 2, (int)S, (int)limit);
|
||||
if (rc < 0) return rc;
|
||||
// Each segment is at most 32768; combined M may exceed that. The grid uses a bounded
|
||||
// per-segment extent without carving a request into independent attention states.
|
||||
dim3 grid((limit + 15) / 16, H, S);
|
||||
#define LAUNCH(DIM) \
|
||||
hipLaunchKernelGGL((attention<DIM>), grid, dim3(32), 0, (hipStream_t)stream, \
|
||||
(const uint16_t*)q.data, (const uint16_t*)k.data, (const uint16_t*)v.data, \
|
||||
(uint16_t*)o.data, (const int*)cu.data, cu.stride[0], (int)M, (int)H, (int)G, \
|
||||
q.stride[0], k.stride[0], v.stride[0], o.stride[0], sc, \
|
||||
(const int*)a->scratch)
|
||||
if (D == 64) {
|
||||
LAUNCH(64);
|
||||
} else {
|
||||
LAUNCH(128);
|
||||
}
|
||||
#undef LAUNCH
|
||||
return finish();
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_pixtral_patch(const RadArgs* a, RadStream stream) {
|
||||
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
|
||||
const auto &x = a->t[0], &w = a->t[1], &y = a->t[2];
|
||||
const int64_t M = x.rank == 2 ? x.shape[0] : 0, N = r4d_geti_or(a, "N", 0);
|
||||
if (M <= 0 || M > INT_MAX || M > r4d_geti_or(a, "M", 0) || N <= 0 || N > INT_MAX ||
|
||||
r4d_geti_or(a, "K", 0) != 588 || !matrix(x, M, 588) || !matrix(w, N, 588) ||
|
||||
!matrix(y, M, N) || x.stride[1] != 1 || w.stride[1] != 1 || y.stride[1] != 1)
|
||||
return RAD_E_SHAPE;
|
||||
hipLaunchKernelGGL(patch_projection, dim3((N + NT - 1) / NT, std::min<int64_t>(M, 65535)),
|
||||
dim3(NT), 0, (hipStream_t)stream, (const uint16_t*)x.data,
|
||||
(const uint16_t*)w.data, (uint16_t*)y.data, (int)M, (int)N, x.stride[0],
|
||||
w.stride[0], y.stride[0]);
|
||||
return finish();
|
||||
}
|
||||
@@ -13,6 +13,19 @@
|
||||
#include "r4d_args.h"
|
||||
|
||||
|
||||
extern "C" {
|
||||
int r4d_rad_pixtral_patch(const RadArgs*, RadStream);
|
||||
int r4d_rad_position_scale(const RadArgs*, RadStream);
|
||||
int r4d_rad_ministral_rope(const RadArgs*, RadStream);
|
||||
int r4d_rad_pixtral_rope(const RadArgs*, RadStream);
|
||||
int r4d_rad_pixtral_merge(const RadArgs*, RadStream);
|
||||
int r4d_rad_embed_attn(const RadArgs*, RadStream);
|
||||
int64_t r4d_nemotron_scratch(const RadArgs*);
|
||||
int r4d_ministral_init(const RadParam*, int, int, int, void**);
|
||||
int r4d_pixtral_init(const RadParam*, int, int, int, void**);
|
||||
void r4d_rotary_fini(void*);
|
||||
}
|
||||
|
||||
/* ================================================================== the priority ladder
|
||||
*
|
||||
* The selector ranks candidate rows by an integer priority (spec §2.1); where two rows of one op
|
||||
|
||||
+74
-6
@@ -2915,7 +2915,37 @@ static const RadOperandSpec oQuantCols[] = {
|
||||
{ "x", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 }, { "scale", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
|
||||
static const RadParamSpec pPositionScale[] = { P_INT("M"), P_INT("n"), P_INT("original"),
|
||||
P_F64("beta"), P_STR("dtype") };
|
||||
static const RadOperandSpec oPositionScale[] = { { "x", RAD_OPD_INOUT, 0 }, { "positions", RAD_OPD_IN, 0 } };
|
||||
|
||||
static const RadParamSpec pEmbedAttn[] = { P_INT("M"), P_INT("head_dim"), P_INT("gqa"),
|
||||
P_INT("causal"), P_INTO("max_seqlen"), P_STR("dtype"), P_F64O("scale") };
|
||||
static const RadOperandSpec oEmbedAttn[] = { { "q", RAD_OPD_IN, 0 }, { "k", RAD_OPD_IN, 0 }, { "v", RAD_OPD_IN, 0 }, { "cu_seqlens", RAD_OPD_IN, 0 }, { "out", RAD_OPD_OUT, 0 } };
|
||||
static const RadParamSpec pMinistralRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
|
||||
P_INT("n_head_kv"), P_F64("theta"), P_STR("dtype"), P_F64O("yarn_factor"),
|
||||
P_F64O("yarn_low"), P_F64O("yarn_high"), P_F64O("yarn_magnitude") };
|
||||
static const RadOperandSpec oMinistralRope[] = { { "qkv", RAD_OPD_INOUT, 0 }, { "pos", RAD_OPD_IN, 0 } };
|
||||
static const RadParamSpec pPixtralRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
|
||||
P_F64("theta"), P_STR("dtype") };
|
||||
static const RadOperandSpec oPixtralRope[] = { { "qkv", RAD_OPD_INOUT, 0 }, { "coord", RAD_OPD_IN, 0 } };
|
||||
static const RadParamSpec pPixtralMerge[] = { P_INT("M"), P_INT("n"), P_INT("merge"), P_STR("dtype") };
|
||||
static const RadOperandSpec oPixtralMerge[] = { { "x", RAD_OPD_IN, 0 }, { "coord", RAD_OPD_IN, 0 }, { "y", RAD_OPD_OUT, 0 } };
|
||||
|
||||
|
||||
static const RadOpSchema kSchemas[] = {
|
||||
{ "embed_attn", ROW(pEmbedAttn), ROW(oEmbedAttn),
|
||||
"Whole noncausal segments with QK, scaled scores and probabilities rounded to activation dtype." },
|
||||
{ "ministral_rope", ROW(pMinistralRope), ROW(oMinistralRope),
|
||||
"Ministral NeoX rotary with BF16 cos/sin and separately rounded products; V is unchanged." },
|
||||
{ "pixtral_rope", ROW(pPixtralRope), ROW(oPixtralRope),
|
||||
"Pixtral axial rotary with interleaved height/width frequencies and BF16 product rounding." },
|
||||
{ "pixtral_merge", ROW(pPixtralMerge), ROW(oPixtralMerge),
|
||||
"Pack full raster patch grids into channel-major spatial merge blocks, without mixing images." },
|
||||
{ "position_scale", ROW(pPositionScale), ROW(oPositionScale),
|
||||
"Scale query row p by 1 + beta * log(1 + floor(position[p] / original)), rounding the scale "
|
||||
"to x's dtype before multiplication. Positions are nonnegative i32." },
|
||||
|
||||
SCHEMA("rmsnorm", pRmsNorm, oRmsNorm,
|
||||
"y = x * rsqrt(mean(x^2) + eps) * (w + wadd). `wadd` is GemmaRMSNorm's `1 +`, added in "
|
||||
"f32 after the load because forming it in bf16 first costs 0.3% per channel."),
|
||||
@@ -4760,16 +4790,16 @@ static const RadConstraint cLogitRerank[] = {
|
||||
static const RadConstraint cGemmNt[] = {
|
||||
C_LE("M", 16), C_DIV("K", 256), C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* No 64-row cap: grid.y covers any row count, so the band is the grid's reach. A cap would force a
|
||||
* caller with a taller projection (the gated delta net's a|b) to slice it. */
|
||||
/* Matrix rows use the signed-int device ABI. Bounded row launches cover taller matrices without
|
||||
* requiring callers to slice their complete operations; arenas still follow configured budgets. */
|
||||
static const RadConstraint cGemmNt64[] = {
|
||||
C_LE("M", 65536), C_DIV("K", 16), C_IN("dtype", "bf16"),
|
||||
C_LE("M", 2147483647), C_DIV("K", 16), C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* M > 64 is the prefill band, where the skinny row's per-wave fragment loads re-read the
|
||||
* activation once per N tile. N >= 128 because a 64- or 128-wide tile over a narrower N computes
|
||||
* mostly clamped rows; those shapes (the GDN a|b at 48, the shared-expert gate at 1) stay skinny. */
|
||||
static const RadConstraint cGemmNtTiled[] = {
|
||||
C_GE("M", 65), C_LE("M", 65536), C_GE("N", 128), C_DIV("K", 64), C_IN("dtype", "bf16"),
|
||||
C_GE("M", 65), C_LE("M", 2147483647), C_GE("N", 128), C_DIV("K", 64), C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* The dense integer kernels take their group from a build constant and read no `group` argument.
|
||||
* A declaration at another group gets no kernel rather than a silent 128. */
|
||||
@@ -5588,7 +5618,43 @@ static const RadConstraint cVsa[] = {
|
||||
/* A thread quantises eight columns: one 16-byte load and one 8-byte store a row. */
|
||||
static const RadConstraint cQuantCols[] = { C_DIV("n", 8), C_IN("dtype", "bf16") };
|
||||
|
||||
static const RadConstraint cPixtralPatch[] = { C_IN("dtype","bf16"), C_EQ("K",588), C_GE("M",1), C_LE("M",2147483647), C_GE("N",1), C_LE("N",2147483647) };
|
||||
static const RadConstraint cNemotron[] = { C_IN("dtype", "bf16"), C_GE("M", 1), C_LE("M", 2147483647) };
|
||||
static const RadConstraint cNemotronMerge[] = { C_IN("dtype", "bf16"), C_GE("M", 1), C_LE("M", 2147483647), C_EQ("merge",2) };
|
||||
static const RadConstraint cNemotronRope64[] = { C_IN("dtype", "bf16"), C_GE("M", 1), C_LE("M", 2147483647), C_EQ("head_dim",64) };
|
||||
static const RadConstraint cNemotronRope128[] = { C_IN("dtype", "bf16"), C_GE("M", 1), C_LE("M", 2147483647), C_EQ("head_dim",128) };
|
||||
static const RadConstraint cNemotronAttn64[] = { C_IN("dtype", "bf16"), C_GE("M", 1), C_LE("M", 2147483647), C_EQ("head_dim",64), C_EQ("causal",0) };
|
||||
static const RadConstraint cNemotronAttn128[] = { C_IN("dtype", "bf16"), C_GE("M", 1), C_LE("M", 2147483647), C_EQ("head_dim",128), C_EQ("causal",0) };
|
||||
static const RadKernelInfo kKernels[] = {
|
||||
{ "gemm_bf16_pixtral_patch", "gemm_nt", "gemm", "RGB 14x14 patch projection with ordered f32 dots",
|
||||
"K=588, M<=INT_MAX, N<=INT_MAX, pitched bf16 rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 10, ROW(cPixtralPatch), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_pixtral_patch },
|
||||
{ "position_scale_bf16", "position_scale", "attn", "Ministral rounded query scale", "pitched active rows, nonnegative i32 positions", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotron), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_position_scale, r4d_nemotron_scratch },
|
||||
{ "pixtral_merge_bf16", "pixtral_merge", "vision", "Channel-major complete raster-grid merger", "merge 2, pitched active rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotronMerge), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_pixtral_merge, r4d_nemotron_scratch },
|
||||
{ "ministral_rope_h64_bf16", "ministral_rope", "attn", "Ministral rotary with separately rounded products", "head_dim 64, pitched active rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotronRope64), NOTUNE,
|
||||
r4d_ministral_init, r4d_rotary_fini, r4d_rad_ministral_rope, r4d_nemotron_scratch },
|
||||
{ "ministral_rope_h128_bf16", "ministral_rope", "attn", "Ministral rotary with separately rounded products", "head_dim 128, pitched active rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotronRope128), NOTUNE,
|
||||
r4d_ministral_init, r4d_rotary_fini, r4d_rad_ministral_rope, r4d_nemotron_scratch },
|
||||
{ "pixtral_rope_h64_bf16", "pixtral_rope", "attn", "Pixtral axial rotary with BF16 product seams", "head_dim 64, pitched active rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotronRope64), NOTUNE,
|
||||
r4d_pixtral_init, r4d_rotary_fini, r4d_rad_pixtral_rope, r4d_nemotron_scratch },
|
||||
{ "pixtral_rope_h128_bf16", "pixtral_rope", "attn", "Pixtral axial rotary with BF16 product seams", "head_dim 128, pitched active rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotronRope128), NOTUNE,
|
||||
r4d_pixtral_init, r4d_rotary_fini, r4d_rad_pixtral_rope, r4d_nemotron_scratch },
|
||||
{ "embed_attn_h64_bf16", "embed_attn", "attn", "Three-pass BF16 eager whole-segment attention", "head_dim 64, pitched active rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotronAttn64), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_embed_attn, r4d_nemotron_scratch },
|
||||
{ "embed_attn_h128_bf16", "embed_attn", "attn", "Three-pass BF16 eager whole-segment attention", "head_dim 128, pitched active rows", "bf16",
|
||||
RAD_DOMAIN_DEVICE, 50, ROW(cNemotronAttn128), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_embed_attn, r4d_nemotron_scratch },
|
||||
|
||||
/* ---------------------------------- the small model ops (r4d_model_bf16.hip) ----------------
|
||||
*
|
||||
* The ops between the larger kernels. They need device rows because the engine's buffers live in
|
||||
@@ -7796,7 +7862,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
|
||||
|
||||
{ "gemm_bf16_nt_m64", "gemm_nt", "gemm",
|
||||
"skinny GEMM C[M,N] = A[M,K] @ W[N,K]^T with 16x16x16 WMMA, split-K reduced in LDS",
|
||||
"any M -- MT row tiles a block and grid.y covers the rest; K divisible by SK*16; N, K runtime",
|
||||
"signed-int M, bounded row launches; K divisible by SK*16; N*K <= UINT32_MAX elements",
|
||||
"bf16 A / W / C, fp32 accumulate, deterministic reduction",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
|
||||
ROW(cGemmNt64), ROW(tGemmSkinny), r4d_tune_valid_gemm,
|
||||
@@ -7806,7 +7872,7 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
|
||||
{ "gemm_bf16_nt_tiled", "gemm_nt", "gemm",
|
||||
"the same GEMM at prefill row counts: eight waves a block over an LDS-staged 64-deep K tile, "
|
||||
"the next tile prefetched in registers while the current one is computed",
|
||||
"M > 64; N >= 128; K a multiple of 64; fragment rows past M or N clamped, never stored",
|
||||
"64 < M <= INT_MAX; N >= 128; K%64; bounded row launches; N*K <= UINT32_MAX elements",
|
||||
"bf16 A / W / C, fp32 accumulate. BYTE-IDENTICAL to gemm_bf16_nt_m64: the same WMMA sequence "
|
||||
"a tile and the same split count, the slices summed in the same order from 0.f",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_BF16_TILED,
|
||||
@@ -10213,6 +10279,8 @@ extern "C" int rad_kernel_concurrent(int i) {
|
||||
"hc_", "mtp_enter", "ar_", "gemm_w4a8_tiled", "gemm_w8a8_tiled", "lora_",
|
||||
/* The residency map's epoch, hand, heat counters and counters live across launches. */
|
||||
"kv_resolve",
|
||||
// Each model-local entry validates metadata before its dependent data launch.
|
||||
"position_scale_", "ministral_rope_", "pixtral_rope_", "pixtral_merge_", "embed_attn_",
|
||||
};
|
||||
for (const char* p : kStateful)
|
||||
if (std::strncmp(k->name, p, std::strlen(p)) == 0) return 0;
|
||||
|
||||
Reference in new issue
Block a user