pub struct MultiQueryAttention {
pub dim: usize,
pub num_heads: usize,
pub head_dim: usize,
pub causal: bool,
/* private fields */
}Expand description
Multi-Query Attention (MQA) - Extreme efficiency with single KV head
MQA uses a single key-value head shared across all query heads. This provides maximum memory efficiency at some quality cost.
§Use Cases
- Inference-optimized models
- Memory-constrained deployments
- High-throughput serving
§Example
let ctx = pollster::block_on(WgpuContext::new());
let engine = ComputeEngine::new(&ctx);
// [batch, seq_len, hidden]
let q = UnifiedTensor::zeros(&ctx, &[1, 128, 768]);
let k = UnifiedTensor::zeros(&ctx, &[1, 128, 64]);
let v = UnifiedTensor::zeros(&ctx, &[1, 128, 64]);
// 12 query heads share one KV head
let mqa = MultiQueryAttention::new(768, 12, true, 0.0);
let output = mqa.forward(&q, &k, &v, None, &ctx, &engine);Fields§
§dim: usizeTotal hidden dimension
num_heads: usizeNumber of query heads
head_dim: usizeDimension per head
causal: boolWhether to use causal masking
Implementations§
Source§impl MultiQueryAttention
impl MultiQueryAttention
Sourcepub fn new(
dim: usize,
num_heads: usize,
causal: bool,
dropout_prob: f32,
) -> Self
pub fn new( dim: usize, num_heads: usize, causal: bool, dropout_prob: f32, ) -> Self
Create a new MQA layer.
§Arguments
dim- Total hidden dimensionnum_heads- Number of query headscausal- Whether to apply causal masking
Sourcepub fn forward(
&self,
q: &UnifiedTensor,
k: &UnifiedTensor,
v: &UnifiedTensor,
mask: Option<&UnifiedTensor>,
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> UnifiedTensor
pub fn forward( &self, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, mask: Option<&UnifiedTensor>, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor
Sourcepub fn memory_savings(&self) -> String
pub fn memory_savings(&self) -> String
Get memory savings compared to MHA
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MultiQueryAttention
impl RefUnwindSafe for MultiQueryAttention
impl Send for MultiQueryAttention
impl Sync for MultiQueryAttention
impl Unpin for MultiQueryAttention
impl UnsafeUnpin for MultiQueryAttention
impl UnwindSafe for MultiQueryAttention
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more