Skip to main content

SlidingWindowAttention

Struct SlidingWindowAttention 

Source
pub struct SlidingWindowAttention {
    pub dim: usize,
    pub num_heads: usize,
    pub head_dim: usize,
    pub window_size: usize,
    pub causal: bool,
    /* private fields */
}
Expand description

Sliding Window Attention for efficient long-context processing

Limits attention to a local window around each position, enabling linear memory scaling for very long sequences.

§Use Cases

  • Long document processing (100K+ tokens)
  • Streaming input processing
  • Memory-efficient inference

§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 swa = SlidingWindowAttention::new(768, 12, 4096, true);
let output = swa.forward(&q, &k, &v, &ctx, &engine);

Fields§

§dim: usize

Total hidden dimension

§num_heads: usize

Number of attention heads

§head_dim: usize

Dimension per head

§window_size: usize

Window size for local attention

§causal: bool

Whether to use causal masking

Implementations§

Source§

impl SlidingWindowAttention

Source

pub fn new( dim: usize, num_heads: usize, window_size: usize, causal: bool, ) -> Self

Create a new sliding window attention layer.

§Arguments
  • dim - Total hidden dimension
  • num_heads - Number of attention heads
  • window_size - Maximum distance for attention (tokens only attend within this window)
  • causal - Whether to apply causal masking
Source

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

Forward pass with sliding window attention.

Each position can only attend to positions within window_size distance.

§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 memory_savings(&self, seq_len: usize) -> String

Get memory savings compared to full attention

Trait Implementations§

Source§

impl Debug for SlidingWindowAttention

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,