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.
1119 lines
62 KiB
Plaintext
1119 lines
62 KiB
Plaintext
/* Derived from libr4d/r4d_fused_quant_fp8.hip by the libhalo rename transform (2026-10-26),
|
|
* then ported to gfx1151; see libhalo/README.md. */
|
|
// halo_fused_quant_fp8.hip - producers that already hold an activation row in registers, fused with
|
|
// halo_quant_act_fp8's per-(row, 128) E4M3 quantisation.
|
|
//
|
|
// The fp8a8 GEMMs need a quantised activation before every linear (`activation_scheme: dynamic`,
|
|
// so no shipped scale). A decode step issues ~300 quantisers; each costs a launch, a grid fill and
|
|
// a second pass over a row another kernel just had in registers, not bandwidth. Fused producers:
|
|
//
|
|
// rmsnorm -> quant the input of every attention, GDN and MLP linear
|
|
// silu_mul -> quant down_proj's input
|
|
//
|
|
// A GEMM output feeding the next linear stays a separate launch. The int8 path has the same fusion
|
|
// (halo_rmsnorm_had_quant_i8, halo_gated_had_quant_i8), where the rotation is required to survive one
|
|
// scale per row; a 128-wide block scale does not need it.
|
|
//
|
|
// Bit-identical to the unfused pair it replaces (generated text is compared byte for byte across
|
|
// builds). The pair writes a bf16 norm output and the quantiser reads it back, so amax and codes
|
|
// are taken over bf16-rounded values; these kernels round to bf16 first and quantise the
|
|
// round-tripped float.
|
|
//
|
|
// The norm re-reads its row for the second pass, with or without the residual. Unlike
|
|
// halo_rmsnorm_had_quant_i8 (which normalises the f32 sum), the sum is rounded to bf16 and stored
|
|
// first, matching `add` writing a bf16 residual that `rmsnorm` reads back; the re-read is L1-hot.
|
|
//
|
|
// The residual may alias `out_bf16`, and every caller here does so: a block reads the previous
|
|
// block's delta from the shared `h` buffer and writes its norm output back into it. Pass one reads
|
|
// every `x` element before the block_sum barrier and pass two writes `y` after it, so they never
|
|
// race.
|
|
//
|
|
// `wadd` is added to every gain in f32 (see halo_rmsnorm_had_quant_i8): Qwen3.5's GemmaRMSNorm uses
|
|
// a zero-centred gamma, and forming `1 + w` in bf16 is a systematic 0.3% error. Pass 0 for a plain
|
|
// RMS norm.
|
|
//
|
|
// Returns a negative code rather than throwing, for the reason given in halo_gemm_fp8a8.hip.
|
|
|
|
#include "halo.h"
|
|
#include "halo_ar_seq.h" // the all-reduce sequence step the fused all-reduce + norm takes
|
|
#include "halo_fp8_args.h"
|
|
#include "halo_emit.h"
|
|
#include "halo_common.h" // halo_act_threads: the workgroup width, shared with the int8 path
|
|
#include "halo_fwht.h" // the in-register rotation the `_had_` forms put before the absmax
|
|
#include "halo_q8.h" // the 8-bit codes: E4M3, or int8 for an int8 trunk linear
|
|
|
|
namespace {
|
|
|
|
constexpr uint32_t kWave = 32;
|
|
constexpr uint32_t kGroup = 128;
|
|
|
|
__device__ __forceinline__ float bf2f(uint16_t h) { return __uint_as_float((uint32_t)h << 16); }
|
|
|
|
// Round-to-nearest-even, as every bf16 store in this library rounds.
|
|
__device__ __forceinline__ uint16_t f2bf(float f) {
|
|
const uint32_t u = __float_as_uint(f);
|
|
return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
|
|
}
|
|
|
|
// Both reductions use DPP and permlanex16 (halo_xor_lane, halo_wave_sum) rather than `__shfl_xor`,
|
|
// which gfx1201 lowers to ds_bpermute_b32, an LDS round trip per stage. Same partners in the same
|
|
// order, so the same bits.
|
|
__device__ __forceinline__ float wave_max(float v) {
|
|
v = fmaxf(v, halo_xor_lane<16>(v));
|
|
v = fmaxf(v, halo_xor_lane<8>(v));
|
|
v = fmaxf(v, halo_xor_lane<4>(v));
|
|
v = fmaxf(v, halo_xor_lane<2>(v));
|
|
v = fmaxf(v, halo_xor_lane<1>(v));
|
|
return v;
|
|
}
|
|
|
|
template <uint32_t NW>
|
|
__device__ __forceinline__ float block_sum(float v, float* lds) {
|
|
v = halo_wave_sum(v);
|
|
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
|
|
if (lane == 0) lds[wave] = v;
|
|
__syncthreads();
|
|
float s = 0.0f;
|
|
#pragma unroll
|
|
for (uint32_t w = 0; w < NW; ++w) s += lds[w];
|
|
return s;
|
|
}
|
|
|
|
// Quantise one 128-column group from the four bf16-rounded values a lane owns. The clamp in
|
|
// q8_pack4 is required (see halo_q8.h): v_cvt_pk_fp8_f32 returns NaN out of range, and
|
|
// `amax * (1/scale)` can land an ulp above 448.
|
|
//
|
|
// I8 writes int8 codes at amax / 127 instead (halo_q8.h), the same codes quant_act_i8g produces.
|
|
template <bool I8 = false>
|
|
__device__ __forceinline__ void quant_group(const float v[4], float* __restrict__ s,
|
|
uint8_t* __restrict__ q, size_t s_at, uint32_t c0,
|
|
uint32_t n, uint32_t lane) {
|
|
float m = fmaxf(fmaxf(fabsf(v[0]), fabsf(v[1])), fmaxf(fabsf(v[2]), fabsf(v[3])));
|
|
m = wave_max(m);
|
|
const float sc = m > 0.0f ? m * (1.0f / q8_top<I8>()) : 1.0f;
|
|
if (lane == 0) s[s_at] = sc;
|
|
const float inv = 1.0f / sc;
|
|
const uint32_t pk = q8_pack4<I8>(v[0], v[1], v[2], v[3], inv);
|
|
// A ragged last group is written zero-padded, as in halo_quant_act_fp8; the caller owns `n`
|
|
// rounded up to 4 bytes of `q`.
|
|
if (c0 < n) *(uint32_t*)(q + c0) = pk;
|
|
}
|
|
|
|
// Pass two of every norm kernel here: each wave takes a 128-column group and each lane four
|
|
// consecutive columns, so the group's amax is a wave reduction and the store a single dword. `nr`
|
|
// is the row the norm is taken of, already in memory (see the bit-identity note above). Shared so
|
|
// the fused all-reduce cannot drift from the standalone norm.
|
|
//
|
|
// HI8 is had_quant_act_i8g's form (the `_had_quant_i8g` ops): the width-128 Hadamard between the
|
|
// bf16-rounded norm output and the absmax, then int8 codes, as gated_quant_fp8_kernel's ROT and I8
|
|
// together. halo_quant_act_fp8's kernel has the same mapping, rotation and rounding, so the codes
|
|
// are the norm + had_quant_act_i8g pair's bytes. `y` still receives the unrotated norm output.
|
|
// The launchers require n % 128 == 0, so no rotated group contains padding.
|
|
template <bool WF32, uint32_t THR, bool HI8 = false>
|
|
__device__ __forceinline__ void norm_quant_row(
|
|
const uint16_t* __restrict__ nr, const void* __restrict__ w, uint16_t* __restrict__ yr,
|
|
uint8_t* __restrict__ qr, float* __restrict__ s, size_t s_row, uint32_t n, uint32_t nkb,
|
|
float rs, float wadd) {
|
|
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
|
|
for (uint32_t kb = wave; kb < nkb; kb += THR / kWave) {
|
|
const uint32_t c0 = kb * kGroup + lane * 4u;
|
|
float v[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
|
// One branch for all four columns: every launcher requires n % 4 == 0, so a lane's columns
|
|
// are all in or all out. Per-column guards would serialise the loads.
|
|
if (c0 < n) {
|
|
float g[4];
|
|
uint16_t x[4];
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) {
|
|
g[j] = WF32 ? ((const float*)w)[c0 + j] : bf2f(((const uint16_t*)w)[c0 + j]);
|
|
x[j] = nr[c0 + j];
|
|
}
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) {
|
|
// The bf16 round trip is deliberate: the unfused quantiser reads these bytes.
|
|
const uint16_t h = f2bf(bf2f(x[j]) * rs * (g[j] + wadd));
|
|
if (yr) yr[c0 + j] = h;
|
|
v[j] = bf2f(h);
|
|
}
|
|
}
|
|
if constexpr (HI8) halo_fwht_wave<(int)kGroup>(v, (int)lane);
|
|
quant_group<HI8>(v, s, qr, s_row + kb, c0, n, lane);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------- rmsnorm -> quant
|
|
// One block per row. Pass one is the block-wide sum of squares; pass two is norm_quant_row.
|
|
template <bool WF32, bool RES, uint32_t THR, bool HI8 = false>
|
|
__global__ __launch_bounds__(THR) void rmsnorm_quant_fp8_kernel(
|
|
const uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w,
|
|
uint16_t* __restrict__ y, uint8_t* __restrict__ q, float* __restrict__ s, uint32_t n,
|
|
uint32_t nkb, float eps, float wadd, uint32_t x_ld, uint32_t r_ld, uint32_t y_ld,
|
|
uint32_t q_ld, uint32_t s_ld) {
|
|
__shared__ float lds[THR / kWave];
|
|
const uint32_t row = blockIdx.x;
|
|
const uint16_t* __restrict__ xr = x + (size_t)row * x_ld;
|
|
uint16_t* __restrict__ rr = RES ? res + (size_t)row * r_ld : nullptr;
|
|
// The norm is taken of the residual when there is one, else of the input.
|
|
const uint16_t* __restrict__ nr = RES ? rr : xr;
|
|
|
|
// Four strides are loaded before any is used, avoiding one round trip per stride. A stride past
|
|
// the row reloads this thread's first column and is skipped below. The summation order is still
|
|
// that of `i = tid; i += THR`, shared by every kernel using halo_act_threads' partition.
|
|
constexpr uint32_t kU = 4;
|
|
float ss = 0.0f;
|
|
for (uint32_t i0 = threadIdx.x; i0 < n; i0 += kU * THR) {
|
|
uint16_t xa[kU], ra[kU];
|
|
#pragma unroll
|
|
for (uint32_t u = 0; u < kU; ++u) {
|
|
const uint32_t i = i0 + u * THR < n ? i0 + u * THR : i0;
|
|
xa[u] = xr[i];
|
|
if (RES) ra[u] = rr[i];
|
|
}
|
|
#pragma unroll
|
|
for (uint32_t u = 0; u < kU; ++u) {
|
|
const uint32_t i = i0 + u * THR;
|
|
if (i < n) {
|
|
float v;
|
|
if (RES) {
|
|
const uint16_t h = f2bf(bf2f(ra[u]) + bf2f(xa[u]));
|
|
rr[i] = h;
|
|
v = bf2f(h);
|
|
} else {
|
|
v = bf2f(xa[u]);
|
|
}
|
|
ss += v * v;
|
|
}
|
|
}
|
|
}
|
|
const float rs = rsqrtf(block_sum<THR / kWave>(ss, lds) / (float)n + eps);
|
|
|
|
norm_quant_row<WF32, THR, HI8>(nr, w, y ? y + (size_t)row * y_ld : nullptr,
|
|
q + (size_t)row * q_ld, s, (size_t)row * s_ld, n, nkb, rs, wadd);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------- gate -> quant
|
|
// silu(gate) * up or sigmoid(gate) * up, then the same quantiser. No row reduction, so no first
|
|
// pass and no barrier: one wave per group.
|
|
//
|
|
// ROT inserts the width-128 Hadamard between the product and the absmax, in registers, since this
|
|
// mapping (one wave per group, four values per lane) is halo_fwht_wave<128>'s. `y` still receives
|
|
// the unrotated product: it is for a bf16 consumer, and the rotation belongs to the quantised pair
|
|
// only.
|
|
//
|
|
// The launcher sizes the workgroup to the row's groups (capped at halo_act_threads' width) and the
|
|
// kernel reads the width from blockDim, so a narrow row does not leave most waves idle.
|
|
template <int ACT, bool ROT = false, bool I8 = false>
|
|
__global__ __launch_bounds__(1024) void gated_quant_fp8_kernel(
|
|
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y, uint8_t* __restrict__ q,
|
|
float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t gu_ld, uint32_t y_ld,
|
|
uint32_t q_ld, uint32_t s_ld) {
|
|
const uint32_t row = blockIdx.x;
|
|
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
|
|
const uint16_t* __restrict__ gr = gu + (size_t)row * gu_ld;
|
|
uint16_t* __restrict__ yr = y ? y + (size_t)row * y_ld : nullptr;
|
|
uint8_t* __restrict__ qr = q + (size_t)row * q_ld;
|
|
|
|
for (uint32_t kb = wave; kb < nkb; kb += blockDim.x / kWave) {
|
|
const uint32_t c0 = kb * kGroup + lane * 4u;
|
|
float v[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
|
// One branch for all four columns; see norm_quant_row.
|
|
if (c0 < n) {
|
|
uint16_t gb[4], ub[4];
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) {
|
|
gb[j] = gr[c0 + j];
|
|
ub[j] = gr[n + c0 + j];
|
|
}
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) {
|
|
const float g = bf2f(gb[j]);
|
|
const float a = ACT == 0 ? g / (1.0f + expf(-g)) : 1.0f / (1.0f + expf(-g));
|
|
const uint16_t h = f2bf(a * bf2f(ub[j]));
|
|
if (yr) yr[c0 + j] = h;
|
|
v[j] = bf2f(h);
|
|
}
|
|
}
|
|
// The rotated launcher requires n % 128 == 0, so no rotated group contains padding.
|
|
if constexpr (ROT) halo_fwht_wave<(int)kGroup>(v, (int)lane);
|
|
quant_group<I8>(v, s, qr, (size_t)row * s_ld + kb, c0, n, lane);
|
|
}
|
|
}
|
|
|
|
// -------------------------------------------------- gate . x -> quant, two source buffers
|
|
//
|
|
// The same epilogue with the gate in a separate buffer. For gated attention, `x` is the attention
|
|
// output [T, n_head * head_dim] and the gate is a column slice of the [q|gate] projection,
|
|
// [T * n_head, 2 * head_dim] with the gate half at `g_off`. A row is a (token, head) pair: `x` is
|
|
// then contiguous, the gate a fixed stride apart, and the scale plane [T * n_head, head_dim / 128]
|
|
// is byte-identical to the [T, n_head * head_dim / 128] the out projection reads. Replaces three
|
|
// launches (sigmoid, multiply, quantise) per attention layer.
|
|
template <int ACT, bool I8 = false>
|
|
__global__ __launch_bounds__(1024) void gate_quant_fp8_kernel(
|
|
const uint16_t* __restrict__ g, const uint16_t* __restrict__ x, uint16_t* __restrict__ y,
|
|
uint8_t* __restrict__ q, float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t g_ld,
|
|
uint32_t x_ld, uint32_t y_ld, uint32_t q_ld, uint32_t s_ld) {
|
|
const uint32_t row = blockIdx.x;
|
|
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
|
|
const uint16_t* __restrict__ gr = g + (size_t)row * g_ld;
|
|
const uint16_t* __restrict__ xr = x + (size_t)row * x_ld;
|
|
uint16_t* __restrict__ yr = y ? y + (size_t)row * y_ld : nullptr;
|
|
uint8_t* __restrict__ qr = q + (size_t)row * q_ld;
|
|
|
|
for (uint32_t kb = wave; kb < nkb; kb += blockDim.x / kWave) {
|
|
const uint32_t c0 = kb * kGroup + lane * 4u;
|
|
float v[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
|
// One branch for all four columns; see norm_quant_row.
|
|
if (c0 < n) {
|
|
uint16_t gb[4], xb[4];
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) {
|
|
gb[j] = gr[c0 + j];
|
|
xb[j] = xr[c0 + j];
|
|
}
|
|
#pragma unroll
|
|
for (int j = 0; j < 4; ++j) {
|
|
const float gv = bf2f(gb[j]);
|
|
// The activation is rounded to bf16 before the multiply, unlike gated_quant_fp8:
|
|
// the unfused reference here is `sigmoid` then `mul` with a bf16 buffer between
|
|
// them (silu_mul_bf16 keeps f32). Skipping the round trip changes the output.
|
|
const uint16_t ab =
|
|
f2bf(ACT == 0 ? gv / (1.0f + expf(-gv)) : 1.0f / (1.0f + expf(-gv)));
|
|
const uint16_t h = f2bf(bf2f(ab) * bf2f(xb[j]));
|
|
if (yr) yr[c0 + j] = h;
|
|
v[j] = bf2f(h);
|
|
}
|
|
}
|
|
quant_group<I8>(v, s, qr, (size_t)row * s_ld + kb, c0, n, lane);
|
|
}
|
|
}
|
|
|
|
// -------------------------------------------------------- all-reduce -> rmsnorm -> quant
|
|
//
|
|
// The same norm with the tensor-parallel all-reduce that always precedes it folded in (o_proj,
|
|
// out_proj and down_proj outputs are all-reduced, then normalised). Saves a launch, a dispatch gap
|
|
// and a round trip through memory. The int8 path has the same fusion (halo_ar_ln_had_quant_i8).
|
|
//
|
|
// Partitioned by row: halo_ar_oneshot_2rank_exact gives block b a contiguous range of 16B words with
|
|
// its own flag and no grid barrier, but a sum of squares needs whole rows. Rows are contiguous, so a
|
|
// row range is a word range; both ranks derive it from (M, gridDim.x), keeping the handshake
|
|
// per block.
|
|
//
|
|
// Bit-identical to the pair it replaces. Pass two is norm_quant_row. Pass one reproduces the
|
|
// all-reduce's arithmetic (an f32 sum stored as bf16, as in halo_ar_oneshot_2rank_exact's vadd) and
|
|
// stores it back into `x`, as the replaced kernel did, so other readers of `x` see the same bytes.
|
|
// Both sum the squares in halo_act_threads' `i = tid; i += THR` order.
|
|
//
|
|
// Both wires are served. Above --tp-wire-min-kb the message is sent 6-bit rotated; a step carries
|
|
// n_seq * (1 + n_spec) rows, so it crosses that floor from the second concurrent sequence on, and
|
|
// an exact-wire-only fusion would rarely apply.
|
|
//
|
|
// The compressed wire is halo_ar_oneshot_2rank_wht6's format (orthonormal Walsh-Hadamard over 64,
|
|
// amax-scaled, 6 uniform bits plus a bf16 scale) packed in chunks of 512 elements instead of 2048.
|
|
// Rotation and amax close over a 64-element group, so the bytes are identical; the chunk size only
|
|
// sets ownership. A chunk may not straddle a row (both ranks must agree on a block's bytes), and a
|
|
// wave owns a whole chunk, so a smaller chunk employs more waves (see halo_ar_ln_had_quant_i8.hip).
|
|
//
|
|
// The reduced row goes back to global `x`, not LDS: unlike the int8 twin this norm needs no
|
|
// in-place rotation, and pass two re-reads the row L1-hot as the standalone norm does.
|
|
//
|
|
// This rank's own packed half also goes to global, in the standalone kernel's side buffer
|
|
// (`loc_pack`). LDS (64 KiB) holds only ~15 rows of ~4 KiB packed halves at n = 5120, while one op
|
|
// serves M = 8 through a 2048-row prefill chunk; a refused launch would abort the step. The side
|
|
// buffer costs 0.75 bytes per element, written and read back from L2.
|
|
#define HALO_ARQ_SPIN_MAX 4000000000ULL
|
|
|
|
#define ARQ_GROUP 64
|
|
#define ARQ_BITS 6
|
|
#define ARQ_EPL 8
|
|
#define ARQ_LPG (ARQ_GROUP / ARQ_EPL) /* 8 lanes span one group */
|
|
#define ARQ_WV 32
|
|
#define ARQ_GPW (ARQ_WV * ARQ_EPL / ARQ_GROUP) /* 4 groups a wave-iteration */
|
|
#define ARQ_CG 8 /* groups a chunk */
|
|
#define ARQ_CE (ARQ_CG * ARQ_GROUP) /* 512 elements a chunk */
|
|
#define ARQ_ITERS (ARQ_CE / (ARQ_WV * ARQ_EPL)) /* 2 iterations to cover a chunk */
|
|
#define ARQ_NWORD (ARQ_ITERS * ARQ_EPL * ARQ_BITS / 32) /* 3 dwords a lane holds for a chunk */
|
|
#define ARQ_CBYTES (ARQ_CE * ARQ_BITS / 8) /* 384 wire bytes a chunk */
|
|
#define ARQ_QMAX ((1 << (ARQ_BITS - 1)) - 1)
|
|
#define ARQ_QMIN (-(1 << (ARQ_BITS - 1)))
|
|
#define ARQ_MASK ((1u << ARQ_BITS) - 1u)
|
|
|
|
// `lo ? (v + o) : (o - v)` written as `o + (lo ? v : -v)`: the sign flip is one XOR with a mask
|
|
// uniform over the stage, removing the select. Bit-identical, since IEEE negation is exact.
|
|
template <int M>
|
|
__device__ __forceinline__ void arq_xstage(float* v, int lig) {
|
|
const unsigned int sm = (lig & M) ? 0x80000000u : 0u;
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i) {
|
|
const float o = halo_xor_lane<M>(v[i]);
|
|
v[i] = __int_as_float(__float_as_int(v[i]) ^ sm) + o;
|
|
}
|
|
}
|
|
|
|
// Orthonormal Walsh-Hadamard over one group; its own inverse, so this also undoes it.
|
|
__device__ __forceinline__ void arq_wht(float* v, int lig) {
|
|
static_assert(ARQ_LPG == 8, "the cross-lane stages are unrolled for a group of 8 lanes");
|
|
#pragma unroll
|
|
for (int h = 1; h < ARQ_EPL; h <<= 1)
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i)
|
|
if ((i & h) == 0) { const float a = v[i], c = v[i ^ h]; v[i] = a + c; v[i ^ h] = a - c; }
|
|
arq_xstage<1>(v, lig);
|
|
arq_xstage<2>(v, lig);
|
|
arq_xstage<4>(v, lig);
|
|
const float nrm = rsqrtf((float)ARQ_GROUP);
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i) v[i] *= nrm;
|
|
}
|
|
|
|
__device__ __forceinline__ void arq_store_rel(unsigned int* p, unsigned int v) {
|
|
__hip_atomic_store(p, v, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_SYSTEM);
|
|
}
|
|
__device__ __forceinline__ unsigned int arq_load_acq(const unsigned int* p) {
|
|
return __hip_atomic_load(p, __ATOMIC_ACQUIRE, __HIP_MEMORY_SCOPE_SYSTEM);
|
|
}
|
|
|
|
// POST: a sandwich-norm junction (Gemma 3): the reduced sum is post-normed against `wp` before the
|
|
// residual add, `v = bf16(sum * s1 * (wp + wadd))` with s1 the sum's own rms, as rmsnorm_quant_i8's
|
|
// POST computes it. `x` still receives the reduced sum. Exact wire and a residual only.
|
|
//
|
|
// HI8: ar_rmsnorm_had_quant_i8g, pass two in norm_quant_row's rotated int8 form. The reduction and
|
|
// the norm are untouched, so `x`, the residual and `y` are ar_rmsnorm_quant_fp8's bytes.
|
|
template <bool WF32, bool RES, uint32_t THR, int WIRE, bool POST = false, bool HI8 = false>
|
|
__global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel(
|
|
uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w,
|
|
const void* __restrict__ wp,
|
|
uint16_t* __restrict__ y, uint8_t* __restrict__ q, float* __restrict__ s,
|
|
int4* peer_base, const int4* my_base, unsigned int* peer_flags, unsigned int* my_flags,
|
|
unsigned int* seq_ctrs, char* loc_pack, long loc_scale_off, int M, uint32_t n, uint32_t nkb,
|
|
float eps, float wadd, uint32_t r_ld, uint32_t y_ld, uint32_t q_ld, uint32_t s_ld,
|
|
int slot_stride16, int drain, int acq, unsigned int nbw) {
|
|
__shared__ float lds[THR / kWave];
|
|
__shared__ unsigned int s_seq, s_slot;
|
|
const int b = (int)blockIdx.x, nb = (int)gridDim.x;
|
|
const uint32_t tid = threadIdx.x;
|
|
/* Integer arithmetic only, so both ranks land on the same split. */
|
|
const int r0 = (int)(((long)b * M) / nb), r1 = (int)(((long)(b + 1) * M) / nb);
|
|
/* Chunks per row and this block's window into the global chunk numbering, derived identically
|
|
* on both ranks from (M, nb, n). */
|
|
const int cpr = WIRE ? (int)(n / ARQ_CE) : 0;
|
|
const int c00 = r0 * cpr, cpb = (r1 - r0) * cpr;
|
|
const uint32_t lane = tid & (ARQ_WV - 1), wave = tid / ARQ_WV;
|
|
const int lig = (int)(lane & (ARQ_LPG - 1)), gw = (int)(lane / ARQ_LPG);
|
|
|
|
halo_ar_seq_take(seq_ctrs, nbw, s_seq, s_slot); /* device-resident, replay-safe */
|
|
const unsigned int sq = s_seq;
|
|
int4* const ps = peer_base + (size_t)s_slot * slot_stride16;
|
|
const int4* const ms = my_base + (size_t)s_slot * slot_stride16;
|
|
/* The slot is split as in the standalone compressed all-reduce: packed symbols from the base,
|
|
* bf16 group scales from half way up. Group indices are global over the [M][n] message. */
|
|
char* const p_pay = (char*)ps;
|
|
const char* const m_pay = (const char*)ms;
|
|
unsigned short* const p_sc =
|
|
(unsigned short*)((char*)ps + (size_t)(slot_stride16 >> 1) * 16);
|
|
const unsigned short* const m_sc =
|
|
(const unsigned short*)((const char*)ms + (size_t)(slot_stride16 >> 1) * 16);
|
|
/* This rank's own packed half, laid out like the peer slot (symbols from the base, scales from
|
|
* `loc_scale_off`) and indexed by global chunk and group, so blocks never overlap. */
|
|
unsigned int* const Lpk = (unsigned int*)loc_pack;
|
|
float* const Lsc = (float*)(loc_pack + loc_scale_off);
|
|
|
|
unsigned int hck = 0u; // the push check (halo_ar_seq.h)
|
|
if constexpr (WIRE == 0) {
|
|
/* Push my rows into the peer's slot. `x` is a tight [M][n] of bf16 (the launcher refuses a
|
|
* pitched one), so a row range is a 16B-word range. */
|
|
const int wpr = (int)(n >> 3);
|
|
const int wend = r1 * wpr;
|
|
const int4* const in4 = (const int4*)x;
|
|
for (int k = r0 * wpr + (int)tid; k < wend; k += (int)THR) {
|
|
const int4 v = in4[k];
|
|
ps[k] = v;
|
|
hck += halo_archk_u4(v, 4u * (unsigned)k);
|
|
}
|
|
} else {
|
|
/* Rotate, amax, quantise and pack straight into the peer's slot. A wave owns whole chunks,
|
|
* so every lane emits ARQ_NWORD dwords per chunk and the flush is lane-uniform. No group is
|
|
* partial (n is whole chunks, a block owns whole rows), so the standalone kernel's `live`
|
|
* guard is unnecessary and every cross-lane stage is fully active. */
|
|
for (int ci = (int)wave; ci < cpb; ci += (int)(THR / ARQ_WV)) {
|
|
const int c = c00 + ci;
|
|
unsigned int acc[ARQ_NWORD];
|
|
#pragma unroll
|
|
for (int wi = 0; wi < ARQ_NWORD; ++wi) acc[wi] = 0u;
|
|
#pragma unroll
|
|
for (int t = 0; t < ARQ_ITERS; ++t) {
|
|
const int g = c * ARQ_CG + t * ARQ_GPW + gw;
|
|
const long off = (long)g * ARQ_GROUP + (long)lig * ARQ_EPL;
|
|
float v[ARQ_EPL];
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i) v[i] = bf2f(x[off + i]);
|
|
arq_wht(v, lig);
|
|
float a = 0.0f;
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i) a = fmaxf(a, fabsf(v[i]));
|
|
a = fmaxf(a, halo_xor_lane<1>(a));
|
|
a = fmaxf(a, halo_xor_lane<2>(a));
|
|
a = fmaxf(a, halo_xor_lane<4>(a));
|
|
float sc = (a > 0.0f) ? bf2f(f2bf(a * (1.0f / (float)ARQ_QMAX))) : 1.0f;
|
|
if (!(sc > 0.0f)) sc = 1.0f;
|
|
if (lig == 0) {
|
|
const unsigned short sc16 = (unsigned short)(__float_as_uint(sc) >> 16);
|
|
p_sc[g] = sc16;
|
|
Lsc[g] = sc;
|
|
hck += halo_archk_mix(sc16, HALO_ARCHK_U16 + (unsigned)g);
|
|
}
|
|
const float inv = 1.0f / sc;
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i) {
|
|
const unsigned int sym = (unsigned int)(
|
|
min(max((int)rintf(v[i] * inv), ARQ_QMIN), ARQ_QMAX) - ARQ_QMIN);
|
|
const int bit = (t * ARQ_EPL + i) * ARQ_BITS, wi = bit >> 5, bo = bit & 31;
|
|
acc[wi] |= sym << bo;
|
|
if (bo + ARQ_BITS > 32) acc[wi + 1] |= sym >> (32 - bo);
|
|
}
|
|
}
|
|
/* Flush lane-contiguously, widest region first, so each is one coalesced store. */
|
|
char* const pb = p_pay + (size_t)c * ARQ_CBYTES;
|
|
const unsigned int cd = (unsigned)c * (ARQ_CBYTES / 4); // the chunk's first dword
|
|
#pragma unroll
|
|
for (int w4 = 0; w4 + 4 <= ARQ_NWORD; w4 += 4) {
|
|
*(uint4*)(pb + w4 * 4 * ARQ_WV + lane * 16) =
|
|
make_uint4(acc[w4], acc[w4 + 1], acc[w4 + 2], acc[w4 + 3]);
|
|
hck += halo_archk_u4(make_uint4(acc[w4], acc[w4 + 1], acc[w4 + 2], acc[w4 + 3]),
|
|
cd + (unsigned)(w4 * ARQ_WV) + lane * 4u);
|
|
}
|
|
if constexpr (ARQ_NWORD % 4 >= 2) {
|
|
*(uint2*)(pb + (ARQ_NWORD & ~3) * 4 * ARQ_WV + lane * 8) =
|
|
make_uint2(acc[ARQ_NWORD & ~3], acc[(ARQ_NWORD & ~3) + 1]);
|
|
const unsigned int e = cd + (unsigned)((ARQ_NWORD & ~3) * ARQ_WV) + lane * 2u;
|
|
hck += halo_archk_mix(acc[ARQ_NWORD & ~3], e) +
|
|
halo_archk_mix(acc[(ARQ_NWORD & ~3) + 1], e + 1u);
|
|
}
|
|
if constexpr (ARQ_NWORD % 2 == 1) {
|
|
*(unsigned int*)(pb + (ARQ_NWORD - 1) * 4 * ARQ_WV + lane * 4) =
|
|
acc[ARQ_NWORD - 1];
|
|
hck += halo_archk_mix(acc[ARQ_NWORD - 1],
|
|
cd + (unsigned)((ARQ_NWORD - 1) * ARQ_WV) + lane);
|
|
}
|
|
#pragma unroll
|
|
for (int wi = 0; wi < ARQ_NWORD; ++wi)
|
|
Lpk[((size_t)c * ARQ_NWORD + wi) * ARQ_WV + lane] = acc[wi];
|
|
}
|
|
}
|
|
halo_archk_put(hck, peer_flags, nbw, s_slot, (unsigned)b, sq);
|
|
/* The drain modes are halo_ar_oneshot_2rank_exact's, selected by the same variant table. */
|
|
if (drain == 1) __threadfence_system();
|
|
else if (drain == 3) asm volatile("s_waitcnt vmcnt(0)" ::: "memory"); // pre-gfx12 store wait
|
|
__syncthreads();
|
|
if (tid == 0) {
|
|
if (drain == 2) __threadfence_system();
|
|
arq_store_rel(&peer_flags[b], sq);
|
|
unsigned long long z = 0;
|
|
/* A handshake that cannot complete is a lockstep violation; trap rather than sum a slot the
|
|
* peer never filled. */
|
|
while (halo_ar_seq_behind(arq_load_acq(&my_flags[b]), sq)) {
|
|
if (++z > HALO_ARQ_SPIN_MAX) __builtin_trap();
|
|
}
|
|
}
|
|
__syncthreads();
|
|
if (acq) __threadfence_system();
|
|
halo_archk_wait(my_flags, nbw, s_slot, (unsigned)b, sq, [&](unsigned int& h) {
|
|
if constexpr (WIRE == 0) {
|
|
const int wpr = (int)(n >> 3);
|
|
const int4* const m4 = (const int4*)ms;
|
|
for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR)
|
|
h += halo_archk_u4(m4[k], 4u * (unsigned)k);
|
|
} else {
|
|
for (int ci = (int)wave; ci < cpb; ci += (int)(THR / ARQ_WV)) {
|
|
const int c = c00 + ci;
|
|
const char* const pb = m_pay + (size_t)c * ARQ_CBYTES;
|
|
const unsigned int cd = (unsigned)c * (ARQ_CBYTES / 4);
|
|
#pragma unroll
|
|
for (int w4 = 0; w4 + 4 <= ARQ_NWORD; w4 += 4)
|
|
h += halo_archk_u4(*(const uint4*)(pb + w4 * 4 * ARQ_WV + lane * 16),
|
|
cd + (unsigned)(w4 * ARQ_WV) + lane * 4u);
|
|
if constexpr (ARQ_NWORD % 4 >= 2) {
|
|
const uint2 u = *(const uint2*)(pb + (ARQ_NWORD & ~3) * 4 * ARQ_WV + lane * 8);
|
|
const unsigned int e = cd + (unsigned)((ARQ_NWORD & ~3) * ARQ_WV) + lane * 2u;
|
|
h += halo_archk_mix(u.x, e) + halo_archk_mix(u.y, e + 1u);
|
|
}
|
|
if constexpr (ARQ_NWORD % 2 == 1)
|
|
h += halo_archk_mix(*(const unsigned int*)(pb + (ARQ_NWORD - 1) * 4 * ARQ_WV + lane * 4),
|
|
cd + (unsigned)((ARQ_NWORD - 1) * ARQ_WV) + lane);
|
|
#pragma unroll
|
|
for (int t = 0; t < ARQ_ITERS; ++t) {
|
|
const int g = c * ARQ_CG + t * ARQ_GPW + gw;
|
|
if (lig == 0) h += halo_archk_mix(m_sc[g], HALO_ARCHK_U16 + (unsigned)g);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if constexpr (WIRE == 6) {
|
|
/* Reduce: unpack both halves of every chunk this block owns, add in fp32, inverse-rotate,
|
|
* and store the bf16 sum into `x`, as the standalone compressed all-reduce does. */
|
|
for (int ci = (int)wave; ci < cpb; ci += (int)(THR / ARQ_WV)) {
|
|
const int c = c00 + ci;
|
|
const char* const pb = m_pay + (size_t)c * ARQ_CBYTES;
|
|
unsigned int pac[ARQ_NWORD], lac[ARQ_NWORD];
|
|
#pragma unroll
|
|
for (int w4 = 0; w4 + 4 <= ARQ_NWORD; w4 += 4) {
|
|
const uint4 pq = *(const uint4*)(pb + w4 * 4 * ARQ_WV + lane * 16);
|
|
pac[w4] = pq.x; pac[w4 + 1] = pq.y; pac[w4 + 2] = pq.z; pac[w4 + 3] = pq.w;
|
|
}
|
|
if constexpr (ARQ_NWORD % 4 >= 2) {
|
|
const uint2 pu = *(const uint2*)(pb + (ARQ_NWORD & ~3) * 4 * ARQ_WV + lane * 8);
|
|
pac[ARQ_NWORD & ~3] = pu.x; pac[(ARQ_NWORD & ~3) + 1] = pu.y;
|
|
}
|
|
if constexpr (ARQ_NWORD % 2 == 1)
|
|
pac[ARQ_NWORD - 1] =
|
|
*(const unsigned int*)(pb + (ARQ_NWORD - 1) * 4 * ARQ_WV + lane * 4);
|
|
#pragma unroll
|
|
for (int wi = 0; wi < ARQ_NWORD; ++wi)
|
|
lac[wi] = Lpk[((size_t)c * ARQ_NWORD + wi) * ARQ_WV + lane];
|
|
#pragma unroll
|
|
for (int t = 0; t < ARQ_ITERS; ++t) {
|
|
const int gl = t * ARQ_GPW + gw;
|
|
const int g = c * ARQ_CG + gl;
|
|
const float psc = __uint_as_float((unsigned int)m_sc[g] << 16);
|
|
const float lsc = Lsc[g];
|
|
float v[ARQ_EPL];
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i) {
|
|
const int bit = (t * ARQ_EPL + i) * ARQ_BITS, wi = bit >> 5, bo = bit & 31;
|
|
unsigned int psy = pac[wi] >> bo, lsy = lac[wi] >> bo;
|
|
if (bo + ARQ_BITS > 32) {
|
|
psy |= pac[wi + 1] << (32 - bo);
|
|
lsy |= lac[wi + 1] << (32 - bo);
|
|
}
|
|
/* The two ranks add the pair in opposite order; fp32 add commutes exactly, so
|
|
* they agree bit for bit as long as neither product is contracted into an FMA
|
|
* (libhalo/CMakeLists.txt passes -ffp-contract=off for this). */
|
|
v[i] = (float)((int)(psy & ARQ_MASK) + ARQ_QMIN) * psc
|
|
+ (float)((int)(lsy & ARQ_MASK) + ARQ_QMIN) * lsc;
|
|
}
|
|
arq_wht(v, lig);
|
|
const long off = (long)g * ARQ_GROUP + (long)lig * ARQ_EPL;
|
|
#pragma unroll
|
|
for (int i = 0; i < ARQ_EPL; ++i) x[off + i] = f2bf(v[i]);
|
|
}
|
|
}
|
|
/* Pass two reads all of `x`'s row; each wave wrote only its own chunks. */
|
|
__syncthreads();
|
|
}
|
|
|
|
/* WIRE == 0: the peer's rows are now in this rank's scratch, in x's [M][n] layout. */
|
|
const uint16_t* __restrict__ P = (const uint16_t*)ms;
|
|
|
|
for (int row = r0; row < r1; ++row) {
|
|
uint16_t* __restrict__ xr = x + (size_t)row * n;
|
|
const uint16_t* __restrict__ pr = P + (size_t)row * n;
|
|
uint16_t* __restrict__ rr = RES ? res + (size_t)row * r_ld : nullptr;
|
|
const uint16_t* __restrict__ nr = RES ? rr : xr;
|
|
|
|
(void)pr;
|
|
float ss = 0.0f;
|
|
if constexpr (POST && WIRE == 0 && RES) {
|
|
/* Pass one reduces and takes the sum's sum of squares; pass two post-norms it, adds it
|
|
* into the stream and takes the stream's. Each thread reads back only its own columns. */
|
|
float s1s = 0.0f;
|
|
for (uint32_t i = tid; i < n; i += THR) {
|
|
const uint16_t a = f2bf(bf2f(xr[i]) + bf2f(pr[i]));
|
|
xr[i] = a;
|
|
const float av = bf2f(a);
|
|
s1s += av * av;
|
|
}
|
|
const float s1 = rsqrtf(block_sum<THR / kWave>(s1s, lds) / (float)n + eps);
|
|
__syncthreads(); // lds is reused by the second sum
|
|
for (uint32_t i = tid; i < n; i += THR) {
|
|
const float g = WF32 ? ((const float*)wp)[i] : bf2f(((const uint16_t*)wp)[i]);
|
|
const uint16_t pv = f2bf(bf2f(xr[i]) * s1 * (g + wadd));
|
|
const uint16_t h = f2bf(bf2f(rr[i]) + bf2f(pv));
|
|
rr[i] = h;
|
|
const float v = bf2f(h);
|
|
ss += v * v;
|
|
}
|
|
} else
|
|
for (uint32_t i = tid; i < n; i += THR) {
|
|
/* The exact wire reduces here, in the norm's first pass; the compressed one already
|
|
* reduced into `x`. */
|
|
uint16_t a;
|
|
if constexpr (WIRE == 0) { a = f2bf(bf2f(xr[i]) + bf2f(pr[i])); xr[i] = a; }
|
|
else { a = xr[i]; }
|
|
float v;
|
|
if (RES) { const uint16_t h = f2bf(bf2f(rr[i]) + bf2f(a)); rr[i] = h; v = bf2f(h); }
|
|
else { v = bf2f(a); }
|
|
ss += v * v;
|
|
}
|
|
/* block_sum's barrier is also what publishes pass one's stores to pass two's reads. */
|
|
const float rs = rsqrtf(block_sum<THR / kWave>(ss, lds) / (float)n + eps);
|
|
norm_quant_row<WF32, THR, HI8>(nr, w, y ? y + (size_t)row * y_ld : nullptr,
|
|
q + (size_t)row * q_ld, s, (size_t)row * s_ld, n, nkb, rs,
|
|
wadd);
|
|
/* lds is reused by this block's next row; without the barrier a thread still reading it
|
|
* would race the next row's write. */
|
|
__syncthreads();
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// `w` is the norm gain, f32 when `w_f32` and bf16 otherwise (see halo_model_bf16's rmsnorm). `y`
|
|
// may be null when nothing reads the bf16 post-norm tensor; every current caller has a reader (the
|
|
// fp8a16 matvec at M = 1).
|
|
//
|
|
// `res`, when given, is the fused_add_rms_norm form: `res += x` in f32, rounded to bf16 and stored,
|
|
// and the norm is taken of the stored value, bit-identical to `add` + `rmsnorm`. Pass 0 for the
|
|
// plain form. It may alias `y`; see the note at the top of the file.
|
|
//
|
|
// x [M][n] bf16, `x_ld` elements a row (0 = n)
|
|
// res [M][n] bf16, `r_ld` elements a row (0 = n), optional, read and written
|
|
// w [n] f32 or bf16
|
|
// y [M][n] bf16, `y_ld` elements a row (0 = n), optional
|
|
// q [M][n] E4M3 (rotated int8 when `hi8`), `q_ld` bytes a row (0 = n), rounded
|
|
// up to 4
|
|
// s [M][ceil(n/128)] f32, `s_ld` elements a row (0 = ceil(n/128))
|
|
//
|
|
// `hi8` additionally requires n % 128 == 0, as gated_quant_fp8_launch's `rot`.
|
|
static int rmsnorm_quant_launch(long x, long res, long w, long y, long q, long s, int M, int n,
|
|
float eps, float wadd, int w_f32, int x_ld, int r_ld, int y_ld,
|
|
int q_ld, int s_ld, long stream, bool hi8) {
|
|
if (!x || !w || !q || !s) return -1;
|
|
if (M <= 0 || n <= 0) return -2;
|
|
if (n % 4) return -3; // a lane owns four columns; see the kernel
|
|
if (hi8 && (n % (int)kGroup)) return -6;
|
|
if (!x_ld) x_ld = n;
|
|
if (!r_ld) r_ld = n;
|
|
if (!y_ld) y_ld = n;
|
|
if (!q_ld) q_ld = n;
|
|
const int nkb = (n + (int)kGroup - 1) / (int)kGroup;
|
|
if (!s_ld) s_ld = nkb;
|
|
|
|
// One workgroup per row, so a decode batch is 8-72 workgroups and the kernel is latency bound;
|
|
// the workgroup width is the parallelism knob (crossover table in halo_act_threads).
|
|
//
|
|
// rmsnorm_bf16 must pick the same width from the same function: the width decides which
|
|
// columns a thread sums, so a different width changes the last bits and breaks bit identity.
|
|
const int thr = halo_act_threads(M, n);
|
|
#define HALO_RQ_H(wf, rs, T, H) \
|
|
hipLaunchKernelGGL((rmsnorm_quant_fp8_kernel<wf, rs, T, H>), dim3((uint32_t)M), dim3(T), 0, \
|
|
(hipStream_t)stream, (const uint16_t*)x, (uint16_t*)res, (const void*)w, \
|
|
(uint16_t*)y, (uint8_t*)q, (float*)s, (uint32_t)n, (uint32_t)nkb, eps, \
|
|
wadd, (uint32_t)x_ld, (uint32_t)r_ld, (uint32_t)y_ld, (uint32_t)q_ld, \
|
|
(uint32_t)s_ld)
|
|
#define HALO_RQ_T(wf, rs, T) \
|
|
do { if (hi8) HALO_RQ_H(wf, rs, T, true); else HALO_RQ_H(wf, rs, T, false); } while (0)
|
|
#define HALO_RQ(wf, rs) \
|
|
do { if (thr == 1024) HALO_RQ_T(wf, rs, 1024); \
|
|
else if (thr == 512) HALO_RQ_T(wf, rs, 512); \
|
|
else HALO_RQ_T(wf, rs, 256); } while (0)
|
|
if (w_f32) { if (res) HALO_RQ(true, true); else HALO_RQ(true, false); }
|
|
else { if (res) HALO_RQ(false, true); else HALO_RQ(false, false); }
|
|
#undef HALO_RQ
|
|
#undef HALO_RQ_T
|
|
#undef HALO_RQ_H
|
|
return hipGetLastError() == hipSuccess ? 0 : -4;
|
|
}
|
|
|
|
extern "C" int halo_rmsnorm_quant_fp8(long x, long res, long w, long y, long q, long s, int M, int n,
|
|
float eps, float wadd, int w_f32, int x_ld, int r_ld,
|
|
int y_ld, int q_ld, int s_ld, long stream) {
|
|
return rmsnorm_quant_launch(x, res, w, y, q, s, M, n, eps, wadd, w_f32, x_ld, r_ld, y_ld,
|
|
q_ld, s_ld, stream, false);
|
|
}
|
|
|
|
// The same norm with had_quant_act_i8g's quantiser on its pass: the width-128 Hadamard, then int8
|
|
// codes at amax / 127 per (row, 128). For a rotated int8 linear (dtype i8a8h); `q` is int8.
|
|
extern "C" int halo_rmsnorm_had_quant_i8g(long x, long res, long w, long y, long q, long s, int M,
|
|
int n, float eps, float wadd, int w_f32, int x_ld,
|
|
int r_ld, int y_ld, int q_ld, int s_ld, long stream) {
|
|
return rmsnorm_quant_launch(x, res, w, y, q, s, M, n, eps, wadd, w_f32, x_ld, r_ld, y_ld,
|
|
q_ld, s_ld, stream, true);
|
|
}
|
|
|
|
// `mode` is 0 for silu (down_proj's input) and 1 for sigmoid (a gated attention o_proj's), as in
|
|
// halo_gated_had_quant_i8.
|
|
//
|
|
// gu [M][2n] bf16, gate first then up, `gu_ld` elements a row (0 = 2n)
|
|
// y [M][n] bf16, `y_ld` elements a row (0 = n), optional
|
|
// q [M][n] E4M3 (int8 when `i8`), `q_ld` bytes a row (0 = n), rounded up to 4
|
|
// s [M][ceil(n/128)] f32, `s_ld` elements a row (0 = ceil(n/128))
|
|
//
|
|
// `rot` additionally requires n % 128 == 0: a ragged last group is fine unrotated (zero-padded,
|
|
// masked by the GEMM) but a butterfly mixing real values with padding has no matching weight block.
|
|
// `rot` with `i8` is had_quant_act_i8g's form (gated_had_quant_i8g), for a rotated int8 down
|
|
// projection; `rot` alone pairs with a rotated four-bit expert, which reads E4M3.
|
|
static int gated_quant_fp8_launch(long gu, long y, long q, long s, int M, int n, int mode,
|
|
int gu_ld, int y_ld, int q_ld, int s_ld, RadStream stream,
|
|
bool rot, bool i8 = false) {
|
|
int emit_rc = RAD_OK;
|
|
if (!gu || !q || !s) return -1;
|
|
if (M <= 0 || n <= 0) return -2;
|
|
if (n % 4) return -3;
|
|
if (mode != 0 && mode != 1) return -5;
|
|
if (rot && (n % (int)kGroup)) return -6;
|
|
if (!gu_ld) gu_ld = 2 * n;
|
|
if (!y_ld) y_ld = n;
|
|
if (!q_ld) q_ld = n;
|
|
const int nkb = (n + (int)kGroup - 1) / (int)kGroup;
|
|
if (!s_ld) s_ld = nkb;
|
|
// One wave per group, capped at halo_act_threads' width. The width does not affect the numerics:
|
|
// there is no cross-row reduction and the rotation is wave-local (halo_fwht.h).
|
|
const int thr = std::min(nkb, halo_act_threads(M, n) / (int)kWave) * (int)kWave;
|
|
#define HALO_GQ_R(a, R, I) \
|
|
emit_rc = HALO_EMIT(stream, (gated_quant_fp8_kernel<a, R, I>), dim3((uint32_t)M), dim3(thr), \
|
|
0, (const uint16_t*)gu, (uint16_t*)y, (uint8_t*)q, \
|
|
(float*)s, (uint32_t)n, (uint32_t)nkb, (uint32_t)gu_ld, (uint32_t)y_ld, \
|
|
(uint32_t)q_ld, (uint32_t)s_ld)
|
|
#define HALO_GQ(a) \
|
|
do { if (rot && i8) HALO_GQ_R(a, true, true); \
|
|
else if (rot) HALO_GQ_R(a, true, false); \
|
|
else if (i8) HALO_GQ_R(a, false, true); \
|
|
else HALO_GQ_R(a, false, false); } while (0)
|
|
if (mode == 0) HALO_GQ(0);
|
|
else HALO_GQ(1);
|
|
#undef HALO_GQ
|
|
#undef HALO_GQ_R
|
|
if (emit_rc != RAD_OK) return -4;
|
|
return (hipGetLastError() != hipSuccess) ? -4 : 0;
|
|
}
|
|
|
|
extern "C" int halo_gated_quant_fp8(long gu, long y, long q, long s, int M, int n, int mode,
|
|
int gu_ld, int y_ld, int q_ld, int s_ld, long stream) {
|
|
return gated_quant_fp8_launch(gu, y, q, s, M, n, mode, gu_ld, y_ld, q_ld, s_ld,
|
|
(RadStream)(uintptr_t)stream, false);
|
|
}
|
|
|
|
extern "C" int halo_gated_had_quant_fp8(long gu, long y, long q, long s, int M, int n, int mode,
|
|
int gu_ld, int y_ld, int q_ld, int s_ld, long stream) {
|
|
return gated_quant_fp8_launch(gu, y, q, s, M, n, mode, gu_ld, y_ld, q_ld, s_ld,
|
|
(RadStream)(uintptr_t)stream, true);
|
|
}
|
|
|
|
// The same product with had_quant_act_i8g's quantiser: `q` is int8.
|
|
extern "C" int halo_gated_had_quant_i8g(long gu, long y, long q, long s, int M, int n, int mode,
|
|
int gu_ld, int y_ld, int q_ld, int s_ld, long stream) {
|
|
return gated_quant_fp8_launch(gu, y, q, s, M, n, mode, gu_ld, y_ld, q_ld, s_ld,
|
|
(RadStream)(uintptr_t)stream, true, true);
|
|
}
|
|
|
|
// act . x with the gate and x in separate buffers; see the kernel for why a row is a (token, head)
|
|
// pair and why the activation is rounded to bf16 before the product. `mode` as in
|
|
// halo_gated_quant_fp8.
|
|
//
|
|
// g [M][.] bf16, `g_ld` elements a row; the gate starts at element 0 of the row,
|
|
// so a caller with a [q|gate] projection passes g already offset
|
|
// x [M][n] bf16, `x_ld` elements a row (0 = n)
|
|
// y [M][n] bf16, `y_ld` elements a row (0 = n), optional
|
|
// q [M][n] E4M3 (int8 when `i8`), `q_ld` bytes a row (0 = n), rounded up to 4
|
|
// s [M][ceil(n/128)] f32, `s_ld` elements a row (0 = ceil(n/128))
|
|
static int gate_quant_fp8_emit(long g, long x, long y, long q, long s, int M, int n, int mode,
|
|
int g_ld, int x_ld, int y_ld, int q_ld, int s_ld,
|
|
RadStream stream, bool i8 = false) {
|
|
int emit_rc = RAD_OK;
|
|
if (!g || !x || !q || !s) return -1;
|
|
if (M <= 0 || n <= 0) return -2;
|
|
if (n % 4) return -3;
|
|
if (mode != 0 && mode != 1) return -5;
|
|
if (!g_ld) g_ld = n;
|
|
if (!x_ld) x_ld = n;
|
|
if (!y_ld) y_ld = n;
|
|
if (!q_ld) q_ld = n;
|
|
const int nkb = (n + (int)kGroup - 1) / (int)kGroup;
|
|
if (!s_ld) s_ld = nkb;
|
|
// A wave a group, capped at halo_act_threads' width, as gated_quant_fp8 above.
|
|
const int thr = std::min(nkb, halo_act_threads(M, n) / (int)kWave) * (int)kWave;
|
|
#define HALO_GTQ_I(a, I) \
|
|
emit_rc = HALO_EMIT(stream, (gate_quant_fp8_kernel<a, I>), dim3((uint32_t)M), dim3(thr), 0, \
|
|
(const uint16_t*)g, (const uint16_t*)x, \
|
|
(uint16_t*)y, (uint8_t*)q, (float*)s, (uint32_t)n, (uint32_t)nkb, \
|
|
(uint32_t)g_ld, (uint32_t)x_ld, (uint32_t)y_ld, (uint32_t)q_ld, \
|
|
(uint32_t)s_ld)
|
|
#define HALO_GTQ(a) do { if (i8) HALO_GTQ_I(a, true); else HALO_GTQ_I(a, false); } while (0)
|
|
if (mode == 0) HALO_GTQ(0);
|
|
else HALO_GTQ(1);
|
|
#undef HALO_GTQ
|
|
#undef HALO_GTQ_I
|
|
if (emit_rc != RAD_OK) return -4;
|
|
return (hipGetLastError() != hipSuccess) ? -4 : 0;
|
|
}
|
|
|
|
extern "C" int halo_gate_quant_fp8(long g, long x, long y, long q, long s, int M, int n, int mode,
|
|
int g_ld, int x_ld, int y_ld, int q_ld, int s_ld, long stream) {
|
|
return gate_quant_fp8_emit(g, x, y, q, s, M, n, mode, g_ld, x_ld, y_ld, q_ld, s_ld,
|
|
(RadStream)(uintptr_t)stream);
|
|
}
|
|
|
|
// The fused all-reduce. The IPC arguments are halo_ar_oneshot_2rank_exact's (double-buffered
|
|
// scratch, per-block flags, device-resident sequence counters); the rest are halo_rmsnorm_quant_fp8's,
|
|
// except:
|
|
//
|
|
// * `x` has no pitch: it is also the all-reduce message, pushed as a range of 16B words, and a
|
|
// pitch would make the ranks disagree about a block's bytes.
|
|
// * `nblocks` is clamped to M, since a block owns whole rows.
|
|
//
|
|
// `res` behaves as in halo_rmsnorm_quant_fp8. Returns 0, or a negative code for an unserved shape.
|
|
extern "C" int halo_ar_rmsnorm_quant_fp8_chunk_elems(void) { return ARQ_CE; }
|
|
|
|
// `hi8` is the rotated int8 form (ar_rmsnorm_had_quant_i8g): `q` is int8, n % 128 == 0, and there
|
|
// is no post-norm.
|
|
static int ar_rmsnorm_quant_launch(long peer_scratch, long my_scratch, long peer_flags,
|
|
long my_flags, long seq_ctrs, long slot_stride16,
|
|
long x, long res, long w, long y, long q, long s,
|
|
int M, int n, float eps, float wadd, int w_f32,
|
|
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
|
int nblocks, int nthreads, int drain, int acq, int wire,
|
|
long loc_pack, long loc_scale_off, long wp, bool hi8) {
|
|
if (!x || !w || !q || !s) return -1;
|
|
if (wp && (!res || wire != 0)) return -10; // a post-norm needs the stream and the exact wire
|
|
if (hi8 && wp) return -11;
|
|
if (hi8 && (n % (int)kGroup)) return -12;
|
|
if (!peer_scratch || !my_scratch || !peer_flags || !my_flags || !seq_ctrs) return -1;
|
|
if (M <= 0 || n <= 0) return -2;
|
|
// n % 8: n bf16 is a whole number of 16B words (see the push), which also gives every lane its
|
|
// four columns (see norm_quant_row).
|
|
if (n % 8) return -5;
|
|
if (wire != 0 && wire != 6) return -7;
|
|
// A chunk may not straddle a row, or the ranks would disagree about a block's bytes.
|
|
if (wire == 6 && (n % ARQ_CE)) return -8;
|
|
if (wire == 6 && (!loc_pack || loc_scale_off <= 0)) return -9;
|
|
if (!r_ld) r_ld = n;
|
|
if (!y_ld) y_ld = n;
|
|
if (!q_ld) q_ld = n;
|
|
const int nkb = (n + (int)kGroup - 1) / (int)kGroup;
|
|
if (!s_ld) s_ld = nkb;
|
|
|
|
int nb = nblocks < 1 ? 1 : nblocks;
|
|
if (nb > M) nb = M;
|
|
const int nbw = halo_ar_max_blocks(); // the counter capacity; see halo_ar_seq.h
|
|
if (nb > nbw) nb = nbw;
|
|
// The width must be halo_act_threads', as for the standalone norm (it fixes the summation order).
|
|
const int thr = nthreads ? nthreads : halo_act_threads(M, n);
|
|
if (thr != 256 && thr != 512 && thr != 1024) return -6;
|
|
#define HALO_ARQ_H(wf, rs, T, W, PO, H) \
|
|
hipLaunchKernelGGL((ar_rmsnorm_quant_fp8_kernel<wf, rs, T, W, PO, H>), dim3((uint32_t)nb), \
|
|
dim3(T), 0, \
|
|
(hipStream_t)stream, (uint16_t*)x, (uint16_t*)res, (const void*)w, \
|
|
(const void*)wp, \
|
|
(uint16_t*)y, (uint8_t*)q, (float*)s, (int4*)peer_scratch, \
|
|
(const int4*)my_scratch, (unsigned int*)peer_flags, \
|
|
(unsigned int*)my_flags, (unsigned int*)seq_ctrs, (char*)loc_pack, \
|
|
(long)loc_scale_off, M, (uint32_t)n, \
|
|
(uint32_t)nkb, eps, wadd, (uint32_t)r_ld, (uint32_t)y_ld, (uint32_t)q_ld, \
|
|
(uint32_t)s_ld, (int)slot_stride16, drain, acq, (unsigned)nbw)
|
|
#define HALO_ARQ_P(wf, rs, T, W, PO) HALO_ARQ_H(wf, rs, T, W, PO, false)
|
|
#define HALO_ARQ_T(wf, rs, T, W) \
|
|
do { if (hi8) HALO_ARQ_H(wf, rs, T, W, false, true); \
|
|
else HALO_ARQ_P(wf, rs, T, W, false); } while (0)
|
|
#define HALO_ARQ_W(wf, rs, T) \
|
|
do { if (wire == 6) HALO_ARQ_T(wf, rs, T, 6); \
|
|
else if (rs && wp) HALO_ARQ_P(wf, rs, T, 0, true); \
|
|
else HALO_ARQ_T(wf, rs, T, 0); } while (0)
|
|
#define HALO_ARQ(wf, rs) \
|
|
do { if (thr == 1024) HALO_ARQ_W(wf, rs, 1024); \
|
|
else if (thr == 512) HALO_ARQ_W(wf, rs, 512); \
|
|
else HALO_ARQ_W(wf, rs, 256); } while (0)
|
|
if (w_f32) { if (res) HALO_ARQ(true, true); else HALO_ARQ(true, false); }
|
|
else { if (res) HALO_ARQ(false, true); else HALO_ARQ(false, false); }
|
|
#undef HALO_ARQ
|
|
#undef HALO_ARQ_W
|
|
#undef HALO_ARQ_T
|
|
#undef HALO_ARQ_P
|
|
#undef HALO_ARQ_H
|
|
return hipGetLastError() == hipSuccess ? 0 : -4;
|
|
}
|
|
|
|
extern "C" int halo_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long peer_flags,
|
|
long my_flags, long seq_ctrs, long slot_stride16,
|
|
long x, long res, long w, long y, long q, long s,
|
|
int M, int n, float eps, float wadd, int w_f32,
|
|
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
|
int nblocks, int nthreads, int drain, int acq, int wire,
|
|
long loc_pack, long loc_scale_off, long wp) {
|
|
return ar_rmsnorm_quant_launch(peer_scratch, my_scratch, peer_flags, my_flags, seq_ctrs,
|
|
slot_stride16, x, res, w, y, q, s, M, n, eps, wadd, w_f32,
|
|
r_ld, y_ld, q_ld, s_ld, stream, nblocks, nthreads, drain, acq,
|
|
wire, loc_pack, loc_scale_off, wp, false);
|
|
}
|
|
|
|
// The same with had_quant_act_i8g's quantiser on the norm's pass (no post-norm).
|
|
extern "C" int halo_ar_rmsnorm_had_quant_i8g(long peer_scratch, long my_scratch, long peer_flags,
|
|
long my_flags, long seq_ctrs, long slot_stride16,
|
|
long x, long res, long w, long y, long q, long s,
|
|
int M, int n, float eps, float wadd, int w_f32,
|
|
int r_ld, int y_ld, int q_ld, int s_ld, long stream,
|
|
int nblocks, int nthreads, int drain, int acq,
|
|
int wire, long loc_pack, long loc_scale_off) {
|
|
return ar_rmsnorm_quant_launch(peer_scratch, my_scratch, peer_flags, my_flags, seq_ctrs,
|
|
slot_stride16, x, res, w, y, q, s, M, n, eps, wadd, w_f32,
|
|
r_ld, y_ld, q_ld, s_ld, stream, nblocks, nthreads, drain, acq,
|
|
wire, loc_pack, loc_scale_off, 0, true);
|
|
}
|
|
|
|
// ============================================================== the plugin ABI entry points
|
|
//
|
|
// The shared operand reader is halo_fp8_args.h; see libhalo/halo_args.h.
|
|
/* ================================================================== the two fused quantisers
|
|
*
|
|
* The standalone quantiser's shape rules, applied to the operand that produces the row. The bf16
|
|
* output is optional so a layer without a bf16 consumer skips the store. */
|
|
|
|
/* `hi8`: rmsnorm_had_quant_i8g, whose codes are int8 and whose row is whole 128s (the rotation's
|
|
* width). Separate ops for the reason given at halo_rad_gated_had_quant_fp8. */
|
|
static int rad_rmsnorm_quant(const RadArgs* a, RadStream s, bool hi8) {
|
|
const RadTensor* x = halo_opt(a, RQ_X);
|
|
const RadTensor* rs = halo_opt(a, RQ_RES);
|
|
const RadTensor* w = halo_opt(a, RQ_W);
|
|
const RadTensor* q = halo_opt(a, RQ_Q);
|
|
const RadTensor* sc = halo_opt(a, RQ_S);
|
|
const RadTensor* y = halo_opt(a, RQ_Y);
|
|
if (!x || !w || !q || !sc) return HALO_NO(RAD_E_INVAL);
|
|
if (q->dtype != (hi8 ? RAD_I8 : RAD_F8E4M3)) return HALO_NO(RAD_E_DTYPE);
|
|
/* Rank 2 on the input too, unlike the standalone quantiser: a norm has one gain per channel,
|
|
* and folding trailing dims would spread a gain across heads. */
|
|
if (x->rank != 2 || q->rank != 2 || sc->rank != 2) return HALO_NO(RAD_E_SHAPE);
|
|
if (y && y->rank != 2) return HALO_NO(RAD_E_SHAPE);
|
|
const long long M = x->shape[0], K = x->shape[1];
|
|
const long long grp = halo_quant_act_fp8_group();
|
|
const long long nkb = (K + grp - 1) / grp;
|
|
if (halo_geti_or(a, "group", grp) != grp) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(w) != K) return HALO_NO(RAD_E_SHAPE);
|
|
if (w->dtype != RAD_F32 && w->dtype != RAD_BF16) return HALO_NO(RAD_E_DTYPE);
|
|
if (q->shape[0] != M || q->shape[1] != K) return HALO_NO(RAD_E_SHAPE);
|
|
if (sc->shape[0] != M || sc->shape[1] != nkb) return HALO_NO(RAD_E_SHAPE);
|
|
if (y && (y->shape[0] != M || y->shape[1] != K)) return HALO_NO(RAD_E_SHAPE);
|
|
if (hi8 && K % grp) return HALO_NO(RAD_E_SHAPE);
|
|
/* The residual has the input's extent and is read and written. It may alias `out_bf16` (see
|
|
* the note at the top of the file). */
|
|
if (rs && (rs->rank != 2 || rs->shape[0] != M || rs->shape[1] != K)) return HALO_NO(RAD_E_SHAPE);
|
|
const long long xp = halo_row_pitch(x), qp = halo_row_pitch(q), sp = halo_row_pitch(sc);
|
|
const long long yp = y ? halo_row_pitch(y) : 0, rp = rs ? halo_row_pitch(rs) : 0;
|
|
if (xp < 0 || qp < 0 || sp < 0 || yp < 0 || rp < 0) return HALO_NO(RAD_E_STRIDE);
|
|
const int e = rmsnorm_quant_launch(halo_addr(x), rs ? halo_addr(rs) : 0, halo_addr(w),
|
|
y ? halo_addr(y) : 0, halo_addr(q), halo_addr(sc),
|
|
(int)M, (int)K,
|
|
(float)halo_getf(a, "eps", 1e-6), (float)halo_getf(a, "wadd", 0.0),
|
|
w->dtype == RAD_F32, (int)xp, (int)rp, (int)yp, (int)qp,
|
|
(int)sp, (long)(uintptr_t)s, hi8);
|
|
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
|
}
|
|
|
|
extern "C" int halo_rad_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) {
|
|
return rad_rmsnorm_quant(a, s, false);
|
|
}
|
|
|
|
extern "C" int halo_rad_rmsnorm_had_quant_i8g(const RadArgs* a, RadStream s) {
|
|
return rad_rmsnorm_quant(a, s, true);
|
|
}
|
|
|
|
/* The gate's activation: `act` by name overrides `mode` (libhalo's integer: 0 silu, 1 sigmoid) when
|
|
* both are given, as for halo_gated_had_quant_i8. Returns -1 for an unknown name. */
|
|
static int act_mode(const RadArgs* a, int dflt) {
|
|
const char* act = halo_gets(a, "act");
|
|
if (!act) return (int)halo_geti_or(a, "mode", dflt);
|
|
if (!std::strcmp(act, "silu")) return 0;
|
|
if (!std::strcmp(act, "sigmoid")) return 1;
|
|
return -1;
|
|
}
|
|
|
|
/* `hi8` (with `rot`): gated_had_quant_i8g, int8 codes only. */
|
|
static int rad_gated_quant_fp8(const RadArgs* a, RadStream stream, bool rot, bool hi8 = false) {
|
|
const RadTensor* gu = halo_opt(a, GQ_GU);
|
|
const RadTensor* q = halo_opt(a, GQ_Q);
|
|
const RadTensor* sc = halo_opt(a, GQ_S);
|
|
const RadTensor* y = halo_opt(a, GQ_Y);
|
|
if (!gu || !q || !sc) return HALO_NO(RAD_E_INVAL);
|
|
/* The codes' dtype picks the quantiser: E4M3, or int8 for an int8 down projection. The two
|
|
* rotated ops each take one: E4M3 for a four-bit expert, int8 for a rotated int8 weight. */
|
|
if (q->dtype != RAD_F8E4M3 && q->dtype != RAD_I8) return HALO_NO(RAD_E_DTYPE);
|
|
if (rot && q->dtype != (hi8 ? RAD_I8 : RAD_F8E4M3)) return HALO_NO(RAD_E_DTYPE);
|
|
if (gu->rank != 2 || q->rank != 2 || sc->rank != 2) return HALO_NO(RAD_E_SHAPE);
|
|
if (y && y->rank != 2) return HALO_NO(RAD_E_SHAPE);
|
|
const long long M = gu->shape[0], K = q->shape[1];
|
|
const long long grp = halo_quant_act_fp8_group();
|
|
const long long nkb = (K + grp - 1) / grp;
|
|
if (halo_geti_or(a, "group", grp) != grp) return HALO_NO(RAD_E_SHAPE);
|
|
/* The gated operand is [M][2K], gate then up, as the fused gate_up projection writes it. */
|
|
if (gu->shape[1] != 2 * K) return HALO_NO(RAD_E_SHAPE);
|
|
if (q->shape[0] != M) return HALO_NO(RAD_E_SHAPE);
|
|
if (sc->shape[0] != M || sc->shape[1] != nkb) return HALO_NO(RAD_E_SHAPE);
|
|
if (y && (y->shape[0] != M || y->shape[1] != K)) return HALO_NO(RAD_E_SHAPE);
|
|
const long long gp = halo_row_pitch(gu), qp = halo_row_pitch(q), sp = halo_row_pitch(sc);
|
|
const long long yp = y ? halo_row_pitch(y) : 0;
|
|
if (gp < 0 || qp < 0 || sp < 0 || yp < 0) return HALO_NO(RAD_E_STRIDE);
|
|
const int mode = act_mode(a, 0); /* silu: the gated MLP */
|
|
if (mode < 0) return HALO_NO(RAD_E_UNSUPPORTED);
|
|
const int e = gated_quant_fp8_launch(halo_addr(gu), y ? halo_addr(y) : 0, halo_addr(q),
|
|
halo_addr(sc), (int)M, (int)K, mode, (int)gp, (int)yp,
|
|
(int)qp, (int)sp, stream, rot, q->dtype == RAD_I8);
|
|
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
|
}
|
|
|
|
extern "C" int halo_rad_gated_quant_fp8(const RadArgs* a, RadStream stream) {
|
|
return rad_gated_quant_fp8(a, stream, false);
|
|
}
|
|
|
|
/* The rotated form is a separate op for the same reason as had_quant_act_fp8 (different codes in
|
|
* the same shape). It pairs only with a down-projection weight stored at dtype w4a8h. */
|
|
extern "C" int halo_rad_gated_had_quant_fp8(const RadArgs* a, RadStream stream) {
|
|
return rad_gated_quant_fp8(a, stream, true);
|
|
}
|
|
|
|
/* Its int8 twin, had_quant_act_i8g's codes: pairs only with a weight stored at dtype i8a8h. */
|
|
extern "C" int halo_rad_gated_had_quant_i8g(const RadArgs* a, RadStream stream) {
|
|
return rad_gated_quant_fp8(a, stream, true, true);
|
|
}
|
|
|
|
/* Elements between consecutive `width`-wide rows, as halo_gdn.cpp's gn_row_pitch: a
|
|
* [.., heads, width] operand steps by the stride of the second-to-last axis; a flattened one is
|
|
* contiguous and steps by `width`. This reads a column view of an interleaved projection (a gated
|
|
* attention's gate). */
|
|
static long long tq_pitch(const RadTensor* t, long long width) {
|
|
if (!t || t->rank < 1) return -1;
|
|
if (t->shape[t->rank - 1] != width) return halo_contig(t) ? width : -1;
|
|
if (t->stride[t->rank - 1] != 1) return -1;
|
|
if (t->rank == 1) return width;
|
|
/* Rows must also be uniformly spaced: a [T, n_head, head_dim] column view flattens to
|
|
* T * n_head rows only if T's stride is n_head of them, which fails if an outer axis was also
|
|
* sliced. Refuse otherwise; a wrong pitch would read the next head's gate. */
|
|
for (int i = (int)t->rank - 2; i >= 1; --i)
|
|
if (t->stride[i - 1] != t->shape[i] * t->stride[i]) return -1;
|
|
return t->stride[t->rank - 2];
|
|
}
|
|
|
|
/* Two-operand form of gated_quant_fp8 for gated attention: `x` is the attention output and the
|
|
* gate a column view of the [q|gate] projection. The gate offset comes from the view, so there is
|
|
* no `gate_offset` parameter. */
|
|
extern "C" int halo_rad_gate_quant_fp8(const RadArgs* a, RadStream stream) {
|
|
const RadTensor* g = halo_opt(a, TQ_G);
|
|
const RadTensor* x = halo_opt(a, TQ_X);
|
|
const RadTensor* q = halo_opt(a, TQ_Q);
|
|
const RadTensor* sc = halo_opt(a, TQ_S);
|
|
const RadTensor* y = halo_opt(a, TQ_Y);
|
|
if (!g || !x || !q || !sc) return HALO_NO(RAD_E_INVAL);
|
|
if (q->dtype != RAD_F8E4M3 && q->dtype != RAD_I8) return HALO_NO(RAD_E_DTYPE);
|
|
/* M is numel(x) / n. The `M` parameter is the band's declared maximum, not this issue's, and
|
|
* shape[0] counts tokens while a row here is a (token, head) pair. */
|
|
const long long K = halo_geti_or(a, "n", 0);
|
|
const long long grp = halo_quant_act_fp8_group();
|
|
if (K <= 0 || halo_numel(x) % K) return HALO_NO(RAD_E_SHAPE);
|
|
const long long M = halo_numel(x) / K;
|
|
if (M <= 0) return HALO_NO(RAD_E_SHAPE);
|
|
const long long nkb = (K + grp - 1) / grp;
|
|
if (halo_geti_or(a, "group", grp) != grp) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(g) < M * K) return HALO_NO(RAD_E_SHAPE);
|
|
if (halo_numel(q) < M * K || halo_numel(sc) < M * nkb) return HALO_NO(RAD_E_SHAPE);
|
|
if (y && halo_numel(y) < M * K) return HALO_NO(RAD_E_SHAPE);
|
|
const long long gp = tq_pitch(g, K), xp = tq_pitch(x, K);
|
|
const long long qp = tq_pitch(q, K), sp = tq_pitch(sc, nkb);
|
|
const long long yp = y ? tq_pitch(y, K) : 0;
|
|
if (gp < 0 || xp < 0 || qp < 0 || sp < 0 || yp < 0) return HALO_NO(RAD_E_STRIDE);
|
|
const int mode = act_mode(a, 1); /* sigmoid: gated attention */
|
|
if (mode < 0) return HALO_NO(RAD_E_UNSUPPORTED);
|
|
const int e = gate_quant_fp8_emit(halo_addr(g), halo_addr(x), y ? halo_addr(y) : 0,
|
|
halo_addr(q), halo_addr(sc), (int)M, (int)K, mode,
|
|
(int)gp, (int)xp, (int)yp, (int)qp, (int)sp, stream,
|
|
q->dtype == RAD_I8);
|
|
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
|
}
|
|
|