Archived
libr4d: keep short IQR verification on the decode path
A speculative verify issues several rows where a plain decode issues one. Two IQR kernels accumulated those rows differently, so the same weights and activations could round to different bf16 outputs. A greedy choice near a tie could therefore diverge even though every draft was accepted. gemm_iqr_nt_m64: a U5 row-scale trellis (computed trellis, 5-bit, one f32 scale a row) already folds one to four rows as a GEMV in the single-token order. Above that, through the GEMV's eight-row limit, the multi-row fold differs. Those rows now run as independent single-row GEMV blocks on grid.y, including a chained launch. Gated GEMMs and every other form keep their dispatch. moe_gemm_iqr: a short token-order gate/up batch (more than one token, at most eight times top_k, top_k below the one-wave threshold, K at least eight 128-blocks, and a one-token step inside the decode band) used the run grid. Several tokens on one expert then took the WMMA tile instead of the one-row dot. Those batches now give each sorted slot its own block and the same wide-K dot reduction as a one-token gate/up. Sorted down projections, longer batches and the prefill map are unchanged. r4d_selftest checks both against independent single-row launches, bit for bit, including an untouched output tail on the dense row. The existing grouped numerical suite still passes. Measured on an R9700, the dense U5 path at N=8192 K=2560 is 26.5 us at M=1, 51.2 at M=4 and 105.2 at M=7. A 512-expert Qwen3.8-Flash-Next gate/up (768:512 x 2560, 5-bit, ten slots a token) is 28.9 us at one token, 52.4 at four and 82.2 at seven. Prefill remains the old kernel. On the isolated build, Qwen3.8-Flash-Next U5-gate/W5-down matched its non-speculative token ids at MTP 3 and 6 on arithmetic, writing, code, explanation and JSON. StillDeadcode's IQR-V4 control matched on the arithmetic and writing prompts at MTP 3. A layer trace of the previously divergent writing token was bit-identical between the two schedules.
This commit is contained in:
1 parent
ba3ebd380f
commit
1d94e416a6
3 files changed
+159
-11
No files matched your search
+26
-4
@@ -223,7 +223,7 @@ __device__ __forceinline__ void moe_iqr_body(R4D_MOEIQR_PARAMS) {
|
||||
static_assert(TPW == 1u || KW == 1u, "several weight tiles a wave: a wave a tile's whole K");
|
||||
constexpr uint32_t NW = KW * RTB, NTHR = NW * kWave, kCols = RTB * TPW * 16u;
|
||||
/* `mode`: bit 0 `a_sorted`, bits 1-2 the class shift, bit 3 the activation's scale per 256 of
|
||||
* K, bits 8+ `parts` (r4d_moe.hip's). */
|
||||
* K, bit 4 the short token-order parity grid, bits 8+ `parts` (r4d_moe.hip's). */
|
||||
const uint32_t a_sorted = mode & 1u, cshift = (mode >> 1) & 3u, ash = (mode >> 3) & 1u;
|
||||
const uint32_t parts = mode >> 8;
|
||||
/* the waves' transpose tiles, then their partials (8 x 32 floats a wave, in its own tile) */
|
||||
@@ -253,6 +253,21 @@ __device__ __forceinline__ void moe_iqr_body(R4D_MOEIQR_PARAMS) {
|
||||
const uint32_t oe = (uint32_t)off[e + 1];
|
||||
re = oe < T ? oe : T;
|
||||
if (re > rs + kRows * NT) re = rs + kRows * NT;
|
||||
} else if (mode & 16u) {
|
||||
/* A short verification batch must use the single-token dot and K reduction even when
|
||||
* several tokens route to the same expert. One block owns one sorted slot; locate its
|
||||
* expert from the monotonic offsets. Dropped slots are zeroed just as in the run grid. */
|
||||
rs = blockIdx.x;
|
||||
if (rs >= T) return;
|
||||
if (rs >= (uint32_t)off[n_expert]) { zero_rows(rs, rs + 1u); return; }
|
||||
uint32_t lo = 0, hi = n_expert;
|
||||
while (lo < hi) {
|
||||
const uint32_t mid = (lo + hi) / 2u;
|
||||
if ((uint32_t)off[mid + 1u] <= rs) lo = mid + 1u;
|
||||
else hi = mid;
|
||||
}
|
||||
e = lo;
|
||||
re = rs + 1u;
|
||||
} else {
|
||||
/* The (tile, run) grid. The block's first wave finds the run and the others take it from
|
||||
* LDS after one barrier: each of its lanes holds kMaxExpert / 32 consecutive experts'
|
||||
@@ -569,7 +584,8 @@ int launch_form(const Launch& g, RadStream stream) {
|
||||
* a wide K (eight 128-K blocks and up) under kOneWaveMinRows routed rows is shared between
|
||||
* four waves a tile, and a narrow one between kTwoTileMinRows and kTwoTileMaxRows rows gives a
|
||||
* wave two tiles in turn and a block eight. */
|
||||
const bool wide_k = g.nkb >= 8u && (g.T < kOneWaveMinRows || (g.N / 16u) % 4u);
|
||||
const bool wide_k = g.nkb >= 8u &&
|
||||
((g.mode & 16u) || g.T < kOneWaveMinRows || (g.N / 16u) % 4u);
|
||||
const bool two = !wide_k && g.nkb < 8u && g.N % (16u * 8u) == 0u &&
|
||||
g.T >= kTwoTileMinRows && g.T <= kTwoTileMaxRows;
|
||||
if (g.map) R4D_MOEIQR_GO(1u, 4u, true, kMapTiles, 1u);
|
||||
@@ -672,7 +688,13 @@ int moe_iqr_launch(const RadArgs* a, RadStream stream, int ncls) {
|
||||
if (M < (a_sorted ? T : (T + top_k - 1) / top_k)) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
g.nkb = (uint32_t)(K / kBlock);
|
||||
const bool band = r4d_moe_in_decode_band(T, n_expert);
|
||||
/* The common short verification shape uses the same wide-K dot reduction as a one-token
|
||||
* gate/up. Limit this to geometries whose one-token step takes that decode path; larger batches
|
||||
* retain the throughput-oriented run/map kernels. */
|
||||
const bool parity_rows = !a_sorted && top_k < (long long)kOneWaveMinRows &&
|
||||
T > top_k && T <= 8 * top_k && g.nkb >= 8u &&
|
||||
r4d_moe_in_decode_band(top_k, n_expert);
|
||||
const bool band = parity_rows || r4d_moe_in_decode_band(T, n_expert);
|
||||
/* a narrow-K projection and the run map's grid take four weight tiles a block */
|
||||
if (((!band || g.nkb < 8u) && (N / 16) % 4) || (N / parts) % 16) return R4D_NO(RAD_E_SHAPE);
|
||||
g.x = (const int8_t*)Aq->data;
|
||||
@@ -694,7 +716,7 @@ int moe_iqr_launch(const RadArgs* a, RadStream stream, int ncls) {
|
||||
g.inv_ft = parts > 1 ? (uint32_t)(((uint64_t)1 << 32) / (uint64_t)(N / parts / 16) + 1u) : 0u;
|
||||
const uint32_t cshift = ncls == 4 ? 2u : 1u;
|
||||
g.mode = (a_sorted ? 1u : 0u) | cshift << 1 | (a_group == 2 * (long long)kBlock ? 8u : 0u) |
|
||||
(uint32_t)parts << 8;
|
||||
(parity_rows ? 16u : 0u) | (uint32_t)parts << 8;
|
||||
|
||||
if (band) {
|
||||
g.map = nullptr;
|
||||
|
||||
Reference in new issue
Block a user