feat(kar): isolate target native routed storage in explicit mode
KAR CUDA compile (GPU hidden) / kar-cuda-compile (pull_request) Failing after 52s
KAR CUDA compile (GPU hidden) / kar-cuda-compile (pull_request) Failing after 52s
This commit is contained in:
@@ -5,11 +5,15 @@ on:
|
||||
paths:
|
||||
- 'gpu/kar/**'
|
||||
- 'engine/strata/CMakeLists.txt'
|
||||
- 'engine/strata/include/strata/core/**'
|
||||
- 'engine/strata/src/core/**'
|
||||
- 'engine/strata/include/strata/prefill/**'
|
||||
- 'engine/strata/src/prefill/**'
|
||||
- 'engine/strata/src/program/generate.cpp'
|
||||
- 'tests/kar-prefill-contract.cpp'
|
||||
- 'scripts/test-kar-prefill-contract.sh'
|
||||
- 'tests/kar-all-routed-contract.cpp'
|
||||
- 'scripts/test-kar-all-routed-contract.sh'
|
||||
- 'docs/upstream.md'
|
||||
- 'gpu/kar/README.md'
|
||||
- 'scripts/bootstrap-cuda-toolkit.py'
|
||||
@@ -84,6 +88,7 @@ jobs:
|
||||
python3 scripts/test-kar-cuda-build-lock.py
|
||||
CXX=g++-13 scripts/test-kar-expert-cache.sh
|
||||
CXX=g++-13 scripts/test-kar-prefill-contract.sh
|
||||
CXX=g++-13 scripts/test-kar-all-routed-contract.sh
|
||||
python3 scripts/bootstrap-cuda-toolkit.py --output /tmp/cuda-toolkit
|
||||
export CUDACXX=/tmp/cuda-toolkit/bin/nvcc
|
||||
export CUDAToolkit_ROOT=/tmp/cuda-toolkit
|
||||
|
||||
@@ -65,3 +65,49 @@ Host grouping contracts and GPU-hidden native-engine compilation are separate
|
||||
from real GPU validation. Tensor/logit parity, compressed residency, model
|
||||
quality and paired end-to-end measurements remain mandatory; this source change
|
||||
does not establish those results or enable the experimental profile by default.
|
||||
|
||||
## Experimental KAR all-routed target isolation, 2026-10-07
|
||||
|
||||
The pinned upstream SHA remains unchanged. A separate local default-off
|
||||
`STRATA_NEXT_KAR_ALL_ROUTED=1` arm requires `STRATA_NEXT_KAR_PREFILL=1`, the
|
||||
experimental KAR CUDA build, explicit expert archive and level, a native pack,
|
||||
fixed positive `--prefill CHUNK`, `--spec 2..8`, `--expert-cache 0`, `--pcie-frac 0`, and disabled
|
||||
adaptive native cache (`--adapt-every 0` or `--adapt-swaps 0`). It rejects native
|
||||
expert profiles, mmap/resident/shared native banks, nonzero PCIe share, native
|
||||
cache policy/order switches, disabled callbacks, GPU-floor/no-capture modes,
|
||||
multi-device configurations, automatic chunks and a native device expert plan.
|
||||
Admission precedes opening the KAR backend or mutating its runtime options.
|
||||
|
||||
For the target layers, this arm omits opening `ArenaExpertSource` or
|
||||
`FileExpertSource`, native `ExpertCache` slots and native hit buffers. The early
|
||||
multi-token callback validates the whole window before any native lookahead,
|
||||
source begin/prefetch, quantization, plan or CPU expert job. Every routed row
|
||||
starts unowned and must return as a finite unweighted `kind=3` row in routing
|
||||
order; malformed input, duplicate experts within a token, missing owners,
|
||||
unhandled rows or backend faults fail the layer. Completed KAR rows count as
|
||||
native cache refusals, never native hits; native CPU job/miss counts remain zero.
|
||||
The verifier accepts an absent native tier only in this explicit mode, requires
|
||||
the host callback, copies all completed host rows to its existing combine and
|
||||
omits native routed activation/hit/PCIe staging buffers and kernels. Its normal
|
||||
empty-plan completion publishes the A/B flags after the callback returns.
|
||||
|
||||
Prefill selects this allocation mode before initialization and keeps it across
|
||||
reset and relayout. Counting and carving use the same mode; routed GU, fallback
|
||||
activation/intermediate, MMQ, dequantized expert and staging rings, native
|
||||
stager workers and ring events are omitted. Router/grouping, routed output rows,
|
||||
shared experts, dense/embedding/head/PLE, attention, KV/indexer and session
|
||||
state remain. Attention and MoE scratch share a region sized by their maximum,
|
||||
so omitted MoE buffers cannot be added together as a claimed VRAM saving. The
|
||||
CPU expert-pool object remains for existing callback plumbing but runs no native
|
||||
routed jobs. The MTP drafter still uploads its separate native draft experts
|
||||
when configured: this is **not** a zero-copy full compressed-model claim.
|
||||
|
||||
`scripts/test-kar-all-routed-contract.sh` uses synthetic expert matrices and
|
||||
non-unit router weights to check 128 rows against an independent CPU combine
|
||||
oracle (absolute tolerance `1e-5`), strict admission, successful counters/usage,
|
||||
null native handles, bounds, ownership, nonfinite/duplicate input and injected
|
||||
backend faults under address/undefined-behavior sanitizers. The existing
|
||||
prefill contract test also passes. These CPU results establish host contracts;
|
||||
CUDA compilation, real tensor/logit parity, model quality, allocation telemetry
|
||||
and paired end-to-end performance measurements are separate gates. No quality,
|
||||
measured memory saving or performance qualification is established by this patch.
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
namespace strata::core {
|
||||
|
||||
// Separate from KAR misses and KAR_PREFILL: this arm removes the target's
|
||||
// native routed bank. Every selected row must therefore have a KAR owner.
|
||||
inline bool parse_experimental_all_routed_flag(const char* text, bool& enabled, std::string& error) {
|
||||
enabled = text && std::strcmp(text, "1") == 0;
|
||||
if (!text || std::strcmp(text, "0") == 0 || enabled) return true;
|
||||
error = "STRATA_NEXT_KAR_ALL_ROUTED must be exactly 0 or 1";
|
||||
return false;
|
||||
}
|
||||
|
||||
struct ExperimentalRoutedProfile {
|
||||
bool built = false, native_pack = false, prefill = false;
|
||||
bool no_pool = false, no_capture = false, multi_device = false;
|
||||
bool native_bank_option = false, adaptive_cache = false, device_plan = false;
|
||||
const char* archive = nullptr;
|
||||
const char* level = nullptr;
|
||||
int64_t chunk = 0, window = 0;
|
||||
bool automatic_chunk = false;
|
||||
int native_cache = 0;
|
||||
bool native_profile = false;
|
||||
};
|
||||
|
||||
// Admission must precede archive GPU allocations and changes to options.
|
||||
inline bool validate_experimental_routed_profile(const ExperimentalRoutedProfile& p, std::string& error) {
|
||||
if (!p.built || !p.native_pack || !p.prefill || !p.archive || !*p.archive ||
|
||||
!p.level || std::strlen(p.level) != 1 || p.level[0] < '1' || p.level[0] > '8' ||
|
||||
p.chunk <= 0 || p.automatic_chunk || p.window < 2 || p.window > 8 ||
|
||||
p.no_pool || p.no_capture || p.multi_device || p.native_bank_option ||
|
||||
p.adaptive_cache || p.device_plan || p.native_cache != 0 || p.native_profile) {
|
||||
error = "KAR all-routed requires the experimental CUDA build, native pack, explicit archive/level, "
|
||||
"KAR_PREFILL=1, fixed --prefill CHUNK, --spec 2..8, enabled callback, --expert-cache 0, --pcie-frac 0, "
|
||||
"--adapt-every 0 (or --adapt-swaps 0), one GPU, and no native expert profile/bank or device plan";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
using ExperimentalRoutedBackend = bool (*)(void*, int64_t, const float*, const int32_t*,
|
||||
int64_t, int64_t, int32_t*, float*, std::string&);
|
||||
|
||||
// Unweighted rows in routing order: the caller's existing combine applies weights.
|
||||
// No source, native pool, residency table, activation quantizer or plan is needed.
|
||||
inline bool dispatch_experimental_all_routed(int64_t layer, int64_t layers, int64_t hidden, int64_t experts,
|
||||
const float* input, const int32_t* ids, int64_t tokens, int64_t routes,
|
||||
ExperimentalRoutedBackend backend, void* user, float* output,
|
||||
int64_t& bad_expert, std::string& error) {
|
||||
bad_expert = -1;
|
||||
error.clear();
|
||||
if (layers < 1 || layers > 512 || layer < 0 || layer >= layers || hidden < 1 || hidden > 16384 || experts < 1 ||
|
||||
experts > 65536 || tokens < 1 || tokens > 8 || routes < 1 || routes > 16 ||
|
||||
!input || !ids || !output || !backend || !user) {
|
||||
error = "invalid KAR all-routed dispatch"; return false;
|
||||
}
|
||||
const int64_t entries = tokens * routes;
|
||||
for (int64_t i = 0; i < tokens * hidden; ++i)
|
||||
if (!std::isfinite(input[i])) { error = "non-finite KAR routed activation"; return false; }
|
||||
for (int64_t i = 0; i < entries; ++i) {
|
||||
if (ids[i] < 0 || ids[i] >= experts) {
|
||||
bad_expert = ids[i]; error = "a routed expert id is out of range"; return false;
|
||||
}
|
||||
// Top-k never duplicates an expert within a token. Refuse an invalid
|
||||
// group before the backend can partially stage or write its results.
|
||||
for (int64_t j = i / routes * routes; j < i; ++j)
|
||||
if (ids[j] == ids[i]) {
|
||||
bad_expert = ids[i]; error = "duplicate KAR routed expert within a token"; return false;
|
||||
}
|
||||
}
|
||||
std::array<int32_t, 128> kind;
|
||||
kind.fill(-1);
|
||||
std::fill(output, output + entries * hidden, std::numeric_limits<float>::quiet_NaN());
|
||||
if (!backend(user, layer, input, ids, tokens, routes, kind.data(), output, error)) {
|
||||
if (error.empty()) error = "KAR all-routed backend failed";
|
||||
return false;
|
||||
}
|
||||
for (int64_t i = 0; i < entries; ++i)
|
||||
if (kind[(size_t) i] != 3) { error = "KAR all-routed backend left an unhandled row"; return false; }
|
||||
for (int64_t i = 0; i < entries * hidden; ++i)
|
||||
if (!std::isfinite(output[i])) { error = "KAR all-routed backend returned a non-finite row"; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
// The actual early dispatcher is CPU-testable without CUDA or a native source.
|
||||
// A failure preserves the failing layer and completed counters; later calls
|
||||
// stay failed. Keep the error storage alive for ExpertDispatch::fail.
|
||||
template<class Dispatch>
|
||||
bool dispatch_experimental_routed_window(Dispatch& d, int64_t hidden, const float* input,
|
||||
const int32_t* ids, int64_t tokens, int64_t routes, float* output) {
|
||||
if (d.failed) return false;
|
||||
static thread_local std::string error;
|
||||
int64_t bad_expert = -1;
|
||||
error.clear();
|
||||
const bool usage_ok = d.usage.empty() || (d.layers >= 0 && d.n_expert > 0 &&
|
||||
(uint64_t) d.layers < (uint64_t) d.usage.size() / (uint64_t) d.n_expert);
|
||||
if (!usage_ok) error = "KAR all-routed usage table is too small";
|
||||
if (!usage_ok || !dispatch_experimental_all_routed(d.layers, d.experimental_layers, hidden, d.n_expert,
|
||||
input, ids, tokens, routes, d.experimental_miss, d.experimental_miss_user, output, bad_expert, error)) {
|
||||
d.failed = true; d.fail = error.c_str(); d.fail_layer = d.layers; d.fail_expert = bad_expert;
|
||||
return false;
|
||||
}
|
||||
if (!d.usage.empty())
|
||||
for (int64_t i = 0; i < tokens * routes; ++i)
|
||||
d.usage[(size_t) d.layers * (size_t) d.n_expert + (size_t) ids[i]] += 1.0f;
|
||||
d.cache_refused += tokens * routes; // completed KAR rows, never native hits
|
||||
d.experts += tokens * routes;
|
||||
++d.layers;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace strata::core
|
||||
@@ -217,6 +217,8 @@ struct ExpertDispatch {
|
||||
int64_t, int64_t, int32_t*, float*, std::string&);
|
||||
ExperimentalMiss experimental_miss = nullptr;
|
||||
void* experimental_miss_user = nullptr;
|
||||
bool experimental_all_routed = false; ///< strict separate arm; no native source or cache fallback
|
||||
int64_t experimental_layers = 0; ///< target archive layer bound, not the MTP draft layer
|
||||
strata::kernels::cpu::ExpertPool* pool = nullptr;
|
||||
ExpertSource* src = nullptr;
|
||||
RouterLookahead* lookahead = nullptr; ///< CS-T: warms the next layer's predicted file-tier experts
|
||||
|
||||
@@ -44,6 +44,7 @@ using PoolMultiFn = void (*)(void* user, const float* x_f, const int32_t* ids, i
|
||||
int64_t layer);
|
||||
|
||||
struct VerifyHits {
|
||||
bool experimental_all_routed = false; ///< no native routed tier; mandatory host KAR callback
|
||||
const int32_t* d_res = nullptr; ///< device [n_layers * n_expert] slot or -1
|
||||
const uint8_t* cache_base = nullptr; ///< slot 0 of the VRAM expert arena
|
||||
const uint64_t* slot_off = nullptr; ///< E-6: host per-slot offsets when slots differ in size (null: slot * blob)
|
||||
|
||||
@@ -66,6 +66,10 @@ public:
|
||||
experimental_experts_ = backend; experimental_user_ = user;
|
||||
}
|
||||
|
||||
/// Separate strict all-routed arm: omit native routed scratch/staging.
|
||||
/// Configure before init; immutable across relayout and survives reset.
|
||||
bool set_experimental_all_routed(ExperimentalExperts backend, void* user, std::string& err);
|
||||
|
||||
/// `host_res`: the static residency table (n_layers x n_expert, slot or -1) or null; `cache` its slots.
|
||||
/// `borrow`/`borrow_bytes`: device memory to carve every buffer from (the top slots of the expert cache,
|
||||
/// lent for the prompt and refilled after it); null = allocate normally.
|
||||
@@ -87,7 +91,8 @@ public:
|
||||
static void set_ring_override(int slots);
|
||||
|
||||
/// Device bytes `init` needs for a chunk of `chunk` tokens (what a borrowed region must hold).
|
||||
static uint64_t bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk);
|
||||
static uint64_t bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
|
||||
bool experimental_all_routed = false);
|
||||
|
||||
/// Positions [pos0, pos0 + n) holding `tokens`; `ss.ple_prev` must be the two tokens before pos0 (oldest
|
||||
/// first, -1 for none) and is advanced to the last two of these.
|
||||
@@ -125,6 +130,7 @@ public:
|
||||
private:
|
||||
ExperimentalExperts experimental_experts_ = nullptr;
|
||||
void* experimental_user_ = nullptr;
|
||||
bool experimental_all_routed_ = false;
|
||||
int64_t stage_lb_ = 0, stage_le_ = -1;
|
||||
Prefill* next_ = nullptr;
|
||||
const float* hand_in_ = nullptr; ///< the previous stage's rows of the chunk being read (host, pinned)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// src/core/expert_source.cpp - the adapter. See the header for the three clauses of the contract.
|
||||
#include "strata/core/expert_source.hpp"
|
||||
#include "strata/core/experimental_routed.hpp"
|
||||
#include "strata/core/remote_experts.hpp"
|
||||
#include "strata/kernels/cpu/expert_layout.hpp"
|
||||
#include "strata/artifact/gguf_reader.hpp"
|
||||
@@ -1615,6 +1616,16 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
|
||||
float* out) {
|
||||
using namespace strata::kernels::cpu;
|
||||
if (d.failed) return;
|
||||
// Must precede lookahead, source begin/prefetch, native planning and pool
|
||||
// activation/job allocations. The all-routed arm has no native expert bank.
|
||||
if (d.experimental_all_routed) {
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
if (!dispatch_experimental_routed_window(d, H, x_f, ids, n_tok, k, out)) return;
|
||||
d.ms_run += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
|
||||
// Verifier::run publishes an empty native plan after this callback;
|
||||
// all host rows cross to its existing combine, with router weights once.
|
||||
return;
|
||||
}
|
||||
if (n_tok < 1 || n_tok > MAXT) {
|
||||
d.failed = true;
|
||||
d.fail = "a verify window has more tokens than the multi-token expert kernel takes";
|
||||
|
||||
@@ -190,6 +190,11 @@ Verifier::~Verifier() {
|
||||
|
||||
bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& ss, const VerifyHits& hits,
|
||||
const NativeHead* head, int max_t, std::string& err) {
|
||||
if (hits.experimental_all_routed && (hits.d_res || hits.cache_base || hits.slot_off || hits.n_slots || hits.blob ||
|
||||
lb_ != 0 || (le_ >= 0 && le_ != g.n_layers) || hand_in_ || hand_out_)) {
|
||||
err = "verify: all-routed mode requires one complete target stage and no native expert tier";
|
||||
return false;
|
||||
}
|
||||
g_diag_verifier.store(this);
|
||||
diag_verify_fn().store(&diag_active_verifier);
|
||||
for (auto& slot : g_live) {
|
||||
@@ -226,7 +231,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
err = "verify: the window must hold 2.." + std::to_string(strata::kernels::kVerifyMaxT) + " tokens";
|
||||
return false;
|
||||
}
|
||||
if (hits.d_res == nullptr || hits.cache_base == nullptr || hits.blob <= 0) {
|
||||
if (!hits.experimental_all_routed && (hits.d_res == nullptr || hits.cache_base == nullptr || hits.blob <= 0)) {
|
||||
err = "verify: needs the profile-filled VRAM expert tier (--expert-profile and --expert-cache); with "
|
||||
"--expert-cache auto, no VRAM was left for it: lower --max-context, use --kv k8v4 or images on the CPU";
|
||||
return false;
|
||||
@@ -324,15 +329,20 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
attn_scratch_ = b.take<float>(T * (uint64_t) attn_scratch_floats_);
|
||||
tail_snap_ = b.take<float>(nQ * TS);
|
||||
logits_ = b.take<float>(T * (uint64_t) g.n_expert); w_ = b.take<float>(T * K); ids_ = b.take<int32_t>(T * K);
|
||||
shared_ = b.take<float>(T * N); parts_ = b.take<float>(T * K * N); hit_out_ = b.take<float>(T * K * N);
|
||||
hit_slot_ = b.take<int32_t>(T * K); hit_dst_ = b.take<int32_t>(T * K); hit_count_ = b.take<int32_t>(4);
|
||||
shared_ = b.take<float>(T * N); parts_ = b.take<float>(T * K * N);
|
||||
if (!hits.experimental_all_routed) {
|
||||
hit_out_ = b.take<float>(T * K * N);
|
||||
hit_slot_ = b.take<int32_t>(T * K); hit_dst_ = b.take<int32_t>(T * K); hit_count_ = b.take<int32_t>(4);
|
||||
}
|
||||
plan_ = b.take<int32_t>(2 * ((uint64_t) plan_i32_ + 16));
|
||||
staging_ = b.take<uint8_t>((uint64_t) kStagingBlobs * strata::kernels::cpu::expert_layout().max_blob);
|
||||
hit_xq_ = b.take<uint8_t>(T * (N / 32) * 34); hit_xs_ = b.take<float>(T * (N / 32));
|
||||
nat_xq_ = b.take<uint8_t>(T * (N / 32) * 36);
|
||||
hit_scratch_ = b.take<uint8_t>(std::max<uint64_t>(
|
||||
strata::kernels::moe_hit_grouped_scratch_bytes((int64_t) (T * K), g.n_embd, g.n_ff),
|
||||
strata::kernels::native_expert_scratch_bytes((int64_t) (T * K), g.n_ff)));
|
||||
if (!hits.experimental_all_routed) {
|
||||
staging_ = b.take<uint8_t>((uint64_t) kStagingBlobs * strata::kernels::cpu::expert_layout().max_blob);
|
||||
hit_xq_ = b.take<uint8_t>(T * (N / 32) * 34); hit_xs_ = b.take<float>(T * (N / 32));
|
||||
nat_xq_ = b.take<uint8_t>(T * (N / 32) * 36);
|
||||
hit_scratch_ = b.take<uint8_t>(std::max<uint64_t>(
|
||||
strata::kernels::moe_hit_grouped_scratch_bytes((int64_t) (T * K), g.n_embd, g.n_ff),
|
||||
strata::kernels::native_expert_scratch_bytes((int64_t) (T * K), g.n_ff)));
|
||||
}
|
||||
head_mixed_ = b.take<float>(T * N); head_inj_ = b.take<float>(HC);
|
||||
sh_bf16_ = b.take<uint16_t>(T * N); sh_gate_ = b.take<float>(T * (uint64_t) g.n_ff);
|
||||
sh_up_ = b.take<float>(T * (uint64_t) g.n_ff); sh_g_ = b.take<float>(T + 4);
|
||||
@@ -356,7 +366,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
real.base = (uint8_t*) arena_;
|
||||
carve(real);
|
||||
sink_.staging = (unsigned long long) staging_;
|
||||
sink_.staging_cap = kStagingBlobs;
|
||||
sink_.staging_cap = hits.experimental_all_routed ? 0 : kStagingBlobs;
|
||||
(void) TS;
|
||||
if (cudaStreamCreateWithFlags(©_, cudaStreamNonBlocking) != cudaSuccess) {
|
||||
err = "verify: copy stream create failed";
|
||||
@@ -374,7 +384,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
// exact, but neutral on RIBPC 1-2 GPUs: off by default)
|
||||
{
|
||||
const char* v = std::getenv("STRATA_VERIFY_DEVICE_PLAN");
|
||||
device_plan_ = v != nullptr && std::atoi(v) != 0;
|
||||
device_plan_ = !hits.experimental_all_routed && v != nullptr && std::atoi(v) != 0;
|
||||
}
|
||||
if (device_plan_) {
|
||||
bool ok2 = cudaMalloc((void**) &skip_, 64) == cudaSuccess && cudaMemset(skip_, 0, 64) == cudaSuccess;
|
||||
@@ -751,7 +761,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (strata::kernels::cpu::expert_layout().native)
|
||||
if (hits_.experimental_all_routed) {
|
||||
// KAR consumes the original normed FP32 rows, not native routed activations.
|
||||
} else if (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);
|
||||
@@ -764,6 +776,13 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
const int tb = tb_[grp], te = te_[grp], n = te - tb;
|
||||
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;
|
||||
if (hits_.experimental_all_routed) {
|
||||
// Every row belongs to the strict host backend. Waiting for the
|
||||
// normal completion flag preserves failure/release behavior.
|
||||
wait_flag_ge(m_flag_, ring, cs);
|
||||
copy_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N,
|
||||
(int64_t) n * K * N, cs);
|
||||
} else {
|
||||
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)
|
||||
wait_flag_ge_or(m_flagA_, ring, skip_ + grp, cs);
|
||||
@@ -823,6 +842,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
copy_from_mapped(parts_ + (size_t) tb * K * N, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K * N, cs);
|
||||
}
|
||||
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 {
|
||||
native_moe_combine_multi(parts_ + (size_t) tb * K * N, w_ + tb * K, shared_ + tb * N, bo_ + tb * N, N, K, n, cs);
|
||||
@@ -1054,6 +1074,9 @@ bool Verifier::capture_commit(std::string& err) {
|
||||
bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out,
|
||||
std::string& err) {
|
||||
using namespace strata::kernels;
|
||||
if (hits_.experimental_all_routed && (!pool || !user)) {
|
||||
err = "verify: all-routed mode requires its host expert callback"; return false;
|
||||
}
|
||||
const OnDevice on_device(device_);
|
||||
if (T < 1 || T > max_t_) { err = "verify: window size out of range"; return false; }
|
||||
if (released_.load()) { err = "verify: an earlier window never finished on the GPU (#267); restart the engine"; return false; }
|
||||
@@ -1279,8 +1302,9 @@ void Verifier::set_plan_slot(int grp) {
|
||||
sink_.start2 = base + ptr_off + 4 * cap;
|
||||
const int G = groups_[last_t_] > 0 ? groups_[last_t_] : 1;
|
||||
const int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;
|
||||
sink_.staging = (unsigned long long) (staging_ + (size_t) (grp * per) * strata::kernels::cpu::expert_layout().max_blob);
|
||||
sink_.staging_cap = per;
|
||||
sink_.staging = hits_.experimental_all_routed ? 0 :
|
||||
(unsigned long long) (staging_ + (size_t) (grp * per) * strata::kernels::cpu::expert_layout().max_blob);
|
||||
sink_.staging_cap = hits_.experimental_all_routed ? 0 : per;
|
||||
}
|
||||
|
||||
// Flag B only rises: a host function of an earlier layer may run after a later layer already raised it directly.
|
||||
|
||||
@@ -290,6 +290,9 @@ struct Stager {
|
||||
};
|
||||
|
||||
struct Prefill::Impl {
|
||||
bool experimental_all_routed = false;
|
||||
ExperimentalExperts routed_backend = nullptr;
|
||||
void* routed_user = nullptr;
|
||||
const core::WeightTable* wt = nullptr;
|
||||
const core::ModelGeometry* g = nullptr;
|
||||
core::SessionState* ss = nullptr;
|
||||
@@ -411,6 +414,17 @@ strata::kernels::QsaAttnPools pools_of(const strata::kernels::KvHostPools& h, co
|
||||
Prefill::Prefill() : impl_(new Impl) {}
|
||||
Prefill::~Prefill() { release(); }
|
||||
|
||||
bool Prefill::set_experimental_all_routed(ExperimentalExperts backend, void* user, std::string& err) {
|
||||
if (impl_->g != nullptr || !backend || !user) {
|
||||
err = "prefill: all-routed backend must be configured before init with an owner";
|
||||
return false;
|
||||
}
|
||||
experimental_all_routed_ = true;
|
||||
experimental_experts_ = backend;
|
||||
experimental_user_ = user;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Prefill::reset() {
|
||||
release();
|
||||
impl_.reset(new Impl);
|
||||
@@ -495,17 +509,17 @@ const MmqPlan& mmq_plan() {
|
||||
}();
|
||||
return plan;
|
||||
}
|
||||
uint64_t moe_set_bytes(size_t T, int64_t n_expert) {
|
||||
uint64_t moe_set_bytes(size_t T, int64_t n_expert, bool native_routed = true) {
|
||||
const MmqPlan& mp = mmq_plan();
|
||||
Alloc a; a.count_only = true; bool ok = true;
|
||||
a.take<float>(T * n_expert, ok); a.take<float>(T * K, ok); a.take<int32_t>(T * K, ok); a.take<int32_t>(T * K, ok);
|
||||
a.take<int32_t>(T * K, ok);
|
||||
if (mp.fallback) a.take<uint16_t>(T * K * N, ok);
|
||||
a.take<float>(T * K * 1280, ok);
|
||||
if (mp.fallback) a.take<uint16_t>(T * K * 640, ok);
|
||||
if (native_routed && mp.fallback) a.take<uint16_t>(T * K * N, ok);
|
||||
if (native_routed) a.take<float>(T * K * 1280, ok);
|
||||
if (native_routed && mp.fallback) a.take<uint16_t>(T * K * 640, ok);
|
||||
a.take<float>(T * K * N, ok); a.take<float>(T * 640, ok);
|
||||
a.take<float>(T * 640, ok); a.take<uint16_t>(T * 640, ok); a.take<float>(T * N, ok); a.take<float>(T, ok);
|
||||
if (mp.any) {
|
||||
if (native_routed && mp.any) {
|
||||
a.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), N), ok);
|
||||
a.take<float>(T * K * 640, ok);
|
||||
a.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), 640), ok);
|
||||
@@ -518,6 +532,12 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
|
||||
core::ExpertSource* src, const core::ExpertCache* cache, const int32_t* host_res, int64_t chunk,
|
||||
void* stream, std::string& err, void* borrow, uint64_t borrow_bytes) {
|
||||
Impl& m = *impl_;
|
||||
if (experimental_all_routed_ && (!experimental_experts_ || !experimental_user_ || src || host_res ||
|
||||
(cache && cache->slots() > 0) || borrow || borrow_bytes)) {
|
||||
err = "prefill: all-routed mode requires its backend and no native source/cache/borrow"; return false;
|
||||
}
|
||||
m.experimental_all_routed = experimental_all_routed_;
|
||||
m.routed_backend = experimental_experts_; m.routed_user = experimental_user_;
|
||||
m.wt = &wt; m.g = &g; m.ss = &ss; m.src = src; m.cache = cache; m.host_res = host_res;
|
||||
m.T = chunk; m.cs = (cudaStream_t) stream; m.stats = &stats_;
|
||||
if (g.n_embd != N || g.hc != HC || g.hc_lr != LR || g.n_expert < 1 || ss.k != K) {
|
||||
@@ -548,11 +568,11 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
|
||||
m.borrowed = borrow != nullptr;
|
||||
bool ok = true;
|
||||
// one-time: events, the stager, the host buffers (for the largest chunk), the identity page table
|
||||
for (int i = 0; i < RING_MAX; ++i) {
|
||||
for (int i = 0; !m.experimental_all_routed && i < RING_MAX; ++i) {
|
||||
if (cudaEventCreateWithFlags(&m.copied[i], cudaEventDisableTiming) != cudaSuccess) ok = false;
|
||||
if (cudaEventCreateWithFlags(&m.used[i], cudaEventDisableTiming) != cudaSuccess) ok = false;
|
||||
}
|
||||
if (!m.stager) {
|
||||
if (!m.experimental_all_routed && !m.stager) {
|
||||
m.stager = std::make_unique<Stager>();
|
||||
const int hw = (int) std::thread::hardware_concurrency();
|
||||
const char* stv = std::getenv("STRATA_STAGER_THREADS"); // D-5: the host copy threads of unpinned blobs
|
||||
@@ -569,7 +589,8 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
|
||||
m.steps_host.resize(T * strata::kernels::kStepCount);
|
||||
m.ids_host.resize(T * K); m.slot_host.resize(T * K); m.src_host.resize(T * K); m.cnt.resize(m.g->n_expert); m.off.resize(m.g->n_expert + 1);
|
||||
{
|
||||
const size_t need = 3 * T * K + (size_t) (2 * (m.g->n_expert + m.g->n_expert / MMQ_GROUP + 2));
|
||||
const size_t need = 3 * T * K + (m.experimental_all_routed ? 0 :
|
||||
(size_t) (2 * (m.g->n_expert + m.g->n_expert / MMQ_GROUP + 2)));
|
||||
const char* gc = std::getenv("STRATA_GROUP_COPY");
|
||||
if (m.grp_n < need && !(gc && gc[0] == '1')) {
|
||||
if (m.grp_host) cudaFreeHost(m.grp_host);
|
||||
@@ -653,7 +674,7 @@ bool Prefill::carve(size_t T, void* alloc) {
|
||||
{
|
||||
// one region for the attention half's and the MoE half's scratch (see gdn_set_bytes)
|
||||
const uint64_t region = std::max({gdn_set_bytes(T), qsa_set_bytes(T, m.cap, m.max_blocks, m.sel_batch,
|
||||
m.attn_batch, s), moe_set_bytes(T, m.g->n_expert)});
|
||||
m.attn_batch, s), moe_set_bytes(T, m.g->n_expert, !m.experimental_all_routed)});
|
||||
uint8_t* base = o.take<uint8_t>((size_t) region, ok);
|
||||
m.region = base;
|
||||
m.region_bytes = region;
|
||||
@@ -675,21 +696,21 @@ bool Prefill::carve(size_t T, void* alloc) {
|
||||
m.logits = c.take<float>(T * m.g->n_expert, ok); m.w = c.take<float>(T * K, ok); m.ids = c.take<int32_t>(T * K, ok);
|
||||
m.slot_dev = c.take<int32_t>(T * K, ok); m.src_dev = c.take<int32_t>(T * K, ok);
|
||||
const MmqPlan& mp = mmq_plan();
|
||||
m.Xs = mp.fallback ? c.take<uint16_t>(T * K * N, ok) : nullptr;
|
||||
m.GU = c.take<float>(T * K * 1280, ok);
|
||||
m.Hh = mp.fallback ? c.take<uint16_t>(T * K * 640, ok) : nullptr;
|
||||
m.Xs = !m.experimental_all_routed && mp.fallback ? c.take<uint16_t>(T * K * N, ok) : nullptr;
|
||||
m.GU = !m.experimental_all_routed ? c.take<float>(T * K * 1280, ok) : nullptr;
|
||||
m.Hh = !m.experimental_all_routed && mp.fallback ? c.take<uint16_t>(T * K * 640, ok) : nullptr;
|
||||
m.Dm = c.take<float>(T * K * N, ok);
|
||||
m.sgate = c.take<float>(T * 640, ok); m.sup = c.take<float>(T * 640, ok); m.sh_h = c.take<uint16_t>(T * 640, ok);
|
||||
m.shared = c.take<float>(T * N, ok); m.sg = c.take<float>(T, ok);
|
||||
if (mp.any) {
|
||||
if (!m.experimental_all_routed && mp.any) {
|
||||
m.Xq = c.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), N), ok);
|
||||
m.H = c.take<float>(T * K * 640, ok);
|
||||
m.Hq = c.take<uint8_t>(mmq::q8_bytes((int64_t) (T * K), 640), ok);
|
||||
}
|
||||
if (base == nullptr) ok = false;
|
||||
}
|
||||
for (int i = 0; i < DQ; ++i) { m.dq_gu[i] = o.take<uint16_t>(1280 * 2560, ok); m.dq_d[i] = o.take<uint16_t>(2560 * 640, ok); }
|
||||
if (mmq_plan().any) {
|
||||
for (int i = 0; !m.experimental_all_routed && i < DQ; ++i) { m.dq_gu[i] = o.take<uint16_t>(1280 * 2560, ok); m.dq_d[i] = o.take<uint16_t>(2560 * 640, ok); }
|
||||
if (!m.experimental_all_routed && mmq_plan().any) {
|
||||
const MmqPlan& mp = mmq_plan();
|
||||
m.ids_identity = o.take<int32_t>(T * K, ok);
|
||||
m.bounds_dev = o.take<int32_t>((size_t) (2 * (m.g->n_expert + m.g->n_expert / MMQ_GROUP + 2)), ok);
|
||||
@@ -699,7 +720,7 @@ bool Prefill::carve(size_t T, void* alloc) {
|
||||
// lends them - a write now would corrupt a resident expert)
|
||||
if (!m.mmq_ctx) m.mmq_ctx = std::make_unique<mmq::Context>();
|
||||
}
|
||||
m.ring = ring_slots(T);
|
||||
m.ring = m.experimental_all_routed ? 0 : ring_slots(T);
|
||||
for (int i = 0; i < m.ring; ++i) {
|
||||
m.stage_dev[i] = o.take<uint8_t>((size_t) MAXBLOB(), ok);
|
||||
m.stage_live[i] = false; // a new buffer: nothing of an earlier layout to wait for
|
||||
@@ -888,7 +909,8 @@ void Prefill::set_pinned_share(double share) { g_pinned_share = share; }
|
||||
void Prefill::set_ring_override(int slots) { g_ring_override = slots > 0 ? slots : 0; }
|
||||
double Prefill::pinned_share() { return g_pinned_share; }
|
||||
|
||||
uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk) {
|
||||
uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
|
||||
bool experimental_all_routed) {
|
||||
// the same allocation sequence as `init`, counted
|
||||
const size_t T = (size_t) chunk;
|
||||
bool ok = true;
|
||||
@@ -908,16 +930,16 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Session
|
||||
const int64_t cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
|
||||
const int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
|
||||
o.take<uint8_t>((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s),
|
||||
moe_set_bytes(T, g.n_expert)}), ok);
|
||||
for (int i = 0; i < DQ; ++i) { o.take<uint16_t>(1280 * 2560, ok); o.take<uint16_t>(2560 * 640, ok); }
|
||||
if (mmq_plan().any) {
|
||||
moe_set_bytes(T, g.n_expert, !experimental_all_routed)}), ok);
|
||||
for (int i = 0; !experimental_all_routed && i < DQ; ++i) { o.take<uint16_t>(1280 * 2560, ok); o.take<uint16_t>(2560 * 640, ok); }
|
||||
if (!experimental_all_routed && mmq_plan().any) {
|
||||
const MmqPlan& mp = mmq_plan();
|
||||
o.take<int32_t>(T * K, ok);
|
||||
o.take<int32_t>((size_t) (2 * (g.n_expert + g.n_expert / MMQ_GROUP + 2)), ok);
|
||||
o.take<uint8_t>(MMQ_GROUP * mp.gu_max + MMQ_TAIL, ok);
|
||||
o.take<uint8_t>(MMQ_GROUP * mp.d_max + MMQ_TAIL, ok);
|
||||
}
|
||||
for (int i = 0; i < ring_slots(T); ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok);
|
||||
for (int i = 0; !experimental_all_routed && i < ring_slots(T); ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok);
|
||||
f(T * N);
|
||||
f((size_t) strata::kernels::NG_HC_DIM);
|
||||
strata::kernels::KvHostPools stage;
|
||||
@@ -1001,6 +1023,10 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
err = "prefill: missing experimental expert backend owner"; return false;
|
||||
}
|
||||
Impl& m = *impl_;
|
||||
if (m.experimental_all_routed != experimental_all_routed_ ||
|
||||
(m.experimental_all_routed && (m.routed_backend != experimental_experts_ || m.routed_user != experimental_user_))) {
|
||||
err = "prefill: all-routed allocation mode/backend changed after init"; return false;
|
||||
}
|
||||
const core::OnDevice on_device(m.device);
|
||||
const core::ModelGeometry& g = *m.g;
|
||||
core::SessionState& ss = *m.ss;
|
||||
@@ -2021,7 +2047,10 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
return false;
|
||||
}
|
||||
stats_.ms_total += ms_since(t_start);
|
||||
if (experimental_experts_)
|
||||
if (m.experimental_all_routed)
|
||||
std::fprintf(stderr, "strata prefill: experimental KAR routed rows=%llu; target native routed storage omitted; quality not qualified\n",
|
||||
(unsigned long long) (stats_.experimental_expert_rows - experimental_before));
|
||||
else if (experimental_experts_)
|
||||
std::fprintf(stderr, "strata prefill: experimental KAR routed rows=%llu; native cache retained; quality not qualified\n",
|
||||
(unsigned long long) (stats_.experimental_expert_rows - experimental_before));
|
||||
if (pt.on) {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "strata/core/conversation_memory.hpp"
|
||||
#include "strata/core/coupled_draft.hpp"
|
||||
#include "strata/core/expert_source.hpp"
|
||||
#include "strata/core/experimental_routed.hpp"
|
||||
#if defined(STRATA_NEXT_EXPERIMENTAL_KAR_EXPERTS)
|
||||
#include "kar_expert_miss.hpp"
|
||||
#endif
|
||||
@@ -1286,6 +1287,39 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Strict all-routed admission before device probing, contexts or native
|
||||
// resource setup. Native layout/geometry is confirmed after metadata load.
|
||||
const char* kar_expert_archive = std::getenv("STRATA_NEXT_KAR_EXPERTS_ARCHIVE");
|
||||
bool kar_all_routed = false, kar_prefill = false;
|
||||
std::string kar_option_error;
|
||||
if (!strata::core::parse_experimental_all_routed_flag(std::getenv("STRATA_NEXT_KAR_ALL_ROUTED"), kar_all_routed, kar_option_error)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", kar_option_error.c_str()); return 2;
|
||||
}
|
||||
if (kar_all_routed && !strata::prefill::parse_experimental_prefill_flag(std::getenv("STRATA_NEXT_KAR_PREFILL"), kar_prefill, kar_option_error)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", kar_option_error.c_str()); return 2;
|
||||
}
|
||||
if (kar_all_routed) {
|
||||
strata::core::ExperimentalRoutedProfile p;
|
||||
#if defined(STRATA_NEXT_EXPERIMENTAL_KAR_EXPERTS)
|
||||
p.built = true;
|
||||
#endif
|
||||
p.native_pack = !o.native_preset.empty(); p.prefill = kar_prefill; p.archive = kar_expert_archive;
|
||||
p.level = std::getenv("STRATA_NEXT_KAR_EXPERTS_LEVEL");
|
||||
p.chunk = o.prefill_chunk; p.automatic_chunk = o.prefill_auto; p.window = o.spec;
|
||||
p.no_pool = o.no_pool; p.no_capture = o.no_capture || o.graph_only || o.gpu_only_full || o.gpu_stages;
|
||||
p.multi_device = !o.layer_split.empty() || !o.split_device.empty() ||
|
||||
std::any_of(o.expert_cache_remote.begin(), o.expert_cache_remote.end(), [](int n) { return n != 0; });
|
||||
p.native_bank_option = o.mmap_experts || o.resident_cpu_experts || !o.shared_expert_arena.empty() ||
|
||||
o.pcie_frac != 0 || o.expert_cache_per_layer || o.expert_cache_cpu_order;
|
||||
p.native_cache = o.expert_cache; p.native_profile = !o.expert_profile.empty();
|
||||
p.adaptive_cache = o.adapt_every > 0 && o.adapt_swaps > 0;
|
||||
const char* device_plan = std::getenv("STRATA_VERIFY_DEVICE_PLAN");
|
||||
p.device_plan = device_plan && std::strcmp(device_plan, "0") != 0;
|
||||
if (!strata::core::validate_experimental_routed_profile(p, kar_option_error)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", kar_option_error.c_str()); return 2;
|
||||
}
|
||||
}
|
||||
|
||||
strata::core::set_coupled_draft(o.coupled_draft);
|
||||
if ((std::getenv("STRATA_KEYLESS_TRACE") || std::getenv("STRATA_KEYLESS_PREFILL_TRACE")) && (o.serve || o.max_new > 512)) {
|
||||
std::fprintf(stderr, "strata generate: STRATA_KEYLESS_TRACE is offline only and needs --max-new <= 512\n");
|
||||
@@ -1748,6 +1782,9 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
const bool native_pack = strata::kernels::cpu::expert_layout().native;
|
||||
if (kar_all_routed && !native_pack) {
|
||||
std::fprintf(stderr, "strata generate: KAR all-routed requires a native expert layout\n"); return 2;
|
||||
}
|
||||
// STRATA_EARLY_REMOTE_CONTEXTS=1: create EVERY secondary context here, like CUDA1's. Under WSL2 the driver's
|
||||
// pinned/mapped host budget (dxg gpadl, ~1 GiB) is spent by CUDA0's weights and MTP before the later loop runs,
|
||||
// and a new context then fails with cudaErrorMemoryAllocation (CUDA2: "cudaSetDevice(2) failed: out of memory").
|
||||
@@ -1894,8 +1931,6 @@ int main(int argc, char** argv) {
|
||||
rope_cfg.type == RST::YaRN ? "YaRN" : "--yarn-attn-factor", rope_cfg.mscale());
|
||||
}
|
||||
}
|
||||
const char* kar_expert_archive = std::getenv("STRATA_NEXT_KAR_EXPERTS_ARCHIVE");
|
||||
bool kar_prefill = false;
|
||||
if (!strata::prefill::parse_experimental_prefill_flag(std::getenv("STRATA_NEXT_KAR_PREFILL"), kar_prefill, err)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 2;
|
||||
}
|
||||
@@ -1927,7 +1962,10 @@ int main(int argc, char** argv) {
|
||||
// Native resident hits are the baseline. Route all otherwise CPU/PCIe
|
||||
// misses to the explicit experimental arm; use pcie-frac=0 in its baseline.
|
||||
o.pcie_frac = 0;
|
||||
if (kar_prefill)
|
||||
if (kar_all_routed)
|
||||
std::fprintf(stderr, "strata generate: experimental KAR all-routed ON: target native routed RAM/VRAM bank omitted; "
|
||||
"MTP/shared/dense/KV retained; quality not qualified\n");
|
||||
else if (kar_prefill)
|
||||
std::fprintf(stderr, "strata generate: experimental KAR prefill ON: all routed experts; native cache retained; quality not qualified\n");
|
||||
}
|
||||
#else
|
||||
@@ -2362,7 +2400,7 @@ int main(int argc, char** argv) {
|
||||
// An explicit number truncates the ranked list ("what would 2,000 slots give" without rebuilding the
|
||||
// file). `--expert-cache 0` used to take the count the profile was built for; the profile now ranks
|
||||
// every pair (issue #46: a card that holds more than the old 8,000 used to stop there), so it means auto.
|
||||
if (o.expert_cache == 0) o.expert_cache = -1;
|
||||
if (o.expert_cache == 0 && !kar_all_routed) o.expert_cache = -1;
|
||||
std::fprintf(stderr, "strata generate: profile %s: %zu ranked pairs, built for %lld slots\n",
|
||||
o.expert_profile.c_str(), profile.size(), (long long) pslots);
|
||||
}
|
||||
@@ -2684,7 +2722,11 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
strata::core::ArenaExpertSource arena_src;
|
||||
strata::core::ExpertSource* srcp = nullptr;
|
||||
if (o.mmap_experts) {
|
||||
if (kar_all_routed) {
|
||||
// No target routed IQ bank or mmap fallback. Null is intentional: the
|
||||
// strict callbacks must bypass every native source consumer.
|
||||
std::fprintf(stderr, "strata generate: target native routed expert source omitted (KAR all-routed)\n");
|
||||
} else if (o.mmap_experts) {
|
||||
// FileExpertSource maps the pack's experts.bin: a canonical pack has it; a native (IQ) pack has it when
|
||||
// built with `tools/iq_pack.py --experts-bin` (the per-layer blob sizes of its layout, PR #121). The low-RAM
|
||||
// mode: the experts come from the file through the OS cache instead of a pinned copy in RAM, for a PC whose
|
||||
@@ -3135,6 +3177,8 @@ int main(int argc, char** argv) {
|
||||
if (kar_expert_archive != nullptr) {
|
||||
drive.d.experimental_miss = &strata::kar::ExpertMiss::dispatch;
|
||||
drive.d.experimental_miss_user = &kar_expert_miss;
|
||||
drive.d.experimental_all_routed = kar_all_routed;
|
||||
drive.d.experimental_layers = g.n_layers;
|
||||
}
|
||||
#endif
|
||||
for (int r = 0; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0)
|
||||
@@ -3835,7 +3879,7 @@ int main(int argc, char** argv) {
|
||||
strata::prefill::Prefill::set_pinned_share(total ? (double) pinned / (double) total : 1.0);
|
||||
}
|
||||
auto lend_slots = [&](int64_t c) -> int64_t {
|
||||
const uint64_t need = strata::prefill::Prefill::bytes_needed(g, ss, c);
|
||||
const uint64_t need = strata::prefill::Prefill::bytes_needed(g, ss, c, kar_all_routed);
|
||||
const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob;
|
||||
int64_t k = (int64_t) ((need + (uint64_t) blob - 1) / (uint64_t) blob);
|
||||
if (xcache.slot_offsets() != nullptr) { // sized slots: take slots from the end until they hold `need`
|
||||
@@ -3959,7 +4003,11 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
strata::prefill::Prefill sp;
|
||||
#if defined(STRATA_NEXT_EXPERIMENTAL_KAR_EXPERTS)
|
||||
if (kar_prefill) sp.set_experimental_experts(&strata::kar::ExpertMiss::dispatch, &kar_expert_miss);
|
||||
if (kar_all_routed) {
|
||||
if (!sp.set_experimental_all_routed(&strata::kar::ExpertMiss::dispatch, &kar_expert_miss, err)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 2;
|
||||
}
|
||||
} else if (kar_prefill) sp.set_experimental_experts(&strata::kar::ExpertMiss::dispatch, &kar_expert_miss);
|
||||
#endif
|
||||
void* borrow = nullptr;
|
||||
uint64_t borrow_bytes = 0;
|
||||
@@ -3986,7 +4034,7 @@ int main(int argc, char** argv) {
|
||||
std::vector<std::pair<int32_t, int32_t>> lent;
|
||||
};
|
||||
auto part_slots = [&](const PfPart& p, int64_t c) -> int64_t {
|
||||
const uint64_t need = strata::prefill::Prefill::bytes_needed(g, *p.ses, c);
|
||||
const uint64_t need = strata::prefill::Prefill::bytes_needed(g, *p.ses, c, kar_all_routed);
|
||||
strata::core::ExpertCache& xc = *p.cache;
|
||||
if (xc.slot_offsets() != nullptr) { // sized slots: from the end until they hold `need`
|
||||
int64_t k = 0;
|
||||
@@ -4114,7 +4162,7 @@ int main(int argc, char** argv) {
|
||||
const strata::core::OnDevice on(p.dev);
|
||||
size_t fb = 0, tb = 0;
|
||||
if (cudaMemGetInfo(&fb, &tb) != cudaSuccess) { (void) cudaGetLastError(); continue; }
|
||||
const uint64_t need = strata::prefill::Prefill::bytes_needed(g, *p.ses, chunk);
|
||||
const uint64_t need = strata::prefill::Prefill::bytes_needed(g, *p.ses, chunk, kar_all_routed);
|
||||
if ((uint64_t) fb >= need + (3ull << 29)) {
|
||||
std::fprintf(stderr, "strata serve: CUDA%d keeps its own prompt buffers (%.2f GiB of "
|
||||
"%.2f GiB free): no loan\n", p.dev < 0 ? 0 : p.dev,
|
||||
@@ -4183,7 +4231,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
if (multi_gpu) sp.set_stage(0, split_at[0], &stages[0]->sp);
|
||||
if (!sp.init(wt, g, ss, srcp, &xcache, host_res.data(), o.prefill_chunk, main_cs, err, borrow, borrow_bytes))
|
||||
if (!sp.init(wt, g, ss, srcp, &xcache, host_res.empty() ? nullptr : host_res.data(), o.prefill_chunk, main_cs, err, borrow, borrow_bytes))
|
||||
return err.find("do not fit") != std::string::npos ? 2 : 1;
|
||||
return 0;
|
||||
};
|
||||
@@ -4201,7 +4249,7 @@ int main(int argc, char** argv) {
|
||||
const strata::core::OnDevice on(dev);
|
||||
size_t fb = 0, tb = 0;
|
||||
cudaMemGetInfo(&fb, &tb);
|
||||
const int64_t need = (int64_t) strata::prefill::Prefill::bytes_needed(g, i == 0 ? ss : stages[i - 1]->ss, c);
|
||||
const int64_t need = (int64_t) strata::prefill::Prefill::bytes_needed(g, i == 0 ? ss : stages[i - 1]->ss, c, kar_all_routed);
|
||||
if (need + kHeadroom > (int64_t) fb) {
|
||||
dev_out = dev < 0 ? 0 : dev; need_out = need; free_out = (int64_t) fb;
|
||||
return false;
|
||||
@@ -4317,6 +4365,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
strata::core::Verifier ver;
|
||||
strata::core::VerifyHits vh;
|
||||
vh.experimental_all_routed = kar_all_routed;
|
||||
vh.d_res = thits.d_res;
|
||||
vh.cache_base = thits.cache_base;
|
||||
vh.blob = thits.blob;
|
||||
@@ -5843,7 +5892,11 @@ int main(int argc, char** argv) {
|
||||
int64_t spec_pos = 0; // plan v0.3 P6: where the speculative loop starts (0 = not used)
|
||||
strata::prefill::Prefill prefill;
|
||||
#if defined(STRATA_NEXT_EXPERIMENTAL_KAR_EXPERTS)
|
||||
if (kar_prefill) prefill.set_experimental_experts(&strata::kar::ExpertMiss::dispatch, &kar_expert_miss);
|
||||
if (kar_all_routed) {
|
||||
if (!prefill.set_experimental_all_routed(&strata::kar::ExpertMiss::dispatch, &kar_expert_miss, err)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 2;
|
||||
}
|
||||
} else if (kar_prefill) prefill.set_experimental_experts(&strata::kar::ExpertMiss::dispatch, &kar_expert_miss);
|
||||
#endif
|
||||
double prefill_batched_ms = 0;
|
||||
std::FILE* final_r = o.dump_final_r.empty() ? nullptr : std::fopen(o.dump_final_r.c_str(), "wb");
|
||||
@@ -6169,7 +6222,7 @@ int main(int argc, char** argv) {
|
||||
// round emits (accepted drafts + 1) tokens. `commit` keeps the state of the tokens that were emitted.
|
||||
const bool ended = o.stop_eos && !produced.empty() &&
|
||||
std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) produced.back()) != o.eos_ids.end();
|
||||
if (spec_pos > 0 && (int64_t) produced.size() < o.max_new && !ended) {
|
||||
if ((spec_pos > 0 || kar_all_routed) && (int64_t) produced.size() < o.max_new && !ended) {
|
||||
std::vector<int64_t> oracle;
|
||||
if (!o.spec_oracle.empty()) {
|
||||
std::ifstream in(o.spec_oracle);
|
||||
@@ -6180,7 +6233,7 @@ int main(int argc, char** argv) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
if (thits.d_res == nullptr) {
|
||||
if (thits.d_res == nullptr && !kar_all_routed) {
|
||||
std::fprintf(stderr, "strata generate: --spec needs the device residency table (--expert-profile, "
|
||||
"--expert-cache and the token graph)\n");
|
||||
return 2;
|
||||
@@ -6188,6 +6241,7 @@ int main(int argc, char** argv) {
|
||||
mem_mark("the head and the prompt path");
|
||||
strata::core::Verifier ver;
|
||||
strata::core::VerifyHits vh;
|
||||
vh.experimental_all_routed = kar_all_routed;
|
||||
vh.d_res = thits.d_res;
|
||||
vh.cache_base = thits.cache_base;
|
||||
vh.blob = thits.blob;
|
||||
@@ -6619,7 +6673,7 @@ int main(int argc, char** argv) {
|
||||
std::printf("%-24s wait-park %.3f drain %.3f re-park %.3f ms/token\n",
|
||||
" pool phases", wp / per, dr / per, rp / per);
|
||||
}
|
||||
std::printf("%-24s %lld blobs read\n", " expert blobs", (long long) srcp->reads());
|
||||
std::printf("%-24s %lld blobs read\n", " expert blobs", (long long) (srcp ? srcp->reads() : 0));
|
||||
for (int r = 0; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0)
|
||||
std::printf(" CUDA%d experts %lld routed entries computed\n",
|
||||
r + 1, (long long) remote_experts[(size_t) r].computed());
|
||||
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
build_dir="$(mktemp -d "${TMPDIR:-/tmp}/kar-all-routed-contract.XXXXXX")"
|
||||
trap 'rm -rf "$build_dir"' EXIT
|
||||
"${CXX:-c++}" -std=c++17 -Wall -Wextra -Werror -pedantic \
|
||||
-fsanitize=address,undefined -fno-omit-frame-pointer \
|
||||
-I"$repo_root/engine/strata/include" \
|
||||
"$repo_root/tests/kar-all-routed-contract.cpp" -o "$build_dir/kar-all-routed-contract"
|
||||
if [[ "$(uname -s)" == Darwin ]]; then
|
||||
ASAN_OPTIONS=detect_leaks=0 UBSAN_OPTIONS=halt_on_error=1 "$build_dir/kar-all-routed-contract"
|
||||
else
|
||||
ASAN_OPTIONS=detect_leaks=1 UBSAN_OPTIONS=halt_on_error=1 "$build_dir/kar-all-routed-contract"
|
||||
fi
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "strata/core/experimental_routed.hpp"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace strata::core;
|
||||
struct Backend { int calls = 0, mode = 0; };
|
||||
struct Dispatch {
|
||||
bool failed = false;
|
||||
const char* fail = nullptr;
|
||||
int64_t layers = 2, experimental_layers = 4, n_expert = 16;
|
||||
int64_t fail_layer = -1, fail_expert = -1, cache_refused = 5, experts = 10;
|
||||
int64_t cache_hits = 7, multi_entries = 0, multi_misses = 0;
|
||||
// Null native handles are intentional, matching the strict runtime arm.
|
||||
void *src = nullptr, *pool = nullptr, *plan = nullptr;
|
||||
ExperimentalRoutedBackend experimental_miss = nullptr;
|
||||
void* experimental_miss_user = nullptr;
|
||||
std::vector<float> usage = std::vector<float>(4 * 16, 0);
|
||||
};
|
||||
// Synthetic two-coordinate experts: [2*x0+e, 3*x1-e]. The oracle below
|
||||
// independently combines these linear expert matrices with non-unit weights.
|
||||
bool backend(void* user, int64_t layer, const float* x, const int32_t* ids,
|
||||
int64_t tokens, int64_t routes, int32_t* kind, float* y, std::string& error) {
|
||||
auto& b = *static_cast<Backend*>(user);
|
||||
++b.calls;
|
||||
assert(layer == 2);
|
||||
if (b.mode == 1) { error = "synthetic stage/run/finish fault"; return false; }
|
||||
if (b.mode == 2) return false; // empty errors still become actionable
|
||||
for (int64_t t = 0; t < tokens; ++t)
|
||||
for (int64_t k = 0; k < routes; ++k) {
|
||||
const auto i = t * routes + k;
|
||||
assert(kind[i] == -1); // Native cache hits must never be pre-owned.
|
||||
kind[i] = 3;
|
||||
y[2 * i] = 2 * x[2 * t] + ids[i];
|
||||
y[2 * i + 1] = 3 * x[2 * t + 1] - ids[i];
|
||||
}
|
||||
if (b.mode == 3) kind[tokens * routes - 1] = 0; // Native hit is forbidden.
|
||||
if (b.mode == 4) kind[0] = -1;
|
||||
if (b.mode == 5) y[0] = std::numeric_limits<float>::infinity();
|
||||
if (b.mode == 6) y[1] = std::numeric_limits<float>::quiet_NaN();
|
||||
return true;
|
||||
}
|
||||
int main() {
|
||||
bool enabled = true;
|
||||
std::string error;
|
||||
assert(parse_experimental_all_routed_flag(nullptr, enabled, error) && !enabled);
|
||||
assert(parse_experimental_all_routed_flag("0", enabled, error) && !enabled);
|
||||
assert(parse_experimental_all_routed_flag("1", enabled, error) && enabled);
|
||||
for (const auto* bad : {"", "01", "true", "2", " 1", "1 "})
|
||||
assert(!parse_experimental_all_routed_flag(bad, enabled, error));
|
||||
ExperimentalRoutedProfile valid;
|
||||
valid.built = valid.native_pack = valid.prefill = true;
|
||||
valid.archive = "synthetic-unused.kar"; valid.level = "2"; valid.chunk = 32; valid.window = 4;
|
||||
assert(validate_experimental_routed_profile(valid, error));
|
||||
auto reject = [&](auto edit) {
|
||||
auto p = valid; edit(p);
|
||||
assert(!validate_experimental_routed_profile(p, error) && !error.empty());
|
||||
};
|
||||
for (auto member : {&ExperimentalRoutedProfile::built, &ExperimentalRoutedProfile::native_pack,
|
||||
&ExperimentalRoutedProfile::prefill}) reject([&](auto& p) { p.*member = false; });
|
||||
for (auto member : {&ExperimentalRoutedProfile::no_pool, &ExperimentalRoutedProfile::no_capture,
|
||||
&ExperimentalRoutedProfile::multi_device, &ExperimentalRoutedProfile::native_bank_option,
|
||||
&ExperimentalRoutedProfile::adaptive_cache, &ExperimentalRoutedProfile::device_plan,
|
||||
&ExperimentalRoutedProfile::automatic_chunk, &ExperimentalRoutedProfile::native_profile})
|
||||
reject([&](auto& p) { p.*member = true; });
|
||||
reject([](auto& p) { p.archive = nullptr; });
|
||||
reject([](auto& p) { p.archive = ""; });
|
||||
reject([](auto& p) { p.level = nullptr; });
|
||||
for (const char* level : {"", "0", "9", "02", " 2", "2 "})
|
||||
reject([&](auto& p) { p.level = level; });
|
||||
reject([](auto& p) { p.chunk = 0; });
|
||||
for (int window : {0, 1, 9}) reject([&](auto& p) { p.window = window; });
|
||||
for (int cache : {-1, 1, 8000}) reject([&](auto& p) { p.native_cache = cache; });
|
||||
|
||||
constexpr int tokens = 8, routes = 16, hidden = 2;
|
||||
std::array<float, tokens * hidden> input{};
|
||||
std::array<int32_t, tokens * routes> ids{};
|
||||
std::array<float, tokens * routes * hidden> output{};
|
||||
for (int t = 0; t < tokens; ++t) {
|
||||
input[2 * t] = t + 0.5f; input[2 * t + 1] = t - 0.25f;
|
||||
for (int k = 0; k < routes; ++k) ids[t * routes + k] = (k + t) % 16;
|
||||
}
|
||||
Backend b;
|
||||
int64_t bad_expert = -1;
|
||||
auto run = [&](int64_t n = tokens, int64_t k = routes, int64_t layer = 2,
|
||||
ExperimentalRoutedBackend fn = backend, void* owner = nullptr) {
|
||||
return dispatch_experimental_all_routed(layer, 4, hidden, 16, input.data(), ids.data(), n, k,
|
||||
fn, owner ? owner : &b, output.data(), bad_expert, error);
|
||||
};
|
||||
assert(run() && b.calls == 1);
|
||||
// Verify routing order and exactly one router-weight multiplication. No
|
||||
// baseline cache/source/pool exists in this test: all 128 rows are owned.
|
||||
for (int t = 0; t < tokens; ++t) {
|
||||
float actual0 = 0, actual1 = 0, weight_sum = 0, weighted_expert = 0;
|
||||
for (int k = 0; k < routes; ++k) {
|
||||
const float weight = (k + 1) / 256.0f;
|
||||
const auto i = t * routes + k;
|
||||
actual0 += weight * output[2 * i]; actual1 += weight * output[2 * i + 1];
|
||||
weight_sum += weight; weighted_expert += weight * ids[i];
|
||||
}
|
||||
const float oracle0 = 2 * input[2 * t] * weight_sum + weighted_expert;
|
||||
const float oracle1 = 3 * input[2 * t + 1] * weight_sum - weighted_expert;
|
||||
assert(std::abs(actual0 - oracle0) <= 1e-5f && std::abs(actual1 - oracle1) <= 1e-5f);
|
||||
}
|
||||
for (int mode = 1; mode <= 6; ++mode) {
|
||||
b = Backend{}; b.mode = mode;
|
||||
assert(!run() && b.calls == 1 && !error.empty());
|
||||
}
|
||||
// Invalid admission must leave outputs untouched and never enter backend.
|
||||
b = Backend{}; output.fill(19);
|
||||
auto untouched = [&] { assert(b.calls == 0); for (float x : output) assert(x == 19); };
|
||||
for (int n : {0, 9}) { assert(!run(n)); untouched(); }
|
||||
for (int k : {0, 17, 1024}) { assert(!run(tokens, k)); untouched(); }
|
||||
for (int layer : {-1, 4}) { assert(!run(tokens, routes, layer)); untouched(); }
|
||||
ids.back() = 16;
|
||||
assert(!run() && bad_expert == 16); untouched();
|
||||
ids.back() = -1;
|
||||
assert(!run() && bad_expert == -1); untouched();
|
||||
ids.back() = 6;
|
||||
ids[1] = ids[0];
|
||||
assert(!run() && bad_expert == ids[0]); untouched();
|
||||
ids[1] = 1;
|
||||
input[0] = std::numeric_limits<float>::quiet_NaN();
|
||||
assert(!run()); untouched(); input[0] = 0.5f;
|
||||
assert(!run(tokens, routes, 2, nullptr)); untouched();
|
||||
assert(!dispatch_experimental_all_routed(2, 4, hidden, 16, input.data(), ids.data(), tokens, routes,
|
||||
backend, nullptr, output.data(), bad_expert, error)); untouched();
|
||||
Dispatch d;
|
||||
d.experimental_miss = backend; d.experimental_miss_user = &b;
|
||||
assert(dispatch_experimental_routed_window(d, hidden, input.data(), ids.data(), tokens, routes, output.data()));
|
||||
assert(d.layers == 3 && d.experts == 138 && d.cache_refused == 133 && d.cache_hits == 7);
|
||||
assert(d.multi_entries == 0 && d.multi_misses == 0 && !d.failed);
|
||||
for (int i = 0; i < 64; ++i) assert(d.usage[i] == (i / 16 == 2 ? 8 : 0));
|
||||
b = Backend{}; b.mode = 1;
|
||||
d = Dispatch{}; d.experimental_miss = backend; d.experimental_miss_user = &b;
|
||||
assert(!dispatch_experimental_routed_window(d, hidden, input.data(), ids.data(), tokens, routes, output.data()));
|
||||
assert(d.failed && d.fail_layer == 2 && d.layers == 2 && d.experts == 10 && d.cache_refused == 5);
|
||||
assert(std::string(d.fail) == "synthetic stage/run/finish fault");
|
||||
assert(!dispatch_experimental_routed_window(d, hidden, input.data(), ids.data(), tokens, routes, output.data()));
|
||||
assert(b.calls == 1); // a previous failure stays failed
|
||||
b = Backend{};
|
||||
d = Dispatch{}; d.experimental_miss = backend; d.experimental_miss_user = &b;
|
||||
d.usage.resize(16);
|
||||
assert(!dispatch_experimental_routed_window(d, hidden, input.data(), ids.data(), tokens, routes, output.data()));
|
||||
assert(b.calls == 0 && d.failed && d.fail_layer == 2 && d.layers == 2);
|
||||
std::cout << "PASS: strict all-routed admission, 128-row weighted CPU oracle (absolute tolerance 1e-5), "
|
||||
"ownership, counters/usage, null native handles, bounds, duplicate/nonfinite input and backend fault rejection\n";
|
||||
}
|
||||
Reference in New Issue
Block a user