trustformers-models 0.1.1

Model implementations for TrustformeRS
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
use serde::{Deserialize, Serialize};
use trustformers_core::{errors::invalid_config, traits::Config};

/// Configuration for hierarchical transformer models
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HierarchicalConfig {
    /// Base hidden size of the model
    pub hidden_size: usize,

    /// Number of hierarchical levels
    pub num_levels: usize,

    /// Number of attention heads per level
    pub num_heads: usize,

    /// Reduction factor between levels
    pub reduction_factor: usize,

    /// Number of layers per level
    pub num_layers_per_level: usize,

    /// Intermediate size for feed-forward networks
    pub intermediate_size: usize,

    /// Dropout rate
    pub dropout: f32,

    /// Attention dropout rate
    pub attention_dropout: f32,

    /// Layer norm epsilon
    pub layer_norm_eps: f32,

    /// Type of hierarchical architecture
    pub hierarchical_type: HierarchicalType,

    /// Method for reducing sequence length between levels
    pub reduction_method: ReductionMethod,

    /// Method for aggregating multi-level features
    pub aggregation_method: AggregationMethod,

    /// Maximum sequence length per level
    pub max_seq_lengths: Vec<usize>,

    /// Whether to use residual connections across levels
    pub cross_level_residual: bool,

    /// Whether to use position embeddings
    pub use_position_embeddings: bool,

    /// Configuration for tree-structured attention
    pub tree_config: Option<TreeConfig>,

    /// Configuration for pyramid architecture
    pub pyramid_config: Option<PyramidConfig>,

    /// Configuration for nested transformers
    pub nested_config: Option<NestedConfig>,
}

/// Types of hierarchical architectures
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HierarchicalType {
    /// Standard hierarchical attention
    Hierarchical,
    /// Pyramid transformer
    Pyramid,
    /// Tree-structured transformer
    Tree,
    /// Nested transformer
    Nested,
    /// Hybrid architecture
    Hybrid,
}

/// Methods for reducing sequence length between levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReductionMethod {
    /// Average pooling
    AveragePooling,
    /// Max pooling
    MaxPooling,
    /// Learnable pooling
    LearnablePooling,
    /// Strided convolution
    StridedConvolution,
    /// Attention-based pooling
    AttentionPooling,
    /// Token merging
    TokenMerging,
}

/// Methods for aggregating features across levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AggregationMethod {
    /// Sum aggregation
    Sum,
    /// Concatenation
    Concatenation,
    /// Weighted sum
    WeightedSum,
    /// Attention-based aggregation
    AttentionAggregation,
    /// Gated aggregation
    GatedAggregation,
}

/// Configuration for tree-structured attention
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TreeConfig {
    /// Branching factor of the tree
    pub branching_factor: usize,

    /// Maximum tree depth
    pub max_depth: usize,

    /// Whether to use learnable tree structure
    pub learnable_structure: bool,

    /// Method for constructing the tree
    pub tree_construction: TreeConstruction,
}

/// Methods for constructing tree structures
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TreeConstruction {
    /// Binary tree
    Binary,
    /// Balanced k-ary tree
    Balanced,
    /// Learned tree structure
    Learned,
    /// Syntax-guided tree
    SyntaxGuided,
}

/// Configuration for pyramid transformers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyramidConfig {
    /// Scaling factors for each level
    pub scaling_factors: Vec<f32>,

    /// Whether to use skip connections
    pub skip_connections: bool,

    /// Method for upsampling
    pub upsampling_method: UpsamplingMethod,

    /// Whether to use feature pyramid networks
    pub use_fpn: bool,
}

/// Methods for upsampling in pyramid architectures
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpsamplingMethod {
    /// Linear interpolation
    Linear,
    /// Transposed convolution
    TransposedConvolution,
    /// Learned upsampling
    Learned,
    /// Pixel shuffle
    PixelShuffle,
}

/// Configuration for nested transformers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NestedConfig {
    /// Number of nested levels
    pub num_nested_levels: usize,

    /// Share parameters across nested levels
    pub share_parameters: bool,

    /// Method for information flow between levels
    pub information_flow: InformationFlow,

    /// Whether to use progressive training
    pub progressive_training: bool,
}

/// Methods for information flow in nested architectures
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InformationFlow {
    /// Bottom-up only
    BottomUp,
    /// Top-down only
    TopDown,
    /// Bidirectional
    Bidirectional,
    /// Skip connections
    SkipConnections,
}

impl Default for HierarchicalConfig {
    fn default() -> Self {
        Self {
            hidden_size: 768,
            num_levels: 4,
            num_heads: 12,
            reduction_factor: 2,
            num_layers_per_level: 3,
            intermediate_size: 3072,
            dropout: 0.1,
            attention_dropout: 0.1,
            layer_norm_eps: 1e-5,
            hierarchical_type: HierarchicalType::Hierarchical,
            reduction_method: ReductionMethod::AveragePooling,
            aggregation_method: AggregationMethod::WeightedSum,
            max_seq_lengths: vec![512, 256, 128, 64],
            cross_level_residual: true,
            use_position_embeddings: true,
            tree_config: None,
            pyramid_config: None,
            nested_config: None,
        }
    }
}

impl Default for TreeConfig {
    fn default() -> Self {
        Self {
            branching_factor: 2,
            max_depth: 8,
            learnable_structure: false,
            tree_construction: TreeConstruction::Binary,
        }
    }
}

impl Default for PyramidConfig {
    fn default() -> Self {
        Self {
            scaling_factors: vec![1.0, 0.5, 0.25, 0.125],
            skip_connections: true,
            upsampling_method: UpsamplingMethod::Linear,
            use_fpn: false,
        }
    }
}

impl Default for NestedConfig {
    fn default() -> Self {
        Self {
            num_nested_levels: 3,
            share_parameters: false,
            information_flow: InformationFlow::Bidirectional,
            progressive_training: false,
        }
    }
}

impl HierarchicalConfig {
    /// Create hierarchical attention configuration
    pub fn hierarchical(hidden_size: usize, num_levels: usize) -> Self {
        Self {
            hidden_size,
            num_levels,
            hierarchical_type: HierarchicalType::Hierarchical,
            max_seq_lengths: (0..num_levels).map(|i| 512 / (2_usize.pow(i as u32))).collect(),
            ..Default::default()
        }
    }

    /// Create pyramid transformer configuration
    pub fn pyramid(hidden_size: usize, num_levels: usize) -> Self {
        Self {
            hidden_size,
            num_levels,
            hierarchical_type: HierarchicalType::Pyramid,
            pyramid_config: Some(PyramidConfig::default()),
            max_seq_lengths: (0..num_levels).map(|i| 512 / (2_usize.pow(i as u32))).collect(),
            ..Default::default()
        }
    }

    /// Create tree transformer configuration
    pub fn tree(hidden_size: usize, branching_factor: usize, max_depth: usize) -> Self {
        Self {
            hidden_size,
            num_levels: max_depth,
            hierarchical_type: HierarchicalType::Tree,
            tree_config: Some(TreeConfig {
                branching_factor,
                max_depth,
                ..Default::default()
            }),
            ..Default::default()
        }
    }

    /// Create nested transformer configuration
    pub fn nested(hidden_size: usize, num_nested_levels: usize) -> Self {
        Self {
            hidden_size,
            num_levels: num_nested_levels,
            hierarchical_type: HierarchicalType::Nested,
            nested_config: Some(NestedConfig {
                num_nested_levels,
                ..Default::default()
            }),
            ..Default::default()
        }
    }

    /// Get the hidden size for a specific level
    pub fn get_hidden_size(&self, level: usize) -> usize {
        match self.hierarchical_type {
            HierarchicalType::Pyramid => {
                if let Some(pyramid_config) = &self.pyramid_config {
                    if level < pyramid_config.scaling_factors.len() {
                        (self.hidden_size as f32 * pyramid_config.scaling_factors[level]) as usize
                    } else {
                        self.hidden_size
                    }
                } else {
                    self.hidden_size
                }
            },
            _ => self.hidden_size,
        }
    }

    /// Get the sequence length for a specific level
    pub fn get_seq_length(&self, level: usize) -> usize {
        if level < self.max_seq_lengths.len() {
            self.max_seq_lengths[level]
        } else {
            512 / (2_usize.pow(level as u32))
        }
    }

    /// Get the reduction factor for a specific level
    pub fn get_reduction_factor(&self, level: usize) -> usize {
        self.reduction_factor.pow(level as u32)
    }

    /// Validate the configuration
    pub fn validate(&self) -> std::result::Result<(), String> {
        if self.num_levels == 0 {
            return Err("num_levels must be greater than 0".to_string());
        }

        if self.reduction_factor == 0 {
            return Err("reduction_factor must be greater than 0".to_string());
        }

        if self.num_heads == 0 {
            return Err("num_heads must be greater than 0".to_string());
        }

        if !self.hidden_size.is_multiple_of(self.num_heads) {
            return Err("hidden_size must be divisible by num_heads".to_string());
        }

        if self.dropout < 0.0 || self.dropout > 1.0 {
            return Err("dropout must be between 0.0 and 1.0".to_string());
        }

        if self.attention_dropout < 0.0 || self.attention_dropout > 1.0 {
            return Err("attention_dropout must be between 0.0 and 1.0".to_string());
        }

        if !self.max_seq_lengths.is_empty() && self.max_seq_lengths.len() != self.num_levels {
            return Err("max_seq_lengths length must match num_levels".to_string());
        }

        // Validate tree config if present
        if let Some(tree_config) = &self.tree_config {
            if tree_config.branching_factor == 0 {
                return Err("branching_factor must be greater than 0".to_string());
            }
            if tree_config.max_depth == 0 {
                return Err("max_depth must be greater than 0".to_string());
            }
        }

        // Validate pyramid config if present
        if let Some(pyramid_config) = &self.pyramid_config {
            if pyramid_config.scaling_factors.is_empty() {
                return Err("scaling_factors cannot be empty".to_string());
            }
            for &factor in &pyramid_config.scaling_factors {
                if factor <= 0.0 {
                    return Err("scaling_factors must be positive".to_string());
                }
            }
        }

        // Validate nested config if present
        if let Some(nested_config) = &self.nested_config {
            if nested_config.num_nested_levels == 0 {
                return Err("num_nested_levels must be greater than 0".to_string());
            }
        }

        Ok(())
    }

    /// Get the total number of parameters (rough estimate)
    pub fn estimate_parameters(&self) -> usize {
        let mut total = 0;

        for level in 0..self.num_levels {
            let hidden_size = self.get_hidden_size(level);
            let _seq_len = self.get_seq_length(level);

            // Attention parameters
            total += hidden_size * hidden_size * 4; // Q, K, V, O projections

            // FFN parameters
            total += hidden_size * self.intermediate_size * 2; // Up and down projections

            // Layer norm parameters
            total += hidden_size * 2; // Weight and bias

            // Multiply by number of layers at this level
            total *= self.num_layers_per_level;
        }

        total
    }
}

impl Config for HierarchicalConfig {
    fn validate(&self) -> trustformers_core::errors::Result<()> {
        // Validate hierarchical configuration parameters
        if self.num_levels == 0 {
            return Err(invalid_config(
                "config_field",
                "num_levels must be greater than 0".to_string(),
            ));
        }
        Ok(())
    }

    fn architecture(&self) -> &'static str {
        "hierarchical"
    }
}

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

    struct Lcg {
        state: u64,
    }
    impl Lcg {
        fn new(seed: u64) -> Self {
            Lcg { state: seed }
        }
        fn next(&mut self) -> u64 {
            self.state = self
                .state
                .wrapping_mul(6364136223846793005u64)
                .wrapping_add(1442695040888963407u64);
            self.state
        }
        fn next_f32(&mut self) -> f32 {
            (self.next() >> 11) as f32 / (1u64 << 53) as f32
        }
    }

    #[test]
    fn test_default_config_fields() {
        let cfg = HierarchicalConfig::default();
        assert_eq!(cfg.hidden_size, 768);
        assert_eq!(cfg.num_levels, 4);
        assert_eq!(cfg.num_heads, 12);
        assert_eq!(cfg.reduction_factor, 2);
        assert!(cfg.cross_level_residual);
        assert!(cfg.use_position_embeddings);
    }

    #[test]
    fn test_default_validate_passes() {
        let cfg = HierarchicalConfig::default();
        let result = Config::validate(&cfg);
        assert!(result.is_ok());
    }

    #[test]
    fn test_architecture_name() {
        let cfg = HierarchicalConfig::default();
        assert_eq!(cfg.architecture(), "hierarchical");
    }

    #[test]
    fn test_zero_num_levels_fails_trait_validate() {
        let cfg = HierarchicalConfig {
            num_levels: 0,
            max_seq_lengths: vec![],
            ..HierarchicalConfig::default()
        };
        assert!(Config::validate(&cfg).is_err());
    }

    #[test]
    fn test_validate_method_zero_levels_fails() {
        let cfg = HierarchicalConfig {
            num_levels: 0,
            max_seq_lengths: vec![],
            ..HierarchicalConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_validate_zero_reduction_factor_fails() {
        let cfg = HierarchicalConfig {
            reduction_factor: 0,
            ..HierarchicalConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_validate_hidden_not_divisible_fails() {
        let cfg = HierarchicalConfig {
            hidden_size: 100,
            num_heads: 12,
            ..HierarchicalConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_validate_dropout_out_of_range_fails() {
        let cfg = HierarchicalConfig {
            dropout: 1.5,
            ..HierarchicalConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_validate_seq_lengths_mismatch_fails() {
        let cfg = HierarchicalConfig {
            num_levels: 4,
            max_seq_lengths: vec![512, 256], // only 2, not 4
            ..HierarchicalConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_get_hidden_size_non_pyramid() {
        let cfg = HierarchicalConfig::default();
        assert_eq!(cfg.get_hidden_size(0), cfg.hidden_size);
        assert_eq!(cfg.get_hidden_size(2), cfg.hidden_size);
    }

    #[test]
    fn test_get_hidden_size_pyramid() {
        let cfg = HierarchicalConfig::pyramid(768, 4);
        // level 0: 768 * 1.0 = 768
        assert_eq!(cfg.get_hidden_size(0), 768);
        // level 1: 768 * 0.5 = 384
        assert_eq!(cfg.get_hidden_size(1), 384);
    }

    #[test]
    fn test_get_seq_length_within_bounds() {
        let cfg = HierarchicalConfig::default();
        assert_eq!(cfg.get_seq_length(0), cfg.max_seq_lengths[0]);
        assert_eq!(cfg.get_seq_length(1), cfg.max_seq_lengths[1]);
    }

    #[test]
    fn test_get_seq_length_out_of_bounds_uses_formula() {
        let cfg = HierarchicalConfig::default();
        // beyond max_seq_lengths range
        let lvl = cfg.max_seq_lengths.len() + 2;
        let expected = 512 / (2_usize.pow(lvl as u32));
        assert_eq!(cfg.get_seq_length(lvl), expected);
    }

    #[test]
    fn test_get_reduction_factor_level_zero() {
        let cfg = HierarchicalConfig::default();
        assert_eq!(cfg.get_reduction_factor(0), 1); // reduction_factor^0 = 1
    }

    #[test]
    fn test_get_reduction_factor_level_two() {
        let cfg = HierarchicalConfig::default();
        assert_eq!(cfg.get_reduction_factor(2), 4); // 2^2 = 4
    }

    #[test]
    fn test_hierarchical_factory() {
        let cfg = HierarchicalConfig::hierarchical(512, 3);
        assert_eq!(cfg.hidden_size, 512);
        assert_eq!(cfg.num_levels, 3);
        assert_eq!(cfg.max_seq_lengths.len(), 3);
    }

    #[test]
    fn test_pyramid_factory_sets_pyramid_config() {
        let cfg = HierarchicalConfig::pyramid(768, 4);
        assert!(cfg.pyramid_config.is_some());
        assert_eq!(cfg.num_levels, 4);
    }

    #[test]
    fn test_tree_factory_sets_tree_config() {
        let cfg = HierarchicalConfig::tree(512, 2, 5);
        assert!(cfg.tree_config.is_some());
        if let Some(tc) = &cfg.tree_config {
            assert_eq!(tc.branching_factor, 2);
            assert_eq!(tc.max_depth, 5);
        }
    }

    #[test]
    fn test_nested_factory_sets_nested_config() {
        let cfg = HierarchicalConfig::nested(768, 3);
        assert!(cfg.nested_config.is_some());
        if let Some(nc) = &cfg.nested_config {
            assert_eq!(nc.num_nested_levels, 3);
        }
    }

    #[test]
    fn test_estimate_parameters_nonzero() {
        let cfg = HierarchicalConfig::default();
        let params = cfg.estimate_parameters();
        assert!(params > 0);
    }

    #[test]
    fn test_tree_config_default() {
        let tc = TreeConfig::default();
        assert_eq!(tc.branching_factor, 2);
        assert_eq!(tc.max_depth, 8);
        assert!(!tc.learnable_structure);
    }

    #[test]
    fn test_pyramid_config_default() {
        let pc = PyramidConfig::default();
        assert!(!pc.scaling_factors.is_empty());
        assert!(pc.skip_connections);
        assert!(!pc.use_fpn);
    }

    #[test]
    fn test_nested_config_default() {
        let nc = NestedConfig::default();
        assert_eq!(nc.num_nested_levels, 3);
        assert!(!nc.share_parameters);
        assert!(!nc.progressive_training);
    }

    #[test]
    fn test_lcg_values_in_range() {
        let mut rng = Lcg::new(314159);
        for _ in 0..100 {
            let v = rng.next_f32();
            assert!((0.0..1.0).contains(&v));
        }
    }
}