[LD] VETTA_PROF per-phase forward profiler

Gated (VETTA_PROF=1) timing of embed/prelude/core/coda/final plus a thread-local
expert-time accumulator, to locate decode hotspots without perturbing the normal
path. Used to measure the M2 perf increment.
This commit is contained in:
adm committed 2026-12-01 17:27:07 +01:00
1 parent 4bc268f69c
commit dfa6013bfd
1 file changed
+24 -1
+24 -1
View File
@@ -423,6 +423,7 @@ fn dbg_norm(name: &str, buf: &DeviceBuf, n: usize) {
fn run_experts(
cache: &WeightCache, ip: usize, ids_h: &[i32], mlpin: &DeviceBuf, d: &Dims, h: usize,
) -> Result<Vec<f32>, String> {
let t0 = std::time::Instant::now();
let gu = DeviceBuf::alloc(2 * d.inter * 4)?;
let mut y_part_h = vec![0f32; d.top_k * h];
if std::env::var("VETTA_F16_EXPERT").is_ok() {
@@ -452,6 +453,7 @@ fn run_experts(
ye.d2h(&mut y_part_h[kk * h..(kk + 1) * h])?;
}
}
EXPERT_NS.with(|c| c.set(c.get() + t0.elapsed().as_nanos()));
Ok(y_part_h)
}
@@ -499,7 +501,10 @@ pub fn forward_inkling_with_state(
states: &mut [Option<LayerState>],
) -> Result<DeviceBuf, String> {
let h = d.hidden;
let prof = std::env::var("VETTA_PROF").is_ok();
let tp0 = std::time::Instant::now();
let cur = embed_token(cache, token, h, vocab)?;
let t_embed = tp0.elapsed().as_nanos();
if std::env::var("VETTA_DBG_NORM").is_ok() {
let mut eh = vec![0f32; h]; cur.d2h(&mut eh).unwrap();
let n = eh.iter().map(|v| v * v).sum::<f32>().sqrt();
@@ -507,7 +512,9 @@ pub fn forward_inkling_with_state(
}
let mut blocks: Vec<DeviceBuf> = Vec::new();
// Prelude.
let tp1 = std::time::Instant::now();
for ip in 0..6 { run_layer(cache, d, ip, &cur, &mut blocks, states, max_seq, pos, 0, None)?; }
let t_prelude = tp1.elapsed().as_nanos();
let mut prelude = vec![0f32; h]; cur.d2h(&mut prelude)?;
let n_prelude_blocks = blocks.len();
if std::env::var("VETTA_DBG_NORM").is_ok() {
@@ -515,6 +522,7 @@ pub fn forward_inkling_with_state(
eprintln!("[dbg] after prelude: cur_norm={n:.3} blocks={}", blocks.len());
}
// Core: 3 passes.
let tp2 = std::time::Instant::now();
for p in 0..3 {
let mut bank: Vec<DeviceBuf> = Vec::new();
let bank_out = DeviceBuf::alloc(h * 4)?;
@@ -547,14 +555,29 @@ pub fn forward_inkling_with_state(
}
}
// Coda.
let t_core = tp2.elapsed().as_nanos();
let tp3 = std::time::Instant::now();
for ip in 18..24 { run_layer(cache, d, ip, &cur, &mut blocks, states, max_seq, pos, 0, None)?; }
let t_coda = tp3.elapsed().as_nanos();
if std::env::var("VETTA_DBG_NORM").is_ok() {
let mut ch = vec![0f32; h]; cur.d2h(&mut ch).unwrap();
let n = ch.iter().map(|v| v * v).sum::<f32>().sqrt();
eprintln!("[dbg] after coda: cur_norm={n:.3} blocks={}", blocks.len());
for (i, b) in blocks.iter().enumerate() { let mut bh = vec![0f32; h]; b.d2h(&mut bh).unwrap(); let bn = bh.iter().map(|v| v * v).sum::<f32>().sqrt(); eprintln!("[dbg] block[{i}]_norm={bn:.3}"); }
}
final_logits(cache, &cur, &blocks, h, vocab, d.eps, cap)
let tp4 = std::time::Instant::now();
let out = final_logits(cache, &cur, &blocks, h, vocab, d.eps, cap);
if prof {
let t_final = tp4.elapsed().as_nanos();
let exp = EXPERT_NS.with(|c| c.replace(0));
eprintln!("[PROF] pos={pos} embed={:.2}ms prelude={:.2}ms core={:.2}ms coda={:.2}ms final={:.2}ms experts={:.2}ms total={:.2}ms",
t_embed as f64 / 1e6, t_prelude as f64 / 1e6, t_core as f64 / 1e6, t_coda as f64 / 1e6, t_final as f64 / 1e6, exp as f64 / 1e6, tp0.elapsed().as_nanos() as f64 / 1e6);
}
out
}
thread_local! {
static EXPERT_NS: std::cell::Cell<u128> = const { std::cell::Cell::new(0) };
}
/// Load a full-attn ShortConv static weight tensor [4, C] transposed to the