Skip to content

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

C++

verify: a fully resident decode window without the host round trip (opt-in), and fewer quantize launches

Niko1221be7889ed84a1
diff --git a/include/strata/core/verify.hpp b/include/strata/core/verify.hpp
index 5717eb4fa..34f9ca90c 100644
--- a/include/strata/core/verify.hpp
+++ b/include/strata/core/verify.hpp
@@ -174,6 +174,7 @@ private:
int device_ = -1; ///< the device `init` ran on: run/commit switch to it (layer split)
std::atomic<bool> released_{false}; ///< #267: release_gpu_waits ran (maybe on the watchdog thread): no more windows
bool device_plan_ = false; ///< E-6: resident-only layers planned on the device (STRATA_VERIFY_DEVICE_PLAN)
+ bool resident_ = false; ///< STRATA_VERIFY_RESIDENT: every expert resident, no host step per layer
uint32_t* skip_ = nullptr; ///< E-6: per group, the ring whose plan the device built (0: the host's)
unsigned long long* slot_off_d_ = nullptr; ///< E-6: the slot offsets on the device
int64_t lb_ = 0, le_ = -1; ///< set_stage: the layers this verifier runs (-1: to the last)
diff --git a/include/strata/kernels/native_moe.hpp b/include/strata/kernels/native_moe.hpp
index 34760efe6..1e36dc113 100644
--- a/include/strata/kernels/native_moe.hpp
+++ b/include/strata/kernels/native_moe.hpp
@@ -18,4 +18,8 @@ void native_moe_combine(const float* parts, const float* weights, const float* s
/// n_tok rows (parts [n,k,N], weights [n,k], shared/output [n,N]) in one launch, each as the single call.
void native_moe_combine_multi(const float* parts, const float* weights, const float* shared, float* output,
int64_t n_embd, int64_t k, int n_tok, void* stream);
+/// STRATA_VERIFY_RESIDENT: the same, reading every part as `0.0f + hit[i]` - bitwise what a zeroed part row plus
+/// moe_hit_add (parts += hit) gave, without the zero fill and the add launch.
+void native_moe_combine_multi_hits(const float* hits, const float* weights, const float* shared, float* output,
+ int64_t n_embd, int64_t k, int n_tok, void* stream);
}
diff --git a/include/strata/kernels/shared_expert.hpp b/include/strata/kernels/shared_expert.hpp
index 8909ed9e6..e9148a10e 100644
--- a/include/strata/kernels/shared_expert.hpp
+++ b/include/strata/kernels/shared_expert.hpp
@@ -24,6 +24,7 @@ namespace strata::kernels {
/// Configure before session capture; captured graphs retain their selected kernels.
/// In this mode shared_expert requires its optional unrounded x_f32 input.
void shared_expert_set_native_bf16(bool enabled);
+bool shared_expert_native_bf16(); ///< the native BF16 gate is on: x_bf16 is not read
/// Optional native GGUF projections. Each supported type with nonnull data
/// replaces only that canonical projection; absent or unsupported entries fall
@@ -72,9 +73,12 @@ void shared_expert(const uint8_t* x_q8_0, const uint8_t* x_q8k, const uint16_t*
/// multi-column MMVQ, so the weights are read once; every token is bitwise `shared_expert` on that token.
/// `x` (n_tok, n_embd) f32, `x_bf16` the same rounded (only read when the native BF16 gate is off), `gate`/`up`
/// (n_tok, n_ff) scratch, `g` n_tok floats, `out` (n_tok, n_embd). `nw.q8_1` must hold n_tok columns of n_embd.
+/// x_q8_1_ready: the q8_1 image of x (n_tok rows), already quantized by the caller with the same quantizer (the
+/// same bytes native_quantize_q8_1 writes): its gate/up read it and x is not quantized again, and the SwiGLU and
+/// its q8_1 run as one launch (same values). Null: the separate launches.
void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, const NativeSharedWeights& nw,
const uint16_t* gate_inp_bf16, float* gate, float* up, float* g, float* out, int64_t n_embd,
- int64_t n_ff, void* stream);
+ int64_t n_ff, void* stream, const void* x_q8_1_ready = nullptr);
/// The MoE block's final combination, `ref/moe.py::moe` L156:
///
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index 999f85d86..f6f1a0602 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -56,6 +56,7 @@ constexpr float EPS = 1e-6f;
using Clock = std::chrono::steady_clock;
double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }
const bool g_dbg = std::getenv("STRATA_VERIFY_DEBUG") != nullptr;
+const bool g_qdedup = [] { const char* v = std::getenv("STRATA_VERIFY_QDEDUP"); return v != nullptr && std::atoi(v) != 0; }();
#define VDBG(...) do { if (g_dbg) { std::fprintf(stderr, "verify dbg: " __VA_ARGS__); std::fflush(stderr); } } while (0)
struct Bump {
@@ -358,6 +359,25 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
const char* v = std::getenv("STRATA_VERIFY_DEVICE_PLAN");
device_plan_ = v != nullptr && std::atoi(v) != 0;
}
+ // STRATA_VERIFY_RESIDENT=1 (opt-in): when EVERY routed expert of every layer is resident in the VRAM/UMA tier
+ // (checked here, once; a full cache never evicts), the window graph is recorded without the host round trip:
+ // no doorbell, no flag waits, no plan copy from mapped memory, no PCIe/CPU share (fetch, rebase, its grouped
+ // launch, the mapped-row copy); the device plans each group (the host's exact loop, E-6) and the combine reads
+ // the hit rows as `0.0f + hit` (what the zeroed CPU rows + moe_hit_add gave). Same tokens, same arithmetic.
+ {
+ const char* v = std::getenv("STRATA_VERIFY_RESIDENT");
+ if (v != nullptr && std::atoi(v) != 0 && lb_ == 0 && le_ == g.n_layers && next_ == nullptr) {
+ std::vector<int32_t> res((size_t) (g.n_layers * g.n_expert));
+ bool all = cudaMemcpy(res.data(), hits.d_res, res.size() * sizeof(int32_t), cudaMemcpyDeviceToHost) ==
+ cudaSuccess;
+ for (size_t i = 0; all && i < res.size(); ++i) all = res[i] >= 0;
+ if (all) device_plan_ = true;
+ resident_ = all;
+ std::fprintf(stderr, "strata verify: STRATA_VERIFY_RESIDENT=1: %s\n",
+ all ? "every expert resident, the window runs without the host round trip"
+ : "not every expert is resident - the host-planned window stays");
+ }
+ }
if (device_plan_) {
bool ok2 = cudaMalloc((void**) &skip_, 64) == cudaSuccess && cudaMemset(skip_, 0, 64) == cudaSuccess;
if (ok2 && hits.slot_off != nullptr && hits.n_slots > 0) {
@@ -365,7 +385,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
cudaMemcpy(slot_off_d_, hits.slot_off, (size_t) hits.n_slots * sizeof(unsigned long long),
cudaMemcpyHostToDevice) == cudaSuccess;
}
- if (!ok2) { cudaGetLastError(); device_plan_ = false; }
+ if (!ok2) { cudaGetLastError(); device_plan_ = false; resident_ = false; }
}
std::fprintf(stderr, "strata verify: window up to %d tokens, %.1f MiB of device buffers\n", max_t,
(double) count.used / 1048576.0);
@@ -688,8 +708,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
hits_.cache_base, slot_off_d_, (long long) hits_.blob,
plan_ + (size_t) grp * (size_t) (plan_i32_ + 16), (long long) max_t_ * K, skip_ + grp,
(uint32_t) ((l - lb_) * G + grp + 1), cs);
- doorbell_publish(xm, ids_ + tb * K, w_ + tb * K, (int64_t) n * N, (int64_t) n * K, m_x_ + tb * N,
- m_ids_ + tb * K, m_w_ + tb * K, m_seq_, cs);
+ if (!resident_) // STRATA_VERIFY_RESIDENT: no host step, so no doorbell
+ doorbell_publish(xm, ids_ + tb * K, w_ + tb * K, (int64_t) n * N, (int64_t) n * K, m_x_ + tb * N,
+ m_ids_ + tb * K, m_w_ + tb * K, m_seq_, cs);
stamp(l, 17, grp);
{
const WeightRef *wgi = need(v, "ffn_gate_inp_shexp.weight", err), *wsg = need(v, "ffn_gate_shexp.weight", err),
@@ -703,19 +724,28 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
nsw.up_type = wsu->native_type; nsw.up_data = wsu->native_data;
nsw.down_type = wsd->native_type; nsw.down_data = wsd->native_data;
nsw.q8_1 = xq_;
+ // STRATA_VERIFY_QDEDUP=1: the experts' q8_1 image of xm is made first and the shared expert's gate/up
+ // read it (quantize_q8_1_rows and native_quantize_q8_1 write the same bytes), and the BF16 copy of xm
+ // is skipped when the shared expert's native BF16 gate (which never reads it) is on.
+ const bool qdedup = g_qdedup && strata::kernels::cpu::expert_layout().native;
+ if (qdedup) quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);
+ if (!(qdedup && shared_expert_native_bf16())) {
if (dec_batch) f32_to_bf16_bulk(mixed_ + tb * N, sh_bf16_ + tb * N, (int64_t) n * N, cs); // contiguous rows
else for (int t = tb; t < te; ++t) f32_to_bf16_bulk(mixed_ + t * N, sh_bf16_ + t * N, N, cs);
+ }
try {
shared_expert_multi(n, xm, sh_bf16_ + tb * N, nsw, (const uint16_t*) wgi->data, sh_gate_ + (size_t) tb * g.n_ff,
- sh_up_ + (size_t) tb * g.n_ff, sh_g_ + tb, shared_ + tb * N, N, g.n_ff, cs);
+ sh_up_ + (size_t) tb * g.n_ff, sh_g_ + tb, shared_ + tb * N, N, g.n_ff, cs,
+ qdedup ? (const void*) (nat_xq_ + (size_t) tb * (N / 32) * 36) : nullptr);
} catch (const std::exception& e) {
err = std::string("verify shared expert: ") + e.what();
return false;
}
}
- if (strata::kernels::cpu::expert_layout().native)
- quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);
- else
+ if (strata::kernels::cpu::expert_layout().native) {
+ if (!(g_qdedup && strata::kernels::cpu::expert_layout().native))
+ quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);
+ } else
quantize_q8_0_scaled(xm, hit_xq_ + (size_t) tb * (N / 32) * 34, hit_xs_ + (size_t) tb * (N / 32), (int64_t) n * N, cs);
stamp(l, 18, grp);
return true;
@@ -727,7 +757,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
const uint32_t ring = (uint32_t) ((l - lb_) * G + grp + 1);
const int64_t cap = (int64_t) n * K, capx = (int64_t) max_t_ * K;
int32_t* pl = plan_ + (size_t) grp * (size_t) (plan_i32_ + 16);
- if (device_plan_) { // E-6: skipped when the device planned this group (all its experts resident)
+ if (resident_) {
+ // STRATA_VERIFY_RESIDENT: resident_plan (pre) wrote this group's plan into `pl`; nothing to wait for
+ } else if (device_plan_) { // E-6: skipped when the device planned this group (all its experts resident)
wait_flag_ge_or(m_flagA_, ring, skip_ + grp, cs);
copy_i32_from_mapped_unless(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, skip_ + grp, ring, cs);
} else {
@@ -760,6 +792,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
};
grouped(p_ptr, p_start, p_counts);
stamp(l, 20, grp);
+ if (resident_ && native_moe_combine_enabled()) {
+ // every row is a hit: combine straight from hit_out as 0.0f + hit (= the zeroed row + moe_hit_add)
+ try {
+ native_moe_combine_multi_hits(hit_out, w_ + tb * K, shared_ + tb * N, bo_ + tb * N, N, K, n, cs);
+ } catch (const std::exception& e) { err = "verify combine: " + std::string(e.what()); return false; }
+ } else {
+ if (resident_) {
+ if (cudaMemsetAsync(parts_ + (size_t) tb * K * N, 0, sizeof(float) * (size_t) n * K * N, cs) != cudaSuccess) {
+ err = "verify: zeroing the parts failed";
+ return false;
+ }
+ } else {
if (device_plan_) wait_flag_ge_or(m_flagB_, ring, skip_ + grp, cs);
else wait_flag_ge(m_flagB_, ring, cs); // the PCIe share is in staging (DMA) or mapped
if (sink_.pcie_mode == 2) { // stage it with a copy kernel, then point at staging
@@ -784,6 +828,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
else
copy_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K * N, cs);
}
+ } // !resident_
moe_hit_add(parts_ + (size_t) tb * K * N, hit_out, p_dst, p_counts + 1, cap, N, cs);
if (dec_batch && n > 1 && native_moe_combine_enabled()) { // one launch for the window's rows
try {
@@ -795,6 +840,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
mb.weights = w_ + t * K; mb.shared = shared_ + t * N;
if (!moe_combine_parts(g, l, K, mb, parts_ + (size_t) t * K * N, bo_ + t * N, cs, err)) return false;
}
+ } // the resident combine from hit_out
stamp(l, 24, grp);
if (l == g.n_layers - 1) {
for (int t = tb; t < te; ++t) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);
@@ -1063,7 +1109,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
volatile uint32_t* const flag = h_flag_;
const int G = groups_[T] > 0 ? groups_[T] : 1;
const int gtb[2] = {0, (T + 1) / 2}, gte[2] = {G == 2 ? (T + 1) / 2 : T, T};
- const int64_t steps = (le_ - lb_) * G;
+ const int64_t steps = resident_ ? 0 : (le_ - lb_) * G; // STRATA_VERIFY_RESIDENT: the GPU never rings
const bool test_stall = g_test_stall > 0 && windows + 1 == g_test_stall; // #267 test hook (off: false)
for (int64_t k = 0; k < steps; ++k) {
const int64_t l = lb_ + k / G;
diff --git a/src/kernels/cuda/native_moe.cu b/src/kernels/cuda/native_moe.cu
index 7f1f91ce6..a8fa43374 100644
--- a/src/kernels/cuda/native_moe.cu
+++ b/src/kernels/cuda/native_moe.cu
@@ -31,6 +31,7 @@
namespace strata::kernels {
namespace {
std::atomic<bool> enabled{false};
+template<bool ZADD = false>
__global__ void combine(const float* __restrict__ parts, const float* __restrict__ weights,
const float* __restrict__ shared, float* __restrict__ output,
int64_t n_embd, int k) {
@@ -39,9 +40,14 @@ __global__ void combine(const float* __restrict__ parts, const float* __restrict
parts += tk * k * n_embd; weights += tk * k; if (shared) shared += tk * n_embd; output += tk * n_embd;
const int64_t col = int64_t(blockIdx.x) * blockDim.x + threadIdx.x;
if (col >= n_embd) return;
- float sum = parts[col] * weights[0];
+ // ZADD: a part is 0.0f + hit (the zeroed row plus moe_hit_add of the verify window), rounded as that add
+ float p0 = parts[col];
+ if (ZADD) p0 = 0.0f + p0;
+ float sum = p0 * weights[0];
for (int expert = 1; expert < k; ++expert) {
- sum += parts[int64_t(expert) * n_embd + col] * weights[expert];
+ float p = parts[int64_t(expert) * n_embd + col];
+ if (ZADD) p = 0.0f + p;
+ sum += p * weights[expert];
}
if (shared) sum += shared[col];
output[col] = sum;
@@ -83,4 +89,13 @@ void native_moe_combine_multi(const float* parts, const float* weights, const fl
const auto error = cudaGetLastError();
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
}
+void native_moe_combine_multi_hits(const float* hits, const float* weights, const float* shared, float* output,
+ int64_t n_embd, int64_t k, int n_tok, void* stream) {
+ if (!stream || n_embd <= 0 || k < 1 || k > 15 || n_tok < 1)
+ throw std::invalid_argument("native MoE combine (hits) requires a stream, width, 1..15 experts, tokens");
+ combine<true><<<dim3(unsigned((n_embd + 255) / 256), (unsigned) n_tok), 256, 0, static_cast<cudaStream_t>(stream)>>>(
+ hits, weights, shared, output, n_embd, int(k));
+ const auto error = cudaGetLastError();
+ if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
+}
}
diff --git a/src/kernels/cuda/shared_expert.cu b/src/kernels/cuda/shared_expert.cu
index d795bf840..1bb5c997d 100644
--- a/src/kernels/cuda/shared_expert.cu
+++ b/src/kernels/cuda/shared_expert.cu
@@ -27,6 +27,7 @@
#include "strata/kernels/s_gemv.hpp"
#include "strata/kernels/native_mmvq.hpp"
+#include <cuda_fp16.h>
#include <cuda_runtime.h>
#include <cmath>
@@ -68,6 +69,27 @@ __global__ void native_swiglu_kernel(const float* gate, const float* up, float*
out[i] = __fdividef(gate[i], 1.0f + __expf(-gate[i])) * up[i];
}
+
+// STRATA_VERIFY_QDEDUP: native_swiglu_kernel and native_quantize_q8_1_kernel in one launch. Each thread computes
+// the same swiglu expression (and still writes it to `out`), then the 32-lane block's q8_1 exactly as
+// native_quantize_q8_1_kernel does (XOR-tree max / sum, d = amax / 127, roundf(x / d), ds = (d, sum)).
+struct SwqQ81 { half2 ds; int8_t qs[32]; };
+__global__ void native_swiglu_q8_1_kernel(const float* gate, const float* up, float* out, SwqQ81* __restrict__ y,
+ int n) {
+ const int i = blockIdx.x * 256 + threadIdx.x;
+ if (i >= n) return; // n is a multiple of 32: only whole warps return
+ const float xi = __fdividef(gate[i], 1.0f + __expf(-gate[i])) * up[i];
+ out[i] = xi;
+ float amax = fabsf(xi), sum = xi;
+#pragma unroll
+ for (int offset = 16; offset > 0; offset >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, offset, 32));
+#pragma unroll
+ for (int offset = 16; offset > 0; offset >>= 1) sum += __shfl_xor_sync(0xffffffff, sum, offset, 32);
+ const float d = amax / 127.0f;
+ const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
+ y[i / 32].qs[i % 32] = q;
+ if (i % 32 == 0) y[i / 32].ds = make_half2(d, sum);
+}
__global__ void to_f16_kernel(const float* __restrict__ in, uint16_t* __restrict__ out, int n) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) out[i] = f16_from_f32(in[i]);
@@ -157,6 +179,7 @@ __global__ void moe_combine_kernel(const float* __restrict__ parts, const float*
} // namespace
void shared_expert_set_native_bf16(bool enabled) { native_bf16 = enabled; }
+bool shared_expert_native_bf16() { return native_bf16; }
namespace {
__global__ void scale_rows_kernel(float* __restrict__ out, const float* __restrict__ g, int n) {
@@ -168,16 +191,21 @@ __global__ void scale_rows_kernel(float* __restrict__ out, const float* __restri
void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, const NativeSharedWeights& nw,
const uint16_t* gate_inp_bf16, float* gate, float* up, float* g, float* out, int64_t n_embd,
- int64_t n_ff, void* stream) {
+ int64_t n_ff, void* stream, const void* x_q8_1_ready) {
if (n_tok < 1 || n_tok > 8 || !nw.q8_1 || !nw.gate_data || !nw.up_data || !nw.down_data || !stream)
throw std::invalid_argument("shared_expert_multi: needs 1..8 tokens, native weights, scratch and a stream");
cudaStream_t cs = (cudaStream_t) stream;
- native_quantize_q8_1(x, nw.q8_1, (int) n_embd, n_tok, stream);
- native_mmvq(nw.gate_type, nw.gate_data, nw.q8_1, gate, (int) n_embd, (int) n_ff, n_tok, stream);
- native_mmvq(nw.up_type, nw.up_data, nw.q8_1, up, (int) n_embd, (int) n_ff, n_tok, stream);
+ const void* xq = x_q8_1_ready ? x_q8_1_ready : nw.q8_1;
+ if (!x_q8_1_ready) native_quantize_q8_1(x, nw.q8_1, (int) n_embd, n_tok, stream);
+ native_mmvq(nw.gate_type, nw.gate_data, xq, gate, (int) n_embd, (int) n_ff, n_tok, stream);
+ native_mmvq(nw.up_type, nw.up_data, xq, up, (int) n_embd, (int) n_ff, n_tok, stream);
const int n = (int) (n_ff * n_tok);
- native_swiglu_kernel<<<(unsigned) ((n + THREADS - 1) / THREADS), THREADS, 0, cs>>>(gate, up, gate, n);
- native_quantize_q8_1(gate, nw.q8_1, (int) n_ff, n_tok, stream);
+ if (x_q8_1_ready != nullptr && (n_ff % 32) == 0) { // STRATA_VERIFY_QDEDUP: swiglu + its q8_1 in one launch
+ native_swiglu_q8_1_kernel<<<(unsigned) ((n + 255) / 256), 256, 0, cs>>>(gate, up, gate, (SwqQ81*) nw.q8_1, n);
+ } else {
+ native_swiglu_kernel<<<(unsigned) ((n + THREADS - 1) / THREADS), THREADS, 0, cs>>>(gate, up, gate, n);
+ native_quantize_q8_1(gate, nw.q8_1, (int) n_ff, n_tok, stream);
+ }
native_mmvq(nw.down_type, nw.down_data, nw.q8_1, out, (int) n_ff, (int) n_embd, n_tok, stream);
static const bool batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
if (native_bf16 && batch && n_tok > 1) { // one gemv for all rows (outputs identical), one sigmoid launch
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index 7908ce404..1272f7e03 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -471,6 +471,71 @@ __global__ void resident_plan_kernel(const int32_t* __restrict__ ids, int n, int
__threadfence();
*skip = ring;
}
+// The same plan in one block of 128 threads (n <= 128): thread i owns entry i. Groups are the distinct experts in
+// order of first occurrence; group g's entries are its occurrences in increasing i - exactly the loop above
+// (S26: the one-thread loop took ~44 us per call on gfx1151, 48 per window).
+__global__ void resident_plan_par_kernel(const int32_t* __restrict__ ids, int n, int k, const int32_t* __restrict__ res,
+ int n_expert, const uint8_t* cache_base, const unsigned long long* slot_off,
+ long long blob, int32_t* __restrict__ pl, long long capx, uint32_t* skip,
+ uint32_t ring) {
+ __shared__ int32_t s_id[128];
+ __shared__ int s_first[128], s_size[128];
+ __shared__ int s_bad;
+ const int i = threadIdx.x;
+ if (i == 0) s_bad = 0;
+ __syncthreads();
+ int32_t e = -1;
+ if (i < n) {
+ e = ids[i];
+ s_id[i] = e;
+ if (e < 0 || e >= n_expert || res[e] < 0) s_bad = 1;
+ }
+ __syncthreads();
+ if (s_bad) { if (i == 0) *skip = 0; return; }
+ int first = i, rank = 0, size = 0;
+ if (i < n) {
+ for (int j = 0; j < n; ++j) {
+ const bool same = s_id[j] == e;
+ if (same && j < first) first = j;
+ if (same && j < i) ++rank;
+ size += same;
+ }
+ }
+ if (i < n) { s_first[i] = first; s_size[i] = size; }
+ __syncthreads();
+ int32_t* counts = pl;
+ int32_t* start = pl + 4;
+ int32_t* dst = start + capx + 1;
+ int32_t* tok = dst + capx;
+ const long long ptr_off = ((4 + (capx + 1) + 2 * capx) + 1) & ~1ll;
+ unsigned long long* ptr = (unsigned long long*) (pl + ptr_off);
+ int32_t* start2 = pl + ptr_off + 4 * capx;
+ if (i < n) {
+ // group of entry i = the number of first occurrences before `first`; its start = their sizes' sum
+ int group = 0, gstart = 0;
+ for (int j = 0; j < first; ++j)
+ if (s_first[j] == j) { ++group; gstart += s_size[j]; }
+ if (i == first) {
+ const int32_t slot = res[e];
+ ptr[group] = (unsigned long long) (cache_base + (slot_off ? (size_t) slot_off[slot] : (size_t) slot * (size_t) blob));
+ start[group] = gstart;
+ }
+ dst[gstart + rank] = i;
+ tok[gstart + rank] = i / k;
+ if (i == 0) {
+ int groups = 0;
+ for (int j = 0; j < n; ++j) groups += s_first[j] == j;
+ start[groups] = n;
+ start2[0] = n;
+ counts[0] = groups;
+ counts[1] = n;
+ counts[2] = 0;
+ }
+ }
+ __threadfence();
+ __syncthreads();
+ if (i == 0) *skip = ring;
+}
__global__ void wait_flag_ge_or_kernel(const volatile uint32_t* flag, uint32_t value, const volatile uint32_t* skip) {
if (*skip == value) return;
while (*flag < value) strata_spin_pause();
@@ -492,8 +557,12 @@ __global__ void copy_or_zero_kernel(float4* __restrict__ dst, const volatile flo
void resident_plan(const int32_t* ids, int n_entries, int k, const int32_t* res_layer, int n_expert,
const uint8_t* cache_base, const unsigned long long* slot_off, long long blob, int32_t* plan,
long long capx, uint32_t* skip, uint32_t ring, void* stream) {
- resident_plan_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(ids, n_entries, k, res_layer, n_expert, cache_base, slot_off,
- blob, plan, capx, skip, ring);
+ if (n_entries <= 128)
+ resident_plan_par_kernel<<<1, 128, 0, (cudaStream_t) stream>>>(ids, n_entries, k, res_layer, n_expert, cache_base,
+ slot_off, blob, plan, capx, skip, ring);
+ else
+ resident_plan_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(ids, n_entries, k, res_layer, n_expert, cache_base, slot_off,
+ blob, plan, capx, skip, ring);
check("resident_plan");
}
void wait_flag_ge_or(const uint32_t* flag, uint32_t value, const uint32_t* skip, void* stream) {