Files
Strata/src/core/verify.cpp
T
Eddoursul fbb8bfa3e2 The argmax and the draft's top probability over many blocks a row
The verify window's head ended with sampler_greedy_kernel, one 1024-thread block a token over the 248,320 logits,
and each draft pass with it and row_top_prob over the 40,525-token draft head, one block each.  argmax_rows scans a
row in up to 128 blocks (4,096 logits each), the row's last block merging their picks (the larger value, on equality
the lower index: the one-block kernel's order); row_top_prob_split computes row_top_prob's 32 warps' sums in 8 blocks,
the row's last block adding them in order.  Both keep their counters and partials in per-row slots of one scratch,
whatever a launch's row count.  Bitwise: verify_parity (ties, NaN, -inf, 1-8 rows on one scratch), and the window
hashes on fixed text equal the previous build's (IQ3_S, UD-Q4_K_XL; thinking and code text).

CUPTI, IQ3_S thinking text: the head's argmax 84-86 -> 9.7 us a window, a draft pass's argmax and top probability
15.1 + 10.1 -> 5.6 + 4.3 us; ~105 us a round (~0.7%).

Claude-Session: https://claude.ai/code/session_019XxRxQQNyk7Ycdr4ComSs1
2026-09-30 06:09:02 +02:00

1430 lines
78 KiB
C++

// src/core/verify.cpp - see include/strata/core/verify.hpp.
#include "strata/core/verify.hpp"
#if defined(_WIN32)
#include <intrin.h>
#endif
#include "strata/core/native_head.hpp"
#include "strata/kernels/iq_kernels.hpp"
#include "strata/kernels/cpu/expert_layout.hpp"
#include "strata/kernels/bf16_gemv.hpp"
#include "strata/kernels/cpu/expert.hpp"
#include "strata/kernels/elementwise.hpp"
#include "strata/kernels/fused_gr.hpp"
#include "strata/kernels/gr.hpp"
#include "strata/kernels/kv_q8.hpp"
#include "strata/kernels/native_mmvq.hpp"
#include "strata/kernels/native_moe.hpp"
#include "strata/kernels/native_ple_postops.hpp"
#include "strata/kernels/native_qsa.hpp"
#include "strata/kernels/native_qsa_indexer.hpp"
#include "strata/kernels/native_router.hpp"
#include "strata/kernels/native_rope.hpp"
#include "strata/kernels/ngram.hpp"
#include "strata/kernels/ple.hpp"
#include "strata/kernels/qsa.hpp"
#include "strata/kernels/qsa_decode_attn.hpp"
#include "strata/kernels/qsa_select.hpp"
#include "strata/kernels/quantize_act.hpp"
#include "strata/kernels/s2_expert_grouped.hpp"
#include "strata/kernels/sampler.hpp"
#include "strata/kernels/shared_expert.hpp"
#include "strata/kernels/verify_kernels.hpp"
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <immintrin.h>
namespace strata::core {
namespace {
constexpr float EPS = 1e-6f;
using Clock = std::chrono::steady_clock;
double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }
const bool g_dbg = std::getenv("STRATA_VERIFY_DEBUG") != nullptr;
#define VDBG(...) do { if (g_dbg) { std::fprintf(stderr, "verify dbg: " __VA_ARGS__); std::fflush(stderr); } } while (0)
struct Bump {
uint8_t* base = nullptr;
uint64_t used = 0;
template <typename T> T* take(uint64_t n) {
T* p = base ? (T*) (base + used) : nullptr;
used += (n * sizeof(T) + 255) & ~255ull;
return p;
}
};
bool mapped(size_t bytes, void** h, void** d, unsigned flags = cudaHostAllocMapped) {
if (cudaHostAlloc(h, bytes, flags) != cudaSuccess) return false;
std::memset(*h, 0, bytes);
return cudaHostGetDevicePointer(d, *h, 0) == cudaSuccess;
}
strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) {
strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
s.n_head = g.n_head;
s.n_head_kv = g.n_head_kv;
s.head_dim = g.head_dim;
s.idx_n_head = g.idx_q_heads;
s.idx_dim = g.idx_key_dim;
return s;
}
const WeightRef* need(const LayerView& v, const char* suffix, std::string& err) {
const WeightRef* r = v.get(suffix);
if (r == nullptr && err.empty()) err = v.name(suffix) + " is missing";
return r;
}
bool native_of(const WeightRef* w, const std::string& name, std::string& err) {
if (w == nullptr) return false;
if (w->native_data == nullptr) {
err = "verify: " + name + " is not served natively (run with --native)";
return false;
}
return true;
}
} // namespace
Verifier::~Verifier() {
if (cs_) cudaStreamSynchronize(cs_);
for (auto& e : exec_)
if (e) cudaGraphExecDestroy(e);
if (commit_exec_) cudaGraphExecDestroy(commit_exec_);
if (cs_) cudaStreamDestroy(cs_);
if (copy_) { cudaStreamSynchronize(copy_); cudaStreamDestroy(copy_); }
if (side_) cudaStreamDestroy(side_);
if (shs_) cudaStreamDestroy(shs_);
if (g2s_) cudaStreamDestroy(g2s_);
if (cps_) cudaStreamDestroy(cps_);
if (kst_) cudaStreamDestroy(kst_);
if (sgs_) cudaStreamDestroy(sgs_);
for (cudaEvent_t e : {join_, bfork_, bjoin_, pfork_, pjoin_, shfork_, shjoin_, res_ready_, done_, g2fork_,
g2join_[0], g2join_[1], cpfork_, cpjoin_[0], cpjoin_[1], efork_, rdone_, kjoin_, iproj_,
sgjoin_})
if (e) cudaEventDestroy(e);
if (arena_) cudaFree(arena_);
if (stamps_) cudaFree(stamps_);
void* hosts[] = {h_tok_, h_step_, h_pos_, h_commit_, h_ple_, h_out_, h_x_, h_ids_, h_w_, h_seq_, h_flag_, h_ymiss_,
h_flagA_, h_plan_, h_flagB_, h_pids_, h_pw_, h_pseq_, h_pleflag_, h_rows_, h_res_, h_flag2_,
h_list2_, h_xk_, h_xq1_};
for (void* h : hosts)
if (h) cudaFreeHost(h);
}
bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& ss, const VerifyHits& hits,
const NativeHead* head, int max_t, std::string& err) {
wt_ = &wt;
g_ = &g;
ss_ = &ss;
hits_ = hits;
head_ = head;
max_t_ = max_t;
if (max_t < 2 || max_t > strata::kernels::kVerifyMaxT || max_t > strata::kernels::cpu::MAXT) {
err = "verify: the window must hold 2.." + std::to_string(strata::kernels::kVerifyMaxT) + " tokens";
return false;
}
if (hits.res == nullptr || hits.cache_base == nullptr || hits.blob <= 0 || hits.slots <= 0) {
err = "verify: needs the profile-filled VRAM expert tier (--expert-profile and --expert-cache)";
return false;
}
std::string why;
if (!layer_verify_compatible(why)) {
err = "verify: " + why + " (the verify window reproduces the default native decode path)";
return false;
}
if (!strata::kernels::fused_gr_supported(g.n_embd, g.hc, g.hc_lr) || ss.k != 10 || g.ssm_state_size != 128 ||
g.ssm_d_conv != 4) {
err = "verify: geometry differs from the artifact's";
return false;
}
if (ss.ple.ready() && (!strata::kernels::ple_native_postops_enabled() || !strata::kernels::ple_native_bf16() ||
(ss.ple.w.key_bf16 == nullptr && ss.ple.w.key_native_data == nullptr))) {
err = "verify: the PLE block needs the native postops and value and a BF16 or native key";
return false;
}
const WeightRef* wo = wt.find("output.weight");
if (wo == nullptr) { err = "verify: output.weight is missing"; return false; }
n_vocab_ = wo->ne1;
const strata::kernels::QsaShapes s = shapes_of(g);
cap_ = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
max_blocks_ = ss.qsa_states[0].max_cells / s.idx_block + 2;
attn_scratch_floats_ = (int64_t) strata::kernels::qsa_decode_attn_scratch_floats(cap_, s);
const uint64_t T = (uint64_t) max_t, N = (uint64_t) g.n_embd, HC = (uint64_t) g.hc, K = (uint64_t) ss.k;
const uint64_t C = (uint64_t) g.ssm_conv_channels, ZV = (uint64_t) g.ssm_value_dim, HV = (uint64_t) g.ssm_v_heads;
const uint64_t NH = (uint64_t) g.n_head, HD = (uint64_t) g.head_dim, NKV = (uint64_t) g.n_head_kv;
const uint64_t IQ = (uint64_t) g.idx_q_heads, ID = (uint64_t) g.idx_key_dim;
const uint64_t nG = (uint64_t) g.n_gdn_layers(), nQ = (uint64_t) g.n_qsa_layers();
const uint64_t HS = (uint64_t) strata::kernels::NG_HIST * strata::kernels::NG_HC_DIM;
const uint64_t TS = (uint64_t) (s.idx_block - 1) * ID;
const int max_in = (int) std::max<uint64_t>(std::max<uint64_t>(N, ZV), NH * HD);
// ---- mapped staging (portable where the second GPU writes: its rows and flags)
const unsigned portable = cudaHostAllocMapped | cudaHostAllocPortable;
list2_stride_ = (4 + (int64_t) (T * K) + 3) & ~3ll;
const auto& lay = strata::kernels::cpu::expert_layout();
quant_router_ = strata::kernels::native_router_enabled() && lay.native;
xk_layer_.assign((size_t) g.n_layers, 0);
for (int64_t l = 0; quant_router_ && l < g.n_layers; ++l)
xk_layer_[(size_t) l] = pool_takes_q8k(lay.fmt[(size_t) l]);
xk_stride_ = ((int64_t) (N / 256) * 292 + 15) & ~15ll;
bool ok = mapped(T * 4, (void**) &h_tok_, (void**) &m_tok_) &&
mapped(T * strata::kernels::kStepCount * 4, (void**) &h_step_, (void**) &m_step_) &&
mapped(T * NH * 4, (void**) &h_pos_, (void**) &m_pos_) &&
mapped((2 + T) * 4 + 16, (void**) &h_commit_, (void**) &m_commit_) &&
mapped(T * N * 4, (void**) &h_ple_, (void**) &m_ple_) &&
mapped(T * 4 + 16, (void**) &h_out_, (void**) &m_out_) &&
mapped(T * N * 4, (void**) &h_x_, (void**) &m_x_) &&
mapped(T * K * 4, (void**) &h_ids_, (void**) &m_ids_) &&
mapped(T * K * 4, (void**) &h_w_, (void**) &m_w_) &&
mapped(64, (void**) &h_seq_, (void**) &m_seq_) &&
mapped(64, (void**) &h_flag_, (void**) &m_flag_) &&
mapped(64, (void**) &h_flagA_, (void**) &m_flagA_) &&
mapped(64, (void**) &h_flagB_, (void**) &m_flagB_) &&
mapped(T * K * N * 4, (void**) &h_ymiss_, (void**) &m_ymiss_, portable) &&
mapped(128, (void**) &h_flag2_, (void**) &m_flag2_, portable) &&
mapped((size_t) (2 * list2_stride_) * 4, (void**) &h_list2_, (void**) &m_list2_) &&
mapped(T * (uint64_t) xk_stride_, (void**) &h_xk_, (void**) &m_xk_) &&
mapped(T * (N / 32) * 36, (void**) &h_xq1_, (void**) &m_xq1_) &&
mapped(2 * T * K * 4, (void**) &h_pids_, (void**) &m_pids_) &&
mapped(2 * T * K * 4, (void**) &h_pw_, (void**) &m_pw_) &&
mapped(64, (void**) &h_pseq_, (void**) &m_pseq_) &&
mapped(64, (void**) &h_pleflag_, (void**) &m_pleflag_) &&
mapped(T * HC * N * 4, (void**) &h_rows_, (void**) &m_rows_);
if (!ok) { err = "verify: mapped staging allocation failed"; return false; }
// A plan block, one per token group: counts(4) | start(cap+1) | dst(cap) | tok(cap) | pad | ptr(cap u64) |
// ptr2(cap u64) | start2(cap+1). The main GPU's hits (`verify_hit_plan`, device) fill counts [groups, entries],
// start, dst, tok and ptr; the pool's PCIe share (mapped, `sink_`) counts [-, entries, groups], dst, tok, ptr2 and
// start2.
{
const int64_t cap = (int64_t) (T * K);
plan_ptr_off_ = (4 + (cap + 1) + cap + cap + 1) & ~1ll;
plan_i32_ = (plan_ptr_off_ + 4 * cap + (cap + 1) + 2) & ~1ll;
plan_stride_ = plan_i32_ + 16;
if (!mapped((size_t) plan_i32_ * 4 * 2 + 64, (void**) &h_plan_, (void**) &m_plan_)) {
err = "verify: mapped plan allocation failed";
return false;
}
sink_.counts = h_plan_;
sink_.dst = h_plan_ + 4 + cap + 1;
sink_.tok = sink_.dst + cap;
sink_.ptr2 = (unsigned long long*) (h_plan_ + plan_ptr_off_) + cap;
sink_.start2 = h_plan_ + plan_ptr_off_ + 4 * cap;
sink_.cap = cap;
sink_.publish = &Verifier::publish_plan;
sink_.host_rows_only = true; // the gather-combine (post) reads the GPUs' from VRAM
sink_.fetch = &Verifier::fetch_dma;
sink_.ctx = this;
}
// the residency snapshot, rounded up to whole 16-byte copies
res_words_ = (g.n_layers * g.n_expert + 3) & ~3ll;
if (!mapped((size_t) res_words_ * 4, (void**) &h_res_, (void**) &m_res_)) {
err = "verify: mapped residency allocation failed";
return false;
}
std::memcpy(h_res_, hits.res, (size_t) (g.n_layers * g.n_expert) * sizeof(int32_t));
// ---- the device arena: the same sequence counted, then carved
auto carve = [&](Bump& b) {
tok_ = b.take<int32_t>(T); step_ = b.take<int32_t>(T * strata::kernels::kStepCount);
pos_ = b.take<int32_t>(T * NH); commit_ = b.take<int32_t>(2 + T);
ple_ = b.take<float>(T * N); emb_ = b.take<float>(T * N); R_ = b.take<float>(T * HC * N);
mixed_ = b.take<float>(T * N); bo_ = b.take<float>(T * N);
inj_ = b.take<float>(T * HC); inj2_ = b.take<float>(T * HC);
lo_ = b.take<float>(T * (uint64_t) g.hc_lr); rs_ = b.take<float>(T * HC);
grs_ = (float*) b.take<uint8_t>(strata::kernels::fused_gr_scratch_bytes());
xq_ = b.take<uint8_t>(strata::kernels::native_q8_1_bytes(max_in, (int) T));
xil_ = b.take<uint8_t>(strata::kernels::native_q8_1_il_bytes(max_in, (int) T));
qkv_L_ = b.take<float>(nG * T * C); h_L_ = b.take<float>(nG * T * C);
gate_L_ = b.take<float>(nG * T * HV); beta_L_ = b.take<float>(nG * T * HV);
z_ = b.take<float>(T * ZV); y_ = b.take<float>(T * ZV); y_dummy_ = b.take<float>(T * ZV);
qfull_ = b.take<float>(T * NH * 2 * HD); qcur_ = b.take<float>(T * NH * HD);
kcur_ = b.take<float>(T * NKV * HD); vcur_ = b.take<float>(T * NKV * HD);
idx_raw_L_ = b.take<float>(nQ * T * ID); qidx_ = b.take<float>(T * IQ * ID);
scores_ = b.take<float>(T * (uint64_t) max_blocks_); sel_ = b.take<int32_t>(T * (uint64_t) cap_);
attn32_ = b.take<float>(T * NH * HD);
attn_scratch_ = b.take<float>(T * (uint64_t) attn_scratch_floats_);
tail_snap_ = b.take<float>(nQ * TS);
logits_ = b.take<float>(T * (uint64_t) g.n_expert); w_ = b.take<float>(T * K); ids_ = b.take<int32_t>(T * K);
plogits_ = b.take<float>(2 * T * (uint64_t) g.n_expert); pw_ = b.take<float>(T * K);
pids_ = b.take<int32_t>(T * K);
pmixed_ = b.take<float>(T * N); gate_ema_ = b.take<float>((uint64_t) g.n_layers * HC * N);
pbias_ = b.take<float>((uint64_t) (g.n_layers * g.n_expert));
shared_ = b.take<float>(T * N); hit_out_ = b.take<float>(T * K * N);
hit_slot_ = b.take<int32_t>(T * K); hit_dst_ = b.take<int32_t>(T * K); hit_count_ = b.take<int32_t>(4);
list2_ = b.take<int32_t>(2 * (uint64_t) list2_stride_);
ring_count_ = b.take<unsigned>(2);
plan_ = b.take<int32_t>(2 * (uint64_t) plan_stride_); dplan_ = b.take<int32_t>(2 * (uint64_t) plan_stride_);
res_ = b.take<int32_t>((uint64_t) res_words_); slot_ptr_ = b.take<unsigned long long>((uint64_t) hits.slots);
staging_ = b.take<uint8_t>((uint64_t) kStagingBlobs * strata::kernels::cpu::expert_layout().max_blob);
hit_xq_ = b.take<uint8_t>(T * (N / 32) * 34); hit_xs_ = b.take<float>(T * (N / 32));
nat_xq_ = b.take<uint8_t>(T * (N / 32) * 36);
hit_scratch_ = b.take<uint8_t>(std::max<uint64_t>(
strata::kernels::moe_hit_grouped_scratch_bytes((int64_t) (T * K), g.n_embd, g.n_ff),
strata::kernels::native_expert_scratch_bytes((int64_t) (T * K), g.n_ff)));
head_mixed_ = b.take<float>(T * N); head_inj_ = b.take<float>(HC);
sh_gate_ = b.take<float>(T * (uint64_t) g.n_ff);
sh_up_ = b.take<float>(T * (uint64_t) g.n_ff); sh_g_ = b.take<float>(T + 4);
head_logits_ = b.take<float>(T * (uint64_t) n_vocab_);
arg_scratch_ = b.take<uint8_t>(strata::kernels::argmax_rows_scratch_bytes((int) T));
hist_snap_ = b.take<float>(T * HS);
const uint64_t PD = (uint64_t) strata::kernels::NG_HC_DIM;
ple_key_ = b.take<float>(T * PD); ple_val_ = b.take<float>(T * N); ple_nkey_ = b.take<float>(T * PD);
ple_norm_ = b.take<float>(T * PD); ple_gated_ = b.take<float>(T * PD); ple_gate_ = b.take<float>(T * HC);
};
Bump count;
carve(count);
if (cudaMalloc(&arena_, count.used) != cudaSuccess) {
err = "verify: the device arena (" + std::to_string(count.used >> 20) + " MiB) does not fit";
return false;
}
cudaMemset(arena_, 0, count.used);
Bump real;
real.base = (uint8_t*) arena_;
carve(real);
sink_.staging = (unsigned long long) staging_;
sink_.staging_cap = kStagingBlobs;
(void) TS;
{ // the gates' running averages start at a gate's middle
const std::vector<float> half((size_t) (g.n_layers * g.hc * g.n_embd), 0.5f);
if (cudaMemcpy(gate_ema_, half.data(), half.size() * sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess) {
err = "verify: the gates' averages could not be set";
return false;
}
}
{
std::vector<unsigned long long> sp((size_t) hits.slots);
for (int64_t i = 0; i < hits.slots; ++i)
sp[(size_t) i] = (unsigned long long) (hits.cache_base + (hits.slot_off != nullptr ? hits.slot_off[i]
: (uint64_t) i * (uint64_t) hits.blob));
if (cudaMemcpy(slot_ptr_, sp.data(), sp.size() * sizeof(unsigned long long), cudaMemcpyHostToDevice) != cudaSuccess) {
err = "verify: the slot table could not be uploaded";
return false;
}
}
if (cudaStreamCreateWithFlags(&copy_, cudaStreamNonBlocking) != cudaSuccess) {
err = "verify: copy stream create failed";
return false;
}
if (cudaStreamCreateWithFlags(&cs_, cudaStreamNonBlocking) != cudaSuccess ||
cudaStreamCreateWithFlags(&side_, cudaStreamNonBlocking) != cudaSuccess ||
cudaStreamCreateWithFlags(&shs_, cudaStreamNonBlocking) != cudaSuccess ||
cudaStreamCreateWithFlags(&g2s_, cudaStreamNonBlocking) != cudaSuccess ||
cudaStreamCreateWithFlags(&cps_, cudaStreamNonBlocking) != cudaSuccess ||
cudaStreamCreateWithFlags(&kst_, cudaStreamNonBlocking) != cudaSuccess ||
cudaEventCreateWithFlags(&kjoin_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&iproj_, cudaEventDisableTiming) != cudaSuccess ||
cudaStreamCreateWithFlags(&sgs_, cudaStreamNonBlocking) != cudaSuccess ||
cudaEventCreateWithFlags(&sgjoin_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&cpfork_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&cpjoin_[0], cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&cpjoin_[1], cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&efork_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&rdone_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&g2fork_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&g2join_[0], cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&g2join_[1], cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&join_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&bfork_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&bjoin_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&pfork_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&pjoin_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&shfork_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&shjoin_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&res_ready_, cudaEventDisableTiming) != cudaSuccess ||
cudaEventCreateWithFlags(&done_, cudaEventDisableTiming) != cudaSuccess) {
err = "verify: stream create failed";
return false;
}
std::fprintf(stderr, "strata verify: window up to %d tokens, %.1f MiB of device buffers\n", max_t,
(double) count.used / 1048576.0);
return true;
}
const float* Verifier::final_R(int t) const { return R_ + (size_t) t * (size_t) (g_->hc * g_->n_embd); }
// ================================ THE WINDOW, AS CAPTURED ================================
//
// Plan v0.3 P6 (split window): with `groups_ == 2` the window's tokens are cut into two groups A = [0, T/2 up) and
// B = the rest, and the stream is ordered
//
// pre(0,A) pre(0,B) | post(0,A) pre(1,A) | post(0,B) pre(1,B) | post(1,A) pre(2,A) | ...
//
// so the CPU computes A's experts of layer l while the GPU runs B's mixer and router of layer l, and B's experts
// while the GPU combines A and runs A's layer l+1. B's mixer only needs A's mixer of the same layer (K/V, GDN
// state), never A's experts, so nothing waits that did not wait before. Every token's arithmetic is unchanged.
bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
using namespace strata::kernels;
const ModelGeometry& g = *g_;
const WeightTable& wt = *wt_;
SessionState& ss = *ss_;
const int64_t N = g.n_embd, HC = g.hc, K = ss.k, C = g.ssm_conv_channels, ZV = g.ssm_value_dim;
const int64_t HV = g.ssm_v_heads, HK = g.ssm_k_heads, NH = g.n_head, HD = g.head_dim, NKV = g.n_head_kv;
const int64_t IQ = g.idx_q_heads, ID = g.idx_key_dim, NE = g.n_expert, MT = max_t_;
const QsaShapes s = shapes_of(g);
const GrShapes gs{g.n_embd, g.hc, g.hc_lr};
const uint64_t gdn_floats = (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size +
(uint64_t) g.ssm_conv_channels * (g.ssm_d_conv - 1);
const int64_t HS = (int64_t) NG_HIST * NG_HC_DIM;
const int64_t TS = (s.idx_block - 1) * ID;
const bool ple_on = ss.ple.ready();
auto Rt = [&](int t) { return R_ + (size_t) t * HC * N; };
const int G = (split_ && T >= 2) ? 2 : 1;
const int tb_[2] = {0, (T + 1) / 2}, te_[2] = {G == 2 ? (T + 1) / 2 : T, T};
groups_[T] = G;
// --window-profile: row l < n_layers holds layer l's stages, row n_layers the window's own stamps
auto stamp = [&](int64_t row, int i, cudaStream_t st) {
if (stamps_ != nullptr && G == 1)
gpu_stamp(stamps_ + ((size_t) T * (size_t) (g.n_layers + 1) + (size_t) row) * kStamps + i, st);
};
stamp(g.n_layers, 0, cs);
if (gpu2_ && !quant_router_) {
err = "verify: the second GPU takes the native router's q8_1 rows (a native pack)";
return false;
}
// the layer's short branches: `fork` starts stream `to` from the main stream's work so far, `mark` records what
// `from` has queued, `wait` makes the main stream wait for a mark
auto fork = [&](cudaStream_t to, cudaEvent_t ev) {
if (cudaEventRecord(ev, cs) == cudaSuccess && cudaStreamWaitEvent(to, ev, 0) == cudaSuccess) return true;
err = "verify: a branch could not fork";
return false;
};
auto mark = [&](cudaStream_t from, cudaEvent_t ev) {
if (cudaEventRecord(ev, from) == cudaSuccess) return true;
err = "verify: a branch could not join";
return false;
};
auto wait = [&](cudaEvent_t ev) {
if (cudaStreamWaitEvent(cs, ev, 0) == cudaSuccess) return true;
err = "verify: a branch could not join";
return false;
};
// ---- the window's inputs, from mapped staging; the residency snapshot on the shared expert's branch (the first
// hit plan waits for it)
copy_i32_from_mapped(tok_, m_tok_, T, cs);
copy_i32_from_mapped(step_, m_step_, (int64_t) T * kStepCount, cs);
copy_i32_from_mapped(pos_, m_pos_, (int64_t) T * NH, cs);
if (cudaEventRecord(shfork_, cs) != cudaSuccess || cudaStreamWaitEvent(shs_, shfork_, 0) != cudaSuccess) {
err = "verify: the residency copy could not fork";
return false;
}
copy_from_mapped((float*) res_, (const float*) m_res_, res_words_, shs_); // int32 words, copied as they are
if (cudaEventRecord(res_ready_, shs_) != cudaSuccess) {
err = "verify: the residency copy could not join";
return false;
}
// ---- the embeddings, broadcast to the hc streams
if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table
ne->gather_dev(tok_, T, emb_, cs);
broadcast_streams(emb_, R_, N, (int) HC, T, cs);
} else {
const WeightRef* w = wt.find("token_embd.weight");
if (w == nullptr || w->codebook_iq4nl || (w->code_bits != 2 && w->code_bits != 4 && w->code_bits != 8)) {
err = "verify: token_embd.weight is missing or not an S2/S4/S8 tensor";
return false;
}
const auto* codes = (const uint8_t*) w->data;
const auto* scales = (const float*) (codes + w->codes_bytes);
const auto* offsets = w->has_offset ? (const float*) (codes + w->codes_bytes + w->scales_bytes) : nullptr;
const uint64_t row_codes = (uint64_t) (w->ne0 / (8 / w->code_bits));
const uint64_t row_groups = (uint64_t) (w->ne0 / w->group_elems);
embedding_gather_dev(codes, scales, offsets, tok_, T, w->ne0, w->code_bits, w->code_bias, w->group_elems,
row_codes, row_groups, emb_, cs);
broadcast_streams(emb_, R_, N, (int) HC, T, cs);
}
// set_predict: this layer's FFN read also estimates the next layer's FFN input (that layer's read of this state,
// its gates their running average over the windows), and on a side branch forked after the read the next layer's
// router runs on it with that layer's logit bias. When learning, the FFN read of a layer the previous one
// predicts keeps its gates' average, and the layer corrects its bias by its own logits on that branch.
auto predicts = [&](int64_t l) {
return predict_ != nullptr && G == 1 && l + 1 < g.n_layers && native_router_enabled();
};
auto corrects = [&](int64_t l) { return learn_ && l > 0 && predicts(l - 1); };
auto branches = [&](int64_t l) { return predicts(l) || corrects(l); };
// per-layer state indices (GDN and QSA layers are numbered separately)
std::vector<int64_t> gdn_idx((size_t) g.n_layers, -1), qsa_idx((size_t) g.n_layers, -1);
{
int64_t qi = 0, gi = 0;
for (int64_t l = 0; l < g.n_layers; ++l) {
if (is_qsa_layer(g, l)) qsa_idx[(size_t) l] = qi++;
else gdn_idx[(size_t) l] = gi++;
}
}
// the shared expert: in a window of one token group on a branch of its own forked after the FFN read (`efork_`),
// beside the router and the routed experts (joined before the combine); in a split window inline. It ends with
// its down projection: the combine applies its scalar gate, whose logits come from a branch of their own.
const bool beside = G == 1;
auto shared = [&](int64_t l, int grp) -> bool {
const int tb = tb_[grp], te = te_[grp], n = te - tb;
const LayerView v(wt, l);
const WeightRef *wgi = need(v, "ffn_gate_inp_shexp.weight", err), *wsg = need(v, "ffn_gate_shexp.weight", err),
*wsu = need(v, "ffn_up_shexp.weight", err), *wsd = need(v, "ffn_down_shexp.weight", err);
if (!wgi || !wsg || !wsu || !wsd) return false;
if (!native_of(wsg, v.name("ffn_gate_shexp.weight"), err) || !native_of(wsu, v.name("ffn_up_shexp.weight"), err) ||
!native_of(wsd, v.name("ffn_down_shexp.weight"), err))
return false;
NativeSharedWeights nsw;
nsw.gate_type = wsg->native_type; nsw.gate_data = wsg->native_data;
nsw.up_type = wsu->native_type; nsw.up_data = wsu->native_data;
nsw.down_type = wsd->native_type; nsw.down_data = wsd->native_data;
nsw.q8_1 = xq_; // the main stream's next use is the next layer's mixer
nsw.q8_1_il = xil_;
cudaStream_t ws = cs, gs = cs;
if (beside) {
if (cudaStreamWaitEvent(shs_, efork_, 0) != cudaSuccess ||
cudaStreamWaitEvent(sgs_, efork_, 0) != cudaSuccess) {
err = "verify: the shared expert's branch could not fork";
return false;
}
ws = shs_;
gs = sgs_;
}
try {
shared_expert_multi(n, mixed_ + tb * N, nsw, (const uint16_t*) wgi->data, sh_gate_ + (size_t) tb * g.n_ff,
sh_up_ + (size_t) tb * g.n_ff, sh_g_ + tb, shared_ + tb * N, N, g.n_ff, ws, gs);
} catch (const std::exception& e) {
err = std::string("verify shared expert: ") + e.what();
return false;
}
if (beside) {
stamp(l, kLayerStamps, shs_);
if (cudaEventRecord(shjoin_, shs_) != cudaSuccess || cudaEventRecord(sgjoin_, sgs_) != cudaSuccess) {
err = "verify: the shared expert's branch could not join";
return false;
}
}
return true;
};
// ---------------------------------------------------------------- pre(l, group): up to the ring
auto pre = [&](int64_t l, int grp) -> bool {
const int tb = tb_[grp], te = te_[grp], n = te - tb;
stamp(l, 0, cs);
const LayerView v(wt, l);
const char* pfx[2] = {"hc_attn_", "hc_ffn_"};
const WeightRef *wn[2], *wd[2], *wu[2], *wi[2];
for (int h = 0; h < 2; ++h) {
wn[h] = need(v, (std::string(pfx[h]) + "norm.weight").c_str(), err);
wd[h] = need(v, (std::string(pfx[h]) + "down.weight").c_str(), err);
wu[h] = need(v, (std::string(pfx[h]) + "up.weight").c_str(), err);
wi[h] = need(v, (std::string(pfx[h]) + "inject.weight").c_str(), err);
if (!wn[h] || !wd[h] || !wu[h] || !wi[h]) return false;
}
bool pending = l > 0; // the previous layer's FFN write, folded into this layer's first read
// the projections of the group's tokens: 2+ tokens are quantized with the interleaved copy the multi-token
// kernels read
auto quant = [&](const float* x, int64_t n_in) {
if (n >= 2) native_quantize_q8_1_il(x, xq_, xil_, (int) n_in, n, cs);
else native_quantize_q8_1(x, xq_, (int) n_in, n, cs);
};
auto mm = [&](int type, const void* w, float* y, int64_t n_in, int64_t n_out, cudaStream_t ps) {
native_mmvq_il(type, w, xq_, xil_, y, (int) n_in, (int) n_out, n, ps);
};
auto proj = [&](const WeightRef* w, float* y, int64_t n_in, int64_t n_out, cudaStream_t ps) {
mm(w->native_type, w->native_data, y, n_in, n_out, ps);
};
if (l == 1 && ple_on) {
if (grp == 0) { // the host reads the rows while layer 0 runs
wait_flag_ge(m_pleflag_, 1, cs);
copy_from_mapped(ple_, m_ple_, (int64_t) T * N, cs);
}
for (int t = tb; t < te; ++t) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);
// the PLE block: the group's projections with each weight read once, then the rest of the block over its
// tokens in order, which leaves the history after each of them for the commit
const PleWeights& pw = ss.ple.w;
const float* emb = ple_ + tb * N;
float* key = ple_key_ + (size_t) tb * NG_HC_DIM;
float* norm = ple_norm_ + (size_t) tb * NG_HC_DIM;
try {
if (pw.key_bf16 != nullptr) {
bf16_gemv_fp32_mmvf_multi(emb, pw.key_bf16, key, N, NG_HC_DIM, n, cs);
} else {
quant(emb, N);
mm(pw.key_native_type, pw.key_native_data, key, N, NG_HC_DIM, cs);
}
bf16_gemv_fp32_mmvf_multi(emb, pw.value_bf16, ple_val_ + tb * N, N, N, n, cs);
const NativePleTokensBuffers pb{ple_nkey_ + (size_t) tb * NG_HC_DIM, norm, ple_gate_ + tb * HC,
ple_gated_ + (size_t) tb * NG_HC_DIM, norm, Rt(tb)};
native_ple_postops_tokens(key, Rt(tb), ple_val_ + tb * N, ss.ple.hist, pw, pb, n, cs,
hist_snap_ + (size_t) tb * HS);
} catch (const std::exception& e) {
err = std::string("verify PLE: ") + e.what();
return false;
}
pending = false;
}
auto gr_read_group = [&](int half, bool apply, float* inj_prev, float* inj_out, float* gate_ema,
const float* est_norm, const float* est_gates) {
FusedGrArgs fa[kFusedGrMaxT];
for (int t = tb; t < te; ++t) {
FusedGrArgs& a = fa[t - tb];
a.R = Rt(t); a.R_out = Rt(t); a.apply = apply;
a.bo_prev = bo_ + t * N; a.inj_prev = inj_prev + t * HC;
a.w_norm = (const float*) wn[half]->data; a.w_down = (const uint16_t*) wd[half]->data;
a.w_up = (const uint16_t*) wu[half]->data; a.w_inject = (const uint16_t*) wi[half]->data;
a.eps = EPS; a.lo = lo_ + t * g.hc_lr; a.rs = rs_ + t * HC;
a.inject_out = inj_out + t * HC; a.mixed = mixed_ + t * N; a.gate_ema = gate_ema;
a.est_norm = est_norm; a.est_gates = est_gates; a.est = pmixed_ + t * N;
}
fused_gr_read_multi(fa, n, grs_, cs);
};
gr_read_group(0, pending, inj2_, inj_, nullptr, nullptr, nullptr);
stamp(l, 1, cs);
float* xm = mixed_ + tb * N;
try {
if (!is_qsa_layer(g, l)) {
// ======================= GDN =======================
const WeightRef *wqkv = need(v, "attn_qkv.weight", err), *wg = need(v, "attn_gate.weight", err),
*wout = need(v, "ssm_out.weight", err), *wa = need(v, "ssm_alpha.weight", err),
*wb = need(v, "ssm_beta.weight", err), *wc = need(v, "ssm_conv1d.weight", err),
*wnm = need(v, "ssm_norm.weight", err), *wdt = need(v, "ssm_dt.bias", err),
*wsa = need(v, "ssm_a", err);
if (!wqkv || !wg || !wout || !wa || !wb || !wc || !wnm || !wdt || !wsa) return false;
if (!native_of(wqkv, v.name("attn_qkv.weight"), err) || !native_of(wg, v.name("attn_gate.weight"), err) ||
!native_of(wout, v.name("ssm_out.weight"), err))
return false;
const int64_t gi = gdn_idx[(size_t) l];
float* state = ss.gdn_state + (size_t) gi * gdn_floats;
float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
float* qkv = qkv_L_ + (size_t) gi * MT * C;
float* hb = h_L_ + (size_t) gi * MT * C;
float* gate = gate_L_ + (size_t) gi * MT * HV;
float* beta = beta_L_ + (size_t) gi * MT * HV;
// beside qkv and the conv: alpha/beta (a few blocks, latency-bound) and the gate projection, joined
// before the recurrence. qkv's grid is recorded first, so it is dispatched first and the conv runs
// beside the gate projection's last blocks.
if (!fork(side_, bfork_)) return false;
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
(const float*) wsa->data, gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) N, (int) HV,
n, side_);
if (!mark(side_, bjoin_)) return false;
quant(xm, N);
if (!fork(shs_, pfork_)) return false;
proj(wqkv, qkv + (size_t) tb * C, N, C, cs);
proj(wg, z_ + (size_t) tb * ZV, N, ZV, shs_);
if (!mark(shs_, pjoin_)) return false;
gdn_conv_l2_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, cs, tb);
if (!wait(bjoin_) || !wait(pjoin_)) return false;
// the recurrence from the untouched state over tokens [0, te); outputs only for this group's
gdn_step_norm_multi(state, hb, (int) C, gate, beta, z_, (const float*) wnm->data, EPS, y_, (int) HK,
(int) HV, te, nullptr, cs, tb);
quant(y_ + (size_t) tb * ZV, ZV);
proj(wout, bo_ + tb * N, ZV, N, cs);
} else {
// ======================= QSA =======================
const int64_t qi = qsa_idx[(size_t) l];
const QsaState& st = ss.qsa_states[qi];
const WeightRef *wik = need(v, "indexer.k_proj.weight", err), *wq = need(v, "attn_q.weight", err),
*wk = need(v, "attn_k.weight", err), *wv = need(v, "attn_v.weight", err),
*wo = need(v, "attn_output.weight", err), *wiq = need(v, "indexer.q_proj.weight", err),
*wqn = need(v, "attn_q_norm.weight", err), *wkn = need(v, "attn_k_norm.weight", err),
*wiqn = need(v, "indexer.q_norm.weight", err), *wikn = need(v, "indexer.k_norm.weight", err);
if (!wik || !wq || !wk || !wv || !wo || !wiq || !wqn || !wkn || !wiqn || !wikn) return false;
if (!native_of(wq, v.name("attn_q.weight"), err) || !native_of(wk, v.name("attn_k.weight"), err) ||
!native_of(wv, v.name("attn_v.weight"), err) || !native_of(wo, v.name("attn_output.weight"), err))
return false;
// the rms norm and rope of the group's heads in one launch: rows of `cols` read at in + r * stride,
// token t's positions at pos_ + t * NH
auto norm_rope = [&](const float* in, int stride, float* out, const WeightRef* norm, int heads,
int cols, cudaStream_t ns) {
native_qsa_norm_rope_tokens(in, stride, (const float*) norm->data, out, cols, n * heads, EPS,
(int) s.n_rot, (float) qsa_freq_base(), pos_ + tb * NH, heads, (int) NH,
ns);
};
// three branches beside the values, joined before the attention: the indexer (its key and query
// projections in one launch, the keys appended, the queries' block scores and top-k), the keys and the
// queries. A grid is dispatched whole before a later kernel's blocks, so the keys' and values'
// projections go ahead of the queries' grid, and that grid waits for the indexer's projections: the
// append is dispatched ahead of it, and the indexer's last kernels end about when the queries' do.
if (!fork(side_, bfork_)) return false;
float* idx_raw = idx_raw_L_ + (size_t) qi * MT * ID;
bf16_gemv_fp32_mmvf_multi(xm, (const uint16_t*) wik->data, idx_raw + tb * ID, N, ID, n, side_,
(const uint16_t*) wiq->data, qidx_ + tb * IQ * ID, IQ * ID);
if (!mark(side_, iproj_)) return false;
const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos};
native_qsa_indexer_append_multi(idx_raw + tb * ID, step_ + tb * kStepCount + kStepPos, (int) kStepCount, n,
0, (const float*) wikn->data, EPS, ib, s, st.max_cells,
(float) qsa_freq_base(), side_,
grp == 0 ? tail_snap_ + (size_t) qi * TS : nullptr);
norm_rope(qidx_ + tb * IQ * ID, (int) ID, qidx_ + tb * IQ * ID, wiqn, (int) IQ, (int) ID, side_);
qsa_block_scores(st.idx_pooled, st.idx_dead, qidx_ + tb * IQ * ID, step_ + tb * kStepCount, n, max_blocks_,
s, scores_ + (size_t) tb * max_blocks_, side_, qsa_score_grid_blocks);
qsa_block_topk(scores_ + (size_t) tb * max_blocks_, step_ + tb * kStepCount, n, max_blocks_, cap_, s,
sel_ + (size_t) tb * cap_, side_);
if (!mark(side_, bjoin_)) return false;
quant(xm, N);
if (cudaEventRecord(pfork_, cs) != cudaSuccess || cudaStreamWaitEvent(kst_, pfork_, 0) != cudaSuccess ||
cudaStreamWaitEvent(shs_, pfork_, 0) != cudaSuccess ||
cudaStreamWaitEvent(shs_, iproj_, 0) != cudaSuccess) {
err = "verify: a branch could not fork";
return false;
}
proj(wk, kcur_ + tb * NKV * HD, N, NKV * HD, kst_);
norm_rope(kcur_ + tb * NKV * HD, (int) HD, kcur_ + tb * NKV * HD, wkn, (int) NKV, (int) HD, kst_);
if (!mark(kst_, kjoin_)) return false;
proj(wv, vcur_ + tb * NKV * HD, N, NKV * HD, cs);
proj(wq, qfull_ + tb * NH * 2 * HD, N, NH * 2 * HD, shs_);
// the query halves of every token's heads (rows of 2 * HD: query, then gate)
norm_rope(qfull_ + tb * NH * 2 * HD, (int) (2 * HD), qcur_ + tb * NH * HD, wqn, (int) NH, (int) HD,
shs_);
if (!mark(shs_, pjoin_) || !wait(kjoin_)) return false;
if (st.kv_int8)
kv_append_q8_steps(st.k_q, st.v_q, st.k_scale, st.v_scale, st.page_table, step_ + tb * kStepCount,
(int) kStepCount, kcur_ + tb * NKV * HD, vcur_ + tb * NKV * HD, n, s, cs);
else
for (int t = tb; t < te; ++t)
kv_append_step(st.k_pool, st.v_pool, st.page_table, step_ + t * kStepCount, kcur_ + t * NKV * HD,
vcur_ + t * NKV * HD, s, cs);
if (!wait(bjoin_) || !wait(pjoin_)) return false;
QsaAttnPools pools;
pools.page_table = st.page_table;
if (st.kv_int8) { pools.k_q = st.k_q; pools.v_q = st.v_q; pools.k_scale = st.k_scale; pools.v_scale = st.v_scale; }
else { pools.k_pool = st.k_pool; pools.v_pool = st.v_pool; }
qsa_decode_attn_batch(qcur_ + tb * NH * HD, pools, sel_ + (size_t) tb * cap_, step_ + tb * kStepCount, cap_,
s, attn_scratch_ + (size_t) tb * attn_scratch_floats_, attn32_ + tb * NH * HD,
n, cs, qfull_ + tb * NH * 2 * HD);
quant(attn32_ + tb * NH * HD, NH * HD);
proj(wo, bo_ + tb * N, NH * HD, N, cs);
}
} catch (const std::exception& e) {
err = "verify layer " + std::to_string(l) + ": " + e.what();
return false;
}
stamp(l, 2, cs);
// the FFN read, and the next layer's estimated FFN input for the prediction (set_predict)
const WeightRef* wn1 = predicts(l) ? need(LayerView(wt, l + 1), "hc_ffn_norm.weight", err) : nullptr;
if (predicts(l) && !wn1) return false;
gr_read_group(1, true, inj_, inj2_, corrects(l) ? gate_ema_ + (size_t) l * HC * N : nullptr,
wn1 != nullptr ? (const float*) wn1->data : nullptr,
wn1 != nullptr ? gate_ema_ + (size_t) (l + 1) * HC * N : nullptr);
stamp(l, 3, cs);
// the routed experts' activations: from the router kernel, else beside it (joined before the experts)
if (!quant_router_) {
if (!fork(side_, bfork_)) return false;
if (strata::kernels::cpu::expert_layout().native)
quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, side_);
else
quantize_q8_0_scaled(xm, hit_xq_ + (size_t) tb * (N / 32) * 34, hit_xs_ + (size_t) tb * (N / 32),
(int64_t) n * N, side_);
if (!mark(side_, bjoin_)) return false;
}
// the branches that need only the FFN read fork here; they are recorded after the router, whose blocks are
// then dispatched first, and run beside its routing, the ring and the hit plan
if (beside && cudaEventRecord(efork_, cs) != cudaSuccess) {
err = "verify: the FFN read's branches could not fork";
return false;
}
// the router, the doorbell and the main GPU's routed experts - the ones its VRAM tier holds, decided here from
// the residency snapshot the pool reads too (it leaves their rows to the GPU)
if (l == 0 && grp == 0 && cudaStreamWaitEvent(cs, res_ready_, 0) != cudaSuccess) {
err = "verify: the residency copy could not join";
return false;
}
int32_t* hit_plan = dplan_ + (size_t) grp * (size_t) plan_stride_;
if (native_router_enabled()) { // one kernel: logits, top 10, ring, hit plan
const WeightRef* wr = need(v, "ffn_gate_inp.weight", err);
if (!wr) return false;
VerifyRouterArgs ra;
ra.x = xm; ra.w = (const uint16_t*) wr->data; ra.logits = logits_ + tb * NE;
ra.ids = ids_ + tb * K; ra.weights = w_ + tb * K;
ra.x_out = xk_layer_[(size_t) l] ? nullptr : m_x_ + tb * N;
ra.ids_out = m_ids_ + tb * K; ra.w_out = m_w_ + tb * K; ra.seq = m_seq_;
if (quant_router_) { // the q8_1 rows for both GPUs' experts, the Q8_K rows for the CPU's
ra.xq1 = nat_xq_ + (size_t) tb * (N / 32) * 36;
ra.xq1_out = gpu2_ ? m_xq1_ + (size_t) tb * (N / 32) * 36 : nullptr;
ra.xk_out = xk_layer_[(size_t) l] ? m_xk_ + (size_t) tb * (size_t) xk_stride_ : nullptr;
ra.xk_stride = (int) xk_stride_;
}
ra.ring = (uint32_t) (l * G + grp + 1); // the rings so far (the host waits for this one)
ra.res = res_ + (size_t) l * NE; ra.slot_ptr = slot_ptr_; ra.plan = hit_plan;
ra.cap = (int) (MT * K); ra.ptr_off = (int) plan_ptr_off_;
ra.counter = ring_count_; ra.n_tok = n; ra.n_embd = (int) N; ra.n_expert = (int) NE;
try {
verify_router(ra, cs);
} catch (const std::exception& e) {
err = "verify layer " + std::to_string(l) + " router: " + e.what();
return false;
}
} else {
for (int t = tb; t < te; ++t) {
MoEBuffers mb = ss.moe;
mb.logits = logits_ + t * NE; mb.ids = ids_ + t * K; mb.weights = w_ + t * K;
if (!moe_route(wt, g, l, K, mb, mixed_ + t * N, cs, err, nullptr)) return false;
}
doorbell_publish(xm, ids_ + tb * K, w_ + tb * K, (int64_t) n * N, (int64_t) n * K, m_x_ + tb * N,
m_ids_ + tb * K, m_w_ + tb * K, m_seq_, cs);
verify_hit_plan(ids_ + tb * K, (int) (n * K), (int) K, res_ + (size_t) l * NE, (int) NE, slot_ptr_, hit_plan,
MT * K, plan_ptr_off_, cs);
}
stamp(l, 4, cs);
// after the doorbell: the second GPU's rows of this token group, taken into VRAM once their flag rises, and
// the wait for the CPU's rows, each on a branch beside the routed experts; then the branches forked after the
// FFN read: the shared expert, and the next layer's prediction
if (gpu2_) {
if (!fork(g2s_, g2fork_)) return false;
wait_flag_ge(m_flag2_ + (size_t) grp * 16, (uint32_t) (l * G + grp + 1), g2s_);
fetch_listed_rows(m_list2_ + grp * list2_stride_, m_ymiss_ + (size_t) tb * K * N,
hit_out_ + (size_t) tb * K * N, list2_ + grp * list2_stride_, (int) (n * K), N, g2s_);
stamp(l, kLayerStamps + 1, g2s_);
if (!mark(g2s_, g2join_[grp])) return false;
}
if (!fork(cps_, cpfork_)) return false;
wait_flag_ge(m_flag_, (uint32_t) (l * G + grp + 1), cps_); // the CPU's share is in the mapped rows
if (!mark(cps_, cpjoin_[grp])) return false;
if (beside && cudaEventRecord(rdone_, cs) != cudaSuccess) {
err = "verify: the router's end could not be marked";
return false;
}
if (beside && !shared(l, grp)) return false;
if (branches(l) && cudaStreamWaitEvent(side_, efork_, 0) != cudaSuccess) {
err = "verify: the prediction branch could not fork";
return false;
}
if (predicts(l)) {
const WeightRef* wr1 = need(LayerView(wt, l + 1), "ffn_gate_inp.weight", err);
if (!wr1) return false;
const size_t half = (size_t) ((l + 1) & 1) * (size_t) (MT * K);
VerifyRouterArgs ra;
ra.x = pmixed_; ra.w = (const uint16_t*) wr1->data;
ra.logits = plogits_ + (size_t) ((l + 1) & 1) * MT * NE; ra.ids = pids_; ra.weights = pw_;
ra.ids_out = m_pids_ + half; ra.w_out = m_pw_ + half; ra.seq = m_pseq_;
ra.ring = (uint32_t) (l + 1); // the predictions published so far
ra.counter = ring_count_ + 1; ra.n_tok = n; ra.n_embd = (int) N; ra.n_expert = (int) NE;
if (learn_) ra.bias = pbias_ + (size_t) (l + 1) * NE;
try {
verify_router(ra, side_);
} catch (const std::exception& e) {
err = "verify layer " + std::to_string(l) + " prediction: " + e.what();
return false;
}
}
if (corrects(l)) { // after the prediction and the router, from the prediction the previous layer made
if (cudaStreamWaitEvent(side_, rdone_, 0) != cudaSuccess) {
err = "verify: the prediction's bias could not wait for the router";
return false;
}
try {
verify_router_bias_update(logits_ + tb * NE, plogits_ + (size_t) (l & 1) * MT * NE, n,
pbias_ + (size_t) l * NE, side_);
} catch (const std::exception& e) {
err = "verify layer " + std::to_string(l) + " prediction bias: " + e.what();
return false;
}
}
if (branches(l) && cudaEventRecord(join_, side_) != cudaSuccess) {
err = "verify: the prediction branch could not join";
return false;
}
if (!quant_router_ && !wait(bjoin_)) return false;
stamp(l, 5, cs);
return beside || shared(l, grp);
};
// ---------------------------------------------------------------- post(l, group): experts, combine
auto post = [&](int64_t l, int grp) -> bool {
const int tb = tb_[grp], te = te_[grp], n = te - tb;
const uint32_t ring = (uint32_t) (l * G + grp + 1);
const int64_t cap = (int64_t) n * K, capx = (int64_t) max_t_ * K;
struct Plan { // a plan block's fields (`init`)
const int32_t *counts = nullptr, *start = nullptr, *dst = nullptr, *tok = nullptr, *start2 = nullptr;
const unsigned long long *ptr = nullptr, *ptr2 = nullptr;
};
auto plan_at = [&](const int32_t* pl) {
Plan p;
p.counts = pl;
p.start = pl + 4;
p.dst = p.start + capx + 1;
p.tok = p.dst + capx;
p.ptr = (const unsigned long long*) (pl + plan_ptr_off_);
p.ptr2 = p.ptr + capx;
p.start2 = pl + plan_ptr_off_ + 4 * capx;
return p;
};
float* hit_out = hit_out_ + (size_t) tb * K * N;
const auto& lay = strata::kernels::cpu::expert_layout();
auto grouped = [&](const Plan& p, const unsigned long long* gp, const int32_t* gs, const int32_t* gn) {
if (lay.native) {
// the layer's GGUF formats (i-quant gate/up, Q2_0 / IQ4_NL down)
const auto& f = lay.fmt[(size_t) l];
const NativeExpertLayout L = native_expert_layout(f.gu_type, f.d_type, f.n_embd, f.n_ff);
native_expert_grouped(L, gp, gs, gn, p.dst, p.tok, cap, cap,
nat_xq_ + (size_t) tb * (N / 32) * 36, hit_scratch_, hit_out, cs);
} else {
moe_grouped_s2(gp, gs, gn, p.dst, p.tok, cap, cap, hit_xq_ + (size_t) tb * (N / 32) * 34,
hit_xs_ + (size_t) tb * (N / 32), hit_scratch_, hit_out, cs);
}
};
// plan v0.3 P6: the VRAM groups at once; the PCIe groups once the pool has published them and the copy engine
// has landed them in staging
const Plan vram = plan_at(dplan_ + (size_t) grp * (size_t) plan_stride_);
grouped(vram, vram.ptr, vram.start, vram.counts);
stamp(l, 6, cs);
Plan pcie;
if (pcie_share_) {
wait_flag_ge(m_flagA_, ring, cs); // the pool published this group's PCIe share
stamp(l, 7, cs);
int32_t* pl = plan_ + (size_t) grp * (size_t) plan_stride_;
copy_i32_from_mapped(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
pcie = plan_at(pl);
wait_flag_ge(m_flagB_, ring, cs); // it is in staging (DMA) or mapped
if (sink_.pcie_mode == 2) { // stage it with a copy kernel, then point at staging
const int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;
uint8_t* stage = staging_ + (size_t) (grp * per) * lay.max_blob;
fetch_blobs(pcie.ptr2, pcie.counts + 2, stage, (int64_t) lay.blob_bytes(l), (int) per, cs);
rebase_ptrs((unsigned long long*) pcie.ptr2, pcie.counts + 2, stage, (int64_t) lay.blob_bytes(l), cs);
}
grouped(pcie, pcie.ptr2, pcie.start2, pcie.counts + 2);
} else {
stamp(l, 7, cs);
}
stamp(l, 8, cs);
if (!wait(cpjoin_[grp])) return false; // the CPU's share is in the mapped rows
stamp(l, 9, cs);
if (gpu2_ && !wait(g2join_[grp])) return false; // the second GPU's, in `hit_out`
stamp(l, 10, cs);
if (beside && (cudaStreamWaitEvent(cs, shjoin_, 0) != cudaSuccess ||
cudaStreamWaitEvent(cs, sgjoin_, 0) != cudaSuccess)) {
err = "verify: the shared expert's branch could not join";
return false;
}
// only the CPU's rows cross PCIe here; the GPUs' come from `hit_out` (the pool leaves their rows unwritten)
const int32_t* list2 = gpu2_ ? list2_ + grp * list2_stride_ : nullptr;
try {
native_moe_gather_combine(hit_out, m_ymiss_ + (size_t) tb * K * N, vram.dst, vram.counts + 1, pcie.dst,
pcie.counts != nullptr ? pcie.counts + 1 : nullptr,
list2 != nullptr ? list2 + 4 : nullptr, list2, w_ + tb * K, shared_ + tb * N,
sh_g_ + tb, bo_ + tb * N, N, K, n, cs);
} catch (const std::exception& e) {
err = "verify layer " + std::to_string(l) + " combine: " + e.what();
return false;
}
stamp(l, 11, cs);
if (branches(l) && cudaStreamWaitEvent(cs, join_, 0) != cudaSuccess) {
err = "verify: the prediction branch could not join";
return false;
}
if (l == g.n_layers - 1)
for (int t = tb; t < te; ++t) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);
stamp(l, 12, cs);
return true;
};
stamp(g.n_layers, 1, cs);
for (int grp = 0; grp < G; ++grp)
if (!pre(0, grp)) return false;
for (int64_t l = 0; l < g.n_layers; ++l)
for (int grp = 0; grp < G; ++grp) {
if (!post(l, grp)) return false;
if (l + 1 < g.n_layers && !pre(l + 1, grp)) return false;
}
stamp(g.n_layers, 2, cs);
// ---- the head, T columns, and the argmax of each
{
const WeightRef *hn = wt.find("output_hc_norm.weight"), *hd = wt.find("output_hc_down.weight"),
*hu = wt.find("output_hc_up.weight");
if (!hn || !hd || !hu) { err = "verify: an output_hc_* weight is missing"; return false; }
for (int t = 0; t < T; ++t) {
BlockBuffers bb = ss.block;
bb.R = Rt(t);
bb.mixed = head_mixed_ + t * N;
if (head_ != nullptr && head_->loaded()) {
if (!lm_head_mix(wt, g, bb, cs, err)) return false;
} else if (!lm_head(wt, g, bb, head_logits_ + (size_t) t * n_vocab_, cs, err)) {
return false;
}
}
if (head_ != nullptr && head_->loaded()) {
try {
if (T >= 2) {
native_quantize_q8_1_il(head_mixed_, xq_, xil_, (int) N, T, cs);
native_mmvq_il(head_->type(), head_->weights(), xq_, xil_, head_logits_, (int) N, (int) n_vocab_, T, cs);
} else {
native_quantize_q8_1(head_mixed_, xq_, (int) N, T, cs);
native_mmvq(head_->type(), head_->weights(), xq_, head_logits_, (int) N, (int) n_vocab_, T, cs);
}
} catch (const std::exception& e) {
err = std::string("verify head: ") + e.what();
return false;
}
}
argmax_rows(head_logits_, T, (int) n_vocab_, arg_scratch_, m_out_, cs);
}
if (host_rows_) copy_from_mapped(m_rows_, R_, (int64_t) T * HC * N, cs); // a plain copy kernel, here to host
stamp(g.n_layers, 3, cs);
stamp(g.n_layers, 4, cs); // right after the last: one stamp's own cost
return true;
}
bool Verifier::capture(int T, std::string& err) {
if (exec_[T] != nullptr) return true;
if (profile_ && stamps_ == nullptr &&
cudaMalloc(&stamps_, (size_t) (strata::kernels::kVerifyMaxT + 1) * (size_t) (g_->n_layers + 1) * kStamps *
sizeof(unsigned long long)) != cudaSuccess) {
err = "verify: the profile's stamp buffer does not fit";
return false;
}
if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) {
err = "verify: begin capture failed";
return false;
}
std::string rerr;
const bool ok = record_window(T, cs_, rerr);
cudaGraph_t graph = nullptr;
const cudaError_t ce = cudaStreamEndCapture(cs_, &graph);
if (!ok) {
if (graph) cudaGraphDestroy(graph);
err = rerr;
return false;
}
if (ce != cudaSuccess) {
err = std::string("verify: end capture: ") + cudaGetErrorString(ce);
return false;
}
const cudaError_t ie = cudaGraphInstantiate(&exec_[T], graph, 0);
cudaGraphDestroy(graph);
if (ie != cudaSuccess) {
err = std::string("verify: instantiate: ") + cudaGetErrorString(ie);
return false;
}
const cudaError_t ue = cudaGraphUpload(exec_[T], cs_);
const cudaError_t us = cudaStreamSynchronize(cs_);
std::fprintf(stderr, "strata verify: captured the %d-token window (upload %s, sync %s)\n", T,
cudaGetErrorString(ue), cudaGetErrorString(us));
return true;
}
bool Verifier::capture_commit(std::string& err) {
if (commit_exec_ != nullptr) return true;
using namespace strata::kernels;
const ModelGeometry& g = *g_;
SessionState& ss = *ss_;
const QsaShapes s = shapes_of(g);
const int64_t C = g.ssm_conv_channels, HV = g.ssm_v_heads, ID = g.idx_key_dim, MT = max_t_;
const uint64_t gdn_floats = (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size +
(uint64_t) g.ssm_conv_channels * (g.ssm_d_conv - 1);
const int64_t TS = (s.idx_block - 1) * ID;
const int64_t HS = (int64_t) NG_HIST * NG_HC_DIM;
if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) {
err = "verify: begin commit capture failed";
return false;
}
bool ok = true;
try {
copy_i32_from_mapped(commit_, m_commit_, 2 + MT, cs_);
int64_t qsa_index = 0, gdn_index = 0;
for (int64_t l = 0; l < g.n_layers && ok; ++l) {
const LayerView v(*wt_, l);
if (!is_qsa_layer(g, l)) {
const WeightRef* wnm = need(v, "ssm_norm.weight", err);
if (!wnm) { ok = false; break; }
float* state = ss.gdn_state + (size_t) gdn_index * gdn_floats;
float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
const float* qkv = qkv_L_ + (size_t) gdn_index * MT * C;
gdn_conv_commit(conv, qkv, (int) C, commit_, cs_);
gdn_step_norm_multi(state, h_L_ + (size_t) gdn_index * MT * C, (int) C, gate_L_ + (size_t) gdn_index * MT * HV,
beta_L_ + (size_t) gdn_index * MT * HV, z_, (const float*) wnm->data, EPS, y_dummy_,
(int) g.ssm_k_heads, (int) HV, (int) MT, commit_, cs_);
++gdn_index;
} else {
const QsaState& st = ss.qsa_states[qsa_index];
const WeightRef* wikn = need(v, "indexer.k_norm.weight", err);
if (!wikn) { ok = false; break; }
copy_from_mapped(st.idx_tail, tail_snap_ + (size_t) qsa_index * TS, TS, cs_);
const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos};
native_qsa_indexer_append_multi(idx_raw_L_ + (size_t) (qsa_index * MT) * ID, commit_ + 2, 1, (int) MT, 0,
(const float*) wikn->data, EPS, ib, s, st.max_cells,
(float) qsa_freq_base(), cs_);
++qsa_index;
}
}
if (ok && ss.ple.ready()) copy_indexed(ss.ple.hist, hist_snap_, HS, commit_ + 1, HS, cs_);
} catch (const std::exception& e) {
err = std::string("verify commit: ") + e.what();
ok = false;
}
cudaGraph_t graph = nullptr;
const cudaError_t ce = cudaStreamEndCapture(cs_, &graph);
if (!ok) {
if (graph) cudaGraphDestroy(graph);
return false;
}
if (ce != cudaSuccess || cudaGraphInstantiate(&commit_exec_, graph, 0) != cudaSuccess) {
if (graph) cudaGraphDestroy(graph);
err = std::string("verify: commit capture: ") + cudaGetErrorString(ce);
return false;
}
cudaGraphDestroy(graph);
return true;
}
bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out,
std::string& err) {
using namespace strata::kernels;
if (T < 1 || T > max_t_) { err = "verify: window size out of range"; return false; }
const ModelGeometry& g = *g_;
SessionState& ss = *ss_;
if (pos0 + T > ss.qsa_states[0].max_cells) { err = "verify: the window runs past the context"; return false; }
if (!capture(T, err) || !capture_commit(err)) return false;
VDBG("captured; staging\n");
const Clock::time_point t0 = Clock::now();
const QsaShapes s = shapes_of(g);
for (int t = 0; t < T; ++t) {
h_tok_[t] = tokens[t];
qsa_step_fill(h_step_ + t * kStepCount, pos0 + t, s);
for (int64_t h = 0; h < g.n_head; ++h) h_pos_[t * g.n_head + h] = (int32_t) (pos0 + t);
}
const bool ple_on = ss.ple.ready();
if (ple_on) { // the reads start here unless ple_ahead started them
int32_t prev[2] = {ss.ple_prev[0], ss.ple_prev[1]};
for (int t = 0; t < T; ++t) {
uint32_t rows[PLE_N_HEADS];
ngram_rows(&tokens[t], prev, 1, ss.ple.consts, rows);
if (!ss.ple.table->ahead_holds(t, rows)) ss.ple.table->ahead(t, rows);
prev[0] = prev[1];
prev[1] = tokens[t];
}
}
// the residency this window decides the main GPU's hits by, and the pool reads (`residency`)
std::memcpy(h_res_, hits_.res, (size_t) (g.n_layers * g.n_expert) * sizeof(int32_t));
*(volatile uint32_t*) h_seq_ = 0;
*(volatile uint32_t*) h_flag_ = 0;
*(volatile uint32_t*) h_flagA_ = 0;
*(volatile uint32_t*) h_flagB_ = 0;
*(volatile uint32_t*) h_pseq_ = 0;
*(volatile uint32_t*) h_pleflag_ = 0;
h_flag2_[0] = h_flag2_[16] = 0;
sink_.gpu2_ring = 0;
std::atomic_thread_fence(std::memory_order_seq_cst);
last_t_ = T;
last_pos0_ = pos0;
for (int t = 0; t < T; ++t) last_tokens_[t] = tokens[t];
ms_host += ms_since(t0);
VDBG("staged; launching\n");
const cudaError_t le = cudaGraphLaunch(exec_[T], cs_);
if (le != cudaSuccess || cudaEventRecord(done_, cs_) != cudaSuccess) {
err = std::string("verify: launch: ") + cudaGetErrorString(cudaGetLastError());
return false;
}
(void) cudaStreamQuery(cs_);
VDBG("launched\n");
// on a failure every wait of the graph passes, so it runs to its end before the next window resets the flags
auto release = [&] {
h_list2_[0] = h_list2_[list2_stride_] = 0;
std::atomic_thread_fence(std::memory_order_seq_cst);
for (uint32_t* f : {h_flag_, h_flagA_, h_flagB_, h_pleflag_, h_flag2_, h_flag2_ + 16})
*(volatile uint32_t*) f = 0xffffffffu;
cudaStreamSynchronize(cs_);
return false;
};
if (ple_on) { // before layer 1: the graph waits for the flag, raised even when a read failed
const Clock::time_point tp = Clock::now();
bool ok = true;
for (int t = 0; t < T && ok; ++t) ok = ss.ple.table->ahead_collect(t, h_ple_ + (size_t) t * g.n_embd, err);
std::atomic_thread_fence(std::memory_order_seq_cst);
_mm_sfence();
*(volatile uint32_t*) h_pleflag_ = 1;
ms_host += ms_since(tp);
if (!ok) return release();
}
volatile uint32_t* const seq = h_seq_;
volatile uint32_t* const flag = h_flag_;
const int G = groups_[T] > 0 ? groups_[T] : 1;
const int gtb[2] = {0, (T + 1) / 2}, gte[2] = {G == 2 ? (T + 1) / 2 : T, T};
// a pool whose second-GPU rows came after the CPU's: the host time until they were in
const volatile uint32_t* late_flag = nullptr;
uint32_t late_want = 0;
Clock::time_point late_at{};
auto settle = [&](bool now) {
if (late_flag != nullptr && (now || *late_flag >= late_want)) {
ms_late2 += ms_since(late_at);
late_flag = nullptr;
}
};
for (int64_t k = 0; k < g.n_layers * G; ++k) {
const int64_t l = k / G;
const int grp = (int) (k % G);
const uint32_t want = (uint32_t) (k + 1);
const Clock::time_point a = Clock::now();
auto last_flush = a;
uint32_t spins = 0;
while (*seq < want) {
settle(false);
_mm_pause();
if ((++spins & 1023u) != 0) continue;
const auto now = Clock::now();
if (now - last_flush > std::chrono::microseconds(2000)) {
last_flush = now;
const cudaError_t q = cudaStreamQuery(cs_);
if (q != cudaErrorNotReady && *seq < want) {
err = "verify: layer " + std::to_string(l) + " never rang (" +
(q == cudaSuccess ? std::string("graph finished") : std::string(cudaGetErrorString(q))) + ")";
return release();
}
if (watch_ != nullptr && !watch_(watch_user_, err)) return release();
}
if (now - a > std::chrono::seconds(20)) {
err = "verify: timed out at layer " + std::to_string(l);
return release();
}
}
const Clock::time_point b = Clock::now();
VDBG("layer %lld rang\n", (long long) l);
cur_layer_ = want - 1;
set_plan_slot(grp);
const int tb = gtb[grp], n = gte[grp] - gtb[grp];
sink_.xk = xk_layer_[(size_t) l] ? h_xk_ + (size_t) tb * (size_t) xk_stride_ : nullptr;
sink_.xk_stride = xk_stride_;
sink_.x1 = h_xq1_ + (size_t) tb * (size_t) (g.n_embd / 32) * 36;
if (pool != nullptr)
pool(user, h_x_ + (size_t) tb * g.n_embd, h_ids_ + (size_t) tb * ss.k, n, ss.k,
h_ymiss_ + (size_t) tb * ss.k * g.n_embd, l);
VDBG("layer %lld served\n", (long long) l);
std::atomic_thread_fence(std::memory_order_seq_cst);
_mm_sfence();
if (pcie_share_ && *(volatile uint32_t*) h_flagA_ != want) { // no PCIe share published: an empty one
sink_.counts[0] = 0;
sink_.counts[1] = 0;
sink_.counts[2] = 0;
sink_.start2[0] = 0;
std::atomic_thread_fence(std::memory_order_seq_cst);
*(volatile uint32_t*) h_flagA_ = want;
raise_flag(h_flagB_, want);
}
if (gpu2_ && sink_.gpu2_ring != want) { // no second-GPU share went out (the pool failed): an empty one
sink_.gpu2_list[0] = 0;
std::atomic_thread_fence(std::memory_order_seq_cst);
*(volatile uint32_t*) sink_.gpu2_flag = want;
}
*flag = want;
ms_wait += std::chrono::duration<double, std::milli>(b - a).count();
ms_pool += ms_since(b);
if (gpu2_ && *(volatile uint32_t*) sink_.gpu2_flag < want) {
settle(true); // a split window's other token group
late_flag = sink_.gpu2_flag;
late_want = want;
late_at = Clock::now();
++late2;
}
if (tail_ != nullptr && k + 1 == g.n_layers * G) tail_(tail_user_);
// the next layer's prediction, published during this layer's pool, else microseconds later (its branch is
// joined before the next layer): waited for, so the experts the second GPU takes never depend on timing
if (predict_ != nullptr && G == 1 && l + 1 < g.n_layers && native_router_enabled()) {
const Clock::time_point c = Clock::now();
if (*(volatile uint32_t*) h_pseq_ < (uint32_t) (l + 1)) {
++predict_late;
while (*(volatile uint32_t*) h_pseq_ < (uint32_t) (l + 1) &&
Clock::now() - c < std::chrono::milliseconds(50)) {
settle(false);
_mm_pause();
}
}
if (*(volatile uint32_t*) h_pseq_ >= (uint32_t) (l + 1)) {
std::atomic_thread_fence(std::memory_order_acquire);
const size_t half = (size_t) ((l + 1) & 1) * (size_t) max_t_ * (size_t) ss.k;
predict_(predict_user_, l + 1, h_pids_ + half, h_pw_ + half, T, ss.k);
}
ms_predict += ms_since(c);
}
if (rows_in_ != nullptr && k + 1 < g.n_layers * G) rows_in_(rows_user_, l);
}
while (late_flag != nullptr && ms_since(late_at) < 50.0) { // the last layer's (the graph ends after them)
settle(false);
_mm_pause();
}
settle(true);
const cudaError_t se = cudaStreamSynchronize(cs_);
if (se != cudaSuccess) { err = std::string("verify: ") + cudaGetErrorString(se); return false; }
cudaStreamSynchronize(copy_); // no host function of this window may raise flag B in the next one
// a sampled or penalized request: the head's sampling again, after the graph (see set_sampling)
if (head_sampling_ && ((!sampling_.greedy && sampling_.temperature > 0.0f) || hist_d_ != nullptr)) {
SamplerParams sp = sampling_;
sp.counter = (uint64_t) pos0;
sample_tokens(head_logits_, T, (int) n_vocab_, hist_d_, hist_len_, sp, m_out_, cs_);
if (cudaStreamSynchronize(cs_) != cudaSuccess) { err = "verify: the head sampling failed"; return false; }
}
for (int t = 0; t < T; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
if (stamps_ != nullptr && G == 1) {
const int64_t NL = g.n_layers;
h_stamps_.resize((size_t) (NL + 1) * kStamps);
cudaMemcpy(h_stamps_.data(), stamps_ + (size_t) T * (size_t) (NL + 1) * kStamps,
h_stamps_.size() * sizeof(unsigned long long), cudaMemcpyDeviceToHost);
if (prof_ns_.empty()) prof_ns_.assign(2 * kStamps, 0.0);
const unsigned long long* w = h_stamps_.data() + (size_t) NL * kStamps;
for (int64_t l = 0; l < NL; ++l) {
const unsigned long long* st = h_stamps_.data() + (size_t) l * kStamps;
const int kind = is_qsa_layer(g, l) ? 1 : 0;
const unsigned long long before = l == 0 ? w[1] : (st - kStamps)[kLayerStamps - 1];
prof_ns_[(size_t) kind * kStamps] += (double) (st[0] - before);
for (int i = 1; i < kLayerStamps; ++i) prof_ns_[(size_t) kind * kStamps + i] += (double) (st[i] - st[i - 1]);
// the shared expert's branch and the second GPU's rows', from the doorbell (they fork there) to their ends
prof_ns_[(size_t) kind * kStamps + kLayerStamps] += (double) (st[kLayerStamps] - st[4]);
if (gpu2_) prof_ns_[(size_t) kind * kStamps + kLayerStamps + 1] += (double) (st[kLayerStamps + 1] - st[4]);
{ // the wait for the CPU's rows, by bucket
const double us = (double) (st[9] - st[8]) / 1e3;
int b = 0;
while (b < kWaitBuckets - 1 && us >= kWaitEdges[b]) ++b;
++prof_wait_hist_[kind][b];
}
++prof_layers_[kind];
}
prof_window_ns_[0] += (double) (w[1] - w[0]);
prof_window_ns_[1] += (double) (w[3] - w[2]);
prof_window_ns_[2] += (double) (w[3] - w[0]);
prof_window_ns_[3] += (double) (w[4] - w[3]);
++prof_windows_;
}
VDBG("window done\n");
++windows;
return true;
}
bool Verifier::copy_logits(int T, float* out, std::string& err) const {
if (cudaMemcpyAsync(out, head_logits_, (size_t) T * (size_t) n_vocab_ * sizeof(float), cudaMemcpyDeviceToHost,
cs_) != cudaSuccess ||
cudaStreamSynchronize(cs_) != cudaSuccess) {
err = std::string("verify: reading the logits back: ") + cudaGetErrorString(cudaGetLastError());
return false;
}
return true;
}
void Verifier::print_profile() const {
if (prof_windows_ == 0) return;
static const char* const names[kStamps] = {"stamp gap", "HC read, mixer", "mixer", "HC read, FFN",
"router, ring, hits", "expert inputs", "VRAM experts",
"wait for PCIe plan", "PCIe experts", "wait for CPU",
"wait for 2nd GPU", "rows + combine", "last HC write",
"shared expert, beside", "2nd GPU rows, beside"};
const double nw = (double) prof_windows_;
std::printf("%-24s GPU time over %lld windows, ms per window: inputs %.3f, head %.3f, whole window %.3f; "
"one stamp %.2f us (each stage below includes one; the last two run from the ring beside the "
"six before \"rows + combine\")\n", "window profile", (long long) prof_windows_,
prof_window_ns_[0] / nw / 1e6, prof_window_ns_[1] / nw / 1e6, prof_window_ns_[2] / nw / 1e6,
prof_window_ns_[3] / nw / 1e3);
std::printf(" %-22s %12s %12s %14s\n", "stage", "GDN us/layer", "QSA us/layer", "ms per window");
for (int i = 0; i < kStamps; ++i) {
const double a = prof_ns_[(size_t) i], b = prof_ns_[(size_t) kStamps + i];
std::printf(" %-22s %12.1f %12.1f %14.3f\n", names[i],
prof_layers_[0] ? a / (double) prof_layers_[0] / 1e3 : 0.0,
prof_layers_[1] ? b / (double) prof_layers_[1] / 1e3 : 0.0, (a + b) / nw / 1e6);
}
std::printf(" %-22s", "wait for CPU, layers");
for (int b = 0; b < kWaitBuckets; ++b) {
const double f = 100.0 * (double) (prof_wait_hist_[0][b] + prof_wait_hist_[1][b]) /
(double) std::max<int64_t>(1, prof_layers_[0] + prof_layers_[1]);
if (b < kWaitBuckets - 1) std::printf(" <%.0f us %.0f%%,", kWaitEdges[b], f);
else std::printf(" more %.0f%%\n", f);
}
}
void Verifier::ple_ahead(int t, int32_t token) {
using namespace strata::kernels;
SessionState& ss = *ss_;
if (!ss.ple.ready() || t < 0 || t >= max_t_) return;
ahead_tok_[t] = token;
int32_t prev[2] = {t >= 2 ? ahead_tok_[t - 2] : ss.ple_prev[t], t >= 1 ? ahead_tok_[t - 1] : ss.ple_prev[1]};
uint32_t rows[PLE_N_HEADS];
ngram_rows(&token, prev, 1, ss.ple.consts, rows);
ss.ple.table->ahead(t, rows);
}
void Verifier::set_plan_slot(int grp) {
const int64_t cap = sink_.cap;
int32_t* base = h_plan_ + (size_t) grp * (size_t) plan_i32_;
sink_.counts = base;
sink_.dst = base + 4 + cap + 1;
sink_.tok = sink_.dst + cap;
sink_.ptr2 = (unsigned long long*) (base + plan_ptr_off_) + cap;
sink_.start2 = base + plan_ptr_off_ + 4 * cap;
const int G = groups_[last_t_] > 0 ? groups_[last_t_] : 1;
const int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;
sink_.staging = (unsigned long long) (staging_ + (size_t) (grp * per) * strata::kernels::cpu::expert_layout().max_blob);
sink_.staging_cap = per;
if (gpu2_) {
sink_.gpu2_list = h_list2_ + (size_t) grp * (size_t) list2_stride_;
sink_.gpu2_flag = h_flag2_ + (size_t) grp * 16;
sink_.ring = cur_layer_ + 1;
}
}
// Flag B only rises: a host function of an earlier layer may run after a later layer already raised it directly.
void Verifier::raise_flag(uint32_t* flag, uint32_t value) {
volatile long* f = (volatile long*) flag;
#if defined(_WIN32)
long cur = *f;
while ((uint32_t) cur < value) {
const long prev = _InterlockedCompareExchange(f, (long) value, cur);
if (prev == cur) break;
cur = prev;
}
#else
uint32_t cur = __atomic_load_n((uint32_t*) flag, __ATOMIC_SEQ_CST);
while (cur < value && !__atomic_compare_exchange_n((uint32_t*) flag, &cur, value, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {}
#endif
}
// Plan v0.3 P6: the PCIe share by DMA. The copy engine moves the blobs while the CPU computes its own share and the
// GPU its VRAM experts; a host function raises flag B when they have landed (the graph waits for it before the PCIe
// groups). Staging is split between the two token groups of a split window.
void Verifier::fetch_dma(void* ctx, const uint8_t* const* src, int n, size_t bytes) {
Verifier* v = (Verifier*) ctx;
const uint32_t want = v->cur_layer_ + 1;
if (n <= 0) { raise_flag(v->h_flagB_, want); return; }
uint8_t* stage = (uint8_t*) v->sink_.staging; // this group's half in a split window
for (int i = 0; i < n; ++i) cudaMemcpyAsync(stage + (size_t) i * bytes, src[i], bytes, cudaMemcpyHostToDevice, v->copy_);
FlagSet& fs = v->flag_sets_[v->cur_layer_ % (sizeof v->flag_sets_ / sizeof v->flag_sets_[0])];
fs.flag = v->h_flagB_;
fs.value = want;
cudaLaunchHostFunc(v->copy_, [](void* p) { FlagSet* s = (FlagSet*) p; raise_flag(s->flag, s->value); }, &fs);
}
void Verifier::publish_plan(void* ctx) {
Verifier* v = (Verifier*) ctx;
_mm_sfence();
*(volatile uint32_t*) v->h_flagA_ = v->cur_layer_ + 1;
}
bool Verifier::commit(int n_keep, std::string& err, bool wait) {
if (n_keep < 1 || n_keep > last_t_) { err = "verify: commit count out of range"; return false; }
const Clock::time_point t0 = Clock::now();
h_commit_[0] = n_keep;
h_commit_[1] = n_keep - 1;
for (int t = 0; t < max_t_; ++t) h_commit_[2 + t] = t < n_keep ? (int32_t) (last_pos0_ + t) : -1;
std::atomic_thread_fence(std::memory_order_seq_cst);
const cudaError_t le = cudaGraphLaunch(commit_exec_, cs_);
if (le != cudaSuccess) { err = std::string("verify: commit launch: ") + cudaGetErrorString(le); return false; }
(void) cudaStreamQuery(cs_); // submit now, not at the next driver call
commit_pending_ = true;
for (int t = 0; t < n_keep; ++t) {
ss_->ple_prev[0] = ss_->ple_prev[1];
ss_->ple_prev[1] = last_tokens_[t];
}
ms_commit += ms_since(t0);
return !wait || wait_commit(err);
}
bool Verifier::wait_commit(std::string& err) {
if (!commit_pending_) return true;
const Clock::time_point t0 = Clock::now();
commit_pending_ = false;
const cudaError_t se = cudaStreamSynchronize(cs_);
ms_commit += ms_since(t0);
if (se != cudaSuccess) { err = std::string("verify: commit: ") + cudaGetErrorString(se); return false; }
return true;
}
} // namespace strata::core