Module conservation

Module conservation 

Source
Expand description

Conservation Metrics for Bounded Forgetting

Provides metrics to validate that memory operations preserve important properties. Inspired by RCP’s conservation laws.

§Conservation Laws

LawFormulaMeaning
Magnitude‖C‖ = constContext doesn’t disappear
EnergyE = ½Σᵢⱼ aᵢaⱼ cos(eᵢ, eⱼ)Attention capacity conserved
InformationH = -Σ aᵢ log(aᵢ)Shannon entropy of attention

§Usage

use rag_plusplus_core::trajectory::ConservationMetrics;

// Example embeddings (3 vectors of dimension 4)
let embeddings_before: Vec<&[f32]> = vec![
    &[1.0, 0.0, 0.0, 0.0],
    &[0.0, 1.0, 0.0, 0.0],
    &[0.0, 0.0, 1.0, 0.0],
];
let attention_before = vec![0.5, 0.3, 0.2];

let embeddings_after: Vec<&[f32]> = vec![
    &[0.9, 0.1, 0.0, 0.0],
    &[0.1, 0.9, 0.0, 0.0],
    &[0.0, 0.0, 1.0, 0.0],
];
let attention_after = vec![0.5, 0.3, 0.2];

// Compute metrics before and after an operation
let before = ConservationMetrics::compute(&embeddings_before, &attention_before);
let after = ConservationMetrics::compute(&embeddings_after, &attention_after);

// Check if conservation is preserved
if before.is_conserved(&after, 0.1) {
    println!("Operation preserved conservation laws");
} else {
    println!("Warning: Conservation violation detected");
}

Structs§

ConservationConfig
Configuration for conservation checking.
ConservationMetrics
Conservation metrics for a set of embeddings with attention weights.
ConservationTracker
Track conservation over time.
ConservationViolation
Details of a conservation violation.

Functions§

weighted_centroid
Compute the attention-weighted centroid of embeddings.
weighted_covariance
Compute the attention-weighted covariance matrix (flattened).