STRA-338 S13: memory and expert-tier settings
Memory topic of Settings: tail-skip rank and its cache limit, span cache, refill defer, swap wait, PCIe locked-only and auto modes, the adaptive tier numbers (min count, margin, decay text), routing dump, per-layer sizing, the fault-injection count of the expert cache, and the CPU pool switches (pool path, fence, lookahead, open cache, prefetch word, eager admission, prefetch-off file). ExpertTierConfig::from_environment keeps its signature and delegates to from_source(&impl EnvSource), the strict parser (a value that does not parse is refused); tier_env_tests.rs pins its spellings, defaults and refusals. LELEKA_ADAPT_DECAY stays text so its range check and log line still run, once, where they did. LELEKA_CPU_PREFETCH_OFF_FILE keeps the path from the settings and still asks the file system on every call. OnceLock readers of these keys are gone (process-global 93 to 81, env-read 59 to 37).
This commit is contained in:
@@ -165,7 +165,7 @@
|
||||
"env-read:leleka-npu-service": 1,
|
||||
"env-read:leleka-npu-worker": 1,
|
||||
"env-read:leleka-obs": 2,
|
||||
"env-read:leleka-runtime": 59,
|
||||
"env-read:leleka-runtime": 37,
|
||||
"env-read:leleka-tokenizer": 2,
|
||||
"env-read:strata-control": 19,
|
||||
"env-read:strata-desktop-core": 15,
|
||||
@@ -198,7 +198,7 @@
|
||||
"process-global:leleka-model-format": 8,
|
||||
"process-global:leleka-native-cpu-expert": 32,
|
||||
"process-global:leleka-obs": 7,
|
||||
"process-global:leleka-runtime": 93,
|
||||
"process-global:leleka-runtime": 81,
|
||||
"process-global:leleka-tokenizer": 4,
|
||||
"process-global:strata-control": 14
|
||||
}
|
||||
|
||||
@@ -61,7 +61,11 @@ impl Geometry {
|
||||
static SIZED_OVERRIDE:std::sync::atomic::AtomicI8=std::sync::atomic::AtomicI8::new(-1);
|
||||
pub(crate) fn set_sized_override(value:i8){SIZED_OVERRIDE.store(value,std::sync::atomic::Ordering::Relaxed);}
|
||||
fn sized_per_layer()->bool{
|
||||
match SIZED_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed){0=>false,1=>true,_=>std::env::var("LELEKA_ORIGINAL_SERVE_PER_LAYER_SIZED").map_or(true,|v|v!="0")}
|
||||
match SIZED_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed){
|
||||
0=>false,
|
||||
1=>true,
|
||||
_=>crate::settings::process().memory.per_layer_sized,
|
||||
}
|
||||
}
|
||||
struct Plan{slots:usize,sizes:Vec<usize>,blob:usize,layers:Option<(usize,[usize;48])>}
|
||||
impl Plan {
|
||||
@@ -160,7 +164,7 @@ pub(crate) fn open<'ctx>(binding:&ModelBinding,context:&'ctx Context<'ctx>,optio
|
||||
if plan.slots==0{return Ok(None);}
|
||||
let (mut failed,mut zero_reads)=(0usize,0usize);
|
||||
// Same optional fault-injection knob; no execution is done by this source port.
|
||||
let mut fake_fails=std::env::var("STRATA_TEST_CACHE_FAIL").ok().and_then(|s|s.parse::<i32>().ok()).unwrap_or(0);
|
||||
let mut fake_fails=crate::settings::process().memory.test_cache_fail;
|
||||
for attempt in 0usize..{
|
||||
let opened=if fake_fails>0{fake_fails-=1;Err(leleka_cuda_driver::Error::BudgetExceeded)}
|
||||
else if plan.sizes.is_empty(){context.original_expert_cache_uniform(plan.slots,48,512,geometry.max_blob)}
|
||||
|
||||
@@ -176,19 +176,22 @@ impl<'a> CpuExpertPool<'a> {
|
||||
let mut routes = Vec::new();
|
||||
routes.try_reserve_exact(table).map_err(|_| Error::InputAllocation)?;
|
||||
routes.resize_with(table, || None);
|
||||
let group_fence = std::env::var("LELEKA_CPU_POOL_FENCE").as_deref() != Ok("job");
|
||||
let pool_settings = &crate::settings::process().memory;
|
||||
let group_fence = pool_settings.cpu_pool_group_fence;
|
||||
if !group_fence {
|
||||
eprintln!("leleka-serve cpu expert pool: one source fence per job (LELEKA_CPU_POOL_FENCE=job)");
|
||||
}
|
||||
// Opt-in, like the Original's: on the 8-core laptop with a warm page cache it measured no gain, and
|
||||
// its router GEMVs (~0.7 ms per layer) compete with the workers for the cores.
|
||||
let lookahead_enabled = std::env::var("LELEKA_CPU_POOL_LOOKAHEAD").as_deref() == Ok("on");
|
||||
let lookahead_enabled = pool_settings.cpu_pool_lookahead;
|
||||
let mut opened_cache = Vec::new();
|
||||
opened_cache.try_reserve_exact(table).map_err(|_| Error::InputAllocation)?;
|
||||
opened_cache.resize_with(table, || None);
|
||||
let open_cache_on = std::env::var("LELEKA_OPEN_CACHE").as_deref() == Ok("on");
|
||||
let open_cache_on = pool_settings.open_cache;
|
||||
let prefetch_text = pool_settings.cpu_prefetch.as_deref();
|
||||
let prefetch_mode = super::super::prefetch_policy::Mode::from_value(prefetch_text, cfg!(windows));
|
||||
let mut tier = DirectTier { pool, held, routes, group_fence, lookahead_enabled, lookahead: None, next_resident: vec![false; EXPERTS as usize], opened_cache, open_cache_on,
|
||||
prefetch: super::super::prefetch_policy::PrefetchPolicy::new(super::super::prefetch_policy::Mode::from_value(std::env::var("LELEKA_CPU_PREFETCH").ok().as_deref(), cfg!(windows))) };
|
||||
prefetch: super::super::prefetch_policy::PrefetchPolicy::new(prefetch_mode) };
|
||||
if admit_eager() {
|
||||
let started = std::time::Instant::now();
|
||||
let n = tier.admit_all(binding)?;
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ impl Drop for DirectTier {
|
||||
|
||||
/// `LELEKA_CPU_ADMIT_EAGER=off` keeps admission at an expert's first use (A/B control); default: every expert at open.
|
||||
pub(super) fn admit_eager() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| !matches!(std::env::var("LELEKA_CPU_ADMIT_EAGER").as_deref(), Ok("off" | "0")))
|
||||
crate::settings::process().memory.cpu_admit_eager
|
||||
}
|
||||
|
||||
pub(super) fn sink_result(written: leleka_cuda_driver::Result<()>, failure: Option<Error>) -> Result<()> {
|
||||
|
||||
+5
-5
@@ -6,8 +6,8 @@ use super::*;
|
||||
|
||||
/// A bring-up file (`LELEKA_CPU_PREFETCH_OFF_FILE`, A/B inside one engine): while it exists the page-touch pass is skipped, whatever the policy says.
|
||||
pub(super) fn prefetch_off_file_exists() -> bool {
|
||||
static FILE: std::sync::OnceLock<Option<std::path::PathBuf>> = std::sync::OnceLock::new();
|
||||
FILE.get_or_init(|| std::env::var_os("LELEKA_CPU_PREFETCH_OFF_FILE").map(std::path::PathBuf::from)).as_ref().is_some_and(|f| f.exists())
|
||||
let file = crate::settings::process().memory.cpu_prefetch_off_file.as_ref();
|
||||
file.is_some_and(|f| std::path::Path::new(f).exists())
|
||||
}
|
||||
|
||||
/// Group the router's ids by expert. Experts are visited in first-use order (row-major), pairs keep
|
||||
@@ -71,9 +71,9 @@ pub(in crate::qwen_forward) fn scatter(out: &mut [f32], job: &ExpertJob, values:
|
||||
}
|
||||
impl PoolPath {
|
||||
pub(in crate::qwen_forward) fn from_env() -> Self {
|
||||
match std::env::var("LELEKA_CPU_POOL").as_deref() {
|
||||
Ok("legacy") => Self::Legacy,
|
||||
Ok("crosscheck") => Self::Crosscheck,
|
||||
match crate::settings::process().memory.cpu_pool.as_deref() {
|
||||
Some("legacy") => Self::Legacy,
|
||||
Some("crosscheck") => Self::Crosscheck,
|
||||
_ => Self::Direct,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ use super::{
|
||||
original_serve_verifier::driver_error,
|
||||
};
|
||||
use crate::model_binding::qwen4exp_input::ModelBinding;
|
||||
use crate::settings::EnvSource;
|
||||
use leleka_model_format::qwen4exp::ResidentExpertBankSpan;
|
||||
use leleka_cuda_driver::{
|
||||
Context, MappedExpertBank, ORIGINAL_VERIFY_MAX_PCIE_FETCHES, OriginalExpertCache, OriginalGpuStream,
|
||||
@@ -241,6 +242,8 @@ pub(in crate::qwen_forward) struct GpuExpertPool<'p, 'a, 'ctx> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(test)]
|
||||
mod tier_env_tests;
|
||||
|
||||
mod tail_skip_rank_fn;
|
||||
#[allow(unused_imports)]
|
||||
|
||||
+7
-5
@@ -9,8 +9,7 @@ impl std::fmt::Debug for SpanCache {
|
||||
}
|
||||
}
|
||||
pub(super) fn span_cache_enabled() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| !std::env::var("LELEKA_SPAN_CACHE").is_ok_and(|v| v == "off" || v == "0"))
|
||||
crate::settings::process().memory.span_cache
|
||||
}
|
||||
impl SpanCache {
|
||||
pub(super) fn key(layer: usize, expert: u32) -> u32 {
|
||||
@@ -31,7 +30,7 @@ impl PcieLag {
|
||||
pub(super) const MAX: u32 = 176;
|
||||
pub(super) const WAIT: std::time::Duration = std::time::Duration::from_millis(3);
|
||||
pub(crate) fn from_environment(start_num: u32) -> Option<Self> {
|
||||
if !std::env::var("LELEKA_PCIE_AUTO").is_ok_and(|v| v == "lag") || start_num == 0 {
|
||||
if !crate::settings::process().memory.pcie_auto_lag || start_num == 0 {
|
||||
return None;
|
||||
}
|
||||
Some(Self { num: start_num.clamp(Self::MIN, Self::MAX), lag_us: 0.0, groups: 0, ema_us: None, rounds_since_up: 0 })
|
||||
@@ -74,7 +73,7 @@ impl PcieAuto {
|
||||
pub(super) const TRIAL: u32 = 6;
|
||||
pub(super) const EXPLORE_EVERY: u32 = 30;
|
||||
pub(crate) fn from_environment(start_num: u32) -> Option<Self> {
|
||||
if !std::env::var("LELEKA_PCIE_AUTO").is_ok_and(|v| v == "on") || start_num == 0 {
|
||||
if !crate::settings::process().memory.pcie_auto_on || start_num == 0 {
|
||||
return None;
|
||||
}
|
||||
let levels: Vec<u32> = (2..=10).map(|i| i * 16).collect(); // 0.125 .. 0.625
|
||||
@@ -135,7 +134,10 @@ impl PcieAuto {
|
||||
}
|
||||
impl ResidencyState {
|
||||
pub(crate) fn new(config: &ExpertTierConfig) -> Self {
|
||||
let routing = std::env::var_os("LELEKA_ORIGINAL_SERVE_DUMP_ROUTING")
|
||||
let routing = crate::settings::process()
|
||||
.memory
|
||||
.dump_routing
|
||||
.as_ref()
|
||||
.and_then(|path| std::fs::File::create(path).ok())
|
||||
.map(std::io::BufWriter::new);
|
||||
Self {
|
||||
|
||||
@@ -183,6 +183,5 @@ pub(in crate::qwen_forward) fn settle_refill(cache: &mut OriginalExpertCache<'_>
|
||||
/// the earlier variant (queued at the end of the prompt phase, waited later) as no gain: the PLE seed upload waited for the same
|
||||
/// DMA. The current variant has no GPU result yet (job 904), so it stays opt-in.
|
||||
pub(in crate::qwen_forward) fn refill_defer_enabled() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var("LELEKA_REFILL_DEFER").is_ok_and(|v| v == "on" || v == "1"))
|
||||
crate::settings::process().memory.refill_defer
|
||||
}
|
||||
|
||||
@@ -189,6 +189,5 @@ impl<'ctx> OriginalServePool<'ctx> for GpuExpertPool<'_, '_, 'ctx> {
|
||||
|
||||
/// `LELEKA_SERVE_SWAP_WAIT=off`: poll the adaptive swaps at a round start instead of waiting for them.
|
||||
pub(super) fn swap_wait() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| !std::env::var("LELEKA_SERVE_SWAP_WAIT").is_ok_and(|v| v == "off" || v == "0"))
|
||||
crate::settings::process().memory.swap_wait
|
||||
}
|
||||
|
||||
+4
-5
@@ -107,8 +107,7 @@ pub(in crate::qwen_forward) fn split_group_pcie(
|
||||
/// `LELEKA_PCIE_LOCKED_ONLY=on|1` (read once): over a pageable file-mapped bank only the experts whose pages `pin_resident` locked may
|
||||
/// take the PCIe share; every other miss stays with the CPU pool. Off by default (the unrestricted split).
|
||||
pub(in crate::qwen_forward) fn pcie_locked_only() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var("LELEKA_PCIE_LOCKED_ONLY").is_ok_and(|v| v == "on" || v == "1"))
|
||||
crate::settings::process().memory.pcie_locked_only
|
||||
}
|
||||
|
||||
/// Experiment knobs of the adaptive tier (claude/decode-parity; the defaults are generate.cpp's constants):
|
||||
@@ -117,12 +116,12 @@ pub(in crate::qwen_forward) fn pcie_locked_only() -> bool {
|
||||
pub(super) fn adapt_tuning() -> (f32, f32, f32) {
|
||||
static TUNING: std::sync::OnceLock<(f32, f32, f32)> = std::sync::OnceLock::new();
|
||||
*TUNING.get_or_init(|| {
|
||||
let var = |name: &str, default: f32| std::env::var(name).ok().and_then(|v| v.parse::<f32>().ok()).filter(|v| v.is_finite()).unwrap_or(default);
|
||||
let decay = adapt_decay(std::env::var("LELEKA_ADAPT_DECAY").ok().as_deref());
|
||||
let memory = &crate::settings::process().memory;
|
||||
let decay = adapt_decay(memory.adapt_decay.as_deref());
|
||||
if let Some(line) = decay.1 {
|
||||
eprintln!("leleka-serve {line}");
|
||||
}
|
||||
(var("LELEKA_ADAPT_MIN_COUNT", 2.0), var("LELEKA_ADAPT_MARGIN", 1.5), decay.0)
|
||||
(memory.adapt_min_count, memory.adapt_margin, decay.0)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -9,7 +9,7 @@ pub(crate) fn tail_skip_rank() -> u32 {
|
||||
if rank != TAIL_UNRESOLVED {
|
||||
return rank;
|
||||
}
|
||||
let rank = std::env::var("LELEKA_ROUTE_TAIL_SKIP").ok().and_then(|v| v.trim().parse::<u32>().ok()).filter(|r| (1..TOP_K as u32).contains(r)).unwrap_or(0);
|
||||
let rank = crate::settings::process().memory.route_tail_skip.filter(|r| (1..TOP_K as u32).contains(r)).unwrap_or(0);
|
||||
TAIL_SKIP_RANK.store(rank, Ordering::Relaxed);
|
||||
rank
|
||||
}
|
||||
@@ -25,7 +25,7 @@ pub(crate) fn effective_tail_rank(cache: &OriginalExpertCache<'_>) -> u32 {
|
||||
}
|
||||
let cached: usize = (0..LAYERS).map(|l| (0..EXPERTS).filter(|&e| cache.resident_slot(l, e).is_some()).count()).sum();
|
||||
let percent = 100.0 * cached as f64 / (LAYERS * EXPERTS) as f64;
|
||||
let limit = std::env::var("LELEKA_ROUTE_TAIL_SKIP_MAX_CACHED").ok().and_then(|v| v.trim().parse::<f64>().ok()).unwrap_or(90.0);
|
||||
let limit = crate::settings::process().memory.route_tail_skip_max_cached;
|
||||
let effective = if percent >= limit { 0 } else { rank };
|
||||
static ANNOUNCED: AtomicU32 = AtomicU32::new(TAIL_UNRESOLVED);
|
||||
if ANNOUNCED.swap(effective, Ordering::Relaxed) != effective {
|
||||
@@ -118,12 +118,16 @@ impl ExpertTierConfig {
|
||||
/// `_DECODE_RESERVE_MIB`, `_PCIE_FRAC`, `_ADAPT_EVERY`, `_ADAPT_SWAPS`, `_PREFILL_BORROW` (`no`).
|
||||
/// A value that does not parse is refused, never ignored.
|
||||
pub(crate) fn from_environment() -> Result<Self> {
|
||||
let var = |name: &str| std::env::var(format!("LELEKA_ORIGINAL_SERVE_{name}")).ok();
|
||||
Self::from_source(crate::settings::process().env())
|
||||
}
|
||||
/// The same switches read from `env`.
|
||||
pub(crate) fn from_source(env: &impl EnvSource) -> Result<Self> {
|
||||
let var = |name: &str| env.get(&format!("LELEKA_ORIGINAL_SERVE_{name}"));
|
||||
let mut config = Self::off();
|
||||
if let Some(v) = var("EXPERT_SLOTS") {
|
||||
config.slots = if v == "auto" { -1 } else { v.parse().map_err(|_| Error::InvalidDimension)? };
|
||||
}
|
||||
config.profile = std::env::var_os("LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE").map(PathBuf::from);
|
||||
config.profile = env.get_os("LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE").map(PathBuf::from);
|
||||
if let Some(v) = var("EXPERT_PER_LAYER") {
|
||||
config.per_layer = matches!(v.as_str(), "yes" | "1" | "on");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
//! Golden cases of `ExpertTierConfig::from_source`, the strict parser of the `LELEKA_ORIGINAL_SERVE_*` tier
|
||||
//! switches: a value that does not parse (or fails `validate`) is refused, never ignored.
|
||||
use super::*;
|
||||
use crate::settings::MapEnv;
|
||||
|
||||
const P: &str = "LELEKA_ORIGINAL_SERVE_";
|
||||
|
||||
fn read(pairs: &[(&str, &str)]) -> Result<ExpertTierConfig> {
|
||||
let pairs: Vec<(String, &str)> = pairs.iter().map(|(k, v)| (format!("{P}{k}"), *v)).collect();
|
||||
let env = pairs
|
||||
.iter()
|
||||
.fold(MapEnv::new(), |env, (k, v)| env.with(k, v));
|
||||
ExpertTierConfig::from_source(&env)
|
||||
}
|
||||
|
||||
fn defaults() -> ExpertTierConfig {
|
||||
ExpertTierConfig {
|
||||
seed_swaps: super::super::original_serve_seed::DEFAULT_SEED_SWAPS,
|
||||
..ExpertTierConfig::off()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_environment_is_off_with_the_seed_on() {
|
||||
assert_eq!(read(&[]), Ok(defaults()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slots_are_auto_or_a_number_not_below_minus_one() {
|
||||
for (value, want) in [
|
||||
("auto", Some(-1)),
|
||||
("64", Some(64)),
|
||||
("0", Some(0)),
|
||||
("-1", Some(-1)),
|
||||
("+5", Some(5)),
|
||||
] {
|
||||
assert_eq!(
|
||||
read(&[("EXPERT_SLOTS", value)]).map(|c| c.slots).ok(),
|
||||
want,
|
||||
"{value:?}"
|
||||
);
|
||||
}
|
||||
for value in ["-2", "x", "", "1.5", " 5", "AUTO"] {
|
||||
assert!(read(&[("EXPERT_SLOTS", value)]).is_err(), "{value:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn per_layer_and_prefill_borrow_never_fail() {
|
||||
for (value, want) in [
|
||||
("yes", true),
|
||||
("1", true),
|
||||
("on", true),
|
||||
("no", false),
|
||||
("0", false),
|
||||
("true", false),
|
||||
("", false),
|
||||
] {
|
||||
assert_eq!(
|
||||
read(&[("EXPERT_PER_LAYER", value)]).map(|c| c.per_layer),
|
||||
Ok(want),
|
||||
"{value:?}"
|
||||
);
|
||||
}
|
||||
for (value, want) in [
|
||||
("no", false),
|
||||
("0", false),
|
||||
("off", false),
|
||||
("yes", true),
|
||||
("1", true),
|
||||
("", true),
|
||||
("NO", true),
|
||||
] {
|
||||
assert_eq!(
|
||||
read(&[("PREFILL_BORROW", value)]).map(|c| c.prefill_borrow),
|
||||
Ok(want),
|
||||
"{value:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_reserves_and_adapt_counts_are_untrimmed_unsigned_numbers() {
|
||||
assert_eq!(
|
||||
read(&[("VRAM_RESERVE_MIB", "512")]).map(|c| c.reserve_mib),
|
||||
Ok(512)
|
||||
);
|
||||
assert_eq!(
|
||||
read(&[("DECODE_RESERVE_MIB", "130")]).map(|c| c.decode_reserve_mib),
|
||||
Ok(130)
|
||||
);
|
||||
assert_eq!(read(&[("ADAPT_EVERY", "8")]).map(|c| c.adapt_every), Ok(8));
|
||||
assert_eq!(
|
||||
read(&[("ADAPT_SWAPS", "4096")]).map(|c| c.adapt_swaps),
|
||||
Ok(4096)
|
||||
);
|
||||
for key in [
|
||||
"VRAM_RESERVE_MIB",
|
||||
"DECODE_RESERVE_MIB",
|
||||
"ADAPT_EVERY",
|
||||
"ADAPT_SWAPS",
|
||||
"SEED_MAX_ROWS",
|
||||
] {
|
||||
for value in ["-1", "x", "", " 5", "1.5"] {
|
||||
assert!(read(&[(key, value)]).is_err(), "{key}={value:?}");
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
read(&[("ADAPT_SWAPS", "4097")]).is_err(),
|
||||
"validate refuses more than 4096 swaps"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pcie_share_is_a_fraction_and_marks_itself_as_set() {
|
||||
let config = read(&[("PCIE_FRAC", "0.3")]).unwrap();
|
||||
assert_eq!((config.pcie_frac, config.pcie_frac_set), (0.3, true));
|
||||
assert!(!read(&[]).unwrap().pcie_frac_set);
|
||||
for value in ["1.5", "-0.1", "NaN", "inf", "x", ""] {
|
||||
assert!(read(&[("PCIE_FRAC", value)]).is_err(), "{value:?}");
|
||||
}
|
||||
for (value, want) in [
|
||||
("auto", PcieMode::Auto),
|
||||
("dma", PcieMode::Dma),
|
||||
("kernel", PcieMode::Kernel),
|
||||
] {
|
||||
assert_eq!(
|
||||
read(&[("PCIE_MODE", value)]).map(|c| c.pcie_mode),
|
||||
Ok(want),
|
||||
"{value:?}"
|
||||
);
|
||||
}
|
||||
for value in ["Dma", "", "copy"] {
|
||||
assert!(read(&[("PCIE_MODE", value)]).is_err(), "{value:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_seed_is_a_word_or_a_swap_count_inside_the_bank() {
|
||||
let on = super::super::original_serve_seed::DEFAULT_SEED_SWAPS;
|
||||
for (value, want) in [
|
||||
("off", 0),
|
||||
("no", 0),
|
||||
("0", 0),
|
||||
("on", on),
|
||||
("yes", on),
|
||||
("100", 100),
|
||||
("24576", 24576),
|
||||
] {
|
||||
assert_eq!(
|
||||
read(&[("SEED", value)]).map(|c| c.seed_swaps),
|
||||
Ok(want),
|
||||
"{value:?}"
|
||||
);
|
||||
}
|
||||
for value in ["x", "", "24577", "-1", "Off"] {
|
||||
assert!(read(&[("SEED", value)]).is_err(), "{value:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_seed_weights_and_the_resident_budget_are_non_negative_numbers() {
|
||||
let config = read(&[
|
||||
("SEED_KEEP", "2.5"),
|
||||
("SEED_USAGE", "0.5"),
|
||||
("SEED_DECAY", "0.25"),
|
||||
("RESIDENT_BUDGET_GIB", "12.5"),
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
(
|
||||
config.seed_keep,
|
||||
config.seed_usage,
|
||||
config.seed_decay,
|
||||
config.resident_budget_gib
|
||||
),
|
||||
(2.5, 0.5, 0.25, 12.5)
|
||||
);
|
||||
for (key, value) in [
|
||||
("SEED_KEEP", "-1"),
|
||||
("SEED_USAGE", "-1"),
|
||||
("SEED_DECAY", "1.5"),
|
||||
("SEED_DECAY", "-0.1"),
|
||||
("RESIDENT_BUDGET_GIB", "-1"),
|
||||
("SEED_KEEP", "x"),
|
||||
] {
|
||||
assert!(read(&[(key, value)]).is_err(), "{key}={value:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_profile_path_is_kept_as_set_without_a_check() {
|
||||
assert_eq!(read(&[]).unwrap().profile, None);
|
||||
assert_eq!(
|
||||
read(&[("EXPERT_PROFILE", "/p/x.strp")]).unwrap().profile,
|
||||
Some(PathBuf::from("/p/x.strp"))
|
||||
);
|
||||
assert_eq!(
|
||||
read(&[("EXPERT_PROFILE", "")]).unwrap().profile,
|
||||
Some(PathBuf::from(""))
|
||||
);
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
//! read the captured namespace through [`Settings::env`] where they always did. Every field keeps the default, the
|
||||
//! spelling and the fallback of the call site it replaced; the tests of each topic pin them with a `MapEnv`.
|
||||
mod decode;
|
||||
mod memory;
|
||||
mod parse;
|
||||
mod serve;
|
||||
mod source;
|
||||
@@ -15,11 +16,14 @@ mod capture_tests;
|
||||
#[cfg(test)]
|
||||
mod decode_tests;
|
||||
#[cfg(test)]
|
||||
mod memory_tests;
|
||||
#[cfg(test)]
|
||||
mod serve_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use decode::DRAFT_AFTER_COMMIT_ON;
|
||||
pub(crate) use decode::DecodeSettings;
|
||||
pub(crate) use memory::MemorySettings;
|
||||
#[cfg(test)]
|
||||
pub(crate) use parse::text_is_one_of;
|
||||
pub(crate) use serve::ServeSettings;
|
||||
@@ -34,6 +38,7 @@ pub(crate) struct Settings {
|
||||
env: MapEnv,
|
||||
pub(crate) serve: ServeSettings,
|
||||
pub(crate) decode: DecodeSettings,
|
||||
pub(crate) memory: MemorySettings,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
@@ -43,6 +48,7 @@ impl Settings {
|
||||
Self {
|
||||
serve: ServeSettings::from_source(&env),
|
||||
decode: DecodeSettings::from_source(&env),
|
||||
memory: MemorySettings::from_source(&env),
|
||||
env,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
//! Settings of the memory and expert-tier path: the VRAM expert tier's adaptive knobs, the CPU expert pool and the
|
||||
//! per-layer cache sizing.
|
||||
use super::parse::{equals, number, on_unless_off, on_unless_zero, one_of, trimmed_number};
|
||||
use super::source::EnvSource;
|
||||
use std::ffi::OsString;
|
||||
|
||||
/// What the operator chose for the expert tiers.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct MemorySettings {
|
||||
/// `LELEKA_ROUTE_TAIL_SKIP`: a trimmed `u32`; the caller keeps it only inside `1..TOP_K`.
|
||||
pub(crate) route_tail_skip: Option<u32>,
|
||||
/// `LELEKA_ROUTE_TAIL_SKIP_MAX_CACHED`: a trimmed `f64` (no finiteness check), 90.0 when unset or malformed.
|
||||
pub(crate) route_tail_skip_max_cached: f64,
|
||||
/// `LELEKA_SPAN_CACHE`: on unless `off` or `0`.
|
||||
pub(crate) span_cache: bool,
|
||||
/// `LELEKA_REFILL_DEFER` is `on` or `1` (EXPERIMENTAL, off by default).
|
||||
pub(crate) refill_defer: bool,
|
||||
/// `LELEKA_SERVE_SWAP_WAIT`: on unless `off` or `0`.
|
||||
pub(crate) swap_wait: bool,
|
||||
/// `LELEKA_PCIE_LOCKED_ONLY` is `on` or `1`.
|
||||
pub(crate) pcie_locked_only: bool,
|
||||
/// `LELEKA_PCIE_AUTO=lag`.
|
||||
pub(crate) pcie_auto_lag: bool,
|
||||
/// `LELEKA_PCIE_AUTO=on`.
|
||||
pub(crate) pcie_auto_on: bool,
|
||||
/// `LELEKA_ADAPT_MIN_COUNT`: finite `f32`, 2.0 otherwise.
|
||||
pub(crate) adapt_min_count: f32,
|
||||
/// `LELEKA_ADAPT_MARGIN`: finite `f32`, 1.5 otherwise.
|
||||
pub(crate) adapt_margin: f32,
|
||||
/// `LELEKA_ADAPT_DECAY`, as set: the range check and its log line stay with the tier that logs them.
|
||||
pub(crate) adapt_decay: Option<String>,
|
||||
/// `LELEKA_ORIGINAL_SERVE_DUMP_ROUTING`: the file the routing is dumped to.
|
||||
pub(crate) dump_routing: Option<OsString>,
|
||||
/// `LELEKA_ORIGINAL_SERVE_PER_LAYER_SIZED`: on unless exactly `0`.
|
||||
pub(crate) per_layer_sized: bool,
|
||||
/// `STRATA_TEST_CACHE_FAIL`: how many expert cache opens fail on purpose (an `i32`, 0 otherwise).
|
||||
pub(crate) test_cache_fail: i32,
|
||||
/// `LELEKA_CPU_POOL`, as set (`legacy`, `crosscheck`, anything else is the direct pool).
|
||||
pub(crate) cpu_pool: Option<String>,
|
||||
/// `LELEKA_CPU_POOL_FENCE`: one fence per group unless the value is exactly `job`.
|
||||
pub(crate) cpu_pool_group_fence: bool,
|
||||
/// `LELEKA_CPU_POOL_LOOKAHEAD=on`.
|
||||
pub(crate) cpu_pool_lookahead: bool,
|
||||
/// `LELEKA_OPEN_CACHE=on`.
|
||||
pub(crate) open_cache: bool,
|
||||
/// `LELEKA_CPU_PREFETCH`, as set (parsed by the prefetch policy).
|
||||
pub(crate) cpu_prefetch: Option<String>,
|
||||
/// `LELEKA_CPU_ADMIT_EAGER`: on unless `off` or `0`.
|
||||
pub(crate) cpu_admit_eager: bool,
|
||||
/// `LELEKA_CPU_PREFETCH_OFF_FILE`: while this file exists the page-touch pass is skipped.
|
||||
pub(crate) cpu_prefetch_off_file: Option<OsString>,
|
||||
}
|
||||
|
||||
impl MemorySettings {
|
||||
pub(super) fn from_source(env: &impl EnvSource) -> Self {
|
||||
let tuning = |key: &str, default: f32| {
|
||||
number::<f32>(env, key)
|
||||
.filter(|v| v.is_finite())
|
||||
.unwrap_or(default)
|
||||
};
|
||||
Self {
|
||||
route_tail_skip: trimmed_number::<u32>(env, "LELEKA_ROUTE_TAIL_SKIP"),
|
||||
route_tail_skip_max_cached: trimmed_number::<f64>(
|
||||
env,
|
||||
"LELEKA_ROUTE_TAIL_SKIP_MAX_CACHED",
|
||||
)
|
||||
.unwrap_or(90.0),
|
||||
span_cache: on_unless_off(env, "LELEKA_SPAN_CACHE"),
|
||||
refill_defer: one_of(env, "LELEKA_REFILL_DEFER", &["on", "1"]),
|
||||
swap_wait: on_unless_off(env, "LELEKA_SERVE_SWAP_WAIT"),
|
||||
pcie_locked_only: one_of(env, "LELEKA_PCIE_LOCKED_ONLY", &["on", "1"]),
|
||||
pcie_auto_lag: equals(env, "LELEKA_PCIE_AUTO", "lag"),
|
||||
pcie_auto_on: equals(env, "LELEKA_PCIE_AUTO", "on"),
|
||||
adapt_min_count: tuning("LELEKA_ADAPT_MIN_COUNT", 2.0),
|
||||
adapt_margin: tuning("LELEKA_ADAPT_MARGIN", 1.5),
|
||||
adapt_decay: env.get("LELEKA_ADAPT_DECAY"),
|
||||
dump_routing: env.get_os("LELEKA_ORIGINAL_SERVE_DUMP_ROUTING"),
|
||||
per_layer_sized: on_unless_zero(env, "LELEKA_ORIGINAL_SERVE_PER_LAYER_SIZED"),
|
||||
test_cache_fail: number::<i32>(env, "STRATA_TEST_CACHE_FAIL").unwrap_or(0),
|
||||
cpu_pool: env.get("LELEKA_CPU_POOL"),
|
||||
cpu_pool_group_fence: !equals(env, "LELEKA_CPU_POOL_FENCE", "job"),
|
||||
cpu_pool_lookahead: equals(env, "LELEKA_CPU_POOL_LOOKAHEAD", "on"),
|
||||
open_cache: equals(env, "LELEKA_OPEN_CACHE", "on"),
|
||||
cpu_prefetch: env.get("LELEKA_CPU_PREFETCH"),
|
||||
cpu_admit_eager: on_unless_off(env, "LELEKA_CPU_ADMIT_EAGER"),
|
||||
cpu_prefetch_off_file: env.get_os("LELEKA_CPU_PREFETCH_OFF_FILE"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
//! Golden cases of the memory topic: each row is what the call site this field replaced returned for that value.
|
||||
use super::*;
|
||||
|
||||
fn memory(pairs: &[(&str, &str)]) -> MemorySettings {
|
||||
MemorySettings::from_source(&MapEnv::of(pairs))
|
||||
}
|
||||
|
||||
fn rows(key: &str, field: fn(&MemorySettings) -> bool, unset: bool, values: &[(&str, bool)]) {
|
||||
assert_eq!(field(&memory(&[])), unset, "{key} unset");
|
||||
for (value, want) in values {
|
||||
assert_eq!(field(&memory(&[(key, value)])), *want, "{key}={value:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn span_cache_and_swap_wait_are_on_unless_off_or_zero() {
|
||||
let spellings = [
|
||||
("off", false),
|
||||
("0", false),
|
||||
("on", true),
|
||||
("1", true),
|
||||
("OFF", true),
|
||||
("", true),
|
||||
];
|
||||
rows("LELEKA_SPAN_CACHE", |m| m.span_cache, true, &spellings);
|
||||
rows("LELEKA_SERVE_SWAP_WAIT", |m| m.swap_wait, true, &spellings);
|
||||
rows(
|
||||
"LELEKA_CPU_ADMIT_EAGER",
|
||||
|m| m.cpu_admit_eager,
|
||||
true,
|
||||
&spellings,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refill_defer_and_locked_only_are_on_or_one() {
|
||||
let spellings = [
|
||||
("on", true),
|
||||
("1", true),
|
||||
("off", false),
|
||||
("0", false),
|
||||
("yes", false),
|
||||
("", false),
|
||||
];
|
||||
rows("LELEKA_REFILL_DEFER", |m| m.refill_defer, false, &spellings);
|
||||
rows(
|
||||
"LELEKA_PCIE_LOCKED_ONLY",
|
||||
|m| m.pcie_locked_only,
|
||||
false,
|
||||
&spellings,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pcie_auto_modes_are_exact_words() {
|
||||
rows(
|
||||
"LELEKA_PCIE_AUTO",
|
||||
|m| m.pcie_auto_lag,
|
||||
false,
|
||||
&[("lag", true), ("on", false), ("LAG", false), ("", false)],
|
||||
);
|
||||
rows(
|
||||
"LELEKA_PCIE_AUTO",
|
||||
|m| m.pcie_auto_on,
|
||||
false,
|
||||
&[("on", true), ("lag", false), ("1", false), ("", false)],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pool_switches_are_exact_words() {
|
||||
rows(
|
||||
"LELEKA_CPU_POOL_FENCE",
|
||||
|m| m.cpu_pool_group_fence,
|
||||
true,
|
||||
&[("job", false), ("group", true), ("JOB", true), ("", true)],
|
||||
);
|
||||
rows(
|
||||
"LELEKA_CPU_POOL_LOOKAHEAD",
|
||||
|m| m.cpu_pool_lookahead,
|
||||
false,
|
||||
&[("on", true), ("1", false), ("off", false)],
|
||||
);
|
||||
rows(
|
||||
"LELEKA_OPEN_CACHE",
|
||||
|m| m.open_cache,
|
||||
false,
|
||||
&[("on", true), ("1", false), ("off", false)],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn per_layer_sizing_is_on_unless_exactly_zero() {
|
||||
let key = "LELEKA_ORIGINAL_SERVE_PER_LAYER_SIZED";
|
||||
rows(
|
||||
key,
|
||||
|m| m.per_layer_sized,
|
||||
true,
|
||||
&[
|
||||
("0", false),
|
||||
("off", true),
|
||||
("1", true),
|
||||
("", true),
|
||||
("00", true),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_adaptive_tuning_numbers_are_finite_f32_or_the_default() {
|
||||
let (count, margin) = ("LELEKA_ADAPT_MIN_COUNT", "LELEKA_ADAPT_MARGIN");
|
||||
assert_eq!(
|
||||
(memory(&[]).adapt_min_count, memory(&[]).adapt_margin),
|
||||
(2.0, 1.5)
|
||||
);
|
||||
for (value, want) in [
|
||||
("3", 3.0),
|
||||
("0.5", 0.5),
|
||||
("-1", -1.0),
|
||||
("inf", 2.0),
|
||||
("NaN", 2.0),
|
||||
("x", 2.0),
|
||||
(" 3", 2.0),
|
||||
("", 2.0),
|
||||
] {
|
||||
assert_eq!(
|
||||
memory(&[(count, value)]).adapt_min_count,
|
||||
want,
|
||||
"{count}={value:?}"
|
||||
);
|
||||
}
|
||||
for (value, want) in [("2", 2.0), ("inf", 1.5), ("x", 1.5), ("3 ", 1.5)] {
|
||||
assert_eq!(
|
||||
memory(&[(margin, value)]).adapt_margin,
|
||||
want,
|
||||
"{margin}={value:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_adaptive_decay_is_kept_as_set_for_its_range_check() {
|
||||
assert_eq!(memory(&[]).adapt_decay, None);
|
||||
assert_eq!(
|
||||
memory(&[("LELEKA_ADAPT_DECAY", " 0.5 ")])
|
||||
.adapt_decay
|
||||
.as_deref(),
|
||||
Some(" 0.5 ")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_tail_skip_numbers_are_trimmed() {
|
||||
assert_eq!(memory(&[]).route_tail_skip, None);
|
||||
for (value, want) in [
|
||||
("7", Some(7)),
|
||||
(" 7 ", Some(7)),
|
||||
("0", Some(0)),
|
||||
("-1", None),
|
||||
("7.0", None),
|
||||
("x", None),
|
||||
("", None),
|
||||
] {
|
||||
assert_eq!(
|
||||
memory(&[("LELEKA_ROUTE_TAIL_SKIP", value)]).route_tail_skip,
|
||||
want,
|
||||
"{value:?}"
|
||||
);
|
||||
}
|
||||
let key = "LELEKA_ROUTE_TAIL_SKIP_MAX_CACHED";
|
||||
assert_eq!(memory(&[]).route_tail_skip_max_cached, 90.0);
|
||||
for (value, want) in [
|
||||
("50", 50.0),
|
||||
(" 50 ", 50.0),
|
||||
("0", 0.0),
|
||||
("-5", -5.0),
|
||||
("x", 90.0),
|
||||
("", 90.0),
|
||||
] {
|
||||
assert_eq!(
|
||||
memory(&[(key, value)]).route_tail_skip_max_cached,
|
||||
want,
|
||||
"{value:?}"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
memory(&[(key, "NaN")]).route_tail_skip_max_cached.is_nan(),
|
||||
"a NaN limit is not replaced"
|
||||
);
|
||||
assert!(
|
||||
memory(&[(key, "inf")])
|
||||
.route_tail_skip_max_cached
|
||||
.is_infinite()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_cache_fail_count_is_an_untrimmed_i32() {
|
||||
let key = "STRATA_TEST_CACHE_FAIL";
|
||||
assert_eq!(memory(&[]).test_cache_fail, 0);
|
||||
for (value, want) in [
|
||||
("2", 2),
|
||||
("-1", -1),
|
||||
("x", 0),
|
||||
(" 2", 0),
|
||||
("2.5", 0),
|
||||
("", 0),
|
||||
] {
|
||||
assert_eq!(memory(&[(key, value)]).test_cache_fail, want, "{value:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pool_words_and_paths_are_kept_as_set() {
|
||||
let none = memory(&[]);
|
||||
assert_eq!(
|
||||
(
|
||||
none.cpu_pool,
|
||||
none.cpu_prefetch,
|
||||
none.dump_routing,
|
||||
none.cpu_prefetch_off_file
|
||||
),
|
||||
(None, None, None, None)
|
||||
);
|
||||
let set = memory(&[
|
||||
("LELEKA_CPU_POOL", "legacy"),
|
||||
("LELEKA_CPU_PREFETCH", "auto"),
|
||||
("LELEKA_ORIGINAL_SERVE_DUMP_ROUTING", "/tmp/r"),
|
||||
("LELEKA_CPU_PREFETCH_OFF_FILE", "/tmp/off"),
|
||||
]);
|
||||
assert_eq!(set.cpu_pool.as_deref(), Some("legacy"));
|
||||
assert_eq!(set.cpu_prefetch.as_deref(), Some("auto"));
|
||||
assert_eq!(set.dump_routing, Some(std::ffi::OsString::from("/tmp/r")));
|
||||
assert_eq!(
|
||||
set.cpu_prefetch_off_file,
|
||||
Some(std::ffi::OsString::from("/tmp/off"))
|
||||
);
|
||||
}
|
||||
@@ -13,6 +13,16 @@ pub(super) fn one_of(env: &impl EnvSource, key: &str, wants: &[&str]) -> bool {
|
||||
env.get(key).is_some_and(|v| wants.contains(&v.as_str()))
|
||||
}
|
||||
|
||||
/// `std::env::var(key).map_or(true, |v| v != "0")`: on unless the value is exactly `0`.
|
||||
pub(super) fn on_unless_zero(env: &impl EnvSource, key: &str) -> bool {
|
||||
env.get(key).is_none_or(|v| v != "0")
|
||||
}
|
||||
|
||||
/// `std::env::var(key).ok().and_then(|v| v.trim().parse::<T>().ok())`.
|
||||
pub(super) fn trimmed_number<T: std::str::FromStr>(env: &impl EnvSource, key: &str) -> Option<T> {
|
||||
env.get(key).and_then(|v| v.trim().parse().ok())
|
||||
}
|
||||
|
||||
/// `!std::env::var(key).is_ok_and(|v| v == "off" || v == "0")`: on unless the value is exactly `off` or `0`.
|
||||
pub(super) fn on_unless_off(env: &impl EnvSource, key: &str) -> bool {
|
||||
!one_of(env, key, &["off", "0"])
|
||||
|
||||
Reference in New Issue
Block a user