rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/// Optimization algorithms for neural networks
/// ニューラルネットワーク用最適化アルゴリズム
pub mod adamw;
pub mod lr_scheduler;

// Common structures and traits for Phase 2 refactoring
/// Common optimizer structures and traits for Adam-based optimizers
pub mod common;

// Phase 2: Advanced Adam-based optimizers
/// Adamax optimizer - Adam with infinity norm
pub mod adamax;
/// NAdam optimizer - Nesterov-accelerated Adam
pub mod nadam;
/// RAdam optimizer - Rectified Adam with variance rectification
pub mod radam;

// Advanced optimizers
/// AdaBound optimizer bridging Adam and SGD
pub mod adabound;
/// LAMB optimizer for large batch training
pub mod lamb;
/// L-BFGS second-order optimizer with enhanced line search
pub mod lbfgs;

/// Optimization utilities for memory efficiency and numerical stability
pub mod utils;

/// Performance benchmarks and profiling for optimizers
pub mod benchmarks;

// Standard optimizer exports
pub use adamw::AdamW;
pub use lr_scheduler::{
    AnnealStrategy, CosineAnnealingLR, ExponentialLR, LRScheduler, MultiStepLR, OneCycleLR,
    PlateauMode, PolynomialLR, ReduceLROnPlateau, StepLR, ThresholdMode, WarmupScheduler,
};

// Phase 2 optimizer exports
pub use adamax::Adamax;
pub use nadam::NAdam;
pub use radam::RAdam;

// Re-export advanced optimizers
pub use adabound::AdaBound;
pub use lamb::LAMB;
pub use lbfgs::{LineSearchMethod, LBFGS};

/// SGD (Stochastic Gradient Descent) optimizer module
/// SGD(確率的勾配降下法)オプティマイザモジュール
pub mod sgd {
    pub use super::SGD;
}

use crate::tensor::Tensor;
use std::collections::HashMap;
use std::ops::Add;

/// Optimizer trait for parameter updates
pub trait Optimizer {
    /// Update parameters with gradients
    fn step(&mut self, param: &Tensor<f32>, grad: &Tensor<f32>);

    /// Zero gradients (if needed)
    fn zero_grad(&mut self) {}

    /// Get learning rate
    fn learning_rate(&self) -> f32;

    /// Set learning rate
    fn set_learning_rate(&mut self, lr: f32);

    /// Get optimizer state
    fn state_dict(&self) -> HashMap<String, f32>;

    /// Load optimizer state
    fn load_state_dict(&mut self, state: HashMap<String, f32>);
}

/// Stochastic Gradient Descent optimizer
#[derive(Debug, Clone)]
pub struct SGD {
    learning_rate: f32,
    momentum: f32,
    dampening: f32,
    weight_decay: f32,
    nesterov: bool,
    momentum_buffers: HashMap<usize, Tensor<f32>>,
}

impl SGD {
    /// Create new SGD optimizer
    pub fn new(learning_rate: f32) -> Self {
        Self::with_momentum(learning_rate, 0.0)
    }

    /// Create new SGD optimizer with momentum
    pub fn with_momentum(learning_rate: f32, momentum: f32) -> Self {
        Self {
            learning_rate,
            momentum,
            dampening: 0.0,
            weight_decay: 0.0,
            nesterov: false,
            momentum_buffers: HashMap::new(),
        }
    }

    /// Create SGD with weight decay
    pub fn with_weight_decay(learning_rate: f32, momentum: f32, weight_decay: f32) -> Self {
        Self {
            learning_rate,
            momentum,
            dampening: 0.0,
            weight_decay,
            nesterov: false,
            momentum_buffers: HashMap::new(),
        }
    }

    /// Create SGD with Nesterov momentum
    pub fn with_nesterov(learning_rate: f32, momentum: f32, nesterov: bool) -> Self {
        Self {
            learning_rate,
            momentum,
            dampening: 0.0,
            weight_decay: 0.0,
            nesterov,
            momentum_buffers: HashMap::new(),
        }
    }

    /// Set dampening factor
    pub fn set_dampening(&mut self, dampening: f32) {
        self.dampening = dampening;
    }
}

impl Optimizer for SGD {
    fn step(&mut self, param: &Tensor<f32>, grad: &Tensor<f32>) {
        let param_id = param.as_ptr() as usize;
        let mut d_p = grad.clone();

        // Apply weight decay
        if self.weight_decay != 0.0 {
            let weight_decay_term = param * self.weight_decay;
            d_p = &d_p + &weight_decay_term;
        }

        // Apply momentum
        if self.momentum != 0.0 {
            let buf = if let Some(momentum_buffer) = self.momentum_buffers.get(&param_id) {
                let momentum_term = momentum_buffer * self.momentum;
                let dampening_term = &d_p * (1.0 - self.dampening);
                (&momentum_term) + (&dampening_term)
            } else {
                d_p.clone()
            };

            self.momentum_buffers.insert(param_id, buf.clone());

            if self.nesterov {
                let momentum_term = &buf * self.momentum;
                d_p = &d_p + &momentum_term;
            } else {
                d_p = buf;
            }
        }

        // Update parameters
        let lr_term = &d_p * self.learning_rate;
        let update = param - &lr_term;
        param.copy_from(&update);
    }

    fn learning_rate(&self) -> f32 {
        self.learning_rate
    }

    fn set_learning_rate(&mut self, lr: f32) {
        self.learning_rate = lr;
    }

    fn state_dict(&self) -> HashMap<String, f32> {
        let mut state = HashMap::new();
        state.insert("learning_rate".to_string(), self.learning_rate);
        state.insert("momentum".to_string(), self.momentum);
        state.insert("dampening".to_string(), self.dampening);
        state.insert("weight_decay".to_string(), self.weight_decay);
        state.insert(
            "nesterov".to_string(),
            if self.nesterov { 1.0 } else { 0.0 },
        );
        state
    }

    fn load_state_dict(&mut self, state: HashMap<String, f32>) {
        if let Some(&lr) = state.get("learning_rate") {
            self.learning_rate = lr;
        }
        if let Some(&momentum) = state.get("momentum") {
            self.momentum = momentum;
        }
        if let Some(&dampening) = state.get("dampening") {
            self.dampening = dampening;
        }
        if let Some(&weight_decay) = state.get("weight_decay") {
            self.weight_decay = weight_decay;
        }
        if let Some(&nesterov) = state.get("nesterov") {
            self.nesterov = nesterov > 0.5;
        }
    }
}

/// Adam optimizer
#[derive(Debug, Clone)]
pub struct Adam {
    learning_rate: f32,
    beta1: f32,
    beta2: f32,
    epsilon: f32,
    weight_decay: f32,
    amsgrad: bool,
    step_count: usize,
    exp_avg: HashMap<usize, Tensor<f32>>,
    exp_avg_sq: HashMap<usize, Tensor<f32>>,
    max_exp_avg_sq: HashMap<usize, Tensor<f32>>,
}

impl Adam {
    /// Create new Adam optimizer
    pub fn new(learning_rate: f32, beta1: f32, beta2: f32, epsilon: f32) -> Self {
        Self {
            learning_rate,
            beta1,
            beta2,
            epsilon,
            weight_decay: 0.0,
            amsgrad: false,
            step_count: 0,
            exp_avg: HashMap::new(),
            exp_avg_sq: HashMap::new(),
            max_exp_avg_sq: HashMap::new(),
        }
    }

    /// Create Adam with default parameters
    pub fn default_params(learning_rate: f32) -> Self {
        Self::new(learning_rate, 0.9, 0.999, 1e-8)
    }

    /// Create Adam with weight decay (AdamW)
    pub fn with_weight_decay(
        learning_rate: f32,
        beta1: f32,
        beta2: f32,
        epsilon: f32,
        weight_decay: f32,
    ) -> Self {
        Self {
            learning_rate,
            beta1,
            beta2,
            epsilon,
            weight_decay,
            amsgrad: false,
            step_count: 0,
            exp_avg: HashMap::new(),
            exp_avg_sq: HashMap::new(),
            max_exp_avg_sq: HashMap::new(),
        }
    }

    /// Create Adam with AMSGrad
    pub fn with_amsgrad(
        learning_rate: f32,
        beta1: f32,
        beta2: f32,
        epsilon: f32,
        amsgrad: bool,
    ) -> Self {
        Self {
            learning_rate,
            beta1,
            beta2,
            epsilon,
            weight_decay: 0.0,
            amsgrad,
            step_count: 0,
            exp_avg: HashMap::new(),
            exp_avg_sq: HashMap::new(),
            max_exp_avg_sq: HashMap::new(),
        }
    }
}

impl Optimizer for Adam {
    fn step(&mut self, param: &Tensor<f32>, grad: &Tensor<f32>) {
        let param_id = param.as_ptr() as usize;
        self.step_count += 1;

        let mut d_p = grad.clone();

        // Apply weight decay
        if self.weight_decay != 0.0 {
            let weight_decay_term = param * self.weight_decay;
            d_p = &d_p + &weight_decay_term;
        }

        // Get or initialize momentum buffers
        let exp_avg = if let Some(avg) = self.exp_avg.get(&param_id) {
            let beta1_term = avg * self.beta1;
            let one_minus_beta1_term = (&d_p) * (1.0 - self.beta1);
            (&beta1_term) + (&one_minus_beta1_term)
        } else {
            d_p.clone() * (1.0 - self.beta1)
        };

        let exp_avg_sq = if let Some(avg_sq) = self.exp_avg_sq.get(&param_id) {
            let beta2_term = avg_sq * self.beta2;
            let d_p_squared = &d_p * &d_p;
            let one_minus_beta2_term = (&d_p_squared) * (1.0 - self.beta2);
            (&beta2_term) + (&one_minus_beta2_term)
        } else {
            let d_p_squared = &d_p * &d_p;
            d_p_squared * (1.0 - self.beta2)
        };

        self.exp_avg.insert(param_id, exp_avg.clone());
        self.exp_avg_sq.insert(param_id, exp_avg_sq.clone());

        // Bias correction
        let bias_correction1 = 1.0 - self.beta1.powi(self.step_count as i32);
        let bias_correction2 = 1.0 - self.beta2.powi(self.step_count as i32);

        let corrected_exp_avg = &exp_avg / bias_correction1;
        let corrected_exp_avg_sq = &exp_avg_sq / bias_correction2;

        // Compute update
        let denom = if self.amsgrad {
            let max_exp_avg_sq = if let Some(max_avg_sq) = self.max_exp_avg_sq.get(&param_id) {
                max_avg_sq
                    .maximum(&corrected_exp_avg_sq)
                    .unwrap_or_else(|_| corrected_exp_avg_sq.clone())
            } else {
                corrected_exp_avg_sq.clone()
            };
            self.max_exp_avg_sq.insert(param_id, max_exp_avg_sq.clone());
            max_exp_avg_sq.sqrt() + self.epsilon
        } else {
            corrected_exp_avg_sq.sqrt() + self.epsilon
        };

        let step_size = self.learning_rate;
        let update_term = (&corrected_exp_avg / &denom) * step_size;
        let update = param - &update_term;
        param.copy_from(&update);
    }

    fn learning_rate(&self) -> f32 {
        self.learning_rate
    }

    fn set_learning_rate(&mut self, lr: f32) {
        self.learning_rate = lr;
    }

    fn state_dict(&self) -> HashMap<String, f32> {
        let mut state = HashMap::new();
        state.insert("learning_rate".to_string(), self.learning_rate);
        state.insert("beta1".to_string(), self.beta1);
        state.insert("beta2".to_string(), self.beta2);
        state.insert("epsilon".to_string(), self.epsilon);
        state.insert("weight_decay".to_string(), self.weight_decay);
        state.insert("amsgrad".to_string(), if self.amsgrad { 1.0 } else { 0.0 });
        state.insert("step_count".to_string(), self.step_count as f32);
        state
    }

    fn load_state_dict(&mut self, state: HashMap<String, f32>) {
        if let Some(&lr) = state.get("learning_rate") {
            self.learning_rate = lr;
        }
        if let Some(&beta1) = state.get("beta1") {
            self.beta1 = beta1;
        }
        if let Some(&beta2) = state.get("beta2") {
            self.beta2 = beta2;
        }
        if let Some(&epsilon) = state.get("epsilon") {
            self.epsilon = epsilon;
        }
        if let Some(&weight_decay) = state.get("weight_decay") {
            self.weight_decay = weight_decay;
        }
        if let Some(&amsgrad) = state.get("amsgrad") {
            self.amsgrad = amsgrad > 0.5;
        }
        if let Some(&step_count) = state.get("step_count") {
            self.step_count = step_count as usize;
        }
    }
}

/// RMSprop optimizer
#[derive(Debug, Clone)]
pub struct RMSprop {
    learning_rate: f32,
    alpha: f32,
    epsilon: f32,
    weight_decay: f32,
    momentum: f32,
    centered: bool,
    step_count: usize,
    square_avg: HashMap<usize, Tensor<f32>>,
    momentum_buffer: HashMap<usize, Tensor<f32>>,
    grad_avg: HashMap<usize, Tensor<f32>>, // for centered variant
}

impl RMSprop {
    /// Create new RMSprop optimizer
    /// 新しいRMSpropオプティマイザーを作成
    pub fn new(learning_rate: f32, alpha: f32, epsilon: f32) -> Self {
        Self {
            learning_rate,
            alpha,
            epsilon,
            weight_decay: 0.0,
            momentum: 0.0,
            centered: false,
            step_count: 0,
            square_avg: HashMap::new(),
            momentum_buffer: HashMap::new(),
            grad_avg: HashMap::new(),
        }
    }

    /// Create RMSprop with default parameters
    /// デフォルトパラメータでRMSpropを作成
    pub fn default_params(learning_rate: f32) -> Self {
        Self::new(learning_rate, 0.99, 1e-8)
    }

    /// Create RMSprop with momentum
    /// モーメンタム付きRMSpropを作成
    pub fn with_momentum(learning_rate: f32, alpha: f32, epsilon: f32, momentum: f32) -> Self {
        Self {
            learning_rate,
            alpha,
            epsilon,
            weight_decay: 0.0,
            momentum,
            centered: false,
            step_count: 0,
            square_avg: HashMap::new(),
            momentum_buffer: HashMap::new(),
            grad_avg: HashMap::new(),
        }
    }

    /// Create centered RMSprop
    /// センタード RMSpropを作成
    pub fn centered(learning_rate: f32, alpha: f32, epsilon: f32, centered: bool) -> Self {
        Self {
            learning_rate,
            alpha,
            epsilon,
            weight_decay: 0.0,
            momentum: 0.0,
            centered,
            step_count: 0,
            square_avg: HashMap::new(),
            momentum_buffer: HashMap::new(),
            grad_avg: HashMap::new(),
        }
    }

    /// Create RMSprop with weight decay
    /// 重み減衰付きRMSpropを作成
    pub fn with_weight_decay(
        learning_rate: f32,
        alpha: f32,
        epsilon: f32,
        weight_decay: f32,
    ) -> Self {
        Self {
            learning_rate,
            alpha,
            epsilon,
            weight_decay,
            momentum: 0.0,
            centered: false,
            step_count: 0,
            square_avg: HashMap::new(),
            momentum_buffer: HashMap::new(),
            grad_avg: HashMap::new(),
        }
    }
}

impl Optimizer for RMSprop {
    fn step(&mut self, param: &Tensor<f32>, grad: &Tensor<f32>) {
        let param_id = param.as_ptr() as usize;
        self.step_count += 1;

        let mut d_p = grad.clone();

        // Apply weight decay
        if self.weight_decay != 0.0 {
            let weight_decay_term = param * self.weight_decay;
            d_p = &d_p + &weight_decay_term;
        }

        // Update biased second raw moment estimate
        let square_avg = if let Some(sq_avg) = self.square_avg.get(&param_id) {
            let alpha_term = sq_avg * self.alpha;
            let grad_squared = &d_p * &d_p;
            let one_minus_alpha_term = (&grad_squared) * (1.0 - self.alpha);
            (&alpha_term) + (&one_minus_alpha_term)
        } else {
            let grad_squared = &d_p * &d_p;
            grad_squared * (1.0 - self.alpha)
        };

        self.square_avg.insert(param_id, square_avg.clone());

        let avg = if self.centered {
            // Centered variant: subtract the squared mean of gradients
            let grad_avg = if let Some(g_avg) = self.grad_avg.get(&param_id) {
                let alpha_term = g_avg * self.alpha;
                let one_minus_alpha_term = (&d_p) * (1.0 - self.alpha);
                (&alpha_term) + (&one_minus_alpha_term)
            } else {
                d_p.clone() * (1.0 - self.alpha)
            };

            self.grad_avg.insert(param_id, grad_avg.clone());

            // avg = square_avg - grad_avg^2
            let grad_avg_squared = &grad_avg * &grad_avg;
            &square_avg - &grad_avg_squared
        } else {
            square_avg.clone()
        };

        // Compute update
        let denom = avg.sqrt() + self.epsilon;

        if self.momentum > 0.0 {
            // Apply momentum
            let buf = if let Some(momentum_buf) = self.momentum_buffer.get(&param_id) {
                let momentum_term = momentum_buf * self.momentum;
                let grad_term = (&d_p / &denom) * self.learning_rate;
                (&momentum_term) + (&grad_term)
            } else {
                (&d_p / &denom) * self.learning_rate
            };

            self.momentum_buffer.insert(param_id, buf.clone());
            let update = param - &buf;
            param.copy_from(&update);
        } else {
            // No momentum
            let update_term = (&d_p / &denom) * self.learning_rate;
            let update = param - &update_term;
            param.copy_from(&update);
        }
    }

    fn learning_rate(&self) -> f32 {
        self.learning_rate
    }

    fn set_learning_rate(&mut self, lr: f32) {
        self.learning_rate = lr;
    }

    fn state_dict(&self) -> HashMap<String, f32> {
        let mut state = HashMap::new();
        state.insert("learning_rate".to_string(), self.learning_rate);
        state.insert("alpha".to_string(), self.alpha);
        state.insert("epsilon".to_string(), self.epsilon);
        state.insert("weight_decay".to_string(), self.weight_decay);
        state.insert("momentum".to_string(), self.momentum);
        state.insert(
            "centered".to_string(),
            if self.centered { 1.0 } else { 0.0 },
        );
        state.insert("step_count".to_string(), self.step_count as f32);
        state
    }

    fn load_state_dict(&mut self, state: HashMap<String, f32>) {
        if let Some(&lr) = state.get("learning_rate") {
            self.learning_rate = lr;
        }
        if let Some(&alpha) = state.get("alpha") {
            self.alpha = alpha;
        }
        if let Some(&epsilon) = state.get("epsilon") {
            self.epsilon = epsilon;
        }
        if let Some(&weight_decay) = state.get("weight_decay") {
            self.weight_decay = weight_decay;
        }
        if let Some(&momentum) = state.get("momentum") {
            self.momentum = momentum;
        }
        if let Some(&centered) = state.get("centered") {
            self.centered = centered > 0.5;
        }
        if let Some(&step_count) = state.get("step_count") {
            self.step_count = step_count as usize;
        }
    }
}

/// AdaGrad optimizer
#[derive(Debug, Clone)]
pub struct AdaGrad {
    learning_rate: f32,
    epsilon: f32,
    weight_decay: f32,
    initial_accumulator_value: f32,
    step_count: usize,
    sum_of_squares: HashMap<usize, Tensor<f32>>,
}

impl AdaGrad {
    /// Create new AdaGrad optimizer
    /// 新しいAdaGradオプティマイザーを作成
    pub fn new(learning_rate: f32, epsilon: f32) -> Self {
        Self {
            learning_rate,
            epsilon,
            weight_decay: 0.0,
            initial_accumulator_value: 0.0,
            step_count: 0,
            sum_of_squares: HashMap::new(),
        }
    }

    /// Create AdaGrad with default parameters
    /// デフォルトパラメータでAdaGradを作成
    pub fn default_params(learning_rate: f32) -> Self {
        Self::new(learning_rate, 1e-10)
    }

    /// Create AdaGrad with weight decay
    /// 重み減衰付きAdaGradを作成
    pub fn with_weight_decay(learning_rate: f32, epsilon: f32, weight_decay: f32) -> Self {
        Self {
            learning_rate,
            epsilon,
            weight_decay,
            initial_accumulator_value: 0.0,
            step_count: 0,
            sum_of_squares: HashMap::new(),
        }
    }

    /// Create AdaGrad with initial accumulator value
    /// 初期アキュムレータ値付きAdaGradを作成
    pub fn with_initial_accumulator(
        learning_rate: f32,
        epsilon: f32,
        initial_accumulator_value: f32,
    ) -> Self {
        Self {
            learning_rate,
            epsilon,
            weight_decay: 0.0,
            initial_accumulator_value,
            step_count: 0,
            sum_of_squares: HashMap::new(),
        }
    }
}

impl Optimizer for AdaGrad {
    fn step(&mut self, param: &Tensor<f32>, grad: &Tensor<f32>) {
        let param_id = param.as_ptr() as usize;
        self.step_count += 1;

        let mut d_p = grad.clone();

        // Apply weight decay
        if self.weight_decay != 0.0 {
            let weight_decay_term = param * self.weight_decay;
            d_p = &d_p + &weight_decay_term;
        }

        // Update sum of squares of gradients
        let sum_of_squares = if let Some(sos) = self.sum_of_squares.get(&param_id) {
            let grad_squared = &d_p * &d_p;
            sos + &grad_squared
        } else {
            // Initialize with initial accumulator value if first step
            let grad_squared = &d_p * &d_p;
            if self.initial_accumulator_value > 0.0 {
                grad_squared + self.initial_accumulator_value
            } else {
                grad_squared
            }
        };

        self.sum_of_squares.insert(param_id, sum_of_squares.clone());

        // Compute adaptive learning rate
        let std = sum_of_squares.sqrt() + self.epsilon;
        let update_term = (&d_p / &std) * self.learning_rate;
        let update = param - &update_term;
        param.copy_from(&update);
    }

    fn learning_rate(&self) -> f32 {
        self.learning_rate
    }

    fn set_learning_rate(&mut self, lr: f32) {
        self.learning_rate = lr;
    }

    fn state_dict(&self) -> HashMap<String, f32> {
        let mut state = HashMap::new();
        state.insert("learning_rate".to_string(), self.learning_rate);
        state.insert("epsilon".to_string(), self.epsilon);
        state.insert("weight_decay".to_string(), self.weight_decay);
        state.insert(
            "initial_accumulator_value".to_string(),
            self.initial_accumulator_value,
        );
        state.insert("step_count".to_string(), self.step_count as f32);
        state
    }

    fn load_state_dict(&mut self, state: HashMap<String, f32>) {
        if let Some(&lr) = state.get("learning_rate") {
            self.learning_rate = lr;
        }
        if let Some(&epsilon) = state.get("epsilon") {
            self.epsilon = epsilon;
        }
        if let Some(&weight_decay) = state.get("weight_decay") {
            self.weight_decay = weight_decay;
        }
        if let Some(&initial_accumulator_value) = state.get("initial_accumulator_value") {
            self.initial_accumulator_value = initial_accumulator_value;
        }
        if let Some(&step_count) = state.get("step_count") {
            self.step_count = step_count as usize;
        }
    }
}