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

+21 -32
View File
@@ -27,7 +27,7 @@
//
// No state is keyed on the stream, since a recorded pass may replay on another stream. The split
// partial is the op's scratch (sized by the scratch hook, read+write, so overlapping ops get
// disjoint slices); the arrival counters are keyed on the output buffer (mxd_out_ctr), zero at
// disjoint slices); the arrival counters ride the scratch's tail, zero at
// entry and reset by the reducer. Launches writing the same output are ordered; overlapping ones
// write different outputs. The row is therefore concurrent (rad_kernel_concurrent), like
// gemm_fp8a8_nt_m16. A scratch shorter than the split needs is refused.
@@ -457,31 +457,8 @@ void r4d_gemm_mxfp4a8_decode1_kernel(
}
namespace {
// The arrival counters, one block per output buffer (see the header). They cannot live in the
// scratch, which other ops overwrite, because 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), as gemm_fp8a8_nt_m16's split_ctr.
unsigned* mxd_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;
const size_t n = R4D_MXD_MAX_N / (R4D_MXD_DWN * 16);
unsigned* p = nullptr;
if (hipMalloc((void**)&p, n * sizeof(unsigned)) != 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, n * sizeof(unsigned)) != hipSuccess || hipDeviceSynchronize() != hipSuccess) {
(void)hipFree(p);
return nullptr;
}
blocks.emplace(key, p);
return p;
}
// The arrival counters now ride the op's scratch (mxd_partial_bytes); the per-output library
// allocation this replaced was a pointer a recorded pass could not relocate.
// The split, slab and load hint for a launch, from M, the shape and the tunables (0 = the rule; see
// the header). Shared by the launch and the scratch hook so the sized partial is the one written.
@@ -508,8 +485,15 @@ MxdPlan mxd_plan(int M, int K, int N, int KS, int BK, int NT) {
return p;
}
/* The scratch: the split partial when ks > 1, then the arrival counters. The counters live in the
* scratch rather than in a library-owned allocation because the scratch is a declared op buffer:
* a recorded pass relocates it with the other operands, while a pointer the library allocated
* itself is baked into the recorded kernarg and cannot be relocated. */
constexpr size_t mxd_ctr_count() { return R4D_MXD_MAX_N / (R4D_MXD_DWN * 16); }
long mxd_partial_bytes(int M, int N, int ks) {
return ks > 1 ? (long)ks * M * N * (long)sizeof(float) : 0;
return (ks > 1 ? (long)ks * M * N * (long)sizeof(float) : 0) +
(long)mxd_ctr_count() * (long)sizeof(unsigned);
}
void mxd_launch(long a, long ascale, long wq, long ws, long wref, long c, int M, int K, int N,
@@ -530,12 +514,16 @@ void mxd_launch(long a, long ascale, long wq, long ws, long wref, long c, int M,
throw std::runtime_error("r4d gemm_mxfp4a8_decode: KS must be 1, 2, 4, 8 or 16");
if (bk != 64 && bk != 128)
throw std::runtime_error("r4d gemm_mxfp4a8_decode: BK must be 64 or 128");
if (scratch_bytes < mxd_partial_bytes(M, N, ks) || (ks > 1 && !scratch))
if (!scratch || scratch_bytes < mxd_partial_bytes(M, N, ks))
throw std::runtime_error("r4d gemm_mxfp4a8_decode: the op's scratch is shorter than the split "
"partial; the scratch hook sizes it");
"partial and the arrival counters; the scratch hook sizes it");
float* P = (float*)scratch;
unsigned* cnt = mxd_out_ctr((const void*)c);
if (!cnt) throw std::runtime_error("r4d gemm_mxfp4a8_decode: split counter alloc failed");
unsigned* cnt = (unsigned*)((unsigned char*)P +
(ks > 1 ? (size_t)ks * (size_t)M * (size_t)N * sizeof(float) : 0));
/* Every launch enters with the counters zero: the scratch is shared across ops, and a replayed
* pass writes the same slots. One 1.1 KiB memset beside the launch is not measurable. */
if (hipMemsetAsync(cnt, 0, mxd_ctr_count() * sizeof(unsigned), (hipStream_t)stream) != hipSuccess)
throw std::runtime_error("r4d gemm_mxfp4a8_decode: zeroing the arrival counters failed");
const bool ablk = as_group == 128, nt = pl.nt;
const int nblk = (N + R4D_MXD_DWN * 16 - 1) / (R4D_MXD_DWN * 16);
const int tm = (M + R4D_MXD_MTILE - 1) / R4D_MXD_MTILE;
@@ -638,5 +626,6 @@ extern "C" int64_t r4d_rad_gemm_mxfp4a8_decode_scratch(const RadArgs* a) {
const int64_t b = (int64_t)mxd_partial_bytes((int)M, (int)np, ks);
if (b > worst) worst = b;
}
return worst;
const int64_t ctr = (int64_t)mxd_ctr_count() * (int64_t)sizeof(unsigned);
return worst > ctr ? worst : ctr;
}