pub struct GroupedQueryAttention {
pub dim: usize,
pub num_heads: usize,
pub num_kv_heads: usize,
pub head_dim: usize,
pub num_groups: usize,
pub causal: bool,
pub dropout_prob: f32,
/* private fields */
}Expand description
Grouped Query Attention (GQA) - Efficient attention with fewer KV heads
GQA uses fewer key-value heads than query heads, reducing memory and computation while maintaining quality. This is the attention mechanism used by Llama 3, Mistral, and many modern LLMs.
§Memory Savings
- Standard MHA:
num_headsK and V projections - GQA:
num_kv_headsK and V projections (typically 2-8x fewer)
§Example
let ctx = pollster::block_on(WgpuContext::new());
let engine = ComputeEngine::new(&ctx);
// [batch, seq_len, hidden]
let q = UnifiedTensor::zeros(&ctx, &[1, 128, 4096]);
let k = UnifiedTensor::zeros(&ctx, &[1, 128, 1024]);
let v = UnifiedTensor::zeros(&ctx, &[1, 128, 1024]);
// Llama 3 8B: 32 query heads share 8 KV heads (no dropout, no mask)
let gqa = GroupedQueryAttention::new(4096, 32, 8, true, 0.0);
let output = gqa.forward(&q, &k, &v, None, &ctx, &engine);Fields§
§dim: usizeTotal hidden dimension
num_heads: usizeNumber of query heads
num_kv_heads: usizeNumber of key-value heads (fewer than query heads)
head_dim: usizeDimension per head
num_groups: usizeNumber of query heads per KV head
causal: boolWhether to use causal masking
dropout_prob: f32Dropout probability (0.0 = no dropout)
Implementations§
Source§impl GroupedQueryAttention
impl GroupedQueryAttention
Sourcepub fn new(
dim: usize,
num_heads: usize,
num_kv_heads: usize,
causal: bool,
dropout_prob: f32,
) -> Self
pub fn new( dim: usize, num_heads: usize, num_kv_heads: usize, causal: bool, dropout_prob: f32, ) -> Self
Create a new GQA layer.
§Arguments
dim- Total hidden dimensionnum_heads- Number of query headsnum_kv_heads- Number of key-value heads (must divide num_heads evenly)causal- Whether to apply causal maskingdropout_prob- Dropout probability
§Panics
Panics if num_heads is not divisible by num_kv_heads
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 forward_at(
&self,
q: &UnifiedTensor,
k: &UnifiedTensor,
v: &UnifiedTensor,
mask: Option<&UnifiedTensor>,
pos: usize,
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> UnifiedTensor
pub fn forward_at( &self, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, mask: Option<&UnifiedTensor>, pos: usize, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor
Like forward, with the first query at absolute
position pos: query i attends to keys 0..=pos + i when causal.
Used to decode against a KV cache, where k/v hold the cache.
Sourcepub fn memory_savings(&self) -> String
pub fn memory_savings(&self) -> String
Get memory savings compared to standard MHA
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GroupedQueryAttention
impl RefUnwindSafe for GroupedQueryAttention
impl Send for GroupedQueryAttention
impl Sync for GroupedQueryAttention
impl Unpin for GroupedQueryAttention
impl UnsafeUnpin for GroupedQueryAttention
impl UnwindSafe for GroupedQueryAttention
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
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> ⓘ
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> ⓘ
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