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.
442 lines
22 KiB
Plaintext
442 lines
22 KiB
Plaintext
/* Derived from libr4d/r4d_attn_dense_bf16.hip by the libhalo rename transform (2026-10-26),
|
|
* then ported to gfx1151; see libhalo/README.md. */
|
|
// Halo dense attention, varlen over cu_seqlens segments, grouped-query: an encoder's, non-causal with
|
|
// an optional two-sided window, i - j < window behind and j - i < wright ahead (wright is
|
|
// `window_right`, or `window` for the symmetric |i - j| < window), or causal with an optional
|
|
// window behind (a codec's sliding layers, a text encoder's prompt). bf16 in and out, f16
|
|
// operands, f32 accumulate. Head widths 64, 256 and 512 are compiled; each (head_dim, gqa) pair
|
|
// is a row.
|
|
//
|
|
// Core from the vision kernel (halo_attn_vit_h72_bf16.hip): S^T = K.Q^T, so the wave32
|
|
// column-distributed accumulator gives each lane one query row, softmax state is lane-private, and
|
|
// the C fragment of S^T is already the B fragment of P.V, so P never goes through LDS. Additions:
|
|
//
|
|
// GQA in the workgroup: a workgroup owns one KV head and all query heads reading it, so each
|
|
// staged K/V tile feeds GQA times as many WMMAs. Grid is (query blocks, kv heads, segments).
|
|
//
|
|
// Window: the key range is cut to keys any of the workgroup's queries can see, and the
|
|
// per-element mask runs only on tiles straddling a window edge or the segment end. Sliding
|
|
// layers cost O(T * window), not O(T^2).
|
|
//
|
|
// Blocks: `block` cuts a segment into blocks of that many rows from its start, each attending
|
|
// only within itself (a chunked audio encoder's windows). The key range is cut to the blocks
|
|
// the workgroup's queries are in, and the per-element test runs only where a workgroup or a
|
|
// tile straddles a block edge. `cu_div` reads cu_seqlens in units that many rows apart.
|
|
//
|
|
// Head split across waves (DSPLIT): a lane's output is head_dim / 2 floats, so head_dim 512
|
|
// would need 256 accumulator VGPRs. At DSPLIT 2 two waves share 16 queries and each owns half
|
|
// the head: partial q.k scores are summed through LDS, both run the same softmax (a + b == b + a,
|
|
// so both copies agree bit for bit), and each accumulates its half of P.V.
|
|
//
|
|
// Every operand is read at its own token pitch, so q, k and v may be column slices of one fused
|
|
// projection; pitches and head offsets must be multiples of 8 elements for the 16-byte fetches.
|
|
#include "halo.h"
|
|
#include "halo_args.h"
|
|
#include "halo_common.h"
|
|
#include "halo_dt16.h"
|
|
|
|
#include <cmath>
|
|
|
|
template<int HD, int GQA, int NQB, int DSPLIT, int TILE>
|
|
__global__ __launch_bounds__(NQB * GQA * DSPLIT * 32)
|
|
void halo_attn_dense_kernel(const uint16_t* __restrict__ gq, const uint16_t* __restrict__ gk,
|
|
const uint16_t* __restrict__ gv, uint16_t* __restrict__ go,
|
|
const int* __restrict__ cu, float scale, int window, int wright,
|
|
int block, int cdiv, int causal, int q_ld, int k_ld, int v_ld, int o_ld,
|
|
int krep)
|
|
{
|
|
typedef DT16<1> D;
|
|
typedef D::frag frag16;
|
|
|
|
constexpr int NWARPS = NQB * GQA * DSPLIT;
|
|
constexpr int NTHR = NWARPS * 32;
|
|
constexpr int BQ = NQB * 16; // query rows per head per workgroup
|
|
constexpr int HDP = HD / DSPLIT; // the head dims one wave owns
|
|
constexpr int NKS = HDP / 16; // its 16-wide k-tiles
|
|
constexpr int MT = TILE / 16;
|
|
// Row strides padded by 8 shorts: 16-byte aligned, and off the bank period.
|
|
constexpr int KSTR = HD + 8;
|
|
constexpr int VSTR = TILE + 8;
|
|
constexpr int DG = HD / 8; // eight-element dim groups in a row
|
|
constexpr int KGRP = TILE / 8; // eight-key groups in a tile
|
|
constexpr int KITEMS = TILE * DG; // 16-byte K fetches a tile
|
|
constexpr int KP = (KITEMS + NTHR - 1) / NTHR;
|
|
constexpr int NTR = KGRP * DG; // transposed V units, eight lanes each
|
|
constexpr int LGRP = NTHR / 8;
|
|
constexpr int VP = (NTR + LGRP - 1) / LGRP;
|
|
constexpr float GROW = D::GROW;
|
|
static_assert(HD % (16 * DSPLIT) == 0 && TILE % 16 == 0, "geometry");
|
|
static_assert(DSPLIT == 1 || DSPLIT == 2, "a head is split across at most two waves");
|
|
|
|
__shared__ __align__(16) uint16_t sK[TILE * KSTR];
|
|
__shared__ __align__(16) uint16_t sV[HD * VSTR];
|
|
__shared__ __align__(16) float sX[DSPLIT > 1 ? NWARPS * 32 * 8 : 4];
|
|
|
|
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
|
|
const int c = lane & 15, h = lane >> 4;
|
|
const int dp = warp % DSPLIT;
|
|
const int g = (warp / DSPLIT) % GQA;
|
|
const int qbi = warp / (DSPLIT * GQA);
|
|
// blockIdx.y is a group of GQA query heads; krep groups read one KV head.
|
|
const int kvh = blockIdx.y / krep, seq = blockIdx.z;
|
|
const int hq = blockIdx.y * GQA + g;
|
|
|
|
const int s0 = cu[seq] / cdiv, slen = cu[seq + 1] / cdiv - s0;
|
|
const int q0 = blockIdx.x * BQ;
|
|
if (slen <= 0 || q0 >= slen) return;
|
|
const int qlast = min(q0 + BQ, slen) - 1;
|
|
|
|
// The keys any query of this workgroup can see: none past its last query when causal.
|
|
int kb = 0, ke = slen;
|
|
if (window > 0) {
|
|
kb = max(0, q0 - (window - 1));
|
|
if (!causal) ke = min(slen, qlast + wright);
|
|
}
|
|
if (causal) ke = min(ke, qlast + 1);
|
|
const bool blocks = block > 0;
|
|
if (blocks) {
|
|
kb = max(kb, q0 / block * block);
|
|
ke = min(ke, (qlast / block + 1) * block);
|
|
}
|
|
// A workgroup whose queries are all in one block masks only the tiles crossing its edges.
|
|
const bool q_straddle = blocks && q0 / block != qlast / block;
|
|
|
|
const float mul = scale * 1.44269504089f;
|
|
const int qpos = q0 + qbi * 16 + c;
|
|
const bool live = qpos < slen;
|
|
|
|
// ---- this wave's half (or whole) of its 16 query rows, scaled once -------------------------
|
|
frag16 qf[NKS];
|
|
{
|
|
const int qrow = live ? qpos : slen - 1;
|
|
const uint16_t* qp = gq + (long)(s0 + qrow) * q_ld + (long)hq * HD + dp * HDP;
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t) {
|
|
const uint4 u0 = *(const uint4*)(qp + 16 * t);
|
|
const uint4 u1 = *(const uint4*)(qp + 16 * t + 8);
|
|
auto cv = [&](uint32_t w) {
|
|
return D::pk(__builtin_bit_cast(float, w << 16) * mul,
|
|
__builtin_bit_cast(float, w & 0xffff0000u) * mul);
|
|
};
|
|
const uint32_t w8[8] = { cv(u0.x), cv(u0.y), cv(u0.z), cv(u0.w),
|
|
cv(u1.x), cv(u1.y), cv(u1.z), cv(u1.w) };
|
|
qf[t] = D::mk(w8);
|
|
}
|
|
}
|
|
|
|
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};
|
|
|
|
const int vj = tid & 7, lgi = tid >> 3;
|
|
const uint16_t* kbase = gk + (long)kvh * HD;
|
|
const uint16_t* vbase = gv + (long)kvh * HD;
|
|
|
|
const int ntiles = (ke - kb + TILE - 1) / TILE;
|
|
for (int ti = 0; ti < ntiles; ++ti) {
|
|
const int k0 = kb + ti * TILE;
|
|
// Whether any (query, key) pair of this tile is outside the window or past the keys.
|
|
// Workgroup-uniform, so interior tiles skip the test.
|
|
const bool mask = (k0 + TILE > ke) ||
|
|
(causal && k0 + TILE - 1 > q0) ||
|
|
(window > 0 && (k0 < qlast - (window - 1) ||
|
|
k0 + TILE - 1 > q0 + (wright - 1))) ||
|
|
(blocks && (q_straddle || k0 / block != (k0 + TILE - 1) / block ||
|
|
k0 / block != q0 / block));
|
|
|
|
uint4 kr[KP], vr[VP];
|
|
#pragma unroll
|
|
for (int p = 0; p < KP; ++p) {
|
|
const int i = tid + p * NTHR;
|
|
if (KITEMS % NTHR == 0 || i < KITEMS) {
|
|
const int key = i / DG, gg = i % DG;
|
|
const int kk = (k0 + key < ke) ? k0 + key : ke - 1;
|
|
kr[p] = *(const uint4*)(kbase + (long)(s0 + kk) * k_ld + gg * 8);
|
|
}
|
|
}
|
|
// V transposed on load: a group's eight lanes name eight key rows at the same eight dims,
|
|
// and global_load_tr_b128 gives lane j dim d0+j across all eight.
|
|
#pragma unroll
|
|
for (int r = 0; r < VP; ++r) {
|
|
const int u = lgi + r * LGRP;
|
|
if (NTR % LGRP == 0 || u < NTR) {
|
|
const int kg = u % KGRP, dg = u / KGRP;
|
|
const int kk0 = k0 + kg * 8 + vj;
|
|
const int kk = (kk0 < ke) ? kk0 : ke - 1;
|
|
vr[r] = __builtin_bit_cast(uint4, load_tr_b128(vbase + (long)(s0 + kk) * v_ld +
|
|
dg * 8));
|
|
}
|
|
}
|
|
|
|
lds_barrier(); // the previous tile's readers are done
|
|
#pragma unroll
|
|
for (int r = 0; r < VP; ++r) {
|
|
const int u = lgi + r * LGRP;
|
|
if (NTR % LGRP == 0 || u < NTR) {
|
|
const int kg = u % KGRP, dg = u / KGRP;
|
|
const uint4 w = vr[r];
|
|
*(uint4*)&sV[(dg * 8 + vj) * VSTR + kg * 8] =
|
|
make_uint4(D::from_bf16w(w.x), D::from_bf16w(w.y),
|
|
D::from_bf16w(w.z), D::from_bf16w(w.w));
|
|
}
|
|
}
|
|
#pragma unroll
|
|
for (int p = 0; p < KP; ++p) {
|
|
const int i = tid + p * NTHR;
|
|
if (KITEMS % NTHR == 0 || i < KITEMS) {
|
|
const uint4 w = kr[p];
|
|
*(uint4*)&sK[(i / DG) * KSTR + (i % DG) * 8] =
|
|
make_uint4(D::from_bf16w(w.x), D::from_bf16w(w.y),
|
|
D::from_bf16w(w.z), D::from_bf16w(w.w));
|
|
}
|
|
}
|
|
lds_barrier();
|
|
|
|
#pragma unroll 1
|
|
for (int mt = 0; mt < MT; ++mt) {
|
|
v8f s = (v8f){0, 0, 0, 0, 0, 0, 0, 0};
|
|
{
|
|
const uint16_t* krow = &sK[(mt * 16 + c) * KSTR + dp * HDP];
|
|
#pragma unroll
|
|
for (int t0 = 0; t0 < NKS; t0 += 4) {
|
|
frag16 kf[4];
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j)
|
|
if (t0 + j < NKS) {
|
|
const uint4 u0 = *(const uint4*)(krow + 16 * (t0 + j));
|
|
const uint4 u1 = *(const uint4*)(krow + 16 * (t0 + j) + 8);
|
|
const uint32_t w8[8] = { u0.x, u0.y, u0.z, u0.w,
|
|
u1.x, u1.y, u1.z, u1.w };
|
|
kf[j] = D::mk(w8);
|
|
}
|
|
sched_barrier();
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j)
|
|
if (t0 + j < NKS) s = D::wmma(kf[j], qf[t0 + j], s);
|
|
}
|
|
}
|
|
if (DSPLIT > 1) {
|
|
// The other half of q.k, from the wave that owns it.
|
|
float* mine = &sX[(warp * 32 + lane) * 8];
|
|
const float* theirs = &sX[((warp ^ 1) * 32 + lane) * 8];
|
|
*(float4*)mine = make_float4(s[0], s[1], s[2], s[3]);
|
|
*(float4*)(mine + 4) = make_float4(s[4], s[5], s[6], s[7]);
|
|
lds_barrier();
|
|
const float4 a = *(const float4*)theirs, b = *(const float4*)(theirs + 4);
|
|
s[0] += a.x; s[1] += a.y; s[2] += a.z; s[3] += a.w;
|
|
s[4] += b.x; s[5] += b.y; s[6] += b.z; s[7] += b.w;
|
|
lds_barrier();
|
|
}
|
|
|
|
float sc[8];
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) sc[e] = s[e];
|
|
if (mask) {
|
|
const int kq = k0 + mt * 16;
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) {
|
|
const int key = kq + 2 * e + h;
|
|
const int d = qpos - key;
|
|
const bool ok = key < ke && (!causal || d >= 0) &&
|
|
(window <= 0 || (d < window && -d < wright)) &&
|
|
(!blocks || key / block == qpos / block);
|
|
sc[e] = ok ? sc[e] : -INFINITY;
|
|
}
|
|
}
|
|
|
|
// Softmax against a reference max, rescaled only when the row max outgrows the f16
|
|
// P range (halo_dt16.h).
|
|
float smax = sc[0];
|
|
#pragma unroll
|
|
for (int e = 1; e < 8; ++e) smax = fmaxf(smax, sc[e]);
|
|
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;
|
|
}
|
|
|
|
uint32_t pw[4];
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; e += 2)
|
|
pw[e >> 1] = D::pk(ex2(sc[e] + soff), ex2(sc[e + 1] + soff));
|
|
// Row sum from the packed P, so the denominator matches the values P.V used.
|
|
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);
|
|
uint32_t pw16[8];
|
|
p16w<D>(pw, pw16);
|
|
const frag16 p16 = D::mk(pw16);
|
|
|
|
{
|
|
const uint16_t* vrow = &sV[(dp * HDP + c) * VSTR + mt * 16];
|
|
#pragma unroll
|
|
for (int t0 = 0; t0 < NKS; t0 += 4) {
|
|
frag16 vf[4];
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j)
|
|
if (t0 + j < NKS) {
|
|
const uint4 u0 = *(const uint4*)(vrow + (t0 + j) * 16 * VSTR);
|
|
const uint4 u1 = *(const uint4*)(vrow + (t0 + j) * 16 * VSTR + 8);
|
|
const uint32_t w8[8] = { u0.x, u0.y, u0.z, u0.w,
|
|
u1.x, u1.y, u1.z, u1.w };
|
|
vf[j] = D::mk(w8);
|
|
}
|
|
sched_barrier();
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j)
|
|
if (t0 + j < NKS) acc[t0 + j] = D::wmma(vf[j], p16, acc[t0 + j]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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)(s0 + qpos) * o_ld + (long)hq * HD + dp * HDP;
|
|
// gfx1151: element e of the accumulator is head dim 2*e + (lane>>4), one value a lane, so
|
|
// the store is a scatter of eight 16-bit values rather than one 16-byte write.
|
|
#pragma unroll
|
|
for (int t = 0; t < NKS; ++t)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e)
|
|
((uint16_t*)op)[t * 16 + 2 * e + h] = f32_to_bf16(acc[t][e] * inv);
|
|
}
|
|
|
|
// ---- launch ----------------------------------------------------------------------------------
|
|
// One instantiation per (head_dim, gqa), at a workgroup shape picked for it:
|
|
// h64 gqa1: 8 query blocks of 16, K/V tiles of 64 keys (the vision towers' heads)
|
|
// h256 gqa1: 8 query blocks of 16, tiles of 32 (LDS: 37 KiB)
|
|
// h256 gqa2, 4, 8: 8, 4, 2 query blocks x the heads, tiles of 32 (LDS: 37 KiB)
|
|
// h512 gqa2, 4, 8: 4, 2, 1 query blocks x the heads x 2 head halves, tiles of 16 (LDS: 57 KiB)
|
|
// The LDS holds one K/V tile, so the query heads a workgroup carries change its waves, not it.
|
|
// A wider group (16 query heads a KV head and up, any multiple of 8) runs as krep workgroups of
|
|
// eight heads each over the same KV head: 16 heads in one would need 32 waves and, at h512, more
|
|
// than 64 KiB of LDS.
|
|
template<int HD, int GQA, int NQB, int DSPLIT, int TILE>
|
|
static int halo_attn_dense_launch(const void* q, const void* k, const void* v, void* o,
|
|
const void* cu, int num_seqs, int max_seqlen, int n_head_kv,
|
|
float scale, int window, int wright, int block, int cdiv,
|
|
int causal, int q_ld, int k_ld, int v_ld, int o_ld, hipStream_t s,
|
|
int krep = 1)
|
|
{
|
|
constexpr int BQ = NQB * 16;
|
|
dim3 grid((max_seqlen + BQ - 1) / BQ, n_head_kv * krep, num_seqs);
|
|
hipLaunchKernelGGL((halo_attn_dense_kernel<HD, GQA, NQB, DSPLIT, TILE>), grid,
|
|
dim3(NQB * GQA * DSPLIT * 32), 0, s, (const uint16_t*)q, (const uint16_t*)k,
|
|
(const uint16_t*)v, (uint16_t*)o, (const int*)cu, scale, window, wright,
|
|
block, cdiv, causal, q_ld, k_ld, v_ld, o_ld, krep);
|
|
const hipError_t e = hipGetLastError();
|
|
return e == hipSuccess ? 0 : -100 - (int)e;
|
|
}
|
|
|
|
// q, o: [tokens, n_head * head_dim]; k, v: [tokens, n_head_kv * head_dim], each at its own token
|
|
// pitch in elements. cu_seqlens is [num_seqs + 1] int32 on the device; max_seqlen is a host-side
|
|
// bound on the segment lengths, in rows, and sizes the grid. window 0 is the whole segment (with
|
|
// causal, every earlier key), wright 0 the window's own right side, block 0 no blocks; cu_seqlens
|
|
// counts in units cdiv rows apart.
|
|
extern "C" int halo_attn_dense_bf16(const void* q, const void* k, const void* v, void* o,
|
|
const void* cu_seqlens, int num_seqs, int max_seqlen,
|
|
int n_head, int n_head_kv, int head_dim, float scale,
|
|
int window, int block, int cdiv, int q_ld, int k_ld, int v_ld,
|
|
int o_ld, void* stream, int wright = 0, int causal = 0)
|
|
{
|
|
if (n_head <= 0 || n_head_kv <= 0 || n_head % n_head_kv) return -2;
|
|
if ((q_ld | k_ld | v_ld | o_ld) & 7) return -3;
|
|
if (window < 0 || wright < 0 || block < 0 || cdiv <= 0 ||
|
|
(wright > 0 && (window == 0 || causal)))
|
|
return -4;
|
|
if (num_seqs <= 0 || max_seqlen <= 0) return 0;
|
|
if (wright == 0) wright = window;
|
|
int gqa = n_head / n_head_kv, krep = 1;
|
|
if (gqa > 8 && gqa % 8 == 0) krep = gqa / 8, gqa = 8;
|
|
hipStream_t s = (hipStream_t)stream;
|
|
if (head_dim == 64 && gqa == 1)
|
|
return halo_attn_dense_launch<64, 1, 8, 1, 64>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen,
|
|
n_head_kv, scale, window, wright, block,
|
|
cdiv, causal, q_ld, k_ld, v_ld, o_ld, s);
|
|
#define HALO_DENSE(HD, G, NQB, DS, T) \
|
|
return halo_attn_dense_launch<HD, G, NQB, DS, T>(q, k, v, o, cu_seqlens, num_seqs, max_seqlen, \
|
|
n_head_kv, scale, window, wright, block, \
|
|
cdiv, causal, q_ld, k_ld, v_ld, o_ld, s, krep)
|
|
if (head_dim == 256) {
|
|
switch (gqa) {
|
|
case 1: HALO_DENSE(256, 1, 8, 1, 32);
|
|
case 2: HALO_DENSE(256, 2, 8, 1, 32);
|
|
case 4: HALO_DENSE(256, 4, 4, 1, 32);
|
|
case 8: HALO_DENSE(256, 8, 2, 1, 32);
|
|
default: return -1;
|
|
}
|
|
}
|
|
if (head_dim == 512) {
|
|
switch (gqa) {
|
|
case 2: HALO_DENSE(512, 2, 4, 2, 16);
|
|
case 4: HALO_DENSE(512, 4, 2, 2, 16);
|
|
case 8: HALO_DENSE(512, 8, 1, 2, 16);
|
|
default: return -1;
|
|
}
|
|
}
|
|
#undef HALO_DENSE
|
|
return -1;
|
|
}
|
|
|
|
// ============================================================== the plugin ABI entry point
|
|
|
|
enum { DN_Q = 0, DN_K, DN_V, DN_CU, DN_OUT };
|
|
|
|
extern "C" int halo_rad_attn_dense(const RadArgs* a, RadStream s) {
|
|
const RadTensor* q = halo_opt(a, DN_Q);
|
|
const RadTensor* k = halo_opt(a, DN_K);
|
|
const RadTensor* v = halo_opt(a, DN_V);
|
|
const RadTensor* cu = halo_opt(a, DN_CU);
|
|
const RadTensor* o = halo_opt(a, DN_OUT);
|
|
if (!q || !k || !v || !cu || !o) return HALO_NO(RAD_E_INVAL);
|
|
if (!halo_contig(cu)) return HALO_NO(RAD_E_STRIDE);
|
|
const long long causal = halo_geti_or(a, "causal", 0);
|
|
if (causal < 0 || causal > 1) return HALO_NO(RAD_E_SHAPE);
|
|
|
|
const long long head_dim = halo_geti_or(a, "head_dim", 0);
|
|
const long long gqa = halo_geti_or(a, "gqa", 1);
|
|
if (head_dim <= 0 || gqa <= 0) return HALO_NO(RAD_E_SHAPE);
|
|
/* [tokens, heads * head_dim] (or [tokens, heads, head_dim]) at the operand's own row pitch. */
|
|
long long total = 0, nh = 0, q_ld = 0;
|
|
long long tk = 0, hk = 0, k_ld = 0, tv = 0, hv = 0, v_ld = 0, to = 0, ho = 0, o_ld = 0;
|
|
if (!halo_head_rows(q, head_dim, &total, &nh, &q_ld) ||
|
|
!halo_head_rows(k, head_dim, &tk, &hk, &k_ld) ||
|
|
!halo_head_rows(v, head_dim, &tv, &hv, &v_ld) ||
|
|
!halo_head_rows(o, head_dim, &to, &ho, &o_ld))
|
|
return HALO_NO(RAD_E_STRIDE);
|
|
if (hk != hv || ho != nh || hk * gqa != nh) return HALO_NO(RAD_E_SHAPE);
|
|
if (tk < total || tv < total || to < total) return HALO_NO(RAD_E_SHAPE);
|
|
if (cu->rank != 1 || cu->shape[0] < 2) return HALO_NO(RAD_E_SHAPE);
|
|
const int num_seqs = (int)(cu->shape[0] - 1);
|
|
|
|
/* max_seqlen is a grid-sizing bound (the kernel reads device-side cu_seqlens); defaults to
|
|
* the total token count. */
|
|
long long msl = halo_geti_or(a, "max_seqlen", 0);
|
|
if (msl <= 0 || msl > total) msl = total;
|
|
const float scale = (float)halo_getf(a, "scale", 1.0 / std::sqrt((double)head_dim));
|
|
const long long window = halo_geti_or(a, "window", 0);
|
|
const long long wright = halo_geti_or(a, "window_right", 0);
|
|
const long long block = halo_geti_or(a, "block", 0);
|
|
const long long cdiv = halo_geti_or(a, "cu_div", 1);
|
|
/* A right side needs a non-causal window to be the right side of (libref refuses it alike). */
|
|
if (wright < 0 || (wright > 0 && (window == 0 || causal))) return HALO_NO(RAD_E_SHAPE);
|
|
|
|
const int e = halo_attn_dense_bf16(q->data, k->data, v->data, o->data, cu->data, num_seqs,
|
|
(int)msl, (int)nh, (int)hk, (int)head_dim, scale,
|
|
(int)window, (int)block, (int)cdiv, (int)q_ld, (int)k_ld,
|
|
(int)v_ld, (int)o_ld, s, (int)wright, (int)causal);
|
|
if (e == 0) return RAD_OK;
|
|
return HALO_NO(e == -3 ? RAD_E_STRIDE : e <= -100 ? RAD_E_DEVICE : RAD_E_SHAPE);
|
|
}
|