libr4d: hc_read decode forms G, S and kernel B3 back, B3 without a lane branch after its WMMA

2.0.0 dropped #396's hc_read decode forms. With ROCm 7.2.1 (clang 22) they gave NaN logits (#413's report);
ROCm 10.1 was fine. The cause is kernel B3 alone: clang 22's backend sinks its WMMA up product into the
lane-divergent `if (col < RG && col < rows)` around the store of x, so the WMMA runs with part of EXEC off
(an s_and_saveexec right before the first v_wmma). In the IR the WMMA is convergent and outside the branch.
At 16 rows every lane passes the test, which is why form S at 16 rows was right.

- Forms G (1..8 rows) and S (9..16) and kernel B3 at Flash-Next's shape, the bodies of #396, chained in
  2.1's style (r4dchain / R4dUnchained), hc_rot_group<I8>. They take these rows from the fused grid and the
  tiled read; from 17 rows nothing changes.
- B3: a lane past the step's rows takes the last row and every lane stores, bit for bit what that row's own
  lane stores, so there is no branch after the WMMA.
- hc_decode_forms is #396's again (d against fp64, x within rounding up to 16 rows), with 2.1's rq dtype, a
  count of non-finite outputs (the error sums went through std::max and >, which pass NaN) and rows 9 and 12.

r4d_selftest --case hc_read_e4m3: 66 passed with ROCm 7.2.1 and with 10.1; 27 failed with 7.2.1 without
B3's change. Flash-Next on 2x R9700, ABBA, ms per decode step at 1/2/4/8/16 streams: 15.72/20.62/26.83/
37.37/51.89 -> 14.92/18.61/26.67/37.15/50.83; prefill unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-16 18:02:02 +01:00
1 parent 1d94e416a6
commit bd3fa646bc
2 files changed
+809 -23

No files matched your search

+117 -18
View File
@@ -19815,15 +19815,21 @@ static void hc_read_i8(const Plugin& pl, int64_t M, bool real_stream, int form)
: "the int8 run changed" + bad);
}
/* The gated residual's read at its decode kernels, byte for byte: the launcher's rule (kernels A and
* B as one grid at one to eight rows, the tiled up-mix pipelined from nine) against the two-grid
* form, pinned through r4d_hc_read_pin_decode(1). E4M3 or int8 codes, on a real stream (the
* one-grid form keys its counters on it). The two forms must dispatch different kernels, so a pin
* that is ignored cannot pass by comparing a form with itself. */
/* The gated residual's read at its decode kernels: the launcher's rule against the two-grid form,
* pinned through r4d_hc_read_pin_decode(1). E4M3 or int8 codes, on a real stream (the one-grid
* form keys its counters on it). The two forms must dispatch different kernels, so a pin that is
* ignored cannot pass by comparing a form with itself.
*
* Up to sixteen rows at this shape the rule serves decode forms G (1..8) and S (9..16) with kernel
* B3: bf16 WMMA dots, S scaling after the dot. Byte for byte they match neither the per-row kernels
* nor the tiled ones, so there d is held to an fp64 host reference - no further from it than the
* two-grid form's d - the inject gains must match byte for byte, and x must stay within rounding of
* the two-grid form's. Above sixteen rows both forms are the tiled path's and match byte for byte. */
static void hc_decode_forms(const Plugin& pl, int64_t M, bool i8) {
const std::string nm = fmt("hc_read_e4m3 decode kernels at M=%lld, %s codes: the rule "
"byte-identical to the two-grid form", (long long)M,
i8 ? "int8" : "E4M3");
const bool level = M <= 16;
const std::string nm = fmt("hc_read_e4m3 decode kernels at M=%lld, %s codes: the rule %s the "
"two-grid form", (long long)M, i8 ? "int8" : "E4M3",
level ? "as exact as" : "byte-identical to");
const RadKernelInfo* r = find(pl, "hc_read_e4m3");
if (!r) { skip(nm.c_str(), "row absent"); return; }
if (!pl.pin_hc_dec || !pl.record) { skip(nm.c_str(), "no r4d_hc_read_pin_decode"); return; }
@@ -19898,6 +19904,7 @@ static void hc_decode_forms(const Plugin& pl, int64_t M, bool i8) {
}
struct Out { std::vector<uint8_t> x, inj, q, qs, rq, rs; std::vector<const void*> fns; };
Out out[2];
std::vector<uint8_t> dsv[2]; /* d: the scratch's first M x lr bf16 values */
for (int form = 0; form < 2; ++form) {
pl.pin_hc_dec(form);
st_memset(d_x, 0, xb); st_memset(d_q, 0, qb); st_memset(d_qs, 0, sb);
@@ -19918,19 +19925,111 @@ static void hc_decode_forms(const Plugin& pl, int64_t M, bool i8) {
};
get(out[form].x, d_x, xb); get(out[form].inj, d_inj, ib); get(out[form].q, d_q, qb);
get(out[form].qs, d_qs, sb); get(out[form].rq, d_rq, qb); get(out[form].rs, d_rs, sb);
if (a.scratch) {
dsv[form].resize((size_t)(M * lr * 2));
st_memcpy(dsv[form].data(), a.scratch, dsv[form].size(), hipMemcpyDeviceToHost);
}
}
pl.pin_hc_dec(0);
(void)hipStreamDestroy(hs);
std::string bad;
if (out[0].x != out[1].x) bad += " x";
if (out[0].inj != out[1].inj) bad += " inj";
if (out[0].q != out[1].q) bad += " q";
if (out[0].qs != out[1].qs) bad += " qs";
if (out[0].rq != out[1].rq) bad += " rq";
if (out[0].rs != out[1].rs) bad += " rs";
int64_t nz = 0;
for (uint8_t v : out[0].q) nz += v != 0;
const bool distinct = out[0].fns != out[1].fns;
std::string bad;
/* Non-finite outputs first: the error sums below are blind to NaN (std::max drops it and every
* comparison with it is false), and NaN in x is what a broken form writes. */
{
auto nf16 = [](const std::vector<uint8_t>& v) {
int64_t c = 0;
const uint16_t* p = (const uint16_t*)v.data();
for (size_t i = 0; i < v.size() / 2; ++i) c += (p[i] & 0x7f80u) == 0x7f80u;
return c;
};
auto nf32 = [](const std::vector<uint8_t>& v) {
int64_t c = 0;
const float* p = (const float*)v.data();
for (size_t i = 0; i < v.size() / 4; ++i) c += !std::isfinite(p[i]);
return c;
};
for (int f = 0; f < 2; ++f) {
const int64_t a1 = nf16(out[f].x), a2 = nf16(out[f].inj), a3 = nf32(out[f].qs),
a4 = nf32(out[f].rs), a5 = dsv[f].empty() ? 0 : nf16(dsv[f]);
if (a1 || a2 || a3 || a4 || a5)
bad += fmt(" non-finite (%s: x %lld, inj %lld, qs %lld, rs %lld, d %lld)",
f ? "two-grid" : "rule", (long long)a1, (long long)a2, (long long)a3,
(long long)a4, (long long)a5);
}
}
if (!level) {
if (out[0].x != out[1].x) bad += " x";
if (out[0].inj != out[1].inj) bad += " inj";
if (out[0].q != out[1].q) bad += " q";
if (out[0].qs != out[1].qs) bad += " qs";
if (out[0].rq != out[1].rq) bad += " rq";
if (out[0].rs != out[1].rs) bad += " rs";
} else {
if (out[0].inj != out[1].inj) bad += " inj";
if (dsv[0].empty()) bad += " (no scratch for d)";
/* d against fp64: N unrounded, the dot over the decoded codes and their group scales. The
* error of each form as a fraction of the row's RMS of d, max and mean over every row. */
const int64_t G = r4d_hc8::down_group(HN);
double emax[2] = { 0, 0 }, esum[2] = { 0, 0 };
for (int64_t r0 = 0; r0 < M && !dsv[0].empty(); ++r0) {
std::vector<double> N((size_t)HN);
for (int64_t c = 0; c < hc; ++c) {
double ss = 0;
for (int64_t j = 0; j < n; ++j) {
const double v = h_bf16(hh[(size_t)(r0 * HN + c * n + j)]);
ss += v * v;
}
const double sc = 1.0 / std::sqrt(ss / (double)n + 1e-6);
for (int64_t j = 0; j < n; ++j)
N[(size_t)(c * n + j)] = h_bf16(hh[(size_t)(r0 * HN + c * n + j)]) * sc
* ((double)wg[(size_t)(c * n + j)] + 1.0);
}
std::vector<double> dref((size_t)lr);
double ssd = 0;
for (int64_t j = 0; j < lr; ++j) {
const uint8_t* row = down.data() + j * dld;
const float* gsc = (const float*)(row + HN);
double dot = 0;
for (int64_t kk = 0; kk < HN; ++kk)
dot += (double)h_e4m3(row[kk]) * (double)gsc[kk / G] * N[(size_t)kk];
const double z = dot / (double)hc;
dref[(size_t)j] = z / (1.0 + std::exp(-z));
ssd += dref[(size_t)j] * dref[(size_t)j];
}
const double rms = std::sqrt(ssd / (double)lr) + 1e-30;
for (int f = 0; f < 2; ++f) {
const uint16_t* df = (const uint16_t*)dsv[f].data();
for (int64_t j = 0; j < lr; ++j) {
const double e = std::fabs(h_bf16(df[r0 * lr + j]) - dref[(size_t)j]) / rms;
emax[f] = std::max(emax[f], e);
esum[f] += e;
}
}
}
const double cnt = (double)(M * lr);
if (esum[0] > esum[1] * 1.05 + 1e-6 * cnt || emax[0] > emax[1] * 1.10 + 1e-4)
bad += fmt(" d (rule %.5f / %.4f, two-grid %.5f / %.4f of RMS, mean / max)",
esum[0] / cnt, emax[0], esum[1] / cnt, emax[1]);
/* x: within rounding of the two-grid form's, as a fraction of the row's RMS. */
const uint16_t* x0 = (const uint16_t*)out[0].x.data();
const uint16_t* x1 = (const uint16_t*)out[1].x.data();
double xmax = 0, xsum = 0;
for (int64_t r0 = 0; r0 < M; ++r0) {
double ss = 0;
for (int64_t j = 0; j < n; ++j) { const double v = h_bf16(x1[r0 * n + j]); ss += v * v; }
const double rms = std::sqrt(ss / (double)n) + 1e-30;
for (int64_t j = 0; j < n; ++j) {
const double e = std::fabs(h_bf16(x0[r0 * n + j]) - h_bf16(x1[r0 * n + j])) / rms;
xmax = std::max(xmax, e);
xsum += e;
}
}
if (xsum / (double)(M * n) > 0.005 || xmax > 0.15)
bad += fmt(" x (%.5f mean, %.4f max of RMS)", xsum / (double)(M * n), xmax);
}
ok(nm.c_str(), bad.empty() && nz > (int64_t)qb / 2 && distinct,
!bad.empty() ? "differs in" + bad
: !distinct ? std::string("both forms dispatched the same kernels")
@@ -33208,9 +33307,9 @@ static void device_phase(const Plugin& pl) {
for (int form : { 1, 2 }) hc_read_i8(pl, m, true, form);
for (int64_t m : { 700, 2048 }) hc_read_i8(pl, m, true, 3);
}
/* The served decode kernels against the two-grid form: each one-grid instantiation (1 to
* 8 rows) and the tiled up-mix at both of its row tiles. */
for (int64_t m : { 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 32, 33, 64 })
/* The served decode kernels against the two-grid form: each row count of form G (1 to 8),
* form S at 9, 12 and 16 rows, and the tiled up-mix at both of its row tiles. */
for (int64_t m : { 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 16, 32, 33, 64 })
for (bool i8 : { false, true }) hc_decode_forms(pl, m, i8);
}
if (sel("hc_read_bf16")) {