tsai_models 0.1.2

Model zoo for tsai-rs: CNN, Transformer, ROCKET, RNN, and Tabular architectures
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
//! Base Transformer model for time series.
//!
//! This is a vanilla Transformer encoder for time series classification/regression,
//! similar to the base TransformerModel in Python tsai.

use burn::module::Param;
use burn::nn::{
    attention::{MhaInput, MultiHeadAttention, MultiHeadAttentionConfig},
    Dropout, DropoutConfig, Linear, LinearConfig, LayerNorm, LayerNormConfig,
};
use burn::prelude::*;
use burn::tensor::activation::{gelu, softmax};
use serde::{Deserialize, Serialize};

/// Configuration for the base Transformer model.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformerModelConfig {
    /// Number of input variables/channels.
    pub n_vars: usize,
    /// Sequence length.
    pub seq_len: usize,
    /// Number of output classes (for classification) or targets (for regression).
    pub n_outputs: usize,
    /// Model dimension (embedding size).
    pub d_model: usize,
    /// Number of attention heads.
    pub n_heads: usize,
    /// Number of transformer encoder layers.
    pub n_layers: usize,
    /// Feedforward network dimension (typically 4 * d_model).
    pub d_ff: usize,
    /// Dropout rate.
    pub dropout: f64,
    /// Attention dropout rate.
    pub attn_dropout: f64,
    /// Whether to use positional encoding.
    pub use_pos_encoding: bool,
    /// Type of positional encoding.
    pub pos_encoding_type: PositionalEncodingType,
    /// Whether to use pre-layer normalization (more stable training).
    pub pre_norm: bool,
    /// Activation function for feedforward network.
    pub activation: ActivationType,
    /// How to aggregate sequence for output (mean, first, last, flatten).
    pub aggregation: AggregationType,
}

/// Type of positional encoding.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum PositionalEncodingType {
    /// Sinusoidal positional encoding (original Transformer).
    #[default]
    Sinusoidal,
    /// Learnable positional embeddings.
    Learnable,
    /// No positional encoding.
    None,
}

/// Activation function type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum ActivationType {
    /// ReLU activation.
    ReLU,
    /// GELU activation (default, better for transformers).
    #[default]
    GELU,
}

/// Aggregation type for output.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum AggregationType {
    /// Mean pooling over sequence dimension.
    #[default]
    Mean,
    /// Use first token (CLS-style).
    First,
    /// Use last token.
    Last,
    /// Flatten all tokens.
    Flatten,
}

impl Default for TransformerModelConfig {
    fn default() -> Self {
        Self {
            n_vars: 1,
            seq_len: 100,
            n_outputs: 2,
            d_model: 128,
            n_heads: 8,
            n_layers: 3,
            d_ff: 512,
            dropout: 0.1,
            attn_dropout: 0.0,
            use_pos_encoding: true,
            pos_encoding_type: PositionalEncodingType::Sinusoidal,
            pre_norm: false,
            activation: ActivationType::GELU,
            aggregation: AggregationType::Mean,
        }
    }
}

impl TransformerModelConfig {
    /// Create a new config with basic parameters.
    pub fn new(n_vars: usize, seq_len: usize, n_outputs: usize) -> Self {
        Self {
            n_vars,
            seq_len,
            n_outputs,
            ..Default::default()
        }
    }

    /// Set model dimension.
    pub fn with_d_model(mut self, d_model: usize) -> Self {
        self.d_model = d_model;
        self
    }

    /// Set number of attention heads.
    pub fn with_n_heads(mut self, n_heads: usize) -> Self {
        self.n_heads = n_heads;
        self
    }

    /// Set number of layers.
    pub fn with_n_layers(mut self, n_layers: usize) -> Self {
        self.n_layers = n_layers;
        self
    }

    /// Set feedforward dimension.
    pub fn with_d_ff(mut self, d_ff: usize) -> Self {
        self.d_ff = d_ff;
        self
    }

    /// Set dropout rate.
    pub fn with_dropout(mut self, dropout: f64) -> Self {
        self.dropout = dropout;
        self
    }

    /// Set positional encoding type.
    pub fn with_pos_encoding(mut self, pos_type: PositionalEncodingType) -> Self {
        self.pos_encoding_type = pos_type;
        self.use_pos_encoding = !matches!(pos_type, PositionalEncodingType::None);
        self
    }

    /// Enable pre-layer normalization.
    pub fn with_pre_norm(mut self, pre_norm: bool) -> Self {
        self.pre_norm = pre_norm;
        self
    }

    /// Set activation function.
    pub fn with_activation(mut self, activation: ActivationType) -> Self {
        self.activation = activation;
        self
    }

    /// Set aggregation type.
    pub fn with_aggregation(mut self, aggregation: AggregationType) -> Self {
        self.aggregation = aggregation;
        self
    }

    /// Initialize the model.
    pub fn init<B: Backend>(&self, device: &B::Device) -> TransformerModel<B> {
        TransformerModel::new(self.clone(), device)
    }
}

/// Transformer encoder layer with pre-norm or post-norm option.
#[derive(Module, Debug)]
pub struct TransformerEncoderLayer<B: Backend> {
    attention: MultiHeadAttention<B>,
    norm1: LayerNorm<B>,
    ff_linear1: Linear<B>,
    ff_linear2: Linear<B>,
    norm2: LayerNorm<B>,
    dropout1: Dropout,
    dropout2: Dropout,
    #[module(skip)]
    pre_norm: bool,
    #[module(skip)]
    use_gelu: bool,
}

impl<B: Backend> TransformerEncoderLayer<B> {
    fn new(config: &TransformerModelConfig, device: &B::Device) -> Self {
        let attention = MultiHeadAttentionConfig::new(config.d_model, config.n_heads)
            .with_dropout(config.attn_dropout)
            .init(device);
        let norm1 = LayerNormConfig::new(config.d_model).init(device);
        let ff_linear1 = LinearConfig::new(config.d_model, config.d_ff).init(device);
        let ff_linear2 = LinearConfig::new(config.d_ff, config.d_model).init(device);
        let norm2 = LayerNormConfig::new(config.d_model).init(device);
        let dropout1 = DropoutConfig::new(config.dropout).init();
        let dropout2 = DropoutConfig::new(config.dropout).init();

        Self {
            attention,
            norm1,
            ff_linear1,
            ff_linear2,
            norm2,
            dropout1,
            dropout2,
            pre_norm: config.pre_norm,
            use_gelu: matches!(config.activation, ActivationType::GELU),
        }
    }

    fn forward(&self, x: Tensor<B, 3>) -> Tensor<B, 3> {
        if self.pre_norm {
            // Pre-norm: norm -> attention -> residual
            let x_norm = self.norm1.forward(x.clone());
            let attn_input = MhaInput::self_attn(x_norm);
            let attn_out = self.attention.forward(attn_input).context;
            let x = x + self.dropout1.forward(attn_out);

            // Feedforward with pre-norm
            let x_norm = self.norm2.forward(x.clone());
            let ff_out = self.ff_linear1.forward(x_norm);
            let ff_out = if self.use_gelu {
                gelu(ff_out)
            } else {
                ff_out.clamp_min(0.0) // ReLU
            };
            let ff_out = self.dropout2.forward(ff_out);
            let ff_out = self.ff_linear2.forward(ff_out);

            x + self.dropout2.forward(ff_out)
        } else {
            // Post-norm (original Transformer): attention -> residual -> norm
            let attn_input = MhaInput::self_attn(x.clone());
            let attn_out = self.attention.forward(attn_input).context;
            let x = self.norm1.forward(x + self.dropout1.forward(attn_out));

            // Feedforward
            let ff_out = self.ff_linear1.forward(x.clone());
            let ff_out = if self.use_gelu {
                gelu(ff_out)
            } else {
                ff_out.clamp_min(0.0) // ReLU
            };
            let ff_out = self.dropout2.forward(ff_out);
            let ff_out = self.ff_linear2.forward(ff_out);

            self.norm2.forward(x + self.dropout2.forward(ff_out))
        }
    }
}

/// Base Transformer model for time series.
///
/// This is a vanilla transformer encoder that can be used for:
/// - Time series classification
/// - Time series regression
/// - As a backbone for more complex models
///
/// # Example
///
/// ```rust,ignore
/// use tsai_models::transformer::{TransformerModel, TransformerModelConfig};
///
/// let config = TransformerModelConfig::new(3, 100, 5)  // 3 vars, 100 timesteps, 5 classes
///     .with_d_model(64)
///     .with_n_layers(4)
///     .with_n_heads(4);
///
/// let model = config.init::<NdArray>(&device);
/// let output = model.forward(input);  // [batch, n_classes]
/// ```
#[derive(Module, Debug)]
pub struct TransformerModel<B: Backend> {
    /// Input embedding (projects n_vars to d_model).
    input_embedding: Linear<B>,
    /// Learnable positional encoding (optional).
    pos_embedding: Option<Param<Tensor<B, 2>>>,
    /// Input dropout.
    input_dropout: Dropout,
    /// Transformer encoder layers.
    encoder_layers: Vec<TransformerEncoderLayer<B>>,
    /// Final layer normalization (for pre-norm models).
    final_norm: Option<LayerNorm<B>>,
    /// Output projection head.
    head: Linear<B>,
    /// Model dimension.
    #[module(skip)]
    d_model: usize,
    /// Sequence length.
    #[module(skip)]
    seq_len: usize,
    /// Whether to use positional encoding.
    #[module(skip)]
    use_pos_encoding: bool,
    /// Aggregation type (0=Mean, 1=First, 2=Last, 3=Flatten).
    #[module(skip)]
    aggregation: u8,
}

impl<B: Backend> TransformerModel<B> {
    /// Create a new TransformerModel.
    pub fn new(config: TransformerModelConfig, device: &B::Device) -> Self {
        // Input embedding: project n_vars to d_model
        let input_embedding = LinearConfig::new(config.n_vars, config.d_model).init(device);

        // Positional embedding (learnable)
        let pos_embedding = if config.use_pos_encoding
            && matches!(config.pos_encoding_type, PositionalEncodingType::Learnable)
        {
            let pe = Tensor::random(
                [config.seq_len, config.d_model],
                burn::tensor::Distribution::Normal(0.0, 0.02),
                device,
            );
            Some(Param::from_tensor(pe))
        } else {
            None
        };

        // Input dropout
        let input_dropout = DropoutConfig::new(config.dropout).init();

        // Encoder layers
        let encoder_layers: Vec<_> = (0..config.n_layers)
            .map(|_| TransformerEncoderLayer::new(&config, device))
            .collect();

        // Final norm for pre-norm models
        let final_norm = if config.pre_norm {
            Some(LayerNormConfig::new(config.d_model).init(device))
        } else {
            None
        };

        // Output head
        let head_input_size = match config.aggregation {
            AggregationType::Flatten => config.d_model * config.seq_len,
            _ => config.d_model,
        };
        let head = LinearConfig::new(head_input_size, config.n_outputs).init(device);

        Self {
            input_embedding,
            pos_embedding,
            input_dropout,
            encoder_layers,
            final_norm,
            head,
            d_model: config.d_model,
            seq_len: config.seq_len,
            use_pos_encoding: config.use_pos_encoding,
            aggregation: match config.aggregation {
                AggregationType::Mean => 0,
                AggregationType::First => 1,
                AggregationType::Last => 2,
                AggregationType::Flatten => 3,
            },
        }
    }

    /// Create sinusoidal positional encoding.
    fn sinusoidal_encoding(&self, seq_len: usize, d_model: usize, device: &B::Device) -> Tensor<B, 2> {
        let mut pe = vec![0.0f32; seq_len * d_model];

        for pos in 0..seq_len {
            for i in 0..d_model {
                let angle = pos as f32 / (10000.0f32).powf((2 * (i / 2)) as f32 / d_model as f32);
                pe[pos * d_model + i] = if i % 2 == 0 { angle.sin() } else { angle.cos() };
            }
        }

        Tensor::<B, 1>::from_floats(pe.as_slice(), device).reshape([seq_len, d_model])
    }

    /// Aggregate sequence outputs.
    fn aggregate(&self, x: Tensor<B, 3>) -> Tensor<B, 2> {
        let [batch, seq_len, d_model] = x.dims();

        match self.aggregation {
            0 => x.mean_dim(1).reshape([batch, d_model]), // Mean
            1 => x.slice([0..batch, 0..1, 0..d_model]).reshape([batch, d_model]), // First
            2 => x.slice([0..batch, seq_len - 1..seq_len, 0..d_model]).reshape([batch, d_model]), // Last
            _ => x.reshape([batch, seq_len * d_model]), // Flatten
        }
    }

    /// Forward pass returning logits.
    pub fn forward(&self, x: Tensor<B, 3>) -> Tensor<B, 2> {
        let [_batch, _n_vars, seq_len] = x.dims();
        let device = x.device();

        // Transpose to (batch, seq_len, n_vars)
        let x = x.swap_dims(1, 2);

        // Project to d_model: (batch, seq_len, d_model)
        let mut x = self.input_embedding.forward(x);
        let [_, _, d_model] = x.dims();

        // Add positional encoding
        if self.use_pos_encoding {
            let pos_enc = match &self.pos_embedding {
                Some(pe) => pe.val().clone(),
                None => self.sinusoidal_encoding(seq_len, d_model, &device),
            };
            x = x + pos_enc.unsqueeze::<3>();
        }

        // Input dropout
        x = self.input_dropout.forward(x);

        // Transformer encoder
        for layer in &self.encoder_layers {
            x = layer.forward(x);
        }

        // Final normalization (for pre-norm)
        if let Some(ref norm) = self.final_norm {
            x = norm.forward(x);
        }

        // Aggregate and project
        let x = self.aggregate(x);
        self.head.forward(x)
    }

    /// Forward pass returning probabilities (for classification).
    pub fn forward_probs(&self, x: Tensor<B, 3>) -> Tensor<B, 2> {
        let logits = self.forward(x);
        softmax(logits, 1)
    }

    /// Get encoder output without classification head.
    /// Returns: (batch, seq_len, d_model)
    pub fn encode(&self, x: Tensor<B, 3>) -> Tensor<B, 3> {
        let [_batch, _n_vars, seq_len] = x.dims();
        let device = x.device();

        // Transpose to (batch, seq_len, n_vars)
        let x = x.swap_dims(1, 2);

        // Project to d_model
        let mut x = self.input_embedding.forward(x);
        let [_, _, d_model] = x.dims();

        // Add positional encoding
        if self.use_pos_encoding {
            let pos_enc = match &self.pos_embedding {
                Some(pe) => pe.val().clone(),
                None => self.sinusoidal_encoding(seq_len, d_model, &device),
            };
            x = x + pos_enc.unsqueeze::<3>();
        }

        // Input dropout
        x = self.input_dropout.forward(x);

        // Transformer encoder
        for layer in &self.encoder_layers {
            x = layer.forward(x);
        }

        // Final normalization
        if let Some(ref norm) = self.final_norm {
            x = norm.forward(x);
        }

        x
    }

    /// Get the model dimension.
    pub fn d_model(&self) -> usize {
        self.d_model
    }

    /// Get the sequence length.
    pub fn seq_len(&self) -> usize {
        self.seq_len
    }
}

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

    type TestBackend = NdArray;

    #[test]
    fn test_transformer_model_default() {
        let device = Default::default();
        let config = TransformerModelConfig::new(3, 50, 5);
        let model: TransformerModel<TestBackend> = config.init(&device);

        let input = Tensor::random([4, 3, 50], burn::tensor::Distribution::Normal(0.0, 1.0), &device);
        let output = model.forward(input);

        assert_eq!(output.dims(), [4, 5]);
    }

    #[test]
    fn test_transformer_model_with_pre_norm() {
        let device = Default::default();
        let config = TransformerModelConfig::new(2, 100, 10)
            .with_d_model(64)
            .with_n_layers(4)
            .with_n_heads(4)
            .with_pre_norm(true);
        let model: TransformerModel<TestBackend> = config.init(&device);

        let input = Tensor::random([2, 2, 100], burn::tensor::Distribution::Normal(0.0, 1.0), &device);
        let output = model.forward(input);

        assert_eq!(output.dims(), [2, 10]);
    }

    #[test]
    fn test_transformer_model_learnable_pos() {
        let device = Default::default();
        let config = TransformerModelConfig::new(1, 32, 3)
            .with_pos_encoding(PositionalEncodingType::Learnable);
        let model: TransformerModel<TestBackend> = config.init(&device);

        let input = Tensor::random([8, 1, 32], burn::tensor::Distribution::Normal(0.0, 1.0), &device);
        let output = model.forward(input);

        assert_eq!(output.dims(), [8, 3]);
    }

    #[test]
    fn test_transformer_encode() {
        let device = Default::default();
        let config = TransformerModelConfig::new(3, 50, 5)
            .with_d_model(32);
        let model: TransformerModel<TestBackend> = config.init(&device);

        let input = Tensor::random([4, 3, 50], burn::tensor::Distribution::Normal(0.0, 1.0), &device);
        let encoded = model.encode(input);

        // Encoded shape: (batch, seq_len, d_model)
        assert_eq!(encoded.dims(), [4, 50, 32]);
    }

    #[test]
    fn test_aggregation_types() {
        let device = Default::default();

        // Test flatten aggregation
        let config = TransformerModelConfig::new(2, 10, 4)
            .with_d_model(16)
            .with_aggregation(AggregationType::Flatten);
        let model: TransformerModel<TestBackend> = config.init(&device);

        let input = Tensor::random([2, 2, 10], burn::tensor::Distribution::Normal(0.0, 1.0), &device);
        let output = model.forward(input);

        assert_eq!(output.dims(), [2, 4]);
    }

    #[test]
    fn test_forward_probs() {
        let device = Default::default();
        let config = TransformerModelConfig::new(3, 50, 5);
        let model: TransformerModel<TestBackend> = config.init(&device);

        let input = Tensor::random([4, 3, 50], burn::tensor::Distribution::Normal(0.0, 1.0), &device);
        let probs = model.forward_probs(input);

        assert_eq!(probs.dims(), [4, 5]);

        // Probabilities should sum to 1
        let sum = probs.clone().sum_dim(1);
        let expected = Tensor::ones([4, 1], &device);
        let diff = (sum - expected).abs().max().into_scalar();
        assert!(diff < 1e-5, "Probabilities should sum to 1");
    }
}