phase 11: GPU-resident KV cache
- GpuKVCache: pre-allocated GPU buffers, D2D copy append at offset - Per-head strided layout [num_kv_heads, max_seq_len, head_dim] - Fixed critical bug: seq_len must advance AFTER all layers write (not inside the loop per-layer) - GpuBuffer::copy_from_device_at for offset-based D2D copy - Tensor::from_storage constructor for wrapping raw GPU buffers - Exported Storage and Dims from xserv-tensor Correctness: GPU KV cache vs CPU KV cache = 50/50 bit-identical Performance: ~neutral (KV cache was never the main bottleneck — reshape/merge/transpose CPU round-trips dominate for Qwen3-8B) TTFT: 122ms, TBT: 142ms, 7.0 tok/s (marginal change from 7.3) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
118
crates/xserv-model/src/kv_cache.rs
Normal file
118
crates/xserv-model/src/kv_cache.rs
Normal file
@@ -0,0 +1,118 @@
|
||||
use xserv_cuda::GpuBuffer;
|
||||
use xserv_tensor::{DType, Device, Tensor};
|
||||
use crate::config::ModelConfig;
|
||||
|
||||
/// GPU-resident KV cache. Pre-allocates max_seq_len on GPU,
|
||||
/// appends new K/V via D2D copy at offset (no CPU round-trip).
|
||||
pub struct GpuKVCache {
|
||||
// Per layer: contiguous GPU buffer for K and V
|
||||
// Layout: [num_kv_heads, max_seq_len, head_dim] — contiguous per head
|
||||
k_bufs: Vec<GpuBuffer>,
|
||||
v_bufs: Vec<GpuBuffer>,
|
||||
seq_len: usize,
|
||||
max_seq_len: usize,
|
||||
num_kv_heads: usize,
|
||||
head_dim: usize,
|
||||
elem_size: usize,
|
||||
dtype: DType,
|
||||
}
|
||||
|
||||
impl GpuKVCache {
|
||||
pub fn new(config: &ModelConfig, max_seq_len: usize, dtype: DType) -> Self {
|
||||
let num_layers = config.num_layers();
|
||||
let num_kv_heads = config.num_kv_heads();
|
||||
let head_dim = config.head_dim();
|
||||
let elem_size = dtype.size_bytes();
|
||||
let buf_size = num_kv_heads * max_seq_len * head_dim * elem_size;
|
||||
|
||||
let mut k_bufs = Vec::with_capacity(num_layers);
|
||||
let mut v_bufs = Vec::with_capacity(num_layers);
|
||||
for _ in 0..num_layers {
|
||||
let mut k = GpuBuffer::alloc(buf_size).expect("alloc KV cache K");
|
||||
let mut v = GpuBuffer::alloc(buf_size).expect("alloc KV cache V");
|
||||
k.zero().unwrap();
|
||||
v.zero().unwrap();
|
||||
k_bufs.push(k);
|
||||
v_bufs.push(v);
|
||||
}
|
||||
|
||||
Self { k_bufs, v_bufs, seq_len: 0, max_seq_len, num_kv_heads, head_dim, elem_size, dtype }
|
||||
}
|
||||
|
||||
pub fn seq_len(&self) -> usize { self.seq_len }
|
||||
pub fn max_seq_len(&self) -> usize { self.max_seq_len }
|
||||
|
||||
/// Append new K/V tensors for a given layer.
|
||||
/// k_new, v_new: [1, num_kv_heads, new_tokens, head_dim] on GPU, contiguous.
|
||||
/// `write_pos` is the sequence position to write at (caller manages this).
|
||||
pub fn append(&mut self, layer: usize, k_new: &Tensor, v_new: &Tensor, new_tokens: usize, write_pos: usize) {
|
||||
assert!(write_pos + new_tokens <= self.max_seq_len, "KV cache overflow");
|
||||
let es = self.elem_size;
|
||||
let hd = self.head_dim;
|
||||
let max_s = self.max_seq_len;
|
||||
let nh = self.num_kv_heads;
|
||||
|
||||
let k_src = k_new.storage().gpu_buffer();
|
||||
let v_src = v_new.storage().gpu_buffer();
|
||||
|
||||
for h in 0..nh {
|
||||
let src_off = h * new_tokens * hd * es;
|
||||
let dst_off = (h * max_s + write_pos) * hd * es;
|
||||
let count = new_tokens * hd * es;
|
||||
self.k_bufs[layer].copy_from_device_at(k_src, src_off, dst_off, count).unwrap();
|
||||
self.v_bufs[layer].copy_from_device_at(v_src, src_off, dst_off, count).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn advance_seq_len(&mut self, new_tokens: usize) {
|
||||
self.seq_len += new_tokens;
|
||||
}
|
||||
|
||||
/// Get K/V cache tensors for a layer up to `seq_len` tokens: [1, num_kv_heads, seq_len, head_dim]
|
||||
pub fn get_kv(&self, layer: usize) -> (Tensor, Tensor) {
|
||||
let sl = self.seq_len;
|
||||
self.get_kv_len(layer, sl)
|
||||
}
|
||||
|
||||
pub fn get_kv_len(&self, layer: usize, sl: usize) -> (Tensor, Tensor) {
|
||||
let hd = self.head_dim;
|
||||
let nh = self.num_kv_heads;
|
||||
let es = self.elem_size;
|
||||
let max_s = self.max_seq_len;
|
||||
|
||||
// Allocate output tensors [1, nh, sl, hd]
|
||||
let out_size = nh * sl * hd * es;
|
||||
let mut k_out = GpuBuffer::alloc(out_size).expect("alloc k_out");
|
||||
let mut v_out = GpuBuffer::alloc(out_size).expect("alloc v_out");
|
||||
|
||||
// Copy each head's valid portion
|
||||
for h in 0..nh {
|
||||
let src_off = (h * max_s) * hd * es;
|
||||
let dst_off = (h * sl) * hd * es;
|
||||
let count = sl * hd * es;
|
||||
k_out.copy_from_device_at(&self.k_bufs[layer], src_off, dst_off, count).unwrap();
|
||||
v_out.copy_from_device_at(&self.v_bufs[layer], src_off, dst_off, count).unwrap();
|
||||
}
|
||||
|
||||
let shape = &[1usize, nh, sl, hd];
|
||||
let k = unsafe { tensor_from_gpu_buffer(k_out, shape, self.dtype) };
|
||||
let v = unsafe { tensor_from_gpu_buffer(v_out, shape, self.dtype) };
|
||||
(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a Tensor from a GpuBuffer (takes ownership).
|
||||
unsafe fn tensor_from_gpu_buffer(buf: GpuBuffer, shape: &[usize], dtype: DType) -> Tensor {
|
||||
use xserv_tensor::storage::Storage;
|
||||
use xserv_tensor::shape::contiguous_strides;
|
||||
use smallvec::SmallVec;
|
||||
|
||||
let storage = Storage::cuda(buf);
|
||||
Tensor::from_storage(
|
||||
storage,
|
||||
SmallVec::from_slice(shape),
|
||||
contiguous_strides(shape),
|
||||
0,
|
||||
dtype,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user