feat(serve): --dump-routing for the expert-cache profile (LELEKA_ORIGINAL_SERVE_DUMP_ROUTING)
generate.cpp's --dump-routing writes every routed expert so tools/make_profile.py can rank the (layer, expert) pairs a workload uses; the expert-cache profile then decides what starts resident. The port's tier writes the same records (layer, k, ids, weights) for every decode window row it serves (prompt routing is not traced). Off unless the variable names a file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
7e930efc91
commit
973e0d2fdf
@@ -343,10 +343,41 @@ impl ResidencyStats {
|
||||
pub(crate) struct ResidencyState {
|
||||
usage: Vec<f32>,
|
||||
pub(crate) stats: ResidencyStats,
|
||||
/// generate.cpp `--dump-routing FILE` (`LELEKA_ORIGINAL_SERVE_DUMP_ROUTING`): every decode window row's routed
|
||||
/// experts, as `tools/make_profile.py` reads them (layer i32, k i32, k ids i32, k weights f32).
|
||||
routing: Option<std::io::BufWriter<std::fs::File>>,
|
||||
}
|
||||
impl ResidencyState {
|
||||
pub(crate) fn new(config: &ExpertTierConfig) -> Self {
|
||||
Self { usage: if config.adaptive() { vec![0.0; LAYERS * EXPERTS] } else { Vec::new() }, stats: ResidencyStats::default() }
|
||||
let routing = std::env::var_os("LELEKA_ORIGINAL_SERVE_DUMP_ROUTING")
|
||||
.and_then(|path| std::fs::File::create(path).ok())
|
||||
.map(std::io::BufWriter::new);
|
||||
Self {
|
||||
usage: if config.adaptive() { vec![0.0; LAYERS * EXPERTS] } else { Vec::new() },
|
||||
stats: ResidencyStats::default(),
|
||||
routing,
|
||||
}
|
||||
}
|
||||
/// Write the buffered routing records out (end of a request).
|
||||
pub(crate) fn flush_routing(&mut self) {
|
||||
if let Some(routing) = self.routing.as_mut() {
|
||||
let _ = std::io::Write::flush(routing);
|
||||
}
|
||||
}
|
||||
fn record_routing(&mut self, layer: usize, ids: &[i32], weights: &[f32]) {
|
||||
let Some(routing) = self.routing.as_mut() else { return };
|
||||
if ids.len() != weights.len() {
|
||||
return;
|
||||
}
|
||||
let mut record = Vec::with_capacity(8 + 8 * TOP_K);
|
||||
for (row, w) in ids.chunks_exact(TOP_K).zip(weights.chunks_exact(TOP_K)) {
|
||||
record.clear();
|
||||
record.extend_from_slice(&(layer as i32).to_le_bytes());
|
||||
record.extend_from_slice(&(TOP_K as i32).to_le_bytes());
|
||||
row.iter().for_each(|e| record.extend_from_slice(&e.to_le_bytes()));
|
||||
w.iter().for_each(|x| record.extend_from_slice(&x.to_le_bytes()));
|
||||
let _ = std::io::Write::write_all(routing, &record);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -375,6 +406,7 @@ impl<'ctx> OriginalServePool<'ctx> for GpuExpertPool<'_, '_, 'ctx> {
|
||||
return Err(Error::InvalidDimension);
|
||||
}
|
||||
let mut clock = super::original_serve_pool::window_trace::now();
|
||||
self.state.record_routing(layer, rows.ids, rows.route_weights);
|
||||
if !self.state.usage.is_empty() {
|
||||
for &e in rows.ids {
|
||||
if (0..EXPERTS as i32).contains(&e) {
|
||||
|
||||
@@ -1010,6 +1010,7 @@ impl<'a, 'ctx> RequestOwners for OriginalServeStack<'a, 'ctx> {
|
||||
profile::add("mtp.instantiate", instantiate_ms);
|
||||
profile::add("prefill.phase", self.timing.0);
|
||||
profile::report("request");
|
||||
self.residency.flush_routing();
|
||||
result
|
||||
}
|
||||
fn finish(&mut self, ended: std::result::Result<(), ServeError>) {
|
||||
|
||||
Reference in New Issue
Block a user