qualia_core_db/inference/
sparse_cache.rs1use crate::solvers::linear_algebra::{ConstTensorContractor, Tensor3x3x3};
2use crate::solvers::{SolverConfig, SolverResult, SolverState};
3
4pub struct SparseDictionaryCache {
7 pub dictionary: Tensor3x3x3,
10}
11
12impl SparseDictionaryCache {
13 pub fn new(dictionary: Tensor3x3x3) -> Self {
14 Self { dictionary }
15 }
16
17 pub fn compress_kv_block(&self, dense_block: &Tensor3x3x3) -> SolverResult<Tensor3x3x3> {
20 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)], config: SolverConfig::default(),
27 solver_state: SolverState::default(),
28 };
29
30 let sparse_coefficients = contractor
32 .tensor_a
33 .contract(&contractor.tensor_b, &contractor.contraction_indices);
34
35 let mut thresholded = Tensor3x3x3::zero();
37
38 let threshold = 1e-3; 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}