optirs-learned 0.3.1

OptiRS learned optimizers and meta-learning
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
use std::fmt::Debug;
// Gradient processing strategies for transformer optimization
//
// This module implements various gradient transformation and processing strategies
// used by the transformer optimizer to improve optimization performance.

#[allow(dead_code)]
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use std::collections::{HashMap, VecDeque};

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

/// Gradient processing strategies
#[derive(Debug, Clone, Copy)]
pub enum GradientProcessingStrategy {
    /// Raw gradients without processing
    Raw,
    /// Gradient clipping
    Clipping,
    /// Gradient normalization
    Normalization,
    /// Adaptive gradient scaling
    AdaptiveScaling,
    /// Adaptive processing (general)
    Adaptive,
    /// Gradient smoothing
    Smoothing,
    /// Gradient accumulation
    Accumulation,
    /// Gradient dropout
    Dropout,
    /// Gradient compression
    Compression,
}

/// Gradient processor for transformer optimizer
#[derive(Debug, Clone)]
pub struct GradientProcessor<
    T: Float
        + Debug
        + Default
        + Clone
        + std::iter::Sum
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync
        + 'static,
> {
    /// Processing strategy
    strategy: GradientProcessingStrategy,

    /// Gradient history for smoothing
    gradient_history: VecDeque<Array1<T>>,

    /// Accumulated gradients
    accumulated_gradients: Option<Array1<T>>,

    /// Gradient statistics
    gradient_stats: GradientStatistics<T>,

    /// Processing parameters
    processing_params: GradientProcessingParams<T>,
}

/// Gradient processing parameters
#[derive(Debug, Clone)]
pub struct GradientProcessingParams<T: Float + Debug + Send + Sync + 'static> {
    /// Clipping threshold
    clip_threshold: T,

    /// Smoothing factor
    smoothing_factor: T,

    /// Accumulation steps
    accumulation_steps: usize,

    /// Dropout probability
    dropout_prob: f64,

    /// Compression ratio
    compression_ratio: f64,

    /// Normalization epsilon
    norm_eps: T,
}

/// Gradient statistics tracking
#[derive(Debug, Clone)]
pub struct GradientStatistics<T: Float + Debug + Send + Sync + 'static> {
    /// Running mean of gradient magnitudes
    mean_magnitude: T,

    /// Running variance of gradient magnitudes
    var_magnitude: T,

    /// Maximum gradient magnitude seen
    max_magnitude: T,

    /// Minimum gradient magnitude seen
    min_magnitude: T,

    /// Update count
    update_count: usize,

    /// Gradient sparsity
    sparsity: T,
}

impl<
        T: Float
            + Debug
            + Default
            + Clone
            + std::iter::Sum
            + scirs2_core::ndarray::ScalarOperand
            + Send
            + Sync
            + 'static,
    > GradientProcessor<T>
{
    /// Create new gradient processor
    pub fn new(strategy: GradientProcessingStrategy) -> Self {
        Self {
            strategy,
            gradient_history: VecDeque::new(),
            accumulated_gradients: None,
            gradient_stats: GradientStatistics::new(),
            processing_params: GradientProcessingParams::default(),
        }
    }

    /// Create with custom parameters
    pub fn new_with_params(
        strategy: GradientProcessingStrategy,
        params: GradientProcessingParams<T>,
    ) -> Self {
        Self {
            strategy,
            gradient_history: VecDeque::new(),
            accumulated_gradients: None,
            gradient_stats: GradientStatistics::new(),
            processing_params: params,
        }
    }

    /// Process gradients according to the selected strategy
    pub fn process_gradients(&mut self, gradients: &Array1<T>) -> Result<Array1<T>> {
        // Update statistics first
        self.gradient_stats.update(gradients);

        match self.strategy {
            GradientProcessingStrategy::Raw => Ok(gradients.clone()),
            GradientProcessingStrategy::Clipping => self.clip_gradients(gradients),
            GradientProcessingStrategy::Normalization => self.normalize_gradients(gradients),
            GradientProcessingStrategy::AdaptiveScaling => self.adaptive_scale_gradients(gradients),
            GradientProcessingStrategy::Adaptive => self.adaptive_scale_gradients(gradients), // Use adaptive scaling as default
            GradientProcessingStrategy::Smoothing => self.smooth_gradients(gradients),
            GradientProcessingStrategy::Accumulation => self.accumulate_gradients(gradients),
            GradientProcessingStrategy::Dropout => self.dropout_gradients(gradients),
            GradientProcessingStrategy::Compression => self.compress_gradients(gradients),
        }
    }

    /// Clip gradients to prevent explosion
    fn clip_gradients(&self, gradients: &Array1<T>) -> Result<Array1<T>> {
        let grad_norm = self.compute_gradient_norm(gradients);

        if grad_norm > self.processing_params.clip_threshold {
            let scale = self.processing_params.clip_threshold / grad_norm;
            Ok(gradients * scale)
        } else {
            Ok(gradients.clone())
        }
    }

    /// Normalize gradients
    fn normalize_gradients(&self, gradients: &Array1<T>) -> Result<Array1<T>> {
        let grad_norm = self.compute_gradient_norm(gradients);

        if grad_norm > self.processing_params.norm_eps {
            Ok(gradients / grad_norm)
        } else {
            Ok(gradients.clone())
        }
    }

    /// Adaptively scale gradients based on statistics
    fn adaptive_scale_gradients(&self, gradients: &Array1<T>) -> Result<Array1<T>> {
        let current_norm = self.compute_gradient_norm(gradients);
        let mean_norm = self.gradient_stats.mean_magnitude;

        if mean_norm > T::zero() {
            let adaptive_scale =
                scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()) * mean_norm
                    / current_norm
                    + scirs2_core::numeric::NumCast::from(0.1).unwrap_or_else(|| T::zero());
            Ok(gradients * adaptive_scale)
        } else {
            Ok(gradients.clone())
        }
    }

    /// Smooth gradients using exponential moving average
    fn smooth_gradients(&mut self, gradients: &Array1<T>) -> Result<Array1<T>> {
        let alpha = self.processing_params.smoothing_factor;

        if let Some(prev_grad) = self.gradient_history.back() {
            let smoothed = gradients * alpha + prev_grad * (T::one() - alpha);
            self.gradient_history.push_back(smoothed.clone());

            // Keep only recent history
            if self.gradient_history.len() > 10 {
                self.gradient_history.pop_front();
            }

            Ok(smoothed)
        } else {
            self.gradient_history.push_back(gradients.clone());
            Ok(gradients.clone())
        }
    }

    /// Accumulate gradients over multiple steps
    fn accumulate_gradients(&mut self, gradients: &Array1<T>) -> Result<Array1<T>> {
        if let Some(ref mut accumulated) = self.accumulated_gradients {
            *accumulated = accumulated.clone() + gradients;
        } else {
            self.accumulated_gradients = Some(gradients.clone());
        }

        // Return accumulated gradients if we've reached the target steps
        if self
            .gradient_stats
            .update_count
            .is_multiple_of(self.processing_params.accumulation_steps)
        {
            if let Some(accumulated) = self.accumulated_gradients.take() {
                let scale = scirs2_core::numeric::NumCast::from(
                    1.0 / self.processing_params.accumulation_steps as f64,
                )
                .unwrap_or_else(|| T::zero());
                Ok(accumulated * scale)
            } else {
                Ok(gradients.clone())
            }
        } else {
            // Return zero gradients for intermediate steps
            Ok(Array1::zeros(gradients.len()))
        }
    }

    /// Apply dropout to gradients
    fn dropout_gradients(&self, gradients: &Array1<T>) -> Result<Array1<T>> {
        // Simplified dropout - in practice would use proper random sampling
        let mut result = gradients.clone();

        // Apply deterministic "dropout" pattern for reproducibility
        for (i, elem) in result.iter_mut().enumerate() {
            if (i % 10) < (self.processing_params.dropout_prob * 10.0) as usize {
                *elem = T::zero();
            }
        }

        Ok(result)
    }

    /// Compress gradients (simplified sparsification)
    fn compress_gradients(&self, gradients: &Array1<T>) -> Result<Array1<T>> {
        let mut result = gradients.clone();
        let threshold = self.compute_gradient_norm(gradients)
            * scirs2_core::numeric::NumCast::from(self.processing_params.compression_ratio)
                .unwrap_or_else(|| T::zero());

        // Zero out small gradients
        for elem in result.iter_mut() {
            if elem.abs() < threshold {
                *elem = T::zero();
            }
        }

        Ok(result)
    }

    /// Compute L2 norm of gradients
    fn compute_gradient_norm(&self, gradients: &Array1<T>) -> T {
        let sum_squares = gradients
            .iter()
            .map(|&x| x * x)
            .fold(T::zero(), |a, b| a + b);
        sum_squares.sqrt()
    }

    /// Get gradient statistics
    pub fn statistics(&self) -> &GradientStatistics<T> {
        &self.gradient_stats
    }

    /// Update processing strategy
    pub fn set_strategy(&mut self, strategy: GradientProcessingStrategy) {
        self.strategy = strategy;
    }

    /// Update processing parameters
    pub fn set_parameters(&mut self, params: GradientProcessingParams<T>) {
        self.processing_params = params;
    }

    /// Reset processor state
    pub fn reset(&mut self) {
        self.gradient_history.clear();
        self.accumulated_gradients = None;
        self.gradient_stats = GradientStatistics::new();
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> Default for GradientStatistics<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> GradientStatistics<T> {
    /// Create new gradient statistics
    pub fn new() -> Self {
        Self {
            mean_magnitude: T::zero(),
            var_magnitude: T::zero(),
            max_magnitude: T::zero(),
            min_magnitude: scirs2_core::numeric::NumCast::from(f64::INFINITY)
                .unwrap_or_else(|| T::zero()),
            update_count: 0,
            sparsity: T::zero(),
        }
    }

    /// Update statistics with new gradients
    pub fn update(&mut self, gradients: &Array1<T>) {
        let magnitude = gradients
            .iter()
            .map(|&x| x * x)
            .fold(T::zero(), |a, b| a + b)
            .sqrt();

        self.update_count += 1;
        let count = scirs2_core::numeric::NumCast::from(self.update_count as f64)
            .unwrap_or_else(|| T::zero());

        // Update running mean
        let delta = magnitude - self.mean_magnitude;
        self.mean_magnitude = self.mean_magnitude + delta / count;

        // Update running variance
        let delta2 = magnitude - self.mean_magnitude;
        self.var_magnitude = self.var_magnitude + delta * delta2;

        // Update min/max
        if magnitude > self.max_magnitude {
            self.max_magnitude = magnitude;
        }
        if magnitude < self.min_magnitude {
            self.min_magnitude = magnitude;
        }

        // Update sparsity (fraction of near-zero elements)
        let zero_count = gradients
            .iter()
            .filter(|&&x| {
                x.abs() < scirs2_core::numeric::NumCast::from(1e-8).unwrap_or_else(|| T::zero())
            })
            .count();
        let current_sparsity =
            T::from(zero_count as f64 / gradients.len() as f64).expect("unwrap failed");
        let alpha = scirs2_core::numeric::NumCast::from(0.1).unwrap_or_else(|| T::zero());
        self.sparsity = self.sparsity * (T::one() - alpha) + current_sparsity * alpha;
    }

    /// Get mean magnitude
    pub fn mean_magnitude(&self) -> T {
        self.mean_magnitude
    }

    /// Get variance of magnitude
    pub fn variance_magnitude(&self) -> T {
        if self.update_count > 1 {
            self.var_magnitude / T::from((self.update_count - 1) as f64).expect("unwrap failed")
        } else {
            T::zero()
        }
    }

    /// Get standard deviation of magnitude
    pub fn std_magnitude(&self) -> T {
        self.variance_magnitude().sqrt()
    }

    /// Get gradient sparsity
    pub fn sparsity(&self) -> T {
        self.sparsity
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> Default
    for GradientProcessingParams<T>
{
    fn default() -> Self {
        Self {
            clip_threshold: scirs2_core::numeric::NumCast::from(1.0).unwrap_or_else(|| T::zero()),
            smoothing_factor: scirs2_core::numeric::NumCast::from(0.9).unwrap_or_else(|| T::zero()),
            accumulation_steps: 4,
            dropout_prob: 0.1,
            compression_ratio: 0.1,
            norm_eps: scirs2_core::numeric::NumCast::from(1e-8).unwrap_or_else(|| T::zero()),
        }
    }
}