cpu-service2: admit every routed expert at CPU pool open (same open_group admission and checks; LELEKA_CPU_ADMIT_EAGER=off keeps first-use admission, 50 us per expert on the decode path); bench: first-use admission scan
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
b693ef491e
commit
82f71834bc
@@ -277,6 +277,21 @@ fn same_spans(a: &[ResidentExpertBankSpan; 3], b: &[ResidentExpertBankSpan; 3])
|
||||
}
|
||||
|
||||
impl DirectTier {
|
||||
/// Admit every routed expert of every layer now (stack open), through the same `open_group` admission the decode runs at an
|
||||
/// expert's first CPU or PCIe use (source scope, descriptor checks, the full FP16-scale scan of `mapped_span`): the checks are
|
||||
/// unchanged, they only stop costing the decode its first use of each expert (~50 us each on the 5090 host, 1.3 ms per round
|
||||
/// in the first request of a fresh process). Returns how many were admitted. `LELEKA_CPU_ADMIT_EAGER=off`: first use, as before.
|
||||
fn admit_all(&mut self, binding: &ModelBinding) -> Result<usize> {
|
||||
let all: Vec<u32> = (0..EXPERTS as u32).collect();
|
||||
let mut admitted = 0usize;
|
||||
for layer in 0..48u32 {
|
||||
for chunk in all.chunks(64) {
|
||||
admitted += self.open_group(binding, layer, chunk, |_, _| Ok(()))?.iter().filter(|(_, _, ready)| !*ready).count();
|
||||
}
|
||||
}
|
||||
Ok(admitted)
|
||||
}
|
||||
|
||||
fn start_lookahead(&mut self, binding: &ModelBinding) -> Result<RouterLookahead> {
|
||||
let mut routers: Vec<Box<[u16]>> = Vec::new();
|
||||
let mut layouts: Vec<LayerLayout> = Vec::new();
|
||||
@@ -457,8 +472,14 @@ impl<'a> CpuExpertPool<'a> {
|
||||
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");
|
||||
Some(DirectTier { pool, held, routes, group_fence, lookahead_enabled, lookahead: None, next_resident: vec![false; EXPERTS as usize], opened_cache, open_cache_on,
|
||||
prefetch: super::prefetch_policy::PrefetchPolicy::new(super::prefetch_policy::Mode::from_value(std::env::var("LELEKA_CPU_PREFETCH").ok().as_deref(), 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::prefetch_policy::PrefetchPolicy::new(super::prefetch_policy::Mode::from_value(std::env::var("LELEKA_CPU_PREFETCH").ok().as_deref(), cfg!(windows))) };
|
||||
if admit_eager() {
|
||||
let started = std::time::Instant::now();
|
||||
let n = tier.admit_all(binding)?;
|
||||
eprintln!("leleka-serve cpu expert pool: {n} routed experts admitted at open in {:.2} s (LELEKA_CPU_ADMIT_EAGER=off admits at first use)", started.elapsed().as_secs_f64());
|
||||
}
|
||||
Some(tier)
|
||||
}
|
||||
};
|
||||
let legacy = match path {
|
||||
@@ -793,6 +814,12 @@ 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.
|
||||
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")))
|
||||
}
|
||||
|
||||
fn sink_result(written: leleka_cuda_driver::Result<()>, failure: Option<Error>) -> Result<()> {
|
||||
match (written, failure) {
|
||||
(Ok(()), None) => Ok(()),
|
||||
|
||||
@@ -102,5 +102,43 @@ fn cpu_service_bench() {
|
||||
}
|
||||
eprintln!("{line}");
|
||||
}
|
||||
// First use of an expert in the serve path: the single-threaded admission scan (every block's FP16 scale, as
|
||||
// `encoded_finite::validate_row`) then the compute; the scan written with an early-exit branch per block (main) and branch-free.
|
||||
for branch_free in [false, true] {
|
||||
let mut next = 0usize;
|
||||
let pairs = [(0usize, 0usize)];
|
||||
let start = std::time::Instant::now();
|
||||
let mut t_scan = 0.0f64;
|
||||
for _ in 0..calls {
|
||||
let x = &experts[next % n_experts];
|
||||
next += 1;
|
||||
let t1 = std::time::Instant::now();
|
||||
let mut bad = false;
|
||||
for (m, block) in [(&x.gate, 110usize), (&x.up, 110), (&x.down, 18)] {
|
||||
if branch_free {
|
||||
let mut acc = 0u32;
|
||||
for b in m.chunks_exact(block) {
|
||||
acc |= u32::from(u16::from_le_bytes([b[0], b[1]]) & 0x7c00 == 0x7c00);
|
||||
}
|
||||
bad |= acc != 0;
|
||||
} else {
|
||||
for b in m.chunks_exact(block) {
|
||||
if u16::from_le_bytes([b[0], b[1]]) & 0x7c00 == 0x7c00 {
|
||||
bad = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(!std::hint::black_box(bad));
|
||||
t_scan += t1.elapsed().as_secs_f64();
|
||||
let e = [BorrowedExpert { gate: &x.gate, up: &x.up, down: &x.down, pairs: &pairs }];
|
||||
let group = BorrowedGroup { tokens: &tokens[..H], experts: &e, stride: TOP_K };
|
||||
pool.compute_group(GateUpFormat::Iq3S, DownFormat::Iq4Nl, &group, &mut out[..TOP_K * H]).expect("compute_group");
|
||||
}
|
||||
let _ = pool.take_timing();
|
||||
eprintln!("pass {pass} first use, branch_free={branch_free}: {:.1} us per expert (scan {:.1}, compute the rest)",
|
||||
start.elapsed().as_secs_f64() * 1e6 / calls as f64, t_scan * 1e6 / calls as f64);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user