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/libr3d/r3d_fp8_dequant.h
T
Asim Siddiqui 6cb9826975 libr3d: the gfx11 (RDNA3) kernel library
A complete row set for the gfx11 family (1100/1101/1102/1103/1150/1151 plus gfx11-generic),
mirroring libr4d's structure: the quantised GEMM rows (m16, tiled, gated, the bf16 logits
gemm), the paged and ragged attention for decode and prefill, the KV store, the sampler
spine and the spec rows, with the recorded-fixture, board, selftest and comparison-driver
gates for each.

The gates are the record. Every row is measured against libref on the shape matrix: the
recorded fixture at 1153 cases with worst rel_l2 1.362e-03, the board's five modes with
bit-identical decision lines, the selftest at 74 rows, and the 233-case served-shape pairing
at 0 divergent against the tree libref. End-to-end on a 2B fp8 checkpoint served on the
card, coherent through prefix-cache adoption.

Two platform laws each shape the design. A kernel that calls device printf takes the
implicit hidden_hostcall_buffer argument and is undispatchable by the AQL backend, so the
trace records are plain stores behind a budget. And a plugin-owned __device__ global is not
a channel into the engine's dispatch path: in a dlopened plugin the runtime's symbol stub
is not the address the kernel's module writes, so the served trace channel is a hipMalloc'd
record slot passed as a kernel argument, with the device-global ring kept for the board.

kv_store bounds its slots at the pool's own plane (n_blocks x block_size), the span the
cache operand declares, not at the step's own span: a request that adopts a cached prefix
writes its new tokens at the allocator's global block indices, which sit past any bound
built from the step's token count, and a slot lost there is KV that attention then reads
stale. A corrupt slot map is dropped at the same bound.
2026-10-28 13:51:45 +01:00

80 lines
4.2 KiB
C

/* r3d_fp8_dequant.h -- the E4M3 -> f16 decoder as a table, for the rows that read fp8 codes.
*
* WHY THIS EXISTS, WITH THE MEASUREMENT THAT ASKED FOR IT
*
* gfx11 has no instruction that converts E4M3 to f16. clang lists `__builtin_amdgcn_cvt_pk_f16_fp8`
* for the target, and a build that calls it on gfx1102 contains ZERO `v_cvt_pk_f16_fp8` in its ISA:
* the builtin is software here, lowered to the same exponent work `e4m3_to_f32` does. r3d_codecs.h's
* codec is two `e4m3_to_f32` (a dozen instructions of shifts, exponent bias and subnormal handling
* each) plus a pack, about 25 instructions a pair, 100 an eight-code group. Measured on the
* vocabulary head at M=1 (r3d_vocab_bf16.hip, RX 7600 / gfx1102 / 32 CU): 13.603 ms a token at
* 19.6 GB/s of codes, 8x above the 1.64 ms the codes' 255 MiB floor at this row's own 163 GB/s, and
* the ALU was the binding resource, not DRAM.
*
* E4M3 has 256 bit patterns and every one of them has an f16 value (f16's exponent range covers
* E4M3's, its subnormals included, and E4M3's NaN maps to an f16 NaN). So the decoder is a
* 256-entry table of 512 bytes, and eight LDS reads replace the hundred ALU instructions. That is
* the whole idea, and the table goes in LDS -- not __constant__, whose broadcast is a liability here
* because every lane decodes a DIFFERENT code, and divergent constant indexes serialise.
*
* Cost: 512 bytes of LDS a block, one fill of 256 stores at block start plus a barrier. Conflict
* behaviour on the reads: 16-bit loads at data-dependent addresses, so a wavefront's 32 lanes land
* on 16 bank words; random codes give roughly two lanes per word, and measured cost is about two
* cycles a read -- eight reads an eight-code group against the codec's hundred instructions.
*
* The table is EXACT: it is built by calling the codec once per entry, so it is the codec, moved
* from the loop to the block's start. Nothing about the arithmetic changes, and a row's rel_l2
* against its reference is unchanged by this header. The one thing to keep: the table must be filled
* before any lane reads it, so every use is `..._init` then the barrier it contains, then reads.
*
* int8 codes do NOT want a table: `v_cvt_f32_i8` is a hardware instruction, so
* r3d_codecs.h's i8x2_to_f16x2 is two converts and a pack. This header serves the fp8 codes.
*
* Header-only, like the rest of libr3d. Include it in a .hip after r3d_codecs.h.
*/
#ifndef R3D_FP8_DEQUANT_H
#define R3D_FP8_DEQUANT_H
#include "r3d_codecs.h"
#define R3D_E4M3_TBL 256 /* every E4M3 bit pattern, in order, as the f16 it means */
#define R3D_E4M3_TBL_BYTES (R3D_E4M3_TBL * 2)
/* Fill the block's table: `tbl` is __shared__ uint16_t[R3D_E4M3_TBL]. The barrier is inside it,
* because the only correct order is fill-then-read and the row should not have to remember it. */
__device__ __forceinline__ void r3d_e4m3_f16_init(uint16_t* tbl, int tid, int nthr)
{
for (int i = tid; i < R3D_E4M3_TBL; i += nthr)
tbl[i] = f32_to_f16(e4m3_to_f32((uint8_t)i));
__syncthreads();
}
/* Two codes (the low 16 bits of a dword) as one f16 pair, the pair layout dot2_f16x2 reads: the
* first code in the low half. Same answer as e4m3x2_to_f16x2, eight table reads per eight codes. */
__device__ __forceinline__ uint32_t r3d_e4m3x2_ld(const uint16_t* tbl, uint32_t pair)
{
const uint16_t lo = tbl[pair & 0xffu];
const uint16_t hi = tbl[(pair >> 8) & 0xffu];
return (uint32_t)lo | ((uint32_t)hi << 16);
}
/* Eight codes (two dwords, straddling pairs as always) as four f16 pairs. The sibling of
* codes8_to_f16x4 in r3d_vocab_bf16.hip; the int8 side of that helper stays with the codec, because
* int8 has a hardware convert and fp8 does not. */
__device__ __forceinline__ void r3d_e4m3x8_ld(const uint16_t* tbl, uint2 c, uint32_t* w)
{
const uint32_t d0 = c.x, d1 = c.y;
w[0] = r3d_e4m3x2_ld(tbl, d0 & 0xffffu);
w[1] = r3d_e4m3x2_ld(tbl, d0 >> 16);
w[2] = r3d_e4m3x2_ld(tbl, d1 & 0xffffu);
w[3] = r3d_e4m3x2_ld(tbl, d1 >> 16);
}
/* One code, for the rows that decode a scalar a step (an activation tile staged a byte a lane). */
__device__ __forceinline__ uint16_t r3d_e4m3_ld(const uint16_t* tbl, uint8_t code)
{
return tbl[code];
}
#endif /* R3D_FP8_DEQUANT_H */