diff --git a/crates/vetta-infer-ld/src/layer.rs b/crates/vetta-infer-ld/src/layer.rs index baf0c05..90d32e1 100644 --- a/crates/vetta-infer-ld/src/layer.rs +++ b/crates/vetta-infer-ld/src/layer.rs @@ -84,6 +84,7 @@ pub struct WeightCache { experts: RefCell>>, arenas: RefCell>>, ctrl: RefCell>>, + msgq: RefCell>>, } impl WeightCache { @@ -98,6 +99,7 @@ impl WeightCache { experts: RefCell::new(HashMap::new()), arenas: RefCell::new(HashMap::new()), ctrl: RefCell::new(HashMap::new()), + msgq: RefCell::new(HashMap::new()), } } @@ -162,6 +164,17 @@ impl WeightCache { Ok(v) } + /// Stacked cross-pass msg_queries [4, hidden] for core layer ip, memoized. + /// These are STATIC model weights: building them per visit did 2 sync D2H + 1 + /// sync H2D each (72 PCIe stalls/chunk in prefill, 24/token in decode). Cache + /// the device buffer once => zero host round-trips on the hot path. + pub fn msg_queries(&self, ip: usize, hidden: usize) -> Result, String> { + if let Some(v) = self.msgq.borrow().get(&ip) { return Ok(v.clone()); } + let v = Arc::new(load_msg_queries(self, ip, hidden)?); + self.msgq.borrow_mut().insert(ip, v.clone()); + Ok(v) + } + /// Eagerly load the WHOLE model to device: trunk (embed/norms/unembed/output /// query/cross-pass queries), every attention layer (SWA or full), the core /// loop controllers, and all expert ARENAS (the contiguous device-MoE layout). @@ -228,7 +241,7 @@ pub fn load_layer(ar: &SafetensorsArchive, ip: usize, is_swa: bool) -> Result Result { - let zero = |n: usize| -> Result { let b = DeviceBuf::alloc(n * 4)?; b.h2d(&vec![0f32; n])?; Ok(b) }; + let zero = |n: usize| -> Result { let b = DeviceBuf::alloc(n * 4)?; b.zero_async()?; Ok(b) }; // KV cache dtype: F32 by default (the verified CPU-oracle baseline); FP16 via // VETTA_F16_KV=1 matches the strata default (KvDtype::FP16) and its // length-dependent rounding (the F32 cache diverges on near-ties as it grows). @@ -236,12 +249,15 @@ pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result Result { - if f16_kv() { - let b = DeviceBuf::alloc_unpooled(kv_n * 2)?; b.h2d_bytes(&vec![0u8; kv_n * 2])?; Ok(b) - } else { - let b = DeviceBuf::alloc_unpooled(kv_n * 4)?; b.h2d(&vec![0f32; kv_n])?; Ok(b) - } + let b = if f16_kv() { DeviceBuf::alloc_unpooled(kv_n * 2)? } else { DeviceBuf::alloc_unpooled(kv_n * 4)? }; + b.zero_async()?; Ok(b) }; let kv_vec = || -> Result, String> { (0..n_passes.max(1)).map(|_| kv_buf()).collect() }; Ok(LayerState { @@ -883,7 +899,7 @@ pub fn forward_inkling_with_state( } for ip in 6..18 { if p > 0 && std::env::var("VETTA_NO_CTRL").is_err() { - let mq = load_msg_queries(cache, ip, h)?; + let mq = cache.msg_queries(ip, h)?; let n_bank = bank.len(); let blk = DeviceBuf::alloc(n_bank.max(1) * h * 4)?; let mut bh = vec![0f32; n_bank.max(1) * h]; @@ -996,7 +1012,7 @@ pub fn forward_batch( } for ip in 6..18 { if p > 0 && !no_ctrl { - let mq = load_msg_queries(cache, ip, h)?; + let mq = cache.msg_queries(ip, h)?; let msgs_buf = DeviceBuf::alloc(bu * 4 * h * 4)?; vetta_infer_hip::attn_res_mix(&bank_buf, &bank_out, &mq, &msgs_buf, n_bank as i32, b, 4, h as i32, d.eps)?; let cw = cache.controllers(ip)?;