use std::rc::Rc;
use crate::{DType, Engine, Error, Matrix, Result, matrix};
use super::super::{Module, ModuleRegistry, Parameter, autograd};
use super::MultiHeadAttention;
pub struct AttentionPool {
model_width: usize,
query: Parameter,
attention: Rc<MultiHeadAttention>,
registry: ModuleRegistry,
}
impl AttentionPool {
pub fn new_with_seed_on(
engine: &Engine,
model_width: usize,
num_heads: usize,
seed: u64,
) -> Result<Self> {
if model_width == 0 {
return Err(Error::invalid_argument(
"attention pool requires a nonzero model width",
));
}
let attention = Rc::new(MultiHeadAttention::new_with_seed_on(
engine,
model_width,
num_heads,
1,
seed,
)?);
let query = Parameter::new(
"query",
Matrix::new_from_f32_on(engine, [1, model_width], &vec![0.0_f32; model_width])?,
)?;
let mut registry = ModuleRegistry::new();
registry.register_parameter("query", query.clone())?;
registry.register_module("attention", Rc::clone(&attention) as Rc<dyn Module>)?;
Ok(Self {
model_width,
query,
attention,
registry,
})
}
pub fn new_with_seed(model_width: usize, num_heads: usize, seed: u64) -> Result<Self> {
Self::new_with_seed_on(&Engine::current()?, model_width, num_heads, seed)
}
pub fn forward(&self, input: &Matrix, batch: usize) -> Result<Matrix> {
let [rows, width] = input.shape() else {
return Err(Error::invalid_argument(format!(
"attention pool requires [B*S, D]; found {:?}",
input.shape(),
)));
};
if input.dtype() != DType::F32 || *width != self.model_width {
return Err(Error::invalid_argument(format!(
"attention pool requires F32 width {}; found {:?} {}",
self.model_width,
input.shape(),
input.dtype().token(),
)));
}
if batch == 0 || !rows.is_multiple_of(batch) {
return Err(Error::invalid_argument(format!(
"attention pool rows {rows} must be a multiple of batch {batch}"
)));
}
autograd::record_parameter_leaf(&self.query)?;
let query = matrix::repeat_interleave(&self.query.data(), batch, 0)?;
self.attention.forward_kv(&query, input)
}
}
impl Module for AttentionPool {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
let _ = input;
Err(Error::invalid_argument(
"AttentionPool requires the batch count; call AttentionPool::forward(input, batch)",
))
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}