From d0c44cf5a3d5b1ec3ff5e03e233fad9e29e521e2 Mon Sep 17 00:00:00 2001 From: dustin Date: Fri, 23 Oct 2026 10:11:57 -0500 Subject: [PATCH] tools: a w2a8 recipe in the fallback shape table -- rad-tune can now size the draft head it serves rad-tune skips a (kernel, shape) pair it cannot size: a schema names operands, not extents, and a tool has to allocate the bytes the kernel will read. For gemm_nt_q the reference describes the fp8 and the symmetric integer grids and declines every grid with a zero point, because ref_planes.h joins scale and zero into one operand a RadOpdDesc cannot state. w2a8 is such a grid, and it is the one the DFlash2 draft head serves, so both of its bands were skipped at every tune and the served card ran the axis defaults for them. The extents are not a guess. r4d_rad_layout_w2a8 owns them: codes to fragment order, N*K/4 bytes (r4d.w2a8.frag.g128), and one dword per row and 128 of K joining that group's f16 scale and integer zero, tile-major, N*K/128 of them (r4d.w2a8.sz.g128). The activation triple is quant_act_i8's own output -- int8 codes, one f32 scale a row, and the group-major code sums with rows padded to 16 -- and the grid is asymmetric, so the kernel refuses the case without the sums. b_ref stays absent; the reference exponent is mxfp4's. relayout_parity_test is where both sides exist, so the claim goes there: the table's byte total for b and b_scale against the same hooks' RadLayout.bytes, over libquant's planes, for w2a8 and for mxfp4a8, whose sizes the table has carried untested since it was written. Writing the table's K/4 as K/2 fails it with "the table sizes 32768 byte(s), the kernel stores 16384". The kernel's own guards -- K a whole number of 128-column blocks, N a whole number of 16-row tiles, the group the stored form has -- are pinned as refusals, and the shape a two-rank draft head tunes at (M 8, N 124160, K 2560, group 128) is pinned as accepted. --- tests/CMakeLists.txt | 6 +++ tests/relayout_parity_test.cpp | 86 +++++++++++++++++++++++++++++++++- tools/opshapes.cpp | 32 +++++++++++-- 3 files changed, 118 insertions(+), 6 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cb1e974a..91c97a2e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -176,6 +176,12 @@ foreach(t ${RAD_TESTS}) target_link_libraries(${n} PRIVATE ${RAD_TEST_CORE} ${CMAKE_DL_LIBS}) target_include_directories(${n} PRIVATE ${CMAKE_SOURCE_DIR}/libr4d) target_compile_options(${n} PRIVATE -ffp-contract=off) + # relayout_parity_test also holds tools/opshapes.cpp's fallback table to these hooks' byte + # totals, so it needs the table's source too. + if(n STREQUAL relayout_parity_test) + target_sources(${n} PRIVATE ${CMAKE_SOURCE_DIR}/tools/opshapes.cpp) + target_include_directories(${n} PRIVATE ${CMAKE_SOURCE_DIR}/tools) + endif() elseif(n IN_LIST RAD_TESTS_WHOLE_CORE) add_executable(${n} ${t}) target_link_libraries(${n} PRIVATE stdc++ "$") diff --git a/tests/relayout_parity_test.cpp b/tests/relayout_parity_test.cpp index 427a428f..b5a3a364 100644 --- a/tests/relayout_parity_test.cpp +++ b/tests/relayout_parity_test.cpp @@ -9,10 +9,13 @@ * see. Where the kernel has an inverse, it must give the planes back exactly. * * libr4d needs a HIP toolchain to build but its layouts do not, so their host sources are compiled - * into this test (tests/CMakeLists.txt). The n-gram table's hook is libavx's, from the plugin. + * into this test (tests/CMakeLists.txt). The n-gram table's hook is libavx's, from the plugin. One + * test here therefore reaches back into tools/opshapes.cpp: the fallback table sizes the packed + * weights these hooks own, and only this file can hold both sides of that claim. */ #include "quant_fixture.h" #include "r4d_plugin.h" +#include "opshapes.h" #include #include @@ -346,6 +349,87 @@ TEST(mxfp4_codes_exponents_and_the_row_reference) { 0xc35d26b146836d77ull); } +/* tools/opshapes.cpp sizes a packed weight from the kernel's stored form, because no schema states + * an extent and libref declines every grid with a zero point (ref_planes.h joins scale and zero + * into one operand a description cannot state). A stale entry is not a failed check: rad-tune + * allocates what the table says and hands the kernel the short buffer, which is a read past the + * allocation on the card, not a refusal. So the table's byte total and the hook's `bytes` must + * agree. The table's shapes are the tool's own view -- mxfp4's exponents are [K/32][N] there and + * [N][K/32] in the store -- so the totals, not the shapes, are the contract. */ +TEST(the_tool_table_sizes_packed_weights_as_the_kernel_stores_them) { + NEED_LIBQUANT(); + const int64_t N = 128, K = 512, M = 8; + auto check = [&](const char* dtype, int64_t group, const Hooks& hk, + const std::vector& bscale_roles, Opts qo) { + const std::vector w = weight(N, K, 21, 1, group); + const Quantised q = quantise(quantizer("rtn"), qo, "output.weight", N, K, w); + if (q.status != RAD_OK) { + CHECK_EQ(q.status, RAD_OK); + return; + } + Opts kp; + kp.i("N", N).i("K", K); + rad::Geometry g; + g.set_i("M", M); g.set_i("N", N); g.set_i("K", K); g.set_i("group", group); + g.set_s("dtype", dtype); + std::vector tab; + if (rad::rad_operand_shapes("gemm_nt_q", g, 7, M, &tab) != RAD_OK) { + std::fprintf(stderr, " %s: the table refuses the geometry\n", dtype); + CHECK(false); + return; + } + CHECK_EQ((int)tab.size(), 7); + struct P { int operand; size_t opd; std::vector roles; }; + const std::vector

ops = { { B, 2, { "codes" } }, { BSCALE, 3, bscale_roles } }; + for (const P& o : ops) { + const Selection s = select(q, N, K, o.roles); + RadLayout L{}; + CHECK_EQ(hk.layout(kp.data(), kp.n(), o.operand, &q.enc, s.sel.data(), s.t.data(), + (int)s.t.size(), &L), RAD_OK); + const rad::OpdShape& t = tab[o.opd]; + const int64_t got = t.absent ? 0 : rad_dtype_bytes(t.dtype, rad::rad_numel(t.shape)); + if (got != (int64_t)L.bytes) + std::fprintf(stderr, " %s operand %d: the table sizes %lld byte(s), the kernel " + "stores %lld\n", dtype, o.operand, (long long)got, + (long long)L.bytes); + CHECK_EQ(got, (int64_t)L.bytes); + } + }; + { + Opts qo; + qo.s("codes", "u2").s("zero", "u8").i("group", 128).s("scale", "f16"); + check("w2a8", 128, nW2, { "scale", "zero" }, qo); + } + { + Opts qo; + qo.s("codes", "fp4_e2m1").i("group", 32).s("scale", "e8m0"); + check("mxfp4a8", 32, nMx, { "scale" }, qo); + } + /* The guards are the kernel's own: K a whole number of 128-column blocks + * (r4d_layout.cpp refuses a weight whose K is not), N a whole number of 16-row tiles (the + * row's constraint), and the group the stored form has. A geometry the kernel would refuse has + * to be refused here too, or rad-tune would size buffers for a launch that never happens. */ + auto refuses = [&](int64_t n, int64_t k, int64_t group) { + rad::Geometry g; + g.set_i("M", M); g.set_i("N", n); g.set_i("K", k); g.set_i("group", group); + g.set_s("dtype", "w2a8"); + std::vector tab; + CHECK_EQ(rad::rad_operand_shapes("gemm_nt_q", g, 7, M, &tab), RAD_E_UNSUPPORTED); + }; + refuses(128, 500, 128); /* K leaves a partial block */ + refuses(120, 512, 128); /* N leaves a partial tile */ + refuses(128, 512, 64); /* a group the stored form does not have */ + /* The shape the DFlash2 draft head tunes at on a two-rank serve, which the kernel takes. */ + { + rad::Geometry g; + g.set_i("M", M); g.set_i("N", 124160); g.set_i("K", 2560); g.set_i("group", 128); + g.set_s("dtype", "w2a8"); + std::vector tab; + CHECK_EQ(rad::rad_operand_shapes("gemm_nt_q", g, 7, M, &tab), RAD_OK); + CHECK_EQ((int)tab.size(), 7); + } +} + TEST(moe_int4_experts_plain_and_rotated_both_classes) { NEED_LIBQUANT(); const int64_t N = 64, K = 256, N1 = 48, K1 = 128; diff --git a/tools/opshapes.cpp b/tools/opshapes.cpp index 72769ed7..32953917 100644 --- a/tools/opshapes.cpp +++ b/tools/opshapes.cpp @@ -154,11 +154,15 @@ int rad_operand_shapes(const std::string& op, const Geometry& g, int n_opd, int6 add((uint32_t)RAD_F32, {M, (n + group - 1) / group}); } else if (op == "gemm_nt_q") { - /* Block-scaled fp8 and mxfp4 only. `gemm_nt_q` covers six weight formats whose scale grids - * (per row, per row-group, per output-row-block, e8m0 exponents, zero points, reference - * exponents) belong to the reading kernel, not the schema. The fp8 grid is fixed by the - * checkpoint: E4M3 [N][K] with a bf16 [ceil(N/128)][ceil(K/128)] scale plane, `group` - * being the 128. Other formats are skipped and counted. + /* Block-scaled fp8, mxfp4 and the 2-bit asymmetric grid. `gemm_nt_q` covers six weight + * formats whose scale grids (per row, per row-group, per output-row-block, e8m0 exponents, + * zero points, reference exponents) belong to the reading kernel, not the schema. The fp8 + * grid is fixed by the checkpoint: E4M3 [N][K] with a bf16 [ceil(N/128)][ceil(K/128)] + * scale plane, `group` being the 128. libref describes the symmetric integer grids, so a + * tool takes those from its hook and never reaches this table for them; every grid with a + * zero point is declined there (ref_planes.h joins scale and zero into one operand a + * description cannot state), and the ones named here are sized from the kernel's own + * stored layout instead. Anything else is skipped and counted. * * `a_sum` and `b_ref` belong to the asymmetric-integer and mxfp4 grids. */ const char* q = g.get_s("dtype"); @@ -175,6 +179,24 @@ int rad_operand_shapes(const std::string& op, const Geometry& g, int n_opd, int6 add((uint32_t)RAD_BF16, {M, N}); /* y */ skip_opd(); /* a_sum */ add((uint32_t)RAD_U8, {N}); /* b_ref -- the per-row reference */ + } else if (q && std::strcmp(q, "w2a8") == 0) { + /* The DFlash2 draft head's grid (libr4d/r4d_gemm_w2a8_nt.hip): unsigned 2-bit codes in + * the kernel's fragment order, N*K/4 bytes (`r4d.w2a8.frag.g128`), and one dword per + * row and `group` of K holding that group's f16 scale and integer zero, tile-major, + * N*K/group of them (`r4d.w2a8.sz.g128`) -- both extents owned by + * libr4d/r4d_layout.cpp's r4d_rad_layout_w2a8, which is what sizes the real weight. + * The activation pair and the group sums are quant_act_i8's own outputs above, and the + * grid is asymmetric, so the kernel refuses the case without `a_sum`. `b_ref` is + * mxfp4's. Sizes are the stored bytes; random bytes suffice for timing. */ + if (N <= 0 || K <= 0 || group != 128 || (K % 128) || (N % 16)) + return RAD_E_UNSUPPORTED; + add((uint32_t)RAD_I8, {M, K}); /* a -- int8 codes */ + add((uint32_t)RAD_F32, {M, 1}); /* a_scale -- one f32 per row */ + add((uint32_t)RAD_U8, {N, K / 4}); /* b -- u2 codes, fragment order */ + add((uint32_t)RAD_U32, {N, K / group}); /* b_scale -- scale+zero dwords */ + add((uint32_t)RAD_BF16, {M, N}); /* y */ + add((uint32_t)RAD_F32, {K / group, (M + 15) / 16 * 16}); /* a_sum, group-major */ + skip_opd(); /* b_ref */ } else { const bool a8 = q && std::strcmp(q, "fp8a8") == 0; const bool a16 = q && std::strcmp(q, "fp8a16") == 0;