Skip to main content

FlashAttention

Struct FlashAttention 

Source
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

Source

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 heads
  • causal - Whether to apply causal masking (for autoregressive models)
Source

pub fn forward( &self, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, ctx: &WgpuContext, engine: &ComputeEngine, ) -> UnifiedTensor

Forward pass using Flash Attention algorithm.

§Arguments
  • q - Query tensor [batch, seq_q, dim], [seq_q, dim] or [batch, seq_q, num_heads, head_dim]
  • k - Key tensor with the same layout and seq_k tokens
  • v - Value tensor with the same layout as k
§Returns

Output tensor with the same shape as q.

Source

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]

Source

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.

Source

pub fn memory_savings(&self, seq_len: usize) -> String

Get memory savings compared to standard attention.

Source

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 pass
  • k - Key tensor from forward pass
  • v - Value tensor from forward pass
  • output - Output from forward pass
  • grad_output - Gradient of the output
§Returns

FlashAttentionGrads containing dQ, dK, dV

Trait Implementations§

Source§

impl Debug for FlashAttention

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,