Archived
libr4d: w5a16 / w4a16_asym GEMM (5- and 4-bit asymmetric, any M, parts), givens_rotate kernel, layouts, selftest
This commit is contained in:
1 parent
ffa235bf0f
commit
9e9fce3aca
9 files changed
+722
-23
No files matched your search
+6
-1
@@ -344,13 +344,14 @@ that points each name at its copy. A name two shards hold that no index entry se
|
||||
The conversions of record live in `scripts/convfn.sh` and `scripts/convdspark.sh`, with their
|
||||
recipes in `data/recipes/`.
|
||||
|
||||
**A release someone else already quantised is read as it ships.** Three forms are imported weight
|
||||
**A release someone else already quantised is read as it ships.** Four forms are imported weight
|
||||
by weight, so their quantised linears need no recipe rule:
|
||||
|
||||
| `quantization_config` | read as | |
|
||||
|---|---|---|
|
||||
| `quark`, MXFP4 weights (fp4, per_group 32, e8m0) | `fp4_e2m1*e8m0[1x32]` | byte for byte: the same codes and exponents. A checkpoint of this form is also served as it stands (§3.4) |
|
||||
| `paroquant_mxfp4` (z-lab's ParoQuant on the MXFP4 grid: `format mxfp4`, `mxfp4_block 32`, `group_size 128`) | `fp4_e2m1*e8m0[1x32]/givens` | the codes and exponents byte for byte, plus the learned rotation as the encoding's `givens` tables: per checkpoint tensor the Givens pairs (made whole-row columns), angles and `1 / channel_scales`, a fused gate\|up or qkv\|z carrying one set a part. Convert-time only (the tables are built, not mapped); served by the rotating activation quantiser `givens_quant_act_fp8` and the ordinary MXFP4 GEMM (docs/OPS.md). Its fp8 heads and embedding (one f32 scale a row) are read exactly, for a recipe's `cast`/`rtn` rule |
|
||||
| `paroquant`, `format int5-bitplane` (bits 5, group 128) and z-lab's plain `paroquant` (bits 4, group 128) | `u5*f16[1x128]-u8[1x128]/givens` and `u4*f16[1x128]-u8[1x128]/givens` | the AWQ-packed low nibbles (plus the fifth-bit plane) re-laid as a code bit stream along K, the f16 scales and the integer zero points transposed to `[N, K/128]`, and the rotation as the `givens` tables above. Convert-time only; served by `givens_rotate` and `gemm_nt_q` at `w5a16` / `w4a16_asym` over an f16 activation (docs/OPS.md) |
|
||||
| `auto-round` with `packing_format: auto_round:auto_gptq`, and `gptq` (v1) | `i4*bf16[1x128]` or `i8*bf16[1x128]` | the GPTQ-packed codes transposed and made signed, the f16 scales rounded to bf16. Symmetric, group 128 or a multiple of it (per channel included), no act-order. Convert-time only, because the scales are not served as shipped |
|
||||
|
||||
The width is read per tensor, so a release mixing int4 experts with int8 projections imports both.
|
||||
@@ -371,6 +372,10 @@ rad-convert ~/models/amd/Qwen3.8-27B-Quark-AWQ-MXFP4 --draft-model ~/models/z-la
|
||||
rad-convert ~/models/MisterSnrub/Qwen3.8-27B-PARO-MXFP4-v2 --draft-model ~/models/z-lab/Qwen3.8-27B-DFlash2 \
|
||||
--recipe data/recipes/q38-27b-paro-mxfp4-df2.recipe -o q38-27b-paro-mxfp4-df2.rad
|
||||
|
||||
# ParoQuant int5 (Launch80/Qwen3.8-27B-PARO-int5) and z-lab's int4 (z-lab/Qwen3.8-27B-PARO): same recipe
|
||||
rad-convert ~/models/Launch80/Qwen3.8-27B-PARO-int5 --draft-model ~/models/z-lab/Qwen3.8-27B-DFlash2 \
|
||||
--recipe data/recipes/q38-27b-paro-mxfp4-df2.recipe -o q38-27b-paro-int5-df2.rad
|
||||
|
||||
# An AutoRound int4 Qwen3.8-Flash-Next: its experts imported, its int4 projections made int8
|
||||
rad-convert ~/models/ukisai/Swift-1.5-Qwen3.8-Flash-Next-W4A16-AutoRound \
|
||||
--recipe data/recipes/qwen4exp-autoround-i8-hc8m.recipe -o swift-autoround.rad
|
||||
|
||||
+2
-1
@@ -363,6 +363,7 @@ spelling of `act`, not the vocabulary's.
|
||||
| `quant_act_fp8` | `M` `n` `group` `dtype` | `x` → `q`, `scale` |
|
||||
| `had_quant_act_fp8` | `M` `n` `group` `dtype` | `x` → `q`, `scale` |
|
||||
| `givens_quant_act_fp8` | `M` `n` `group` `dtype` `stages` `parts` | `x`, `pairs`w, `angle`w, `rscale`w → `q`, `scale` |
|
||||
| `givens_rotate` | `M` `n` `group` `dtype` `stages` `parts` | `x`, `pairs`w, `angle`w, `rscale`w → `y` |
|
||||
| `rmsnorm_quant_fp8` | `M` `n` `eps`:f64 `group` `dtype` `wadd`:f64/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? |
|
||||
| `gated_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate_up` → `q`, `scale`, `out_bf16`? |
|
||||
| `gemm_nt_q_gated` | `M` `N` `K` `group` `dtype` `act` | `a`, `a_scale`, `b`w, `b_scale`w → `q`, `scale`, `out_bf16`? |
|
||||
@@ -385,7 +386,7 @@ copies of the activation, written side by side -- `q` is `[M, parts * n]`, `scal
|
||||
`[M, parts * n / group]` -- and `pairs` and `angle` stack the parts' `stages` rows each, `rscale`
|
||||
one row a part. `gemm_nt_q` then takes `pb1` and `pb2`, the row bounds of the weight's parts: its
|
||||
rows `[0, pb1)`, `[pb1, pb2)` and `[pb2, N)` meet copy 0, 1 and 2, and `a`/`a_scale` are the wide
|
||||
planes. `pb1` absent is one part; `pb2` absent ends the second part at `N`. Only the `mxfp4a8` rows
|
||||
planes. `givens_rotate` is the same rotation with no quantiser, `y` in its own dtype (f16), for the 16-bit-activation weights `w5a16` (ParoQuant int5: u5 codes, an f16 scale and a u8 zero point a 128 of K) and `w4a16_asym` (z-lab's int4, the same at 4 bits); their GEMM serves any `M` (rows past 64 run as chunks of 64). `pb1` absent is one part; `pb2` absent ends the second part at `N`. Only the `mxfp4a8` rows
|
||||
read them, and the host rows that do not (`libavx`) refuse a call that names one rather than
|
||||
contract against the wrong copy.
|
||||
|
||||
|
||||
@@ -107,6 +107,7 @@ set(R4D_UNITS
|
||||
"r4d_gemm_bf16_nt_m64|"
|
||||
"r4d_gemm_bf16_nt_tiled|"
|
||||
"r4d_gemm_w4a16_nt_m64|-DR4D_GEMM_W4_GROUP=${RAD_W4_GROUP}"
|
||||
"r4d_gemm_w5a16_nt_m64|-DR4D_GEMM_W4_GROUP=${RAD_W4_GROUP}"
|
||||
"r4d_gemm_w8a16_nt_m64|-DR4D_GEMM_W8_GROUP=${RAD_W4_GROUP}"
|
||||
"r4d_gemm_w2a8_nt|-DR4D_GEMM_W2A8_GROUP=${RAD_W4_GROUP}"
|
||||
"r4d_gemm_w4a8_nt_m64|-DR4D_GEMM_W4A8_GROUP=${RAD_W4_GROUP}"
|
||||
|
||||
@@ -0,0 +1,376 @@
|
||||
// r4d_gemm_w5a16_nt_m64.hip - r4d_gemm_w5a16_nt_m64: 5-bit-weight skinny GEMM for M up to 64,
|
||||
// gfx1201. C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T, f16 A, bf16 C, fp32 accumulate.
|
||||
//
|
||||
// r4d_gemm_w4a16_nt_m64 with a fifth bit per weight, for ParoQuant's int5 (asymmetric, an f16 scale
|
||||
// and a 5-bit integer zero per 128 of K, the rotation done on the activation in front of it). The
|
||||
// reading of that kernel's header holds word for word -- weight-streaming, memory-bound, f16
|
||||
// because 1024 + n is exact -- and this file only says what the fifth bit changes.
|
||||
//
|
||||
// THE WEIGHT BLOB IS TWO PLANES. The low four bits of every code are w4's fragment order exactly
|
||||
// (offset binary here: the nibble is q & 15, no sign flip). The fifth bits follow as a second
|
||||
// plane of 32-bit words, ONE A (n-tile, 64-wide k block, lane):
|
||||
//
|
||||
// hi[(t * (K/64) + kb) * 32 + lane] byte s = k step s of the block, bit e = element e
|
||||
//
|
||||
// element e of a lane's fragment being the same one nibble 2e / 2(e-4)+1 of dword s holds. So a
|
||||
// lane's 16 bytes of nibbles and its 4 bytes of fifth bits cover the same 32 weights, and the
|
||||
// second load is one dword a block.
|
||||
//
|
||||
// THE UNPACK ADDS TWO MULTIPLIES TO w4's. The even elements' fifth bits are the byte's low nibble,
|
||||
// the odd elements' its high one; a 4-bit mask n becomes one bit a byte by n * 0x00204081 (bit j
|
||||
// lands at 8j and nothing else lands on a multiple of eight below bit 32), masked with 0x01010101
|
||||
// and shifted to the 16s place of the nibble's byte. The 5-bit code is then 0..31, 1024 + q is
|
||||
// exact in f16, and the zero point -- an integer 0..31 in the scale dword's high half as
|
||||
// -(1024 + zero) -- subtracts exactly.
|
||||
//
|
||||
// The dispatcher takes M past 64 as chunks of 64 rows (the weight is re-read per chunk, which at a
|
||||
// 2048-row prefill chunk is near the compute bound of the f16 matrix instruction anyway), and
|
||||
// partitioned weights (ParoQuant's gate|up: `pb1`/`pb2`, see r4d_gemm_args.h) as one launch per
|
||||
// part over windows of the planes.
|
||||
#include "r4d.h"
|
||||
#include "r4d_gemm_args.h"
|
||||
#include "r4d_gdn_wmma.h"
|
||||
#include <hip/hip_bf16.h>
|
||||
#include <hip/hip_fp16.h>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
#define R4D_GEMM_W4_MAX_M 64
|
||||
#ifndef R4D_GEMM_W4_GROUP
|
||||
#define R4D_GEMM_W4_GROUP 128 // K per (scale, zero) pair
|
||||
#endif
|
||||
#define R4D_GEMM_W4_KPB 64 // K per packed block: 4 k steps, 16 bytes per lane
|
||||
#define WAVE32 32
|
||||
|
||||
typedef _Float16 v8h __attribute__((ext_vector_type(8)));
|
||||
typedef unsigned uint4v __attribute__((ext_vector_type(4)));
|
||||
|
||||
// NT reads the weight non-temporally. The weight is streamed once per step and never reused, while
|
||||
// the activation is re-read once per column tile and wants to stay resident, so telling the cache
|
||||
// not to keep the weight decides which of the two occupies the last level. It is a gain at M=8 and
|
||||
// M=16 and a LOSS from M=24 up on the shapes with the longest K, so it is per-call, like every
|
||||
// other geometry parameter, and the table in radiance_w4.py carries the value per M band.
|
||||
//
|
||||
// One copy of any of these weights fits this part's 64 MiB last level, so a bench that streams a
|
||||
// SINGLE buffer in a loop measures a cache-resident machine and cannot see this effect at all; a
|
||||
// pooled working set is what makes the difference visible.
|
||||
|
||||
// NPW (column tiles per WAVE) is a template parameter because it sizes the accumulator array, and a
|
||||
// launch argument because the right value is a property of M: at M=8 the kernel is at the DRAM
|
||||
// roofline and NPW>1 only costs blocks, while at M=64 the activation re-read is four times the
|
||||
// weight and NPW=4 is what claws it back. Instantiated only for the two values that win in
|
||||
// practice; anything else is rejected rather than silently rounded.
|
||||
|
||||
// Address-space-qualified so the loads stay global_load rather than flat_load: on gfx12 a flat
|
||||
// load increments dscnt as well as loadcnt, so a later LDS wait would also wait on HBM. See the
|
||||
// same declaration in r4d_gemm_bf16_nt_m64.hip for why the element type is a scalar there.
|
||||
typedef __attribute__((address_space(1))) const uint4v* r4d_gw4;
|
||||
typedef __attribute__((address_space(1))) const unsigned long long* r4d_gu2;
|
||||
typedef __attribute__((address_space(1))) const unsigned* r4d_gu1;
|
||||
|
||||
__device__ __forceinline__ v8h fragA(r4d_gu2 p, int u) {
|
||||
union { unsigned long long w[2]; v8h f; } x;
|
||||
x.w[0] = p[4 * u];
|
||||
x.w[1] = p[4 * u + 2];
|
||||
return x.f;
|
||||
}
|
||||
|
||||
// One dword of eight nibbles -> one 16x16x16 f16 B fragment, minus the zero point.
|
||||
// t0/t1 split the even and odd nibbles into bytes; each v_perm_b32 pairs two of those bytes with
|
||||
// the constant high byte 0x64, which is the f16 exponent field for 1024. nz carries
|
||||
// -(1024 + zero) in BOTH halves so one v_pk_add_f16 corrects a pair.
|
||||
// The unpack is under a percent of this kernel at M=8, where it is at the DRAM roofline and the
|
||||
// arithmetic hides in the load latency. It is a real fraction at M=64, but that band belongs to
|
||||
// r4d_gemm_w4a8_nt_m64 -- and with no dequant at all the f16 form still trails the int8 one there,
|
||||
// because the gap is the matrix instruction and not the dequant. Nothing here is worth a knob.
|
||||
#ifndef R4D_GEMM_W4_TWOS
|
||||
#define R4D_GEMM_W4_TWOS 1
|
||||
#endif
|
||||
|
||||
// DO NOT FOLD THE GROUP SCALE INTO THE B FRAGMENT. One v_pk_mul_f16 per pair on the finished
|
||||
// integer q is exact in f16, removes the second accumulator array entirely and takes the M=64 shape
|
||||
// from 192 VGPRs to 135 -- and it is SLOWER on most of the drafter's shapes. The multiply lands
|
||||
// inside the per-fragment dependent chain between the unpack and the WMMA, where the per-group
|
||||
// epilogue it replaces sits outside it; this kernel is stall-bound, not register-bound.
|
||||
|
||||
__device__ __forceinline__ v8h dequant(unsigned w, __half2 nz) {
|
||||
const unsigned K64 = 0x64646464u;
|
||||
#if R4D_GEMM_W4_TWOS
|
||||
// The stored nibble is a 4-bit TWO'S COMPLEMENT code, because that is what
|
||||
// r4d_gemm_w4a8_nt_m64 needs -- it shifts the nibble into the byte's high half and reads the
|
||||
// byte signed. This kernel wants OFFSET BINARY, so that 1024 + n minus the constant 1024 + 8
|
||||
// is the value. The two differ by bit 3 of the nibble, so one XOR per spread dword converts,
|
||||
// and the zero point is then pinned to 8 for every group. Two instructions per fragment, on
|
||||
// the kernel whose whole unpack is a fraction of a percent of its time at M=8 -- which is why
|
||||
// the conversion lives here and not in the int8 kernel, where the unpack is three instructions
|
||||
// and does matter. One quantised weight, two kernels, dispatched on M.
|
||||
const unsigned t0 = (w & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
const unsigned t1 = ((w >> 4) & 0x0F0F0F0Fu) ^ 0x08080808u;
|
||||
#else
|
||||
const unsigned t0 = w & 0x0F0F0F0Fu;
|
||||
const unsigned t1 = (w >> 4) & 0x0F0F0F0Fu;
|
||||
#endif
|
||||
union { unsigned u[4]; __half2 h2[4]; v8h f; } x;
|
||||
x.u[0] = __builtin_amdgcn_perm(K64, t0, 0x05010400u); // nibbles 0, 2
|
||||
x.u[1] = __builtin_amdgcn_perm(K64, t0, 0x05030402u); // nibbles 4, 6
|
||||
x.u[2] = __builtin_amdgcn_perm(K64, t1, 0x05010400u); // nibbles 1, 3
|
||||
x.u[3] = __builtin_amdgcn_perm(K64, t1, 0x05030402u); // nibbles 5, 7
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; i++) x.h2[i] = __hadd2(x.h2[i], nz);
|
||||
return x.f;
|
||||
}
|
||||
|
||||
// One dword of eight nibbles plus the byte of their fifth bits -> one 16x16x16 f16 B fragment,
|
||||
// minus the zero point. Nibble 2e (e < 4) / 2(e-4)+1 is fragment element e, so bits 0..3 of `hb`
|
||||
// are the fifth bits of the even nibbles' elements and bits 4..7 the odd nibbles'.
|
||||
__device__ __forceinline__ unsigned spread16(unsigned n) { // 4 bits -> bit 4 of each byte
|
||||
return ((n * 0x00204081u) & 0x01010101u) << 4;
|
||||
}
|
||||
__device__ __forceinline__ v8h dequant5(unsigned w, unsigned hb, __half2 nz) {
|
||||
const unsigned K64 = 0x64646464u;
|
||||
const unsigned t0 = (w & 0x0F0F0F0Fu) | spread16(hb & 15u);
|
||||
const unsigned t1 = ((w >> 4) & 0x0F0F0F0Fu) | spread16(hb >> 4);
|
||||
union { unsigned u[4]; __half2 h2[4]; v8h f; } x;
|
||||
x.u[0] = __builtin_amdgcn_perm(K64, t0, 0x05010400u); // elements 0, 1
|
||||
x.u[1] = __builtin_amdgcn_perm(K64, t0, 0x05030402u); // elements 2, 3
|
||||
x.u[2] = __builtin_amdgcn_perm(K64, t1, 0x05010400u); // elements 4, 5
|
||||
x.u[3] = __builtin_amdgcn_perm(K64, t1, 0x05030402u); // elements 6, 7
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; i++) x.h2[i] = __hadd2(x.h2[i], nz);
|
||||
return x.f;
|
||||
}
|
||||
|
||||
__device__ __forceinline__ v8f mmah(v8h a, v8h b, v8f c) {
|
||||
return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a, b, c);
|
||||
}
|
||||
|
||||
// MT = row tiles per BLOCK, as in the bf16 kernel: more of them amortise the weight fragment and
|
||||
// its unpack over more WMMA, fewer put more blocks on the machine.
|
||||
// HI false is the 4-bit asymmetric grid (z-lab's plain int4): the same kernel with no fifth-bit plane.
|
||||
template <int MT, int NPW, bool NT, bool HI>
|
||||
__global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel(
|
||||
const unsigned short* __restrict__ A, const unsigned* __restrict__ Wq,
|
||||
const unsigned* __restrict__ Whi, const unsigned* __restrict__ Wsz,
|
||||
__hip_bfloat16* __restrict__ C, int M, int K, int N, int WV, int SK, int lda, int ldc) {
|
||||
extern __shared__ float red[]; // WV*NPW*SK*256 floats: a 16x16 tile per (column, split)
|
||||
const int tid = threadIdx.x, wave = tid / WAVE32, lane = tid % WAVE32;
|
||||
const int col_local = wave / SK, ksplit = wave % SK;
|
||||
const int nt0 = (blockIdx.x * WV + col_local) * NPW;
|
||||
const int n0 = nt0 * 16;
|
||||
const int mt0 = blockIdx.y * MT;
|
||||
const int Ks = K / SK;
|
||||
const int groups = Ks / R4D_GEMM_W4_GROUP; // groups this split owns
|
||||
const int kbase = ksplit * Ks;
|
||||
|
||||
v8f acc[NPW][MT];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++)
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++) acc[j][i] = (v8f)(0.f);
|
||||
|
||||
if (n0 < N) {
|
||||
// Activation: rows clamped, never masked -- a tile past M re-reads the last valid row and
|
||||
// computes a result nobody stores.
|
||||
unsigned aoff[MT];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++)
|
||||
aoff[i] = rowGo((unsigned)lda, (mt0 + i) * 16, M - 1, lane) + (unsigned)kbase;
|
||||
r4d_gu2 pa[MT];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++) pa[i] = (r4d_gu2)(const unsigned long long*)(A + aoff[i]);
|
||||
|
||||
// Weight: one uint4 per lane per four k steps, tiles already permuted, so the whole k loop is
|
||||
// global_load_b128 with a constant stride and no address arithmetic per step.
|
||||
const int kpb = K / R4D_GEMM_W4_KPB; // packed blocks in a full row of tiles
|
||||
const int nsz = K / R4D_GEMM_W4_GROUP; // (scale, zero) dwords per row of tiles
|
||||
r4d_gw4 pw[NPW];
|
||||
r4d_gu1 ph[NPW];
|
||||
r4d_gu1 ps[NPW];
|
||||
const int ntlast = N / 16 - 1;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++) {
|
||||
// Clamped, like the row tiles: a wave whose last column tiles run past N re-reads the last
|
||||
// real tile and computes a result the store discards, rather than reading memory it does not
|
||||
// own or diverging the whole k loop on a mask.
|
||||
const int t = (nt0 + j) < ntlast ? (nt0 + j) : ntlast;
|
||||
pw[j] = (r4d_gw4)(const uint4v*)(Wq) +
|
||||
(size_t)(t * kpb + kbase / R4D_GEMM_W4_KPB) * WAVE32 + lane;
|
||||
// Quantizer parameters: 16 distinct dwords per group, read by the 16 lanes that own the rows.
|
||||
ph[j] = (r4d_gu1)(const unsigned*)(HI ? Whi : Wq) +
|
||||
(size_t)(t * kpb + kbase / R4D_GEMM_W4_KPB) * WAVE32 + lane;
|
||||
ps[j] = (r4d_gu1)(const unsigned*)(Wsz) +
|
||||
(size_t)(t * nsz + kbase / R4D_GEMM_W4_GROUP) * 16 + (lane & 15);
|
||||
}
|
||||
|
||||
const int bpg = R4D_GEMM_W4_GROUP / R4D_GEMM_W4_KPB; // packed blocks per group
|
||||
|
||||
for (int g = 0; g < groups; g++) {
|
||||
float sc[NPW];
|
||||
__half2 nz[NPW];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++) {
|
||||
const unsigned sz = ps[j][(size_t)g * 16];
|
||||
sc[j] = __half2float(__ushort_as_half((unsigned short)(sz & 0xFFFFu)));
|
||||
nz[j] = __half2half2(__ushort_as_half((unsigned short)(sz >> 16)));
|
||||
}
|
||||
|
||||
// The first WMMA of a group takes an inline zero for C, so the group accumulator costs no
|
||||
// instruction to clear.
|
||||
v8f g_acc[NPW][MT];
|
||||
#pragma unroll
|
||||
for (int b = 0; b < bpg; b++) {
|
||||
uint4v wv[NPW];
|
||||
unsigned hv[NPW];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++) {
|
||||
const size_t o = (size_t)(g * bpg + b) * WAVE32;
|
||||
wv[j] = NT ? __builtin_nontemporal_load(&pw[j][o]) : pw[j][o];
|
||||
hv[j] = HI ? (NT ? __builtin_nontemporal_load(&ph[j][o]) : ph[j][o]) : 0u;
|
||||
}
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 4; s++) {
|
||||
const int u = b * 4 + s;
|
||||
v8h afrag[MT];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++) afrag[i] = fragA(pa[i], u); // loaded once, spent NPW times
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++) {
|
||||
const v8h bfrag = dequant5(wv[j][s], (hv[j] >> (8 * s)) & 0xFFu, nz[j]);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++)
|
||||
g_acc[j][i] = u == 0 ? mmah(afrag[i], bfrag, (v8f)(0.f))
|
||||
: mmah(afrag[i], bfrag, g_acc[j][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++)
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; e++) acc[j][i][e] = fmaf(g_acc[j][i][e], sc[j], acc[j][i][e]);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++) pa[i] += (R4D_GEMM_W4_GROUP / 16) * 4;
|
||||
}
|
||||
}
|
||||
|
||||
// Split-K reduction and store: identical to r4d_gemm_bf16_nt_m64, one row tile at a time so the
|
||||
// LDS is WV*SK KiB whatever MT is, and in a fixed order over splits so the result does not depend
|
||||
// on how the waves were scheduled.
|
||||
const int ncols = WV * NPW;
|
||||
const int nthreads = WV * SK * WAVE32;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++) {
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
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 + e * WAVE32 + lane] = acc[j][i][e];
|
||||
}
|
||||
__syncthreads();
|
||||
for (int idx = tid; idx < ncols * 256; idx += nthreads) {
|
||||
const int col = idx >> 8, off = idx & 255;
|
||||
float v = 0.f;
|
||||
for (int s = 0; s < SK; s++) v += red[(col * SK + s) * 256 + off];
|
||||
const int l = off & 31;
|
||||
const int n = (blockIdx.x * ncols + col) * 16 + (l & 15);
|
||||
const int m = (mt0 + i) * 16 + 8 * (l >> 4) + (off >> 5);
|
||||
if (n < N && m < M) C[(size_t)m * ldc + n] = (__hip_bfloat16)v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, int K, int N,
|
||||
int WV, int SK, int MB, int NPW, int NT, long stream, int lda, int ldc,
|
||||
bool hi5) {
|
||||
if (!lda) lda = K;
|
||||
if (!ldc) ldc = N;
|
||||
if (M < 1 || M > R4D_GEMM_W4_MAX_M)
|
||||
throw std::runtime_error("r4d gemm_w4a16_nt_m64: M must be 1..64");
|
||||
if (N % 16) throw std::runtime_error("r4d gemm_w4a16_nt_m64: N must be a multiple of 16");
|
||||
if (K % (SK * R4D_GEMM_W4_GROUP))
|
||||
throw std::runtime_error("r4d gemm_w4a16_nt_m64: K must be divisible by SK*128");
|
||||
if (WV * SK * WAVE32 > 1024) throw std::runtime_error("r4d gemm_w4a16_nt_m64: threads/block > 1024");
|
||||
if (MB < 1 || MB > 4) throw std::runtime_error("r4d gemm_w4a16_nt_m64: MB must be 1..4");
|
||||
if (NPW != 1 && NPW != 4) throw std::runtime_error("r4d gemm_w4a16_nt_m64: NPW must be 1 or 4");
|
||||
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);
|
||||
size_t smem = (size_t)ncols * SK * 256 * sizeof(float);
|
||||
if (smem > 64 * 1024) throw std::runtime_error("r4d gemm_w4a16_nt_m64: WV*NPW*SK too large");
|
||||
auto Ap = (const unsigned short*)a;
|
||||
auto Wp = (const unsigned*)wq;
|
||||
auto Hp = (const unsigned*)whi;
|
||||
auto Sp = (const unsigned*)wsz;
|
||||
auto Cp = (__hip_bfloat16*)c;
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
#define LAUNCH(MT, NP, TT) do { \
|
||||
if (hi5) hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<MT, NP, TT, true>), grid, block, smem, \
|
||||
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \
|
||||
else hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<MT, NP, TT, false>), grid, block, smem, \
|
||||
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0)
|
||||
#define LAUNCH_MB(NP, TT) switch (MB) { \
|
||||
case 1: LAUNCH(1, NP, TT); break; \
|
||||
case 2: LAUNCH(2, NP, TT); break; \
|
||||
case 3: LAUNCH(3, NP, TT); break; \
|
||||
case 4: LAUNCH(4, NP, TT); break; \
|
||||
default: throw std::runtime_error("r4d gemm_w4a16_nt_m64: MB unsupported"); }
|
||||
if (NPW == 1) {
|
||||
if (NT) { LAUNCH_MB(1, true); } else { LAUNCH_MB(1, false); }
|
||||
} else {
|
||||
if (NT) { LAUNCH_MB(4, true); } else { LAUNCH_MB(4, false); }
|
||||
}
|
||||
#undef LAUNCH_MB
|
||||
#undef LAUNCH
|
||||
}
|
||||
|
||||
int r4d_gemm_w5a16_nt_m64_max_m(void) { return R4D_GEMM_W4_MAX_M; }
|
||||
int r4d_gemm_w5a16_nt_m64_group(void) { return R4D_GEMM_W4_GROUP; }
|
||||
|
||||
// ============================================================== the plugin ABI entry points
|
||||
//
|
||||
// The operand reader and the split law are in r4d_gemm_args.h. See libr4d/r4d_args.h for why there
|
||||
// is no adapter directory between the core and these launches.
|
||||
/* ------------------------------------------------------------------ f16-activation grids */
|
||||
|
||||
static int w5_dispatch(const RadArgs* a, RadStream s, bool hi5) {
|
||||
GemmOps g;
|
||||
int rc = gemm_ops(a, &g, 1);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (g.N % 16) return R4D_NO(RAD_E_SHAPE);
|
||||
MxParts mp;
|
||||
rc = mx_parts(a, &g, &mp);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (g.K % R4D_GEMM_W4_GROUP) return R4D_NO(RAD_E_SHAPE);
|
||||
const int group = r4d_gemm_w5a16_nt_m64_group();
|
||||
// The row's M is the step's, not the kernel's: rows past 64 are chunks of 64, each its own pass
|
||||
// over the weight, so a prefill chunk costs the weight's bytes once a 64 rows.
|
||||
const long long mc = std::min<long long>(g.M, r4d_gemm_w5a16_nt_m64_max_m());
|
||||
Cfg c;
|
||||
rc = cfg_for(a, g.K, mc, group, BR_FLAT, &c);
|
||||
if (rc != RAD_OK) return rc;
|
||||
const long long Ntot = g.N, K = g.K, lda = mp.lda ? mp.lda : g.K;
|
||||
const long long nsz = K / R4D_GEMM_W4_GROUP;
|
||||
for (int p = 0; p < mp.n; ++p) {
|
||||
const long long n0 = mp.n0[p], np = mp.n1[p] - mp.n0[p];
|
||||
for (long long m0 = 0; m0 < g.M; m0 += mc) {
|
||||
const long long mm = std::min<long long>(mc, g.M - m0);
|
||||
R4D_TRY("gemm_w5a16_nt_m64",
|
||||
r4d_gemm_w5a16_nt_m64(
|
||||
r4d_addr(g.a) + (m0 * lda + p * K) * 2,
|
||||
r4d_addr(g.b) + n0 * K / 2, /* the nibble plane */
|
||||
hi5 ? r4d_addr(g.b) + Ntot * K / 2 + n0 * K / 8 : 0L, /* the fifth-bit plane */
|
||||
r4d_addr(g.bscale) + (n0 / 16) * nsz * 16 * 4,
|
||||
r4d_addr(g.y) + (m0 * Ntot + n0) * 2,
|
||||
(int)mm, (int)K, (int)np, c.wv, c.sk, c.mb, c.npw, c.nt, (long)(uintptr_t)s,
|
||||
(int)lda, (int)Ntot, hi5));
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_gemm_w5a16_m64(const RadArgs* a, RadStream s) { return w5_dispatch(a, s, true); }
|
||||
/* ...and the plain 4-bit asymmetric grid (z-lab's int4), whose blob is the nibble plane alone. */
|
||||
extern "C" int r4d_rad_gemm_w4a16asym_m64(const RadArgs* a, RadStream s) { return w5_dispatch(a, s, false); }
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "r4d_emit.h"
|
||||
#include "r4d_fp8_args.h"
|
||||
#include "r4d_fp8_wmma.h"
|
||||
#include <hip/hip_fp16.h>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -57,6 +58,9 @@ __device__ __forceinline__ void wave_sync() {
|
||||
// spreads a row's `parts * nkb` groups over blocks of kWaves waves -- one group a wave, so a row is
|
||||
// several blocks and a decode step of a few rows still fills the part. A wave that has no group
|
||||
// returns at once; nothing in the kernel is a block barrier.
|
||||
// QUANT false is givens_rotate: the same rotation, no quantiser, the rotated row written as f16 into
|
||||
// `q` (read as a uint16 plane, y_ld elements a row) for a GEMM that reads a 16-bit activation.
|
||||
template <bool QUANT>
|
||||
__global__ __launch_bounds__(kThreads) void givens_quant_act_fp8_kernel(
|
||||
const uint16_t* __restrict__ x, const int32_t* __restrict__ pairs,
|
||||
const float* __restrict__ angle, const float* __restrict__ rscale, uint8_t* __restrict__ q,
|
||||
@@ -122,6 +126,15 @@ __global__ __launch_bounds__(kThreads) void givens_quant_act_fp8_kernel(
|
||||
|
||||
const float4 r = *(const float4*)(buf + lane * 4u);
|
||||
float v[4] = { r.x, r.y, r.z, r.w };
|
||||
if constexpr (!QUANT) {
|
||||
// f16 holds +-65504; a rotated post-norm row is orders below that, and the clamp keeps an
|
||||
// outlier finite instead of inf
|
||||
union { __half h[4]; uint2 u; } o;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 4; ++j) o.h[j] = __float2half(fminf(fmaxf(v[j], -65504.0f), 65504.0f));
|
||||
*(uint2*)((uint16_t*)q + (size_t)row * q_ld + (size_t)part * K + c0 + lane * 4u) = o.u;
|
||||
return;
|
||||
}
|
||||
float m = 0.0f;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 4; ++j) m = fmaxf(m, fabsf(v[j]));
|
||||
@@ -147,8 +160,8 @@ __global__ __launch_bounds__(kThreads) void givens_quant_act_fp8_kernel(
|
||||
// s [M][parts*K/128] f32
|
||||
static int givens_quant_launch(long x, long pairs, long angle, long rscale, long q, long s, int M,
|
||||
int K, int parts, int stages, int x_ld, int q_ld, int s_ld,
|
||||
const R4dSink& sk) {
|
||||
if (!x || !pairs || !angle || !rscale || !q || !s) return -1;
|
||||
const R4dSink& sk, bool quant = true) {
|
||||
if (!x || !pairs || !angle || !rscale || !q || (quant && !s)) return -1;
|
||||
if (M <= 0 || K <= 0 || parts <= 0 || stages <= 0) return -2;
|
||||
if (K % (int)kGroup) return -3; // a rotation group cannot straddle the end of a row
|
||||
if (!x_ld) x_ld = K;
|
||||
@@ -156,11 +169,17 @@ static int givens_quant_launch(long x, long pairs, long angle, long rscale, long
|
||||
if (!q_ld) q_ld = parts * K;
|
||||
if (!s_ld) s_ld = parts * nkb;
|
||||
const uint32_t gy = ((uint32_t)(parts * nkb) + kWaves - 1) / kWaves;
|
||||
const int rc = R4D_EMIT(sk, givens_quant_act_fp8_kernel, dim3((uint32_t)M, gy), dim3(kThreads), 0,
|
||||
(const uint16_t*)x, (const int32_t*)pairs, (const float*)angle,
|
||||
(const float*)rscale, (uint8_t*)q, (float*)s, (uint32_t)K,
|
||||
(uint32_t)nkb, (uint32_t)parts, (uint32_t)stages, (uint32_t)x_ld,
|
||||
(uint32_t)q_ld, (uint32_t)s_ld);
|
||||
const int rc = quant
|
||||
? R4D_EMIT(sk, givens_quant_act_fp8_kernel<true>, dim3((uint32_t)M, gy), dim3(kThreads), 0,
|
||||
(const uint16_t*)x, (const int32_t*)pairs, (const float*)angle,
|
||||
(const float*)rscale, (uint8_t*)q, (float*)s, (uint32_t)K,
|
||||
(uint32_t)nkb, (uint32_t)parts, (uint32_t)stages, (uint32_t)x_ld,
|
||||
(uint32_t)q_ld, (uint32_t)s_ld)
|
||||
: R4D_EMIT(sk, givens_quant_act_fp8_kernel<false>, dim3((uint32_t)M, gy), dim3(kThreads), 0,
|
||||
(const uint16_t*)x, (const int32_t*)pairs, (const float*)angle,
|
||||
(const float*)rscale, (uint8_t*)q, (float*)s, (uint32_t)K,
|
||||
(uint32_t)nkb, (uint32_t)parts, (uint32_t)stages, (uint32_t)x_ld,
|
||||
(uint32_t)q_ld, (uint32_t)s_ld);
|
||||
if (rc != RAD_OK) return -4;
|
||||
return (!sk.desc && hipGetLastError() != hipSuccess) ? -4 : 0;
|
||||
}
|
||||
@@ -170,16 +189,17 @@ extern "C" int r4d_givens_quant_act_fp8_group(void) { return (int)kGroup; }
|
||||
// ============================================================== the plugin ABI entry point
|
||||
enum { GV_X = 0, GV_PAIRS = 1, GV_ANGLE = 2, GV_RSCALE = 3, GV_Q = 4, GV_S = 5 };
|
||||
|
||||
static int rad_givens_quant_act_fp8_body(const RadArgs* a, const R4dSink& sk) {
|
||||
static int givens_body(const RadArgs* a, const R4dSink& sk, bool quant) {
|
||||
const RadTensor* x = r4d_opt(a, GV_X);
|
||||
const RadTensor* pr = r4d_opt(a, GV_PAIRS);
|
||||
const RadTensor* an = r4d_opt(a, GV_ANGLE);
|
||||
const RadTensor* rs = r4d_opt(a, GV_RSCALE);
|
||||
const RadTensor* q = r4d_opt(a, GV_Q);
|
||||
const RadTensor* sc = r4d_opt(a, GV_S);
|
||||
if (!x || !pr || !an || !rs || !q || !sc) return R4D_NO(RAD_E_INVAL);
|
||||
if (x->rank < 2 || q->rank != 2 || sc->rank != 2 || pr->rank != 2 || an->rank != 2 ||
|
||||
const RadTensor* sc = quant ? r4d_opt(a, GV_S) : nullptr;
|
||||
if (!x || !pr || !an || !rs || !q || (quant && !sc)) return R4D_NO(RAD_E_INVAL);
|
||||
if (x->rank < 2 || q->rank != 2 || (quant && sc->rank != 2) || pr->rank != 2 || an->rank != 2 ||
|
||||
rs->rank != 2) return R4D_NO(RAD_E_SHAPE);
|
||||
if (!quant && q->dtype != RAD_F16) return R4D_NO(RAD_E_DTYPE);
|
||||
if (pr->dtype != RAD_I32 || an->dtype != RAD_F32 || rs->dtype != RAD_F32)
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
// rows are dim 0 and everything after it is the row, as quant_act_fp8 reads them
|
||||
@@ -189,18 +209,25 @@ static int rad_givens_quant_act_fp8_body(const RadArgs* a, const R4dSink& sk) {
|
||||
if (r4d_geti_or(a, "group", grp) != grp || K % grp || parts < 1 || stages < 1)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
if (q->shape[0] != M || q->shape[1] != parts * K) return R4D_NO(RAD_E_SHAPE);
|
||||
if (sc->shape[0] != M || sc->shape[1] != parts * nkb) return R4D_NO(RAD_E_SHAPE);
|
||||
if (quant && (sc->shape[0] != M || sc->shape[1] != parts * nkb)) return R4D_NO(RAD_E_SHAPE);
|
||||
// the tables are the weight's, contiguous: parts * stages rows of K pairs and K/2 angles
|
||||
if (pr->shape[0] != parts * stages || pr->shape[1] != K) return R4D_NO(RAD_E_SHAPE);
|
||||
if (an->shape[0] != parts * stages || an->shape[1] != K / 2) return R4D_NO(RAD_E_SHAPE);
|
||||
if (rs->shape[0] != parts || rs->shape[1] != K) return R4D_NO(RAD_E_SHAPE);
|
||||
if (!r4d_aligned(rs->data, 16) || !r4d_aligned(pr->data, 8) || !r4d_aligned(q->data, 4))
|
||||
return R4D_NO(RAD_E_ALIGN);
|
||||
const long long xp = r4d_row_pitch(x), qp = r4d_row_pitch(q), sp = r4d_row_pitch(sc);
|
||||
const long long xp = r4d_row_pitch(x), qp = r4d_row_pitch(q), sp = quant ? r4d_row_pitch(sc) : 0;
|
||||
if (xp < 0 || qp < 0 || sp < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
const int e = givens_quant_launch(r4d_addr(x), r4d_addr(pr), r4d_addr(an), r4d_addr(rs),
|
||||
r4d_addr(q), r4d_addr(sc), (int)M, (int)K, (int)parts,
|
||||
(int)stages, (int)xp, (int)qp, (int)sp, sk);
|
||||
(int)stages, (int)xp, (int)qp, (int)sp, sk, quant);
|
||||
return e == 0 ? RAD_OK : RAD_E_SHAPE;
|
||||
}
|
||||
static int rad_givens_quant_act_fp8_body(const RadArgs* a, const R4dSink& sk) {
|
||||
return givens_body(a, sk, true);
|
||||
}
|
||||
static int rad_givens_rotate_body(const RadArgs* a, const R4dSink& sk) {
|
||||
return givens_body(a, sk, false);
|
||||
}
|
||||
R4D_ENTRY(r4d_rad_givens_quant_act_fp8, rad_givens_quant_act_fp8_body)
|
||||
R4D_ENTRY(r4d_rad_givens_rotate, rad_givens_rotate_body)
|
||||
+95
-1
@@ -342,6 +342,26 @@ void sz_read(const WGeom& g, const uint32_t* sz, const RadTensor* pl, bool asym)
|
||||
}
|
||||
}
|
||||
|
||||
/* A ROTATED asymmetric grid (ParoQuant): codes of `codes` dtype, an f16 scale and a u8 zero point a
|
||||
* group, then a `givens` transform and its tables -- the same bytes as the unrotated grid, the
|
||||
* rotation being the activation's. */
|
||||
static bool paro_asym_enc(const RadEncoding* e, uint32_t codes, long long group) {
|
||||
if (!e || !rad_enc_is(e, "affine") || e->n_planes < 3) return false;
|
||||
const bool giv = std::strncmp(e->transform, "givens", RAD_ENC_STR) == 0;
|
||||
if (!giv && e->transform[0]) return false;
|
||||
if (!giv && e->n_planes != 3) return false;
|
||||
if (giv && (!rad_enc_valid(e) || rad_enc_find(e, "t.pairs") < 0)) return false;
|
||||
if (std::strncmp(e->plane[0].role, "codes", RAD_ENC_STR) != 0 || e->plane[0].dtype != codes ||
|
||||
e->plane[0].block[0] != 1 || e->plane[0].block[1] != 1) return false;
|
||||
const int s = rad_enc_find(e, "scale"), z = rad_enc_find(e, "zero");
|
||||
if (s != 1 || z != 2) return false;
|
||||
return e->plane[s].dtype == RAD_F16 && e->plane[s].kind == RAD_PLANE_TILED &&
|
||||
e->plane[s].block[0] == 1 && e->plane[s].block[1] == group &&
|
||||
(e->plane[z].dtype == RAD_U8 || e->plane[z].dtype == codes) &&
|
||||
e->plane[z].kind == RAD_PLANE_TILED && e->plane[z].block[0] == 1 &&
|
||||
e->plane[z].block[1] == group;
|
||||
}
|
||||
|
||||
/* ---- w4: a symmetric grid is i4 codes with an f16 scale; `_asym` is u4 codes with a zero. */
|
||||
int w4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out, WGeom* gout) {
|
||||
@@ -352,7 +372,8 @@ int w4_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en
|
||||
if (g.K % R4D_W4_KPB) return RAD_E_SHAPE;
|
||||
const bool asym = w4_asym(p, n_p);
|
||||
const bool ok = asym ? (r4d_enc::affine(enc, RAD_U4, RAD_F16, 1, g.group, RAD_U8) ||
|
||||
r4d_enc::affine(enc, RAD_U4, RAD_F16, 1, g.group, RAD_U4))
|
||||
r4d_enc::affine(enc, RAD_U4, RAD_F16, 1, g.group, RAD_U4) ||
|
||||
paro_asym_enc(enc, RAD_U4, g.group))
|
||||
: r4d_enc::affine(enc, RAD_I4, RAD_F16, 1, g.group);
|
||||
if (!ok) return RAD_E_DTYPE;
|
||||
if (gout) *gout = g;
|
||||
@@ -437,6 +458,79 @@ extern "C" int r4d_rad_unrelayout_w4(const RadParam* p, int n_p, int operand,
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ---- w5: ParoQuant's int5, asymmetric: u5 codes, an f16 scale and a u8 zero point (0..31) a 128 of K.
|
||||
* The blob is w4's fragment order for the low four bits of every code (offset binary, no sign flip)
|
||||
* followed by a plane of 32-bit words holding the fifth bits, one a (n-tile, 64 of K, lane): byte s
|
||||
* is k step s of the block and bit e the fragment element e -- the element whose nibble is
|
||||
* 2e (e < 4) or 2(e-4)+1 in dword s of that lane's 16 bytes (r4d_gemm_w5a16_nt_m64.hip). The scale
|
||||
* operand is w4u's (scale, zero) dword plane. A ROTATED weight (a `givens` transform and its
|
||||
* tables beside the first three planes) is the same bytes: the rotation is the activation's. */
|
||||
namespace {
|
||||
bool w5_enc(const RadEncoding* e, long long group) { return paro_asym_enc(e, RAD_U5, group); }
|
||||
|
||||
int w5_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out, WGeom* gout) {
|
||||
if (!out || !pl) return RAD_E_INVAL;
|
||||
WGeom g;
|
||||
int rc = wgeom(p, n_p, R4D_W4_GROUP, &g);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (g.K % R4D_W4_KPB) return RAD_E_SHAPE;
|
||||
if (!w5_enc(enc, g.group)) return RAD_E_DTYPE;
|
||||
if (gout) *gout = g;
|
||||
if (operand == Q_BSCALE)
|
||||
return sz_layout(g, enc, sel, pl, n, true, "r4d.w5.sz.g128", R4D_DT_W4_SZ, 4, out);
|
||||
if (operand != Q_B) return RAD_E_UNSUPPORTED;
|
||||
if (!selects(enc, sel, n, "codes") || !dims(pl[0], g.N, g.K)) return RAD_E_SHAPE;
|
||||
fill(out, "r4d.w5.frag.g128", R4D_DT_W4_FRAG, g.N, g.K, g.N * g.K * 5 / 8);
|
||||
return RAD_OK;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
extern "C" int r4d_rad_layout_w5(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* pl, int n, RadLayout* out) {
|
||||
return w5_enc_layout(p, n_p, operand, enc, sel, pl, n, out, nullptr);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_relayout_w5(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* pl, int n, void* dst,
|
||||
int64_t dst_bytes) {
|
||||
RadLayout L;
|
||||
WGeom g;
|
||||
const int rc = w5_enc_layout(p, n_p, operand, enc, sel, pl, n, &L, &g);
|
||||
if (rc != RAD_OK) return rc;
|
||||
if (dst_bytes != L.bytes) return RAD_E_SHAPE;
|
||||
std::memset(dst, 0, (size_t)dst_bytes);
|
||||
if (operand == Q_BSCALE) {
|
||||
sz_write(g, pl, -1, (uint32_t*)dst);
|
||||
return RAD_OK;
|
||||
}
|
||||
uint32_t* wq = (uint32_t*)dst;
|
||||
uint32_t* hi = wq + g.N * g.K / 8; /* after the N*K/2 bytes of nibbles */
|
||||
const long long kpb = g.K / R4D_W4_KPB;
|
||||
const long long rb = (g.K * 5 + 7) / 8;
|
||||
for (long long nn = 0; nn < g.N; ++nn) {
|
||||
const uint8_t* row = pdata(pl[0]) + nn * rb;
|
||||
for (long long k = 0; k < g.K; ++k) {
|
||||
const long long bit = k * 5;
|
||||
uint32_t v = row[bit >> 3];
|
||||
if ((bit & 7) > 3) v |= (uint32_t)row[(bit >> 3) + 1] << 8;
|
||||
const uint32_t q = (v >> (bit & 7)) & 31u;
|
||||
long long w;
|
||||
int nib;
|
||||
w4_slot(g, nn, k, &w, &nib);
|
||||
wq[w] |= (q & 15u) << (4 * nib);
|
||||
/* the fifth bit: word (t, kb, lane) = w / 4 (w4_slot's dword index is that * 4 + s);
|
||||
* byte s = w % 4, bit e from the nibble position */
|
||||
const long long hw = w / 4;
|
||||
const int s = (int)(w % 4);
|
||||
const int e = nib % 2 == 0 ? nib / 2 : 4 + nib / 2; /* nibble 2e / 2(e-4)+1 -> e */
|
||||
hi[hw] |= (q >> 4) << (8 * s + e);
|
||||
}
|
||||
}
|
||||
(void)kpb;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ---- w8: i8 codes, symmetric about zero, an f16 scale a group. The f16 kernel (a16) reads the
|
||||
* code offset binary with zero 128 in the shared dword; the int8 kernel (a8) reads it signed with a
|
||||
* bare f16 scale plane. */
|
||||
|
||||
@@ -216,6 +216,8 @@ int r4d_rad_gemm_bf16_m16 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_bf16_m64 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_bf16_tiled (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_w4a16_m64 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_w5a16_m64 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_w4a16asym_m64(const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_w8a16_m64 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_w4a8_m64 (const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_w4a8_tiled (const RadArgs*, RadStream);
|
||||
@@ -319,6 +321,8 @@ int r4d_rad_quant_act_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_quant_act_fp8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_had_quant_act_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_had_quant_act_fp8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_givens_rotate (const RadArgs*, RadStream);
|
||||
int r4d_rad_givens_rotate_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_givens_quant_act_fp8 (const RadArgs*, RadStream);
|
||||
int r4d_rad_givens_quant_act_fp8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
int r4d_rad_rmsnorm_quant_fp8(const RadArgs*, RadStream);
|
||||
@@ -387,6 +391,10 @@ int r4d_rad_relayout_w8a16(const RadParam*, int, int, const RadEncoding*, const
|
||||
const RadTensor*, int, void*, int64_t);
|
||||
int r4d_rad_relayout_w8a8(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
const RadTensor*, int, void*, int64_t);
|
||||
int r4d_rad_layout_w5(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
const RadTensor*, int, RadLayout*);
|
||||
int r4d_rad_relayout_w5(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
const RadTensor*, int, void*, int64_t);
|
||||
int r4d_rad_layout_mxfp4(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
const RadTensor*, int, RadLayout*);
|
||||
int r4d_rad_relayout_mxfp4(const RadParam*, int, int, const RadEncoding*, const int*,
|
||||
|
||||
@@ -1062,6 +1062,13 @@ static const RadParamSpec pGivensQuantAct[] = {
|
||||
{ "stages", RAD_P_INT, RAD_REQUIRED },
|
||||
{ "parts", RAD_P_INT, RAD_REQUIRED },
|
||||
};
|
||||
static const RadOperandSpec oGivensRotate[] = {
|
||||
{ "x", RAD_OPD_IN, 0 },
|
||||
{ "pairs", RAD_OPD_WEIGHT, 0 },
|
||||
{ "angle", RAD_OPD_WEIGHT, 0 },
|
||||
{ "rscale", RAD_OPD_WEIGHT, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadOperandSpec oGivensQuantAct[] = {
|
||||
{ "x", RAD_OPD_IN, 0 },
|
||||
{ "pairs", RAD_OPD_WEIGHT, 0 },
|
||||
@@ -2324,6 +2331,10 @@ static const RadOpSchema kSchemas[] = {
|
||||
SCHEMA("had_quant_act_i8g", pQuantActFp8, oQuantActFp8,
|
||||
"quant_act_i8g with a `group`-wide Hadamard along K first; pairs with an i8 weight stored "
|
||||
"rotated, its scale carrying the residual factor of `group`."),
|
||||
SCHEMA("givens_rotate", pGivensQuantAct, oGivensRotate,
|
||||
"givens_quant_act_fp8 without the quantiser: y = x / rscale rotated by `stages` rounds of "
|
||||
"Givens pairs, `parts` copies side by side ([M, parts * n]) in y's own dtype. For a weight "
|
||||
"whose GEMM reads a 16-bit activation. `group` is the width a pair stays inside."),
|
||||
SCHEMA("givens_quant_act_fp8", pGivensQuantAct, oGivensQuantAct,
|
||||
"ParoQuant's activation quantiser: x is divided by the channel scale `rscale`, rotated by "
|
||||
"`stages` rounds of disjoint Givens pairs (a' = a cos t + b sin t, b' = b cos t - a sin t, "
|
||||
@@ -2926,6 +2937,13 @@ static const RadConstraint cGemmNtTiled[] = {
|
||||
static const RadConstraint cGemmNtW4[] = {
|
||||
C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w4a16"),
|
||||
};
|
||||
/* w5a16 serves any M: the dispatcher takes rows past 64 as chunks of 64. */
|
||||
static const RadConstraint cGemmNtW5[] = {
|
||||
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w5a16"),
|
||||
};
|
||||
static const RadConstraint cGemmNtW4Asym[] = {
|
||||
C_GE("M", 1), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w4a16_asym"),
|
||||
};
|
||||
static const RadConstraint cGemmNtW2A8[] = {
|
||||
C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "w2a8"),
|
||||
};
|
||||
@@ -4672,6 +4690,29 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_gemm_w4a16_m64, nullptr, r4d_rad_layout_w4, r4d_rad_relayout_w4,
|
||||
nullptr, r4d_rad_unrelayout_w4 },
|
||||
|
||||
{ "gemm_w5a16_nt_m64", "gemm_nt_q", "gemm",
|
||||
"skinny GEMM with a 5-bit asymmetric weight (ParoQuant int5), 16x16x16 f16 WMMA, split-K reduced "
|
||||
"in LDS; rows past 64 in chunks of 64, a partitioned weight (pb1/pb2) one launch a part",
|
||||
"N a multiple of 16; K divisible by SK*128; weight pre-permuted into fragment order, a nibble "
|
||||
"plane then a fifth-bit plane",
|
||||
"f16 A (parts side by side), 5-bit W with an f16 scale and integer zero per 128 K, bf16 C, fp32 "
|
||||
"accumulate",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
|
||||
ROW(cGemmNtW5), ROW(tGemmSkinny), r4d_tune_valid_gemm,
|
||||
nullptr, nullptr, r4d_rad_gemm_w5a16_m64, nullptr, r4d_rad_layout_w5, r4d_rad_relayout_w5,
|
||||
nullptr, nullptr },
|
||||
|
||||
{ "gemm_w4a16_asym_nt_m64", "gemm_nt_q", "gemm",
|
||||
"skinny GEMM with a 4-bit ASYMMETRIC weight (z-lab's ParoQuant int4), the 5-bit row's kernel with "
|
||||
"no fifth-bit plane; rows past 64 in chunks of 64, a partitioned weight (pb1/pb2) one launch a part",
|
||||
"N a multiple of 16; K divisible by SK*128; weight pre-permuted into fragment order, unsigned nibbles",
|
||||
"f16 A (parts side by side), 4-bit W with an f16 scale and integer zero per 128 K, bf16 C, fp32 "
|
||||
"accumulate",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
|
||||
ROW(cGemmNtW4Asym), ROW(tGemmSkinny), r4d_tune_valid_gemm,
|
||||
nullptr, nullptr, r4d_rad_gemm_w4a16asym_m64, nullptr, r4d_rad_layout_w4, r4d_rad_relayout_w4,
|
||||
nullptr, nullptr },
|
||||
|
||||
{ "gemm_w8a16_nt_m64", "gemm_nt_q", "gemm",
|
||||
"skinny GEMM with an 8-bit weight, 16x16x16 WMMA, split-K reduced in LDS",
|
||||
"M <= 64; N a multiple of 16; K divisible by SK*128; weight pre-permuted into fragment order, "
|
||||
@@ -5169,6 +5210,16 @@ static const RadKernelInfo kKernels[] = {
|
||||
r4d_rad_relayout_givens_pairs, nullptr, r4d_rad_unrelayout_givens_pairs, nullptr,
|
||||
r4d_rad_givens_quant_act_fp8_describe },
|
||||
|
||||
{ "givens_rotate", "givens_rotate", "quant",
|
||||
"the channel scale and ParoQuant's Givens rotations, once for each rotated part, to f16",
|
||||
"n a multiple of 128 and group 128: a rotation group cannot straddle the end of a row",
|
||||
"bf16 in, f16 out, parts side by side; the tables are the weight's int32 pairs, f32 angles and "
|
||||
"f32 channel scales",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cHadQuantActFp8), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_givens_rotate, nullptr, nullptr, nullptr,
|
||||
nullptr, nullptr, nullptr, r4d_rad_givens_rotate_describe },
|
||||
|
||||
{ "quant_act_i8g", "quant_act_i8g", "quant",
|
||||
"per-(row, 128-column) symmetric int8 quantisation of a bf16 activation",
|
||||
"quant_act_fp8's: n a multiple of 4, group 128, a ragged last group written zero-padded",
|
||||
|
||||
+142
-6
@@ -3740,8 +3740,9 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
|
||||
* kernel's scale must match it to the rounding of its f32 sincos, and every dequantised element
|
||||
* must sit within the E4M3 grid of the reference (half a step at the top of the range is 1/16 of
|
||||
* the group's maximum, so 7% of it is a bound a wrong pair or a wrong angle cannot meet). */
|
||||
static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, int stages) {
|
||||
const char* row = "givens_quant_act_fp8";
|
||||
static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, int stages,
|
||||
bool rot_only = false) {
|
||||
const char* row = rot_only ? "givens_rotate" : "givens_quant_act_fp8";
|
||||
if (!sel(row)) return;
|
||||
char nm[160];
|
||||
std::snprintf(nm, sizeof nm, "%s numeric M=%lld K=%lld parts=%d stages=%d", row, (long long)M,
|
||||
@@ -3781,7 +3782,7 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
|
||||
void* d_p = dalloc(pairs.size() * 4);
|
||||
void* d_a = dalloc(ang.size() * 4);
|
||||
void* d_r = dalloc(rs.size() * 4);
|
||||
void* d_q = dalloc((size_t)(M * parts * K));
|
||||
void* d_q = dalloc((size_t)(M * parts * K) * (rot_only ? 2 : 1));
|
||||
void* d_s = dalloc((size_t)(M * parts * ng) * 4);
|
||||
if (!d_x || !d_p || !d_a || !d_r || !d_q || !d_s) { ok(nm, false, "hipMalloc failed"); return; }
|
||||
st_memcpy(d_x, hx.data(), hx.size() * 2, hipMemcpyHostToDevice);
|
||||
@@ -3790,9 +3791,11 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
|
||||
st_memcpy(d_r, rs.data(), rs.size() * 4, hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_x, RAD_BF16, { M, K }), T(d_p, RAD_I32, { NPS, K }), T(d_a, RAD_F32, { NPS, K / 2 }),
|
||||
T(d_r, RAD_F32, { (int64_t)parts, K }), T(d_q, RAD_F8E4M3, { M, parts * K }),
|
||||
T(d_r, RAD_F32, { (int64_t)parts, K }),
|
||||
T(d_q, rot_only ? (uint32_t)RAD_F16 : (uint32_t)RAD_F8E4M3, { M, parts * K }),
|
||||
T(d_s, RAD_F32, { M, parts * ng }),
|
||||
};
|
||||
if (rot_only) ts.pop_back();
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("n", K), PI("group", 128), PS("dtype", "bf16"),
|
||||
PI("stages", stages), PI("parts", parts) };
|
||||
RadArgs a;
|
||||
@@ -3802,7 +3805,7 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
|
||||
a.world_size = 1;
|
||||
const int rc = r->launch(&a, (RadStream) nullptr);
|
||||
if (rc != RAD_OK || st_stream_sync(nullptr) != hipSuccess) { ok(nm, false, "rc=" + std::to_string(rc)); return; }
|
||||
std::vector<uint8_t> hq((size_t)(M * parts * K));
|
||||
std::vector<uint8_t> hq((size_t)(M * parts * K) * (rot_only ? 2 : 1));
|
||||
std::vector<float> hs((size_t)(M * parts * ng));
|
||||
st_memcpy(hq.data(), d_q, hq.size(), hipMemcpyDeviceToHost);
|
||||
st_memcpy(hs.data(), d_s, hs.size() * 4, hipMemcpyDeviceToHost);
|
||||
@@ -3824,6 +3827,13 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
|
||||
for (int64_t g = 0; g < ng; ++g) {
|
||||
double amax = 0;
|
||||
for (int k = 0; k < G; ++k) amax = std::max(amax, std::fabs(b[(size_t)(g * G + k)]));
|
||||
if (rot_only) { // f16: relative 2^-11 of the value, 1e-3 of the group max
|
||||
for (int k = 0; k < G; ++k) {
|
||||
const double y = rad_load_f32(hq.data(), RAD_F16, i * parts * K + p * K + g * G + k);
|
||||
worst_val = std::max(worst_val, std::fabs(y - b[(size_t)(g * G + k)]) / (amax + 1e-30));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const double sc = hs[(size_t)(i * parts * ng + p * ng + g)];
|
||||
worst_scale = std::max(worst_scale, std::fabs(sc - amax / 448.0) / (amax / 448.0 + 1e-30));
|
||||
for (int k = 0; k < G; ++k) {
|
||||
@@ -3834,7 +3844,115 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
|
||||
}
|
||||
char d[96];
|
||||
std::snprintf(d, sizeof d, "scale rel %.2e, value/amax %.3f", worst_scale, worst_val);
|
||||
ok(nm, worst_scale < 1e-4 && worst_val < 0.07, d);
|
||||
ok(nm, rot_only ? worst_val < 1e-3 : (worst_scale < 1e-4 && worst_val < 0.07), d);
|
||||
}
|
||||
|
||||
|
||||
/* PARO'S 5-BIT SKINNY GEMM against what the planes mean: u5 codes (a bit stream along K), an f16 scale
|
||||
* and a zero point 0..31 a 128 of K, an f16 activation, the weight stored through the row's hooks.
|
||||
* `pb1`/`pb2` give a partitioned weight (the activation copies side by side). M past 64 runs the
|
||||
* dispatcher's 64-row chunks. */
|
||||
static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int64_t pb1 = 0,
|
||||
int64_t pb2 = 0) {
|
||||
const char* row = "gemm_w5a16_nt_m64";
|
||||
if (!sel(row)) return;
|
||||
const int64_t e1 = pb1 > 0 ? std::min(pb1, N) : N;
|
||||
const int64_t e2 = pb2 > 0 ? std::max(e1, std::min(pb2, N)) : N;
|
||||
const int parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
|
||||
auto part_of = [&](int64_t n) { return (int)(n >= e1) + (int)(n >= e2); };
|
||||
char nm[160];
|
||||
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d", row, (long long)M,
|
||||
(long long)N, (long long)K, parts);
|
||||
const RadKernelInfo* r = find(pl, row);
|
||||
if (!r || !r->layout || !r->relayout) { ok(nm, false, "row or hooks absent"); return; }
|
||||
const int64_t G = K / 128;
|
||||
Planes p;
|
||||
p.enc = rad_enc_affine(RAD_U5, RAD_F16, 1, 128);
|
||||
rad_enc_add_plane(&p.enc, "zero", RAD_U8, 1, 128);
|
||||
p.size(N, K);
|
||||
const int ic = p.find("codes"), is = p.find("scale"), iz = p.find("zero");
|
||||
const int64_t rb = p.row_bytes(ic);
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
for (int64_t k = 0; k < K; ++k) {
|
||||
const uint32_t q = st_mix32((uint32_t)(n * K + k) ^ 0x5eedu) & 31u;
|
||||
const int64_t bit = k * 5;
|
||||
uint8_t* b = p.data[(size_t)ic].data() + n * rb + (bit >> 3);
|
||||
const uint32_t v = q << (bit & 7);
|
||||
b[0] |= (uint8_t)(v & 0xFF);
|
||||
if ((bit & 7) > 3) b[1] |= (uint8_t)(v >> 8);
|
||||
}
|
||||
for (int64_t g = 0; g < G; ++g) {
|
||||
const uint32_t h = st_mix32((uint32_t)(n * G + g) ^ 0x2545u);
|
||||
const float sc = 0.002f * (float)(1 + h % 40);
|
||||
rad_store_f32(p.data[(size_t)is].data(), RAD_F16, n * G + g, sc);
|
||||
p.data[(size_t)iz][(size_t)(n * G + g)] = (uint8_t)(8 + (h >> 8) % 20);
|
||||
}
|
||||
}
|
||||
std::vector<RadParam> lp = { PI("N", N), PI("K", K), PI("group", 128) };
|
||||
std::vector<uint8_t> wq, ws;
|
||||
const int rw = stored(r, lp, 2, p, { "codes" }, &wq);
|
||||
const int rs = stored(r, lp, 3, p, { "scale", "zero" }, &ws);
|
||||
if (rw != RAD_OK || rs != RAD_OK) {
|
||||
ok(nm, false, "relayout rc " + std::to_string(rw) + "/" + std::to_string(rs));
|
||||
return;
|
||||
}
|
||||
const int64_t PK = parts * K;
|
||||
std::vector<uint16_t> ha((size_t)(M * PK));
|
||||
std::vector<float> av((size_t)(M * PK));
|
||||
for (int64_t i = 0; i < M; ++i)
|
||||
for (int64_t k = 0; k < PK; ++k) {
|
||||
const uint32_t h = st_mix32((uint32_t)(i * 0x10001 + k) ^ 0x3cu);
|
||||
const float v = (float)((int)(h % 401) - 200) * 0.01f;
|
||||
rad_store_f32(&ha[(size_t)(i * PK + k)], RAD_F16, 0, v);
|
||||
av[(size_t)(i * PK + k)] = rad_load_f32(&ha[(size_t)(i * PK + k)], RAD_F16, 0);
|
||||
}
|
||||
std::vector<uint16_t> hy((size_t)(M * N), 0);
|
||||
void* d_a = dalloc(ha.size() * 2);
|
||||
void* d_w = dalloc(wq.size());
|
||||
void* d_s = dalloc(ws.size());
|
||||
void* d_y = dalloc(hy.size() * 2);
|
||||
if (!d_a || !d_w || !d_s || !d_y) { ok(nm, false, "hipMalloc failed"); return; }
|
||||
st_memcpy(d_a, ha.data(), ha.size() * 2, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_w, wq.data(), wq.size(), hipMemcpyHostToDevice);
|
||||
st_memcpy(d_s, ws.data(), ws.size(), hipMemcpyHostToDevice);
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_a, RAD_F16, { M, PK }), T(nullptr, RAD_F32, { 1 }),
|
||||
T(d_w, RAD_U8, { (int64_t)wq.size() }), T(d_s, RAD_U8, { (int64_t)ws.size() }),
|
||||
T(d_y, RAD_BF16, { M, N }), T(nullptr, RAD_F32, { 1 }), T(nullptr, RAD_F32, { 1 }),
|
||||
};
|
||||
absent(ts[1]); absent(ts[5]); absent(ts[6]);
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("N", N), PI("K", K), PI("group", 128),
|
||||
PS("dtype", "w5a16") };
|
||||
if (pb1) ps.push_back(PI("pb1", pb1));
|
||||
if (pb2) ps.push_back(PI("pb2", pb2));
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
a.t = ts.data(); a.n_t = (int)ts.size();
|
||||
a.p = ps.data(); a.n_p = (int)ps.size();
|
||||
a.world_size = 1;
|
||||
const int rc = r->launch(&a, (RadStream) nullptr);
|
||||
if (rc != RAD_OK || st_stream_sync(nullptr) != hipSuccess) { ok(nm, false, "rc=" + std::to_string(rc)); return; }
|
||||
st_memcpy(hy.data(), d_y, hy.size() * 2, hipMemcpyDeviceToHost);
|
||||
const int64_t nstep = N > 96 ? N / 96 : 1;
|
||||
double num = 0, den = 0;
|
||||
for (int64_t i = 0; i < M; ++i)
|
||||
for (int64_t n = 0; n < N; n = (n + 1 == N) ? N : std::min(n + nstep, N - 1)) {
|
||||
const int pt = part_of(n);
|
||||
double acc = 0;
|
||||
for (int64_t k = 0; k < K; ++k) {
|
||||
const double q = rad_load_f32(p.data[(size_t)ic].data() + n * rb, RAD_U5, k);
|
||||
const double z = p.data[(size_t)iz][(size_t)(n * G + k / 128)];
|
||||
const double sc = rad_load_f32(p.data[(size_t)is].data(), RAD_F16, n * G + k / 128);
|
||||
acc += (double)av[(size_t)(i * PK + pt * K + k)] * (q - z) * sc;
|
||||
}
|
||||
const double d = (double)h_bf16(hy[(size_t)(i * N + n)]) - acc;
|
||||
num += d * d;
|
||||
den += acc * acc;
|
||||
}
|
||||
const double rel = den > 0 ? std::sqrt(num / den) : std::sqrt(num);
|
||||
char d[64];
|
||||
std::snprintf(d, sizeof d, "rel_l2 %.3e", rel);
|
||||
ok(nm, rel < 1e-2, d);
|
||||
}
|
||||
|
||||
|
||||
@@ -12777,6 +12895,24 @@ static void device_phase(const Plugin& pl) {
|
||||
moe_w4_rot(pl);
|
||||
}
|
||||
|
||||
if (sel("gemm_w5a16_nt_m64")) {
|
||||
if (!g_case) std::printf("\nParoQuant's 5-bit GEMM, against the planes' meaning\n");
|
||||
w5a16_numeric(pl, 8, 272, 640);
|
||||
w5a16_numeric(pl, 64, 400, 2560);
|
||||
w5a16_numeric(pl, 5, 400, 640, 144);
|
||||
w5a16_numeric(pl, 40, 512, 2560, 128, 384);
|
||||
w5a16_numeric(pl, 300, 400, 640, 192);
|
||||
w5a16_numeric(pl, 4, 34816, 5120, 17408);
|
||||
w5a16_numeric(pl, 150, 5120, 17408);
|
||||
}
|
||||
|
||||
if (sel("givens_rotate")) {
|
||||
if (!g_case) std::printf("\nParoQuant's rotation to f16, against an f64 rotation\n");
|
||||
givens_numeric(pl, 3, 5120, 1, 8, true);
|
||||
givens_numeric(pl, 5, 5120, 2, 8, true);
|
||||
givens_numeric(pl, 300, 6144, 2, 8, true);
|
||||
}
|
||||
|
||||
if (sel("givens_quant_act_fp8")) {
|
||||
if (!g_case) std::printf("\nParoQuant's rotate-and-quantise, against an f64 rotation\n");
|
||||
givens_numeric(pl, 1, 128, 1, 8);
|
||||
|
||||
Reference in new issue
Block a user