torsh-optim 0.1.3

Optimization algorithms for ToRSh with PyTorch-compatible API
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
//! Memory-efficient optimizer implementations
//!
//! This module provides optimizers designed to minimize memory usage during training,
//! particularly useful for large models or resource-constrained environments.

use crate::{
    Optimizer, OptimizerError, OptimizerResult, OptimizerState, ParamGroup, ParamGroupState,
};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::ops::Add;
use std::sync::Arc;
use torsh_core::error::{Result, TorshError};
use torsh_tensor::Tensor;

/// Memory pool for reusing tensor allocations
pub struct MemoryPool {
    tensors: Vec<Tensor>,
    shapes_cache: HashMap<Vec<usize>, Vec<usize>>, // shape -> indices in tensors vec
}

impl MemoryPool {
    pub fn new() -> Self {
        Self {
            tensors: Vec::new(),
            shapes_cache: HashMap::new(),
        }
    }

    /// Get a tensor from the pool or allocate a new one
    pub fn get_tensor(
        &mut self,
        shape: &[usize],
        device: torsh_core::device::DeviceType,
    ) -> torsh_core::error::Result<Tensor> {
        let shape_vec = shape.to_vec();

        if let Some(indices) = self.shapes_cache.get_mut(&shape_vec) {
            if let Some(idx) = indices.pop() {
                // Reuse existing tensor
                let mut tensor = self.tensors.swap_remove(idx);
                let _ = tensor.zero_();
                return Ok(tensor);
            }
        }

        // Allocate new tensor
        Ok(Tensor::zeros(shape, device)?)
    }

    /// Return a tensor to the pool
    pub fn return_tensor(&mut self, tensor: Tensor) {
        let shape = tensor.shape().dims().to_vec();
        let idx = self.tensors.len();
        self.tensors.push(tensor);

        self.shapes_cache.entry(shape).or_default().push(idx);
    }

    /// Clear the pool to free memory
    pub fn clear(&mut self) {
        self.tensors.clear();
        self.shapes_cache.clear();
    }
}

/// Configuration for memory-efficient optimizers
#[derive(Clone)]
pub struct MemoryConfig {
    /// Maximum memory usage in bytes (0 = unlimited)
    pub max_memory_bytes: usize,
    /// Use memory pooling
    pub use_memory_pool: bool,
    /// Enable state compression
    pub compress_state: bool,
    /// Use lazy gradient accumulation
    pub lazy_gradients: bool,
    /// Gradient checkpointing interval
    pub checkpoint_interval: usize,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            max_memory_bytes: 0, // Unlimited
            use_memory_pool: true,
            compress_state: false,
            lazy_gradients: true,
            checkpoint_interval: 100,
        }
    }
}

/// Memory-efficient Adam optimizer with reduced memory footprint
pub struct MemoryEfficientAdam {
    param_groups: Vec<ParamGroup>,
    state: HashMap<String, HashMap<String, Tensor>>,
    step_count: usize,

    // Adam parameters
    beta1: f32,
    beta2: f32,
    eps: f32,
    weight_decay: f32,
    amsgrad: bool,

    // Memory optimization
    memory_pool: MemoryPool,
    config: MemoryConfig,
    memory_usage: usize,
}

impl MemoryEfficientAdam {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        params: Vec<Arc<RwLock<Tensor>>>,
        lr: f32,
        beta1: Option<f32>,
        beta2: Option<f32>,
        eps: Option<f32>,
        weight_decay: Option<f32>,
        amsgrad: Option<bool>,
        memory_config: Option<MemoryConfig>,
    ) -> Self {
        let param_group = ParamGroup::new(params, lr);

        Self {
            param_groups: vec![param_group],
            state: HashMap::new(),
            step_count: 0,
            beta1: beta1.unwrap_or(0.9),
            beta2: beta2.unwrap_or(0.999),
            eps: eps.unwrap_or(1e-8),
            weight_decay: weight_decay.unwrap_or(0.0),
            amsgrad: amsgrad.unwrap_or(false),
            memory_pool: MemoryPool::new(),
            config: memory_config.unwrap_or_default(),
            memory_usage: 0,
        }
    }

    fn get_param_id(param: &Arc<RwLock<Tensor>>) -> String {
        format!("{:p}", Arc::as_ptr(param))
    }

    /// Estimate memory usage for a tensor
    fn estimate_tensor_memory(tensor: &Tensor) -> usize {
        tensor.shape().numel() * std::mem::size_of::<f32>()
    }

    /// Check if we can allocate more memory
    fn can_allocate(&self, size: usize) -> bool {
        if self.config.max_memory_bytes == 0 {
            return true; // Unlimited
        }
        self.memory_usage + size <= self.config.max_memory_bytes
    }

    /// Update memory usage tracking
    fn update_memory_usage(&mut self, delta: isize) {
        if delta < 0 {
            self.memory_usage = self.memory_usage.saturating_sub((-delta) as usize);
        } else {
            self.memory_usage += delta as usize;
        }
    }

    /// Compress state if needed
    fn maybe_compress_state(&mut self, param_id: &str) -> Result<()> {
        if !self.config.compress_state {
            return Ok(());
        }

        // Get the state for this parameter
        if let Some(param_state) = self.state.get_mut(param_id) {
            // Compress momentum and squared gradient states using quantization
            let state_keys: Vec<String> = param_state.keys().cloned().collect();
            for state_name in state_keys {
                if state_name == "exp_avg"
                    || state_name == "exp_avg_sq"
                    || state_name == "max_exp_avg_sq"
                {
                    if let Some(state_tensor) = param_state.get(&state_name).cloned() {
                        // Apply quantization to reduce memory usage
                        // Convert to lower precision and back to maintain approximate values
                        let quantized = Self::quantize_tensor(&state_tensor)?;
                        param_state.insert(state_name, quantized);
                    }
                }
            }
        }

        Ok(())
    }

    /// Quantize tensor to reduce memory usage
    fn quantize_tensor(tensor: &Tensor) -> Result<Tensor> {
        // Simple quantization scheme: convert to f16 precision and back to f32
        // This reduces memory usage by approximately 50% for state tensors

        // Get tensor data as f32 values
        let data = tensor.to_vec()?;

        // Find min and max values for quantization scaling
        let min_val = data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
        let max_val = data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));

        // Avoid division by zero
        let range = (max_val - min_val).max(1e-8);

        // Quantize to 8-bit integers and dequantize back to f32
        // This provides significant compression with acceptable precision loss
        let quantized_data: Vec<f32> = data
            .iter()
            .map(|&val| {
                // Normalize to [0, 1]
                let normalized = (val - min_val) / range;
                // Quantize to 8-bit (0-255)
                let quantized = (normalized * 255.0).round().clamp(0.0, 255.0) as u8;
                // Dequantize back to f32
                min_val + (quantized as f32 / 255.0) * range
            })
            .collect();

        // Create new tensor with quantized data
        let quantized_tensor = Tensor::from_data(
            quantized_data,
            tensor.shape().dims().to_vec(),
            tensor.device(),
        )?;

        Ok(quantized_tensor)
    }

    /// Perform memory-efficient Adam step for a single parameter
    fn adam_step_memory_efficient(
        &mut self,
        param: &Arc<RwLock<Tensor>>,
        group_lr: f32,
    ) -> Result<()> {
        let param_id = Self::get_param_id(param);
        let mut param_write = param.write();
        let grad = param_write.grad().ok_or_else(|| {
            TorshError::invalid_argument_with_context(
                "Parameter has no gradient",
                "memory_efficient_adam_step",
            )
        })?;

        // Apply weight decay if specified
        let effective_grad = if self.weight_decay > 0.0 {
            grad.add(&param_write.mul_scalar(self.weight_decay)?)?
        } else {
            grad.clone()
        };

        // Get or initialize momentum buffers
        let momentum_key = "momentum".to_string();
        let velocity_key = "velocity".to_string();
        let max_exp_avg_sq_key = "max_exp_avg_sq".to_string();

        // Check memory constraints before allocation
        let param_memory = Self::estimate_tensor_memory(&param_write);

        // Check if we need to allocate memory first
        let needs_momentum = !self.state.contains_key(&param_id)
            || !self
                .state
                .get(&param_id)
                .expect("state should exist after contains_key check")
                .contains_key(&momentum_key);
        let needs_velocity = !self.state.contains_key(&param_id)
            || !self
                .state
                .get(&param_id)
                .expect("state should exist after contains_key check")
                .contains_key(&velocity_key);

        if needs_momentum && !self.can_allocate(param_memory) {
            return Err(TorshError::invalid_argument_with_context(
                "Insufficient memory for momentum buffer",
                "memory_efficient_adam_step",
            ));
        }

        if needs_velocity && !self.can_allocate(param_memory) {
            return Err(TorshError::invalid_argument_with_context(
                "Insufficient memory for velocity buffer",
                "memory_efficient_adam_step",
            ));
        }

        // Update memory usage for new allocations
        let memory_to_add = (if needs_momentum { 1 } else { 0 }
            + if needs_velocity { 1 } else { 0 })
            * param_memory;
        if memory_to_add > 0 {
            self.update_memory_usage(memory_to_add as isize);
        }

        // Now safely get the parameter state
        let param_state = self.state.entry(param_id.clone()).or_default();

        let momentum = if let Some(m) = param_state.get(&momentum_key) {
            m.clone()
        } else {
            let m = if self.config.use_memory_pool {
                self.memory_pool
                    .get_tensor(param_write.shape().dims(), param_write.device())?
            } else {
                Tensor::zeros(param_write.shape().dims(), param_write.device())?
            };
            param_state.insert(momentum_key.clone(), m.clone());
            m
        };

        let velocity = if let Some(v) = param_state.get(&velocity_key) {
            v.clone()
        } else {
            let v = if self.config.use_memory_pool {
                self.memory_pool
                    .get_tensor(param_write.shape().dims(), param_write.device())?
            } else {
                Tensor::zeros(param_write.shape().dims(), param_write.device())?
            };
            param_state.insert(velocity_key.clone(), v.clone());
            v
        };

        // Update biased first moment estimate
        let new_momentum = momentum
            .mul_scalar(self.beta1)?
            .add(&effective_grad.mul_scalar(1.0 - self.beta1)?)?;

        // Update biased second raw moment estimate
        let grad_squared = effective_grad.mul_op(&effective_grad)?;
        let new_velocity = velocity
            .mul_scalar(self.beta2)?
            .add(&grad_squared.mul_scalar(1.0 - self.beta2)?)?;

        // 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_momentum = new_momentum.div_scalar(bias_correction1)?;
        let corrected_velocity = new_velocity.div_scalar(bias_correction2)?;

        // Handle AMSGrad variant - check memory first before borrowing param_state again
        let needs_max_velocity_check =
            self.amsgrad && !param_state.contains_key(&max_exp_avg_sq_key);

        // Drop param_state temporarily to check memory if needed
        if needs_max_velocity_check {
            let _ = param_state;

            if !self.can_allocate(param_memory) {
                return Err(TorshError::invalid_argument_with_context(
                    "Insufficient memory for max velocity buffer",
                    "memory_efficient_adam_step",
                ));
            }

            self.update_memory_usage(param_memory as isize);
        }

        // Re-acquire param_state for the rest of the function
        let param_state = self.state.entry(param_id.clone()).or_default();

        let exp_avg_sq_hat = if self.amsgrad {
            let max_exp_avg_sq = if let Some(max_v) = param_state.get(&max_exp_avg_sq_key) {
                max_v.clone()
            } else {
                let max_v = if self.config.use_memory_pool {
                    self.memory_pool
                        .get_tensor(param_write.shape().dims(), param_write.device())?
                } else {
                    Tensor::zeros(param_write.shape().dims(), param_write.device())?
                };
                param_state.insert(max_exp_avg_sq_key.clone(), max_v.clone());
                max_v
            };

            let new_max_exp_avg_sq = max_exp_avg_sq.maximum(&corrected_velocity)?;
            param_state.insert(max_exp_avg_sq_key, new_max_exp_avg_sq.clone());
            new_max_exp_avg_sq
        } else {
            corrected_velocity.clone()
        };

        // Compute update
        let denominator = exp_avg_sq_hat.sqrt()?.add_scalar(self.eps)?;
        let update = corrected_momentum
            .div(&denominator)?
            .mul_scalar(-group_lr)?;

        // Update parameters
        *param_write = param_write.add(&update)?;

        // Update state
        param_state.insert(momentum_key, new_momentum);
        param_state.insert(velocity_key, new_velocity);

        // Compress state periodically
        if self.step_count % self.config.checkpoint_interval == 0 {
            self.maybe_compress_state(&param_id)?;
        }

        Ok(())
    }
}

impl Optimizer for MemoryEfficientAdam {
    fn step(&mut self) -> OptimizerResult<()> {
        self.step_count += 1;

        // Collect parameters and learning rates first to avoid borrowing issues
        let param_data: Vec<(Arc<RwLock<Tensor>>, f32)> = self
            .param_groups
            .iter()
            .flat_map(|group| {
                let group_lr = group.lr;
                group
                    .params
                    .iter()
                    .map(move |param| (param.clone(), group_lr))
            })
            .collect();

        for (param, group_lr) in param_data {
            self.adam_step_memory_efficient(&param, group_lr)?;
        }

        Ok(())
    }

    fn zero_grad(&mut self) {
        for group in &self.param_groups {
            for param in &group.params {
                param.write().zero_grad();
            }
        }
    }

    fn get_lr(&self) -> Vec<f32> {
        self.param_groups.iter().map(|g| g.lr).collect()
    }

    fn set_lr(&mut self, lr: f32) {
        for group in &mut self.param_groups {
            group.lr = lr;
        }
    }

    fn add_param_group(&mut self, params: Vec<Arc<RwLock<Tensor>>>, options: HashMap<String, f32>) {
        let lr = options.get("lr").copied().unwrap_or(1e-3);
        let group = ParamGroup::new(params, lr).with_options(options);
        self.param_groups.push(group);
    }

    fn parameters(&self) -> Vec<Arc<RwLock<Tensor>>> {
        crate::optimizer::collect_parameters(&self.param_groups)
    }

    fn state_dict(&self) -> OptimizerResult<OptimizerState> {
        let param_groups = self
            .param_groups
            .iter()
            .map(|g| ParamGroupState {
                lr: g.lr,
                options: g.options.clone(),
                param_count: g.params.len(),
            })
            .collect();

        Ok(OptimizerState {
            optimizer_type: "MemoryEfficientAdam".to_string(),
            version: "0.1.0".to_string(),
            param_groups,
            state: self.state.clone(),
            global_state: HashMap::new(),
        })
    }

    fn load_state_dict(&mut self, state: OptimizerState) -> OptimizerResult<()> {
        if state.param_groups.len() != self.param_groups.len() {
            return Err(OptimizerError::InvalidParameter(
                "Parameter group count mismatch".to_string(),
            ));
        }

        for (i, group_state) in state.param_groups.iter().enumerate() {
            self.param_groups[i].lr = group_state.lr;
            self.param_groups[i].options = group_state.options.clone();
        }

        self.state = state.state;
        Ok(())
    }
}

/// Memory-efficient L-BFGS with improved history management
pub struct MemoryEfficientLBFGS {
    param_groups: Vec<ParamGroup>,
    state: HashMap<String, HashMap<String, Tensor>>,
    step_count: usize,

    // L-BFGS parameters
    max_iter: usize,
    tolerance_grad: f32,
    tolerance_change: f32,
    history_size: usize,

    // Memory optimization
    memory_pool: MemoryPool,
    config: MemoryConfig,
    history_buffer: CircularBuffer<(Tensor, Tensor, f32)>, // (s, y, rho)
}

/// Circular buffer for L-BFGS history with memory management
pub struct CircularBuffer<T> {
    data: Vec<T>,
    capacity: usize,
    start: usize,
    len: usize,
}

impl<T> CircularBuffer<T> {
    pub fn new(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            capacity,
            start: 0,
            len: 0,
        }
    }

    pub fn push(&mut self, item: T) {
        if self.len < self.capacity {
            self.data.push(item);
            self.len += 1;
        } else {
            let index = (self.start + self.len) % self.capacity;
            self.data[index] = item;
            self.start = (self.start + 1) % self.capacity;
        }
    }

    pub fn get(&self, index: usize) -> Option<&T> {
        if index < self.len {
            let actual_index = (self.start + index) % self.capacity;
            self.data.get(actual_index)
        } else {
            None
        }
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn clear(&mut self) {
        self.data.clear();
        self.start = 0;
        self.len = 0;
    }
}

impl MemoryEfficientLBFGS {
    pub fn new(
        params: Vec<Arc<RwLock<Tensor>>>,
        lr: Option<f32>,
        max_iter: Option<usize>,
        tolerance_grad: Option<f32>,
        tolerance_change: Option<f32>,
        history_size: Option<usize>,
        memory_config: Option<MemoryConfig>,
    ) -> Self {
        let lr = lr.unwrap_or(1.0);
        let history_size = history_size.unwrap_or(10); // Reduced default for memory efficiency

        let param_group = ParamGroup::new(params, lr);

        Self {
            param_groups: vec![param_group],
            state: HashMap::new(),
            step_count: 0,
            max_iter: max_iter.unwrap_or(20),
            tolerance_grad: tolerance_grad.unwrap_or(1e-7),
            tolerance_change: tolerance_change.unwrap_or(1e-9),
            history_size,
            memory_pool: MemoryPool::new(),
            config: memory_config.unwrap_or_default(),
            history_buffer: CircularBuffer::new(history_size),
        }
    }

    /// Get memory usage statistics
    pub fn memory_stats(&self) -> HashMap<String, usize> {
        let mut stats = HashMap::new();
        stats.insert(
            "total_usage".to_string(),
            self.memory_pool.tensors.len() * std::mem::size_of::<Tensor>(),
        );
        stats.insert("history_size".to_string(), self.history_buffer.len());
        stats.insert("pooled_tensors".to_string(), self.memory_pool.tensors.len());
        stats
    }

    /// Clear memory pools and history to free up memory
    pub fn clear_memory(&mut self) {
        self.memory_pool.clear();
        self.history_buffer.clear();
        self.state.clear();
    }
}

impl Optimizer for MemoryEfficientLBFGS {
    fn step(&mut self) -> OptimizerResult<()> {
        // Simplified memory-efficient L-BFGS implementation
        // This is a placeholder that would need full implementation
        self.step_count += 1;

        // For now, just implement a simple gradient descent step
        for group in &self.param_groups {
            for param in &group.params {
                let mut param_write = param.write();
                if let Some(grad) = param_write.grad() {
                    let update = grad.mul_scalar(-group.lr)?;
                    *param_write = param_write.add(&update)?;
                }
            }
        }

        Ok(())
    }

    fn zero_grad(&mut self) {
        for group in &self.param_groups {
            for param in &group.params {
                param.write().zero_grad();
            }
        }
    }

    fn get_lr(&self) -> Vec<f32> {
        self.param_groups.iter().map(|g| g.lr).collect()
    }

    fn set_lr(&mut self, lr: f32) {
        for group in &mut self.param_groups {
            group.lr = lr;
        }
    }

    fn add_param_group(&mut self, params: Vec<Arc<RwLock<Tensor>>>, options: HashMap<String, f32>) {
        let lr = options.get("lr").copied().unwrap_or(1.0);
        let group = ParamGroup::new(params, lr).with_options(options);
        self.param_groups.push(group);
    }

    fn parameters(&self) -> Vec<Arc<RwLock<Tensor>>> {
        crate::optimizer::collect_parameters(&self.param_groups)
    }

    fn state_dict(&self) -> OptimizerResult<OptimizerState> {
        let param_groups = self
            .param_groups
            .iter()
            .map(|g| ParamGroupState {
                lr: g.lr,
                options: g.options.clone(),
                param_count: g.params.len(),
            })
            .collect();

        Ok(OptimizerState {
            optimizer_type: "MemoryEfficientAdam".to_string(),
            version: "0.1.0".to_string(),
            param_groups,
            state: self.state.clone(),
            global_state: HashMap::new(),
        })
    }

    fn load_state_dict(&mut self, state: OptimizerState) -> OptimizerResult<()> {
        if state.param_groups.len() != self.param_groups.len() {
            return Err(OptimizerError::InvalidParameter(
                "Parameter group count mismatch".to_string(),
            ));
        }

        for (i, group_state) in state.param_groups.iter().enumerate() {
            self.param_groups[i].lr = group_state.lr;
            self.param_groups[i].options = group_state.options.clone();
        }

        self.state = state.state;
        Ok(())
    }
}

/// Builder for memory-efficient optimizers
pub struct MemoryEfficientOptimizerBuilder {
    memory_config: MemoryConfig,
}

impl MemoryEfficientOptimizerBuilder {
    pub fn new() -> Self {
        Self {
            memory_config: MemoryConfig::default(),
        }
    }

    pub fn max_memory_gb(mut self, gb: f32) -> Self {
        self.memory_config.max_memory_bytes = (gb * 1_000_000_000.0) as usize;
        self
    }

    pub fn use_memory_pool(mut self, use_pool: bool) -> Self {
        self.memory_config.use_memory_pool = use_pool;
        self
    }

    pub fn compress_state(mut self, compress: bool) -> Self {
        self.memory_config.compress_state = compress;
        self
    }

    pub fn lazy_gradients(mut self, lazy: bool) -> Self {
        self.memory_config.lazy_gradients = lazy;
        self
    }

    pub fn checkpoint_interval(mut self, interval: usize) -> Self {
        self.memory_config.checkpoint_interval = interval;
        self
    }

    pub fn build_adam(self, params: Vec<Arc<RwLock<Tensor>>>, lr: f32) -> MemoryEfficientAdam {
        MemoryEfficientAdam::new(
            params,
            lr,
            None,
            None,
            None,
            None,
            None,
            Some(self.memory_config),
        )
    }

    pub fn build_lbfgs(self, params: Vec<Arc<RwLock<Tensor>>>, lr: f32) -> MemoryEfficientLBFGS {
        MemoryEfficientLBFGS::new(
            params,
            Some(lr),
            None,
            None,
            None,
            None,
            Some(self.memory_config),
        )
    }
}

impl Default for MemoryEfficientOptimizerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_memory_pool() -> OptimizerResult<()> {
        let mut pool = MemoryPool::new();
        let tensor = pool.get_tensor(&[2, 2], torsh_core::device::DeviceType::Cpu)?;
        assert_eq!(tensor.shape().dims(), &[2, 2]);

        pool.return_tensor(tensor);
        let reused = pool.get_tensor(&[2, 2], torsh_core::device::DeviceType::Cpu)?;
        assert_eq!(reused.shape().dims(), &[2, 2]);
        Ok(())
    }

    #[test]
    fn test_circular_buffer() {
        let mut buffer = CircularBuffer::new(3);
        buffer.push(1);
        buffer.push(2);
        buffer.push(3);
        buffer.push(4); // Should overwrite 1

        assert_eq!(buffer.len(), 3);
        assert_eq!(buffer.get(0), Some(&2));
        assert_eq!(buffer.get(1), Some(&3));
        assert_eq!(buffer.get(2), Some(&4));
    }

    #[test]
    fn test_memory_efficient_adam_creation() -> OptimizerResult<()> {
        let params = vec![Arc::new(RwLock::new(randn::<f32>(&[2, 2])?))];
        let optimizer = MemoryEfficientAdam::new(params, 0.001, None, None, None, None, None, None);
        assert_eq!(optimizer.get_lr()[0], 0.001);
        Ok(())
    }

    #[test]
    fn test_memory_efficient_lbfgs_creation() -> OptimizerResult<()> {
        let params = vec![Arc::new(RwLock::new(randn::<f32>(&[2, 2])?))];
        let optimizer =
            MemoryEfficientLBFGS::new(params, Some(0.1), None, None, None, Some(5), None);
        assert_eq!(optimizer.get_lr()[0], 0.1);
        assert_eq!(optimizer.history_size, 5);
        Ok(())
    }

    #[test]
    fn test_builder_pattern() -> OptimizerResult<()> {
        let params = vec![Arc::new(RwLock::new(randn::<f32>(&[2, 2])?))];
        let optimizer = MemoryEfficientOptimizerBuilder::new()
            .max_memory_gb(1.0)
            .use_memory_pool(true)
            .compress_state(true)
            .build_adam(params, 0.001);

        assert_eq!(optimizer.get_lr()[0], 0.001);
        assert_eq!(optimizer.config.max_memory_bytes, 1_000_000_000);
        assert!(optimizer.config.use_memory_pool);
        assert!(optimizer.config.compress_state);
        Ok(())
    }
}