burn-msa 0.2.4

MiniMax Sparse Attention (MSA) - block-sparse attention with per-GQA-group top-k block selection for Burn
Documentation
use burn::tensor::backend::Backend;
use burn::tensor::Tensor;

#[derive(Clone, Debug)]
pub struct MsaCache<B: Backend> {
    pub k_cache: Tensor<B, 4>,
    pub v_cache: Tensor<B, 4>,
    pub k_idx_cache: Tensor<B, 4>,
}

impl<B: Backend> MsaCache<B> {
    pub fn new(k: Tensor<B, 4>, v: Tensor<B, 4>, k_idx: Tensor<B, 4>) -> Self {
        Self {
            k_cache: k,
            v_cache: v,
            k_idx_cache: k_idx,
        }
    }

    pub fn update(self, new_k: Tensor<B, 4>, new_v: Tensor<B, 4>, new_k_idx: Tensor<B, 4>) -> Self {
        Self {
            k_cache: Tensor::cat(vec![self.k_cache, new_k], 2),
            v_cache: Tensor::cat(vec![self.v_cache, new_v], 2),
            k_idx_cache: Tensor::cat(vec![self.k_idx_cache, new_k_idx], 2),
        }
    }
}