Skip to main content

qualia_core_db/inference/
sparse_cache.rs

1use crate::solvers::linear_algebra::{ConstTensorContractor, Tensor3x3x3};
2use crate::solvers::{SolverConfig, SolverResult, SolverState};
3
4/// Orthogonal Matching Pursuit (OMP) mathematically decomposes dense KV Cache vectors
5/// against a pre-loaded symbolic lattice dictionary.
6pub struct SparseDictionaryCache {
7    /// The pre-computed symbolic lattice (e.g., CML dictionaries mapped from WordNet).
8    /// For zero-allocation constraints, we model this as a fixed-size tensor.
9    pub dictionary: Tensor3x3x3,
10}
11
12impl SparseDictionaryCache {
13    /// Creates `SparseDictionaryCache` with the given state.
14    pub fn new(dictionary: Tensor3x3x3) -> Self {
15        Self { dictionary }
16    }
17
18    /// Decomposes a dense KV cache block into a sparse representation using
19    /// Orthogonal Matching Pursuit (OMP) via constant tensor contraction.
20    pub fn compress_kv_block(&self, dense_block: &Tensor3x3x3) -> SolverResult<Tensor3x3x3> {
21        // We use ConstTensorContractor to project the dense block against the dictionary.
22        let contractor = ConstTensorContractor {
23            tensor_a: *dense_block,
24            tensor_b: self.dictionary,
25            result: Tensor3x3x3::zero(),
26            contraction_indices: [(0, 0), (1, 1), (2, 2)], // Trace/Inner product approximation
27            config: SolverConfig::default(),
28            solver_state: SolverState::default(),
29        };
30
31        // Perform the constant tensor contraction to find the sparse coefficients.
32        let sparse_coefficients = contractor
33            .tensor_a
34            .contract(&contractor.tensor_b, &contractor.contraction_indices);
35
36        // Apply a hard threshold to enforce sparsity (Top-K approximation).
37        let mut thresholded = Tensor3x3x3::zero();
38
39        let threshold = 1e-3; // Define sparsity cutoff
40
41        for i in 0..3 {
42            for j in 0..3 {
43                for k in 0..3 {
44                    let val = sparse_coefficients.get(i, j, k);
45                    if val.abs() > threshold {
46                        thresholded.set(i, j, k, val);
47                    } else {
48                        thresholded.set(i, j, k, 0.0);
49                    }
50                }
51            }
52        }
53
54        Ok(thresholded)
55    }
56}