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++
gfx906: route the verify window's tokens in one BF16 projection + one top-k (bitwise the per-token calls)
JeanP00lb18278ce7e32
diff --git a/CMakeLists.txt b/CMakeLists.txtindex e9f18c8f2..1fe793b7e 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -689,6 +689,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)target_link_libraries(quantize_act_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})add_test(NAME quantize_act_parity COMMAND quantize_act_parity --selftest)+ # the verify window's routing in one multi-column BF16 projection + one top-k, bitwise the per-token calls+ add_executable(route_window_parity src/kernels/route_window_parity.cpp)+ target_include_directories(route_window_parity PRIVATE ${_strata_gpu_include_directories})+ target_link_libraries(route_window_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})+ add_test(NAME route_window_parity COMMAND route_window_parity)+add_executable(router_top10_parity src/kernels/router_top10_parity.cpp)target_include_directories(router_top10_parity PRIVATE ${_strata_gpu_include_directories})target_link_libraries(router_top10_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})diff --git a/include/strata/core/layer.hpp b/include/strata/core/layer.hppindex a8965da13..aec6e9eb7 100644--- a/include/strata/core/layer.hpp+++ b/include/strata/core/layer.hpp@@ -168,6 +168,9 @@ bool moe_layer(const WeightTable& tables, const ModelGeometry& g, int64_t layer,/// rung, so a host that sees it knows `h_x_f`/`h_ids`/`h_weights` are in place.bool moe_route(const WeightTable& tables, const ModelGeometry& g, int64_t layer, int64_t k, const MoEBuffers& b,const float* x, void* stream, std::string& err, const Doorbell* db = nullptr);+/// The verify window's n tokens routed at once (see layer.cpp); bitwise per token what `moe_route` gives.+bool moe_route_window(const WeightTable& tables, const ModelGeometry& g, int64_t layer, int64_t k, const MoEBuffers& b,+ const float* x, float* logits, int32_t* ids, float* weights, int n, void* stream, std::string& err);/// The finishing half: the shared expert and the combination.///diff --git a/include/strata/kernels/bf16_gemv.hpp b/include/strata/kernels/bf16_gemv.hppindex 906a8f0b1..e3efe0ff3 100644--- a/include/strata/kernels/bf16_gemv.hpp+++ b/include/strata/kernels/bf16_gemv.hpp@@ -44,6 +44,10 @@ void bf16_gemv_split(const uint16_t* x, const uint16_t* w, float* y, int64_t n_i/// No allocations or synchronization, including when stream is null (the CUDA default stream).void bf16_gemv_fp32_mmvf(const float* x, const uint16_t* w, float* y,int64_t n_in, int64_t n_out, void* stream);+/// `ncols` columns at once: x is [ncols][n_in], y is [ncols][n_out]; each column bitwise equal to a+/// `bf16_gemv_fp32_mmvf` call on it.+void bf16_gemv_fp32_mmvf_cols(const float* x, const uint16_t* w, float* y, int64_t n_in, int64_t n_out, int ncols,+ void* stream);/// bf16_gemv_fp32_mmvf for n_tok (1..8) activation rows x[t * ldx], outputs y[t * ldy + j]; one launch,/// the weight read once, every output bit-identical to its own single-row call.void bf16_gemv_fp32_mmvf_multi(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy,diff --git a/src/core/layer.cpp b/src/core/layer.cppindex ff7fc17b5..33450f7d2 100644--- a/src/core/layer.cpp+++ b/src/core/layer.cpp@@ -387,6 +387,34 @@ if (db != nullptr && g_publish_kernel) {// write idempotent across replays of the same graph - a captured literal would ring the same number// forever and the host would never see a change.strata::kernels::doorbell_ring(db->d_seq, stream); } return true;}+// The verify window's routing: n tokens' logits in one BF16 projection and their top-k in one launch, each token+// bitwise what `moe_route` gives it (the projection is the same kernel per column, the top-k kernel is per token).+// Anything else (a native router, the canonical BF16 GEMVs, a doorbell) takes the per-token `moe_route`.+bool moe_route_window(const WeightTable& tables, const ModelGeometry& g, int64_t layer, int64_t k, const MoEBuffers& b,+ const float* x, float* logits, int32_t* ids, float* weights, int n, void* stream, std::string& err) {+ using namespace strata::kernels;+ const bool batched = native_bf16_projections && !(native_router_enabled() && g.n_expert == 512 && k == 10) &&+ !std::getenv("STRATA_ROUTE_PER_TOKEN");+ if (!batched || n == 1) {+ for (int t = 0; t < n; ++t) {+ MoEBuffers mb = b;+ mb.logits = logits + t * g.n_expert; mb.ids = ids + t * k; mb.weights = weights + t * k;+ if (!moe_route(tables, g, layer, k, mb, x + t * g.n_embd, stream, err, nullptr)) return false;+ }+ return true;+ }+ const LayerView v(tables, layer);+ const WeightRef* w_router = v.get("ffn_gate_inp.weight");+ if (w_router == nullptr) { err = v.name("ffn_gate_inp.weight") + " is missing"; return false; }+ if (std::getenv("STRATA_ROUTE_PROJ_PER_TOKEN"))+ for (int t = 0; t < n; ++t)+ bf16_gemv_fp32_mmvf(x + t * g.n_embd, (const uint16_t*) w_router->data, logits + t * g.n_expert, g.n_embd,+ g.n_expert, stream);+ else+ bf16_gemv_fp32_mmvf_cols(x, (const uint16_t*) w_router->data, logits, g.n_embd, g.n_expert, n, stream);+ router_top10(logits, n, (int) g.n_expert, (int) k, ids, weights, stream);+ return true;+}bool moe_shared(const WeightTable& tables, const ModelGeometry& g, int64_t layer, const MoEBuffers& b, const float* x, void* stream, std::string& err) { using namespace strata::kernels; const LayerView v(tables, layer); const WeightRef* w_ginp = v.get("ffn_gate_inp_shexp.weight"); const WeightRef* w_sgate = v.get("ffn_gate_shexp.weight"); const WeightRef* w_sup = v.get("ffn_up_shexp.weight"); const WeightRef* w_sdown = v.get("ffn_down_shexp.weight"); const char* missing = !w_ginp ? "ffn_gate_inp_shexp.weight" : !w_sgate ? "ffn_gate_shexp.weight" : !w_sup ? "ffn_up_shexp.weight" : !w_sdown ? "ffn_down_shexp.weight" : nullptr; if (missing) { err = v.name(missing) + " is missing"; return false; }// ---- the shared expert. Its three weights are quantized, so they need their planes.SForm f_gate, f_up, f_down; if (!sform_of(*w_sgate, f_gate, v.name("ffn_gate_shexp.weight"), err)) return false; if (!sform_of(*w_sup, f_up, v.name("ffn_up_shexp.weight"), err)) return false; if (!sform_of(*w_sdown, f_down, v.name("ffn_down_shexp.weight"), err)) return false; Planes p_gate, p_up, p_down; if (!plane_ptrs(*w_sgate, v.name("ffn_gate_shexp.weight"), p_gate, err)) return false; if (!plane_ptrs(*w_sup, v.name("ffn_up_shexp.weight"), p_up, err)) return false; if (!plane_ptrs(*w_sdown, v.name("ffn_down_shexp.weight"), p_down, err)) return false;diff --git a/src/core/verify.cpp b/src/core/verify.cppindex 8a8b69bca..c4b0dcc0d 100644--- a/src/core/verify.cpp+++ b/src/core/verify.cpp@@ -801,6 +801,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {NE, n, cs);native_router_top10_multi(logits_ + tb * NE, ids_ + tb * K, w_ + tb * K, n, cs);} catch (const std::exception& e) { err = "verify router: " + std::string(e.what()); return false; }+#if defined(STRATA_HIP_GFX906)+ } else if (dec_batch) {+ // gfx906: the Coder's 256-expert router (the native router is 512 x 10 only) for the whole window in one+ // multi-column BF16 projection and one top-k, bitwise the per-token calls (route_window_parity); ~3 ms+ // of a ~55 ms verify window on 2x MI50. STRATA_ROUTE_PER_TOKEN=1: the per-token calls.+ if (!moe_route_window(wt, g, l, K, ss.moe, mixed_ + tb * N, logits_ + tb * NE, ids_ + tb * K, w_ + tb * K, n,+ cs, err))+ return false;+#endif} elsefor (int t = tb; t < te; ++t) {MoEBuffers mb = ss.moe;diff --git a/src/kernels/cuda/native_bf16.cu b/src/kernels/cuda/native_bf16.cuindex 1390a6abe..fdbdc947c 100644--- a/src/kernels/cuda/native_bf16.cu+++ b/src/kernels/cuda/native_bf16.cu@@ -183,4 +183,17 @@ void bf16_gemv_fp32_mmvf(const float* x, const uint16_t* w, float* y,}++void bf16_gemv_fp32_mmvf_cols(const float* x, const uint16_t* w, float* y, int64_t n_in, int64_t n_out, int ncols,+ void* stream) {+ // columns contiguous: the existing multi-row kernel (weight read once, each output bitwise its one-row call),+ // up to 8 rows per launch+ for (int c0 = 0; c0 < ncols; c0 += 8) {+ const int nc = ncols - c0 < 8 ? ncols - c0 : 8;+ if (nc == 1) bf16_gemv_fp32_mmvf(x + (size_t) c0 * n_in, w, y + (size_t) c0 * n_out, n_in, n_out, stream);+ else bf16_gemv_fp32_mmvf_multi(x + (size_t) c0 * n_in, n_in, w, y + (size_t) c0 * n_out, n_out, n_in, n_out, nc,+ stream);+ }+}+} // namespace strata::kernelsdiff --git a/src/kernels/route_window_parity.cpp b/src/kernels/route_window_parity.cppnew file mode 100644index 000000000..bb0d6b9a4--- /dev/null+++ b/src/kernels/route_window_parity.cpp@@ -0,0 +1,75 @@+// route_window_parity - the verify window's batched routing against the per-token calls, bit for bit.+//+// `moe_route_window` replaces n per-token (projection + top-k) pairs by one multi-column BF16 projection and one+// n-token top-k. Speculative decoding is exact only if a token's route does not depend on how many tokens share+// its window, so both batched kernels must equal their one-token forms exactly - this checks that on random data+// shaped like the router (2560 -> 256, top-10) and like the shared-expert gate (2560 -> 1).+#include "strata/kernels/bf16_gemv.hpp"+#include "strata/kernels/router_top10.hpp"++#include <cuda_runtime.h>++#include <cstdint>+#include <cstdio>+#include <cstring>+#include <random>+#include <vector>++namespace {+void ck(cudaError_t e, const char* what) {+ if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); std::exit(2); }+}+uint16_t bf16(float f) { uint32_t i; std::memcpy(&i, &f, 4); return (uint16_t) ((i + ((i >> 16) & 1u) + 0x7FFFu) >> 16); }+} // namespace++int main() {+ const int N = 2560, E = 256, K = 10;+ int bad = 0;+ std::mt19937 rng(7);+ std::normal_distribution<float> nd(0.0f, 1.0f);+ for (int n_out : {E, 1}) {+ for (int T : {2, 3, 4, 6}) {+ std::vector<uint16_t> w((size_t) n_out * N);+ std::vector<float> x((size_t) T * N);+ for (auto& v : w) v = bf16(nd(rng) * 0.05f);+ for (auto& v : x) v = nd(rng);+ uint16_t* dw; float *dx, *dy1, *dyn;+ ck(cudaMalloc(&dw, w.size() * 2), "w"); ck(cudaMalloc(&dx, x.size() * 4), "x");+ ck(cudaMalloc(&dy1, (size_t) T * n_out * 4), "y1"); ck(cudaMalloc(&dyn, (size_t) T * n_out * 4), "yn");+ ck(cudaMemcpy(dw, w.data(), w.size() * 2, cudaMemcpyHostToDevice), "up w");+ ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "up x");+ for (int t = 0; t < T; ++t)+ strata::kernels::bf16_gemv_fp32_mmvf(dx + (size_t) t * N, dw, dy1 + (size_t) t * n_out, N, n_out, nullptr);+ strata::kernels::bf16_gemv_fp32_mmvf_cols(dx, dw, dyn, N, n_out, T, nullptr);+ ck(cudaDeviceSynchronize(), "sync");+ std::vector<float> y1((size_t) T * n_out), yn((size_t) T * n_out);+ ck(cudaMemcpy(y1.data(), dy1, y1.size() * 4, cudaMemcpyDeviceToHost), "down 1");+ ck(cudaMemcpy(yn.data(), dyn, yn.size() * 4, cudaMemcpyDeviceToHost), "down n");+ const bool same = std::memcmp(y1.data(), yn.data(), y1.size() * 4) == 0;+ std::printf(" mmvf cols n_out %3d T %d %s\n", n_out, T, same ? "bit-identical" : "*** DIFFERS ***");+ if (!same) ++bad;+ if (n_out == E) {+ int *i1, *in_; float *w1, *wn;+ ck(cudaMalloc(&i1, (size_t) T * K * 4), "i1"); ck(cudaMalloc(&in_, (size_t) T * K * 4), "in");+ ck(cudaMalloc(&w1, (size_t) T * K * 4), "w1"); ck(cudaMalloc(&wn, (size_t) T * K * 4), "wn");+ for (int t = 0; t < T; ++t)+ strata::kernels::router_top10(dy1 + (size_t) t * E, 1, E, K, i1 + t * K, w1 + t * K, nullptr);+ strata::kernels::router_top10(dy1, T, E, K, in_, wn, nullptr);+ ck(cudaDeviceSynchronize(), "sync top");+ std::vector<int> a((size_t) T * K), b((size_t) T * K);+ std::vector<float> fa((size_t) T * K), fb((size_t) T * K);+ ck(cudaMemcpy(a.data(), i1, a.size() * 4, cudaMemcpyDeviceToHost), "ids1");+ ck(cudaMemcpy(b.data(), in_, b.size() * 4, cudaMemcpyDeviceToHost), "idsn");+ ck(cudaMemcpy(fa.data(), w1, fa.size() * 4, cudaMemcpyDeviceToHost), "w1");+ ck(cudaMemcpy(fb.data(), wn, fb.size() * 4, cudaMemcpyDeviceToHost), "wn");+ const bool s2 = a == b && std::memcmp(fa.data(), fb.data(), fa.size() * 4) == 0;+ std::printf(" top10 n T %d %s\n", T, s2 ? "bit-identical" : "*** DIFFERS ***");+ if (!s2) ++bad;+ cudaFree(i1); cudaFree(in_); cudaFree(w1); cudaFree(wn);+ }+ cudaFree(dw); cudaFree(dx); cudaFree(dy1); cudaFree(dyn);+ }+ }+ std::printf(bad ? "route_window_parity: %d FAILURES\n" : "route_window_parity OK\n", bad);+ return bad ? 1 : 0;+}