Inkling CUDA: the elastic expert pool for the COMPACT path (16 GB RTX 50, every expert on the card as SF4)

The all-resident COMPACT load put d_sec4 in one cudaMalloc and the global K/V in the slab; what the card could not hold of the K/V went to mapped host RAM
(5060 Ti 16 GB, SQ-20 v2: 8448 tokens on the card, the rest over PCIe: decode 18 tok/s at 32K, 6 at 100K). Now, for one slot, d_sec4 is the elastic pool
(strata::core::VmmRange through InkElasticSec4Pool, packed hottest first, MX8 experts held transcoded to SF4 and their host copy is the SF4 form), the global K/V
has its own virtual memory beside the slab (the slab keeps the SWA rings and ShortConv state) and grows by taking 2 MiB chunks from the coldest experts, which
the existing arming / eviction / regrow / guard code serves from the host copy through the exact lane (same bits as on the card).

- load(): compact_elastic_layout() lays the experts out in vram_load_order and fills the pool; the loop writes each expert at its pool offset.
- init(): a COMPACT branch of the K/V sizing (start from what is left after the 200 MiB the first long prompt's macro-prefill scratch needs, grow through
  kv_vmm_grow; the slab pass skips the global K/V), gated on one slot, the pool, vmm, STRATA_INK_ELASTIC_COMPACT (0 = off) and
  STRATA_INK_ELASTIC_COMPACT_MAX_CTX (default 262144; above it the old host K/V).
- the macro prefill (single sweep of a long prompt) runs a host pass for the experts that left: each layer's evicted experts are streamed through the staging pair
  in chunks and run with the same tensor-core kernels (STRATA_INK_ELASTIC_MACRO_HOST=0 turns it off); the layer-major prefill, which streams whole layers, is not used with the pool.
- the VRAM guard watermarks for COMPACT are 96 / 144 / 320 MiB (the card starts with ~175 MiB free by design; 192 / 352 / 512 shed 350 MiB at the first poll).
- the drafter rule (principle 2) counts the pool as hybrid only from STRATA_INK_ELASTIC_DRAFT_HOST_PCT (default 10) percent of the experts on the host.

Measured, RTX 5060 Ti 16 GB (Vast, driver 595), SQ-20 v2 + DFlash 2, FP8 K/V, base = the tip before this commit (details in notes-ink-evmm-compact.md):
prose / code / 4K document unchanged (90 / 200 / 117 tok/s, drafts accepted 144/386 and 218/294 as on the base); 16K document prefill 1.6K -> 3.5K tok/s, decode 25 -> 80;
32K 680 -> 3050 tok/s, 18 -> 62; 100K (drafter off) decode 5.8 -> 36, prefill 146 -> 1457; 200K prompt at max-context 262144: prefill 947 tok/s, decode 22.7 (the base: more than 25 minutes of prefill).
Identity: with the deterministic prefill (STRATA_INK_SM120_PREFILL=0) tokens are identical to the base (chat, 4K, 16K, 2 of 2 rounds each); with the default macro prefill even the base differs
from itself between starts, there the teacher-forced KL against a base dump is inside the base-against-base noise (0.0201 / 0.0144 against 0.0209 / 0.0154). Several slots and
COMPACT_HYBRID / refusal starts are unchanged (16 slots: 193.4 against 193.7 tok/s; ballast cases identical).
This commit is contained in:
Niko1221 committed 2026-12-03 01:30:05 +01:00
1 parent 0fb8ce7ae5
commit 2ba90fd98e
2 files changed
+200 -14

No files matched your search

+14 -2
View File
@@ -711,6 +711,7 @@ public:
strata::ink::sm120_plan_slots_ref().store(max_slots_);
strata::ink::sm120_plan_swa_host_bytes_ref().store(plan.swa_host_bytes);
model_.compact_elastic_ctx = std::max(256, opts.max_ctx); // the COMPACT elastic pool is limited to the contexts it is proven for (STRATA_INK_ELASTIC_COMPACT_MAX_CTX, default 262144)
// Load .strata v1.1 / .gguf (Unsloth + ggml-org) / HF safetensors trunk + experts (Single-GPU, EP2, Layer-Split, or 4-tier GPU+RAM+SSD!)
// A card with too little free memory for even the shared layers (the trunk) is a refusal with a message, not the fatal GPU error that ends the engine (PLAN-0225 item 24).
bool loaded_ok = false;
@@ -1943,8 +1944,19 @@ public:
return best_nd;
}
// Principle 2's "hybrid" (experts in host memory). With the COMPACT elastic pool a few dozen experts are on the host only because the K/V took their chunks: a draft step reads a cold expert
// rarely, the measured cost of principle 2 (a card with a third of the experts on the host) does not apply. The pool counts as hybrid only from STRATA_INK_ELASTIC_DRAFT_HOST_PCT percent of
// the 3072 experts on the host (default 10, the share at which kDraftTierCutLimit calls a drafter's cost "noticeable"; 0 = any host expert, the old rule). Measured on a 5060 Ti 16 GB, SQ-20 v2, DFlash 2:
// 8K prompt 74.7 -> 103.5 tok/s, 16K 23 -> 71 with 2-3% of the experts on the host, 4K level with the base; at 130K (15% on the host) the window governor drafts nothing either way.
bool draft_hybrid_now() const {
const size_t nh = model_.n_host_experts;
if (nh == 0) return false;
if (!model_.elastic_compact) return true;
static const double pct = [] { const char* e = std::getenv("STRATA_INK_ELASTIC_DRAFT_HOST_PCT"); return e ? std::atof(e) : 10.0; }();
return (double)nh * 100.0 > pct * 3072.0;
}
bool draft_plain_now(int ctx_pos) const override {
return strata::core::ink_draft_plain_step(model_.n_host_experts > 0, ctx_pos, draft_forced_, dev_cc_);
return strata::core::ink_draft_plain_step(draft_hybrid_now(), ctx_pos, draft_forced_, dev_cc_);
}
int dspark_spec_step_greedy(
@@ -1958,7 +1970,7 @@ public:
if (!initialized_ || model_.ls_active || !dspark_ || !dspark_->ready || slot != 0 || dspark_slot_ != 0) return 0;
// the ONE drafter rule, principle 2 (strata/core/ink_draft_rule.hpp): on a hybrid card (experts in host memory) a step from kDraftPlainCtxHybrid tokens of context on is a plain step.
// Per step, so it follows every request; `--draft on` turns it off; with all experts in VRAM the window governor decides alone.
if (strata::core::ink_draft_plain_step(model_.n_host_experts > 0, anchor_pos, draft_forced_, dev_cc_)) return 0;
if (strata::core::ink_draft_plain_step(draft_hybrid_now(), anchor_pos, draft_forced_, dev_cc_)) return 0;
const bool dspark_opt = strata::ink::rtx5090::dspark_opt_on() && (runner_->d_tree_anc32 != nullptr);
const bool deep_tree = (runner_->d_tree_anc32 != nullptr); // deep verify tree allocated (5090 builder or the 5060 Ti PLD graft)
const int want_cap = (deep_tree && anchor_pos + 24 < eff_max_ctx_ && anchor_pos + 24 < dspark_->max_ctx)
+186 -12
View File
@@ -874,7 +874,17 @@ struct InkCudaModel {
if (do_compact_vram && all_sf4_or_mx8 && vfree > compact_sec4_bytes + (768ull << 20)) {
// 100.0% ALL-3,072-EXPERT GDDR7 RESIDENCY on 16 GB RTX 5060 Ti (0 host misses, GPU MX8->SF4 transcode < 20 ms)!
sec4_bytes = compact_sec4_bytes;
INK_CK(cudaMalloc(&d_sec4, sec4_bytes));
// one slot: the experts live in the elastic pool (packed hottest first) so the global K/V can take chunks from the coldest ones instead of going to host RAM;
// ce_off[directory index] = the expert's offset in d_sec4. Empty (pool off, several slots, no virtual memory): one cudaMalloc packed in directory order, as before.
std::vector<uint64_t> ce_off;
size_t ce_pack = 0;
if (sm120_plan_slots_ref().load() == 1 && compact_elastic_layout(dir, rd, opt5060_ld.compact_xcode, sf4_body_c, sf4_dn_off_c, ce_pack, ce_off)) {
d_sec4 = elastic_sec4.base();
sec4_bytes = ce_pack;
} else {
ce_off.clear();
INK_CK(cudaMalloc(&d_sec4, sec4_bytes));
}
uint8_t* d_mx8_tmp = nullptr;
uint32_t* d_amax_u32 = nullptr;
INK_CK(cudaMalloc(&d_mx8_tmp, 16ull << 20));
@@ -889,13 +899,14 @@ struct InkCudaModel {
const auto& e = dir[di];
const int key = il * man.c.n_experts + e.expert_id;
auto& dev = h_exp[key];
const size_t at = ce_off.empty() ? v_cur : (size_t)ce_off[(size_t)di]; // where this expert goes in d_sec4
if (!rd.read_at(hdr.sections[4].file_offset + e.body_offset, h_stg, e.body_bytes, err)) {
cudaFreeHost(h_stg); cudaFree(d_mx8_tmp); cudaFree(d_amax_u32);
return false;
}
if (opt5060_ld.compact_xcode && e.gu_code_id == FMT_MX8 && e.dn_code_id == FMT_MX8) {
INK_CK(cudaMemcpy(d_mx8_tmp, h_stg, e.body_bytes, cudaMemcpyHostToDevice));
uint8_t* dst = d_sec4 + v_cur;
uint8_t* dst = d_sec4 + at;
float* dst_hdr = reinterpret_cast<float*>(dst);
// Gate+Up: [2304, 2304]
const uint8_t* gu_sp = d_mx8_tmp;
@@ -913,19 +924,19 @@ struct InkCudaModel {
INK_CK(cudaMemset(d_amax_u32, 0, 4));
k_ink_mx8_amax<<<256, 256>>>(dn_sp, dn_ss, kInkHidden, kInkInter, d_amax_u32);
k_ink_mx8_to_sf4<<<kInkHidden, 256>>>(dn_sp, dn_ss, d_amax_u32, dn_dp, dn_ds, dst_hdr + 2, kInkHidden, kInkInter);
dev.body_off = v_cur;
dev.body_off = at;
dev.dn_off = (uint32_t)sf4_dn_off_c;
dev.gu_code = FMT_SF4;
dev.dn_code = FMT_SF4;
dev.tier = 0;
v_cur += sf4_body_c;
if (ce_off.empty()) v_cur += sf4_body_c;
++n_transcoded;
any_tr_layer = true;
} else {
INK_CK(cudaMemcpy(d_sec4 + v_cur, h_stg, e.body_bytes, cudaMemcpyHostToDevice));
dev.body_off = v_cur;
INK_CK(cudaMemcpy(d_sec4 + at, h_stg, e.body_bytes, cudaMemcpyHostToDevice));
dev.body_off = at;
dev.tier = 0;
v_cur += e.body_bytes;
if (ce_off.empty()) v_cur += e.body_bytes;
}
}
if (any_tr_layer) {
@@ -2577,6 +2588,86 @@ struct InkCudaModel {
return true;
}
// ---- Elastic expert pool, COMPACT (all-SF4 on the card, 16 GB RTX 50) ----
// Lays the 3072 experts out in descending heat order (rd.vram_load_order(): the 3x-looped core first) and makes the pool (one VmmRange over the whole expert section).
// off_of_di[directory index] = the expert's offset in d_sec4; pack_bytes = the section's size. MX8 experts are held transcoded to SF4 on the card, so the pool slot and the facts of
// the host copy (elastic_src) are the SF4 form: an expert that leaves the card is copied device-to-host as it is. false: the pool is off or could not be made (nothing is
// left allocated); the caller keeps its one cudaMalloc packed in directory order. STRATA_INK_ELASTIC_COMPACT=0 turns this path off alone.
template <class Dir, class Reader>
bool compact_elastic_layout(const Dir& dir, Reader& rd, bool xcode, size_t sf4_body, size_t sf4_dn_off, size_t& pack_bytes, std::vector<uint64_t>& off_of_di) {
const char* ec = std::getenv("STRATA_INK_ELASTIC_COMPACT");
const char* kv_vmm_env = std::getenv("STRATA_INK_KV_VMM");
if (!InkElasticSec4Pool::env_enabled() || (ec && ec[0] == '0') || (kv_vmm_env && kv_vmm_env[0] == '0') || !strata::core::vmm_available()) return false;
// measured clean up to 262144 (200K prompt: 947 prefill tok/s, 22.7 decode); above STRATA_INK_ELASTIC_COMPACT_MAX_CTX (default 262144) COMPACT keeps its host K/V as before
const char* lim = std::getenv("STRATA_INK_ELASTIC_COMPACT_MAX_CTX");
const int ctx_limit = lim ? std::atoi(lim) : 262144;
if (compact_elastic_ctx > ctx_limit) {
std::fprintf(stderr, "[InkCUDA] COMPACT elastic expert pool off: max-context %d is above %d (STRATA_INK_ELASTIC_COMPACT_MAX_CTX); the K/V beyond the card stays in host memory as before\n", compact_elastic_ctx, ctx_limit);
return false;
}
const int n_total = man.c.n_phys * man.c.n_experts;
const auto& vorder = rd.vram_load_order();
std::vector<uint16_t> order;
if ((int)vorder.size() == n_total) {
order = vorder;
} else {
order.reserve((size_t)n_total);
for (int il = man.c.core_start; il < man.c.core_end; ++il)
for (int r = 0; r < man.c.n_experts; ++r) order.push_back((uint16_t)(il * man.c.n_experts + r));
for (int il = 0; il < man.c.core_start; ++il)
for (int r = 0; r < man.c.n_experts; ++r) order.push_back((uint16_t)(il * man.c.n_experts + r));
for (int il = man.c.core_end; il < man.c.n_phys; ++il)
for (int r = 0; r < man.c.n_experts; ++r) order.push_back((uint16_t)(il * man.c.n_experts + r));
}
std::vector<uint16_t> key_order;
std::vector<InkElasticSlot> eslots;
key_order.reserve(order.size());
eslots.reserve(order.size());
off_of_di.assign((size_t)n_total, 0);
std::vector<InkHostExpertMeta> src((size_t)n_total);
size_t max_body = 0;
uint64_t pack_off = 0;
for (uint16_t di : order) {
const auto& e = dir[di];
const int il = di / man.c.n_experts;
const int key = il * man.c.n_experts + e.expert_id;
const bool tr = xcode && e.gu_code_id == FMT_MX8 && e.dn_code_id == FMT_MX8; // held as SF4 on the card
const uint32_t bytes = tr ? (uint32_t)sf4_body : (uint32_t)e.body_bytes;
key_order.push_back((uint16_t)key);
InkElasticSlot s{};
s.key = key;
s.body_off = pack_off;
s.body_bytes = bytes;
s.gu_code = tr ? (uint8_t)FMT_SF4 : e.gu_code_id;
s.dn_code = tr ? (uint8_t)FMT_SF4 : e.dn_code_id;
s.resident = true;
eslots.push_back(s);
off_of_di[(size_t)di] = pack_off;
src[(size_t)key].body_bytes = bytes;
src[(size_t)key].dn_off = tr ? (uint32_t)sf4_dn_off : (uint32_t)e.dn_offset;
src[(size_t)key].gu_code = s.gu_code;
src[(size_t)key].dn_code = s.dn_code;
max_body = std::max<size_t>(max_body, bytes);
pack_off += bytes;
}
elastic_sec4.heat().init(man.c.n_phys, man.c.n_experts, man.c.core_start, man.c.core_end, key_order);
if (!elastic_sec4.init_vmm(pack_off, pack_off, eslots)) {
elastic_sec4.release();
off_of_di.clear();
return false;
}
elastic_src = std::move(src);
elastic_max_body = std::max(elastic_max_body, max_body);
pack_bytes = (size_t)pack_off;
elastic_compact = true;
elastic_compact_sf4_only = true;
for (const auto& s : eslots) if (s.gu_code != FMT_SF4 || s.dn_code != FMT_SF4) { elastic_compact_sf4_only = false; break; }
return true;
}
int compact_elastic_ctx = 0; // the context asked for (set by the backend before the load; 0 = not known): the COMPACT pool is limited to the contexts it is proven for
bool elastic_compact = false; // the experts of the COMPACT all-SF4 load are in the elastic pool
bool elastic_compact_sf4_only = false; // and every one of them is SF4 (the macro prefill's host pass runs the SF4 tensor-core kernels only)
// ---- Elastic expert pool (see ink_elastic_sec4.hpp) ----
// What arming allocates on the card: the prefill staging pair, the exact lane's staging slots, the hybrid engine's small buffers. Nothing is set aside in
// advance (an idle card would lose it for nothing, and the drafter's allocations are sized against the free memory): the first eviction frees this much
@@ -2845,7 +2936,7 @@ struct InkCudaModel {
if (d_elastic_heat) { ink_heat_cfg().dev = nullptr; cudaFree(d_elastic_heat); d_elastic_heat = nullptr; }
if (elastic_sec4.active()) { elastic_sec4.release(); d_sec4 = nullptr; }
h_exp_shadow.clear();
elastic_src.clear(); elastic_arena_off.clear(); elastic_armed = false; elastic_arena_ready = false; elastic_tier = false; elastic_arena_used = 0;
elastic_src.clear(); elastic_arena_off.clear(); elastic_armed = false; elastic_arena_ready = false; elastic_tier = false; elastic_arena_used = 0; elastic_compact = false; elastic_compact_sf4_only = false;
if (d_sec4) { cudaFree(d_sec4); d_sec4 = nullptr; }
if (h_sec4_pinned) { cudaFreeHost(h_sec4_pinned); h_sec4_pinned = nullptr; }
if (h_embed_raw) { cudaFreeHost(h_embed_raw); h_embed_raw = nullptr; d_embed_mapped = nullptr; }
@@ -2873,6 +2964,7 @@ struct InkCudaRunner {
// the context grows. Nothing is copied, so the card never holds the old and the new buffer at once and growth reaches max_ctx with every free byte (the copy path needed one old visit
// buffer extra and stopped 1.2 to 1.8 GB short). Off for several slots (the slot stride is the capacity), layer split, the elastic 1M mode and where the driver has no virtual memory.
bool kv_vmm = false;
bool ce_kv_vmm = false; // COMPACT elastic pool: the global K/V is in virtual memory beside the slab (the slab keeps the SWA rings and ShortConv state), grows by taking chunks from the coldest experts
struct KvVmmBuf { strata::core::VmmRange r; size_t bpp = 0; };
KvVmmBuf kv_vmm_buf[kInkVisits][4]; // per visit: K, V, K scales, V scales (FP16: only K and V)
int kv_dev_ctx = 0; // Global KV positions resident in VRAM
@@ -3414,6 +3506,24 @@ struct InkCudaRunner {
kv_dev_ctx = std::max(256, std::min(max_ctx, (max_slots == 1) ? c1_init_dev_cap : 4096));
kv_host_ctx = 0;
kv_alloc_ctx = kv_dev_ctx;
} else if (max_slots == 1 && !m.ls_active && !m.ep2_active && m.all_sf4_vram && kv_offload_mode == 0 && m.elastic_sec4.active() && m.n_host_experts == 0 &&
strata::core::vmm_available() && elastic_fits_max_ctx(avail_glob, per_pos_glob, max_ctx)) {
// COMPACT elastic pool (16 GB RTX 50, every expert on the card as SF4): the global K/V is no longer in the slab. It starts small (what the 128 MiB-free rule leaves, at most
// c1_init_dev_cap tokens) in its own virtual memory and grows by taking chunks from the coldest experts (reclaim_experts_for_kv), never from mapped host RAM.
// 200 MiB, not the 128 of the host-K/V rule: the K/V of the pool is mapped in whole 2 MiB chunks per buffer (a start of a few thousand tokens costs more than its bytes), and the first long prompt
// needs the macro prefill's scratch (72 MiB + a 48 MiB guard, ensure_pf_attn) from what is free after the start: with less the prefill fell back to the chunked path (4K prompt 3.9K -> 2.2K tok/s)
const double keep = 200.0 * 1048576.0;
const double room_s = (double)vfree - (double)eff_swa_vram - (double)sconv_bytes - (double)ws_reserve - keep; // nothing is set aside for arming: the first eviction frees what it needs
const int safe_start = room_s > 0.0 ? ((int)std::min<double>((double)(1 << 20), room_s / (double)std::max<size_t>(1, per_pos_glob)) & ~63) : 0;
kv_dev_ctx = std::max(256, std::min(std::min(max_ctx, c1_init_dev_cap), std::max(256, safe_start)));
kv_host_ctx = 0;
kv_alloc_ctx = kv_dev_ctx;
ce_kv_vmm = kv_dev_ctx < max_ctx;
const size_t room_e = elastic_free_room(avail_glob);
const size_t need_evict = (size_t)max_ctx * per_pos_glob > room_e ? (size_t)max_ctx * per_pos_glob - room_e : 0;
m.elastic_arena_plan = std::min<uint64_t>(m.elastic_sec4.reclaimable_bytes(), (uint64_t)need_evict + need_evict / 8 + (96ull << 20));
std::fprintf(stderr, "[InkCUDA] COMPACT elastic expert pool (kv_fmt=%d): K/V starts at %d tokens on the card (own virtual memory) and grows to %d with no host K/V; at most %.2f GiB of the coldest experts leave the card for it (%.2f GiB may)\n",
(int)kv_dtype, kv_dev_ctx, max_ctx, (double)need_evict / 1073741824.0, (double)m.elastic_sec4.reclaimable_bytes() / 1073741824.0);
} else if (max_slots == 1 && !m.ls_active && !m.ep2_active && !m.all_sf4_vram && kv_offload_mode == 0 && m.elastic_sec4.active() && (m.n_host_experts == 0 || m.elastic_tier) &&
strata::core::vmm_available() && elastic_fits_max_ctx(avail_glob, per_pos_glob, max_ctx)) {
// Elastic expert pool: the whole global K/V stays on the card (kv_host_ctx = 0). It starts small and grows through the K/V's virtual memory;
@@ -3458,7 +3568,7 @@ struct InkCudaRunner {
// 256-byte-aligned slab to eliminate ~240 separate 2 MiB cudaMalloc rounding slabs (~240+ MiB reclaimed)!
const bool use_slab = m.all_sf4_vram && !m.ls_active;
kv_vmm = false;
if (max_slots == 1 && !m.ls_active && !use_slab && !elastic_kv.enabled && kv_host_ctx == 0 && kv_dev_ctx > 0 && kv_dev_ctx < max_ctx) {
if (max_slots == 1 && !m.ls_active && (!use_slab || ce_kv_vmm) && !elastic_kv.enabled && kv_host_ctx == 0 && kv_dev_ctx > 0 && kv_dev_ctx < max_ctx) {
const char* vmm_env = std::getenv("STRATA_INK_KV_VMM");
if (!(vmm_env && vmm_env[0] == '0')) {
cudaSetDevice(m.primary_gpu);
@@ -3476,7 +3586,7 @@ struct InkCudaRunner {
const int ip = ink_visit_phys(v);
const bool swa = m.man.c.is_swa[ip] != 0;
const int dcap = swa ? swa_ring : kv_dev_ctx;
if (dcap > 0 && !(swa && swa_in_host && !swa_pinned[v])) {
if (dcap > 0 && !(swa && swa_in_host && !swa_pinned[v]) && !(kv_vmm && !swa)) { // the global K/V of the COMPACT elastic pool is not in the slab
const size_t elems = (size_t)max_slots * dcap * kInkKVHeads * kInkHeadDim;
if (kv_dtype == InkKvDtype::FP16) {
kv_bump(elems * sizeof(__half));
@@ -3533,7 +3643,7 @@ struct InkCudaRunner {
if (!ok_sw) {
ink_alloc_failed(cudaErrorMemoryAllocation, elems, __FILE__, __LINE__);
}
} else if (use_slab) {
} else if (use_slab && !(kv_vmm && !swa)) {
if (kv_dtype == InkKvDtype::FP16) {
kv_desc[v].k_f16 = reinterpret_cast<__half*>(kv_slice(elems * sizeof(__half)));
kv_desc[v].v_f16 = reinterpret_cast<__half*>(kv_slice(elems * sizeof(__half)));
@@ -4027,6 +4137,7 @@ struct InkCudaRunner {
if (!m.elastic_sec4.active()) return;
strata::platform::VramGuardConfig c = strata::platform::make_cuda_config(m.primary_gpu);
c.chunk_bytes = strata::core::vmm_granularity();
if (m.elastic_compact) { c.low_mib = 96; c.target_mib = 144; c.high_mib = 320; } // COMPACT starts with ~175 MiB free by design and lazy allocations of the first requests take ~60 MiB of it (117 MiB measured): the general 192 / 352 / 512 shed 350 MiB of experts at the first poll, 128 / 176 / 352 still 160 MiB, and the first request paid the arming (3 GiB pinned host copy, ~3 s: decode 89 -> 38 tok/s on a 128-token reply)
if (vram_guard.init(c)) std::fprintf(stderr, "[InkElastic] VRAM guard on: %s (low %llu MiB, target %llu MiB, high %llu MiB)\n", vram_guard.status(),
(unsigned long long)(vram_guard.low_bytes() >> 20), (unsigned long long)(vram_guard.target_bytes() >> 20), (unsigned long long)(vram_guard.high_bytes() >> 20));
}
@@ -6440,6 +6551,7 @@ struct InkCudaRunner {
if (!ink_rtx5060ti_cfg(m.primary_gpu).prefill_lm || !sm120::available()) return false;
if (m.ls_active || m.ep2_active || !m.hybrid.active || m.hybrid.ssd_only || m.hybrid.tier_on) return false;
if (max_passes_override != 0 || m.n_host_experts == 0) return false;
if (m.elastic_compact) return false; // the elastic pool takes experts from the middle of layers, not whole coda layers: this path streams whole layers only
const int fhc = first_host_coda_layer();
return (fhc >= m.man.c.core_end && fhc < m.man.c.n_phys);
}
@@ -6551,9 +6663,20 @@ struct InkCudaRunner {
bool can_allvram_macro_prefill() const noexcept {
if (!ink_rtx5060ti_cfg(m.primary_gpu).prefill_lm || !sm120::available()) return false;
if (!m.all_sf4_vram || m.ls_active || m.ep2_active || max_passes_override != 0 || m.n_host_experts != 0) return false;
if (!m.all_sf4_vram || m.ls_active || m.ep2_active || max_passes_override != 0 || (m.n_host_experts != 0 && !macro_host_pass_ok())) return false;
return (d_pf_slab != nullptr && d_pf_slab_bytes >= (72ull << 20));
}
// COMPACT elastic pool: experts that left the card for the K/V are streamed layer by layer inside the macro prefill (macro_host_pass), so a long prompt keeps its
// single-sweep speed after the first eviction. Needs the exact lane's staging pair, a pinned host copy and SF4 experts only.
bool macro_host_pass_ok() const noexcept {
const auto& h = m.hybrid;
return m.elastic_compact && m.elastic_compact_sf4_only && m.elastic_armed && h.d_pf_stage[0] && h.d_pf_stage[1] && h.pf_stage_bytes >= (8ull << 20) &&
h.sec4_is_pinned && !h.tier_on && !h.ssd_only && !host_macro_off();
}
static bool host_macro_off() noexcept { // STRATA_INK_ELASTIC_MACRO_HOST=0: the evicted experts keep the generic chunked prefill (the old behaviour of this lane)
const char* e = std::getenv("STRATA_INK_ELASTIC_MACRO_HOST");
return e && e[0] == '0';
}
// Visit-Major All-VRAM Macro-Prefill (W = 2048..4096 tokens per MoE weight sweep -> >15,000-20,000 ppts on 8K+!):
// Runs Attn+ShortConv+Controllers+Router in micro-chunks (mc <= max_batch) reusing d_pf_slab for Flash-GQA,
@@ -6731,6 +6854,57 @@ struct InkCudaRunner {
const Sm120MoeSel mac_sel = macro_moe_sel(tile_rows);
sm120::moe_sf4_gu_tc(m.d_sec4, m.d_experts[ip], tb, opt5060_macro.d_xa_fp4, d_ha_mac, max_tiles, tile_rows, st, &mac_sel);
sm120::moe_sf4_dn_tc(m.d_sec4, m.d_experts[ip], tb, d_ha_mac, opt5060_macro.d_tw, d_y_acc, max_tiles, tile_rows, st, /*atomic_reduce=*/true, &mac_sel);
if (m.n_host_experts > 0 && m.layer_has_host[ip]) {
// The experts of this layer that left the card (COMPACT elastic pool): the same single W-token sweep, over the host copy streamed through the staging pair
// in chunks of at most 24 MiB. The binning skips every expert whose table entry is tier 2, so each pass counts only the experts of its chunk.
auto& hyb = m.hybrid;
struct HostChunk { std::vector<int> eids; std::vector<size_t> offs; size_t total = 0; };
std::vector<HostChunk> hchunks;
{
HostChunk cur;
size_t used = 0;
const size_t cap = std::min<size_t>(hyb.pf_stage_bytes, 24ull << 20);
for (int e = 0; e < kInkExperts; ++e) {
const auto& hm = hyb.host_meta[(size_t)ip * kInkExperts + e];
if (!hm.body) continue;
const size_t b = (hm.body_bytes + 255ull) & ~255ull;
if (used + b > cap && !cur.eids.empty()) { cur.total = used; hchunks.push_back(std::move(cur)); cur = {}; used = 0; }
cur.eids.push_back(e);
cur.offs.push_back(used);
used += b;
}
if (!cur.eids.empty()) { cur.total = used; hchunks.push_back(std::move(cur)); }
}
auto issue_host_dma = [&](size_t ci, int buf) {
if (hyb.pf_buf_busy[buf]) { INK_CK(cudaEventSynchronize(hyb.pf_ev_done[buf])); hyb.pf_buf_busy[buf] = false; }
const auto& ch = hchunks[ci];
InkExpertDev* he = hyb.h_pf_exp_pin[buf];
for (int e = 0; e < kInkExperts; ++e) { he[e] = {}; he[e].tier = 2; }
for (size_t i = 0; i < ch.eids.size(); ++i) {
const int e = ch.eids[i];
const auto& hm = hyb.host_meta[(size_t)ip * kInkExperts + e];
he[e].body_off = ch.offs[i];
he[e].dn_off = hm.dn_off;
he[e].gu_code = hm.gu_code;
he[e].dn_code = hm.dn_code;
he[e].tier = 0;
INK_CK(cudaMemcpyAsync(hyb.d_pf_stage[buf] + ch.offs[i], hm.body, hm.body_bytes, cudaMemcpyHostToDevice, hyb.pf_dma_st));
}
INK_CK(cudaMemcpyAsync(hyb.d_pf_exp_tmp[buf], he, (size_t)kInkExperts * sizeof(InkExpertDev), cudaMemcpyHostToDevice, hyb.pf_dma_st));
INK_CK(cudaEventRecord(hyb.pf_ev_dma[buf], hyb.pf_dma_st));
};
if (!hchunks.empty()) issue_host_dma(0, 0);
for (size_t ci = 0; ci < hchunks.size(); ++ci) {
const int buf = (int)(ci & 1);
if (ci + 1 < hchunks.size()) issue_host_dma(ci + 1, 1 - buf);
INK_CK(cudaStreamWaitEvent(st, hyb.pf_ev_dma[buf], 0));
k_ink_moe_bin<<<1, 128, 0, st>>>(hyb.d_pf_exp_tmp[buf], opt5060_macro.d_tidx, W, tile_rows, tb);
sm120::moe_sf4_gu_tc(hyb.d_pf_stage[buf], hyb.d_pf_exp_tmp[buf], tb, opt5060_macro.d_xa_fp4, d_ha_mac, max_tiles, tile_rows, st, &mac_sel);
sm120::moe_sf4_dn_tc(hyb.d_pf_stage[buf], hyb.d_pf_exp_tmp[buf], tb, d_ha_mac, opt5060_macro.d_tw, d_y_acc, max_tiles, tile_rows, st, /*atomic_reduce=*/true, &mac_sel);
INK_CK(cudaEventRecord(hyb.pf_ev_done[buf], st));
hyb.pf_buf_busy[buf] = true;
}
}
if (prof_prefill) { INK_CK(cudaStreamSynchronize(st)); tm_stepB += mms(tb0, mnow()); }
auto tc0 = mnow();