tensorlogic-train 0.1.0

Training loops, loss composition, and optimization schedules for TensorLogic
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! Prodigy optimizer - Auto-tuning learning rate optimizer (2024)
//!
//! Reference: Mishchenko, K., & Defazio, A. (2024).
//! "Prodigyopt: An Adaptive Learning Rate Method That Requires No Manual Tuning"
//! <https://arxiv.org/abs/2306.06101>
//!
//! Key innovation: Automatically estimates the learning rate scale (D) without manual tuning.
//! Uses distance from initialization to estimate appropriate step size.
//!
//! Benefits:
//! - No manual LR tuning required
//! - Works across different problem scales
//! - Adaptive to problem difficulty
//! - Combines benefits of Adam and D-Adaptation
//!
//! Usage:
//! ```rust
//! use tensorlogic_train::{ProdigyConfig, ProdigyOptimizer};
//!
//! // Create Prodigy optimizer with defaults
//! let config = ProdigyConfig::default()
//!     .with_d0(1e-6)      // Initial D estimate (small value)
//!     .with_d_coef(1.0)   // Coefficient for D adaptation
//!     .with_beta1(0.9)    // First moment decay
//!     .with_beta2(0.999); // Second moment decay
//!
//! let mut optimizer = ProdigyOptimizer::new(config);
//!
//! // Prodigy automatically adapts the learning rate!
//! // No need to manually tune LR or use schedulers
//! ```

use crate::error::TrainResult;
use crate::optimizer::{GradClipMode, Optimizer};
use scirs2_core::ndarray::Array2;
use scirs2_core::random::StdRng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Configuration for Prodigy optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProdigyConfig {
    /// Initial D estimate (default: 1e-6)
    /// D represents the distance scale from initialization
    pub d0: f64,

    /// Coefficient for D adaptation (default: 1.0)
    /// Controls how aggressively D is adapted
    pub d_coef: f64,

    /// Learning rate (default: 1.0)
    /// Note: Prodigy is relatively insensitive to this value
    pub lr: f64,

    /// First moment decay rate (default: 0.9)
    pub beta1: f64,

    /// Second moment decay rate (default: 0.999)
    pub beta2: f64,

    /// Small constant for numerical stability (default: 1e-8)
    pub eps: f64,

    /// Weight decay coefficient (default: 0.0)
    /// Applied as decoupled weight decay (like AdamW)
    pub weight_decay: f64,

    /// Gradient clipping threshold (optional)
    pub grad_clip: Option<f64>,

    /// Gradient clipping mode
    pub grad_clip_mode: GradClipMode,

    /// Whether to use bias correction (default: true)
    pub bias_correction: bool,

    /// Growth rate for D (default: infinity, meaning no limit)
    pub d_growth_rate: f64,
}

impl Default for ProdigyConfig {
    fn default() -> Self {
        Self {
            d0: 1e-6,
            d_coef: 1.0,
            lr: 1.0,
            beta1: 0.9,
            beta2: 0.999,
            eps: 1e-8,
            weight_decay: 0.0,
            grad_clip: None,
            grad_clip_mode: GradClipMode::Norm,
            bias_correction: true,
            d_growth_rate: f64::INFINITY,
        }
    }
}

impl ProdigyConfig {
    /// Create new Prodigy config with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set initial D estimate
    pub fn with_d0(mut self, d0: f64) -> Self {
        self.d0 = d0;
        self
    }

    /// Set D coefficient
    pub fn with_d_coef(mut self, d_coef: f64) -> Self {
        self.d_coef = d_coef;
        self
    }

    /// Set learning rate
    pub fn with_lr(mut self, lr: f64) -> Self {
        self.lr = lr;
        self
    }

    /// Set beta1 (first moment decay)
    pub fn with_beta1(mut self, beta1: f64) -> Self {
        self.beta1 = beta1;
        self
    }

    /// Set beta2 (second moment decay)
    pub fn with_beta2(mut self, beta2: f64) -> Self {
        self.beta2 = beta2;
        self
    }

    /// Set epsilon for numerical stability
    pub fn with_eps(mut self, eps: f64) -> Self {
        self.eps = eps;
        self
    }

    /// Set weight decay
    pub fn with_weight_decay(mut self, weight_decay: f64) -> Self {
        self.weight_decay = weight_decay;
        self
    }

    /// Set gradient clipping
    pub fn with_grad_clip(mut self, grad_clip: f64) -> Self {
        self.grad_clip = Some(grad_clip);
        self
    }

    /// Set gradient clipping mode
    pub fn with_grad_clip_mode(mut self, mode: GradClipMode) -> Self {
        self.grad_clip_mode = mode;
        self
    }

    /// Enable or disable bias correction
    pub fn with_bias_correction(mut self, bias_correction: bool) -> Self {
        self.bias_correction = bias_correction;
        self
    }

    /// Set D growth rate limit
    pub fn with_d_growth_rate(mut self, rate: f64) -> Self {
        self.d_growth_rate = rate;
        self
    }
}

/// Prodigy optimizer
///
/// Auto-tuning adaptive learning rate optimizer that estimates the distance scale D
/// from initialization to automatically set appropriate step sizes.
///
/// Key features:
/// - No manual LR tuning needed
/// - Adapts to problem scale automatically
/// - Combines Adam-style updates with D-Adaptation
/// - Maintains first and second moment estimates
pub struct ProdigyOptimizer {
    config: ProdigyConfig,
    /// First moment estimates (momentum)
    first_moments: HashMap<String, Array2<f64>>,
    /// Second moment estimates (variance)
    second_moments: HashMap<String, Array2<f64>>,
    /// Initial parameters (for distance computation)
    initial_params: HashMap<String, Array2<f64>>,
    /// Current step count
    step: usize,
    /// Estimated distance scale D
    d: f64,
    /// Sum of gradient norms (for D estimation)
    sum_grad_norm: f64,
}

impl ProdigyOptimizer {
    /// Create new Prodigy optimizer with given config
    pub fn new(config: ProdigyConfig) -> Self {
        Self {
            config,
            first_moments: HashMap::new(),
            second_moments: HashMap::new(),
            initial_params: HashMap::new(),
            step: 0,
            d: 0.0, // Will be initialized to d0 on first step
            sum_grad_norm: 0.0,
        }
    }

    /// Get current D estimate
    pub fn get_d(&self) -> f64 {
        self.d
    }

    /// Get current step count
    pub fn get_step(&self) -> usize {
        self.step
    }

    /// Compute parameter distance from initialization
    fn compute_distance(&self, parameters: &HashMap<String, Array2<f64>>) -> f64 {
        let mut distance_sq = 0.0;

        for (name, param) in parameters {
            if let Some(init_param) = self.initial_params.get(name) {
                let diff = param - init_param;
                distance_sq += diff.mapv(|x| x * x).sum();
            }
        }

        distance_sq.sqrt()
    }

    /// Update D estimate based on gradients and parameters
    fn update_d(&mut self, parameters: &HashMap<String, Array2<f64>>, grad_norm: f64) {
        // Initialize D on first step
        if self.step == 1 {
            self.d = self.config.d0;
            return;
        }

        // Accumulate gradient norms
        self.sum_grad_norm += grad_norm;

        // Compute parameter distance from initialization
        let param_distance = self.compute_distance(parameters);

        // Estimate D based on ratio of distance to accumulated gradient norm
        if self.sum_grad_norm > 0.0 {
            let d_estimate = self.config.d_coef * param_distance / self.sum_grad_norm;

            // Apply growth rate limit if specified
            if self.config.d_growth_rate.is_finite() {
                let max_d = self.d * (1.0 + self.config.d_growth_rate);
                self.d = d_estimate.min(max_d).max(self.config.d0);
            } else {
                self.d = d_estimate.max(self.config.d0);
            }
        }
    }

    /// Compute total gradient norm
    fn compute_gradient_norm(&self, gradients: &HashMap<String, Array2<f64>>) -> f64 {
        let mut norm_sq = 0.0;
        for grad in gradients.values() {
            norm_sq += grad.mapv(|x| x * x).sum();
        }
        norm_sq.sqrt()
    }

    /// Apply gradient clipping
    fn clip_gradients(
        &self,
        gradients: &mut HashMap<String, Array2<f64>>,
        _rng: Option<&mut StdRng>,
    ) -> TrainResult<()> {
        if let Some(max_val) = self.config.grad_clip {
            match self.config.grad_clip_mode {
                GradClipMode::Value => {
                    // Clip by value
                    for grad in gradients.values_mut() {
                        grad.mapv_inplace(|x| x.max(-max_val).min(max_val));
                    }
                }
                GradClipMode::Norm => {
                    // Clip by norm
                    let total_norm = self.compute_gradient_norm(gradients);
                    if total_norm > max_val {
                        let scale = max_val / (total_norm + self.config.eps);
                        for grad in gradients.values_mut() {
                            grad.mapv_inplace(|x| x * scale);
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

impl Optimizer for ProdigyOptimizer {
    fn zero_grad(&mut self) {
        // Prodigy doesn't maintain gradients separately, so this is a no-op
    }

    fn get_lr(&self) -> f64 {
        self.config.lr
    }

    fn set_lr(&mut self, lr: f64) {
        self.config.lr = lr;
    }

    fn step(
        &mut self,
        parameters: &mut HashMap<String, Array2<f64>>,
        gradients: &HashMap<String, Array2<f64>>,
    ) -> TrainResult<()> {
        // Increment step counter
        self.step += 1;

        // Save initial parameters on first step
        if self.step == 1 {
            for (name, param) in parameters.iter() {
                self.initial_params.insert(name.clone(), param.clone());
            }
        }

        // Clone gradients if clipping is needed
        let gradients = if self.config.grad_clip.is_some() {
            let mut clipped = HashMap::new();
            for (name, grad) in gradients.iter() {
                clipped.insert(name.clone(), grad.clone());
            }
            self.clip_gradients(&mut clipped, None)?;
            clipped
        } else {
            gradients.clone()
        };

        // Compute gradient norm for D estimation
        let grad_norm = self.compute_gradient_norm(&gradients);

        // Update D estimate
        self.update_d(parameters, grad_norm);

        // Compute effective learning rate
        let effective_lr = self.config.lr * self.d;

        // Bias correction factors (if enabled)
        let bias_correction1 = if self.config.bias_correction {
            1.0 - self.config.beta1.powi(self.step as i32)
        } else {
            1.0
        };
        let bias_correction2 = if self.config.bias_correction {
            1.0 - self.config.beta2.powi(self.step as i32)
        } else {
            1.0
        };

        // Update parameters
        for (name, param) in parameters.iter_mut() {
            let grad = match gradients.get(name) {
                Some(g) => g,
                None => continue,
            };

            // Initialize moments if needed
            let m = self
                .first_moments
                .entry(name.clone())
                .or_insert_with(|| Array2::zeros(grad.raw_dim()));
            let v = self
                .second_moments
                .entry(name.clone())
                .or_insert_with(|| Array2::zeros(grad.raw_dim()));

            // Update biased first moment estimate: m_t = beta1 * m_{t-1} + (1 - beta1) * g_t
            *m = &*m * self.config.beta1 + grad * (1.0 - self.config.beta1);

            // Update biased second moment estimate: v_t = beta2 * v_{t-1} + (1 - beta2) * g_t^2
            let grad_sq = grad.mapv(|x| x * x);
            *v = &*v * self.config.beta2 + &grad_sq * (1.0 - self.config.beta2);

            // Compute bias-corrected moments
            let m_hat = m.mapv(|x| x / bias_correction1);
            let v_hat = v.mapv(|x| x / bias_correction2);

            // Compute update: delta = lr * D * m_hat / (sqrt(v_hat) + eps)
            let update = &m_hat / &v_hat.mapv(|x| x.sqrt() + self.config.eps);

            // Apply weight decay (decoupled, like AdamW)
            if self.config.weight_decay > 0.0 {
                param.mapv_inplace(|x| x * (1.0 - effective_lr * self.config.weight_decay));
            }

            // Update parameters: theta_t = theta_{t-1} - lr * D * update
            *param = &*param - &update * effective_lr;
        }

        Ok(())
    }

    fn state_dict(&self) -> HashMap<String, Vec<f64>> {
        let mut state = HashMap::new();

        // Save scalar values
        state.insert("step".to_string(), vec![self.step as f64]);
        state.insert("d".to_string(), vec![self.d]);
        state.insert("sum_grad_norm".to_string(), vec![self.sum_grad_norm]);

        // Save config values
        state.insert("d0".to_string(), vec![self.config.d0]);
        state.insert("d_coef".to_string(), vec![self.config.d_coef]);
        state.insert("lr".to_string(), vec![self.config.lr]);
        state.insert("beta1".to_string(), vec![self.config.beta1]);
        state.insert("beta2".to_string(), vec![self.config.beta2]);
        state.insert("eps".to_string(), vec![self.config.eps]);
        state.insert("weight_decay".to_string(), vec![self.config.weight_decay]);

        state
    }

    fn load_state_dict(&mut self, state: HashMap<String, Vec<f64>>) {
        // Load scalar values
        if let Some(v) = state.get("step") {
            if !v.is_empty() {
                self.step = v[0] as usize;
            }
        }
        if let Some(v) = state.get("d") {
            if !v.is_empty() {
                self.d = v[0];
            }
        }
        if let Some(v) = state.get("sum_grad_norm") {
            if !v.is_empty() {
                self.sum_grad_norm = v[0];
            }
        }

        // Load config values
        if let Some(v) = state.get("d0") {
            if !v.is_empty() {
                self.config.d0 = v[0];
            }
        }
        if let Some(v) = state.get("d_coef") {
            if !v.is_empty() {
                self.config.d_coef = v[0];
            }
        }
        if let Some(v) = state.get("lr") {
            if !v.is_empty() {
                self.config.lr = v[0];
            }
        }
        if let Some(v) = state.get("beta1") {
            if !v.is_empty() {
                self.config.beta1 = v[0];
            }
        }
        if let Some(v) = state.get("beta2") {
            if !v.is_empty() {
                self.config.beta2 = v[0];
            }
        }
        if let Some(v) = state.get("eps") {
            if !v.is_empty() {
                self.config.eps = v[0];
            }
        }
        if let Some(v) = state.get("weight_decay") {
            if !v.is_empty() {
                self.config.weight_decay = v[0];
            }
        }
    }
}

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

    #[test]
    fn test_prodigy_config_default() {
        let config = ProdigyConfig::default();
        assert_eq!(config.d0, 1e-6);
        assert_eq!(config.d_coef, 1.0);
        assert_eq!(config.lr, 1.0);
        assert_eq!(config.beta1, 0.9);
        assert_eq!(config.beta2, 0.999);
        assert_eq!(config.eps, 1e-8);
        assert_eq!(config.weight_decay, 0.0);
    }

    #[test]
    fn test_prodigy_config_builder() {
        let config = ProdigyConfig::default()
            .with_d0(1e-5)
            .with_d_coef(2.0)
            .with_lr(0.5)
            .with_beta1(0.95)
            .with_beta2(0.9999)
            .with_eps(1e-7)
            .with_weight_decay(0.01)
            .with_grad_clip(1.0)
            .with_bias_correction(false)
            .with_d_growth_rate(0.1);

        assert_eq!(config.d0, 1e-5);
        assert_eq!(config.d_coef, 2.0);
        assert_eq!(config.lr, 0.5);
        assert_eq!(config.beta1, 0.95);
        assert_eq!(config.beta2, 0.9999);
        assert_eq!(config.eps, 1e-7);
        assert_eq!(config.weight_decay, 0.01);
        assert_eq!(config.grad_clip, Some(1.0));
        assert!(!config.bias_correction);
        assert_eq!(config.d_growth_rate, 0.1);
    }

    #[test]
    fn test_prodigy_initialization() {
        let config = ProdigyConfig::default();
        let optimizer = ProdigyOptimizer::new(config);

        assert_eq!(optimizer.get_step(), 0);
        assert_eq!(optimizer.get_d(), 0.0);
    }

    #[test]
    fn test_prodigy_first_step() {
        let config = ProdigyConfig::default().with_d0(1e-6);
        let mut optimizer = ProdigyOptimizer::new(config);

        let mut params = HashMap::new();
        params.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 0.1));

        optimizer.step(&mut params, &grads).expect("unwrap");

        assert_eq!(optimizer.get_step(), 1);
        assert_eq!(optimizer.get_d(), 1e-6); // D initialized to d0
    }

    #[test]
    fn test_prodigy_d_adaptation() {
        let config = ProdigyConfig::default().with_d0(1e-6).with_d_coef(1.0);
        let mut optimizer = ProdigyOptimizer::new(config);

        let mut params = HashMap::new();
        params.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        // First step
        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 0.1));
        optimizer.step(&mut params, &grads).expect("unwrap");

        let d_after_step1 = optimizer.get_d();
        assert_eq!(d_after_step1, 1e-6);

        // Second step - D should adapt
        optimizer.step(&mut params, &grads).expect("unwrap");

        let d_after_step2 = optimizer.get_d();
        assert!(d_after_step2 >= 1e-6); // D should increase or stay same
    }

    #[test]
    fn test_prodigy_parameter_update() {
        let config = ProdigyConfig::default();
        let mut optimizer = ProdigyOptimizer::new(config);

        let mut params = HashMap::new();
        let initial_value = 1.0;
        params.insert("w".to_string(), Array2::from_elem((2, 2), initial_value));

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 0.5));

        optimizer.step(&mut params, &grads).expect("unwrap");

        // Parameters should be updated (decreased since gradient is positive)
        let w = params.get("w").expect("unwrap");
        assert!(w[[0, 0]] < initial_value);
    }

    #[test]
    fn test_prodigy_weight_decay() {
        let config = ProdigyConfig::default().with_weight_decay(0.01);
        let mut optimizer = ProdigyOptimizer::new(config);

        let mut params = HashMap::new();
        params.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 0.0));

        // With weight decay and zero gradient, parameters should decay
        let initial_sum: f64 = params.get("w").expect("unwrap").sum();
        optimizer.step(&mut params, &grads).expect("unwrap");
        let final_sum: f64 = params.get("w").expect("unwrap").sum();

        assert!(final_sum < initial_sum);
    }

    #[test]
    fn test_prodigy_gradient_clipping_by_norm() {
        let config = ProdigyConfig::default().with_grad_clip(0.1);
        let mut optimizer = ProdigyOptimizer::new(config);

        let mut params = HashMap::new();
        params.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 10.0)); // Large gradient

        // Should not panic, gradients should be clipped
        optimizer.step(&mut params, &grads).expect("unwrap");

        // Parameters should still be updated, but with clipped gradients
        let w = params.get("w").expect("unwrap");
        assert!(w[[0, 0]] < 1.0);
    }

    #[test]
    fn test_prodigy_state_dict() {
        let config = ProdigyConfig::default();
        let mut optimizer = ProdigyOptimizer::new(config);

        let mut params = HashMap::new();
        params.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 0.1));

        // Take a few steps
        for _ in 0..3 {
            optimizer.step(&mut params, &grads).expect("unwrap");
        }

        let state = optimizer.state_dict();
        assert!(state.contains_key("step"));
        assert!(state.contains_key("d"));
        assert!(state.contains_key("sum_grad_norm"));
    }

    #[test]
    fn test_prodigy_load_state_dict() {
        let config = ProdigyConfig::default();
        let mut optimizer1 = ProdigyOptimizer::new(config.clone());

        let mut params = HashMap::new();
        params.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 0.1));

        // Take a few steps with optimizer1
        for _ in 0..3 {
            optimizer1.step(&mut params, &grads).expect("unwrap");
        }

        let state = optimizer1.state_dict();

        // Create new optimizer and load state
        let mut optimizer2 = ProdigyOptimizer::new(config);
        optimizer2.load_state_dict(state);

        assert_eq!(optimizer1.get_step(), optimizer2.get_step());
        assert_eq!(optimizer1.get_d(), optimizer2.get_d());
    }

    #[test]
    fn test_prodigy_bias_correction() {
        let config_with_bc = ProdigyConfig::default().with_bias_correction(true);
        let config_without_bc = ProdigyConfig::default().with_bias_correction(false);

        let mut opt_with_bc = ProdigyOptimizer::new(config_with_bc);
        let mut opt_without_bc = ProdigyOptimizer::new(config_without_bc);

        let mut params1 = HashMap::new();
        params1.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        let mut params2 = params1.clone();

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 0.1));

        opt_with_bc.step(&mut params1, &grads).expect("unwrap");
        opt_without_bc.step(&mut params2, &grads).expect("unwrap");

        // Results should be different due to bias correction
        let w1 = params1.get("w").expect("unwrap");
        let w2 = params2.get("w").expect("unwrap");

        // They should be different (bias correction affects the updates)
        let diff = (w1[[0, 0]] - w2[[0, 0]]).abs();
        assert!(diff > 1e-10);
    }

    #[test]
    fn test_prodigy_d_growth_rate_limit() {
        let config = ProdigyConfig::default()
            .with_d0(1e-6)
            .with_d_growth_rate(0.1); // 10% max growth per step

        let mut optimizer = ProdigyOptimizer::new(config);

        let mut params = HashMap::new();
        params.insert("w".to_string(), Array2::from_elem((2, 2), 1.0));

        let mut grads = HashMap::new();
        grads.insert("w".to_string(), Array2::from_elem((2, 2), 1.0)); // Large gradient

        // First step
        optimizer.step(&mut params, &grads).expect("unwrap");
        let d1 = optimizer.get_d();

        // Second step
        optimizer.step(&mut params, &grads).expect("unwrap");
        let d2 = optimizer.get_d();

        // D should not grow more than 10% per step
        if d2 > d1 {
            let growth_ratio = d2 / d1;
            assert!(growth_ratio <= 1.11); // Allow small numerical error
        }
    }
}