Archived
gemm_mxfp4a8_gated_gemv: a block per gate/up tile pair, the group quantised by its last block
One block a 128-column group left the grid at n / 128 blocks (68 at Qwen per rank on 32 WGPs) and the fold lost to the pair it replaces. Now the gemv row grid and K order, the bf16 product and row amaxes through the op scratch, an arrival counter per group keyed on the output. Byte for byte the old fold (selftest, twice into one output); r4d_selftest launch_row sizes scratch. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a596f68782
commit
cbc299417e
4 files changed
+188
-85
No files matched your search
@@ -30,7 +30,7 @@
|
||||
// shuffles. TN only widens a block; the sum order of a column is fixed by KW, which is constant.
|
||||
//
|
||||
// The column's 2^(ref - 127) is applied once, as in the decode kernel's epilogue. No scratch and no
|
||||
// counters: there is no split across blocks.
|
||||
// counters: there is no split across blocks (the gated fold at the end of the file has both).
|
||||
#include "r4d.h"
|
||||
#include "r4d_gemm_args.h"
|
||||
#include "r4d_common.h"
|
||||
@@ -40,11 +40,15 @@
|
||||
#include <hip/hip_bf16.h>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
#define R4D_MXG_MAX_M 8
|
||||
#define R4D_MXG_WMMA_MAX_M 64 // the WMMA form's bound: four 16-row tiles a wave
|
||||
#define R4D_MXG_KW 8 // waves on a block's K, both forms; a constant, so the sum order is too
|
||||
#define R4D_MXGG_MAX_GROUPS 1024 // the gated fold's counter block: a gated width up to 131072
|
||||
|
||||
typedef unsigned mxg_u4 __attribute__((ext_vector_type(4)));
|
||||
typedef unsigned mxg_u2 __attribute__((ext_vector_type(2)));
|
||||
@@ -352,20 +356,34 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gemv(const RadArgs* a, RadStream s) {
|
||||
// ============================================================== the gated form (gemm_nt_q_gated_mx)
|
||||
//
|
||||
// The gate_up GEMM with gated_quant_fp8's pass in its epilogue, so the [M, 2n] bf16 product is never
|
||||
// written and the quantiser is not a launch of its own. One block a 128-column group of the gated
|
||||
// output (one activation scale group of the down projection's input): sixteen waves, one 16-column
|
||||
// weight tile each -- the group's eight gate tiles (weight rows 128 g ..) and its eight up tiles (rows
|
||||
// n + 128 g ..).
|
||||
// written and the quantiser is not a launch of its own. A block is one 16-column tile of the gated
|
||||
// output: sixteen waves, the WMMA form's eight K waves over the gate tile (weight rows 16 j ..) and
|
||||
// eight over the up tile (rows n + 16 j ..), each tile's partials met in LDS in that form's order.
|
||||
// The block forms silu(gate) * up in bf16 for its sixteen columns and each row's amax over them. The
|
||||
// quantiser's scale is per 128 columns (one activation scale group of the down projection's input),
|
||||
// so the eight blocks of a group leave their products and amaxes in the op's scratch, and the last
|
||||
// to arrive (an agent-scope counter per group, keyed on the output as the decode row's) takes the
|
||||
// group's amax and writes its codes and scales.
|
||||
//
|
||||
// Byte-identical to the WMMA form above followed by gated_quant_fp8. A wave runs, for its tile, what
|
||||
// all R4D_MXG_KW waves of that kernel run: partial v sums chunks v, v + KW, .. in order, and the
|
||||
// partials are added v = 0, 1, .. from zero, as that kernel's epilogue adds its waves' rows of sP.
|
||||
// Only one partial is live at a time, which is why the chunks are taken in that order rather than
|
||||
// in K order. The scaled sum is rounded to bf16 by the same conversion, and from there the arithmetic
|
||||
// is r4d_fused_quant_fp8.hip's gated_quant_fp8_kernel: silu(gate) * up rounded to bf16, the group's
|
||||
// amax, scale amax / 448, codes through q8_pack4. ACT 1 is the GeGLU form: gelu_mul's tanh GELU with
|
||||
// its constants (r4d_model_bf16.hip), the product rounded to bf16 as gelu_mul stores it, then
|
||||
// quant_act_fp8's quantiser, which is the same arithmetic as gated_quant_fp8's.
|
||||
// Why a tile pair a block: one block a 128-column group, each wave running every K partial of one of
|
||||
// its sixteen tiles, left the grid at n / 128 blocks, 68 at Qwen's per-rank gate_up on 32 WGPs, so a
|
||||
// few WGPs ran a third block while the rest idled, and the fold (97..101 us) lost to the gemv row and
|
||||
// gated_quant_fp8 it replaces (78 + 7 us). Splitting K across a group's blocks instead moved an fp32
|
||||
// partial of the whole product per split through memory (4.4 MB at eight rows; 106 us). A tile pair
|
||||
// a block is the gemv row's own grid and K order; what crosses blocks is the bf16 product and the
|
||||
// amaxes, M * n * 2 bytes and n / 16 * M floats: Qwen 78..81 us, TranslateGemma 99 (as before), at
|
||||
// TP1 148 against the pair's 152 (r4d_selftest --perfmx). End to end the gain is smaller, since a
|
||||
// replayed step overlaps the old fold's idle WGPs with other work: Qwen without speculation x1 / x4 /
|
||||
// x8 +1.4 / +1.8 / +1.5 % (three alternating rounds).
|
||||
//
|
||||
// Byte-identical to the WMMA form above followed by gated_quant_fp8. Wave v of a tile runs what wave
|
||||
// v of that form runs (partial v sums chunks v, v + KW, .. in order), and the partials are added
|
||||
// v = 0, 1, .. from zero, as that form's epilogue adds its waves' rows of sP. The scaled sum is
|
||||
// rounded to bf16 by the same conversion, and from there the arithmetic is r4d_fused_quant_fp8.hip's
|
||||
// gated_quant_fp8_kernel: silu(gate) * up rounded to bf16, the group's amax (a max, so the blocks'
|
||||
// order does not matter), scale amax / 448, codes through q8_pack4. ACT 1 is the GeGLU form: gelu_mul's
|
||||
// tanh GELU with its constants (r4d_model_bf16.hip), the product rounded to bf16 as gelu_mul stores
|
||||
// it, then quant_act_fp8's quantiser, which is the same arithmetic as gated_quant_fp8's.
|
||||
namespace {
|
||||
__device__ __forceinline__ float mxg_bf2f(uint16_t h) { return __uint_as_float((uint32_t)h << 16); }
|
||||
__device__ __forceinline__ uint16_t mxg_f2bf(float f) { // as r4d_fused_quant_fp8.hip's f2bf
|
||||
@@ -375,106 +393,160 @@ __device__ __forceinline__ uint16_t mxg_f2bf(float f) { // as r4d_fused_quant_
|
||||
} // namespace
|
||||
|
||||
template <bool ABLK, int ACT>
|
||||
__global__ __launch_bounds__(16 * 32)
|
||||
__global__ __launch_bounds__(2 * R4D_MXG_KW * 32)
|
||||
void r4d_gemm_mxfp4a8_gated_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, uint8_t* __restrict__ Q, float* __restrict__ S,
|
||||
uint16_t* __restrict__ Y, int M, int K, int n, int as_rs, int lda, int ldws, int q_ld, int s_ld,
|
||||
int y_ld) {
|
||||
uint16_t* __restrict__ Y, uint16_t* __restrict__ H, float* __restrict__ X,
|
||||
unsigned* __restrict__ cnt, int M, int K, int n, int as_rs, int lda, int ldws, int q_ld,
|
||||
int s_ld, int y_ld) {
|
||||
constexpr int KW = R4D_MXG_KW;
|
||||
__shared__ uint16_t sG[16][R4D_MXG_MAX_M][16]; // bf16 gate (waves 0..7) and up (8..15)
|
||||
__shared__ float sP[2][KW][R4D_MXG_MAX_M][16]; // gate (0) and up (1) partials, a K wave each
|
||||
__shared__ unsigned s_last;
|
||||
const int lane = threadIdx.x & 31;
|
||||
const int wave = __builtin_amdgcn_readfirstlane(threadIdx.x >> 5);
|
||||
const int nt = (wave >> 3) * (n >> 4) + blockIdx.x * 8 + (wave & 7); // this wave's weight tile
|
||||
const int half = wave >> 3, kv = wave & 7; // gate or up, and this wave's K partial
|
||||
const int j = blockIdx.x; // the gated output's 16-column tile
|
||||
const int nt = half * (n >> 4) + j; // this wave's weight tile
|
||||
const int col = lane & 15, khalf = lane >> 4;
|
||||
const int row = col;
|
||||
const bool live = row < M;
|
||||
const int ksteps = K >> 4, chunks = K >> 7;
|
||||
const unsigned* __restrict__ Wp = (const unsigned*)W;
|
||||
const int ref = (int)Wref[nt * 16 + col];
|
||||
mxg_v8f sum = (mxg_v8f)(0.f);
|
||||
for (int v = 0; v < KW; ++v) {
|
||||
mxg_v8f tot = (mxg_v8f)(0.f);
|
||||
for (int ch = v; ch < chunks; ch += KW) {
|
||||
unsigned wv[8];
|
||||
mxg_v8f tot = (mxg_v8f)(0.f);
|
||||
for (int ch = kv; ch < chunks; ch += KW) {
|
||||
unsigned wv[8];
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s)
|
||||
wv[s] = __builtin_nontemporal_load(&Wp[((size_t)nt * ksteps + ch * 8 + s) * 32 + lane]);
|
||||
int e8[4];
|
||||
for (int s = 0; s < 8; ++s)
|
||||
wv[s] = __builtin_nontemporal_load(&Wp[((size_t)nt * ksteps + ch * 8 + s) * 32 + lane]);
|
||||
int e8[4];
|
||||
#pragma unroll
|
||||
for (int q = 0; q < 4; ++q) e8[q] = (int)Ws[(size_t)(ch * 4 + q) * ldws + nt * 16 + col];
|
||||
mxg_v2i af[8];
|
||||
for (int q = 0; q < 4; ++q) e8[q] = (int)Ws[(size_t)(ch * 4 + q) * ldws + nt * 16 + col];
|
||||
mxg_v2i af[8];
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
if (live) {
|
||||
const unsigned* pa = (const unsigned*)(A + (size_t)row * lda + (ch * 8 + s) * 16 + khalf * 8);
|
||||
af[s][0] = (int)pa[0]; af[s][1] = (int)pa[1];
|
||||
} else {
|
||||
af[s][0] = 0; af[s][1] = 0;
|
||||
}
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
if (live) {
|
||||
const unsigned* pa = (const unsigned*)(A + (size_t)row * lda + (ch * 8 + s) * 16 + khalf * 8);
|
||||
af[s][0] = (int)pa[0]; af[s][1] = (int)pa[1];
|
||||
} else {
|
||||
af[s][0] = 0; af[s][1] = 0;
|
||||
}
|
||||
mxg_v8f acc = (mxg_v8f)(0.f);
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
unsigned lo, hi;
|
||||
r4d_mxfp4_unpack8(wv[s], r4d_mxfp4_fold_d(ref, e8[s >> 1]), &lo, &hi);
|
||||
mxg_v2i wf;
|
||||
wf[0] = (int)lo; wf[1] = (int)hi;
|
||||
acc = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf, af[s], acc);
|
||||
}
|
||||
const float sc = ABLK ? (live ? As[(size_t)row * as_rs + ch] : 0.f) : 1.f;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) tot[e] = fmaf(acc[e], sc, tot[e]);
|
||||
}
|
||||
mxg_v8f acc = (mxg_v8f)(0.f);
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) sum[e] += tot[e];
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
unsigned lo, hi;
|
||||
r4d_mxfp4_unpack8(wv[s], r4d_mxfp4_fold_d(ref, e8[s >> 1]), &lo, &hi);
|
||||
mxg_v2i wf;
|
||||
wf[0] = (int)lo; wf[1] = (int)hi;
|
||||
acc = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf, af[s], acc);
|
||||
}
|
||||
const float sc = ABLK ? (live ? As[(size_t)row * as_rs + ch] : 0.f) : 1.f;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) tot[e] = fmaf(acc[e], sc, tot[e]);
|
||||
}
|
||||
// acc[e] (and so sum[e]): row (lane & 15), column khalf * 8 + e of the tile.
|
||||
// tot[e]: row (lane & 15), column khalf * 8 + e of the tile.
|
||||
if (live) {
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int c = khalf * 8 + e;
|
||||
const float rf = __int_as_float((int)Wref[nt * 16 + c] << 23);
|
||||
const __hip_bfloat16 h = (__hip_bfloat16)(ABLK ? sum[e] * rf : sum[e] * rf * As[(size_t)row * as_rs]);
|
||||
sG[wave][row][c] = *(const uint16_t*)&h;
|
||||
}
|
||||
for (int e = 0; e < 8; ++e) sP[half][kv][row][khalf * 8 + e] = tot[e];
|
||||
}
|
||||
__syncthreads();
|
||||
// The tile pair's product: a thread a (row, column), sixteen lanes a row, so a row's amax is a
|
||||
// shuffle within its sixteen.
|
||||
const int g = j >> 3; // the 128-column group
|
||||
const int tid = threadIdx.x;
|
||||
if (tid < M * 16) {
|
||||
const int m = tid >> 4, c = tid & 15;
|
||||
float sg = 0.f, su = 0.f;
|
||||
#pragma unroll
|
||||
for (int w = 0; w < KW; ++w) sg += sP[0][w][m][c];
|
||||
#pragma unroll
|
||||
for (int w = 0; w < KW; ++w) su += sP[1][w][m][c];
|
||||
const int cg = j * 16 + c, cu = n + j * 16 + c;
|
||||
const float rg = __int_as_float((int)Wref[cg] << 23), ru = __int_as_float((int)Wref[cu] << 23);
|
||||
const float am = ABLK ? 1.f : As[(size_t)m * as_rs];
|
||||
const __hip_bfloat16 hg = (__hip_bfloat16)(ABLK ? sg * rg : sg * rg * am);
|
||||
const __hip_bfloat16 hu = (__hip_bfloat16)(ABLK ? su * ru : su * ru * am);
|
||||
const float gv = mxg_bf2f(*(const uint16_t*)&hg);
|
||||
const float a = ACT == 0 ? gv / (1.0f + expf(-gv))
|
||||
: 0.5f * gv * (1.0f + tanhf(0.7978845608028654f * (gv + 0.044715f * gv * gv * gv)));
|
||||
const uint16_t h = mxg_f2bf(a * mxg_bf2f(*(const uint16_t*)&hu));
|
||||
if (Y) Y[(size_t)m * y_ld + cg] = h;
|
||||
H[(size_t)m * n + cg] = h;
|
||||
float mx = fabsf(mxg_bf2f(h));
|
||||
#pragma unroll
|
||||
for (int o = 8; o > 0; o >>= 1) mx = fmaxf(mx, __shfl_xor(mx, o, 16));
|
||||
if (c == 0) X[(size_t)j * M + m] = mx;
|
||||
}
|
||||
// Every thread's stores are ordered before the counter; then the group's last arriver quantises.
|
||||
__threadfence();
|
||||
__syncthreads();
|
||||
if (tid == 0)
|
||||
s_last = __hip_atomic_fetch_add(&cnt[g], 1u, __ATOMIC_ACQ_REL, __HIP_MEMORY_SCOPE_AGENT);
|
||||
__syncthreads();
|
||||
if (s_last != 7u) return;
|
||||
if (tid == 0) cnt[g] = 0u; // reset for the next launch
|
||||
__threadfence(); // acquire, before reading the other blocks'
|
||||
// gated_quant_fp8: one wave a row, a lane four consecutive columns of the group.
|
||||
if (wave < M) {
|
||||
const int m = wave, c0 = lane * 4, t = c0 >> 4, cc = c0 & 15;
|
||||
const size_t gcol = (size_t)blockIdx.x * 128 + c0;
|
||||
const int m = wave, c0 = lane * 4;
|
||||
const size_t gcol = (size_t)g * 128 + c0;
|
||||
float mx = 0.f;
|
||||
#pragma unroll
|
||||
for (int t = 0; t < 8; ++t) mx = fmaxf(mx, X[(size_t)(g * 8 + t) * M + m]);
|
||||
float vq[4];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const float g = mxg_bf2f(sG[t][m][cc + j]);
|
||||
const float a = ACT == 0 ? g / (1.0f + expf(-g))
|
||||
: 0.5f * g * (1.0f + tanhf(0.7978845608028654f * (g + 0.044715f * g * g * g)));
|
||||
const uint16_t h = mxg_f2bf(a * mxg_bf2f(sG[8 + t][m][cc + j]));
|
||||
if (Y) Y[(size_t)m * y_ld + gcol + j] = h;
|
||||
vq[j] = mxg_bf2f(h);
|
||||
}
|
||||
float mx = fmaxf(fmaxf(fabsf(vq[0]), fabsf(vq[1])), fmaxf(fabsf(vq[2]), fabsf(vq[3])));
|
||||
mx = r4d_wave_max(mx);
|
||||
for (int i = 0; i < 4; ++i) vq[i] = mxg_bf2f(H[(size_t)m * n + gcol + i]);
|
||||
const float sc = mx > 0.0f ? mx * (1.0f / q8_top<false>()) : 1.0f;
|
||||
if (lane == 0) S[(size_t)m * s_ld + blockIdx.x] = sc;
|
||||
if (lane == 0) S[(size_t)m * s_ld + g] = sc;
|
||||
const float inv = 1.0f / sc;
|
||||
*(uint32_t*)(Q + (size_t)m * q_ld + gcol) = q8_pack4<false>(vq[0], vq[1], vq[2], vq[3], inv);
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
// The arrival counters, one block per output buffer, as the decode row's (mxd_out_ctr): not in the
|
||||
// scratch, which other ops overwrite, since they must be zero at entry. One allocation per (device,
|
||||
// output), kept zero by the reducer and never freed (a recorded pass replays the pointer).
|
||||
unsigned* mxgg_out_ctr(const void* out) {
|
||||
static std::mutex mu;
|
||||
static std::map<std::pair<int, const void*>, unsigned*> blocks;
|
||||
int dev = 0;
|
||||
if (hipGetDevice(&dev) != hipSuccess) return nullptr;
|
||||
std::lock_guard<std::mutex> lk(mu);
|
||||
const auto key = std::make_pair(dev, out);
|
||||
if (auto it = blocks.find(key); it != blocks.end()) return it->second;
|
||||
unsigned* p = nullptr;
|
||||
if (hipMalloc((void**)&p, R4D_MXGG_MAX_GROUPS * sizeof(unsigned)) != hipSuccess) return nullptr;
|
||||
if (hipMemset(p, 0, R4D_MXGG_MAX_GROUPS * sizeof(unsigned)) != hipSuccess) {
|
||||
(void)hipFree(p);
|
||||
return nullptr;
|
||||
}
|
||||
blocks.emplace(key, p);
|
||||
return p;
|
||||
}
|
||||
|
||||
// The scratch: the bf16 product [M, n], then each tile's per-row amax [n / 16][M] fp32.
|
||||
long long mxgg_h_bytes(long long M, long long n) { return (M * n * 2 + 255) / 256 * 256; }
|
||||
long long mxgg_scratch_bytes(long long M, long long n) {
|
||||
return mxgg_h_bytes(M, n) + (n / 16) * M * (long long)sizeof(float);
|
||||
}
|
||||
|
||||
void mxgg_launch(bool ablk, int act, const void* a, const void* as, const void* w, const void* ws,
|
||||
const void* wr, void* q, void* s, void* y, int M, int K, int n, int as_rs, int lda,
|
||||
int ldws, int q_ld, int s_ld, int y_ld, RadStream stream) {
|
||||
const dim3 grid((uint32_t)(n / 128)), block(16 * 32);
|
||||
const void* wr, void* q, void* s, void* y, void* scratch, unsigned* cnt, int M,
|
||||
int K, int n, int as_rs, int lda, int ldws, int q_ld, int s_ld, int y_ld,
|
||||
RadStream stream) {
|
||||
const dim3 grid((uint32_t)(n / 16)), block(2 * R4D_MXG_KW * 32);
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
uint16_t* h = (uint16_t*)scratch;
|
||||
float* x = (float*)((char*)scratch + mxgg_h_bytes(M, n));
|
||||
#define MXGG_L(AB_, ACT_) \
|
||||
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_gated_kernel<AB_, ACT_>), grid, block, 0, st, \
|
||||
(const unsigned char*)a, (const float*)as, (const unsigned char*)w, \
|
||||
(const unsigned char*)ws, (const unsigned char*)wr, (uint8_t*)q, (float*)s, \
|
||||
(uint16_t*)y, M, K, n, as_rs, lda, ldws, q_ld, s_ld, y_ld)
|
||||
(uint16_t*)y, h, x, cnt, M, K, n, as_rs, lda, ldws, q_ld, s_ld, y_ld)
|
||||
if (act == 0) { if (ablk) MXGG_L(true, 0); else MXGG_L(false, 0); }
|
||||
else { if (ablk) MXGG_L(true, 1); else MXGG_L(false, 1); }
|
||||
#undef MXGG_L
|
||||
@@ -512,7 +584,8 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
* weight operands are packed bytes. */
|
||||
const long long M = q->shape[0], nout = q->shape[1];
|
||||
const long long N = r4d_geti_or(a, "N", 0), K = r4d_geti_or(a, "K", 0);
|
||||
if (M < 1 || M > R4D_MXG_MAX_M || nout < 128 || nout % 128) return R4D_NO(RAD_E_SHAPE);
|
||||
if (M < 1 || M > R4D_MXG_MAX_M || nout < 128 || nout % 128 || nout / 128 > R4D_MXGG_MAX_GROUPS)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
if (N != 2 * nout || K < 128 || K % 128) return R4D_NO(RAD_E_SHAPE);
|
||||
if (x->shape[0] != M || r4d_numel(x) != M * K) return R4D_NO(RAD_E_SHAPE);
|
||||
if (sc->shape[0] != M || sc->shape[1] != nout / 128) return R4D_NO(RAD_E_SHAPE);
|
||||
@@ -535,10 +608,23 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
if (!r4d_aligned(x->data, 16) || !r4d_aligned(w->data, 16) || !r4d_aligned(q->data, 4) ||
|
||||
!r4d_aligned(ws->data, 4) || !r4d_aligned(wr->data, 4) || qp % 4)
|
||||
return R4D_NO(RAD_E_ALIGN);
|
||||
/* The cross-block product and amaxes are the op's scratch (sized by the hook below); a
|
||||
* shorter one is refused. */
|
||||
if (!a->scratch || a->scratch_bytes < mxgg_scratch_bytes(M, nout)) return R4D_NO(RAD_E_SCRATCH);
|
||||
unsigned* cnt = mxgg_out_ctr(q->data);
|
||||
if (!cnt) return R4D_NO(RAD_E_NOMEM);
|
||||
|
||||
R4D_TRY("gemm_mxfp4a8_gated",
|
||||
mxgg_launch(ablk, actm, x->data, xs->data, w->data, ws->data, wr->data, q->data, sc->data,
|
||||
y ? y->data : nullptr, (int)M, (int)K, (int)nout, (int)as_rs, (int)lda, (int)N,
|
||||
(int)qp, (int)sp, (int)yp, s));
|
||||
y ? y->data : nullptr, a->scratch, cnt, (int)M, (int)K, (int)nout, (int)as_rs,
|
||||
(int)lda, (int)N, (int)qp, (int)sp, (int)yp, s));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* The op's scratch: the product and amaxes at the band's widest M, from N (twice the gated
|
||||
* width). */
|
||||
extern "C" int64_t r4d_rad_gemm_mxfp4a8_gated_scratch(const RadArgs* a) {
|
||||
const long long M = r4d_geti_or(a, "M", 0), N = r4d_geti_or(a, "N", 0);
|
||||
if (M < 1 || M > R4D_MXG_MAX_M || N < 256 || N % 256) return 0;
|
||||
return (int64_t)mxgg_scratch_bytes(M, N / 2);
|
||||
}
|
||||
@@ -460,6 +460,7 @@ int r4d_rad_gemm_mxfp4a8_decode(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_gemm_mxfp4a8_decode_scratch(const RadArgs*);
|
||||
int r4d_rad_gemm_mxfp4a8_gemv(const RadArgs*, RadStream);
|
||||
int r4d_rad_gemm_mxfp4a8_gated(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_gemm_mxfp4a8_gated_scratch(const RadArgs*);
|
||||
int r4d_rad_dequant_w4 (const RadArgs*, RadStream);
|
||||
int r4d_rad_rowtopk (const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_rowtopk_scratch(const RadArgs*);
|
||||
|
||||
+10
-8
@@ -7768,8 +7768,10 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
|
||||
{ "gemm_mxfp4a8_gated_gemv", "gemm_nt_q_gated_mx", "gemm",
|
||||
"the gemv row's WMMA form over a gate_up weight with gated_quant_fp8's pass in its epilogue: a "
|
||||
"block per 128-column group of the gated output, a wave per gate or up tile running all of that "
|
||||
"form's K partials in its order, so the [M, 2n] bf16 product is never written",
|
||||
"block per 16-column tile of the gated output, that form's eight K waves on the gate tile and "
|
||||
"eight on the up tile, the block's bf16 product and row amaxes in the op's scratch and the last "
|
||||
"of a 128-column group's blocks to arrive writing its codes and scales, so the [M, 2n] bf16 "
|
||||
"product is never written",
|
||||
"M <= 8; K a multiple of 128; N a multiple of 256 (the gated width a multiple of the 128-column "
|
||||
"scale group); act silu, or gelu_tanh without out_bf16; the skinny kernel's fragment-order weight, [K/32][N] E8M0 "
|
||||
"exponents and per-row reference exponent, gate rows then up rows",
|
||||
@@ -7779,7 +7781,7 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
"and quant_act_fp8 (gelu_tanh)",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cGemmNtQGatedMx), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_gemm_mxfp4a8_gated, nullptr,
|
||||
nullptr, nullptr, r4d_rad_gemm_mxfp4a8_gated, r4d_rad_gemm_mxfp4a8_gated_scratch,
|
||||
r4d_rad_layout_mxfp4_gated, r4d_rad_relayout_mxfp4_gated, nullptr, nullptr,
|
||||
/* The byte-identity claim above, as a chain of two rows of this plugin (r4d_fuse.cpp). */
|
||||
r4d_fuse_gemm_nt_q_gated_mx },
|
||||
@@ -9746,11 +9748,11 @@ extern "C" const RadKernelInfo* rad_kernel_at(int i) {
|
||||
* every collective the peer flags and exchange buffers the all-reduces own
|
||||
*
|
||||
* gemm_fp8a8_nt_m16's finisher counters are keyed on the stream and the output;
|
||||
* gemm_mxfp4a8_decode's (and gemm_mxfp4a8_decode_shortk's, the same entry) split partial is the
|
||||
* op's scratch and its arrival counters are keyed on the output alone;
|
||||
* router_gemm_topk_scatter_bf16's arrival counter belongs to its op instance (taken at init, one
|
||||
* per declared op and band). Concurrent launches never share these, so those four rows are
|
||||
* concurrent. A new row with stream-ordered state belongs in the list above.
|
||||
* gemm_mxfp4a8_decode's (and gemm_mxfp4a8_decode_shortk's, the same entry) and
|
||||
* gemm_mxfp4a8_gated_gemv's split partials are the op's scratch and their arrival counters are keyed
|
||||
* on the output alone; router_gemm_topk_scatter_bf16's arrival counter belongs to its op instance
|
||||
* (taken at init, one per declared op and band). Concurrent launches never share these, so those
|
||||
* five rows are concurrent. A new row with stream-ordered state belongs in the list above.
|
||||
*
|
||||
* lora_* ordered on purpose: the core appends them behind the GEMM whose `y` they
|
||||
* update and refills the pools they read between steps with its own copies,
|
||||
|
||||
+15
-1
@@ -5999,6 +5999,15 @@ static int launch_row(const RadKernelInfo* r, std::vector<RadTensor> ts,
|
||||
a.t = ts.data(); a.n_t = (int)ts.size();
|
||||
a.p = ps.data(); a.n_p = (int)ps.size();
|
||||
a.world_size = 1;
|
||||
if (r->scratch) {
|
||||
const int64_t need = r->scratch(&a);
|
||||
if (need < 0) return (int)need;
|
||||
if (need > 0) {
|
||||
a.scratch = dalloc((size_t)need);
|
||||
a.scratch_bytes = need;
|
||||
if (!a.scratch) return RAD_E_NOMEM;
|
||||
}
|
||||
}
|
||||
const int rc = r->launch(&a, (RadStream)nullptr);
|
||||
if (rc != RAD_OK) return rc;
|
||||
return st_stream_sync(nullptr) == hipSuccess ? RAD_OK : RAD_E_STATE;
|
||||
@@ -6181,9 +6190,14 @@ static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K
|
||||
T(d_z[1], RAD_F32, { M, gkb }), none }, gp);
|
||||
st_memcpy(d_b[1], d_b[0], (size_t)(M * n * 2), hipMemcpyDeviceToDevice);
|
||||
}
|
||||
if (rc == RAD_OK)
|
||||
/* Twice into one output, cleared between: the split's arrival counters are keyed on the output,
|
||||
* so the second launch reduces only if the first left them zero. */
|
||||
for (int rep = 0; rep < 2 && rc == RAD_OK; ++rep) {
|
||||
st_memset(d_q[2], 0, (size_t)(M * n));
|
||||
st_memset(d_z[2], 0, (size_t)(M * gkb * 4));
|
||||
rc = launch_row(rg, { ta, tas, tw, te, tr, T(d_q[2], RAD_F8E4M3, { M, n }),
|
||||
T(d_z[2], RAD_F32, { M, gkb }), none }, gp);
|
||||
}
|
||||
if (rc != RAD_OK) { ok(nm, false, "the fold: rc=" + std::to_string(rc)); return; }
|
||||
|
||||
std::vector<uint8_t> c[3];
|
||||
|
||||
Reference in new issue
Block a user