Archived
radiance 1.1.0
- The VRAM split has no ratio: --expert-vs-cache-ratio is gone (now an unknown option). The experts keep what the pinned host pool cannot hold, the KV cache takes the rest and lends idle blocks to the experts. Flash-Next at tp2: 7.9 GiB of KV a card, up from 4.7. Startup, /stats, /metrics and the dashboard report the most the cache can hold. - Linear-state snapshots live in pinned host memory, not VRAM (--checkpoint-slots sizes the store). Prefix-cache misses on agent sessions fixed; a published conversation is no longer trimmed away. - Any world size from the shipped container (baKon): --tp 3 on Flash-Next with no padded checkpoint -- the attention world, uneven delta-net heads, the fill op. - Kernels (keptn42): moe_gemm_q decode loads and one-trip run search, hc_read's prefill form (+3.7% prefill at 18K), hc_up row groups up to eight, the fused router launch. Each is the rule where it was measured faster; no environment switches. - Tool calls: a typed argument is always JSON (Python True/None no longer leak into arguments -- Codeberg #21); nine further reply-parser fixes; the reply path inside a call is ~12x cheaper on the host. - The kernel changes: prefill +2-3% and decode -2% a step on 2x R9700 at tp2, identical text.
This commit is contained in:
1 parent
9f5feb24ee
commit
82b5835c02
101 files changed
+5105
-3767
No files matched your search
+6
-12
@@ -627,21 +627,15 @@ void r4d_qsa_score_pin_form(int form);
|
||||
// prefill form, 2 the (tile, run) decode form, 3 the uniform prefill grid. r4d_selftest --perfmoe
|
||||
// times each and compares it with form 1.
|
||||
void r4d_moe_gemm_pin_prefill(int form);
|
||||
// THE DECODE FORM'S STAGED K, pinned for a test: 0 is R4D_MOE_GU_FK256's rule, 1 one 128-wide block
|
||||
// a pass, 2 two wherever the shape allows it (the token-operand projection, every class's K a
|
||||
// multiple of 256, a format that folds scale groups). The two are bit-identical, and r4d_selftest
|
||||
// compares their bytes.
|
||||
// THE DECODE FORM'S STAGED K, pinned for a test: 0 is the launcher's row limit (kMoeGuFk256Rows),
|
||||
// 1 one 128-wide block a pass, 2 two wherever the shape allows it (the token-operand projection,
|
||||
// every class's K a multiple of 256, a format that folds scale groups). The two are bit-identical,
|
||||
// and r4d_selftest compares their bytes.
|
||||
void r4d_moe_gemm_pin_fk(int pin);
|
||||
// THE DECODE FORM'S WEIGHT ADDRESS, pinned for a test: 0 is R4D_MOE_UWLOAD's rule, 1 the generic
|
||||
// table pointer, 2 a uniform global base. Bit-identical, and r4d_selftest compares their bytes.
|
||||
void r4d_moe_gemm_pin_uw(int pin);
|
||||
// THE DECODE FORM'S RUN SEARCH, pinned for a test: 0 is R4D_MOE_RUNSCAN's rule, 1 the chunked
|
||||
// search, 2 the one-pass search. Bit-identical, and r4d_selftest compares their bytes.
|
||||
void r4d_moe_gemm_pin_rs(int pin);
|
||||
// THE GATED RESIDUAL READ'S PREFILL TILE, pinned for a test: 0 is the launcher's rule, 1 the decode
|
||||
// tiles' kernels at 128 rows (hc_down_tiled, then hc_up_tiled into `ubuf` and hc_mix), 2 the wide
|
||||
// kernels (which then refuse a shape they are not built for), 3..9 the PF2 forms that
|
||||
// R4D_HC_READ_PF2=1..7 puts in the rule (the same, and also refused where the band cannot hold
|
||||
// kernels (which then refuse a shape they are not built for), 3 the PF2 kernels the rule takes from
|
||||
// 512 rows (refused likewise, and also at a band under 512 rows, whose scratch is not sized for
|
||||
// their weight planes). All are bit-identical by construction, and r4d_selftest compares their
|
||||
// bytes.
|
||||
void r4d_hc_read_pin_prefill(int form);
|
||||
|
||||
+131
-579
@@ -1291,31 +1291,20 @@ void hc_up_mix_kernel(const void* __restrict__ upv, long uld,
|
||||
}
|
||||
}
|
||||
|
||||
/* THE GROUP SIZE IS THE ROW COUNT, ROUNDED DOWN TO AN INSTANTIATION. RG is a template parameter
|
||||
* because `acc[RG]` has to be registers; a runtime bound makes it scratch, and one private-memory
|
||||
* round trip an element would cost more than the weight re-read it is here to remove.
|
||||
/* THE GROUP SIZE IS THE ROW COUNT, UP TO EIGHT. RG is a template parameter because `acc[RG]` has
|
||||
* to be registers; a runtime bound makes it scratch, and one private-memory round trip an element
|
||||
* would cost more than the weight re-read it is here to remove. Every group from 1 to 8 is
|
||||
* instantiated, so a row count up to eight is ONE group and the weight is read once: a five- or
|
||||
* six-row verify (depth 4 or 5) split as 4 + 1 or 4 + 2 would read W_up twice. The output is the
|
||||
* same bytes at any group, because only the loop nest over rows changes.
|
||||
*
|
||||
* The LDS cap is the real bound: RG rows of `d` are RG*lr floats beside the 4 KiB of staging
|
||||
* slots, so a model with a wide low rank gets a smaller group rather than a launch failure. */
|
||||
|
||||
/* R4D_HC_RG_EXACT=1: a row count up to eight is ONE group of exactly that many rows (3, 5, 6 and 7
|
||||
* are instantiated too), so a depth-4 or depth-5 verify -- five or six rows -- still reads W_up once
|
||||
* instead of twice (4 + 1, 4 + 2). Bit-identical for the reason the RG note gives: only the loop
|
||||
* nest changes. Unset: the power-of-two rule. Read once, so recorded and live passes agree. */
|
||||
static bool hc_rg_exact() {
|
||||
static const bool v = [] {
|
||||
const char* e = std::getenv("R4D_HC_RG_EXACT");
|
||||
return e && *e && *e != '0';
|
||||
}();
|
||||
return v;
|
||||
}
|
||||
* slots, so a model with a wide low rank gets a smaller group rather than a launch failure -- the
|
||||
* largest that fits, one row at a time, since every size below eight is an instantiation. */
|
||||
static inline long hc_up_rg(long long rows, long long lr) {
|
||||
constexpr long kLdsCap = 60 * 1024; /* leave the 4 KiB of staging slots room */
|
||||
long rg = (rows >= 8) ? 8 : (rows >= 4) ? 4 : (rows >= 2) ? 2 : 1;
|
||||
if (hc_rg_exact() && rows > 1 && rows < 8 &&
|
||||
(long long)rows * lr * (long long)sizeof(float) <= kLdsCap)
|
||||
return (long)rows;
|
||||
while (rg > 1 && (long long)rg * lr * (long long)sizeof(float) > kLdsCap) rg >>= 1;
|
||||
long rg = rows < 8 ? (rows > 1 ? (long)rows : 1) : 8;
|
||||
while (rg > 1 && (long long)rg * lr * (long long)sizeof(float) > kLdsCap) --rg;
|
||||
return rg;
|
||||
}
|
||||
|
||||
@@ -1726,9 +1715,9 @@ constexpr int kWideRT = 128, kWideNT = 112, kWideCH = 16;
|
||||
|
||||
/* THE PREFILL TILE'S FORM, pinned for a test: 0 is the launcher's rule, 1 the decode tiles'
|
||||
* kernels at RT 128 (hc_down_tiled, then hc_up_tiled into `ubuf` and hc_mix), 2 the wide kernels
|
||||
* (which then refuse a shape they are not built for), 3..9 the PF2 forms R4D_HC_READ_PF2=1..7
|
||||
* selects (hc_pf2_launch; refused likewise, and also where the band's `u` cannot hold their
|
||||
* weight planes). */
|
||||
* (which then refuse a shape they are not built for), 3 the PF2 form (hc_pf2_launch; refused
|
||||
* likewise, and also at a band under kPf2MinRows, whose `u` is not sized for its weight
|
||||
* planes). */
|
||||
static int g_hc_prefill_form = 0;
|
||||
|
||||
/* ==================================================== d = silu(W_down @ N / hc) at the PREFILL tile
|
||||
@@ -2207,8 +2196,17 @@ void hc_up_mix_tiled_kernel(const void* __restrict__ upv, long uld,
|
||||
*
|
||||
* THE FP8 TWIN is the arrival counter's, as in hc_up_mix_tiled_kernel: a workgroup owns 16 of a
|
||||
* scale group's 128 channels, and the last of the eight covering a group quantises it for the
|
||||
* tile's rows. */
|
||||
template <int HC, bool F8>
|
||||
* tile's rows.
|
||||
*
|
||||
* RT IS THE ROW TILE, MW = RT / 128 m-tiles a wave: 128 here (one m-tile a wave), 256 in the PF2
|
||||
* form over the mix_up its prologue decoded (two, so each B fragment read out of LDS feeds two
|
||||
* WMMAs). The m-tile loops are the only difference, and at MW 1 each runs once over the same
|
||||
* expressions in the same order as a body without them. The accumulators are ONE FLAT ARRAY,
|
||||
* acc[mi * HC + c], because at MW 1 that is acc[c] exactly: an [MW][HC] array is the same values
|
||||
* but reaches the register allocator in another shape, and gfx1201's code for the 128-row form then
|
||||
* moves its zeros in another order. The rows keep their place in a 16-row tile at either RT (a
|
||||
* tile starts at a multiple of RT), so both are the same bytes. */
|
||||
template <int HC, bool F8, int RT = kWideRT>
|
||||
__global__ __launch_bounds__(kThreads)
|
||||
void hc_up_mix_wide_kernel(const void* __restrict__ upv, long uld,
|
||||
const unsigned short* __restrict__ dbuf,
|
||||
@@ -2216,8 +2214,9 @@ void hc_up_mix_wide_kernel(const void* __restrict__ upv, long uld,
|
||||
const float* __restrict__ scalein,
|
||||
unsigned short* __restrict__ x,
|
||||
long rows, long n, long lr, long hrow, long xrow, float wadd) {
|
||||
constexpr int RT = kWideRT, CH = kWideCH, NT = HC * CH;
|
||||
static_assert(RT / 16 == kWaves, "one m-tile a wave");
|
||||
constexpr int CH = kWideCH, NT = HC * CH;
|
||||
constexpr int MW = RT / 16 / kWaves; /* m-tiles a wave */
|
||||
static_assert(MW >= 1 && MW * 16 * kWaves == RT, "the waves cover the rows");
|
||||
static_assert(RT * NT <= RT * kTAP, "N must fit in the A tile's LDS");
|
||||
__shared__ unsigned short sA[RT * kTAP];
|
||||
__shared__ unsigned short sB[NT * kTAP];
|
||||
@@ -2289,9 +2288,9 @@ void hc_up_mix_wide_kernel(const void* __restrict__ upv, long uld,
|
||||
}
|
||||
};
|
||||
|
||||
v8f acc[HC];
|
||||
v8f acc[MW * HC];
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c) acc[c] = (v8f){0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
|
||||
for (int c = 0; c < MW * HC; ++c) acc[c] = (v8f){0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
|
||||
|
||||
fetch(0);
|
||||
for (long k0 = 0; k0 < lr; k0 += kTKT) {
|
||||
@@ -2313,14 +2312,22 @@ void hc_up_mix_wide_kernel(const void* __restrict__ upv, long uld,
|
||||
}
|
||||
__syncthreads();
|
||||
if (k0 + kTKT < lr) fetch(k0 + kTKT);
|
||||
/* A's fragment is read once a k step and taken to every n-tile. Each accumulator still
|
||||
* sees its k steps in ascending order, which is all its bytes depend on. */
|
||||
/* A's fragments are read once a k step and taken to every n-tile, and B's to every m-tile.
|
||||
* Each accumulator still sees its k steps in ascending order, which is all its bytes
|
||||
* depend on. */
|
||||
#pragma unroll
|
||||
for (int ks = 0; ks < kTKT / 16; ++ks) {
|
||||
const v8bf af = frag(sA, kTAP, wave * 16, ks * 16, lane);
|
||||
v8bf af[MW];
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c)
|
||||
acc[c] = mma(af, frag(sB, kTAP, c * 16, ks * 16, lane), acc[c]);
|
||||
for (int mi = 0; mi < MW; ++mi)
|
||||
af[mi] = frag(sA, kTAP, (wave * MW + mi) * 16, ks * 16, lane);
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c) {
|
||||
const v8bf bf = frag(sB, kTAP, c * 16, ks * 16, lane);
|
||||
#pragma unroll
|
||||
for (int mi = 0; mi < MW; ++mi)
|
||||
acc[mi * HC + c] = mma(af[mi], bf, acc[mi * HC + c]);
|
||||
}
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
@@ -2345,15 +2352,17 @@ void hc_up_mix_wide_kernel(const void* __restrict__ upv, long uld,
|
||||
const float inv_hc = 1.0f / (float)HC;
|
||||
const int e0 = 8 * (lane >> 4), nn = lane & 15;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int rl = wave * 16 + e0 + e;
|
||||
const long r = r0 + rl;
|
||||
float a = 0.0f;
|
||||
for (int mi = 0; mi < MW; ++mi)
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c)
|
||||
a += bf2f(f2bf(sigmoidf_(acc[c][e]))) * bf2f(sN[(rl * HC + c) * CH + nn]);
|
||||
if (r < rows) x[r * xrow + i0 + nn] = f2bf(a * inv_hc);
|
||||
}
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int rl = (wave * MW + mi) * 16 + e0 + e;
|
||||
const long r = r0 + rl;
|
||||
float a = 0.0f;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c)
|
||||
a += bf2f(f2bf(sigmoidf_(acc[mi * HC + c][e]))) * bf2f(sN[(rl * HC + c) * CH + nn]);
|
||||
if (r < rows) x[r * xrow + i0 + nn] = f2bf(a * inv_hc);
|
||||
}
|
||||
}
|
||||
|
||||
/* THE FP8 CODES OF `x` AT THE PREFILL TILE, and the rotated twin: one wave a (row, 128-channel
|
||||
@@ -2486,9 +2495,12 @@ static inline void hc_up_mix_wide_launch(bool f8, bool i8, const void* up, long
|
||||
|
||||
/* ==================================================== hc_read, the SECOND PREFILL FORM (PF2)
|
||||
*
|
||||
* R4D_HC_READ_PF2=1 takes it at a prefill chunk, pinned form 3 takes it for a test. The same five
|
||||
* steps as the wide form and the same bytes out of every one of them -- what moves is which
|
||||
* workgroup does which part of the work, and how often:
|
||||
* THE LAUNCHER'S RULE AT A PREFILL CHUNK of kPf2MinRows rows or more whose shape it is built for --
|
||||
* hc 4, n a multiple of 256, at most kPf2MaxNT n-tiles of columns -- with either dtype of mixing
|
||||
* matrix; pinned form 3 takes it at any 128-row tile of such a band for a test. Every other
|
||||
* prefill call is the wide form's. The same five steps as the wide form and the same bytes out of
|
||||
* every one of them -- what moves is which workgroup does which part of the work, and how often
|
||||
* (711 -> 611 us a call at a 2048-row chunk on gfx1201, E4M3 matrices):
|
||||
*
|
||||
* THE A OPERAND OF THE DOWN PRODUCT IS BUILT ONCE. hc_down_wide_kernel owns 112 columns, so the
|
||||
* three column tiles of a row tile each rebuild N from `h` -- the norm weight, the scale and the
|
||||
@@ -2496,13 +2508,15 @@ static inline void hc_up_mix_wide_launch(bool f8, bool i8, const void* up, long
|
||||
* rows and EVERY column, so `h` is read and N is built exactly once a call.
|
||||
* THE WEIGHT IS DECODED ONCE. Every row tile of the wide form decodes the E4M3 rows of both
|
||||
* mixing matrices it stages -- sixteen decodes of each matrix a call at a 2048-row chunk. A PF2
|
||||
* call decodes each matrix once, in its prologue, into scratch the wide form leaves unused.
|
||||
* call decodes each matrix once, in its prologue, into the `u` scratch, which no fused form
|
||||
* uses. At bf16 the prologue copies mix_down and mix_up is read where it is stored.
|
||||
* THE DOWN PRODUCT'S B OPERAND SKIPS LDS. With every column in one workgroup, each 16-column
|
||||
* n-tile belongs to exactly one wave, so nothing is shared and nothing needs staging: the
|
||||
* prologue writes the decoded matrix in WMMA FRAGMENT ORDER and a wave's fragment is one
|
||||
* 512-byte load. Only N goes through LDS, double-buffered, one barrier a slice.
|
||||
* THE UP PRODUCT'S TILE IS 256 ROWS, two m-tiles a wave. Each B fragment read out of LDS feeds
|
||||
* two WMMAs instead of one, and the weight is staged and read half as often.
|
||||
* THE UP PRODUCT'S TILE IS 256 ROWS: hc_up_mix_wide_kernel at RT 256 over a bf16 mix_up, two
|
||||
* m-tiles a wave. Each B fragment read out of LDS feeds two WMMAs instead of one, and the weight
|
||||
* is staged and read half as often.
|
||||
*
|
||||
* WHAT KEEPS IT THE SAME BYTES. Every output element is the same chain of 16x16x16 WMMAs over
|
||||
* the same fragments in the same order: N is the same expression, the decoded weight is the bf16
|
||||
@@ -2510,48 +2524,15 @@ static inline void hc_up_mix_wide_launch(bool f8, bool i8, const void* up, long
|
||||
* their place inside a 16x16 tile, and the down product keeps the K-split of hc_down_kchunk at
|
||||
* the 128-row tile, partial for partial, summed by the same reduce. The norm scales are
|
||||
* hc_norm_scale_kernel's sums in hc_norm_scale_kernel's order (see the prologue). r4d_selftest
|
||||
* compares all three forms byte for byte. */
|
||||
* compares all three forms byte for byte, at both dtypes.
|
||||
*
|
||||
* THE PLANES' ROOM IS RESERVED, NOT HOPED FOR. r4d_rad_hc_read_scratch sizes `u` for both planes
|
||||
* at every band that reaches kPf2MinRows (hc_u_rows), so the rule never meets a `u` too small. */
|
||||
constexpr int kPf2RT = 64; // down product: rows a workgroup, four m-tiles
|
||||
constexpr int kPf2NW = 3; // down product: n-tiles a wave, at most
|
||||
constexpr int kPf2MaxNT = kWaves * kPf2NW; // ...so ceil((lr + hc) / 16) <= 24
|
||||
constexpr int kPf2WRT = 128; // the 128-row down product (PF2 form 4)...
|
||||
constexpr int kPf2WThreads = 2 * kThreads; // ...is sixteen waves, two m-halves of eight
|
||||
constexpr int kPf2WMaxNT = 21; // ...and holds a B slice of 21 n-tiles in LDS
|
||||
constexpr int kPf2UpRT = 256; // up product: rows a workgroup, two m-tiles a wave
|
||||
constexpr long kPf2MinRows = 512; // the rule's floor; the pin takes any prefill tile
|
||||
|
||||
/* WHAT A PF2 CALL RUNS, as bits: the 64-row down product, the up product and mix at 256 rows, the
|
||||
* 128-row down product, the persistent up product and mix. A side without a bit is the wide
|
||||
* form's kernel, so each half can be timed and checked alone; the prologue (norm scales, and the
|
||||
* planes the chosen kernels read) runs in every PF2 call. */
|
||||
enum : int { kPf2Down = 1, kPf2Up = 2, kPf2DownW = 4, kPf2UpP = 8 };
|
||||
|
||||
/* R4D_HC_READ_PF2 (unset or 0: the wide form, as upstream): 1 both halves, 2 the down product
|
||||
* alone, 3 the up product alone, 4 both with the 128-row down product, 5 the down product with
|
||||
* the persistent up product, 6 the 128-row down product with it, 7 the persistent up product
|
||||
* alone. Pinned forms 3..9 are the same seven. Read once, so a recorded pass and a live one take
|
||||
* the same branch. */
|
||||
constexpr int kPf2Forms = 7;
|
||||
static int hc_pf2_bits(int sel) {
|
||||
if (sel < 1 || sel > kPf2Forms) return 0;
|
||||
switch (sel) {
|
||||
case 1: return kPf2Down | kPf2Up;
|
||||
case 2: return kPf2Down;
|
||||
case 3: return kPf2Up;
|
||||
case 4: return kPf2DownW | kPf2Up;
|
||||
case 5: return kPf2Down | kPf2UpP;
|
||||
case 6: return kPf2DownW | kPf2UpP;
|
||||
case 7: return kPf2UpP;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
static int hc_pf2_env() {
|
||||
static const int v = [] {
|
||||
const char* e = std::getenv("R4D_HC_READ_PF2");
|
||||
return hc_pf2_bits(e != nullptr ? std::atoi(e) : 0);
|
||||
}();
|
||||
return v;
|
||||
}
|
||||
constexpr long kPf2MinRows = 512; // the rule's floor, and where `u` holds the planes
|
||||
|
||||
/* THE PROLOGUE: the per-stream norm scales and the two weight planes, in ONE launch, because
|
||||
* the down product needs all three and none of them needs another.
|
||||
@@ -2585,7 +2566,7 @@ void hc_pf2_prep_kernel(const unsigned short* __restrict__ h, float* __restrict_
|
||||
const unsigned short* __restrict__ injw,
|
||||
const unsigned char* __restrict__ up8, long uld,
|
||||
uint4* __restrict__ plane, unsigned short* __restrict__ udec,
|
||||
uint4* __restrict__ uswz, long lr, long ncol, long ntt) {
|
||||
long lr, long ncol, long ntt) {
|
||||
__shared__ float s_t[kWaves][kThreads];
|
||||
const int tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
|
||||
const long HN = (long)hc * n;
|
||||
@@ -2630,12 +2611,11 @@ void hc_pf2_prep_kernel(const unsigned short* __restrict__ h, float* __restrict_
|
||||
return;
|
||||
}
|
||||
|
||||
const long KS = HN / 16, KSN = lr / 16;
|
||||
const long KS = HN / 16;
|
||||
const long nswz = ntt * KS * kWave;
|
||||
const long nup = udec ? HN * (lr / 8) : 0;
|
||||
const long nusw = uswz ? HN * (lr / 8) : 0;
|
||||
const long step = ((long)gridDim.x - nnb) * kThreads;
|
||||
for (long v = ((long)blockIdx.x - nnb) * kThreads + tid; v < nswz + nup + nusw; v += step) {
|
||||
for (long v = ((long)blockIdx.x - nnb) * kThreads + tid; v < nswz + nup; v += step) {
|
||||
if (v < nswz) {
|
||||
const long f = v / kWave;
|
||||
const int l = (int)(v - f * kWave);
|
||||
@@ -2660,40 +2640,12 @@ void hc_pf2_prep_kernel(const unsigned short* __restrict__ h, float* __restrict_
|
||||
o = make_uint4(lo.x, lo.y, hi.x, hi.y);
|
||||
}
|
||||
plane[v] = o;
|
||||
} else if (v < nswz + nup) {
|
||||
} else {
|
||||
const long u = v - nswz;
|
||||
const long j = u / (lr / 8), k8 = (u - j * (lr / 8)) * 8;
|
||||
const unsigned char* row = up8 + j * uld;
|
||||
*(uint4*)(udec + j * lr + k8) =
|
||||
e4m3x8_bf16(row + k8, ((const float*)(row + lr))[k8 / r4d_hc8::up_group(lr)]);
|
||||
} else {
|
||||
/* mix_up in FRAGMENT ORDER for hc_up_mix_pf2p_kernel: [n/16 blocks][hc][KSN][32]. Lane
|
||||
* l of block b, sub-stream c, k-step g holds row c*n + 16b + (l & 15) at k = 16g +
|
||||
* 4(l >> 4) + {0..3, 8..11}; a k-step never straddles a group of lr/4 (a multiple of
|
||||
* 16, which the launch checks). */
|
||||
const long u = v - nswz - nup;
|
||||
const int l = (int)(u % kWave);
|
||||
const long fr = u / kWave;
|
||||
const long g = fr % KSN, bc = fr / KSN;
|
||||
const long c = bc % hc, b = bc / hc;
|
||||
const long j = c * n + 16 * b + (l & 15);
|
||||
const long k = g * 16 + 4 * (l >> 4);
|
||||
uint4 o;
|
||||
if (f8) {
|
||||
const unsigned char* row = up8 + j * uld;
|
||||
const float sc = ((const float*)(row + lr))[k / r4d_hc8::up_group(lr)];
|
||||
float fv[8];
|
||||
e4m3x8(*(const uint32_t*)(row + k), *(const uint32_t*)(row + k + 8), fv);
|
||||
union { uint4 v; unsigned short s[8]; } uu;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) uu.s[e] = f2bf(fv[e] * sc);
|
||||
o = uu.v;
|
||||
} else {
|
||||
const unsigned short* src = (const unsigned short*)up8 + j * lr;
|
||||
const uint2 lo = *(const uint2*)(src + k), hi = *(const uint2*)(src + k + 8);
|
||||
o = make_uint4(lo.x, lo.y, hi.x, hi.y);
|
||||
}
|
||||
uswz[u] = o;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2839,388 +2791,6 @@ void hc_down_pf2_kernel(const unsigned short* __restrict__ h, const float* __res
|
||||
}
|
||||
}
|
||||
|
||||
/* THE SAME PRODUCT AT 128 ROWS, sixteen waves (PF2 form 4). hc_down_pf2_kernel reads the whole of
|
||||
* B from L2 once per 64 rows -- 3.4 MB a workgroup at a 2048-row chunk, 220 MB a call. Here the
|
||||
* waves of two m-halves share each B slice through LDS, which halves that stream for the price of
|
||||
* staging it and of two barriers a slice. Which of the two the part prefers is a measurement.
|
||||
*
|
||||
* The B slice goes into LDS exactly as it lies in `plane` -- [n-tile][k-step][lane], 2 KB an n-tile
|
||||
* -- so staging is a straight copy and a wave's fragment is one conflict-free 16-byte LDS read.
|
||||
* Wave w is m-half w / 8 (four m-tiles) and n-group w % 8 (n-tiles w % 8, + 8, + 16). Same chains,
|
||||
* same fragments, same places in the tile: the same bytes as every other form. */
|
||||
__global__ __launch_bounds__(kPf2WThreads)
|
||||
void hc_down_pf2w_kernel(const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
const uint4* __restrict__ plane, const float* __restrict__ scalein,
|
||||
unsigned short* __restrict__ dout, unsigned short* __restrict__ inj,
|
||||
float* __restrict__ part,
|
||||
long rows, long n, int hc, long lr, long ncol, long ntt, long hrow,
|
||||
long irow, long kc, float wadd) {
|
||||
constexpr int NTH = kPf2WThreads, RT = kPf2WRT, MW = 4, NW = kPf2NW;
|
||||
constexpr int kKV = kTKT / 8; /* 16-byte vectors a staged A row */
|
||||
constexpr int kRS = NTH / kKV; /* rows between a thread's A rows */
|
||||
constexpr int kAV = RT / kRS;
|
||||
constexpr int KST = kTKT / 16; /* k-steps a slice */
|
||||
constexpr int kFV = KST * kWave; /* 16-byte vectors of one n-tile's slice */
|
||||
constexpr int kBV = (kPf2WMaxNT * kFV + NTH - 1) / NTH;
|
||||
static_assert(RT % kRS == 0 && MW * 16 * 2 == RT && 2 * kWaves * kWave == NTH,
|
||||
"two m-halves of four m-tiles, eight n-groups");
|
||||
__shared__ unsigned short sA[RT * kTAP];
|
||||
__shared__ uint4 sB[kPf2WMaxNT * kFV];
|
||||
|
||||
const int tid = threadIdx.x, lane = tid & (kWave - 1);
|
||||
const int wave = __builtin_amdgcn_readfirstlane(tid / kWave);
|
||||
const int mh = wave / kWaves, ng = wave % kWaves;
|
||||
const long HN = (long)hc * n, KS = HN / 16;
|
||||
const long r0 = (long)blockIdx.x * RT;
|
||||
const long ka = (long)blockIdx.y * kc;
|
||||
const long kb = (ka + kc < HN) ? ka + kc : HN;
|
||||
if (r0 >= rows || ka >= HN) return;
|
||||
const long nsl = (kb - ka) / kTKT;
|
||||
const long nbv = ntt * kFV;
|
||||
const long g0 = ka / 16;
|
||||
|
||||
const int k8a = (tid % kKV) * 8, ar = tid / kKV;
|
||||
long arow[kAV];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kAV; ++i) {
|
||||
const long r = r0 + ar + i * kRS;
|
||||
arow[i] = r < rows ? r : rows - 1;
|
||||
}
|
||||
uint4 pa[kAV], pb[kBV];
|
||||
float psc[kAV], pw[8];
|
||||
auto fetch = [&](long t) {
|
||||
const long k0 = ka + t * kTKT;
|
||||
const int c = (int)(k0 / n);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kAV; ++i) {
|
||||
pa[i] = *(const uint4*)(h + arow[i] * hrow + k0 + k8a);
|
||||
psc[i] = scalein[arow[i] * kHcMax + c];
|
||||
}
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) pw[e] = w[k0 + k8a + e];
|
||||
#pragma unroll
|
||||
for (int b = 0; b < kBV; ++b) {
|
||||
const long idx = tid + (long)b * NTH;
|
||||
if (idx >= nbv) continue;
|
||||
const long nt = idx / kFV, wi = idx - nt * kFV;
|
||||
pb[b] = plane[(nt * KS + g0 + t * KST) * kWave + wi];
|
||||
}
|
||||
};
|
||||
auto stage = [&]() {
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kAV; ++i) {
|
||||
union { uint4 v; unsigned short s[8]; } in, out;
|
||||
in.v = pa[i];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) out.s[e] = f2bf(bf2f(in.s[e]) * psc[i] * (pw[e] + wadd));
|
||||
*(uint4*)(sA + (ar + i * kRS) * kTAP + k8a) = out.v;
|
||||
}
|
||||
#pragma unroll
|
||||
for (int b = 0; b < kBV; ++b) {
|
||||
const long idx = tid + (long)b * NTH;
|
||||
if (idx < nbv) sB[idx] = pb[b];
|
||||
}
|
||||
};
|
||||
|
||||
bool live[NW];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NW; ++i) live[i] = ng + i * kWaves < ntt;
|
||||
v8f acc[MW][NW];
|
||||
#pragma unroll
|
||||
for (int m = 0; m < MW; ++m)
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NW; ++i) acc[m][i] = (v8f){0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
|
||||
|
||||
fetch(0);
|
||||
for (long t = 0; t < nsl; ++t) {
|
||||
__syncthreads();
|
||||
stage();
|
||||
__syncthreads();
|
||||
fetch((t + 1 < nsl) ? t + 1 : t); /* the last slice re-reads itself: no branch */
|
||||
#pragma unroll
|
||||
for (int s = 0; s < KST; ++s) {
|
||||
v8bf af[MW];
|
||||
#pragma unroll
|
||||
for (int m = 0; m < MW; ++m) af[m] = frag(sA, kTAP, (mh * MW + m) * 16, s * 16, lane);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NW; ++i) {
|
||||
if (!live[i]) continue;
|
||||
union { uint4 q; v8bf f; } bu;
|
||||
bu.q = sB[((ng + i * kWaves) * KST + s) * kWave + lane];
|
||||
#pragma unroll
|
||||
for (int m = 0; m < MW; ++m) acc[m][i] = mma(af[m], bu.f, acc[m][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const float inv_hc = 1.0f / (float)hc;
|
||||
const int e0 = 8 * (lane >> 4), nn = lane & 15;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NW; ++i) {
|
||||
if (!live[i]) continue;
|
||||
const long col = (long)(ng + i * kWaves) * 16 + nn;
|
||||
if (col >= ncol) continue;
|
||||
#pragma unroll
|
||||
for (int m = 0; m < MW; ++m)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const long r = r0 + (mh * MW + m) * 16 + e0 + e;
|
||||
if (r >= rows) continue;
|
||||
const float v = acc[m][i][e];
|
||||
if (part) part[((long)blockIdx.y * rows + r) * ncol + col] = v;
|
||||
else if (col < lr) dout[r * lr + col] = f2bf(siluf_(v * inv_hc));
|
||||
else inj[r * irow + (col - lr)] = f2bf(2.0f * sigmoidf_(v * inv_hc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* THE UP PRODUCT AND MIX WITH `d` HELD, PERSISTENT (PF2 forms 7..9). The wide form's 2560
|
||||
* workgroups each restage a [128 x 320] tile of `d` for sixteen channels, so `d` crosses L2 160
|
||||
* times a call and the A staging is a third of the kernel's LDS traffic. Here a wave loads its 16
|
||||
* rows of `d` ONCE, as the twenty WMMA fragments of K = 320 in its registers, and the workgroup
|
||||
* walks a run of channel blocks: each block's B -- four sub-streams of sixteen channels, mix_up
|
||||
* decoded by the prologue into FRAGMENT ORDER, 40 KB contiguous -- is a straight copy into LDS and
|
||||
* a wave's fragment is one 16-byte LDS read. The next block's B and `h` are loaded while this
|
||||
* block's WMMAs run.
|
||||
*
|
||||
* Every u is the same 20-step chain from zero over the same fragments, at the same place in its
|
||||
* tile, and N, the narrowing and the mix are hc_up_mix_wide_kernel's expressions: the same bytes. */
|
||||
constexpr int kPf2PRT = 128; // rows a workgroup: one m-tile a wave
|
||||
template <int KSN> // k-steps of lowrank, lr / 16
|
||||
__global__ __launch_bounds__(kThreads)
|
||||
void hc_up_mix_pf2p_kernel(const uint4* __restrict__ uswz, const unsigned short* __restrict__ dbuf,
|
||||
const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
const float* __restrict__ scalein, unsigned short* __restrict__ x,
|
||||
long rows, long n, long lr, long hrow, long xrow, float wadd,
|
||||
long bpw) {
|
||||
constexpr int HC = 4, CH = kWideCH, NT = HC * CH;
|
||||
constexpr int kBF = HC * KSN * kWave; /* 16-byte vectors of one block's B */
|
||||
constexpr int kBV = kBF / kThreads;
|
||||
constexpr int kNV = kPf2PRT * HC * (CH / 8) / kThreads;
|
||||
static_assert(kBF % kThreads == 0 && (kPf2PRT * HC * (CH / 8)) % kThreads == 0,
|
||||
"every thread stages whole vectors");
|
||||
__shared__ uint4 sB[kBF];
|
||||
__shared__ unsigned short sN[kPf2PRT * NT];
|
||||
|
||||
const int tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
|
||||
const long r0 = (long)blockIdx.y * kPf2PRT;
|
||||
const long nblk = n / CH;
|
||||
const long b0 = (long)blockIdx.x * bpw;
|
||||
const long b1 = (b0 + bpw < nblk) ? b0 + bpw : nblk;
|
||||
if (r0 >= rows || b0 >= b1) return;
|
||||
|
||||
/* This wave's rows of `d`, all twenty fragments: what frag() would read out of an A tile
|
||||
* holding them, read from `dbuf` directly. */
|
||||
v8bf af[KSN];
|
||||
{
|
||||
long r = r0 + wave * 16 + (lane & 15);
|
||||
if (r >= rows) r = rows - 1;
|
||||
const unsigned short* p = dbuf + r * lr + 4 * (lane >> 4);
|
||||
#pragma unroll
|
||||
for (int g = 0; g < KSN; ++g) af[g] = fragP(p + 16 * g);
|
||||
}
|
||||
|
||||
uint4 pb[kBV], pn[kNV];
|
||||
float pns[kNV];
|
||||
auto fetch = [&](long b) {
|
||||
const uint4* src = uswz + b * kBF;
|
||||
#pragma unroll
|
||||
for (int v = 0; v < kBV; ++v) pb[v] = src[tid + v * kThreads];
|
||||
const long i0 = b * CH;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kNV; ++i) {
|
||||
const int idx = tid + i * kThreads;
|
||||
const int rl = idx / (HC * CH / 8), rem = idx % (HC * CH / 8);
|
||||
const int c = rem / (CH / 8), i8 = (rem % (CH / 8)) * 8;
|
||||
const long r = r0 + rl;
|
||||
pn[i] = make_uint4(0u, 0u, 0u, 0u);
|
||||
pns[i] = 0.0f;
|
||||
if (r < rows) {
|
||||
pn[i] = *(const uint4*)(h + r * hrow + (long)c * n + i0 + i8);
|
||||
pns[i] = scalein[r * kHcMax + c];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const float inv_hc = 1.0f / (float)HC;
|
||||
const int e0 = 8 * (lane >> 4), nn = lane & 15;
|
||||
fetch(b0);
|
||||
for (long b = b0; b < b1; ++b) {
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int v = 0; v < kBV; ++v) sB[tid + v * kThreads] = pb[v];
|
||||
const long i0 = b * CH;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kNV; ++i) {
|
||||
const int idx = tid + i * kThreads;
|
||||
const int rl = idx / (HC * CH / 8), rem = idx % (HC * CH / 8);
|
||||
const int c = rem / (CH / 8), i8 = (rem % (CH / 8)) * 8;
|
||||
if (r0 + rl >= rows) continue;
|
||||
const long j = (long)c * n + i0 + i8;
|
||||
union { uint4 v; unsigned short s[8]; } in, out;
|
||||
in.v = pn[i];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) out.s[e] = f2bf(bf2f(in.s[e]) * pns[i] * (w[j + e] + wadd));
|
||||
*(uint4*)(sN + (rl * HC + c) * CH + i8) = out.v;
|
||||
}
|
||||
__syncthreads();
|
||||
fetch((b + 1 < b1) ? b + 1 : b); /* the last block re-reads itself: no branch */
|
||||
|
||||
v8f acc[HC];
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c) acc[c] = (v8f){0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
|
||||
#pragma unroll
|
||||
for (int g = 0; g < KSN; ++g)
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c) {
|
||||
union { uint4 q; v8bf f; } bu;
|
||||
bu.q = sB[(c * KSN + g) * kWave + lane];
|
||||
acc[c] = mma(af[g], bu.f, acc[c]);
|
||||
}
|
||||
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int rl = wave * 16 + e0 + e;
|
||||
const long r = r0 + rl;
|
||||
float a = 0.0f;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c)
|
||||
a += bf2f(f2bf(sigmoidf_(acc[c][e]))) * bf2f(sN[(rl * HC + c) * CH + nn]);
|
||||
if (r < rows) x[r * xrow + i0 + nn] = f2bf(a * inv_hc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* hc_up_mix_wide_kernel at 256 rows, two m-tiles a wave, over a bf16 mix_up (decoded by the
|
||||
* prologue, or the stored one). The (stream, channel) pairing, the prefetch, the N pass and the
|
||||
* mix are that kernel's line for line; what changes is that a B fragment read out of LDS goes to
|
||||
* both of a wave's m-tiles, so the weight's staging and its fragment reads are halved against
|
||||
* the WMMAs they feed. The rows keep their place in a 16-row tile (the tile starts at a multiple
|
||||
* of 256), so every output is the same bytes. */
|
||||
template <int HC>
|
||||
__global__ __launch_bounds__(kThreads)
|
||||
void hc_up_mix_pf2_kernel(const unsigned short* __restrict__ ubf,
|
||||
const unsigned short* __restrict__ dbuf,
|
||||
const unsigned short* __restrict__ h, const float* __restrict__ w,
|
||||
const float* __restrict__ scalein,
|
||||
unsigned short* __restrict__ x,
|
||||
long rows, long n, long lr, long hrow, long xrow, float wadd) {
|
||||
constexpr int RT = kPf2UpRT, CH = kWideCH, NT = HC * CH;
|
||||
constexpr int MW = RT / 16 / kWaves; /* m-tiles a wave */
|
||||
static_assert(MW * 16 * kWaves == RT, "the waves cover the rows");
|
||||
static_assert(NT <= kTAP, "N must fit in the A tile's LDS");
|
||||
__shared__ unsigned short sA[RT * kTAP];
|
||||
__shared__ unsigned short sB[NT * kTAP];
|
||||
|
||||
const int tid = threadIdx.x, lane = tid & (kWave - 1), wave = tid / kWave;
|
||||
const long r0 = (long)blockIdx.y * RT;
|
||||
const long i0 = (long)blockIdx.x * CH;
|
||||
if (r0 >= rows) return;
|
||||
|
||||
constexpr int kKV = kTKT / 8;
|
||||
constexpr int kRS = kThreads / kKV;
|
||||
constexpr int kAV = RT / kRS, kBV = NT / kRS;
|
||||
constexpr int kNV = RT * HC * (CH / 8) / kThreads;
|
||||
static_assert(RT % kRS == 0 && NT % kRS == 0 && (RT * HC * (CH / 8)) % kThreads == 0,
|
||||
"every thread stages whole vectors");
|
||||
const int k8a = (tid % kKV) * 8, tr = tid / kKV;
|
||||
|
||||
uint4 pn[kNV];
|
||||
float pns[kNV];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kNV; ++i) {
|
||||
const int idx = tid + i * kThreads;
|
||||
const int rl = idx / (HC * CH / 8), rem = idx % (HC * CH / 8);
|
||||
const int c = rem / (CH / 8), i8 = (rem % (CH / 8)) * 8;
|
||||
const long r = r0 + rl;
|
||||
pn[i] = make_uint4(0u, 0u, 0u, 0u);
|
||||
pns[i] = 0.0f;
|
||||
if (r < rows) {
|
||||
pn[i] = *(const uint4*)(h + r * hrow + (long)c * n + i0 + i8);
|
||||
pns[i] = scalein[r * kHcMax + c];
|
||||
}
|
||||
}
|
||||
|
||||
uint4 pa[kAV], pb[kBV];
|
||||
auto fetch = [&](long k0) {
|
||||
const long k = k0 + k8a;
|
||||
const bool in = k + 8 <= lr;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kAV; ++i) {
|
||||
long r = r0 + tr + i * kRS; if (r >= rows) r = rows - 1;
|
||||
pa[i] = in ? *(const uint4*)(dbuf + r * lr + k) : make_uint4(0u, 0u, 0u, 0u);
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kBV; ++i) {
|
||||
const int tt = tr + i * kRS;
|
||||
const long j = (long)(tt / CH) * n + i0 + tt % CH;
|
||||
pb[i] = in ? *(const uint4*)(ubf + j * lr + k) : make_uint4(0u, 0u, 0u, 0u);
|
||||
}
|
||||
};
|
||||
|
||||
v8f acc[MW][HC];
|
||||
#pragma unroll
|
||||
for (int mi = 0; mi < MW; ++mi)
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c) acc[mi][c] = (v8f){0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
|
||||
|
||||
fetch(0);
|
||||
for (long k0 = 0; k0 < lr; k0 += kTKT) {
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kAV; ++i) *(uint4*)(sA + (tr + i * kRS) * kTAP + k8a) = pa[i];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kBV; ++i) *(uint4*)(sB + (tr + i * kRS) * kTAP + k8a) = pb[i];
|
||||
__syncthreads();
|
||||
if (k0 + kTKT < lr) fetch(k0 + kTKT);
|
||||
#pragma unroll
|
||||
for (int ks = 0; ks < kTKT / 16; ++ks) {
|
||||
v8bf af[MW];
|
||||
#pragma unroll
|
||||
for (int mi = 0; mi < MW; ++mi)
|
||||
af[mi] = frag(sA, kTAP, (wave * MW + mi) * 16, ks * 16, lane);
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c) {
|
||||
const v8bf bf = frag(sB, kTAP, c * 16, ks * 16, lane);
|
||||
#pragma unroll
|
||||
for (int mi = 0; mi < MW; ++mi) acc[mi][c] = mma(af[mi], bf, acc[mi][c]);
|
||||
}
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
unsigned short* const sN = sA;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kNV; ++i) {
|
||||
const int idx = tid + i * kThreads;
|
||||
const int rl = idx / (HC * CH / 8), rem = idx % (HC * CH / 8);
|
||||
const int c = rem / (CH / 8), i8 = (rem % (CH / 8)) * 8;
|
||||
if (r0 + rl >= rows) continue;
|
||||
const long j = (long)c * n + i0 + i8;
|
||||
union { uint4 v; unsigned short s[8]; } in, out;
|
||||
in.v = pn[i];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) out.s[e] = f2bf(bf2f(in.s[e]) * pns[i] * (w[j + e] + wadd));
|
||||
*(uint4*)(sN + (rl * HC + c) * CH + i8) = out.v;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
const float inv_hc = 1.0f / (float)HC;
|
||||
const int e0 = 8 * (lane >> 4), nn = lane & 15;
|
||||
#pragma unroll
|
||||
for (int mi = 0; mi < MW; ++mi)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int rl = (wave * MW + mi) * 16 + e0 + e;
|
||||
const long r = r0 + rl;
|
||||
float a = 0.0f;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HC; ++c)
|
||||
a += bf2f(f2bf(sigmoidf_(acc[mi][c][e]))) * bf2f(sN[(rl * HC + c) * CH + nn]);
|
||||
if (r < rows) x[r * xrow + i0 + nn] = f2bf(a * inv_hc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* K per down-product workgroup: enough z-blocks that lr/kTNT * z covers the part, rounded to a
|
||||
* whole number of kTKT slices so a slice never straddles a sub-stream. Returns HN when one block
|
||||
@@ -3857,18 +3427,25 @@ static inline void hc_up_tiled_launch(int rt, bool f8, const void* up, long uld,
|
||||
#undef HC_UP_CASE
|
||||
}
|
||||
|
||||
/* THE PF2 FORM, start to finish: prologue, down product, reduce, up product and mix, codes.
|
||||
* `bits` says which halves are PF2's (a side without its bit runs the wide form's kernel, on the
|
||||
* stored weights). `wsp` is the plane space -- the `u` buffer, which no fused form uses -- and the
|
||||
* caller has checked it holds pf2_plane_bytes(). Returns a RAD code. */
|
||||
static inline long long pf2_plane_bytes(int bits, long long HN, long long lr, long long ntt,
|
||||
bool f8) {
|
||||
const bool down = (bits & (kPf2Down | kPf2DownW)) != 0;
|
||||
return (down ? ntt * 16 * HN * 2 : 0) + ((bits & kPf2Up) && f8 ? HN * lr * 2 : 0)
|
||||
+ ((bits & kPf2UpP) ? HN * lr * 2 : 0);
|
||||
/* THE PF2 FORM'S WEIGHT PLANES in the `u` scratch, in bytes: mix_down (and the inject weights) in
|
||||
* fragment order over ntt n-tiles, then -- E4M3 only -- mix_up decoded to [HN][lr] bf16. */
|
||||
static inline long long pf2_plane_bytes(long long HN, long long lr, long long ntt, bool f8) {
|
||||
return ntt * 16 * HN * 2 + (f8 ? HN * lr * 2 : 0);
|
||||
}
|
||||
|
||||
static int hc_pf2_launch(int bits, bool f8, bool i8, const unsigned short* h, const float* w,
|
||||
/* `u`'S ROWS IN THE SCRATCH, [rows][HN] bf16: the band's M, and at a band that reaches the PF2 rule
|
||||
* at least the rows its planes take at the widest fold (ncol = lr + hc) and either dtype --
|
||||
* ceil((lr + hc) / 16) * 16 + lr. The scratch hook and the launcher both size `u` from this, so the
|
||||
* offsets behind it agree, and the rule never finds the planes without room. */
|
||||
static inline long long hc_u_rows(long long M, long long lr, long long hc) {
|
||||
const long long pf2 = ((lr + hc + 15) / 16) * 16 + lr;
|
||||
return (M >= kPf2MinRows && pf2 > M) ? pf2 : M;
|
||||
}
|
||||
|
||||
/* THE PF2 FORM, start to finish: prologue, down product, reduce, up product and mix, codes. `wsp`
|
||||
* is the plane space -- the `u` buffer, which no fused form uses -- and the caller has checked it
|
||||
* holds pf2_plane_bytes() and that hc is 4. Returns a RAD code. */
|
||||
static int hc_pf2_launch(bool f8, bool i8, const unsigned short* h, const float* w,
|
||||
const void* down, long dld, const void* up, long uld,
|
||||
const unsigned short* injw, unsigned short* dbuf, float* scale,
|
||||
unsigned char* wsp, float* part, unsigned short* x,
|
||||
@@ -3876,43 +3453,27 @@ static int hc_pf2_launch(int bits, bool f8, bool i8, const unsigned short* h, co
|
||||
float* rqs, long rows, long n, int hc, long lr, long ncol, long hrow,
|
||||
long irow, long xrow, long qrow, long srow, long rqrow, long rsrow,
|
||||
long kc, int ks, float eps, float wadd, RadStream s) {
|
||||
const long HN = (long)hc * n;
|
||||
const bool pdown = (bits & (kPf2Down | kPf2DownW)) != 0;
|
||||
const bool pup = (bits & kPf2Up) != 0;
|
||||
const bool pupp = (bits & kPf2UpP) != 0;
|
||||
const long ntt = pdown ? (ncol + 15) / 16 : 0; /* plane n-tiles; none unless used */
|
||||
uint4* const plane = pdown ? (uint4*)wsp : nullptr;
|
||||
unsigned char* const utail = wsp + ntt * 16 * HN * 2;
|
||||
unsigned short* const udec = pup && f8 ? (unsigned short*)utail : nullptr;
|
||||
uint4* const uswz = pupp ? (uint4*)utail : nullptr;
|
||||
const long HN = (long)hc * n;
|
||||
const long ntt = (ncol + 15) / 16; /* plane n-tiles */
|
||||
uint4* const plane = (uint4*)wsp;
|
||||
unsigned short* const udec = f8 ? (unsigned short*)(wsp + ntt * 16 * HN * 2) : nullptr;
|
||||
const unsigned short* ubf = f8 ? udec : (const unsigned short*)up;
|
||||
|
||||
const long nnb = (rows * hc + kWaves - 1) / kWaves;
|
||||
const long work = ntt * (HN / 16) * kWave + (udec ? HN * (lr / 8) : 0)
|
||||
+ (uswz ? HN * (lr / 8) : 0);
|
||||
const long work = ntt * (HN / 16) * kWave + (udec ? HN * (lr / 8) : 0);
|
||||
long ndec = (work + kThreads - 1) / kThreads;
|
||||
if (ndec > 512) ndec = 512;
|
||||
hipLaunchKernelGGL(hc_pf2_prep_kernel, dim3((unsigned)(nnb + ndec)), dim3(kThreads), 0,
|
||||
(hipStream_t)s, h, scale, rows, n, hc, hrow, eps, nnb, down, dld, f8, injw,
|
||||
(const unsigned char*)up, uld, plane, udec, uswz, lr, ncol, ntt);
|
||||
(const unsigned char*)up, uld, plane, udec, lr, ncol, ntt);
|
||||
if (hipGetLastError() != hipSuccess) return RAD_E_DEVICE;
|
||||
|
||||
float* const kpart = ks > 1 ? part : nullptr;
|
||||
if (bits & kPf2Down)
|
||||
hipLaunchKernelGGL(hc_down_pf2_kernel,
|
||||
dim3((unsigned)((rows + kPf2RT - 1) / kPf2RT), (unsigned)ks),
|
||||
dim3(kThreads), 0, (hipStream_t)s, h, w, (const uint4*)plane,
|
||||
(const float*)scale, dbuf, inj, kpart, rows, n, hc, lr, ncol, ntt, hrow,
|
||||
irow, kc, wadd);
|
||||
else if (bits & kPf2DownW)
|
||||
hipLaunchKernelGGL(hc_down_pf2w_kernel,
|
||||
dim3((unsigned)((rows + kPf2WRT - 1) / kPf2WRT), (unsigned)ks),
|
||||
dim3(kPf2WThreads), 0, (hipStream_t)s, h, w, (const uint4*)plane,
|
||||
(const float*)scale, dbuf, inj, kpart, rows, n, hc, lr, ncol, ntt, hrow,
|
||||
irow, kc, wadd);
|
||||
else
|
||||
hc_down_wide_launch(f8, h, w, down, dld, scale, injw, dbuf, inj, kpart, rows, n, hc, lr,
|
||||
ncol, hrow, irow, kc, wadd, s);
|
||||
hipLaunchKernelGGL(hc_down_pf2_kernel,
|
||||
dim3((unsigned)((rows + kPf2RT - 1) / kPf2RT), (unsigned)ks),
|
||||
dim3(kThreads), 0, (hipStream_t)s, h, w, (const uint4*)plane,
|
||||
(const float*)scale, dbuf, inj, kpart, rows, n, hc, lr, ncol, ntt, hrow,
|
||||
irow, kc, wadd);
|
||||
if (hipGetLastError() != hipSuccess) return RAD_E_DEVICE;
|
||||
|
||||
if (ks > 1) {
|
||||
@@ -3924,29 +3485,15 @@ static int hc_pf2_launch(int bits, bool f8, bool i8, const unsigned short* h, co
|
||||
if (hipGetLastError() != hipSuccess) return RAD_E_DEVICE;
|
||||
}
|
||||
|
||||
if (pupp) {
|
||||
/* Enough channel runs that the row tiles times the runs fill the part twice over. */
|
||||
const long nrt = (rows + kPf2PRT - 1) / kPf2PRT, nblk = n / kWideCH;
|
||||
long runs = (64 + nrt - 1) / nrt;
|
||||
if (runs > nblk) runs = nblk;
|
||||
const long bpw = (nblk + runs - 1) / runs;
|
||||
hipLaunchKernelGGL(hc_up_mix_pf2p_kernel<20>,
|
||||
dim3((unsigned)((nblk + bpw - 1) / bpw), (unsigned)nrt), dim3(kThreads),
|
||||
0, (hipStream_t)s, (const uint4*)uswz, (const unsigned short*)dbuf, h, w,
|
||||
(const float*)scale, x, rows, n, lr, hrow, xrow, wadd, bpw);
|
||||
if (hipGetLastError() != hipSuccess) return RAD_E_DEVICE;
|
||||
hc_quant_rows_launch(i8, x, q, qs, rq, rqs, rows, n, xrow, qrow, srow, rqrow, rsrow, s);
|
||||
} else if (pup) {
|
||||
hipLaunchKernelGGL(hc_up_mix_pf2_kernel<4>,
|
||||
dim3((unsigned)(n / kWideCH), (unsigned)((rows + kPf2UpRT - 1) / kPf2UpRT)),
|
||||
dim3(kThreads), 0, (hipStream_t)s, ubf, (const unsigned short*)dbuf, h,
|
||||
w, (const float*)scale, x, rows, n, lr, hrow, xrow, wadd);
|
||||
if (hipGetLastError() != hipSuccess) return RAD_E_DEVICE;
|
||||
hc_quant_rows_launch(i8, x, q, qs, rq, rqs, rows, n, xrow, qrow, srow, rqrow, rsrow, s);
|
||||
} else {
|
||||
hc_up_mix_wide_launch(f8, i8, up, uld, dbuf, h, w, scale, x, q, qs, rq, rqs, rows, n, lr,
|
||||
hrow, xrow, qrow, srow, rqrow, rsrow, wadd, s);
|
||||
}
|
||||
/* The up product and mix: the wide kernel at 256 rows over a bf16 mix_up, whose row pitch is
|
||||
* lr -- the decoded plane, or the stored matrix at bf16. */
|
||||
hipLaunchKernelGGL((hc_up_mix_wide_kernel<4, false, kPf2UpRT>),
|
||||
dim3((unsigned)(n / kWideCH), (unsigned)((rows + kPf2UpRT - 1) / kPf2UpRT)),
|
||||
dim3(kThreads), 0, (hipStream_t)s, (const void*)ubf, lr,
|
||||
(const unsigned short*)dbuf, h, w, (const float*)scale, x, rows, n, lr, hrow,
|
||||
xrow, wadd);
|
||||
if (hipGetLastError() != hipSuccess) return RAD_E_DEVICE;
|
||||
hc_quant_rows_launch(i8, x, q, qs, rq, rqs, rows, n, xrow, qrow, srow, rqrow, rsrow, s);
|
||||
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
|
||||
@@ -4184,7 +3731,9 @@ extern "C" int64_t r4d_rad_hc_read_scratch(const RadArgs* a) {
|
||||
const long long hc = r4d_geti_or(a, "hc", 0);
|
||||
if (M <= 0 || lr <= 0 || n <= 0 || hc <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
/* `d` [M, lowrank] bf16, the per-stream norm scales [M, kHcMax] f32, and `u` [M, hc*n] bf16 --
|
||||
* 20 KiB a row at this model's shape, which is what buys the up-matvec its own grid.
|
||||
* 20 KiB a row at this model's shape, which is what buys the up-matvec its own grid. A band
|
||||
* that reaches the PF2 rule holds its weight planes in `u` as well, so `u` there has at least
|
||||
* the rows they take (hc_u_rows).
|
||||
*
|
||||
* PLUS THE SPLIT-K PARTIALS. Every row count below a few thousand takes a split, so this is
|
||||
* not a decode-only buffer and is not bounded by kTileMinRows -- hc_part_bytes
|
||||
@@ -4195,7 +3744,7 @@ extern "C" int64_t r4d_rad_hc_read_scratch(const RadArgs* a) {
|
||||
* alone and the buffer is sized for the widest ncol a fold can produce. Deriving it from ncol
|
||||
* here and from lr there gives two different split counts for the same shape, and the partial
|
||||
* buffer then overruns. */
|
||||
return M * ((long long)lr * 2 + (long long)kHcMax * 4 + hc * n * 2)
|
||||
return M * ((long long)lr * 2 + (long long)kHcMax * 4) + hc_u_rows(M, lr, hc) * HN * 2
|
||||
+ hc_part_bytes(M, lr, HN);
|
||||
}
|
||||
|
||||
@@ -4316,7 +3865,8 @@ static int rad_hc_read_body(const RadArgs* a, const R4dSink& sk) {
|
||||
/* `d`, the scales and `u` are all sized from the BAND, so a step wider than the band it was
|
||||
* planned for does not run slowly -- it writes past three buffers. */
|
||||
if (rows > Mband) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long fixed = Mband * (lr * 2 + (long long)kHcMax * 4 + HN * 2);
|
||||
const long long ubytes = hc_u_rows(Mband, lr, hc) * HN * 2;
|
||||
const long long fixed = Mband * (lr * 2 + (long long)kHcMax * 4) + ubytes;
|
||||
if (!a->scratch || a->scratch_bytes < fixed) return R4D_NO(RAD_E_SCRATCH);
|
||||
unsigned short* dbuf = (unsigned short*)a->scratch;
|
||||
float* scale = (float*)((unsigned char*)a->scratch + Mband * lr * 2);
|
||||
@@ -4368,21 +3918,23 @@ static int rad_hc_read_body(const RadArgs* a, const R4dSink& sk) {
|
||||
return R4D_NO(RAD_E_SCRATCH);
|
||||
float* part = (ks > 1) ? (float*)((unsigned char*)a->scratch + fixed) : nullptr;
|
||||
|
||||
/* THE SECOND PREFILL FORM (hc_pf2_launch), at the 128-row tile only and for the same
|
||||
* bytes. Its weight planes live in `u`, which no fused form touches, so a band too narrow
|
||||
* to hold them simply keeps the wide form -- the pin refuses instead, so a test cannot
|
||||
* pass on the form it did not ask for. */
|
||||
const long ntt = (ncol + 15) / 16;
|
||||
const int pf2 = g_hc_prefill_form >= 3 ? hc_pf2_bits(g_hc_prefill_form - 2)
|
||||
: (g_hc_prefill_form == 0 && rows >= kPf2MinRows) ? hc_pf2_env() : 0;
|
||||
const bool pf2_fits = pf2 != 0 && rt == kWideRT && hc == 4 && (n % kThreads) == 0
|
||||
&& ntt <= (long)((pf2 & kPf2DownW) ? kPf2WMaxNT : kPf2MaxNT)
|
||||
&& (!(pf2 & kPf2UpP) || lr == 20 * 16)
|
||||
&& Mband * HN * 2 >= pf2_plane_bytes(pf2, HN, lr, ntt, f8);
|
||||
if (g_hc_prefill_form >= 3 && !pf2_fits) return R4D_NO(RAD_E_SHAPE);
|
||||
if (pf2_fits) {
|
||||
/* THE SECOND PREFILL FORM (hc_pf2_launch), at the 128-row tile and for the same bytes:
|
||||
* the rule from kPf2MinRows rows wherever the shape is one it is built for -- hc 4, n a
|
||||
* multiple of 256, at most kPf2MaxNT column n-tiles -- and the wide form's below that or
|
||||
* at any other shape. Its weight planes live in `u`, which no fused form touches and which
|
||||
* the scratch hook sizes for them at every band the rule reaches, so the room check below
|
||||
* can only refuse the PIN, at a band under kPf2MinRows. The pin refuses a shape the form
|
||||
* is not built for as well, so a test cannot pass on a form it did not ask for. */
|
||||
const long ntt = (ncol + 15) / 16;
|
||||
const bool pf2_shape = rt == kWideRT && hc == 4 && (n % kThreads) == 0
|
||||
&& ntt <= (long)kPf2MaxNT;
|
||||
const bool pf2 = g_hc_prefill_form == 3
|
||||
|| (g_hc_prefill_form == 0 && rows >= kPf2MinRows && pf2_shape);
|
||||
if (pf2) {
|
||||
if (!pf2_shape) return R4D_NO(RAD_E_SHAPE);
|
||||
if (ubytes < pf2_plane_bytes(HN, lr, ntt, f8)) return R4D_NO(RAD_E_SCRATCH);
|
||||
const int prc = hc_pf2_launch(
|
||||
pf2, f8, i8, (const unsigned short*)h->data, (const float*)w->data, down->data,
|
||||
f8, i8, (const unsigned short*)h->data, (const float*)w->data, down->data,
|
||||
(long)dld, up->data, (long)uld, fold ? (const unsigned short*)injw->data : nullptr,
|
||||
dbuf, scale, (unsigned char*)ubuf, part, (unsigned short*)x->data,
|
||||
fold ? (unsigned short*)inj->data : nullptr,
|
||||
|
||||
+136
-83
@@ -879,9 +879,9 @@ __device__ __forceinline__ uint16_t u16_load_w(const uint16_t* p) {
|
||||
* why the median sits near the resident cost and the upper quantiles near one and two misses.
|
||||
*
|
||||
* SO THE LEVER IS THE MISS RATE AND NOTHING ELSE. Residency is 82% of 256 experts a layer and it
|
||||
* is not raisable here: --expert-vs-cache-ratio already gives the experts everything the KV pool
|
||||
* does not need, and the KV pool is sized by --max-num-seqs and --max-model-len, which are the
|
||||
* deployment's capability rather than a tuning knob. If every routed expert were resident this
|
||||
* is not raisable here: the KV pool lends the experts every block no live request holds, and what
|
||||
* live requests hold is set by --max-num-seqs and --max-model-len, which are the deployment's
|
||||
* capability rather than a tuning knob. If every routed expert were resident this
|
||||
* kernel would run at its resident rate and its share of device time would roughly halve. That is
|
||||
* the size of the prize for anything that raises capacity or sharpens the ranking, and it is why
|
||||
* the kernel's own arithmetic is not the lever.
|
||||
@@ -930,8 +930,13 @@ static_assert(sizeof(MoeTab) == 16, "the code table is four words");
|
||||
* its own activation scale. Same products, same sums, same order: bit-identical to FK 128.
|
||||
*
|
||||
* UW reads the weight and its scales from a uniform global base -- see the UW note at w4_load.
|
||||
* The decode form always takes it. The prefill forms keep the plain loads: the waits UW removes
|
||||
* were measured on the decode grid, and the prefill tiles' timing under UW is unmeasured, so
|
||||
* their instructions stay as they were.
|
||||
*
|
||||
* RS finds an ES block's run in one round trip -- see the RS note at the run search. */
|
||||
* RS finds an ES block's run in one round trip -- see the RS note at the run search. It covers
|
||||
* up to kMaxExpert experts; a wider expert count takes the chunked search, which numbers the runs
|
||||
* the same way. */
|
||||
template <uint32_t MT, uint32_t NTL, uint32_t WGM, uint32_t WGN, int W4 = 0,
|
||||
bool ES = false, bool AL = false, bool SK = false, uint32_t FK = kFK, bool UW = false,
|
||||
bool RS = false>
|
||||
@@ -1117,6 +1122,10 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel(
|
||||
rs_first = s_sel[1];
|
||||
rs_end = s_sel[2];
|
||||
} else if constexpr (ES) {
|
||||
/* THE CHUNKED SEARCH SERVES THE DECODE FORM ABOVE kMaxExpert EXPERTS. RS holds a thread's
|
||||
* share of the offsets in NPT registers sized for kMaxExpert, so a wider expert count is
|
||||
* this loop's, which walks any count a chunk of NTHR at a time. r4d_selftest runs the
|
||||
* decode form at 640 experts against the uniform prefill grid, byte for byte. */
|
||||
__shared__ int32_t s_run[FM];
|
||||
__shared__ uint32_t s_wc[NTHR / kWave];
|
||||
uint32_t nrun = 0;
|
||||
@@ -2341,52 +2350,36 @@ constexpr long long kPrefillCols = 128;
|
||||
static inline bool r4d_moe_decode_band(long long T, long long n_expert) {
|
||||
return 2 * T <= 3 * n_expert;
|
||||
}
|
||||
/* R4D_MOE_GU_FK256: the decode form of the TOKEN-operand projection (gate_up) stages two 128-wide
|
||||
* K blocks a pass instead of one -- see FK at the kernel. Unset or 0: one block, as upstream. 1:
|
||||
* every decode-form gate_up launch that can take it; N > 1: those of at most N routed rows. It
|
||||
* can where the weight's format already folds scale groups (W4 2 to 4) and every class's K is a
|
||||
* multiple of 256; anything else keeps one block. Bit-identical either way. Read once, so a
|
||||
* recorded pass and a live one take the same kernel. */
|
||||
static long long moe_gu_fk256() {
|
||||
static const long long v = [] {
|
||||
const char* e = std::getenv("R4D_MOE_GU_FK256");
|
||||
const long long x = e ? std::atoll(e) : 0;
|
||||
return x > 0 ? x : 0LL;
|
||||
}();
|
||||
return v;
|
||||
}
|
||||
/* THE STAGED K OF THE DECODE FORM, pinned for a test: 0 is R4D_MOE_GU_FK256's rule, 1 one 128-wide
|
||||
* block a pass, 2 two wherever the shape allows it, whatever the switch says. r4d_selftest
|
||||
* compares the two byte for byte. */
|
||||
/* THE ROW LIMIT OF TWO 128-WIDE K BLOCKS A PASS, by weight format: the decode form of the
|
||||
* TOKEN-operand projection (gate_up) stages two blocks a pass instead of one -- see FK at the
|
||||
* kernel -- at most this many routed rows. Only a shape whose weight format already folds scale
|
||||
* groups (W4 2 to 4) and whose every class's K is a multiple of 256 can take two; anything else
|
||||
* keeps one block. The two are bit-identical. A constant, so a recorded pass and a live one take
|
||||
* the same kernel.
|
||||
*
|
||||
* MEASURED, `r4d_selftest --perfmoe M` on gfx1201 at rank 0's tp2 width split (512 experts, top
|
||||
* 10), gate_up in us, one block a pass / two:
|
||||
*
|
||||
* tokens (rows) 1 (10) 2 (20) 4 (40) 8 (80) 16 (160) 32 (320)
|
||||
* w4nl64a8h (2) 17.9/15.6 25.6/22.4 36.4/37.3 65.6/67.8 164.6/170.2 328.7/310.8
|
||||
* w4nl32a8h (3) 19.5/17.0 26.2/24.0 40.0/40.5 71.6/77.7 177.9/177.5 337.0/321.0
|
||||
* w5nl64a8h (4) 20.3/17.6 30.0/25.5 122.3/43.1 212.9/78.6 373.2/219.8 672.3/380.5
|
||||
*
|
||||
* The four-bit formats gain at one and two tokens and not in between, which is where a
|
||||
* speculative decode step sits (one sequence at depth 3 is 40 rows); the five-bit format's one
|
||||
* block a pass is two to three times slower from 40 rows on, and two blocks are the faster at
|
||||
* every row count. */
|
||||
template <int W4>
|
||||
constexpr long long kMoeGuFk256Rows = W4 == 4 ? (1ll << 40) : (W4 == 2 || W4 == 3) ? 20 : 0;
|
||||
/* THE STAGED K OF THE DECODE FORM, pinned for a test: 0 is kMoeGuFk256Rows's rule, 1 one 128-wide
|
||||
* block a pass, 2 two wherever the shape allows it. r4d_selftest compares the two byte for byte,
|
||||
* so the two-block path is held to the one-block bytes whatever the row limit is. */
|
||||
static int g_moe_fk_pin = 0;
|
||||
template <int W4>
|
||||
static inline bool r4d_moe_gu_fk256(long long T, bool a_sorted) {
|
||||
if (a_sorted || g_moe_fk_pin == 1) return false;
|
||||
if (g_moe_fk_pin == 2) return true;
|
||||
const long long sw = moe_gu_fk256();
|
||||
return sw == 1 || (sw > 1 && T <= sw);
|
||||
}
|
||||
/* R4D_MOE_UWLOAD=1: the decode form -- both projections -- reads the weight and its scales from a
|
||||
* uniform global base (see UW at w4_load). Unset or 0: the generic pointer, as upstream.
|
||||
* Bit-identical either way; read once, like the others. A test pin over it: 0 the switch, 1 off,
|
||||
* 2 on. */
|
||||
static int g_moe_uw_pin = 0;
|
||||
static bool r4d_moe_uwload() {
|
||||
static const bool v = [] {
|
||||
const char* e = std::getenv("R4D_MOE_UWLOAD");
|
||||
return e && std::atoll(e) > 0;
|
||||
}();
|
||||
return g_moe_uw_pin ? g_moe_uw_pin == 2 : v;
|
||||
}
|
||||
/* R4D_MOE_RUNSCAN=1: the decode form -- both projections -- finds its block's run in one round
|
||||
* trip (see RS at the run search). Unset or 0: the chunked search, as upstream. Bit-identical;
|
||||
* read once. A test pin over it: 0 the switch, 1 off, 2 on. */
|
||||
static int g_moe_rs_pin = 0;
|
||||
static bool r4d_moe_runscan() {
|
||||
static const bool v = [] {
|
||||
const char* e = std::getenv("R4D_MOE_RUNSCAN");
|
||||
return e && std::atoll(e) > 0;
|
||||
}();
|
||||
return g_moe_rs_pin ? g_moe_rs_pin == 2 : v;
|
||||
return T <= kMoeGuFk256Rows<W4>;
|
||||
}
|
||||
/* THE MAP'S UPPER BOUND, stated once because the scratch hook and the launcher must agree about
|
||||
* it exactly: a hook that answers for a different shape than the launch uses is how a kernel ends
|
||||
@@ -2616,23 +2609,21 @@ static int moe_gemm_launch(const RadArgs* a, const R4dSink& sk, int ncls = 2) {
|
||||
/* THE DECODE GRID IS (tile, run) WIDE -- see the kernel's ES note. One tile of rows is all
|
||||
* a decode step has, so without this the whole layer runs on N/64 workgroups. */
|
||||
const unsigned tiles = (unsigned)((T + 15) / 16);
|
||||
/* WHICH INSTANTIATION: the weight from a uniform global base (R4D_MOE_UWLOAD), the run
|
||||
* search in one round trip (R4D_MOE_RUNSCAN, up to kMaxExpert experts), and two 128-wide
|
||||
* K blocks a pass (R4D_MOE_GU_FK256) where every class's K is whole passes of 256 and the
|
||||
* format folds scale groups. All of them are bit-identical; the function moves with the
|
||||
* choice, which a recorded pass sees as a new node. */
|
||||
const bool uw = r4d_moe_uwload();
|
||||
const bool rs = r4d_moe_runscan() && n_expert <= (long long)kMaxExpert;
|
||||
/* WHICH INSTANTIATION: the weight from a uniform global base (UW) always; the run search
|
||||
* in one round trip (RS) up to kMaxExpert experts and the chunked search above that; and
|
||||
* two 128-wide K blocks a pass where kMoeGuFk256Rows admits the row count, every class's
|
||||
* K is whole passes of 256 and the format folds scale groups. All of them are
|
||||
* bit-identical; the function moves with the choice, which a recorded pass sees as a new
|
||||
* node. */
|
||||
const bool rs = n_expert <= (long long)kMaxExpert;
|
||||
auto pick = [&](auto fk) {
|
||||
constexpr uint32_t F = decltype(fk)::value;
|
||||
return uw ? (rs ? &moe_gemm_q_kernel<1, 4, 1, 4, W4, true, false, false, F, true, true>
|
||||
: &moe_gemm_q_kernel<1, 4, 1, 4, W4, true, false, false, F, true>)
|
||||
: (rs ? &moe_gemm_q_kernel<1, 4, 1, 4, W4, true, false, false, F, false, true>
|
||||
: &moe_gemm_q_kernel<1, 4, 1, 4, W4, true, false, false, F>);
|
||||
return rs ? &moe_gemm_q_kernel<1, 4, 1, 4, W4, true, false, false, F, true, true>
|
||||
: &moe_gemm_q_kernel<1, 4, 1, 4, W4, true, false, false, F, true, false>;
|
||||
};
|
||||
auto kern = pick(std::integral_constant<uint32_t, kFK>{});
|
||||
if constexpr (W4 >= 2) {
|
||||
bool fk2 = r4d_moe_gu_fk256(T, a_sorted);
|
||||
bool fk2 = r4d_moe_gu_fk256<W4>(T, a_sorted);
|
||||
for (int q = 0; q < ncls; ++q) fk2 = fk2 && Kc[q] % (2 * (long long)kBlock) == 0;
|
||||
if (fk2) kern = pick(std::integral_constant<uint32_t, 2 * kFK>{});
|
||||
}
|
||||
@@ -2732,8 +2723,6 @@ extern "C" int64_t r4d_rad_moe_gemm_w4a8_scratch(const RadArgs* a) { return moe_
|
||||
|
||||
extern "C" void r4d_moe_gemm_pin_prefill(int form) { g_moe_prefill_form = form; }
|
||||
extern "C" void r4d_moe_gemm_pin_fk(int pin) { g_moe_fk_pin = pin; }
|
||||
extern "C" void r4d_moe_gemm_pin_uw(int pin) { g_moe_uw_pin = pin; }
|
||||
extern "C" void r4d_moe_gemm_pin_rs(int pin) { g_moe_rs_pin = pin; }
|
||||
|
||||
extern "C" int r4d_rad_moe_gemm_q(const RadArgs* a, RadStream s) {
|
||||
return moe_gemm_launch<0>(a, r4d_stream_sink(s));
|
||||
@@ -3000,8 +2989,8 @@ extern "C" int r4d_rad_scale_rows(const RadArgs* a, RadStream s) {
|
||||
/* ================================================================== router GEMM + router_topk_scatter
|
||||
*
|
||||
* THE ROUTER, ITS SELECTION AND ITS SORT IN ONE LAUNCH -- and, where the layer has a shared expert,
|
||||
* that expert's one-column gate beside them. rad_block_moe_fp8.h declares it under
|
||||
* R4D_ROUTER_FUSED; this is the kernel and its shim.
|
||||
* that expert's one-column gate beside them. rad_block_moe_fp8.h asks for it wherever it declares
|
||||
* router_topk_scatter and issues it up to kRgMaxM rows; this is the kernel and its shim.
|
||||
*
|
||||
* At a decode step the router is a [n_expert, n_embd] bf16 GEMM over a handful of rows, and what
|
||||
* follows it -- `router_topk_scatter` -- is one workgroup with almost no work in it. They are two
|
||||
@@ -3009,7 +2998,8 @@ extern "C" int r4d_rad_scale_rows(const RadArgs* a, RadStream s) {
|
||||
* gap plus a second launch, on the critical path. The selection needs every logit of a row, so it
|
||||
* cannot run inside a column tile; it runs in whichever workgroup finishes LAST, which an arrival
|
||||
* counter names. That is the device-wide pattern gemm_fp8a8's split-K finisher and hc_up_mix's
|
||||
* quantiser already use, and its rule is theirs: one counter a (stream, output), self-resetting.
|
||||
* quantiser already use, and its rule is theirs: self-resetting, and never shared by two launches
|
||||
* that can run at once. Here it is the op instance's, taken at init -- see THE ARRIVAL COUNTER.
|
||||
*
|
||||
* The shared expert's gate is a gemm_nt at N = 1 over the SAME input, a launch of its own for one
|
||||
* output column. It rides along as one more workgroup of this grid (`skg` > 0), so the launch the
|
||||
@@ -3175,27 +3165,87 @@ __global__ __launch_bounds__(NW * kWave) void router_gemm_topk_scatter_kernel(
|
||||
scatter_small_place(sorted, off, cnt, g.M * g.top_k, g.n_local, sl);
|
||||
}
|
||||
|
||||
/* THE ARRIVAL COUNTER IS THE OP INSTANCE'S, taken at init and not on the launch path.
|
||||
*
|
||||
* It has to persist between launches, zeroed: the last block of every launch resets it, and a
|
||||
* launch that found it non-zero would rank before the GEMM was done. A scratch hook cannot carry
|
||||
* that -- the scratch is one arena region every other op's kernel writes between two of these
|
||||
* launches. The other counters in this library (gemm_fp8a8's split_ctr, hc_bf16's mix counters)
|
||||
* are allocated on first use from the launch path, which leaves them out of the VRAM budget and
|
||||
* puts an allocation on the describe path that must not allocate. The init hook has neither
|
||||
* problem: the engine calls it once per resolved instance during declare, on the rank's device,
|
||||
* and reads the free VRAM for its budget after every init has run, so what init takes is
|
||||
* counted.
|
||||
*
|
||||
* ONE COUNTER AN INSTANCE -- one declared op, one band, one domain, one rank. Each routed layer
|
||||
* declares its own op, so two layers never share one, and the launches of one instance are
|
||||
* ordered: every one of them writes the same declared sort outputs, which the engine does not
|
||||
* reorder or overlap. The counters are 64-byte slots of one zeroed block a device, so a model's
|
||||
* few hundred instances cost one allocation; the block is freed with its last instance. */
|
||||
constexpr uint32_t kRgCtrStride = 16; /* uint32 words a slot: one 64-byte line each */
|
||||
constexpr uint32_t kRgCtrSlots = 4096; /* instances a device; a model declares hundreds */
|
||||
|
||||
struct RgCtrBlock {
|
||||
unsigned int* base = nullptr;
|
||||
uint32_t next = 0; /* slots handed out since the block was made */
|
||||
uint32_t live = 0; /* instances holding one */
|
||||
};
|
||||
struct RgInst {
|
||||
int dev = 0;
|
||||
unsigned int* ctr = nullptr;
|
||||
};
|
||||
std::mutex g_rg_mu;
|
||||
std::map<int, RgCtrBlock> g_rg_blocks;
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* THE ARRIVAL COUNTER, one a (stream, logits buffer): ctx.h's rule TWO, and gemm_fp8a8's split_ctr
|
||||
* says why the output is part of the key. Launches that share a logits buffer on one stream are
|
||||
* ordered through it -- every routed layer writes the same plane -- so they reuse one counter.
|
||||
* Zeroed once and reset by the last block of every launch. Null when it cannot be allocated; the
|
||||
* op then refuses rather than computing without it. */
|
||||
static unsigned int* router_fused_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, 64) != hipSuccess) return nullptr;
|
||||
if (hipMemset(p, 0, 64) != hipSuccess) {
|
||||
(void)hipFree(p);
|
||||
return nullptr;
|
||||
extern "C" int r4d_rad_router_gemm_topk_scatter_init(const RadParam*, int, int, int,
|
||||
void** out) {
|
||||
if (!out) return RAD_E_INVAL;
|
||||
*out = nullptr;
|
||||
int dev = 0;
|
||||
if (hipGetDevice(&dev) != hipSuccess) return RAD_E_DEVICE;
|
||||
std::lock_guard<std::mutex> lk(g_rg_mu);
|
||||
RgCtrBlock& blk = g_rg_blocks[dev];
|
||||
if (!blk.base) {
|
||||
const size_t bytes = (size_t)kRgCtrSlots * kRgCtrStride * sizeof(unsigned int);
|
||||
void* p = nullptr;
|
||||
if (hipMalloc(&p, bytes) != hipSuccess) return RAD_E_NOMEM;
|
||||
/* Zero before any launch can read it: the memset is ordered on the null stream, and the
|
||||
* engine's streams need not wait for that one, so it is waited for here. */
|
||||
if (hipMemset(p, 0, bytes) != hipSuccess || hipDeviceSynchronize() != hipSuccess) {
|
||||
(void)hipFree(p);
|
||||
return RAD_E_DEVICE;
|
||||
}
|
||||
blk.base = (unsigned int*)p;
|
||||
blk.next = 0;
|
||||
}
|
||||
blocks.emplace(key, (unsigned int*)p);
|
||||
return (unsigned int*)p;
|
||||
/* A slot is never handed out twice while its block lives: one returned by fini is not known
|
||||
* to be zero if its last launch did not finish, so it is not reused. */
|
||||
if (blk.next >= kRgCtrSlots) return RAD_E_FULL;
|
||||
RgInst* in = new RgInst;
|
||||
in->dev = dev;
|
||||
in->ctr = blk.base + (size_t)blk.next * kRgCtrStride;
|
||||
++blk.next;
|
||||
++blk.live;
|
||||
*out = in;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
extern "C" void r4d_rad_router_gemm_topk_scatter_fini(void* inst) {
|
||||
RgInst* in = (RgInst*)inst;
|
||||
if (!in) return;
|
||||
std::lock_guard<std::mutex> lk(g_rg_mu);
|
||||
auto it = g_rg_blocks.find(in->dev);
|
||||
if (it != g_rg_blocks.end() && it->second.live > 0 && --it->second.live == 0) {
|
||||
int prev = 0;
|
||||
const bool moved = hipGetDevice(&prev) == hipSuccess && prev != in->dev &&
|
||||
hipSetDevice(in->dev) == hipSuccess;
|
||||
(void)hipFree(it->second.base);
|
||||
if (moved) (void)hipSetDevice(prev);
|
||||
g_rg_blocks.erase(it);
|
||||
}
|
||||
delete in;
|
||||
}
|
||||
|
||||
/* Operands: a, b (the router), gate_b? -> logits, expert_ids, expert_w, sorted_tok,
|
||||
@@ -3267,8 +3317,11 @@ static int rad_router_gemm_topk_scatter_body(const RadArgs* a, const R4dSink& sk
|
||||
rc = cfg_for(a, K, M, 16, BR_FLAT, &cg, 1);
|
||||
if (rc != RAD_OK) return rc;
|
||||
}
|
||||
unsigned int* const ctr = router_fused_ctr(sk.stream, lg->data);
|
||||
if (!ctr) return R4D_NO(RAD_E_DEVICE);
|
||||
/* The instance's counter (see THE ARRIVAL COUNTER). A caller that ran no init has none, and
|
||||
* the op is refused: a launch without it would have no last block to rank. */
|
||||
const RgInst* const inst = (const RgInst*)a->instance;
|
||||
if (!inst || !inst->ctr) return R4D_NO(RAD_E_STATE);
|
||||
unsigned int* const ctr = inst->ctr;
|
||||
|
||||
/* THE WAVE COUNT IS THE SELECTION'S, router_topk_scatter's rule: a wave a row up to 32. The
|
||||
* GEMM then takes as many 16-column tiles a block as fills those waves at its split. */
|
||||
|
||||
+4
-1
@@ -227,7 +227,10 @@ int r4d_rad_moe_scatter (const RadArgs*, RadStream);
|
||||
int r4d_rad_router_topk_scatter(const RadArgs*, RadStream);
|
||||
int r4d_rad_router_topk_scatter_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*);
|
||||
/* The router GEMM, the selection and the sort in one launch, the shared gate optionally beside
|
||||
* them (R4D_ROUTER_FUSED in rad_block_moe_fp8.h). */
|
||||
* them. init takes the instance's arrival counter (r4d_moe.hip, THE ARRIVAL COUNTER) and fini
|
||||
* gives it back; a launch with no instance is refused. */
|
||||
int r4d_rad_router_gemm_topk_scatter_init(const RadParam*, int, int rank, int world, void** out);
|
||||
void r4d_rad_router_gemm_topk_scatter_fini(void*);
|
||||
int r4d_rad_router_gemm_topk_scatter(const RadArgs*, RadStream);
|
||||
int r4d_rad_router_gemm_topk_scatter_describe(const RadArgs*, RadStream, RadLaunchDesc*, int,
|
||||
int*);
|
||||
|
||||
+7
-4
@@ -4405,7 +4405,8 @@ static const RadKernelInfo kKernels[] = {
|
||||
"sorted slots, offsets and counts out",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cRouterGemmTopkScatter), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_router_gemm_topk_scatter, nullptr, nullptr, nullptr,
|
||||
r4d_rad_router_gemm_topk_scatter_init, r4d_rad_router_gemm_topk_scatter_fini,
|
||||
r4d_rad_router_gemm_topk_scatter, nullptr, nullptr, nullptr,
|
||||
r4d_shape_router_gemm_topk_scatter, nullptr, nullptr,
|
||||
r4d_rad_router_gemm_topk_scatter_describe },
|
||||
|
||||
@@ -5099,9 +5100,11 @@ extern "C" const RadKernelInfo* rad_kernel_at(int i) {
|
||||
* gemm_w8a8_tiled
|
||||
* 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, so two launches
|
||||
* that could run at once never share them, and the row is concurrent. A row added with state of
|
||||
* its own that the stream's order protects belongs in this list. */
|
||||
* gemm_fp8a8_nt_m16's finisher counters are keyed on the stream AND the output, and
|
||||
* router_gemm_topk_scatter_bf16's arrival counter is its op instance's (taken at init, one a
|
||||
* declared op and band), so two launches that could run at once never share them, and both rows
|
||||
* are concurrent. A row added with state of its own that the stream's order protects belongs in
|
||||
* this list. */
|
||||
extern "C" int rad_kernel_concurrent(int i) {
|
||||
const RadKernelInfo* k = rad_kernel_at(i);
|
||||
if (!k) return 0;
|
||||
|
||||
+123
-74
@@ -163,8 +163,6 @@ struct Plugin {
|
||||
void (*pin_qsa)(int) = nullptr;
|
||||
void (*pin_moe)(int) = nullptr;
|
||||
void (*pin_fk)(int) = nullptr;
|
||||
void (*pin_uw)(int) = nullptr;
|
||||
void (*pin_rs)(int) = nullptr;
|
||||
void (*pin_hc)(int) = nullptr;
|
||||
void (*pin_qsel)(int) = nullptr;
|
||||
};
|
||||
@@ -196,8 +194,6 @@ static bool load(Plugin* pl, const char* path) {
|
||||
okall &= sym(pl->h, "r4d_qsa_score_pin_form", &pl->pin_qsa);
|
||||
okall &= sym(pl->h, "r4d_moe_gemm_pin_prefill", &pl->pin_moe);
|
||||
okall &= sym(pl->h, "r4d_moe_gemm_pin_fk", &pl->pin_fk);
|
||||
okall &= sym(pl->h, "r4d_moe_gemm_pin_uw", &pl->pin_uw);
|
||||
okall &= sym(pl->h, "r4d_moe_gemm_pin_rs", &pl->pin_rs);
|
||||
okall &= sym(pl->h, "r4d_hc_read_pin_prefill", &pl->pin_hc);
|
||||
okall &= sym(pl->h, "r4d_qsa_select_pin_form", &pl->pin_qsel);
|
||||
return okall;
|
||||
@@ -672,7 +668,7 @@ static std::vector<RadParam> with_point(const std::vector<RadParam>& ps, const T
|
||||
* whose data is null, sizes and allocates scratch from the row's own hook, then synchronises so a
|
||||
* launch failure is attributed here and not to whatever runs next. */
|
||||
static void run_case(const RadKernelInfo* r, std::vector<RadTensor>& ts,
|
||||
std::vector<RadParam>& ps, int expect = RAD_OK) {
|
||||
std::vector<RadParam>& ps, int expect = RAD_OK, void* instance = nullptr) {
|
||||
for (auto& t : ts) if (!t.data && t.rank) t.data = alloc_for(t);
|
||||
|
||||
RadArgs a;
|
||||
@@ -680,6 +676,7 @@ static void run_case(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;
|
||||
a.instance = instance;
|
||||
|
||||
if (r->scratch) {
|
||||
const int64_t need = r->scratch(&a);
|
||||
@@ -1816,8 +1813,9 @@ static void topk_scatter_at(const RadKernelInfo* rf, const RadKernelInfo* rt,
|
||||
*
|
||||
* AND TWICE, BACK TO BACK, ON ONE OUTPUT. The last workgroup is named by a counter that resets
|
||||
* itself; a counter left behind by one launch would make the next one rank before the GEMM was
|
||||
* done, or never. The configurations share one logits buffer -- and with it one counter -- in
|
||||
* an order that changes the grid between consecutive launches. */
|
||||
* done, or never. The counter is the instance's, taken by the row's init as the engine takes it,
|
||||
* and every launch here shares that one instance -- and with it one counter -- in an order that
|
||||
* changes the grid between consecutive launches. A launch with no instance is refused. */
|
||||
static void router_gemm_fused_case(const Plugin& pl) {
|
||||
if (!sel("router_gemm_topk_scatter_bf16")) return;
|
||||
const RadKernelInfo* rg = find(pl, "router_gemm_topk_scatter_bf16");
|
||||
@@ -1863,6 +1861,16 @@ static void router_gemm_fused_case(const Plugin& pl) {
|
||||
ok("router_gemm_topk_scatter", false, "hipMalloc failed");
|
||||
return;
|
||||
}
|
||||
void* inst = nullptr;
|
||||
{
|
||||
std::vector<RadParam> ip = { PI("M", maxM), PI("K", maxK), PI("n_expert", maxNe),
|
||||
PI("top_k", 10), PI("norm", 1), PS("dtype", "bf16") };
|
||||
const int irc = rg->init && rg->fini
|
||||
? rg->init(ip.data(), (int)ip.size(), 0, 1, &inst) : RAD_E_INVAL;
|
||||
ok("router_gemm_topk_scatter init takes an instance", irc == RAD_OK && inst != nullptr,
|
||||
"rc=" + std::to_string(irc));
|
||||
if (irc != RAD_OK || !inst) return;
|
||||
}
|
||||
|
||||
for (const RgCase& c : cfgs) {
|
||||
/* A trained router's logits are a few units wide; these are about that, with a tie
|
||||
@@ -1948,7 +1956,7 @@ static void router_gemm_fused_case(const Plugin& pl) {
|
||||
st_memset(d_so[1], 0xFF, (size_t)maxNS * 4);
|
||||
st_memset(d_of[1], 0xFF, (size_t)(maxNe + 1) * 4);
|
||||
}
|
||||
run_case(rg, ts, ps);
|
||||
run_case(rg, ts, ps, RAD_OK, inst);
|
||||
|
||||
const std::string tag = at + (rep ? ", second launch" : "");
|
||||
auto same = [&](const char* what, void* a, void* b, size_t bytes) {
|
||||
@@ -1990,8 +1998,26 @@ static void router_gemm_fused_case(const Plugin& pl) {
|
||||
T(nullptr, RAD_BF16, { M, 1 }) };
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("K", 2560), PI("n_expert", 512),
|
||||
PI("top_k", 10), PI("norm", 1), PS("dtype", "bf16") };
|
||||
run_case(rg, ts, ps, RAD_E_SHAPE);
|
||||
run_case(rg, ts, ps, RAD_E_SHAPE, inst);
|
||||
}
|
||||
/* A served shape with no instance is refused: there is no counter to name the last block. */
|
||||
{
|
||||
const int64_t M = 4;
|
||||
std::vector<RadTensor> ts = { T(d_a, RAD_BF16, { M, 2560 }),
|
||||
T(d_w, RAD_BF16, { 512, 2560 }),
|
||||
T(d_wg, RAD_BF16, { 1, 2560 }),
|
||||
T(d_lg[1], RAD_BF16, { M, 512 }),
|
||||
T(d_id[1], RAD_I32, { M, 10 }),
|
||||
T(d_ew[1], RAD_BF16, { M, 10 }),
|
||||
T(d_so[1], RAD_I32, { M * 10 }),
|
||||
T(d_of[1], RAD_I32, { 513 }),
|
||||
T(d_cn[1], RAD_I32, { 512 }),
|
||||
T(d_g[1], RAD_BF16, { M, 1 }) };
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("K", 2560), PI("n_expert", 512),
|
||||
PI("top_k", 10), PI("norm", 1), PS("dtype", "bf16") };
|
||||
run_case(rg, ts, ps, RAD_E_STATE);
|
||||
}
|
||||
rg->fini(inst);
|
||||
}
|
||||
|
||||
/* THE PROTECTED EXPERTS ARE SORTED LAST, against a host stable sort by place. A layer that keeps a
|
||||
@@ -5090,7 +5116,10 @@ static void moe_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E = 512,
|
||||
struct Proj { const char* name; int64_t N0, K0, N1, K1, parts; bool srt; };
|
||||
const Proj projs[2] = { { "gate_up", 768, 2560, 512, 2560, 2, false },
|
||||
{ "down", 2560, 384, 2560, 256, 1, true } };
|
||||
const int nforms = pl.pin_moe ? 3 : 1;
|
||||
/* Form 4, where the plugin has the pin, is the decode form at two 128-wide K blocks a pass:
|
||||
* the timing kMoeGuFk256Rows is set from. Only gate_up has it (K a multiple of 256 and a
|
||||
* format that folds scale groups); elsewhere it runs as form 2. */
|
||||
const int nforms = pl.pin_moe ? (pl.pin_fk ? 4 : 3) : 1;
|
||||
for (const Proj& P : projs) {
|
||||
const int64_t N = std::max(P.N0, P.N1), K = std::max(P.K0, P.K1), kb = K / group;
|
||||
const int64_t Ma = P.srt ? Tr : Mtok;
|
||||
@@ -5225,7 +5254,8 @@ static void moe_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E = 512,
|
||||
const std::string pname = std::string(P.name) + (w5 ? " w5" : sub == 4 ? " g32"
|
||||
: g64 ? " g64" : "");
|
||||
for (int f = 0; f < nforms; ++f) {
|
||||
if (pl.pin_moe) pl.pin_moe(f + 1);
|
||||
if (pl.pin_moe) pl.pin_moe(f == 3 ? 2 : f + 1);
|
||||
if (pl.pin_fk) pl.pin_fk(f == 3 ? 2 : 1);
|
||||
const int rc = r->launch(&a, nullptr);
|
||||
if (rc != RAD_OK || st_stream_sync(nullptr) != hipSuccess) {
|
||||
std::printf(" %-12s form %d rc=%d (not served)\n", pname.c_str(), f + 1, rc);
|
||||
@@ -5275,9 +5305,14 @@ static void moe_perf(const Plugin& pl, int64_t Mtok, int iters, int64_t E = 512,
|
||||
}
|
||||
}
|
||||
if (pl.pin_moe) pl.pin_moe(0);
|
||||
if (pl.pin_fk) pl.pin_fk(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* The launcher's kPf2MinRows: from this band the rule takes the PF2 form and the scratch hook
|
||||
* sizes `u` for its weight planes; below it the pin to PF2 is refused. */
|
||||
static constexpr int64_t kHcPf2MinRows = 512;
|
||||
|
||||
/* THE GATED RESIDUAL'S READ AT INT8 CODES. The same inputs run twice, the codes plane once E4M3
|
||||
* and once I8: the bf16 block input and the rotated E4M3 twin must come out byte-identical -- the
|
||||
* codes' format is the only thing the dtype changes -- and the int8 codes must be the bf16 input
|
||||
@@ -5325,12 +5360,7 @@ static void hc_read_i8(const Plugin& pl, int64_t M, bool real_stream, int form)
|
||||
void* d_rq = dalloc(qb);
|
||||
void* d_rs = dalloc(sb);
|
||||
static const char* const kForm[] = { "the launcher's rule", "the decode tiles' kernels",
|
||||
"the wide kernels", "the PF2 kernels",
|
||||
"the PF2 down product", "the PF2 up product",
|
||||
"the PF2 kernels at 128 rows",
|
||||
"the PF2 down and persistent up products",
|
||||
"the PF2 128-row down and persistent up products",
|
||||
"the PF2 persistent up product" };
|
||||
"the wide kernels", "the PF2 kernels" };
|
||||
const std::string nm = fmt("hc_read_e4m3 int8 codes at M=%lld (%s, %s)", (long long)M,
|
||||
real_stream ? "a real stream" : "the null stream", kForm[form]);
|
||||
if (!d_h || !d_w || !d_dn || !d_up || !d_iw || !d_x || !d_inj || !d_q || !d_qs || !d_rq || !d_rs) {
|
||||
@@ -5409,13 +5439,18 @@ static void hc_read_i8(const Plugin& pl, int64_t M, bool real_stream, int form)
|
||||
* split. Random codes and small positive scales: what is timed is the traffic and the arithmetic.
|
||||
*
|
||||
* EVERY FORM OF THE PREFILL TILE is run -- the decode tiles' kernels at 128 rows, the wide kernels
|
||||
* and, where the band can hold their weight planes, the PF2 kernels, through the
|
||||
* r4d_hc_read_pin_prefill hook -- and their outputs compared BYTE FOR BYTE, which is what the wide
|
||||
* and the PF2 kernels claim. `timed` times each form as well. */
|
||||
static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
* and, from kHcPf2MinRows rows, the PF2 kernels, through the r4d_hc_read_pin_prefill hook -- and
|
||||
* their outputs compared BYTE FOR BYTE, which is what the wide and the PF2 kernels claim. `timed`
|
||||
* times each form as well.
|
||||
*
|
||||
* `f8` picks the row: hc_read_e4m3, or hc_read_bf16 over bf16 mixing matrices. Both enter the same
|
||||
* launcher and both reach PF2, whose prologue then copies mix_down where it decodes it at E4M3 and
|
||||
* whose up product reads the stored mix_up where it reads a decoded one -- a path of its own. */
|
||||
static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed, bool f8 = true) {
|
||||
if (iters <= 0) iters = 20;
|
||||
const RadKernelInfo* r = find(pl, "hc_read_e4m3");
|
||||
if (!r) { skip("hc_read_e4m3 prefill forms", "row absent"); return; }
|
||||
const char* const rn = f8 ? "hc_read_e4m3" : "hc_read_bf16";
|
||||
const RadKernelInfo* r = find(pl, rn);
|
||||
if (!r) { skip(fmt("%s prefill forms", rn).c_str(), "row absent"); return; }
|
||||
const int64_t n = 2560, hc = 4, lr = 320, HN = hc * n, grp = 128;
|
||||
const int64_t dld = r4d_hc8::row_bytes(HN, r4d_hc8::down_group(HN));
|
||||
const int64_t uld = r4d_hc8::row_bytes(lr, r4d_hc8::up_group(lr));
|
||||
@@ -5436,8 +5471,16 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
}
|
||||
return b;
|
||||
};
|
||||
std::vector<uint8_t> down = plane(lr, HN, r4d_hc8::down_group(HN), dld);
|
||||
std::vector<uint8_t> up = plane(HN, lr, r4d_hc8::up_group(lr), uld);
|
||||
/* bf16 weights of the E4M3 planes' magnitude: |w| up to 0.25. */
|
||||
auto bplane = [&](int64_t count) {
|
||||
std::vector<uint8_t> b((size_t)(count * 2));
|
||||
uint16_t* v = (uint16_t*)b.data();
|
||||
for (int64_t i = 0; i < count; ++i)
|
||||
v[i] = h_to_bf16((float)((int)(rnd() % 2001) - 1000) / 4000.0f);
|
||||
return b;
|
||||
};
|
||||
std::vector<uint8_t> down = f8 ? plane(lr, HN, r4d_hc8::down_group(HN), dld) : bplane(lr * HN);
|
||||
std::vector<uint8_t> up = f8 ? plane(HN, lr, r4d_hc8::up_group(lr), uld) : bplane(HN * lr);
|
||||
std::vector<uint16_t> injw((size_t)(hc * HN));
|
||||
for (auto& v : injw) v = h_to_bf16((float)((int)(rnd() % 201) - 100) / 50000.0f);
|
||||
|
||||
@@ -5455,7 +5498,7 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
void* d_rq = dalloc(qb);
|
||||
void* d_rs = dalloc(sb);
|
||||
if (!d_h || !d_w || !d_dn || !d_up || !d_iw || !d_x || !d_inj || !d_q || !d_qs || !d_rq || !d_rs) {
|
||||
ok("hc_read_e4m3 prefill forms: allocation", false);
|
||||
ok(fmt("%s prefill forms: allocation", rn).c_str(), false);
|
||||
return;
|
||||
}
|
||||
st_memcpy(d_h, hh.data(), hh.size() * 2, hipMemcpyHostToDevice);
|
||||
@@ -5466,9 +5509,10 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
|
||||
/* The mixing matrices arrive at their DECLARED shape with the stored dtype, as the engine
|
||||
* hands them; the launcher derives the stored row pitch itself. */
|
||||
const uint32_t mdt = f8 ? (uint32_t)RAD_F8E4M3 : (uint32_t)RAD_BF16;
|
||||
std::vector<RadTensor> ts = {
|
||||
T(d_h, RAD_BF16, { M, HN }), T(d_w, RAD_F32, { HN }),
|
||||
T(d_dn, RAD_F8E4M3, { lr, HN }), T(d_up, RAD_F8E4M3, { HN, lr }),
|
||||
T(d_dn, mdt, { lr, HN }), T(d_up, mdt, { HN, lr }),
|
||||
T(d_iw, RAD_BF16, { hc, HN }), T(d_x, RAD_BF16, { M, n }),
|
||||
T(d_inj, RAD_BF16, { M, hc }), T(d_q, RAD_F8E4M3, { M, n }),
|
||||
T(d_qs, RAD_F32, { M, n / grp }), T(d_rq, RAD_F8E4M3, { M, n }),
|
||||
@@ -5477,7 +5521,7 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
std::vector<RadParam> ps = {
|
||||
PI("M", M), PI("n", n), PI("hc", hc), PI("lowrank", lr), PF("eps", 1e-6),
|
||||
PS("dtype", "bf16"), PF("wadd", 1.0), PI("inject", 1), PI("group", grp),
|
||||
PI("rotate", 1), PS("mix", "e4m3"),
|
||||
PI("rotate", 1), PS("mix", f8 ? "e4m3" : "bf16"),
|
||||
};
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
@@ -5490,21 +5534,16 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
/* A REAL STREAM, because the op keys per-stream state on it -- the fused forms' arrival
|
||||
* counters -- and on the null stream it finds none and takes the unfused path. */
|
||||
hipStream_t hs = nullptr;
|
||||
if (hipStreamCreate(&hs) != hipSuccess) { ok("hc_read_e4m3 prefill forms: stream", false); return; }
|
||||
if (hipStreamCreate(&hs) != hipSuccess) {
|
||||
ok(fmt("%s prefill forms: stream", rn).c_str(), false);
|
||||
return;
|
||||
}
|
||||
const RadStream rst = (RadStream)hs;
|
||||
|
||||
struct Out { std::vector<uint8_t> x, inj, q, qs, rq, rs; };
|
||||
/* PF2 keeps its two weight planes in the band's `u` buffer, M * hc * n bf16: they need
|
||||
* ceil((lr + hc) / 16) * 16 + lr rows of it, so a smaller M is the wide form's alone. */
|
||||
const int64_t pf2_min_m = ((lr + hc + 15) / 16) * 16 + lr;
|
||||
const int f_lo = pl.pin_hc ? 1 : 0, f_hi = pl.pin_hc ? (M >= pf2_min_m ? 9 : 2) : 0;
|
||||
const int f_lo = pl.pin_hc ? 1 : 0, f_hi = pl.pin_hc ? (M >= kHcPf2MinRows ? 3 : 2) : 0;
|
||||
static const char* const kForm[] = { "the launcher's rule", "the decode tiles' kernels",
|
||||
"the wide kernels", "the PF2 kernels",
|
||||
"the PF2 down product", "the PF2 up product",
|
||||
"the PF2 kernels at 128 rows",
|
||||
"the PF2 down and persistent up products",
|
||||
"the PF2 128-row down and persistent up products",
|
||||
"the PF2 persistent up product" };
|
||||
"the wide kernels", "the PF2 kernels" };
|
||||
std::vector<Out> outs;
|
||||
for (int form = f_lo; form <= f_hi; ++form) {
|
||||
if (pl.pin_hc) pl.pin_hc(form);
|
||||
@@ -5512,7 +5551,7 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
st_memset(d_qs, 0, sb); st_memset(d_rq, 0, qb); st_memset(d_rs, 0, sb);
|
||||
const int rc = r->launch(&a, rst);
|
||||
if (rc != RAD_OK || st_stream_sync(hs) != hipSuccess) {
|
||||
ok(fmt("hc_read_e4m3 prefill form '%s' at M=%lld is served", kForm[form],
|
||||
ok(fmt("%s prefill form '%s' at M=%lld is served", rn, kForm[form],
|
||||
(long long)M).c_str(), false, fmt("rc=%d", rc));
|
||||
if (pl.pin_hc) pl.pin_hc(0);
|
||||
(void)hipStreamDestroy(hs);
|
||||
@@ -5538,8 +5577,8 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
/* A zero output would pass a byte comparison between two forms that both wrote nothing. */
|
||||
size_t nz = 0;
|
||||
for (uint8_t v : outs[f].q) nz += v != 0;
|
||||
ok(fmt("hc_read_e4m3 prefill tile at M=%lld, %s: byte-identical to the decode tiles' "
|
||||
"kernels", (long long)M, what).c_str(),
|
||||
ok(fmt("%s prefill tile at M=%lld, %s: byte-identical to the decode tiles' kernels", rn,
|
||||
(long long)M, what).c_str(),
|
||||
bad.empty() && nz > qb / 2, bad.empty() ? fmt("%zu of %zu codes non-zero", nz, qb)
|
||||
: "differs in" + bad);
|
||||
};
|
||||
@@ -5570,8 +5609,8 @@ static void hc_prefill(const Plugin& pl, int64_t M, int iters, bool timed) {
|
||||
st_ev_destroy(e0); st_ev_destroy(e1);
|
||||
const double us = (double)ms * 1000.0 / iters;
|
||||
const double flop = 2.0 * (double)M * (double)HN * (double)(2 * lr + hc);
|
||||
std::printf("hc_read_e4m3 at M=%lld (n %lld, hc %lld, lowrank %lld, inject, fp8 twin + "
|
||||
"rotated), %s: %.1f us a call, %.1f TFLOP/s\n", (long long)M,
|
||||
std::printf("%s at M=%lld (n %lld, hc %lld, lowrank %lld, inject, fp8 twin + "
|
||||
"rotated), %s: %.1f us a call, %.1f TFLOP/s\n", rn, (long long)M,
|
||||
(long long)n, (long long)hc, (long long)lr, kForm[form], us,
|
||||
flop / (us * 1e6));
|
||||
}
|
||||
@@ -6072,43 +6111,30 @@ static float st_e4m3(uint8_t b) {
|
||||
* its part and its down projection reads a shorter K. */
|
||||
struct MoeW4Shape { const char* name; int64_t N0, K0, N1, K1, parts, ne; };
|
||||
|
||||
/* THE DECODE FORM'S BIT-IDENTICAL VARIANTS, each against the default, byte for byte: the weight from
|
||||
* a uniform global base (R4D_MOE_UWLOAD), the run search in one round trip (R4D_MOE_RUNSCAN), two
|
||||
* 128-wide K blocks a pass (R4D_MOE_GU_FK256), and every combination. Every launch is DESCRIBED
|
||||
* first: a variant that named the default's kernel would match it trivially, so two pins naming
|
||||
* one function is a failure, not a pass. `fk2` is whether this shape can take two blocks a pass --
|
||||
* the token operand, a format that folds scale groups, every class's K a multiple of 256; the
|
||||
* other two are open to every shape. The default here is PINNED, so an R4D_MOE_* switch in the
|
||||
* environment cannot make the comparison one variant against itself.
|
||||
/* THE DECODE FORM'S STAGED K, BOTH WAYS, byte for byte: one 128-wide K block a pass and two. Every
|
||||
* launch is DESCRIBED first: a pin that named the other's kernel would match it trivially, so two
|
||||
* pins naming one function is a failure, not a pass. `fk2` is whether this shape can take two
|
||||
* blocks a pass -- the token operand, a format that folds scale groups, every class's K a multiple
|
||||
* of 256; a shape that cannot has one form only, which the form loop already held to the host
|
||||
* decode. Both launches are PINNED, so the launcher's row limit cannot make the comparison one form
|
||||
* against itself.
|
||||
*
|
||||
* A row with no describe hook -- moe_gemm_fp8a8_mod4 -- is compared on its bytes alone, and says
|
||||
* so in its label: that the variant ran is then not shown, only that what ran agrees. */
|
||||
static void moe_dec_variants(const Plugin& pl, const RadKernelInfo* row, RadArgs& a, void* d_y,
|
||||
size_t elems, bool fk2, const std::string& what) {
|
||||
if (!pl.pin_fk || !pl.pin_uw || !pl.pin_rs || !pl.pin_moe) return;
|
||||
if (!fk2 || !pl.pin_fk || !pl.pin_moe) return;
|
||||
const bool described = row->describe != nullptr;
|
||||
struct Var { int uw, rs, fk; const char* name; };
|
||||
static const Var kVar[] = {
|
||||
{ 1, 1, 1, "as upstream" },
|
||||
{ 2, 1, 1, "uniform weight base" },
|
||||
{ 1, 2, 1, "one-pass run search" },
|
||||
{ 2, 2, 1, "uniform weight base, one-pass run search" },
|
||||
{ 1, 1, 2, "two K blocks a pass" },
|
||||
{ 2, 1, 2, "uniform weight base, two K blocks a pass" },
|
||||
{ 1, 2, 2, "one-pass run search, two K blocks a pass" },
|
||||
{ 2, 2, 2, "uniform weight base, one-pass run search, two K blocks a pass" },
|
||||
};
|
||||
struct Var { int fk; const char* name; };
|
||||
static const Var kVar[] = { { 1, "one K block a pass" }, { 2, "two K blocks a pass" } };
|
||||
constexpr int kN = (int)(sizeof(kVar) / sizeof(kVar[0]));
|
||||
const void* fn[kN] = {};
|
||||
std::vector<uint16_t> base;
|
||||
for (int v = 0; v < kN; ++v) {
|
||||
if (kVar[v].fk == 2 && !fk2) continue;
|
||||
const std::string label = what + " decode form, " + kVar[v].name +
|
||||
(described ? "" : " (launch not described)");
|
||||
auto pin = [&](bool on) {
|
||||
pl.pin_moe(on ? 2 : 0);
|
||||
pl.pin_uw(on ? kVar[v].uw : 0);
|
||||
pl.pin_rs(on ? kVar[v].rs : 0);
|
||||
pl.pin_fk(on ? kVar[v].fk : 0);
|
||||
};
|
||||
if (described) {
|
||||
@@ -6390,7 +6416,20 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int
|
||||
" elements off, worst " + std::to_string(worst) + first);
|
||||
two_cls[form] = got;
|
||||
}
|
||||
/* The decode form's variants; two K blocks a pass where the switch can take this shape. */
|
||||
/* THE DECODE FORM AGAINST THE UNIFORM PREFILL GRID, byte for byte. Every form keeps each
|
||||
* output element's accumulation order, so the bytes agree exactly, not within the host
|
||||
* decode's tolerance. The decode form finds its runs by the one-round-trip search up to 512
|
||||
* experts and by the chunked search above that; the uniform grid walks the offsets from a
|
||||
* binary search, so this holds whichever search the expert count picks to a third way of
|
||||
* finding the same runs. */
|
||||
if (!two_cls[2].empty() && !two_cls[3].empty()) {
|
||||
int64_t diff = 0;
|
||||
for (size_t i = 0; i < two_cls[2].size(); ++i) diff += two_cls[2][i] != two_cls[3][i];
|
||||
ok((what + " decode form, byte for byte with the uniform prefill grid").c_str(),
|
||||
diff == 0, std::to_string(diff) + " of " + std::to_string(Tr * N) +
|
||||
" elements differ");
|
||||
}
|
||||
/* The decode form's staged K both ways, where this shape can take two blocks a pass. */
|
||||
const bool fk2 = g64 && sh.K0 % 256 == 0 && sh.K1 % 256 == 0;
|
||||
moe_dec_variants(pl, r, a, d_y, (size_t)(Tr * N), fk2, what);
|
||||
if (!four) return;
|
||||
@@ -10587,6 +10626,10 @@ static void device_phase(const Plugin& pl) {
|
||||
/* Three passes of 256 along K: the decode form's two-block pass then also runs its
|
||||
* one-ahead prefetch, which a single pass never reaches. */
|
||||
{ "40 experts, gate/up 2:1, K 768", 256, 768, 128, 768, 2, 40 },
|
||||
/* Past 512 experts: the decode form's chunked run search, which the one-round-trip
|
||||
* search hands every wider expert count to. Runs land on experts on both sides of
|
||||
* 512, and K 256 lets the two-block pass run on that search as well. */
|
||||
{ "640 experts, gate/up 2:1, K 256", 256, 256, 128, 256, 2, 640 },
|
||||
};
|
||||
for (const MoeW4Shape& sh : kShapes) {
|
||||
moe_w4_gemm(pl, 24, sh);
|
||||
@@ -10611,6 +10654,7 @@ static void device_phase(const Plugin& pl) {
|
||||
{ "down 2:1", 64, 256, 64, 128, 1, 10 },
|
||||
{ "300 experts, gate/up 2:1", 256, 128, 128, 128, 2, 300 },
|
||||
{ "40 experts, gate/up 2:1, K 768", 256, 768, 128, 768, 2, 40 },
|
||||
{ "640 experts, gate/up 2:1, K 256", 256, 256, 128, 256, 2, 640 },
|
||||
};
|
||||
for (const MoeW4Shape& sh : kShapes4)
|
||||
for (const int64_t t : { (int64_t)24, (int64_t)200, (int64_t)600 }) {
|
||||
@@ -10635,22 +10679,27 @@ static void device_phase(const Plugin& pl) {
|
||||
}
|
||||
|
||||
if (sel("hc_read_e4m3")) {
|
||||
if (!g_case) std::printf("\nthe gated residual's read at the prefill tile, the wide "
|
||||
"kernels against the decode tiles'\n");
|
||||
if (!g_case) std::printf("\nthe gated residual's read at the prefill tile, every form "
|
||||
"against the decode tiles'\n");
|
||||
/* 700 and 1000 put PF2's K chunks across a stream boundary and its row tiles past the
|
||||
* last row; 4096 is the one-chunk epilogue. */
|
||||
for (int64_t m : { 65, 300, 700, 1000, 2048, 4096 }) hc_prefill(pl, m, 0, false);
|
||||
for (int64_t m : { 1, 4, 33, 65, 300, 2048 }) {
|
||||
/* 1 to 7 rows are one up-product row group each, every instantiation below eight. */
|
||||
for (int64_t m : { 1, 2, 3, 4, 5, 6, 7, 33, 65, 300, 2048 }) {
|
||||
hc_read_i8(pl, m, false, 0);
|
||||
hc_read_i8(pl, m, true, 0);
|
||||
}
|
||||
if (pl.pin_hc) {
|
||||
for (int64_t m : { 65, 2048 })
|
||||
for (int form : { 1, 2 }) hc_read_i8(pl, m, true, form);
|
||||
for (int64_t m : { 700, 2048 })
|
||||
for (int form = 3; form <= 9; ++form) hc_read_i8(pl, m, true, form);
|
||||
for (int64_t m : { 700, 2048 }) hc_read_i8(pl, m, true, 3);
|
||||
}
|
||||
}
|
||||
if (sel("hc_read_bf16")) {
|
||||
if (!g_case) std::printf("\nthe gated residual's read over bf16 mixing matrices at the "
|
||||
"prefill tile, every form against the decode tiles'\n");
|
||||
for (int64_t m : { 300, 700, 2048 }) hc_prefill(pl, m, 0, false, false);
|
||||
}
|
||||
|
||||
if (sel("moe_gemm_w4a8")) {
|
||||
if (!g_case) std::printf("\nthe arranged plane, against the stated format and its "
|
||||
@@ -10934,7 +10983,7 @@ static void usage(void) {
|
||||
" rows -- at one shape, each with its TFLOP/s.\n"
|
||||
" --perfw2 N K M [iters]: the int2 DFlash2 draft head at every variant it has.\n"
|
||||
" --perfhc M [iters]: hc_read_e4m3 at Qwen3.8-Flash-Next's shape and M rows, every\n"
|
||||
" prefill-tile form (PF2 from M=656) compared byte for byte and timed\n"
|
||||
" prefill-tile form (PF2 from M=512) compared byte for byte and timed\n"
|
||||
" as one op each;\n"
|
||||
" run it under libr4d/isa/trace.sh for its launches.\n"
|
||||
" --perfmoe M [iters] [experts] [top_k] [pooled%%]: the routed-expert w4 GEMM,\n"
|
||||
|
||||
@@ -1293,7 +1293,9 @@ extern "C" int r4d_shape_router_gemm_topk_scatter(const RadParam* p, int n_p, in
|
||||
const int64_t K = pi(p, n_p, "K");
|
||||
const int64_t ne = pi(p, n_p, "n_expert");
|
||||
const int64_t k = pi(p, n_p, "top_k");
|
||||
if (ne <= 0 || ne > 256 || k <= 0 || k > ne || K <= 0) return RAD_E_UNSUPPORTED;
|
||||
/* 512 experts: the selection keeps sixteen logits a lane (kMaxExpert in r4d_moe.hip), which
|
||||
* is the shim's bound and the row's constraint. */
|
||||
if (ne <= 0 || ne > 512 || k <= 0 || k > ne || K <= 0) return RAD_E_UNSUPPORTED;
|
||||
if (M > 64 || M * k > 1024) return RAD_E_UNSUPPORTED;
|
||||
const int64_t nl = pi(p, n_p, "n_local", ne);
|
||||
if (nl <= 0 || nl > ne) return RAD_E_UNSUPPORTED;
|
||||
|
||||
Reference in new issue
Block a user