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 memory_savings(&self, seq_len: usize) -> String
pub fn memory_savings(&self, seq_len: usize) -> String
Memory for one head’s attention scores: the full seq x seq matrix
that standard attention stores versus the seq x head_dim accumulator
Flash Attention keeps instead (f32, per head, scores only).
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