tensorlogic-trustformers 0.1.0

Transformer-as-rules: Self-attention and FFN layers as einsum expressions
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
//! Transformer encoder and decoder stacks.
//!
//! This module implements complete transformer stacks by composing multiple
//! encoder or decoder layers.
//!
//! ## Transformer Encoder Stack
//!
//! ```text
//! x = input + position_encoding
//! for layer in encoder_layers:
//!     x = layer(x)
//! output = final_layer_norm(x)
//! ```
//!
//! ## Transformer Decoder Stack
//!
//! ```text
//! x = target + position_encoding
//! for layer in decoder_layers:
//!     x = layer(x, encoder_output)
//! output = final_layer_norm(x)
//! ```

use tensorlogic_ir::EinsumGraph;

use crate::{
    error::Result,
    layers::{DecoderLayer, DecoderLayerConfig, EncoderLayer, EncoderLayerConfig},
    normalization::{LayerNorm, LayerNormConfig},
    position::{LearnedPositionEncoding, PositionEncodingConfig, SinusoidalPositionEncoding},
};

/// Configuration for transformer encoder stack
#[derive(Clone, Debug)]
pub struct EncoderStackConfig {
    /// Number of encoder layers
    pub num_layers: usize,
    /// Configuration for each encoder layer
    pub layer_config: EncoderLayerConfig,
    /// Position encoding configuration
    pub position_encoding: PositionEncodingConfig,
    /// Whether to apply final layer normalization
    pub final_layer_norm: bool,
}

impl EncoderStackConfig {
    /// Create a new encoder stack configuration
    pub fn new(
        num_layers: usize,
        d_model: usize,
        n_heads: usize,
        d_ff: usize,
        max_seq_len: usize,
    ) -> Result<Self> {
        Ok(Self {
            num_layers,
            layer_config: EncoderLayerConfig::new(d_model, n_heads, d_ff)?,
            position_encoding: PositionEncodingConfig::sinusoidal(d_model, max_seq_len),
            final_layer_norm: true,
        })
    }

    /// Set position encoding type to learned
    pub fn with_learned_position_encoding(mut self) -> Self {
        self.position_encoding = PositionEncodingConfig::learned(
            self.position_encoding.d_model,
            self.position_encoding.max_seq_len,
        );
        self
    }

    /// Set whether to use final layer normalization
    pub fn with_final_layer_norm(mut self, final_layer_norm: bool) -> Self {
        self.final_layer_norm = final_layer_norm;
        self
    }

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

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.num_layers == 0 {
            return Err(crate::error::TrustformerError::InvalidDimension {
                expected: 1,
                got: 0,
                context: "num_layers must be positive".to_string(),
            });
        }

        self.layer_config.validate()?;
        self.position_encoding.validate()?;

        Ok(())
    }
}

/// Transformer encoder stack
#[derive(Clone, Debug)]
pub struct EncoderStack {
    /// Configuration
    pub config: EncoderStackConfig,
    /// Encoder layers
    pub layers: Vec<EncoderLayer>,
    /// Position encoding (if sinusoidal)
    pub position_encoding_sin: Option<SinusoidalPositionEncoding>,
    /// Position encoding (if learned)
    pub position_encoding_learned: Option<LearnedPositionEncoding>,
    /// Final layer normalization
    pub final_norm: Option<LayerNorm>,
}

impl EncoderStack {
    /// Create a new encoder stack
    pub fn new(config: EncoderStackConfig) -> Result<Self> {
        config.validate()?;

        let mut layers = Vec::with_capacity(config.num_layers);
        for _ in 0..config.num_layers {
            layers.push(EncoderLayer::new(config.layer_config.clone())?);
        }

        let position_encoding_sin = match config.position_encoding.encoding_type {
            crate::position::PositionEncodingType::Sinusoidal { .. } => Some(
                SinusoidalPositionEncoding::new(config.position_encoding.clone())?,
            ),
            _ => None,
        };

        let position_encoding_learned = match config.position_encoding.encoding_type {
            crate::position::PositionEncodingType::Learned => Some(LearnedPositionEncoding::new(
                config.position_encoding.clone(),
            )?),
            _ => None,
        };

        let final_norm = if config.final_layer_norm {
            Some(LayerNorm::new(LayerNormConfig::new(
                config.layer_config.attention.d_model,
            ))?)
        } else {
            None
        };

        Ok(Self {
            config,
            layers,
            position_encoding_sin,
            position_encoding_learned,
            final_norm,
        })
    }

    /// Build einsum graph for encoder stack
    ///
    /// Input tensors:
    /// - 0: x (input) [batch, seq_len, d_model]
    /// - 1-N: all parameters for position encoding, layers, and final norm
    ///
    /// Output tensors:
    /// - output: [batch, seq_len, d_model]
    pub fn build_encoder_stack_graph(&self, graph: &mut EinsumGraph) -> Result<Vec<usize>> {
        // Step 1: Add position encoding
        let mut current_output = if let Some(ref pe_sin) = self.position_encoding_sin {
            pe_sin.build_encoding_graph(graph)?[0]
        } else if let Some(ref pe_learned) = self.position_encoding_learned {
            pe_learned.build_encoding_graph(graph)?[0]
        } else {
            0 // No position encoding
        };

        // Step 2: Apply each encoder layer sequentially
        for (i, layer) in self.layers.iter().enumerate() {
            // Update the input tensor reference for this layer
            let layer_outputs = layer.build_encoder_layer_graph(graph)?;
            current_output = layer_outputs[0];

            // Add a marker for layer boundary
            let layer_marker = graph.add_tensor(format!("encoder_layer_{}_output", i));
            let marker_node =
                tensorlogic_ir::EinsumNode::elem_unary("identity", current_output, layer_marker);
            graph.add_node(marker_node)?;
            current_output = layer_marker;
        }

        // Step 3: Apply final layer normalization if configured
        if let Some(ref final_norm) = self.final_norm {
            let final_outputs = final_norm.build_layernorm_graph(graph)?;
            current_output = final_outputs[0];
        }

        Ok(vec![current_output])
    }

    /// Get number of layers
    pub fn num_layers(&self) -> usize {
        self.config.num_layers
    }
}

/// Configuration for transformer decoder stack
#[derive(Clone, Debug)]
pub struct DecoderStackConfig {
    /// Number of decoder layers
    pub num_layers: usize,
    /// Configuration for each decoder layer
    pub layer_config: DecoderLayerConfig,
    /// Position encoding configuration
    pub position_encoding: PositionEncodingConfig,
    /// Whether to apply final layer normalization
    pub final_layer_norm: bool,
}

impl DecoderStackConfig {
    /// Create a new decoder stack configuration
    pub fn new(
        num_layers: usize,
        d_model: usize,
        n_heads: usize,
        d_ff: usize,
        max_seq_len: usize,
    ) -> Result<Self> {
        Ok(Self {
            num_layers,
            layer_config: DecoderLayerConfig::new(d_model, n_heads, d_ff)?,
            position_encoding: PositionEncodingConfig::sinusoidal(d_model, max_seq_len),
            final_layer_norm: true,
        })
    }

    /// Set position encoding type to learned
    pub fn with_learned_position_encoding(mut self) -> Self {
        self.position_encoding = PositionEncodingConfig::learned(
            self.position_encoding.d_model,
            self.position_encoding.max_seq_len,
        );
        self
    }

    /// Set whether to use final layer normalization
    pub fn with_final_layer_norm(mut self, final_layer_norm: bool) -> Self {
        self.final_layer_norm = final_layer_norm;
        self
    }

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

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.num_layers == 0 {
            return Err(crate::error::TrustformerError::InvalidDimension {
                expected: 1,
                got: 0,
                context: "num_layers must be positive".to_string(),
            });
        }

        self.layer_config.validate()?;
        self.position_encoding.validate()?;

        Ok(())
    }
}

/// Transformer decoder stack
#[derive(Clone, Debug)]
pub struct DecoderStack {
    /// Configuration
    pub config: DecoderStackConfig,
    /// Decoder layers
    pub layers: Vec<DecoderLayer>,
    /// Position encoding (if sinusoidal)
    pub position_encoding_sin: Option<SinusoidalPositionEncoding>,
    /// Position encoding (if learned)
    pub position_encoding_learned: Option<LearnedPositionEncoding>,
    /// Final layer normalization
    pub final_norm: Option<LayerNorm>,
}

impl DecoderStack {
    /// Create a new decoder stack
    pub fn new(config: DecoderStackConfig) -> Result<Self> {
        config.validate()?;

        let mut layers = Vec::with_capacity(config.num_layers);
        for _ in 0..config.num_layers {
            layers.push(DecoderLayer::new(config.layer_config.clone())?);
        }

        let position_encoding_sin = match config.position_encoding.encoding_type {
            crate::position::PositionEncodingType::Sinusoidal { .. } => Some(
                SinusoidalPositionEncoding::new(config.position_encoding.clone())?,
            ),
            _ => None,
        };

        let position_encoding_learned = match config.position_encoding.encoding_type {
            crate::position::PositionEncodingType::Learned => Some(LearnedPositionEncoding::new(
                config.position_encoding.clone(),
            )?),
            _ => None,
        };

        let final_norm = if config.final_layer_norm {
            Some(LayerNorm::new(LayerNormConfig::new(
                config.layer_config.self_attention.d_model,
            ))?)
        } else {
            None
        };

        Ok(Self {
            config,
            layers,
            position_encoding_sin,
            position_encoding_learned,
            final_norm,
        })
    }

    /// Build einsum graph for decoder stack
    ///
    /// Input tensors:
    /// - 0: x (target input) [batch, tgt_len, d_model]
    /// - 1: encoder_output [batch, src_len, d_model]
    /// - 2-N: all parameters
    ///
    /// Output tensors:
    /// - output: [batch, tgt_len, d_model]
    pub fn build_decoder_stack_graph(&self, graph: &mut EinsumGraph) -> Result<Vec<usize>> {
        // Step 1: Add position encoding to target
        let mut current_output = if let Some(ref pe_sin) = self.position_encoding_sin {
            pe_sin.build_encoding_graph(graph)?[0]
        } else if let Some(ref pe_learned) = self.position_encoding_learned {
            pe_learned.build_encoding_graph(graph)?[0]
        } else {
            0 // No position encoding
        };

        // Step 2: Apply each decoder layer sequentially
        for (i, layer) in self.layers.iter().enumerate() {
            let layer_outputs = layer.build_decoder_layer_graph(graph)?;
            current_output = layer_outputs[0];

            // Add a marker for layer boundary
            let layer_marker = graph.add_tensor(format!("decoder_layer_{}_output", i));
            let marker_node =
                tensorlogic_ir::EinsumNode::elem_unary("identity", current_output, layer_marker);
            graph.add_node(marker_node)?;
            current_output = layer_marker;
        }

        // Step 3: Apply final layer normalization if configured
        if let Some(ref final_norm) = self.final_norm {
            let final_outputs = final_norm.build_layernorm_graph(graph)?;
            current_output = final_outputs[0];
        }

        Ok(vec![current_output])
    }

    /// Get number of layers
    pub fn num_layers(&self) -> usize {
        self.config.num_layers
    }
}

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

    #[test]
    fn test_encoder_stack_config_creation() {
        let config = EncoderStackConfig::new(6, 512, 8, 2048, 1024).expect("unwrap");
        assert_eq!(config.num_layers, 6);
        assert_eq!(config.layer_config.attention.d_model, 512);
        assert!(config.final_layer_norm);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_encoder_stack_config_with_learned_pe() {
        let config = EncoderStackConfig::new(6, 512, 8, 2048, 1024)
            .expect("unwrap")
            .with_learned_position_encoding();
        assert!(matches!(
            config.position_encoding.encoding_type,
            crate::position::PositionEncodingType::Learned
        ));
    }

    #[test]
    fn test_encoder_stack_creation() {
        let config = EncoderStackConfig::new(6, 512, 8, 2048, 1024).expect("unwrap");
        let stack = EncoderStack::new(config).expect("unwrap");
        assert_eq!(stack.num_layers(), 6);
        assert!(stack.position_encoding_sin.is_some());
        assert!(stack.final_norm.is_some());
    }

    #[test]
    fn test_encoder_stack_graph_building() {
        let config = EncoderStackConfig::new(2, 512, 8, 2048, 1024).expect("unwrap");
        let stack = EncoderStack::new(config).expect("unwrap");

        let mut graph = EinsumGraph::new();
        graph.add_tensor("x");

        let outputs = stack.build_encoder_stack_graph(&mut graph).expect("unwrap");
        assert_eq!(outputs.len(), 1);
        assert!(!graph.nodes.is_empty());
    }

    #[test]
    fn test_decoder_stack_config_creation() {
        let config = DecoderStackConfig::new(6, 512, 8, 2048, 1024).expect("unwrap");
        assert_eq!(config.num_layers, 6);
        assert_eq!(config.layer_config.self_attention.d_model, 512);
        assert!(config.layer_config.self_attention.causal);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_decoder_stack_creation() {
        let config = DecoderStackConfig::new(6, 512, 8, 2048, 1024).expect("unwrap");
        let stack = DecoderStack::new(config).expect("unwrap");
        assert_eq!(stack.num_layers(), 6);
        assert!(stack.position_encoding_sin.is_some());
        assert!(stack.final_norm.is_some());
    }

    #[test]
    fn test_decoder_stack_graph_building() {
        let config = DecoderStackConfig::new(2, 512, 8, 2048, 1024).expect("unwrap");
        let stack = DecoderStack::new(config).expect("unwrap");

        let mut graph = EinsumGraph::new();
        graph.add_tensor("target");
        graph.add_tensor("encoder_output");

        let outputs = stack.build_decoder_stack_graph(&mut graph).expect("unwrap");
        assert_eq!(outputs.len(), 1);
        assert!(!graph.nodes.is_empty());
    }

    #[test]
    fn test_invalid_zero_layers() {
        let result = EncoderStackConfig::new(0, 512, 8, 2048, 1024);
        // Should fail validation when creating EncoderStack
        if let Ok(config) = result {
            assert!(config.validate().is_err());
        }
    }
}