pub struct FlashAttention { /* private fields */ }Expand description
Flash Attention layer with tiled computation and online softmax.
Memory efficient: O(N) instead of O(N²) for standard attention. Speed: 2-4x faster than naive implementation.
§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, 768]);
let v = UnifiedTensor::zeros(&ctx, &[1, 128, 768]);
let flash_attn = FlashAttention::new(768, 12, true);
let output = flash_attn.forward(&q, &k, &v, &ctx, &engine);Implementations§
Source§impl FlashAttention
impl FlashAttention
Sourcepub fn new(dim: usize, num_heads: usize, causal: bool) -> Self
pub fn new(dim: usize, num_heads: usize, causal: bool) -> Self
Create a new FlashAttention layer.
§Arguments
dim- Total dimension (will be split across heads)num_heads- Number of attention headscausal- Whether to apply causal masking (for autoregressive models)
Sourcepub fn forward(
&self,
q: &UnifiedTensor,
k: &UnifiedTensor,
v: &UnifiedTensor,
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> UnifiedTensor
pub fn forward( &self, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor
Sourcepub fn forward_multi_head(
&self,
q: &UnifiedTensor,
k: &UnifiedTensor,
v: &UnifiedTensor,
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> UnifiedTensor
pub fn forward_multi_head( &self, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor
Multi-head attention forward pass with automatic head splitting.
This method automatically splits the input into heads and applies Flash Attention to each head in parallel.
§Arguments
q- Query tensor [batch, seq_q, dim]k- Key tensor [batch, seq_k, dim]v- Value tensor [batch, seq_k, dim]
§Returns
Output tensor [batch, seq_q, dim]
Sourcepub fn multi_head_forward(
&self,
hidden: &UnifiedTensor,
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> UnifiedTensor
pub fn multi_head_forward( &self, hidden: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor
Multi-head attention with Flash Attention.
Handles Q, K, V projection and output projection internally.
Sourcepub fn memory_savings(&self, seq_len: usize) -> String
pub fn memory_savings(&self, seq_len: usize) -> String
Get memory savings compared to standard attention.
Sourcepub fn backward(
&self,
q: &UnifiedTensor,
k: &UnifiedTensor,
v: &UnifiedTensor,
output: &UnifiedTensor,
grad_output: &UnifiedTensor,
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> FlashAttentionGrads
pub fn backward( &self, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, output: &UnifiedTensor, grad_output: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, ) -> FlashAttentionGrads
Backward pass for Flash Attention.
Computes gradients dQ, dK, dV using recomputation to avoid storing the full attention matrix.
§Arguments
q- Query tensor from forward passk- Key tensor from forward passv- Value tensor from forward passoutput- Output from forward passgrad_output- Gradient of the output
§Returns
FlashAttentionGrads containing dQ, dK, dV
Trait Implementations§
Auto Trait Implementations§
impl Freeze for FlashAttention
impl RefUnwindSafe for FlashAttention
impl Send for FlashAttention
impl Sync for FlashAttention
impl Unpin for FlashAttention
impl UnsafeUnpin for FlashAttention
impl UnwindSafe for FlashAttention
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