optirs-learned 0.3.2

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
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
// Core transformer layer implementations

use super::config::ActivationFunction;
use crate::error::{OptimError, Result};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use std::fmt::Debug;

/// Draw a Xavier/Glorot uniform matrix with limit `sqrt(6 / (fan_in + fan_out))`.
fn xavier_uniform<
    T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
>(
    fan_in: usize,
    fan_out: usize,
) -> Array2<T> {
    let mut rng = scirs2_core::random::thread_rng();
    let bound = (6.0 / (fan_in + fan_out).max(1) as f64).sqrt();
    let mut weights = Array2::zeros((fan_in, fan_out));
    for elem in weights.iter_mut() {
        *elem = scirs2_core::numeric::NumCast::from((rng.random::<f64>() - 0.5) * 2.0 * bound)
            .unwrap_or_else(|| T::zero());
    }
    weights
}

/// Input projection layer.
///
/// Optimization traces are continuous vectors, not token ids, so this is a
/// dense linear projection `input @ W` rather than a vocabulary lookup. The
/// previous implementation cast each float to a `usize` index, which mapped
/// every value in `(-1, 1)` onto row 0 and discarded the input entirely.
pub struct EmbeddingLayer<
    T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
> {
    /// Projection matrix, shape `(input_dimension, output_dimension)`
    embedding_matrix: Array2<T>,

    /// Input dimension
    input_dimension: usize,

    /// Output dimension (model dimension)
    output_dimension: usize,
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>
    EmbeddingLayer<T>
{
    /// Create a new input projection with Xavier-initialized weights.
    pub fn new(input_dimension: usize, output_dimension: usize) -> Result<Self> {
        if input_dimension == 0 || output_dimension == 0 {
            return Err(OptimError::InvalidConfig(
                "Embedding dimensions must be positive".to_string(),
            ));
        }

        Ok(Self {
            embedding_matrix: xavier_uniform(input_dimension, output_dimension),
            input_dimension,
            output_dimension,
        })
    }

    /// Project a `(sequence_length, input_dimension)` matrix to the model dimension.
    pub fn forward(&self, input: &Array2<T>) -> Result<Array2<T>> {
        let (_sequence_length, width) = input.dim();
        if width != self.input_dimension {
            return Err(OptimError::InvalidConfig(format!(
                "Embedding input width {width} does not match the expected {}",
                self.input_dimension
            )));
        }

        Ok(input.dot(&self.embedding_matrix))
    }

    /// Get parameter count
    pub fn parameter_count(&self) -> usize {
        self.input_dimension * self.output_dimension
    }

    /// Projection weights
    pub fn weights(&self) -> &Array2<T> {
        &self.embedding_matrix
    }

    /// Replace the projection weights
    pub fn set_weights(&mut self, weights: Array2<T>) -> Result<()> {
        if weights.dim() != (self.input_dimension, self.output_dimension) {
            return Err(OptimError::InvalidConfig(format!(
                "Weight shape {:?} does not match ({}, {})",
                weights.dim(),
                self.input_dimension,
                self.output_dimension
            )));
        }
        self.embedding_matrix = weights;
        Ok(())
    }

    /// Gradient of the projection with respect to its weights, and an SGD step.
    ///
    /// For `y = x W`, `dL/dW = x^T (dL/dy)`.
    pub fn backward(
        &mut self,
        input: &Array2<T>,
        grad_output: &Array2<T>,
        learning_rate: T,
    ) -> Result<Array2<T>> {
        if input.ncols() != self.input_dimension || grad_output.ncols() != self.output_dimension {
            return Err(OptimError::InvalidConfig(
                "Embedding backward received mismatched shapes".to_string(),
            ));
        }
        if input.nrows() != grad_output.nrows() {
            return Err(OptimError::InvalidConfig(
                "Embedding backward received mismatched row counts".to_string(),
            ));
        }

        let grad_weights = input.t().dot(grad_output);
        self.embedding_matrix = &self.embedding_matrix - &(grad_weights.clone() * learning_rate);
        Ok(grad_weights)
    }

    /// Re-randomize the projection weights.
    pub fn reset(&mut self) -> Result<()> {
        self.embedding_matrix = xavier_uniform(self.input_dimension, self.output_dimension);
        Ok(())
    }
}

/// Layer normalization implementation
pub struct LayerNormalization<
    T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
> {
    /// Layer dimension
    dimension: usize,

    /// Learnable scale parameters
    gamma: Array1<T>,

    /// Learnable shift parameters
    beta: Array1<T>,

    /// Small constant for numerical stability
    epsilon: T,
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>
    LayerNormalization<T>
{
    /// Create new layer normalization
    pub fn new(dimension: usize) -> Result<Self> {
        let gamma = Array1::ones(dimension);
        let beta = Array1::zeros(dimension);
        let epsilon = scirs2_core::numeric::NumCast::from(1e-5).unwrap_or_else(|| T::zero());

        Ok(Self {
            dimension,
            gamma,
            beta,
            epsilon,
        })
    }

    /// Forward pass through layer normalization.
    ///
    /// Rejects inputs whose width differs from the configured dimension instead
    /// of indexing out of bounds.
    pub fn forward(&self, input: &Array2<T>) -> Result<Array2<T>> {
        let (rows, width) = input.dim();
        if width != self.dimension {
            return Err(OptimError::InvalidConfig(format!(
                "Layer norm input width {width} does not match its dimension {}",
                self.dimension
            )));
        }
        if width == 0 {
            return Ok(input.clone());
        }

        let mut output = input.clone();
        let width_t: T =
            scirs2_core::numeric::NumCast::from(width as f64).unwrap_or_else(|| T::one());

        for i in 0..rows {
            let row = input.row(i);

            let mean = row.sum() / width_t;
            let variance = row
                .iter()
                .map(|&x| {
                    let diff = x - mean;
                    diff * diff
                })
                .fold(T::zero(), |acc, x| acc + x)
                / width_t;

            let std_dev = (variance + self.epsilon).sqrt();

            for j in 0..self.dimension {
                let normalized = (input[[i, j]] - mean) / std_dev;
                output[[i, j]] = self.gamma[j] * normalized + self.beta[j];
            }
        }

        Ok(output)
    }

    /// Normalized dimension
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Backward pass through layer normalization.
    ///
    /// With `xhat = (x - mu) / sqrt(var + eps)` and `y = gamma * xhat + beta`,
    /// the exact input gradient for one row is
    /// `dL/dx = (1/sigma) * (g' - mean(g') - xhat * mean(g' * xhat))`
    /// where `g' = dL/dy * gamma`. `gamma` and `beta` are updated in place with
    /// a plain SGD step and the input gradient is returned.
    pub fn backward(
        &mut self,
        input: &Array2<T>,
        grad_output: &Array2<T>,
        learning_rate: T,
    ) -> Result<Array2<T>> {
        if input.dim() != grad_output.dim() {
            return Err(OptimError::InvalidConfig(format!(
                "Layer norm backward shape mismatch: {:?} vs {:?}",
                input.dim(),
                grad_output.dim()
            )));
        }
        let (rows, width) = input.dim();
        if width != self.dimension {
            return Err(OptimError::InvalidConfig(format!(
                "Layer norm backward width {width} does not match dimension {}",
                self.dimension
            )));
        }
        if width == 0 {
            return Ok(Array2::zeros(input.dim()));
        }

        let width_t: T =
            scirs2_core::numeric::NumCast::from(width as f64).unwrap_or_else(|| T::one());
        let mut grad_input = Array2::zeros(input.dim());
        let mut grad_gamma = Array1::zeros(width);
        let mut grad_beta = Array1::zeros(width);

        for i in 0..rows {
            let row = input.row(i);
            let mean = row.sum() / width_t;
            let variance = row
                .iter()
                .map(|&x| (x - mean) * (x - mean))
                .fold(T::zero(), |acc, x| acc + x)
                / width_t;
            let sigma = (variance + self.epsilon).sqrt();

            let mut xhat = Array1::zeros(width);
            for j in 0..width {
                xhat[j] = (input[[i, j]] - mean) / sigma;
            }

            let mut g_scaled = Array1::zeros(width);
            for j in 0..width {
                let g = grad_output[[i, j]];
                grad_gamma[j] = grad_gamma[j] + g * xhat[j];
                grad_beta[j] = grad_beta[j] + g;
                g_scaled[j] = g * self.gamma[j];
            }

            let mean_g = g_scaled.iter().fold(T::zero(), |a, &b| a + b) / width_t;
            let mean_gx = g_scaled
                .iter()
                .zip(xhat.iter())
                .map(|(&g, &x)| g * x)
                .fold(T::zero(), |a, b| a + b)
                / width_t;

            for j in 0..width {
                grad_input[[i, j]] = (g_scaled[j] - mean_g - xhat[j] * mean_gx) / sigma;
            }
        }

        self.gamma = &self.gamma - &(grad_gamma * learning_rate);
        self.beta = &self.beta - &(grad_beta * learning_rate);

        Ok(grad_input)
    }

    /// Get parameter count
    pub fn parameter_count(&self) -> usize {
        2 * self.dimension // gamma + beta
    }

    /// Reset normalization parameters
    pub fn reset(&mut self) -> Result<()> {
        self.gamma.fill(T::one());
        self.beta.fill(T::zero());
        Ok(())
    }
}

/// Dropout layer for regularization, backed by a seeded generator so training
/// runs are reproducible.
#[derive(Debug, Clone)]
pub struct DropoutLayer {
    /// Dropout probability
    dropout_rate: f64,

    /// Training mode flag. Defaults to `false`, so inference is the safe default.
    training: bool,

    /// xorshift64* state (never zero)
    rng_state: u64,
}

impl DropoutLayer {
    /// Create a new dropout layer with a default seed. Starts in inference mode.
    pub fn new(dropout_rate: f64) -> Self {
        Self::with_seed(dropout_rate, 0x9E37_79B9_7F4A_7C15)
    }

    /// Create a new dropout layer with an explicit seed. Starts in inference mode.
    pub fn with_seed(dropout_rate: f64, seed: u64) -> Self {
        Self {
            dropout_rate: dropout_rate.clamp(0.0, 1.0),
            training: false,
            rng_state: seed | 1,
        }
    }

    fn next_f64(&mut self) -> f64 {
        let mut x = self.rng_state;
        x ^= x >> 12;
        x ^= x << 25;
        x ^= x >> 27;
        self.rng_state = x;
        let value = x.wrapping_mul(0x2545_F491_4F6C_DD1D);
        (value >> 11) as f64 / (1u64 << 53) as f64
    }

    /// Inverted dropout: zero entries with probability `dropout_rate` and scale
    /// the survivors by `1 / (1 - rate)` so the expectation is preserved.
    pub fn forward<
        T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
    >(
        &mut self,
        input: &Array2<T>,
    ) -> Array2<T> {
        if !self.training || self.dropout_rate <= 0.0 {
            return input.clone();
        }
        if self.dropout_rate >= 1.0 {
            return Array2::zeros(input.dim());
        }

        let mut output = input.clone();
        let keep_prob = 1.0 - self.dropout_rate;
        let scale: T =
            scirs2_core::numeric::NumCast::from(1.0 / keep_prob).unwrap_or_else(|| T::one());

        for elem in output.iter_mut() {
            if self.next_f64() < self.dropout_rate {
                *elem = T::zero();
            } else {
                *elem = *elem * scale;
            }
        }

        output
    }

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

    /// Get training mode
    pub fn is_training(&self) -> bool {
        self.training
    }

    /// Dropout probability
    pub fn dropout_rate(&self) -> f64 {
        self.dropout_rate
    }
}

/// Output projection layer
pub struct OutputProjection<
    T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
> {
    /// Weight matrix
    weight: Array2<T>,

    /// Bias vector
    bias: Array1<T>,

    /// Input dimension
    input_dim: usize,

    /// Output dimension
    output_dim: usize,
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>
    OutputProjection<T>
{
    /// Create a new output projection with Xavier-initialized weights.
    pub fn new(input_dim: usize, output_dim: usize) -> Result<Self> {
        if input_dim == 0 || output_dim == 0 {
            return Err(OptimError::InvalidConfig(
                "Output projection dimensions must be positive".to_string(),
            ));
        }

        Ok(Self {
            weight: xavier_uniform(input_dim, output_dim),
            bias: Array1::zeros(output_dim),
            input_dim,
            output_dim,
        })
    }

    /// Forward pass: `input @ weight + bias`.
    pub fn forward(&self, input: &Array2<T>) -> Result<Array2<T>> {
        let (_rows, width) = input.dim();
        if width != self.input_dim {
            return Err(OptimError::InvalidConfig(format!(
                "Projection input width {width} does not match the expected {}",
                self.input_dim
            )));
        }

        Ok(input.dot(&self.weight) + &self.bias)
    }

    /// Get parameter count
    pub fn parameter_count(&self) -> usize {
        self.input_dim * self.output_dim + self.output_dim
    }

    /// Projection weights
    pub fn weights(&self) -> &Array2<T> {
        &self.weight
    }

    /// Projection bias
    pub fn bias(&self) -> &Array1<T> {
        &self.bias
    }

    /// Replace the projection parameters
    pub fn set_parameters(&mut self, weight: Array2<T>, bias: Array1<T>) -> Result<()> {
        if weight.dim() != (self.input_dim, self.output_dim) || bias.len() != self.output_dim {
            return Err(OptimError::InvalidConfig(format!(
                "Parameter shapes {:?}/{} do not match ({}, {})",
                weight.dim(),
                bias.len(),
                self.input_dim,
                self.output_dim
            )));
        }
        self.weight = weight;
        self.bias = bias;
        Ok(())
    }

    /// Backward pass for `y = x W + b`.
    ///
    /// Returns `dL/dx` and applies an SGD step to `W` and `b`.
    pub fn backward(
        &mut self,
        input: &Array2<T>,
        grad_output: &Array2<T>,
        learning_rate: T,
    ) -> Result<Array2<T>> {
        if input.ncols() != self.input_dim || grad_output.ncols() != self.output_dim {
            return Err(OptimError::InvalidConfig(
                "Output projection backward received mismatched shapes".to_string(),
            ));
        }
        if input.nrows() != grad_output.nrows() {
            return Err(OptimError::InvalidConfig(
                "Output projection backward received mismatched row counts".to_string(),
            ));
        }

        let grad_weight = input.t().dot(grad_output);
        let mut grad_bias = Array1::zeros(self.output_dim);
        for j in 0..self.output_dim {
            grad_bias[j] = grad_output.column(j).iter().fold(T::zero(), |a, &b| a + b);
        }

        let grad_input = grad_output.dot(&self.weight.t());

        self.weight = &self.weight - &(grad_weight * learning_rate);
        self.bias = &self.bias - &(grad_bias * learning_rate);

        Ok(grad_input)
    }

    /// Re-randomize the projection weights and zero the bias.
    pub fn reset(&mut self) -> Result<()> {
        self.weight = xavier_uniform(self.input_dim, self.output_dim);
        self.bias.fill(T::zero());
        Ok(())
    }
}

/// Residual connections manager
pub struct ResidualConnections<
    T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
> {
    /// Model dimension
    dimension: usize,

    /// Optional learnable scaling factor
    scale_factor: Option<T>,
}

impl<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>
    ResidualConnections<T>
{
    /// Create new residual connections
    pub fn new(dimension: usize) -> Self {
        Self {
            dimension,
            scale_factor: None,
        }
    }

    /// Create with learnable scaling
    pub fn new_with_scaling(dimension: usize, initial_scale: T) -> Self {
        Self {
            dimension,
            scale_factor: Some(initial_scale),
        }
    }

    /// The model dimension this residual path was built for.
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Add residual connection.
    ///
    /// Both operands must be `(rows, dimension)`. Checking only that the two
    /// shapes matched each other let a pair that agreed with each other but not
    /// with the model dimension pass silently — the residual would then be added
    /// to activations from a differently-sized layer without complaint.
    pub fn add(&self, input: &Array2<T>, residual: &Array2<T>) -> Result<Array2<T>> {
        if input.shape() != residual.shape() {
            return Err(crate::error::OptimError::Other(
                "Shape mismatch in residual connection".to_string(),
            ));
        }
        if input.ncols() != self.dimension {
            return Err(crate::error::OptimError::InvalidConfig(format!(
                "Residual connection was built for model dimension {} but received \
                 {} features",
                self.dimension,
                input.ncols()
            )));
        }

        let mut output = input + residual;

        if let Some(scale) = self.scale_factor {
            output.mapv_inplace(|x| x * scale);
        }

        Ok(output)
    }

    /// Set scaling factor
    pub fn set_scale_factor(&mut self, scale: T) {
        self.scale_factor = Some(scale);
    }

    /// Get scaling factor
    pub fn get_scale_factor(&self) -> Option<T> {
        self.scale_factor
    }
}

/// Activation layer with various activation functions
pub struct ActivationLayer;

impl ActivationLayer {
    /// Apply activation function
    pub fn apply<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>(
        input: &Array2<T>,
        activation: ActivationFunction,
    ) -> Array2<T> {
        match activation {
            ActivationFunction::ReLU => Self::relu(input),
            ActivationFunction::GELU => Self::gelu(input),
            ActivationFunction::Swish => Self::swish(input),
            ActivationFunction::Tanh => Self::tanh(input),
            ActivationFunction::Sigmoid => Self::sigmoid(input),
            ActivationFunction::LeakyReLU => Self::leaky_relu(
                input,
                scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero()),
            ),
        }
    }

    /// ReLU activation
    fn relu<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>(
        input: &Array2<T>,
    ) -> Array2<T> {
        input.map(|&x| if x > T::zero() { x } else { T::zero() })
    }

    /// GELU activation (approximation)
    fn gelu<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>(
        input: &Array2<T>,
    ) -> Array2<T> {
        input.map(|&x| {
            let half = scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero());
            let one = T::one();
            let sqrt_2_pi =
                scirs2_core::numeric::NumCast::from(0.797884560802865).unwrap_or_else(|| T::zero()); // sqrt(2/Ï€)
            let coeff = scirs2_core::numeric::NumCast::from(0.044715).unwrap_or_else(|| T::zero());

            let tanh_arg = sqrt_2_pi * (x + coeff * x * x * x);
            let tanh_val = tanh_arg.tanh();

            half * x * (one + tanh_val)
        })
    }

    /// Swish activation
    fn swish<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>(
        input: &Array2<T>,
    ) -> Array2<T> {
        input.map(|&x| x * Self::sigmoid_scalar(x))
    }

    /// Tanh activation
    fn tanh<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>(
        input: &Array2<T>,
    ) -> Array2<T> {
        input.map(|&x| x.tanh())
    }

    /// Sigmoid activation
    fn sigmoid<T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static>(
        input: &Array2<T>,
    ) -> Array2<T> {
        input.map(|&x| Self::sigmoid_scalar(x))
    }

    /// Leaky ReLU activation
    fn leaky_relu<
        T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
    >(
        input: &Array2<T>,
        alpha: T,
    ) -> Array2<T> {
        input.map(|&x| if x > T::zero() { x } else { alpha * x })
    }

    /// Numerically stable sigmoid.
    fn sigmoid_scalar<
        T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
    >(
        x: T,
    ) -> T {
        let one = T::one();
        if x >= T::zero() {
            one / (one + (-x).exp())
        } else {
            let e = x.exp();
            e / (one + e)
        }
    }

    /// Elementwise derivative of the activation, evaluated at its *input*.
    pub fn derivative<
        T: Float + Debug + scirs2_core::ndarray::ScalarOperand + Send + Sync + 'static,
    >(
        pre_activation: &Array2<T>,
        activation: ActivationFunction,
    ) -> Array2<T> {
        match activation {
            ActivationFunction::ReLU => {
                pre_activation.map(|&x| if x > T::zero() { T::one() } else { T::zero() })
            }
            ActivationFunction::LeakyReLU => {
                let alpha: T =
                    scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero());
                pre_activation.map(|&x| if x > T::zero() { T::one() } else { alpha })
            }
            ActivationFunction::Tanh => pre_activation.map(|&x| {
                let t = x.tanh();
                T::one() - t * t
            }),
            ActivationFunction::Sigmoid => pre_activation.map(|&x| {
                let s = Self::sigmoid_scalar(x);
                s * (T::one() - s)
            }),
            ActivationFunction::Swish => pre_activation.map(|&x| {
                let s = Self::sigmoid_scalar(x);
                s + x * s * (T::one() - s)
            }),
            ActivationFunction::GELU => pre_activation.map(|&x| {
                // Derivative of 0.5 x (1 + tanh(a (x + c x^3)))
                let a: T = scirs2_core::numeric::NumCast::from(0.797_884_560_802_865)
                    .unwrap_or_else(|| T::one());
                let c: T =
                    scirs2_core::numeric::NumCast::from(0.044715).unwrap_or_else(|| T::zero());
                let half: T = scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::one());
                let three: T = scirs2_core::numeric::NumCast::from(3.0).unwrap_or_else(|| T::one());
                let inner = a * (x + c * x * x * x);
                let t = inner.tanh();
                let sech2 = T::one() - t * t;
                half * (T::one() + t) + half * x * sech2 * a * (T::one() + three * c * x * x)
            }),
        }
    }
}

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

    #[test]
    fn embedding_layer_projects_continuous_inputs() {
        let embedding = EmbeddingLayer::<f64>::new(6, 4).expect("embedding creation");
        assert_eq!(embedding.parameter_count(), 24);

        // Values inside (-1, 1) used to collapse onto embedding row 0.
        let input = Array2::<f64>::from_shape_fn((3, 6), |(i, j)| 0.1 * (i + j) as f64 - 0.3);
        let output = embedding.forward(&input).expect("forward");
        assert_eq!(output.dim(), (3, 4));
        assert!(
            output.iter().any(|&v| v != 0.0),
            "projection produced all zeros"
        );

        // Different rows must yield different projections.
        assert!(output.row(0) != output.row(2));
    }

    #[test]
    fn embedding_layer_matches_a_manual_matmul() {
        let mut embedding = EmbeddingLayer::<f64>::new(2, 2).expect("embedding creation");
        embedding
            .set_weights(
                Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 3.0, 4.0]).expect("valid shape"),
            )
            .expect("weights");
        let input = Array2::from_shape_vec((1, 2), vec![1.0, 1.0]).expect("valid shape");
        let output = embedding.forward(&input).expect("forward");
        assert!((output[[0, 0]] - 4.0).abs() < 1e-12);
        assert!((output[[0, 1]] - 6.0).abs() < 1e-12);
    }

    #[test]
    fn embedding_reset_re_randomizes() {
        let mut embedding = EmbeddingLayer::<f64>::new(8, 8).expect("embedding creation");
        let before = embedding.weights().clone();
        embedding.reset().expect("reset");
        assert!(embedding.weights().iter().any(|&v| v != 0.0));
        assert_ne!(*embedding.weights(), before);
    }

    #[test]
    fn embedding_rejects_wrong_width() {
        let embedding = EmbeddingLayer::<f64>::new(6, 4).expect("embedding creation");
        assert!(embedding.forward(&Array2::<f64>::zeros((2, 5))).is_err());
        assert!(EmbeddingLayer::<f64>::new(0, 4).is_err());
    }

    #[test]
    fn output_projection_produces_nonzero_output() {
        let projection = OutputProjection::<f64>::new(8, 4).expect("projection creation");
        let input = Array2::<f64>::ones((2, 8));
        let output = projection.forward(&input).expect("forward");
        assert_eq!(output.dim(), (2, 4));
        assert!(
            output.iter().any(|&v| v != 0.0),
            "zero-initialized projection produced all zeros"
        );
        assert_eq!(projection.parameter_count(), 8 * 4 + 4);
    }

    #[test]
    fn output_projection_reset_re_randomizes() {
        let mut projection = OutputProjection::<f64>::new(8, 4).expect("projection creation");
        let before = projection.weights().clone();
        projection.reset().expect("reset");
        assert!(projection.weights().iter().any(|&v| v != 0.0));
        assert_ne!(*projection.weights(), before);
    }

    #[test]
    fn layer_normalization_standardizes_and_validates() {
        let norm = LayerNormalization::<f64>::new(4).expect("layer norm creation");
        let input =
            Array2::<f64>::from_shape_vec((1, 4), vec![1.0, 2.0, 3.0, 4.0]).expect("valid shape");
        let output = norm.forward(&input).expect("forward");
        let mean: f64 = output.row(0).iter().sum::<f64>() / 4.0;
        assert!(mean.abs() < 1e-9, "mean {mean}");
        assert_eq!(norm.parameter_count(), 8);

        // Mismatched widths are an error, not an out-of-bounds index.
        assert!(norm.forward(&Array2::<f64>::zeros((2, 8))).is_err());
    }

    #[test]
    fn dropout_defaults_to_inference_and_is_reproducible() {
        let mut dropout = DropoutLayer::with_seed(0.5, 11);
        let input = Array2::<f64>::ones((8, 8));

        assert!(!dropout.is_training());
        assert_eq!(dropout.forward(&input), input);

        dropout.set_training(true);
        let first = dropout.forward(&input);
        let zeros = first.iter().filter(|&&v| v == 0.0).count();
        assert!(zeros > 0 && zeros < input.len());
        assert!(first.iter().all(|&v| v == 0.0 || (v - 2.0).abs() < 1e-12));

        let mut replay = DropoutLayer::with_seed(0.5, 11);
        replay.set_training(true);
        assert_eq!(replay.forward(&input), first);
    }

    #[test]
    fn residual_connections_validate_shapes() {
        let residual = ResidualConnections::<f64>::new(4);
        let input = Array2::<f64>::ones((2, 4));
        let other = Array2::<f64>::from_elem((2, 4), 0.5);
        let output = residual.add(&input, &other).expect("residual add");
        assert!((output[[0, 0]] - 1.5).abs() < 1e-12);
        assert!(residual.add(&input, &Array2::<f64>::ones((3, 4))).is_err());
    }

    #[test]
    fn activation_functions_behave() {
        let input =
            Array2::<f64>::from_shape_vec((2, 2), vec![-1.0, 0.0, 0.5, 1.0]).expect("valid shape");

        let relu = ActivationLayer::apply(&input, ActivationFunction::ReLU);
        assert_eq!(relu[[0, 0]], 0.0);
        assert_eq!(relu[[1, 1]], 1.0);

        let gelu = ActivationLayer::apply(&input, ActivationFunction::GELU);
        assert_eq!(gelu.shape(), input.shape());

        let sigmoid = ActivationLayer::apply(&input, ActivationFunction::Sigmoid);
        assert!(sigmoid.iter().all(|&x| (0.0..=1.0).contains(&x)));
    }

    #[test]
    fn activations_stay_finite_for_extreme_inputs() {
        let input =
            Array2::<f64>::from_shape_vec((1, 3), vec![-1e6, 0.0, 1e6]).expect("valid shape");
        for activation in [
            ActivationFunction::ReLU,
            ActivationFunction::GELU,
            ActivationFunction::Swish,
            ActivationFunction::Tanh,
            ActivationFunction::Sigmoid,
            ActivationFunction::LeakyReLU,
        ] {
            let output = ActivationLayer::apply(&input, activation);
            assert!(
                output.iter().all(|v| v.is_finite()),
                "{activation:?} produced {output:?}"
            );
        }
    }
}