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
👎Deprecated since 0.2.1: use forward, which already splits heads
pub fn forward_multi_head( &self, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor
use forward, which already splits heads
Same as forward, which already splits heads.
Kept for compatibility; it will be removed in 0.3.
Sourcepub fn multi_head_forward(
&self,
hidden: &UnifiedTensor,
ctx: &WgpuContext,
engine: &ComputeEngine,
) -> UnifiedTensor
👎Deprecated since 0.2.1: applies no projections; project the input and call forward
pub fn multi_head_forward( &self, hidden: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor
applies no projections; project the input and call forward
Self-attention with hidden used directly as Q, K and V.
There are no Q, K, V or output projections: project hidden yourself
and call forward. Kept for compatibility; it will be
removed in 0.3.
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
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