gfx12 decode: the split HC norm from registers (-1.8 us a read), the host-flag waits folded into the copies that follow them (-2 nodes a layer); STRATA_VERIFY_NODES lists the window graph on HIP; the stage profile splits the FFN-side HC read; STRATA_DUMMY_NODES=k measures what an empty graph node costs

This commit is contained in:
Niko1221 committed 2026-10-11 04:04:07 +02:00
1 parent bb519f5bca
commit 7fc02cb5c2
6 files changed
+218 -8

No files matched your search

+82 -3
View File
@@ -1,5 +1,6 @@
// src/core/verify.cpp - see include/strata/core/verify.hpp.
#include "strata/core/batch_rows.hpp"
#include "strata/kernels/gfx_arch.hpp"
#include "strata/core/verify.hpp"
#include "strata/core/remote_expert_opt.hpp"
#include "strata/core/dma_batch.hpp"
@@ -205,6 +206,30 @@ bool head_mix_multi_enabled() {
#endif
}
// 143-amdk: the wait for a host flag folded into the copy that follows it (wait_copy_i32_from_mapped,
// copy_rows_from_mapped_after): same reads, same values, one kernel node fewer each. A dependent kernel costs ~2.4 us in a
// gfx1201 graph whatever it does; measured on gfx12 only, so it is on there by default. STRATA_FUSE_WAIT=0 / 1 forces it
// off / on (HIP builds; a CUDA build never fuses).
bool fuse_wait_on() {
#if defined(STRATA_USE_HIP)
static const int env = [] {
const char* v = std::getenv("STRATA_FUSE_WAIT");
return v != nullptr ? (std::atoi(v) != 0 ? 1 : 0) : -1;
}();
if (env >= 0) return env != 0;
static int per_dev[64]; // 0 unknown, 1 no, 2 yes
int dev = 0;
if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) return false;
if (!per_dev[dev]) {
cudaDeviceProp p;
per_dev[dev] = cudaGetDeviceProperties(&p, dev) == cudaSuccess && strata::kernels::gfx_arch_is_gfx12(p.gcnArchName) ? 2 : 1;
}
return per_dev[dev] == 2;
#else
return false;
#endif
}
bool one_token_self_commit() {
#if defined(STRATA_USE_HIP)
static const bool on = [] {
@@ -1001,6 +1026,10 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
(int) (l * kProfPer + (half == 0 ? 27 : 30)));
};
const bool q8_attn = gr_read_group(0, pending, inj2_, inj_); // true: xq_ holds mixed's q8_1 (STRATA_QFUSE)
{ // measurement only: STRATA_DUMMY_NODES=k adds k empty kernel nodes per layer (what one node costs in a window)
static const int dummy = [] { const char* e = std::getenv("STRATA_DUMMY_NODES"); return e != nullptr ? std::atoi(e) : 0; }();
for (int d = 0; d < dummy; ++d) gpu_noop(cs);
}
if (inputs_pending) { // the window's steps and positions, before the first mixer reads them
join(1);
inputs_pending = false;
@@ -1331,6 +1360,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
}
stamp(l, 16, grp);
const bool q8_ffn = gr_read_group(1, true, inj_, inj2_); // true: nat_xq_ holds the MoE input's q8_1 (STRATA_QFUSE)
stamp(l, 32, grp); // the profile only: the FFN-side read ends here, the router starts
// with CPU experts in the window the shared expert forks off once the doorbell is published (it no longer
// competes with it for the GPU's first microseconds); STRATA_SH_FORK_LATE=0 forks at the top as before
static const bool sh_fork_late_env = [] {
@@ -1511,8 +1541,12 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
wait_flag_ge_or(m_flagA_, ring, skip_ + grp, cs);
copy_i32_from_mapped_unless(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, skip_ + grp, ring, cs);
} else {
if (fuse_wait_on() && !prof_on_) { // 143-amdk: one launch for the wait and the copy
wait_copy_i32_from_mapped(m_flagA_, ring, pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
} else {
wait_flag_ge(m_flagA_, ring, cs); // the pool published this group's GPU plan
copy_i32_from_mapped(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
}
}
stamp(l, 19, grp);
grouped(p_ptr, p_start, p_counts, 0, hit_out);
@@ -1535,10 +1569,14 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
copy_or_zero_from_mapped(parts_out, m_ymiss_ + (size_t) tb * K * N, (long long) n * K * N,
skip_ + grp, ring, cs);
} else {
wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
const bool fuse_rows = fuse_wait_on() && !prof_on_ && !remote_opt_ && dec_batch; // 143-amdk
if (!fuse_rows) wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
stamp(l, 23, grp);
if (remote_opt_) // #578: the helper's rows come back reduced; skip them as well
remote_opt_->copy_rows(parts_out, m_ymiss_ + (size_t) tb * K * N, tb, n, p_dst, p_counts + 1, cs);
else if (fuse_rows) // the wait folded into the copy's blocks
copy_rows_from_mapped_after(m_flag_, ring, parts_out, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K, N,
p_dst, p_counts + 1, cs);
else if (dec_batch) // only the CPU rows cross PCIe (p_dst[0, counts[1]) = the GPU's own rows)
copy_rows_from_mapped(parts_out, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K, N,
p_dst, p_counts + 1, cs);
@@ -1697,9 +1735,9 @@ std::string Verifier::profile_report() {
if (!prof_on_ || prof_windows_ == 0) return std::string();
static const char* names[kProfPer] = {"-", "hc-read0", "q8+qkv/q-idx gemv", "conv", "ab", "z", "rec", "q8+kv-idx",
"k/v+norm-rope", "kv+idx append", "q+q-idx", "scores+topk", "kv-resolve",
"attention", "gate", "", "out-proj", "hc-read1+router", "shared+quant",
"attention", "gate", "", "out-proj", "router+plan+bell", "shared+quant",
"waitA", "VRAM hits", "waitB", "PCIe grp", "waitCPU", "copy+combine",
"(gap)", "head", " hc0 norm", " hc0 down", " hc0 up", "", "", ""};
"(gap)", "head", " hc0 norm", " hc0 down", " hc0 up", " hc1 norm", " hc1 down", " hc1 up"};
std::string out;
char b[80];
double total = 0;
@@ -1765,6 +1803,38 @@ bool Verifier::capture(int T, std::string& err) {
err = std::string("verify: end capture: ") + cudaGetErrorString(ce);
return false;
}
#if defined(STRATA_USE_HIP) // 143-amdk: the same listing on HIP (kernel names by hipKernelNameRefByPtr); STRATA_VERIFY_NODES=2 also lists the first nodes in graph order
if (std::getenv("STRATA_VERIFY_NODES") != nullptr) {
size_t nn = 0;
hipGraphGetNodes(graph, nullptr, &nn);
std::vector<hipGraphNode_t> nodes(nn);
hipGraphGetNodes(graph, nodes.data(), &nn);
std::map<std::string, int> kinds;
std::string seq;
size_t shown = 0;
for (hipGraphNode_t nd : nodes) {
hipGraphNodeType ty;
hipGraphNodeGetType(nd, &ty);
std::string name = "type" + std::to_string((int) ty);
if (ty == hipGraphNodeTypeKernel) {
hipKernelNodeParams kp{};
if (hipGraphKernelNodeGetParams(nd, &kp) == hipSuccess && kp.func != nullptr) {
const char* fn = hipKernelNameRefByPtr(kp.func, nullptr);
if (fn != nullptr) name = fn;
}
} else if (ty == hipGraphNodeTypeMemcpy) name = "memcpy";
else if (ty == hipGraphNodeTypeMemset) name = "memset";
++kinds[name];
if (std::atoi(std::getenv("STRATA_VERIFY_NODES")) >= 2 && shown++ < 400) seq += "\n " + name.substr(0, 110);
}
std::vector<std::pair<int, std::string>> v;
for (auto& [k2, c] : kinds) v.push_back({c, k2});
std::sort(v.rbegin(), v.rend());
std::fprintf(stderr, "strata verify: the %d-token window graph has %zu nodes:", T, nn);
for (size_t i = 0; i < v.size() && i < 80; ++i) std::fprintf(stderr, "\n %4d x %.110s", v[i].first, v[i].second.c_str());
std::fprintf(stderr, "\n%s\n", seq.c_str());
}
#endif
#if !defined(STRATA_USE_HIP) // a CUDA debug listing (node types, kernel names)
if (std::getenv("STRATA_VERIFY_NODES") != nullptr) { // what the window graph holds
size_t nn = 0;
@@ -1956,6 +2026,15 @@ void Verifier::accumulate_profile(const unsigned long long* stamps) {
prof_sum_[kind][i] += (double) (x - prev);
prev = x;
}
{ // the FFN-side read split like hc-read0: norm 30, down 31, up 32; what is left of 17 is the router, plan, doorbell
const double n1 = gap(at(l, 30), at(l, 16)), d1 = gap(at(l, 31), at(l, 30)), u1 = gap(at(l, 32), at(l, 31));
if (n1 > 0 && d1 > 0 && u1 > 0) {
prof_sum_[kind][30] += n1;
prof_sum_[kind][31] += d1;
prof_sum_[kind][32] += u1;
prof_sum_[kind][17] -= gap(at(l, 32), at(l, 16));
}
}
if (l + 1 < le_) prof_sum_[kind][25] += gap(at(l + 1, 0), at(l, 24));
const double dn = gap(at(l, 27), at(l, 0)), dd = gap(at(l, 28), at(l, 27)), du = gap(at(l, 1), at(l, 28));
if (dn > 0 && dd > 0 && du > 0) { // the split exists: show it split, not twice
+16 -1
View File
@@ -236,10 +236,15 @@ __global__ void copy_from_mapped_kernel(float4* __restrict__ dst, const volatile
// the CPU rows of a verify window, skipping the rows the GPU plan computes itself (the
// pool writes +0.0 into those, so this writes +0.0 too): block = row, the plan's hit rows `dst[0, *count)`.
__global__ void copy_rows_from_mapped_kernel(float4* __restrict__ dst, const volatile float4* src, int64_t row4,
const int32_t* __restrict__ hit_rows, const int32_t* __restrict__ count) {
const int32_t* __restrict__ hit_rows, const int32_t* __restrict__ count,
const volatile uint32_t* wait_flag = nullptr, uint32_t wait_value = 0) {
const int row = blockIdx.x;
__shared__ int hit;
if (threadIdx.x == 0) {
if (wait_flag != nullptr) { // 143-amdk: the wait_flag_ge launch folded in
while (*wait_flag < wait_value) strata_spin_pause();
__threadfence_system();
}
int h = 0;
const int c = *count;
for (int i = 0; i < c; ++i) h |= hit_rows[i] == row;
@@ -281,6 +286,16 @@ void copy_rows_from_mapped(float* dst, const float* src, int64_t rows, int64_t w
copy_rows_from_mapped_kernel<<<(unsigned) rows, 128, 0, (cudaStream_t) stream>>>((float4*) dst, (const volatile float4*) src,
width / 4, hit_rows, count);
}
void copy_rows_from_mapped_after(const uint32_t* flag, uint32_t value, float* dst, const float* src, int64_t rows,
int64_t width, const int32_t* hit_rows, const int32_t* count, void* stream) {
if (rows <= 0) return;
if ((width & 3) != 0 || ((uintptr_t) dst & 15) != 0 || ((uintptr_t) src & 15) != 0) {
std::fprintf(stderr, "copy_rows_from_mapped_after: width must be a multiple of 4 and both pointers 16-byte aligned\n");
std::exit(1);
}
copy_rows_from_mapped_kernel<<<(unsigned) rows, 128, 0, (cudaStream_t) stream>>>(
(float4*) dst, (const volatile float4*) src, width / 4, hit_rows, count, (const volatile uint32_t*) flag, value);
}
void copy_from_mapped(float* dst, const float* src, int64_t n, void* stream) {
if (n <= 0) return;
if ((n & 3) != 0 || ((uintptr_t) dst & 15) != 0 || ((uintptr_t) src & 15) != 0) {
+88 -4
View File
@@ -4,6 +4,7 @@
#include "strata/kernels/fused_gr.hpp"
#include "strata/kernels/bf16_bits.hpp"
#include "strata/kernels/verify_kernels.hpp"
#include "strata/kernels/gfx_arch.hpp"
#include "s26_tsum.cuh"
#include <cuda_fp16.h>
@@ -618,6 +619,66 @@ __global__ void __launch_bounds__(THREADS) gr_norm_split_kernel(GrMulti m) {
for (int d = t; d < N; d += THREADS) xn[c * N + d] *= rs;
}
// 143-amdk: gr_norm_split_kernel with the loads of a thread's (at most three) float4 issued together and the scaled
// product written once from registers. The same elements go through the same operations in the same order (the
// thread's own sum of squares ascends in k; the warp sum, part[] sum and rsqrt are the kernel's), so rs and xn are
// bitwise the same; what goes is the loop's one-iteration-at-a-time loads and the second pass that read xn back from
// global memory (a write, a barrier, a read: two memory latencies).
__global__ void __launch_bounds__(THREADS) gr_norm_split_fast_kernel(GrMulti m) {
__shared__ float part[WARPS];
__shared__ float s_rs;
const FusedGrArgs& a = m.a[blockIdx.x];
const int c = blockIdx.y;
float* xn = m.xn + (size_t) blockIdx.x * D;
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const float gw = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f;
// the thread's float4 of stream c: i = 4 t + 1024 k in [c N, (c + 1) N), ascending k; N / 1024 = 2.5 -> at most 3
constexpr int KS = 3;
int k0 = (c * N - 4 * t + THREADS * 4 - 1) / (THREADS * 4);
if (k0 < 0) k0 = 0;
float4 r[KS], g[KS];
bool ok[KS];
#pragma unroll
for (int j = 0; j < KS; ++j) {
const int i = t * 4 + (k0 + j) * THREADS * 4;
ok[j] = i < (c + 1) * N;
if (ok[j]) {
r[j] = *reinterpret_cast<const float4*>(a.R + i);
g[j] = *reinterpret_cast<const float4*>(a.w_norm + i);
if (a.apply) {
const float4 b = *reinterpret_cast<const float4*>(a.bo_prev + (i - c * N));
r[j].x = fmaf(b.x, gw, r[j].x); r[j].y = fmaf(b.y, gw, r[j].y);
r[j].z = fmaf(b.z, gw, r[j].z); r[j].w = fmaf(b.w, gw, r[j].w);
}
}
}
float ss = 0.0f;
#pragma unroll
for (int j = 0; j < KS; ++j) {
if (ok[j]) {
const float sq = r[j].x * r[j].x + r[j].y * r[j].y + r[j].z * r[j].z + r[j].w * r[j].w;
ss += sq;
r[j] = make_float4(r[j].x * g[j].x, r[j].y * g[j].y, r[j].z * g[j].z, r[j].w * g[j].w);
}
}
const float v = warp_sum(ss);
if (lane == 0) part[warp] = v;
__syncthreads();
if (t == 0) {
float s = 0.0f;
for (int w = 0; w < WARPS; ++w) s += part[w];
s_rs = rsqrtf(s / (float) N + a.eps);
a.rs[c] = s_rs;
}
__syncthreads();
const float rs = s_rs;
#pragma unroll
for (int j = 0; j < KS; ++j)
if (ok[j])
*reinterpret_cast<float4*>(xn + t * 4 + (k0 + j) * THREADS * 4) =
make_float4(r[j].x * rs, r[j].y * rs, r[j].z * rs, r[j].w * rs);
}
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 && !defined(__HIPCC__)
#define STRATA_GR_CP_ASYNC 1
#endif
@@ -864,18 +925,41 @@ bool gr_down_max4() {
return on;
}
// the split norm with registers (gr_norm_split_fast_kernel): gfx12 only until another arch is measured;
// STRATA_GR_NORM_FAST=0 / 1 forces it off / on
bool norm_fast_on() {
#if defined(__HIPCC__)
static const int env = [] { const char* e = std::getenv("STRATA_GR_NORM_FAST"); return e != nullptr ? (std::atoi(e) != 0 ? 1 : 0) : -1; }();
if (env >= 0) return env != 0;
static int per_dev[64]; // 0 unknown, 1 no, 2 yes
int dev = 0;
if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) return false;
if (!per_dev[dev]) {
cudaDeviceProp p;
per_dev[dev] = cudaGetDeviceProperties(&p, dev) == cudaSuccess && strata::kernels::gfx_arch_is_gfx12(p.gcnArchName) ? 2 : 1;
}
return per_dev[dev] == 2;
#else
return false;
#endif
}
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 STRATA_GR_FAST_BUILD
// gfx906: the latency-hidden norm/up (STRATA_GR_FAST=0: off) - the same sums in the same order as the kernels
// they replace (gr_parity checks the multi-token read against single-token calls bitwise)
const bool fast = gr_fast();
if (variant >= kHcSplit) gr_norm_split_kernel<<<dim3((unsigned) n_tok, HC), THREADS, 0, st>>>(m);
else if (fast) gr_norm_fast_kernel<<<n_tok, THREADS, 0, st>>>(m);
if (variant >= kHcSplit) {
if (norm_fast_on()) gr_norm_split_fast_kernel<<<dim3((unsigned) n_tok, HC), THREADS, 0, st>>>(m);
else gr_norm_split_kernel<<<dim3((unsigned) n_tok, HC), THREADS, 0, st>>>(m);
} else if (fast) gr_norm_fast_kernel<<<n_tok, THREADS, 0, st>>>(m);
else gr_norm_multi_kernel<<<n_tok, THREADS, 0, st>>>(m);
#else
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 (variant >= kHcSplit) {
if (norm_fast_on()) gr_norm_split_fast_kernel<<<dim3((unsigned) n_tok, HC), THREADS, 0, st>>>(m);
else gr_norm_split_kernel<<<dim3((unsigned) n_tok, HC), THREADS, 0, st>>>(m);
} else gr_norm_multi_kernel<<<n_tok, THREADS, 0, st>>>(m);
#endif
if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st);
const bool staged = variant >= kHcStaged;
+21
View File
@@ -1193,6 +1193,23 @@ void copy_or_zero_from_mapped(float* dst, const float* src, long long n, const u
check("copy_or_zero_from_mapped");
}
namespace {
__global__ void wait_copy_i32_kernel(const volatile uint32_t* flag, uint32_t value, int32_t* __restrict__ dst,
const volatile int32_t* src, int n) {
if (threadIdx.x == 0) {
while (*flag < value) strata_spin_pause();
__threadfence_system();
}
__syncthreads();
for (int i = threadIdx.x; i < n; i += blockDim.x) dst[i] = src[i];
}
} // namespace
void wait_copy_i32_from_mapped(const uint32_t* flag, uint32_t value, int32_t* dst, const int32_t* src, int64_t n,
void* stream) {
wait_copy_i32_kernel<<<1, 128, 0, (cudaStream_t) stream>>>(flag, value, dst, src, (int) n);
check("wait_copy_i32_from_mapped");
}
void wait_flag_ge(const uint32_t* flag, uint32_t value, void* stream) {
wait_flag_ge_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(flag, value);
check("wait_flag_ge");
@@ -1239,6 +1256,10 @@ __global__ void gpu_stamp_kernel(unsigned long long* buf, int i) {
buf[i] = t;
}
} // namespace
namespace {
__global__ void noop_kernel() {}
} // namespace
void gpu_noop(void* stream) { noop_kernel<<<1, 32, 0, (cudaStream_t) stream>>>(); }
void gpu_stamp(unsigned long long* buf, int i, void* stream) {
if (pdl_scope()) launch_pdl(gpu_stamp_kernel<true>, dim3(1), dim3(1), 0, (cudaStream_t) stream, buf, i);
else gpu_stamp_kernel<false><<<1, 1, 0, (cudaStream_t) stream>>>(buf, i);