[Core] Cache static msg_queries in WeightCache; async KV zero-fill
load_msg_queries rebuilt the stacked cross-pass queries [4,hidden] on EVERY core visit: 2 sync D2H + 1 sync H2D of STATIC model weights = 72 PCIe stalls per prefill chunk (720/prompt) and 24 per decode token (audit §3.1). Memoize the 12 device buffers once in WeightCache.msg_queries => zero host round-trips on the hot path. Measured prefill 2048tok 40.9 -> 38.5s. new_state zeroed ~3.75GB KV via host vec![0u8] + blocking h2d_bytes every request (system-RAM alloc + CPU zero + sync PCIe copy, ~0.5-1.5s stall, even on a prefix-cache hit). Replace with async device hipMemsetAsync (zero_async, ~7.5ms GPU): removes the blocking copy while keeping the cache deterministic for the full-byte parity tests (causal attention only reads [0..=pos]).
This commit is contained in:
1 parent
ac1b55425c
commit
844d9e68ee
1 file changed
+24
-8
@@ -84,6 +84,7 @@ pub struct WeightCache {
|
|||||||
experts: RefCell<HashMap<(usize, usize, String), Arc<ExpertMatrix>>>,
|
experts: RefCell<HashMap<(usize, usize, String), Arc<ExpertMatrix>>>,
|
||||||
arenas: RefCell<HashMap<(usize, String), Arc<ExpertArena>>>,
|
arenas: RefCell<HashMap<(usize, String), Arc<ExpertArena>>>,
|
||||||
ctrl: RefCell<HashMap<usize, Arc<ControllerWeights>>>,
|
ctrl: RefCell<HashMap<usize, Arc<ControllerWeights>>>,
|
||||||
|
msgq: RefCell<HashMap<usize, Arc<DeviceBuf>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WeightCache {
|
impl WeightCache {
|
||||||
@@ -98,6 +99,7 @@ impl WeightCache {
|
|||||||
experts: RefCell::new(HashMap::new()),
|
experts: RefCell::new(HashMap::new()),
|
||||||
arenas: RefCell::new(HashMap::new()),
|
arenas: RefCell::new(HashMap::new()),
|
||||||
ctrl: RefCell::new(HashMap::new()),
|
ctrl: RefCell::new(HashMap::new()),
|
||||||
|
msgq: RefCell::new(HashMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +164,17 @@ impl WeightCache {
|
|||||||
Ok(v)
|
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<Arc<DeviceBuf>, 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
|
/// Eagerly load the WHOLE model to device: trunk (embed/norms/unembed/output
|
||||||
/// query/cross-pass queries), every attention layer (SWA or full), the core
|
/// query/cross-pass queries), every attention layer (SWA or full), the core
|
||||||
/// loop controllers, and all expert ARENAS (the contiguous device-MoE layout).
|
/// 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<La
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result<LayerState, String> {
|
pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result<LayerState, String> {
|
||||||
let zero = |n: usize| -> Result<DeviceBuf, String> { let b = DeviceBuf::alloc(n * 4)?; b.h2d(&vec![0f32; n])?; Ok(b) };
|
let zero = |n: usize| -> Result<DeviceBuf, String> { let b = DeviceBuf::alloc(n * 4)?; b.zero_async()?; Ok(b) };
|
||||||
// KV cache dtype: F32 by default (the verified CPU-oracle baseline); FP16 via
|
// 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
|
// 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).
|
// 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<LayerState
|
|||||||
// KV is unpooled: its size scales with max_seq (prompt+max_new), which varies
|
// KV is unpooled: its size scales with max_seq (prompt+max_new), which varies
|
||||||
// per request. Pooling by exact size would retain every distinct KV size and
|
// per request. Pooling by exact size would retain every distinct KV size and
|
||||||
// grow VRAM without bound across a multi-turn chat; free it on request end.
|
// grow VRAM without bound across a multi-turn chat; free it on request end.
|
||||||
|
// Zero-filled with ASYNC device memset (hipMemsetAsync, ~7.5ms GPU for 3.75GB),
|
||||||
|
// NOT a host vec![0]+sync h2d: the latter allocated ~3.75GB system RAM, zeroed
|
||||||
|
// it on CPU and copied it over blocking PCIe every request (~0.5-1.5s stall).
|
||||||
|
// Causal attention only reads [0..=pos] (kv_store overwrites before read), so
|
||||||
|
// the zero is defensive/deterministic rather than required, but keeps the
|
||||||
|
// full-cache byte-parity tests stable across pool reuse.
|
||||||
let kv_buf = || -> Result<DeviceBuf, String> {
|
let kv_buf = || -> Result<DeviceBuf, String> {
|
||||||
if f16_kv() {
|
let b = if f16_kv() { DeviceBuf::alloc_unpooled(kv_n * 2)? } else { DeviceBuf::alloc_unpooled(kv_n * 4)? };
|
||||||
let b = DeviceBuf::alloc_unpooled(kv_n * 2)?; b.h2d_bytes(&vec![0u8; kv_n * 2])?; Ok(b)
|
b.zero_async()?; Ok(b)
|
||||||
} else {
|
|
||||||
let b = DeviceBuf::alloc_unpooled(kv_n * 4)?; b.h2d(&vec![0f32; kv_n])?; Ok(b)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let kv_vec = || -> Result<Vec<DeviceBuf>, String> { (0..n_passes.max(1)).map(|_| kv_buf()).collect() };
|
let kv_vec = || -> Result<Vec<DeviceBuf>, String> { (0..n_passes.max(1)).map(|_| kv_buf()).collect() };
|
||||||
Ok(LayerState {
|
Ok(LayerState {
|
||||||
@@ -883,7 +899,7 @@ pub fn forward_inkling_with_state(
|
|||||||
}
|
}
|
||||||
for ip in 6..18 {
|
for ip in 6..18 {
|
||||||
if p > 0 && std::env::var("VETTA_NO_CTRL").is_err() {
|
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 n_bank = bank.len();
|
||||||
let blk = DeviceBuf::alloc(n_bank.max(1) * h * 4)?;
|
let blk = DeviceBuf::alloc(n_bank.max(1) * h * 4)?;
|
||||||
let mut bh = vec![0f32; n_bank.max(1) * h];
|
let mut bh = vec![0f32; n_bank.max(1) * h];
|
||||||
@@ -996,7 +1012,7 @@ pub fn forward_batch(
|
|||||||
}
|
}
|
||||||
for ip in 6..18 {
|
for ip in 6..18 {
|
||||||
if p > 0 && !no_ctrl {
|
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)?;
|
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)?;
|
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)?;
|
let cw = cache.controllers(ip)?;
|
||||||
|
|||||||
Reference in new issue
Block a user