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: usizeTotal hidden dimension
num_heads: usizeNumber of attention heads
head_dim: usizeDimension per head
window_size: usizeWindow size for local attention
causal: boolWhether to use causal masking
Implementations§
Source§impl SlidingWindowAttention
impl SlidingWindowAttention
Sourcepub fn new(
dim: usize,
num_heads: usize,
window_size: usize,
causal: bool,
) -> Self
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 dimensionnum_heads- Number of attention headswindow_size- Maximum distance for attention (tokens only attend within this window)causal- Whether to apply causal masking
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
Get memory savings compared to full attention
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SlidingWindowAttention
impl RefUnwindSafe for SlidingWindowAttention
impl Send for SlidingWindowAttention
impl Sync for SlidingWindowAttention
impl Unpin for SlidingWindowAttention
impl UnsafeUnpin for SlidingWindowAttention
impl UnwindSafe for SlidingWindowAttention
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