Skip to main content

SparseKernel

Struct SparseKernel 

Source
pub struct SparseKernel { /* private fields */ }
Expand description

Manager for sparse attention kernel execution.

Handles the execution of attention computations with dynamic head sparsity, including index mapping caching for efficient repeated execution with the same sparsity pattern.

§Performance Optimizations

  • Pre-computes index mappings for gather/scatter operations
  • Caches mappings by pattern hash for repeated use
  • Supports flash attention for memory efficiency
  • Handles different memory layouts (contiguous, channels-last, grouped)

§Example

let mut kernel = SparseKernel::new(KernelConfig::default());
kernel.prepare(&mask, layer_idx)?;
let output = kernel.execute(&query, &key, &value, &mask, layer_idx)?;

Implementations§

Source§

impl SparseKernel

Source

pub fn new(config: KernelConfig) -> Self

Create a new kernel with config

Source

pub fn prepare(&mut self, mask: &AttentionMask, layer: usize) -> Result<()>

Prepare kernel for a specific mask

Source

pub fn execute( &self, mask: &AttentionMask, layer: usize, _q: &[f32], _k: &[f32], _v: &[f32], ) -> Result<Vec<f32>>

Execute sparse attention for a layer

This is a simulated implementation. In practice, this would:

  1. Gather only active Q, K, V heads
  2. Compute attention only for active heads
  3. Scatter results back to full head positions
Source

pub fn estimate_savings(&self, mask: &AttentionMask) -> ComputeEstimate

Estimate compute savings for a mask

Source

pub fn config(&self) -> &KernelConfig

Get current configuration

Source

pub fn clear_cache(&mut self)

Clear cached index mappings

Trait Implementations§

Source§

impl Debug for SparseKernel

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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V