Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

#315 follow-up: its variants and switch renamed (plain / split / staged, STRATA_HC_SPLIT) - no clash with STRATA_GR_V3

Niko12219d0b28df545a
diff --git a/include/strata/kernels/fused_gr.hpp b/include/strata/kernels/fused_gr.hpp
index 1860f8249..40eff30f2 100644
--- a/include/strata/kernels/fused_gr.hpp
+++ b/include/strata/kernels/fused_gr.hpp
@@ -49,14 +49,16 @@ constexpr int kFusedGrMaxT = 8;
void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream,
unsigned long long* stamp_buf = nullptr, int stamp_i0 = 0);
-/// The multi read's variants, all computing every output with v1's operations in v1's order, so bitwise v1's and the
-/// single-token read's: v1 (the norm one block per token, the down projection on 41 blocks), v2 (the norm one block
-/// per token and stream, then v1's down projection) and v3 (v2's norm, and the down projection's activations staged
-/// ahead by cp.async, two half-stream tiles in flight, bank-conflict-free). `fused_gr_check` runs all of them and
-/// the single-token read on the current card with random weights and inputs (1..8 tokens, with and without the
-/// pending write) and from then on uses there the newest one that agrees with v1 bit for bit; STRATA_HC_V2=0 keeps
-/// v1, =2 stops at v2. It runs once per card (Verifier::init calls it) and prints which one runs. On a card it
-/// has not checked, `fused_gr_variant` is v1 unless STRATA_HC_V2=2 or 3 names a variant.
+/// The multi read's variants (#315; not main's opt-in STRATA_GR_V3 read, which sums in another order), all computing
+/// every output with the plain read's operations in its order, so bitwise the plain read's and the single-token
+/// read's: plain (0.1.31's default: the norm one block per token, the down projection on 41 blocks), split (the norm
+/// one block per token and stream, then the plain down projection) and staged (split's norm, and the down
+/// projection's activations staged ahead by cp.async, two half-stream tiles in flight, bank-conflict-free).
+/// `fused_gr_check` runs all of them and the single-token read on the current card with random weights and inputs
+/// (1..8 tokens, with and without the pending write) and from then on uses there the newest one that agrees with the
+/// plain read bit for bit; STRATA_HC_SPLIT=0 keeps the plain read, =1 stops at split. It runs once per card
+/// (Verifier::init calls it) and prints which one runs. On a card it has not checked, `fused_gr_variant` is the
+/// plain read unless STRATA_HC_SPLIT=1 or 2 names a variant.
void fused_gr_check();
int fused_gr_variant();
diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu
index 5a0c6b4fd..56bb2f57f 100644
--- a/src/kernels/cuda/fused_gr.cu
+++ b/src/kernels/cuda/fused_gr.cu
@@ -495,29 +495,31 @@ __global__ void __launch_bounds__(THREADS) gr_up_v3_kernel(GrMulti m, const floa
m.a[k].mixed[d0 + col] = sum / (float) HC;
}
}
-// ================================ v2 / v3: the same arithmetic, split differently ================================
-// Every variant computes each output with exactly v1's operations in v1's order (so bitwise v1 and the single-token
-// read); only who does what, and when, changes. `fused_gr_check` compares them on the card before a verify window
-// uses them (STRATA_HC_V2 below).
-// - the norm (v2 and v3): one block per token AND stream instead of one per token. Each thread visits exactly the
-// elements, in the order, it visited for that stream in v1, so every sum of squares and rs is the same; then it
-// recomputes its R' * w_norm and writes it times rs (v1 stored the product and scaled it in place: the same two
-// roundings). On an RTX 4080 SUPER, v1's norm took 0.58 ms per 4-token round (96 launches of 1-4 blocks), this
-// one 0.23 ms.
-// - the down projection, v2: v1's kernel.
-// - the down projection, v3: v1's 8 rows per block and lane order, but the activations arrive by cp.async in
-// half-stream tiles, two in flight, so no thread waits on a chain of loads, and they sit in shared memory as two
-// planes (floats 0-3 and 4-7 of every 8), so a warp's reads hit no bank twice. The next tile's weights are
-// loaded while the current one is used. A tile is 160 = 5 x 32 chunks of 8, so a lane still accumulates its
-// chunks lane + 32 q in ascending order, as in v1 with either tile. Before sm_80 (and on HIP) the staging is a
-// plain copy: the same bits, only not asynchronous.
-constexpr int H_TILE = 1280; // v3 tile: half a stream = 160 chunks of 8, 5 per lane
+// ================================ #315: split / staged - the same arithmetic, split differently ==================
+// The multi read's variants beside the plain read (0.1.31's default; not main's opt-in STRATA_GR_V3 read, which sums
+// in another order). Every variant computes each output with exactly the plain read's operations in its order (so
+// bitwise the plain read and the single-token read); only who does what, and when, changes. `fused_gr_check`
+// compares them on the card before a verify window uses them (STRATA_HC_SPLIT below).
+// - the norm (split and staged): one block per token AND stream instead of one per token. Each thread visits
+// exactly the elements, in the order, it visited for that stream in the plain read, so every sum of squares and rs
+// is the same; then it recomputes its R' * w_norm and writes it times rs (the plain read stored the product and
+// scaled it in place: the same two roundings). On an RTX 4080 SUPER, the plain read's norm took 0.58 ms per
+// 4-token round (96 launches of 1-4 blocks), this one 0.23 ms.
+// - the down projection, split: the plain read's kernel.
+// - the down projection, staged: the plain read's 8 rows per block and lane order, but the activations arrive by
+// cp.async in half-stream tiles, two in flight, so no thread waits on a chain of loads, and they sit in shared
+// memory as two planes (floats 0-3 and 4-7 of every 8), so a warp's reads hit no bank twice. The next tile's
+// weights are loaded while the current one is used. A tile is 160 = 5 x 32 chunks of 8, so a lane still
+// accumulates its chunks lane + 32 q in ascending order, as in the plain read with either tile. Before sm_80
+// (and on HIP) the staging is a plain copy: the same bits, only not asynchronous.
+constexpr int kHcPlain = 1, kHcSplit = 2, kHcStaged = 3;
+constexpr int H_TILE = 1280; // staged tile: half a stream = 160 chunks of 8, 5 per lane
constexpr int HQ = H_TILE / 8 / 32;
constexpr int N_HTILES = D / H_TILE; // 8
-static_assert(N % H_TILE == 0, "a v3 tile never straddles two streams");
-static_assert(H_TILE % 256 == 0, "a v3 tile holds whole rounds of 32 chunks: the lane order stays v1's");
+static_assert(N % H_TILE == 0, "a staged tile never straddles two streams");
+static_assert(H_TILE % 256 == 0, "a staged tile holds whole rounds of 32 chunks: the plain read's lane order");
-__global__ void __launch_bounds__(THREADS) gr_norm_multi2_kernel(GrMulti m) {
+__global__ void __launch_bounds__(THREADS) gr_norm_split_kernel(GrMulti m) {
__shared__ float part[WARPS];
__shared__ float s_rs;
const FusedGrArgs& a = m.a[blockIdx.x];
@@ -614,7 +616,7 @@ __device__ __forceinline__ void stage_htile(const GrMulti& m, int T, int h, floa
}
}
-__global__ void __launch_bounds__(THREADS) gr_down_multi3_kernel(GrMulti m) {
+__global__ void __launch_bounds__(THREADS) gr_down_staged_kernel(GrMulti m) {
extern __shared__ __align__(16) float4 hbuf[]; // 2 buffers x [T][2][160] float4
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int T = m.T;
@@ -684,14 +686,14 @@ __global__ void __launch_bounds__(THREADS) gr_down_multi3_kernel(GrMulti m) {
}
}
-// The tokens a down kernel may carry in one launch on the current card, and (v1) its tile: v1 stages n_tok * TILEV
-// floats (1280 on sm_75, 2560 elsewhere), v3 two tiles of n_tok * H_TILE. The shared-memory opt-in is set here once
-// per device (a per-DEVICE setting: a layer split runs these kernels on two cards).
-int down_chunk(bool v3, int* tile_out) {
+// The tokens a down kernel may carry in one launch on the current card, and the plain read's tile: the plain read
+// stages n_tok * TILEV floats (1280 on sm_75, 2560 elsewhere), staged two tiles of n_tok * H_TILE. The shared-memory
+// opt-in is set here once per device (a per-DEVICE setting: a layer split runs these kernels on two cards).
+int down_chunk(bool staged, int* tile_out) {
static bool attr[64] = {};
- static int chunk[64] = {}; // tokens the v1 down kernel may carry in one launch on this card
- static int chunk3[64] = {}; // the same for v3
- static int tile[64] = {}; // the v1 down kernel's TILEV on this card (1280 on sm_75, 2560 elsewhere; see above)
+ static int chunk[64] = {}; // tokens the plain down kernel may carry in one launch on this card
+ static int chunk_staged[64] = {}; // the same for staged
+ static int tile[64] = {}; // the plain down kernel's TILEV on this card (1280 on sm_75, 2560 elsewhere)
int dev = 0;
cudaGetDevice(&dev);
if (dev < 0 || dev >= 64) {
@@ -725,9 +727,9 @@ int down_chunk(bool v3, int* tile_out) {
} else {
cudaFuncSetAttribute(gr_down_multi_kernel<2560>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);
}
- int want3 = (int) (2 * kFusedGrMaxT * H_TILE * sizeof(float));
- if (optin > 0 && want3 > optin) want3 = optin;
- cudaFuncSetAttribute(gr_down_multi3_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, want3);
+ int want_staged = (int) (2 * kFusedGrMaxT * H_TILE * sizeof(float));
+ if (optin > 0 && want_staged > optin) want_staged = optin;
+ cudaFuncSetAttribute(gr_down_staged_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, want_staged);
cudaGetLastError(); // drop any error the attempt left behind
// The opt-in is a promise a pre-Volta card does not keep: an sm_60 answers 65536 and accepts the
// cudaFuncSetAttribute for 61440 B, then fails the LAUNCH with "invalid argument". What such a card
@@ -744,26 +746,27 @@ int down_chunk(bool v3, int* tile_out) {
#endif
const int capacity = usable / (int) (tv * sizeof(float));
chunk[dev] = capacity < 1 ? 1 : (capacity > kFusedGrMaxT ? kFusedGrMaxT : capacity);
- const int capacity3 = usable / (int) (2 * H_TILE * sizeof(float));
- chunk3[dev] = capacity3 < 1 ? 1 : (capacity3 > kFusedGrMaxT ? kFusedGrMaxT : capacity3);
+ const int capacity_staged = usable / (int) (2 * H_TILE * sizeof(float));
+ chunk_staged[dev] = capacity_staged < 1 ? 1 : (capacity_staged > kFusedGrMaxT ? kFusedGrMaxT : capacity_staged);
attr[dev] = true;
}
*tile_out = tile[dev] ? tile[dev] : 2560;
- return v3 ? chunk3[dev] : chunk[dev];
+ return staged ? chunk_staged[dev] : chunk[dev];
}
-// The multi read as `variant` (1, 2 or 3): the norm, the down projection in launches of as many tokens as fit the
-// card, the up projection; the profile's stamps after the norm and after the down projection. Variant 1 is the
-// default read exactly as before (never main's opt-in STRATA_GR_V3 path, which fused_gr_read_multi takes first).
+// The multi read as `variant` (kHcPlain, kHcSplit or kHcStaged): the norm, the down projection in launches of as
+// many tokens as fit the card, the up projection; the profile's stamps after the norm and after the down projection.
+// kHcPlain is the default read exactly as before (never main's opt-in STRATA_GR_V3 path, which fused_gr_read_multi
+// takes first).
void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long long* stamp_buf, int stamp_i0) {
const int n_tok = m.T;
- if (variant >= 2) gr_norm_multi2_kernel<<<dim3((unsigned) n_tok, HC), THREADS, 0, st>>>(m);
+ if (variant >= kHcSplit) gr_norm_split_kernel<<<dim3((unsigned) n_tok, HC), THREADS, 0, st>>>(m);
else gr_norm_multi_kernel<<<n_tok, THREADS, 0, st>>>(m);
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st);
- const bool v3 = variant >= 3;
+ const bool staged = variant >= kHcStaged;
int tv = 2560;
- const int chunk_tok = down_chunk(v3, &tv);
- const size_t per_tok = (v3 ? (size_t) 2 * H_TILE : (size_t) tv) * sizeof(float);
+ const int chunk_tok = down_chunk(staged, &tv);
+ const size_t per_tok = (staged ? (size_t) 2 * H_TILE : (size_t) tv) * sizeof(float);
for (int c0 = 0; c0 < n_tok; c0 += chunk_tok) {
const int ct = n_tok - c0 < chunk_tok ? n_tok - c0 : chunk_tok;
GrMulti c{};
@@ -775,7 +778,7 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long
for (int k = 0; k < ct; ++k) c.a[k] = m.a[c0 + k];
}
const size_t smem = (size_t) ct * per_tok;
- if (v3) gr_down_multi3_kernel<<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
+ if (staged) gr_down_staged_kernel<<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
else if (tv == 1280) gr_down_multi_kernel<1280><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
else gr_down_multi_kernel<2560><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);
}
@@ -783,13 +786,13 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long
gr_up_multi_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);
}
-/// STRATA_HC_V2: unset = the newest the check accepts (3), 0 = v1, 2 = at most v2, 3 = v3
+/// STRATA_HC_SPLIT: unset or 2 = the newest the check accepts (staged), 1 = at most split, 0 = the plain read
int env_variant() {
- const char* e = std::getenv("STRATA_HC_V2");
- if (e == nullptr || e[0] == '\0') return 3;
- if (e[0] == '0' || e[0] == '1') return 1;
- if (e[0] == '2') return 2;
- return 3;
+ const char* e = std::getenv("STRATA_HC_SPLIT");
+ if (e == nullptr || e[0] == '\0') return kHcStaged;
+ if (e[0] == '0') return kHcPlain;
+ if (e[0] == '1') return kHcSplit;
+ return kHcStaged;
}
// per device: the variant `fused_gr_check` chose (0 = not checked yet)
@@ -890,12 +893,13 @@ void fused_gr_read(const FusedGrArgs& a, void* stream) {
namespace {
-/// v1, v2, v3 and (for one token) the single-token read on random bf16 weights and random inputs, 1..8 tokens,
-/// with and without the pending write; every output of v2 and v3 compared with v1's bit for bit (and v1's with the
-/// single-token read's). `why[v]` gets the first difference of variant v; false if the check itself could not run.
+/// The plain read, split, staged and (for one token) the single-token read on random bf16 weights and random inputs,
+/// 1..8 tokens, with and without the pending write; every output of split and staged compared with the plain read's
+/// bit for bit (and the plain read's with the single-token read's). `why[v]` gets the first difference of variant
+/// v; false if the check itself could not run.
bool fused_gr_selftest(bool ok_variant[4], std::string why[4]) {
constexpr int TM = kFusedGrMaxT;
- constexpr int NV = 4; // sets: 0 = v1, 1 = v2, 2 = v3, 3 = the single-token read
+ constexpr int NV = 4; // sets: 0 = plain, 1 = split, 2 = staged, 3 = single-token
std::mt19937 rng(20260930u);
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);
@@ -1011,8 +1015,8 @@ bool fused_gr_selftest(bool ok_variant[4], std::string why[4]) {
if (ok && T == 1 && single_ok && !all_same(set[3], set[0], T, apply, why[1])) single_ok = false;
}
}
- if (!single_ok && ok) { // v1 itself disagrees with the single-token read
- why[2] = why[3] = "v1 differs from the single-token read: " + why[1];
+ if (!single_ok && ok) { // the plain read itself disagrees with the single-token read
+ why[2] = why[3] = "the plain read differs from the single-token read: " + why[1];
ok_variant[2] = ok_variant[3] = false;
}
if (st != nullptr) {
@@ -1032,9 +1036,9 @@ int fused_gr_variant() {
cudaGetDevice(&dev);
const int v = dev >= 0 && dev < 64 ? g_variant[dev].load() : 0;
if (v > 0) return v;
- // not checked on this card: v1, unless STRATA_HC_V2 names a variant (a test such as gr_parity)
- const char* e = std::getenv("STRATA_HC_V2");
- return e != nullptr && (e[0] == '2' || e[0] == '3') ? env_variant() : 1;
+ // not checked on this card: the plain read, unless STRATA_HC_SPLIT names a variant (a test such as gr_parity)
+ const char* e = std::getenv("STRATA_HC_SPLIT");
+ return e != nullptr && (e[0] == '1' || e[0] == '2') ? env_variant() : kHcPlain;
}
void fused_gr_check() {
@@ -1042,30 +1046,33 @@ void fused_gr_check() {
cudaGetDevice(&dev);
if (dev < 0 || dev >= 64 || g_variant[dev].load() > 0) return;
const int want = env_variant();
- if (want == 1) {
- g_variant[dev].store(1);
- std::fprintf(stderr, "strata hc: CUDA%d: the hyper-connection read runs as v1 (STRATA_HC_V2=0)\n", dev);
+ if (want == kHcPlain) {
+ g_variant[dev].store(kHcPlain);
+ std::fprintf(stderr, "strata hc: CUDA%d: the hyper-connection read runs as the plain one (STRATA_HC_SPLIT=0)\n",
+ dev);
return;
}
bool okv[4];
std::string why[4];
const bool ran = fused_gr_selftest(okv, why);
- int use = 1;
- if (want >= 3 && okv[3]) use = 3;
- else if (okv[2]) use = 2;
+ int use = kHcPlain;
+ if (want >= kHcStaged && okv[kHcStaged]) use = kHcStaged;
+ else if (okv[kHcSplit]) use = kHcSplit;
g_variant[dev].store(use);
- if (!ran) std::fprintf(stderr, "strata hc: CUDA%d: the check of v2/v3 could not run (%s)\n", dev, why[0].c_str());
- for (int v = 3; v >= 2; --v)
+ if (!ran)
+ std::fprintf(stderr, "strata hc: CUDA%d: the check of split/staged could not run (%s)\n", dev, why[0].c_str());
+ static const char* const name[4] = {"", "plain", "split", "staged"};
+ for (int v = kHcStaged; v >= kHcSplit; --v)
if (v <= want && !okv[v] && ran)
- std::fprintf(stderr, "strata hc: CUDA%d: v%d differs from v1 on this card - not used: %s\n", dev, v,
- why[v].c_str());
+ std::fprintf(stderr, "strata hc: CUDA%d: the %s read differs from the plain read on this card - not used: "
+ "%s\n", dev, name[v], why[v].c_str());
static const char* const what[4] = {
- "", "v1 (the norm per token, the down projection on 41 blocks)",
- "v2 (the norm per token and stream, then v1's down projection)",
- "v3 (the norm per token and stream, the down projection's activations staged ahead by cp.async)"};
+ "", "the plain read (the norm per token, the down projection on 41 blocks)",
+ "split (the norm per token and stream, then the plain read's down projection)",
+ "staged (the norm per token and stream, the down projection's activations staged ahead by cp.async)"};
std::fprintf(stderr, "strata hc: CUDA%d: the hyper-connection read runs as %s%s\n", dev, what[use],
- use >= 2 ? "; checked bit for bit against v1 on this card (STRATA_HC_V2=2 or 0 for the earlier ones)"
- : "");
+ use >= kHcSplit ? "; checked bit for bit against the plain read on this card (STRATA_HC_SPLIT=1 or 0 "
+ "for the earlier ones)" : "");
}