vsa-optim-rs 0.1.1

Deterministic training optimization using VSA compression and closed-form gradient prediction
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Deterministic gradient prediction for phase-based training.
//!
//! This module implements a deterministic prediction system that models
//! gradient evolution during training. Unlike stochastic extrapolation,
//! this approach guarantees reproducible predictions given the same history.
//!
//! # Algorithm
//!
//! The predictor fits a linear regression model to the gradient trajectory:
//!
//! ```text
//! g(t) = g(0) + α * t + residual(t)
//! ```
//!
//! Where:
//! - `g(t)` is the gradient at step t
//! - `g(0)` is the baseline gradient (from warmup)
//! - `α` is the fitted gradient velocity (change per step)
//! - `residual(t)` accumulates prediction errors for correction
//!
//! # Phases
//!
//! 1. **Warmup**: `warmup_steps` of full gradient computation to establish baseline
//! 2. **Predict**: Extrapolate gradients using fitted model
//! 3. **Correct**: Compare prediction with actual, update model and residuals
//!
//! # Determinism
//!
//! Predictions are fully deterministic because:
//! - No random sampling or stochastic operations
//! - Model fit uses closed-form least squares (no iterative optimization)
//! - Same history always produces same prediction

use std::collections::HashMap;

use candle_core::{DType, Device, Tensor};

use crate::error::{OptimError, Result};

/// Configuration for deterministic gradient prediction.
#[derive(Debug, Clone)]
pub struct DeterministicPredictionConfig {
    /// Minimum steps of full training before prediction begins.
    pub warmup_steps: usize,

    /// Number of history steps to use for fitting.
    pub history_window: usize,

    /// Steps to predict before correction.
    pub prediction_horizon: usize,

    /// Exponential decay for older history (1.0 = no decay).
    pub history_decay: f32,

    /// Threshold for residual magnitude to trigger early correction.
    pub residual_threshold: f32,
}

impl Default for DeterministicPredictionConfig {
    fn default() -> Self {
        Self {
            warmup_steps: 10,
            history_window: 8,
            prediction_horizon: 4,
            history_decay: 0.95,
            residual_threshold: 0.5,
        }
    }
}

impl DeterministicPredictionConfig {
    /// Builder: Set warmup steps.
    #[must_use]
    pub const fn with_warmup_steps(mut self, steps: usize) -> Self {
        self.warmup_steps = steps;
        self
    }

    /// Builder: Set history window.
    #[must_use]
    pub const fn with_history_window(mut self, window: usize) -> Self {
        self.history_window = window;
        self
    }

    /// Builder: Set prediction horizon.
    #[must_use]
    pub const fn with_prediction_horizon(mut self, horizon: usize) -> Self {
        self.prediction_horizon = horizon;
        self
    }

    /// Builder: Set history decay.
    #[must_use]
    pub const fn with_history_decay(mut self, decay: f32) -> Self {
        self.history_decay = decay;
        self
    }
}

/// Gradient history entry with step index.
#[derive(Clone)]
struct GradientSnapshot {
    /// Global step index when this gradient was recorded.
    step: usize,
    /// The gradient tensor.
    gradient: Tensor,
}

/// Linear model for gradient evolution: g(t) = baseline + velocity * t
#[derive(Clone)]
struct LinearGradientModel {
    /// Baseline gradient (intercept).
    baseline: Tensor,
    /// Gradient velocity (slope) per step.
    velocity: Tensor,
    /// Step index at which model was fitted.
    fit_step: usize,
}

/// Deterministic gradient predictor.
///
/// Maintains gradient history and fits a linear model to predict
/// future gradients deterministically.
pub struct DeterministicPredictor {
    config: DeterministicPredictionConfig,
    device: Device,

    /// Parameter shapes for reconstruction.
    shapes: HashMap<String, Vec<usize>>,

    /// Gradient history per parameter.
    history: HashMap<String, Vec<GradientSnapshot>>,

    /// Fitted linear models per parameter.
    models: HashMap<String, LinearGradientModel>,

    /// Accumulated residuals (prediction errors) per parameter.
    residuals: HashMap<String, Tensor>,

    /// Current global step.
    global_step: usize,

    /// Steps since last model fit.
    steps_since_fit: usize,

    /// Whether warmup is complete.
    warmup_complete: bool,

    /// Statistics tracking.
    stats: PredictorStatistics,
}

/// Statistics for prediction quality monitoring.
#[derive(Debug, Clone, Default)]
pub struct PredictorStatistics {
    /// Total steps processed.
    pub total_steps: usize,
    /// Steps with full gradient computation.
    pub full_steps: usize,
    /// Steps with predicted gradients.
    pub predicted_steps: usize,
    /// Mean absolute prediction error.
    pub mean_abs_error: f32,
    /// Maximum observed residual.
    pub max_residual: f32,
    /// Number of early corrections triggered.
    pub early_corrections: usize,
}

impl DeterministicPredictor {
    /// Create a new deterministic predictor.
    ///
    /// # Arguments
    ///
    /// * `param_shapes` - List of (name, shape) tuples for parameters
    /// * `config` - Prediction configuration
    /// * `device` - Device for tensor storage
    pub fn new(
        param_shapes: &[(String, Vec<usize>)],
        config: DeterministicPredictionConfig,
        device: &Device,
    ) -> Result<Self> {
        let mut shapes = HashMap::new();
        let mut history = HashMap::new();
        let mut residuals = HashMap::new();

        for (name, shape) in param_shapes {
            shapes.insert(name.clone(), shape.clone());
            history.insert(name.clone(), Vec::with_capacity(config.history_window + 4));
            // Initialize residuals to zero
            residuals.insert(
                name.clone(),
                Tensor::zeros(shape.as_slice(), DType::F32, device)?,
            );
        }

        Ok(Self {
            config,
            device: device.clone(),
            shapes,
            history,
            models: HashMap::new(),
            residuals,
            global_step: 0,
            steps_since_fit: 0,
            warmup_complete: false,
            stats: PredictorStatistics::default(),
        })
    }

    /// Check if still in warmup phase (must compute full gradients).
    #[must_use]
    pub fn in_warmup(&self) -> bool {
        !self.warmup_complete
    }

    /// Check if correction is needed based on residual magnitude.
    #[must_use]
    pub fn needs_correction(&self) -> bool {
        // Need correction after prediction horizon
        if self.steps_since_fit >= self.config.prediction_horizon {
            return true;
        }

        // Check residual threshold
        for residual in self.residuals.values() {
            if let Ok(max_abs) = residual.abs().and_then(|t| t.max(0)).and_then(|t| t.to_scalar::<f32>()) {
                if max_abs > self.config.residual_threshold {
                    return true;
                }
            }
        }

        false
    }

    /// Record a gradient from full computation.
    ///
    /// Updates history and potentially refits the prediction model.
    ///
    /// # Arguments
    ///
    /// * `gradients` - Map of parameter names to gradient tensors
    /// * `is_correction` - Whether this is a correction step (vs. regular full step)
    pub fn record_gradient(
        &mut self,
        gradients: &HashMap<String, Tensor>,
        is_correction: bool,
    ) -> Result<()> {
        // Record to history
        for (name, grad) in gradients {
            if let Some(hist) = self.history.get_mut(name) {
                hist.push(GradientSnapshot {
                    step: self.global_step,
                    gradient: grad.clone(),
                });

                // Trim history to window size
                let window = self.config.history_window;
                if hist.len() > window + 2 {
                    hist.drain(0..hist.len() - window - 2);
                }
            }
        }

        // Update statistics
        self.stats.total_steps += 1;
        self.stats.full_steps += 1;

        // Check warmup completion
        if !self.warmup_complete {
            let min_history = self.history.values().map(|h| h.len()).min().unwrap_or(0);
            if min_history >= self.config.warmup_steps {
                self.warmup_complete = true;
                self.fit_models()?;
            }
        } else if is_correction {
            // Update residuals based on prediction error
            self.update_residuals(gradients)?;
            // Refit model with new data
            self.fit_models()?;
        } else {
            // Regular full step - refit model
            self.fit_models()?;
        }

        self.global_step += 1;
        self.steps_since_fit = 0;

        Ok(())
    }

    /// Predict gradient for current step.
    ///
    /// Uses the fitted linear model to extrapolate from history.
    /// Prediction is fully deterministic.
    ///
    /// # Returns
    ///
    /// Map of parameter names to predicted gradient tensors.
    pub fn predict_gradient(&mut self) -> Result<HashMap<String, Tensor>> {
        if !self.warmup_complete {
            return Err(OptimError::Prediction(
                "Cannot predict during warmup phase".to_string(),
            ));
        }

        let mut predicted = HashMap::new();

        for (name, model) in &self.models {
            // Steps since model was fitted
            let dt = (self.global_step - model.fit_step) as f64;

            // Linear prediction: g(t) = baseline + velocity * dt
            let velocity_term = (&model.velocity * dt)?;
            let mut prediction = model.baseline.add(&velocity_term)?;

            // Add accumulated residual correction
            if let Some(residual) = self.residuals.get(name) {
                // Apply weighted residual (decays with steps since correction)
                let residual_weight = self.config.history_decay.powi(self.steps_since_fit as i32);
                let scaled_residual = (residual * residual_weight as f64)?;
                prediction = prediction.add(&scaled_residual)?;
            }

            predicted.insert(name.clone(), prediction);
        }

        // Update statistics
        self.stats.total_steps += 1;
        self.stats.predicted_steps += 1;
        self.global_step += 1;
        self.steps_since_fit += 1;

        Ok(predicted)
    }

    /// Update residuals based on prediction error.
    ///
    /// Called during correction step to accumulate the difference
    /// between predicted and actual gradients.
    fn update_residuals(&mut self, actual: &HashMap<String, Tensor>) -> Result<()> {
        for (name, actual_grad) in actual {
            if let Some(model) = self.models.get(name) {
                // What we predicted for this step
                let dt = (self.global_step - model.fit_step) as f64;
                let velocity_term = (&model.velocity * dt)?;
                let predicted = model.baseline.add(&velocity_term)?;

                // Residual = actual - predicted
                let error = actual_grad.sub(&predicted)?;

                // Update accumulated residual with exponential averaging
                if let Some(existing) = self.residuals.get(name) {
                    let decay = self.config.history_decay as f64;
                    let decayed_existing = (existing * decay)?;
                    let new_contribution = (&error * (1.0 - decay))?;
                    self.residuals
                        .insert(name.clone(), decayed_existing.add(&new_contribution)?);
                } else {
                    self.residuals.insert(name.clone(), error);
                }

                // Track statistics
                if let Ok(mean_err) = actual_grad
                    .sub(&predicted)
                    .and_then(|t| t.abs())
                    .and_then(|t| t.mean_all())
                    .and_then(|t| t.to_scalar::<f32>())
                {
                    self.stats.mean_abs_error =
                        0.9 * self.stats.mean_abs_error + 0.1 * mean_err;
                }
            }
        }

        Ok(())
    }

    /// Fit linear models to gradient history.
    ///
    /// Uses weighted least squares to fit g(t) = baseline + velocity * t
    /// for each parameter.
    fn fit_models(&mut self) -> Result<()> {
        for (name, hist) in &self.history {
            if hist.len() < 2 {
                continue;
            }

            let shape = self.shapes.get(name).ok_or_else(|| {
                OptimError::Prediction(format!("Unknown parameter: {name}"))
            })?;

            // Weighted least squares fitting
            // g(t) = baseline + velocity * t
            // Minimize: sum_i w_i * (g_i - baseline - velocity * t_i)^2

            let n = hist.len();
            let mut sum_w = 0.0f64;
            let mut sum_wt = 0.0f64;
            let mut sum_wt2 = 0.0f64;
            let mut sum_wg: Option<Tensor> = None;
            let mut sum_wtg: Option<Tensor> = None;

            // Reference step for numerical stability
            let t_ref = hist.last().map(|s| s.step).unwrap_or(0);

            for (i, snapshot) in hist.iter().enumerate() {
                // Exponential weight favoring recent gradients
                let age = (n - 1 - i) as i32;
                let w = self.config.history_decay.powi(age) as f64;

                // Relative step index
                let t = (snapshot.step as i64 - t_ref as i64) as f64;

                sum_w += w;
                sum_wt += w * t;
                sum_wt2 += w * t * t;

                // Accumulate weighted gradients
                let wg = (&snapshot.gradient * w)?;
                let wtg = (&snapshot.gradient * (w * t))?;

                sum_wg = Some(match sum_wg {
                    Some(acc) => acc.add(&wg)?,
                    None => wg,
                });

                sum_wtg = Some(match sum_wtg {
                    Some(acc) => acc.add(&wtg)?,
                    None => wtg,
                });
            }

            // Solve normal equations for least squares
            // [sum_w    sum_wt  ] [baseline]   [sum_wg ]
            // [sum_wt   sum_wt2 ] [velocity] = [sum_wtg]

            let det = sum_w * sum_wt2 - sum_wt * sum_wt;
            if det.abs() < 1e-10 {
                // Degenerate case: use latest gradient as baseline, zero velocity
                let baseline = hist.last().unwrap().gradient.clone();
                let velocity = Tensor::zeros(shape.as_slice(), DType::F32, &self.device)?;
                self.models.insert(
                    name.clone(),
                    LinearGradientModel {
                        baseline,
                        velocity,
                        fit_step: self.global_step,
                    },
                );
                continue;
            }

            let sum_wg = sum_wg.ok_or_else(|| {
                OptimError::Prediction("Empty gradient history".to_string())
            })?;
            let sum_wtg = sum_wtg.ok_or_else(|| {
                OptimError::Prediction("Empty gradient history".to_string())
            })?;

            // Cramer's rule
            // baseline = (sum_wt2 * sum_wg - sum_wt * sum_wtg) / det
            // velocity = (sum_w * sum_wtg - sum_wt * sum_wg) / det

            let baseline = {
                let term1 = (&sum_wg * sum_wt2)?;
                let term2 = (&sum_wtg * sum_wt)?;
                let numer = term1.sub(&term2)?;
                (&numer * (1.0 / det))?
            };

            let velocity = {
                let term1 = (&sum_wtg * sum_w)?;
                let term2 = (&sum_wg * sum_wt)?;
                let numer = term1.sub(&term2)?;
                (&numer * (1.0 / det))?
            };

            self.models.insert(
                name.clone(),
                LinearGradientModel {
                    baseline,
                    velocity,
                    fit_step: self.global_step,
                },
            );
        }

        Ok(())
    }

    /// Get prediction statistics.
    #[must_use]
    pub fn get_stats(&self) -> &PredictorStatistics {
        &self.stats
    }

    /// Reset predictor state.
    pub fn reset(&mut self) -> Result<()> {
        for hist in self.history.values_mut() {
            hist.clear();
        }
        self.models.clear();

        // Reset residuals to zero
        for (name, shape) in &self.shapes {
            self.residuals.insert(
                name.clone(),
                Tensor::zeros(shape.as_slice(), DType::F32, &self.device)?,
            );
        }

        self.global_step = 0;
        self.steps_since_fit = 0;
        self.warmup_complete = false;
        self.stats = PredictorStatistics::default();

        Ok(())
    }

    /// Get current global step.
    #[must_use]
    pub const fn global_step(&self) -> usize {
        self.global_step
    }

    /// Check if warmup is complete.
    #[must_use]
    pub const fn is_ready(&self) -> bool {
        self.warmup_complete
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_shapes() -> Vec<(String, Vec<usize>)> {
        vec![
            ("layer.weight".to_string(), vec![16, 32]),
            ("layer.bias".to_string(), vec![16]),
        ]
    }

    #[test]
    fn test_warmup_phase() {
        let config = DeterministicPredictionConfig::default().with_warmup_steps(5);
        let mut predictor =
            DeterministicPredictor::new(&create_shapes(), config, &Device::Cpu).unwrap();

        assert!(predictor.in_warmup());
        assert!(!predictor.is_ready());

        // Record warmup gradients
        for i in 0..5 {
            let mut grads = HashMap::new();
            grads.insert(
                "layer.weight".to_string(),
                Tensor::ones((16, 32), DType::F32, &Device::Cpu)
                    .unwrap()
                    .affine(i as f64, 0.0)
                    .unwrap(),
            );
            grads.insert(
                "layer.bias".to_string(),
                Tensor::ones(16, DType::F32, &Device::Cpu)
                    .unwrap()
                    .affine(i as f64, 0.0)
                    .unwrap(),
            );
            predictor.record_gradient(&grads, false).unwrap();
        }

        assert!(!predictor.in_warmup());
        assert!(predictor.is_ready());
    }

    #[test]
    fn test_deterministic_prediction() {
        let config = DeterministicPredictionConfig::default()
            .with_warmup_steps(3)
            .with_prediction_horizon(2);
        let device = Device::Cpu;

        // Create two identical predictors
        let shapes = create_shapes();
        let mut pred1 = DeterministicPredictor::new(&shapes, config.clone(), &device).unwrap();
        let mut pred2 = DeterministicPredictor::new(&shapes, config, &device).unwrap();

        // Feed identical history
        for i in 0..5 {
            let mut grads = HashMap::new();
            grads.insert(
                "layer.weight".to_string(),
                Tensor::ones((16, 32), DType::F32, &device)
                    .unwrap()
                    .affine(1.0 + i as f64 * 0.1, 0.0)
                    .unwrap(),
            );
            grads.insert(
                "layer.bias".to_string(),
                Tensor::ones(16, DType::F32, &device)
                    .unwrap()
                    .affine(1.0 + i as f64 * 0.1, 0.0)
                    .unwrap(),
            );
            pred1.record_gradient(&grads, false).unwrap();
            pred2.record_gradient(&grads, false).unwrap();
        }

        // Predictions should be identical
        let p1 = pred1.predict_gradient().unwrap();
        let p2 = pred2.predict_gradient().unwrap();

        for (name, t1) in &p1 {
            let t2 = p2.get(name).unwrap();
            let diff: f32 = t1
                .sub(t2)
                .unwrap()
                .abs()
                .unwrap()
                .flatten_all()
                .unwrap()
                .max(0)
                .unwrap()
                .to_scalar()
                .unwrap();
            assert!(
                diff < 1e-6,
                "Predictions should be deterministic, got diff={diff}"
            );
        }
    }

    #[test]
    fn test_linear_fit_quality() {
        // Test that linear model correctly fits linear gradient evolution
        let config = DeterministicPredictionConfig::default()
            .with_warmup_steps(5)
            .with_prediction_horizon(3);
        let device = Device::Cpu;
        let shapes = vec![("param".to_string(), vec![8])];

        let mut predictor = DeterministicPredictor::new(&shapes, config, &device).unwrap();

        // Generate perfectly linear gradients: g(t) = 1 + 0.1*t
        for t in 0..5 {
            let mut grads = HashMap::new();
            grads.insert(
                "param".to_string(),
                Tensor::ones(8, DType::F32, &device)
                    .unwrap()
                    .affine(1.0 + 0.1 * t as f64, 0.0)
                    .unwrap(),
            );
            predictor.record_gradient(&grads, false).unwrap();
        }

        // Predict next step - should be close to 1 + 0.1*5 = 1.5
        let predicted = predictor.predict_gradient().unwrap();
        let pred_vals: Vec<f32> = predicted
            .get("param")
            .unwrap()
            .to_vec1()
            .unwrap();

        // All values should be close to 1.5
        for v in &pred_vals {
            assert!(
                (*v - 1.5).abs() < 0.1,
                "Linear prediction should be accurate, got {v}"
            );
        }
    }
}