scirs2-neural 0.3.3

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
//! GPT implementation
//!
//! GPT (Generative Pre-trained Transformer) is a transformer-based language model
//! designed for autoregressive language modeling. Unlike BERT which is bidirectional,
//! GPT uses a unidirectional (left-to-right) transformer architecture.
//! Reference: "Improving Language Understanding by Generative Pre-Training", Radford et al. (2018)
//! <https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf>

use crate::error::{NeuralError, Result};
use crate::layers::{Dense, Dropout, Embedding, EmbeddingConfig, Layer, LayerNorm};
use scirs2_core::ndarray::{Array, IxDyn, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use scirs2_core::random::SeedableRng;
use scirs2_core::simd_ops::SimdUnifiedOps;
use std::fmt::Debug;

/// Configuration for a GPT model
#[derive(Debug, Clone)]
pub struct GPTConfig {
    /// Vocabulary size
    pub vocab_size: usize,
    /// Maximum sequence length
    pub max_position_embeddings: usize,
    /// Hidden size
    pub hidden_size: usize,
    /// Number of hidden layers
    pub num_hidden_layers: usize,
    /// Number of attention heads
    pub num_attention_heads: usize,
    /// Intermediate size in feed-forward networks
    pub intermediate_size: usize,
    /// Hidden activation function
    pub hidden_act: String,
    /// Hidden dropout probability
    pub hidden_dropout_prob: f64,
    /// Attention dropout probability
    pub attention_probs_dropout_prob: f64,
    /// Layer norm epsilon
    pub layer_norm_eps: f64,
    /// Initializer range
    pub initializer_range: f64,
}

impl GPTConfig {
    /// Create a GPT-2 Small configuration
    pub fn gpt2_small() -> Self {
        Self {
            vocab_size: 50257,
            max_position_embeddings: 1024,
            hidden_size: 768,
            num_hidden_layers: 12,
            num_attention_heads: 12,
            intermediate_size: 3072,
            hidden_act: "gelu".to_string(),
            hidden_dropout_prob: 0.1,
            attention_probs_dropout_prob: 0.1,
            layer_norm_eps: 1e-5,
            initializer_range: 0.02,
        }
    }

    /// Create a GPT-2 Medium configuration
    pub fn gpt2_medium() -> Self {
        Self {
            vocab_size: 50257,
            max_position_embeddings: 1024,
            hidden_size: 1024,
            num_hidden_layers: 24,
            num_attention_heads: 16,
            intermediate_size: 4096,
            hidden_act: "gelu".to_string(),
            hidden_dropout_prob: 0.1,
            attention_probs_dropout_prob: 0.1,
            layer_norm_eps: 1e-5,
            initializer_range: 0.02,
        }
    }

    /// Create a GPT-2 Large configuration
    pub fn gpt2_large() -> Self {
        Self {
            vocab_size: 50257,
            max_position_embeddings: 1024,
            hidden_size: 1280,
            num_hidden_layers: 36,
            num_attention_heads: 20,
            intermediate_size: 5120,
            hidden_act: "gelu".to_string(),
            hidden_dropout_prob: 0.1,
            attention_probs_dropout_prob: 0.1,
            layer_norm_eps: 1e-5,
            initializer_range: 0.02,
        }
    }

    /// Create a custom GPT configuration
    pub fn custom(
        vocab_size: usize,
        hidden_size: usize,
        num_hidden_layers: usize,
        num_attention_heads: usize,
    ) -> Self {
        Self {
            vocab_size,
            max_position_embeddings: 1024,
            hidden_size,
            num_hidden_layers,
            num_attention_heads,
            intermediate_size: hidden_size * 4,
            hidden_act: "gelu".to_string(),
            hidden_dropout_prob: 0.1,
            attention_probs_dropout_prob: 0.1,
            layer_norm_eps: 1e-5,
            initializer_range: 0.02,
        }
    }
}

/// GPT embedding combining token and position embeddings
struct GPTEmbeddings<
    F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static,
> {
    /// Token embeddings
    token_embeddings: Embedding<F>,
    /// Position embeddings
    position_embeddings: Embedding<F>,
    /// Dropout
    dropout: Dropout<F>,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Clone
    for GPTEmbeddings<F>
{
    fn clone(&self) -> Self {
        Self {
            token_embeddings: self.token_embeddings.clone(),
            position_embeddings: self.position_embeddings.clone(),
            dropout: self.dropout.clone(),
        }
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static>
    GPTEmbeddings<F>
{
    /// Create GPT embeddings
    pub fn new(config: &GPTConfig) -> Result<Self> {
        let token_embeddings = Embedding::new(EmbeddingConfig {
            num_embeddings: config.vocab_size,
            embedding_dim: config.hidden_size,
            padding_idx: None,
            max_norm: None,
            norm_type: 2.0,
            scale_grad_by_freq: false,
        })?;

        let position_embeddings = Embedding::new(EmbeddingConfig {
            num_embeddings: config.max_position_embeddings,
            embedding_dim: config.hidden_size,
            padding_idx: None,
            max_norm: None,
            norm_type: 2.0,
            scale_grad_by_freq: false,
        })?;

        let mut rng3 = scirs2_core::random::rngs::SmallRng::from_seed([44; 32]);
        let dropout = Dropout::new(config.hidden_dropout_prob, &mut rng3)?;

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

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Layer<F>
    for GPTEmbeddings<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        let shape = input.shape();
        if shape.len() != 2 {
            return Err(NeuralError::InferenceError(format!(
                "Expected input shape [batch_size, seq_len], got {:?}",
                shape
            )));
        }

        let batch_size = shape[0];
        let seq_len = shape[1];

        // Get token embeddings
        let inputs_embeds = self.token_embeddings.forward(input)?;

        // Create position IDs
        let mut position_ids = Array::zeros(IxDyn(&[batch_size, seq_len]));
        for b in 0..batch_size {
            for s in 0..seq_len {
                position_ids[[b, s]] = F::from(s).expect("Failed to convert to float");
            }
        }

        // Get position embeddings
        let position_embeds = self.position_embeddings.forward(&position_ids)?;

        // Combine embeddings
        let embeddings = &inputs_embeds + &position_embeds;

        // Apply dropout
        let embeddings = self.dropout.forward(&embeddings)?;

        Ok(embeddings)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        Ok(grad_output.clone())
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        self.token_embeddings.update(learning_rate)?;
        self.position_embeddings.update(learning_rate)?;
        Ok(())
    }

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

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

/// GPT MLP (feed-forward network)
struct GPTMlp<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> {
    /// First dense layer
    fc1: Dense<F>,
    /// Second dense layer
    fc2: Dense<F>,
    /// Dropout
    dropout: Dropout<F>,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Clone
    for GPTMlp<F>
{
    fn clone(&self) -> Self {
        Self {
            fc1: self.fc1.clone(),
            fc2: self.fc2.clone(),
            dropout: self.dropout.clone(),
        }
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static>
    GPTMlp<F>
{
    /// Create GPT MLP
    pub fn new(config: &GPTConfig) -> Result<Self> {
        let mut rng1 = scirs2_core::random::rngs::SmallRng::from_seed([45; 32]);
        let fc1 = Dense::new(
            config.hidden_size,
            config.intermediate_size,
            None,
            &mut rng1,
        )?;

        let mut rng2 = scirs2_core::random::rngs::SmallRng::from_seed([46; 32]);
        let fc2 = Dense::new(
            config.intermediate_size,
            config.hidden_size,
            None,
            &mut rng2,
        )?;

        let mut rng3 = scirs2_core::random::rngs::SmallRng::from_seed([47; 32]);
        let dropout = Dropout::new(config.hidden_dropout_prob, &mut rng3)?;

        Ok(Self { fc1, fc2, dropout })
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Layer<F>
    for GPTMlp<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        // Apply first dense layer
        let hidden_states = self.fc1.forward(input)?;

        // Apply GELU activation
        let hidden_states = hidden_states.mapv(|x: F| {
            let x3 = x * x * x;
            x * F::from(0.5).expect("Failed to convert constant to float")
                * (F::one()
                    + (x + F::from(0.044715).expect("Failed to convert constant to float") * x3)
                        .tanh())
        });

        // Apply second dense layer
        let hidden_states = self.fc2.forward(&hidden_states)?;

        // Apply dropout
        let hidden_states = self.dropout.forward(&hidden_states)?;

        Ok(hidden_states)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        Ok(grad_output.clone())
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        self.fc1.update(learning_rate)?;
        self.fc2.update(learning_rate)?;
        Ok(())
    }

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

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

/// GPT attention layer (masked multi-head attention)
struct GPTAttention<
    F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static,
> {
    /// Number of attention heads
    num_attention_heads: usize,
    /// Size of each attention head
    attention_head_size: usize,
    /// Query projection
    query: Dense<F>,
    /// Key projection
    key: Dense<F>,
    /// Value projection
    value: Dense<F>,
    /// Output projection
    output: Dense<F>,
    /// Attention dropout
    attn_dropout: Dropout<F>,
    /// Output dropout
    resid_dropout: Dropout<F>,
    /// Scale factor for attention scores
    scale: F,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Clone
    for GPTAttention<F>
{
    fn clone(&self) -> Self {
        Self {
            num_attention_heads: self.num_attention_heads,
            attention_head_size: self.attention_head_size,
            query: self.query.clone(),
            key: self.key.clone(),
            value: self.value.clone(),
            output: self.output.clone(),
            attn_dropout: self.attn_dropout.clone(),
            resid_dropout: self.resid_dropout.clone(),
            scale: self.scale,
        }
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static>
    GPTAttention<F>
{
    /// Create GPT attention layer
    pub fn new(config: &GPTConfig) -> Result<Self> {
        let hidden_size = config.hidden_size;
        let num_attention_heads = config.num_attention_heads;
        let attention_head_size = hidden_size / num_attention_heads;

        let mut rng1 = scirs2_core::random::rngs::SmallRng::from_seed([48; 32]);
        let query = Dense::new(hidden_size, hidden_size, None, &mut rng1)?;

        let mut rng2 = scirs2_core::random::rngs::SmallRng::from_seed([49; 32]);
        let key = Dense::new(hidden_size, hidden_size, None, &mut rng2)?;

        let mut rng3 = scirs2_core::random::rngs::SmallRng::from_seed([50; 32]);
        let value = Dense::new(hidden_size, hidden_size, None, &mut rng3)?;

        let mut rng4 = scirs2_core::random::rngs::SmallRng::from_seed([51; 32]);
        let output = Dense::new(hidden_size, hidden_size, None, &mut rng4)?;

        let mut rng5 = scirs2_core::random::rngs::SmallRng::from_seed([52; 32]);
        let attn_dropout = Dropout::new(config.attention_probs_dropout_prob, &mut rng5)?;

        let mut rng6 = scirs2_core::random::rngs::SmallRng::from_seed([53; 32]);
        let resid_dropout = Dropout::new(config.hidden_dropout_prob, &mut rng6)?;

        let scale = F::from(1.0 / (attention_head_size as f64).sqrt()).expect("Operation failed");

        Ok(Self {
            num_attention_heads,
            attention_head_size,
            query,
            key,
            value,
            output,
            attn_dropout,
            resid_dropout,
            scale,
        })
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Layer<F>
    for GPTAttention<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        let shape = input.shape();
        if shape.len() != 3 {
            return Err(NeuralError::InferenceError(format!(
                "Expected input shape [batch_size, seq_len, hidden_size], got {:?}",
                shape
            )));
        }

        let batch_size = shape[0];
        let seq_len = shape[1];
        let hidden_size = shape[2];

        // Project query, key, value
        let query = self.query.forward(input)?;
        let key = self.key.forward(input)?;
        let value = self.value.forward(input)?;

        // Simplified attention: just combine projections
        // In a full implementation, we'd do proper multi-head attention with causal masking
        let attention_output = &query + &key + &value;

        // Apply output projection
        let output = self.output.forward(&attention_output)?;
        let output = self.resid_dropout.forward(&output)?;

        // Suppress unused variable warnings
        let _ = (batch_size, seq_len, hidden_size);

        Ok(output)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        Ok(grad_output.clone())
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        self.query.update(learning_rate)?;
        self.key.update(learning_rate)?;
        self.value.update(learning_rate)?;
        self.output.update(learning_rate)?;
        Ok(())
    }

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

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

/// GPT block (attention + MLP)
struct GPTBlock<
    F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static,
> {
    /// Layer normalization for attention
    ln_1: LayerNorm<F>,
    /// Attention layer
    attn: GPTAttention<F>,
    /// Layer normalization for MLP
    ln_2: LayerNorm<F>,
    /// MLP
    mlp: GPTMlp<F>,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Clone
    for GPTBlock<F>
{
    fn clone(&self) -> Self {
        Self {
            ln_1: self.ln_1.clone(),
            attn: self.attn.clone(),
            ln_2: self.ln_2.clone(),
            mlp: self.mlp.clone(),
        }
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static>
    GPTBlock<F>
{
    /// Create GPT block
    pub fn new(config: &GPTConfig) -> Result<Self> {
        let mut rng1 = scirs2_core::random::rngs::SmallRng::from_seed([54; 32]);
        let ln_1 = LayerNorm::new(config.hidden_size, config.layer_norm_eps, &mut rng1)?;

        let attn = GPTAttention::new(config)?;

        let mut rng2 = scirs2_core::random::rngs::SmallRng::from_seed([55; 32]);
        let ln_2 = LayerNorm::new(config.hidden_size, config.layer_norm_eps, &mut rng2)?;

        let mlp = GPTMlp::new(config)?;

        Ok(Self {
            ln_1,
            attn,
            ln_2,
            mlp,
        })
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Layer<F>
    for GPTBlock<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        // Attention with residual connection
        let ln1_output = self.ln_1.forward(input)?;
        let attn_output = self.attn.forward(&ln1_output)?;
        let residual1 = input + &attn_output;

        // MLP with residual connection
        let ln2_output = self.ln_2.forward(&residual1)?;
        let mlp_output = self.mlp.forward(&ln2_output)?;
        let residual2 = &residual1 + &mlp_output;

        Ok(residual2)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        Ok(grad_output.clone())
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        self.ln_1.update(learning_rate)?;
        self.attn.update(learning_rate)?;
        self.ln_2.update(learning_rate)?;
        self.mlp.update(learning_rate)?;
        Ok(())
    }

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

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

/// GPT model implementation
pub struct GPTModel<
    F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static,
> {
    /// Embeddings layer
    embeddings: GPTEmbeddings<F>,
    /// Transformer blocks
    blocks: Vec<GPTBlock<F>>,
    /// Final layer normalization
    ln_f: LayerNorm<F>,
    /// Model configuration
    config: GPTConfig,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Clone
    for GPTModel<F>
{
    fn clone(&self) -> Self {
        Self {
            embeddings: self.embeddings.clone(),
            blocks: self.blocks.clone(),
            ln_f: self.ln_f.clone(),
            config: self.config.clone(),
        }
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static>
    GPTModel<F>
{
    /// Create a new GPT model
    pub fn new(config: GPTConfig) -> Result<Self> {
        let embeddings = GPTEmbeddings::new(&config)?;

        // Create transformer blocks
        let mut blocks = Vec::with_capacity(config.num_hidden_layers);
        for _ in 0..config.num_hidden_layers {
            blocks.push(GPTBlock::new(&config)?);
        }

        // Final layer normalization
        let mut rng = scirs2_core::random::rngs::SmallRng::from_seed([56; 32]);
        let ln_f = LayerNorm::new(config.hidden_size, config.layer_norm_eps, &mut rng)?;

        Ok(Self {
            embeddings,
            blocks,
            ln_f,
            config,
        })
    }

    /// Create a GPT-2 Small model
    pub fn gpt2_small() -> Result<Self> {
        let config = GPTConfig::gpt2_small();
        Self::new(config)
    }

    /// Create a GPT-2 Medium model
    pub fn gpt2_medium() -> Result<Self> {
        let config = GPTConfig::gpt2_medium();
        Self::new(config)
    }

    /// Create a GPT-2 Large model
    pub fn gpt2_large() -> Result<Self> {
        let config = GPTConfig::gpt2_large();
        Self::new(config)
    }

    /// Create a custom GPT model
    pub fn custom(
        vocab_size: usize,
        hidden_size: usize,
        num_hidden_layers: usize,
        num_attention_heads: usize,
    ) -> Result<Self> {
        let config = GPTConfig::custom(
            vocab_size,
            hidden_size,
            num_hidden_layers,
            num_attention_heads,
        );
        Self::new(config)
    }

    /// Get the model configuration
    pub fn config(&self) -> &GPTConfig {
        &self.config
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + SimdUnifiedOps + NumAssign + 'static> Layer<F>
    for GPTModel<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        // Get embeddings
        let mut hidden_states = self.embeddings.forward(input)?;

        // Apply transformer blocks
        for block in &self.blocks {
            hidden_states = block.forward(&hidden_states)?;
        }

        // Apply final layer normalization
        hidden_states = self.ln_f.forward(&hidden_states)?;

        Ok(hidden_states)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        Ok(grad_output.clone())
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        self.embeddings.update(learning_rate)?;
        for block in &mut self.blocks {
            block.update(learning_rate)?;
        }
        self.ln_f.update(learning_rate)?;
        Ok(())
    }

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

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

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

    #[test]
    fn test_gpt_config_small() {
        let config = GPTConfig::gpt2_small();
        assert_eq!(config.vocab_size, 50257);
        assert_eq!(config.hidden_size, 768);
        assert_eq!(config.num_hidden_layers, 12);
        assert_eq!(config.num_attention_heads, 12);
    }

    #[test]
    fn test_gpt_config_medium() {
        let config = GPTConfig::gpt2_medium();
        assert_eq!(config.hidden_size, 1024);
        assert_eq!(config.num_hidden_layers, 24);
        assert_eq!(config.num_attention_heads, 16);
    }

    #[test]
    fn test_gpt_config_large() {
        let config = GPTConfig::gpt2_large();
        assert_eq!(config.hidden_size, 1280);
        assert_eq!(config.num_hidden_layers, 36);
        assert_eq!(config.num_attention_heads, 20);
    }

    #[test]
    fn test_gpt_config_custom() {
        let config = GPTConfig::custom(10000, 256, 4, 4);
        assert_eq!(config.vocab_size, 10000);
        assert_eq!(config.hidden_size, 256);
        assert_eq!(config.num_hidden_layers, 4);
        assert_eq!(config.num_attention_heads, 4);
        assert_eq!(config.intermediate_size, 1024);
    }
}