runtime: per-layer expert cache with each layer's own blob size (the Original's #369 plan) instead of slots of the largest blob; cluster decode top-k, wide prompt top-k and the no-store GDN output norm on by default

Per-layer cache: 48 x quota slots of each layer's blob (1.94 MiB average) where the uniform plan gave slots of the largest blob (2.54 MiB): same VRAM, about 30 % more
resident experts (night5: 8,702 slots in 21.58 GiB; vanilla Strata 12,575 in 23.87 GiB). LELEKA_ORIGINAL_SERVE_PER_LAYER_SIZED=0 keeps the uniform plan.
Defaults flipped after gate I (7 arms, 1,502 forced rows with zero deviation, prefill chunk hashes equal up to the cache cut) and gate R (QSA stage -4..-6 %, decode window
-8..-10 % per round, replay of night5-long-256k): LELEKA_QSA_CLUSTER=0, LELEKA_QSA_TOPK=fast, LELEKA_PREFILL_GDN_NOSTORE=0 switch them off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-27 20:13:16 +01:00
co-authored by Claude Sonnet 5.5
parent e96c6332c1
commit b66484a470
4 changed files with 80 additions and 5 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ pub(crate) fn gdn_nostore() -> bool {
*ON.get_or_init(|| std::env::var("LELEKA_PREFILL_GDN_NOSTORE").map_or(GDN_NOSTORE_DEFAULT, |v| v != "0"))
}
/// Default of `LELEKA_PREFILL_GDN_NOSTORE` (flipped after its identity gate).
pub(crate) const GDN_NOSTORE_DEFAULT: bool = false;
pub(crate) const GDN_NOSTORE_DEFAULT: bool = true;
static NOSTORE_OVERRIDE: std::sync::atomic::AtomicI8 = std::sync::atomic::AtomicI8::new(-1);
pub fn set_gdn_nostore_override(value: i8) {
NOSTORE_OVERRIDE.store(value, std::sync::atomic::Ordering::Relaxed);
+1 -1
View File
@@ -57,7 +57,7 @@ pub fn set_topk_override(value: i8) {
TOPK_OVERRIDE.store(value, std::sync::atomic::Ordering::Relaxed);
}
/// Default of `LELEKA_QSA_TOPK` (flipped after its R gate).
pub(crate) const TOPK_DEFAULT_WIDE: bool = false;
pub(crate) const TOPK_DEFAULT_WIDE: bool = true;
pub(crate) fn topk_entry(tile_reach: usize) -> (usize, u32) {
static WIDE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let wide = match TOPK_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) {
@@ -55,14 +55,31 @@ impl Geometry {
Ok(Self{layer_bytes,max_blob})
}
}
struct Plan{slots:usize,sizes:Vec<usize>,blob:usize}
/// `layers`: the per-layer plan (`--expert-cache-per-layer` on a native pack, generate.cpp ~6333): `quota` slots of EACH layer's own blob
/// size, in layer order (layer l owns slots l * quota .. (l + 1) * quota, the table's ranges), instead of `slots` slots of the largest blob.
struct Plan{slots:usize,sizes:Vec<usize>,blob:usize,layers:Option<(usize,[usize;48])>}
impl Plan {
fn bytes(&self)->Result<usize>{
if self.sizes.is_empty(){self.slots.checked_mul(self.blob).ok_or(Error::Overflow)}
else{self.sizes.iter().try_fold(0usize,|n,&s|n.checked_add(align(s)?).ok_or(Error::Overflow))}
}
/// The per-layer list for `quota` slots per layer.
fn per_layer_sizes(quota:usize,layer_bytes:&[usize;48])->Result<Vec<usize>>{
let mut sizes=Vec::new();
sizes.try_reserve_exact(quota.checked_mul(48).ok_or(Error::Overflow)?).map_err(|_|Error::InputAllocation)?;
for &b in layer_bytes{for _ in 0..quota{sizes.push(b);}}
Ok(sizes)
}
fn shrink_to(&mut self,keep:usize)->Result<bool>{
if keep==0{self.slots=0;self.sizes.clear();return Ok(false);}
if let Some((quota,layer_bytes))=&mut self.layers{
// the same quota of every layer that fits `keep` (a truncated list would shift the layers' slot ranges)
let sum=layer_bytes.iter().try_fold(0usize,|n,&b|n.checked_add(align(b)?).ok_or(Error::Overflow))?;
*quota=(keep/sum).min(512);
if *quota==0{self.slots=0;self.sizes.clear();return Ok(false);}
self.sizes=Self::per_layer_sizes(*quota,layer_bytes)?;self.slots=self.sizes.len();
return Ok(true);
}
if self.sizes.is_empty(){self.slots=keep/self.blob;}
else{
let (mut used,mut count)=(0usize,0usize);
@@ -100,7 +117,20 @@ pub(crate) fn open<'ctx>(binding:&ModelBinding,context:&'ctx Context<'ctx>,optio
free.saturating_sub(reserved)/geometry.max_blob
}else{options.slots as usize};
if auto{if let Some(profile)=profile.filter(|p|!p.is_empty()){slots=slots.min(profile.pairs.len());}}
let mut plan=Plan{slots,sizes:Vec::new(),blob:geometry.max_blob};
let mut plan=Plan{slots,sizes:Vec::new(),blob:geometry.max_blob,layers:None};
// Row "5090 gap to vanilla": the per-layer cache took `slots` slots of the LARGEST layer blob (2.54 MiB; 8,702 slots in 21.6 GiB) where the Original
// (and vanilla Strata) give each layer slots of its own blob size (about 1.94 MiB on average; 12,575 slots in 23.9 GiB with the profile). Same VRAM,
// 30 % more resident experts. LELEKA_ORIGINAL_SERVE_PER_LAYER_SIZED=0 keeps the uniform slots (the A/B arm).
if options.per_layer&&slots>0&&std::env::var("LELEKA_ORIGINAL_SERVE_PER_LAYER_SIZED").map_or(true,|v|v!="0"){
let free=context.memory_info().map_err(driver)?.0;
let cap=slots.checked_mul(geometry.max_blob).ok_or(Error::Overflow)?.min(free.saturating_sub(reserve));
let sum=geometry.layer_bytes.iter().try_fold(0usize,|n,&b|n.checked_add(align(b)?).ok_or(Error::Overflow))?;
let quota=(cap/sum).min(512);
if quota>0{
plan.sizes=Plan::per_layer_sizes(quota,&geometry.layer_bytes)?;plan.slots=plan.sizes.len();
plan.layers=Some((quota,geometry.layer_bytes));
}
}
if let Some(profile)=profile.filter(|p|!p.is_empty()&&!options.per_layer&&slots>0){
// Original rereads the card before sizing variable slots.
let free=context.memory_info().map_err(driver)?.0;
@@ -140,3 +170,48 @@ pub(crate) fn open<'ctx>(binding:&ModelBinding,context:&'ctx Context<'ctx>,optio
}
unreachable!()
}
#[cfg(test)]
mod per_layer_plan_tests {
use super::*;
fn layer_bytes() -> [usize; 48] {
let mut b = [2_000_000usize; 48];
b[3] = 2_660_000;
b[47] = 1_500_000;
b
}
#[test]
fn the_per_layer_list_is_a_quota_of_every_layers_own_blob_in_layer_order() {
let lb = layer_bytes();
let sizes = Plan::per_layer_sizes(5, &lb).unwrap();
assert_eq!(sizes.len(), 5 * 48);
for layer in 0..48 {
assert!(sizes[layer * 5..(layer + 1) * 5].iter().all(|&s| s == lb[layer]), "layer {layer} owns slots layer * quota ..");
}
}
#[test]
fn a_shrunk_per_layer_plan_keeps_the_layer_ranges_and_fits_the_bytes() {
let lb = layer_bytes();
let quota = 100;
let sizes = Plan::per_layer_sizes(quota, &lb).unwrap();
let mut plan = Plan { slots: sizes.len(), sizes, blob: 2_660_000, layers: Some((quota, lb)) };
let before = plan.bytes().unwrap();
assert!(plan.shrink_to(before / 4 * 3).unwrap());
let q = plan.slots / 48;
assert_eq!(plan.slots, q * 48);
assert!(q < quota && q > 0);
assert!(plan.bytes().unwrap() <= before / 4 * 3);
for layer in 0..48 {
assert!(plan.sizes[layer * q..(layer + 1) * q].iter().all(|&s| s == lb[layer]));
}
assert!(!plan.shrink_to(0).unwrap());
}
#[test]
fn the_sized_per_layer_cache_holds_more_experts_than_slots_of_the_largest_blob_in_the_same_bytes() {
let lb = layer_bytes();
let bytes = 21 << 30;
let uniform = bytes / 2_660_000;
let sum: usize = lb.iter().map(|&b| align(b).unwrap()).sum();
assert!((bytes / sum) * 48 > uniform);
}
}
@@ -29,7 +29,7 @@ const PLE_HISTORY_VALUES: usize = 9 * 10240;
pub(in crate::qwen_forward) const PLE_HISTORY_BYTES: usize = PLE_HISTORY_VALUES * 4;
/// Default of `LELEKA_QSA_CLUSTER` (the cluster top-k of the decode window; flipped after its gates, docs/upstream-port-plan.md row 2.5).
const QSA_CLUSTER_DEFAULT: bool = false;
const QSA_CLUSTER_DEFAULT: bool = true;
/// A refusal with the driver's own words when it has them (an unbound producer, an unsealed asset).
#[derive(Clone, Debug, Eq, PartialEq)]