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++
perf(gr): multi-token hyper-connection read/write, exact-T GR and BF16 projections (#783 PR-g)
Niko1221104a8485e9bb
diff --git a/CMakeLists.txt b/CMakeLists.txtindex ce6989c1f..cd6051779 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -820,6 +820,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)target_include_directories(resident_plan_parity PRIVATE ${_strata_gpu_include_directories})target_link_libraries(resident_plan_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})add_test(NAME resident_plan_parity COMMAND resident_plan_parity)+ # #783 PR-g: the multi-token hyper-connection read/write (exact-T projections under them) against the single-token calls+ add_executable(gr_multi_parity src/kernels/gr_multi_parity.cpp)+ target_include_directories(gr_multi_parity PRIVATE ${_strata_gpu_include_directories})+ target_link_libraries(gr_multi_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})+ add_test(NAME gr_multi_parity COMMAND gr_multi_parity)if(STRATA_HIP_GFX906)# the S6 router (STRATA_HIP_ROUTER_FAST=1 on gfx906) against the portable kernel, bitwise, on wave64add_executable(hip_router_fast tests/hip/router_fast.cpp)diff --git a/include/strata/kernels/gr.hpp b/include/strata/kernels/gr.hppindex ce15813a8..d666a25bd 100644--- a/include/strata/kernels/gr.hpp+++ b/include/strata/kernels/gr.hpp@@ -111,6 +111,11 @@ void gr_read(const float* R, const float* w_norm, const uint16_t* w_down, constconst uint16_t* w_inject, float eps, const GrShapes& s, const GrWorkspace& ws, float* mixed,float* inject, void* stream);+void gr_read_multi(const float* R, const float* w_norm, const uint16_t* w_down, const uint16_t* w_up,+ const uint16_t* w_inject, float eps, const GrShapes& s, const GrWorkspace& ws,+ float* xn_multi, float* lo_multi, float* gated_multi, float* mixed, float* inject,+ int n_tok, void* stream);+/// `build_hc_combine`. `R_out[i] = R[i] + block_out[d] * w[c]` with `w[c] = 2*sigmoid(inject[c]/hc)`.////// The `2*sigmoid` is what CENTRES the gate on 1, so a zero injection is a plain residual add - the source@@ -119,4 +124,7 @@ void gr_read(const float* R, const float* w_norm, const uint16_t* w_down, constvoid gr_write(const float* R, const float* block_out, const float* inject, const GrShapes& s, float* R_out,void* stream);+void gr_write_multi(const float* R, const float* block_out, const float* inject, const GrShapes& s, float* R_out,+ int n_tok, void* stream);+} // namespace strata::kernelsdiff --git a/include/strata/kernels/native_gr_norm.hpp b/include/strata/kernels/native_gr_norm.hppindex b8bb4ed1b..4bbeeea96 100644--- a/include/strata/kernels/native_gr_norm.hpp+++ b/include/strata/kernels/native_gr_norm.hpp@@ -15,4 +15,7 @@ namespace strata::kernels {void native_gr_rms_norm_weighted(const float* input, const float* gamma, float* output,int n_cols, int n_rows, float epsilon, void* stream);+void native_gr_rms_norm_weighted_multi(const float* input, const float* gamma, float* output,+ int n_cols, int n_rows, int n_tok, float epsilon, void* stream);+} // namespace strata::kernelsdiff --git a/include/strata/kernels/native_gr_postops.hpp b/include/strata/kernels/native_gr_postops.hppindex 790b883c6..d104706f6 100644--- a/include/strata/kernels/native_gr_postops.hpp+++ b/include/strata/kernels/native_gr_postops.hpp@@ -19,10 +19,16 @@ void native_gr_down_silu(float* lo, int hc_lr, int hc, void* stream);void native_gr_pre_gated(const float* xn, float* gate, float* mixed,int n_embd, int hc, bool fused_layer, void* stream);+void native_gr_pre_gated_multi(const float* xn, float* gate, float* mixed,+ int n_embd, int hc, int n_tok, bool fused_layer, void* stream);+// inject[hc] -> SCALE(1/hc), sigmoid, SCALE(2), identity DSV4_HC_POST.// residual/output contain [n_embd,hc], block_out has n_embd elements.// output may equal residual exactly; all other overlaps are invalid.void native_gr_post(const float* residual, const float* block_out, const float* inject,float* output, int n_embd, int hc, void* stream);+void native_gr_post_multi(const float* residual, const float* block_out, const float* inject,+ float* output, int n_embd, int hc, int n_tok, void* stream);+} // namespace strata::kernelsdiff --git a/src/core/mtp.cpp b/src/core/mtp.cppindex 21f932699..c7509a257 100644--- a/src/core/mtp.cpp+++ b/src/core/mtp.cpp@@ -1,1449 +1,1457 @@-// src/core/mtp.cpp - see include/strata/core/mtp.hpp.-#include "strata/core/mtp.hpp"-#include "strata/core/coupled_draft.hpp"-#include "strata/core/on_device.hpp"--#include "strata/core/native_head.hpp"-#include "strata/core/peer_experts.hpp"-#include "strata/platform/memory.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_q4.hpp"-#include "strata/kernels/kv_q8.hpp"-#include "strata/kernels/native_moe.hpp"-#include "strata/kernels/native_mmvq.hpp"-#include "strata/kernels/native_qsa.hpp"-#include "strata/kernels/native_rope.hpp"-#include "strata/kernels/native_router.hpp"-#include "strata/kernels/qsa.hpp"-#include "strata/kernels/qsa_decode_attn.hpp"-#include "strata/kernels/quantize_act.hpp"-#include "strata/kernels/rope.hpp"-#include "strata/kernels/router_top10.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 "strata/kernels/ngram.hpp"--#include <algorithm>-#include <atomic>-#include <chrono>-#include <cstdio>-#include <cstdlib>-#include <cstring>-#include <exception>-#include <fstream>-#include <sstream>-#include <vector>-#if defined(_WIN32)-#include <intrin.h>-#elif defined(__x86_64__)-#include <immintrin.h>-#endif--#ifdef STRATA_NATIVE_EXPERTS-#include "ggml.h" // --mtp-q4: the head rows' and projections' formats, dequantized and requantized at load-#endif--namespace strata::core {-namespace {--constexpr float EPS = 1e-6f;-constexpr int GGML_Q8_0 = 8;-constexpr int GGML_Q4_0 = 2;-using Clock = std::chrono::steady_clock;-double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }--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) {- if (cudaHostAlloc(h, bytes, cudaHostAllocMapped | (peer_portable() ? cudaHostAllocPortable : 0)) != 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;-}--// 64-bit seek/tell on a `FILE*`: `fseek`/`ftell` take a 32-bit `long` on Windows and would wrap past 2 GiB.-#if defined(_WIN32)-#define STRATA_FILE_SEEK64(f, o, w) _fseeki64((f), (long long) (o), (w))-#define STRATA_FILE_TELL64(f) _ftelli64(f)-#else-#define STRATA_FILE_SEEK64(f, o, w) fseeko((f), (off_t) (o), (w))-#define STRATA_FILE_TELL64(f) ftello(f)-#endif--bool read_file(const std::string& path, std::vector<uint8_t>& out) {- // Loader fix (0.1.15+loaderfix.2): `ifstream::read` reaches the disk as 4095-byte reads on MSVC - the same- // split `load_experts_ranges` had - so the drafter's 111 MiB dense blob paid 4 KiB per operation on a cold- // start. `fread` passes a request bigger than the stream buffer straight to `ReadFile()`.- FILE* f = std::fopen(path.c_str(), "rb");- if (f == nullptr) return false;- // A `FILE*` has no destructor and the short-read path below returns early: the guard closes on every path.- struct Closer {- FILE* f;- ~Closer() { if (f != nullptr) std::fclose(f); }- } closer{f};- if (STRATA_FILE_SEEK64(f, 0, SEEK_END) != 0) return false;- const long long n = (long long) STRATA_FILE_TELL64(f);- if (n < 0 || STRATA_FILE_SEEK64(f, 0, SEEK_SET) != 0) return false;-#if !defined(_WIN32)- strata::platform::advise_willneed(fileno(f), 0, (uint64_t) n);-#endif- out.resize((size_t) n);- return n == 0 || std::fread(out.data(), 1, (size_t) n, f) == (size_t) n;-}--} // namespace--MtpDrafter::~MtpDrafter() {- if (cs_) cudaStreamSynchronize(cs_);- for (auto& e : prefill_exec_) if (e) cudaGraphExecDestroy(e);- for (auto& e : prefill_dev_exec_) if (e) cudaGraphExecDestroy(e);- if (pf_dev_) cudaFree(pf_dev_);- if (dense4_) cudaFree(dense4_);- for (auto& e : round_exec_) if (e) cudaGraphExecDestroy(e);- for (auto& e : step_exec_) if (e) cudaGraphExecDestroy(e);- for (auto& e : round_exec_c_) if (e) cudaGraphExecDestroy(e);- for (auto& e : step_exec_c_) if (e) cudaGraphExecDestroy(e);- if (cparams_) cudaFree(cparams_);- if (cring_) cudaFree(cring_);- if (dinv_) cudaFree(dinv_);- if (cscratch_) cudaFree(cscratch_);- if (h_cparams_) cudaFreeHost(h_cparams_);- if (h_chist_) cudaFreeHost(h_chist_);- if (cs_) cudaStreamDestroy(cs_);- if (side_) cudaStreamDestroy(side_);- if (sh_fork_) cudaEventDestroy(sh_fork_);- if (sh_join_) cudaEventDestroy(sh_join_);- if (owns_weights_ && dense_) cudaFree(dense_);- if (owns_weights_ && experts_) cudaFree(experts_);- if (state_arena_) cudaFree(state_arena_);- if (arena_) cudaFree(arena_);- if (head_logits_) cudaFree(head_logits_);- if (owns_draft_head_ && dhead_) { strata::kernels::native_q6_k_unpack(dhead_); cudaFree(dhead_); }- if (owns_draft_head_ && dvocab_) cudaFree(dvocab_);- if (ev_chain_) cudaEventDestroy(ev_chain_);- for (cudaEvent_t e : ev_step_) if (e) cudaEventDestroy(e);- void* hosts[] = {h_tok_, h_step_, h_pos_, h_row_, h_out_, h_prob_, h_force_};- for (void* h : hosts) if (h) cudaFreeHost(h);-}--const float* MtpDrafter::f32(const char* name) const {- for (const auto& t : tensors_) if (t.name == name && t.kind == "f32") return (const float*) (dense_ + t.off);- return nullptr;-}-const uint16_t* MtpDrafter::bf16(const char* name) const {- for (const auto& t : tensors_) if (t.name == name && t.kind == "bf16") return (const uint16_t*) (dense_ + t.off);- return nullptr;-}-const void* MtpDrafter::q8(const char* name) const {- for (const auto& t : tensors_) if (t.name == name && t.kind == "q8_0") return dense_ + t.off;- return nullptr;-}-const void* MtpDrafter::wq(const char* name, int& type) const {- if (dense4_ != nullptr)- for (const auto& e : q4_off_)- if (e.first == name) { type = GGML_Q4_0; return dense4_ + e.second; }- type = GGML_Q8_0;- return q8(name);-}--// --mtp-q4: every Q8_0 tensor of dense.bin as Q4_0 (ggml's reference quantizer), in one device buffer-bool MtpDrafter::make_q4_dense(const std::vector<uint8_t>& blob, std::string& err) {-#ifdef STRATA_NATIVE_EXPERTS- uint64_t total = 0;- std::vector<std::pair<const Tensor*, uint64_t>> plan;- for (const auto& t : tensors_) {- if (t.kind != "q8_0" || t.cols % 32 != 0) continue;- plan.push_back({&t, total});- total += ((uint64_t) t.rows * (uint64_t) (t.cols / 32) * 18 + 255) & ~255ull;- }- std::vector<uint8_t> host((size_t) total);- std::vector<float> row;- const auto* q8t = ggml_get_type_traits(GGML_TYPE_Q8_0);- for (const auto& [t, off] : plan) {- row.resize((size_t) t->cols);- const uint64_t in_row = (uint64_t) (t->cols / 32) * 34, out_row = (uint64_t) (t->cols / 32) * 18;- for (int64_t r = 0; r < t->rows; ++r) {- q8t->to_float(blob.data() + t->off + (uint64_t) r * in_row, row.data(), t->cols);- ggml_quantize_chunk(GGML_TYPE_Q4_0, row.data(), host.data() + off + (uint64_t) r * out_row, 0, 1, t->cols,- nullptr);- }- q4_off_.push_back({t->name, off});- }- if (cudaMalloc((void**) &dense4_, (size_t) total) != cudaSuccess) { err = "mtp: the Q4_0 projections do not fit"; return false; }- cudaMemcpy(dense4_, host.data(), (size_t) total, cudaMemcpyHostToDevice);- vram_ += total;- std::fprintf(stderr, "strata mtp: --mtp-q4: %zu projections as Q4_0 (%.1f MiB)\n", plan.size(), (double) total / 1048576.0);- return true;-#else- (void) blob;- err = "mtp: --mtp-q4 needs a build with native experts (ggml)";- return false;-#endif-}--// --mtp-q4: the draft head's subset rows as Q4_0 (dequantized from the main head's format)-bool MtpDrafter::make_q4_head(std::string& err) {-#ifdef STRATA_NATIVE_EXPERTS- const int64_t N = g_->n_embd;- const auto* tt = ggml_get_type_traits((ggml_type) head_->type());- if (tt == nullptr || tt->to_float == nullptr || N % 32 != 0) { err = "mtp: --mtp-q4 cannot read the head's format"; return false; }- const size_t in_row = head_->row_bytes(), out_row = (size_t) (N / 32) * 18;- std::vector<uint8_t> src((size_t) n_dvocab_ * in_row), dst((size_t) n_dvocab_ * out_row);- if (cudaMemcpy(src.data(), dhead_, src.size(), cudaMemcpyDeviceToHost) != cudaSuccess) { err = "mtp: reading the draft head"; return false; }- std::vector<float> row((size_t) N);- for (int64_t r = 0; r < n_dvocab_; ++r) {- tt->to_float(src.data() + (size_t) r * in_row, row.data(), N);- ggml_quantize_chunk(GGML_TYPE_Q4_0, row.data(), dst.data() + (size_t) r * out_row, 0, 1, N, nullptr);- }- // the Q4_0 rows replace the subset in place (they are smaller)- if (cudaMemcpy(dhead_, dst.data(), dst.size(), cudaMemcpyHostToDevice) != cudaSuccess) { err = "mtp: writing the draft head"; return false; }- dhead_type_ = GGML_Q4_0;- std::fprintf(stderr, "strata mtp: --mtp-q4: draft head over %lld tokens as Q4_0 (%.1f MiB read per step, was %.1f)\n",- (long long) n_dvocab_, (double) dst.size() / 1048576.0, (double) src.size() / 1048576.0);- return true;-#else- err = "mtp: --mtp-q4 needs a build with native experts (ggml)";- return false;-#endif-}--bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, SessionState& ss, int max_t, std::string& err,- int64_t window, const MtpDrafter* shared) {- cudaGetDevice(&device_); // a layer split's last stage on another GPU: the drafter lives there- g_ = &g;- ss_ = &ss;- if (ple_ss_ == nullptr) ple_ss_ = &ss;- max_t_ = max_t;- rt_dir_ = rt_dir;- if (max_t < 1 || max_t > strata::kernels::kVerifyMaxT) { err = "mtp: max_t out of range"; return false; }- if (shared != nullptr) {- if (shared->device_ != device_ || shared->g_ != &g || shared->dense_ == nullptr ||- shared->experts_ == nullptr || shared->rt_dir_ != rt_dir) {- err = "mtp: incompatible shared weights";- return false;- }- dense_ = shared->dense_;- experts_ = shared->experts_;- tensors_ = shared->tensors_;- owns_weights_ = false;- owns_draft_head_ = false;- }- // Loader fix (0.1.15+loaderfix.2): the two reads below are the whole “drafter files” cost; reporting- // them apart from the rest of the stage is what makes the next regression visible.- const auto t_files = std::chrono::steady_clock::now();- // ---- the index and the dense weights- if (shared == nullptr) {- std::ifstream idx(rt_dir + "/dense.txt");- if (!idx) { err = "mtp: cannot open " + rt_dir + "/dense.txt (run tools/mtp_rt.py)"; return false; }- std::string line;- while (std::getline(idx, line)) {- if (line.empty()) continue;- std::istringstream is(line);- Tensor t;- is >> t.name >> t.kind >> t.rows >> t.cols >> t.off >> t.bytes;- if (!is) { err = "mtp: malformed dense.txt line: " + line; return false; }- tensors_.push_back(t);- }- std::vector<uint8_t> blob;- if (!read_file(rt_dir + "/dense.bin", blob)) { err = "mtp: cannot read dense.bin"; return false; }- const cudaError_t alloc = cudaMalloc((void**) &dense_, blob.size());- if (alloc != cudaSuccess) {- size_t free_bytes = 0, total_bytes = 0;- const cudaError_t info = cudaMemGetInfo(&free_bytes, &total_bytes);- err = "mtp: dense weights allocation failed (" + std::string(cudaGetErrorString(alloc)) +- "), requested " + std::to_string(blob.size() >> 20) + " MiB, CUDA0 free " +- (info == cudaSuccess ? std::to_string(free_bytes >> 20) + " MiB" : "unknown");- return false;- }- cudaMemcpy(dense_, blob.data(), blob.size(), cudaMemcpyHostToDevice);- vram_ += blob.size();- if (q4_ && !make_q4_dense(blob, err)) return false;- }- // ---- the 512 routed experts, one blob each- if (shared == nullptr) {- const uint64_t bytes = (uint64_t) g.n_expert * strata::kernels::cpu::BLOB;- // Loader fix (0.1.15+loaderfix.2): each 64 MiB read below reached the disk as ~16k 4095-byte reads under- // MSVC's `basic_filebuf::xsgetn`, which is what made 675 MiB of drafter experts take minutes.- FILE* f = std::fopen((rt_dir + "/experts.bin").c_str(), "rb");- if (f == nullptr) { err = "mtp: cannot open experts.bin"; return false; }- struct Closer {- FILE* f;- ~Closer() { if (f != nullptr) std::fclose(f); }- } closer{f};- if (cudaMalloc((void**) &experts_, bytes) != cudaSuccess) { err = "mtp: the 512 experts do not fit in VRAM"; return false; }-#if !defined(_WIN32)- strata::platform::advise_willneed(fileno(f), 0, bytes);-#endif- std::vector<uint8_t> chunk(64u << 20);- for (uint64_t off = 0; off < bytes;) {- const uint64_t n = std::min<uint64_t>(chunk.size(), bytes - off);- if (std::fread(chunk.data(), 1, (size_t) n, f) != (size_t) n) { err = "mtp: experts.bin is truncated"; return false; }- cudaMemcpy(experts_ + off, chunk.data(), n, cudaMemcpyHostToDevice);- off += n;- }- vram_ += bytes;- }- const char* required[] = {"fc_embedding.weight", "fc_hidden.weight", "self_attn.q_proj.weight", "self_attn.k_proj.weight",- "self_attn.v_proj.weight", "self_attn.o_proj.weight", "mlp.shared_expert.gate_proj.weight",- "mlp.shared_expert.up_proj.weight", "mlp.shared_expert.down_proj.weight"};- for (const char* n : required) if (!q8(n)) { err = std::string("mtp: ") + n + " is missing (q8_0)"; return false; }-- // ---- the layer's own K/V (dense attention: no indexer state is read)- const strata::kernels::QsaShapes s = shapes_of(g);- const int64_t max_cells = ss.qsa_states[ss.qsa_primary()].max_cells;- // KV streaming: the drafter only reads its last `window` cells, so with streaming on its K/V is a ring of the- // window (plus the cells a round writes ahead of its queries) over a host copy, refilled on a resume. The host copy- // is pinned after the expert arena has pinned what it could: if it does not fit, the K/V stays whole in VRAM.- int64_t ring = (window > 0 && window < max_cells) ? window + 4 * (int64_t) max_t + 64 : 0;- // K8V4 never applies to the drafter: its own attention paths (below, and verify.cpp) handle whole formats- // only, whatever ring shape it takes (0, a window, or the -1 fully-resident fallback).- const bool kv_hybrid_was = qsa_kv_hybrid();- const bool kv_int8_was = qsa_kv_int8();- qsa_set_kv_hybrid(false);- if (kv_hybrid_was) qsa_set_kv_int8(true); // the drafter under --kv k8v4: plain INT8- uint64_t sb = qsa_state_bytes(g, max_cells, false, ring);- if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }- if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[ss.qsa_primary()], ring) == 0) {- if (st_.kv_mode == 0) { err = "mtp: state init failed"; return false; }- std::fprintf(stderr, "strata mtp: no pinned RAM left for the draft layer's K/V copy; keeping it in VRAM\n");- cudaGetLastError();- cudaFree(state_arena_);- st_ = QsaState{};- ring = -1; // fully resident- sb = qsa_state_bytes(g, max_cells, false, ring);- if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }- if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[ss.qsa_primary()], ring) == 0) { err = "mtp: state init failed"; return false; }- }- qsa_set_kv_int8(kv_int8_was);- qsa_set_kv_hybrid(kv_hybrid_was);- qsa_state_zero(st_, g, nullptr);- cudaDeviceSynchronize();- vram_ += sb;-- // ---- buffers- window_ = (window > 0 && window < max_cells) ? window : 0;- cap_ = (((window_ > 0 ? window_ : max_cells) + 63) / 64) * 64;- 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 NH = (uint64_t) g.n_head, HD = (uint64_t) g.head_dim, NKV = (uint64_t) g.n_head_kv;- const uint64_t R2 = 2 * T; // step/pos rows: T catch-up rows + up to T-2 chain steps- bool ok = mapped(T * 4 + 64, (void**) &h_tok_, (void**) &m_tok_) &&- mapped(R2 * 4 * 4 + 64, (void**) &h_step_, (void**) &m_step_) &&- mapped(R2 * NH * 4 + 64, (void**) &h_pos_, (void**) &m_pos_) &&- mapped(64, (void**) &h_row_, (void**) &m_row_) &&- mapped(T * 4 + 64, (void**) &h_out_, (void**) &m_out_) &&- mapped(T * 4 + 64, (void**) &h_prob_, (void**) &m_prob_) &&- (!force_on_ || mapped(64, (void**) &h_force_, (void**) &m_force_));- if (!ok) { err = "mtp: mapped staging failed"; return false; }- if (h_force_ != nullptr) for (int j = 0; j < 16; ++j) h_force_[j] = -1;- auto carve = [&](Bump& b) {- tok_ = b.take<int32_t>(T); step_ = b.take<int32_t>(R2 * 4); pos_ = b.take<int32_t>(R2 * NH); row_ = b.take<int32_t>(4);- ident_ = b.take<int32_t>(T * (uint64_t) cap_);- Rin_ = b.take<float>(T * HC * N); R_ = b.take<float>(T * HC * N);- emb_ = b.take<float>(T * N); en_ = b.take<float>(T * N); e2_ = b.take<float>(T * N);- hn_ = b.take<float>(T * HC * N); h2_ = b.take<float>(T * HC * N);- mixed_ = 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); bo_ = b.take<float>(T * N);- xn_ = b.take<float>(T * HC * N);- xq_ = b.take<uint8_t>(strata::kernels::native_q8_1_bytes((int) (NH * HD), 8));- 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);- attn_ = b.take<float>(T * NH * HD); attn32_ = b.take<float>(T * NH * HD);- attn_scratch_ = b.take<float>((uint64_t) attn_scratch_floats_); // the full layer runs one row at a time- logits_ = b.take<float>(T * (uint64_t) g.n_expert); w_ = b.take<float>(T * K); ids_ = b.take<int32_t>(T * K);- shared_ = b.take<float>(T * N); parts_ = b.take<float>(T * K * N); y_ = b.take<float>(T * N);- sample_ = b.take<float>(T * N);- hit_slot_ = b.take<int32_t>(T * K); hit_dst_ = b.take<int32_t>(T * K); hit_count_ = b.take<int32_t>(4);- grp_ptr_ = b.take<unsigned long long>(T * K); grp_start_ = b.take<int32_t>(T * K + 1);- grp_counts_ = b.take<int32_t>(4);- hit_xq_ = b.take<uint8_t>(T * (N / 32) * 34); hit_xs_ = b.take<float>(T * (N / 32));- hit_scratch_ = b.take<uint8_t>(strata::kernels::moe_hit_grouped_scratch_bytes((int64_t) (T * K), g.n_embd, g.n_ff));- sh_scratch_ = (float*) b.take<uint8_t>(strata::kernels::shared_expert_scratch_bytes(g.n_ff));- x_bf16_ = b.take<uint16_t>(N);- out_ids_ = b.take<int32_t>(T + 4);- probs_ = b.take<float>(T + 4);- arg_scratch_ = b.take<uint8_t>(strata::kernels::argmax_rows_scratch_bytes((int) T));- top_scratch_ = b.take<uint8_t>(strata::kernels::row_top_prob_scratch_bytes((int) T));- dummy_inj_ = b.take<float>(HC);- };- Bump count;- carve(count);- if (cudaMalloc(&arena_, count.used) != cudaSuccess) { err = "mtp: buffers do not fit"; return false; }- cudaMemset(arena_, 0, count.used);- Bump real;- real.base = (uint8_t*) arena_;- carve(real);- vram_ += count.used;- {- std::vector<int32_t> id((size_t) (T * (uint64_t) cap_));- for (uint64_t t = 0; t < T; ++t)- for (int64_t i = 0; i < cap_; ++i) id[(size_t) (t * (uint64_t) cap_ + (uint64_t) i)] = (int32_t) i;- cudaMemcpy(ident_, id.data(), id.size() * 4, cudaMemcpyHostToDevice);- }- // --pipeline-windows 2 (the forcing graphs): the chain shares its card with stage 1's windows and the next launch- // on stage 0 waits for it, so its stream gets the highest priority there (STRATA_MTP_PRIORITY=0: the default)- bool prio = false;-#if !defined(STRATA_USE_HIP) // (HIP: the default priority)- static const bool hi_prio = [] { const char* v = std::getenv("STRATA_MTP_PRIORITY"); return v == nullptr || std::atoi(v) != 0; }();- int prio_lo = 0, prio_hi = 0;- if (force_on_ && hi_prio && cudaDeviceGetStreamPriorityRange(&prio_lo, &prio_hi) == cudaSuccess)- prio = cudaStreamCreateWithPriority(&cs_, cudaStreamNonBlocking, prio_hi) == cudaSuccess;- cudaGetLastError();-#endif- if (!prio && cudaStreamCreateWithFlags(&cs_, cudaStreamNonBlocking) != cudaSuccess) { err = "mtp: stream"; return false; }- if (cudaStreamCreateWithFlags(&side_, cudaStreamNonBlocking) != cudaSuccess ||- cudaEventCreateWithFlags(&sh_fork_, cudaEventDisableTiming) != cudaSuccess ||- cudaEventCreateWithFlags(&sh_join_, cudaEventDisableTiming) != cudaSuccess) {- err = "mtp: streams";- return false;- }- const double files_s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t_files).count();- if (shared != nullptr) {- std::fprintf(stderr, "strata mtp: shared draft weights, %.0f MiB of private state and buffers\n",- (double) vram_ / 1048576.0);- } else {- std::fprintf(stderr, "strata mtp: draft layer loaded, %.0f MiB of VRAM (experts %.0f, dense %.0f), files read in %.2f s (%.0f MiB/s)\n",- (double) vram_ / 1048576.0, (double) g.n_expert * strata::kernels::cpu::BLOB / 1048576.0,- (double) tensors_.back().off / 1048576.0, files_s,- files_s > 0 ? ((double) g.n_expert * strata::kernels::cpu::BLOB + (double) tensors_.back().off) /- 1048576.0 / files_s : 0.0);- }- return true;-}--namespace {-// STRATA_MTP_FULL_HEAD=1 (a diagnostic): draft over the whole vocabulary (the main head itself) instead of-// rt/draft_vocab.bin's subset - the reference head, to measure what the subset costs in acceptance-bool full_head_env() {- static const bool on = [] { const char* v = std::getenv("STRATA_MTP_FULL_HEAD"); return v && v[0] == '1'; }();- return on;-}-} // namespace--bool MtpDrafter::top2_env() {- static const bool on = [] { const char* v = std::getenv("STRATA_MTP_TOP2"); return v && v[0] == '1'; }();- return on;-}--// STRATA_MTP_TOP2=1 (a diagnostic for tree drafts): draft j's runner-up under the draft layer, from the head logits-// the step just wrote (row 0), on the host. Slow; only for measuring what a second branch would have caught.-void MtpDrafter::record_top2(int j) {- const int64_t nv = dhead_ != nullptr ? n_dvocab_ : n_vocab_;- if ((int64_t) lg_host_.size() < nv) lg_host_.resize((size_t) nv);- if ((int) top2_.size() < max_t_) top2_.assign((size_t) max_t_, -1);- if (j < 0 || j >= max_t_) return;- if (cudaMemcpy(lg_host_.data(), head_logits_, (size_t) nv * sizeof(float), cudaMemcpyDeviceToHost) != cudaSuccess) {- top2_[(size_t) j] = -1;- return;- }- int64_t b1 = -1, b2 = -1;- for (int64_t i = 0; i < nv; ++i) {- const float v = lg_host_[(size_t) i];- if (b1 < 0 || v > lg_host_[(size_t) b1]) { b2 = b1; b1 = i; }- else if (b2 < 0 || v > lg_host_[(size_t) b2]) b2 = i;- }- top2_[(size_t) j] = b2 < 0 ? -1 : dhead_ != nullptr ? dvocab_host_[(size_t) b2] : (int32_t) b2;-}--uint64_t MtpDrafter::bind_bytes(uint64_t head_row_bytes, int64_t n_vocab) const {- uint64_t bytes = head_logits_ ? 0 : (uint64_t) max_t_ * (uint64_t) n_vocab * sizeof(float);- if (dhead_ == nullptr && owns_draft_head_ && !full_head_env()) {- if (FILE* f = std::fopen(vocab_file().c_str(), "rb")) {- std::fseek(f, 0, SEEK_END);- const long size = std::ftell(f);- std::fclose(f);- if (size >= 4 && size % 4 == 0) bytes += (uint64_t) (size / 4) * head_row_bytes + (uint64_t) size;- }- }- // coupled draft sampling (STRATA_SPEC_COUPLED=1 only): an upper bound - the id -> subset map, the penalty ring,- // the split scratch (64 lists of 64 entries at most) and the parameters- if (coupled_draft_env() && cparams_ == nullptr)- bytes += (uint64_t) n_vocab * 4 + (uint64_t) (kCoupledHistCap + max_t_) * 4 + 64ull * 64ull * 8ull + 4096;- return bytes;-}--bool MtpDrafter::setup_coupled(std::string& err) {- const int64_t nv = dhead_ != nullptr ? n_dvocab_ : n_vocab_;- const size_t scratch = strata::kernels::coupled_draft_scratch_bytes((int) nv);- if (scratch == 0) {- std::fprintf(stderr, "strata mtp: STRATA_SPEC_COUPLED: %lld draft logits are too wide for the coupled sampler; "- "argmax drafts\n", (long long) nv);- return true;- }- const size_t ring = (size_t) (kCoupledHistCap + max_t_) * sizeof(int32_t);- if (cudaMalloc((void**) &cparams_, sizeof(strata::kernels::SamplerParams)) != cudaSuccess ||- cudaMalloc((void**) &cring_, ring) != cudaSuccess || cudaMalloc(&cscratch_, scratch) != cudaSuccess ||- !mapped(sizeof(strata::kernels::SamplerParams), (void**) &h_cparams_, (void**) &m_cparams_) ||- !mapped((size_t) kCoupledHistCap * sizeof(int32_t), (void**) &h_chist_, (void**) &m_chist_)) {- err = "mtp: the coupled draft sampler's buffers do not fit";- return false;- }- cudaMemset(cring_, 0xff, ring); // -1: no token- std::fill(h_chist_, h_chist_ + kCoupledHistCap, -1);- vram_ += sizeof(strata::kernels::SamplerParams) + ring + scratch;- if (dhead_ != nullptr) { // token id -> subset index (-1: not in the draft head), for the penalties- std::vector<int32_t> sub((size_t) n_dvocab_), inv((size_t) n_vocab_, -1);- cudaMemcpy(sub.data(), dvocab_, sub.size() * sizeof(int32_t), cudaMemcpyDeviceToHost);- for (size_t i = 0; i < sub.size(); ++i)- if (sub[i] >= 0 && sub[i] < n_vocab_ && inv[(size_t) sub[i]] < 0) inv[(size_t) sub[i]] = (int32_t) i;- if (cudaMalloc((void**) &dinv_, inv.size() * sizeof(int32_t)) != cudaSuccess) {- err = "mtp: the coupled draft sampler's token map does not fit";- return false;- }- cudaMemcpy(dinv_, inv.data(), inv.size() * sizeof(int32_t), cudaMemcpyHostToDevice);- vram_ += inv.size() * sizeof(int32_t);- }- if (cudaDeviceSynchronize() != cudaSuccess) {- err = std::string("mtp: coupled setup: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- coupled_ok_ = true;- std::fprintf(stderr, "strata mtp: coupled draft sampling on (STRATA_SPEC_COUPLED): sampled requests draft with the "- "target's chain and Philox draw over %lld tokens\n", (long long) nv);- return true;-}--void MtpDrafter::set_draft_sampling(const strata::kernels::SamplerParams& sp) {- coupled_active_ = coupled_ok_ && !sp.greedy && sp.temperature > 0.0f;- if (!coupled_active_) return;- *h_cparams_ = sp; // read by the next round graph (after the previous one has synced)- std::fill(h_chist_, h_chist_ + kCoupledHistCap, -1);-}--void MtpDrafter::set_draft_history(const int32_t* tail, int64_t n_tail, int32_t next) {- if (!coupled_active_) return;- const int h = coupled_hist_len(h_cparams_->penalty_last_n, kCoupledHistCap);- coupled_hist_base(tail, n_tail, next, h, h_chist_ + (kCoupledHistCap - h));-}--namespace {-// #474: what to change when the draft head's token subset does not fit the VRAM left - a smaller subset that setup-// ships (data/draft_vocab_*.bin; their token counts below), and how to pick it. The start used to stop at "the draft-// head does not fit" with no hint. Text only, after the failure: nothing changes for a start that fits.-void draft_head_hint(int64_t n_tokens, int64_t row_bytes) {- size_t free_b = 0, total_b = 0;- const bool have_free = cudaMemGetInfo(&free_b, &total_b) == cudaSuccess;- auto mib = [&](int64_t n) { return (double) (n * row_bytes) / 1048576.0; };- std::string free_s;- if (have_free) {- char b[64];- std::snprintf(b, sizeof(b), " and %.0f MiB is free", (double) free_b / 1048576.0);- free_s = b;- }- std::fprintf(stderr, "strata mtp: the draft head over %lld tokens needs %.0f MiB of VRAM%s.\n",- (long long) n_tokens, mib(n_tokens), free_s.c_str());- struct Subset { const char* name; int64_t tokens; const char* what; };- static const Subset smaller[] = {{"cyrillic", 58963, "English, code and the Cyrillic script"},- {"en", 40525, "English and code"}};- std::string opts;- for (const Subset& s : smaller)- if (s.tokens < n_tokens) {- char b[160];- std::snprintf(b, sizeof(b), "%s--draft-vocab %s (%s, ~%.0f MiB)", opts.empty() ? "" : " or ", s.name,- s.what, mib(s.tokens));- opts += b;- }- if (!opts.empty())- std::fprintf(stderr, "strata mtp: hint: a smaller draft vocabulary needs less VRAM: %s. Start once with it - "- "START-HERE.bat --draft-vocab en (Windows) or ./setup.sh --draft-vocab en - and the model "- "keeps it (\"draft_vocab\" in its strata-*.json config); or a smaller --context in "- "setup.\n",- opts.c_str());- else- std::fprintf(stderr, "strata mtp: hint: this is already the smallest shipped draft vocabulary: a smaller "- "--context (or closing what else uses the GPU) leaves it room.\n");-}-} // namespace--bool MtpDrafter::bind(const WeightTable& wt, const NativeHead* head, const float* window_R, std::string& err,- const MtpDrafter* shared) {- const OnDevice on_device(device_);- wt_ = &wt;- head_ = head;- window_R_ = window_R;- const WeightRef* wo = wt.find("output.weight");- if (!wo) { err = "mtp: output.weight is missing"; return false; }- n_vocab_ = wo->ne1;- if (head == nullptr || !head->loaded()) { err = "mtp: the draft layer needs the native head (--native)"; return false; }- if (head_logits_ == nullptr &&- cudaMalloc((void**) &head_logits_, (size_t) max_t_ * (size_t) n_vocab_ * sizeof(float)) != cudaSuccess) {- err = "mtp: the draft logits do not fit";- return false;- }- if (shared != nullptr) {- if (shared->head_ != head || shared->device_ != device_ ||- shared->n_vocab_ != n_vocab_) {- err = "mtp: incompatible shared draft head";- return false;- }- dhead_ = shared->dhead_;- dvocab_ = shared->dvocab_;- n_dvocab_ = shared->n_dvocab_;- owns_draft_head_ = false;- }- // the draft head's token subset, when tools/draft_vocab.py wrote one- if (dhead_ == nullptr && shared == nullptr && !full_head_env()) {- std::vector<uint8_t> raw;- if (read_file(vocab_file(), raw) && raw.size() >= 4 && raw.size() % 4 == 0) {- n_dvocab_ = (int64_t) (raw.size() / 4);- const int64_t row_bytes = (int64_t) head->row_bytes(); // a vocabulary row of the native head- if (cudaMalloc((void**) &dvocab_, raw.size()) != cudaSuccess ||- cudaMalloc((void**) &dhead_, (size_t) (n_dvocab_ * row_bytes)) != cudaSuccess) {- err = "mtp: the draft head does not fit";- draft_head_hint(n_dvocab_, row_bytes);- return false;- }- cudaMemcpy(dvocab_, raw.data(), raw.size(), cudaMemcpyHostToDevice);- dvocab_host_.resize((size_t) n_dvocab_);- std::memcpy(dvocab_host_.data(), raw.data(), raw.size());- strata::kernels::gather_rows((const uint8_t*) head->weights(), row_bytes, dvocab_, n_dvocab_, dhead_, nullptr);- cudaDeviceSynchronize();- vram_ += (uint64_t) (n_dvocab_ * row_bytes) + raw.size();- std::fprintf(stderr, "strata mtp: draft head over %lld tokens (%.1f MiB)\n", (long long) n_dvocab_,- (double) (n_dvocab_ * row_bytes) / 1048576.0);- dhead_type_ = head->type();- if (q4_head_ && !make_q4_head(err)) return false;- if (dhead_type_ == 14 && strata::kernels::native_q6_k_packed_enabled()) // STRATA_Q6_PACKED=1- strata::kernels::native_q6_k_pack(dhead_, (int) (row_bytes / 210) * 256, (int) n_dvocab_,- "MTP draft head");- }- }- if (coupled_draft_env() && cparams_ == nullptr && !setup_coupled(err)) return false;- return true;-}--// The layer for T rows. full = false stops after the K/V append (the prompt only needs the cache).-bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::string& err) {- // step_row0 >= 0: the full layer on step rows [step_row0, +T); step_row0 < 0: K/V only on rows [-1 - step_row0, +T)- const bool full = step_row0 >= 0;- if (full && T != 1) { err = "mtp: the full layer runs one row at a time (its attention scratch is sized for one)"; return false; }- if (!record_front(T, full ? step_row0 : -1 - step_row0, cs, err)) return false;- return !full || record_rest(step_row0, cs, err);-}--bool MtpDrafter::record_front(int T, int row0, cudaStream_t cs, std::string& err) {- using namespace strata::kernels;- const ModelGeometry& g = *g_;- const int64_t N = g.n_embd, HC = g.hc, NH = g.n_head, HD = g.head_dim, NKV = g.n_head_kv;- const QsaShapes s = shapes_of(g);- const int32_t* step = step_ + row0 * 4;- const int32_t* pos = pos_ + row0 * NH;- // --mtp-q4: each projection's format and weights for this pass (Q8_0 unless the Q4_0 copies exist)- int wt_fc_embedding, wt_fc_hidden, wt_k_proj, wt_v_proj;- const void* wp_fc_embedding = wq("fc_embedding.weight", wt_fc_embedding);- const void* wp_fc_hidden = wq("fc_hidden.weight", wt_fc_hidden);- const void* wp_k_proj = wq("self_attn.k_proj.weight", wt_k_proj);- const void* wp_v_proj = wq("self_attn.v_proj.weight", wt_v_proj);- try {- // ---- the two input branches- const WeightRef* we = wt_->find("token_embd.weight");- if (!we) { err = "mtp: token_embd.weight is missing"; return false; }- if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table- ne->gather_dev(tok_, T, emb_, cs);- } else {- const auto* codes = (const uint8_t*) we->data;- const auto* scales = (const float*) (codes + we->codes_bytes);- const auto* offsets = we->has_offset ? (const float*) (codes + we->codes_bytes + we->scales_bytes) : nullptr;- embedding_gather_dev(codes, scales, offsets, tok_, T, we->ne0, we->code_bits, we->code_bias, we->group_elems,- (uint64_t) (we->ne0 / (8 / we->code_bits)), (uint64_t) (we->ne0 / we->group_elems), emb_, cs);- }- native_qsa_rms_norm_weighted(emb_, f32("pre_fc_norm_embedding.weight"), en_, (int) N, T, EPS, cs);- native_quantize_q8_1(en_, xq_, (int) N, T, cs);- native_mmvq(wt_fc_embedding, wp_fc_embedding, xq_, e2_, (int) N, (int) N, T, cs);- if (hnorm_stream_) // --mtp-hnorm stream: one RMS per stream, each scaled by its slice of the weight- native_qsa_rms_norm_grouped(Rin_, f32("pre_fc_norm_hidden.weight"), hn_, (int) N, (int) HC, (int) (T * HC), EPS, cs);- else- native_qsa_rms_norm_weighted(Rin_, f32("pre_fc_norm_hidden.weight"), hn_, (int) (HC * N), T, EPS, cs);- for (int c0 = 0; c0 < T * HC; c0 += 8) {- const int nc = (int) std::min<int64_t>(8, T * HC - c0);- native_quantize_q8_1(hn_ + (size_t) c0 * N, xq_, (int) N, nc, cs);- native_mmvq(wt_fc_hidden, wp_fc_hidden, xq_, h2_ + (size_t) c0 * N, (int) N, (int) N, nc, cs);- }- add_streams_broadcast(h2_, e2_, R_, N, (int) HC, T, cs);- // ---- the attention hyper-connection- {- FusedGrArgs fa[kFusedGrMaxT];- for (int t = 0; t < T; ++t) {- fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = false;- fa[t].w_norm = f32("attn_hyper_connection.hc_norm.weight");- fa[t].w_down = bf16("attn_hyper_connection.input_mix_weight_down.weight");- fa[t].w_up = bf16("attn_hyper_connection.input_mix_weight_up.weight");- fa[t].w_inject = bf16("attn_hyper_connection.block_inject_weight.weight");- fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC;- fa[t].inject_out = inj_ + t * HC; fa[t].mixed = mixed_ + t * N;- }- fused_gr_read_multi(fa, T, xn_, cs);- }- // ---- attention: K/V into the layer's own cache- native_quantize_q8_1(mixed_, xq_, (int) N, T, cs);- native_mmvq(wt_k_proj, wp_k_proj, xq_, kcur_, (int) N, (int) (NKV * HD), T, cs);- native_mmvq(wt_v_proj, wp_v_proj, xq_, vcur_, (int) N, (int) (NKV * HD), T, cs);- // #783 PR-d (stuchapin909): the T tokens' K norm, K/V rotation and K/V append each run once over all T rows- // (every row is independent; the rope keeps its per-token positions). STRATA_NO_BATCH_KV_STEP=1 appends per token.- static const bool no_batch_kv = [] {- const char* v = std::getenv("STRATA_NO_BATCH_KV_STEP");- return v != nullptr && v[0] != '\0' && v[0] != '0';- }();- // #783 PR-f (stuchapin909): the per-head RMSNorm and the rope fused (bit-identical to the pair, rope_parity- // check 6; STRATA_NO_NORM_ROPE=1 keeps the two; off on HIP until its parity check passes). K rows of one token- // are consecutive in `pos`, tokens are NH apart, so a one-token window is the fusable case.- const bool fuse_nr = native_rope_enabled() && native_norm_rope_usable((int) HD, (int) s.n_rot);- if (T == 1 && fuse_nr) {- native_qsa_rms_norm_rope(kcur_, (int) HD, f32("self_attn.k_norm.weight"), kcur_, (int) NKV, (int) HD,- (int) s.n_rot, EPS, rope_scaling(), pos, cs);- } else {- native_qsa_rms_norm_weighted(kcur_, f32("self_attn.k_norm.weight"), kcur_, (int) HD, (int) (T * NKV), EPS, cs);- for (int t = 0; t < T; ++t) {- float* kc = kcur_ + t * NKV * HD;- if (native_rope_enabled()) native_rope_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, rope_scaling(), pos + t * NH, cs);- else rope_neox_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, st_.cos_tab, st_.sin_tab, pos + t * NH, cs);- }- }- if (st_.kv_rot) { // rotated K and V (kv_q4.hpp): Q4_0, and INT8 with STRATA_KV_ROT=1- fwht256_inplace_cuda(kcur_, (int64_t) T * NKV, cs);- fwht256_inplace_cuda(vcur_, (int64_t) T * NKV, cs);- }- // stored in the state's own format (#293 appended rotated INT8 K/V as Q4_0, into pools INT8 never has)- if (!no_batch_kv && st_.kv_q4)- kv_append_q4_steps(st_.k_q4, st_.v_q4, st_.page_table, step, 4, T, kcur_, vcur_, s, cs, &st_.host);- else if (!no_batch_kv && st_.kv_int8)- kv_append_q8_steps(st_.k_q, st_.v_q, st_.k_scale, st_.v_scale, st_.page_table, step, 4, kcur_, vcur_,- (int) (NKV * HD), T, s, cs, &st_.host);- else- for (int t = 0; t < T; ++t) {- if (st_.kv_q4)- kv_append_q4_step(st_.k_q4, st_.v_q4, st_.page_table, step + t * 4, kcur_ + t * NKV * HD,- vcur_ + t * NKV * HD, s, cs, &st_.host);- else if (st_.kv_int8)- kv_append_q8_step(st_.k_q, st_.v_q, st_.k_scale, st_.v_scale, st_.page_table, step + t * 4,- kcur_ + t * NKV * HD, vcur_ + t * NKV * HD, s, cs, &st_.host);- else- kv_append_step(st_.k_pool, st_.v_pool, st_.page_table, step + t * 4, kcur_ + t * NKV * HD,- vcur_ + t * NKV * HD, s, cs, &st_.host);- }- } catch (const std::exception& e) {- err = std::string("mtp: ") + e.what();- return false;- }- return true;-}--void MtpDrafter::norm_rope(float* data, const float* gamma, int rows, int cols, const int32_t* p, cudaStream_t cs) {- using namespace strata::kernels;- const QsaShapes s = shapes_of(*g_);- native_qsa_rms_norm_weighted(data, gamma, data, cols, rows, EPS, cs);- if (native_rope_enabled()) native_rope_apply(data, data, rows, cols, (int) s.n_rot, rope_scaling(), p, cs);- else rope_neox_apply(data, data, rows, cols, (int) s.n_rot, st_.cos_tab, st_.sin_tab, p, cs);-}--bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) {- using namespace strata::kernels;- const ModelGeometry& g = *g_;- SessionState& ss = *ss_;- const int T = 1;- const int64_t N = g.n_embd, HC = g.hc, K = ss.k, NH = g.n_head, HD = g.head_dim;- const QsaShapes s = shapes_of(g);- const GrShapes gs{g.n_embd, g.hc, g.hc_lr};- const int32_t* step = step_ + step_row * 4;- const int32_t* pos = pos_ + step_row * NH;- // --mtp-q4: the q and o projections' format and weights (Q8_0 unless the Q4_0 copies exist)- int wt_q_proj, wt_o_proj;- const void* wp_q_proj = wq("self_attn.q_proj.weight", wt_q_proj);- const void* wp_o_proj = wq("self_attn.o_proj.weight", wt_o_proj);- const bool fuse_nr = native_rope_enabled() && native_norm_rope_usable((int) HD, (int) s.n_rot); // #783 PR-f- try {- // ---- dense attention over every cell- native_mmvq(wt_q_proj, wp_q_proj, xq_, qfull_, (int) N, (int) (NH * 2 * HD), T, cs);- // all T tokens' q rows at once (the rows of token t sit at pos[t * NH ..], so row r reads pos[r])- if (fuse_nr) {- native_qsa_rms_norm_rope(qfull_, (int) (2 * HD), f32("self_attn.q_norm.weight"), qcur_, (int) (T * NH),- (int) HD, (int) s.n_rot, EPS, rope_scaling(), pos, cs);- } else {- if (cudaMemcpy2DAsync(qcur_, (size_t) HD * 4, qfull_, (size_t) HD * 2 * 4, (size_t) HD * 4,- (size_t) (T * NH), cudaMemcpyDeviceToDevice, cs) != cudaSuccess) {- err = "mtp: q split failed";- return false;- }- norm_rope(qcur_, f32("self_attn.q_norm.weight"), (int) (T * NH), (int) HD, pos, cs);- }- if (st_.kv_rot) fwht256_inplace_cuda(qcur_, (int64_t) T * NH, cs);- const QsaAttnPools pools = qsa_attn_pools(st_);- if (window_ > 0) window_ids(const_cast<int32_t*>(step), T, (int) window_, ident_, cap_, cs);- qsa_decode_attn_batch(qcur_, pools, ident_, step, cap_, s, attn_scratch_, attn_, T, cs);- if (st_.kv_rot) fwht256_inplace_cuda(attn_, (int64_t) T * NH, cs);- native_qsa_gate_apply(attn_, qfull_, attn32_, (int) (T * NH), (int) HD, cs);- native_quantize_q8_1(attn32_, xq_, (int) (NH * HD), T, cs);- native_mmvq(wt_o_proj, wp_o_proj, xq_, bo_, (int) (NH * HD), (int) N, T, cs);- // ---- the MLP hyper-connection (the attention write folded in)- {- FusedGrArgs fa[kFusedGrMaxT];- for (int t = 0; t < T; ++t) {- fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = true;- fa[t].bo_prev = bo_ + t * N; fa[t].inj_prev = inj_ + t * HC;- fa[t].w_norm = f32("mlp_hyper_connection.hc_norm.weight");- fa[t].w_down = bf16("mlp_hyper_connection.input_mix_weight_down.weight");- fa[t].w_up = bf16("mlp_hyper_connection.input_mix_weight_up.weight");- fa[t].w_inject = bf16("mlp_hyper_connection.block_inject_weight.weight");- fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC;- fa[t].inject_out = inj2_ + t * HC; fa[t].mixed = mixed_ + t * N;- }- fused_gr_read_multi(fa, T, xn_, cs);- }- // ---- MoE: router, the 512 resident experts, the shared expert (on a branch beside them on CUDA), the- // combine, the write- NativeSharedWeights nsw;- nsw.gate_data = wq("mlp.shared_expert.gate_proj.weight", nsw.gate_type);- nsw.up_data = wq("mlp.shared_expert.up_proj.weight", nsw.up_type);- nsw.down_data = wq("mlp.shared_expert.down_proj.weight", nsw.down_type);- nsw.q8_1 = xq_; // the routed experts read hit_xq_- const SForm none{};- const bool need_bf16_x = !shared_expert_native_bf16_enabled();- static const bool fuse_head_gr = [] {- const char* v = std::getenv("STRATA_FUSE_HEAD_GR");- return v != nullptr && std::atoi(v) != 0;- }();- static const bool head_mix_multi_on = [] {-#if defined(STRATA_USE_HIP)- return false;-#else- const char* v = std::getenv("STRATA_HEAD_MIX_MULTI");- return v == nullptr || std::atoi(v) != 0;-#endif- }();- // the shared expert only reads mixed_ / xq_ and writes shared_: a branch beside the router and the routed- // experts, joined before the combine (CUDA; HIP keeps one stream: STRATA_MTP_SHARED_BRANCH=0 does too)- static const bool branch_on = [] {-#if defined(STRATA_USE_HIP)- return false;-#else- const char* v = std::getenv("STRATA_MTP_SHARED_BRANCH");- return v == nullptr || std::atoi(v) != 0;-#endif- }();- cudaStream_t sh_cs = cs;- if (branch_on) {- if (cudaEventRecord(sh_fork_, cs) != cudaSuccess || cudaStreamWaitEvent(side_, sh_fork_, 0) != cudaSuccess) {- err = "mtp: the shared expert's branch";- return false;- }- sh_cs = side_;- }- auto shared_rows = [&]() {- for (int t = 0; t < T; ++t) {- if (need_bf16_x) f32_to_bf16_bulk(mixed_ + t * N, x_bf16_, N, sh_cs);- shared_expert(nullptr, nullptr, x_bf16_, none, nullptr, nullptr, nullptr, none, nullptr, nullptr, nullptr,- none, nullptr, nullptr, nullptr, bf16("mlp.shared_expert_gate.weight"), sh_scratch_,- shared_ + t * N, N, g.n_ff, 32, sh_cs, mixed_ + t * N, &nsw);- }- };- if (branch_on) {- shared_rows();- if (cudaEventRecord(sh_join_, side_) != cudaSuccess) { err = "mtp: the shared expert's branch"; return false; }- }- for (int t = 0; t < T; ++t) {- bf16_gemv_fp32_mmvf(mixed_ + t * N, bf16("mlp.gate.weight"), logits_ + t * g.n_expert, (int) N, (int) g.n_expert, cs);- if (native_router_enabled()) native_router_top10(logits_ + t * g.n_expert, ids_ + t * K, w_ + t * K, cs);- else router_top10(logits_ + t * g.n_expert, 1, (int) g.n_expert, (int) K, ids_ + t * K, w_ + t * K, cs);- }- moe_group_resident(ids_, (int) (T * K), (int) K, experts_, (int64_t) strata::kernels::cpu::BLOB, grp_ptr_,- grp_start_, grp_counts_, hit_dst_, hit_slot_, cs);- quantize_q8_0_scaled(mixed_, hit_xq_, hit_xs_, (int64_t) T * N, cs);- moe_grouped_s2(grp_ptr_, grp_start_, grp_counts_, hit_dst_, hit_slot_, (int64_t) T * K, (int64_t) T * K, hit_xq_,- hit_xs_, hit_scratch_, parts_, cs);- if (branch_on) {- if (cudaStreamWaitEvent(cs, sh_join_, 0) != cudaSuccess) { err = "mtp: the shared expert's join"; return false; }- } else {- shared_rows();- }- for (int t = 0; t < T; ++t) {- if (native_moe_combine_enabled())- native_moe_combine(parts_ + (size_t) t * K * N, w_ + t * K, shared_ + t * N, y_ + t * N, N, K, cs);- else- moe_combine(parts_ + (size_t) t * K * N, w_ + t * K, shared_ + t * N, y_ + t * N, N, K, cs);- if (!fuse_head_gr)- gr_write(R_ + (size_t) t * HC * N, y_ + t * N, inj2_ + t * HC, gs, R_ + (size_t) t * HC * N, cs);- }- // ---- the final mixer and the main model's head- if (fuse_head_gr) {- FusedGrArgs fa[kFusedGrMaxT];- for (int t = 0; t < T; ++t) {- fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = true;- fa[t].bo_prev = y_ + t * N; fa[t].inj_prev = inj2_ + t * HC;- fa[t].w_norm = f32("hyper_connection_mixer.hc_norm.weight");- fa[t].w_down = bf16("hyper_connection_mixer.input_mix_weight_down.weight");- fa[t].w_up = bf16("hyper_connection_mixer.input_mix_weight_up.weight");- fa[t].w_inject = nullptr;- fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC;- fa[t].inject_out = dummy_inj_; fa[t].mixed = sample_ + t * N;- }- fused_gr_read_multi(fa, T, xn_, cs);- } else if (head_mix_multi_on) {- // the window's rows in one read (CUDA; HIP keeps gr_read per row): bitwise the same sums- FusedGrArgs fa[kFusedGrMaxT];- for (int t = 0; t < T; ++t) {- fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = false;- fa[t].w_norm = f32("hyper_connection_mixer.hc_norm.weight");- fa[t].w_down = bf16("hyper_connection_mixer.input_mix_weight_down.weight");- fa[t].w_up = bf16("hyper_connection_mixer.input_mix_weight_up.weight");- fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC; fa[t].mixed = sample_ + t * N;- }- fused_gr_read_multi(fa, T, xn_, cs);- } else {- for (int t = 0; t < T; ++t)- gr_read(R_ + (size_t) t * HC * N, f32("hyper_connection_mixer.hc_norm.weight"),- bf16("hyper_connection_mixer.input_mix_weight_down.weight"),- bf16("hyper_connection_mixer.input_mix_weight_up.weight"), nullptr, EPS, gs, ss.block.gr,- sample_ + t * N, dummy_inj_, cs);- }- native_quantize_q8_1(sample_, xq_, (int) N, T, cs);- const bool sub = dhead_ != nullptr;- const int64_t nv = sub ? n_dvocab_ : n_vocab_;- native_mmvq(sub ? dhead_type_ : head_->type(), sub ? dhead_ : head_->weights(), xq_, head_logits_, (int) N,- (int) nv, T, cs);- if (coupled_rec_) {- // coupled draft sampling: the target's chain and Philox draw for the row that will verify this draft- // (counter = this cell + 1, from its step record), penalties over the ring; T == 1 (full layer)- coupled_draft_sample(head_logits_, (int) nv, sub ? dvocab_ : nullptr, sub ? dinv_ : nullptr, (int) n_vocab_,- cparams_, cring_, kCoupledHistCap, coupled_j_, step, cscratch_, out_ids_, probs_, cs);- return true;- }- if (argmax_rows_wanted()) {- argmax_rows(head_logits_, T, (int) nv, arg_scratch_, out_ids_, cs);- } else {- SamplerParams sp;- sp.greedy = true;- sp.temperature = 0.0f;- sample_tokens(head_logits_, T, (int) nv, nullptr, 0, sp, out_ids_, cs);- }- if (multi_block_head_ops()) row_top_prob_split(head_logits_, T, (int) nv, out_ids_, probs_, top_scratch_, cs);- else row_top_prob(head_logits_, T, (int) nv, out_ids_, probs_, cs);- if (sub) map_ids(out_ids_, dvocab_, T, cs);- } catch (const std::exception& e) {- err = std::string("mtp: ") + e.what();- return false;- }- return true;-}--namespace {-bool finish_capture(cudaStream_t cs, bool ok, cudaGraphExec_t& exec, const char* what, std::string& err) {- cudaGraph_t graph = nullptr;- const cudaError_t ce = cudaStreamEndCapture(cs, &graph);- if (!ok) {- if (graph) cudaGraphDestroy(graph);- return false;- }- if (ce != cudaSuccess || cudaGraphInstantiate(&exec, graph, 0) != cudaSuccess) {- if (graph) cudaGraphDestroy(graph);- err = std::string("mtp: ") + what + " capture: " + cudaGetErrorString(ce);- return false;- }- cudaGraphDestroy(graph);- // an explicit upload: the first launch's implicit one blocked behind a device-side spin (verify.cpp)- cudaGraphUpload(exec, cs);- cudaStreamSynchronize(cs);- return true;-}-} // namespace--bool MtpDrafter::capture_prefill(int T, std::string& err) {- if (prefill_exec_[T]) return true;- using namespace strata::kernels;- if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }- copy_i32_from_mapped(tok_, m_tok_, T, cs_);- copy_i32_from_mapped(step_, m_step_, (int64_t) T * 4, cs_);- copy_i32_from_mapped(pos_, m_pos_, (int64_t) T * g_->n_head, cs_);- const bool ok = record_forward(T, -1, cs_, err); // K/V only, rows [0, T)- return finish_capture(cs_, ok, prefill_exec_[T], "prefill", err);-}--bool MtpDrafter::capture_prefill_dev(int T, std::string& err) {- if (prefill_dev_exec_[T]) return true;- if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }- const bool ok = record_forward(T, -1, cs_, err); // K/V only, rows [0, T); tok_/step_/pos_ filled before launch- return finish_capture(cs_, ok, prefill_dev_exec_[T], "prefill (device inputs)", err);-}--bool MtpDrafter::capture_round(int T, bool coupled, std::string& err) {- cudaGraphExec_t& exec = coupled ? round_exec_c_[T] : round_exec_[T];- if (exec) return true;- using namespace strata::kernels;- const int64_t HCN = g_->hc * g_->n_embd;- if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }- bool ok = true;- // coupled: the request's chain and the penalty history's base, for this round's drafts- if (coupled) coupled_draft_stage(m_cparams_, m_chist_, cparams_, cring_, kCoupledHistCap, cs_);- // the catch-up: the layer's front for the window's T cells (their K/V), then its rest for row a only, on row- // a's intermediates copied to row 0, at the cell the host staged in step row 2*max_t - 1; the draft chain is- // one graph per step (`capture_step`) so the host can stop it when a draft is unlikely- const int ra = 2 * max_t_ - 1;- const int64_t N = g_->n_embd, NH = g_->n_head;-#if defined(STRATA_USE_HIP) // AMD keeps its separate copies- copy_i32_from_mapped(tok_, m_tok_, T, cs_);- copy_i32_from_mapped(step_, m_step_, (int64_t) 2 * T * 4, cs_);- copy_i32_from_mapped(pos_, m_pos_, (int64_t) 2 * T * NH, cs_);- copy_i32_from_mapped(row_, m_row_, 2, cs_);- copy_from_mapped(Rin_, window_R_, (int64_t) T * HCN, cs_);- copy_i32_from_mapped(step_ + ra * 4, m_step_ + ra * 4, 4, cs_);- copy_i32_from_mapped(pos_ + ra * NH, m_pos_ + ra * NH, NH, cs_);-#else // the draft round's seven inputs in one launch- const MappedCopy in[7] = {{tok_, m_tok_, T}, {step_, m_step_, (int64_t) 2 * T * 4}, {pos_, m_pos_, 2 * T * NH},- {row_, m_row_, 2}, {Rin_, window_R_, T * HCN}, {step_ + ra * 4, m_step_ + ra * 4, 4},- {pos_ + ra * NH, m_pos_ + ra * NH, NH}};- copy_from_mapped_multi(in, 7, cs_);-#endif- ok = record_front(T, 0, cs_, err);- if (ok) {- if (T > 1) {- copy_row_to_first(row_, R_, HCN, inj_, g_->hc, mixed_, N, cs_);- native_quantize_q8_1(mixed_, xq_, (int) N, 1, cs_);- }- coupled_rec_ = coupled;- coupled_j_ = 0;- ok = record_rest(ra, cs_, err);- coupled_rec_ = false;- }- if (ok) mtp_select(R_, HCN, out_ids_, row_ + 1, Rin_, tok_, m_out_, 0, cs_, probs_, m_prob_);- if (ok && force_on_ && !coupled) force_token(tok_, m_force_, 0, cs_); // chain_launch: step 1's input- return finish_capture(cs_, ok, exec, coupled ? "round (coupled)" : "round", err);-}--// Chain step j (1..max_t-2): one row at the cell staged in step row `max_t + j - 1`, from the previous step's-// residual and token (left in Rin_[0] / tok_[0] by mtp_select); draft j and its probability to the mapped outputs.-bool MtpDrafter::capture_step(int j, bool coupled, std::string& err) {- cudaGraphExec_t& exec = coupled ? step_exec_c_[j] : step_exec_[j];- if (exec) return true;- using namespace strata::kernels;- const int64_t HCN = g_->hc * g_->n_embd;- const int row = max_t_ + j - 1;- if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }-#if defined(STRATA_USE_HIP)- copy_i32_from_mapped(step_ + row * 4, m_step_ + row * 4, 4, cs_);- copy_i32_from_mapped(pos_ + row * g_->n_head, m_pos_ + row * g_->n_head, g_->n_head, cs_);-#else- const MappedCopy in[2] = {{step_ + row * 4, m_step_ + row * 4, 4},- {pos_ + row * g_->n_head, m_pos_ + row * g_->n_head, g_->n_head}};- copy_from_mapped_multi(in, 2, cs_);-#endif- coupled_rec_ = coupled;- coupled_j_ = j;- bool ok = record_forward(1, row, cs_, err);- coupled_rec_ = false;- if (ok) mtp_select(R_, HCN, out_ids_, row_ + 1, Rin_, tok_, m_out_, j, cs_, probs_, m_prob_);- if (ok && force_on_ && !coupled) force_token(tok_, m_force_, j, cs_); // chain_launch: step j+1's input- return finish_capture(cs_, ok, exec, coupled ? "step (coupled)" : "step", err);-}--void MtpDrafter::kv_restore(int64_t upto) {- const OnDevice on_device(device_);- if (st_.kv_mode != 2 || upto <= 0) return;- // the ring's blocks below `upto`, from the host copy: a checkpoint resume may have left later cells in them- const strata::kernels::QsaShapes s = shapes_of(*g_);- const int64_t b1 = (upto + s.page_size - 1) / s.page_size, b0 = std::max<int64_t>(0, b1 - st_.n_slots);- strata::kernels::kv_ring_restore(qsa_attn_pools(st_), st_.host, qsa_kv_format(st_), b0, b1, st_.n_slots, s, cs_);- cudaStreamSynchronize(cs_);-}--bool MtpDrafter::prepare_prefill(std::string& err) {- const OnDevice on_device(device_);- const int64_t per_row = 1 + 4 + g_->n_head;- if (pf_cap_ < (int64_t) max_t_ * per_row) {- if (pf_dev_) cudaFree(pf_dev_);- pf_dev_ = nullptr;- pf_cap_ = 0;- if (cudaMalloc((void**) &pf_dev_, (size_t) (max_t_ * per_row) * sizeof(int32_t)) != cudaSuccess) {- err = "mtp prefill: the input records do not fit";- return false;- }- pf_cap_ = max_t_ * per_row;- }- for (int T = 1; T <= max_t_; ++T)- if (!capture_prefill_dev(T, err)) return false; // (STRATA_MTP_PREFILL_SYNC's loop syncs anyway)- return true;-}--bool MtpDrafter::prefill(const float* R_rows, const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err,- bool sync) {- const OnDevice on_device(device_);- const Clock::time_point t0 = Clock::now();- const int64_t HCN = g_->hc * g_->n_embd;- // cells the window can never reach again need no K/V- const int64_t first_needed = (window_ > 0 && prompt_len_ > 0) ? prompt_len_ - window_ - 64 : 0;- // E-4: every group's token / step / position records uploaded at once; each group is then device copies and a- // graph on the one stream, with a single sync at the end (a group of <= max_t rows used to be staged in mapped- // memory and synced before the next: ~5,500 host round trips on a 32K prompt). The same work in the same- // order. STRATA_MTP_PREFILL_SYNC=1 keeps the old loop, =0 forces E-4.- // HIP defaults to the old loop. This pass runs whenever E-9 (Prefill::draft_kv) declines, which it does for the- // drafter's ring (KV streaming, --kv-resident), and on gfx1201 / ROCm 7.2.4 the E-4 queue (a graph launch and four- // device copies per group, ~2,000 groups per 8192-token chunk, no sync) sometimes never completes: the prompt hangs- // in hipGraphLaunch or the final sync until the watchdog ends the engine. R9700, full IQ3_XXS, --kv-resident- // 32768, mixed load (chats, 32K and 7K prompts, deep follow-ups): E-4 hung in the first round on 2 of 2 tries,- // the old loop ran 30 of 30 rounds clean, prompt speed unchanged.- static const bool per_group_sync = [] {- if (const char* v = std::getenv("STRATA_MTP_PREFILL_SYNC")) return std::atoi(v) != 0;-#if defined(STRATA_USE_HIP)- return true;-#else- return false;-#endif- }();- const int64_t NHp = g_->n_head, per_row = 1 + 4 + NHp;- if (!per_group_sync && n > 0) {- if (pf_cap_ < n * per_row) {- if (pf_dev_) cudaFree(pf_dev_);- pf_dev_ = nullptr;- pf_cap_ = 0;- if (cudaMalloc((void**) &pf_dev_, (size_t) (n * per_row) * sizeof(int32_t)) != cudaSuccess) {- err = "mtp prefill: the input records do not fit";- return false;- }- pf_cap_ = n * per_row;- }- std::vector<int32_t> rec((size_t) (n * per_row));- int32_t* tk = rec.data();- int32_t* stp = tk + n;- int32_t* ps = stp + 4 * n;- for (int64_t i = 0; i < n; ++i) {- const int64_t cell = cell0 + i;- tk[i] = next_tokens[i];- stp[i * 4 + 0] = (int32_t) cell;- stp[i * 4 + 1] = (int32_t) (cell + 1);- stp[i * 4 + 2] = (int32_t) ((cell + 1) / 4);- stp[i * 4 + 3] = (int32_t) (cell + 1);- for (int64_t h = 0; h < NHp; ++h) ps[i * NHp + h] = (int32_t) cell;- }- if (cudaMemcpyAsync(pf_dev_, rec.data(), rec.size() * sizeof(int32_t), cudaMemcpyHostToDevice, cs_) != cudaSuccess) {- err = "mtp prefill: the input records upload failed";- return false;- }- const int32_t* d_tk = pf_dev_;- const int32_t* d_stp = d_tk + n;- const int32_t* d_ps = d_stp + 4 * n;- for (int64_t c = 0; c < n; c += max_t_) {- const int T = (int) std::min<int64_t>(max_t_, n - c);- if (cell0 + c + T <= first_needed) continue;- if (!capture_prefill_dev(T, err)) return false;- if (cudaMemcpyAsync(tok_, d_tk + c, (size_t) T * 4, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||- cudaMemcpyAsync(step_, d_stp + c * 4, (size_t) T * 16, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||- cudaMemcpyAsync(pos_, d_ps + c * NHp, (size_t) (T * NHp) * 4, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||- cudaMemcpyAsync(Rin_, R_rows + (size_t) c * HCN, (size_t) T * HCN * sizeof(float), cudaMemcpyDeviceToDevice,- cs_) != cudaSuccess ||- cudaGraphLaunch(prefill_dev_exec_[T], cs_) != cudaSuccess) {- err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- }- if (!sync) { // the caller orders on stream(); the records are staged (a pageable copy returns once taken)- (void) cudaStreamQuery(cs_);- ms_prefill += ms_since(t0);- return true;- }- if (cudaStreamSynchronize(cs_) != cudaSuccess) {- err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- ms_prefill += ms_since(t0);- return true;- }- for (int64_t c = 0; c < n; c += max_t_) {- const int T = (int) std::min<int64_t>(max_t_, n - c);- if (cell0 + c + T <= first_needed) continue;- if (!capture_prefill(T, err)) return false;- for (int t = 0; t < T; ++t) {- const int64_t cell = cell0 + c + t;- h_tok_[t] = next_tokens[c + t];- h_step_[t * 4 + 0] = (int32_t) cell;- h_step_[t * 4 + 1] = (int32_t) (cell + 1);- h_step_[t * 4 + 2] = (int32_t) ((cell + 1) / 4);- h_step_[t * 4 + 3] = (int32_t) (cell + 1);- for (int64_t h = 0; h < g_->n_head; ++h) h_pos_[t * g_->n_head + h] = (int32_t) cell;- }- if (cudaMemcpyAsync(Rin_, R_rows + (size_t) c * HCN, (size_t) T * HCN * sizeof(float), cudaMemcpyDeviceToDevice,- cs_) != cudaSuccess ||- cudaGraphLaunch(prefill_exec_[T], cs_) != cudaSuccess ||- cudaStreamSynchronize(cs_) != cudaSuccess) {- err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- }- ms_prefill += ms_since(t0);- return true;-}--bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* drafts, std::string& err,- float* probs, float min_p, int* n_drafts) {- const OnDevice on_device(device_);- if (T < 1 || T > max_t_ || a < 0 || a >= T) { err = "mtp: draft arguments out of range"; return false; }- if (chain_live_) { err = "mtp: a pipelined chain is still in flight"; return false; }- const bool cp = coupled_active_; // coupled draft sampling for this request: its own graphs- if (!capture_round(T, cp, err)) return false;- const int max_steps = std::min(max_t_ - 1, max_drafts_);- for (int j = 1; j < max_steps; ++j)- if (!capture_step(j, cp, err)) return false;- const Clock::time_point t0 = Clock::now();- const int64_t NH = g_->n_head;- auto put = [&](int row, int64_t cell) {- h_step_[row * 4 + 0] = (int32_t) cell;- h_step_[row * 4 + 1] = (int32_t) (cell + 1);- h_step_[row * 4 + 2] = (int32_t) ((cell + 1) / 4);- h_step_[row * 4 + 3] = (int32_t) (cell + 1);- for (int64_t h = 0; h < NH; ++h) h_pos_[row * NH + h] = (int32_t) cell;- };- for (int t = 0; t < T; ++t) {- h_tok_[t] = tokens[t];- put(t, p + t);- }- put(2 * max_t_ - 1, coupled_draft_cell(p, a, 0)); // p + a: draft 0's cell- for (int j = 1; j < max_steps; ++j)- put(max_t_ + j - 1, coupled_draft_cell(p, a, j));- for (int j = 0; j < max_steps; ++j) ((volatile int32_t*) h_out_)[j] = -1;- h_row_[0] = a;- h_row_[1] = 0;- if (h_force_ != nullptr) for (int j = 0; j < 16; ++j) h_force_[j] = -1; // the forcing kernels: no-ops here- std::atomic_thread_fence(std::memory_order_seq_cst);- if (!stage_source_R(T, err)) return false;-- SessionState* const pss = ple_ss_ ? ple_ss_ : ss_;- const bool do_ple = pss != nullptr && pss->ple.ready() && pss->ple.table != nullptr;- int32_t ple_prev[2] = {do_ple ? pss->ple_prev[0] : 0, do_ple ? pss->ple_prev[1] : 0};- auto prefetch_ple = [&](int32_t tok) {- if (!do_ple || tok < 0) return;- uint32_t rows16[strata::kernels::PLE_N_HEADS];- strata::kernels::ngram_rows(&tok, ple_prev, 1, pss->ple.consts, rows16);- ple_prev[0] = ple_prev[1];- ple_prev[1] = tok;- pss->ple.table->prefetch_rows(rows16);- };-- int n = 0;- if (min_p <= 0.0f && max_steps > 0) {- if (cudaGraphLaunch(cp ? round_exec_c_[T] : round_exec_[T], cs_) != cudaSuccess) {- err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- for (int j = 1; j < max_steps; ++j) {- if (cudaGraphLaunch(cp ? step_exec_c_[j] : step_exec_[j], cs_) != cudaSuccess) {- err = std::string("mtp draft step: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- }- (void) cudaStreamQuery(cs_);- prefetch_ple(tokens[a]);- for (int j = 0; j + 1 < max_steps; ++j) {- uint32_t spins = 0;- while (((volatile int32_t*) h_out_)[j] < 0) {-#if defined(_WIN32) || defined(__x86_64__)- _mm_pause();-#endif- if ((++spins & 1023u) == 0 && cudaStreamQuery(cs_) != cudaErrorNotReady) break;- }- drafts[j] = ((volatile int32_t*) h_out_)[j];- if (probs) probs[j] = ((volatile float*) h_prob_)[j];- prefetch_ple(drafts[j]);- }- if (cudaStreamSynchronize(cs_) != cudaSuccess) {- err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- const int last = max_steps - 1;- drafts[last] = ((volatile int32_t*) h_out_)[last];- if (probs) probs[last] = ((volatile float*) h_prob_)[last];- prefetch_ple(drafts[last]);- n = max_steps;- } else if (max_steps > 0) {- if (cudaGraphLaunch(cp ? round_exec_c_[T] : round_exec_[T], cs_) != cudaSuccess) {- err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- (void) cudaStreamQuery(cs_);- prefetch_ple(tokens[a]);- auto wait_step = [&](int j) {- uint32_t spins = 0;- while (((volatile int32_t*) h_out_)[j] < 0) {-#if defined(_WIN32) || defined(__x86_64__)- _mm_pause();-#endif- if ((++spins & 1023u) == 0 && cudaStreamQuery(cs_) != cudaErrorNotReady) break;- }- };- wait_step(0);- drafts[0] = ((volatile int32_t*) h_out_)[0];- float pj = ((volatile float*) h_prob_)[0];- if (probs) probs[0] = pj;- n = 1;- for (int j = 1; j < max_steps && pj >= min_p; ++j) {- if (cudaGraphLaunch(cp ? step_exec_c_[j] : step_exec_[j], cs_) != cudaSuccess) {- err = std::string("mtp draft step: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- (void) cudaStreamQuery(cs_);- prefetch_ple(drafts[j - 1]);- wait_step(j);- drafts[j] = ((volatile int32_t*) h_out_)[j];- pj = ((volatile float*) h_prob_)[j];- if (probs) probs[j] = pj;- ++n;- }- prefetch_ple(drafts[n - 1]);- if (cudaStreamSynchronize(cs_) != cudaSuccess) {- err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- }- for (int j = n; j < max_t_ - 1; ++j) { drafts[j] = 0; if (probs) probs[j] = 0.0f; }- if (n_drafts) *n_drafts = n;- ms_draft += ms_since(t0);- ++rounds;- return true;-}--bool MtpDrafter::stage_source_R(int T, std::string& err) {- if (src_R_ == nullptr || src_R_ == window_R_) return true;- if (cudaMemcpyAsync((void*) window_R_, src_R_, (size_t) T * (size_t) (g_->hc * g_->n_embd) * sizeof(float),- cudaMemcpyDeviceToDevice, cs_) != cudaSuccess) {- err = "mtp: staging the window's residual rows failed";- return false;- }- return true;-}--bool MtpDrafter::prepare_chain(std::string& err) {- const OnDevice on_device(device_);- if (!force_on_ || h_force_ == nullptr) { err = "mtp: chains need set_force_capture before load"; return false; }- for (int T = 1; T <= max_t_; ++T)- if (!capture_round(T, false, err)) return false;- for (int j = 1; j <= max_t_ - 2; ++j)- if (!capture_step(j, false, err)) return false;- if (ev_chain_ == nullptr && cudaEventCreateWithFlags(&ev_chain_, cudaEventDisableTiming) != cudaSuccess) {- err = "mtp: event creation failed";- return false;- }- for (cudaEvent_t& e : ev_step_)- if (e == nullptr && cudaEventCreateWithFlags(&e, cudaEventDisableTiming) != cudaSuccess) {- err = "mtp: event creation failed";- return false;- }- return true;-}--bool MtpDrafter::chain_launch(int T, const int32_t* tokens, int64_t p, int a, const int32_t* force, int n_force,- int n_out, int n_early, std::string& err) {- const OnDevice on_device(device_);- if (chain_live_) { err = "mtp: a chain is already in flight"; return false; }- if (!force_on_ || ev_chain_ == nullptr) { err = "mtp: chains need prepare_chain"; return false; }- if (T < 1 || T > max_t_ || a < 0 || a >= T || n_out < 1 || n_out > max_t_ - 1 || n_force < 0 || n_force > n_out ||- n_force > 16) {- err = "mtp: chain arguments out of range";- return false;- }- if (round_exec_[T] == nullptr) { err = "mtp: chain graphs not prepared"; return false; }- for (int j = 1; j < n_out; ++j)- if (step_exec_[j] == nullptr) { err = "mtp: chain graphs not prepared"; return false; }- const Clock::time_point t0 = Clock::now();- const int64_t NH = g_->n_head;- auto put = [&](int row, int64_t cell) {- h_step_[row * 4 + 0] = (int32_t) cell;- h_step_[row * 4 + 1] = (int32_t) (cell + 1);- h_step_[row * 4 + 2] = (int32_t) ((cell + 1) / 4);- h_step_[row * 4 + 3] = (int32_t) (cell + 1);- for (int64_t h = 0; h < NH; ++h) h_pos_[row * NH + h] = (int32_t) cell;- };- for (int t = 0; t < T; ++t) {- h_tok_[t] = tokens[t];- put(t, p + t);- }- put(2 * max_t_ - 1, p + a); // output 0's cell- for (int j = 1; j < n_out; ++j) put(max_t_ + j - 1, p + a + j); // every step's cell, staged at once- for (int j = 0; j < 16; ++j) h_force_[j] = j < n_force ? force[j] : -1;- h_row_[0] = a;- h_row_[1] = 0;- std::atomic_thread_fence(std::memory_order_seq_cst);- if (!stage_source_R(T, err)) return false;- chain_early_ = std::max(0, std::min(n_early, n_out));- bool ok = cudaGraphLaunch(round_exec_[T], cs_) == cudaSuccess;- if (ok && chain_early_ >= 1) ok = cudaEventRecord(ev_step_[0], cs_) == cudaSuccess;- for (int j = 1; ok && j < n_out; ++j) {- ok = cudaGraphLaunch(step_exec_[j], cs_) == cudaSuccess;- if (ok && j < chain_early_) ok = cudaEventRecord(ev_step_[j], cs_) == cudaSuccess;- }- if (!ok || cudaEventRecord(ev_chain_, cs_) != cudaSuccess) {- err = std::string("mtp chain: ") + cudaGetErrorString(cudaGetLastError());- return false;- }- (void) cudaStreamQuery(cs_); // WDDM: submit now- steps_seen_ = 0;- chain_live_ = true;- chain_n_ = n_out;- ms_draft += ms_since(t0);- ++rounds;- return true;-}--int MtpDrafter::chain_outputs_ready(std::string& err) {- if (!chain_live_) return chain_n_;- const OnDevice on_device(device_);- while (steps_seen_ < chain_early_) {- const cudaError_t q = cudaEventQuery(ev_step_[steps_seen_]);- if (q == cudaErrorNotReady) break;- if (q != cudaSuccess) { err = std::string("mtp chain: ") + cudaGetErrorString(q); return -1; }- chain_tok_[steps_seen_] = ((volatile int32_t*) h_out_)[steps_seen_];- chain_prob_[steps_seen_] = ((volatile float*) h_prob_)[steps_seen_];- ++steps_seen_;- }- return steps_seen_;-}--int MtpDrafter::chain_poll(std::string& err) {- if (!chain_live_) return 1;- const OnDevice on_device(device_);- const cudaError_t q = cudaEventQuery(ev_chain_);- if (q == cudaErrorNotReady) return 0;- chain_live_ = false;- if (q != cudaSuccess) { err = std::string("mtp chain: ") + cudaGetErrorString(q); return -1; }- for (int j = 0; j < chain_n_; ++j) {- chain_tok_[j] = ((volatile int32_t*) h_out_)[j];- chain_prob_[j] = ((volatile float*) h_prob_)[j];- }- for (int j = chain_n_; j < 8; ++j) { chain_tok_[j] = 0; chain_prob_[j] = 0.0f; }- return 1;-}--bool MtpDrafter::draft_first(int T, const float* R_row, int32_t token, int64_t cell, int32_t* drafts, std::string& err,- float* probs, float min_p, int* n_drafts) {- const OnDevice on_device(device_);- // row 0 is the real pair; rows 1.. repeat it and only write cells the next round overwrites- const int64_t HCN = g_->hc * g_->n_embd;- for (int t = 0; t < T; ++t)- if (cudaMemcpy((void*) (window_R_ + (size_t) t * HCN), R_row, (size_t) HCN * sizeof(float),- cudaMemcpyDeviceToDevice) != cudaSuccess) {- err = "mtp: staging the first residual failed";- return false;- }- std::vector<int32_t> toks((size_t) T, token);- return draft(T, toks.data(), cell, 0, drafts, err, probs, min_p, n_drafts);-}--} // namespace strata::core+// src/core/mtp.cpp - see include/strata/core/mtp.hpp.+#include "strata/core/mtp.hpp"+#include "strata/core/coupled_draft.hpp"+#include "strata/core/on_device.hpp"++#include "strata/core/native_head.hpp"+#include "strata/core/peer_experts.hpp"+#include "strata/platform/memory.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_q4.hpp"+#include "strata/kernels/kv_q8.hpp"+#include "strata/kernels/native_moe.hpp"+#include "strata/kernels/native_mmvq.hpp"+#include "strata/kernels/native_qsa.hpp"+#include "strata/kernels/native_rope.hpp"+#include "strata/kernels/native_router.hpp"+#include "strata/kernels/qsa.hpp"+#include "strata/kernels/qsa_decode_attn.hpp"+#include "strata/kernels/quantize_act.hpp"+#include "strata/kernels/rope.hpp"+#include "strata/kernels/router_top10.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 "strata/kernels/ngram.hpp"++#include <algorithm>+#include <atomic>+#include <chrono>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <exception>+#include <fstream>+#include <sstream>+#include <vector>+#if defined(_WIN32)+#include <intrin.h>+#elif defined(__x86_64__)+#include <immintrin.h>+#endif++#ifdef STRATA_NATIVE_EXPERTS+#include "ggml.h" // --mtp-q4: the head rows' and projections' formats, dequantized and requantized at load+#endif++namespace strata::core {+namespace {++constexpr float EPS = 1e-6f;+constexpr int GGML_Q8_0 = 8;+constexpr int GGML_Q4_0 = 2;+using Clock = std::chrono::steady_clock;+double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }++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) {+ if (cudaHostAlloc(h, bytes, cudaHostAllocMapped | (peer_portable() ? cudaHostAllocPortable : 0)) != 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;+}++// 64-bit seek/tell on a `FILE*`: `fseek`/`ftell` take a 32-bit `long` on Windows and would wrap past 2 GiB.+#if defined(_WIN32)+#define STRATA_FILE_SEEK64(f, o, w) _fseeki64((f), (long long) (o), (w))+#define STRATA_FILE_TELL64(f) _ftelli64(f)+#else+#define STRATA_FILE_SEEK64(f, o, w) fseeko((f), (off_t) (o), (w))+#define STRATA_FILE_TELL64(f) ftello(f)+#endif++bool read_file(const std::string& path, std::vector<uint8_t>& out) {+ // Loader fix (0.1.15+loaderfix.2): `ifstream::read` reaches the disk as 4095-byte reads on MSVC - the same+ // split `load_experts_ranges` had - so the drafter's 111 MiB dense blob paid 4 KiB per operation on a cold+ // start. `fread` passes a request bigger than the stream buffer straight to `ReadFile()`.+ FILE* f = std::fopen(path.c_str(), "rb");+ if (f == nullptr) return false;+ // A `FILE*` has no destructor and the short-read path below returns early: the guard closes on every path.+ struct Closer {+ FILE* f;+ ~Closer() { if (f != nullptr) std::fclose(f); }+ } closer{f};+ if (STRATA_FILE_SEEK64(f, 0, SEEK_END) != 0) return false;+ const long long n = (long long) STRATA_FILE_TELL64(f);+ if (n < 0 || STRATA_FILE_SEEK64(f, 0, SEEK_SET) != 0) return false;+#if !defined(_WIN32)+ strata::platform::advise_willneed(fileno(f), 0, (uint64_t) n);+#endif+ out.resize((size_t) n);+ return n == 0 || std::fread(out.data(), 1, (size_t) n, f) == (size_t) n;+}++} // namespace++MtpDrafter::~MtpDrafter() {+ if (cs_) cudaStreamSynchronize(cs_);+ for (auto& e : prefill_exec_) if (e) cudaGraphExecDestroy(e);+ for (auto& e : prefill_dev_exec_) if (e) cudaGraphExecDestroy(e);+ if (pf_dev_) cudaFree(pf_dev_);+ if (dense4_) cudaFree(dense4_);+ for (auto& e : round_exec_) if (e) cudaGraphExecDestroy(e);+ for (auto& e : step_exec_) if (e) cudaGraphExecDestroy(e);+ for (auto& e : round_exec_c_) if (e) cudaGraphExecDestroy(e);+ for (auto& e : step_exec_c_) if (e) cudaGraphExecDestroy(e);+ if (cparams_) cudaFree(cparams_);+ if (cring_) cudaFree(cring_);+ if (dinv_) cudaFree(dinv_);+ if (cscratch_) cudaFree(cscratch_);+ if (h_cparams_) cudaFreeHost(h_cparams_);+ if (h_chist_) cudaFreeHost(h_chist_);+ if (cs_) cudaStreamDestroy(cs_);+ if (side_) cudaStreamDestroy(side_);+ if (sh_fork_) cudaEventDestroy(sh_fork_);+ if (sh_join_) cudaEventDestroy(sh_join_);+ if (owns_weights_ && dense_) cudaFree(dense_);+ if (owns_weights_ && experts_) cudaFree(experts_);+ if (state_arena_) cudaFree(state_arena_);+ if (arena_) cudaFree(arena_);+ if (head_logits_) cudaFree(head_logits_);+ if (owns_draft_head_ && dhead_) { strata::kernels::native_q6_k_unpack(dhead_); cudaFree(dhead_); }+ if (owns_draft_head_ && dvocab_) cudaFree(dvocab_);+ if (ev_chain_) cudaEventDestroy(ev_chain_);+ for (cudaEvent_t e : ev_step_) if (e) cudaEventDestroy(e);+ void* hosts[] = {h_tok_, h_step_, h_pos_, h_row_, h_out_, h_prob_, h_force_};+ for (void* h : hosts) if (h) cudaFreeHost(h);+}++const float* MtpDrafter::f32(const char* name) const {+ for (const auto& t : tensors_) if (t.name == name && t.kind == "f32") return (const float*) (dense_ + t.off);+ return nullptr;+}+const uint16_t* MtpDrafter::bf16(const char* name) const {+ for (const auto& t : tensors_) if (t.name == name && t.kind == "bf16") return (const uint16_t*) (dense_ + t.off);+ return nullptr;+}+const void* MtpDrafter::q8(const char* name) const {+ for (const auto& t : tensors_) if (t.name == name && t.kind == "q8_0") return dense_ + t.off;+ return nullptr;+}+const void* MtpDrafter::wq(const char* name, int& type) const {+ if (dense4_ != nullptr)+ for (const auto& e : q4_off_)+ if (e.first == name) { type = GGML_Q4_0; return dense4_ + e.second; }+ type = GGML_Q8_0;+ return q8(name);+}++// --mtp-q4: every Q8_0 tensor of dense.bin as Q4_0 (ggml's reference quantizer), in one device buffer+bool MtpDrafter::make_q4_dense(const std::vector<uint8_t>& blob, std::string& err) {+#ifdef STRATA_NATIVE_EXPERTS+ uint64_t total = 0;+ std::vector<std::pair<const Tensor*, uint64_t>> plan;+ for (const auto& t : tensors_) {+ if (t.kind != "q8_0" || t.cols % 32 != 0) continue;+ plan.push_back({&t, total});+ total += ((uint64_t) t.rows * (uint64_t) (t.cols / 32) * 18 + 255) & ~255ull;+ }+ std::vector<uint8_t> host((size_t) total);+ std::vector<float> row;+ const auto* q8t = ggml_get_type_traits(GGML_TYPE_Q8_0);+ for (const auto& [t, off] : plan) {+ row.resize((size_t) t->cols);+ const uint64_t in_row = (uint64_t) (t->cols / 32) * 34, out_row = (uint64_t) (t->cols / 32) * 18;+ for (int64_t r = 0; r < t->rows; ++r) {+ q8t->to_float(blob.data() + t->off + (uint64_t) r * in_row, row.data(), t->cols);+ ggml_quantize_chunk(GGML_TYPE_Q4_0, row.data(), host.data() + off + (uint64_t) r * out_row, 0, 1, t->cols,+ nullptr);+ }+ q4_off_.push_back({t->name, off});+ }+ if (cudaMalloc((void**) &dense4_, (size_t) total) != cudaSuccess) { err = "mtp: the Q4_0 projections do not fit"; return false; }+ cudaMemcpy(dense4_, host.data(), (size_t) total, cudaMemcpyHostToDevice);+ vram_ += total;+ std::fprintf(stderr, "strata mtp: --mtp-q4: %zu projections as Q4_0 (%.1f MiB)\n", plan.size(), (double) total / 1048576.0);+ return true;+#else+ (void) blob;+ err = "mtp: --mtp-q4 needs a build with native experts (ggml)";+ return false;+#endif+}++// --mtp-q4: the draft head's subset rows as Q4_0 (dequantized from the main head's format)+bool MtpDrafter::make_q4_head(std::string& err) {+#ifdef STRATA_NATIVE_EXPERTS+ const int64_t N = g_->n_embd;+ const auto* tt = ggml_get_type_traits((ggml_type) head_->type());+ if (tt == nullptr || tt->to_float == nullptr || N % 32 != 0) { err = "mtp: --mtp-q4 cannot read the head's format"; return false; }+ const size_t in_row = head_->row_bytes(), out_row = (size_t) (N / 32) * 18;+ std::vector<uint8_t> src((size_t) n_dvocab_ * in_row), dst((size_t) n_dvocab_ * out_row);+ if (cudaMemcpy(src.data(), dhead_, src.size(), cudaMemcpyDeviceToHost) != cudaSuccess) { err = "mtp: reading the draft head"; return false; }+ std::vector<float> row((size_t) N);+ for (int64_t r = 0; r < n_dvocab_; ++r) {+ tt->to_float(src.data() + (size_t) r * in_row, row.data(), N);+ ggml_quantize_chunk(GGML_TYPE_Q4_0, row.data(), dst.data() + (size_t) r * out_row, 0, 1, N, nullptr);+ }+ // the Q4_0 rows replace the subset in place (they are smaller)+ if (cudaMemcpy(dhead_, dst.data(), dst.size(), cudaMemcpyHostToDevice) != cudaSuccess) { err = "mtp: writing the draft head"; return false; }+ dhead_type_ = GGML_Q4_0;+ std::fprintf(stderr, "strata mtp: --mtp-q4: draft head over %lld tokens as Q4_0 (%.1f MiB read per step, was %.1f)\n",+ (long long) n_dvocab_, (double) dst.size() / 1048576.0, (double) src.size() / 1048576.0);+ return true;+#else+ err = "mtp: --mtp-q4 needs a build with native experts (ggml)";+ return false;+#endif+}++bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, SessionState& ss, int max_t, std::string& err,+ int64_t window, const MtpDrafter* shared) {+ cudaGetDevice(&device_); // a layer split's last stage on another GPU: the drafter lives there+ g_ = &g;+ ss_ = &ss;+ if (ple_ss_ == nullptr) ple_ss_ = &ss;+ max_t_ = max_t;+ rt_dir_ = rt_dir;+ if (max_t < 1 || max_t > strata::kernels::kVerifyMaxT) { err = "mtp: max_t out of range"; return false; }+ if (shared != nullptr) {+ if (shared->device_ != device_ || shared->g_ != &g || shared->dense_ == nullptr ||+ shared->experts_ == nullptr || shared->rt_dir_ != rt_dir) {+ err = "mtp: incompatible shared weights";+ return false;+ }+ dense_ = shared->dense_;+ experts_ = shared->experts_;+ tensors_ = shared->tensors_;+ owns_weights_ = false;+ owns_draft_head_ = false;+ }+ // Loader fix (0.1.15+loaderfix.2): the two reads below are the whole “drafter files” cost; reporting+ // them apart from the rest of the stage is what makes the next regression visible.+ const auto t_files = std::chrono::steady_clock::now();+ // ---- the index and the dense weights+ if (shared == nullptr) {+ std::ifstream idx(rt_dir + "/dense.txt");+ if (!idx) { err = "mtp: cannot open " + rt_dir + "/dense.txt (run tools/mtp_rt.py)"; return false; }+ std::string line;+ while (std::getline(idx, line)) {+ if (line.empty()) continue;+ std::istringstream is(line);+ Tensor t;+ is >> t.name >> t.kind >> t.rows >> t.cols >> t.off >> t.bytes;+ if (!is) { err = "mtp: malformed dense.txt line: " + line; return false; }+ tensors_.push_back(t);+ }+ std::vector<uint8_t> blob;+ if (!read_file(rt_dir + "/dense.bin", blob)) { err = "mtp: cannot read dense.bin"; return false; }+ const cudaError_t alloc = cudaMalloc((void**) &dense_, blob.size());+ if (alloc != cudaSuccess) {+ size_t free_bytes = 0, total_bytes = 0;+ const cudaError_t info = cudaMemGetInfo(&free_bytes, &total_bytes);+ err = "mtp: dense weights allocation failed (" + std::string(cudaGetErrorString(alloc)) ++ "), requested " + std::to_string(blob.size() >> 20) + " MiB, CUDA0 free " ++ (info == cudaSuccess ? std::to_string(free_bytes >> 20) + " MiB" : "unknown");+ return false;+ }+ cudaMemcpy(dense_, blob.data(), blob.size(), cudaMemcpyHostToDevice);+ vram_ += blob.size();+ if (q4_ && !make_q4_dense(blob, err)) return false;+ }+ // ---- the 512 routed experts, one blob each+ if (shared == nullptr) {+ const uint64_t bytes = (uint64_t) g.n_expert * strata::kernels::cpu::BLOB;+ // Loader fix (0.1.15+loaderfix.2): each 64 MiB read below reached the disk as ~16k 4095-byte reads under+ // MSVC's `basic_filebuf::xsgetn`, which is what made 675 MiB of drafter experts take minutes.+ FILE* f = std::fopen((rt_dir + "/experts.bin").c_str(), "rb");+ if (f == nullptr) { err = "mtp: cannot open experts.bin"; return false; }+ struct Closer {+ FILE* f;+ ~Closer() { if (f != nullptr) std::fclose(f); }+ } closer{f};+ if (cudaMalloc((void**) &experts_, bytes) != cudaSuccess) { err = "mtp: the 512 experts do not fit in VRAM"; return false; }+#if !defined(_WIN32)+ strata::platform::advise_willneed(fileno(f), 0, bytes);+#endif+ std::vector<uint8_t> chunk(64u << 20);+ for (uint64_t off = 0; off < bytes;) {+ const uint64_t n = std::min<uint64_t>(chunk.size(), bytes - off);+ if (std::fread(chunk.data(), 1, (size_t) n, f) != (size_t) n) { err = "mtp: experts.bin is truncated"; return false; }+ cudaMemcpy(experts_ + off, chunk.data(), n, cudaMemcpyHostToDevice);+ off += n;+ }+ vram_ += bytes;+ }+ const char* required[] = {"fc_embedding.weight", "fc_hidden.weight", "self_attn.q_proj.weight", "self_attn.k_proj.weight",+ "self_attn.v_proj.weight", "self_attn.o_proj.weight", "mlp.shared_expert.gate_proj.weight",+ "mlp.shared_expert.up_proj.weight", "mlp.shared_expert.down_proj.weight"};+ for (const char* n : required) if (!q8(n)) { err = std::string("mtp: ") + n + " is missing (q8_0)"; return false; }++ // ---- the layer's own K/V (dense attention: no indexer state is read)+ const strata::kernels::QsaShapes s = shapes_of(g);+ const int64_t max_cells = ss.qsa_states[ss.qsa_primary()].max_cells;+ // KV streaming: the drafter only reads its last `window` cells, so with streaming on its K/V is a ring of the+ // window (plus the cells a round writes ahead of its queries) over a host copy, refilled on a resume. The host copy+ // is pinned after the expert arena has pinned what it could: if it does not fit, the K/V stays whole in VRAM.+ int64_t ring = (window > 0 && window < max_cells) ? window + 4 * (int64_t) max_t + 64 : 0;+ // K8V4 never applies to the drafter: its own attention paths (below, and verify.cpp) handle whole formats+ // only, whatever ring shape it takes (0, a window, or the -1 fully-resident fallback).+ const bool kv_hybrid_was = qsa_kv_hybrid();+ const bool kv_int8_was = qsa_kv_int8();+ qsa_set_kv_hybrid(false);+ if (kv_hybrid_was) qsa_set_kv_int8(true); // the drafter under --kv k8v4: plain INT8+ uint64_t sb = qsa_state_bytes(g, max_cells, false, ring);+ if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }+ if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[ss.qsa_primary()], ring) == 0) {+ if (st_.kv_mode == 0) { err = "mtp: state init failed"; return false; }+ std::fprintf(stderr, "strata mtp: no pinned RAM left for the draft layer's K/V copy; keeping it in VRAM\n");+ cudaGetLastError();+ cudaFree(state_arena_);+ st_ = QsaState{};+ ring = -1; // fully resident+ sb = qsa_state_bytes(g, max_cells, false, ring);+ if (cudaMalloc(&state_arena_, sb) != cudaSuccess) { err = "mtp: the K/V state does not fit"; return false; }+ if (qsa_state_init(g, max_cells, state_arena_, st_, &ss.qsa_states[ss.qsa_primary()], ring) == 0) { err = "mtp: state init failed"; return false; }+ }+ qsa_set_kv_int8(kv_int8_was);+ qsa_set_kv_hybrid(kv_hybrid_was);+ qsa_state_zero(st_, g, nullptr);+ cudaDeviceSynchronize();+ vram_ += sb;++ // ---- buffers+ window_ = (window > 0 && window < max_cells) ? window : 0;+ cap_ = (((window_ > 0 ? window_ : max_cells) + 63) / 64) * 64;+ 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 NH = (uint64_t) g.n_head, HD = (uint64_t) g.head_dim, NKV = (uint64_t) g.n_head_kv;+ const uint64_t R2 = 2 * T; // step/pos rows: T catch-up rows + up to T-2 chain steps+ bool ok = mapped(T * 4 + 64, (void**) &h_tok_, (void**) &m_tok_) &&+ mapped(R2 * 4 * 4 + 64, (void**) &h_step_, (void**) &m_step_) &&+ mapped(R2 * NH * 4 + 64, (void**) &h_pos_, (void**) &m_pos_) &&+ mapped(64, (void**) &h_row_, (void**) &m_row_) &&+ mapped(T * 4 + 64, (void**) &h_out_, (void**) &m_out_) &&+ mapped(T * 4 + 64, (void**) &h_prob_, (void**) &m_prob_) &&+ (!force_on_ || mapped(64, (void**) &h_force_, (void**) &m_force_));+ if (!ok) { err = "mtp: mapped staging failed"; return false; }+ if (h_force_ != nullptr) for (int j = 0; j < 16; ++j) h_force_[j] = -1;+ auto carve = [&](Bump& b) {+ tok_ = b.take<int32_t>(T); step_ = b.take<int32_t>(R2 * 4); pos_ = b.take<int32_t>(R2 * NH); row_ = b.take<int32_t>(4);+ ident_ = b.take<int32_t>(T * (uint64_t) cap_);+ Rin_ = b.take<float>(T * HC * N); R_ = b.take<float>(T * HC * N);+ emb_ = b.take<float>(T * N); en_ = b.take<float>(T * N); e2_ = b.take<float>(T * N);+ hn_ = b.take<float>(T * HC * N); h2_ = b.take<float>(T * HC * N);+ mixed_ = 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); bo_ = b.take<float>(T * N);+ xn_ = b.take<float>(T * HC * N);+ xq_ = b.take<uint8_t>(strata::kernels::native_q8_1_bytes((int) (NH * HD), 8));+ 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);+ attn_ = b.take<float>(T * NH * HD); attn32_ = b.take<float>(T * NH * HD);+ attn_scratch_ = b.take<float>((uint64_t) attn_scratch_floats_); // the full layer runs one row at a time+ logits_ = b.take<float>(T * (uint64_t) g.n_expert); w_ = b.take<float>(T * K); ids_ = b.take<int32_t>(T * K);+ shared_ = b.take<float>(T * N); parts_ = b.take<float>(T * K * N); y_ = b.take<float>(T * N);+ sample_ = b.take<float>(T * N);+ hit_slot_ = b.take<int32_t>(T * K); hit_dst_ = b.take<int32_t>(T * K); hit_count_ = b.take<int32_t>(4);+ grp_ptr_ = b.take<unsigned long long>(T * K); grp_start_ = b.take<int32_t>(T * K + 1);+ grp_counts_ = b.take<int32_t>(4);+ hit_xq_ = b.take<uint8_t>(T * (N / 32) * 34); hit_xs_ = b.take<float>(T * (N / 32));+ hit_scratch_ = b.take<uint8_t>(strata::kernels::moe_hit_grouped_scratch_bytes((int64_t) (T * K), g.n_embd, g.n_ff));+ sh_scratch_ = (float*) b.take<uint8_t>(strata::kernels::shared_expert_scratch_bytes(g.n_ff));+ x_bf16_ = b.take<uint16_t>(N);+ out_ids_ = b.take<int32_t>(T + 4);+ probs_ = b.take<float>(T + 4);+ arg_scratch_ = b.take<uint8_t>(strata::kernels::argmax_rows_scratch_bytes((int) T));+ top_scratch_ = b.take<uint8_t>(strata::kernels::row_top_prob_scratch_bytes((int) T));+ dummy_inj_ = b.take<float>(HC);+ };+ Bump count;+ carve(count);+ if (cudaMalloc(&arena_, count.used) != cudaSuccess) { err = "mtp: buffers do not fit"; return false; }+ cudaMemset(arena_, 0, count.used);+ Bump real;+ real.base = (uint8_t*) arena_;+ carve(real);+ vram_ += count.used;+ {+ std::vector<int32_t> id((size_t) (T * (uint64_t) cap_));+ for (uint64_t t = 0; t < T; ++t)+ for (int64_t i = 0; i < cap_; ++i) id[(size_t) (t * (uint64_t) cap_ + (uint64_t) i)] = (int32_t) i;+ cudaMemcpy(ident_, id.data(), id.size() * 4, cudaMemcpyHostToDevice);+ }+ // --pipeline-windows 2 (the forcing graphs): the chain shares its card with stage 1's windows and the next launch+ // on stage 0 waits for it, so its stream gets the highest priority there (STRATA_MTP_PRIORITY=0: the default)+ bool prio = false;+#if !defined(STRATA_USE_HIP) // (HIP: the default priority)+ static const bool hi_prio = [] { const char* v = std::getenv("STRATA_MTP_PRIORITY"); return v == nullptr || std::atoi(v) != 0; }();+ int prio_lo = 0, prio_hi = 0;+ if (force_on_ && hi_prio && cudaDeviceGetStreamPriorityRange(&prio_lo, &prio_hi) == cudaSuccess)+ prio = cudaStreamCreateWithPriority(&cs_, cudaStreamNonBlocking, prio_hi) == cudaSuccess;+ cudaGetLastError();+#endif+ if (!prio && cudaStreamCreateWithFlags(&cs_, cudaStreamNonBlocking) != cudaSuccess) { err = "mtp: stream"; return false; }+ if (cudaStreamCreateWithFlags(&side_, cudaStreamNonBlocking) != cudaSuccess ||+ cudaEventCreateWithFlags(&sh_fork_, cudaEventDisableTiming) != cudaSuccess ||+ cudaEventCreateWithFlags(&sh_join_, cudaEventDisableTiming) != cudaSuccess) {+ err = "mtp: streams";+ return false;+ }+ const double files_s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t_files).count();+ if (shared != nullptr) {+ std::fprintf(stderr, "strata mtp: shared draft weights, %.0f MiB of private state and buffers\n",+ (double) vram_ / 1048576.0);+ } else {+ std::fprintf(stderr, "strata mtp: draft layer loaded, %.0f MiB of VRAM (experts %.0f, dense %.0f), files read in %.2f s (%.0f MiB/s)\n",+ (double) vram_ / 1048576.0, (double) g.n_expert * strata::kernels::cpu::BLOB / 1048576.0,+ (double) tensors_.back().off / 1048576.0, files_s,+ files_s > 0 ? ((double) g.n_expert * strata::kernels::cpu::BLOB + (double) tensors_.back().off) /+ 1048576.0 / files_s : 0.0);+ }+ return true;+}++namespace {+// STRATA_MTP_FULL_HEAD=1 (a diagnostic): draft over the whole vocabulary (the main head itself) instead of+// rt/draft_vocab.bin's subset - the reference head, to measure what the subset costs in acceptance+bool full_head_env() {+ static const bool on = [] { const char* v = std::getenv("STRATA_MTP_FULL_HEAD"); return v && v[0] == '1'; }();+ return on;+}+} // namespace++bool MtpDrafter::top2_env() {+ static const bool on = [] { const char* v = std::getenv("STRATA_MTP_TOP2"); return v && v[0] == '1'; }();+ return on;+}++// STRATA_MTP_TOP2=1 (a diagnostic for tree drafts): draft j's runner-up under the draft layer, from the head logits+// the step just wrote (row 0), on the host. Slow; only for measuring what a second branch would have caught.+void MtpDrafter::record_top2(int j) {+ const int64_t nv = dhead_ != nullptr ? n_dvocab_ : n_vocab_;+ if ((int64_t) lg_host_.size() < nv) lg_host_.resize((size_t) nv);+ if ((int) top2_.size() < max_t_) top2_.assign((size_t) max_t_, -1);+ if (j < 0 || j >= max_t_) return;+ if (cudaMemcpy(lg_host_.data(), head_logits_, (size_t) nv * sizeof(float), cudaMemcpyDeviceToHost) != cudaSuccess) {+ top2_[(size_t) j] = -1;+ return;+ }+ int64_t b1 = -1, b2 = -1;+ for (int64_t i = 0; i < nv; ++i) {+ const float v = lg_host_[(size_t) i];+ if (b1 < 0 || v > lg_host_[(size_t) b1]) { b2 = b1; b1 = i; }+ else if (b2 < 0 || v > lg_host_[(size_t) b2]) b2 = i;+ }+ top2_[(size_t) j] = b2 < 0 ? -1 : dhead_ != nullptr ? dvocab_host_[(size_t) b2] : (int32_t) b2;+}++uint64_t MtpDrafter::bind_bytes(uint64_t head_row_bytes, int64_t n_vocab) const {+ uint64_t bytes = head_logits_ ? 0 : (uint64_t) max_t_ * (uint64_t) n_vocab * sizeof(float);+ if (dhead_ == nullptr && owns_draft_head_ && !full_head_env()) {+ if (FILE* f = std::fopen(vocab_file().c_str(), "rb")) {+ std::fseek(f, 0, SEEK_END);+ const long size = std::ftell(f);+ std::fclose(f);+ if (size >= 4 && size % 4 == 0) bytes += (uint64_t) (size / 4) * head_row_bytes + (uint64_t) size;+ }+ }+ // coupled draft sampling (STRATA_SPEC_COUPLED=1 only): an upper bound - the id -> subset map, the penalty ring,+ // the split scratch (64 lists of 64 entries at most) and the parameters+ if (coupled_draft_env() && cparams_ == nullptr)+ bytes += (uint64_t) n_vocab * 4 + (uint64_t) (kCoupledHistCap + max_t_) * 4 + 64ull * 64ull * 8ull + 4096;+ return bytes;+}++bool MtpDrafter::setup_coupled(std::string& err) {+ const int64_t nv = dhead_ != nullptr ? n_dvocab_ : n_vocab_;+ const size_t scratch = strata::kernels::coupled_draft_scratch_bytes((int) nv);+ if (scratch == 0) {+ std::fprintf(stderr, "strata mtp: STRATA_SPEC_COUPLED: %lld draft logits are too wide for the coupled sampler; "+ "argmax drafts\n", (long long) nv);+ return true;+ }+ const size_t ring = (size_t) (kCoupledHistCap + max_t_) * sizeof(int32_t);+ if (cudaMalloc((void**) &cparams_, sizeof(strata::kernels::SamplerParams)) != cudaSuccess ||+ cudaMalloc((void**) &cring_, ring) != cudaSuccess || cudaMalloc(&cscratch_, scratch) != cudaSuccess ||+ !mapped(sizeof(strata::kernels::SamplerParams), (void**) &h_cparams_, (void**) &m_cparams_) ||+ !mapped((size_t) kCoupledHistCap * sizeof(int32_t), (void**) &h_chist_, (void**) &m_chist_)) {+ err = "mtp: the coupled draft sampler's buffers do not fit";+ return false;+ }+ cudaMemset(cring_, 0xff, ring); // -1: no token+ std::fill(h_chist_, h_chist_ + kCoupledHistCap, -1);+ vram_ += sizeof(strata::kernels::SamplerParams) + ring + scratch;+ if (dhead_ != nullptr) { // token id -> subset index (-1: not in the draft head), for the penalties+ std::vector<int32_t> sub((size_t) n_dvocab_), inv((size_t) n_vocab_, -1);+ cudaMemcpy(sub.data(), dvocab_, sub.size() * sizeof(int32_t), cudaMemcpyDeviceToHost);+ for (size_t i = 0; i < sub.size(); ++i)+ if (sub[i] >= 0 && sub[i] < n_vocab_ && inv[(size_t) sub[i]] < 0) inv[(size_t) sub[i]] = (int32_t) i;+ if (cudaMalloc((void**) &dinv_, inv.size() * sizeof(int32_t)) != cudaSuccess) {+ err = "mtp: the coupled draft sampler's token map does not fit";+ return false;+ }+ cudaMemcpy(dinv_, inv.data(), inv.size() * sizeof(int32_t), cudaMemcpyHostToDevice);+ vram_ += inv.size() * sizeof(int32_t);+ }+ if (cudaDeviceSynchronize() != cudaSuccess) {+ err = std::string("mtp: coupled setup: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ coupled_ok_ = true;+ std::fprintf(stderr, "strata mtp: coupled draft sampling on (STRATA_SPEC_COUPLED): sampled requests draft with the "+ "target's chain and Philox draw over %lld tokens\n", (long long) nv);+ return true;+}++void MtpDrafter::set_draft_sampling(const strata::kernels::SamplerParams& sp) {+ coupled_active_ = coupled_ok_ && !sp.greedy && sp.temperature > 0.0f;+ if (!coupled_active_) return;+ *h_cparams_ = sp; // read by the next round graph (after the previous one has synced)+ std::fill(h_chist_, h_chist_ + kCoupledHistCap, -1);+}++void MtpDrafter::set_draft_history(const int32_t* tail, int64_t n_tail, int32_t next) {+ if (!coupled_active_) return;+ const int h = coupled_hist_len(h_cparams_->penalty_last_n, kCoupledHistCap);+ coupled_hist_base(tail, n_tail, next, h, h_chist_ + (kCoupledHistCap - h));+}++namespace {+// #474: what to change when the draft head's token subset does not fit the VRAM left - a smaller subset that setup+// ships (data/draft_vocab_*.bin; their token counts below), and how to pick it. The start used to stop at "the draft+// head does not fit" with no hint. Text only, after the failure: nothing changes for a start that fits.+void draft_head_hint(int64_t n_tokens, int64_t row_bytes) {+ size_t free_b = 0, total_b = 0;+ const bool have_free = cudaMemGetInfo(&free_b, &total_b) == cudaSuccess;+ auto mib = [&](int64_t n) { return (double) (n * row_bytes) / 1048576.0; };+ std::string free_s;+ if (have_free) {+ char b[64];+ std::snprintf(b, sizeof(b), " and %.0f MiB is free", (double) free_b / 1048576.0);+ free_s = b;+ }+ std::fprintf(stderr, "strata mtp: the draft head over %lld tokens needs %.0f MiB of VRAM%s.\n",+ (long long) n_tokens, mib(n_tokens), free_s.c_str());+ struct Subset { const char* name; int64_t tokens; const char* what; };+ static const Subset smaller[] = {{"cyrillic", 58963, "English, code and the Cyrillic script"},+ {"en", 40525, "English and code"}};+ std::string opts;+ for (const Subset& s : smaller)+ if (s.tokens < n_tokens) {+ char b[160];+ std::snprintf(b, sizeof(b), "%s--draft-vocab %s (%s, ~%.0f MiB)", opts.empty() ? "" : " or ", s.name,+ s.what, mib(s.tokens));+ opts += b;+ }+ if (!opts.empty())+ std::fprintf(stderr, "strata mtp: hint: a smaller draft vocabulary needs less VRAM: %s. Start once with it - "+ "START-HERE.bat --draft-vocab en (Windows) or ./setup.sh --draft-vocab en - and the model "+ "keeps it (\"draft_vocab\" in its strata-*.json config); or a smaller --context in "+ "setup.\n",+ opts.c_str());+ else+ std::fprintf(stderr, "strata mtp: hint: this is already the smallest shipped draft vocabulary: a smaller "+ "--context (or closing what else uses the GPU) leaves it room.\n");+}+} // namespace++bool MtpDrafter::bind(const WeightTable& wt, const NativeHead* head, const float* window_R, std::string& err,+ const MtpDrafter* shared) {+ const OnDevice on_device(device_);+ wt_ = &wt;+ head_ = head;+ window_R_ = window_R;+ const WeightRef* wo = wt.find("output.weight");+ if (!wo) { err = "mtp: output.weight is missing"; return false; }+ n_vocab_ = wo->ne1;+ if (head == nullptr || !head->loaded()) { err = "mtp: the draft layer needs the native head (--native)"; return false; }+ if (head_logits_ == nullptr &&+ cudaMalloc((void**) &head_logits_, (size_t) max_t_ * (size_t) n_vocab_ * sizeof(float)) != cudaSuccess) {+ err = "mtp: the draft logits do not fit";+ return false;+ }+ if (shared != nullptr) {+ if (shared->head_ != head || shared->device_ != device_ ||+ shared->n_vocab_ != n_vocab_) {+ err = "mtp: incompatible shared draft head";+ return false;+ }+ dhead_ = shared->dhead_;+ dvocab_ = shared->dvocab_;+ n_dvocab_ = shared->n_dvocab_;+ owns_draft_head_ = false;+ }+ // the draft head's token subset, when tools/draft_vocab.py wrote one+ if (dhead_ == nullptr && shared == nullptr && !full_head_env()) {+ std::vector<uint8_t> raw;+ if (read_file(vocab_file(), raw) && raw.size() >= 4 && raw.size() % 4 == 0) {+ n_dvocab_ = (int64_t) (raw.size() / 4);+ const int64_t row_bytes = (int64_t) head->row_bytes(); // a vocabulary row of the native head+ if (cudaMalloc((void**) &dvocab_, raw.size()) != cudaSuccess ||+ cudaMalloc((void**) &dhead_, (size_t) (n_dvocab_ * row_bytes)) != cudaSuccess) {+ err = "mtp: the draft head does not fit";+ draft_head_hint(n_dvocab_, row_bytes);+ return false;+ }+ cudaMemcpy(dvocab_, raw.data(), raw.size(), cudaMemcpyHostToDevice);+ dvocab_host_.resize((size_t) n_dvocab_);+ std::memcpy(dvocab_host_.data(), raw.data(), raw.size());+ strata::kernels::gather_rows((const uint8_t*) head->weights(), row_bytes, dvocab_, n_dvocab_, dhead_, nullptr);+ cudaDeviceSynchronize();+ vram_ += (uint64_t) (n_dvocab_ * row_bytes) + raw.size();+ std::fprintf(stderr, "strata mtp: draft head over %lld tokens (%.1f MiB)\n", (long long) n_dvocab_,+ (double) (n_dvocab_ * row_bytes) / 1048576.0);+ dhead_type_ = head->type();+ if (q4_head_ && !make_q4_head(err)) return false;+ if (dhead_type_ == 14 && strata::kernels::native_q6_k_packed_enabled()) // STRATA_Q6_PACKED=1+ strata::kernels::native_q6_k_pack(dhead_, (int) (row_bytes / 210) * 256, (int) n_dvocab_,+ "MTP draft head");+ }+ }+ if (coupled_draft_env() && cparams_ == nullptr && !setup_coupled(err)) return false;+ return true;+}++// The layer for T rows. full = false stops after the K/V append (the prompt only needs the cache).+bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::string& err) {+ // step_row0 >= 0: the full layer on step rows [step_row0, +T); step_row0 < 0: K/V only on rows [-1 - step_row0, +T)+ const bool full = step_row0 >= 0;+ if (full && T != 1) { err = "mtp: the full layer runs one row at a time (its attention scratch is sized for one)"; return false; }+ if (!record_front(T, full ? step_row0 : -1 - step_row0, cs, err)) return false;+ return !full || record_rest(step_row0, cs, err);+}++bool MtpDrafter::record_front(int T, int row0, cudaStream_t cs, std::string& err) {+ using namespace strata::kernels;+ const ModelGeometry& g = *g_;+ const int64_t N = g.n_embd, HC = g.hc, NH = g.n_head, HD = g.head_dim, NKV = g.n_head_kv;+ const QsaShapes s = shapes_of(g);+ const int32_t* step = step_ + row0 * 4;+ const int32_t* pos = pos_ + row0 * NH;+ // --mtp-q4: each projection's format and weights for this pass (Q8_0 unless the Q4_0 copies exist)+ int wt_fc_embedding, wt_fc_hidden, wt_k_proj, wt_v_proj;+ const void* wp_fc_embedding = wq("fc_embedding.weight", wt_fc_embedding);+ const void* wp_fc_hidden = wq("fc_hidden.weight", wt_fc_hidden);+ const void* wp_k_proj = wq("self_attn.k_proj.weight", wt_k_proj);+ const void* wp_v_proj = wq("self_attn.v_proj.weight", wt_v_proj);+ try {+ // ---- the two input branches+ const WeightRef* we = wt_->find("token_embd.weight");+ if (!we) { err = "mtp: token_embd.weight is missing"; return false; }+ if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table+ ne->gather_dev(tok_, T, emb_, cs);+ } else {+ const auto* codes = (const uint8_t*) we->data;+ const auto* scales = (const float*) (codes + we->codes_bytes);+ const auto* offsets = we->has_offset ? (const float*) (codes + we->codes_bytes + we->scales_bytes) : nullptr;+ embedding_gather_dev(codes, scales, offsets, tok_, T, we->ne0, we->code_bits, we->code_bias, we->group_elems,+ (uint64_t) (we->ne0 / (8 / we->code_bits)), (uint64_t) (we->ne0 / we->group_elems), emb_, cs);+ }+ native_qsa_rms_norm_weighted(emb_, f32("pre_fc_norm_embedding.weight"), en_, (int) N, T, EPS, cs);+ native_quantize_q8_1(en_, xq_, (int) N, T, cs);+ native_mmvq(wt_fc_embedding, wp_fc_embedding, xq_, e2_, (int) N, (int) N, T, cs);+ if (hnorm_stream_) // --mtp-hnorm stream: one RMS per stream, each scaled by its slice of the weight+ native_qsa_rms_norm_grouped(Rin_, f32("pre_fc_norm_hidden.weight"), hn_, (int) N, (int) HC, (int) (T * HC), EPS, cs);+ else+ native_qsa_rms_norm_weighted(Rin_, f32("pre_fc_norm_hidden.weight"), hn_, (int) (HC * N), T, EPS, cs);+ for (int c0 = 0; c0 < T * HC; c0 += 8) {+ const int nc = (int) std::min<int64_t>(8, T * HC - c0);+ native_quantize_q8_1(hn_ + (size_t) c0 * N, xq_, (int) N, nc, cs);+ native_mmvq(wt_fc_hidden, wp_fc_hidden, xq_, h2_ + (size_t) c0 * N, (int) N, (int) N, nc, cs);+ }+ add_streams_broadcast(h2_, e2_, R_, N, (int) HC, T, cs);+ // ---- the attention hyper-connection+ {+ FusedGrArgs fa[kFusedGrMaxT];+ for (int t = 0; t < T; ++t) {+ fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = false;+ fa[t].w_norm = f32("attn_hyper_connection.hc_norm.weight");+ fa[t].w_down = bf16("attn_hyper_connection.input_mix_weight_down.weight");+ fa[t].w_up = bf16("attn_hyper_connection.input_mix_weight_up.weight");+ fa[t].w_inject = bf16("attn_hyper_connection.block_inject_weight.weight");+ fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC;+ fa[t].inject_out = inj_ + t * HC; fa[t].mixed = mixed_ + t * N;+ }+ fused_gr_read_multi(fa, T, xn_, cs);+ }+ // ---- attention: K/V into the layer's own cache+ native_quantize_q8_1(mixed_, xq_, (int) N, T, cs);+ native_mmvq(wt_k_proj, wp_k_proj, xq_, kcur_, (int) N, (int) (NKV * HD), T, cs);+ native_mmvq(wt_v_proj, wp_v_proj, xq_, vcur_, (int) N, (int) (NKV * HD), T, cs);+ // #783 PR-d (stuchapin909): the T tokens' K norm, K/V rotation and K/V append each run once over all T rows+ // (every row is independent; the rope keeps its per-token positions). STRATA_NO_BATCH_KV_STEP=1 appends per token.+ static const bool no_batch_kv = [] {+ const char* v = std::getenv("STRATA_NO_BATCH_KV_STEP");+ return v != nullptr && v[0] != '\0' && v[0] != '0';+ }();+ // #783 PR-f (stuchapin909): the per-head RMSNorm and the rope fused (bit-identical to the pair, rope_parity+ // check 6; STRATA_NO_NORM_ROPE=1 keeps the two; off on HIP until its parity check passes). K rows of one token+ // are consecutive in `pos`, tokens are NH apart, so a one-token window is the fusable case.+ const bool fuse_nr = native_rope_enabled() && native_norm_rope_usable((int) HD, (int) s.n_rot);+ if (T == 1 && fuse_nr) {+ native_qsa_rms_norm_rope(kcur_, (int) HD, f32("self_attn.k_norm.weight"), kcur_, (int) NKV, (int) HD,+ (int) s.n_rot, EPS, rope_scaling(), pos, cs);+ } else {+ native_qsa_rms_norm_weighted(kcur_, f32("self_attn.k_norm.weight"), kcur_, (int) HD, (int) (T * NKV), EPS, cs);+ for (int t = 0; t < T; ++t) {+ float* kc = kcur_ + t * NKV * HD;+ if (native_rope_enabled()) native_rope_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, rope_scaling(), pos + t * NH, cs);+ else rope_neox_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, st_.cos_tab, st_.sin_tab, pos + t * NH, cs);+ }+ }+ if (st_.kv_rot) { // rotated K and V (kv_q4.hpp): Q4_0, and INT8 with STRATA_KV_ROT=1+ fwht256_inplace_cuda(kcur_, (int64_t) T * NKV, cs);+ fwht256_inplace_cuda(vcur_, (int64_t) T * NKV, cs);+ }+ // stored in the state's own format (#293 appended rotated INT8 K/V as Q4_0, into pools INT8 never has)+ if (!no_batch_kv && st_.kv_q4)+ kv_append_q4_steps(st_.k_q4, st_.v_q4, st_.page_table, step, 4, T, kcur_, vcur_, s, cs, &st_.host);+ else if (!no_batch_kv && st_.kv_int8)+ kv_append_q8_steps(st_.k_q, st_.v_q, st_.k_scale, st_.v_scale, st_.page_table, step, 4, kcur_, vcur_,+ (int) (NKV * HD), T, s, cs, &st_.host);+ else+ for (int t = 0; t < T; ++t) {+ if (st_.kv_q4)+ kv_append_q4_step(st_.k_q4, st_.v_q4, st_.page_table, step + t * 4, kcur_ + t * NKV * HD,+ vcur_ + t * NKV * HD, s, cs, &st_.host);+ else if (st_.kv_int8)+ kv_append_q8_step(st_.k_q, st_.v_q, st_.k_scale, st_.v_scale, st_.page_table, step + t * 4,+ kcur_ + t * NKV * HD, vcur_ + t * NKV * HD, s, cs, &st_.host);+ else+ kv_append_step(st_.k_pool, st_.v_pool, st_.page_table, step + t * 4, kcur_ + t * NKV * HD,+ vcur_ + t * NKV * HD, s, cs, &st_.host);+ }+ } catch (const std::exception& e) {+ err = std::string("mtp: ") + e.what();+ return false;+ }+ return true;+}++void MtpDrafter::norm_rope(float* data, const float* gamma, int rows, int cols, const int32_t* p, cudaStream_t cs) {+ using namespace strata::kernels;+ const QsaShapes s = shapes_of(*g_);+ native_qsa_rms_norm_weighted(data, gamma, data, cols, rows, EPS, cs);+ if (native_rope_enabled()) native_rope_apply(data, data, rows, cols, (int) s.n_rot, rope_scaling(), p, cs);+ else rope_neox_apply(data, data, rows, cols, (int) s.n_rot, st_.cos_tab, st_.sin_tab, p, cs);+}++bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) {+ using namespace strata::kernels;+ const ModelGeometry& g = *g_;+ SessionState& ss = *ss_;+ const int T = 1;+ const int64_t N = g.n_embd, HC = g.hc, K = ss.k, NH = g.n_head, HD = g.head_dim;+ const QsaShapes s = shapes_of(g);+ const GrShapes gs{g.n_embd, g.hc, g.hc_lr};+ const int32_t* step = step_ + step_row * 4;+ const int32_t* pos = pos_ + step_row * NH;+ // --mtp-q4: the q and o projections' format and weights (Q8_0 unless the Q4_0 copies exist)+ int wt_q_proj, wt_o_proj;+ const void* wp_q_proj = wq("self_attn.q_proj.weight", wt_q_proj);+ const void* wp_o_proj = wq("self_attn.o_proj.weight", wt_o_proj);+ const bool fuse_nr = native_rope_enabled() && native_norm_rope_usable((int) HD, (int) s.n_rot); // #783 PR-f+ try {+ // ---- dense attention over every cell+ native_mmvq(wt_q_proj, wp_q_proj, xq_, qfull_, (int) N, (int) (NH * 2 * HD), T, cs);+ // all T tokens' q rows at once (the rows of token t sit at pos[t * NH ..], so row r reads pos[r])+ if (fuse_nr) {+ native_qsa_rms_norm_rope(qfull_, (int) (2 * HD), f32("self_attn.q_norm.weight"), qcur_, (int) (T * NH),+ (int) HD, (int) s.n_rot, EPS, rope_scaling(), pos, cs);+ } else {+ if (cudaMemcpy2DAsync(qcur_, (size_t) HD * 4, qfull_, (size_t) HD * 2 * 4, (size_t) HD * 4,+ (size_t) (T * NH), cudaMemcpyDeviceToDevice, cs) != cudaSuccess) {+ err = "mtp: q split failed";+ return false;+ }+ norm_rope(qcur_, f32("self_attn.q_norm.weight"), (int) (T * NH), (int) HD, pos, cs);+ }+ if (st_.kv_rot) fwht256_inplace_cuda(qcur_, (int64_t) T * NH, cs);+ const QsaAttnPools pools = qsa_attn_pools(st_);+ if (window_ > 0) window_ids(const_cast<int32_t*>(step), T, (int) window_, ident_, cap_, cs);+ qsa_decode_attn_batch(qcur_, pools, ident_, step, cap_, s, attn_scratch_, attn_, T, cs);+ if (st_.kv_rot) fwht256_inplace_cuda(attn_, (int64_t) T * NH, cs);+ native_qsa_gate_apply(attn_, qfull_, attn32_, (int) (T * NH), (int) HD, cs);+ native_quantize_q8_1(attn32_, xq_, (int) (NH * HD), T, cs);+ native_mmvq(wt_o_proj, wp_o_proj, xq_, bo_, (int) (NH * HD), (int) N, T, cs);+ // ---- the MLP hyper-connection (the attention write folded in)+ {+ FusedGrArgs fa[kFusedGrMaxT];+ for (int t = 0; t < T; ++t) {+ fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = true;+ fa[t].bo_prev = bo_ + t * N; fa[t].inj_prev = inj_ + t * HC;+ fa[t].w_norm = f32("mlp_hyper_connection.hc_norm.weight");+ fa[t].w_down = bf16("mlp_hyper_connection.input_mix_weight_down.weight");+ fa[t].w_up = bf16("mlp_hyper_connection.input_mix_weight_up.weight");+ fa[t].w_inject = bf16("mlp_hyper_connection.block_inject_weight.weight");+ fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC;+ fa[t].inject_out = inj2_ + t * HC; fa[t].mixed = mixed_ + t * N;+ }+ fused_gr_read_multi(fa, T, xn_, cs);+ }+ // ---- MoE: router, the 512 resident experts, the shared expert (on a branch beside them on CUDA), the+ // combine, the write+ NativeSharedWeights nsw;+ nsw.gate_data = wq("mlp.shared_expert.gate_proj.weight", nsw.gate_type);+ nsw.up_data = wq("mlp.shared_expert.up_proj.weight", nsw.up_type);+ nsw.down_data = wq("mlp.shared_expert.down_proj.weight", nsw.down_type);+ nsw.q8_1 = xq_; // the routed experts read hit_xq_+ const SForm none{};+ const bool need_bf16_x = !shared_expert_native_bf16_enabled();+ static const bool fuse_head_gr = [] {+ const char* v = std::getenv("STRATA_FUSE_HEAD_GR");+ return v != nullptr && std::atoi(v) != 0;+ }();+ static const bool head_mix_multi_on = [] {+#if defined(STRATA_USE_HIP)+ return false;+#else+ const char* v = std::getenv("STRATA_HEAD_MIX_MULTI");+ return v == nullptr || std::atoi(v) != 0;+#endif+ }();+ // the shared expert only reads mixed_ / xq_ and writes shared_: a branch beside the router and the routed+ // experts, joined before the combine (CUDA; HIP keeps one stream: STRATA_MTP_SHARED_BRANCH=0 does too)+ static const bool branch_on = [] {+#if defined(STRATA_USE_HIP)+ return false;+#else+ const char* v = std::getenv("STRATA_MTP_SHARED_BRANCH");+ return v == nullptr || std::atoi(v) != 0;+#endif+ }();+ cudaStream_t sh_cs = cs;+ if (branch_on) {+ if (cudaEventRecord(sh_fork_, cs) != cudaSuccess || cudaStreamWaitEvent(side_, sh_fork_, 0) != cudaSuccess) {+ err = "mtp: the shared expert's branch";+ return false;+ }+ sh_cs = side_;+ }+ auto shared_rows = [&]() {+ for (int t = 0; t < T; ++t) {+ if (need_bf16_x) f32_to_bf16_bulk(mixed_ + t * N, x_bf16_, N, sh_cs);+ shared_expert(nullptr, nullptr, x_bf16_, none, nullptr, nullptr, nullptr, none, nullptr, nullptr, nullptr,+ none, nullptr, nullptr, nullptr, bf16("mlp.shared_expert_gate.weight"), sh_scratch_,+ shared_ + t * N, N, g.n_ff, 32, sh_cs, mixed_ + t * N, &nsw);+ }+ };+ if (branch_on) {+ shared_rows();+ if (cudaEventRecord(sh_join_, side_) != cudaSuccess) { err = "mtp: the shared expert's branch"; return false; }+ }+ for (int t = 0; t < T; ++t) {+ bf16_gemv_fp32_mmvf(mixed_ + t * N, bf16("mlp.gate.weight"), logits_ + t * g.n_expert, (int) N, (int) g.n_expert, cs);+ if (native_router_enabled()) native_router_top10(logits_ + t * g.n_expert, ids_ + t * K, w_ + t * K, cs);+ else router_top10(logits_ + t * g.n_expert, 1, (int) g.n_expert, (int) K, ids_ + t * K, w_ + t * K, cs);+ }+ moe_group_resident(ids_, (int) (T * K), (int) K, experts_, (int64_t) strata::kernels::cpu::BLOB, grp_ptr_,+ grp_start_, grp_counts_, hit_dst_, hit_slot_, cs);+ quantize_q8_0_scaled(mixed_, hit_xq_, hit_xs_, (int64_t) T * N, cs);+ moe_grouped_s2(grp_ptr_, grp_start_, grp_counts_, hit_dst_, hit_slot_, (int64_t) T * K, (int64_t) T * K, hit_xq_,+ hit_xs_, hit_scratch_, parts_, cs);+ if (branch_on) {+ if (cudaStreamWaitEvent(cs, sh_join_, 0) != cudaSuccess) { err = "mtp: the shared expert's join"; return false; }+ } else {+ shared_rows();+ }+ static const bool no_multi_gr = [] { // #783 PR-g: STRATA_NO_MULTI_GR=1 keeps the per-token GR calls+ const char* v = std::getenv("STRATA_NO_MULTI_GR");+ return v != nullptr && v[0] != ' ' && v[0] != '0';+ }();+ for (int t = 0; t < T; ++t) {+ if (native_moe_combine_enabled())+ native_moe_combine(parts_ + (size_t) t * K * N, w_ + t * K, shared_ + t * N, y_ + t * N, N, K, cs);+ else+ moe_combine(parts_ + (size_t) t * K * N, w_ + t * K, shared_ + t * N, y_ + t * N, N, K, cs);+ if (!fuse_head_gr && no_multi_gr)+ gr_write(R_ + (size_t) t * HC * N, y_ + t * N, inj2_ + t * HC, gs, R_ + (size_t) t * HC * N, cs);+ }+ // #783 PR-g (stuchapin909): the T residual writes in one launch (each token's R, block output and injection are+ // its own, so doing them after the loop changes nothing)+ if (!fuse_head_gr && !no_multi_gr)+ gr_write_multi(R_, y_, inj2_, gs, R_, T, cs);+ // ---- the final mixer and the main model's head+ if (fuse_head_gr) {+ FusedGrArgs fa[kFusedGrMaxT];+ for (int t = 0; t < T; ++t) {+ fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = true;+ fa[t].bo_prev = y_ + t * N; fa[t].inj_prev = inj2_ + t * HC;+ fa[t].w_norm = f32("hyper_connection_mixer.hc_norm.weight");+ fa[t].w_down = bf16("hyper_connection_mixer.input_mix_weight_down.weight");+ fa[t].w_up = bf16("hyper_connection_mixer.input_mix_weight_up.weight");+ fa[t].w_inject = nullptr;+ fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC;+ fa[t].inject_out = dummy_inj_; fa[t].mixed = sample_ + t * N;+ }+ fused_gr_read_multi(fa, T, xn_, cs);+ } else if (head_mix_multi_on) {+ // the window's rows in one read (CUDA; HIP keeps gr_read per row): bitwise the same sums+ FusedGrArgs fa[kFusedGrMaxT];+ for (int t = 0; t < T; ++t) {+ fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = false;+ fa[t].w_norm = f32("hyper_connection_mixer.hc_norm.weight");+ fa[t].w_down = bf16("hyper_connection_mixer.input_mix_weight_down.weight");+ fa[t].w_up = bf16("hyper_connection_mixer.input_mix_weight_up.weight");+ fa[t].eps = EPS; fa[t].lo = lo_ + t * g.hc_lr; fa[t].rs = rs_ + t * HC; fa[t].mixed = sample_ + t * N;+ }+ fused_gr_read_multi(fa, T, xn_, cs);+ } else {+ for (int t = 0; t < T; ++t)+ gr_read(R_ + (size_t) t * HC * N, f32("hyper_connection_mixer.hc_norm.weight"),+ bf16("hyper_connection_mixer.input_mix_weight_down.weight"),+ bf16("hyper_connection_mixer.input_mix_weight_up.weight"), nullptr, EPS, gs, ss.block.gr,+ sample_ + t * N, dummy_inj_, cs);+ }+ native_quantize_q8_1(sample_, xq_, (int) N, T, cs);+ const bool sub = dhead_ != nullptr;+ const int64_t nv = sub ? n_dvocab_ : n_vocab_;+ native_mmvq(sub ? dhead_type_ : head_->type(), sub ? dhead_ : head_->weights(), xq_, head_logits_, (int) N,+ (int) nv, T, cs);+ if (coupled_rec_) {+ // coupled draft sampling: the target's chain and Philox draw for the row that will verify this draft+ // (counter = this cell + 1, from its step record), penalties over the ring; T == 1 (full layer)+ coupled_draft_sample(head_logits_, (int) nv, sub ? dvocab_ : nullptr, sub ? dinv_ : nullptr, (int) n_vocab_,+ cparams_, cring_, kCoupledHistCap, coupled_j_, step, cscratch_, out_ids_, probs_, cs);+ return true;+ }+ if (argmax_rows_wanted()) {+ argmax_rows(head_logits_, T, (int) nv, arg_scratch_, out_ids_, cs);+ } else {+ SamplerParams sp;+ sp.greedy = true;+ sp.temperature = 0.0f;+ sample_tokens(head_logits_, T, (int) nv, nullptr, 0, sp, out_ids_, cs);+ }+ if (multi_block_head_ops()) row_top_prob_split(head_logits_, T, (int) nv, out_ids_, probs_, top_scratch_, cs);+ else row_top_prob(head_logits_, T, (int) nv, out_ids_, probs_, cs);+ if (sub) map_ids(out_ids_, dvocab_, T, cs);+ } catch (const std::exception& e) {+ err = std::string("mtp: ") + e.what();+ return false;+ }+ return true;+}++namespace {+bool finish_capture(cudaStream_t cs, bool ok, cudaGraphExec_t& exec, const char* what, std::string& err) {+ cudaGraph_t graph = nullptr;+ const cudaError_t ce = cudaStreamEndCapture(cs, &graph);+ if (!ok) {+ if (graph) cudaGraphDestroy(graph);+ return false;+ }+ if (ce != cudaSuccess || cudaGraphInstantiate(&exec, graph, 0) != cudaSuccess) {+ if (graph) cudaGraphDestroy(graph);+ err = std::string("mtp: ") + what + " capture: " + cudaGetErrorString(ce);+ return false;+ }+ cudaGraphDestroy(graph);+ // an explicit upload: the first launch's implicit one blocked behind a device-side spin (verify.cpp)+ cudaGraphUpload(exec, cs);+ cudaStreamSynchronize(cs);+ return true;+}+} // namespace++bool MtpDrafter::capture_prefill(int T, std::string& err) {+ if (prefill_exec_[T]) return true;+ using namespace strata::kernels;+ if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }+ copy_i32_from_mapped(tok_, m_tok_, T, cs_);+ copy_i32_from_mapped(step_, m_step_, (int64_t) T * 4, cs_);+ copy_i32_from_mapped(pos_, m_pos_, (int64_t) T * g_->n_head, cs_);+ const bool ok = record_forward(T, -1, cs_, err); // K/V only, rows [0, T)+ return finish_capture(cs_, ok, prefill_exec_[T], "prefill", err);+}++bool MtpDrafter::capture_prefill_dev(int T, std::string& err) {+ if (prefill_dev_exec_[T]) return true;+ if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }+ const bool ok = record_forward(T, -1, cs_, err); // K/V only, rows [0, T); tok_/step_/pos_ filled before launch+ return finish_capture(cs_, ok, prefill_dev_exec_[T], "prefill (device inputs)", err);+}++bool MtpDrafter::capture_round(int T, bool coupled, std::string& err) {+ cudaGraphExec_t& exec = coupled ? round_exec_c_[T] : round_exec_[T];+ if (exec) return true;+ using namespace strata::kernels;+ const int64_t HCN = g_->hc * g_->n_embd;+ if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }+ bool ok = true;+ // coupled: the request's chain and the penalty history's base, for this round's drafts+ if (coupled) coupled_draft_stage(m_cparams_, m_chist_, cparams_, cring_, kCoupledHistCap, cs_);+ // the catch-up: the layer's front for the window's T cells (their K/V), then its rest for row a only, on row+ // a's intermediates copied to row 0, at the cell the host staged in step row 2*max_t - 1; the draft chain is+ // one graph per step (`capture_step`) so the host can stop it when a draft is unlikely+ const int ra = 2 * max_t_ - 1;+ const int64_t N = g_->n_embd, NH = g_->n_head;+#if defined(STRATA_USE_HIP) // AMD keeps its separate copies+ copy_i32_from_mapped(tok_, m_tok_, T, cs_);+ copy_i32_from_mapped(step_, m_step_, (int64_t) 2 * T * 4, cs_);+ copy_i32_from_mapped(pos_, m_pos_, (int64_t) 2 * T * NH, cs_);+ copy_i32_from_mapped(row_, m_row_, 2, cs_);+ copy_from_mapped(Rin_, window_R_, (int64_t) T * HCN, cs_);+ copy_i32_from_mapped(step_ + ra * 4, m_step_ + ra * 4, 4, cs_);+ copy_i32_from_mapped(pos_ + ra * NH, m_pos_ + ra * NH, NH, cs_);+#else // the draft round's seven inputs in one launch+ const MappedCopy in[7] = {{tok_, m_tok_, T}, {step_, m_step_, (int64_t) 2 * T * 4}, {pos_, m_pos_, 2 * T * NH},+ {row_, m_row_, 2}, {Rin_, window_R_, T * HCN}, {step_ + ra * 4, m_step_ + ra * 4, 4},+ {pos_ + ra * NH, m_pos_ + ra * NH, NH}};+ copy_from_mapped_multi(in, 7, cs_);+#endif+ ok = record_front(T, 0, cs_, err);+ if (ok) {+ if (T > 1) {+ copy_row_to_first(row_, R_, HCN, inj_, g_->hc, mixed_, N, cs_);+ native_quantize_q8_1(mixed_, xq_, (int) N, 1, cs_);+ }+ coupled_rec_ = coupled;+ coupled_j_ = 0;+ ok = record_rest(ra, cs_, err);+ coupled_rec_ = false;+ }+ if (ok) mtp_select(R_, HCN, out_ids_, row_ + 1, Rin_, tok_, m_out_, 0, cs_, probs_, m_prob_);+ if (ok && force_on_ && !coupled) force_token(tok_, m_force_, 0, cs_); // chain_launch: step 1's input+ return finish_capture(cs_, ok, exec, coupled ? "round (coupled)" : "round", err);+}++// Chain step j (1..max_t-2): one row at the cell staged in step row `max_t + j - 1`, from the previous step's+// residual and token (left in Rin_[0] / tok_[0] by mtp_select); draft j and its probability to the mapped outputs.+bool MtpDrafter::capture_step(int j, bool coupled, std::string& err) {+ cudaGraphExec_t& exec = coupled ? step_exec_c_[j] : step_exec_[j];+ if (exec) return true;+ using namespace strata::kernels;+ const int64_t HCN = g_->hc * g_->n_embd;+ const int row = max_t_ + j - 1;+ if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }+#if defined(STRATA_USE_HIP)+ copy_i32_from_mapped(step_ + row * 4, m_step_ + row * 4, 4, cs_);+ copy_i32_from_mapped(pos_ + row * g_->n_head, m_pos_ + row * g_->n_head, g_->n_head, cs_);+#else+ const MappedCopy in[2] = {{step_ + row * 4, m_step_ + row * 4, 4},+ {pos_ + row * g_->n_head, m_pos_ + row * g_->n_head, g_->n_head}};+ copy_from_mapped_multi(in, 2, cs_);+#endif+ coupled_rec_ = coupled;+ coupled_j_ = j;+ bool ok = record_forward(1, row, cs_, err);+ coupled_rec_ = false;+ if (ok) mtp_select(R_, HCN, out_ids_, row_ + 1, Rin_, tok_, m_out_, j, cs_, probs_, m_prob_);+ if (ok && force_on_ && !coupled) force_token(tok_, m_force_, j, cs_); // chain_launch: step j+1's input+ return finish_capture(cs_, ok, exec, coupled ? "step (coupled)" : "step", err);+}++void MtpDrafter::kv_restore(int64_t upto) {+ const OnDevice on_device(device_);+ if (st_.kv_mode != 2 || upto <= 0) return;+ // the ring's blocks below `upto`, from the host copy: a checkpoint resume may have left later cells in them+ const strata::kernels::QsaShapes s = shapes_of(*g_);+ const int64_t b1 = (upto + s.page_size - 1) / s.page_size, b0 = std::max<int64_t>(0, b1 - st_.n_slots);+ strata::kernels::kv_ring_restore(qsa_attn_pools(st_), st_.host, qsa_kv_format(st_), b0, b1, st_.n_slots, s, cs_);+ cudaStreamSynchronize(cs_);+}++bool MtpDrafter::prepare_prefill(std::string& err) {+ const OnDevice on_device(device_);+ const int64_t per_row = 1 + 4 + g_->n_head;+ if (pf_cap_ < (int64_t) max_t_ * per_row) {+ if (pf_dev_) cudaFree(pf_dev_);+ pf_dev_ = nullptr;+ pf_cap_ = 0;+ if (cudaMalloc((void**) &pf_dev_, (size_t) (max_t_ * per_row) * sizeof(int32_t)) != cudaSuccess) {+ err = "mtp prefill: the input records do not fit";+ return false;+ }+ pf_cap_ = max_t_ * per_row;+ }+ for (int T = 1; T <= max_t_; ++T)+ if (!capture_prefill_dev(T, err)) return false; // (STRATA_MTP_PREFILL_SYNC's loop syncs anyway)+ return true;+}++bool MtpDrafter::prefill(const float* R_rows, const int32_t* next_tokens, int64_t n, int64_t cell0, std::string& err,+ bool sync) {+ const OnDevice on_device(device_);+ const Clock::time_point t0 = Clock::now();+ const int64_t HCN = g_->hc * g_->n_embd;+ // cells the window can never reach again need no K/V+ const int64_t first_needed = (window_ > 0 && prompt_len_ > 0) ? prompt_len_ - window_ - 64 : 0;+ // E-4: every group's token / step / position records uploaded at once; each group is then device copies and a+ // graph on the one stream, with a single sync at the end (a group of <= max_t rows used to be staged in mapped+ // memory and synced before the next: ~5,500 host round trips on a 32K prompt). The same work in the same+ // order. STRATA_MTP_PREFILL_SYNC=1 keeps the old loop, =0 forces E-4.+ // HIP defaults to the old loop. This pass runs whenever E-9 (Prefill::draft_kv) declines, which it does for the+ // drafter's ring (KV streaming, --kv-resident), and on gfx1201 / ROCm 7.2.4 the E-4 queue (a graph launch and four+ // device copies per group, ~2,000 groups per 8192-token chunk, no sync) sometimes never completes: the prompt hangs+ // in hipGraphLaunch or the final sync until the watchdog ends the engine. R9700, full IQ3_XXS, --kv-resident+ // 32768, mixed load (chats, 32K and 7K prompts, deep follow-ups): E-4 hung in the first round on 2 of 2 tries,+ // the old loop ran 30 of 30 rounds clean, prompt speed unchanged.+ static const bool per_group_sync = [] {+ if (const char* v = std::getenv("STRATA_MTP_PREFILL_SYNC")) return std::atoi(v) != 0;+#if defined(STRATA_USE_HIP)+ return true;+#else+ return false;+#endif+ }();+ const int64_t NHp = g_->n_head, per_row = 1 + 4 + NHp;+ if (!per_group_sync && n > 0) {+ if (pf_cap_ < n * per_row) {+ if (pf_dev_) cudaFree(pf_dev_);+ pf_dev_ = nullptr;+ pf_cap_ = 0;+ if (cudaMalloc((void**) &pf_dev_, (size_t) (n * per_row) * sizeof(int32_t)) != cudaSuccess) {+ err = "mtp prefill: the input records do not fit";+ return false;+ }+ pf_cap_ = n * per_row;+ }+ std::vector<int32_t> rec((size_t) (n * per_row));+ int32_t* tk = rec.data();+ int32_t* stp = tk + n;+ int32_t* ps = stp + 4 * n;+ for (int64_t i = 0; i < n; ++i) {+ const int64_t cell = cell0 + i;+ tk[i] = next_tokens[i];+ stp[i * 4 + 0] = (int32_t) cell;+ stp[i * 4 + 1] = (int32_t) (cell + 1);+ stp[i * 4 + 2] = (int32_t) ((cell + 1) / 4);+ stp[i * 4 + 3] = (int32_t) (cell + 1);+ for (int64_t h = 0; h < NHp; ++h) ps[i * NHp + h] = (int32_t) cell;+ }+ if (cudaMemcpyAsync(pf_dev_, rec.data(), rec.size() * sizeof(int32_t), cudaMemcpyHostToDevice, cs_) != cudaSuccess) {+ err = "mtp prefill: the input records upload failed";+ return false;+ }+ const int32_t* d_tk = pf_dev_;+ const int32_t* d_stp = d_tk + n;+ const int32_t* d_ps = d_stp + 4 * n;+ for (int64_t c = 0; c < n; c += max_t_) {+ const int T = (int) std::min<int64_t>(max_t_, n - c);+ if (cell0 + c + T <= first_needed) continue;+ if (!capture_prefill_dev(T, err)) return false;+ if (cudaMemcpyAsync(tok_, d_tk + c, (size_t) T * 4, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||+ cudaMemcpyAsync(step_, d_stp + c * 4, (size_t) T * 16, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||+ cudaMemcpyAsync(pos_, d_ps + c * NHp, (size_t) (T * NHp) * 4, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||+ cudaMemcpyAsync(Rin_, R_rows + (size_t) c * HCN, (size_t) T * HCN * sizeof(float), cudaMemcpyDeviceToDevice,+ cs_) != cudaSuccess ||+ cudaGraphLaunch(prefill_dev_exec_[T], cs_) != cudaSuccess) {+ err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ }+ if (!sync) { // the caller orders on stream(); the records are staged (a pageable copy returns once taken)+ (void) cudaStreamQuery(cs_);+ ms_prefill += ms_since(t0);+ return true;+ }+ if (cudaStreamSynchronize(cs_) != cudaSuccess) {+ err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ ms_prefill += ms_since(t0);+ return true;+ }+ for (int64_t c = 0; c < n; c += max_t_) {+ const int T = (int) std::min<int64_t>(max_t_, n - c);+ if (cell0 + c + T <= first_needed) continue;+ if (!capture_prefill(T, err)) return false;+ for (int t = 0; t < T; ++t) {+ const int64_t cell = cell0 + c + t;+ h_tok_[t] = next_tokens[c + t];+ h_step_[t * 4 + 0] = (int32_t) cell;+ h_step_[t * 4 + 1] = (int32_t) (cell + 1);+ h_step_[t * 4 + 2] = (int32_t) ((cell + 1) / 4);+ h_step_[t * 4 + 3] = (int32_t) (cell + 1);+ for (int64_t h = 0; h < g_->n_head; ++h) h_pos_[t * g_->n_head + h] = (int32_t) cell;+ }+ if (cudaMemcpyAsync(Rin_, R_rows + (size_t) c * HCN, (size_t) T * HCN * sizeof(float), cudaMemcpyDeviceToDevice,+ cs_) != cudaSuccess ||+ cudaGraphLaunch(prefill_exec_[T], cs_) != cudaSuccess ||+ cudaStreamSynchronize(cs_) != cudaSuccess) {+ err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ }+ ms_prefill += ms_since(t0);+ return true;+}++bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* drafts, std::string& err,+ float* probs, float min_p, int* n_drafts) {+ const OnDevice on_device(device_);+ if (T < 1 || T > max_t_ || a < 0 || a >= T) { err = "mtp: draft arguments out of range"; return false; }+ if (chain_live_) { err = "mtp: a pipelined chain is still in flight"; return false; }+ const bool cp = coupled_active_; // coupled draft sampling for this request: its own graphs+ if (!capture_round(T, cp, err)) return false;+ const int max_steps = std::min(max_t_ - 1, max_drafts_);+ for (int j = 1; j < max_steps; ++j)+ if (!capture_step(j, cp, err)) return false;+ const Clock::time_point t0 = Clock::now();+ const int64_t NH = g_->n_head;+ auto put = [&](int row, int64_t cell) {+ h_step_[row * 4 + 0] = (int32_t) cell;+ h_step_[row * 4 + 1] = (int32_t) (cell + 1);+ h_step_[row * 4 + 2] = (int32_t) ((cell + 1) / 4);+ h_step_[row * 4 + 3] = (int32_t) (cell + 1);+ for (int64_t h = 0; h < NH; ++h) h_pos_[row * NH + h] = (int32_t) cell;+ };+ for (int t = 0; t < T; ++t) {+ h_tok_[t] = tokens[t];+ put(t, p + t);+ }+ put(2 * max_t_ - 1, coupled_draft_cell(p, a, 0)); // p + a: draft 0's cell+ for (int j = 1; j < max_steps; ++j)+ put(max_t_ + j - 1, coupled_draft_cell(p, a, j));+ for (int j = 0; j < max_steps; ++j) ((volatile int32_t*) h_out_)[j] = -1;+ h_row_[0] = a;+ h_row_[1] = 0;+ if (h_force_ != nullptr) for (int j = 0; j < 16; ++j) h_force_[j] = -1; // the forcing kernels: no-ops here+ std::atomic_thread_fence(std::memory_order_seq_cst);+ if (!stage_source_R(T, err)) return false;++ SessionState* const pss = ple_ss_ ? ple_ss_ : ss_;+ const bool do_ple = pss != nullptr && pss->ple.ready() && pss->ple.table != nullptr;+ int32_t ple_prev[2] = {do_ple ? pss->ple_prev[0] : 0, do_ple ? pss->ple_prev[1] : 0};+ auto prefetch_ple = [&](int32_t tok) {+ if (!do_ple || tok < 0) return;+ uint32_t rows16[strata::kernels::PLE_N_HEADS];+ strata::kernels::ngram_rows(&tok, ple_prev, 1, pss->ple.consts, rows16);+ ple_prev[0] = ple_prev[1];+ ple_prev[1] = tok;+ pss->ple.table->prefetch_rows(rows16);+ };++ int n = 0;+ if (min_p <= 0.0f && max_steps > 0) {+ if (cudaGraphLaunch(cp ? round_exec_c_[T] : round_exec_[T], cs_) != cudaSuccess) {+ err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ for (int j = 1; j < max_steps; ++j) {+ if (cudaGraphLaunch(cp ? step_exec_c_[j] : step_exec_[j], cs_) != cudaSuccess) {+ err = std::string("mtp draft step: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ }+ (void) cudaStreamQuery(cs_);+ prefetch_ple(tokens[a]);+ for (int j = 0; j + 1 < max_steps; ++j) {+ uint32_t spins = 0;+ while (((volatile int32_t*) h_out_)[j] < 0) {+#if defined(_WIN32) || defined(__x86_64__)+ _mm_pause();+#endif+ if ((++spins & 1023u) == 0 && cudaStreamQuery(cs_) != cudaErrorNotReady) break;+ }+ drafts[j] = ((volatile int32_t*) h_out_)[j];+ if (probs) probs[j] = ((volatile float*) h_prob_)[j];+ prefetch_ple(drafts[j]);+ }+ if (cudaStreamSynchronize(cs_) != cudaSuccess) {+ err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ const int last = max_steps - 1;+ drafts[last] = ((volatile int32_t*) h_out_)[last];+ if (probs) probs[last] = ((volatile float*) h_prob_)[last];+ prefetch_ple(drafts[last]);+ n = max_steps;+ } else if (max_steps > 0) {+ if (cudaGraphLaunch(cp ? round_exec_c_[T] : round_exec_[T], cs_) != cudaSuccess) {+ err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ (void) cudaStreamQuery(cs_);+ prefetch_ple(tokens[a]);+ auto wait_step = [&](int j) {+ uint32_t spins = 0;+ while (((volatile int32_t*) h_out_)[j] < 0) {+#if defined(_WIN32) || defined(__x86_64__)+ _mm_pause();+#endif+ if ((++spins & 1023u) == 0 && cudaStreamQuery(cs_) != cudaErrorNotReady) break;+ }+ };+ wait_step(0);+ drafts[0] = ((volatile int32_t*) h_out_)[0];+ float pj = ((volatile float*) h_prob_)[0];+ if (probs) probs[0] = pj;+ n = 1;+ for (int j = 1; j < max_steps && pj >= min_p; ++j) {+ if (cudaGraphLaunch(cp ? step_exec_c_[j] : step_exec_[j], cs_) != cudaSuccess) {+ err = std::string("mtp draft step: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ (void) cudaStreamQuery(cs_);+ prefetch_ple(drafts[j - 1]);+ wait_step(j);+ drafts[j] = ((volatile int32_t*) h_out_)[j];+ pj = ((volatile float*) h_prob_)[j];+ if (probs) probs[j] = pj;+ ++n;+ }+ prefetch_ple(drafts[n - 1]);+ if (cudaStreamSynchronize(cs_) != cudaSuccess) {+ err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ }+ for (int j = n; j < max_t_ - 1; ++j) { drafts[j] = 0; if (probs) probs[j] = 0.0f; }+ if (n_drafts) *n_drafts = n;+ ms_draft += ms_since(t0);+ ++rounds;+ return true;+}++bool MtpDrafter::stage_source_R(int T, std::string& err) {+ if (src_R_ == nullptr || src_R_ == window_R_) return true;+ if (cudaMemcpyAsync((void*) window_R_, src_R_, (size_t) T * (size_t) (g_->hc * g_->n_embd) * sizeof(float),+ cudaMemcpyDeviceToDevice, cs_) != cudaSuccess) {+ err = "mtp: staging the window's residual rows failed";+ return false;+ }+ return true;+}++bool MtpDrafter::prepare_chain(std::string& err) {+ const OnDevice on_device(device_);+ if (!force_on_ || h_force_ == nullptr) { err = "mtp: chains need set_force_capture before load"; return false; }+ for (int T = 1; T <= max_t_; ++T)+ if (!capture_round(T, false, err)) return false;+ for (int j = 1; j <= max_t_ - 2; ++j)+ if (!capture_step(j, false, err)) return false;+ if (ev_chain_ == nullptr && cudaEventCreateWithFlags(&ev_chain_, cudaEventDisableTiming) != cudaSuccess) {+ err = "mtp: event creation failed";+ return false;+ }+ for (cudaEvent_t& e : ev_step_)+ if (e == nullptr && cudaEventCreateWithFlags(&e, cudaEventDisableTiming) != cudaSuccess) {+ err = "mtp: event creation failed";+ return false;+ }+ return true;+}++bool MtpDrafter::chain_launch(int T, const int32_t* tokens, int64_t p, int a, const int32_t* force, int n_force,+ int n_out, int n_early, std::string& err) {+ const OnDevice on_device(device_);+ if (chain_live_) { err = "mtp: a chain is already in flight"; return false; }+ if (!force_on_ || ev_chain_ == nullptr) { err = "mtp: chains need prepare_chain"; return false; }+ if (T < 1 || T > max_t_ || a < 0 || a >= T || n_out < 1 || n_out > max_t_ - 1 || n_force < 0 || n_force > n_out ||+ n_force > 16) {+ err = "mtp: chain arguments out of range";+ return false;+ }+ if (round_exec_[T] == nullptr) { err = "mtp: chain graphs not prepared"; return false; }+ for (int j = 1; j < n_out; ++j)+ if (step_exec_[j] == nullptr) { err = "mtp: chain graphs not prepared"; return false; }+ const Clock::time_point t0 = Clock::now();+ const int64_t NH = g_->n_head;+ auto put = [&](int row, int64_t cell) {+ h_step_[row * 4 + 0] = (int32_t) cell;+ h_step_[row * 4 + 1] = (int32_t) (cell + 1);+ h_step_[row * 4 + 2] = (int32_t) ((cell + 1) / 4);+ h_step_[row * 4 + 3] = (int32_t) (cell + 1);+ for (int64_t h = 0; h < NH; ++h) h_pos_[row * NH + h] = (int32_t) cell;+ };+ for (int t = 0; t < T; ++t) {+ h_tok_[t] = tokens[t];+ put(t, p + t);+ }+ put(2 * max_t_ - 1, p + a); // output 0's cell+ for (int j = 1; j < n_out; ++j) put(max_t_ + j - 1, p + a + j); // every step's cell, staged at once+ for (int j = 0; j < 16; ++j) h_force_[j] = j < n_force ? force[j] : -1;+ h_row_[0] = a;+ h_row_[1] = 0;+ std::atomic_thread_fence(std::memory_order_seq_cst);+ if (!stage_source_R(T, err)) return false;+ chain_early_ = std::max(0, std::min(n_early, n_out));+ bool ok = cudaGraphLaunch(round_exec_[T], cs_) == cudaSuccess;+ if (ok && chain_early_ >= 1) ok = cudaEventRecord(ev_step_[0], cs_) == cudaSuccess;+ for (int j = 1; ok && j < n_out; ++j) {+ ok = cudaGraphLaunch(step_exec_[j], cs_) == cudaSuccess;+ if (ok && j < chain_early_) ok = cudaEventRecord(ev_step_[j], cs_) == cudaSuccess;+ }+ if (!ok || cudaEventRecord(ev_chain_, cs_) != cudaSuccess) {+ err = std::string("mtp chain: ") + cudaGetErrorString(cudaGetLastError());+ return false;+ }+ (void) cudaStreamQuery(cs_); // WDDM: submit now+ steps_seen_ = 0;+ chain_live_ = true;+ chain_n_ = n_out;+ ms_draft += ms_since(t0);+ ++rounds;+ return true;+}++int MtpDrafter::chain_outputs_ready(std::string& err) {+ if (!chain_live_) return chain_n_;+ const OnDevice on_device(device_);+ while (steps_seen_ < chain_early_) {+ const cudaError_t q = cudaEventQuery(ev_step_[steps_seen_]);+ if (q == cudaErrorNotReady) break;+ if (q != cudaSuccess) { err = std::string("mtp chain: ") + cudaGetErrorString(q); return -1; }+ chain_tok_[steps_seen_] = ((volatile int32_t*) h_out_)[steps_seen_];+ chain_prob_[steps_seen_] = ((volatile float*) h_prob_)[steps_seen_];+ ++steps_seen_;+ }+ return steps_seen_;+}++int MtpDrafter::chain_poll(std::string& err) {+ if (!chain_live_) return 1;+ const OnDevice on_device(device_);+ const cudaError_t q = cudaEventQuery(ev_chain_);+ if (q == cudaErrorNotReady) return 0;+ chain_live_ = false;+ if (q != cudaSuccess) { err = std::string("mtp chain: ") + cudaGetErrorString(q); return -1; }+ for (int j = 0; j < chain_n_; ++j) {+ chain_tok_[j] = ((volatile int32_t*) h_out_)[j];+ chain_prob_[j] = ((volatile float*) h_prob_)[j];+ }+ for (int j = chain_n_; j < 8; ++j) { chain_tok_[j] = 0; chain_prob_[j] = 0.0f; }+ return 1;+}++bool MtpDrafter::draft_first(int T, const float* R_row, int32_t token, int64_t cell, int32_t* drafts, std::string& err,+ float* probs, float min_p, int* n_drafts) {+ const OnDevice on_device(device_);+ // row 0 is the real pair; rows 1.. repeat it and only write cells the next round overwrites+ const int64_t HCN = g_->hc * g_->n_embd;+ for (int t = 0; t < T; ++t)+ if (cudaMemcpy((void*) (window_R_ + (size_t) t * HCN), R_row, (size_t) HCN * sizeof(float),+ cudaMemcpyDeviceToDevice) != cudaSuccess) {+ err = "mtp: staging the first residual failed";+ return false;+ }+ std::vector<int32_t> toks((size_t) T, token);+ return draft(T, toks.data(), cell, 0, drafts, err, probs, min_p, n_drafts);+}++} // namespace strata::corediff --git a/src/core/verify.cpp b/src/core/verify.cppindex 7a6014540..c2a411a05 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -89,6 +89,7 @@ const bool g_coherent = env_on("STRATA_VERIFY_COHERENT");const bool g_doorbell_store = env_on("STRATA_DOORBELL_STORE");#endifconst bool g_trace = env_on("STRATA_VERIFY_TRACE");+const bool g_no_multi_gr = env_on("STRATA_NO_MULTI_GR"); // #783 PR-g: per-token GR reads/writes and generic-T kernels againconst bool g_no_batch_kv = env_on("STRATA_NO_BATCH_KV_STEP"); // #783 PR-d: per-token K/V and indexer appends again// The shared expert runs on its own stream, forked off and joined back per layer (`sh_fork` in record_window). The@@ -774,9 +775,12 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {return false;}}+ // #783 PR-g (stuchapin909): the window's residual writes in one launch (STRATA_NO_MULTI_GR=1: per token)+ const bool multi_write = dec_batch && !g_no_multi_gr && n > 1;+ if (multi_write) gr_write_multi(Rt(tb), bo_ + tb * N, inj2_ + tb * HC, gs, Rt(tb), n, cs);for (int t = tb; t < te; ++t) {if (batched) { pw.pre_key = ple_key_ + (size_t) t * NG_HC_DIM; pw.pre_value = ple_val_ + t * N; }- gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);+ if (!multi_write) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);PleOut po;po.normalized = normalized;po.result = Rt(t);@@ -1300,7 +1304,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {stamp(l, 24, grp);if (l == g.n_layers - 1) {if (!fuse_head_gr) {- for (int t = tb; t < te; ++t) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);+ if (dec_batch && !g_no_multi_gr) gr_write_multi(Rt(tb), bo_ + tb * N, inj2_ + tb * HC, gs, Rt(tb), n, cs);+ else for (int t = tb; t < te; ++t) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);if (cvec().covers(l)) cvec_apply(Rt(tb), l, n, HC * N, nullptr, 0, nullptr, 0, false, cs);}} else if (cvec().covers(l)) {diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cuindex 23b37959b..a2c2bc298 100644--- a/src/kernels/cuda/fused_gr.cu+++ b/src/kernels/cuda/fused_gr.cu@@ -36,17 +36,37 @@ __device__ __forceinline__ float warp_sum(float v) {}__device__ __forceinline__ float sigmoidf_(float x) { return 1.0f / (1.0f + __expf(-x)); }-// 8 bf16 packed in a uint4 against 8 floats.-__device__ __forceinline__ float dot8(const uint4 w, const float* x) {+// 8 bf16 packed in a uint4, unpacked to two float4 once when reused across tokens.+struct Bf16x8 { float4 a, b; };+__device__ __forceinline__ Bf16x8 unpack8(const uint4 w) {+ return {+ make_float4(__uint_as_float(w.x << 16), __uint_as_float(w.x & 0xffff0000u),+ __uint_as_float(w.y << 16), __uint_as_float(w.y & 0xffff0000u)),+ make_float4(__uint_as_float(w.z << 16), __uint_as_float(w.z & 0xffff0000u),+ __uint_as_float(w.w << 16), __uint_as_float(w.w & 0xffff0000u))+ };+}+__device__ __forceinline__ float dot8u(const Bf16x8& w, const float4 x0, const float4 x1) {float acc = 0.0f;- const uint32_t v[4] = {w.x, w.y, w.z, w.w};-#pragma unroll- for (int j = 0; j < 4; ++j) {- acc = fmaf(__uint_as_float(v[j] << 16), x[2 * j], acc);- acc = fmaf(__uint_as_float(v[j] & 0xffff0000u), x[2 * j + 1], acc);- }+ acc = fmaf(w.a.x, x0.x, acc);+ acc = fmaf(w.a.y, x0.y, acc);+ acc = fmaf(w.a.z, x0.z, acc);+ acc = fmaf(w.a.w, x0.w, acc);+ acc = fmaf(w.b.x, x1.x, acc);+ acc = fmaf(w.b.y, x1.y, acc);+ acc = fmaf(w.b.z, x1.z, acc);+ acc = fmaf(w.b.w, x1.w, acc);return acc;}+__device__ __forceinline__ float dot8u_ptr(const Bf16x8& w, const float* x) {+ const float4* x4 = reinterpret_cast<const float4*>(x);+ return dot8u(w, x4[0], x4[1]);+}++// 8 bf16 packed in a uint4 against 8 16-byte-aligned floats (same 8-FMA order).+__device__ __forceinline__ float dot8(const uint4 w, const float* x) {+ return dot8u_ptr(unpack8(w), x);+}__global__ void __launch_bounds__(THREADS) gr_down_kernel(FusedGrArgs a) {__shared__ __align__(16) float xn[D];@@ -292,19 +312,20 @@ constexpr int UPM_BLOCKS = N / UPM_COLS; // 160// `gr_up_kernel` for T tokens: each row of w_up read once; the T dots reduced by xor so every lane holds every// sum, and lane k runs token k's epilogue - the T epilogues in parallel instead of one after another.+template <int MAX_T = kFusedGrMaxT, bool EXACT_T = false>__global__ void __launch_bounds__(THREADS) gr_up_multi_kernel(GrMulti m) {- __shared__ __align__(16) float lo[kFusedGrMaxT][LR];- __shared__ float g[kFusedGrMaxT][HC][UPM_COLS];+ __shared__ __align__(16) float lo[MAX_T][LR];+ __shared__ float g[MAX_T][HC][UPM_COLS];const int t = threadIdx.x, lane = t & 31, warp = t >> 5;- const int T = m.T;+ const int T = EXACT_T ? MAX_T : m.T;const int d0 = blockIdx.x * UPM_COLS;for (int i = t; i < T * LR; i += THREADS) lo[i / LR][i % LR] = m.a[i / LR].lo[i % LR];__syncthreads();for (int r = warp; r < HC * UPM_COLS; r += WARPS) {const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd;const uint4* w4 = reinterpret_cast<const uint4*>(m.a[0].w_up + (size_t) i * LR);- const uint4 wa = __ldg(w4 + lane);- const uint4 wb = lane < LR / 8 - 32 ? __ldg(w4 + 32 + lane) : make_uint4(0, 0, 0, 0);+ const Bf16x8 wa = unpack8(__ldg(w4 + lane));+ const Bf16x8 wb = unpack8(lane < LR / 8 - 32 ? __ldg(w4 + 32 + lane) : make_uint4(0, 0, 0, 0));// the epilogue inputs of this lane's token, fetched while the dots runfloat rv = 0.0f, wn = 0.0f, rsc = 0.0f, bo = 0.0f, ip = 0.0f;bool apply = false;@@ -318,10 +339,10 @@ __global__ void __launch_bounds__(THREADS) gr_up_multi_kernel(GrMulti m) {}float mine = 0.0f;#pragma unroll- for (int k = 0; k < kFusedGrMaxT; ++k) {- if (k >= T) break;- float acc = dot8(wa, lo[k] + lane * 8);- if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8);+ for (int k = 0; k < MAX_T; ++k) {+ if (!EXACT_T && k >= T) break;+ float acc = dot8u_ptr(wa, lo[k] + lane * 8);+ if (lane < LR / 8 - 32) acc += dot8u_ptr(wb, lo[k] + (32 + lane) * 8);acc = warp_sum(acc);if (lane == k) mine = acc;}@@ -421,10 +442,10 @@ __global__ void __launch_bounds__(THREADS) gr_down_v3_kernel(GrMulti m, float* _#pragma unrollfor (int q = 0; q < TQS; ++q) {const int j = lane + 32 * q;- const uint4 wvq = __ldg(w4 + j);+ const Bf16x8 wvq = unpack8(__ldg(w4 + j));#pragma unrollfor (int k = 0; k < MAX_T; ++k)- if (EXACT_T || k < T) acc[k] += dot8(wvq, xs + (size_t) k * SL + j * 8);+ if (EXACT_T || k < T) acc[k] += dot8u_ptr(wvq, xs + (size_t) k * SL + j * 8);}const int prow = inject_block ? LR + warp : row0 + r;#pragma unroll@@ -489,8 +510,8 @@ __global__ void __launch_bounds__(THREADS) gr_up_v3_kernel(GrMulti m, const floaconst int r = warp + q * WARPS;const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd;const uint4* w4 = reinterpret_cast<const uint4*>(m.a[0].w_up + (size_t) i * LR);- const uint4 wa = __ldg(w4 + lane);- const uint4 wb = lane < LR / 8 - 32 ? __ldg(w4 + 32 + lane) : make_uint4(0, 0, 0, 0);+ const Bf16x8 wa = unpack8(__ldg(w4 + lane));+ const Bf16x8 wb = unpack8(lane < LR / 8 - 32 ? __ldg(w4 + 32 + lane) : make_uint4(0, 0, 0, 0));float rv = 0.0f, wn = 0.0f, bo = 0.0f, ip = 0.0f;bool apply = false;if (lane < T) {@@ -504,8 +525,8 @@ __global__ void __launch_bounds__(THREADS) gr_up_v3_kernel(GrMulti m, const floa#pragma unrollfor (int k = 0; k < MAX_T; ++k) {if (!EXACT_T && k >= T) break;- float acc = dot8(wa, lo[k] + lane * 8);- if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8);+ float acc = dot8u_ptr(wa, lo[k] + lane * 8);+ if (lane < LR / 8 - 32) acc += dot8u_ptr(wb, lo[k] + (32 + lane) * 8);acc = warp_sum(acc);if (lane == k) mine = acc;}@@ -648,11 +669,11 @@ __device__ __forceinline__ void stage_htile(const GrMulti& m, int T, int h, floa}}-template <int MAX_T = kFusedGrMaxT>+template <int MAX_T = kFusedGrMaxT, bool EXACT_T = false>__global__ void __launch_bounds__(THREADS) gr_down_staged_kernel(GrMulti m) {extern __shared__ __align__(16) float4 hbuf[]; // 2 buffers x [T][2][160] float4const int t = threadIdx.x, lane = t & 31, warp = t >> 5;- const int T = m.T;+ const int T = EXACT_T ? MAX_T : m.T;const bool inject_block = blockIdx.x == DOWN_BLOCKS;const int row = inject_block ? warp : blockIdx.x * WARPS + warp;const bool active = !(inject_block && (m.a[0].w_inject == nullptr || warp >= HC));@@ -685,11 +706,12 @@ __global__ void __launch_bounds__(THREADS) gr_down_staged_kernel(GrMulti m) {#pragma unrollfor (int q = 0; q < HQ; ++q) {const int j = lane + 32 * q;+ const Bf16x8 wvq = unpack8(wv[q]);#pragma unrollfor (int k = 0; k < MAX_T; ++k) {- if (k < T) {+ if (EXACT_T || k < T) {const float4* pk = cur + (size_t) k * (H_TILE / 4);- acc[k] += dot8v(wv[q], pk[j], pk[H_TILE / 8 + j]);+ acc[k] += dot8u(wvq, pk[j], pk[H_TILE / 8 + j]);}}}@@ -705,11 +727,11 @@ __global__ void __launch_bounds__(THREADS) gr_down_staged_kernel(GrMulti m) {if (!active) return;float s[MAX_T];#pragma unroll- for (int k = 0; k < MAX_T; ++k) s[k] = k < T ? warp_sum(acc[k]) : 0.0f;+ for (int k = 0; k < MAX_T; ++k) s[k] = (EXACT_T || k < T) ? warp_sum(acc[k]) : 0.0f;// lane k writes token k (every lane holds every sum after the xor reduction)#pragma unrollfor (int k = 0; k < MAX_T; ++k) {- if (k >= T || lane != k) continue;+ if ((!EXACT_T && k >= T) || lane != k) continue;if (inject_block) {m.a[k].inject_out[row] = s[k];} else {@@ -764,9 +786,21 @@ int down_chunk(bool staged, int* tile_out) {} else {cudaFuncSetAttribute(gr_down_multi_kernel<2560>, cudaFuncAttributeMaxDynamicSharedMemorySize, want);}- 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);+ auto set_staged_attr = [&](auto kernel, int max_t) {+ int ws = (int) (2 * max_t * H_TILE * sizeof(float));+ if (optin > 0 && ws > optin) ws = optin;+ if (ws > 48 * 1024)+ cudaFuncSetAttribute(kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, ws);+ cudaFuncSetAttribute(kernel, cudaFuncAttributePreferredSharedMemoryCarveout, 100);+ };+ set_staged_attr(gr_down_staged_kernel<1, true>, 1);+ set_staged_attr(gr_down_staged_kernel<2, true>, 2);+ set_staged_attr(gr_down_staged_kernel<3, true>, 3);+ set_staged_attr(gr_down_staged_kernel<4, true>, 4);+ set_staged_attr(gr_down_staged_kernel<5, true>, 5);+ set_staged_attr(gr_down_staged_kernel<6, true>, 6);+ set_staged_attr(gr_down_staged_kernel<4, false>, 4);+ set_staged_attr(gr_down_staged_kernel<kFusedGrMaxT, false>, kFusedGrMaxT);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@@ -848,6 +882,11 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned longint tv = 2560;const int chunk_tok = down_chunk(staged, &tv);const size_t per_tok = (staged ? (size_t) 2 * H_TILE : (size_t) tv) * sizeof(float);+ static const bool no_multi_gr = [] {+ const char* no = std::getenv("STRATA_NO_MULTI_GR");+ return no != nullptr && no[0] != '\0' && no[0] != '0';+ }();+ const bool exact_t = !no_multi_gr;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{};@@ -864,7 +903,15 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long// (decided once per device: this runs per layer when decode is not captured)const bool max4 = ct <= 4 && gr_down_max4();if (staged) {- if (max4) gr_down_staged_kernel<4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);+ // #783 PR-g (stuchapin909): a launch of exactly ct <= 6 tokens is its own instantiation, the loop bounds+ // are compile-time (the same sums in the same order); STRATA_NO_MULTI_GR=1 keeps the generic kernels+ if (exact_t && ct == 1) gr_down_staged_kernel<1, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);+ else if (exact_t && ct == 2) gr_down_staged_kernel<2, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);+ else if (exact_t && ct == 3) gr_down_staged_kernel<3, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);+ else if (exact_t && ct == 4) gr_down_staged_kernel<4, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);+ else if (exact_t && ct == 5) gr_down_staged_kernel<5, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);+ else if (exact_t && ct == 6) gr_down_staged_kernel<6, true><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);+ else if (max4) gr_down_staged_kernel<4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);else gr_down_staged_kernel<><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);} else if (tv == 1280) {if (max4) gr_down_multi_kernel<1280, 4><<<DOWN_BLOCKS + 1, THREADS, smem, st>>>(c);@@ -879,7 +926,15 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned longif (fast) gr_up_fast_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);else#endif- gr_up_multi_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);+ switch (no_multi_gr ? kFusedGrMaxT : n_tok) {+ case 1: gr_up_multi_kernel<1, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;+ case 2: gr_up_multi_kernel<2, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;+ case 3: gr_up_multi_kernel<3, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;+ case 4: gr_up_multi_kernel<4, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;+ case 5: gr_up_multi_kernel<5, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;+ case 6: gr_up_multi_kernel<6, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;+ default: gr_up_multi_kernel<kFusedGrMaxT, false><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;+ }}/// STRATA_HC_SPLIT: unset or 2 = the newest the check accepts (staged), 1 = at most split, 0 = the plain read@@ -1625,14 +1680,21 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voiint optin = 0;cudaDeviceGetAttribute(&optin, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev3);const int limit = optin > 0 ? optin : 48 * 1024;- const int need1 = (int) (kFusedGrMaxT * N * sizeof(float)), need6 = (int) (6 * N * sizeof(float)), need2 = need1 / 2;+ const int need1 = (int) (kFusedGrMaxT * N * sizeof(float)), need6 = (int) (6 * N * sizeof(float)),+ need5 = (int) (5 * N * sizeof(float)), need2 = need1 / 2;int split = -1;if (need1 <= limit &&cudaFuncSetAttribute(gr_down_v3_kernel<1, kFusedGrMaxT, false>, cudaFuncAttributeMaxDynamicSharedMemorySize, need1) == cudaSuccess &&- cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, need6) == cudaSuccess) {+ cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, need6) == cudaSuccess &&+ cudaFuncSetAttribute(gr_down_v3_kernel<1, 5, true>, cudaFuncAttributeMaxDynamicSharedMemorySize, need5) == cudaSuccess) {+ cudaFuncSetAttribute(gr_down_v3_kernel<1, 1, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);+ cudaFuncSetAttribute(gr_down_v3_kernel<1, 2, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);+ cudaFuncSetAttribute(gr_down_v3_kernel<1, 3, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);cudaFuncSetAttribute(gr_down_v3_kernel<1, 4, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);+ cudaFuncSetAttribute(gr_down_v3_kernel<1, 5, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);+ cudaFuncSetAttribute(gr_down_v3_kernel<1, 6, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);cudaFuncSetAttribute(gr_down_v3_kernel<1, 4, false>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);- cudaFuncSetAttribute(gr_down_v3_kernel<1, 2, true>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);+ cudaFuncSetAttribute(gr_down_v3_kernel<1, kFusedGrMaxT, false>, cudaFuncAttributePreferredSharedMemoryCarveout, 100);split = 1;} else if (need2 <= limit)// #375 (kenh0u): the S = 2 split (a 64 KB opt-in card: Turing) disagrees with itself in gr_parity@@ -1650,17 +1712,21 @@ bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voiconst size_t sm = (size_t) n_tok * (N / split) * sizeof(float);if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, stream);if (split == 2) gr_down_v3_kernel<2><<<dim3(LR / WARPS + 1, HC * 2), THREADS, sm, st>>>(m, part, ssg);+ else if (n_tok == 1) gr_down_v3_kernel<1, 1, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);else if (n_tok == 2) gr_down_v3_kernel<1, 2, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);+ else if (n_tok == 3) gr_down_v3_kernel<1, 3, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);else if (n_tok == 4) gr_down_v3_kernel<1, 4, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);+ else if (n_tok == 5) gr_down_v3_kernel<1, 5, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);else if (n_tok == 6) gr_down_v3_kernel<1, 6, true><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);- else if (n_tok < 4) gr_down_v3_kernel<1, 4, false><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);else gr_down_v3_kernel<1, kFusedGrMaxT, false><<<dim3(LR / WARPS + 1, HC), THREADS, sm, st>>>(m, part, ssg);if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, stream);if (split == 2) gr_up_v3_kernel<2><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);+ else if (n_tok == 1) gr_up_v3_kernel<1, 1, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);else if (n_tok == 2) gr_up_v3_kernel<1, 2, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);+ else if (n_tok == 3) gr_up_v3_kernel<1, 3, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);else if (n_tok == 4) gr_up_v3_kernel<1, 4, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);+ else if (n_tok == 5) gr_up_v3_kernel<1, 5, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);else if (n_tok == 6) gr_up_v3_kernel<1, 6, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);- else if (n_tok < 4) gr_up_v3_kernel<1, 4, false><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);else gr_up_v3_kernel<1, kFusedGrMaxT, false><<<UPM_BLOCKS, THREADS, 0, st>>>(m, part, ssg);const cudaError_t e3 = cudaGetLastError();if (e3 != cudaSuccess) {diff --git a/src/kernels/cuda/gr.cu b/src/kernels/cuda/gr.cuindex 7eea7f49f..2f5508430 100644--- a/src/kernels/cuda/gr.cu+++ b/src/kernels/cuda/gr.cu@@ -410,6 +410,52 @@ void gr_read(const float* R, const float* w_norm, const uint16_t* w_down, const}}+static bool no_multi_gr() {+ static const bool off = [] {+ const char* v = std::getenv("STRATA_NO_MULTI_GR");+ return v != nullptr && v[0] != '\0' && v[0] != '0';+ }();+ return off;+}++void gr_read_multi(const float* R, const float* w_norm, const uint16_t* w_down, const uint16_t* w_up,+ const uint16_t* w_inject, float eps, const GrShapes& s, const GrWorkspace& ws,+ float* xn_multi, float* lo_multi, float* gated_multi, float* mixed, float* inject,+ int n_tok, void* stream) {+ if (n_tok <= 0 || s.n_embd <= 0 || s.hc <= 0 || s.hc_lr <= 0) return;+ if (!no_multi_gr() && native_mmvf && n_tok > 1 && xn_multi != nullptr && lo_multi != nullptr && gated_multi != nullptr) {+ const int n_embd = (int) s.n_embd, hc = (int) s.hc, hc_lr = (int) s.hc_lr;+ const int hc_dim = (int) (s.hc * s.n_embd);+ if ((hc_dim & 1) != 0 || (hc_lr & 1) != 0)+ throw std::invalid_argument("gr_read native MMVF requires even hc*n_embd and hc_lr");+ native_gr_rms_norm_weighted_multi(R, w_norm, xn_multi, n_embd, hc, n_tok, eps, stream);+ bf16_gemv_fp32_mmvf_multi(xn_multi, hc_dim, w_down, lo_multi, hc_lr, hc_dim, hc_lr, n_tok, stream);+ native_gr_down_silu(lo_multi, hc_lr * n_tok, hc, stream);+ bf16_gemv_fp32_mmvf_multi(lo_multi, hc_lr, w_up, gated_multi, hc_dim, hc_lr, hc_dim, n_tok, stream);+ native_gr_pre_gated_multi(xn_multi, gated_multi, mixed, n_embd, hc, n_tok, w_inject != nullptr, stream);+ if (w_inject != nullptr)+ bf16_gemv_fp32_mmvf_multi(xn_multi, hc_dim, w_inject, inject, hc, hc_dim, hc, n_tok, stream);+ const cudaError_t e = cudaGetLastError();+ if (e != cudaSuccess) {+ std::fprintf(stderr, "gr_read_multi launch: %s\n", cudaGetErrorString(e));+ std::exit(1);+ }+ if (stream == nullptr) {+ const cudaError_t se = cudaDeviceSynchronize();+ if (se != cudaSuccess) {+ std::fprintf(stderr, "gr_read_multi: %s\n", cudaGetErrorString(se));+ std::exit(1);+ }+ }+ return;+ }+ const size_t hc_dim = (size_t) s.hc * (size_t) s.n_embd;+ for (int t = 0; t < n_tok; ++t) {+ gr_read(R + (size_t) t * hc_dim, w_norm, w_down, w_up, w_inject, eps, s, ws,+ mixed + (size_t) t * s.n_embd, inject ? inject + (size_t) t * s.hc : nullptr, stream);+ }+}+void gr_write(const float* R, const float* block_out, const float* inject, const GrShapes& s, float* R_out,void* stream) {if (s.n_embd <= 0 || s.hc <= 0) return;@@ -429,4 +475,25 @@ void gr_write(const float* R, const float* block_out, const float* inject, const}}+void gr_write_multi(const float* R, const float* block_out, const float* inject, const GrShapes& s, float* R_out,+ int n_tok, void* stream) {+ if (n_tok <= 0 || s.n_embd <= 0 || s.hc <= 0) return;+ if (!no_multi_gr() && native_mmvf && n_tok > 1) {+ native_gr_post_multi(R, block_out, inject, R_out, (int) s.n_embd, (int) s.hc, n_tok, stream);+ if (stream == nullptr) {+ const cudaError_t e = cudaDeviceSynchronize();+ if (e != cudaSuccess) {+ std::fprintf(stderr, "gr_write_multi: %s\n", cudaGetErrorString(e));+ std::exit(1);+ }+ }+ return;+ }+ const size_t hc_dim = (size_t) s.hc * (size_t) s.n_embd;+ for (int t = 0; t < n_tok; ++t) {+ gr_write(R + (size_t) t * hc_dim, block_out + (size_t) t * s.n_embd, inject + (size_t) t * s.hc, s,+ R_out + (size_t) t * hc_dim, stream);+ }+}+} // namespace strata::kernelsdiff --git a/src/kernels/cuda/native_bf16.cu b/src/kernels/cuda/native_bf16.cuindex 9715e4605..a059c5fb0 100644--- a/src/kernels/cuda/native_bf16.cu+++ b/src/kernels/cuda/native_bf16.cu@@ -50,10 +50,8 @@ __global__ void bf16_f32_mmvf_kernel(const float* __restrict__ x, const uint16_tconst uint32_t* weights2 = reinterpret_cast<const uint32_t*>(row);const float2* inputs2 = reinterpret_cast<const float2*>(x);__shared__ float partials[32];- if constexpr (BLOCK_SIZE > 32) {- if (t < 32) partials[t] = 0.0f;- __syncthreads();- }+ if (t < 32) partials[t] = 0.0f;+ __syncthreads();float acc = 0.0f;for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) {const uint32_t weight = weights2[pair];@@ -75,49 +73,51 @@ __global__ void bf16_f32_mmvf_kernel(const float* __restrict__ x, const uint16_t// the same kernel for up to 8 activation rows - the weight row is read ONCE and every// token keeps its own accumulator with exactly the single-row kernel's order (pairs, two ordered FMAs, the same warp// and block reductions), so each output is bit-identical to a bf16_f32_mmvf_kernel launch of its own.-template <int BLOCK_SIZE, int NT>+template <int BLOCK_SIZE, int NT, bool EXACT_T = true>__global__ void bf16_f32_mmvf_multi_kernel(const float* __restrict__ x, int64_t ldx, const uint16_t* __restrict__ w,float* __restrict__ y, int64_t ldy, int n_in, int n_tok) {const int t = threadIdx.x;const uint16_t* row = w + (size_t) blockIdx.x * n_in;const uint32_t* weights2 = reinterpret_cast<const uint32_t*>(row);__shared__ float partials[NT][32];- if constexpr (BLOCK_SIZE > 32) {- if (t < 32)+ if (t < 32) {#pragma unroll- for (int k = 0; k < NT; ++k) partials[k][t] = 0.0f;- __syncthreads();+ for (int k = 0; k < NT; ++k) partials[k][t] = 0.0f;}+ __syncthreads();float acc[NT];#pragma unrollfor (int k = 0; k < NT; ++k) acc[k] = 0.0f;for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) {- const uint32_t weight = weights2[pair];+ const uint32_t weight = __ldg(weights2 + pair);const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16));#pragma unrollfor (int k = 0; k < NT; ++k) {- if (k < n_tok) {- const float2 input = reinterpret_cast<const float2*>(x + (size_t) k * ldx)[pair];+ if (EXACT_T || k < n_tok) {+ const float2 input = __ldg(reinterpret_cast<const float2*>(x + (size_t) k * ldx) + pair);acc[k] = __fmaf_rn(w0, input.x, acc[k]);acc[k] = __fmaf_rn(w1, input.y, acc[k]);}}}#pragma unroll- for (int k = 0; k < NT; ++k) acc[k] = mmvf_warp_sum(acc[k]);+ for (int k = 0; k < NT; ++k)+ if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(acc[k]);if constexpr (BLOCK_SIZE > 32) {if ((t & 31) == 0)#pragma unroll- for (int k = 0; k < NT; ++k) partials[k][t / 32] = acc[k];+ for (int k = 0; k < NT; ++k)+ if (EXACT_T || k < n_tok) partials[k][t / 32] = acc[k];__syncthreads();if (t < 32)#pragma unroll- for (int k = 0; k < NT; ++k) acc[k] = mmvf_warp_sum(partials[k][t]);+ for (int k = 0; k < NT; ++k)+ if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(partials[k][t]);}if (t == 0)#pragma unrollfor (int k = 0; k < NT; ++k)- if (k < n_tok) y[(size_t) k * ldy + blockIdx.x] = acc[k];+ if (EXACT_T || k < n_tok) y[(size_t) k * ldy + blockIdx.x] = acc[k];}// S25 (STRATA_MMVF_ROWS=1): RPB output rows per block. Each block read its row's weights once but all T activation@@ -305,8 +305,15 @@ void bf16_gemv_fp32_mmvf_multi(const float* x, int64_t ldx, const uint16_t* w, freturn;}#define STRATA_MMVF_M(N) case N: \- if (n_tok <= 4) bf16_f32_mmvf_multi_kernel<N, 4><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); \- else bf16_f32_mmvf_multi_kernel<N, 8><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break+ switch (n_tok) { \+ case 1: bf16_f32_mmvf_multi_kernel<N, 1, true><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break; \+ case 2: bf16_f32_mmvf_multi_kernel<N, 2, true><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break; \+ case 3: bf16_f32_mmvf_multi_kernel<N, 3, true><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break; \+ case 4: bf16_f32_mmvf_multi_kernel<N, 4, true><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break; \+ case 5: bf16_f32_mmvf_multi_kernel<N, 5, true><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break; \+ case 6: bf16_f32_mmvf_multi_kernel<N, 6, true><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break; \+ default: bf16_f32_mmvf_multi_kernel<N, 8, false><<<(unsigned) n_out, N, 0, st>>>(x, ldx, w, y, ldy, (int) n_in, n_tok); break; \+ } breakswitch (mmvf_block_size(n_in)) {STRATA_MMVF_M(32); STRATA_MMVF_M(64); STRATA_MMVF_M(96); STRATA_MMVF_M(128);STRATA_MMVF_M(160); STRATA_MMVF_M(192); STRATA_MMVF_M(224); STRATA_MMVF_M(256);diff --git a/src/kernels/cuda/native_gr_norm.cu b/src/kernels/cuda/native_gr_norm.cuindex 5abe013ec..668c0bd5a 100644--- a/src/kernels/cuda/native_gr_norm.cu+++ b/src/kernels/cuda/native_gr_norm.cu@@ -47,9 +47,10 @@ __global__ void weighted_rms_norm(const float* __restrict__ input,const float* __restrict__ gamma,float* __restrict__ output, int n_cols, float epsilon) {const int tid = threadIdx.x;- const std::size_t row_offset = std::size_t(blockIdx.x) * n_cols;+ const std::size_t row = std::size_t(blockIdx.y) * gridDim.x + blockIdx.x;+ const std::size_t row_offset = row * n_cols;input += row_offset;- gamma += row_offset;+ gamma += std::size_t(blockIdx.x) * n_cols;output += row_offset;float partial = 0.0f;for (int col = tid; col < n_cols; col += BlockSize) {@@ -84,16 +85,22 @@ void check_pointer(const void* p) {void native_gr_rms_norm_weighted(const float* input, const float* gamma, float* output,int n_cols, int n_rows, float epsilon, void* stream) {- if (n_cols <= 0 || n_rows <= 0 || !std::isfinite(epsilon) || epsilon < 0.0f)+ native_gr_rms_norm_weighted_multi(input, gamma, output, n_cols, n_rows, 1, epsilon, stream);+}++void native_gr_rms_norm_weighted_multi(const float* input, const float* gamma, float* output,+ int n_cols, int n_rows, int n_tok, float epsilon, void* stream) {+ if (n_cols <= 0 || n_rows <= 0 || n_tok <= 0 || !std::isfinite(epsilon) || epsilon < 0.0f)throw std::invalid_argument("native GR RMSNorm requires positive dimensions and finite nonnegative epsilon");check_pointer(input);check_pointer(gamma);check_pointer(output);const auto cuda_stream = static_cast<cudaStream_t>(stream);+ const dim3 grid{unsigned(n_rows), unsigned(n_tok), 1u};if (n_cols < 1024)- weighted_rms_norm<256><<<unsigned(n_rows), 256, 0, cuda_stream>>>(input, gamma, output, n_cols, epsilon);+ weighted_rms_norm<256><<<grid, 256, 0, cuda_stream>>>(input, gamma, output, n_cols, epsilon);else- weighted_rms_norm<1024><<<unsigned(n_rows), 1024, 0, cuda_stream>>>(input, gamma, output, n_cols, epsilon);+ weighted_rms_norm<1024><<<grid, 1024, 0, cuda_stream>>>(input, gamma, output, n_cols, epsilon);const auto error = cudaGetLastError();if (error != cudaSuccess)throw std::runtime_error(std::string("native GR RMSNorm launch: ") + cudaGetErrorString(error));diff --git a/src/kernels/cuda/native_gr_postops.cu b/src/kernels/cuda/native_gr_postops.cuindex 8616e331c..bbb8493d1 100644--- a/src/kernels/cuda/native_gr_postops.cu+++ b/src/kernels/cuda/native_gr_postops.cu@@ -50,6 +50,10 @@ __global__ void pre_gated(const float* __restrict__ xn, float* __restrict__ gatefloat* __restrict__ mixed, int n_embd, int hc, float scale) {const std::size_t d = std::size_t(blockIdx.x) * blockDim.x + threadIdx.x;if (d >= std::size_t(n_embd)) return;+ const std::size_t t = blockIdx.y;+ xn += t * std::size_t(n_embd) * hc;+ gate += t * std::size_t(n_embd) * hc;+ mixed += t * std::size_t(n_embd);float sum = 0.0f;for (int c = 0; c < hc; ++c) {const std::size_t i = std::size_t(c) * n_embd + d;@@ -67,6 +71,11 @@ __global__ void post(const float* residual, const float* __restrict__ block_out,int n_embd, int hc, float scale) {const std::size_t i = std::size_t(blockIdx.x) * blockDim.x + threadIdx.x;if (i >= std::size_t(n_embd) * hc) return;+ const std::size_t t = blockIdx.y;+ residual += t * std::size_t(n_embd) * hc;+ block_out += t * std::size_t(n_embd);+ inject += t * std::size_t(hc);+ output += t * std::size_t(n_embd) * hc;const int c = int(i / n_embd), d = int(i % n_embd);const float weight = scale_zero_bias(sigmoid(scale_zero_bias(inject[c], scale)), 2.0f);// Exact residual/output alias is supported; no other thread reads residual[i].@@ -96,19 +105,33 @@ void native_gr_down_silu(float* lo, int hc_lr, int hc, void* stream) {}void native_gr_pre_gated(const float* xn, float* gate, float* mixed,int n_embd, int hc, bool fused_layer, void* stream) {+ native_gr_pre_gated_multi(xn, gate, mixed, n_embd, hc, 1, fused_layer, stream);+}+void native_gr_pre_gated_multi(const float* xn, float* gate, float* mixed,+ int n_embd, int hc, int n_tok, bool fused_layer, void* stream) {+ if (n_tok <= 0)+ throw std::invalid_argument("native GR postops require positive n_tok");check_shape(n_embd, hc);check_pointer(xn); check_pointer(gate); check_pointer(mixed);+ const dim3 grid{blocks(n_embd), unsigned(n_tok), 1u};if (fused_layer)- pre_gated<true><<<blocks(n_embd), THREADS, 0, static_cast<cudaStream_t>(stream)>>>(xn, gate, mixed, n_embd, hc, 1.0f / float(hc));+ pre_gated<true><<<grid, THREADS, 0, static_cast<cudaStream_t>(stream)>>>(xn, gate, mixed, n_embd, hc, 1.0f / float(hc));else- pre_gated<false><<<blocks(n_embd), THREADS, 0, static_cast<cudaStream_t>(stream)>>>(xn, gate, mixed, n_embd, hc, 1.0f / float(hc));+ pre_gated<false><<<grid, THREADS, 0, static_cast<cudaStream_t>(stream)>>>(xn, gate, mixed, n_embd, hc, 1.0f / float(hc));check_launch();}void native_gr_post(const float* residual, const float* block_out, const float* inject,float* output, int n_embd, int hc, void* stream) {+ native_gr_post_multi(residual, block_out, inject, output, n_embd, hc, 1, stream);+}+void native_gr_post_multi(const float* residual, const float* block_out, const float* inject,+ float* output, int n_embd, int hc, int n_tok, void* stream) {+ if (n_tok <= 0)+ throw std::invalid_argument("native GR postops require positive n_tok");check_shape(n_embd, hc);check_pointer(residual); check_pointer(block_out); check_pointer(inject); check_pointer(output);- post<<<blocks(std::size_t(n_embd) * hc), THREADS, 0, static_cast<cudaStream_t>(stream)>>>(+ const dim3 grid{blocks(std::size_t(n_embd) * hc), unsigned(n_tok), 1u};+ post<<<grid, THREADS, 0, static_cast<cudaStream_t>(stream)>>>(residual, block_out, inject, output, n_embd, hc, 1.0f / float(hc));check_launch();}diff --git a/src/kernels/gr_multi_parity.cpp b/src/kernels/gr_multi_parity.cppnew file mode 100644index 000000000..398718ae1--- /dev/null+++ b/src/kernels/gr_multi_parity.cpp@@ -0,0 +1,143 @@+// src/kernels/gr_multi_parity.cpp - the multi-token hyper-connection read and write (#783 PR-g: gr_read_multi,+// gr_write_multi and the exact-T BF16 projections under them) against the single-token calls, memcmp.+//+// Real geometry (n_embd 2560, hc 4, hc_lr 320), the pinned native MMVF path, every T = 1..8, with and without an+// injection weight (a layer read vs the final mixer), and the write both out of place and in place (R_out == R).+// Each output of the multi call must equal the token-by-token one bit for bit. GPU, synthetic, no model.+#include "strata/kernels/gr.hpp"++#include <cuda_runtime.h>++#include <cstdint>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <random>+#include <vector>++namespace k = strata::kernels;++namespace {+void ck(cudaError_t e, const char* w) {+ if (e != cudaSuccess) {+ std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e));+ std::exit(2);+ }+}+template <typename T>+T* up(const std::vector<T>& h) {+ void* p = nullptr;+ ck(cudaMalloc(&p, h.size() * sizeof(T) + 64), "malloc");+ ck(cudaMemcpy(p, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "h2d");+ return (T*) p;+}+template <typename T>+T* dev(size_t n) {+ void* p = nullptr;+ ck(cudaMalloc(&p, n * sizeof(T) + 64), "malloc");+ ck(cudaMemset(p, 0, n * sizeof(T) + 64), "memset");+ return (T*) p;+}+template <typename T>+std::vector<T> down(const T* d, size_t n) {+ std::vector<T> h(n);+ ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "d2h");+ return h;+}+uint16_t bf16_of(float f) {+ uint32_t u;+ std::memcpy(&u, &f, 4);+ return (uint16_t) (u >> 16);+}+} // namespace++int main() {+ const long long n_embd = 2560, hc = 4, hc_lr = 320, hc_dim = hc * n_embd;+ const float eps = 1e-6f;+ std::mt19937 rng(783);+ std::normal_distribution<float> gauss(0.f, 1.f);+ k::gr_set_native_mmvf(true);++ std::vector<float> w_norm((size_t) hc_dim);+ for (auto& x : w_norm) x = 1.0f + 0.1f * gauss(rng);+ std::vector<uint16_t> w_down((size_t) (hc_lr * hc_dim)), w_up((size_t) (hc_dim * hc_lr)), w_inj((size_t) (hc * hc_dim));+ for (auto& x : w_down) x = bf16_of(gauss(rng) * 0.05f);+ for (auto& x : w_up) x = bf16_of(gauss(rng) * 0.05f);+ for (auto& x : w_inj) x = bf16_of(gauss(rng) * 0.02f);+ float* d_norm = up(w_norm);+ uint16_t *d_down = up(w_down), *d_up = up(w_up), *d_inj = up(w_inj);++ const k::GrShapes sh{n_embd, hc, hc_lr};+ void* ws_raw = nullptr;+ ck(cudaMalloc(&ws_raw, k::gr_workspace_bytes(sh)), "ws");+ k::GrWorkspace ws;+ k::gr_workspace_init(sh, ws_raw, ws);++ int cases = 0, bad = 0;+ for (int T = 1; T <= 8; ++T) {+ std::vector<float> R((size_t) T * hc_dim);+ for (int t = 0; t < T; ++t)+ for (long long c = 0; c < hc; ++c)+ for (long long d = 0; d < n_embd; ++d) R[(size_t) t * hc_dim + c * n_embd + d] = gauss(rng) * (float) (1 << (2 * c));+ float* d_R = up(R);+ float* d_xn = dev<float>((size_t) T * hc_dim);+ float* d_lo = dev<float>((size_t) T * hc_lr);+ float* d_g = dev<float>((size_t) T * hc_dim);+ for (int with_inj = 0; with_inj < 2; ++with_inj) {+ float* d_mixed_m = dev<float>((size_t) T * n_embd);+ float* d_mixed_s = dev<float>((size_t) T * n_embd);+ float* d_inject_m = dev<float>((size_t) T * hc);+ float* d_inject_s = dev<float>((size_t) T * hc);+ const uint16_t* wi = with_inj ? d_inj : nullptr;+ k::gr_read_multi(d_R, d_norm, d_down, d_up, wi, eps, sh, ws, d_xn, d_lo, d_g, d_mixed_m,+ with_inj ? d_inject_m : nullptr, T, nullptr);+ for (int t = 0; t < T; ++t)+ k::gr_read(d_R + (size_t) t * hc_dim, d_norm, d_down, d_up, wi, eps, sh, ws, d_mixed_s + (size_t) t * n_embd,+ with_inj ? d_inject_s + (size_t) t * hc : nullptr, nullptr);+ ck(cudaDeviceSynchronize(), "sync");+ bool ok = std::memcmp(down(d_mixed_m, (size_t) T * n_embd).data(), down(d_mixed_s, (size_t) T * n_embd).data(),+ (size_t) T * n_embd * 4) == 0;+ if (with_inj)+ ok = ok && std::memcmp(down(d_inject_m, (size_t) T * hc).data(), down(d_inject_s, (size_t) T * hc).data(),+ (size_t) T * hc * 4) == 0;+ ++cases;+ if (!ok) {+ std::printf(" *** gr_read_multi T=%d %s differs from gr_read ***\n", T, with_inj ? "with injection" : "final mixer");+ ++bad;+ }+ cudaFree(d_mixed_m); cudaFree(d_mixed_s); cudaFree(d_inject_m); cudaFree(d_inject_s);+ }++ // the write: R_out[t] = R[t] + block_out[t] * 2 sigmoid(inject[t] / hc)+ std::vector<float> bo((size_t) T * n_embd), inj((size_t) T * hc);+ for (auto& x : bo) x = gauss(rng);+ for (auto& x : inj) x = gauss(rng) * 3.0f;+ float* d_bo = up(bo);+ float* d_in = up(inj);+ for (int inplace = 0; inplace < 2; ++inplace) {+ float* d_Rm = up(R);+ float* d_Rs = up(R);+ float* d_om = inplace ? d_Rm : dev<float>((size_t) T * hc_dim);+ float* d_os = inplace ? d_Rs : dev<float>((size_t) T * hc_dim);+ k::gr_write_multi(d_Rm, d_bo, d_in, sh, d_om, T, nullptr);+ for (int t = 0; t < T; ++t)+ k::gr_write(d_Rs + (size_t) t * hc_dim, d_bo + (size_t) t * n_embd, d_in + (size_t) t * hc, sh,+ d_os + (size_t) t * hc_dim, nullptr);+ ck(cudaDeviceSynchronize(), "sync");+ const bool ok = std::memcmp(down(d_om, (size_t) T * hc_dim).data(), down(d_os, (size_t) T * hc_dim).data(),+ (size_t) T * hc_dim * 4) == 0;+ ++cases;+ if (!ok) {+ std::printf(" *** gr_write_multi T=%d %s differs from gr_write ***\n", T, inplace ? "in place" : "out of place");+ ++bad;+ }+ cudaFree(d_Rm); cudaFree(d_Rs);+ if (!inplace) { cudaFree(d_om); cudaFree(d_os); }+ }+ cudaFree(d_R); cudaFree(d_xn); cudaFree(d_lo); cudaFree(d_g); cudaFree(d_bo); cudaFree(d_in);+ }+ std::printf("gr_multi: %d cases, %d failures\n", cases, bad);+ if (bad) return 1;+ std::printf("gr_multi_parity OK\n");+ return 0;+}