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.
472 lines
24 KiB
Plaintext
472 lines
24 KiB
Plaintext
/* Derived from libr4d/r4d_qsa_score_bf16.hip by the libhalo rename transform (2026-10-26),
|
|
* then ported to gfx1151; see libhalo/README.md. */
|
|
// The QSA indexer's block score: how much one query wants each compressed block of key history.
|
|
//
|
|
// score[m][b] = ( sum over heads h of relu( q[m][h] . bk[bt[seq(m)][b]] ) ) / sqrt(n)
|
|
//
|
|
// The top budget/ratio scores per query select the blocks attention reads (docs/QSA.md;
|
|
// halo_qsa_block_key_bf16.hip builds `bk`).
|
|
//
|
|
// The relu is per (block, head), then summed over heads, as in the reference
|
|
// (`relu(scores).sum(dim=-1)`); summing first would let one head's negative cancel another's
|
|
// positive and still rank plausibly.
|
|
//
|
|
// `bt` holds page ids, not block indices: the attention KV group is paged at `ratio` tokens, so a
|
|
// compress block is a page, block keys are stored per page, and the sequence's attention block
|
|
// table is the list scored. The top-k is then directly the block table attention reads.
|
|
//
|
|
// An absent entry scores -inf, not zero: tables are padded, and a real score can be zero.
|
|
//
|
|
// Returns a RAD status rather than throwing, for the reason given in halo_gemm_fp8a8.hip.
|
|
|
|
#include "halo.h"
|
|
#include "halo_args.h"
|
|
#include "halo_emit.h"
|
|
#include "halo_common.h"
|
|
#include "halo_gdn_wmma.h"
|
|
|
|
#include <hip/hip_runtime.h>
|
|
#include <hip/hip_bf16.h>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
|
|
namespace {
|
|
|
|
constexpr int kWave = 32;
|
|
constexpr int kThreads = 256;
|
|
constexpr int kWaves = kThreads / kWave;
|
|
constexpr int kMaxH = 8; // indexer query heads; 4 on this model
|
|
constexpr int kMaxVPL = 8; // n / kWave, so n <= 256
|
|
// Scalar form: one wave per block, and a workgroup covers a tile of queries, so the key history
|
|
// (the only real traffic) is read once per tile instead of once per query row.
|
|
//
|
|
// kQT = 4 matches a decode row group (1 + n_spec rows at depth 3), so tiles usually align with
|
|
// sequences; a tile straddling two sequences is processed per part, reading the keys once per
|
|
// part. The tile lives in registers (see the scoring loop).
|
|
constexpr int kBPW = 4; // blocks a WAVE; a workgroup owns kWaves * kBPW = 32
|
|
constexpr int kQT = 4; // query rows a workgroup, and the grid's third axis
|
|
|
|
/* The scoring loop spells out halo_wave_sum's five butterfly steps so they can be hoisted out of
|
|
* the head loop (see there); same lanes, order and bits. */
|
|
|
|
// `m` is a query row, not a sequence: a prefill chunk has many queries over one history, each
|
|
// selecting its own set (docs/QSA.md, "Per-query selection"). A query finds its sequence's block-table row by
|
|
// scanning `cu`.
|
|
//
|
|
// A lane owns n / 32 consecutive dims of the query heads and the block key, so a head's dot is one
|
|
// wave reduction and each block key is read once for all heads and all kQT queries.
|
|
//
|
|
// `nc` (complete blocks per sequence) bounds the block loop; nb is a declared width sized for
|
|
// max_ctx and would mostly score padding. nc is an upper bound for every query in the chunk;
|
|
// qsa_select applies the exact per-query bound from `pos`.
|
|
template <int VPL, int HEADS>
|
|
__global__ __launch_bounds__(kThreads) void qsa_score_kernel(
|
|
const __hip_bfloat16* __restrict__ q, const __hip_bfloat16* __restrict__ bk,
|
|
const int32_t* __restrict__ bt, const int32_t* __restrict__ cu,
|
|
const int32_t* __restrict__ nc, float* __restrict__ out,
|
|
int n, int heads, int nb, int nseq, int q_ld, int bk_ld, int bt_ld, int out_ld) {
|
|
|
|
__shared__ int sh_seq;
|
|
const int lane = threadIdx.x & (kWave - 1), wave = threadIdx.x >> 5;
|
|
const float inv = rsqrtf((float)n);
|
|
/* Load width: the widest vector dividing both VPL and the row pitch (a KV cache row pitch
|
|
* need not be a multiple of anything). */
|
|
const int kvec = ((VPL & 3) == 0 && (bk_ld & 3) == 0) ? 4
|
|
: ((VPL & 1) == 0 && (bk_ld & 1) == 0) ? 2 : 1;
|
|
const int m0 = (int)blockIdx.y * kQT;
|
|
|
|
/* The tile walks its queries one sequence at a time (block table row, complete-block count
|
|
* and block bound are per sequence). Usually one iteration. */
|
|
for (int m = m0; m < m0 + kQT; ) {
|
|
/* The sequence of query `m`. Exactly one i matches, so the store is deterministic. A query
|
|
* past the batch matches nothing and ends the tile. */
|
|
if (threadIdx.x == 0) sh_seq = -1;
|
|
__syncthreads();
|
|
for (int i = (int)threadIdx.x; i < nseq; i += kThreads)
|
|
if (m >= cu[i] && m < cu[i + 1]) sh_seq = i;
|
|
__syncthreads();
|
|
const int seq = sh_seq;
|
|
if (seq < 0) return;
|
|
const int mend = cu[seq + 1] < m0 + kQT ? cu[seq + 1] : m0 + kQT;
|
|
const int nq = mend - m;
|
|
|
|
const int nbs = nc[seq] < nb ? nc[seq] : nb;
|
|
/* The block axis is sized from the declared width, so most workgroups on a short context
|
|
* skip here, before the query load. A skip, not a return: a straddling tile may be past
|
|
* the first sequence's blocks but inside the second's. The bound is uniform, so barriers
|
|
* stay matched.
|
|
*
|
|
* The skip path still needs the barrier: the next part starts with thread 0 resetting
|
|
* sh_seq, and a wave that has not read this part's value would leave early. */
|
|
const int b0 = (int)blockIdx.x * (kWaves * kBPW);
|
|
if (nbs <= 0 || b0 >= nbs) {
|
|
m = mend;
|
|
if (m < m0 + kQT) __syncthreads();
|
|
continue;
|
|
}
|
|
const int b1 = (b0 + kWaves * kBPW) < nbs ? (b0 + kWaves * kBPW) : nbs;
|
|
|
|
/* The query tile in registers (kQT * HEADS * VPL, 64 at this model's shape), loaded once
|
|
* for all of this workgroup's blocks; in LDS every block would re-read it, and LDS issue
|
|
* is this kernel's bottleneck. A query past the part reads its last row, unscored. */
|
|
float qv[kQT][HEADS][VPL];
|
|
#pragma unroll
|
|
for (int mi = 0; mi < kQT; ++mi) {
|
|
const int row = m + (mi < nq ? mi : nq - 1);
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) {
|
|
const __hip_bfloat16* qp = q + (size_t)row * q_ld + (size_t)h * n + lane * VPL;
|
|
#pragma unroll
|
|
for (int jj = 0; jj < VPL; ++jj) qv[mi][h][jj] = __bfloat162float(qp[jj]);
|
|
}
|
|
}
|
|
|
|
/* Issue all of this wave's loads before scoring: each block is two dependent reads (page
|
|
* id, then key), so all page ids are read first, then all keys, instead of 2 * kBPW
|
|
* serial latencies. A block past the end or without a page reads page 0's key (always
|
|
* present) and is skipped below. */
|
|
int pg[kBPW];
|
|
float kvs[kBPW][VPL];
|
|
#pragma unroll
|
|
for (int i = 0; i < kBPW; ++i) {
|
|
const int b = b0 + wave + i * kWaves;
|
|
pg[i] = b < b1 ? bt[(size_t)seq * bt_ld + b] : -1;
|
|
}
|
|
#pragma unroll
|
|
for (int i = 0; i < kBPW; ++i) {
|
|
float* kv = kvs[i];
|
|
/* One vector load per lane instead of VPL two-byte loads, each of which would touch
|
|
* the wave's whole 256-byte row (about 145 GB/s on a 621 GB/s part). Same bytes and
|
|
* registers, so the sum is bit-identical. */
|
|
const uint16_t* kp = (const uint16_t*)bk + (size_t)(pg[i] >= 0 ? pg[i] : 0) * bk_ld
|
|
+ lane * VPL;
|
|
if (kvec == 4) {
|
|
#pragma unroll
|
|
for (int j = 0; j < VPL; j += 4) {
|
|
const uint2 v = *(const uint2*)(kp + j);
|
|
kv[j + 0] = __uint_as_float((v.x & 0xFFFFu) << 16);
|
|
kv[j + 1] = __uint_as_float(v.x & 0xFFFF0000u);
|
|
kv[j + 2] = __uint_as_float((v.y & 0xFFFFu) << 16);
|
|
kv[j + 3] = __uint_as_float(v.y & 0xFFFF0000u);
|
|
}
|
|
} else if (kvec == 2) {
|
|
#pragma unroll
|
|
for (int j = 0; j < VPL; j += 2) {
|
|
const uint32_t v = *(const uint32_t*)(kp + j);
|
|
kv[j + 0] = __uint_as_float((v & 0xFFFFu) << 16);
|
|
kv[j + 1] = __uint_as_float(v & 0xFFFF0000u);
|
|
}
|
|
} else {
|
|
#pragma unroll
|
|
for (int j = 0; j < VPL; ++j)
|
|
kv[j] = __uint_as_float((uint32_t)kp[j] << 16);
|
|
}
|
|
}
|
|
|
|
#pragma unroll
|
|
for (int i = 0; i < kBPW; ++i) {
|
|
const int b = b0 + wave + i * kWaves;
|
|
if (b >= b1) break;
|
|
if (pg[i] < 0) {
|
|
if (lane == 0)
|
|
for (int mi = 0; mi < nq; ++mi)
|
|
out[(size_t)(m + mi) * out_ld + b] = -INFINITY;
|
|
continue;
|
|
}
|
|
const float* kv = kvs[i];
|
|
#pragma unroll
|
|
for (int mi = 0; mi < kQT; ++mi) {
|
|
if (mi >= nq) break;
|
|
/* Every head's dot first, then one butterfly over all of them: five dependent steps
|
|
* with HEADS independent shuffles each, instead of a HEADS * 5 chain. Same values in
|
|
* the same order, so bit-identical. HEADS is a template parameter so dv[] and qv
|
|
* stay in registers. */
|
|
float dv[HEADS];
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) {
|
|
float d = 0.0f;
|
|
#pragma unroll
|
|
for (int j = 0; j < VPL; ++j) d = fmaf(qv[mi][h][j], kv[j], d);
|
|
dv[h] = d;
|
|
}
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) dv[h] += swap16(dv[h]); /* xor 16 */
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) dv[h] = halo_dpp_xor_add<0x168>(dv[h]); /* xor 8 */
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) dv[h] = halo_dpp_xor_add<0x164>(dv[h]); /* xor 4 */
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) dv[h] = halo_dpp_xor_add<0x162>(dv[h]); /* xor 2 */
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) dv[h] = halo_dpp_xor_add<0x161>(dv[h]); /* xor 1 */
|
|
/* Every lane holds the full reduction; lane 0 stores. */
|
|
float s = 0.0f;
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) s += fmaxf(dv[h], 0.0f);
|
|
if (lane == 0) out[(size_t)(m + mi) * out_ld + b] = s * inv;
|
|
}
|
|
}
|
|
m = mend;
|
|
/* The next part resets sh_seq, so all reads of it must be done. */
|
|
__syncthreads();
|
|
}
|
|
}
|
|
|
|
/* ================================================================== the matrix form
|
|
*
|
|
* The same score as a matrix product. Per head, 16 queries x 16 blocks is one 16x16 tile of
|
|
* K_tile . Q_h^T (eight bf16 WMMA steps over 128 dims); a lane then holds one query against eight
|
|
* consecutive blocks of every head's tile, so relu and head sum happen in registers.
|
|
*
|
|
* The scalar form reads the key history once per four query rows and does a lane reduction per
|
|
* head per block, so it is linear in M (a 2048-row chunk at 80K context: 9.3 ms per call). Here
|
|
* the history is read once per 16 queries with no reductions. It is used for every batch it fits,
|
|
* since it was faster at every measured shape except a single short-context query (by ~0.25 us),
|
|
* e.g. at 200K context: M=1 29.9 -> 12.7 us, M=32 437 -> 27.2 us, M=2048 27.7 -> 1.36 ms.
|
|
*
|
|
* One form for all batch sizes also keeps a score a function of its query: the two forms sum in
|
|
* different orders and differ in the last bit (rel_l2 ~4e-8), which could flip an exact tie.
|
|
*
|
|
* One wave owns 16 query rows (in registers: HEADS x 8 fragments, 128 VGPRs at four heads) and a
|
|
* range of `tpw` block tiles, each one page-id read and eight fragment loads. A workgroup is four
|
|
* waves on consecutive block ranges of the same query rows, sharing them through the WGP cache.
|
|
* No LDS, no barrier. A tile with rows of two sequences is scored per sequence, as in the scalar
|
|
* form (decode rows next to a prefill chunk). */
|
|
constexpr int kMxWaves = 4; // waves a workgroup, each on its own block range
|
|
constexpr int kMxQ = 16; // query rows a wave: the WMMA tile's N
|
|
constexpr int kMxKS = 8; // 16-wide K steps: n = 128, the only width this form is built for
|
|
/* Target grid size in waves for sizing a wave's block range: a couple of rounds of resident
|
|
* capacity (64 CUs x 4 SIMDs x ~6 waves at this form's register count) ... */
|
|
constexpr long long kMxWavesTarget = 4096;
|
|
/* ... and at least this many tiles per wave, since loading the 16 query rows (16 KiB at four
|
|
* heads) would otherwise dominate. */
|
|
constexpr long long kMxMinTiles = 4;
|
|
|
|
template <int HEADS>
|
|
__global__ __launch_bounds__(kMxWaves * kWave) void qsa_score_mx_kernel(
|
|
const unsigned short* __restrict__ q, const unsigned short* __restrict__ bk,
|
|
const int32_t* __restrict__ bt, const int32_t* __restrict__ cu,
|
|
const int32_t* __restrict__ nc, float* __restrict__ out,
|
|
int M, int nb, int nseq, int tpw, int q_ld, int bk_ld, int bt_ld, int out_ld, int vec) {
|
|
|
|
const int lane = threadIdx.x & (kWave - 1), wave = threadIdx.x >> 5;
|
|
const int half = lane >> 4, col = lane & 15;
|
|
const int m0 = (int)blockIdx.y * kMxQ;
|
|
const int wb0 = ((int)blockIdx.x * kMxWaves + wave) * tpw * 16;
|
|
if (m0 >= M || wb0 >= nb) return; /* wave-uniform, and nothing below synchronises */
|
|
const float inv = rsqrtf((float)(kMxKS * 16));
|
|
|
|
/* B operand: query row `col` of the tile, every head and K step. A row past the batch reads
|
|
* the last row and is never stored. */
|
|
v16bf qf[HEADS][kMxKS];
|
|
{
|
|
const int qr = m0 + col < M ? m0 + col : M - 1;
|
|
/* gfx1151: the lane holds the whole 16-wide k run, so there is no half-wave k term
|
|
* (libhalo/halo_wmma.h). */
|
|
const unsigned short* qp = q + (size_t)qr * q_ld;
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h)
|
|
#pragma unroll
|
|
for (int ks = 0; ks < kMxKS; ++ks) qf[h][ks] = fragP(qp + h * kMxKS * 16 + ks * 16);
|
|
}
|
|
|
|
const int mlim = m0 + kMxQ < M ? m0 + kMxQ : M;
|
|
const int row = m0 + col;
|
|
for (int m = m0; m < mlim; ) {
|
|
/* The sequence of query `m`: exactly one entry of `cu` brackets it. */
|
|
int seq = -1;
|
|
for (int i0 = 0; i0 < nseq; i0 += kWave) {
|
|
const int i = i0 + lane;
|
|
const unsigned long long hit = __ballot(i < nseq && m >= cu[i] && m < cu[i + 1]);
|
|
if (hit) { seq = i0 + __ffsll((long long)hit) - 1; break; }
|
|
}
|
|
if (seq < 0) return; /* past the batch: so is every later row */
|
|
const int mend = cu[seq + 1] < mlim ? cu[seq + 1] : mlim;
|
|
const bool live = row >= m && row < mend;
|
|
const int nbs = nc[seq] < nb ? nc[seq] : nb;
|
|
const int bend = wb0 + tpw * 16 < nbs ? wb0 + tpw * 16 : nbs;
|
|
const int32_t* tab = bt + (size_t)seq * bt_ld;
|
|
|
|
/* Read the next tile's page id while this tile's key loads, hiding one dependent latency
|
|
* per tile. */
|
|
int pg_next = wb0 + col < bend ? tab[wb0 + col] : -1;
|
|
for (int b0 = wb0; b0 < bend; b0 += 16) {
|
|
/* A operand: block b0 + col's key. A block past the end or without a page reads page
|
|
* 0's key (always present) and is masked or skipped at the store. */
|
|
const int b = b0 + col;
|
|
const int pg = pg_next;
|
|
const unsigned short* kp = bk + (size_t)(pg >= 0 ? pg : 0) * bk_ld;
|
|
v16bf kf[kMxKS];
|
|
#pragma unroll
|
|
for (int ks = 0; ks < kMxKS; ++ks) kf[ks] = fragP(kp + ks * 16);
|
|
pg_next = b + 16 < bend ? tab[b + 16] : -1;
|
|
/* Which of the tile's sixteen blocks have no page: lanes 0-15 each read one. */
|
|
const unsigned absent = (unsigned)__ballot(pg < 0) & 0xFFFFu;
|
|
|
|
v8f acc[HEADS];
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) acc[h][e] = 0.0f;
|
|
#pragma unroll
|
|
for (int ks = 0; ks < kMxKS; ++ks)
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) acc[h] = mma(kf[ks], qf[h][ks], acc[h]);
|
|
|
|
/* D[block][query]: this lane holds query `col` against blocks b0 + 2*e + half on
|
|
* gfx1151 (D[2*e + (lane>>4)][lane & 15], halo_wmma.h) -- the even (or odd) blocks
|
|
* of the tile, so these are eight strided f32 stores rather than gfx12's two
|
|
* contiguous float4s. The wave's stores go to sixteen different query rows anyway
|
|
* (out_ld apart), so the vector width buys nothing here. */
|
|
(void)vec;
|
|
if (!live) continue;
|
|
float* op = out + (size_t)row * out_ld + b0;
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) {
|
|
const int bb = 2 * e + half; /* the block within the tile */
|
|
if (b0 + bb >= bend) continue;
|
|
float s = 0.0f;
|
|
#pragma unroll
|
|
for (int h = 0; h < HEADS; ++h) s += fmaxf(acc[h][e], 0.0f);
|
|
op[bb] = ((absent >> bb) & 1u) ? -INFINITY : s * inv;
|
|
}
|
|
}
|
|
m = mend;
|
|
}
|
|
}
|
|
|
|
/* Form override for tests (halo_selftest compares the two): 0 = automatic, 1 = scalar,
|
|
* 2 = matrix. Like halo_gemm_fp8a8_pin_mw. */
|
|
int g_qsa_form = 0;
|
|
|
|
} // namespace
|
|
|
|
extern "C" void halo_qsa_score_pin_form(int form) { g_qsa_form = form; }
|
|
|
|
// q [M][heads*n] bf16, `q_ld` elements a row (0 = heads*n); M is query rows
|
|
// bk [pages][n] bf16, `bk_ld` elements a row (0 = n)
|
|
// bt [nseq][nb] i32, `bt_ld` elements a row (0 = nb); negative = no block
|
|
// cu [nseq+1] i32, cumulative query counts: which sequence a query row belongs to
|
|
// nc [nseq] i32, complete blocks a sequence; the block loop's bound
|
|
// out [M][nb] f32, `out_ld` elements a row (0 = nb)
|
|
static int halo_qsa_score_bf16_emit(long q, long bk, long bt, long cu, long nc, long out,
|
|
int M, int n, int heads, int nb, int nseq,
|
|
int q_ld, int bk_ld, int bt_ld, int out_ld, RadStream stream) {
|
|
int emit_rc = RAD_OK;
|
|
if (!q || !bk || !bt || !cu || !nc || !out) return HALO_NO(RAD_E_INVAL);
|
|
if (M <= 0 || n <= 0 || heads <= 0 || nb <= 0 || nseq <= 0) return HALO_NO(RAD_E_SHAPE);
|
|
/* A lane owns n/32 dims, at most 8. */
|
|
if (n % kWave || n / kWave > kMaxVPL || heads > kMaxH) return HALO_NO(RAD_E_SHAPE);
|
|
if (!q_ld) q_ld = heads * n;
|
|
if (!bk_ld) bk_ld = n;
|
|
if (!bt_ld) bt_ld = nb;
|
|
if (!out_ld) out_ld = nb;
|
|
/* Grid axes are sized from declared widths (live counts are device data); surplus workgroups
|
|
* exit early. The matrix form is used whenever it fits (n = 128, at most four heads, so the
|
|
* query rows fit in registers); the scalar form serves the rest. */
|
|
const bool mx_fits = n == kMxKS * 16 && heads <= 4;
|
|
if (g_qsa_form == 2 && !mx_fits) return HALO_NO(RAD_E_SHAPE);
|
|
const bool mx = g_qsa_form == 1 ? false : mx_fits;
|
|
if (mx) {
|
|
/* Tiles per wave: about kMxWavesTarget waves in the grid, at least kMxMinTiles each. */
|
|
const long long qt = (M + kMxQ - 1) / kMxQ, tiles = (nb + 15) / 16;
|
|
long long tpw = (tiles * qt + kMxWavesTarget - 1) / kMxWavesTarget;
|
|
if (tpw < kMxMinTiles) tpw = kMxMinTiles;
|
|
const unsigned gx = (unsigned)((tiles + tpw * kMxWaves - 1) / (tpw * kMxWaves));
|
|
const int vec = (out_ld % 4 == 0) && ((out & 15) == 0);
|
|
#define RAD_QSA_MX(H) \
|
|
emit_rc = HALO_EMIT(stream, (qsa_score_mx_kernel<H>), dim3(gx, (unsigned)qt), \
|
|
dim3(kMxWaves * kWave), 0, (const unsigned short*)q, \
|
|
(const unsigned short*)bk, \
|
|
(const int32_t*)bt, (const int32_t*)cu, (const int32_t*)nc, \
|
|
(float*)out, M, nb, nseq, (int)tpw, q_ld, bk_ld, bt_ld, out_ld, vec)
|
|
switch (heads) {
|
|
case 1: RAD_QSA_MX(1); break;
|
|
case 2: RAD_QSA_MX(2); break;
|
|
case 3: RAD_QSA_MX(3); break;
|
|
case 4: RAD_QSA_MX(4); break;
|
|
default: return HALO_NO(RAD_E_SHAPE);
|
|
}
|
|
#undef RAD_QSA_MX
|
|
return emit_rc;
|
|
}
|
|
const unsigned nbg = (unsigned)((nb + kWaves * kBPW - 1) / (kWaves * kBPW));
|
|
const unsigned nmg = (unsigned)((M + kQT - 1) / kQT);
|
|
/* Dims per lane (n / 32) are a template parameter: as a runtime value the dot is a rolled loop
|
|
* and the block key a runtime-indexed array in scratch. The instantiations cover every n and
|
|
* head count the checks admit (n % 32 == 0, n <= 256, heads <= 8). */
|
|
#define RAD_QSA_GO(V, H) \
|
|
emit_rc = HALO_EMIT(stream, (qsa_score_kernel<V, H>), dim3(nbg, nmg), dim3(kThreads), 0, \
|
|
(const __hip_bfloat16*)q, (const __hip_bfloat16*)bk, (const int32_t*)bt, \
|
|
(const int32_t*)cu, (const int32_t*)nc, \
|
|
(float*)out, n, heads, nb, nseq, q_ld, bk_ld, bt_ld, out_ld)
|
|
#define RAD_QSA_HEADS(V) \
|
|
switch (heads) { \
|
|
case 1: RAD_QSA_GO(V, 1); break; \
|
|
case 2: RAD_QSA_GO(V, 2); break; \
|
|
case 3: RAD_QSA_GO(V, 3); break; \
|
|
case 4: RAD_QSA_GO(V, 4); break; \
|
|
case 5: RAD_QSA_GO(V, 5); break; \
|
|
case 6: RAD_QSA_GO(V, 6); break; \
|
|
case 7: RAD_QSA_GO(V, 7); break; \
|
|
case 8: RAD_QSA_GO(V, 8); break; \
|
|
default: return HALO_NO(RAD_E_SHAPE); \
|
|
}
|
|
switch (n / kWave) {
|
|
case 1: RAD_QSA_HEADS(1); break;
|
|
case 2: RAD_QSA_HEADS(2); break;
|
|
case 3: RAD_QSA_HEADS(3); break;
|
|
case 4: RAD_QSA_HEADS(4); break;
|
|
case 5: RAD_QSA_HEADS(5); break;
|
|
case 6: RAD_QSA_HEADS(6); break;
|
|
case 7: RAD_QSA_HEADS(7); break;
|
|
case 8: RAD_QSA_HEADS(8); break;
|
|
default: return HALO_NO(RAD_E_SHAPE);
|
|
}
|
|
#undef RAD_QSA_HEADS
|
|
#undef RAD_QSA_GO
|
|
return emit_rc;
|
|
}
|
|
|
|
// ============================================================== the plugin ABI entry point
|
|
|
|
enum { QS_Q = 0, QS_BK = 1, QS_BT = 2, QS_CU = 3, QS_NC = 4, QS_OUT = 5 };
|
|
|
|
extern "C" int halo_rad_qsa_score(const RadArgs* a, RadStream stream) {
|
|
const RadTensor* q = halo_opt(a, QS_Q);
|
|
const RadTensor* bk = halo_opt(a, QS_BK);
|
|
const RadTensor* bt = halo_opt(a, QS_BT);
|
|
const RadTensor* cu = halo_opt(a, QS_CU);
|
|
const RadTensor* nc = halo_opt(a, QS_NC);
|
|
const RadTensor* o = halo_opt(a, QS_OUT);
|
|
if (!q || !bk || !bt || !cu || !nc || !o) return HALO_NO(RAD_E_INVAL);
|
|
/* bk may be a rank-4 KV cache (see halo_qsa_block_key_bf16.hip); a page holds n contiguous
|
|
* values either way. */
|
|
if (bk->rank < 2 || bt->rank != 2 || o->rank != 2) return HALO_NO(RAD_E_SHAPE);
|
|
const long long n = halo_geti_or(a, "n", 0);
|
|
const long long heads = halo_geti_or(a, "heads", 0);
|
|
if (n <= 0 || heads <= 0) return HALO_NO(RAD_E_INVAL);
|
|
const long long M = o->shape[0];
|
|
/* nb = min(table width, score-plane width). Both are sized for max_ctx / ratio (by the KV
|
|
* manager and the architecture respectively, and may round differently), so the minimum is
|
|
* never below the live count. qsa_select bounds its reads by nc regardless. */
|
|
const long long nb = bt->shape[1] < o->shape[1] ? bt->shape[1] : o->shape[1];
|
|
if (M < 1 || nb < 1) return HALO_NO(RAD_E_SHAPE);
|
|
/* The sequence count comes from cu, not the table height (max_seqs), as in qsa_work and
|
|
* qsa_tail_store. */
|
|
if (cu->rank != 1 || cu->shape[0] < 2) return HALO_NO(RAD_E_SHAPE);
|
|
const long long nseq = cu->shape[0] - 1;
|
|
if (bt->shape[0] < nseq || halo_numel(nc) < nseq) return HALO_NO(RAD_E_SHAPE);
|
|
if (bk->shape[0] < 1 || halo_numel(bk) / bk->shape[0] != n) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(q) < M * heads * n) return HALO_NO(RAD_E_SHAPE);
|
|
const long long qp = halo_row_pitch(q), kp = halo_row_pitch(bk);
|
|
const long long tp = halo_row_pitch(bt), op = halo_row_pitch(o);
|
|
if (qp < 0 || kp < 0 || tp < 0 || op < 0) return HALO_NO(RAD_E_STRIDE);
|
|
return halo_qsa_score_bf16_emit(halo_addr(q), halo_addr(bk), halo_addr(bt), halo_addr(cu),
|
|
halo_addr(nc), halo_addr(o),
|
|
(int)M, (int)n, (int)heads, (int)nb, (int)nseq,
|
|
(int)qp, (int)kp, (int)tp, (int)op, stream);
|
|
}
|
|
|