torsh-text 0.1.2

Natural language processing utilities for ToRSh deep learning framework
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
use crate::{Result, TextError};
use std::collections::HashMap;
use std::path::Path;
use torsh_core::{device::DeviceType as Device, dtype::DType};
use torsh_tensor::creation::{arange, rand, randn, tensor_1d};
use torsh_tensor::Tensor;

// Temporary placeholder types until torsh-nn is available
#[derive(Debug, Clone)]
pub struct Parameter {
    tensor: Tensor,
    requires_grad: bool,
}

impl Parameter {
    pub fn new(tensor: Tensor, requires_grad: bool) -> Self {
        Self {
            tensor,
            requires_grad,
        }
    }

    pub fn tensor(&self) -> &Tensor {
        &self.tensor
    }
}

pub trait Module {
    fn forward(&self, input: &Tensor) -> Result<Tensor>;
    fn parameters(&self) -> Vec<&Parameter>;
    fn train(&mut self, mode: bool);
    fn eval(&mut self);
}

#[derive(Debug, Clone)]
pub struct LayerNorm {
    weight: Parameter,
    bias: Parameter,
    eps: f32,
}

impl LayerNorm {
    pub fn new(normalized_shape: usize, eps: f32, device: &Device, _dtype: DType) -> Result<Self> {
        let weight = Parameter::new(Tensor::<f32>::ones(&[normalized_shape], *device)?, true);
        let bias = Parameter::new(Tensor::<f32>::zeros(&[normalized_shape], *device)?, true);

        Ok(Self { weight, bias, eps })
    }
}

impl Module for LayerNorm {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // Simplified layer norm implementation
        let last_dim = input.shape().dims().len() - 1;
        let mean = input.mean(Some(&[last_dim]), true)?;
        let diff = input - &mean;
        let variance = diff.pow(2.0)?.mean(Some(&[last_dim]), true)?;
        let eps_tensor = Tensor::from_vec(vec![self.eps], &[1])?;
        let var_plus_eps = &variance + &eps_tensor;
        let std_dev = var_plus_eps.sqrt()?;
        let normalized = &diff / &std_dev;
        let scaled = &normalized * self.weight.tensor();
        let shifted = &scaled + self.bias.tensor();
        Ok(shifted)
    }

    fn parameters(&self) -> Vec<&Parameter> {
        vec![&self.weight, &self.bias]
    }

    fn train(&mut self, _mode: bool) {
        // No-op for this simple implementation
    }

    fn eval(&mut self) {
        // No-op for this simple implementation
    }
}

// ============================================================================
// Word Embeddings
// ============================================================================

#[derive(Debug, Clone)]
pub struct WordEmbedding {
    pub weight: Parameter,
    pub vocab_size: usize,
    pub embedding_dim: usize,
    pub padding_idx: Option<usize>,
    pub max_norm: Option<f32>,
    pub norm_type: f32,
    pub scale_grad_by_freq: bool,
    pub sparse: bool,
}

impl WordEmbedding {
    pub fn new(
        vocab_size: usize,
        embedding_dim: usize,
        padding_idx: Option<usize>,
        max_norm: Option<f32>,
        norm_type: f32,
        scale_grad_by_freq: bool,
        sparse: bool,
        _device: &Device,
        _dtype: DType,
    ) -> Result<Self> {
        let weight = Parameter::new(randn::<f32>(&[vocab_size, embedding_dim])?, true);

        // Zero out padding embeddings if specified
        if let Some(pad_idx) = padding_idx {
            if pad_idx < vocab_size {
                let weight_data = weight.tensor().clone();
                let mut padding_row =
                    weight_data.index_select(0, &tensor_1d(&[pad_idx as i64])?)?;
                padding_row.fill_(0.0)?;
            }
        }

        Ok(Self {
            weight,
            vocab_size,
            embedding_dim,
            padding_idx,
            max_norm,
            norm_type,
            scale_grad_by_freq,
            sparse,
        })
    }

    pub fn from_pretrained(
        embeddings: Tensor,
        freeze: bool,
        padding_idx: Option<usize>,
        max_norm: Option<f32>,
        norm_type: f32,
        scale_grad_by_freq: bool,
        sparse: bool,
    ) -> Result<Self> {
        let shape = embeddings.shape();
        if shape.ndim() != 2 {
            return Err(TextError::ModelError(
                "Embedding tensor must be 2-dimensional".to_string(),
            ));
        }

        let dims = shape.dims();
        let vocab_size = dims[0];
        let embedding_dim = dims[1];

        let weight = Parameter::new(embeddings, !freeze);

        Ok(Self {
            weight,
            vocab_size,
            embedding_dim,
            padding_idx,
            max_norm,
            norm_type,
            scale_grad_by_freq,
            sparse,
        })
    }

    #[allow(clippy::too_many_arguments)]
    pub fn simple(
        vocab_size: usize,
        embedding_dim: usize,
        device: &Device,
        dtype: DType,
    ) -> Result<Self> {
        Self::new(
            vocab_size,
            embedding_dim,
            None,
            None,
            2.0,
            false,
            false,
            device,
            dtype,
        )
    }
}

impl Module for WordEmbedding {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // Convert float indices to i64 for indexing
        let float_data = input.to_vec()?;
        let int_data: Vec<i64> = float_data.iter().map(|&x| x.round() as i64).collect();
        let indices = Tensor::from_vec(int_data, input.shape().dims())?;
        let embeddings = self.weight.tensor().index_select(0, &indices)?;

        // Apply max norm if specified
        if let Some(max_norm) = self.max_norm {
            // Compute L2 norm along the last dimension for each embedding
            let squared = embeddings.pow(2.0)?;
            let last_dim = (embeddings.shape().ndim() - 1) as i32;
            let norms = squared.sum_dim(&[last_dim], true)?;
            let l2_norms = norms.sqrt()?;

            // Create scale factor tensor: min(max_norm / norm, 1.0)
            let max_norm_tensor = Tensor::from_vec(vec![max_norm], &[1])?;
            let scale_factors = l2_norms.div(&max_norm_tensor)?;
            let ones = Tensor::ones_like(&scale_factors)?;
            let scale_factors = scale_factors.minimum(&ones)?;

            // Apply scaling to embeddings
            let scaled_embeddings = embeddings.mul_op(&scale_factors)?;
            return Ok(scaled_embeddings);
        }

        Ok(embeddings)
    }

    fn parameters(&self) -> Vec<&Parameter> {
        vec![&self.weight]
    }

    fn train(&mut self, _mode: bool) {
        // No-op for embeddings
    }

    fn eval(&mut self) {
        // No-op for embeddings
    }
}

// ============================================================================
// Positional Encoding
// ============================================================================

#[derive(Debug, Clone)]
pub struct PositionalEncoding {
    pub encoding: Tensor,
    pub dropout: f32,
    pub max_len: usize,
    pub d_model: usize,
}

impl PositionalEncoding {
    pub fn new(
        d_model: usize,
        dropout: f32,
        max_len: usize,
        _device: &Device,
        _dtype: DType,
    ) -> Result<Self> {
        // Create proper sinusoidal positional encoding using CPU computation
        // This is more efficient than attempting tensor operations for sine/cosine

        // Create proper sinusoidal positional encoding
        // For even indices (0, 2, 4, ...), use sine
        // For odd indices (1, 3, 5, ...), use cosine
        let mut pe_data = vec![0.0f32; max_len * d_model];

        for pos in 0..max_len {
            for i in 0..d_model {
                let angle =
                    pos as f32 * (-10000.0_f32.ln() / d_model as f32 * (i / 2) as f32).exp();
                if i % 2 == 0 {
                    pe_data[pos * d_model + i] = angle.sin();
                } else {
                    pe_data[pos * d_model + i] = angle.cos();
                }
            }
        }

        let pe = Tensor::from_vec(pe_data, &[max_len, d_model])?;

        Ok(Self {
            encoding: pe,
            dropout,
            max_len,
            d_model,
        })
    }

    pub fn sinusoidal(
        d_model: usize,
        max_len: usize,
        device: &Device,
        dtype: DType,
    ) -> Result<Self> {
        Self::new(d_model, 0.0, max_len, device, dtype)
    }

    pub fn learnable(
        d_model: usize,
        max_len: usize,
        device: &Device,
        dtype: DType,
    ) -> Result<LearnablePositionalEncoding> {
        LearnablePositionalEncoding::new(d_model, max_len, device, dtype)
    }
}

impl Module for PositionalEncoding {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let seq_len = input.shape().dims()[1];
        if seq_len > self.max_len {
            return Err(TextError::ModelError(format!(
                "Sequence length {} exceeds maximum length {}",
                seq_len, self.max_len
            )));
        }

        let pos_encoding = self.encoding.narrow(0, 0, seq_len)?.unsqueeze(0)?;

        let output = input + &pos_encoding;

        if self.dropout > 0.0 {
            // Apply dropout (simplified - would need proper dropout implementation)
            Ok(output)
        } else {
            Ok(output)
        }
    }

    fn parameters(&self) -> Vec<&Parameter> {
        vec![] // Sinusoidal encoding has no learnable parameters
    }

    fn train(&mut self, _mode: bool) {
        // No-op for fixed positional encoding
    }

    fn eval(&mut self) {
        // No-op for fixed positional encoding
    }
}

// ============================================================================
// Learnable Positional Encoding
// ============================================================================

#[derive(Debug, Clone)]
pub struct LearnablePositionalEncoding {
    pub weight: Parameter,
    pub max_len: usize,
    pub d_model: usize,
}

impl LearnablePositionalEncoding {
    pub fn new(d_model: usize, max_len: usize, _device: &Device, _dtype: DType) -> Result<Self> {
        let weight = Parameter::new(randn::<f32>(&[max_len, d_model])?, true);

        Ok(Self {
            weight,
            max_len,
            d_model,
        })
    }
}

impl Module for LearnablePositionalEncoding {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let seq_len = input.shape().dims()[1];
        if seq_len > self.max_len {
            return Err(TextError::ModelError(format!(
                "Sequence length {} exceeds maximum length {}",
                seq_len, self.max_len
            )));
        }

        let pos_encoding = self.weight.tensor().narrow(0, 0, seq_len)?.unsqueeze(0)?;

        Ok(input + &pos_encoding)
    }

    fn parameters(&self) -> Vec<&Parameter> {
        vec![&self.weight]
    }

    fn train(&mut self, _mode: bool) {
        // No-op
    }

    fn eval(&mut self) {
        // No-op
    }
}

// ============================================================================
// Token Type Embeddings
// ============================================================================

#[derive(Debug, Clone)]
pub struct TokenTypeEmbedding {
    pub embedding: WordEmbedding,
    pub num_types: usize,
}

impl TokenTypeEmbedding {
    pub fn new(
        num_types: usize,
        embedding_dim: usize,
        device: &Device,
        dtype: DType,
    ) -> Result<Self> {
        let embedding = WordEmbedding::simple(num_types, embedding_dim, device, dtype)?;

        Ok(Self {
            embedding,
            num_types,
        })
    }
}

impl Module for TokenTypeEmbedding {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        self.embedding.forward(input)
    }

    fn parameters(&self) -> Vec<&Parameter> {
        self.embedding.parameters()
    }

    fn train(&mut self, mode: bool) {
        self.embedding.train(mode);
    }

    fn eval(&mut self) {
        self.embedding.eval();
    }
}

// ============================================================================
// Combined Embeddings (Token + Position + Type)
// ============================================================================

#[derive(Debug, Clone)]
pub struct CombinedEmbeddings {
    pub token_embeddings: WordEmbedding,
    pub position_embeddings: Option<LearnablePositionalEncoding>,
    pub token_type_embeddings: Option<TokenTypeEmbedding>,
    pub layer_norm: Option<LayerNorm>,
    pub dropout: f32,
}

impl CombinedEmbeddings {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        vocab_size: usize,
        embedding_dim: usize,
        max_position_embeddings: usize,
        num_token_types: Option<usize>,
        layer_norm_eps: Option<f32>,
        dropout: f32,
        device: &Device,
        dtype: DType,
    ) -> Result<Self> {
        let token_embeddings = WordEmbedding::simple(vocab_size, embedding_dim, device, dtype)?;

        let position_embeddings = Some(LearnablePositionalEncoding::new(
            embedding_dim,
            max_position_embeddings,
            device,
            dtype,
        )?);

        let token_type_embeddings = if let Some(num_types) = num_token_types {
            Some(TokenTypeEmbedding::new(
                num_types,
                embedding_dim,
                device,
                dtype,
            )?)
        } else {
            None
        };

        let layer_norm = if let Some(eps) = layer_norm_eps {
            Some(LayerNorm::new(embedding_dim, eps, device, dtype)?)
        } else {
            None
        };

        Ok(Self {
            token_embeddings,
            position_embeddings,
            token_type_embeddings,
            layer_norm,
            dropout,
        })
    }

    pub fn forward_with_positions_and_types(
        &self,
        input_ids: &Tensor,
        position_ids: Option<&Tensor>,
        token_type_ids: Option<&Tensor>,
    ) -> Result<Tensor> {
        let mut embeddings = self.token_embeddings.forward(input_ids)?;

        // Add position embeddings
        if let Some(pos_emb) = &self.position_embeddings {
            if let Some(pos_ids) = position_ids {
                // Convert position_ids to i64 for indexing
                let float_data: Vec<f32> = pos_ids.to_vec()?;
                let mut int_data = Vec::new();
                for val in float_data.iter() {
                    int_data.push(val.round() as i64);
                }
                let pos_indices = Tensor::from_vec(int_data, pos_ids.shape().dims())?;
                let pos_emb_selected = pos_emb.weight.tensor().index_select(0, &pos_indices)?;
                embeddings = &embeddings + &pos_emb_selected;
            } else {
                // Use default position indices
                let seq_len = input_ids.shape().dims()[1];
                let pos_ids = arange(0i64, seq_len as i64, 1i64)?;
                let pos_emb_selected = pos_emb.weight.tensor().index_select(0, &pos_ids)?;
                embeddings = &embeddings + &pos_emb_selected;
            }
        }

        // Add token type embeddings
        if let Some(type_emb) = &self.token_type_embeddings {
            if let Some(type_ids) = token_type_ids {
                let type_emb_out = type_emb.forward(type_ids)?;
                embeddings = &embeddings + &type_emb_out;
            } else {
                // Use default token type (all zeros)
                let zeros = Tensor::zeros_like(input_ids)?;
                let type_emb_out = type_emb.forward(&zeros)?;
                embeddings = &embeddings + &type_emb_out;
            }
        }

        // Apply layer normalization
        if let Some(ln) = &self.layer_norm {
            embeddings = ln.forward(&embeddings)?;
        }

        // Apply dropout (simplified - would need proper dropout implementation)
        if self.dropout > 0.0 {
            // Would apply dropout here
        }

        Ok(embeddings)
    }
}

impl Module for CombinedEmbeddings {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        self.forward_with_positions_and_types(input, None, None)
    }

    fn parameters(&self) -> Vec<&Parameter> {
        let mut params = self.token_embeddings.parameters();

        if let Some(pos_emb) = &self.position_embeddings {
            params.extend(pos_emb.parameters());
        }

        if let Some(type_emb) = &self.token_type_embeddings {
            params.extend(type_emb.parameters());
        }

        if let Some(ln) = &self.layer_norm {
            params.extend(ln.parameters());
        }

        params
    }

    fn train(&mut self, mode: bool) {
        self.token_embeddings.train(mode);
        if let Some(pos_emb) = &mut self.position_embeddings {
            pos_emb.train(mode);
        }
        if let Some(type_emb) = &mut self.token_type_embeddings {
            type_emb.train(mode);
        }
        if let Some(ln) = &mut self.layer_norm {
            ln.train(mode);
        }
    }

    fn eval(&mut self) {
        self.token_embeddings.eval();
        if let Some(pos_emb) = &mut self.position_embeddings {
            pos_emb.eval();
        }
        if let Some(type_emb) = &mut self.token_type_embeddings {
            type_emb.eval();
        }
        if let Some(ln) = &mut self.layer_norm {
            ln.eval();
        }
    }
}

// ============================================================================
// Embedding Utilities
// ============================================================================

pub struct EmbeddingUtils;

impl EmbeddingUtils {
    /// Load pre-trained word embeddings from a text file (word2vec/GloVe format)
    pub fn load_pretrained_embeddings(
        path: &Path,
        vocab: &HashMap<String, u32>,
        embedding_dim: usize,
        _device: &Device,
        dtype: DType,
    ) -> Result<Tensor> {
        use std::fs::File;
        use std::io::{BufRead, BufReader};

        let file = File::open(path).map_err(TextError::IoError)?;
        let reader = BufReader::new(file);

        let vocab_size = vocab.len();
        let mut embedding_matrix = vec![vec![0.0f32; embedding_dim]; vocab_size];

        for line in reader.lines() {
            let line = line.map_err(TextError::IoError)?;
            let parts: Vec<&str> = line.split_whitespace().collect();

            if parts.len() != embedding_dim + 1 {
                continue;
            }

            let word = parts[0];
            if let Some(&idx) = vocab.get(word) {
                for (i, &value_str) in parts[1..].iter().enumerate() {
                    if let Ok(value) = value_str.parse::<f32>() {
                        embedding_matrix[idx as usize][i] = value;
                    }
                }
            }
        }

        // Convert to tensor
        let flat_embeddings: Vec<f32> = embedding_matrix.into_iter().flatten().collect();
        let tensor =
            Tensor::from_vec(flat_embeddings, &[vocab_size, embedding_dim])?.to_dtype(dtype)?;

        Ok(tensor)
    }

    /// Initialize embeddings with Xavier/Glorot uniform distribution
    pub fn xavier_uniform_embeddings(
        vocab_size: usize,
        embedding_dim: usize,
        _device: &Device,
        _dtype: DType,
    ) -> Result<Tensor> {
        let fan_in = vocab_size as f32;
        let fan_out = embedding_dim as f32;
        let bound = (6.0 / (fan_in + fan_out)).sqrt();

        let uniform = rand(&[vocab_size, embedding_dim])?;
        Ok(uniform.mul_scalar(2.0 * bound)?.add_scalar(-bound)?)
    }

    /// Initialize embeddings with Xavier/Glorot normal distribution
    pub fn xavier_normal_embeddings(
        vocab_size: usize,
        embedding_dim: usize,
        _device: &Device,
        _dtype: DType,
    ) -> Result<Tensor> {
        let fan_in = vocab_size as f32;
        let fan_out = embedding_dim as f32;
        let std = (2.0 / (fan_in + fan_out)).sqrt();

        Ok(randn(&[vocab_size, embedding_dim])?.mul_scalar(std)?)
    }

    /// Compute cosine similarity between embeddings
    pub fn cosine_similarity(embeddings: &Tensor, idx1: usize, idx2: usize) -> Result<f32> {
        let emb1 = embeddings.select(0, idx1 as i64)?;
        let emb2 = embeddings.select(0, idx2 as i64)?;

        let dot_product = emb1.mul_op(&emb2)?.sum()?;
        let norm1 = emb1.mul_op(&emb1)?.sum()?.sqrt()?;
        let norm2 = emb2.mul_op(&emb2)?.sum()?.sqrt()?;

        let similarity = dot_product.div(&norm1.mul_op(&norm2)?)?;
        Ok(similarity.item()?)
    }

    /// Find most similar embeddings to a given embedding
    pub fn most_similar(
        embeddings: &Tensor,
        target_idx: usize,
        top_k: usize,
    ) -> Result<Vec<(usize, f32)>> {
        let _target_emb = embeddings.select(0, target_idx as i64)?;
        let vocab_size = embeddings.shape().dims()[0];

        let mut similarities = Vec::new();

        for i in 0..vocab_size {
            if i == target_idx {
                continue;
            }

            let similarity = Self::cosine_similarity(embeddings, target_idx, i)?;
            similarities.push((i, similarity));
        }

        similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        similarities.truncate(top_k);

        Ok(similarities)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use torsh_core::{device::DeviceType as Device, dtype::DType};

    #[test]
    fn test_word_embedding_creation() {
        let device = Device::Cpu;
        let dtype = DType::F32;
        let vocab_size = 1000;
        let embedding_dim = 300;

        let embedding = WordEmbedding::simple(vocab_size, embedding_dim, &device, dtype);
        assert!(embedding.is_ok());

        let embedding = embedding.unwrap();
        assert_eq!(embedding.vocab_size, vocab_size);
        assert_eq!(embedding.embedding_dim, embedding_dim);
    }

    #[test]
    fn test_positional_encoding_creation() {
        let device = Device::Cpu;
        let dtype = DType::F32;
        let d_model = 512;
        let max_len = 1000;

        let pos_encoding = PositionalEncoding::sinusoidal(d_model, max_len, &device, dtype);
        assert!(pos_encoding.is_ok());

        let pos_encoding = pos_encoding.unwrap();
        assert_eq!(pos_encoding.d_model, d_model);
        assert_eq!(pos_encoding.max_len, max_len);
    }

    #[test]
    fn test_embedding_forward() {
        let device = Device::Cpu;
        let dtype = DType::F32;
        let vocab_size = 100;
        let embedding_dim = 64;

        let embedding = WordEmbedding::simple(vocab_size, embedding_dim, &device, dtype).unwrap();
        let input = Tensor::from_vec(vec![1.0_f32, 5.0, 10.0, 25.0], &[4]).unwrap();

        let result = embedding.forward(&input);
        assert!(result.is_ok());

        let output = result.unwrap();
        assert_eq!(output.shape().dims(), &[4, embedding_dim]);
    }
}