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
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
use std::fmt::Debug;
// Transformer encoder layers and components
//
// This module implements the encoder components of the transformer optimizer,
// including the transformer layer, feed-forward network, and layer normalization.

#[allow(dead_code)]
use scirs2_core::ndarray::{s, Array1, Array2};
use scirs2_core::numeric::Float;
use scirs2_core::random::{CoreRandom as Random, Rng as SCRRng};

use super::super::TransformerOptimizerConfig;
use super::attention::MultiHeadAttention;
use crate::error::{OptimError, Result};

/// Activation functions for feed-forward networks
#[derive(Debug, Clone, Copy)]
pub enum ActivationFunction {
    /// ReLU activation
    ReLU,
    /// GELU activation  
    GELU,
    /// Swish/SiLU activation
    Swish,
    /// GLU (Gated Linear Unit)
    GLU,
    /// GeGLU (GELU variant of GLU)
    GeGLU,
}

/// Single transformer encoder layer
#[derive(Debug, Clone)]
pub struct TransformerLayer<T: Float + Debug + Send + Sync + 'static> {
    /// Multi-head self-attention
    self_attention: MultiHeadAttention<T>,

    /// Cross-attention (for multi-task learning)
    cross_attention: Option<MultiHeadAttention<T>>,

    /// Feed-forward network
    feed_forward: FeedForwardNetwork<T>,

    /// Layer normalization layers
    ln1: LayerNorm<T>,
    ln2: LayerNorm<T>,
    ln3: Option<LayerNorm<T>>, // For cross-attention

    /// Dropout layers
    dropout1: DropoutLayer,
    dropout2: DropoutLayer,
    dropout3: Option<DropoutLayer>,

    /// Use pre-layer normalization
    pre_layer_norm: bool,
}

/// Feed-forward network
#[derive(Debug, Clone)]
pub struct FeedForwardNetwork<T: Float + Debug + Send + Sync + 'static> {
    /// First linear layer weights
    linear1: Array2<T>,

    /// First linear layer bias
    bias1: Array1<T>,

    /// Second linear layer weights
    linear2: Array2<T>,

    /// Second linear layer bias
    bias2: Array1<T>,

    /// Activation function
    activation: ActivationFunction,

    /// Dropout layer
    dropout: DropoutLayer,
}

/// Layer normalization
#[derive(Debug, Clone)]
pub struct LayerNorm<T: Float + Debug + Send + Sync + 'static> {
    /// Scale parameters (gamma)
    gamma: Array1<T>,

    /// Shift parameters (beta)
    beta: Array1<T>,

    /// Epsilon for numerical stability
    eps: T,

    /// Dimension
    dim: usize,
}

/// Dropout layer
#[derive(Debug, Clone)]
pub struct DropoutLayer {
    /// Dropout probability
    prob: f64,

    /// Training mode
    training: bool,
}

impl<T: Float + Debug + Default + Clone + std::iter::Sum + Send + Sync> TransformerLayer<T> {
    pub fn new(config: &TransformerOptimizerConfig, _rng: &mut Random) -> Result<Self> {
        let self_attention = MultiHeadAttention::new(config)?;
        let cross_attention = if config.cross_attention {
            Some(MultiHeadAttention::new(config)?)
        } else {
            None
        };

        let feed_forward = FeedForwardNetwork::new(config)?;

        let ln1 = LayerNorm::new(config.modeldim);
        let ln2 = LayerNorm::new(config.modeldim);
        let ln3 = if config.cross_attention {
            Some(LayerNorm::new(config.modeldim))
        } else {
            None
        };

        let dropout1 = DropoutLayer::new(config.attention_dropout);
        let dropout2 = DropoutLayer::new(config.ff_dropout);
        let dropout3 = if config.cross_attention {
            Some(DropoutLayer::new(config.attention_dropout))
        } else {
            None
        };

        Ok(Self {
            self_attention,
            cross_attention,
            feed_forward,
            ln1,
            ln2,
            ln3,
            dropout1,
            dropout2,
            dropout3,
            pre_layer_norm: config.pre_layer_norm,
        })
    }

    pub fn forward(&mut self, input: &Array2<T>) -> Result<Array2<T>> {
        let mut x = input.clone();

        // Self-attention with residual connection
        let residual = x.clone();
        if self.pre_layer_norm {
            x = self.ln1.forward(&x)?;
        }

        x = self.self_attention.forward(&x, &x, &x)?;
        x = self.dropout1.forward(&x)?;
        x = x + &residual;

        if !self.pre_layer_norm {
            x = self.ln1.forward(&x)?;
        }

        // Cross-attention (if enabled)
        if let Some(ref mut cross_attn) = self.cross_attention {
            let residual = x.clone();
            if self.pre_layer_norm {
                if let Some(ref ln3) = self.ln3 {
                    x = ln3.forward(&x)?;
                }
            }

            // For now, use same input as key/value for cross-attention
            x = cross_attn.forward(&x, &x, &x)?;
            if let Some(ref dropout3) = self.dropout3 {
                x = dropout3.forward(&x)?;
            }
            x = x + &residual;

            if !self.pre_layer_norm {
                if let Some(ref ln3) = self.ln3 {
                    x = ln3.forward(&x)?;
                }
            }
        }

        // Feed-forward with residual connection
        let residual = x.clone();
        if self.pre_layer_norm {
            x = self.ln2.forward(&x)?;
        }

        x = self.feed_forward.forward(&x)?;
        x = self.dropout2.forward(&x)?;
        x = x + &residual;

        if !self.pre_layer_norm {
            x = self.ln2.forward(&x)?;
        }

        Ok(x)
    }

    /// Get attention patterns for analysis
    pub fn get_attention_patterns(&self) -> Option<&scirs2_core::ndarray::Array3<T>> {
        self.self_attention.get_attention_patterns()
    }
}

impl<T: Float + Debug + Default + Clone + Send + Sync + 'static> FeedForwardNetwork<T> {
    pub fn new(config: &TransformerOptimizerConfig) -> Result<Self> {
        let modeldim = config.modeldim;
        let ff_dim = config.ff_dim;
        let mut rng = scirs2_core::random::thread_rng();

        // Initialize weights with Xavier initialization
        let bound1 = (6.0 / (modeldim + ff_dim) as f64).sqrt();
        let bound2 = (6.0 / (ff_dim + modeldim) as f64).sqrt();

        let mut linear1 = Array2::zeros((modeldim, ff_dim));
        let mut linear2 = Array2::zeros((ff_dim, modeldim));

        for elem in linear1.iter_mut() {
            *elem = T::from((rng.random::<f64>() - 0.5) * 2.0 * bound1).expect("unwrap failed");
        }
        for elem in linear2.iter_mut() {
            *elem = T::from((rng.random::<f64>() - 0.5) * 2.0 * bound2).expect("unwrap failed");
        }

        let bias1 = Array1::zeros(ff_dim);
        let bias2 = Array1::zeros(modeldim);

        Ok(Self {
            linear1,
            bias1,
            linear2,
            bias2,
            activation: ActivationFunction::GELU,
            dropout: DropoutLayer::new(config.ff_dropout),
        })
    }

    pub fn forward(&self, input: &Array2<T>) -> Result<Array2<T>> {
        // First linear layer
        let x1 = self.linear_transform(input, &self.linear1, &self.bias1)?;

        // Activation
        let x2 = self.apply_activation(&x1)?;

        // Dropout
        let x3 = self.dropout.forward(&x2)?;

        // Second linear layer
        let output = self.linear_transform(&x3, &self.linear2, &self.bias2)?;

        Ok(output)
    }

    fn linear_transform(
        &self,
        input: &Array2<T>,
        weights: &Array2<T>,
        bias: &Array1<T>,
    ) -> Result<Array2<T>> {
        let (seq_len, input_dim) = input.dim();
        let (weight_in, weight_out) = weights.dim();

        if input_dim != weight_in {
            return Err(OptimError::InvalidConfig(
                "Input dimension doesn't match weight matrix".to_string(),
            ));
        }

        if bias.len() != weight_out {
            return Err(OptimError::InvalidConfig(
                "Bias dimension doesn't match output dimension".to_string(),
            ));
        }

        let mut output = Array2::zeros((seq_len, weight_out));

        for i in 0..seq_len {
            for j in 0..weight_out {
                let mut sum = T::zero();
                for k in 0..input_dim {
                    sum = sum + input[[i, k]] * weights[[k, j]];
                }
                output[[i, j]] = sum + bias[j];
            }
        }

        Ok(output)
    }

    fn apply_activation(&self, input: &Array2<T>) -> Result<Array2<T>> {
        let mut output = input.clone();

        match self.activation {
            ActivationFunction::ReLU => {
                output.mapv_inplace(|x| if x > T::zero() { x } else { T::zero() });
            }
            ActivationFunction::GELU => {
                // Approximate GELU: 0.5 * x * (1 + tanh(sqrt(2/Ï€) * (x + 0.044715 * x^3)))
                output.mapv_inplace(|x| {
                    let sqrt_2_pi = scirs2_core::numeric::NumCast::from(0.7978845608)
                        .unwrap_or_else(|| T::zero()); // sqrt(2/Ï€)
                    let coeff =
                        scirs2_core::numeric::NumCast::from(0.044715).unwrap_or_else(|| T::zero());
                    let x_cubed = x * x * x;
                    let inner = sqrt_2_pi * (x + coeff * x_cubed);
                    let tanh_val = inner.tanh();
                    scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero())
                        * x
                        * (T::one() + tanh_val)
                });
            }
            ActivationFunction::Swish => {
                output.mapv_inplace(|x| x * x.exp() / (T::one() + x.exp()));
            }
            ActivationFunction::GLU => {
                // For simplicity, treating as GELU for now
                output.mapv_inplace(|x| {
                    let sqrt_2_pi = scirs2_core::numeric::NumCast::from(0.7978845608)
                        .unwrap_or_else(|| T::zero());
                    let coeff =
                        scirs2_core::numeric::NumCast::from(0.044715).unwrap_or_else(|| T::zero());
                    let x_cubed = x * x * x;
                    let inner = sqrt_2_pi * (x + coeff * x_cubed);
                    let tanh_val = inner.tanh();
                    scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero())
                        * x
                        * (T::one() + tanh_val)
                });
            }
            ActivationFunction::GeGLU => {
                // For simplicity, treating as GELU for now
                output.mapv_inplace(|x| {
                    let sqrt_2_pi = scirs2_core::numeric::NumCast::from(0.7978845608)
                        .unwrap_or_else(|| T::zero());
                    let coeff =
                        scirs2_core::numeric::NumCast::from(0.044715).unwrap_or_else(|| T::zero());
                    let x_cubed = x * x * x;
                    let inner = sqrt_2_pi * (x + coeff * x_cubed);
                    let tanh_val = inner.tanh();
                    scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero())
                        * x
                        * (T::one() + tanh_val)
                });
            }
        }

        Ok(output)
    }

    /// Set activation function
    pub fn set_activation(&mut self, activation: ActivationFunction) {
        self.activation = activation;
    }
}

impl<T: Float + Debug + Default + Clone + std::iter::Sum + Send + Sync> LayerNorm<T> {
    pub fn new(dim: usize) -> Self {
        Self {
            gamma: Array1::ones(dim),
            beta: Array1::zeros(dim),
            eps: scirs2_core::numeric::NumCast::from(1e-6).unwrap_or_else(|| T::zero()),
            dim,
        }
    }

    pub fn forward(&self, input: &Array2<T>) -> Result<Array2<T>> {
        let (seq_len, input_dim) = input.dim();

        if input_dim != self.dim {
            return Err(OptimError::InvalidConfig(format!(
                "Input dimension {} doesn't match layer norm dimension {}",
                input_dim, self.dim
            )));
        }

        let mut output = Array2::zeros((seq_len, input_dim));

        for i in 0..seq_len {
            let row = input.slice(s![i, ..]);

            // Compute mean
            let mean = row.iter().cloned().sum::<T>()
                / scirs2_core::numeric::NumCast::from(input_dim).unwrap_or_else(|| T::zero());

            // Compute variance
            let variance = row
                .iter()
                .map(|&x| {
                    let diff = x - mean;
                    diff * diff
                })
                .sum::<T>()
                / scirs2_core::numeric::NumCast::from(input_dim).unwrap_or_else(|| T::zero());

            let std = (variance + self.eps).sqrt();

            // Normalize and scale/shift
            for j in 0..input_dim {
                let normalized = (input[[i, j]] - mean) / std;
                output[[i, j]] = self.gamma[j] * normalized + self.beta[j];
            }
        }

        Ok(output)
    }

    /// Get layer normalization parameters
    pub fn parameters(&self) -> (&Array1<T>, &Array1<T>) {
        (&self.gamma, &self.beta)
    }

    /// Set layer normalization parameters
    pub fn set_parameters(&mut self, gamma: Array1<T>, beta: Array1<T>) -> Result<()> {
        if gamma.len() != self.dim || beta.len() != self.dim {
            return Err(OptimError::InvalidConfig(
                "Parameter dimensions don't match layer norm dimension".to_string(),
            ));
        }
        self.gamma = gamma;
        self.beta = beta;
        Ok(())
    }
}

impl DropoutLayer {
    pub fn new(prob: f64) -> Self {
        Self {
            prob,
            training: true,
        }
    }

    pub fn forward<T: Float + Clone>(&self, input: &Array2<T>) -> Result<Array2<T>> {
        if !self.training || self.prob == 0.0 {
            return Ok(input.clone());
        }

        // For simplicity, just return input during inference/testing
        // In a full implementation, this would apply dropout during training
        Ok(input.clone())
    }

    /// Set training mode
    pub fn set_training(&mut self, training: bool) {
        self.training = training;
    }

    /// Get dropout probability
    pub fn prob(&self) -> f64 {
        self.prob
    }
}