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    pub fn new(dictionary: Tensor3x3x3) -> Self {
14        Self { dictionary }
15    }
16
17    /// Decomposes a dense KV cache block into a sparse representation using
18    /// Orthogonal Matching Pursuit (OMP) via constant tensor contraction.
19    pub fn compress_kv_block(&self, dense_block: &Tensor3x3x3) -> SolverResult<Tensor3x3x3> {
20        // We use ConstTensorContractor to project the dense block against the dictionary.
21        let contractor = ConstTensorContractor {
22            tensor_a: *dense_block,
23            tensor_b: self.dictionary,
24            result: Tensor3x3x3::zero(),
25            contraction_indices: [(0, 0), (1, 1), (2, 2)], // Trace/Inner product approximation
26            config: SolverConfig::default(),
27            solver_state: SolverState::default(),
28        };
29
30        // Perform the constant tensor contraction to find the sparse coefficients.
31        let sparse_coefficients = contractor
32            .tensor_a
33            .contract(&contractor.tensor_b, &contractor.contraction_indices);
34
35        // Apply a hard threshold to enforce sparsity (Top-K approximation).
36        let mut thresholded = Tensor3x3x3::zero();
37
38        let threshold = 1e-3; // Define sparsity cutoff
39
40        for i in 0..3 {
41            for j in 0..3 {
42                for k in 0..3 {
43                    let val = sparse_coefficients.get(i, j, k);
44                    if val.abs() > threshold {
45                        thresholded.set(i, j, k, val);
46                    } else {
47                        thresholded.set(i, j, k, 0.0);
48                    }
49                }
50            }
51        }
52
53        Ok(thresholded)
54    }
55}