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
impl SparseKernel
Sourcepub fn new(config: KernelConfig) -> Self
pub fn new(config: KernelConfig) -> Self
Create a new kernel with config
Sourcepub fn prepare(&mut self, mask: &AttentionMask, layer: usize) -> Result<()>
pub fn prepare(&mut self, mask: &AttentionMask, layer: usize) -> Result<()>
Prepare kernel for a specific mask
Sourcepub fn execute(
&self,
mask: &AttentionMask,
layer: usize,
_q: &[f32],
_k: &[f32],
_v: &[f32],
) -> Result<Vec<f32>>
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:
- Gather only active Q, K, V heads
- Compute attention only for active heads
- Scatter results back to full head positions
Sourcepub fn estimate_savings(&self, mask: &AttentionMask) -> ComputeEstimate
pub fn estimate_savings(&self, mask: &AttentionMask) -> ComputeEstimate
Estimate compute savings for a mask
Sourcepub fn config(&self) -> &KernelConfig
pub fn config(&self) -> &KernelConfig
Get current configuration
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clear cached index mappings
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SparseKernel
impl RefUnwindSafe for SparseKernel
impl Send for SparseKernel
impl Sync for SparseKernel
impl Unpin for SparseKernel
impl UnsafeUnpin for SparseKernel
impl UnwindSafe for SparseKernel
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