use super::module::prefixed;
use super::{Linear, Module};
use crate::functional::scaled_dot_product_attention;
use crate::tensor::Tensor;
use crate::Device;
#[derive(Debug, Clone, Default)]
pub struct KvCache {
kv: Option<(Tensor, Tensor)>,
}
impl KvCache {
pub fn new() -> Self {
Self::default()
}
pub fn len(&self) -> usize {
self.kv.as_ref().map_or(0, |(k, _)| k.shape[2])
}
pub fn is_empty(&self) -> bool {
self.kv.is_none()
}
pub fn clear(&mut self) {
self.kv = None;
}
pub fn keys_values(&self) -> Option<(&Tensor, &Tensor)> {
self.kv.as_ref().map(|(k, v)| (k, v))
}
}
pub struct MultiHeadAttention {
pub q_proj: Linear,
pub k_proj: Linear,
pub v_proj: Linear,
pub o_proj: Linear,
pub n_heads: usize,
pub head_dim: usize,
pub hidden_dim: usize,
pub is_causal: bool,
}
impl MultiHeadAttention {
pub fn new(hidden_dim: usize, n_heads: usize, device: &Device) -> Self {
assert!(
n_heads > 0 && hidden_dim.is_multiple_of(n_heads),
"hidden_dim must be divisible by n_heads"
);
Self {
q_proj: Linear::new_no_bias(hidden_dim, hidden_dim, device),
k_proj: Linear::new_no_bias(hidden_dim, hidden_dim, device),
v_proj: Linear::new_no_bias(hidden_dim, hidden_dim, device),
o_proj: Linear::new_no_bias(hidden_dim, hidden_dim, device),
n_heads,
head_dim: hidden_dim / n_heads,
hidden_dim,
is_causal: false,
}
}
pub fn causal(mut self, causal: bool) -> Self {
self.is_causal = causal;
self
}
pub fn forward_cached(&self, x: &Tensor, cache: &mut KvCache) -> Tensor {
let batched = x.ndim() == 3;
assert!(
(x.ndim() == 2 || batched) && x.shape[x.ndim() - 1] == self.hidden_dim,
"MultiHeadAttention({}) expects [seq, {0}] or [batch, seq, {0}], got {:?}",
self.hidden_dim,
x.shape
);
let (b, l) = if batched {
(x.shape[0], x.shape[1])
} else {
(1, x.shape[0])
};
let q = self.split_heads(&self.q_proj.forward(x), b, l);
let mut k = self.split_heads(&self.k_proj.forward(x), b, l);
let mut v = self.split_heads(&self.v_proj.forward(x), b, l);
if let Some((past_k, past_v)) = cache.kv.take() {
assert_eq!(
past_k.shape[0], b,
"KvCache holds batch size {}, got {b}",
past_k.shape[0]
);
k = Tensor::cat(&[past_k, k], 2);
v = Tensor::cat(&[past_v, v], 2);
}
let heads = scaled_dot_product_attention(&q, &k, &v, self.is_causal);
cache.kv = Some((k, v));
let merged = heads.permute(&[0, 2, 1, 3]).reshape(&x.shape);
self.o_proj.forward(&merged)
}
fn split_heads(&self, x: &Tensor, b: usize, l: usize) -> Tensor {
x.reshape(&[b, l, self.n_heads, self.head_dim])
.permute(&[0, 2, 1, 3])
}
}
impl Module for MultiHeadAttention {
fn forward(&self, x: &Tensor) -> Tensor {
self.forward_cached(x, &mut KvCache::new())
}
fn parameters(&self) -> Vec<Tensor> {
self.named_parameters()
.into_iter()
.map(|(_, p)| p)
.collect()
}
fn named_parameters(&self) -> Vec<(String, Tensor)> {
let mut params = prefixed("q_proj", self.q_proj.named_parameters());
params.extend(prefixed("k_proj", self.k_proj.named_parameters()));
params.extend(prefixed("v_proj", self.v_proj.named_parameters()));
params.extend(prefixed("o_proj", self.o_proj.named_parameters()));
params
}
}
impl std::fmt::Debug for MultiHeadAttention {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MultiHeadAttention")
.field("hidden_dim", &self.hidden_dim)
.field("n_heads", &self.n_heads)
.field("head_dim", &self.head_dim)
.field("is_causal", &self.is_causal)
.finish()
}
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
use super::*;
use crate::no_grad;
fn max_diff(a: &Tensor, b: &Tensor) -> f32 {
assert_eq!(a.shape, b.shape);
a.to_vec()
.iter()
.zip(b.to_vec())
.map(|(x, y)| (x - y).abs())
.fold(0.0, f32::max)
}
fn decode(attn: &MultiHeadAttention, x: &Tensor, pieces: &[usize]) -> Tensor {
let mut cache = KvCache::new();
let mut start = 0;
let outs: Vec<Tensor> = pieces
.iter()
.map(|&len| {
let out = no_grad(|| attn.forward_cached(&x.narrow(1, start, len), &mut cache));
start += len;
assert_eq!(cache.len(), start);
out
})
.collect();
assert_eq!(start, x.shape[1]);
Tensor::cat(&outs, 1)
}
fn check_incremental_matches_full(device: &Device) {
let attn = MultiHeadAttention::new(16, 4, device).causal(true);
let x = Tensor::randn(&[2, 7, 16], device);
let full = no_grad(|| attn.forward(&x));
for pieces in [&[1, 1, 1, 1, 1, 1, 1][..], &[3, 1, 2, 1], &[7]] {
let diff = max_diff(&decode(&attn, &x, pieces), &full);
assert!(diff < 1e-5, "pieces {pieces:?}: max diff {diff}");
}
}
#[test]
fn incremental_decoding_matches_full_sequence() {
check_incremental_matches_full(&Device::cpu());
}
#[test]
fn unbatched_input_uses_the_cache_too() {
let d = Device::cpu();
let attn = MultiHeadAttention::new(8, 2, &d).causal(true);
let x = Tensor::randn(&[4, 8], &d);
let full = attn.forward(&x);
let mut cache = KvCache::new();
let first = attn.forward_cached(&x.narrow(0, 0, 3), &mut cache);
let last = attn.forward_cached(&x.narrow(0, 3, 1), &mut cache);
let (k, v) = cache.keys_values().unwrap();
assert_eq!(k.shape, vec![1, 2, 4, 4]);
assert_eq!(v.shape, vec![1, 2, 4, 4]);
assert!(max_diff(&Tensor::cat(&[first, last], 0), &full) < 1e-5);
cache.clear();
assert!(cache.is_empty());
assert_eq!(cache.len(), 0);
}
#[test]
fn non_causal_cache_attends_to_every_cached_position() {
let d = Device::cpu();
let attn = MultiHeadAttention::new(8, 2, &d);
let x = Tensor::randn(&[1, 5, 8], &d);
let full = attn.forward(&x).narrow(1, 4, 1);
let mut cache = KvCache::new();
attn.forward_cached(&x.narrow(1, 0, 4), &mut cache);
let last = attn.forward_cached(&x.narrow(1, 4, 1), &mut cache);
assert!(max_diff(&last, &full) < 1e-5);
}
#[test]
#[should_panic(expected = "KvCache holds batch size 2")]
fn cache_rejects_a_different_batch() {
let d = Device::cpu();
let attn = MultiHeadAttention::new(8, 2, &d).causal(true);
let mut cache = KvCache::new();
attn.forward_cached(&Tensor::randn(&[2, 1, 8], &d), &mut cache);
attn.forward_cached(&Tensor::randn(&[3, 1, 8], &d), &mut cache);
}
#[test]
fn gpu_incremental_decoding_matches_full_sequence() {
if std::env::var("FERROTENSOR_RUN_GPU_TESTS").as_deref() != Ok("1") {
eprintln!("skipping GPU KV cache test; set FERROTENSOR_RUN_GPU_TESTS=1 to enable");
return;
}
let gpu = crate::tensor::tests::shared_gpu();
check_incremental_matches_full(&gpu);
}
}