Narrow BF16 activations inside asymmetric W4 decode GEMMs

Replace the separate BF16-to-F16 activation cast with the same nearest-even narrowing on load. Keep existing split-K and WMMA arithmetic. Test finite and raw-bit activations, canonical arithmetic, byte parity and output guards; provide BF16-input cold-weight timing sweeps.
This commit is contained in:
Constantin authored and StillDeadcode committed 2026-10-27 18:28:46 +01:00
1 parent aeedae4027
commit 492d33c470
6 files changed
+207 -64

No files matched your search

+6 -14
View File
@@ -748,8 +748,8 @@ struct LinearFP8 {
* gemm_nt_q at w5a16 over u5 codes with an f16 scale and a zero point. */
bool paro5 = false;
/* A 4-bit weight with a zero point and no rotation (linear_is_w4): the int4 W4A16 releases.
* At or below `w4_split` rows `op_a16` casts the input's bf16 plane to f16 (`a16`) for the
* w4a16_asym GEMM `op`; above it `op_q` quantises it to int8 with a scale per (row, 128)
* At or below `w4_split` rows the w4a16_asym GEMM `op` reads the input's bf16 plane and
* narrows it on load; above it `op_q` quantises it to int8 with a scale per (row, 128)
* (`a8q`, `a8s`) for the w4a8i8_asym GEMM `op2`. */
bool w4 = false;
int64_t w4_split = 0;
@@ -1289,7 +1289,7 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
/* ---- 4-bit weight with a zero point and no rotation: the rotated int4's two GEMMs over this
* linear's own copies of the input's bf16 plane. At or below kSplit rows (the skinny f16
* kernel's ceiling) the plane is cast to f16 for w4a16_asym; above it, where that kernel is
* kernel's ceiling) the GEMM reads the bf16 plane and narrows it on load; above it, where that kernel is
* bound by the f16 matrix instruction, it is quantised to int8 with an f32 scale per
* (row, 128) for w4a8i8_asym. Two views: codes, and (scale, zero) as `b_scale`, both at the
* logical extents, so row parts and spans apply to both as they are; a column slice must be
@@ -1331,15 +1331,8 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
const int64_t hi = max_rows > 0 ? max_rows : g.max_tok;
const int64_t kSplit = 64;
const int64_t f16_hi = hi > kSplit ? kSplit : hi;
a16 = decl_b(b, nm.f("%s.a16", base), RAD_F16, { f16_hi, K });
if (!a16) return RAD_E_INVAL;
op_a16 = rw(b, RAD_OP(b, "cast",
RAD_PARAMS(RAD_RANGE("M", 1, f16_hi), RAD_INT("n", K),
RAD_STR("from", g.dtype), RAD_STR("to", "f16")),
RAD_NOWEIGHTS),
{ a.x }, { a16 });
op = decl_gemm_q(b, 1, f16_hi, RAD_FP8_BLOCK, "w4a16_asym", 0, 0, { w, ws }, { a16 }, y);
if (!op_a16 || !op) return RAD_E_INVAL;
op = decl_gemm_q(b, 1, f16_hi, RAD_FP8_BLOCK, "w4a16_asym", 0, 0, { w, ws }, { a.x }, y);
if (!op) return RAD_E_INVAL;
if (hi > kSplit) {
w4_split = kSplit;
a8q = decl_b(b, nm.f("%s.a8q", base), RAD_I8, { hi, K });
@@ -1556,8 +1549,7 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
return;
}
if (w4) {
RAD_ISSUE_N(c, op_a16, rows, brow_slice(a.x, r0, rows, K), brow_slice(a16, 0, rows, K));
RAD_ISSUE_N(c, op, rows, brow_slice(a16, 0, rows, K), RAD_NONE, RAD_W(w), RAD_W(ws),
RAD_ISSUE_N(c, op, rows, brow_slice(a.x, r0, rows, K), RAD_NONE, RAD_W(w), RAD_W(ws),
brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
return;
}
+15 -6
View File
@@ -115,14 +115,23 @@ diverge by about 1 ULP.
6. Add a case to `libr4d/r4d_selftest.cpp` so the row is called at least once; a row without one
reports `no smoke case`.
## Measuring asymmetric W4 GEMMs
## Measuring W4 and BF16 decode GEMMs
`r4d_selftest <libr4d.so> --perfw4 N K M [iters]` sweeps configurations of the asymmetric
W4-weight/F16-activation kernel used by HYV3's attention and dense MLP at decode. For example,
`--perfw4 4096 2048 1` measures its TP1 query projection. Activations are initialized and weights
rotate through more than 256 MiB, including at small shapes. This is a timing diagnostic, not a
correctness check or whole-model throughput result; validate candidate settings separately before
changing deployment defaults. It does not measure the routed-expert `moe_gemm_q` kernel.
W4-weight/F16-activation kernel. `--perfw4bf16` has the same arguments but reads BF16 activations,
with the existing BF16-to-F16 conversion inside the GEMM, as HYV3's attention and dense MLP do at
decode. For example, `--perfw4bf16 4096 2048 1` measures its TP1 query projection. Activations are
initialized and weights rotate through more than 256 MiB, including at small shapes.
`--perfbf16 N K M [iters]` sweeps the plain BF16 skinny GEMMs. Its weights also rotate through
more than 256 MiB and at least two complete banks, without a copy-count cap that would leave small
shared-expert projections in cache. HYV3 TP1's shared gate/up and down are `1536 2048 1` and
`2048 768 1` respectively.
These are timing diagnostics, not correctness checks or whole-model throughput results; validate
candidate settings numerically and end to end before keeping them. Store machine-specific choices
in the normal rad-tune cache, not architecture-specific launch overrides. Neither dense sweep
measures the routed-expert `moe_gemm_q` kernel.
`r4d_selftest <libr4d.so> --perfhyv3moe M [iters]` measures HYV3's actual TP1
signed-I4 expert projections (gate/up 1536x2048, down 2048x768, top-8 of 128).
+21 -13
View File
@@ -47,11 +47,17 @@ typedef __attribute__((address_space(1))) const uint4v* r4d_gw4;
typedef __attribute__((address_space(1))) const unsigned long long* r4d_gu2;
typedef __attribute__((address_space(1))) const unsigned* r4d_gu1;
__device__ __forceinline__ v8h fragA(r4d_gu2 p, int u) {
union { unsigned long long w[2]; v8h f; } x;
__device__ __forceinline__ v8h fragA(r4d_gu2 p, int u, int bf) {
union { unsigned long long w[2]; uint16_t h[8]; v8h f; } x;
x.w[0] = p[4 * u];
x.w[1] = p[4 * u + 2];
return x.f;
if (!bf) return x.f;
// Same narrowing as narrow_f16_kernel: bf16 -> f32 -> f16, nearest even.
v8h f;
#pragma unroll
for (int i = 0; i < 8; i++)
f[i] = (_Float16)__builtin_bit_cast(float, (uint32_t)x.h[i] << 16);
return f;
}
// Do not fold the group scale into the B fragment; see r4d_gemm_w4a16_nt_m64.hip.
@@ -87,7 +93,8 @@ template <int MT, int NPW, bool NT, bool HI, bool ONE = false>
__global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel(
const unsigned short* __restrict__ A, const unsigned* __restrict__ Wq,
const unsigned* __restrict__ Whi, const unsigned* __restrict__ Wsz,
__hip_bfloat16* __restrict__ C, int M, int K, int N, int WV, int SK, int lda, int ldc) {
__hip_bfloat16* __restrict__ C, int M, int K, int N, int WV, int SK, int lda, int ldc,
int bf) {
extern __shared__ float red[]; // WV*NPW*SK*256 floats: a 16x16 tile per (column, split)
const int tid = threadIdx.x, wave = tid / WAVE32, lane = tid % WAVE32;
const int col_local = wave / SK, ksplit = wave % SK;
@@ -165,7 +172,7 @@ __global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel(
const int u = b * 4 + s;
v8h afrag[MT];
#pragma unroll
for (int i = 0; i < MT; i++) afrag[i] = fragA(pa[i], u); // loaded once, spent NPW times
for (int i = 0; i < MT; i++) afrag[i] = fragA(pa[i], u, bf); // loaded once, spent NPW times
#pragma unroll
for (int j = 0; j < NPW; j++) {
const v8h bfrag = dequant5(wv[j][s], (hv[j] >> (8 * s)) & 0xFFu, nz[j]);
@@ -245,7 +252,7 @@ __global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel(
void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, int K, int N,
int WV, int SK, int MB, int NPW, int NT, long stream, int lda, int ldc,
bool hi5) {
bool hi5, bool bf) {
if (!lda) lda = K;
if (!ldc) ldc = N;
if (M < 1 || M > R4D_GEMM_W4_MAX_M)
@@ -273,14 +280,14 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i
hipStream_t st = (hipStream_t)stream;
#define LAUNCH(MT, NP, TT) do { \
if (hi5) hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<MT, NP, TT, true>), grid, block, smem, \
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc, bf); \
else hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<MT, NP, TT, false>), grid, block, smem, \
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0)
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc, bf); } while (0)
#define LAUNCH_ONE(NP, TT) do { \
if (hi5) hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<1, NP, TT, true, true>), grid, block, smem, \
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc, bf); \
else hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<1, NP, TT, false, true>), grid, block, smem, \
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0)
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc, bf); } while (0)
#define LAUNCH_MB(NP, TT) switch (MB) { \
case 1: if (one) { LAUNCH_ONE(NP, TT); } else { LAUNCH(1, NP, TT); } break; \
case 2: LAUNCH(2, NP, TT); break; \
@@ -310,8 +317,9 @@ static int w5_dispatch(const RadArgs* a, RadStream s, bool hi5) {
int rc = gemm_ops(a, &g, 1, /*parts=*/1);
if (rc != RAD_OK) return rc;
if (g.N % 16) return R4D_NO(RAD_E_SHAPE);
/* Any other activation dtype would read plausible but wrong numbers. */
if (g.a->dtype != RAD_F16) return R4D_NO(RAD_E_DTYPE);
/* BF16 is narrowed on load with the cast kernel's rounding, so a separate cast is not required. */
const bool bf = g.a->dtype == RAD_BF16;
if (g.a->dtype != RAD_F16 && !bf) return R4D_NO(RAD_E_DTYPE);
MxParts mp;
rc = mx_parts(a, &g, &mp);
if (rc != RAD_OK) return rc;
@@ -342,7 +350,7 @@ static int w5_dispatch(const RadArgs* a, RadStream s, bool hi5) {
r4d_addr(g.bscale) + (n0 / 16) * nsz * 16 * 4,
r4d_addr(g.y) + (m0 * Ntot + n0) * 2,
(int)mm, (int)K, (int)np, c.wv, c.sk, c.mb, c.npw, c.nt, (long)(uintptr_t)s,
(int)lda, (int)Ntot, hi5));
(int)lda, (int)Ntot, hi5, bf));
}
}
return RAD_OK;
+2 -2
View File
@@ -7671,8 +7671,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
"skinny GEMM with a 4-bit ASYMMETRIC weight (z-lab's ParoQuant int4), the 5-bit row's kernel with "
"no fifth-bit plane; rows past 64 in chunks of 64, a partitioned weight (pb1/pb2) one launch a part",
"N a multiple of 16; K divisible by SK*128; weight pre-permuted into fragment order, unsigned nibbles",
"f16 A (parts side by side), 4-bit W with an f16 scale and integer zero per 128 K, bf16 C, fp32 "
"accumulate",
"f16 or bf16 A (parts side by side; bf16 is narrowed on load as cast_bf16_f16 does), 4-bit W "
"with an f16 scale and integer zero per 128 K, bf16 C, fp32 accumulate",
RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_SKINNY,
ROW(cGemmNtW4Asym), ROW(tGemmSkinny), r4d_tune_valid_gemm,
nullptr, nullptr, r4d_rad_gemm_w4a16asym_m64, nullptr, r4d_rad_layout_w4, r4d_rad_relayout_w4,
+159 -20
View File
@@ -4496,11 +4496,12 @@ static void dflash_conv_numeric(const Plugin& pl, int64_t nt, int64_t H, int64_t
/* The skinny bf16 GEMM against a host reference at a given K. K is the dimension this kernel
* decomposes along (pick_sk splits it, rowGo strides rows by it, the unrolled fragment loop walks
* it), so it is a parameter. The weight is plain row-major [N, K]: the row has no layout hook. */
static void bf16_gemm_numeric(const Plugin& pl, const char* row, int64_t M, int64_t N, int64_t K) {
static void bf16_gemm_numeric(const Plugin& pl, const char* row, int64_t M, int64_t N, int64_t K,
int tuned_sk = 0) {
if (!sel(row)) return;
char nm[128];
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld", row,
(long long)M, (long long)N, (long long)K);
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld sk=%d", row,
(long long)M, (long long)N, (long long)K, tuned_sk);
const RadKernelInfo* r = find(pl, row);
if (!r) { ok(nm, false, "row absent"); return; }
@@ -4527,6 +4528,7 @@ static void bf16_gemm_numeric(const Plugin& pl, const char* row, int64_t M, int6
st_memcpy(ts[1].data, hw.data(), hw.size() * 2, hipMemcpyHostToDevice);
std::vector<RadParam> ps = { PI("M", M), PI("N", N), PI("K", K), PS("dtype", "bf16") };
if (tuned_sk > 0) ps.push_back(PI("sk", tuned_sk));
RadArgs a;
std::memset(&a, 0, sizeof(a));
a.t = ts.data(); a.n_t = (int)ts.size();
@@ -5187,21 +5189,90 @@ static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, in
}
/* The 5-bit skinny GEMM (ParoQuant's int5) and its 4-bit sibling w4a16_asym against the planes'
* meaning: u5 or u4 codes (a bit stream along K), an f16 scale and a zero point per 128 of K (of
* dtype `zdt`), an f16 activation, the weight stored through the row's hooks. `pb1`/`pb2` give a
* partitioned weight. M past 64 runs the dispatcher's 64-row chunks. */
/* The decode graph no longer casts before this GEMM. A bf16 activation, narrowed on load, must
* match cast_bf16_f16 followed by the f16 kernel, including the one-row epilogue. */
static void w4_bf16_activation_matches_cast(const Plugin& pl, int64_t N = 48, int64_t K = 256,
bool edge_bits = false) {
const char* what = "gemm_w4a16_asym bf16 activation matches the cast then the f16 GEMM";
const RadKernelInfo* gemm = find(pl, "gemm_w4a16_asym_nt_m64");
const RadKernelInfo* cast = find(pl, "cast_bf16_f16");
if (!gemm || !cast) { ok(what, false, "row absent"); return; }
const Planes p = asym_planes(4, N, K, RAD_U8);
std::vector<RadParam> lp = { PI("N", N), PI("K", K), PI("group", 128), PS("dtype", "w4a16_asym") };
std::vector<uint8_t> wq, ws;
if (stored(gemm, lp, 2, p, { "codes" }, &wq) != RAD_OK ||
stored(gemm, lp, 3, p, { "scale", "zero" }, &ws) != RAD_OK) {
ok(what, false, "relayout failed"); return;
}
std::vector<uint16_t> hb((size_t)(17 * K));
for (size_t i = 0; i < hb.size(); ++i) {
const uint32_t h = st_mix32((uint32_t)i);
hb[i] = edge_bits ? (uint16_t)h : h_to_bf16((float)((int)(h % 65537) - 32768) / 4096.f);
}
void* d_b = dalloc(hb.size() * 2);
void* d_f = dalloc(hb.size() * 2);
void* d_w = dalloc(wq.size());
void* d_s = dalloc(ws.size());
void* d_y0 = dalloc((size_t)(17 * N * 2));
void* d_y1 = dalloc((size_t)(17 * N * 2));
if (!d_b || !d_f || !d_w || !d_s || !d_y0 || !d_y1) { ok(what, false, "hipMalloc failed"); return; }
st_memcpy(d_b, hb.data(), hb.size() * 2, hipMemcpyHostToDevice);
st_memcpy(d_w, wq.data(), wq.size(), hipMemcpyHostToDevice);
st_memcpy(d_s, ws.data(), ws.size(), hipMemcpyHostToDevice);
for (const int64_t M : { (int64_t)1, (int64_t)17 }) {
std::vector<RadTensor> ct = { T(d_b, RAD_BF16, { M, K }), T(d_f, RAD_F16, { M, K }) };
std::vector<RadParam> cp = { PI("M", M), PI("n", K), PS("from", "bf16"), PS("to", "f16") };
RadArgs ca{};
ca.t = ct.data(); ca.n_t = 2; ca.p = cp.data(); ca.n_p = 4; ca.world_size = 1;
auto gemm_launch = [&](void* a, uint32_t dt, void* y) {
std::vector<RadTensor> ts = {
T(a, dt, { M, K }), T(nullptr, RAD_F32, { 1 }), T(d_w, RAD_U8, { (int64_t)wq.size() }),
T(d_s, RAD_U8, { (int64_t)ws.size() }), T(y, RAD_BF16, { M, N }),
T(nullptr, RAD_F32, { 1 }), T(nullptr, RAD_F32, { 1 }) };
absent(ts[1]); absent(ts[5]); absent(ts[6]);
std::vector<RadParam> ps = { PI("M", M), PI("N", N), PI("K", K), PI("group", 128),
PS("dtype", "w4a16_asym") };
RadArgs ga{};
ga.t = ts.data(); ga.n_t = (int)ts.size(); ga.p = ps.data(); ga.n_p = (int)ps.size();
ga.world_size = 1;
return gemm->launch(&ga, nullptr) == RAD_OK && st_stream_sync(nullptr) == hipSuccess;
};
const bool ran = cast->launch(&ca, nullptr) == RAD_OK && st_stream_sync(nullptr) == hipSuccess &&
gemm_launch(d_f, RAD_F16, d_y0) && gemm_launch(d_b, RAD_BF16, d_y1);
std::vector<uint16_t> y0((size_t)(M * N)), y1(y0.size());
if (ran) {
st_memcpy(y0.data(), d_y0, y0.size() * 2, hipMemcpyDeviceToHost);
st_memcpy(y1.data(), d_y1, y1.size() * 2, hipMemcpyDeviceToHost);
}
// Random raw BF16 bits often make every output NaN. The finite dataset must exercise
// actual arithmetic as well, so an all-NaN (or all-zero) comparison cannot pass it.
const bool finite = std::all_of(y0.begin(), y0.end(),
[](uint16_t v) { return std::isfinite(h_bf16(v)); });
const bool nonzero = std::any_of(y0.begin(), y0.end(),
[](uint16_t v) { return (v & 0x7fff) != 0; });
const std::string at = " M=" + std::to_string(M) + " N=" + std::to_string(N) +
" K=" + std::to_string(K) + (edge_bits ? " raw bits" : " finite");
ok((std::string(what) + at).c_str(),
ran && y0 == y1 && (edge_bits || (finite && nonzero)),
!ran ? "launch failed" : !edge_bits && (!finite || !nonzero)
? "finite oracle was nonfinite or all zero" : "output bytes differ");
if (!ran) return;
}
}
/* The asymmetric W4/W5 skinny GEMMs against the canonical codes/scales/zeros, including
* partitioned weights and the dispatcher's 64-row chunks. */
static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int64_t pb1 = 0,
int64_t pb2 = 0, int bits = 5, uint32_t zdt = RAD_U8,
bool one_row_exact = false) {
bool one_row_exact = false, int tuned_sk = 0) {
const char* row = bits == 5 ? "gemm_w5a16_nt_m64" : "gemm_w4a16_asym_nt_m64";
const char* dtype = bits == 5 ? "w5a16" : "w4a16_asym";
if (!sel(row)) return;
const PartCut cut(N, pb1, pb2);
const int parts = cut.parts;
char nm[160];
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d zero %s", row,
(long long)M, (long long)N, (long long)K, parts, rad_dtype_name(zdt));
std::snprintf(nm, sizeof nm, "%s numeric M=%lld N=%lld K=%lld parts=%d zero %s sk=%d", row,
(long long)M, (long long)N, (long long)K, parts, rad_dtype_name(zdt), tuned_sk);
const RadKernelInfo* r = find(pl, row);
if (!r || !r->layout || !r->relayout) { ok(nm, false, "row or hooks absent"); return; }
const int64_t G = K / 128;
@@ -5245,6 +5316,7 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int
PS("dtype", dtype) };
if (pb1) ps.push_back(PI("pb1", pb1));
if (pb2) ps.push_back(PI("pb2", pb2));
if (tuned_sk > 0) ps.push_back(PI("sk", tuned_sk));
RadArgs a;
std::memset(&a, 0, sizeof(a));
a.t = ts.data(); a.n_t = (int)ts.size();
@@ -5323,7 +5395,7 @@ static void w5a16_numeric(const Plugin& pl, int64_t M, int64_t N, int64_t K, int
* A measurement, not a correctness check. The weight is replicated past the last-level cache
* and walked in turn, as in perf_row, so the printed GB/s is the weight stream's. */
static void w5a16_perf(const Plugin& pl, int bits, int64_t M, int64_t N, int64_t K,
int min_iters = 12) {
int min_iters = 12, uint32_t adt = RAD_F16) {
const char* row = bits == 5 ? "gemm_w5a16_nt_m64" : "gemm_w4a16_asym_nt_m64";
const RadKernelInfo* r = find(pl, row);
if (!r) { std::printf("row absent\n"); return; }
@@ -5352,14 +5424,14 @@ static void w5a16_perf(const Plugin& pl, int bits, int64_t M, int64_t N, int64_t
if (!d_w || !d_s || !d_a || !d_y) { std::printf("hipMalloc failed\n"); return; }
std::vector<uint16_t> ha((size_t)(M * K));
for (size_t i = 0; i < ha.size(); ++i)
rad_store_f32(ha.data(), RAD_F16, (int64_t)i, 0.003f * (float)((int)(i % 97) - 48));
rad_store_f32(ha.data(), adt, (int64_t)i, 0.003f * (float)((int)(i % 97) - 48));
st_memcpy(d_a, ha.data(), ha.size() * 2, hipMemcpyHostToDevice);
for (size_t c = 0; c < copies; ++c) {
st_memcpy((uint8_t*)d_w + c * wq.size(), wq.data(), wq.size(), hipMemcpyHostToDevice);
st_memcpy((uint8_t*)d_s + c * ws.size(), ws.data(), ws.size(), hipMemcpyHostToDevice);
}
std::printf("%s M=%lld N=%lld K=%lld (weight %.1f MiB)\n", row, (long long)M, (long long)N,
(long long)K, wq.size() / 1048576.0);
std::printf("%s M=%lld N=%lld K=%lld A=%s (weight %.1f MiB)\n", row, (long long)M, (long long)N,
(long long)K, rad_dtype_name(adt), wq.size() / 1048576.0);
struct Cfg { int wv, sk, mb, npw, nt; };
std::vector<Cfg> cfgs;
for (int sk : { 0, 1, 2, 4, 5, 8 })
@@ -5370,7 +5442,7 @@ static void w5a16_perf(const Plugin& pl, int bits, int64_t M, int64_t N, int64_t
Cfg bc{};
for (const Cfg& c : cfgs) {
std::vector<RadTensor> ts = {
T(d_a, RAD_F16, { M, K }), T(nullptr, RAD_F32, { 1 }), T(d_w, RAD_U8, { (int64_t)wq.size() }),
T(d_a, adt, { M, K }), T(nullptr, RAD_F32, { 1 }), T(d_w, RAD_U8, { (int64_t)wq.size() }),
T(d_s, RAD_U8, { (int64_t)ws.size() }), T(d_y, RAD_BF16, { M, N }), T(nullptr, RAD_F32, { 1 }),
T(nullptr, RAD_F32, { 1 }) };
absent(ts[1]); absent(ts[5]); absent(ts[6]);
@@ -6176,6 +6248,53 @@ static void i8a8h_rot(const Plugin& pl, const char* row, int64_t M, int64_t N, i
}
/* Single-row reduction vs the unchanged padded-row epilogue. MB=2 forces the latter without
* changing a split or a WMMA. Check ragged columns, narrow projections and HYV3's shared MLP. */
static void bf16_one_row_identity(const Plugin& pl, int64_t N, int64_t K) {
const RadKernelInfo* r = find(pl, "gemm_bf16_nt_m64");
if (!r) { ok("bf16 one-row bytes", false, "row absent"); return; }
std::vector<uint16_t> ha((size_t)K), hw((size_t)(N * K));
for (size_t i = 0; i < ha.size(); ++i)
ha[i] = h_to_bf16((float)((int)(st_mix32((uint32_t)i) % 401) - 200) * 0.01f);
for (size_t i = 0; i < hw.size(); ++i)
hw[i] = h_to_bf16((float)((int)(st_mix32((uint32_t)i ^ 137u) % 199) - 99) * 0.01f);
void* d_a = dalloc(ha.size() * 2);
void* d_w = dalloc(hw.size() * 2);
void* d_y = dalloc((size_t)(N + 16) * 2);
if (!d_a || !d_w || !d_y) { ok("bf16 one-row bytes", false, "hipMalloc failed"); return; }
st_memcpy(d_a, ha.data(), ha.size() * 2, hipMemcpyHostToDevice);
st_memcpy(d_w, hw.data(), hw.size() * 2, hipMemcpyHostToDevice);
std::vector<RadTensor> ts = { T(d_a, RAD_BF16, { 1, K }), T(d_w, RAD_BF16, { N, K }),
T(d_y, RAD_BF16, { 1, N }) };
std::vector<uint16_t> compact((size_t)(N + 16)), padded(compact.size());
for (int wv : { 1, 2, 4 })
for (int sk : { 1, 2, 3, 4, 8, 16, 32 }) {
if (K % (sk * 16) || wv * sk > 32) continue;
std::vector<RadParam> ps = { PI("M", 1), PI("N", N), PI("K", K), PS("dtype", "bf16"),
PI("wv", wv), PI("sk", sk), PI("mb", 1) };
RadArgs a{};
a.t = ts.data(); a.n_t = (int)ts.size(); a.p = ps.data(); a.n_p = (int)ps.size();
a.world_size = 1;
bool ran = true;
for (int pass = 0; pass < 2; ++pass) {
ps.back() = PI("mb", pass + 1);
auto& out = pass ? padded : compact;
if (st_memset(d_y, 0xa5, out.size() * 2) != hipSuccess ||
r->launch(&a, nullptr) != RAD_OK || st_stream_sync(nullptr) != hipSuccess ||
st_memcpy(out.data(), d_y, out.size() * 2, hipMemcpyDeviceToHost) != hipSuccess) {
ran = false; break;
}
}
const bool guard = std::all_of(compact.end() - 16, compact.end(),
[](uint16_t v) { return v == 0xa5a5; });
const std::string at = " N=" + std::to_string(N) + " K=" + std::to_string(K) +
" wv=" + std::to_string(wv) + " sk=" + std::to_string(sk);
ok(("bf16 one-row bytes" + at).c_str(), ran && guard && compact == padded,
!ran ? "launch/copy failed" : !guard ? "output guard overwritten" : "bytes differ");
if (!ran) return;
}
}
/* The tiled bf16 GEMM against the skinny one, byte for byte, at every tile variant. An op over
* M 1..max_tok is served by the skinny row at M <= 64 and the tiled one above, so they must agree
* exactly or a decode step and a prefill chunk would disagree about the same row. The shapes reach
@@ -18930,8 +19049,10 @@ static void bf16_perf(const Plugin& pl, int64_t N, int64_t K, int64_t M, int ite
if (!r) { std::printf("--perfbf16: row absent\n"); return; }
const size_t wbytes = (size_t)N * (size_t)K * 2;
int copies = (int)(((size_t)768 << 20) / wbytes) + 1;
if (copies > 16) copies = 16;
// Even a small shared MLP must walk more than the last-level cache. A 16-copy cap
// would keep HYV3's 3 MiB down projection warm and overstate its decode bandwidth.
const int copies = (int)std::max<size_t>(2, ((size_t)256 << 20) / wbytes + 1);
iters = std::max(iters, copies * 2);
void* d_w = dalloc(wbytes * (size_t)copies);
void* d_a = dalloc((size_t)M * K * 2);
@@ -30650,6 +30771,15 @@ static void device_phase(const Plugin& pl) {
* zero plane: five groups a row (a pad nibble ends each zero row), then decode and verify
* rows at the 27B's shapes. */
// HYV3 decode geometries, plus column tails and two/three partitioned activation copies.
w4_bf16_activation_matches_cast(pl);
w4_bf16_activation_matches_cast(pl, 48, 256, true);
w4_bf16_activation_matches_cast(pl, 4096, 2048);
w4_bf16_activation_matches_cast(pl, 2048, 4096);
// Cold-sweep candidates at the actual decode row and the selected band's upper edge.
for (int64_t m : { 1, 8 }) {
w5a16_numeric(pl, m, 4096, 2048, 0, 0, 4, RAD_U8, false, 4);
w5a16_numeric(pl, m, 2048, 4096, 0, 0, 4, RAD_U8, false, 8);
}
for (const auto& nk : { std::pair<int64_t, int64_t>{4096, 2048}, {512, 2048},
{2048, 4096}, {13824, 2048}, {2048, 6912}, {48, 640} })
w5a16_numeric(pl, 1, nk.first, nk.second, 0, 0, 4, RAD_U8, true);
@@ -30837,6 +30967,13 @@ static void device_phase(const Plugin& pl) {
bf16_gemm_numeric(pl, row, 16, 5120, 25600);
}
bf16_gemm_numeric(pl, "gemm_bf16_nt_m64", 64, 5120, 25600);
for (int64_t m : { 1, 16 })
bf16_gemm_numeric(pl, "gemm_bf16_nt_m64", m, 1536, 2048, 8);
for (const auto& nk : { std::pair<int64_t, int64_t>{1536, 2048}, {2048, 768},
{48, 5120}, {129, 256}, {1, 16}, {16, 16} }) {
bf16_one_row_identity(pl, nk.first, nk.second);
bf16_gemm_numeric(pl, "gemm_bf16_nt_m64", 1, nk.first, nk.second);
}
}
if (sel("gemm_bf16_nt_tiled")) {
if (!g_case) std::printf("\nthe tiled bf16 GEMM, against the skinny one\n");
@@ -31039,6 +31176,7 @@ static void usage(void) {
" --perfq N K M [iters]: the block-quantised trunk rows GLM resolves to: E4M3 with\n"
" F32 block scales and the three OCP-MXFP4 rows, GB/s on weight + scales.\n"
" --perfw4 N K M [iters]: a tuning sweep of the asymmetric W4 x F16 GEMM;\n"
" --perfw4bf16: the same sweep with BF16 inputs narrowed inside the GEMM.\n"
" M <= 64, N 16-aligned, K 128-aligned. Measurement only, cold weights.\n"
" --perfw8 N K M [iters]: the 8-bit trunk GEMMs -- block E4M3 W8A8 and the int8\n"
" rows -- at one shape, each with its TFLOP/s.\n"
@@ -31258,9 +31396,9 @@ int main(int argc, char** argv) {
perf_k = std::strtoll(argv[++i], nullptr, 0);
perf_m = std::strtoll(argv[++i], nullptr, 0);
if (i + 1 < argc && argv[i + 1][0] != '-') perf_it = (int)std::strtol(argv[++i], nullptr, 0);
} else if (!std::strcmp(argv[i], "--perfw4")) {
} else if (!std::strcmp(argv[i], "--perfw4") || !std::strcmp(argv[i], "--perfw4bf16")) {
if (i + 3 >= argc) { usage(); return 2; }
perf_w4 = 1;
perf_w4 = !std::strcmp(argv[i], "--perfw4bf16") ? 2 : 1;
perf_n = std::strtoll(argv[++i], nullptr, 0);
perf_k = std::strtoll(argv[++i], nullptr, 0);
perf_m = std::strtoll(argv[++i], nullptr, 0);
@@ -31681,7 +31819,8 @@ int main(int argc, char** argv) {
else if (perf_wpa) wpa_perf(pl, perf_n, perf_k, perf_m, perf_it, perf_pb1);
else if (perf_mx) mx_perf(pl, perf_n, perf_k, perf_m, perf_it);
else if (perf_bf) bf16_perf(pl, perf_n, perf_k, perf_m, perf_it);
else if (perf_w4) w5a16_perf(pl, 4, perf_m, perf_n, perf_k, perf_it);
else if (perf_w4) w5a16_perf(pl, 4, perf_m, perf_n, perf_k, perf_it,
perf_w4 == 2 ? RAD_BF16 : RAD_F16);
else if (perf_w8) w8_perf(pl, perf_n, perf_k, perf_m, perf_it);
else if (perf_w2) w2_perf(pl, perf_n, perf_k, perf_m, perf_it);
else fp8_perf(pl, perf_n, perf_k, perf_m, perf_it);
+4 -9
View File
@@ -7638,7 +7638,7 @@ int qwen35_fp8_int4_declare(RadBuilder* b, const RadModelMeta* m, const RadBuild
} /* namespace */
/* An int4 container's linears are read by the 4-bit GEMMs over their input's bf16 plane: at or
* below 64 rows a `cast` to f16 and w4a16_asym, above that quant_act_i8g's int8 codes and
* below 64 rows w4a16_asym narrows that plane on load, above that quant_act_i8g's int8 codes and
* w4a8i8_asym, both over the codes and the joined (scale, zero) view. No linear keeps an fp8a8
* GEMM, and the gated fold (an fp8a8 tile) is not declared over a 4-bit gate_up. */
TEST(qwen35_fp8_reads_an_int4_container_with_the_4bit_gemms) {
@@ -7666,18 +7666,13 @@ TEST(qwen35_fp8_reads_an_int4_container_with_the_4bit_gemms) {
CHECK_EQ(pint(&o, "group"), 128);
}
/* q|gate, k, v, o for 2 attention layers; qkv|z and out for 6 delta-net layers; gate_up and
* down for 8 feed-forwards: 36 linears, each with both GEMMs, a cast and a quantiser. */
* down for 8 feed-forwards: 36 linears, each with both GEMMs and a quantiser. The decode GEMM
* consumes bf16 directly. */
CHECK_EQ(f16, 36);
CHECK_EQ(i8, 36);
CHECK_EQ(other, 0);
int casts = 0;
for (const auto& o : b.ops)
if (o.op == "cast" && pstr(&o, "to") == "f16") {
++casts;
CHECK_EQ(pstr(&o, "from"), std::string("bf16"));
CHECK_EQ(param(&o, "M")->ihi, 64LL);
}
CHECK_EQ(casts, 36);
CHECK(o.op != "cast" || pstr(&o, "to") != "f16");
CHECK_EQ(count_op(b, "quant_act_i8g"), 36);
CHECK_EQ(count_op(b, "gemm_nt_q_gated"), 0);
int joined = 0;