use super::{add_row_bias, expect_shape};
use crate::error::{CortexError, Result, unwrap_compat};
use crate::tensor::Tensor;
use crate::tensor::finite::softmax_row;
use crate::tensor::ops::{try_batched_matmul, try_matmul};
#[derive(Clone)]
pub struct MultiHeadAttention {
pub num_heads: usize,
pub head_dim: usize,
pub dim: usize,
pub wq: Tensor,
pub wb_q: Tensor, pub wk: Tensor,
pub wb_k: Tensor,
pub wv: Tensor,
pub wb_v: Tensor,
pub wo: Tensor,
pub wb_o: Tensor,
}
reference_serde!(
MultiHeadAttention,
AttentionWire {
num_heads: usize,
head_dim: usize,
dim: usize,
wq: Tensor,
wb_q: Tensor,
wk: Tensor,
wb_k: Tensor,
wv: Tensor,
wb_v: Tensor,
wo: Tensor,
wb_o: Tensor,
}
);
impl MultiHeadAttention {
pub(super) fn validate_wire(&self) -> Result<()> {
super::check_head_config(
"MultiHeadAttention::validate_wire",
self.dim,
self.num_heads,
0,
1,
)?;
if self.num_heads.checked_mul(self.head_dim) != Some(self.dim) {
return Err(CortexError::InvalidConfig(
"attention head dimensions disagree".into(),
));
}
for weight in [&self.wq, &self.wk, &self.wv, &self.wo] {
expect_shape(weight, &[self.dim, self.dim])?;
}
for bias in [&self.wb_q, &self.wb_k, &self.wb_v, &self.wb_o] {
expect_shape(bias, &[1, self.dim])?;
}
Ok(())
}
pub fn new(dim: usize, num_heads: usize) -> Self {
unwrap_compat(Self::try_new(dim, num_heads), "MultiHeadAttention::new")
}
pub fn try_new(dim: usize, num_heads: usize) -> Result<Self> {
Self::try_new_with_rng(dim, num_heads, &mut rand::rng())
}
pub fn try_new_seeded(dim: usize, num_heads: usize, seed: u64) -> Result<Self> {
Self::try_new_with_rng(dim, num_heads, &mut crate::tensor::seeded_rng(seed))
}
pub fn try_new_with_rng<R: rand::Rng + ?Sized>(
dim: usize,
num_heads: usize,
rng: &mut R,
) -> Result<Self> {
super::check_head_config("MultiHeadAttention::try_new", dim, num_heads, 0, 1)?;
let head_dim = dim / num_heads;
let scale = 1.0 / (dim as f32).sqrt();
Ok(Self {
num_heads,
head_dim,
dim,
wq: Tensor::try_randn_with_rng(&[dim, dim], 0.0, scale, rng)?,
wb_q: Tensor::try_zeros(&[1, dim])?,
wk: Tensor::try_randn_with_rng(&[dim, dim], 0.0, scale, rng)?,
wb_k: Tensor::try_zeros(&[1, dim])?,
wv: Tensor::try_randn_with_rng(&[dim, dim], 0.0, scale, rng)?,
wb_v: Tensor::try_zeros(&[1, dim])?,
wo: Tensor::try_randn_with_rng(&[dim, dim], 0.0, scale, rng)?,
wb_o: Tensor::try_zeros(&[1, dim])?,
})
}
pub fn forward(&self, x: &Tensor) -> Tensor {
unwrap_compat(self.try_forward(x), "MultiHeadAttention::forward")
}
pub fn try_forward(&self, x: &Tensor) -> Result<Tensor> {
if x.ndim() != 2 {
return Err(CortexError::RankMismatch {
expected: 2,
got: x.ndim(),
});
}
let seq_len = x.shape()[0];
let nh = self.num_heads;
let hd = self.head_dim;
if x.shape()[1] != self.dim {
return Err(CortexError::DimMismatch {
op: "MultiHeadAttention::try_forward",
axis: 1,
expected: self.dim,
got: x.shape()[1],
});
}
if nh == 0 {
return Err(CortexError::ZeroWidth {
op: "MultiHeadAttention::try_forward",
axis: 1,
});
}
if nh.checked_mul(hd) != Some(self.dim) {
return Err(CortexError::InvalidConfig(format!(
"MultiHeadAttention: num_heads ({nh}) * head_dim ({hd}) != dim ({})",
self.dim
)));
}
for bias in [&self.wb_q, &self.wb_k, &self.wb_v, &self.wb_o] {
if bias.shape() != [1, self.dim] {
return Err(CortexError::ShapeMismatch {
expected: vec![1, self.dim],
got: bias.shape().to_vec(),
});
}
}
let project = |w: &Tensor, b: &Tensor| -> Result<Tensor> {
let mut out = try_matmul(x, w)?;
add_row_bias(&mut out, b)?;
Ok(out)
};
let q = project(&self.wq, &self.wb_q)?;
let k = project(&self.wk, &self.wb_k)?;
let v = project(&self.wv, &self.wb_v)?;
let q = reshape_heads(&q, seq_len, nh, hd);
let k = reshape_heads(&k, seq_len, nh, hd);
let v = reshape_heads(&v, seq_len, nh, hd);
let kt = transpose_last_two(&k); let scale = 1.0 / (hd as f32).sqrt();
let mut scores = try_batched_matmul(&q, &kt)?;
causal_scale_softmax_in_place(&mut scores, scale, nh, seq_len);
let attn = scores;
let ctx = causal_context(&attn, &v, nh, seq_len, hd);
let ctx = merge_heads(&ctx, seq_len, nh, hd);
let mut out = try_matmul(&ctx, &self.wo)?;
add_row_bias(&mut out, &self.wb_o)?;
Ok(out)
}
pub fn param_count(&self) -> usize {
super::sum_numel([
&self.wq, &self.wb_q, &self.wk, &self.wb_k, &self.wv, &self.wb_v, &self.wo, &self.wb_o,
])
}
}
fn reshape_heads(t: &Tensor, seq_len: usize, nh: usize, hd: usize) -> Tensor {
let d = t.data();
let mut out = vec![0.0f32; t.numel()];
for s in 0..seq_len {
for h in 0..nh {
for i in 0..hd {
out[h * seq_len * hd + s * hd + i] = d[s * (nh * hd) + h * hd + i];
}
}
}
Tensor::from_vec(out, &[nh, seq_len, hd])
}
fn merge_heads(t: &Tensor, seq_len: usize, nh: usize, hd: usize) -> Tensor {
let d = t.data();
let dim = nh * hd;
let mut out = vec![0.0f32; t.numel()];
for s in 0..seq_len {
for h in 0..nh {
for i in 0..hd {
out[s * dim + h * hd + i] = d[h * seq_len * hd + s * hd + i];
}
}
}
Tensor::from_vec(out, &[seq_len, dim])
}
fn transpose_last_two(t: &Tensor) -> Tensor {
let b = t.shape()[0];
let m = t.shape()[1];
let n = t.shape()[2];
let d = t.data();
let mut out = vec![0.0f32; t.numel()];
for bi in 0..b {
for i in 0..m {
for j in 0..n {
out[bi * n * m + j * m + i] = d[bi * m * n + i * n + j];
}
}
}
Tensor::from_vec(out, &[b, n, m])
}
fn causal_scale_softmax_in_place(t: &mut Tensor, scale: f32, nh: usize, seq: usize) {
assert_eq!(
t.shape(),
&[nh, seq, seq],
"scores must be [heads, seq, seq]"
);
if seq == 0 {
return;
}
let mut scratch = vec![0.0f32; seq];
for slice in t.data_mut().chunks_exact_mut(seq * seq) {
for (r, row) in slice.chunks_exact_mut(seq).enumerate() {
let (visible, future) = row.split_at_mut(r + 1);
for (s, x) in scratch.iter_mut().zip(visible.iter()) {
*s = *x * scale;
}
softmax_row(&scratch[..=r], visible);
future.fill(0.0);
}
}
}
fn causal_context(attn: &Tensor, v: &Tensor, nh: usize, seq: usize, hd: usize) -> Tensor {
assert_eq!(
attn.shape(),
&[nh, seq, seq],
"weights must be [heads, seq, seq]"
);
assert_eq!(
v.shape(),
&[nh, seq, hd],
"values must be [heads, seq, head_dim]"
);
let a = attn.data();
let vd = v.data();
let mut out = vec![0.0f32; v.numel()];
for h in 0..nh {
for r in 0..seq {
let a_off = h * seq * seq + r * seq;
let o_off = h * seq * hd + r * hd;
for p in 0..=r {
let w = a[a_off + p];
let v_off = h * seq * hd + p * hd;
for j in 0..hd {
out[o_off + j] += w * vd[v_off + j];
}
}
}
}
Tensor::from_vec(out, v.shape())
}
#[cfg(test)]
mod tests {
use super::*;
fn identity(dim: usize, sign: f32) -> Tensor {
let mut d = vec![0.0f32; dim * dim];
for i in 0..dim {
d[i * dim + i] = sign;
}
Tensor::from_vec(d, &[dim, dim])
}
fn pseudo_random(shape: &[usize], seed: u64, scale: f32) -> Tensor {
let n: usize = shape.iter().product();
let mut state = seed;
let data = (0..n)
.map(|_| {
state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
let unit = ((state >> 40) as f32) / ((1u64 << 24) as f32);
(unit * 2.0 - 1.0) * scale
})
.collect();
Tensor::from_vec(data, shape)
}
fn causal_batched_softmax(t: &Tensor, nh: usize, seq: usize) -> Tensor {
let mut out = t.clone();
causal_scale_softmax_in_place(&mut out, 1.0, nh, seq);
out
}
#[test]
fn fused_scale_softmax_matches_scale_then_softmax() {
let (nh, seq) = (3, 7);
let scores = pseudo_random(&[nh, seq, seq], 9, 4.0);
let scale = 1.0 / 5.0f32.sqrt();
let mut fused = scores.clone();
causal_scale_softmax_in_place(&mut fused, scale, nh, seq);
let reference = causal_batched_softmax(&scores.scale(scale), nh, seq);
assert_eq!(fused.data(), reference.data());
}
#[test]
fn add_row_bias_matches_tiled_add() {
let out = pseudo_random(&[5, 4], 11, 1.0);
let bias = pseudo_random(&[1, 4], 12, 1.0);
let mut tiled = Vec::new();
for _ in 0..5 {
tiled.extend_from_slice(bias.data());
}
let expect = out.try_add(&Tensor::from_vec(tiled, &[5, 4])).unwrap();
let mut got = out.clone();
add_row_bias(&mut got, &bias).unwrap();
assert_eq!(got.data(), expect.data());
let bad = pseudo_random(&[1, 3], 13, 1.0);
assert!(add_row_bias(&mut got, &bad).is_err());
}
fn overflow_attention() -> MultiHeadAttention {
let mut attn = MultiHeadAttention::new(2, 1);
attn.wq = identity(2, -1.0);
attn.wk = identity(2, 1.0);
attn.wv = identity(2, 1.0);
attn.wo = identity(2, 1.0);
attn
}
#[test]
fn seeded_attention_is_reproducible() {
let a = MultiHeadAttention::try_new_seeded(16, 4, 5).unwrap();
let b = MultiHeadAttention::try_new_seeded(16, 4, 5).unwrap();
let c = MultiHeadAttention::try_new_seeded(16, 4, 6).unwrap();
assert_eq!(a.wq.data(), b.wq.data());
assert_eq!(a.wo.data(), b.wo.data());
assert_ne!(a.wq.data(), c.wq.data());
}
#[test]
fn test_attention_shapes() {
let attn = MultiHeadAttention::new(64, 4);
let x = pseudo_random(&[8, 64], 1, 0.1);
let out = attn.forward(&x);
assert_eq!(out.shape(), &[8, 64]);
}
#[test]
fn all_neg_inf_visible_prefix_does_not_leak_future_tokens() {
let attn = overflow_attention();
let a = attn.forward(&Tensor::from_vec(vec![1e20, 0.0, 0.0, 7.0], &[2, 2]));
let b = attn.forward(&Tensor::from_vec(vec![1e20, 0.0, 0.0, -7.0], &[2, 2]));
assert_eq!(&a.data()[..2], &b.data()[..2]);
assert_eq!(&a.data()[..2], &[1e20, 0.0]);
}
#[test]
fn causal_softmax_all_neg_inf_row_zeroes_future_slots() {
let ninf = f32::NEG_INFINITY;
let scores = Tensor::from_vec(
vec![ninf, ninf, ninf, ninf, ninf, ninf, ninf, ninf, ninf],
&[1, 3, 3],
);
let p = causal_batched_softmax(&scores, 1, 3);
let d = p.data();
assert!(d.iter().all(|w| !w.is_nan()));
assert_eq!(&d[0..3], &[1.0, 0.0, 0.0]);
assert_eq!(&d[3..6], &[0.5, 0.5, 0.0]);
for w in &d[6..9] {
assert!((w - 1.0 / 3.0).abs() < 1e-7);
}
}
#[test]
fn causal_softmax_ignores_future_scores_and_matches_row_softmax() {
let ninf = f32::NEG_INFINITY;
let scores = Tensor::from_vec(
vec![0.5, ninf, ninf, ninf, 1.0, ninf, 0.1, 0.2, 0.3],
&[1, 3, 3],
);
let p = causal_batched_softmax(&scores, 1, 3);
let d = p.data();
assert_eq!(&d[0..3], &[1.0, 0.0, 0.0]);
assert_eq!(&d[3..6], &[0.0, 1.0, 0.0]);
let mut expect = [0.0f32; 3];
softmax_row(&[0.1, 0.2, 0.3], &mut expect);
assert_eq!(&d[6..9], &expect);
assert!((d[6..9].iter().sum::<f32>() - 1.0).abs() < 1e-6);
}
#[test]
fn nan_inside_prefix_poisons_only_that_row() {
let scores = Tensor::from_vec(
vec![0.5, 9.0, 9.0, f32::NAN, 1.0, 9.0, 0.1, 0.2, 0.3],
&[1, 3, 3],
);
let p = causal_batched_softmax(&scores, 1, 3);
let d = p.data();
assert_eq!(&d[0..3], &[1.0, 0.0, 0.0]);
assert!(d[3].is_nan() && d[4].is_nan());
assert_eq!(d[5], 0.0);
assert!(d[6..9].iter().all(|w| w.is_finite()));
}
#[test]
fn plus_inf_inside_prefix_is_one_hot_and_future_inf_is_ignored() {
let inf = f32::INFINITY;
let scores = Tensor::from_vec(
vec![0.0, inf, inf, 1.0, inf, inf, 0.1, 0.2, 0.3],
&[1, 3, 3],
);
let p = causal_batched_softmax(&scores, 1, 3);
let d = p.data();
assert_eq!(&d[0..3], &[1.0, 0.0, 0.0]);
assert_eq!(&d[3..6], &[0.0, 1.0, 0.0]);
assert!(d[6..9].iter().all(|w| w.is_finite()));
}
#[test]
fn non_finite_future_value_does_not_poison_earlier_rows() {
let mut attn = MultiHeadAttention::new(2, 1);
attn.wq = identity(2, 1.0);
attn.wk = identity(2, 1.0);
attn.wv = identity(2, 1.0);
attn.wo = identity(2, 1.0);
for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
let out = attn.forward(&Tensor::from_vec(vec![1.0, 2.0, bad, bad], &[2, 2]));
let clean = attn.forward(&Tensor::from_vec(vec![1.0, 2.0, 0.0, 0.0], &[2, 2]));
assert_eq!(&out.data()[..2], &clean.data()[..2], "bad = {bad}");
assert!(out.data()[..2].iter().all(|x| x.is_finite()));
}
}
#[test]
fn causal_context_matches_dense_matmul_for_finite_inputs() {
let (nh, seq, hd) = (2, 5, 3);
let scores = pseudo_random(&[nh, seq, seq], 2, 1.0);
let attn = causal_batched_softmax(&scores, nh, seq);
let v = pseudo_random(&[nh, seq, hd], 3, 1.0);
let dense = try_batched_matmul(&attn, &v).unwrap();
let causal = causal_context(&attn, &v, nh, seq, hd);
assert_eq!(dense.data(), causal.data());
}
#[test]
fn causal_attention_output_independent_of_future_tokens() {
let attn = MultiHeadAttention::new(8, 2);
let base = pseudo_random(&[4, 8], 4, 0.5);
let mut changed = base.data().to_vec();
for v in &mut changed[2 * 8..] {
*v += 3.0; }
let changed = Tensor::from_vec(changed, &[4, 8]);
let a = attn.forward(&base);
let b = attn.forward(&changed);
assert_eq!(&a.data()[..2 * 8], &b.data()[..2 * 8]);
}
}