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.
414 lines
22 KiB
Plaintext
414 lines
22 KiB
Plaintext
/* Derived from libr4d/r4d_glm.hip by the libhalo rename transform (2026-10-26),
|
|
* then ported to gfx1151; see libhalo/README.md. */
|
|
#include <hip/hip_runtime.h>
|
|
#include "halo_args.h"
|
|
#include "halo_common.h"
|
|
#include "halo_emit.h"
|
|
#include "halo_moe_sort.h"
|
|
#include "halo_fp8_wmma.h" // halo_fp8::cvt_pk_fp8_f32 (gfx1151 has no fp8 convert)
|
|
#include <climits>
|
|
#include <cmath>
|
|
|
|
namespace {
|
|
// One wave computes R rows by C columns, so each loaded weight serves R rows and each input
|
|
// C columns. Every output has the same arithmetic: lane l accumulates k = l, l+32, ... in
|
|
// ascending order with fmaf, then the xor butterfly over 16, 8, 4, 2, 1. A lane loads U steps of
|
|
// k before their FMAs: at a decode pass's few rows a wave streams its weight row with no other
|
|
// work to hide the load behind, and one step in flight left the router at 150 GB/s. Tail rows and
|
|
// columns read a clamped valid address and are never stored.
|
|
template <int R, int C, int U>
|
|
__global__ void __launch_bounds__(128)
|
|
gemm_nt_f32_tile_kernel(const uint16_t* x, const float* w, float* y, long xp, long wp, long yp,
|
|
int m, int n, int k) {
|
|
const int lane = threadIdx.x & 31, c0 = (blockIdx.x * 4 + (threadIdx.x >> 5)) * C,
|
|
r0 = blockIdx.y * R;
|
|
if (c0 >= n) return;
|
|
const uint16_t* xr[R];
|
|
const float* wc[C];
|
|
#pragma unroll
|
|
for (int q = 0; q < R; ++q) xr[q] = x + (long)(r0 + q < m ? r0 + q : m - 1) * xp;
|
|
#pragma unroll
|
|
for (int c = 0; c < C; ++c) wc[c] = w + (long)(c0 + c < n ? c0 + c : n - 1) * wp;
|
|
float sum[R][C];
|
|
#pragma unroll
|
|
for (int q = 0; q < R; ++q)
|
|
#pragma unroll
|
|
for (int c = 0; c < C; ++c) sum[q][c] = 0;
|
|
int j = lane;
|
|
for (; j + 32 * (U - 1) < k; j += 32 * U) {
|
|
float weight[U][C];
|
|
uint16_t in[U][R];
|
|
#pragma unroll
|
|
for (int u = 0; u < U; ++u)
|
|
#pragma unroll
|
|
for (int c = 0; c < C; ++c) weight[u][c] = wc[c][j + 32 * u];
|
|
#pragma unroll
|
|
for (int u = 0; u < U; ++u)
|
|
#pragma unroll
|
|
for (int q = 0; q < R; ++q) in[u][q] = xr[q][j + 32 * u];
|
|
#pragma unroll
|
|
for (int u = 0; u < U; ++u)
|
|
#pragma unroll
|
|
for (int q = 0; q < R; ++q) {
|
|
const float v = bf16_to_f32(in[u][q]);
|
|
#pragma unroll
|
|
for (int c = 0; c < C; ++c) sum[q][c] = fmaf(v, weight[u][c], sum[q][c]);
|
|
}
|
|
}
|
|
for (; j < k; j += 32) {
|
|
#pragma unroll
|
|
for (int q = 0; q < R; ++q) {
|
|
const float v = bf16_to_f32(xr[q][j]);
|
|
#pragma unroll
|
|
for (int c = 0; c < C; ++c) sum[q][c] = fmaf(v, wc[c][j], sum[q][c]);
|
|
}
|
|
}
|
|
#pragma unroll
|
|
for (int q = 0; q < R; ++q)
|
|
#pragma unroll
|
|
for (int c = 0; c < C; ++c) {
|
|
#pragma unroll
|
|
for (int mask = 16; mask; mask >>= 1) sum[q][c] += __shfl_xor(sum[q][c], mask, 32);
|
|
}
|
|
if (lane) return;
|
|
#pragma unroll
|
|
for (int q = 0; q < R; ++q)
|
|
if (r0 + q < m)
|
|
#pragma unroll
|
|
for (int c = 0; c < C; ++c)
|
|
if (c0 + c < n) y[(long)(r0 + q) * yp + c0 + c] = sum[q][c];
|
|
}
|
|
|
|
template <int R, int C, int U = 1>
|
|
int gemm_f32_tile(RadStream stream, const RadTensor& x, const RadTensor& w, const RadTensor& y,
|
|
long n, long k) {
|
|
const long m = x.shape[0];
|
|
return HALO_EMIT(stream, (gemm_nt_f32_tile_kernel<R, C, U>),
|
|
dim3((unsigned)((n + 4 * C - 1) / (4 * C)), (unsigned)((m + R - 1) / R)),
|
|
dim3(128), 0, (const uint16_t*)x.data, (const float*)w.data, (float*)y.data,
|
|
x.stride[0], w.stride[0], y.stride[0], (int)m, (int)n, (int)k);
|
|
}
|
|
int gemm_f32_body(const RadArgs* a, RadStream stream) {
|
|
if (!a || a->n_t < 3) return RAD_E_INVAL;
|
|
const auto &x = a->t[0], &w = a->t[1], &y = a->t[2];
|
|
const long n = halo_geti_or(a, "N", 0), k = halo_geti_or(a, "K", 0);
|
|
if (x.dtype != RAD_BF16 || w.dtype != RAD_F32 || y.dtype != RAD_F32) return RAD_E_DTYPE;
|
|
if (!x.data || !w.data || !y.data) return RAD_E_INVAL;
|
|
if (n < 1 || n > 512 || k < 1 || k > 16384 || x.rank != 2 || w.rank != 2 || y.rank != 2 ||
|
|
x.shape[0] < 1 || x.shape[0] > 65535 || x.shape[1] != k || w.shape[0] != n ||
|
|
w.shape[1] != k || y.shape[0] != x.shape[0] || y.shape[1] != n || x.stride[1] != 1 ||
|
|
w.stride[1] != 1 || y.stride[1] != 1 || x.stride[0] < k || w.stride[0] < k ||
|
|
y.stride[0] < n)
|
|
return RAD_E_SHAPE;
|
|
/* The tile form, rows x columns per wave and steps of k in flight, by M; all forms are
|
|
* byte-identical, so the choice is speed only. Measured on gfx1201 (halo_selftest --perff32,
|
|
* cold) at N 4/32/36/288, K 4096: 16x4 gains 3-5 % at N 288/32 above 64 rows but loses 16 % at
|
|
* N 36; 8x4 holds at every width. Below 64 rows the steps of k in flight decide: 2x2 with 16
|
|
* steps is 2 to 2.5 times as fast as one step at N 288 up to 16 rows, and about 3 times at N 32
|
|
* at every count below 64; at N 288 from 17 rows 8x1 with 8 steps is faster.
|
|
* A projection of a few columns has too few waves for wide tiles at any M. */
|
|
const long rows = x.shape[0];
|
|
if (n <= 8) return gemm_f32_tile<4, 1>(stream, x, w, y, n, k);
|
|
if (rows <= 16 || (rows < 64 && n < 128)) return gemm_f32_tile<2, 2, 16>(stream, x, w, y, n, k);
|
|
if (rows < 64) return gemm_f32_tile<8, 1, 8>(stream, x, w, y, n, k);
|
|
return gemm_f32_tile<8, 4>(stream, x, w, y, n, k);
|
|
}
|
|
// router_sigmoid_topk in one wave per row: p = sigmoid(logit), the top_k largest ranks p + bias
|
|
// in descending order, equal ranks to the lower expert, as a serial scan with a strict `>` takes
|
|
// them. Lane l holds experts l, l+32, ... as 64-bit keys: the rank's order-preserving bits (-0
|
|
// folded to +0) over ~e, so equal ranks order to the lower expert. Round j takes the largest key
|
|
// below round j-1's winner and parks it on lane j (top_k <= 32). `total` sums the selected p in
|
|
// round order, mult = norm ? factor / (total + 1e-20) : factor, and a weight is p * mult. A NaN
|
|
// rank is never selected.
|
|
// OWN is a template bound because the keys are registers; a lane-derived trip count would index
|
|
// them divergently, which gfx12 lowers to a v_movrels loop.
|
|
__device__ __forceinline__ unsigned long long router_rank_key(float rank, int e) {
|
|
uint32_t u = __float_as_uint(rank);
|
|
if (u == 0x80000000u) u = 0;
|
|
const uint32_t k = (u & 0x80000000u) ? ~u : (u | 0x80000000u);
|
|
return rank == rank ? ((unsigned long long)k << 32) | (uint32_t)~e : 0ull;
|
|
}
|
|
__device__ __forceinline__ unsigned long long router_wave_max(unsigned long long b) {
|
|
unsigned long long o;
|
|
o = halo_xor_lane_u64<16>(b);
|
|
b = b > o ? b : o;
|
|
o = halo_xor_lane_u64<8>(b);
|
|
b = b > o ? b : o;
|
|
o = halo_xor_lane_u64<4>(b);
|
|
b = b > o ? b : o;
|
|
o = halo_xor_lane_u64<2>(b);
|
|
b = b > o ? b : o;
|
|
o = halo_xor_lane_u64<1>(b);
|
|
b = b > o ? b : o;
|
|
return b;
|
|
}
|
|
// One row in one wave, from any grid: the standalone select and the fused one below run this. A
|
|
// lane below `k` returns its slot's expert as written to `ids`.
|
|
template <int OWN>
|
|
__device__ __forceinline__ int router_sigmoid_row(const float* logits, const float* bias, int* ids,
|
|
float* weights, long lp, long bp, long ip,
|
|
long wp, int r, int ne, int k, int norm,
|
|
float factor, int lane) {
|
|
float p[OWN];
|
|
unsigned long long key[OWN];
|
|
#pragma unroll
|
|
for (int i = 0; i < OWN; ++i) {
|
|
const int e = lane + 32 * i, s = e < ne ? e : 0;
|
|
p[i] = 1.0f / (1.0f + expf(-logits[(long)r * lp + s]));
|
|
key[i] = e < ne ? router_rank_key(p[i] + bias[(long)s * bp], e) : 0ull;
|
|
}
|
|
unsigned long long below = ~0ull, mine = 0;
|
|
for (int j = 0; j < k; ++j) {
|
|
unsigned long long best = 0;
|
|
#pragma unroll
|
|
for (int i = 0; i < OWN; ++i) {
|
|
const unsigned long long c = key[i] < below ? key[i] : 0ull;
|
|
best = c > best ? c : best;
|
|
}
|
|
best = router_wave_max(best);
|
|
if (lane == j) mine = best;
|
|
below = best;
|
|
}
|
|
const int e = mine ? (int)~(uint32_t)mine : 0;
|
|
float chosen = 0;
|
|
#pragma unroll
|
|
for (int i = 0; i < OWN; ++i) {
|
|
const float v = __shfl(p[i], e & 31, 32);
|
|
if ((e >> 5) == i) chosen = v;
|
|
}
|
|
float total = 0;
|
|
for (int j = 0; j < k; ++j)
|
|
total += __int_as_float(__builtin_amdgcn_readlane(__float_as_int(chosen), j));
|
|
const float mult = norm ? factor / (total + 1e-20f) : factor;
|
|
if (lane < k) {
|
|
ids[(long)r * ip + lane] = e;
|
|
weights[(long)r * wp + lane] = chosen * mult;
|
|
}
|
|
return e;
|
|
}
|
|
template <int OWN>
|
|
__global__ void __launch_bounds__(128)
|
|
router_sigmoid_wave_kernel(const float* logits, const float* bias, int* ids, float* weights,
|
|
long lp, long bp, long ip, long wp, int m, int ne, int k, int norm,
|
|
float factor) {
|
|
const int lane = threadIdx.x & 31, r = blockIdx.x * 4 + (threadIdx.x >> 5);
|
|
if (r >= m) return;
|
|
(void)router_sigmoid_row<OWN>(logits, bias, ids, weights, lp, bp, ip, wp, r, ne, k, norm,
|
|
factor, lane);
|
|
}
|
|
// router_sigmoid_topk, then moe_scatter over its ids, then the `fill` of an ungated shared arm's
|
|
// gate with 1, in one workgroup. Rows wrap over the NW waves; each leaves its slots in the sort's
|
|
// LDS list instead of re-reading `ids`. A slot carries the expert its row wrote, including the
|
|
// expert 0 of a row with fewer finite ranks than top_k, so every output equals the three launches'.
|
|
// INV also writes the inverse, as moe_scatter_inv does.
|
|
template <int OWN, uint32_t NW, bool INV>
|
|
__global__ void __launch_bounds__(NW * 32)
|
|
router_sigmoid_scatter_kernel(const float* logits, const float* bias, int* ids, float* weights,
|
|
int32_t* sorted, int32_t* off, int32_t* cnt, uint16_t* gate,
|
|
int32_t* inv, long lp, long bp, long ip, long wp, long gp, int m,
|
|
int ne, int n_local, int k, int norm, float factor, int base,
|
|
halo_moesort::Protect prot) {
|
|
__shared__ halo_moesort::ScatterLds<NW> sl;
|
|
const int lane = threadIdx.x & 31, wave = threadIdx.x >> 5;
|
|
for (int e = threadIdx.x; e < n_local; e += (int)(NW * 32)) sl.cnt[e] = 0;
|
|
for (int r = wave; r < m; r += (int)NW) {
|
|
const int e = router_sigmoid_row<OWN>(logits, bias, ids, weights, lp, bp, ip, wp, r, ne, k,
|
|
norm, factor, lane) -
|
|
base;
|
|
if (lane < k)
|
|
sl.e[r * k + lane] =
|
|
e >= 0 && e < n_local ? halo_moesort::sort_place(e, (uint32_t)n_local, prot) : -1;
|
|
if (gate && !lane) gate[(long)r * gp] = f32_to_bf16(1.0f);
|
|
}
|
|
lds_barrier();
|
|
halo_moesort::scatter_small_place<NW, INV>(sorted, off, cnt, (uint32_t)(m * k),
|
|
(uint32_t)n_local, sl, inv);
|
|
}
|
|
int router_body(const RadArgs* a, RadStream stream) {
|
|
if (!a || a->n_t < 4) return RAD_E_INVAL;
|
|
const auto &lg = a->t[0], &bias = a->t[1], &ids = a->t[2], &w = a->t[3];
|
|
const long ne = halo_geti_or(a, "n_expert", 0), k = halo_geti_or(a, "top_k", 0);
|
|
const float factor = (float)halo_getf(a, "route_scale", 0);
|
|
if (lg.dtype != RAD_F32 || bias.dtype != RAD_F32 || ids.dtype != RAD_I32 || w.dtype != RAD_F32)
|
|
return RAD_E_DTYPE;
|
|
if (ne < 1 || ne > 512 || k < 1 || k > 32 || k > ne || !std::isfinite(factor) || factor <= 0 ||
|
|
!lg.data || !bias.data || !ids.data || !w.data || lg.rank != 2 || lg.shape[0] < 1 ||
|
|
lg.shape[0] > 65535 || lg.shape[1] != ne || bias.rank != 1 || bias.shape[0] != ne ||
|
|
ids.rank != 2 || w.rank != 2 || ids.shape[0] != lg.shape[0] || w.shape[0] != lg.shape[0] ||
|
|
ids.shape[1] != k || w.shape[1] != k || lg.stride[1] != 1 || ids.stride[1] != 1 ||
|
|
w.stride[1] != 1 || lg.stride[0] < ne || ids.stride[0] < k || w.stride[0] < k ||
|
|
bias.stride[0] < 1)
|
|
return RAD_E_SHAPE;
|
|
const int norm = (int)(halo_geti_or(a, "norm", 0) != 0);
|
|
const dim3 grid((unsigned)((lg.shape[0] + 3) / 4));
|
|
if (ne <= 9 * 32)
|
|
return HALO_EMIT(stream, router_sigmoid_wave_kernel<9>, grid, dim3(128), 0,
|
|
(const float*)lg.data, (const float*)bias.data, (int*)ids.data,
|
|
(float*)w.data, lg.stride[0], bias.stride[0], ids.stride[0], w.stride[0],
|
|
(int)lg.shape[0], (int)ne, (int)k, norm, factor);
|
|
return HALO_EMIT(stream, router_sigmoid_wave_kernel<16>, grid, dim3(128), 0,
|
|
(const float*)lg.data, (const float*)bias.data, (int*)ids.data, (float*)w.data,
|
|
lg.stride[0], bias.stride[0], ids.stride[0], w.stride[0], (int)lg.shape[0],
|
|
(int)ne, (int)k, norm, factor);
|
|
}
|
|
/* Operands: router_sigmoid_topk's four, moe_scatter's three outputs, then the optional gate and the
|
|
* optional inverse. The sort is the single-workgroup form, so M * top_k must fit it; a larger pass
|
|
* is refused rather than served at another cost. */
|
|
int router_scatter_body(const RadArgs* a, RadStream stream) {
|
|
if (!a || a->n_t < 7) return RAD_E_INVAL;
|
|
const auto &lg = a->t[0], &bias = a->t[1], &ids = a->t[2], &w = a->t[3];
|
|
const RadTensor *sorted = &a->t[4], *off = &a->t[5], *cnt = &a->t[6];
|
|
const RadTensor* gate = a->n_t > 7 ? halo_opt(a, 7) : nullptr;
|
|
const RadTensor* inv = a->n_t > 8 ? halo_opt(a, 8) : nullptr;
|
|
const long ne = halo_geti_or(a, "n_expert", 0), k = halo_geti_or(a, "top_k", 0);
|
|
const long nl = halo_geti_or(a, "n_local", ne), base = halo_geti_or(a, "expert_base", 0);
|
|
const float factor = (float)halo_getf(a, "route_scale", 0);
|
|
if (lg.dtype != RAD_F32 || bias.dtype != RAD_F32 || ids.dtype != RAD_I32 ||
|
|
w.dtype != RAD_F32 || sorted->dtype != RAD_I32 || off->dtype != RAD_I32 ||
|
|
cnt->dtype != RAD_I32 || (gate && gate->dtype != RAD_BF16) ||
|
|
(inv && inv->dtype != RAD_I32))
|
|
return RAD_E_DTYPE;
|
|
if (ne < 1 || ne > 512 || k < 1 || k > 32 || k > ne || !std::isfinite(factor) || factor <= 0 ||
|
|
!lg.data || !bias.data || !ids.data || !w.data || !sorted->data || !off->data ||
|
|
!cnt->data || lg.rank != 2 || lg.shape[0] < 1 || lg.shape[1] != ne || bias.rank != 1 ||
|
|
bias.shape[0] != ne || ids.rank != 2 || w.rank != 2 || ids.shape[0] != lg.shape[0] ||
|
|
w.shape[0] != lg.shape[0] || ids.shape[1] != k || w.shape[1] != k || lg.stride[1] != 1 ||
|
|
ids.stride[1] != 1 || w.stride[1] != 1 || lg.stride[0] < ne || ids.stride[0] < k ||
|
|
w.stride[0] < k || bias.stride[0] < 1)
|
|
return RAD_E_SHAPE;
|
|
const long m = lg.shape[0], T = m * k;
|
|
if (T > (long)halo_moesort::kScatterSmall || nl < 1 || nl > ne || base < 0 || base + nl > ne ||
|
|
!halo_contig(sorted) || !halo_contig(off) || !halo_contig(cnt) || halo_numel(sorted) < T ||
|
|
halo_numel(off) < nl + 1 || halo_numel(cnt) < nl ||
|
|
(inv && (!halo_contig(inv) || halo_numel(inv) < T)))
|
|
return RAD_E_SHAPE;
|
|
long long grows = 0;
|
|
const long long gp = gate ? halo_rows_pitch(gate, 1, &grows) : 0;
|
|
if (gate && (gp < 1 || grows < m)) return RAD_E_SHAPE;
|
|
halo_moesort::Protect prot;
|
|
const int prc = halo_moesort::protect_of(a, nl, &prot);
|
|
if (prc != RAD_OK) return prc;
|
|
const int norm = (int)(halo_geti_or(a, "norm", 0) != 0);
|
|
// A wave a row up to 32 rows, as router_topk_scatter: the rows a wave serves run back to back.
|
|
const int nw = m <= 8 ? 8 : m <= 16 ? 16 : 32;
|
|
#define HALO_RSS_I(OWN, NW, INV) \
|
|
return HALO_EMIT(stream, (router_sigmoid_scatter_kernel<OWN, NW, INV>), dim3(1), \
|
|
dim3((NW) * 32), 0, (const float*)lg.data, (const float*)bias.data, \
|
|
(int*)ids.data, (float*)w.data, (int32_t*)sorted->data, (int32_t*)off->data, \
|
|
(int32_t*)cnt->data, gate ? (uint16_t*)gate->data : (uint16_t*)nullptr, \
|
|
inv ? (int32_t*)inv->data : (int32_t*)nullptr, lg.stride[0], bias.stride[0], \
|
|
ids.stride[0], w.stride[0], (long)gp, (int)m, (int)ne, (int)nl, (int)k, norm, \
|
|
factor, (int)base, prot)
|
|
#define HALO_RSS(OWN, NW) \
|
|
do { \
|
|
if (inv) HALO_RSS_I(OWN, NW, true); \
|
|
HALO_RSS_I(OWN, NW, false); \
|
|
} while (0)
|
|
if (ne <= 9 * 32) {
|
|
if (nw == 8) HALO_RSS(9, 8u);
|
|
if (nw == 16) HALO_RSS(9, 16u);
|
|
HALO_RSS(9, 32u);
|
|
}
|
|
if (nw == 8) HALO_RSS(16, 8u);
|
|
if (nw == 16) HALO_RSS(16, 16u);
|
|
HALO_RSS(16, 32u);
|
|
#undef HALO_RSS
|
|
#undef HALO_RSS_I
|
|
}
|
|
__global__ void swiglu_clamped_kernel(const uint16_t* gu, uint16_t* y, long gp, long yp, int n,
|
|
float limit) {
|
|
const long r = blockIdx.x;
|
|
const int i = blockIdx.y * 256 + threadIdx.x;
|
|
if (i >= n) return;
|
|
const float g = fminf(bf16_to_f32(gu[r * gp + i]), limit);
|
|
const float u = fminf(fmaxf(bf16_to_f32(gu[r * gp + n + i]), -limit), limit);
|
|
y[r * yp + i] = f32_to_bf16((g / (1.0f + expf(-g))) * u);
|
|
}
|
|
int clamped_body(const RadArgs* a, RadStream stream) {
|
|
if (!a || a->n_t < 2) return RAD_E_INVAL;
|
|
const auto &gu = a->t[0], &y = a->t[1];
|
|
const long n = halo_geti_or(a, "n", 0);
|
|
const float limit = (float)halo_getf(a, "limit", 0);
|
|
if (gu.dtype != RAD_BF16 || y.dtype != RAD_BF16) return RAD_E_DTYPE;
|
|
if (n < 1 || n > INT_MAX / 2 || !std::isfinite(limit) || limit <= 0 || !gu.data || !y.data ||
|
|
gu.rank != 2 || y.rank != 2 || y.shape[0] < 1 || y.shape[0] > 65535 ||
|
|
gu.shape[0] != y.shape[0] || gu.shape[1] != 2 * n || y.shape[1] != n || gu.stride[1] != 1 ||
|
|
y.stride[1] != 1 || gu.stride[0] < 2 * n || y.stride[0] < n)
|
|
return RAD_E_SHAPE;
|
|
return HALO_EMIT(stream, swiglu_clamped_kernel, dim3(y.shape[0], (n + 255) / 256), dim3(256), 0,
|
|
(const uint16_t*)gu.data, (uint16_t*)y.data, gu.stride[0], y.stride[0], (int)n,
|
|
limit);
|
|
}
|
|
// swiglu_clamped, then quant_act_fp8 over its product, in one pass: a wave per (row, 128 columns),
|
|
// four columns a lane, as the quantiser maps them. Each element is swiglu_clamped_kernel's
|
|
// expression and each group quant_act_fp8_kernel's. The product is rounded to bf16 and read back
|
|
// before the absmax because the separate quantiser reads the stored bf16 bytes.
|
|
__global__ void __launch_bounds__(256)
|
|
swiglu_clamped_quant_kernel(const uint16_t* gu, uint16_t* y, uint8_t* q, float* s, long gp,
|
|
long yp, long qp, long sp, int n, int nkb, float limit) {
|
|
const long r = blockIdx.x;
|
|
const int lane = threadIdx.x & 31, wave = threadIdx.x >> 5;
|
|
for (int kb = wave; kb < nkb; kb += (int)(blockDim.x >> 5)) {
|
|
const int c0 = kb * 128 + lane * 4;
|
|
float v[4], m = 0.0f;
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) {
|
|
const int c = c0 + j;
|
|
v[j] = 0.0f;
|
|
if (c < n) {
|
|
const float g = fminf(bf16_to_f32(gu[r * gp + c]), limit);
|
|
const float u = fminf(fmaxf(bf16_to_f32(gu[r * gp + n + c]), -limit), limit);
|
|
const uint16_t h = f32_to_bf16((g / (1.0f + expf(-g))) * u);
|
|
if (y) y[r * yp + c] = h;
|
|
v[j] = bf16_to_f32(h);
|
|
}
|
|
}
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) m = fmaxf(m, fabsf(v[j]));
|
|
m = halo_wave_max(m);
|
|
const float sc = m > 0.0f ? m * (1.0f / 448.0f) : 1.0f;
|
|
if (!lane) s[r * sp + kb] = sc;
|
|
const float inv = 1.0f / sc;
|
|
int pk = 0;
|
|
pk = halo_fp8::cvt_pk_fp8_f32(fminf(fmaxf(v[0] * inv, -448.0f), 448.0f),
|
|
fminf(fmaxf(v[1] * inv, -448.0f), 448.0f), pk, false);
|
|
pk = halo_fp8::cvt_pk_fp8_f32(fminf(fmaxf(v[2] * inv, -448.0f), 448.0f),
|
|
fminf(fmaxf(v[3] * inv, -448.0f), 448.0f), pk, true);
|
|
if (c0 < n) *(uint32_t*)(q + r * qp + c0) = (uint32_t)pk;
|
|
}
|
|
}
|
|
/* Operands: gate_up [M, 2n], q [M, n] E4M3, scale [M, ceil(n/128)] f32, out_bf16 [M, n] optional.
|
|
* n a multiple of 4 and group 128, as quant_act_fp8 requires. */
|
|
int clamped_quant_body(const RadArgs* a, RadStream stream) {
|
|
if (!a || a->n_t < 3) return RAD_E_INVAL;
|
|
const auto &gu = a->t[0], &q = a->t[1], &sc = a->t[2];
|
|
const RadTensor* y = a->n_t > 3 ? halo_opt(a, 3) : nullptr;
|
|
const long n = halo_geti_or(a, "n", 0), nkb = (n + 127) / 128;
|
|
const float limit = (float)halo_getf(a, "limit", 0);
|
|
if (gu.dtype != RAD_BF16 || q.dtype != RAD_F8E4M3 || sc.dtype != RAD_F32 ||
|
|
(y && y->dtype != RAD_BF16))
|
|
return RAD_E_DTYPE;
|
|
if (halo_geti_or(a, "group", 128) != 128 || n < 4 || n % 4 || n > INT_MAX / 2 ||
|
|
!std::isfinite(limit) || limit <= 0 || !gu.data || !q.data || !sc.data || gu.rank != 2 ||
|
|
q.rank != 2 || sc.rank != 2 || gu.shape[0] < 1 || gu.shape[0] > 65535 ||
|
|
gu.shape[1] != 2 * n || q.shape[0] != gu.shape[0] || q.shape[1] != n ||
|
|
sc.shape[0] != gu.shape[0] || sc.shape[1] != nkb || gu.stride[1] != 1 || q.stride[1] != 1 ||
|
|
sc.stride[1] != 1 || gu.stride[0] < 2 * n || q.stride[0] < n || sc.stride[0] < nkb ||
|
|
(y && (y->rank != 2 || y->shape[0] != gu.shape[0] || y->shape[1] != n ||
|
|
y->stride[1] != 1 || y->stride[0] < n)))
|
|
return RAD_E_SHAPE;
|
|
// A wave a group, at most eight: a row of GLM's TP8 expert width is two groups.
|
|
const unsigned thr = (unsigned)(nkb < 8 ? nkb : 8) * 32u;
|
|
return HALO_EMIT(stream, swiglu_clamped_quant_kernel, dim3((unsigned)gu.shape[0]), dim3(thr), 0,
|
|
(const uint16_t*)gu.data, y ? (uint16_t*)y->data : (uint16_t*)nullptr,
|
|
(uint8_t*)q.data, (float*)sc.data, gu.stride[0], y ? (long)y->stride[0] : 0L,
|
|
q.stride[0], sc.stride[0], (int)n, (int)nkb, limit);
|
|
}
|
|
}
|
|
extern "C" int halo_rad_swiglu_clamped(const RadArgs* a, RadStream stream) {
|
|
return clamped_body(a, stream);
|
|
}
|
|
extern "C" int halo_rad_swiglu_clamped_quant_fp8(const RadArgs* a, RadStream stream) {
|
|
return clamped_quant_body(a, stream);
|
|
}
|