Archived
radiance 1.2.0
- MXFP4 weights: block-fp8 Qwen3.5/3.6/3.8 dense models serve OCP-MXFP4 linears (e2m1, an E8M0 exponent per 32) at 4-bit weights against E4M3 activations. New libr4d rows: the decode GEMM (M <= 128, split-K with a fused last-arriver reduction) and the prefill GEMM on a fragment-tiled activation. Qwen3.8-27B MXFP4 (AMD Quark AWQ) is in the model table. - Cards without peer-to-peer access serve: every collective has a host-memory twin; --p2p auto|on|off. - Stop strings apply to the answer, not to the reasoning block a reply starts in. - A prefix hit ends one drafter window before the prompt's end, so a windowed block drafter (DFlash2) drafts from the first token of a follow-up turn.
This commit is contained in:
1 parent
7001841e45
commit
e95d397288
47 files changed
+2611
-219
No files matched your search
@@ -68,6 +68,14 @@
|
||||
// The E8M0 scales are left in [K/32][N] order -- a half-wave reads sixteen consecutive bytes of
|
||||
// those already.
|
||||
//
|
||||
// TWO ACTIVATION SCALE GRIDS, as in r4d_gemm_mxfp4a8_tiled.hip: one f32 per row (ABLK=false), or
|
||||
// one per (row, 128 of K) (ABLK=true, the block-scaled fp8 family's grid). With ABLK each wave
|
||||
// carries its partial sums in units of the current 128-block's scale and rescales them by
|
||||
// s_prev / s_next at each block boundary inside its split-K segment. The segments end in
|
||||
// DIFFERENT blocks, so each partial is brought back to absolute units -- multiplied by its own
|
||||
// last scale -- before the LDS reduction adds them, and the epilogue applies only the weight's
|
||||
// reference factor.
|
||||
//
|
||||
// TRAP FOR ANYONE MEASURING THIS KERNEL: a weight of tens of megabytes streamed in a loop is
|
||||
// Infinity-Cache resident on this part, so a bandwidth figure taken that way can read above the
|
||||
// DRAM roofline. It ranks kernels reading the same bytes; it is not achieved DRAM bandwidth.
|
||||
@@ -78,56 +86,36 @@
|
||||
// the last valid row and the store discards it -- which keeps the inner loop branch-free.
|
||||
#include "r4d_gemm_args.h"
|
||||
#include "r4d_common.h"
|
||||
#include "r4d_mxfp4.h"
|
||||
|
||||
#include <hip/hip_bf16.h>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
#define R4D_GEMM_MXFP4_MAX_M 64
|
||||
#define R4D_GEMM_MXFP4_GROUP 32 // K per E8M0 exponent, fixed by the OCP MX format
|
||||
#define R4D_GEMM_MXFP4_GROUP R4D_MXFP4_GROUP
|
||||
#define WAVE32 32
|
||||
|
||||
typedef int v2i __attribute__((ext_vector_type(2)));
|
||||
typedef unsigned uint2v __attribute__((ext_vector_type(2)));
|
||||
|
||||
// Folded magnitudes: kMag[d] is the eight e2m1 magnitudes scaled by 2^-d, as packed e4m3 bytes,
|
||||
// low table then high table, indexed by the 3-bit magnitude through v_perm_b32.
|
||||
// Every entry is the round-to-nearest-EVEN e4m3 of that product, which is exact for d <= 8 and
|
||||
// a rounding for d >= 9 -- three entries there (d=9 mag 1.5, d=10 mag 3, d=11 mag 6) are the
|
||||
// same exact tie 1.5*2^-9 and take the even neighbour 2^-8, not the smaller one. They matter
|
||||
// little (such an element is 2.9e-3 of its row's reference) but the table is generated, not
|
||||
// hand-rounded, so it should agree with the hardware's own conversion.
|
||||
__device__ __constant__ unsigned int kMag[16][2] = {
|
||||
{0x3c383000u, 0x4c484440u}, {0x34302800u, 0x44403c38u},
|
||||
{0x2c282000u, 0x3c383430u}, {0x24201800u, 0x34302c28u},
|
||||
{0x1c181000u, 0x2c282420u}, {0x14100800u, 0x24201c18u},
|
||||
{0x0c080400u, 0x1c181410u}, {0x06040200u, 0x14100c08u},
|
||||
{0x03020100u, 0x0c080604u}, {0x02010000u, 0x06040302u},
|
||||
{0x01000000u, 0x03020201u}, {0x00000000u, 0x02010100u},
|
||||
{0x00000000u, 0x01000000u}, {0x00000000u, 0x00000000u},
|
||||
{0x00000000u, 0x00000000u}, {0x00000000u, 0x00000000u},
|
||||
};
|
||||
|
||||
// Eight e2m1 codes (four packed bytes) -> eight e4m3 bytes in k order, with the block's exponent
|
||||
// difference already folded in.
|
||||
__device__ __forceinline__ v2i r4d_mxfp4_unpack8(unsigned int wv, int dsh) {
|
||||
const unsigned int t0 = kMag[dsh][0], t1 = kMag[dsh][1];
|
||||
const unsigned int ev = wv & 0x0F0F0F0Fu; // low nibble of each byte -> even elements
|
||||
const unsigned int od = (wv >> 4) & 0x0F0F0F0Fu; // high nibble -> odd elements
|
||||
const unsigned int be = __builtin_amdgcn_perm(t1, t0, ev & 0x07070707u) | ((ev & 0x08080808u) << 4);
|
||||
const unsigned int bo = __builtin_amdgcn_perm(t1, t0, od & 0x07070707u) | ((od & 0x08080808u) << 4);
|
||||
// Eight e2m1 codes (four packed bytes) -> the B fragment, eight e4m3 bytes in k order with the
|
||||
// block's exponent difference folded in (r4d_mxfp4.h, shared with the other two MXFP4 kernels).
|
||||
__device__ __forceinline__ v2i r4d_mxfp4_frag8(unsigned int wv, int d) {
|
||||
unsigned int lo, hi;
|
||||
r4d_mxfp4_unpack8(wv, d, &lo, &hi);
|
||||
v2i out;
|
||||
out[0] = (int)__builtin_amdgcn_perm(bo, be, 0x05010400u); // elements 0..3
|
||||
out[1] = (int)__builtin_amdgcn_perm(bo, be, 0x07030602u); // elements 4..7
|
||||
out[0] = (int)lo;
|
||||
out[1] = (int)hi;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <int MT, int NPW>
|
||||
template <int MT, int NPW, bool ABLK>
|
||||
__global__ void r4d_gemm_mxfp4a8_nt_m64_kernel(
|
||||
const unsigned char* __restrict__ A, const float* __restrict__ As,
|
||||
const unsigned char* __restrict__ W, const unsigned char* __restrict__ Ws,
|
||||
const unsigned char* __restrict__ Wref, __hip_bfloat16* __restrict__ C,
|
||||
int M, int K, int N, int WV, int SK) {
|
||||
int M, int K, int N, int WV, int SK, int as_rs) {
|
||||
extern __shared__ float red[];
|
||||
const int tid = threadIdx.x, wave = tid / WAVE32, lane = tid % WAVE32;
|
||||
const int col_local = wave / SK, ksplit = wave % SK;
|
||||
@@ -161,8 +149,39 @@ __global__ void r4d_gemm_mxfp4a8_nt_m64_kernel(
|
||||
const unsigned int* __restrict__ Wp = (const unsigned int*)W;
|
||||
const int ksteps = K / 16;
|
||||
|
||||
// ABLK: the scale each accumulator row of this lane is carried in -- row (mt0+i)*16 + mhalf + e.
|
||||
float scur[ABLK ? MT : 1][8];
|
||||
if constexpr (ABLK) {
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; e++) {
|
||||
int r = (mt0 + i) * 16 + mhalf + e;
|
||||
r = r < M - 1 ? r : M - 1;
|
||||
const float s = As[(size_t)r * as_rs + (kbase >> 7)];
|
||||
scur[i][e] = s > 0.f ? s : 1.f;
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 0; k < Ks; k += 16) {
|
||||
const int kg = kbase + k; // fragment base, both lane halves
|
||||
if constexpr (ABLK) {
|
||||
if (k && (kg & 127) == 0) { // a new 128-wide activation block
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; e++) {
|
||||
int r = (mt0 + i) * 16 + mhalf + e;
|
||||
r = r < M - 1 ? r : M - 1;
|
||||
float s = As[(size_t)r * as_rs + (kg >> 7)];
|
||||
s = s > 0.f ? s : scur[i][e];
|
||||
const float q = scur[i][e] / s;
|
||||
scur[i][e] = s;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++) acc[j][i][e] *= q;
|
||||
}
|
||||
}
|
||||
}
|
||||
const int kstep = kg / 16;
|
||||
const int blk = kg / R4D_GEMM_MXFP4_GROUP; // one E8M0 block covers this whole step
|
||||
v2i af[MT];
|
||||
@@ -181,9 +200,7 @@ __global__ void r4d_gemm_mxfp4a8_nt_m64_kernel(
|
||||
// wave reads 128 contiguous bytes. In checkpoint order these would be sixteen rows K/2 bytes
|
||||
// apart -- a fully strided read on the operand that dominates traffic.
|
||||
const unsigned int wv = Wp[((size_t)wtile[j] * ksteps + kstep) * WAVE32 + lane];
|
||||
int dsh = (int)wref[j] - (int)Ws[(size_t)blk * N + n];
|
||||
dsh = dsh < 0 ? 0 : (dsh > 15 ? 15 : dsh);
|
||||
const v2i wf = r4d_mxfp4_unpack8(wv, dsh);
|
||||
const v2i wf = r4d_mxfp4_frag8(wv, r4d_mxfp4_fold_d(wref[j], Ws[(size_t)blk * N + n]));
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++)
|
||||
acc[j][i] = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(af[i], wf, acc[j][i]);
|
||||
@@ -199,7 +216,8 @@ __global__ void r4d_gemm_mxfp4a8_nt_m64_kernel(
|
||||
for (int j = 0; j < NPW; j++) {
|
||||
const int slot = ((col_local * NPW + j) * SK + ksplit) * 256;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; e++) red[slot + lane * 8 + e] = acc[j][i][e];
|
||||
for (int e = 0; e < 8; e++)
|
||||
red[slot + lane * 8 + e] = ABLK ? acc[j][i][e] * scur[i][e] : acc[j][i][e];
|
||||
}
|
||||
__syncthreads();
|
||||
// One pass over the block's 256-element tiles; every thread takes a stride.
|
||||
@@ -215,7 +233,7 @@ __global__ void r4d_gemm_mxfp4a8_nt_m64_kernel(
|
||||
float v = 0.f;
|
||||
for (int s = 0; s < SK; s++) v += red[(col * SK + s) * 256 + off];
|
||||
C[(size_t)m * N + n] =
|
||||
(__hip_bfloat16)(v * __int_as_float((int)Wref[n] << 23) * As[m]);
|
||||
(__hip_bfloat16)(v * __int_as_float((int)Wref[n] << 23) * (ABLK ? 1.f : As[m]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,7 +243,7 @@ extern "C" int r4d_gemm_mxfp4a8_nt_m64_group(void) { return R4D_GEMM_MXFP4_GROUP
|
||||
|
||||
extern "C" void r4d_gemm_mxfp4a8_nt_m64(long a, long ascale, long wq, long ws, long wref, long c,
|
||||
int M, int K, int N, int WV, int SK, int MB, int NPW,
|
||||
long stream) {
|
||||
int as_group, int as_rs, long stream) {
|
||||
if (M < 1 || M > R4D_GEMM_MXFP4_MAX_M)
|
||||
throw std::runtime_error("r4d gemm_mxfp4a8_nt_m64: M must be 1..64");
|
||||
if (N % 16) throw std::runtime_error("r4d gemm_mxfp4a8_nt_m64: N must be a multiple of 16");
|
||||
@@ -236,6 +254,9 @@ extern "C" void r4d_gemm_mxfp4a8_nt_m64(long a, long ascale, long wq, long ws, l
|
||||
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_mxfp4a8_nt_m64: MB must be 1..4");
|
||||
if (NPW != 1 && NPW != 2 && NPW != 4 && NPW != 8)
|
||||
throw std::runtime_error("r4d gemm_mxfp4a8_nt_m64: NPW must be 1, 2, 4 or 8");
|
||||
if (as_group != 0 && as_group != 128)
|
||||
throw std::runtime_error("r4d gemm_mxfp4a8_nt_m64: the activation scale is per row or per 128 of K");
|
||||
const bool ablk = as_group == 128;
|
||||
const int ntiles = N / 16, mtiles = (M + 15) / 16;
|
||||
const int ncols = WV * NPW;
|
||||
dim3 grid((ntiles + ncols - 1) / ncols, (mtiles + MB - 1) / MB), block(WV * SK * WAVE32);
|
||||
@@ -249,8 +270,13 @@ extern "C" void r4d_gemm_mxfp4a8_nt_m64(long a, long ascale, long wq, long ws, l
|
||||
auto Rp = (const unsigned char*)wref;
|
||||
auto Cp = (__hip_bfloat16*)c;
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
#define LAUNCH(MTv, NPv) hipLaunchKernelGGL((r4d_gemm_mxfp4a8_nt_m64_kernel<MTv, NPv>), grid, \
|
||||
block, smem, st, Ap, Sp, Wp, Zp, Rp, Cp, M, K, N, WV, SK)
|
||||
#define LAUNCH(MTv, NPv) \
|
||||
do { \
|
||||
if (ablk) hipLaunchKernelGGL((r4d_gemm_mxfp4a8_nt_m64_kernel<MTv, NPv, true>), grid, block, \
|
||||
smem, st, Ap, Sp, Wp, Zp, Rp, Cp, M, K, N, WV, SK, as_rs); \
|
||||
else hipLaunchKernelGGL((r4d_gemm_mxfp4a8_nt_m64_kernel<MTv, NPv, false>), grid, block, \
|
||||
smem, st, Ap, Sp, Wp, Zp, Rp, Cp, M, K, N, WV, SK, as_rs); \
|
||||
} while (0)
|
||||
#define LAUNCH_MB(NPv) switch (MB) { \
|
||||
case 1: LAUNCH(1, NPv); break; \
|
||||
case 2: LAUNCH(2, NPv); break; \
|
||||
@@ -284,6 +310,9 @@ extern "C" int r4d_rad_gemm_mxfp4a8_m64(const RadArgs* a, RadStream s) {
|
||||
* reference and every element would be scaled against zero. */
|
||||
if (!g.bref) return R4D_NO(RAD_E_INVAL);
|
||||
const int group = r4d_gemm_mxfp4a8_nt_m64_group();
|
||||
int as_group = 0, as_rs = 1;
|
||||
rc = mx_ascale_grid(g, &as_group, &as_rs);
|
||||
if (rc != RAD_OK) return rc;
|
||||
Cfg c;
|
||||
rc = cfg_for(a, g.K, g.M, group, BR_FLAT, &c);
|
||||
if (rc != RAD_OK) return rc;
|
||||
@@ -291,7 +320,7 @@ extern "C" int r4d_rad_gemm_mxfp4a8_m64(const RadArgs* a, RadStream s) {
|
||||
r4d_gemm_mxfp4a8_nt_m64(r4d_addr(g.a), r4d_addr(g.ascale), r4d_addr(g.b),
|
||||
r4d_addr(g.bscale), r4d_addr(g.bref), r4d_addr(g.y),
|
||||
(int)g.M, (int)g.K, (int)g.N, c.wv, c.sk, c.mb, c.npw,
|
||||
(long)(uintptr_t)s));
|
||||
as_group, as_rs, (long)(uintptr_t)s));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user