M2XFP4 (MXFP4v2): quantiser, gfx1201 WMMA kernels, host rows, df2 wiring, .rad packer (Import::ParoM2), stability fixes (counters-in-scratch x3, aql publish fences), kbench rows, runbook (REMAINING.md)

This commit is contained in:
hugypufy committed 2026-10-23 12:25:59 +01:00
1 parent 9eaf10da6e
commit ea0908596b
60 files changed
+4619 -193

No files matched your search

+32 -41
View File
@@ -15,7 +15,8 @@
// 2. The weight is the WMMA A operand on both tiles, so D is C^T of `C[M,N] = A[M,K] @ W[N,K]^T`
// and a lane's eight accumulator slots are eight weight rows of one activation row.
// 3. Split-K uses the caller's scratch (nothing on a launch path allocates), sized by
// r4d_gemm_fp8a8_nt_m16_scratch(). The summation order is fixed.
// r4d_gemm_fp8a8_nt_m16_scratch(): the split partial planes with the finisher's arrival
// counters in the tail. The summation order is fixed.
// 4. C is bf16, like every GEMM in this library.
//
// Load-bearing: the padded LDS row pitch (r4d_fp8_wmma.h `lds_pitch`), the one-K-tile-ahead
@@ -35,7 +36,6 @@
#include <atomic>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <mutex>
#include <type_traits>
@@ -560,9 +560,9 @@ __device__ __forceinline__ void gemm_fp8a8_body(
// The z loop runs in fixed ascending order with no atomic on an accumulator, so the result is
// bit-identical to the separate reduce and deterministic.
//
// The counters are a small allocation keyed on (stream, output) (`split_ctr`), not the caller's
// scratch, which other ops overwrite. The self-reset keeps them zero, which is sound only while
// launches sharing a counter block never overlap; see split_ctr.
// The counters are the caller's scratch tail past the split partials (see kMaxCtr), zeroed
// beside each launch: the engine hands the op a scratch slice no unordered launch shares, and
// the self-reset keeps them zero through the launch.
if (ck && Ctr) {
// Every thread's stores must be visible device-wide before the arrival is published: a
// per-thread fence, then the barrier.
@@ -844,36 +844,15 @@ void gemm_8a8_prefill_kernel(const uint8_t* __restrict__ A, const float* __restr
// Double-buffering the narrow tile is ~10% slower even where it fits (MW = 2): the tile is short
// of LDS, not waiting on the barrier.
// The finisher's arrival counters, one per grid.x column (see the finisher). Zeroed once and kept
// zero by the finisher. If allocation fails, the caller falls back to the separate reduce kernel,
// which computes the same bytes.
// The finisher's arrival counters, one per grid.x column (see the finisher). They ride the op's
// scratch tail past the split partials, as the mxfp4 decode's counters do (mxd_partial_bytes): the
// scratch is a declared op buffer a recorded pass relocates with the other operands, where the
// per-(stream, output) library allocation this replaced (`split_ctr`) was a pointer baked into
// the recorded kernarg that a replaying pass could not relocate. Beyond kMaxCtr columns, a short
// scratch, or a failed zero, the separate reduce kernel computes the same bytes.
constexpr uint32_t kMaxCtr = 8192;
/* One counter block per (stream, output), so concurrent launches never share one (sharing would
* mean a hang or a wrong answer). Launches overlap across streams (`rad_lane`) or on one stream
* (rad_kernel_concurrent); overlapping launches on one stream write disjoint outputs, and launches
* sharing an output on one stream are ordered by their dependency. A step has a few dozen distinct
* outputs at fixed arena addresses, so this is a few dozen 32 KiB blocks, allocated once. A block
* is never freed: a recorded pass replays the pointer. */
unsigned int* split_ctr(RadStream s, const void* out) {
static std::mutex m;
static std::map<std::pair<RadStream, const void*>, unsigned int*> blocks;
std::lock_guard<std::mutex> lk(m);
const auto key = std::make_pair(s, out);
if (auto it = blocks.find(key); it != blocks.end()) return it->second;
void* p = nullptr;
if (hipMalloc(&p, (size_t)kMaxCtr * sizeof(unsigned int)) != hipSuccess) return nullptr;
/* Zero before any launch reads it. In the engine hipMemset is the AQL backend's and has
* finished when it returns (core/device/aql.cpp); under HIP's own runtime (r4d_selftest) it
* runs on the null stream, which a non-blocking stream does not wait for, so wait here. */
if (hipMemset(p, 0, (size_t)kMaxCtr * sizeof(unsigned int)) != hipSuccess ||
hipDeviceSynchronize() != hipSuccess) {
(void)hipFree(p);
return nullptr;
}
blocks.emplace(key, (unsigned int*)p);
return (unsigned int*)p;
}
// The narrow tile's wave count MW, which is its M tile: the wave grid is MW x 1, so a block covers
// MW*16 weight rows and the grid over N is N/(MW*16). The rule is fitted together with
// kSplitTarget (which shapes split decides whether narrow or wide blocks win) on the shapes the
@@ -998,9 +977,12 @@ extern "C" void r4d_gemm_fp8a8_pin_nt(int nt) {
g_nt_force.store(v, std::memory_order_relaxed);
}
// Bytes of f32 scratch one r4d_gemm_fp8a8_nt_m16 call needs, or 0 when it will not split. The
// caller may pass more, or none (the launch then runs unsplit: correct but slow, and unreported).
// The bound is max_m() so the query and the band cannot drift apart.
// Bytes of f32 scratch one r4d_gemm_fp8a8_nt_m16 call needs, or 0 when it will not split, plus
// the arrival counters (kMaxCtr words) in the tail, as the mxfp4 decode's mxd_partial_bytes.
// The caller may pass more, or none (the launch then runs unsplit: correct but slow, and
// unreported). The bound is max_m() so the query and the band cannot drift apart. The counters
// are reserved whether or not this bound splits: the launch decides its own split at its own M,
// and the counters must fit the arena this query sized.
//
// `split_n` (0: N) is the width the split is chosen for. The split depends on N alone, and it
// fixes the order K partials are summed in, so a row slice of a wider linear (N rows of a
@@ -1011,7 +993,8 @@ static long m16_scratch(int M, int K, int N, int split_n) {
uint32_t ck = 0;
const uint32_t k2 = narrow_ksplit((uint32_t)(split_n > 0 ? split_n : N), (uint32_t)K,
(uint32_t)M, &ck);
return k2 > 1 ? (long)k2 * M * N * (long)sizeof(float) : 0;
return (k2 > 1 ? (long)k2 * M * N * (long)sizeof(float) : 0) +
(long)kMaxCtr * (long)sizeof(unsigned int);
}
extern "C" long r4d_gemm_fp8a8_nt_m16_scratch(int M, int K, int N) {
return m16_scratch(M, K, N, 0);
@@ -1068,10 +1051,18 @@ static int gemm_fp8a8_nt_m16_emit(long a, long ascale, long wq, long ws, long c,
* the arrival counter, and the finisher hangs. */
const bool full = ((uint32_t)N % fm == 0) && ((uint32_t)M % (nt * 16u) == 0) &&
(K % (int)kBlock == 0);
// The finisher needs one counter per column; beyond kMaxCtr or on allocation failure, the
// separate reduce kernel computes the same bytes.
unsigned int* const ctr =
(ksplit > 1 && gx <= kMaxCtr) ? split_ctr(stream, (const void*)c) : nullptr;
// The finisher's counters ride the scratch tail past the split partials and are zeroed here,
// so every launch enters with them zero however the last launch left them or whatever else
// wrote the arena. Beyond kMaxCtr columns, a short scratch, or a failed zero, the separate
// reduce kernel computes the same bytes.
unsigned int* ctr = nullptr;
const long ctr_bytes = (long)kMaxCtr * (long)sizeof(unsigned int);
if (ksplit > 1 && gx <= kMaxCtr && scratch && scratch_bytes >= need + ctr_bytes) {
ctr = (unsigned int*)(scratch + need);
if (hipMemsetAsync(ctr, 0, (size_t)gx * sizeof(unsigned int),
(hipStream_t)stream) != hipSuccess)
ctr = nullptr;
}
#define R4D_F8D(f, mw_, nt_, fk_, i8_) \
emit_rc = R4D_EMIT(stream, \