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.
1145 lines
56 KiB
Plaintext
1145 lines
56 KiB
Plaintext
/* Derived from libr4d/r4d_vsa.hip by the libhalo rename transform (2026-10-26), then ported to
|
|
* gfx1151; see libhalo/README.md. */
|
|
// Halo video sparse attention (docs/OPS.md, "Video generation"): `vsa_select`, the per-head tile
|
|
// selection of a distilled video transformer, and `attn_vsa`, the block-sparse attention that reads
|
|
// it, both at head_dim 128 over 4 x 4 x 4 tiles.
|
|
//
|
|
// The packed sequence is dense segments (text, a soundtrack's channels) and one video grid of
|
|
// F x Hp x Wp rows. Each dense segment is cut on its own into chunks of 64 rows, the prefix tiles;
|
|
// the grid into 4 x 4 x 4 boxes, the video tiles. A tile's rows are found from its number in
|
|
// closed form (vsa_tile, vsa_row), so neither op permutes rows. The geometry is a device operand,
|
|
// read by every workgroup: a video grid that does not fit the operand's rows has no tiles, and the
|
|
// tile count is cut to the selection's capacity, so no value of `segs` or `geom` reaches outside
|
|
// an operand.
|
|
//
|
|
// vsa_select, two steps a group of heads (the scratch holds the means of as many heads as fit):
|
|
// pool the f32 means of q, k (and v) over each tile, summed in the tile's (t, h, w) raster;
|
|
// select a workgroup a few query tiles: their q means, the f32 scores against every tile's k
|
|
// mean (each a sequential 128-term dot product, so a score is the reference's to the
|
|
// bit), the top k video tiles by a bisection on the scores' 32 bits with ties to the
|
|
// lower tile, written ascending, and with `coarse` the softmax over every tile times
|
|
// the v means, rounded once.
|
|
//
|
|
// attn_vsa: libr4d's r4d_attn_gen.hip S^T = K.Q^T flash attention (a lane owns a query row; P
|
|
// stays in registers, packed to f16 with the row sum taken from those same packed values), a
|
|
// workgroup two query tiles of one head walking the merge of their visible key tiles: every tile
|
|
// for a prefix query tile, the prefix tiles and its selected tiles for a video one. With `coarse`
|
|
// the output is bf16(bf16(o) + coarse * gate).
|
|
//
|
|
// attn_vsa's int8 Q.K form (`qk` "i8", operands `k_i8` and `k_scale`), attn_prefix_self's: the
|
|
// keys as quant_act_i8g codes and scales at group head_dim (per (row, head) scale amax / 127, 1
|
|
// for an all-zero row, code rint(x / scale) clamped to +-127), the query quantised the same way
|
|
// here, a score the exact integer dot product times both scales. gfx1151 has no int8 WMMA in
|
|
// halo_wmma.h's layout, so the codes are sign-extended exactly to f16 and the dot runs on the f16
|
|
// WMMA: every product and every partial sum of the 128-term integer dot is below 2^24, so the f32
|
|
// accumulator holds it exactly and the score is the same integer. The K stream halves (8 KiB a
|
|
// tile instead of 16).
|
|
//
|
|
// The fp8 P.V form (`pv` "fp8"): `v` as E4M3 codes with a scale per column (quant_cols_fp8's),
|
|
// the codes widened exactly to f16 at the fragment read (no fp8 WMMA on this part) with P as f16,
|
|
// and the epilogue multiplies by the column scale. P is therefore not rounded to E4M3 here: an
|
|
// E4M3 P would keep 3 mantissa bits, this keeps f16's 10, which is strictly closer to the
|
|
// reference -- the selftest's fp8 tolerance (4e-2) covers the difference in rounding. The V
|
|
// stream halves all the same.
|
|
#include "halo.h"
|
|
#include "halo_args.h"
|
|
#include "halo_common.h"
|
|
#include "halo_dt16.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <type_traits>
|
|
|
|
namespace {
|
|
|
|
constexpr int kHd = 128; // head_dim
|
|
constexpr int kTile = 64; // rows a tile at most: 4 x 4 x 4, and a prefix chunk
|
|
|
|
__device__ __forceinline__ int vsa_clamp(int x, int lo, int hi) {
|
|
return x < lo ? lo : (x > hi ? hi : x);
|
|
}
|
|
|
|
// The tiling of one pass, the same in every workgroup.
|
|
struct VsaGeom {
|
|
int nd; // dense segments
|
|
int P, V, N; // prefix, video and all tiles
|
|
int F, Hp, Wp; // the video grid
|
|
int nb, nc; // boxes along h and w
|
|
unsigned mb, mc; // ceil(2^32 / nb), ceil(2^32 / nc): x / n = mulhi(x, m) for x < 2^16
|
|
int vbase; // the grid's first row
|
|
int k; // video tiles a video query tile selects
|
|
};
|
|
|
|
__device__ __forceinline__ VsaGeom vsa_geom(const int* __restrict__ segs, int n_seg,
|
|
const int* __restrict__ geom, int patch, int R,
|
|
int tiles_cap, double sparsity) {
|
|
VsaGeom g;
|
|
g.nd = n_seg - 1;
|
|
int P = 0;
|
|
for (int s = 0; s < g.nd; ++s) {
|
|
const int a = vsa_clamp(segs[s], 0, R), b = vsa_clamp(segs[s + 1], a, R);
|
|
P += (b - a + kTile - 1) / kTile;
|
|
}
|
|
g.vbase = vsa_clamp(segs[g.nd], 0, R);
|
|
int F = geom[0], Hp = geom[1] / patch, Wp = geom[2] / patch;
|
|
// A grid that does not fit the rows after its first is no grid: every row index below stays
|
|
// under R in 32 bits.
|
|
bool fits = F > 0 && Hp > 0 && Wp > 0 && F <= R && Hp <= R && Wp <= R;
|
|
if (fits) {
|
|
const long long fh = (long long)F * Hp;
|
|
fits = fh <= R && fh * Wp <= (long long)(R - g.vbase);
|
|
}
|
|
if (!fits) F = Hp = Wp = 0;
|
|
g.F = F; g.Hp = Hp; g.Wp = Wp;
|
|
g.nb = (Hp + 3) >> 2;
|
|
g.nc = (Wp + 3) >> 2;
|
|
g.mb = g.nb > 1 ? 0xffffffffu / (unsigned)g.nb + 1u : 0u;
|
|
g.mc = g.nc > 1 ? 0xffffffffu / (unsigned)g.nc + 1u : 0u;
|
|
int V = ((F + 3) >> 2) * g.nb * g.nc;
|
|
P = min(P, tiles_cap);
|
|
V = min(V, tiles_cap - P);
|
|
g.P = P; g.V = V; g.N = P + V;
|
|
g.k = 0;
|
|
if (V > 0) {
|
|
const double keep = (1.0 - sparsity) * (double)V;
|
|
g.k = max(1, min((int)ceil(keep), V));
|
|
}
|
|
return g;
|
|
}
|
|
|
|
// A tile's rows: local index kk (its raster, w fastest) is row base + dt sT + dh sH + dw, with
|
|
// (dt, dh, dw) from kk by two divisions by extents of at most 4, done as (x * m) >> 8 with
|
|
// m = ceil(256 / n), exact for x < 64. A prefix tile is one run (nw 64, so dt = dh = 0).
|
|
struct VsaTile {
|
|
int base, sT, sH, nw, nh, mw, mh, size;
|
|
};
|
|
|
|
__device__ __forceinline__ int vsa_magic(int n) { return (256 + n - 1) / n; }
|
|
|
|
__device__ __forceinline__ VsaTile vsa_tile(const VsaGeom& g, const int* __restrict__ segs,
|
|
int R, int j) {
|
|
VsaTile d;
|
|
if (j < g.P) {
|
|
d.base = 0;
|
|
d.size = 1;
|
|
for (int s = 0, jj = j; s < g.nd; ++s) {
|
|
const int a = vsa_clamp(segs[s], 0, R), b = vsa_clamp(segs[s + 1], a, R);
|
|
const int nt = (b - a + kTile - 1) / kTile;
|
|
if (jj < nt) {
|
|
d.base = a + jj * kTile;
|
|
d.size = min(kTile, b - d.base);
|
|
break;
|
|
}
|
|
jj -= nt;
|
|
}
|
|
d.sT = 0; d.sH = 0; d.nw = kTile; d.nh = 1; d.mw = 4; d.mh = 256;
|
|
return d;
|
|
}
|
|
// Tile numbers are below 2^16 (the capacity is), where the multiply-high is the quotient; a
|
|
// divisor of 1 has no 32-bit reciprocal and is the identity.
|
|
const int v = j - g.P;
|
|
const int ab = g.nc > 1 ? (int)__umulhi((unsigned)v, g.mc) : v, c = v - ab * g.nc;
|
|
const int a = g.nb > 1 ? (int)__umulhi((unsigned)ab, g.mb) : ab, b = ab - a * g.nb;
|
|
const int t0 = 4 * a, h0 = 4 * b, w0 = 4 * c;
|
|
const int nt = min(4, g.F - t0), nh = min(4, g.Hp - h0), nw = min(4, g.Wp - w0);
|
|
d.base = g.vbase + (t0 * g.Hp + h0) * g.Wp + w0;
|
|
d.sT = g.Hp * g.Wp;
|
|
d.sH = g.Wp;
|
|
d.nw = nw; d.nh = nh;
|
|
d.mw = vsa_magic(nw); d.mh = vsa_magic(nh);
|
|
d.size = nt * nh * nw;
|
|
return d;
|
|
}
|
|
|
|
__device__ __forceinline__ int vsa_row(const VsaTile& d, int kk) {
|
|
const int q1 = (kk * d.mw) >> 8, dw = kk - q1 * d.nw;
|
|
const int dt = (q1 * d.mh) >> 8, dh = q1 - dt * d.nh;
|
|
return d.base + dt * d.sT + dh * d.sH + dw;
|
|
}
|
|
|
|
// Four bf16 (one uint2) added in order to four f32 sums.
|
|
__device__ __forceinline__ void vsa_acc4(float4& s, uint2 u) {
|
|
s.x = s.x + bf16w_lo(u.x); s.y = s.y + bf16w_hi(u.x);
|
|
s.z = s.z + bf16w_lo(u.y); s.w = s.w + bf16w_hi(u.y);
|
|
}
|
|
|
|
// The sums of q, k and (when `xv` is given) v over a tile's rows, one head's four dims each, in the
|
|
// tile's raster: the three share each row's address, and a batch of four rows' loads is issued
|
|
// before its adds, which stay in row order.
|
|
__device__ __forceinline__ void vsa_tile_sum3(const uint16_t* __restrict__ xq, int q_ld,
|
|
const uint16_t* __restrict__ xk, int k_ld,
|
|
const uint16_t* __restrict__ xv, int v_ld,
|
|
const VsaTile& d, float4& sq, float4& sk,
|
|
float4& sv) {
|
|
sq = sk = sv = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
|
int kk = 0;
|
|
for (; kk + 4 <= d.size; kk += 4) {
|
|
uint2 uq[4], uk[4], uv[4];
|
|
#pragma unroll
|
|
for (int r = 0; r < 4; ++r) {
|
|
const long row = vsa_row(d, kk + r);
|
|
uq[r] = *(const uint2*)(xq + row * q_ld);
|
|
uk[r] = *(const uint2*)(xk + row * k_ld);
|
|
if (xv) uv[r] = *(const uint2*)(xv + row * v_ld);
|
|
}
|
|
#pragma unroll
|
|
for (int r = 0; r < 4; ++r) {
|
|
vsa_acc4(sq, uq[r]);
|
|
vsa_acc4(sk, uk[r]);
|
|
if (xv) vsa_acc4(sv, uv[r]);
|
|
}
|
|
}
|
|
for (; kk < d.size; ++kk) {
|
|
const long row = vsa_row(d, kk);
|
|
vsa_acc4(sq, *(const uint2*)(xq + row * q_ld));
|
|
vsa_acc4(sk, *(const uint2*)(xk + row * k_ld));
|
|
if (xv) vsa_acc4(sv, *(const uint2*)(xv + row * v_ld));
|
|
}
|
|
}
|
|
|
|
// A score as an unsigned key that orders like the float, -0 folded onto +0 first so that the two
|
|
// zeros tie as they do as floats.
|
|
__device__ __forceinline__ uint32_t vsa_key(float s) {
|
|
const uint32_t u = __float_as_uint(s + 0.0f);
|
|
return (u & 0x80000000u) ? ~u : (u | 0x80000000u);
|
|
}
|
|
__device__ __forceinline__ float vsa_unkey(uint32_t k) {
|
|
return __uint_as_float((k & 0x80000000u) ? (k & 0x7fffffffu) : ~k);
|
|
}
|
|
|
|
__device__ __forceinline__ int vsa_wave_count(int c) { return (int)halo_wave_sum((float)c); }
|
|
|
|
// ---------------------------------------------------------------------------- vsa_select: pool
|
|
// The q, k (and v) means of every tile of G heads into the scratch: k as [G][32][tiles_cap]
|
|
// float4, the dims in groups of four outermost, so the score loop's lanes (consecutive tiles) read
|
|
// 512 contiguous bytes; q and v as [G][tiles_cap][128] f32 rows, read by a wave a tile and by the
|
|
// coarse product's lanes (consecutive dims). A workgroup a tile and four heads, a lane four dims
|
|
// of one head. The sums are a dependent chain a lane, so they run here, at this kernel's
|
|
// occupancy, rather than in the select kernel's.
|
|
__global__ __launch_bounds__(128) void vsa_pool_kernel(
|
|
const uint16_t* __restrict__ gq, const uint16_t* __restrict__ gk,
|
|
const uint16_t* __restrict__ gv, int q_ld, int k_ld, int v_ld,
|
|
const int* __restrict__ segs, int n_seg, const int* __restrict__ geom, int patch, int R,
|
|
int tiles_cap, int h0, int G, float4* __restrict__ kpT, float4* __restrict__ qp,
|
|
float4* __restrict__ vp) {
|
|
const VsaGeom g = vsa_geom(segs, n_seg, geom, patch, R, tiles_cap, 0.0);
|
|
const int j = blockIdx.x;
|
|
const int gh = blockIdx.y * 4 + (threadIdx.x >> 5), d4 = threadIdx.x & 31;
|
|
if (j >= g.N || gh >= G) return;
|
|
const VsaTile d = vsa_tile(g, segs, R, j);
|
|
const long col = (long)(h0 + gh) * kHd + 4 * d4;
|
|
const float n = (float)d.size;
|
|
float4 sq, sk, sv;
|
|
vsa_tile_sum3(gq + col, q_ld, gk + col, k_ld, vp ? gv + col : nullptr, v_ld, d, sq, sk, sv);
|
|
const long rw = ((long)gh * tiles_cap + j) * 32 + d4;
|
|
kpT[((long)gh * 32 + d4) * tiles_cap + j] = make_float4(sk.x / n, sk.y / n, sk.z / n, sk.w / n);
|
|
qp[rw] = make_float4(sq.x / n, sq.y / n, sq.z / n, sq.w / n);
|
|
if (vp) vp[rw] = make_float4(sv.x / n, sv.y / n, sv.z / n, sv.w / n);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------- vsa_select: select
|
|
// QT query tiles of one head a workgroup of eight waves; NMAX bounds the tile count (the scores
|
|
// of every tile for each query tile are kept in LDS, as order keys, then as softmax weights).
|
|
//
|
|
// 1. q means of the QT tiles from the scratch into LDS.
|
|
// 2. Scores: a thread a key tile and all QT query tiles, the k mean read four dims at a time
|
|
// from the transposed scratch (eight loads in flight), each dot product summed over the
|
|
// dims in order.
|
|
// 3. Selection, a wave a query tile: the k-th largest key by bisection on its 32 bits (T is the
|
|
// largest value with at least k keys >= T), then one ascending pass taking the keys above T
|
|
// and the first (k - #above) keys equal to it, compacted by ballots.
|
|
// 4. Coarse: the wave's softmax over every tile (max-subtracted, f32) into the same LDS row,
|
|
// then p times the v means summed over the tiles, a thread a dim and every query tile of
|
|
// the group over half of them, eight tiles' loads in flight.
|
|
template <int QT, int NMAX>
|
|
__global__ __launch_bounds__(256) void vsa_select_kernel(
|
|
const int* __restrict__ segs, int n_seg, const int* __restrict__ geom, int patch, int R,
|
|
int tiles_cap, double sparsity, float scale, int h0, const float4* __restrict__ kpT,
|
|
const float4* __restrict__ qp, const float* __restrict__ vp, int* __restrict__ sel,
|
|
long long sel_hs, long long sel_ts, int k_cap, uint16_t* __restrict__ coarse,
|
|
long long c_hs, long long c_ts) {
|
|
static_assert(QT <= 8, "a wave a query tile");
|
|
__shared__ uint32_t sS[QT * NMAX];
|
|
__shared__ float4 sQ[QT * 32];
|
|
|
|
const VsaGeom g = vsa_geom(segs, n_seg, geom, patch, R, tiles_cap, sparsity);
|
|
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5;
|
|
const int i0 = blockIdx.x * QT, gh = blockIdx.y, hq = h0 + gh;
|
|
if (i0 >= g.N) return;
|
|
|
|
// 1. the q means
|
|
if (wave < QT) {
|
|
const int i = i0 + wave;
|
|
sQ[wave * 32 + lane] = i < g.N ? qp[((long)gh * tiles_cap + i) * 32 + lane]
|
|
: make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
|
}
|
|
__syncthreads();
|
|
|
|
// 2. the scores
|
|
const float4* kb = kpT + (long)gh * 32 * tiles_cap;
|
|
for (int j0 = 0; j0 < g.N; j0 += 256) {
|
|
const int j = j0 + tid, jc = min(j, g.N - 1);
|
|
float acc[QT];
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q) acc[q] = 0.0f;
|
|
#pragma unroll 1
|
|
for (int d0 = 0; d0 < 32; d0 += 8) {
|
|
float4 kv[8];
|
|
#pragma unroll
|
|
for (int u = 0; u < 8; ++u) kv[u] = kb[(long)(d0 + u) * tiles_cap + jc];
|
|
// A fence a step: the eight loads above are what is hoisted, not the LDS reads of
|
|
// every step (eight query tiles' worth each).
|
|
#pragma unroll
|
|
for (int u = 0; u < 8; ++u) {
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q) {
|
|
const float4 qv = sQ[q * 32 + d0 + u];
|
|
acc[q] = acc[q] + qv.x * kv[u].x;
|
|
acc[q] = acc[q] + qv.y * kv[u].y;
|
|
acc[q] = acc[q] + qv.z * kv[u].z;
|
|
acc[q] = acc[q] + qv.w * kv[u].w;
|
|
}
|
|
sched_barrier();
|
|
}
|
|
}
|
|
if (j < g.N) {
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q) sS[q * NMAX + j] = vsa_key(acc[q] * scale);
|
|
}
|
|
}
|
|
__syncthreads();
|
|
|
|
// 3. the selection
|
|
const int i = i0 + wave;
|
|
uint32_t* row = sS + wave * NMAX;
|
|
if (wave < QT && i < g.N && i >= g.P && g.V > 0) {
|
|
const int k = g.k;
|
|
uint32_t T = 0;
|
|
for (int b = 31; b >= 0; --b) {
|
|
const uint32_t cand = T | (1u << b);
|
|
int c = 0;
|
|
for (int j = g.P + lane; j < g.N; j += 32) c += row[j] >= cand ? 1 : 0;
|
|
if (vsa_wave_count(c) >= k) T = cand;
|
|
}
|
|
int c = 0;
|
|
for (int j = g.P + lane; j < g.N; j += 32) c += row[j] > T ? 1 : 0;
|
|
const int need = k - vsa_wave_count(c);
|
|
int* out = sel + (long long)hq * sel_hs + (long long)i * sel_ts;
|
|
const int kw = min(k, k_cap);
|
|
const uint32_t below = (1u << lane) - 1u;
|
|
int eq_seen = 0, written = 0;
|
|
for (int j0 = g.P; j0 < g.N && written < kw; j0 += 32) {
|
|
const int j = j0 + lane;
|
|
const uint32_t key = j < g.N ? row[j] : 0u;
|
|
const bool gt = j < g.N && key > T, eq = j < g.N && key == T;
|
|
const uint32_t em = (uint32_t)__builtin_amdgcn_ballot_w32(eq);
|
|
const bool take = gt || (eq && eq_seen + (int)__popc(em & below) < need);
|
|
const uint32_t tm = (uint32_t)__builtin_amdgcn_ballot_w32(take);
|
|
const int pos = written + (int)__popc(tm & below);
|
|
if (take && pos < kw) out[pos] = j;
|
|
written += (int)__popc(tm);
|
|
eq_seen += (int)__popc(em);
|
|
}
|
|
}
|
|
if (!coarse) return;
|
|
|
|
// 4. the coarse branch
|
|
if (wave < QT && i < g.N) {
|
|
float mx = -INFINITY;
|
|
for (int j = lane; j < g.N; j += 32) mx = fmaxf(mx, vsa_unkey(row[j]));
|
|
mx = halo_wave_max(mx);
|
|
float sum = 0.0f;
|
|
for (int j = lane; j < g.N; j += 32) {
|
|
const float e = expf(vsa_unkey(row[j]) - mx);
|
|
row[j] = __float_as_uint(e);
|
|
sum += e;
|
|
}
|
|
sum = halo_wave_sum(sum);
|
|
for (int j = lane; j < g.N; j += 32)
|
|
row[j] = __float_as_uint(__uint_as_float(row[j]) / sum);
|
|
}
|
|
__syncthreads();
|
|
// A thread a dim and every query tile of the group over half the tiles, so each v load feeds
|
|
// QT products; the second half's sums meet the first's through LDS (sQ is free once the
|
|
// scores are done).
|
|
const int dim = tid & (kHd - 1), half = tid >> 7;
|
|
const int jm = (g.N + 1) / 2, j1 = half ? g.N : jm;
|
|
float acc[QT];
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q) acc[q] = 0.0f;
|
|
const float* vb = vp + (long)gh * tiles_cap * kHd + dim;
|
|
int j = half ? jm : 0;
|
|
for (; j + 8 <= j1; j += 8) {
|
|
float vv[8];
|
|
#pragma unroll
|
|
for (int u = 0; u < 8; ++u) vv[u] = vb[(long)(j + u) * kHd];
|
|
#pragma unroll
|
|
for (int u = 0; u < 8; ++u) {
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q)
|
|
acc[q] = acc[q] + __uint_as_float(sS[q * NMAX + j + u]) * vv[u];
|
|
sched_barrier();
|
|
}
|
|
}
|
|
for (; j < j1; ++j) {
|
|
const float vv = vb[(long)j * kHd];
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q) acc[q] = acc[q] + __uint_as_float(sS[q * NMAX + j]) * vv;
|
|
}
|
|
float* part = (float*)sQ;
|
|
if (half) {
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q) part[q * kHd + dim] = acc[q];
|
|
}
|
|
__syncthreads();
|
|
if (half) return;
|
|
#pragma unroll
|
|
for (int q = 0; q < QT; ++q) {
|
|
const int iq = i0 + q;
|
|
if (iq < g.N)
|
|
coarse[(long long)hq * c_hs + (long long)iq * c_ts + dim] =
|
|
f32_to_bf16(acc[q] + part[q * kHd + dim]);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------- attn_vsa
|
|
// A workgroup NQT query tiles of one head, four waves of 16 query rows each (a tile's raster).
|
|
// Each query tile's visible key tiles are ascending (the prefix tiles, then its selection, which
|
|
// vsa_select writes ascending; every tile for a prefix query tile), so the workgroup walks their
|
|
// merge: a key tile is staged once for every query tile of the group that sees it, and the waves
|
|
// of the others skip its arithmetic.
|
|
//
|
|
// The kernel is bound by the stream of staged K/V tiles, which a random selection draws from the
|
|
// whole head (19 MB at 1344 x 768, past the 8 MB L2): two query tiles a workgroup stage 17%
|
|
// fewer tiles than one at random selections and a third fewer where neighbouring tiles select
|
|
// alike, and four lose more to idle waves than they save. Staging is halo_attn_ps_kernel's (K
|
|
// rows into LDS, V through the transposing load into V^T), one tile ahead: the next tile's loads
|
|
// are issued into registers before the current tile's arithmetic and land in LDS after it. A key
|
|
// past its tile's size reads the tile's last row and is masked; 16-key blocks wholly past it are
|
|
// skipped, and full tiles take the unmasked arithmetic. LDS rows are not padded: 16-byte chunk c
|
|
// of row r is stored at c ^ (r & 7), so the eight lanes of a 16-byte access reach eight
|
|
// different bank groups, and the two tiles take 32 KiB (four workgroups a WGP).
|
|
//
|
|
// gfx1151 (halo_dt16.h). A lane's fragment row is still (lane & 15) -- the query for the scores,
|
|
// the head dim for P.V -- but its element e is k = e over the WHOLE sixteen-wide run rather than
|
|
// k = 8*(lane>>4) + e. So, at every gather site: the half-wave term leaves the address, two
|
|
// 16-byte chunks are read instead of one, the LDS swizzle loses its h (a lane now needs chunks
|
|
// 2t and 2t+1 of its row, XORed with (row & 7) alone), a P fragment is assembled across the
|
|
// half-pair with p16w(), the per-element scale of the int8 form is indexed 2e + h, the 3-D
|
|
// store is a scatter at head dim 2*e + h, and the int8 / E4M3 byte tiles are widened exactly to
|
|
// f16 at the fragment read (this part has no fp8 or int8 WMMA). f16 and not bf16 throughout:
|
|
// both are exact widenings of the stored formats and f16's pack is one instruction.
|
|
constexpr int kNone = 0x7fffffff;
|
|
|
|
typedef DT16<1> VsD; // the 16-bit operand of every form here
|
|
typedef VsD::frag vsa_frag16;
|
|
|
|
// quant_act_i8g codes for four values against `inv` = 1 / scale, packed into one dword.
|
|
__device__ __forceinline__ uint32_t vsa_q8x4(float a, float b, float c, float d, float inv) {
|
|
auto q = [inv](float x) {
|
|
return (uint32_t)((int)rintf(fminf(fmaxf(x * inv, -127.0f), 127.0f)) & 0xff);
|
|
};
|
|
return q(a) | (q(b) << 8) | (q(c) << 16) | (q(d) << 24);
|
|
}
|
|
__device__ __forceinline__ float vsa_amax8(uint4 u) {
|
|
return fmaxf(fmaxf(fmaxf(fabsf(bf16w_lo(u.x)), fabsf(bf16w_hi(u.x))),
|
|
fmaxf(fabsf(bf16w_lo(u.y)), fabsf(bf16w_hi(u.y)))),
|
|
fmaxf(fmaxf(fabsf(bf16w_lo(u.z)), fabsf(bf16w_hi(u.z))),
|
|
fmaxf(fabsf(bf16w_lo(u.w)), fabsf(bf16w_hi(u.w)))));
|
|
}
|
|
__device__ __forceinline__ int vsa_sw(int r, int c) { return c ^ (r & 7); }
|
|
|
|
// Sixteen int8 codes as the lane's eight f16 dwords, sign-extended (exact: |code| <= 127). The
|
|
// rows of the int8 tile are HD + 8 bytes, so an odd row is 8-byte aligned: two uint2 loads.
|
|
__device__ __forceinline__ void vsa_i8x16w(const uint32_t cw[4], uint32_t w8[8]) {
|
|
#pragma unroll
|
|
for (int q = 0; q < 4; ++q) {
|
|
const uint32_t d = cw[q];
|
|
w8[q * 2 + 0] = VsD::pk((float)(int)(int8_t)(d & 0xffu),
|
|
(float)(int)(int8_t)((d >> 8) & 0xffu));
|
|
w8[q * 2 + 1] = VsD::pk((float)(int)(int8_t)((d >> 16) & 0xffu),
|
|
(float)(int)(int8_t)((d >> 24) & 0xffu));
|
|
}
|
|
}
|
|
__device__ __forceinline__ void vsa_i8x16_to_f16x8w(const int8_t* __restrict__ p, uint32_t w8[8]) {
|
|
const uint2 a = *(const uint2*)p, b = *(const uint2*)(p + 8);
|
|
const uint32_t cw[4] = { a.x, a.y, b.x, b.y };
|
|
vsa_i8x16w(cw, w8);
|
|
}
|
|
// The same for the query: sixteen bf16 values (two uint4) quantised against `inv`.
|
|
__device__ __forceinline__ void vsa_q16w(uint4 u0, uint4 u1, float inv, uint32_t w8[8]) {
|
|
const uint32_t cw[4] = {
|
|
vsa_q8x4(bf16w_lo(u0.x), bf16w_hi(u0.x), bf16w_lo(u0.y), bf16w_hi(u0.y), inv),
|
|
vsa_q8x4(bf16w_lo(u0.z), bf16w_hi(u0.z), bf16w_lo(u0.w), bf16w_hi(u0.w), inv),
|
|
vsa_q8x4(bf16w_lo(u1.x), bf16w_hi(u1.x), bf16w_lo(u1.y), bf16w_hi(u1.y), inv),
|
|
vsa_q8x4(bf16w_lo(u1.z), bf16w_hi(u1.z), bf16w_lo(u1.w), bf16w_hi(u1.w), inv) };
|
|
vsa_i8x16w(cw, w8);
|
|
}
|
|
// Sixteen E4M3 codes of a V^T row as eight f16 dwords (halo_dt16.h's exact widen); the rows are
|
|
// TILE + 8 bytes, so an odd row is 8-byte aligned.
|
|
__device__ __forceinline__ void vsa_fp8x16_to_f16x8w(const uint8_t* __restrict__ p,
|
|
uint32_t w8[8]) {
|
|
const uint2 a = *(const uint2*)p, b = *(const uint2*)(p + 8);
|
|
fp8x16_to_16x8w<VsD>(make_uint4(a.x, a.y, b.x, b.y), w8);
|
|
}
|
|
|
|
template <int NQT, bool I8, bool PV8>
|
|
__global__ __launch_bounds__(NQT * 128)
|
|
void vsa_attn_kernel(
|
|
const uint16_t* __restrict__ gq, const uint16_t* __restrict__ gk,
|
|
const uint16_t* __restrict__ gv, uint16_t* __restrict__ go,
|
|
const uint16_t* __restrict__ gc, const uint16_t* __restrict__ gg,
|
|
const int* __restrict__ segs, int n_seg, const int* __restrict__ geom, int patch, int R,
|
|
int tiles_cap, double sparsity, const int* __restrict__ sel, long long sel_hs,
|
|
long long sel_ts, int k_cap, long long c_hs, long long c_ts, float scale, int q_ld, int k_ld,
|
|
int v_ld, int g_ld, int o_ld, const int8_t* __restrict__ gk8, const float* __restrict__ gks,
|
|
int k8_ld, int ks_ld, const float* __restrict__ gvs) {
|
|
typedef VsD D;
|
|
typedef vsa_frag16 frag16;
|
|
|
|
constexpr int HD = kHd;
|
|
constexpr int TILE = kTile;
|
|
constexpr int NTHR = NQT * 128;
|
|
constexpr int NKS = HD / 16;
|
|
constexpr int DG = HD / 8; // 16-byte chunks of a K row
|
|
constexpr int KP = TILE * DG / NTHR; // 16-byte K units a thread, and V units
|
|
constexpr int VP = KP;
|
|
constexpr int VP8 = VP / 2; // the same at E4M3: 16 dims of 8 keys a unit
|
|
constexpr int MT = TILE / 16;
|
|
constexpr float GROW = D::GROW;
|
|
|
|
// The int8 key tile is halo_attn_ps_kernel's: HD bytes a row padded by 8, so a fragment read's
|
|
// sixteen rows hit distinct bank pairs.
|
|
constexpr int KSTR8 = HD + 8;
|
|
// The E4M3 V^T tile: HD rows of TILE key bytes padded by 8.
|
|
constexpr int VSTR8 = TILE + 8;
|
|
__shared__ __align__(16) uint16_t sK[I8 ? 8 : TILE * HD];
|
|
__shared__ __align__(16) int8_t sK8[I8 ? TILE * KSTR8 : 16];
|
|
__shared__ __align__(16) float sKs[I8 ? TILE : 4];
|
|
__shared__ __align__(16) uint16_t sV[PV8 ? 8 : HD * TILE];
|
|
__shared__ __align__(16) uint8_t sV8[PV8 ? HD * VSTR8 : 16];
|
|
|
|
const VsaGeom g = vsa_geom(segs, n_seg, geom, patch, R, tiles_cap, sparsity);
|
|
const int i0 = blockIdx.x * NQT, hq = blockIdx.y;
|
|
if (i0 >= g.N) return;
|
|
|
|
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
|
|
const int c = lane & 15, h = lane >> 4, swz = c & 7;
|
|
const int qt = warp >> 2, wq = warp & 3;
|
|
const int i = i0 + qt;
|
|
const bool tval = i < g.N;
|
|
const VsaTile qd = vsa_tile(g, segs, R, tval ? i : i0);
|
|
const int qpos = wq * 16 + c;
|
|
const bool live = tval && qpos < qd.size;
|
|
const bool wlive = tval && wq * 16 < qd.size;
|
|
const int qrow = vsa_row(qd, qpos < qd.size ? qpos : qd.size - 1);
|
|
|
|
// The query: f16 fragments pre-multiplied by the softmax scale (this lane holds all sixteen
|
|
// dims of each k-tile), or int8 codes against the row's absmax widened to f16, with the scale
|
|
// in `qs`, applied to the integer score afterwards.
|
|
const float mul = scale * 1.44269504089f;
|
|
frag16 qf[NKS];
|
|
float qs = mul;
|
|
{
|
|
const uint16_t* qp = gq + (long)qrow * q_ld + (long)hq * HD;
|
|
if constexpr (I8) {
|
|
float am = 0.0f;
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t) {
|
|
am = fmaxf(am, vsa_amax8(*(const uint4*)(qp + 16 * t)));
|
|
am = fmaxf(am, vsa_amax8(*(const uint4*)(qp + 16 * t + 8)));
|
|
}
|
|
am = fmaxf(am, swap16(am)); // a value every lane of the half-pair already holds
|
|
const float sq = am > 0.0f ? am * (1.0f / 127.0f) : 1.0f;
|
|
const float inv = 1.0f / sq;
|
|
qs = sq * mul;
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t) {
|
|
uint32_t w8[8];
|
|
vsa_q16w(*(const uint4*)(qp + 16 * t), *(const uint4*)(qp + 16 * t + 8), inv, w8);
|
|
qf[t] = D::mk(w8);
|
|
}
|
|
} else {
|
|
// One k-tile's two uint4 at a time: a tile of 16 bf16 values scaled into the lane's eight f16
|
|
// dwords. Holding all NKS tiles at once cost 64 VGPRs for nothing (the loads are L1 hits).
|
|
#define VSA_QCVT(A, B) \
|
|
do { \
|
|
const uint4 a_ = (A), b_ = (B); \
|
|
const uint32_t w8[8] = { \
|
|
D::pk(bf16w_lo(a_.x) * mul, bf16w_hi(a_.x) * mul), \
|
|
D::pk(bf16w_lo(a_.y) * mul, bf16w_hi(a_.y) * mul), \
|
|
D::pk(bf16w_lo(a_.z) * mul, bf16w_hi(a_.z) * mul), \
|
|
D::pk(bf16w_lo(a_.w) * mul, bf16w_hi(a_.w) * mul), \
|
|
D::pk(bf16w_lo(b_.x) * mul, bf16w_hi(b_.x) * mul), \
|
|
D::pk(bf16w_lo(b_.y) * mul, bf16w_hi(b_.y) * mul), \
|
|
D::pk(bf16w_lo(b_.z) * mul, bf16w_hi(b_.z) * mul), \
|
|
D::pk(bf16w_lo(b_.w) * mul, bf16w_hi(b_.w) * mul) }; \
|
|
qf[t] = D::mk(w8); \
|
|
} while (0)
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t)
|
|
VSA_QCVT(*(const uint4*)(qp + 16 * t), *(const uint4*)(qp + 16 * t + 8));
|
|
#undef VSA_QCVT
|
|
}
|
|
}
|
|
|
|
float m_ref = -3.0e38f, soff = 3.0e38f, l_i = 0.0f;
|
|
v8f acc[NKS];
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t) acc[t] = (v8f){0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
// The key-tile lists: query tile q of the group sees its entries e < len[q], entry e being e
|
|
// itself while e < P or the query tile is a prefix tile, else its selection's slot e - P.
|
|
const int ks = min(g.k, k_cap);
|
|
int len[NQT], pos[NQT], cur[NQT];
|
|
const int* srow[NQT];
|
|
#pragma unroll
|
|
for (int q = 0; q < NQT; ++q) {
|
|
const int iq = i0 + q;
|
|
len[q] = iq >= g.N ? 0 : iq < g.P ? g.N : g.P + ks;
|
|
srow[q] = sel + (long long)hq * sel_hs + (long long)(iq < g.N ? iq : 0) * sel_ts;
|
|
pos[q] = 0;
|
|
}
|
|
auto entry = [&](int q, int e) -> int {
|
|
if (e >= len[q]) return kNone;
|
|
if (e < g.P || i0 + q < g.P) return e;
|
|
return vsa_clamp(srow[q][e - g.P], 0, g.N - 1);
|
|
};
|
|
#pragma unroll
|
|
for (int q = 0; q < NQT; ++q) cur[q] = entry(q, 0);
|
|
// The next tile of the merge and the query tiles that see it, as a bit mask.
|
|
auto advance = [&](int& j, int& m) {
|
|
j = kNone;
|
|
#pragma unroll
|
|
for (int q = 0; q < NQT; ++q) j = min(j, cur[q]);
|
|
m = 0;
|
|
if (j == kNone) return;
|
|
#pragma unroll
|
|
for (int q = 0; q < NQT; ++q)
|
|
if (cur[q] == j) { m |= 1 << q; cur[q] = entry(q, ++pos[q]); }
|
|
};
|
|
|
|
// Staging: wave w stages keys KW w .. KW w + KW - 1, their K rows and V rows. Lane l decodes
|
|
// the row of key KW w + l % KW once. Its V units read that key: eight-lane group g of the
|
|
// wave takes key group g % (KW / 8) and dim groups g / (KW / 8) + (32 / KW) r, the eight
|
|
// lanes of a group the group's eight keys, as the transposing load wants. Its K units take
|
|
// keys KW w + l / 16 + 2 p at dims 8 (l % 16), the row from lane l / 16 + 2 p by a permute.
|
|
// So a wave reads whole rows, and a row is decoded once a tile (decoding it per unit cost a
|
|
// tenth of the kernel). An int8 K row is eight 16-byte units: keys KW w + l / 8 + 4 p, the
|
|
// row from lane l / 8 + 4 p, and lane l < KW reads its key's scale. An E4M3 V unit is the same
|
|
// transposing load over pairs of codes (16 dims of the group's 8 keys): lane j receives dims
|
|
// 2j and 2j + 1 interleaved, and four byte permutes split them into two V^T rows of 8 keys.
|
|
constexpr int NW = NTHR / 32;
|
|
constexpr int KW = TILE / NW;
|
|
constexpr int K8P = KW / 4;
|
|
static_assert(KW == 2 * KP && VP == KW / 2 && (KW == 8 || KW == 16), "a wave's keys");
|
|
const int kw0 = KW * warp, lg = lane >> 3, vj = lane & 7;
|
|
const int vkg = kw0 / 8 + lg % (KW / 8), vdg = lg / (KW / 8);
|
|
const uint16_t* kbase = gk + (long)hq * HD + (lane & 15) * 8;
|
|
const int8_t* k8base = I8 ? gk8 + (long)hq * HD + (lane & 7) * 16 : nullptr;
|
|
const uint16_t* vbase = gv + (long)hq * HD;
|
|
const uint8_t* vbase8 = (const uint8_t*)gv + (long)hq * HD;
|
|
// ext_vector_type staging registers: an array of HIP's uint4 carried across the loop is kept
|
|
// in scratch (halo_common.h).
|
|
halo_u32x4 kr[I8 ? K8P : KP], vr[PV8 ? VP8 : VP];
|
|
float ksr = 0.0f;
|
|
auto load = [&](const VsaTile& kd) {
|
|
const int kk = kw0 + lane % KW;
|
|
const int row = vsa_row(kd, kk < kd.size ? kk : kd.size - 1);
|
|
if constexpr (PV8) {
|
|
const uint8_t* v0 = vbase8 + (long)row * v_ld;
|
|
#pragma unroll
|
|
for (int r = 0; r < VP8; ++r)
|
|
vr[r] = __builtin_bit_cast(halo_u32x4,
|
|
load_tr_b128(v0 + (vdg + (32 / KW) * r) * 16));
|
|
} else {
|
|
const uint16_t* v0 = vbase + (long)row * v_ld;
|
|
#pragma unroll
|
|
for (int r = 0; r < VP; ++r)
|
|
vr[r] = __builtin_bit_cast(halo_u32x4, load_tr_b128(v0 + (vdg + (32 / KW) * r) * 8));
|
|
}
|
|
if constexpr (I8) {
|
|
#pragma unroll
|
|
for (int p = 0; p < K8P; ++p) {
|
|
const int krow = __shfl(row, (lane >> 3) + 4 * p);
|
|
kr[p] = *(const halo_u32x4*)(k8base + (long)krow * k8_ld);
|
|
}
|
|
if (lane < KW) ksr = gks[(long)row * ks_ld + hq];
|
|
} else {
|
|
#pragma unroll
|
|
for (int p = 0; p < KP; ++p) {
|
|
const int krow = __shfl(row, (lane >> 4) + 2 * p);
|
|
kr[p] = *(const halo_u32x4*)(kbase + (long)krow * k_ld);
|
|
}
|
|
}
|
|
};
|
|
auto store = [&]() {
|
|
if constexpr (PV8) {
|
|
#pragma unroll
|
|
for (int r = 0; r < VP8; ++r) {
|
|
const int row = (vdg + (32 / KW) * r) * 16 + 2 * vj;
|
|
const halo_u32x4 w = vr[r];
|
|
*(uint2*)&sV8[row * VSTR8 + vkg * 8] =
|
|
make_uint2(__builtin_amdgcn_perm(w.y, w.x, 0x06040200u),
|
|
__builtin_amdgcn_perm(w.w, w.z, 0x06040200u));
|
|
*(uint2*)&sV8[(row + 1) * VSTR8 + vkg * 8] =
|
|
make_uint2(__builtin_amdgcn_perm(w.y, w.x, 0x07050301u),
|
|
__builtin_amdgcn_perm(w.w, w.z, 0x07050301u));
|
|
}
|
|
} else {
|
|
#pragma unroll
|
|
for (int r = 0; r < VP; ++r) {
|
|
const int row = (vdg + (32 / KW) * r) * 8 + vj;
|
|
const halo_u32x4 v = vr[r];
|
|
*(halo_u32x4*)&sV[row * TILE + vsa_sw(row, vkg) * 8] =
|
|
__builtin_bit_cast(halo_u32x4,
|
|
make_uint4(D::from_bf16w(v.x), D::from_bf16w(v.y),
|
|
D::from_bf16w(v.z), D::from_bf16w(v.w)));
|
|
}
|
|
}
|
|
if constexpr (I8) {
|
|
#pragma unroll
|
|
for (int p = 0; p < K8P; ++p) {
|
|
int8_t* d = &sK8[(kw0 + (lane >> 3) + 4 * p) * KSTR8 + (lane & 7) * 16];
|
|
*(uint2*)d = make_uint2(kr[p].x, kr[p].y);
|
|
*(uint2*)(d + 8) = make_uint2(kr[p].z, kr[p].w);
|
|
}
|
|
if (lane < KW) sKs[kw0 + lane] = ksr;
|
|
} else {
|
|
#pragma unroll
|
|
for (int p = 0; p < KP; ++p) {
|
|
const int key = kw0 + (lane >> 4) + 2 * p;
|
|
const halo_u32x4 r = kr[p];
|
|
*(halo_u32x4*)&sK[key * HD + vsa_sw(key, lane & 15) * 8] =
|
|
__builtin_bit_cast(halo_u32x4,
|
|
make_uint4(D::from_bf16w(r.x), D::from_bf16w(r.y),
|
|
D::from_bf16w(r.z), D::from_bf16w(r.w)));
|
|
}
|
|
}
|
|
};
|
|
|
|
// Scores of the keys (mt*16 + 2e + h for element e) of the lane's query, in log2 units once
|
|
// multiplied by `qs` (int8 only; the f16 query already carries the scale).
|
|
auto score = [&](int mt, float (&sc)[8]) {
|
|
v8f s = (v8f){0, 0, 0, 0, 0, 0, 0, 0};
|
|
if constexpr (I8) {
|
|
const int8_t* kr2 = &sK8[(mt * 16 + c) * KSTR8];
|
|
#pragma unroll
|
|
for (int t0 = 0; t0 < NKS; t0 += 4) {
|
|
frag16 kf[4];
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj) {
|
|
uint32_t w8[8];
|
|
vsa_i8x16_to_f16x8w(kr2 + 16 * (t0 + jj), w8);
|
|
kf[jj] = D::mk(w8);
|
|
}
|
|
sched_barrier();
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj) s = D::wmma(kf[jj], qf[t0 + jj], s);
|
|
}
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) sc[e] = s[e] * sKs[mt * 16 + 2 * e + h];
|
|
} else {
|
|
// Row c of the tile is key mt*16 + c; its sixteen dims of k-tile t are chunks
|
|
// (2t)^swz and (2t+1)^swz of that row.
|
|
const uint16_t* kr2 = &sK[(mt * 16 + c) * HD];
|
|
#pragma unroll
|
|
for (int t0 = 0; t0 < NKS; t0 += 4) {
|
|
frag16 kf[4];
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj) {
|
|
const uint4 u0 = *(const uint4*)(kr2 + ((2 * (t0 + jj)) ^ swz) * 8);
|
|
const uint4 u1 = *(const uint4*)(kr2 + ((2 * (t0 + jj) + 1) ^ swz) * 8);
|
|
const uint32_t w8[8] = { u0.x, u0.y, u0.z, u0.w,
|
|
u1.x, u1.y, u1.z, u1.w };
|
|
kf[jj] = D::mk(w8);
|
|
}
|
|
sched_barrier();
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj) s = D::wmma(kf[jj], qf[t0 + jj], s);
|
|
}
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) sc[e] = s[e];
|
|
}
|
|
};
|
|
|
|
// MASK is the edge tiles' form; a full tile's keys are all valid.
|
|
auto compute = [&](int klim, auto maskc) {
|
|
constexpr bool mask = decltype(maskc)::value;
|
|
const int mte = mask ? (klim + 15) >> 4 : TILE / 16;
|
|
#pragma unroll 1
|
|
for (int mt = 0; mt < mte; ++mt) {
|
|
float sc[8];
|
|
score(mt, sc);
|
|
if constexpr (mask) {
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e)
|
|
sc[e] = mt * 16 + 2 * e + h < klim ? sc[e] : -INFINITY;
|
|
}
|
|
|
|
// qs > 0, so the max of the scaled scores is the scaled max.
|
|
float smax = sc[0];
|
|
#pragma unroll
|
|
for (int e = 1; e < 8; ++e) smax = fmaxf(smax, sc[e]);
|
|
if (I8) smax *= qs;
|
|
if (wave_any(smax > m_ref + GROW)) {
|
|
const float rmax = fmaxf(smax, swap16(smax));
|
|
const float mnew = fmaxf(fmaxf(m_ref, rmax), -3.0e38f);
|
|
const float alpha = ex2(m_ref - mnew);
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) acc[t][e] *= alpha;
|
|
l_i *= alpha;
|
|
m_ref = mnew;
|
|
soff = -mnew;
|
|
}
|
|
|
|
// P packed to f16, the row sum from those same packed values.
|
|
uint32_t pw[4];
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; e += 2) {
|
|
const float a0 = I8 ? __builtin_fmaf(sc[e], qs, soff) : sc[e] + soff;
|
|
const float a1 = I8 ? __builtin_fmaf(sc[e + 1], qs, soff) : sc[e + 1] + soff;
|
|
pw[e >> 1] = D::pk(ex2(a0), ex2(a1));
|
|
}
|
|
const v2hf_dt one = {(__fp16)1.0f, (__fp16)1.0f};
|
|
#pragma unroll
|
|
for (int e = 0; e < 4; ++e)
|
|
l_i = __builtin_amdgcn_fdot2(__builtin_bit_cast(v2hf_dt, pw[e]), one, l_i, false);
|
|
// Keys mt*16 + 2e + h are this lane's; the P fragment of the WMMA wants them in
|
|
// order, so the half-pair is interleaved (halo_dt16.h's p16w).
|
|
uint32_t pw16[8];
|
|
p16w<D>(pw, pw16);
|
|
const frag16 p16 = D::mk(pw16);
|
|
|
|
if constexpr (PV8) {
|
|
// A's row is the head dim c + 16t, its sixteen keys the E4M3 codes at
|
|
// (mt*16) of that row, widened exactly.
|
|
const uint8_t* vr2 = &sV8[c * VSTR8 + mt * 16];
|
|
#pragma unroll
|
|
for (int t0 = 0; t0 < NKS; t0 += 4) {
|
|
frag16 vf[4];
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj) {
|
|
uint32_t w8[8];
|
|
vsa_fp8x16_to_f16x8w(vr2 + (t0 + jj) * 16 * VSTR8, w8);
|
|
vf[jj] = D::mk(w8);
|
|
}
|
|
sched_barrier();
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj)
|
|
acc[t0 + jj] = D::wmma(vf[jj], p16, acc[t0 + jj]);
|
|
}
|
|
} else {
|
|
// The V^T tile's row c + 16t, chunks (2*mt)^swz and (2*mt+1)^swz.
|
|
const uint16_t* v0 = &sV[c * TILE + ((2 * mt) ^ swz) * 8];
|
|
const uint16_t* v1 = &sV[c * TILE + ((2 * mt + 1) ^ swz) * 8];
|
|
#pragma unroll
|
|
for (int t0 = 0; t0 < NKS; t0 += 4) {
|
|
frag16 vf[4];
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj) {
|
|
const uint4 a = *(const uint4*)(v0 + (t0 + jj) * 16 * TILE);
|
|
const uint4 b = *(const uint4*)(v1 + (t0 + jj) * 16 * TILE);
|
|
const uint32_t w8[8] = { a.x, a.y, a.z, a.w,
|
|
b.x, b.y, b.z, b.w };
|
|
vf[jj] = D::mk(w8);
|
|
}
|
|
sched_barrier();
|
|
#pragma unroll
|
|
for (int jj = 0; jj < 4; ++jj)
|
|
acc[t0 + jj] = D::wmma(vf[jj], p16, acc[t0 + jj]);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// The walk: the staged tile (jc, the query tiles mc that see it) goes to LDS, the next one's
|
|
// loads are issued, then the staged tile's arithmetic.
|
|
int jc, mc;
|
|
advance(jc, mc);
|
|
VsaTile kdc = qd;
|
|
if (jc != kNone) {
|
|
kdc = vsa_tile(g, segs, R, jc);
|
|
load(kdc);
|
|
}
|
|
while (jc != kNone) {
|
|
int jn, mn;
|
|
advance(jn, mn);
|
|
lds_barrier();
|
|
store();
|
|
lds_barrier();
|
|
VsaTile kdn = kdc;
|
|
if (jn != kNone) {
|
|
kdn = vsa_tile(g, segs, R, jn);
|
|
load(kdn);
|
|
}
|
|
if (wlive && ((mc >> qt) & 1)) {
|
|
if (kdc.size == TILE) compute(kdc.size, std::false_type{});
|
|
else compute(kdc.size, std::true_type{});
|
|
}
|
|
jc = jn;
|
|
mc = mn;
|
|
kdc = kdn;
|
|
}
|
|
|
|
if (!live) return;
|
|
const float ls = l_i + swap16(l_i);
|
|
const float inv = (ls > 0.0f) ? (1.0f / ls) : 0.0f;
|
|
uint16_t* op = go + (long)qrow * o_ld + (long)hq * HD;
|
|
const uint16_t* cp = gc ? gc + (long long)hq * c_hs + (long long)i * c_ts : nullptr;
|
|
const uint16_t* gp = gg ? gg + (long)qrow * g_ld + (long)hq * HD : nullptr;
|
|
// Element e of the accumulator is head dim t*16 + 2*e + h, so dword e of a 16-dim tile holds
|
|
// this lane's two parities for element e and the store is one 16-bit value a lane.
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t) {
|
|
uint32_t cw[8], gw[8];
|
|
if (cp) {
|
|
const uint4 a = *(const uint4*)(cp + t * 16), b = *(const uint4*)(cp + t * 16 + 8);
|
|
const uint4 e = *(const uint4*)(gp + t * 16), f = *(const uint4*)(gp + t * 16 + 8);
|
|
cw[0] = a.x; cw[1] = a.y; cw[2] = a.z; cw[3] = a.w;
|
|
cw[4] = b.x; cw[5] = b.y; cw[6] = b.z; cw[7] = b.w;
|
|
gw[0] = e.x; gw[1] = e.y; gw[2] = e.z; gw[3] = e.w;
|
|
gw[4] = f.x; gw[5] = f.y; gw[6] = f.z; gw[7] = f.w;
|
|
}
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) {
|
|
const int d = 2 * e + h;
|
|
float v = acc[t][e];
|
|
if constexpr (PV8) v *= gvs[(long)hq * HD + t * 16 + d]; // the column scale
|
|
v *= inv;
|
|
if (cp) {
|
|
// The epilogue's addcmul: o rounded to bf16, plus coarse * gate (exact in f32),
|
|
// one rounding.
|
|
const float o0 = bf16_to_f32(f32_to_bf16(v));
|
|
const float cv = h ? bf16w_hi(cw[e]) : bf16w_lo(cw[e]);
|
|
const float gv = h ? bf16w_hi(gw[e]) : bf16w_lo(gw[e]);
|
|
v = o0 + cv * gv;
|
|
}
|
|
op[t * 16 + d] = f32_to_bf16(v);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------------- shared checks
|
|
inline bool is_bf16(const RadTensor* t) { return t && t->dtype == RAD_BF16; }
|
|
inline bool is_i32(const RadTensor* t) { return t && t->dtype == RAD_I32; }
|
|
inline int st_of(hipError_t e) { return e == hipSuccess ? RAD_OK : HALO_NO(RAD_E_DEVICE); }
|
|
|
|
// What both ops read alike: the geometry parameters and the device geometry operands.
|
|
struct VsaCommon {
|
|
long long R, nh, q_ld, k_ld;
|
|
int n_seg, patch;
|
|
double sparsity;
|
|
float scale;
|
|
};
|
|
|
|
int vsa_common(const RadArgs* a, const RadTensor* q, const RadTensor* k, const RadTensor* segs,
|
|
const RadTensor* geom, VsaCommon* c) {
|
|
if (!is_bf16(q) || !is_bf16(k) || !is_i32(segs) || !is_i32(geom)) return HALO_NO(RAD_E_DTYPE);
|
|
if (halo_geti_or(a, "head_dim", 0) != kHd || halo_geti_or(a, "tile_t", 0) != 4 ||
|
|
halo_geti_or(a, "tile_h", 0) != 4 || halo_geti_or(a, "tile_w", 0) != 4)
|
|
return HALO_NO(RAD_E_SHAPE);
|
|
const long long patch = halo_geti_or(a, "patch", 0);
|
|
const double sp = halo_getf(a, "sparsity", -1.0);
|
|
if (patch < 1 || !(sp >= 0.0 && sp < 1.0)) return HALO_NO(RAD_E_INVAL);
|
|
long long tk = 0, hk = 0;
|
|
if (!halo_head_rows(q, kHd, &c->R, &c->nh, &c->q_ld) ||
|
|
!halo_head_rows(k, kHd, &tk, &hk, &c->k_ld) ||
|
|
((c->q_ld | c->k_ld) & 7) || ((uintptr_t)q->data & 15) || ((uintptr_t)k->data & 15))
|
|
return HALO_NO(RAD_E_STRIDE);
|
|
if (hk != c->nh || tk < c->R || c->R <= 0 || c->R > 0x7fffffffLL) return HALO_NO(RAD_E_SHAPE);
|
|
if (!halo_contig(segs) || !halo_contig(geom)) return HALO_NO(RAD_E_STRIDE);
|
|
const long long ns = halo_numel(segs) - 1;
|
|
if (ns < 1 || ns > 4096 || halo_numel(geom) < 3) return HALO_NO(RAD_E_SHAPE);
|
|
c->n_seg = (int)ns;
|
|
c->patch = (int)patch;
|
|
c->sparsity = sp;
|
|
c->scale = (float)halo_getf(a, "scale", 1.0 / std::sqrt((double)kHd));
|
|
return RAD_OK;
|
|
}
|
|
|
|
// An operand of R rows of nh heads at its own pitch, 16-byte aligned.
|
|
bool vsa_rows(const RadTensor* t, long long R, long long nh, long long* ld) {
|
|
long long rows = 0, heads = 0;
|
|
return is_bf16(t) && halo_head_rows(t, kHd, &rows, &heads, ld) && !(*ld & 7) &&
|
|
!((uintptr_t)t->data & 15) && heads == nh && rows >= R;
|
|
}
|
|
|
|
// sel [>= nh, tiles_cap, k_cap] i32 with unit stride on the last axis, and coarse
|
|
// [>= nh, >= tiles_cap, 128] bf16 likewise.
|
|
int vsa_sel(const RadTensor* sel, const RadTensor* coarse, long long nh, long long* tiles_cap,
|
|
long long* k_cap) {
|
|
if (!is_i32(sel) || (coarse && !is_bf16(coarse))) return HALO_NO(RAD_E_DTYPE);
|
|
if (sel->rank != 3 || sel->stride[2] != 1 ||
|
|
(coarse && (coarse->rank != 3 || coarse->stride[2] != 1)))
|
|
return HALO_NO(RAD_E_STRIDE);
|
|
*tiles_cap = sel->shape[1];
|
|
*k_cap = sel->shape[2];
|
|
/* Tile numbers stay below 2^16 (the tile decode's multiply-high division). */
|
|
if (sel->shape[0] < nh || *tiles_cap < 1 || *k_cap < 1 || *tiles_cap > 65536 ||
|
|
*k_cap > 0x7fffffffLL)
|
|
return HALO_NO(RAD_E_SHAPE);
|
|
if (coarse && (coarse->shape[0] < nh || coarse->shape[1] < *tiles_cap ||
|
|
coarse->shape[2] != kHd || ((uintptr_t)coarse->data & 15) ||
|
|
(coarse->stride[1] & 7) || (coarse->stride[0] & 7)))
|
|
return HALO_NO(RAD_E_SHAPE);
|
|
return RAD_OK;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// ============================================================== the plugin ABI entry points
|
|
enum { VS_Q = 0, VS_K, VS_V, VS_SEGS, VS_GEOM, VS_SEL, VS_COARSE };
|
|
|
|
/* The scratch holds the q, k and v means of a group of heads, 1.5 KiB a (tile, head) at the
|
|
* layouts the select kernel reads. The hook sees neither the head count nor the selection's tile
|
|
* capacity, so it sizes for M / 4 (tile, head) slots; the entry point takes as many heads a group
|
|
* as fit (tiles_cap slots each) and refuses only when one does not. At 1344 x 768 (M 37.7K,
|
|
* tiles_cap up to ~1500) that is six heads a group. */
|
|
extern "C" int64_t halo_rad_vsa_select_scratch(const RadArgs* a) {
|
|
const long long M = halo_geti_or(a, "M", 0);
|
|
if (M <= 0) return HALO_NO(RAD_E_SHAPE);
|
|
return M * 384;
|
|
}
|
|
|
|
extern "C" int halo_rad_vsa_select(const RadArgs* a, RadStream s) {
|
|
const RadTensor *q = halo_opt(a, VS_Q), *k = halo_opt(a, VS_K), *v = halo_opt(a, VS_V);
|
|
const RadTensor *segs = halo_opt(a, VS_SEGS), *geom = halo_opt(a, VS_GEOM);
|
|
const RadTensor *sel = halo_opt(a, VS_SEL), *coarse = halo_opt(a, VS_COARSE);
|
|
if (!q || !k || !segs || !geom || !sel) return HALO_NO(RAD_E_INVAL);
|
|
/* v is read only for the coarse branch: both or neither. */
|
|
if ((v != nullptr) != (coarse != nullptr)) return HALO_NO(RAD_E_INVAL);
|
|
VsaCommon c;
|
|
int rc = vsa_common(a, q, k, segs, geom, &c);
|
|
if (rc != RAD_OK) return rc;
|
|
long long v_ld = 0, tiles_cap = 0, k_cap = 0;
|
|
if (v && !vsa_rows(v, c.R, c.nh, &v_ld)) return HALO_NO(RAD_E_STRIDE);
|
|
rc = vsa_sel(sel, coarse, c.nh, &tiles_cap, &k_cap);
|
|
if (rc != RAD_OK) return rc;
|
|
/* Two LDS budgets: eight query tiles a workgroup up to 1536 tiles, four up to 3072. */
|
|
if (tiles_cap > 3072) return HALO_NO(RAD_E_SHAPE);
|
|
const long long per = tiles_cap * (coarse ? 1536 : 1024);
|
|
long long G = a->scratch && !((uintptr_t)a->scratch & 15)
|
|
? std::min<long long>(c.nh, a->scratch_bytes / per) : 0;
|
|
if (G < 1) return HALO_NO(RAD_E_SCRATCH);
|
|
/* As many groups as the scratch needs, the heads spread evenly over them: a last group of a
|
|
* head or two would leave most of the card idle for its two launches. */
|
|
const long long rounds = (c.nh + G - 1) / G;
|
|
G = (c.nh + rounds - 1) / rounds;
|
|
float4* kpT = (float4*)a->scratch;
|
|
float4* qp4 = (float4*)((char*)a->scratch + G * tiles_cap * 512);
|
|
float4* vp4 = coarse ? (float4*)((char*)a->scratch + G * tiles_cap * 1024) : nullptr;
|
|
const hipStream_t st = (hipStream_t)s;
|
|
for (long long h0 = 0; h0 < c.nh; h0 += G) {
|
|
const int g = (int)std::min<long long>(G, c.nh - h0);
|
|
hipLaunchKernelGGL(vsa_pool_kernel, dim3((unsigned)tiles_cap, (unsigned)((g + 3) / 4)),
|
|
dim3(128), 0, st, (const uint16_t*)q->data, (const uint16_t*)k->data,
|
|
v ? (const uint16_t*)v->data : nullptr, (int)c.q_ld, (int)c.k_ld,
|
|
(int)v_ld, (const int*)segs->data, c.n_seg, (const int*)geom->data,
|
|
c.patch, (int)c.R, (int)tiles_cap, (int)h0, g, kpT, qp4, vp4);
|
|
#define HALO_VSEL(QT, NMAX) \
|
|
hipLaunchKernelGGL((vsa_select_kernel<QT, NMAX>), \
|
|
dim3((unsigned)((tiles_cap + QT - 1) / QT), (unsigned)g), dim3(256), 0, \
|
|
st, (const int*)segs->data, c.n_seg, (const int*)geom->data, \
|
|
c.patch, (int)c.R, (int)tiles_cap, c.sparsity, c.scale, (int)h0, \
|
|
(const float4*)kpT, (const float4*)qp4, (const float*)vp4, \
|
|
(int*)sel->data, (long long)sel->stride[0], \
|
|
(long long)sel->stride[1], (int)k_cap, \
|
|
coarse ? (uint16_t*)coarse->data : nullptr, \
|
|
coarse ? (long long)coarse->stride[0] : 0LL, \
|
|
coarse ? (long long)coarse->stride[1] : 0LL)
|
|
if (tiles_cap <= 1536) HALO_VSEL(8, 1536);
|
|
else HALO_VSEL(4, 3072);
|
|
#undef HALO_VSEL
|
|
const hipError_t e = hipGetLastError();
|
|
if (e != hipSuccess) return st_of(e);
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
enum { VA_Q = 0, VA_K, VA_V, VA_SEGS, VA_GEOM, VA_SEL, VA_COARSE, VA_GATE, VA_OUT, VA_K8, VA_KS,
|
|
VA_VS };
|
|
|
|
extern "C" int halo_rad_attn_vsa(const RadArgs* a, RadStream s) {
|
|
const RadTensor *q = halo_opt(a, VA_Q), *k = halo_opt(a, VA_K), *v = halo_opt(a, VA_V);
|
|
const RadTensor *segs = halo_opt(a, VA_SEGS), *geom = halo_opt(a, VA_GEOM);
|
|
const RadTensor *sel = halo_opt(a, VA_SEL), *coarse = halo_opt(a, VA_COARSE);
|
|
const RadTensor *gate = halo_opt(a, VA_GATE), *o = halo_opt(a, VA_OUT);
|
|
if (!q || !k || !v || !segs || !geom || !sel || !o) return HALO_NO(RAD_E_INVAL);
|
|
/* The gated coarse term takes both. */
|
|
if ((coarse != nullptr) != (gate != nullptr)) return HALO_NO(RAD_E_INVAL);
|
|
VsaCommon c;
|
|
int rc = vsa_common(a, q, k, segs, geom, &c);
|
|
if (rc != RAD_OK) return rc;
|
|
/* The fp8 P.V form (`pv` "fp8"): `v` as E4M3 codes [M, n_head * head_dim] and their column
|
|
* scales `v_scale` [n_head * head_dim], as quant_cols_fp8 writes them. */
|
|
const RadTensor* vs = halo_opt(a, VA_VS);
|
|
const char* pvs = halo_gets(a, "pv");
|
|
const bool pv8 = pvs && !std::strcmp(pvs, "fp8");
|
|
if (pvs && !pv8 && std::strcmp(pvs, "bf16")) return HALO_NO(RAD_E_INVAL);
|
|
if (pv8 != (vs != nullptr)) return HALO_NO(RAD_E_INVAL);
|
|
long long v_ld = 0, o_ld = 0, g_ld = 0, tiles_cap = 0, k_cap = 0;
|
|
if (pv8) {
|
|
long long tv = 0, hv = 0;
|
|
if (v->dtype != RAD_F8E4M3 || vs->dtype != RAD_F32) return HALO_NO(RAD_E_DTYPE);
|
|
if (!halo_head_rows(v, kHd, &tv, &hv, &v_ld) || (v_ld & 15) || ((uintptr_t)v->data & 15) ||
|
|
!halo_contig(vs) || ((uintptr_t)vs->data & 15))
|
|
return HALO_NO(RAD_E_STRIDE);
|
|
if (hv != c.nh || tv < c.R || halo_numel(vs) < c.nh * kHd) return HALO_NO(RAD_E_SHAPE);
|
|
} else if (!vsa_rows(v, c.R, c.nh, &v_ld)) {
|
|
return HALO_NO(RAD_E_STRIDE);
|
|
}
|
|
if (!vsa_rows(o, c.R, c.nh, &o_ld) || (gate && !vsa_rows(gate, c.R, c.nh, &g_ld)))
|
|
return HALO_NO(RAD_E_STRIDE);
|
|
rc = vsa_sel(sel, coarse, c.nh, &tiles_cap, &k_cap);
|
|
if (rc != RAD_OK) return rc;
|
|
/* The int8 Q.K form (`qk` "i8"): the key codes [R, n_head * head_dim] and their scales
|
|
* [R, n_head], as quant_act_i8g writes them at group head_dim. */
|
|
const RadTensor *k8 = halo_opt(a, VA_K8), *ks = halo_opt(a, VA_KS);
|
|
const char* qk = halo_gets(a, "qk");
|
|
const bool i8 = qk && !std::strcmp(qk, "i8");
|
|
if (qk && !i8 && std::strcmp(qk, "bf16")) return HALO_NO(RAD_E_INVAL);
|
|
if (i8 != (k8 != nullptr) || i8 != (ks != nullptr)) return HALO_NO(RAD_E_INVAL);
|
|
long long k8_ld = 0, ks_ld = 0;
|
|
if (i8) {
|
|
if (k8->dtype != RAD_I8 || ks->dtype != RAD_F32) return HALO_NO(RAD_E_DTYPE);
|
|
long long t8 = 0, h8 = 0, ts = 0;
|
|
if (!halo_head_rows(k8, kHd, &t8, &h8, &k8_ld) || (k8_ld & 15) || ((uintptr_t)k8->data & 15))
|
|
return HALO_NO(RAD_E_STRIDE);
|
|
ks_ld = halo_rows_pitch(ks, c.nh, &ts);
|
|
if (ks_ld < 0) return HALO_NO(RAD_E_STRIDE);
|
|
if (h8 != c.nh || t8 < c.R || ts < c.R) return HALO_NO(RAD_E_SHAPE);
|
|
}
|
|
/* Two query tiles a workgroup (see vsa_attn_kernel). */
|
|
constexpr int NQT = 2;
|
|
#define HALO_VATT(I8, PV8) \
|
|
hipLaunchKernelGGL((vsa_attn_kernel<NQT, I8, PV8>), \
|
|
dim3((unsigned)((tiles_cap + NQT - 1) / NQT), (unsigned)c.nh), \
|
|
dim3(NQT * 128), 0, (hipStream_t)s, (const uint16_t*)q->data, \
|
|
(const uint16_t*)k->data, (const uint16_t*)v->data, (uint16_t*)o->data, \
|
|
coarse ? (const uint16_t*)coarse->data : nullptr, \
|
|
gate ? (const uint16_t*)gate->data : nullptr, (const int*)segs->data, \
|
|
c.n_seg, (const int*)geom->data, c.patch, (int)c.R, (int)tiles_cap, \
|
|
c.sparsity, (const int*)sel->data, (long long)sel->stride[0], \
|
|
(long long)sel->stride[1], (int)k_cap, \
|
|
coarse ? (long long)coarse->stride[0] : 0LL, \
|
|
coarse ? (long long)coarse->stride[1] : 0LL, c.scale, (int)c.q_ld, \
|
|
(int)c.k_ld, (int)v_ld, (int)g_ld, (int)o_ld, \
|
|
i8 ? (const int8_t*)k8->data : nullptr, \
|
|
i8 ? (const float*)ks->data : nullptr, (int)k8_ld, (int)ks_ld, \
|
|
pv8 ? (const float*)vs->data : nullptr)
|
|
if (i8) {
|
|
if (pv8) HALO_VATT(true, true);
|
|
else HALO_VATT(true, false);
|
|
} else {
|
|
if (pv8) HALO_VATT(false, true);
|
|
else HALO_VATT(false, false);
|
|
}
|
|
#undef HALO_VATT
|
|
return st_of(hipGetLastError());
|
|
}
|