mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
Merge f-0226-q4-rdna-gr (da92a291ef): the rdna_gr read takes N / HC / LR from the compiled geometry and checks itself at startup (smashdog)
One commit by smashdog (authorship kept) on engine/src/kernels/cuda/rdna_gr.cu and fused_gr.cu: rdna_gr hardcoded N=2560 / LR=320 and read past LR=256 on q4_lite; STRATA_HIP_FAST_DEC is on by default for one arch group and rdna_gr_enabled() accepts gfx11 / gfx12, so RDNA3 on the GGUF path would have hit it by default. Now N, HC and LR come from geometry.hpp (static_asserts), and a startup self-check (fused_gr_check -> rdna_gr_check) compares the rdna read with the plain read and turns it off for a card that disagrees (rdna_gr_mark_bad); the HIP body is under __HIP_PLATFORM_AMD__, a CUDA build only sees the rdna_gr_mark_bad stub; the Flash-Next geometry values are unchanged. Lane (p620 SDK 7.10, compile only, -DSTRATA_GEOMETRY=q4_lite, target strata): rdna3 gfx1100 / 1101 / 1102 rc 0 GATE-BIN c49be6acc0a28c0722407e99fd4292baf5f885e8d7b24bd54a1a98c0470e4b28; rdna4 gfx1200 / 1201 rc 0 GATE-BIN c45bf317edcbbfd325d2cb9e50a18d0f5279e43df8adbdba5bf1cd85de0c8b9f. INT: CUDA Flash-Next default geometry, boston sm_86 CUDA 13.1, the tip vs the trial merge: all 73 host objects of the strata target identical.
This commit is contained in:
commit
04a8b197a1
2 files changed
+210
-10
No files matched your search
@@ -36,6 +36,7 @@ bool rdna_gr_enabled();
|
||||
void rdna_gr_read_multi(const FusedGrArgs* a, int n_tok, float* scratch, void* stream,
|
||||
unsigned long long* sb = nullptr, int si = 0);
|
||||
void rdna_gr_init();
|
||||
void rdna_gr_mark_bad(int dev);
|
||||
} // namespace strata::kernels
|
||||
|
||||
namespace strata::kernels {
|
||||
@@ -4729,6 +4730,175 @@ bool fused_gr_selftest(bool ok_variant[4], std::string why[4], int* v70 = nullpt
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace {
|
||||
|
||||
// HIP, the rdna read enabled (STRATA_GR_RDNA=1 / STRATA_HIP_FAST_DEC=1 on gfx11/gfx12): its down is split-K and folded in
|
||||
// another order than the plain read, so it cannot be checked bit for bit like the split/staged variants. Checked here with a
|
||||
// tolerance on 1..4 tokens, the pending write off and on: a direct call, and the same call replayed from a captured graph
|
||||
// (the way the decode loop runs it), each against the plain read on the same inputs. Returns 2 = passed, 1 = could not run
|
||||
// (the read stays on, as before), 0 = disagrees (the caller marks the card: the default read runs).
|
||||
int rdna_gr_check(int dev, std::string* why) {
|
||||
constexpr int TM = 4;
|
||||
std::mt19937 rng(20261204u);
|
||||
std::normal_distribution<float> nd(0.0f, 1.0f);
|
||||
std::vector<uint16_t> h_down((size_t) LR * D), h_up((size_t) D * LR), h_inj((size_t) HC * D);
|
||||
std::vector<float> h_norm(D), h_R((size_t) TM * D), h_bo((size_t) TM * N), h_ip((size_t) TM * HC);
|
||||
for (auto& w : h_down) w = bf16_from_f32(0.02f * nd(rng));
|
||||
for (auto& w : h_up) w = bf16_from_f32(0.05f * nd(rng));
|
||||
for (auto& w : h_inj) w = bf16_from_f32(0.02f * nd(rng));
|
||||
for (auto& w : h_norm) w = 1.0f + 0.1f * nd(rng);
|
||||
for (auto& x : h_R) x = nd(rng);
|
||||
for (auto& x : h_bo) x = 0.5f * nd(rng);
|
||||
for (auto& x : h_ip) x = 2.0f * nd(rng);
|
||||
|
||||
const size_t n_set = (size_t) TM * (D + D + LR + HC + HC + N); // R_out, xn, lo, rs, inject, mixed (floats)
|
||||
const size_t bytes = h_down.size() * 2 + h_up.size() * 2 + h_inj.size() * 2 +
|
||||
(h_norm.size() + h_R.size() + h_bo.size() + h_ip.size() + 2 * n_set) * 4 + 32 * 256;
|
||||
uint8_t* base = nullptr;
|
||||
if (cudaMalloc((void**) &base, bytes) != cudaSuccess) {
|
||||
cudaGetLastError();
|
||||
return 1;
|
||||
}
|
||||
size_t off = 0;
|
||||
auto take = [&](size_t b) { void* p = base + off; off += (b + 255) / 256 * 256; return p; };
|
||||
uint16_t* d_down = (uint16_t*) take(h_down.size() * 2);
|
||||
uint16_t* d_up = (uint16_t*) take(h_up.size() * 2);
|
||||
uint16_t* d_inj = (uint16_t*) take(h_inj.size() * 2);
|
||||
float* d_norm = (float*) take(h_norm.size() * 4);
|
||||
float* d_R = (float*) take(h_R.size() * 4);
|
||||
float* d_bo = (float*) take(h_bo.size() * 4);
|
||||
float* d_ip = (float*) take(h_ip.size() * 4);
|
||||
struct Set { float *R_out, *xn, *lo, *rs, *inj, *mixed; } set[2] = {};
|
||||
for (int v = 0; v < 2; ++v) {
|
||||
Set& x = set[v];
|
||||
x.R_out = (float*) take((size_t) TM * D * 4); x.xn = (float*) take((size_t) TM * D * 4);
|
||||
x.lo = (float*) take((size_t) TM * LR * 4); x.rs = (float*) take((size_t) TM * HC * 4);
|
||||
x.inj = (float*) take((size_t) TM * HC * 4); x.mixed = (float*) take((size_t) TM * N * 4);
|
||||
}
|
||||
cudaStream_t st = nullptr;
|
||||
const bool setup = off <= bytes && cudaStreamCreateWithFlags(&st, cudaStreamNonBlocking) == cudaSuccess &&
|
||||
cudaMemcpyAsync(d_down, h_down.data(), h_down.size() * 2, cudaMemcpyHostToDevice, st) == cudaSuccess &&
|
||||
cudaMemcpyAsync(d_up, h_up.data(), h_up.size() * 2, cudaMemcpyHostToDevice, st) == cudaSuccess &&
|
||||
cudaMemcpyAsync(d_inj, h_inj.data(), h_inj.size() * 2, cudaMemcpyHostToDevice, st) == cudaSuccess &&
|
||||
cudaMemcpyAsync(d_norm, h_norm.data(), h_norm.size() * 4, cudaMemcpyHostToDevice, st) == cudaSuccess &&
|
||||
cudaMemcpyAsync(d_R, h_R.data(), h_R.size() * 4, cudaMemcpyHostToDevice, st) == cudaSuccess &&
|
||||
cudaMemcpyAsync(d_bo, h_bo.data(), h_bo.size() * 4, cudaMemcpyHostToDevice, st) == cudaSuccess &&
|
||||
cudaMemcpyAsync(d_ip, h_ip.data(), h_ip.size() * 4, cudaMemcpyHostToDevice, st) == cudaSuccess;
|
||||
int verdict = setup ? 2 : 1;
|
||||
if (!setup) *why = "setting up the check failed";
|
||||
std::vector<float> h1, h2;
|
||||
float worst = 0.0f;
|
||||
// 1e-4 relative (1e-4 absolute near zero) on every output; NaN/inf on either side fails
|
||||
auto close = [&](const float* d1, const float* d2, size_t n, const char* what, int T, int apply, const char* how) {
|
||||
if (!setup) return false;
|
||||
h1.resize(n);
|
||||
h2.resize(n);
|
||||
if (cudaMemcpyAsync(h1.data(), d1, n * 4, cudaMemcpyDeviceToHost, st) != cudaSuccess ||
|
||||
cudaMemcpyAsync(h2.data(), d2, n * 4, cudaMemcpyDeviceToHost, st) != cudaSuccess ||
|
||||
cudaStreamSynchronize(st) != cudaSuccess) {
|
||||
*why = "reading back the check failed";
|
||||
return false;
|
||||
}
|
||||
float m = 0.0f;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const float r = h1[i], p = h2[i];
|
||||
if (!std::isfinite(r) || !std::isfinite(p)) {
|
||||
char buf[192];
|
||||
std::snprintf(buf, sizeof buf, "%s is not finite for %d token(s)%s at %zu (%.9g, not %.9g)%s", what, T,
|
||||
apply ? " with the pending write" : "", i, r, p, how);
|
||||
*why = buf;
|
||||
return false;
|
||||
}
|
||||
const float diff = std::fabs(r - p);
|
||||
if (diff > m) m = diff;
|
||||
if (diff > 1e-4f * std::fmax(1.0f, std::fabs(p))) {
|
||||
char buf[192];
|
||||
std::snprintf(buf, sizeof buf, "%s differs for %d token(s)%s at %zu (%.9g, not %.9g)%s", what, T,
|
||||
apply ? " with the pending write" : "", i, r, p, how);
|
||||
*why = buf;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (m > worst) worst = m;
|
||||
return true;
|
||||
};
|
||||
for (int apply = 0; apply < 2 && verdict == 2; ++apply) {
|
||||
for (int T = 1; T <= TM && verdict == 2; ++T) {
|
||||
FusedGrArgs a[2][TM];
|
||||
size_t span = 0;
|
||||
for (int v = 0; v < 2; ++v) {
|
||||
span = (size_t) ((uint8_t*) (set[v].mixed + (size_t) TM * N) - (uint8_t*) set[v].R_out);
|
||||
if (cudaMemsetAsync(set[v].R_out, 0xFF, span, st) != cudaSuccess) verdict = 0; // the whole set
|
||||
for (int k = 0; k < T; ++k) {
|
||||
FusedGrArgs& x = a[v][k];
|
||||
x.R = d_R + (size_t) k * D; x.R_out = set[v].R_out + (size_t) k * D; x.apply = apply != 0;
|
||||
x.bo_prev = d_bo + (size_t) k * N; x.inj_prev = d_ip + (size_t) k * HC;
|
||||
x.w_norm = d_norm; x.w_down = d_down; x.w_up = d_up; x.w_inject = d_inj; x.eps = 1e-6f;
|
||||
x.lo = set[v].lo + (size_t) k * LR; x.rs = set[v].rs + (size_t) k * HC;
|
||||
x.inject_out = set[v].inj + (size_t) k * HC; x.mixed = set[v].mixed + (size_t) k * N;
|
||||
}
|
||||
}
|
||||
GrMulti m;
|
||||
for (int k = 0; k < T; ++k) m.a[k] = a[0][k];
|
||||
m.xn = set[0].xn;
|
||||
m.T = T;
|
||||
launch_multi(m, kHcPlain, st, nullptr, 0, false);
|
||||
rdna_gr_read_multi(a[1], T, set[1].xn, st, nullptr, 0);
|
||||
if (cudaGetLastError() != cudaSuccess || cudaStreamSynchronize(st) != cudaSuccess) {
|
||||
if (verdict == 2) *why = "a kernel of the check failed";
|
||||
verdict = 0;
|
||||
break;
|
||||
}
|
||||
if (!close(set[1].lo, set[0].lo, (size_t) T * LR, "lo", T, apply, "") ||
|
||||
!close(set[1].rs, set[0].rs, (size_t) T * HC, "rs", T, apply, "") ||
|
||||
!close(set[1].inj, set[0].inj, (size_t) T * HC, "inject", T, apply, "") ||
|
||||
!close(set[1].mixed, set[0].mixed, (size_t) T * N, "mixed", T, apply, "") ||
|
||||
(apply != 0 && !close(set[1].R_out, set[0].R_out, (size_t) T * D, "R", T, apply, ""))) {
|
||||
verdict = 0;
|
||||
break;
|
||||
}
|
||||
// the same call from a captured graph, the way the decode loop replays it
|
||||
cudaGraph_t g = nullptr;
|
||||
cudaGraphExec_t ge = nullptr;
|
||||
bool gok = cudaMemsetAsync(set[1].R_out, 0xFF, span, st) == cudaSuccess &&
|
||||
cudaStreamBeginCapture(st, cudaStreamCaptureModeThreadLocal) == cudaSuccess;
|
||||
if (gok) {
|
||||
rdna_gr_read_multi(a[1], T, set[1].xn, st, nullptr, 0);
|
||||
gok = cudaStreamEndCapture(st, &g) == cudaSuccess;
|
||||
}
|
||||
if (gok) gok = cudaGraphInstantiate(&ge, g, 0) == cudaSuccess;
|
||||
if (gok) gok = cudaGraphLaunch(ge, st) == cudaSuccess && cudaStreamSynchronize(st) == cudaSuccess;
|
||||
if (verdict == 2 && !gok) {
|
||||
*why = "capturing the read into a graph failed";
|
||||
verdict = 0;
|
||||
}
|
||||
if (verdict == 2 &&
|
||||
(!close(set[1].lo, set[0].lo, (size_t) T * LR, "lo", T, apply, " under the captured graph") ||
|
||||
!close(set[1].rs, set[0].rs, (size_t) T * HC, "rs", T, apply, " under the captured graph") ||
|
||||
!close(set[1].inj, set[0].inj, (size_t) T * HC, "inject", T, apply, " under the captured graph") ||
|
||||
!close(set[1].mixed, set[0].mixed, (size_t) T * N, "mixed", T, apply, " under the captured graph") ||
|
||||
(apply != 0 &&
|
||||
!close(set[1].R_out, set[0].R_out, (size_t) T * D, "R", T, apply, " under the captured graph"))))
|
||||
verdict = 0;
|
||||
if (ge != nullptr) cudaGraphExecDestroy(ge);
|
||||
if (g != nullptr) cudaGraphDestroy(g);
|
||||
cudaGetLastError();
|
||||
}
|
||||
}
|
||||
if (st != nullptr) {
|
||||
cudaStreamSynchronize(st);
|
||||
cudaStreamDestroy(st);
|
||||
}
|
||||
cudaFree(base);
|
||||
cudaGetLastError();
|
||||
if (verdict == 2)
|
||||
std::fprintf(stderr, "strata hc: CUDA%d: the rdna read matches the plain read (1-%d tokens, the pending write off "
|
||||
"and on, a direct call and a captured graph; max diff %.2e)\n", dev, TM, (double) worst);
|
||||
return verdict;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int fused_gr_variant() {
|
||||
int dev = 0;
|
||||
cudaGetDevice(&dev);
|
||||
@@ -4744,6 +4914,17 @@ void fused_gr_check() {
|
||||
cudaGetDevice(&dev);
|
||||
(void) gr_v3_default_on(dev); // decided here, before any graph capture asks
|
||||
rdna_gr_init(); // HIP, STRATA_GR_FUSED=1: the fused read's barrier word, allocated outside any capture (#6730 / #6760)
|
||||
if (dev >= 0 && dev < 64 && rdna_gr_enabled()) { // the rdna read (STRATA_GR_RDNA / STRATA_HIP_FAST_DEC on gfx11+)
|
||||
std::string why;
|
||||
const int r = rdna_gr_check(dev, &why);
|
||||
if (r == 0) {
|
||||
rdna_gr_mark_bad(dev);
|
||||
std::fprintf(stderr, "strata hc: CUDA%d: the rdna read differs from the plain read on this card - not used: %s\n",
|
||||
dev, why.c_str());
|
||||
} else if (r == 1) {
|
||||
std::fprintf(stderr, "strata hc: CUDA%d: the check of the rdna read could not run\n", dev);
|
||||
}
|
||||
}
|
||||
#if !defined(__HIPCC__)
|
||||
{
|
||||
static bool q8_carve_done[64] = {};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// split-K down + partial-folding up (opt-in via STRATA_GR_RDNA=1 or STRATA_HIP_FAST_DEC=1 on gfx11).
|
||||
// Ported from MIT Strata-rdna3 with CU-count-safe persistent fusion gating and WDDM-safe wall_clock64() spin guards
|
||||
// (rdna3_wddm_guard.hpp), operating directly on 0.2.18 FusedGrArgs (BF16 and s_down/s_up per-32 FP32-scale int8).
|
||||
#include "strata/core/geometry.hpp"
|
||||
#include "strata/kernels/fused_gr.hpp"
|
||||
#include "strata/kernels/gfx_arch.hpp"
|
||||
#include "strata/kernels/q8_1_finite.hpp"
|
||||
@@ -22,19 +23,30 @@ bool rdna_gr_enabled();
|
||||
void rdna_gr_read_multi(const FusedGrArgs* a, int n_tok, float* scratch, void* stream,
|
||||
unsigned long long* sb = nullptr, int si = 0);
|
||||
void rdna_gr_init();
|
||||
void rdna_gr_mark_bad(int dev);
|
||||
|
||||
#if defined(__HIP_PLATFORM_AMD__) && !defined(STRATA_HIP_GFX906)
|
||||
namespace {
|
||||
|
||||
constexpr int N = 2560, HC = 4, D = N * HC, LR = 320, PR = LR + HC;
|
||||
constexpr int N = strata::core::kCompiledGeometry.n_embd;
|
||||
constexpr int HC = strata::core::kCompiledGeometry.hc;
|
||||
constexpr int D = N * HC, LR = strata::core::kCompiledGeometry.hc_lr, PR = LR + HC;
|
||||
static_assert(N == strata::core::kCompiledGeometry.n_embd && HC == strata::core::kCompiledGeometry.hc && LR == strata::core::kCompiledGeometry.hc_lr, "rdna_gr is compiled for core/geometry.hpp");
|
||||
constexpr int H = N / 2;
|
||||
constexpr int TH = 256, WV = TH / 32;
|
||||
constexpr int HQ = H / 8 / 32;
|
||||
constexpr int UC = 16;
|
||||
constexpr int UB = N / UC;
|
||||
static_assert(HQ >= 1 && 32 * HQ * 8 == H, "the down read needs whole 32x8 blocks per half stream");
|
||||
static_assert(H % 4 == 0, "the down read loads float4");
|
||||
static_assert(LR % 32 == 0, "up reads LR / 8 uint4 per lane pair");
|
||||
static_assert(LR % (2 * WV) == 0, "the a8 inject row is rg = LR / (RW * WV)");
|
||||
static_assert(N % UC == 0, "up covers N in UC columns");
|
||||
static_assert(8 * UC <= TH, "T * UC lanes fit a block, the threads read one token each up to 8");
|
||||
|
||||
unsigned* g_gr_bar[64] = {};
|
||||
int g_gr_can_fuse[64] = {}; // 0 unknown, 1 no, 2 yes
|
||||
int g_gr_off[64] = {}; // set by the verifier's check (fused_gr_check): the read disagreed, the default one runs
|
||||
|
||||
struct RdnaGr {
|
||||
FusedGrArgs a[kFusedGrMaxT];
|
||||
@@ -375,22 +387,22 @@ __device__ __forceinline__ void up_body(const RdnaGr& m, const int bx) {
|
||||
const int t = threadIdx.x;
|
||||
const int rr = t >> 2, q = t & 3;
|
||||
const int c = rr / UC, dd = rr - c * UC, d = bx * UC + dd, i = c * N + d;
|
||||
uint4 w[10];
|
||||
uint2 w8[10];
|
||||
float d8[10];
|
||||
uint4 w[LR / 32];
|
||||
uint2 w8[LR / 32];
|
||||
float d8[LR / 32];
|
||||
if constexpr (Q8) {
|
||||
const int8_t* qu = reinterpret_cast<const int8_t*>(m.a[0].w_up);
|
||||
const float* su = m.a[0].s_up;
|
||||
const uint2* w2 = reinterpret_cast<const uint2*>(qu + (size_t) i * LR);
|
||||
#pragma unroll
|
||||
for (int u = 0; u < 10; ++u) {
|
||||
for (int u = 0; u < LR / 32; ++u) {
|
||||
w8[u] = ntload2(w2 + q + 4 * u);
|
||||
d8[u] = su[(size_t) i * (LR / 32) + ((q + 4 * u) >> 2)];
|
||||
}
|
||||
} else {
|
||||
const uint4* w4 = reinterpret_cast<const uint4*>(m.a[0].w_up + (size_t) i * LR);
|
||||
#pragma unroll
|
||||
for (int u = 0; u < 10; ++u) w[u] = ntload(w4 + q + 4 * u);
|
||||
for (int u = 0; u < LR / 32; ++u) w[u] = ntload(w4 + q + 4 * u);
|
||||
}
|
||||
float rv[T], wn = m.a[0].w_norm[i];
|
||||
#pragma unroll
|
||||
@@ -430,7 +442,7 @@ __device__ __forceinline__ void up_body(const RdnaGr& m, const int bx) {
|
||||
for (int k = 0; k < T; ++k) {
|
||||
float a = 0.0f;
|
||||
#pragma unroll
|
||||
for (int u = 0; u < 10; ++u) {
|
||||
for (int u = 0; u < LR / 32; ++u) {
|
||||
const int j = (q + 4 * u) * 8;
|
||||
if constexpr (A8) a = fmaf(d8[u] * lod[k][j >> 5], (float) dot8i(w8[u], *reinterpret_cast<const uint2*>(&loq[k][j >> 2])), a);
|
||||
else if constexpr (Q8) a = dot8q(w8[u], d8[u], *reinterpret_cast<const float4*>(&lo[k][j]), *reinterpret_cast<const float4*>(&lo[k][j + 4]), a);
|
||||
@@ -521,7 +533,7 @@ void launch_gr(const RdnaGr& m, cudaStream_t st, unsigned long long* sb, int si,
|
||||
if (sb) gpu_stamp(sb, si, st);
|
||||
if (m.a[0].s_down != nullptr && m.a[0].s_up != nullptr) {
|
||||
const bool fused_req = gr_fused_requested();
|
||||
constexpr int kFusedBlocks = (LR / (2 * WV) + 1) * 2 * HC; // 168 blocks
|
||||
constexpr int kFusedBlocks = (LR / (2 * WV) + 1) * 2 * HC; // 136 on q4_lite, 168 on Flash-Next
|
||||
if (fused_req && m.bar != nullptr && sb == nullptr && dev >= 0 && dev < 64) {
|
||||
if (g_gr_can_fuse[dev] == 0) {
|
||||
int cus = 0, per = 0;
|
||||
@@ -559,10 +571,11 @@ bool rdna_gr_enabled() {
|
||||
return 0;
|
||||
}();
|
||||
if (env == 0) return false;
|
||||
int dev = 0;
|
||||
if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) return env == 1;
|
||||
if (g_gr_off[dev]) return false;
|
||||
if (env == 1) return true;
|
||||
static int arch_ok[64] = {};
|
||||
int dev = 0;
|
||||
if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) return false;
|
||||
if (arch_ok[dev] == 0) {
|
||||
cudaDeviceProp p{};
|
||||
arch_ok[dev] = (cudaGetDeviceProperties(&p, dev) == cudaSuccess &&
|
||||
@@ -583,6 +596,11 @@ void rdna_gr_init() {
|
||||
}
|
||||
}
|
||||
|
||||
// fused_gr_check found the rdna read disagreeing with the plain read on this card: the default read runs instead.
|
||||
void rdna_gr_mark_bad(int dev) {
|
||||
if (dev >= 0 && dev < 64) g_gr_off[dev] = 1;
|
||||
}
|
||||
|
||||
void rdna_gr_read_multi(const FusedGrArgs* a, int n_tok, float* scratch, void* stream,
|
||||
unsigned long long* sb, int si) {
|
||||
RdnaGr m;
|
||||
@@ -613,6 +631,7 @@ void rdna_gr_read_multi(const FusedGrArgs* a, int n_tok, float* scratch, void* s
|
||||
bool rdna_gr_enabled() { return false; }
|
||||
void rdna_gr_read_multi(const FusedGrArgs*, int, float*, void*, unsigned long long*, int) {}
|
||||
void rdna_gr_init() {}
|
||||
void rdna_gr_mark_bad(int) {}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in new issue
Block a user