ruvector_sparse_inference/pi/
mod.rs1pub mod constants;
30pub mod drift;
31pub mod angular;
32pub mod chaos;
33
34pub use constants::{PiCalibration, PI_SCALE_3BIT, PI_SCALE_5BIT, PI_SCALE_7BIT};
35pub use drift::{DriftDetector, DriftReport, QuantizationHonesty};
36pub use angular::{AngularEmbedding, PhaseEncoder, HypersphericalProjection};
37pub use chaos::{PiChaos, DeterministicJitter, PiScheduler};
38
39use crate::precision::PrecisionLane;
40
41#[derive(Debug, Clone)]
43pub struct PiContext {
44 pub calibration: PiCalibration,
46 pub drift: DriftDetector,
48 pub angular: AngularEmbedding,
50 pub chaos: PiChaos,
52 pub lane: PrecisionLane,
54}
55
56impl PiContext {
57 pub fn new(lane: PrecisionLane) -> Self {
59 Self {
60 calibration: PiCalibration::for_lane(lane),
61 drift: DriftDetector::new(lane),
62 angular: AngularEmbedding::new(lane),
63 chaos: PiChaos::new(),
64 lane,
65 }
66 }
67
68 pub fn calibrate(&self, value: f32) -> f32 {
70 self.calibration.normalize(value)
71 }
72
73 pub fn check_honesty(&mut self, original: &[f32], quantized: &[f32]) -> QuantizationHonesty {
75 self.drift.check(original, quantized)
76 }
77
78 pub fn to_angular(&self, values: &[f32]) -> Vec<f32> {
80 self.angular.project(values)
81 }
82
83 pub fn jitter(&self, index: usize) -> f32 {
85 self.chaos.jitter(index)
86 }
87
88 pub fn update_drift(&mut self, error: f32) {
90 self.drift.update(error);
91 }
92
93 pub fn drift_report(&self) -> DriftReport {
95 self.drift.report()
96 }
97
98 pub fn should_escalate(&self) -> bool {
100 self.drift.report().should_escalate
101 }
102}
103
104impl Default for PiContext {
105 fn default() -> Self {
106 Self::new(PrecisionLane::Bit5)
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn test_pi_context_creation() {
116 let ctx = PiContext::new(PrecisionLane::Bit3);
117 assert_eq!(ctx.lane, PrecisionLane::Bit3);
118 }
119
120 #[test]
121 fn test_pi_context_calibration() {
122 let ctx = PiContext::new(PrecisionLane::Bit5);
123 let calibrated = ctx.calibrate(1.0);
124 assert!(calibrated.is_finite());
125 }
126
127 #[test]
128 fn test_pi_context_angular_projection() {
129 let ctx = PiContext::new(PrecisionLane::Bit7);
130 let values = vec![1.0, 2.0, 3.0, 4.0];
131 let angular = ctx.to_angular(&values);
132 assert_eq!(angular.len(), values.len());
133 }
134
135 #[test]
136 fn test_pi_context_jitter() {
137 let ctx = PiContext::new(PrecisionLane::Bit5);
138 let j1 = ctx.jitter(0);
139 let j2 = ctx.jitter(1);
140 assert_eq!(ctx.jitter(0), j1);
142 assert_ne!(j1, j2);
144 }
145}