Archived
New models and formats: - IQR for Qwen3.8-27B: weights on the int8 grid under GPTQ, a trellis down each GPTQ column (IQR-V2), and IQR-V3, a computed trellis code under one scale a row, kept at its codes' width in VRAM and decoded in the GEMM; 15 IQR-V3 recipes from ~2 to ~7 bits a weight (data/recipes/q38-27b-*-v3.recipe, docs/IQR.md) - libhalo: a kernel library for Strix Halo (gfx1151), preferred there over libr3d - Nemotron 3.5 Embed 1B and 8B (nemotron35_embed): text and image embeddings through a Pixtral tower, on the card and on the CPU - Nemotron 3.5 Rerank 8B - /v1/systemone: Cloudflare's clef and clef-flash, Convai Innovations' Laya and Mica beside LiquidAI d1; scored steps read the candidates' logits from the head table with no lm_head pass - LFM2's 8-bit trunk: block-fp8 or int8 linears with the fused producers, and the d1-3B recipe - DeepSeek-V4.1's reply format with DSML tool calls and typed argument tags Weights on the drive: - The file tier reads only the experts the pass routes rows to; the draft head's weights stay in the host pool - A hot VRAM cache for routed experts read off the drive, reads deposited straight into free host pool slots, and copyless evictions; the file tier's counters are now ssd_* - GLM-5 Next shards its routed experts by expert, not by width, so they can live on the drive - The stall watch counts drive reads as progress, and the DMA lane's room check reads the lane's own completion fence Quantisation tooling: - --calib-corpus, --calib-dir and --calib-layers record each linear's input Hessian; beside --kld-record the reference's own run records them - --kld-top N records a sparse KL reference (the N most likely tokens and the tail's mass) - GPTQ in a third of the time with the same codes, trellis GPTQ on the card, vector codebooks with coordinate descent, and rad-convert --reuse repeated, each container supplying what the earlier ones lack Speculation: - Prompt lookup alone (--spec-ngram K, --spec-ngram-min/-max): each sequence keeps an index of its n-grams, and a proposal is verified only when its measured step cost says it pays; --draft-window adaptive follows it Speed: - IQR decode: the narrow tile splits K for four times the int8 tile's waves and folds each 128-K block once; at 8 tokens down and the output projections run in 0.61-0.82x and 0.70-0.92x FP8's time (2-6 bits); IQ6R-S-V3 with its DFlash2 drafter steps in 26.3 ms at TP2, against 29.4 ms before and FP8's 27.3 - Qwen MXFP4 feed-forward: the gated fold up to 16 rows, not 8 - libr4d: the gated prefill fold, the short-prompt split row, and the rows forms of rmsnorm and rope; KLATE again for the head-256 gqa6 paged prefill; gram_accum on the matrix cores Features: - /health, /v1/health and /ping answer 503 with "status": "stuck" once the device has advanced nothing for a minute or a queue has faulted, so a supervisor can restart a wedged server (#356) - --image-gen-max-pixels and --image-gen-max-refs size an image generator's buffers (#344) - The startup report lists only the options not at their default, with a count; -v lists all (#250) - --preempt-wait-ms bounds how long a preempted prefill waits for its whole-recompute room - --placement layer_offload spills dense layers so one --max-model-len sequence fits - Startup refuses amdgpu with CWSR off and says how to fix it; --proceed-with-incompatible-configuration starts anyway under an UNSUPPORTED banner Fixes: - The n-gram read-ahead hands each row to the reader once and reads only as far ahead as the row cache holds, and pauses while decode reads: about one drive read a row instead of two (#355) - A file-tier window reads each of its runs once, not once a run per run - The decode split law's verify floor is bounded by the batch's workgroups: max_seqs 256 no longer declares 64x the scratch (#342) - A prefetch DMA list's mark stays out of the signal pool until the engine has read it, and a prefill stage copies only the blocks its own tables name (#343) - The split-KV row sum is read with fetch-inactive (#328) - LFM2 on the host backend: libref and libavx serve the row-form ops it declares (#348) - The preempted-prefill gate skips a KV group that holds no blocks (#352) - Every request entry point bounded, gated and sanitised (JSON depth, invalid UTF-8, control bytes in the log, the dashboard's CSP, cross-origin browsers on the loopback-only routes); a prompt id outside the vocabulary refused at admission; --api-key "" refused at startup - The telemetry the HTTP thread reads is atomic - An RMS norm row no longer depends on how many rows its launch carries; prefill attention's rescale is decided by live rows only - A --kld-record prefill chunk that finishes no sequence gets its logits on a tied-head model - A kernel's lazily made counter block is zeroed outside the lock other ranks take (a TP2 first-step hang) Changes: Plugin ABI 32. Every plugin exports a layout fingerprint of the ABI structs and the loader refuses a plugin without it or with another (#302), so out-of-tree plugins must be rebuilt. Appended: RadBuildCtx calib_* and gen_max_*, RadArchProbe gen_max_*, RadBatch score_only and plugin_words, RadDeviceProps::unified, RadChatFormat arg_json_value_prefix.
748 lines
35 KiB
Plaintext
748 lines
35 KiB
Plaintext
// gemm_nt_q over the IQR weights (r4d_iqr.h), decoded from their codes where they are read; VRAM
|
|
// holds a B-bit weight in B bits. Two rows, as the int8 family's (r4d_gemm_fp8a8.hip):
|
|
//
|
|
// gemm_iqr_nt_m64 M <= 64 (decode and speculative verify). At M <= 8 a GEMV: a wave a 16-row
|
|
// tile, its lanes across K, each lane's decoded values one sudot4 against the
|
|
// activation. Above, the i8a8 narrow tile with each 128-K block's weight decoded
|
|
// and transposed into fragment order through LDS just before its WMMA steps.
|
|
// gemm_iqr_tiled any M (prefill). The i8a8 prefill tile over the int8 fragment plane in the
|
|
// caller's scratch, which the tile's blocks decode as they arrive, each value
|
|
// once.
|
|
//
|
|
// The narrow and prefill tiles compute what the int8 tiles compute over the same int8 values (their
|
|
// split, staging, accumulation order and fold), so a weight decoded here and the same weight's int8
|
|
// values in the int8 kernels give the same output bytes. The GEMV folds per lane and then sums
|
|
// across lanes: the product within f32 rounding.
|
|
|
|
#include "r4d.h"
|
|
#include "r4d_args.h"
|
|
#include "r4d_emit.h"
|
|
#include "r4d_fp8_wmma.h"
|
|
#include "r4d_iqr.h"
|
|
#include "r4d_iqr_dec.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
|
|
/* The i8a8 narrow tile's grid and split rules and its finisher counters (r4d_gemm_fp8a8.hip): a
|
|
* split here sums each output as the int8 tile's does. */
|
|
uint32_t r4d_8a8_narrow_mw(uint32_t n, uint32_t m);
|
|
uint32_t r4d_8a8_narrow_ksplit(uint32_t N, uint32_t K, uint32_t M, uint32_t* ck, uint32_t target);
|
|
unsigned int* r4d_8a8_split_ctr(RadStream s, const void* out);
|
|
|
|
|
|
namespace {
|
|
|
|
using namespace r4d_fp8;
|
|
using namespace r4d_iqr;
|
|
using namespace r4d_iqr_dev;
|
|
|
|
constexpr uint32_t kWave = 32, kBlock = 128, kFK = 256;
|
|
|
|
/* The waves the narrow tile's K split aims for: four times the int8 tile's 128. A wave here decodes
|
|
* each unit and transposes it through LDS before its WMMA steps, so a weight N = 5120 rows wide
|
|
* (down, the attention and delta-net outputs: 320 waves) left unsplit keeps about one wave a SIMD
|
|
* and stalls on its own decode. At 8 tokens, 2 / 4 / 6 bits: down (K = 8704) 74 -> 54 / 56 / 66 us,
|
|
* the outputs (K = 3072) 29 -> 24 / 25 / 30 us; 1024 and more lose on the outputs. */
|
|
constexpr uint32_t kIqrSplitWaves = 512;
|
|
|
|
#include "r4d_gemm_8a8_prefill.h"
|
|
|
|
/* ---- the GEMV: M <= 8 ----------------------------------------------------------------------
|
|
*
|
|
* A wave a 16-row tile, its lanes across K: lane l takes columns 4l .. 4l + 3 of each 128-K block,
|
|
* so its decoded values of a row are one sudot4 against the activation's four bytes there, and a
|
|
* row's sum is folded into f32 with the (row, block) and (token, block) scales a block at a time,
|
|
* in each lane, then summed across the wave once at the end. The weight stream is the codes' own
|
|
* bytes, fetched a block ahead; the work a value is its decode, a quarter sudot4 a token and half
|
|
* an f32 fold. Split over K into partial planes when the tiles alone leave the card idle; the last
|
|
* z block of a column of blocks sums them in ascending z.
|
|
*
|
|
* The fold order differs from the WMMA tile's (per lane, then across lanes), so the output is the
|
|
* product within f32 rounding rather than the int8 tile's bytes. */
|
|
constexpr uint32_t kGW = 8; /* waves a block */
|
|
template <class F, uint32_t MT>
|
|
__global__ __launch_bounds__(kGW * kWave) R4D_WAVES(6)
|
|
void iqr_gemv_kernel(Ctx c0, const int8_t* __restrict__ X, const float* __restrict__ XS,
|
|
float* __restrict__ part, uint16_t* __restrict__ Y,
|
|
unsigned int* __restrict__ Ctr, uint32_t N, uint32_t x_ld, uint32_t xs_ld,
|
|
uint32_t kbs) {
|
|
using D = Dec<F, true>;
|
|
__shared__ __attribute__((aligned(16))) int8_t s_tab[D::kTab];
|
|
const uint32_t tid = threadIdx.x, lane = tid & (kWave - 1);
|
|
const uint32_t wave = __builtin_amdgcn_readfirstlane(tid / kWave);
|
|
Ctx c = c0;
|
|
c.tab = s_tab;
|
|
D::stage(s_tab, c, tid, kGW * kWave);
|
|
__syncthreads();
|
|
|
|
const uint32_t rt = blockIdx.x * kGW + wave;
|
|
const bool alive = rt * 16u < N;
|
|
const uint32_t kb0 = blockIdx.z * kbs, kb1 = min(c.nkb, kb0 + kbs);
|
|
const uint32_t myrow = rt * 16u + (lane & 15u);
|
|
|
|
float acc[16][MT];
|
|
#pragma unroll
|
|
for (uint32_t q = 0; q < 16; ++q)
|
|
#pragma unroll
|
|
for (uint32_t m = 0; m < MT; ++m) acc[q][m] = 0.0f;
|
|
|
|
typename D::Raw cur = D::fetch(c, rt, kb0, lane, alive && kb0 < kb1), nxt;
|
|
for (uint32_t kb = kb0; kb < kb1; ++kb) {
|
|
if (kb + 1u < kb1) nxt = D::fetch(c, rt, kb + 1u, lane, alive);
|
|
uint32_t xv[MT];
|
|
float as[MT];
|
|
#pragma unroll
|
|
for (uint32_t m = 0; m < MT; ++m) {
|
|
xv[m] = *(const uint32_t*)(X + (size_t)m * x_ld + kb * kBlock + 4u * lane);
|
|
as[m] = XS[(size_t)m * xs_ld + kb];
|
|
}
|
|
/* Row (lane & 15)'s fold scale and ratio, read by every lane through readlane. */
|
|
const float fs = alive ? fold_f32<F>(c, myrow, kb) : 0.0f;
|
|
const float ratio = alive ? rb_ratio<F>(c, myrow, kb) : 0.0f;
|
|
uint32_t w[16];
|
|
/* A computed trellis off the tcq_rb grid decodes each value as its byte sum, the value
|
|
* plus 126: one sudot4 of the unsigned sums less 126 times the lane's activation sum. */
|
|
constexpr bool UNS = D::MAD && F::LRB == 0;
|
|
[[maybe_unused]] int xsum[MT];
|
|
if constexpr (UNS) {
|
|
#pragma unroll
|
|
for (uint32_t m = 0; m < MT; ++m)
|
|
xsum[m] = 126 * __builtin_amdgcn_sudot4(true, (int)xv[m], false, 0x01010101, 0, false);
|
|
}
|
|
if constexpr (UNS && MT == 1) {
|
|
/* One token: each value's hash dotted with its activation byte in every byte lane
|
|
* sums the value's bytes and multiplies in one instruction, nothing packed. */
|
|
uint32_t h[16][4];
|
|
D::hashes(cur, c, h);
|
|
uint32_t xs[4];
|
|
#pragma unroll
|
|
for (uint32_t e = 0; e < 4; ++e) xs[e] = ((xv[0] >> (8u * e)) & 0xFFu) * 0x01010101u;
|
|
#pragma unroll
|
|
for (uint32_t q = 0; q < 16; ++q) {
|
|
const float wsq = __builtin_bit_cast(float, __builtin_amdgcn_readlane(__float_as_int(fs), q));
|
|
int d = -xsum[0];
|
|
#pragma unroll
|
|
for (uint32_t e = 0; e < 4; ++e)
|
|
d = __builtin_amdgcn_sudot4(false, (int)h[q][e], true, (int)xs[e], d, false);
|
|
acc[q][0] = fmaf((float)d, wsq * as[0], acc[q][0]);
|
|
}
|
|
} else {
|
|
D::template rows<UNS>(cur, c, ratio, w);
|
|
D::unzero(c, rt, kb, w);
|
|
#pragma unroll
|
|
for (uint32_t q = 0; q < 16; ++q) {
|
|
const float wsq = __builtin_bit_cast(float, __builtin_amdgcn_readlane(__float_as_int(fs), q));
|
|
#pragma unroll
|
|
for (uint32_t m = 0; m < MT; ++m) {
|
|
int d = __builtin_amdgcn_sudot4(!UNS, (int)w[q], true, (int)xv[m], 0, false);
|
|
if constexpr (UNS) d -= xsum[m];
|
|
acc[q][m] = fmaf((float)d, wsq * as[m], acc[q][m]);
|
|
}
|
|
}
|
|
}
|
|
cur = nxt;
|
|
}
|
|
#pragma unroll
|
|
for (uint32_t q = 0; q < 16; ++q)
|
|
#pragma unroll
|
|
for (uint32_t m = 0; m < MT; ++m) acc[q][m] = r4d_wave_sum(acc[q][m]);
|
|
if constexpr (F::SC == kRowF32) {
|
|
/* each row's own scale, after its blocks' sums (lane q's row q; a split's partials are
|
|
* linear in it) */
|
|
const float rs = alive ? row_scale<F>(c, rt * 16u + (lane & 15u)) : 0.0f;
|
|
#pragma unroll
|
|
for (uint32_t q = 0; q < 16; ++q) {
|
|
const float rq = __builtin_bit_cast(float, __builtin_amdgcn_readlane(__float_as_int(rs), q));
|
|
#pragma unroll
|
|
for (uint32_t m = 0; m < MT; ++m) acc[q][m] *= rq;
|
|
}
|
|
}
|
|
if (lane < 16u && alive) {
|
|
/* lane q stores row q: the sums are wave-uniform, picked by a constant index */
|
|
#pragma unroll
|
|
for (uint32_t q = 0; q < 16; ++q)
|
|
if (lane == q) {
|
|
#pragma unroll
|
|
for (uint32_t m = 0; m < MT; ++m) {
|
|
const size_t o = (size_t)m * N + rt * 16u + q;
|
|
if (gridDim.z > 1u) part[(size_t)blockIdx.z * MT * N + o] = acc[q][m];
|
|
else Y[o] = f32_to_bf16(acc[q][m]);
|
|
}
|
|
}
|
|
}
|
|
if (gridDim.z > 1u) {
|
|
__threadfence();
|
|
__syncthreads();
|
|
__shared__ unsigned int s_arrived;
|
|
if (tid == 0)
|
|
s_arrived = __hip_atomic_fetch_add(&Ctr[blockIdx.x], 1u, __ATOMIC_ACQ_REL,
|
|
__HIP_MEMORY_SCOPE_AGENT);
|
|
__syncthreads();
|
|
if (s_arrived + 1u == gridDim.z) {
|
|
if (tid == 0)
|
|
__hip_atomic_store(&Ctr[blockIdx.x], 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
|
|
__threadfence();
|
|
const uint32_t r0 = blockIdx.x * kGW * 16u, nr = min(kGW * 16u, N - r0);
|
|
for (uint32_t i = tid; i < nr * MT; i += kGW * kWave) {
|
|
const size_t o = (size_t)(i / nr) * N + r0 + i % nr;
|
|
float v = 0.0f;
|
|
for (uint32_t z = 0; z < gridDim.z; ++z) v += part[(size_t)z * MT * N + o];
|
|
Y[o] = f32_to_bf16(v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ---- the narrow tile: 8 < M <= 64 -----------------------------------------------------------
|
|
*
|
|
* r4d_gemm_fp8a8.hip's gemm_fp8a8_body at I8, unfused: MW waves a block, a wave one 16-row weight
|
|
* tile against NT 16-token tiles staged through LDS, split-K over the caller's scratch with the
|
|
* last z block reducing. Only the weight operand's source differs: each 128-K block's unit is
|
|
* decoded and transposed into fragment order through the wave's LDS tile (r4d_iqr_dec.h), its two
|
|
* supertiles just before their eight WMMA steps. The int8 tile's arithmetic, so its bytes. */
|
|
template <class F, uint32_t NT, uint32_t MW>
|
|
__global__ __launch_bounds__(MW * kWave) R4D_WAVES(MW >= 8u ? 4u : 6u)
|
|
void iqr_m64_kernel(Ctx c0, const uint8_t* __restrict__ Bp, const float* __restrict__ Sb,
|
|
void* __restrict__ Y, uint16_t* __restrict__ C, unsigned int* __restrict__ Ctr,
|
|
uint32_t rows, uint32_t cols, uint32_t nb, uint32_t b_ld, uint32_t sb_ld,
|
|
uint32_t ck) {
|
|
using D = Dec<F>;
|
|
constexpr uint32_t FK = kFK, FN = NT * 16, FM = MW * 16, kU = FK / kBlock;
|
|
__shared__ uint8_t s_b[FN * lds_pitch<FK>()];
|
|
__shared__ __attribute__((aligned(16))) int8_t s_tab[D::kTab];
|
|
__shared__ __attribute__((aligned(16))) uint8_t s_tr[MW * kTrBytes];
|
|
|
|
const uint32_t tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
|
|
const uint32_t kbeg = ck ? blockIdx.z * ck : 0u;
|
|
const uint32_t kend = ck ? min(cols, kbeg + ck) : cols;
|
|
if (kbeg >= kend) return;
|
|
|
|
Ctx c = c0;
|
|
c.tab = s_tab;
|
|
uint8_t* const tr = s_tr + wave * kTrBytes;
|
|
D::stage(s_tab, c, tid, MW * kWave);
|
|
|
|
const uint32_t m0 = blockIdx.x * FM, n0 = blockIdx.y * FN;
|
|
const uint32_t wrow0 = m0 + wave * 16u;
|
|
|
|
f8v raw[NT], out[NT];
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < NT; ++j) { raw[j] = acc_zero(); out[j] = acc_zero(); }
|
|
|
|
/* The activation's staging: the int8 narrow tile's (kTPR threads a row). */
|
|
constexpr uint32_t kSeg = 32, kTPR = FK / kSeg, kRPP = (MW * kWave) / kTPR;
|
|
constexpr uint32_t kBP = (FN + kRPP - 1) / kRPP;
|
|
const uint32_t sn = tid / kTPR, sk = (tid % kTPR) * kSeg;
|
|
const uint32_t brow0 = n0 + frag_c(lane);
|
|
const uint8_t* bsrc = Bp + (size_t)(n0 + sn) * b_ld + sk;
|
|
const uint32_t arow = wrow0 / 16u;
|
|
const bool alive = wrow0 < rows;
|
|
const bool k_live = kbeg + sk + kSeg <= cols;
|
|
|
|
Stage8 pb[kBP];
|
|
#pragma unroll
|
|
for (uint32_t p = 0; p < kBP; ++p) {
|
|
const uint32_t r = sn + p * kRPP;
|
|
pb[p] = stage8_load(bsrc + (size_t)p * kRPP * b_ld + kbeg, k_live && r < FN && n0 + r < nb);
|
|
}
|
|
typename D::Raw pf[kU], pfn[kU];
|
|
#pragma unroll
|
|
for (uint32_t p = 0; p < kU; ++p) {
|
|
const uint32_t kb = kbeg / kBlock + p;
|
|
pf[p] = D::fetch(c, arow, kb, lane, alive && kb < c.nkb);
|
|
}
|
|
__syncthreads(); /* the table */
|
|
|
|
for (uint32_t c0k = kbeg; c0k < kend; c0k += FK) {
|
|
#pragma unroll
|
|
for (uint32_t p = 0; p < kBP; ++p) {
|
|
const uint32_t r = sn + p * kRPP;
|
|
if (r < FN) stage8_store<FK>(s_b, r, sk, pb[p]);
|
|
}
|
|
__syncthreads();
|
|
const uint32_t c1 = c0k + FK;
|
|
if (c1 < kend) {
|
|
const bool live = c1 + sk + kSeg <= cols;
|
|
#pragma unroll
|
|
for (uint32_t p = 0; p < kBP; ++p) {
|
|
const uint32_t r = sn + p * kRPP;
|
|
if (r < FN) pb[p] = stage8_load(bsrc + (size_t)p * kRPP * b_ld + c1, live && n0 + r < nb);
|
|
}
|
|
#pragma unroll
|
|
for (uint32_t p = 0; p < kU; ++p) {
|
|
const uint32_t kb = c1 / kBlock + p;
|
|
pfn[p] = D::fetch(c, arow, kb, lane, alive && kb < c.nkb);
|
|
}
|
|
}
|
|
|
|
#pragma unroll
|
|
for (uint32_t p = 0; p < kU; ++p) {
|
|
const uint32_t kbu = c0k / kBlock + p;
|
|
uint32_t w[16];
|
|
D::rows(pf[p], c, alive ? rb_ratio<F>(c, wrow0 + (lane & 15u), kbu) : 0.0f, w);
|
|
D::unzero(c, arow, kbu, w);
|
|
Stage8 a8[2];
|
|
frag(w, tr, lane, a8);
|
|
#pragma unroll
|
|
for (uint32_t kk = 0; kk < kBlock / kK; ++kk) {
|
|
const uint32_t ks = p * kBlock + kk * kK;
|
|
const i2v af = frag_of(a8[kk / 4u], kk % 4u);
|
|
i2v bf[NT];
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < NT; ++j) bf[j] = frag<FK>(s_b, j * 16, ks, lane);
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < NT; ++j) raw[j] = mma8<true>(af, bf[j], raw[j]);
|
|
}
|
|
/* The unit's 128-K block is done: fold its sums. Units are 128-K aligned and a split's
|
|
* slice is a whole number of FK chunks (narrow_ksplit), so every block ends here. */
|
|
{
|
|
const uint32_t kb = kbu;
|
|
/* A lane's eight accumulator rows, consecutive rows of the wave's tile; under one
|
|
* f32 scale a row the block's factor is the same for all of them. */
|
|
float wsr[8];
|
|
if constexpr (F::SC == kRowF32) {
|
|
const float f = alive ? fold_f32<F>(c, wrow0, kb) : 0.0f;
|
|
#pragma unroll
|
|
for (uint32_t t = 0; t < 8; ++t) wsr[t] = f;
|
|
} else {
|
|
#pragma unroll
|
|
for (uint32_t t = 0; t < 8; ++t)
|
|
wsr[t] = alive ? fold_f32<F>(c, wrow0 + acc_row(lane, t), kb) : 0.0f;
|
|
}
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < NT; ++j) {
|
|
const uint32_t tok = brow0 + j * 16;
|
|
const float s = tok < nb ? Sb[(size_t)tok * sb_ld + kb] : 0.0f;
|
|
if constexpr (F::SC == kRowF32) {
|
|
const float ws = wsr[0] * s;
|
|
#pragma unroll
|
|
for (uint32_t t = 0; t < 8; ++t) {
|
|
out[j][t] += acc_val<true>(raw[j][t]) * ws;
|
|
raw[j][t] = 0.0f;
|
|
}
|
|
} else {
|
|
#pragma unroll
|
|
for (uint32_t t = 0; t < 8; ++t) {
|
|
out[j][t] += acc_val<true>(raw[j][t]) * (wsr[t] * s);
|
|
raw[j][t] = 0.0f;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#pragma unroll
|
|
for (uint32_t p = 0; p < kU; ++p) pf[p] = pfn[p];
|
|
__syncthreads();
|
|
}
|
|
|
|
/* The int8 narrow tile's store: eight consecutive weight rows of one token a lane a tile, one
|
|
* 16-byte write (rows is a multiple of 16), into the split's partial plane or C. */
|
|
float* const part = ck ? (float*)Y + (size_t)blockIdx.z * rows * nb : nullptr;
|
|
const uint32_t cl = frag_c(lane);
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < NT; ++j) {
|
|
const uint32_t col = n0 + j * 16 + cl;
|
|
const uint32_t r0 = wrow0 + acc_row(lane, 0);
|
|
if (col >= nb || r0 >= rows) continue;
|
|
f8v ov = out[j];
|
|
if constexpr (F::SC == kRowF32)
|
|
#pragma unroll
|
|
for (uint32_t t = 0; t < 8; ++t) ov[t] *= row_scale<F>(c, r0 + t);
|
|
const size_t o = (size_t)col * rows + r0;
|
|
if (ck) {
|
|
__attribute__((aligned(16))) float pv[8];
|
|
#pragma unroll
|
|
for (uint32_t t = 0; t < 8; ++t) pv[t] = ov[t];
|
|
*(uint4*)(part + o) = *(const uint4*)pv;
|
|
*(uint4*)(part + o + 4) = *(const uint4*)(pv + 4);
|
|
} else {
|
|
*(uint4*)(C + o) = make_uint4(bf16x2_rne(ov[0], ov[1]), bf16x2_rne(ov[2], ov[3]),
|
|
bf16x2_rne(ov[4], ov[5]), bf16x2_rne(ov[6], ov[7]));
|
|
}
|
|
}
|
|
|
|
/* The int8 narrow tile's split finisher: the last z block of a column sums the partials in
|
|
* ascending z, so the bytes are those of the separate reduce. */
|
|
if (ck && Ctr) {
|
|
__threadfence();
|
|
__syncthreads();
|
|
__shared__ unsigned int s_arrived;
|
|
if (tid == 0)
|
|
s_arrived = __hip_atomic_fetch_add(&Ctr[blockIdx.x], 1u, __ATOMIC_ACQ_REL,
|
|
__HIP_MEMORY_SCOPE_AGENT);
|
|
__syncthreads();
|
|
if (s_arrived + 1u == gridDim.z) {
|
|
if (tid == 0)
|
|
__hip_atomic_store(&Ctr[blockIdx.x], 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
|
|
__threadfence();
|
|
const uint32_t rlo = m0, rhi = min(m0 + FM, rows);
|
|
const float* const w0 = (const float*)Y;
|
|
for (uint32_t idx = tid; idx < (rhi - rlo) * nb; idx += MW * kWave) {
|
|
const uint32_t r = rlo + idx / nb, col = idx % nb;
|
|
const size_t o = (size_t)col * rows + r;
|
|
float acc = 0.0f;
|
|
for (uint32_t z = 0; z < gridDim.z; ++z) acc += w0[(size_t)z * rows * nb + o];
|
|
C[o] = f32_to_bf16(acc);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* The split's separate reduce, for a split with no counter block (r4d_gemm_fp8a8.hip's). */
|
|
__global__ void iqr_splitk_reduce(const float* __restrict__ ws, uint16_t* __restrict__ y,
|
|
uint32_t n_out, uint32_t ksplit) {
|
|
const size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x;
|
|
if (i >= n_out) return;
|
|
float acc = 0.0f;
|
|
for (uint32_t z = 0; z < ksplit; ++z) acc += ws[(size_t)z * n_out + i];
|
|
y[i] = f32_to_bf16(acc);
|
|
}
|
|
|
|
/* ---- the prefill: 64 < M ---------------------------------------------------------------------
|
|
*
|
|
* The int8 prefill tile (r4d_gemm_8a8_prefill.h) over the int8 fragment plane in the caller's
|
|
* scratch, which the tile's own blocks decode as they arrive. The blocks of one 128-row N tile are
|
|
* consecutive in the tile's raster order (its M bands within a group); each claims one of the N
|
|
* tile's eight 16-row tiles at a time, decodes it into the plane (and its fold scales, where the
|
|
* scale buffer is not already the int8 tile's), and once all eight are decoded every block of the
|
|
* N tile runs the K loop over them. A block claims until none is left before it waits, so it never
|
|
* waits on a block that has not started: no deadlock whatever the dispatch order. Later groups'
|
|
* blocks find the N tile decoded and go straight to the K loop; the last block of the N tile
|
|
* resets its three counters (claimed, decoded, finished), so they are zero for the next call.
|
|
*
|
|
* The decode is each value's once, overlapped with the other blocks' K loops, and the K loop is
|
|
* the int8 tile's, so the output is its bytes. */
|
|
template <class F>
|
|
struct IqrPre {
|
|
Ctx c;
|
|
uint8_t* fragp;
|
|
uint16_t* sc; /* the fold scales the tile reads, or null where the scale buffer is */
|
|
unsigned int* ctl; /* three counters an N tile */
|
|
uint32_t nbm; /* the M bands: blocks an N tile */
|
|
|
|
__device__ void begin(uint32_t bm, uint32_t bn, uint8_t* lds) const {
|
|
(void)bm;
|
|
using D = Dec<F>;
|
|
static_assert(kPWaves * kTrBytes <= kPrefillLds, "a wave's transpose tile in the staging LDS");
|
|
__shared__ __attribute__((aligned(16))) int8_t s_tab[D::kTab];
|
|
__shared__ uint32_t s_part;
|
|
const uint32_t tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
|
|
unsigned int* const claimed = ctl + 3u * bn;
|
|
unsigned int* const decoded = claimed + 1;
|
|
Ctx cc = c;
|
|
cc.tab = s_tab;
|
|
uint8_t* const tr = lds + wave * kTrBytes;
|
|
const uint32_t ksuper = c.nkb * 2u;
|
|
bool staged = false;
|
|
for (;;) {
|
|
if (tid == 0)
|
|
s_part = __hip_atomic_load(decoded, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT) >= 8u
|
|
? 8u
|
|
: __hip_atomic_fetch_add(claimed, 1u, __ATOMIC_RELAXED,
|
|
__HIP_MEMORY_SCOPE_AGENT);
|
|
__syncthreads();
|
|
const uint32_t part = s_part;
|
|
__syncthreads();
|
|
if (part >= 8u) break;
|
|
if (!staged) {
|
|
D::stage(s_tab, cc, tid, kPWaves * kWave);
|
|
__syncthreads();
|
|
staged = true;
|
|
}
|
|
const uint32_t rt = bn * 8u + part;
|
|
/* A unit's codes are fetched a unit ahead of its decode, so a wave's units do not
|
|
* each wait out a memory round trip. */
|
|
typename D::Raw cur = D::fetch(cc, rt, wave, lane, wave < c.nkb), nxt;
|
|
for (uint32_t kb = wave; kb < c.nkb; kb += kPWaves) {
|
|
if (kb + kPWaves < c.nkb) nxt = D::fetch(cc, rt, kb + kPWaves, lane, true);
|
|
uint32_t w[16];
|
|
D::rows(cur, cc, rb_ratio<F>(cc, rt * 16u + (lane & 15u), kb), w);
|
|
cur = nxt;
|
|
D::unzero(cc, rt, kb, w);
|
|
Stage8 s8[2];
|
|
frag(w, tr, lane, s8);
|
|
#pragma unroll
|
|
for (uint32_t h = 0; h < 2; ++h) {
|
|
uint8_t* d = fragp + ((size_t)rt * ksuper + 2u * kb + h) * kTileBytes +
|
|
lane * kLaneBytes;
|
|
*(uint4*)d = s8[h].v;
|
|
*(uint4*)(d + 16) = s8[h].w;
|
|
}
|
|
}
|
|
if (sc)
|
|
for (uint32_t i = tid; i < 16u * c.nkb; i += kPWaves * kWave) {
|
|
const uint32_t kb = i / 16u;
|
|
sc[((size_t)rt * c.nkb + kb) * 16u + i % 16u] =
|
|
(uint16_t)fold_scale<F>(cc, rt * 16u + i % 16u, kb);
|
|
}
|
|
__threadfence();
|
|
__syncthreads();
|
|
if (tid == 0)
|
|
__hip_atomic_fetch_add(decoded, 1u, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_AGENT);
|
|
}
|
|
if (tid == 0)
|
|
while (__hip_atomic_load(decoded, __ATOMIC_ACQUIRE, __HIP_MEMORY_SCOPE_AGENT) < 8u)
|
|
__builtin_amdgcn_s_sleep(2);
|
|
__syncthreads();
|
|
}
|
|
|
|
__device__ void end(uint32_t bn) const {
|
|
if (threadIdx.x != 0) return;
|
|
unsigned int* const claimed = ctl + 3u * bn;
|
|
if (__hip_atomic_fetch_add(claimed + 2, 1u, __ATOMIC_ACQ_REL, __HIP_MEMORY_SCOPE_AGENT) + 1u ==
|
|
nbm) {
|
|
__hip_atomic_store(claimed, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
|
|
__hip_atomic_store(claimed + 1, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
|
|
__hip_atomic_store(claimed + 2, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
|
|
}
|
|
}
|
|
};
|
|
|
|
template <class F, int RB>
|
|
__global__ __launch_bounds__(kPWaves * kWave) R4D_WAVES(6)
|
|
void iqr_prefill_kernel(IqrPre<F> pre, const uint8_t* __restrict__ A, const float* __restrict__ Sa,
|
|
const uint8_t* __restrict__ W, const uint16_t* __restrict__ Sw,
|
|
uint16_t* __restrict__ C, uint32_t M, uint32_t K, uint32_t N, uint32_t a_ld,
|
|
uint32_t sa_ld, uint32_t sw_ld) {
|
|
if constexpr (F::SC == kRowF32) {
|
|
constexpr Form f{ F::KIND, F::B, F::LV, F::L, F::LRB, F::SC, 0 };
|
|
const uint8_t* const sb = pre.c.S;
|
|
gemm_8a8_prefill_body<true, 32, IqrPre<F>, true>(
|
|
A, Sa, W, Sw, C, M, K, N, a_ld, sa_ld, sw_ld, pre, (const float*)sb,
|
|
(const float*)(sb + cf_off(f, N, K)));
|
|
} else {
|
|
gemm_8a8_prefill_body<true, RB>(A, Sa, W, Sw, C, M, K, N, a_ld, sa_ld, sw_ld, pre);
|
|
}
|
|
}
|
|
|
|
/* ---- the forms built: r4d_iqr.h's list ------------------------------------------------ */
|
|
|
|
#define R4D_IQR_T(k, b, lv, l, lrb, sc) FormT<k, b, lv, l, lrb, sc>
|
|
|
|
struct Operands {
|
|
const RadTensor *a, *as, *b, *bs, *y;
|
|
long long M, N, K, nkb, a_ld, sa_ld;
|
|
uint32_t word;
|
|
};
|
|
|
|
int operands(const RadArgs* a, Operands* g) {
|
|
g->a = r4d_opt(a, 0);
|
|
g->as = r4d_opt(a, 1);
|
|
g->b = r4d_opt(a, 2);
|
|
g->bs = r4d_opt(a, 3);
|
|
g->y = r4d_opt(a, 4);
|
|
if (!g->a || !g->as || !g->b || !g->bs || !g->y) return R4D_NO(RAD_E_INVAL);
|
|
if (!is_weight_dtype(g->b->dtype) || !is_scale_dtype(g->bs->dtype)) return R4D_NO(RAD_E_DTYPE);
|
|
g->word = g->b->dtype & kWordMask;
|
|
if ((g->bs->dtype & kWordMask) != g->word) return R4D_NO(RAD_E_DTYPE);
|
|
if (g->a->dtype != RAD_I8 || g->as->dtype != RAD_F32 || g->y->dtype != RAD_BF16)
|
|
return R4D_NO(RAD_E_DTYPE);
|
|
if (g->b->rank != 2 || g->y->rank < 2 || g->a->rank < 2 || g->as->rank != 2)
|
|
return R4D_NO(RAD_E_SHAPE);
|
|
g->M = g->y->shape[0];
|
|
if (g->M < 1 || r4d_numel(g->y) % g->M) return R4D_NO(RAD_E_SHAPE);
|
|
g->N = r4d_numel(g->y) / g->M;
|
|
g->K = g->b->shape[1];
|
|
if (g->b->shape[0] != g->N || g->N % 16 || g->K % kFK) return R4D_NO(RAD_E_SHAPE);
|
|
g->nkb = g->K / kBlock;
|
|
if (g->a->shape[0] != g->M || r4d_numel(g->a) != g->M * g->K) return R4D_NO(RAD_E_SHAPE);
|
|
if (g->as->shape[0] != g->M || g->as->shape[1] != g->nkb) return R4D_NO(RAD_E_SHAPE);
|
|
g->a_ld = r4d_row_pitch(g->a);
|
|
g->sa_ld = r4d_row_pitch(g->as);
|
|
if (g->a_ld < 0 || g->sa_ld < 0 || r4d_row_pitch(g->y) != g->N) return R4D_NO(RAD_E_STRIDE);
|
|
if (!r4d_aligned(g->a->data, 16) || !r4d_aligned(g->b->data, 256)) return R4D_NO(RAD_E_ALIGN);
|
|
const Form f = form(g->word);
|
|
if (is_trellis(f.kind) && g->N % ((long long)1 << f.lrun)) return R4D_NO(RAD_E_SHAPE);
|
|
return RAD_OK;
|
|
}
|
|
|
|
Ctx ctx_of(const Operands& g) {
|
|
Ctx c{};
|
|
c.W = (const uint8_t*)g.b->data;
|
|
c.S = (const uint8_t*)g.bs->data;
|
|
c.N = (uint32_t)g.N;
|
|
c.nkb = (uint32_t)g.nkb;
|
|
const Form f = form(g.word);
|
|
c.run_tiles = is_trellis(f.kind) ? (1u << f.lrun) / 16u : 1u;
|
|
return c;
|
|
}
|
|
|
|
/* The GEMV's K split: enough waves to keep the card's memory busy (2048 tiles' worth), each split at
|
|
* least four blocks. A function of N and K alone. */
|
|
constexpr uint32_t kGemvMaxM = 8;
|
|
uint32_t gemv_ksplit(uint32_t N, uint32_t nkb) {
|
|
const uint32_t tiles = N / 16u;
|
|
uint32_t ks = std::min((2048u + tiles - 1u) / tiles, std::max(1u, nkb / 4u));
|
|
const uint32_t kbs = (nkb + ks - 1u) / ks;
|
|
return (nkb + kbs - 1u) / kbs;
|
|
}
|
|
|
|
template <class F>
|
|
int gemv_launch(const Operands& g, void* scratch, long long scratch_bytes, RadStream stream) {
|
|
const uint32_t N = (uint32_t)g.N, M = (uint32_t)g.M, nkb = (uint32_t)g.nkb;
|
|
uint32_t ks = gemv_ksplit(N, nkb);
|
|
const uint32_t gx = (N / 16u + kGW - 1u) / kGW;
|
|
unsigned int* ctr = nullptr;
|
|
if (ks > 1u) {
|
|
ctr = gx <= 8192u ? r4d_8a8_split_ctr(stream, g.y->data) : nullptr;
|
|
if (!ctr || !scratch || scratch_bytes < (long long)ks * kGemvMaxM * N * 4) ks = 1u;
|
|
}
|
|
const uint32_t kbs = (nkb + ks - 1u) / ks;
|
|
const Ctx c = ctx_of(g);
|
|
const dim3 grid(gx, 1, ks);
|
|
int rc = RAD_E_SHAPE;
|
|
#define R4D_IQR_GV(mt_) rc = R4D_EMIT(stream, (iqr_gemv_kernel<F, mt_>), grid, dim3(kGW * kWave), 0, c, (const int8_t*)g.a->data, (const float*)g.as->data, (float*)scratch, (uint16_t*)g.y->data, ctr, N, (uint32_t)g.a_ld, (uint32_t)g.sa_ld, kbs)
|
|
switch (M) {
|
|
case 1u: R4D_IQR_GV(1u); break;
|
|
case 2u: R4D_IQR_GV(2u); break;
|
|
case 3u: R4D_IQR_GV(3u); break;
|
|
case 4u: R4D_IQR_GV(4u); break;
|
|
case 5u: R4D_IQR_GV(5u); break;
|
|
case 6u: R4D_IQR_GV(6u); break;
|
|
case 7u: R4D_IQR_GV(7u); break;
|
|
case 8u: R4D_IQR_GV(8u); break;
|
|
default: return R4D_NO(RAD_E_SHAPE);
|
|
}
|
|
#undef R4D_IQR_GV
|
|
return rc == RAD_OK && hipGetLastError() == hipSuccess ? RAD_OK : R4D_NO(RAD_E_DEVICE);
|
|
}
|
|
|
|
template <class F>
|
|
int m64_launch(const Operands& g, void* scratch, long long scratch_bytes, int split_n,
|
|
RadStream stream) {
|
|
const uint32_t N = (uint32_t)g.N, K = (uint32_t)g.K, M = (uint32_t)g.M;
|
|
/* A computed trellis's GEMV pays its decode in VALU work beside every token's dot and fold;
|
|
* from five tokens the narrow tile, which decodes a unit once for all of them, is faster
|
|
* (gate_up at 4 bits: GEMV 120 / 136 / 186 us at 3 / 4 / 8 tokens, the tile 151 / 154 / 157). */
|
|
constexpr uint32_t gemv_max = F::KIND == kTrellisMad ? 4u : kGemvMaxM;
|
|
if (M <= gemv_max) return gemv_launch<F>(g, scratch, scratch_bytes, stream);
|
|
/* At most eight waves a block: each has its own transpose tile in LDS. The wave count changes
|
|
* no output bit (r4d_gemm_fp8a8.hip's narrow_mw). */
|
|
const uint32_t mw = std::min(r4d_8a8_narrow_mw(N, M), 8u);
|
|
const uint32_t gx = (N + mw * 16u - 1) / (mw * 16u);
|
|
uint32_t ck = 0;
|
|
uint32_t ksplit = r4d_8a8_narrow_ksplit(split_n > 0 ? (uint32_t)split_n : N, K, M, &ck,
|
|
kIqrSplitWaves);
|
|
const long long need = ksplit > 1 ? (long long)ksplit * M * N * 4 : 0;
|
|
if (need && (!scratch || scratch_bytes < need)) { ksplit = 1; ck = 0; }
|
|
const uint32_t nt = M > 48 ? 4u : M > 32 ? 3u : M > 16 ? 2u : 1u;
|
|
unsigned int* const ctr =
|
|
ksplit > 1 && gx <= 8192u ? r4d_8a8_split_ctr(stream, g.y->data) : nullptr;
|
|
void* const dst = ksplit > 1 ? scratch : g.y->data;
|
|
const Ctx c = ctx_of(g);
|
|
const dim3 grid(gx, 1, ksplit);
|
|
int rc = RAD_E_SHAPE;
|
|
#define R4D_IQR_M64(mw_, nt_) \
|
|
rc = R4D_EMIT(stream, (iqr_m64_kernel<F, nt_, mw_>), grid, dim3(mw_ * kWave), 0, c, \
|
|
(const uint8_t*)g.a->data, (const float*)g.as->data, dst, (uint16_t*)g.y->data, \
|
|
ctr, N, K, M, (uint32_t)g.a_ld, (uint32_t)g.sa_ld, ck)
|
|
#define R4D_IQR_M64_NT(mw_) \
|
|
do { if (nt == 4u) R4D_IQR_M64(mw_, 4u); else if (nt == 3u) R4D_IQR_M64(mw_, 3u); \
|
|
else if (nt == 2u) R4D_IQR_M64(mw_, 2u); else R4D_IQR_M64(mw_, 1u); } while (0)
|
|
if (mw == 4u) R4D_IQR_M64_NT(4u);
|
|
else if (mw == 8u) R4D_IQR_M64_NT(8u);
|
|
else return R4D_NO(RAD_E_SHAPE);
|
|
#undef R4D_IQR_M64_NT
|
|
#undef R4D_IQR_M64
|
|
if (rc != RAD_OK || hipGetLastError() != hipSuccess) return R4D_NO(RAD_E_DEVICE);
|
|
if (ksplit > 1 && !ctr) {
|
|
const uint32_t n = M * N;
|
|
if (R4D_EMIT(stream, iqr_splitk_reduce, dim3((n + 255) / 256), dim3(256), 0,
|
|
(const float*)scratch, (uint16_t*)g.y->data, n, ksplit) != RAD_OK)
|
|
return R4D_NO(RAD_E_DEVICE);
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* The prefill's scratch: the int8 fragment plane, then (unless the scale buffer already is the
|
|
* int8 tile's) its bf16 tile-major scales. */
|
|
bool own_scales_serve(const Form& f) {
|
|
return (f.scale == kBf16 && f.lrb == 0) || f.scale == kRowF32; /* kRowF32: read in place */
|
|
}
|
|
long long tiled_scratch(const Form& f, long long N, long long K) {
|
|
return align256(N * K) + (own_scales_serve(f) ? 0 : align256(N * (K / kBlock) * 2));
|
|
}
|
|
|
|
template <class F>
|
|
int tiled_launch(const Operands& g, void* scratch, long long scratch_bytes, RadStream stream) {
|
|
const Form f = form(g.word);
|
|
if (!scratch || scratch_bytes < tiled_scratch(f, g.N, g.K)) return R4D_NO(RAD_E_SHAPE);
|
|
if (g.N % kPBN || (uintptr_t)g.y->data & 15u) return R4D_NO(RAD_E_SHAPE);
|
|
unsigned int* const ctl = r4d_8a8_split_ctr(stream, g.y->data);
|
|
if (!ctl || 3u * (uint32_t)(g.N / kPBN) > 8192u) return R4D_NO(RAD_E_DEVICE);
|
|
IqrPre<F> pre;
|
|
pre.c = ctx_of(g);
|
|
pre.fragp = (uint8_t*)scratch;
|
|
pre.sc = own_scales_serve(f) ? nullptr : (uint16_t*)(pre.fragp + align256(g.N * g.K));
|
|
pre.ctl = ctl;
|
|
const dim3 grid((uint32_t)((g.M + kPBM - 1) / kPBM), (uint32_t)(g.N / kPBN), 1);
|
|
pre.nbm = grid.x;
|
|
const uint16_t* const ws = pre.sc ? pre.sc : (const uint16_t*)g.bs->data;
|
|
int rc;
|
|
#define R4D_IQR_PF(rb_) \
|
|
rc = R4D_EMIT(stream, (iqr_prefill_kernel<F, rb_>), grid, dim3(kPWaves * kWave), 0, pre, \
|
|
(const uint8_t*)g.a->data, (const float*)g.as->data, (const uint8_t*)pre.fragp, \
|
|
ws, (uint16_t*)g.y->data, (uint32_t)g.M, (uint32_t)g.K, (uint32_t)g.N, \
|
|
(uint32_t)g.a_ld, (uint32_t)g.sa_ld, (uint32_t)g.nkb)
|
|
if (f.lrb >= 5) R4D_IQR_PF(32);
|
|
else if (f.lrb >= 3) R4D_IQR_PF(8);
|
|
else R4D_IQR_PF(0);
|
|
#undef R4D_IQR_PF
|
|
return rc == RAD_OK && hipGetLastError() == hipSuccess ? RAD_OK : R4D_NO(RAD_E_DEVICE);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
extern "C" int64_t r4d_rad_gemm_iqr_m64_scratch(const RadArgs* a) {
|
|
const long long M = r4d_geti_or(a, "M", 0), N = r4d_geti_or(a, "N", 0),
|
|
K = r4d_geti_or(a, "K", 0), sn = r4d_geti_or(a, "split_n", 0);
|
|
if (M < 1 || N < 1 || K < 1 || M > 64) return 0;
|
|
uint32_t ck = 0;
|
|
const uint32_t k2 = r4d_8a8_narrow_ksplit((uint32_t)(sn > 0 ? sn : N), (uint32_t)K,
|
|
(uint32_t)M, &ck, kIqrSplitWaves);
|
|
/* The band's bound covers the GEMV's split at M <= 4 too. */
|
|
const int64_t tile = k2 > 1 ? (int64_t)k2 * M * N * 4 : 0;
|
|
const uint32_t kg = gemv_ksplit((uint32_t)N, (uint32_t)(K / kBlock));
|
|
const int64_t gemv = kg > 1 ? (int64_t)kg * kGemvMaxM * N * 4 : 0;
|
|
return std::max(tile, gemv);
|
|
}
|
|
|
|
extern "C" int r4d_rad_gemm_iqr_m64(const RadArgs* a, RadStream s) {
|
|
Operands g;
|
|
const int rc = operands(a, &g);
|
|
if (rc != RAD_OK) return rc;
|
|
if (g.M > 64) return R4D_NO(RAD_E_SHAPE);
|
|
const uint32_t kw = kernel_word(g.word);
|
|
const int sn = (int)r4d_geti_or(a, "split_n", 0);
|
|
#define R4D_IQR_GO(...) \
|
|
if (kw == R4D_IQR_T(__VA_ARGS__)::word()) \
|
|
return m64_launch<R4D_IQR_T(__VA_ARGS__)>(g, a->scratch, (long long)a->scratch_bytes, sn, s);
|
|
R4D_IQR_FORMS(R4D_IQR_GO)
|
|
#undef R4D_IQR_GO
|
|
return R4D_NO(RAD_E_UNSUPPORTED);
|
|
}
|
|
|
|
extern "C" int64_t r4d_rad_gemm_iqr_tiled_scratch(const RadArgs* a) {
|
|
const long long N = r4d_geti_or(a, "N", 0), K = r4d_geti_or(a, "K", 0);
|
|
if (N < 1 || K < 1) return 0;
|
|
/* The scale part's size is the form's; the bound covers every form. */
|
|
Form f;
|
|
f.scale = kE4m3Tensor;
|
|
return (int64_t)tiled_scratch(f, N, K);
|
|
}
|
|
|
|
extern "C" int r4d_rad_gemm_iqr_tiled(const RadArgs* a, RadStream s) {
|
|
Operands g;
|
|
const int rc = operands(a, &g);
|
|
if (rc != RAD_OK) return rc;
|
|
if (g.N % kBlock) return R4D_NO(RAD_E_SHAPE);
|
|
const uint32_t kw = kernel_word(g.word);
|
|
#define R4D_IQR_GO(...) \
|
|
if (kw == R4D_IQR_T(__VA_ARGS__)::word()) \
|
|
return tiled_launch<R4D_IQR_T(__VA_ARGS__)>(g, a->scratch, (long long)a->scratch_bytes, s);
|
|
R4D_IQR_FORMS(R4D_IQR_GO)
|
|
#undef R4D_IQR_GO
|
|
return R4D_NO(RAD_E_UNSUPPORTED);
|
|
}
|