scirs2-neural 0.4.2

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
//! Embedding layer implementations
//!
//! This module provides implementations of various embedding layers
//! such as word embeddings, positional embeddings, and patch embeddings for vision.

use crate::error::{NeuralError, Result};
use crate::layers::Layer;
use scirs2_core::ndarray::{Array, IxDyn, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use std::fmt::Debug;
use std::marker::PhantomData;
use std::sync::{Arc, RwLock};

/// Configuration for the Embedding layer
#[derive(Debug, Clone)]
pub struct EmbeddingConfig {
    /// Number of embeddings in the embedding table
    pub num_embeddings: usize,
    /// Dimension of each embedding vector
    pub embedding_dim: usize,
    /// Optional padding index that will have its embedding vector filled with zeros
    pub padding_idx: Option<usize>,
    /// Maximum norm for embedding vectors
    pub max_norm: Option<f64>,
    /// Type of norm to use with max_norm
    pub norm_type: f64,
    /// Whether to scale gradients by the inverse of frequency of the indices
    pub scale_grad_by_freq: bool,
}

impl Default for EmbeddingConfig {
    fn default() -> Self {
        Self {
            num_embeddings: 1,
            embedding_dim: 1,
            padding_idx: None,
            max_norm: None,
            norm_type: 2.0,
            scale_grad_by_freq: false,
        }
    }
}

/// Embedding layer that stores embeddings for discrete inputs
///
/// This layer is often used to store word embeddings and retrieve them using indices.
/// The input to the module is a list of indices, and the output is the corresponding
/// embedding vectors.
///
/// # Examples
///
/// ```rust
/// use scirs2_neural::layers::{Embedding, EmbeddingConfig, Layer};
/// use scirs2_core::ndarray::Array2;
///
/// let config = EmbeddingConfig {
///     num_embeddings: 100,
///     embedding_dim: 64,
///     ..Default::default()
/// };
/// let embedding = Embedding::<f64>::new(config).expect("Operation failed");
///
/// // Input: indices as floats (will be converted to usize)
/// let indices = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).expect("Operation failed");
/// let output = embedding.forward(&indices.into_dyn()).expect("Operation failed");
/// assert_eq!(output.shape(), &[2, 3, 64]);
/// ```
#[derive(Debug, Clone)]
pub struct Embedding<F: Float + Debug + Send + Sync + NumAssign> {
    /// Configuration for the embedding layer
    config: EmbeddingConfig,
    /// Weight matrix containing the embeddings (num_embeddings x embedding_dim)
    weights: Arc<RwLock<Array<F, IxDyn>>>,
    /// Gradient of the weight matrix
    weight_grad: Arc<RwLock<Array<F, IxDyn>>>,
    /// Frequency counter for indices (for scale_grad_by_freq)
    freq_counter: Option<Vec<usize>>,
    /// Input cache for backward pass
    input_cache: Arc<RwLock<Option<Array<F, IxDyn>>>>,
    /// Phantom data
    _phantom: PhantomData<F>,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + 'static + NumAssign> Embedding<F> {
    /// Create a new Embedding layer with the given configuration
    pub fn new(config: EmbeddingConfig) -> Result<Self> {
        if config.num_embeddings == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "num_embeddings must be greater than 0".to_string(),
            ));
        }

        if config.embedding_dim == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "embedding_dim must be greater than 0".to_string(),
            ));
        }

        // Validate padding_idx
        if let Some(idx) = config.padding_idx {
            if idx >= config.num_embeddings {
                return Err(NeuralError::InvalidArchitecture(format!(
                    "padding_idx ({}) must be less than num_embeddings ({})",
                    idx, config.num_embeddings
                )));
            }
        }

        // Initialize weights with uniform distribution scaled to [-1, 1]
        let weights_shape = IxDyn(&[config.num_embeddings, config.embedding_dim]);
        let mut weights = Array::from_shape_fn(weights_shape.clone(), |idx| {
            // Simple deterministic initialization
            let (i, j) = (idx[0], idx[1]);
            let value = ((i * 31 + j * 17) % 1000) as f64 / 1000.0 - 0.5;
            F::from(value).expect("Failed to convert to float")
        });

        // Set padding_idx embeddings to zero if specified
        if let Some(idx) = config.padding_idx {
            for j in 0..config.embedding_dim {
                weights[[idx, j]] = F::zero();
            }
        }

        // Initialize gradients with zeros
        let weight_grad = Array::zeros(weights_shape);

        // Initialize frequency counter if needed
        let freq_counter = if config.scale_grad_by_freq {
            Some(vec![0; config.num_embeddings])
        } else {
            None
        };

        Ok(Self {
            config,
            weights: Arc::new(RwLock::new(weights)),
            weight_grad: Arc::new(RwLock::new(weight_grad)),
            freq_counter,
            input_cache: Arc::new(RwLock::new(None)),
            _phantom: PhantomData,
        })
    }

    /// Create an Embedding layer from pretrained embeddings
    pub fn from_pretrained(
        embeddings: Array<F, IxDyn>,
        padding_idx: Option<usize>,
        max_norm: Option<f64>,
        norm_type: f64,
        scale_grad_by_freq: bool,
    ) -> Result<Self> {
        if embeddings.ndim() != 2 {
            return Err(NeuralError::InvalidArchitecture(
                "Embeddings parameter is expected to be 2-dimensional".to_string(),
            ));
        }

        let shape = embeddings.shape();
        let num_embeddings = shape[0];
        let embedding_dim = shape[1];

        if let Some(idx) = padding_idx {
            if idx >= num_embeddings {
                return Err(NeuralError::InvalidArchitecture(format!(
                    "padding_idx ({}) must be less than num_embeddings ({})",
                    idx, num_embeddings
                )));
            }
        }

        let config = EmbeddingConfig {
            num_embeddings,
            embedding_dim,
            padding_idx,
            max_norm,
            norm_type,
            scale_grad_by_freq,
        };

        let weight_grad = Array::zeros(IxDyn(&[num_embeddings, embedding_dim]));
        let freq_counter = if scale_grad_by_freq {
            Some(vec![0; num_embeddings])
        } else {
            None
        };

        Ok(Self {
            config,
            weights: Arc::new(RwLock::new(embeddings)),
            weight_grad: Arc::new(RwLock::new(weight_grad)),
            freq_counter,
            input_cache: Arc::new(RwLock::new(None)),
            _phantom: PhantomData,
        })
    }

    /// Get the number of embeddings
    pub fn num_embeddings(&self) -> usize {
        self.config.num_embeddings
    }

    /// Get the embedding dimension
    pub fn embedding_dim(&self) -> usize {
        self.config.embedding_dim
    }

    /// Apply max_norm to the embeddings if specified
    fn apply_max_norm(&self) -> Result<()> {
        if let Some(max_norm) = self.config.max_norm {
            let norm_type = self.config.norm_type;
            let p = F::from(norm_type).ok_or_else(|| {
                NeuralError::InvalidArchitecture(format!("Invalid norm_type: {}", norm_type))
            })?;
            let max_norm_f = F::from(max_norm).ok_or_else(|| {
                NeuralError::InvalidArchitecture(format!("Invalid max_norm: {}", max_norm))
            })?;

            let mut weights = self.weights.write().map_err(|_| {
                NeuralError::InferenceError("Failed to acquire write lock on weights".to_string())
            })?;

            // Calculate norms for each embedding vector
            for i in 0..self.config.num_embeddings {
                let mut norm = F::zero();

                // Calculate p-norm
                for j in 0..self.config.embedding_dim {
                    let val = weights[[i, j]];
                    if p == F::from(2.0).expect("Failed to convert constant to float") {
                        norm += val * val;
                    } else {
                        norm += val.abs().powf(p);
                    }
                }

                if p == F::from(2.0).expect("Failed to convert constant to float") {
                    norm = norm.sqrt();
                } else {
                    norm = norm.powf(F::one() / p);
                }

                // Apply max_norm if needed
                if norm > max_norm_f {
                    let scale = max_norm_f / norm;
                    for j in 0..self.config.embedding_dim {
                        weights[[i, j]] *= scale;
                    }
                }
            }
        }
        Ok(())
    }

    /// Internal forward pass implementation
    fn forward_impl(&self, indices: &[usize]) -> Result<Array<F, IxDyn>> {
        // Validate indices
        for &idx in indices.iter() {
            if idx >= self.config.num_embeddings {
                return Err(NeuralError::InferenceError(format!(
                    "Index {} out of bounds for embedding with {} entries",
                    idx, self.config.num_embeddings
                )));
            }
        }

        // Apply max_norm if specified
        self.apply_max_norm()?;

        let weights = self.weights.read().map_err(|_| {
            NeuralError::InferenceError("Failed to acquire read lock on weights".to_string())
        })?;

        // Create output array
        let output_shape = IxDyn(&[indices.len(), self.config.embedding_dim]);
        let mut output = Array::zeros(output_shape);

        // Lookup embeddings
        for (i, &idx) in indices.iter().enumerate() {
            for j in 0..self.config.embedding_dim {
                output[[i, j]] = weights[[idx, j]];
            }
        }

        Ok(output)
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + 'static + NumAssign> Layer<F>
    for Embedding<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        // Cache input for backward pass
        if let Ok(mut cache) = self.input_cache.write() {
            *cache = Some(input.clone());
        }

        // Convert input to indices
        let input_shape = input.shape().to_vec();
        let total_elements: usize = input_shape.iter().product();

        let indices: Vec<usize> = input.iter().map(|&x| x.to_usize().unwrap_or(0)).collect();

        // Get embeddings
        let flat_output = self.forward_impl(&indices)?;

        // Reshape output to match input shape + embedding_dim
        let mut output_shape = input_shape;
        output_shape.push(self.config.embedding_dim);

        let output = flat_output
            .into_shape_with_order(IxDyn(&output_shape))
            .map_err(|e| NeuralError::InferenceError(format!("Shape mismatch: {}", e)))?;

        Ok(output)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        // Get cached input
        let input_guard = self.input_cache.read().map_err(|_| {
            NeuralError::InferenceError("Failed to acquire read lock on input cache".to_string())
        })?;

        let input = input_guard.as_ref().ok_or_else(|| {
            NeuralError::InferenceError(
                "No cached input for backward pass. Call forward() first.".to_string(),
            )
        })?;

        // Convert input to indices
        let indices: Vec<usize> = input.iter().map(|&x| x.to_usize().unwrap_or(0)).collect();

        // Accumulate gradients
        let mut weight_grad = self.weight_grad.write().map_err(|_| {
            NeuralError::InferenceError("Failed to acquire write lock on weight_grad".to_string())
        })?;

        // Flatten grad_output for processing
        let grad_flat = grad_output.view();
        let grad_shape = grad_flat.shape();
        let embedding_dim = grad_shape[grad_shape.len() - 1];

        // Accumulate gradients for each embedding
        for (i, &idx) in indices.iter().enumerate() {
            // Skip padding indices
            if let Some(padding_idx) = self.config.padding_idx {
                if idx == padding_idx {
                    continue;
                }
            }

            for j in 0..embedding_dim {
                // Handle multi-dimensional input
                if grad_shape.len() > 2 {
                    // For now, use flat index
                    let flat_grad_idx = i * embedding_dim + j;
                    if flat_grad_idx < grad_output.len() {
                        weight_grad[[idx, j]] +=
                            grad_output.as_slice().expect("Operation failed")[flat_grad_idx];
                    }
                } else {
                    weight_grad[[idx, j]] += grad_output[[i, j]];
                }
            }
        }

        // Return zeros for input gradient (indices don't have meaningful gradients)
        Ok(Array::zeros(input.raw_dim()))
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        let lr = learning_rate;

        let weight_grad = self.weight_grad.read().map_err(|_| {
            NeuralError::InferenceError("Failed to acquire read lock on weight_grad".to_string())
        })?;

        let mut weights = self.weights.write().map_err(|_| {
            NeuralError::InferenceError("Failed to acquire write lock on weights".to_string())
        })?;

        // Handle frequency-based scaling
        if let Some(ref counter) = self.freq_counter {
            for (i, &count) in counter.iter().enumerate().take(self.config.num_embeddings) {
                // Skip padding indices
                if let Some(padding_idx) = self.config.padding_idx {
                    if i == padding_idx {
                        continue;
                    }
                }

                let scale = if count > 0 {
                    F::from(1.0 / count as f64).expect("Failed to convert to float")
                } else {
                    F::one()
                };

                for j in 0..self.config.embedding_dim {
                    weights[[i, j]] -= lr * scale * weight_grad[[i, j]];
                }
            }
        } else {
            // Standard gradient update
            for i in 0..self.config.num_embeddings {
                // Skip padding indices
                if let Some(padding_idx) = self.config.padding_idx {
                    if i == padding_idx {
                        continue;
                    }
                }

                for j in 0..self.config.embedding_dim {
                    weights[[i, j]] -= lr * weight_grad[[i, j]];
                }
            }
        }

        // Reset gradients
        drop(weights);
        drop(weight_grad);

        let mut weight_grad = self.weight_grad.write().map_err(|_| {
            NeuralError::InferenceError("Failed to acquire write lock on weight_grad".to_string())
        })?;
        weight_grad.fill(F::zero());

        Ok(())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn layer_type(&self) -> &str {
        "Embedding"
    }

    fn parameter_count(&self) -> usize {
        self.config.num_embeddings * self.config.embedding_dim
    }

    fn layer_description(&self) -> String {
        format!(
            "type:Embedding, num_embeddings:{}, embedding_dim:{}, params:{}",
            self.config.num_embeddings,
            self.config.embedding_dim,
            self.parameter_count()
        )
    }

    fn params(&self) -> Vec<Array<F, IxDyn>> {
        if let Ok(w) = self.weights.read() {
            vec![w.clone()]
        } else {
            vec![]
        }
    }

    fn set_params(&mut self, params: &[Array<F, IxDyn>]) -> Result<()> {
        if let Some(new_weights) = params.first() {
            if let Ok(mut w) = self.weights.write() {
                *w = new_weights.clone();
            }
        }
        Ok(())
    }
}

/// Positional Embedding layer for transformers and sequence models
///
/// This layer adds positional information to embeddings to help models
/// understand the position of elements in a sequence.
#[derive(Debug)]
pub struct PositionalEmbedding<F: Float + Debug + Send + Sync + NumAssign> {
    /// Maximum sequence length supported
    max_seq_length: usize,
    /// Embedding dimension
    embedding_dim: usize,
    /// Whether to use learned positional embeddings (true) or fixed sinusoidal (false)
    learned: bool,
    /// Weight matrix for learned positional embeddings
    weights: Option<Arc<RwLock<Array<F, IxDyn>>>>,
    /// Weight gradients
    weight_grad: Option<Arc<RwLock<Array<F, IxDyn>>>>,
    /// Phantom data
    _phantom: PhantomData<F>,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + 'static + NumAssign> PositionalEmbedding<F> {
    /// Create a new PositionalEmbedding layer
    pub fn new(max_seq_length: usize, embedding_dim: usize, learned: bool) -> Result<Self> {
        if max_seq_length == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "max_seq_length must be greater than 0".to_string(),
            ));
        }

        if embedding_dim == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "embedding_dim must be greater than 0".to_string(),
            ));
        }

        let (weights, weight_grad) = if learned {
            // Initialize learned positional embeddings
            let weights_shape = IxDyn(&[max_seq_length, embedding_dim]);

            // Xavier initialization
            let scale = F::from((2.0 / (max_seq_length + embedding_dim) as f64).sqrt())
                .expect("Operation failed");
            let weights = Array::from_shape_fn(weights_shape.clone(), |_| {
                scale
                    * (F::from(0.5).expect("Failed to convert constant to float")
                        - F::from(0.25).expect("Failed to convert constant to float"))
            });

            let weight_grad = Array::zeros(weights_shape);

            (
                Some(Arc::new(RwLock::new(weights))),
                Some(Arc::new(RwLock::new(weight_grad))),
            )
        } else {
            (None, None)
        };

        Ok(Self {
            max_seq_length,
            embedding_dim,
            learned,
            weights,
            weight_grad,
            _phantom: PhantomData,
        })
    }

    /// Generate sinusoidal positional embeddings
    fn generate_sinusoidal_embeddings(&self, seq_length: usize) -> Result<Array<F, IxDyn>> {
        if seq_length > self.max_seq_length {
            return Err(NeuralError::InvalidArchitecture(format!(
                "Sequence length {} exceeds maximum supported length {}",
                seq_length, self.max_seq_length
            )));
        }

        let mut pos_embeddings = Array::zeros(IxDyn(&[seq_length, self.embedding_dim]));

        for pos in 0..seq_length {
            for i in 0..self.embedding_dim {
                let div_term = (10000.0_f64).powf(2.0 * (i / 2) as f64 / self.embedding_dim as f64);
                let angle = pos as f64 / div_term;

                if i % 2 == 0 {
                    pos_embeddings[[pos, i]] = F::from(angle.sin()).expect("Operation failed");
                } else {
                    pos_embeddings[[pos, i]] = F::from(angle.cos()).expect("Operation failed");
                }
            }
        }

        Ok(pos_embeddings)
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + 'static + NumAssign> Layer<F>
    for PositionalEmbedding<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        // Validate input shape - at least 2D with last dimension being embedding_dim
        if input.ndim() < 2 {
            return Err(NeuralError::InferenceError(
                "Input to PositionalEmbedding must be at least 2D".to_string(),
            ));
        }

        let last_dim = *input.shape().last().expect("Operation failed");
        if last_dim != self.embedding_dim {
            return Err(NeuralError::InferenceError(format!(
                "Input embedding dimension {} doesn't match layer embedding dimension {}",
                last_dim, self.embedding_dim
            )));
        }

        // Get sequence length from the input shape (second to last dimension)
        let seq_dim = input.ndim() - 2;
        let seq_length = input.shape()[seq_dim];

        if seq_length > self.max_seq_length {
            return Err(NeuralError::InferenceError(format!(
                "Input sequence length {} exceeds maximum supported length {}",
                seq_length, self.max_seq_length
            )));
        }

        // Get positional embeddings
        let pos_embeddings = if self.learned {
            let weights = self
                .weights
                .as_ref()
                .expect("Operation failed")
                .read()
                .map_err(|_| {
                    NeuralError::InferenceError(
                        "Failed to acquire read lock on weights".to_string(),
                    )
                })?;

            // Extract the first seq_length positions
            let mut pos_emb = Array::zeros(IxDyn(&[seq_length, self.embedding_dim]));
            for i in 0..seq_length {
                for j in 0..self.embedding_dim {
                    pos_emb[[i, j]] = weights[[i, j]];
                }
            }
            pos_emb
        } else {
            self.generate_sinusoidal_embeddings(seq_length)?
        };

        // Add positional embeddings to input
        let mut output = input.clone();
        let input_shape = input.shape();

        // Simple broadcast: iterate over all positions and embedding dims
        // Handle batch dimensions by iterating over the full array
        let batch_dims: usize = input_shape[..seq_dim].iter().product();

        for batch_idx in 0..batch_dims {
            for pos in 0..seq_length {
                for emb_idx in 0..self.embedding_dim {
                    // Calculate flat index for this element
                    // For a simple 3D case (batch, seq, emb), this is straightforward
                    if input_shape.len() == 3 {
                        let b = batch_idx;
                        output[[b, pos, emb_idx]] += pos_embeddings[[pos, emb_idx]];
                    } else if input_shape.len() == 2 {
                        // 2D case (seq, emb)
                        output[[pos, emb_idx]] += pos_embeddings[[pos, emb_idx]];
                    }
                }
            }
        }

        Ok(output)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        // For PositionalEmbedding, gradients flow through directly
        Ok(grad_output.clone())
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        // Only update weights if using learned positional embeddings
        if self.learned {
            if let (Some(ref weights_lock), Some(ref weight_grad_lock)) =
                (&self.weights, &self.weight_grad)
            {
                let lr = learning_rate;

                let weight_grad = weight_grad_lock.read().map_err(|_| {
                    NeuralError::InferenceError(
                        "Failed to acquire read lock on weight_grad".to_string(),
                    )
                })?;

                let mut weights = weights_lock.write().map_err(|_| {
                    NeuralError::InferenceError(
                        "Failed to acquire write lock on weights".to_string(),
                    )
                })?;

                for i in 0..self.max_seq_length {
                    for j in 0..self.embedding_dim {
                        weights[[i, j]] -= lr * weight_grad[[i, j]];
                    }
                }

                // Reset gradients
                drop(weights);
                drop(weight_grad);

                let mut weight_grad = weight_grad_lock.write().map_err(|_| {
                    NeuralError::InferenceError(
                        "Failed to acquire write lock on weight_grad".to_string(),
                    )
                })?;
                weight_grad.fill(F::zero());
            }
        }

        Ok(())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn layer_type(&self) -> &str {
        if self.learned {
            "LearnedPositionalEmbedding"
        } else {
            "SinusoidalPositionalEmbedding"
        }
    }

    fn parameter_count(&self) -> usize {
        if self.learned {
            self.max_seq_length * self.embedding_dim
        } else {
            0
        }
    }
}

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

    #[test]
    fn test_embedding_config_default() {
        let config = EmbeddingConfig::default();
        assert_eq!(config.num_embeddings, 1);
        assert_eq!(config.embedding_dim, 1);
        assert!(config.padding_idx.is_none());
    }

    #[test]
    fn test_embedding_creation() {
        let config = EmbeddingConfig {
            num_embeddings: 100,
            embedding_dim: 64,
            padding_idx: Some(0),
            ..Default::default()
        };

        let embedding = Embedding::<f64>::new(config).expect("Operation failed");
        assert_eq!(embedding.num_embeddings(), 100);
        assert_eq!(embedding.embedding_dim(), 64);
    }

    #[test]
    fn test_embedding_creation_invalid() {
        let config = EmbeddingConfig {
            num_embeddings: 0,
            embedding_dim: 64,
            ..Default::default()
        };
        assert!(Embedding::<f64>::new(config).is_err());
    }

    #[test]
    fn test_embedding_forward() {
        let config = EmbeddingConfig {
            num_embeddings: 10,
            embedding_dim: 4,
            ..Default::default()
        };

        let embedding = Embedding::<f64>::new(config).expect("Operation failed");

        // Input: indices as floats
        let indices = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("Operation failed");
        let output = embedding
            .forward(&indices.into_dyn())
            .expect("Operation failed");

        // Output shape: (2, 3, 4)
        assert_eq!(output.shape(), &[2, 3, 4]);
    }

    #[test]
    fn test_embedding_parameter_count() {
        let config = EmbeddingConfig {
            num_embeddings: 100,
            embedding_dim: 64,
            ..Default::default()
        };

        let embedding = Embedding::<f64>::new(config).expect("Operation failed");
        assert_eq!(embedding.parameter_count(), 100 * 64);
    }

    #[test]
    fn test_positional_embedding_learned() {
        let pos_emb = PositionalEmbedding::<f64>::new(100, 64, true).expect("Operation failed");
        assert!(pos_emb.weights.is_some());
        assert_eq!(pos_emb.parameter_count(), 100 * 64);
    }

    #[test]
    fn test_positional_embedding_sinusoidal() {
        let pos_emb = PositionalEmbedding::<f64>::new(100, 64, false).expect("Operation failed");
        assert!(pos_emb.weights.is_none());
        assert_eq!(pos_emb.parameter_count(), 0);
    }

    #[test]
    fn test_positional_embedding_forward() {
        let pos_emb = PositionalEmbedding::<f64>::new(100, 64, false).expect("Operation failed");

        // Input: (batch=2, seq=10, emb=64)
        let input = Array::from_elem(IxDyn(&[2, 10, 64]), 1.0);
        let output = pos_emb.forward(&input).expect("Operation failed");

        assert_eq!(output.shape(), &[2, 10, 64]);
    }
}