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
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! T5 (Text-to-Text Transfer Transformer) implementation
//!
//! This module implements the T5 encoder-decoder architecture with relative position encoding,
//! layer normalization variants, and support for text-to-text generation tasks.

use super::transformer::{FeedForward, MultiHeadAttention};
use crate::{TextModel, TextModelConfig};
use std::collections::HashMap;
use torsh_core::{device::DeviceType, Result};
use torsh_nn::{prelude::*, Module, Parameter};
use torsh_tensor::creation::*;
use torsh_tensor::Tensor;

/// T5 layer normalization (RMS norm variant)
pub struct T5LayerNorm {
    weight: Parameter,
    variance_epsilon: f32,
    hidden_dim: usize,
    is_training: bool,
}

impl T5LayerNorm {
    pub fn new(hidden_dim: usize, eps: f32) -> Self {
        Self {
            weight: Parameter::new(ones(&[hidden_dim])),
            variance_epsilon: eps,
            hidden_dim,
            is_training: true,
        }
    }

    pub fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // T5 uses RMS norm: x / sqrt(mean(x^2) + eps) * weight
        let input_dtype = input.clone();
        let input_squared = input.mul(input)?;

        // Calculate mean across the last dimension
        let mean = input_squared.mean_keepdim(&[-1])?;
        let variance = mean.add_scalar(self.variance_epsilon)?;
        let inv_std = variance.rsqrt()?;

        let normalized = input.mul(&inv_std)?;
        normalized.mul(&self.weight.data())
    }
}

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

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();
        params.insert("weight".to_string(), self.weight.clone());
        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
    }

    fn eval(&mut self) {
        self.is_training = false;
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        // Move weight parameter to device
        self.weight = Parameter::new(self.weight.data().to_device(device)?);
        Ok(())
    }
}

/// T5 relative position bias for attention
pub struct T5RelativePositionBias {
    relative_attention_bias: Embedding,
    num_buckets: usize,
    max_distance: usize,
    num_heads: usize,
    is_training: bool,
}

impl T5RelativePositionBias {
    pub fn new(num_heads: usize, num_buckets: usize, max_distance: usize) -> Self {
        Self {
            relative_attention_bias: Embedding::new(num_buckets, num_heads),
            num_buckets,
            max_distance,
            num_heads,
            is_training: true,
        }
    }

    fn _relative_position_bucket(&self, relative_position: i32, bidirectional: bool) -> usize {
        let mut relative_buckets = 0;
        let mut n = relative_position;

        if bidirectional {
            self.num_buckets /= 2;
            if n > 0 {
                relative_buckets += self.num_buckets;
            } else {
                n = -n;
            }
        } else {
            n = (-n).max(0);
        }

        let max_exact = self.num_buckets / 2;
        let is_small = n < max_exact as i32;

        if is_small {
            n as usize + relative_buckets
        } else {
            let val = max_exact as f32
                + ((n as f32).ln() / (self.max_distance as f32 / max_exact as f32).ln()
                    * (self.num_buckets / 2 - max_exact) as f32);
            (val as usize).min(self.num_buckets - 1) + relative_buckets
        }
    }

    pub fn compute_bias(
        &self,
        query_length: usize,
        key_length: usize,
        bidirectional: bool,
    ) -> Result<Tensor> {
        // Create position bias matrix - simplified implementation
        let bias_shape = [self.num_heads, query_length, key_length];
        let bias: Tensor<f32> = zeros(&bias_shape);
        Ok(bias)
    }
}

impl Module for T5RelativePositionBias {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // Default forward pass - bias computation is done separately
        Ok(input.clone())
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();
        for (name, param) in self.relative_attention_bias.parameters() {
            params.insert(format!("relative_attention_bias.{}", name), param);
        }
        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        self.relative_attention_bias.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        self.relative_attention_bias.eval();
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.relative_attention_bias.to_device(device)
    }
}

/// T5 Multi-head attention with relative position bias
pub struct T5Attention {
    base_attention: MultiHeadAttention,
    relative_position_bias: Option<T5RelativePositionBias>,
    has_relative_attention_bias: bool,
    is_training: bool,
}

impl T5Attention {
    pub fn new(
        hidden_dim: usize,
        num_heads: usize,
        dropout: f32,
        has_relative_attention_bias: bool,
        device: DeviceType,
    ) -> Result<Self> {
        let relative_position_bias = if has_relative_attention_bias {
            Some(T5RelativePositionBias::new(num_heads, 32, 128)) // T5 defaults
        } else {
            None
        };

        Ok(Self {
            base_attention: MultiHeadAttention::new(hidden_dim, num_heads, dropout, device)?,
            relative_position_bias,
            has_relative_attention_bias,
            is_training: true,
        })
    }

    pub fn forward_with_bias(
        &self,
        query: &Tensor,
        key: &Tensor,
        value: &Tensor,
        attention_mask: Option<&Tensor>,
        position_bias: Option<&Tensor>,
    ) -> Result<Tensor> {
        // Compute relative position bias if available
        let computed_bias = if let Some(key_value) = key.shape().dims().get(1) {
            if let Some(query_len) = query.shape().dims().get(1) {
                if self.has_relative_attention_bias {
                    if let Some(ref pos_bias) = self.relative_position_bias {
                        Some(pos_bias.compute_bias(*query_len, *key_value, true)?)
                    } else {
                        None
                    }
                } else {
                    position_bias.cloned()
                }
            } else {
                position_bias.cloned()
            }
        } else {
            position_bias.cloned()
        };

        // Combine attention mask and position bias
        let combined_mask = match (attention_mask, computed_bias.as_ref()) {
            (Some(mask), Some(bias)) => Some(mask.add(bias)?),
            (Some(mask), None) => Some(mask.clone()),
            (None, Some(bias)) => Some(bias.clone()),
            (None, None) => None,
        };

        self.base_attention
            .forward_with_mask(query, combined_mask.as_ref())
    }
}

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

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();

        for (name, param) in self.base_attention.parameters() {
            params.insert(format!("base_attention.{}", name), param);
        }

        if let Some(ref pos_bias) = self.relative_position_bias {
            for (name, param) in pos_bias.parameters() {
                params.insert(format!("relative_position_bias.{}", name), param);
            }
        }

        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        self.base_attention.train();
        if let Some(ref mut pos_bias) = self.relative_position_bias {
            pos_bias.train();
        }
    }

    fn eval(&mut self) {
        self.is_training = false;
        self.base_attention.eval();
        if let Some(ref mut pos_bias) = self.relative_position_bias {
            pos_bias.eval();
        }
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.base_attention.to_device(device)?;
        if let Some(ref mut pos_bias) = self.relative_position_bias {
            pos_bias.to_device(device)?;
        }
        Ok(())
    }
}

/// T5 encoder layer
pub struct T5EncoderLayer {
    self_attention: T5Attention,
    feed_forward: FeedForward,
    layer_norm: T5LayerNorm,
    dropout: Dropout,
    is_training: bool,
}

impl T5EncoderLayer {
    pub fn new(config: &TextModelConfig, device: DeviceType) -> Result<Self> {
        Ok(Self {
            self_attention: T5Attention::new(
                config.hidden_dim,
                config.num_heads,
                config.attention_dropout,
                true, // First layer has relative attention bias
                device,
            )?,
            feed_forward: FeedForward::new(
                config.hidden_dim,
                config.intermediate_dim,
                config.dropout,
                device,
            )?,
            layer_norm: T5LayerNorm::new(config.hidden_dim, config.layer_norm_eps),
            dropout: Dropout::new(config.dropout),
            is_training: true,
        })
    }

    pub fn forward_with_mask(
        &self,
        input: &Tensor,
        attention_mask: Option<&Tensor>,
    ) -> Result<Tensor> {
        // T5 uses different normalization order compared to standard transformer
        let normed_input = self.layer_norm.forward(input)?;
        let attention_output = self.self_attention.forward_with_bias(
            &normed_input,
            &normed_input,
            &normed_input,
            attention_mask,
            None,
        )?;
        let attention_output = self.dropout.forward(&attention_output)?;
        let hidden_states = input.add(&attention_output)?;

        // Feed forward
        let normed_hidden = self.layer_norm.forward(&hidden_states)?;
        let feed_forward_output = self.feed_forward.forward(&normed_hidden)?;
        let feed_forward_output = self.dropout.forward(&feed_forward_output)?;
        hidden_states.add(&feed_forward_output)
    }
}

impl Module for T5EncoderLayer {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // T5 uses different normalization order compared to standard transformer
        let normed_input = self.layer_norm.forward(input)?;
        let attention_output = self.self_attention.forward(&normed_input)?;
        let attention_output = self.dropout.forward(&attention_output)?;
        let hidden_states = input.add(&attention_output)?;

        // Feed forward
        let normed_hidden = self.layer_norm.forward(&hidden_states)?;
        let feed_forward_output = self.feed_forward.forward(&normed_hidden)?;
        let feed_forward_output = self.dropout.forward(&feed_forward_output)?;
        hidden_states.add(&feed_forward_output)
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();

        for (name, param) in self.self_attention.parameters() {
            params.insert(format!("self_attention.{}", name), param);
        }
        for (name, param) in self.feed_forward.parameters() {
            params.insert(format!("feed_forward.{}", name), param);
        }
        for (name, param) in self.layer_norm.parameters() {
            params.insert(format!("layer_norm.{}", name), param);
        }

        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        self.self_attention.train();
        self.feed_forward.train();
        self.layer_norm.train();
        self.dropout.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        self.self_attention.eval();
        self.feed_forward.eval();
        self.layer_norm.eval();
        self.dropout.eval();
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.self_attention.to_device(device)?;
        self.feed_forward.to_device(device)?;
        self.layer_norm.to_device(device)?;
        Ok(())
    }
}

/// T5 decoder layer with cross-attention
pub struct T5DecoderLayer {
    self_attention: T5Attention,
    cross_attention: T5Attention,
    feed_forward: FeedForward,
    layer_norm_self_attn: T5LayerNorm,
    layer_norm_cross_attn: T5LayerNorm,
    layer_norm_ff: T5LayerNorm,
    dropout: Dropout,
    is_training: bool,
}

impl T5DecoderLayer {
    pub fn new(config: &TextModelConfig, device: DeviceType) -> Result<Self> {
        Ok(Self {
            self_attention: T5Attention::new(
                config.hidden_dim,
                config.num_heads,
                config.attention_dropout,
                true, // Self attention has relative bias
                device,
            )?,
            cross_attention: T5Attention::new(
                config.hidden_dim,
                config.num_heads,
                config.attention_dropout,
                false, // Cross attention doesn't use relative bias
                device,
            )?,
            feed_forward: FeedForward::new(
                config.hidden_dim,
                config.intermediate_dim,
                config.dropout,
                device,
            )?,
            layer_norm_self_attn: T5LayerNorm::new(config.hidden_dim, config.layer_norm_eps),
            layer_norm_cross_attn: T5LayerNorm::new(config.hidden_dim, config.layer_norm_eps),
            layer_norm_ff: T5LayerNorm::new(config.hidden_dim, config.layer_norm_eps),
            dropout: Dropout::new(config.dropout),
            is_training: true,
        })
    }

    pub fn forward_with_encoder_hidden(
        &self,
        hidden_states: &Tensor,
        encoder_hidden_states: Option<&Tensor>,
        self_attention_mask: Option<&Tensor>,
        cross_attention_mask: Option<&Tensor>,
    ) -> Result<Tensor> {
        // Self attention
        let normed_hidden = self.layer_norm_self_attn.forward(hidden_states)?;
        let self_attention_output = self.self_attention.forward_with_bias(
            &normed_hidden,
            &normed_hidden,
            &normed_hidden,
            self_attention_mask,
            None,
        )?;
        let self_attention_output = self.dropout.forward(&self_attention_output)?;
        let hidden_states = hidden_states.add(&self_attention_output)?;

        // Cross attention (if encoder hidden states provided)
        let hidden_states = if let Some(encoder_hidden) = encoder_hidden_states {
            let normed_hidden = self.layer_norm_cross_attn.forward(&hidden_states)?;
            let cross_attention_output = self.cross_attention.forward_with_bias(
                &normed_hidden,
                encoder_hidden,
                encoder_hidden,
                cross_attention_mask,
                None,
            )?;
            let cross_attention_output = self.dropout.forward(&cross_attention_output)?;
            hidden_states.add(&cross_attention_output)?
        } else {
            hidden_states
        };

        // Feed forward
        let normed_hidden = self.layer_norm_ff.forward(&hidden_states)?;
        let feed_forward_output = self.feed_forward.forward(&normed_hidden)?;
        let feed_forward_output = self.dropout.forward(&feed_forward_output)?;
        hidden_states.add(&feed_forward_output)
    }
}

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

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();

        for (name, param) in self.self_attention.parameters() {
            params.insert(format!("self_attention.{}", name), param);
        }
        for (name, param) in self.cross_attention.parameters() {
            params.insert(format!("cross_attention.{}", name), param);
        }
        for (name, param) in self.feed_forward.parameters() {
            params.insert(format!("feed_forward.{}", name), param);
        }
        for (name, param) in self.layer_norm_self_attn.parameters() {
            params.insert(format!("layer_norm_self_attn.{}", name), param);
        }
        for (name, param) in self.layer_norm_cross_attn.parameters() {
            params.insert(format!("layer_norm_cross_attn.{}", name), param);
        }
        for (name, param) in self.layer_norm_ff.parameters() {
            params.insert(format!("layer_norm_ff.{}", name), param);
        }

        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        self.self_attention.train();
        self.cross_attention.train();
        self.feed_forward.train();
        self.layer_norm_self_attn.train();
        self.layer_norm_cross_attn.train();
        self.layer_norm_ff.train();
        self.dropout.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        self.self_attention.eval();
        self.cross_attention.eval();
        self.feed_forward.eval();
        self.layer_norm_self_attn.eval();
        self.layer_norm_cross_attn.eval();
        self.layer_norm_ff.eval();
        self.dropout.eval();
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.self_attention.to_device(device)?;
        self.cross_attention.to_device(device)?;
        self.feed_forward.to_device(device)?;
        self.layer_norm_self_attn.to_device(device)?;
        self.layer_norm_cross_attn.to_device(device)?;
        self.layer_norm_ff.to_device(device)?;
        Ok(())
    }
}

/// T5 encoder stack
pub struct T5Encoder {
    layers: Vec<T5EncoderLayer>,
    final_layer_norm: T5LayerNorm,
    dropout: Dropout,
    is_training: bool,
}

impl T5Encoder {
    pub fn new(config: &TextModelConfig, device: DeviceType) -> Result<Self> {
        let mut layers = Vec::new();
        for _ in 0..config.num_layers {
            layers.push(T5EncoderLayer::new(config, device)?);
        }

        Ok(Self {
            layers,
            final_layer_norm: T5LayerNorm::new(config.hidden_dim, config.layer_norm_eps),
            dropout: Dropout::new(config.dropout),
            is_training: true,
        })
    }

    pub fn forward_with_mask(
        &self,
        input: &Tensor,
        attention_mask: Option<&Tensor>,
    ) -> Result<Tensor> {
        let mut hidden_states = input.clone();

        for layer in &self.layers {
            hidden_states = layer.forward_with_mask(&hidden_states, attention_mask)?;
        }

        // Final layer norm
        hidden_states = self.final_layer_norm.forward(&hidden_states)?;
        self.dropout.forward(&hidden_states)
    }
}

impl Module for T5Encoder {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let mut hidden_states = input.clone();

        for layer in &self.layers {
            hidden_states = layer.forward(&hidden_states)?;
        }

        // Final layer norm
        hidden_states = self.final_layer_norm.forward(&hidden_states)?;
        self.dropout.forward(&hidden_states)
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();

        for (i, layer) in self.layers.iter().enumerate() {
            for (name, param) in layer.parameters() {
                params.insert(format!("layer_{}.{}", i, name), param);
            }
        }

        for (name, param) in self.final_layer_norm.parameters() {
            params.insert(format!("final_layer_norm.{}", name), param);
        }

        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        for layer in &mut self.layers {
            layer.train();
        }
        self.final_layer_norm.train();
        self.dropout.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        for layer in &mut self.layers {
            layer.eval();
        }
        self.final_layer_norm.eval();
        self.dropout.eval();
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        for layer in &mut self.layers {
            layer.to_device(device)?;
        }
        self.final_layer_norm.to_device(device)?;
        Ok(())
    }
}

/// T5 decoder stack
pub struct T5Decoder {
    layers: Vec<T5DecoderLayer>,
    final_layer_norm: T5LayerNorm,
    dropout: Dropout,
    is_training: bool,
}

impl T5Decoder {
    pub fn new(config: &TextModelConfig, device: DeviceType) -> Result<Self> {
        let mut layers = Vec::new();
        for _ in 0..config.num_layers {
            layers.push(T5DecoderLayer::new(config, device)?);
        }

        Ok(Self {
            layers,
            final_layer_norm: T5LayerNorm::new(config.hidden_dim, config.layer_norm_eps),
            dropout: Dropout::new(config.dropout),
            is_training: true,
        })
    }

    pub fn forward_with_encoder_hidden(
        &self,
        input: &Tensor,
        encoder_hidden_states: Option<&Tensor>,
        self_attention_mask: Option<&Tensor>,
        cross_attention_mask: Option<&Tensor>,
    ) -> Result<Tensor> {
        let mut hidden_states = input.clone();

        for layer in &self.layers {
            hidden_states = layer.forward_with_encoder_hidden(
                &hidden_states,
                encoder_hidden_states,
                self_attention_mask,
                cross_attention_mask,
            )?;
        }

        // Final layer norm
        hidden_states = self.final_layer_norm.forward(&hidden_states)?;
        self.dropout.forward(&hidden_states)
    }
}

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

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();

        for (i, layer) in self.layers.iter().enumerate() {
            for (name, param) in layer.parameters() {
                params.insert(format!("layer_{}.{}", i, name), param);
            }
        }

        for (name, param) in self.final_layer_norm.parameters() {
            params.insert(format!("final_layer_norm.{}", name), param);
        }

        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        for layer in &mut self.layers {
            layer.train();
        }
        self.final_layer_norm.train();
        self.dropout.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        for layer in &mut self.layers {
            layer.eval();
        }
        self.final_layer_norm.eval();
        self.dropout.eval();
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        for layer in &mut self.layers {
            layer.to_device(device)?;
        }
        self.final_layer_norm.to_device(device)?;
        Ok(())
    }
}

/// T5 main model (encoder-decoder)
pub struct T5Model {
    shared_embeddings: Embedding,
    encoder: T5Encoder,
    decoder: T5Decoder,
    config: TextModelConfig,
    is_training: bool,
}

impl T5Model {
    pub fn new(config: TextModelConfig, device: DeviceType) -> Result<Self> {
        Ok(Self {
            shared_embeddings: Embedding::new(config.vocab_size, config.hidden_dim),
            encoder: T5Encoder::new(&config, device)?,
            decoder: T5Decoder::new(&config, device)?,
            config,
            is_training: true,
        })
    }

    pub fn encode(&self, input_ids: &Tensor, attention_mask: Option<&Tensor>) -> Result<Tensor> {
        let input_embeddings = self.shared_embeddings.forward(input_ids)?;

        // Convert attention mask to attention bias if provided
        let attention_mask = if let Some(mask) = attention_mask {
            // Convert binary mask (1 for attend, 0 for not attend) to attention bias
            // (0 for attend, -inf for not attend)
            let inverted_mask = mask.sub_scalar(1.0)?.mul_scalar(-1.0)?;
            let attention_bias = inverted_mask.mul_scalar(f32::NEG_INFINITY)?;
            Some(attention_bias)
        } else {
            None
        };

        self.encoder
            .forward_with_mask(&input_embeddings, attention_mask.as_ref())
    }

    pub fn decode(
        &self,
        decoder_input_ids: &Tensor,
        encoder_hidden_states: &Tensor,
        decoder_attention_mask: Option<&Tensor>,
        encoder_attention_mask: Option<&Tensor>,
    ) -> Result<Tensor> {
        let decoder_embeddings = self.shared_embeddings.forward(decoder_input_ids)?;
        self.decoder.forward_with_encoder_hidden(
            &decoder_embeddings,
            Some(encoder_hidden_states),
            decoder_attention_mask,
            encoder_attention_mask,
        )
    }

    pub fn forward_encoder_decoder(
        &self,
        input_ids: &Tensor,
        decoder_input_ids: &Tensor,
        attention_mask: Option<&Tensor>,
        decoder_attention_mask: Option<&Tensor>,
    ) -> Result<(Tensor, Tensor)> {
        let encoder_outputs = self.encode(input_ids, attention_mask)?;
        let decoder_outputs = self.decode(
            decoder_input_ids,
            &encoder_outputs,
            decoder_attention_mask,
            attention_mask,
        )?;
        Ok((encoder_outputs, decoder_outputs))
    }
}

impl Module for T5Model {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // For basic forward pass, just use encoder
        self.encode(input, None)
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();

        for (name, param) in self.shared_embeddings.parameters() {
            params.insert(format!("shared.{}", name), param);
        }
        for (name, param) in self.encoder.parameters() {
            params.insert(format!("encoder.{}", name), param);
        }
        for (name, param) in self.decoder.parameters() {
            params.insert(format!("decoder.{}", name), param);
        }

        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        self.shared_embeddings.train();
        self.encoder.train();
        self.decoder.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        self.shared_embeddings.eval();
        self.encoder.eval();
        self.decoder.eval();
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.shared_embeddings.to_device(device)?;
        self.encoder.to_device(device)?;
        self.decoder.to_device(device)?;
        Ok(())
    }
}

impl TextModel for T5Model {
    fn name(&self) -> &str {
        "T5"
    }

    fn vocab_size(&self) -> usize {
        self.config.vocab_size
    }

    fn hidden_dim(&self) -> usize {
        self.config.hidden_dim
    }

    fn max_seq_length(&self) -> usize {
        self.config.max_position_embeddings
    }
}

/// T5 for conditional generation (with language modeling head)
pub struct T5ForConditionalGeneration {
    transformer: T5Model,
    lm_head: Linear,
    is_training: bool,
}

impl T5ForConditionalGeneration {
    pub fn new(config: TextModelConfig, device: DeviceType) -> Result<Self> {
        Ok(Self {
            transformer: T5Model::new(config.clone(), device)?,
            lm_head: Linear::new(config.hidden_dim, config.vocab_size, false), // T5 doesn't use bias in lm_head
            is_training: true,
        })
    }

    pub fn generate(
        &self,
        input_ids: &Tensor,
        decoder_start_token_id: i32,
        max_length: usize,
        _num_beams: usize,
        _temperature: f32,
    ) -> Result<Tensor> {
        // Encode input
        let encoder_outputs = self.transformer.encode(input_ids, None)?;

        // Initialize decoder input with start token
        let batch_size = input_ids.size(0)?;
        let mut decoder_input_ids = full(&[batch_size, 1], decoder_start_token_id as f32);

        // Generate tokens autoregressively
        for _ in 1..max_length {
            let decoder_outputs =
                self.transformer
                    .decode(&decoder_input_ids, &encoder_outputs, None, None)?;

            // Get logits for next token prediction
            let logits = self.lm_head.forward(&decoder_outputs)?;
            let seq_len = logits.size(1)? as i64;
            let next_token_logits = logits.narrow(1, seq_len - 1, 1)?;

            // For now, just take argmax (greedy decoding)
            let _next_token = next_token_logits.argmax(Some(-1))?;

            // Concatenate next token to decoder input
            // Simplified implementation - proper tensor concatenation needed
            break; // Placeholder until proper concatenation is implemented
        }

        Ok(decoder_input_ids)
    }
}

impl Module for T5ForConditionalGeneration {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let outputs = self.transformer.forward(input)?;
        self.lm_head.forward(&outputs)
    }

    fn parameters(&self) -> HashMap<String, Parameter> {
        let mut params = HashMap::new();

        for (name, param) in self.transformer.parameters() {
            params.insert(format!("transformer.{}", name), param);
        }
        for (name, param) in self.lm_head.parameters() {
            params.insert(format!("lm_head.{}", name), param);
        }

        params
    }

    fn named_parameters(&self) -> HashMap<String, Parameter> {
        self.parameters()
    }

    fn train(&mut self) {
        self.is_training = true;
        self.transformer.train();
        self.lm_head.train();
    }

    fn eval(&mut self) {
        self.is_training = false;
        self.transformer.eval();
        self.lm_head.eval();
    }

    fn training(&self) -> bool {
        self.is_training
    }

    fn to_device(&mut self, device: DeviceType) -> Result<()> {
        self.transformer.to_device(device)?;
        self.lm_head.to_device(device)
    }
}

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

    #[test]
    fn test_t5_layer_norm() {
        let mut layer_norm = T5LayerNorm::new(768, 1e-6);
        let input: Tensor<f32> = randn(&[2, 10, 768]);
        let output = layer_norm.forward(&input).expect("forward pass should succeed");
        assert_eq!(output.shape().dims(), &[2, 10, 768]);
    }

    #[test]
    fn test_t5_model_creation() {
        let config = TextModelConfig::t5_small();
        let model = T5Model::new(config, DeviceType::Cpu).expect("T5Model should succeed");
        assert_eq!(model.name(), "T5");
        assert_eq!(model.vocab_size(), 32128);
        assert_eq!(model.hidden_dim(), 512);
    }

    #[test]
    fn test_t5_encoder_forward() {
        let config = TextModelConfig::t5_small();
        let mut encoder = T5Encoder::new(&config, DeviceType::Cpu).expect("T5Encoder should succeed");
        let input: Tensor<f32> = randn(&[2, 10, 512]);
        let output = encoder.forward(&input).expect("forward pass should succeed");
        assert_eq!(output.shape().dims(), &[2, 10, 512]);
    }

    #[test]
    fn test_t5_decoder_forward() {
        let config = TextModelConfig::t5_small();
        let mut decoder = T5Decoder::new(&config, DeviceType::Cpu).expect("T5Decoder should succeed");
        let input: Tensor<f32> = randn(&[2, 8, 512]);
        let encoder_hidden: Tensor<f32> = randn(&[2, 10, 512]);
        let output = decoder
            .forward_with_encoder_hidden(&input, Some(&encoder_hidden), None, None)
            .expect("operation should succeed");
        assert_eq!(output.shape().dims(), &[2, 8, 512]);
    }

    #[test]
    fn test_t5_conditional_generation_creation() {
        let config = TextModelConfig::t5_small();
        let model = T5ForConditionalGeneration::new(config, DeviceType::Cpu).expect("T5For Conditional Generation should succeed");
        // Test that model can be created without panicking
        assert!(model.training());
    }
}