This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libhalo/halo_quant_act_fp8.hip
Deadcode 9ed3ef7a7b radiance 1.15.0
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.
2026-10-30 22:01:01 +01:00

373 lines
19 KiB
Plaintext

/* Derived from libr4d/r4d_quant_act_fp8.hip by the libhalo rename transform (2026-10-26),
* then ported to gfx1151; see libhalo/README.md. */
// Per-(row, 128-column) symmetric E4M3 quantisation of a bf16 activation, the input to the fp8a8
// GEMMs. The checkpoint's activation_scheme is "dynamic" (no stored activation scale), and a GEMM
// cannot compute one without a reduction across every M block.
//
// Output is plain row-major, unlike halo_quant_act_i8 (which writes gemm_w4a8_nt_m64's register
// fragment order): the fp8a8 GEMMs stage both operands through the same `stage8` copy and fragment
// read, and row-major lets the activation be the B operand of the narrow instantiation.
//
// The group is fixed at 128, the checkpoint's weight-scale block width (unlike
// HALO_GEMM_W4A8_GROUP); another group would make the GEMM's fold read the wrong scale. No LDS: the
// input is already row-major per token.
//
// Not obvious:
// - The scale is `amax / 448` (E4M3's largest finite value), a dequant multiplier like the
// checkpoint's `weight_scale_inv`, so the GEMM's fold only multiplies.
// - A zero block gets scale 1 and quantises to zeros.
// - The clamp is required: `amax * (1/scale)` can land an ulp above 448, and v_cvt_pk_fp8_f32
// returns NaN for out-of-range input instead of saturating, so every block's maximum would
// become NaN.
//
// Returns a negative code rather than throwing, for the reason given in halo_gemm_fp8a8.hip.
#include "halo.h"
#include "halo_emit.h"
#include "halo_fp8_args.h"
#include "halo_fp8_wmma.h"
#include "halo_fwht.h"
#include "halo_chain.h"
namespace {
constexpr uint32_t kWave = 32;
constexpr uint32_t kThreads = 256;
constexpr uint32_t kWaves = kThreads / kWave;
constexpr uint32_t kGroup = 128;
// One block per (row, group chunk); a wave owns one 128-column group, a lane four consecutive
// columns (one dword store, 128 contiguous bytes per wave, two software fp8 packs). Groups are
// independent -- scale and codes both come from the group itself -- so the launcher splits a row
// over several blocks when M alone would leave the card idle: at decode heights one block per row
// is one workgroup walking every group serially (21 us at M=1, K=25600 before the split).
//
// ROT applies halo_fwht_wave<128> in registers between the load and the absmax; the mapping (four
// values per lane) is the one it expects, so no LDS or barrier is needed, and the absmax sees the
// rotated values. The sqrt(128) factor of the unnormalised transform is not removed here:
// <Hw, Ha> = 128 <w, a>, and halo_layout.cpp folds that into the stored weight scale at convert.
//
// I8 is the same quantiser at int8 codes (quant_act_i8g, had_quant_act_i8g): scale amax/127,
// round-to-nearest-even into -127..127, same row-major layout and per-(row, 128) scales.
//
// CHAIN brackets the body with the chained launch's wait and add (halo_chain.h); no block returns
// early. quant_act_fp8_kernel and halo_r4dchain_quant_act_fp8 below are the plain and chained forms.
template <bool ROT, bool I8 = false, bool CHAIN = false>
__device__ __forceinline__ void quant_act_fp8_body(
const uint16_t* __restrict__ x, uint8_t* __restrict__ q, float* __restrict__ s,
uint32_t K, uint32_t nkb, uint32_t x_ld, uint32_t q_ld, uint32_t s_ld) {
if (CHAIN) halo_chain_wait();
const uint32_t row = blockIdx.x;
const uint32_t lane = threadIdx.x & (kWave - 1), wave = threadIdx.x / kWave;
const size_t xrow = (size_t)row * x_ld;
const size_t qrow = (size_t)row * q_ld;
for (uint32_t kb = blockIdx.y * kWaves + wave; kb < nkb; kb += gridDim.y * kWaves) {
const uint32_t c0 = kb * kGroup + lane * 4u;
float v[4];
float m = 0.0f;
#pragma unroll
for (int j = 0; j < 4; ++j) {
const uint32_t c = c0 + (uint32_t)j;
v[j] = c < K ? __uint_as_float((uint32_t)x[xrow + c] << 16) : 0.0f;
}
// The rotated launcher requires K % 128 == 0: a partial block would rotate real values
// with padding zeros, and the weight has no matching block.
if constexpr (ROT) halo_fwht_wave<(int)kGroup>(v, (int)lane);
#pragma unroll
for (int j = 0; j < 4; ++j) m = fmaxf(m, fabsf(v[j]));
m = halo_wave_max(m);
constexpr float kTop = I8 ? 127.0f : 448.0f;
const float sc = m > 0.0f ? m * (1.0f / kTop) : 1.0f;
if (lane == 0) s[(size_t)row * s_ld + kb] = sc;
const float inv = 1.0f / sc;
int pk = 0;
if constexpr (I8) {
uint32_t b = 0;
#pragma unroll
for (int j = 0; j < 4; ++j) {
const int c = (int)rintf(fminf(fmaxf(v[j] * inv, -127.0f), 127.0f));
b |= ((uint32_t)c & 0xFFu) << (8 * j);
}
pk = (int)b;
} else {
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);
}
// A ragged last group is written zero-padded; the GEMM's bounds guard keeps the padding out
// of the accumulators. The caller must own K rounded up to 4 bytes of `q`.
if (c0 < K) *(uint32_t*)(q + qrow + c0) = (uint32_t)pk;
}
if (CHAIN) halo_chain_done();
}
#define HALO_QAF_PARAMS const uint16_t* __restrict__ x, uint8_t* __restrict__ q, \
float* __restrict__ s, uint32_t K, uint32_t nkb, uint32_t x_ld, uint32_t q_ld, uint32_t s_ld
template <bool ROT, bool I8 = false>
__global__ __launch_bounds__(kThreads) void quant_act_fp8_kernel(HALO_QAF_PARAMS) {
quant_act_fp8_body<ROT, I8, false>(x, q, s, K, nkb, x_ld, q_ld, s_ld);
}
template <bool ROT, bool I8 = false>
__global__ __launch_bounds__(kThreads) void halo_r4dchain_quant_act_fp8(HALO_QAF_PARAMS) {
quant_act_fp8_body<ROT, I8, true>(x, q, s, K, nkb, x_ld, q_ld, s_ld);
}
#undef HALO_QAF_PARAMS
} // namespace
extern "C" int halo_quant_act_fp8_group(void) { return (int)kGroup; }
// a [M][K] bf16, `x_ld` elements a row (0 = K)
// q [M][K] E4M3, `q_ld` bytes a row (0 = K), rounded up to 4
// s [M][ceil(K/128)] f32, `s_ld` elements a row (0 = ceil(K/128))
//
// `rot` runs the 128-wide Hadamard first and additionally requires K % 128 == 0 (see the kernel).
static int quant_act_fp8_launch(long a, long q, long s, int M, int K,
int x_ld, int q_ld, int s_ld, RadStream stream, bool rot,
bool i8 = false, bool chain = false) {
int emit_rc = RAD_OK;
if (!a || !q || !s) return -1;
if (M <= 0 || K <= 0) return -2;
if (K % 4) return -3; // a lane owns four columns; see the kernel
if (rot && (K % (int)kGroup)) return -5;
if (!x_ld) x_ld = K;
if (!q_ld) q_ld = K;
const int nkb = (K + (int)kGroup - 1) / (int)kGroup;
if (!s_ld) s_ld = nkb;
/* The chained form at decode heights, where the row resolved with `chain`. */
const bool ch = chain && M <= 64;
/* Split each row into enough group chunks to put ~two resident rounds of workgroups on the
* card (40 CUs hold 160 of these 8-wave blocks); at large M the rows already do that and the
* split stays 1, the libr4d grid. */
const int max_chunks = (nkb + (int)kWaves - 1) / (int)kWaves;
int chunks = (320 + M - 1) / M;
if (chunks > max_chunks) chunks = max_chunks;
if (chunks < 1) chunks = 1;
const dim3 grid((uint32_t)M, (uint32_t)chunks);
#define HALO_QA(ROT_, I8_) \
(ch ? HALO_EMIT(stream, (halo_r4dchain_quant_act_fp8<ROT_, I8_>), grid, \
dim3(kThreads), 0, (const uint16_t*)a, (uint8_t*)q, (float*)s, (uint32_t)K, \
(uint32_t)nkb, (uint32_t)x_ld, (uint32_t)q_ld, (uint32_t)s_ld) \
: HALO_EMIT(stream, (quant_act_fp8_kernel<ROT_, I8_>), grid, \
dim3(kThreads), 0, (const uint16_t*)a, (uint8_t*)q, (float*)s, (uint32_t)K, \
(uint32_t)nkb, (uint32_t)x_ld, (uint32_t)q_ld, (uint32_t)s_ld))
if (i8) emit_rc = rot ? HALO_QA(true, true) : HALO_QA(false, true);
else emit_rc = rot ? HALO_QA(true, false) : HALO_QA(false, false);
#undef HALO_QA
if (emit_rc != RAD_OK) return -4;
return (hipGetLastError() != hipSuccess) ? -4 : 0;
}
extern "C" int halo_quant_act_fp8(long a, long q, long s, int M, int K,
int x_ld, int q_ld, int s_ld, long stream) {
return quant_act_fp8_launch(a, q, s, M, K, x_ld, q_ld, s_ld,
(RadStream)(uintptr_t)stream, false);
}
extern "C" int halo_had_quant_act_fp8(long a, long q, long s, int M, int K,
int x_ld, int q_ld, int s_ld, long stream) {
return quant_act_fp8_launch(a, q, s, M, K, x_ld, q_ld, s_ld,
(RadStream)(uintptr_t)stream, true);
}
// ============================================================== the plugin ABI entry points
//
// The shared operand reader is halo_fp8_args.h; see libhalo/halo_args.h.
/* ================================================================== the fp8 activation quantiser */
static int rad_quant_act_fp8(const RadArgs* a, RadStream stream, bool rot, bool i8 = false) {
const RadTensor* x = halo_opt(a, QF_X);
const RadTensor* q = halo_opt(a, QF_Q);
const RadTensor* sc = halo_opt(a, QF_S);
if (!x || !q || !sc) return HALO_NO(RAD_E_INVAL);
/* Rows are dim 0 and everything after is the row, as halo_row_pitch reads it. A GDN block's
* out-projection input is [tokens, heads, width] (the scan kernels require that rank) and its
* K is heads*width; one row per head would put scales on a grid the GEMM does not read.
* `q` and `sc` are written for the GEMM and stay rank 2. */
if (x->rank < 2 || q->rank != 2 || sc->rank != 2) return HALO_NO(RAD_E_SHAPE);
const long long M = x->shape[0], K = halo_numel(x) / (x->shape[0] > 0 ? x->shape[0] : 1);
const long long grp = halo_quant_act_fp8_group();
const long long nkb = (K + grp - 1) / grp;
/* The group is fixed by the checkpoint's scale grid; a different declared group is refused. */
if (halo_geti_or(a, "group", grp) != grp) return HALO_NO(RAD_E_SHAPE);
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);
const long long xp = halo_row_pitch(x), qp = halo_row_pitch(q), sp = halo_row_pitch(sc);
if (xp < 0 || qp < 0 || sp < 0) return HALO_NO(RAD_E_STRIDE);
const int e = quant_act_fp8_launch(halo_addr(x), halo_addr(q), halo_addr(sc), (int)M, (int)K,
(int)xp, (int)qp, (int)sp, stream, rot, i8,
halo_geti_or(a, "chain", 0) != 0);
return e == 0 ? RAD_OK : RAD_E_SHAPE;
}
extern "C" int halo_rad_quant_act_fp8(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, false);
}
/* The rotated form is a separate op, not a parameter: it writes different numbers, so a swapped
* graph would still run as a different model. The op name pairs with the weight's layout tag in
* arch/common/rad_block_moe_fp8.h. It pairs only with the `w4a8h` expert plane; any consumer not
* rotated at convert reads it as noise. */
extern "C" int halo_rad_had_quant_act_fp8(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, true);
}
/* The int8 pair, for the int8 trunk GEMMs (gemm_i8a8_*). Separate ops for the same reason as the
* rotated form: different numbers in a buffer of the same shape. */
extern "C" int halo_rad_quant_act_i8g(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, false, true);
}
extern "C" int halo_rad_had_quant_act_i8g(const RadArgs* a, RadStream stream) {
return rad_quant_act_fp8(a, stream, true, true);
}
// ============================================================== quant_cols_fp8
// E4M3 codes with one scale per column over every row: an attention's V for its fp8 P.V. The
// formula is quant_act_fp8's (scale amax / 448, 1 for an all-zero column, codes of x / scale
// clamped to +-448) with the group running down a column. A column's scale needs every row, so
// two passes over the rows, split into kColChunks chunks:
// 1. column amax partials, a workgroup a block of columns and a chunk of rows, the bf16
// magnitudes compared as integers (their order is the values');
// 2. a workgroup the same block and chunk: the scales from the chunks' partials, then the codes,
// the chunk's rows walked from its end, which the first pass read last and the last-level
// cache still holds.
// A thread owns eight columns: one 16-byte load and one 8-byte store a row.
namespace {
constexpr int kColChunks = 32;
constexpr int kColThreads = 128;
typedef unsigned short halo_u16x2 __attribute__((ext_vector_type(2)));
__device__ __forceinline__ halo_u16x2 col_mag(uint32_t w) {
return __builtin_bit_cast(halo_u16x2, w & 0x7fff7fffu);
}
__global__ __launch_bounds__(kColThreads) void quant_cols_amax_kernel(
const uint16_t* __restrict__ x, int M, int n, int x_ld, float* __restrict__ part) {
const int c8 = blockIdx.x * kColThreads + threadIdx.x;
if (c8 * 8 >= n) return;
const int r0 = (int)((long)M * blockIdx.y / kColChunks);
const int r1 = (int)((long)M * (blockIdx.y + 1) / kColChunks);
const uint16_t* p = x + (long)c8 * 8;
halo_u16x2 m[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
int r = r0;
for (; r + 8 <= r1; r += 8) {
uint4 u[8];
#pragma unroll
for (int k = 0; k < 8; ++k) u[k] = *(const uint4*)(p + (long)(r + k) * x_ld);
#pragma unroll
for (int k = 0; k < 8; ++k) {
m[0] = __builtin_elementwise_max(m[0], col_mag(u[k].x));
m[1] = __builtin_elementwise_max(m[1], col_mag(u[k].y));
m[2] = __builtin_elementwise_max(m[2], col_mag(u[k].z));
m[3] = __builtin_elementwise_max(m[3], col_mag(u[k].w));
}
}
for (; r < r1; ++r) {
const uint4 u = *(const uint4*)(p + (long)r * x_ld);
m[0] = __builtin_elementwise_max(m[0], col_mag(u.x));
m[1] = __builtin_elementwise_max(m[1], col_mag(u.y));
m[2] = __builtin_elementwise_max(m[2], col_mag(u.z));
m[3] = __builtin_elementwise_max(m[3], col_mag(u.w));
}
float* d = part + (long)blockIdx.y * n + c8 * 8;
*(float4*)d = make_float4(bf16_to_f32(m[0].x), bf16_to_f32(m[0].y), bf16_to_f32(m[1].x),
bf16_to_f32(m[1].y));
*(float4*)(d + 4) = make_float4(bf16_to_f32(m[2].x), bf16_to_f32(m[2].y),
bf16_to_f32(m[3].x), bf16_to_f32(m[3].y));
}
template <bool HI>
__device__ __forceinline__ int col_code2(float a, float b, float ia, float ib, int old) {
return halo_fp8::cvt_pk_fp8_f32(fminf(fmaxf(a * ia, -448.0f), 448.0f),
fminf(fmaxf(b * ib, -448.0f), 448.0f), old, HI);
}
__global__ __launch_bounds__(kColThreads) void quant_cols_code_kernel(
const uint16_t* __restrict__ x, int M, int n, int x_ld, const float* __restrict__ part,
uint8_t* __restrict__ y, int y_ld, float* __restrict__ scale) {
const int c8 = blockIdx.x * kColThreads + threadIdx.x;
if (c8 * 8 >= n) return;
float am[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
for (int ch = 0; ch < kColChunks; ++ch) {
const float4 a = *(const float4*)(part + (long)ch * n + c8 * 8);
const float4 b = *(const float4*)(part + (long)ch * n + c8 * 8 + 4);
am[0] = fmaxf(am[0], a.x); am[1] = fmaxf(am[1], a.y);
am[2] = fmaxf(am[2], a.z); am[3] = fmaxf(am[3], a.w);
am[4] = fmaxf(am[4], b.x); am[5] = fmaxf(am[5], b.y);
am[6] = fmaxf(am[6], b.z); am[7] = fmaxf(am[7], b.w);
}
float sc[8], inv[8];
#pragma unroll
for (int e = 0; e < 8; ++e) {
sc[e] = am[e] > 0.0f ? am[e] * (1.0f / 448.0f) : 1.0f;
inv[e] = 1.0f / sc[e];
}
if (blockIdx.y == 0) {
*(float4*)(scale + c8 * 8) = make_float4(sc[0], sc[1], sc[2], sc[3]);
*(float4*)(scale + c8 * 8 + 4) = make_float4(sc[4], sc[5], sc[6], sc[7]);
}
const int r0 = (int)((long)M * blockIdx.y / kColChunks);
const int r1 = (int)((long)M * (blockIdx.y + 1) / kColChunks);
const uint16_t* p = x + (long)c8 * 8;
uint8_t* q = y + (long)c8 * 8;
auto code = [&](uint4 u) {
int lo = col_code2<false>(bf16w_lo(u.x), bf16w_hi(u.x), inv[0], inv[1], 0);
lo = col_code2<true>(bf16w_lo(u.y), bf16w_hi(u.y), inv[2], inv[3], lo);
int hi = col_code2<false>(bf16w_lo(u.z), bf16w_hi(u.z), inv[4], inv[5], 0);
hi = col_code2<true>(bf16w_lo(u.w), bf16w_hi(u.w), inv[6], inv[7], hi);
return make_uint2((uint32_t)lo, (uint32_t)hi);
};
int r = r1;
for (; r - 8 >= r0; r -= 8) {
uint4 u[8];
#pragma unroll
for (int k = 0; k < 8; ++k) u[k] = *(const uint4*)(p + (long)(r - 1 - k) * x_ld);
#pragma unroll
for (int k = 0; k < 8; ++k) *(uint2*)(q + (long)(r - 1 - k) * y_ld) = code(u[k]);
}
for (; r > r0; --r)
*(uint2*)(q + (long)(r - 1) * y_ld) = code(*(const uint4*)(p + (long)(r - 1) * x_ld));
}
} // namespace
/* The partial maxima of every chunk, kColChunks rows of n floats. */
extern "C" int64_t halo_rad_quant_cols_fp8_scratch(const RadArgs* a) {
const long long n = halo_geti_or(a, "n", 0);
if (n <= 0) return HALO_NO(RAD_E_SHAPE);
return (int64_t)kColChunks * n * (int64_t)sizeof(float);
}
extern "C" int halo_rad_quant_cols_fp8(const RadArgs* a, RadStream stream) {
const RadTensor *x = halo_opt(a, 0), *y = halo_opt(a, 1), *sc = halo_opt(a, 2);
if (!x || !y || !sc) return HALO_NO(RAD_E_INVAL);
if (x->dtype != RAD_BF16 || y->dtype != RAD_F8E4M3 || sc->dtype != RAD_F32)
return HALO_NO(RAD_E_DTYPE);
const long long n = halo_geti_or(a, "n", 0);
if (n <= 0 || n % 8) return HALO_NO(RAD_E_SHAPE);
long long M = 0, my = 0;
const long long xl = halo_rows_pitch(x, n, &M), yl = halo_rows_pitch(y, n, &my);
if (xl < 0 || yl < 0 || (xl & 7) || (yl & 7) || ((uintptr_t)x->data & 15) ||
((uintptr_t)y->data & 7) || !halo_contig(sc) || ((uintptr_t)sc->data & 15))
return HALO_NO(RAD_E_STRIDE);
if (my < M || halo_numel(sc) < n || M > 0x7fffffffLL || n > 0x7fffffffLL)
return HALO_NO(RAD_E_SHAPE);
if (!a->scratch || ((uintptr_t)a->scratch & 15) ||
a->scratch_bytes < (int64_t)kColChunks * n * (int64_t)sizeof(float))
return HALO_NO(RAD_E_SCRATCH);
const dim3 grid((unsigned)((n / 8 + kColThreads - 1) / kColThreads), (unsigned)kColChunks);
hipLaunchKernelGGL(quant_cols_amax_kernel, grid, dim3(kColThreads), 0, (hipStream_t)stream,
(const uint16_t*)x->data, (int)M, (int)n, (int)xl, (float*)a->scratch);
hipLaunchKernelGGL(quant_cols_code_kernel, grid, dim3(kColThreads), 0, (hipStream_t)stream,
(const uint16_t*)x->data, (int)M, (int)n, (int)xl,
(const float*)a->scratch, (uint8_t*)y->data, (int)yl, (float*)sc->data);
return hipGetLastError() == hipSuccess ? RAD_OK : HALO_NO(RAD_E_DEVICE);
}