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.
622 lines
30 KiB
Plaintext
622 lines
30 KiB
Plaintext
/* Derived from libr4d/r4d_rowtopk_bf16.hip by the libhalo rename transform (2026-10-26),
|
|
* then ported to gfx1151; see libhalo/README.md. */
|
|
// halo_rowtopk_bf16.hip - halo_rowtopk_bf16: exact per-row top-R of a wide bf16 matrix.
|
|
//
|
|
// Purpose: a quantised draft head produces coarse logits over a vocabulary shard, and only the
|
|
// best few dozen are rescored exactly. A general top-k costs more than that saves: torch.topk over
|
|
// a 15520-wide array takes 132 us at M=64 in three kernels, and several times that over a full
|
|
// 124160-wide row.
|
|
//
|
|
// Two stages, because scanning and selecting want opposite shapes. Reading the matrix wants
|
|
// thousands of waves in flight; selection is a chain of dependent cross-lane steps (~300 cycles a
|
|
// round), too costly to run thousands of times. On a 15.9 MB matrix with a 25 us roofline:
|
|
//
|
|
// 256-thread blocks, R=32 rounds, two barriers a round 75.6 us (chain, plus barriers)
|
|
// one wave per chunk, R=32 rounds, no barrier 68.5 us (chain, or too few waves)
|
|
//
|
|
// So stage 1 runs many waves with a short chain (one wave per (row, chunk), keeping the chunk's
|
|
// top-RC), and stage 2 runs one wave per row over the nch*RC survivors with the full R-round chain.
|
|
//
|
|
// * A lane keeps its own top-PT in registers, sorted. Insertion is guarded by one compare
|
|
// against the tail, so most elements cost one instruction; only a placing element pays the
|
|
// branch-free shift. No LDS candidate array.
|
|
// * A round only removes a list head: the winner is the head of exactly one lane's list, so that
|
|
// lane shifts its own registers. Nothing is re-read per round.
|
|
// * One wave per block, so a round is five xor-butterfly steps and no barrier.
|
|
//
|
|
// The key is one 64-bit integer, so a round is a plain max. The high 32 bits are the float's
|
|
// order-preserving image (invert all bits if negative, set the sign bit if not), the low 32 are
|
|
// ~index, so a larger key is a larger value and, on a tie, the lower column (a dense top-k's tie
|
|
// rule). The index also makes every key distinct, so a round identifies its winner by equality.
|
|
//
|
|
// PT bounds how many relevant candidates one lane's slice can hold:
|
|
//
|
|
// * Stage 1 gives a lane every 32nd eight-column group, and PT must cover a whole vector load:
|
|
// eight bf16 arrive at once, so PT=4 can lose a candidate to a single load (and does disagree
|
|
// with torch.topk on smooth data). PT=8 places one load in full; beyond that a chunk would need
|
|
// nine of its own top-RC in one of 32 slices.
|
|
// * Stage 2's slice is not random: stage 1 writes chunk-major, so at stride 32 with RC=32 lane 0
|
|
// receives every chunk's maximum, exactly where the row's top-R concentrates. Stage 2 therefore
|
|
// uses PT = 32 >= R: one lane can hold the entire answer, so it is exact for any distribution.
|
|
//
|
|
// RC is the only possible approximation, and the caller's choice. A chunk hands on its top-RC, so
|
|
// the row's top-R survives unless more than RC of it falls in one chunk. RC=R is exact for any
|
|
// data; a smaller RC shortens stage 1's chain at a risk that falls fast with the chunk count (at
|
|
// R=32, RC=8 and 64 chunks it takes nine of the 32 in one sixty-fourth of the row). A caller that
|
|
// rescores exactly then loses a candidate, not correctness.
|
|
#include "halo.h"
|
|
#include "halo_emit.h"
|
|
#include "halo_gemm_args.h"
|
|
#include "halo_common.h"
|
|
#include "halo_fp8_wmma.h"
|
|
#include "halo_fp8_lm.h"
|
|
#include "halo_chain.h"
|
|
#include <hip/hip_bf16.h>
|
|
#include <cstdint>
|
|
|
|
#define HALO_TOPK_WAVE 32
|
|
#define HALO_TOPK_PT2 32 // stage 2: >= R, so a lane can hold the whole answer
|
|
|
|
typedef unsigned u4v_tk __attribute__((ext_vector_type(4)));
|
|
typedef __attribute__((address_space(1))) const u4v_tk* halo_tk_gv4;
|
|
|
|
#define HALO_TOPK_WPB 8 // independent waves per block: one chunk each, no barrier between
|
|
#define HALO_TOPK_UNROLL 4 // vector loads issued before any of them is consumed
|
|
|
|
__device__ __forceinline__ unsigned long long tk_key(unsigned short bf, unsigned idx) {
|
|
unsigned u = (unsigned)bf << 16;
|
|
u = (u & 0x80000000u) ? ~u : (u | 0x80000000u);
|
|
return ((unsigned long long)u << 32) | (unsigned)(0xFFFFFFFFu - idx);
|
|
}
|
|
|
|
__device__ __forceinline__ float tk_val(unsigned long long k) {
|
|
unsigned u = (unsigned)(k >> 32);
|
|
u = (u & 0x80000000u) ? (u & 0x7FFFFFFFu) : ~u;
|
|
return __uint_as_float(u);
|
|
}
|
|
|
|
__device__ __forceinline__ int tk_idx(unsigned long long k) {
|
|
return (int)(0xFFFFFFFFu - (unsigned)(k & 0xFFFFFFFFull));
|
|
}
|
|
|
|
// The larger of this lane's key and its partner's at lane ^ N, read through DPP or permlanex16
|
|
// (see halo_xor_lane).
|
|
template <int N>
|
|
__device__ __forceinline__ unsigned long long tk_xor_max(unsigned long long m) {
|
|
const unsigned long long p = halo_xor_lane_u64<N>(m);
|
|
return p > m ? p : m;
|
|
}
|
|
|
|
// The insert's only guard is its own key compare. A float guard against the tail's value costs
|
|
// ~18% at M=64 in stage 1 (a branch breaks the unrolled straight-line body), and it is wrong with
|
|
// NaN: the key orders a positive NaN above +inf and a negative one below -inf, while a float
|
|
// compare is false, so once a NaN is the tail every later candidate would be dropped. Every stage
|
|
// and the merge rank on the key alone, so they agree on one order.
|
|
template <int PT>
|
|
__device__ __forceinline__ void tk_insert(unsigned long long* t, unsigned long long k) {
|
|
if (k > t[PT - 1]) {
|
|
unsigned long long c = k;
|
|
#pragma unroll
|
|
for (int q = 0; q < PT; q++) {
|
|
const unsigned long long hi = t[q] > c ? t[q] : c;
|
|
c = t[q] > c ? c : t[q];
|
|
t[q] = hi;
|
|
}
|
|
}
|
|
}
|
|
|
|
// One reduction round over the wave: every lane gets the winner and its owner drops its head. No
|
|
// LDS and no barrier.
|
|
template <int PT>
|
|
__device__ __forceinline__ unsigned long long tk_round(unsigned long long* t) {
|
|
unsigned long long m = t[0];
|
|
m = tk_xor_max<16>(m);
|
|
m = tk_xor_max<8>(m);
|
|
m = tk_xor_max<4>(m);
|
|
m = tk_xor_max<2>(m);
|
|
m = tk_xor_max<1>(m);
|
|
if (m && t[0] == m) {
|
|
#pragma unroll
|
|
for (int q = 0; q < PT - 1; q++) t[q] = t[q + 1];
|
|
t[PT - 1] = 0ull;
|
|
}
|
|
return m;
|
|
}
|
|
|
|
// The body; the plain kernel and its chained form (halo_chain.h) wrap it below. A wave past the
|
|
// chunks returns early; there is no barrier in it.
|
|
template <int PT>
|
|
__device__ __forceinline__ void halo_rowtopk_stage1_body(
|
|
const unsigned short* __restrict__ Y, unsigned long long* __restrict__ P,
|
|
int N, int RC, int NCH, int CH) {
|
|
// WPB independent waves per block, each on its own chunk, sharing nothing. Grouping them only
|
|
// matters because 32-thread blocks exhaust the CU's workgroup slots before its wave slots.
|
|
const int m = blockIdx.y;
|
|
const int c = blockIdx.x * HALO_TOPK_WPB + (int)(threadIdx.x >> 5);
|
|
if (c >= NCH) return;
|
|
const int lane = threadIdx.x & 31;
|
|
const int c0 = c * CH;
|
|
int c1 = c0 + CH;
|
|
if (c1 > N) c1 = N;
|
|
|
|
unsigned long long t[PT];
|
|
#pragma unroll
|
|
for (int q = 0; q < PT; q++) t[q] = 0ull;
|
|
|
|
const unsigned short* row = Y + (size_t)m * N;
|
|
// Eight bf16 per lane per load, so the wave moves 512 contiguous bytes per issue. UNROLL loads
|
|
// are issued before the first is consumed, since the insert chain would otherwise put a memory
|
|
// latency between consecutive loads.
|
|
const int v0 = c0 >> 3, v1 = c1 >> 3;
|
|
const int step = HALO_TOPK_WAVE * HALO_TOPK_UNROLL;
|
|
int v = v0 + lane;
|
|
for (; v + step - HALO_TOPK_WAVE < v1; v += step) {
|
|
u4v_tk p[HALO_TOPK_UNROLL];
|
|
#pragma unroll
|
|
for (int u = 0; u < HALO_TOPK_UNROLL; u++)
|
|
p[u] = *(halo_tk_gv4)(row + (size_t)(v + u * HALO_TOPK_WAVE) * 8);
|
|
#pragma unroll
|
|
for (int u = 0; u < HALO_TOPK_UNROLL; u++) {
|
|
const unsigned base = (unsigned)((v + u * HALO_TOPK_WAVE) * 8);
|
|
#pragma unroll
|
|
for (int e = 0; e < 4; e++) {
|
|
tk_insert<PT>(t, tk_key((unsigned short)(p[u][e] & 0xFFFFu), base + 2 * e));
|
|
tk_insert<PT>(t, tk_key((unsigned short)(p[u][e] >> 16), base + 2 * e + 1));
|
|
}
|
|
}
|
|
}
|
|
for (; v < v1; v += HALO_TOPK_WAVE) {
|
|
const u4v_tk p = *(halo_tk_gv4)(row + (size_t)v * 8);
|
|
const unsigned base = (unsigned)(v * 8);
|
|
#pragma unroll
|
|
for (int e = 0; e < 4; e++) {
|
|
tk_insert<PT>(t, tk_key((unsigned short)(p[e] & 0xFFFFu), base + 2 * e));
|
|
tk_insert<PT>(t, tk_key((unsigned short)(p[e] >> 16), base + 2 * e + 1));
|
|
}
|
|
}
|
|
for (int i = (v1 << 3) + lane; i < c1; i += HALO_TOPK_WAVE) {
|
|
tk_insert<PT>(t, tk_key(row[i], (unsigned)i));
|
|
}
|
|
|
|
unsigned long long* out = P + ((size_t)m * NCH + c) * RC;
|
|
for (int r = 0; r < RC; r++) {
|
|
const unsigned long long w = tk_round<PT>(t);
|
|
if (lane == 0) out[r] = w;
|
|
}
|
|
}
|
|
template <int PT>
|
|
__global__ __launch_bounds__(HALO_TOPK_WAVE * HALO_TOPK_WPB) void halo_rowtopk_stage1_kernel(
|
|
const unsigned short* __restrict__ Y, unsigned long long* __restrict__ P,
|
|
int N, int RC, int NCH, int CH) {
|
|
halo_rowtopk_stage1_body<PT>(Y, P, N, RC, NCH, CH);
|
|
}
|
|
template <int PT>
|
|
__global__ __launch_bounds__(HALO_TOPK_WAVE * HALO_TOPK_WPB) void halo_r4dchain_rowtopk_stage1(
|
|
const unsigned short* __restrict__ Y, unsigned long long* __restrict__ P,
|
|
int N, int RC, int NCH, int CH) {
|
|
halo_chain_wait();
|
|
halo_rowtopk_stage1_body<PT>(Y, P, N, RC, NCH, CH);
|
|
halo_chain_done();
|
|
}
|
|
|
|
// `off` is the first global column this rank's plane stands for, and `OP` the optional (id, value)
|
|
// pair plane an all-gather moves: eight bytes an entry, an int32 id in the first float slot, the
|
|
// layout core/sample/sampler.h holds as buf_pairs_. Both are inert at off = 0 with OP null, which
|
|
// is the single-rank launch bit for bit.
|
|
//
|
|
// An empty slot is not token `off`. A chunk that ran out of candidates leaves a zero key; OI gets
|
|
// column 0 for it, without the offset, so a pad cannot look like this rank's first real token. The
|
|
// pair plane writes -1, the sentinel the merge drops (not -INFINITY, a valid masked logit).
|
|
// The body; the plain kernel and its chained form (halo_chain.h) wrap it below.
|
|
__device__ __forceinline__ void halo_rowtopk_stage2_body(
|
|
const unsigned long long* __restrict__ P, float* __restrict__ OV, int* __restrict__ OI,
|
|
float* __restrict__ OP, int off, int R, int n) {
|
|
const int m = blockIdx.x;
|
|
const int lane = threadIdx.x;
|
|
|
|
unsigned long long t[HALO_TOPK_PT2];
|
|
#pragma unroll
|
|
for (int q = 0; q < HALO_TOPK_PT2; q++) t[q] = 0ull;
|
|
|
|
// The key decides, as in stage 1 (see tk_insert). A NaN a chunk handed on displaced a real
|
|
// candidate, so dropping it here can leave the row short of R; the pad filling the gap is what
|
|
// the merge reports as id -1. An empty slot's key is 0 and never places.
|
|
const unsigned long long* p = P + (size_t)m * n;
|
|
for (int i = lane; i < n; i += HALO_TOPK_WAVE) tk_insert<HALO_TOPK_PT2>(t, p[i]);
|
|
|
|
for (int r = 0; r < R; r++) {
|
|
const unsigned long long w = tk_round<HALO_TOPK_PT2>(t);
|
|
if (lane == 0) {
|
|
// A zero key is an empty slot, not a real column.
|
|
const float v = w ? tk_val(w) : -INFINITY;
|
|
OV[(size_t)m * R + r] = v;
|
|
OI[(size_t)m * R + r] = w ? (int)tk_idx(w) + off : 0;
|
|
if (OP) {
|
|
float* const q = OP + ((size_t)m * R + r) * 2;
|
|
const int id = w ? (int)tk_idx(w) + off : -1;
|
|
__builtin_memcpy(q, &id, sizeof(int));
|
|
q[1] = v;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
__global__ __launch_bounds__(HALO_TOPK_WAVE) void halo_rowtopk_stage2_kernel(
|
|
const unsigned long long* __restrict__ P, float* __restrict__ OV, int* __restrict__ OI,
|
|
float* __restrict__ OP, int off, int R, int n) {
|
|
halo_rowtopk_stage2_body(P, OV, OI, OP, off, R, n);
|
|
}
|
|
__global__ __launch_bounds__(HALO_TOPK_WAVE) void halo_r4dchain_rowtopk_stage2(
|
|
const unsigned long long* __restrict__ P, float* __restrict__ OV, int* __restrict__ OI,
|
|
float* __restrict__ OP, int off, int R, int n) {
|
|
halo_chain_wait();
|
|
halo_rowtopk_stage2_body(P, OV, OI, OP, off, R, n);
|
|
halo_chain_done();
|
|
}
|
|
|
|
// Chunks are a multiple of eight columns so stage 1 stays on the vector load; the last one is
|
|
// clamped to N rather than padded (a padded tail would need a mask per element). The caller sizes
|
|
// `partial` from halo_rowtopk_bf16_chunks, so the rounding lives here only.
|
|
static int rowtopk_chunk_width(int N, int NCH) {
|
|
const int CH = ((N + NCH - 1) / NCH + 7) & ~7;
|
|
return CH < 8 ? 8 : CH;
|
|
}
|
|
|
|
int halo_rowtopk_bf16_chunks(int N, int NCH) {
|
|
const int CH = rowtopk_chunk_width(N, NCH);
|
|
return (N + CH - 1) / CH;
|
|
}
|
|
|
|
// The top-R over one rank's slice of a wider plane: `off` is the slice's first global column, and
|
|
// `outp` (optional, 0 for none) is the M*R (int32 id, float value) pair plane an all-gather moves
|
|
// so ranks can agree on a global top-R without gathering the plane.
|
|
static int rowtopk_bf16_sharded_emit(long y, long partial, long outv, long outi, long outp,
|
|
int M, int N, int R, int RC, int NCH, int PT1, int off,
|
|
RadStream stream, bool chain = false) {
|
|
int emit_rc = RAD_OK;
|
|
// R <= PT2 is what makes stage 2 exact for any candidates; a real limit, so checked, not
|
|
// clamped.
|
|
if (M < 1 || N < 1 || N % 8 || R < 1 || R > HALO_TOPK_PT2 || RC < 1 ||
|
|
RC > PT1 * HALO_TOPK_WAVE || NCH < 1)
|
|
return HALO_NO(RAD_E_SHAPE);
|
|
const int CH = rowtopk_chunk_width(N, NCH);
|
|
const int nch = (N + CH - 1) / CH;
|
|
#define HALO_TK_S1(PT) emit_rc = HALO_EMIT(stream, (chain ? halo_r4dchain_rowtopk_stage1<PT> \
|
|
: halo_rowtopk_stage1_kernel<PT>), \
|
|
dim3((nch + HALO_TOPK_WPB - 1) / HALO_TOPK_WPB, M), \
|
|
dim3(HALO_TOPK_WAVE * HALO_TOPK_WPB), 0, \
|
|
(const unsigned short*)y, (unsigned long long*)partial, N, RC, nch, CH)
|
|
switch (PT1) {
|
|
case 2: HALO_TK_S1(2); break;
|
|
case 4: HALO_TK_S1(4); break;
|
|
case 8: HALO_TK_S1(8); break;
|
|
case 16: HALO_TK_S1(16); break;
|
|
default: return HALO_NO(RAD_E_INVAL);
|
|
}
|
|
#undef HALO_TK_S1
|
|
if (emit_rc != RAD_OK) return emit_rc;
|
|
return HALO_EMIT(stream, (chain ? halo_r4dchain_rowtopk_stage2 : halo_rowtopk_stage2_kernel), dim3(M),
|
|
dim3(HALO_TOPK_WAVE), 0,
|
|
(const unsigned long long*)partial, (float*)outv, (int*)outi,
|
|
(float*)outp, off, R, nch * RC);
|
|
}
|
|
|
|
// ============================================================ the vocab-parallel merge
|
|
//
|
|
// The other half of a sharded top-R. Each rank ran the two stages over its own slice and wrote an
|
|
// [M, R, 2] pair plane (int32 global id, float value); an all-gather concatenated those into
|
|
// [M, W*R, 2] with rank r's at [r*R, (r+1)*R). This picks the R largest of the W*R and writes them
|
|
// in stage 2's (idx, val) form.
|
|
//
|
|
// The key is the same 64-bit integer, built from the float's bits, so a round is a plain max and
|
|
// ties break on the lower id, which makes this equal to a top-R over the unsharded plane. A pad
|
|
// carries id -1 and is skipped (a sentinel, since -INFINITY is a legitimate masked logit).
|
|
//
|
|
// One wave per row over W*R entries (32 at world 2, R 16); its cost is its launch.
|
|
__device__ __forceinline__ unsigned long long tk_key_f32(float v, unsigned id) {
|
|
unsigned u = __float_as_uint(v);
|
|
u = (u & 0x80000000u) ? ~u : (u | 0x80000000u);
|
|
return ((unsigned long long)u << 32) | (unsigned)(0xFFFFFFFFu - id);
|
|
}
|
|
|
|
// The body; the plain kernel and its chained form (halo_chain.h) wrap it below.
|
|
__device__ __forceinline__ void halo_rowtopk_merge_body(
|
|
const float* __restrict__ G, float* __restrict__ OV, int* __restrict__ OI, int R, int n) {
|
|
const int m = blockIdx.x;
|
|
const int lane = threadIdx.x;
|
|
|
|
unsigned long long t[HALO_TOPK_PT2];
|
|
#pragma unroll
|
|
for (int q = 0; q < HALO_TOPK_PT2; q++) t[q] = 0ull;
|
|
|
|
const float* const row = G + (size_t)m * n * 2;
|
|
for (int i = lane; i < n; i += HALO_TOPK_WAVE) {
|
|
int id;
|
|
__builtin_memcpy(&id, row + (size_t)i * 2, sizeof(int));
|
|
if (id < 0) continue;
|
|
tk_insert<HALO_TOPK_PT2>(t, tk_key_f32(row[(size_t)i * 2 + 1], (unsigned)id));
|
|
}
|
|
|
|
for (int r = 0; r < R; r++) {
|
|
const unsigned long long w = tk_round<HALO_TOPK_PT2>(t);
|
|
if (lane == 0) {
|
|
OV[(size_t)m * R + r] = w ? tk_val(w) : -INFINITY;
|
|
OI[(size_t)m * R + r] = w ? tk_idx(w) : -1;
|
|
}
|
|
}
|
|
}
|
|
__global__ __launch_bounds__(HALO_TOPK_WAVE) void halo_rowtopk_merge_kernel(
|
|
const float* __restrict__ G, float* __restrict__ OV, int* __restrict__ OI, int R, int n) {
|
|
halo_rowtopk_merge_body(G, OV, OI, R, n);
|
|
}
|
|
__global__ __launch_bounds__(HALO_TOPK_WAVE) void halo_r4dchain_rowtopk_merge(
|
|
const float* __restrict__ G, float* __restrict__ OV, int* __restrict__ OI, int R, int n) {
|
|
halo_chain_wait();
|
|
halo_rowtopk_merge_body(G, OV, OI, R, n);
|
|
halo_chain_done();
|
|
}
|
|
|
|
// ============================================================== logit_rerank
|
|
//
|
|
// The exact second half of a coarse top-R: the coarse head proposes R candidates per row, and this
|
|
// scores only those against the full-precision head, then re-sorts on the exact values. A draft
|
|
// round reads R weight rows instead of the whole head (40 KB a row against 318 MB). On
|
|
// Qwen3.8-Flash-Next the 2-bit head's acceptance is 4 to 8 points below the exact head's, and this
|
|
// closes that gap.
|
|
//
|
|
// One block per row, waves spread over the candidates. A wave walks K with a lane stride and folds
|
|
// with the same five xor-butterfly steps, so the reduction order is a fixed function of the shape.
|
|
// Wave 0 then runs the ordinary top-R chain over the R scores (one lane a candidate, R <= PT2),
|
|
// so ties break by tk_key_f32's rule, as in row_topk and row_topk_merge; ties within and between
|
|
// shards resolve alike, which keeps the merged answer exact.
|
|
//
|
|
// A candidate outside this rank's rows is a pad. `voff` is the shard's first column and ids are
|
|
// global, as row_topk reports them; under vocab-parallel drafting a rank only gets its own ids, and
|
|
// the guard is for a caller that mixes them.
|
|
//
|
|
// I8 reads the int8 head logits_gemm_i8 stores (r4d.i8lm.r128): the same row (K codes, then the
|
|
// row's bf16 scale per 128 columns in its tail) with int8 codes instead of E4M3. The drafter
|
|
// rescores against whichever head the trunk's logits GEMM reads, so an int8 head needs this form.
|
|
#define HALO_RERANK_WPB 8 // waves a block: 256 threads, and R <= 32 so a wave takes <= 4
|
|
|
|
template <bool I8>
|
|
__global__ __launch_bounds__(HALO_TOPK_WAVE * HALO_RERANK_WPB) void halo_logit_rerank_kernel(
|
|
const unsigned short* __restrict__ X, const int* __restrict__ IN,
|
|
const unsigned char* __restrict__ W, int* __restrict__ OI, float* __restrict__ OV,
|
|
float* __restrict__ OP, int R, long K, long voff, long nv, long w_ld) {
|
|
__shared__ float s_sc[HALO_TOPK_PT2];
|
|
__shared__ int s_id[HALO_TOPK_PT2];
|
|
|
|
const int m = blockIdx.x;
|
|
const int lane = threadIdx.x & (HALO_TOPK_WAVE - 1);
|
|
const int wv = threadIdx.x / HALO_TOPK_WAVE;
|
|
const unsigned short* const xr = X + (size_t)m * K;
|
|
|
|
for (int r = wv; r < R; r += HALO_RERANK_WPB) {
|
|
const int gid = IN[(size_t)m * R + r];
|
|
const long loc = (long)gid - voff;
|
|
const bool own = gid >= 0 && loc >= 0 && loc < nv;
|
|
float acc = 0.0f;
|
|
if (own) {
|
|
/* The stored row, as logits_gemm_fp8 reads it (halo_fp8_lm.h): K E4M3 codes then K/128 bf16
|
|
* block scales in the row's tail. A lane owns a strided subset of columns and applies each
|
|
* column's block scale locally, so the fold is a plain sum whose order depends only on the
|
|
* lane count. */
|
|
const unsigned char* const wr = W + (size_t)loc * w_ld;
|
|
const unsigned short* const sr = (const unsigned short*)(wr + K);
|
|
for (long k = lane; k < K; k += HALO_TOPK_WAVE) {
|
|
const float c = I8 ? (float)(int8_t)wr[k] : halo_fp8::e4m3((uint32_t)wr[k]);
|
|
acc = fmaf(c * halo_fp8::bscale(sr[k / 128]), bf16_to_f32(xr[k]), acc);
|
|
}
|
|
acc = halo_wave_sum(acc);
|
|
}
|
|
if (lane == 0) { s_sc[r] = own ? acc : -INFINITY; s_id[r] = own ? gid : -1; }
|
|
}
|
|
__syncthreads();
|
|
if (wv != 0) return;
|
|
|
|
unsigned long long t[HALO_TOPK_PT2];
|
|
#pragma unroll
|
|
for (int q = 0; q < HALO_TOPK_PT2; q++) t[q] = 0ull;
|
|
if (lane < R && s_id[lane] >= 0)
|
|
tk_insert<HALO_TOPK_PT2>(t, tk_key_f32(s_sc[lane], (unsigned)s_id[lane]));
|
|
|
|
for (int r = 0; r < R; r++) {
|
|
const unsigned long long w = tk_round<HALO_TOPK_PT2>(t);
|
|
if (lane == 0) {
|
|
const float v = w ? tk_val(w) : -INFINITY;
|
|
const int i = w ? tk_idx(w) : -1;
|
|
OV[(size_t)m * R + r] = v;
|
|
OI[(size_t)m * R + r] = i;
|
|
if (OP) {
|
|
float* const p = OP + ((size_t)m * R + r) * 2;
|
|
__builtin_memcpy(p, &i, sizeof i);
|
|
p[1] = v;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static int logit_rerank_emit(long x, long idx_in, long head, long outi, long outv, long outp,
|
|
int M, int R, long K, long voff, long nv, RadStream stream,
|
|
bool i8) {
|
|
const long w_ld = halo_fp8::lm_row_bytes(K);
|
|
// R <= PT2 makes the re-sort exact for any scores (one lane holds the whole answer), so it is
|
|
// checked, not clamped.
|
|
if (M < 1 || R < 1 || K < 1 || nv < 1 || R > HALO_TOPK_PT2) return HALO_NO(RAD_E_SHAPE);
|
|
#define HALO_RERANK_EMIT(I8) \
|
|
HALO_EMIT(stream, halo_logit_rerank_kernel<I8>, dim3(M), dim3(HALO_TOPK_WAVE * HALO_RERANK_WPB), \
|
|
0, (const unsigned short*)x, (const int*)idx_in, (const unsigned char*)head, \
|
|
(int*)outi, \
|
|
(float*)outv, (float*)outp, R, K, voff, nv, w_ld)
|
|
const int rc = i8 ? HALO_RERANK_EMIT(true) : HALO_RERANK_EMIT(false);
|
|
#undef HALO_RERANK_EMIT
|
|
return rc;
|
|
}
|
|
|
|
// ============================================================== the plugin ABI entry points
|
|
//
|
|
// The operand reader and the split law are in halo_gemm_args.h. See libhalo/halo_args.h on why there
|
|
// is no adapter layer.
|
|
/* ------------------------------------------------------------------ row top-k */
|
|
|
|
enum { T_X = 0, T_IDX, T_VAL, T_PAIRS };
|
|
|
|
/* Also the scratch sizer's reader, which runs at declare with no tensors and no constraint check, so
|
|
* it repeats the row's R <= 32. */
|
|
static int topk_params(const RadArgs* a,
|
|
const RadTensor** x, const RadTensor** idx, const RadTensor** val,
|
|
long long* M, long long* N, long long* R, int* RC, int* NCH, int* PT1) {
|
|
*x = halo_opt(a, T_X);
|
|
*idx = halo_opt(a, T_IDX);
|
|
*val = halo_opt(a, T_VAL);
|
|
/* The scratch sizer runs at declare, with no tensors, through this same function. `M` and `N`
|
|
* are declared parameters of this op for that reason (spec §2.1) and serve as the fallback;
|
|
* when the tensor is present it wins, since a band ceiling is only an upper bound. */
|
|
if (*x) {
|
|
if ((*x)->rank != 2 || !halo_contig(*x)) return HALO_NO(RAD_E_STRIDE);
|
|
*M = (*x)->shape[0];
|
|
*N = (*x)->shape[1];
|
|
} else {
|
|
*M = halo_geti_or(a, "M", 0);
|
|
*N = halo_geti_or(a, "N", 0);
|
|
}
|
|
*R = halo_geti_or(a, "R", 0);
|
|
if (*M < 1 || *N < 1) return HALO_NO(RAD_E_SHAPE);
|
|
if (*N % 8) return HALO_NO(RAD_E_SHAPE);
|
|
if (*R < 1 || *R > 32) return HALO_NO(RAD_E_SHAPE);
|
|
|
|
HaloTopkCfg tk;
|
|
halo_cfg_topk(a, &tk);
|
|
const HaloTopkCfg* c = &tk;
|
|
*PT1 = c->pt1;
|
|
if (*PT1 != 2 && *PT1 != 4 && *PT1 != 8 && *PT1 != 16) return HALO_NO(RAD_E_INVAL);
|
|
*RC = c->rc > 0 ? c->rc : (int)*R;
|
|
if (*RC < (int)*R) return HALO_NO(RAD_E_INVAL); /* a chunk must keep at least R */
|
|
if (*RC > *PT1 * 32) return HALO_NO(RAD_E_SHAPE);
|
|
/* NCH is a requested chunk count. 0 means one chunk per 2048 columns, at least one, the shape
|
|
* the MoE router this kernel serves runs. */
|
|
*NCH = c->nch > 0 ? c->nch : (int)((*N + 2047) / 2048);
|
|
if (*NCH < 1) *NCH = 1;
|
|
return RAD_OK;
|
|
}
|
|
|
|
extern "C" int64_t halo_rad_rowtopk_scratch(const RadArgs* a) {
|
|
const RadTensor *x, *idx, *val;
|
|
long long M, N, R;
|
|
int RC, NCH, PT1;
|
|
int rc = topk_params(a, &x, &idx, &val, &M, &N, &R, &RC, &NCH, &PT1);
|
|
if (rc != RAD_OK) return rc;
|
|
/* The chunk width is rounded inside libhalo, so the count is asked for, not recomputed. */
|
|
const int nch = halo_rowtopk_bf16_chunks((int)N, NCH);
|
|
return (int64_t)M * nch * RC * (int64_t)sizeof(unsigned long long);
|
|
}
|
|
|
|
extern "C" int halo_rad_rowtopk(const RadArgs* a, RadStream stream) {
|
|
const RadTensor *x, *idx, *val;
|
|
long long M, N, R;
|
|
int RC, NCH, PT1;
|
|
int rc = topk_params(a, &x, &idx, &val, &M, &N, &R, &RC, &NCH, &PT1);
|
|
if (rc != RAD_OK) return rc;
|
|
if (!x || !idx || !val) return HALO_NO(RAD_E_INVAL);
|
|
if (!halo_contig(idx) || !halo_contig(val)) return HALO_NO(RAD_E_STRIDE);
|
|
|
|
const int nch = halo_rowtopk_bf16_chunks((int)N, NCH);
|
|
const int64_t need = (int64_t)M * nch * RC * (int64_t)sizeof(unsigned long long);
|
|
if (!a->scratch || a->scratch_bytes < need) return HALO_NO(RAD_E_SCRATCH);
|
|
|
|
/* The vocab-parallel form; both halves optional. `vocab_off` shifts the reported columns to
|
|
* global ids, and `pairs` is the [M, R, 2] plane an all_gather moves. Absent and zero is the
|
|
* single-rank launch, bit for bit. */
|
|
const RadTensor* pairs = halo_opt(a, T_PAIRS);
|
|
const long long off = halo_geti_or(a, "vocab_off", 0);
|
|
if (off < 0) return HALO_NO(RAD_E_SHAPE);
|
|
if (pairs) {
|
|
if (pairs->dtype != RAD_F32 || !halo_contig(pairs)) return HALO_NO(RAD_E_STRIDE);
|
|
if (halo_numel(pairs) < M * R * 2) return HALO_NO(RAD_E_SHAPE);
|
|
}
|
|
|
|
/* The chained forms at decode heights, where the row resolved with `chain`. */
|
|
const bool ch = halo_geti_or(a, "chain", 0) != 0 && M <= 64;
|
|
return rowtopk_bf16_sharded_emit(halo_addr(x), (long)(uintptr_t)a->scratch, halo_addr(val),
|
|
halo_addr(idx), pairs ? halo_addr(pairs) : 0,
|
|
(int)M, (int)N, (int)R, RC, NCH, PT1, (int)off, stream, ch);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ row_topk_merge
|
|
*
|
|
* The R largest of the all-gathered per-rank candidate sets. No variants and no scratch: one wave
|
|
* per row over world_size * R pairs, so its cost is its launch; it only has to be exact. */
|
|
extern "C" int halo_rad_rowtopk_merge(const RadArgs* a, RadStream s) {
|
|
const RadTensor* g = halo_opt(a, 0);
|
|
const RadTensor* idx = halo_opt(a, 1);
|
|
const RadTensor* val = halo_opt(a, 2);
|
|
if (!g || !idx || !val) return HALO_NO(RAD_E_INVAL);
|
|
if (g->dtype != RAD_F32 || !halo_contig(g)) return HALO_NO(RAD_E_STRIDE);
|
|
if (!halo_contig(idx) || !halo_contig(val)) return HALO_NO(RAD_E_STRIDE);
|
|
|
|
/* M comes from the operand: the `M` parameter is a band ceiling, and the gathered plane is
|
|
* narrowed to the step's rows, so sizing the checks by the parameter would refuse real calls.
|
|
* The parameter is the fallback for a sizing query made before there are tensors. */
|
|
const long long M = (g->rank >= 1) ? g->shape[0] : halo_geti_or(a, "M", 0);
|
|
const long long R = halo_geti_or(a, "R", 0);
|
|
const long long W = halo_geti_or(a, "world_size", 0);
|
|
if (M < 1 || R < 1 || W < 1) return HALO_NO(RAD_E_SHAPE);
|
|
if (R > 32) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(g) < M * W * R * 2) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(idx) < M * R || halo_numel(val) < M * R) return HALO_NO(RAD_E_SHAPE);
|
|
|
|
/* R <= PT2 makes the merge exact for any candidates (one lane can hold the whole answer). */
|
|
const bool ch = halo_geti_or(a, "chain", 0) != 0 && M <= 64;
|
|
return HALO_EMIT(s, (ch ? halo_r4dchain_rowtopk_merge : halo_rowtopk_merge_kernel), dim3((unsigned)M),
|
|
dim3(HALO_TOPK_WAVE), 0, (const float*)g->data, (float*)val->data,
|
|
(int*)idx->data, (int)R, (int)(W * R));
|
|
}
|
|
|
|
/* `M` and the candidate count come from the operands, as in rowtopk_merge: `M` is a band ceiling.
|
|
* `n_vocab` is this rank's rows of the head, so the ownership guard is a shard test. */
|
|
static int rad_logit_rerank(const RadArgs* a, RadStream stream, bool i8) {
|
|
const RadTensor* x = halo_opt(a, 0);
|
|
const RadTensor* w = halo_opt(a, 1);
|
|
const RadTensor* in = halo_opt(a, 2);
|
|
const RadTensor* idx = halo_opt(a, 3);
|
|
const RadTensor* val = halo_opt(a, 4);
|
|
const RadTensor* pr = halo_opt(a, 5);
|
|
if (!x || !in || !w || !idx || !val) return HALO_NO(RAD_E_INVAL);
|
|
if (x->dtype != RAD_BF16) return HALO_NO(RAD_E_DTYPE);
|
|
/* The declared dtype arrives, which for this weight is the trunk's bf16; the E4M3 (or int8)
|
|
* plane is the stored form. Its stored width cannot be checked here and follows the layout
|
|
* hook's rule; the engine's layout-tag check guarantees agreement by refusing a container laid
|
|
* out for another row. So the head is accepted at either spelling and read as r4d.fp8lm.b128,
|
|
* or r4d.i8lm.r128 for the int8 form. */
|
|
if (w->dtype != (i8 ? RAD_I8 : RAD_F8E4M3) && w->dtype != RAD_BF16) return HALO_NO(RAD_E_DTYPE);
|
|
if (in->dtype != RAD_I32 || idx->dtype != RAD_I32 || val->dtype != RAD_F32)
|
|
return HALO_NO(RAD_E_DTYPE);
|
|
if (pr && pr->dtype != RAD_F32) return HALO_NO(RAD_E_DTYPE);
|
|
if (!halo_contig(x) || !halo_contig(in) || !halo_contig(w) || !halo_contig(idx) ||
|
|
!halo_contig(val) || (pr && !halo_contig(pr))) return HALO_NO(RAD_E_STRIDE);
|
|
|
|
const long long K = halo_geti_or(a, "n_embd", 0);
|
|
const long long NV = halo_geti_or(a, "n_vocab", 0);
|
|
const long long R = halo_geti_or(a, "R", 0);
|
|
const long long voff = halo_geti_or(a, "vocab_off", 0);
|
|
if (K < 1 || NV < 1 || R < 1) return HALO_NO(RAD_E_SHAPE);
|
|
if (R > 32) return HALO_NO(RAD_E_SHAPE);
|
|
const long long M = (x->rank >= 1) ? x->shape[0] : halo_geti_or(a, "M", 0);
|
|
if (M < 1) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(x) < M * K || halo_numel(in) < M * R) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(idx) < M * R || halo_numel(val) < M * R) return HALO_NO(RAD_E_SHAPE);
|
|
if (pr && halo_numel(pr) < M * R * 2) return HALO_NO(RAD_E_SHAPE);
|
|
if (K % 128) return HALO_NO(RAD_E_SHAPE); /* the scale block; lm_row_bytes assumes it */
|
|
|
|
return logit_rerank_emit(halo_addr(x), halo_addr(in), halo_addr(w), halo_addr(idx), halo_addr(val),
|
|
pr ? halo_addr(pr) : 0, (int)M, (int)R, (long)K, (long)voff, (long)NV,
|
|
stream, i8);
|
|
}
|
|
extern "C" int halo_rad_logit_rerank(const RadArgs* a, RadStream stream) {
|
|
return rad_logit_rerank(a, stream, false);
|
|
}
|
|
extern "C" int halo_rad_logit_rerank_i8(const RadArgs* a, RadStream stream) {
|
|
return rad_logit_rerank(a, stream, true);
|
|
}
|
|
|