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
use super::config::{
    AdaptiveAttentionConfig, CrossAttentionConfig, GateActivation, GatedAttentionConfig,
    HierarchicalAttentionConfig, SparseAttentionConfig,
};
use super::utils::{
    create_sparse_mask, pool_tensor, reshape_for_multihead, reshape_from_multihead,
    scaled_dot_product_attention, CrossAttentionOutput, PoolingMethod, SparsePattern,
};
use trustformers_core::{
    errors::{invalid_config, tensor_op_error, Result},
    layers::{LayerNorm, Linear},
    ops::activations::{gelu, sigmoid, silu, tanh},
    tensor::Tensor,
    traits::Layer,
};

/// Standard cross-attention layer
pub struct CrossAttention {
    config: CrossAttentionConfig,
    q_proj: Linear,
    k_proj: Linear,
    v_proj: Linear,
    out_proj: Linear,
    scale: f32,
}

impl CrossAttention {
    pub fn new(config: CrossAttentionConfig) -> Result<Self> {
        config.validate().map_err(|e| invalid_config("config_field", e.to_string()))?;

        let hidden_size = config.hidden_size;
        let q_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let k_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let v_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let out_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let scale = config.get_scale();

        Ok(Self {
            config,
            q_proj,
            k_proj,
            v_proj,
            out_proj,
            scale,
        })
    }

    /// Forward pass with separate query, key, and value inputs
    pub fn forward(
        &self,
        query: Tensor,
        key: Tensor,
        value: Tensor,
        mask: Option<Tensor>,
    ) -> Result<CrossAttentionOutput> {
        let q = self.q_proj.forward(query)?;
        let k = self.k_proj.forward(key)?;
        let v = self.v_proj.forward(value)?;

        let attn_output = scaled_dot_product_attention(
            &q,
            &k,
            &v,
            mask.as_ref(),
            self.scale,
            self.config.attention_dropout,
            true, // training flag - would be configurable
        )?;

        let output = self.out_proj.forward(attn_output.output)?;

        Ok(CrossAttentionOutput {
            output,
            attention_weights: attn_output.attention_weights,
            attention_stats: attn_output.attention_stats,
        })
    }
}

/// Multi-head cross-attention layer
pub struct MultiHeadCrossAttention {
    config: CrossAttentionConfig,
    q_proj: Linear,
    k_proj: Linear,
    v_proj: Linear,
    out_proj: Linear,
    head_dim: usize,
    scale: f32,
}

impl MultiHeadCrossAttention {
    pub fn new(config: CrossAttentionConfig) -> Result<Self> {
        config.validate().map_err(|e| invalid_config("config_field", e.to_string()))?;

        let hidden_size = config.hidden_size;
        let head_dim = config.get_head_dim();

        let q_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let k_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let v_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let out_proj = Linear::new(hidden_size, hidden_size, config.bias);
        let scale = config.get_scale();

        Ok(Self {
            config,
            q_proj,
            k_proj,
            v_proj,
            out_proj,
            head_dim,
            scale,
        })
    }

    pub fn forward(
        &self,
        query: Tensor,
        key: Tensor,
        value: Tensor,
        mask: Option<Tensor>,
    ) -> Result<CrossAttentionOutput> {
        let batch_size = query.shape()[0];
        let query_len = query.shape()[1];
        let key_len = key.shape()[1];

        // Project to query, key, value
        let q = self.q_proj.forward(query)?;
        let k = self.k_proj.forward(key)?;
        let v = self.v_proj.forward(value)?;

        // Reshape for multi-head attention
        let q = reshape_for_multihead(q, self.config.num_heads, self.head_dim)?;
        let k = reshape_for_multihead(k, self.config.num_heads, self.head_dim)?;
        let v = reshape_for_multihead(v, self.config.num_heads, self.head_dim)?;

        // Expand mask for multiple heads if provided
        let mask = if let Some(mask) = mask {
            Some(mask.unsqueeze(1)?.broadcast_to(&[
                batch_size,
                self.config.num_heads,
                query_len,
                key_len,
            ])?)
        } else {
            None
        };

        // Compute attention
        let attn_output = scaled_dot_product_attention(
            &q,
            &k,
            &v,
            mask.as_ref(),
            self.scale,
            self.config.attention_dropout,
            true,
        )?;

        // Reshape back
        let output = reshape_from_multihead(attn_output.output, self.config.hidden_size)?;
        let output = self.out_proj.forward(output)?;

        Ok(CrossAttentionOutput {
            output,
            attention_weights: attn_output.attention_weights,
            attention_stats: attn_output.attention_stats,
        })
    }
}

/// Sparse cross-attention layer
pub struct SparseCrossAttention {
    #[allow(dead_code)]
    config: CrossAttentionConfig,
    base_attention: MultiHeadCrossAttention,
    sparse_config: SparseAttentionConfig,
}

impl SparseCrossAttention {
    pub fn new(config: CrossAttentionConfig) -> Result<Self> {
        let sparse_config = config.sparse_config.clone().unwrap_or_default();
        let base_attention = MultiHeadCrossAttention::new(config.clone())?;

        Ok(Self {
            config,
            base_attention,
            sparse_config,
        })
    }

    pub fn forward(
        &self,
        query: Tensor,
        key: Tensor,
        value: Tensor,
        mask: Option<Tensor>,
    ) -> Result<CrossAttentionOutput> {
        let query_len = query.shape()[1];
        let key_len = key.shape()[1];

        // Create sparse mask
        let sparse_pattern = match self.sparse_config.pattern {
            crate::cross_attention::config::SparsePattern::Random => SparsePattern::Random,
            crate::cross_attention::config::SparsePattern::Block => {
                SparsePattern::Block(self.sparse_config.block_size.unwrap_or(64))
            },
            crate::cross_attention::config::SparsePattern::Strided => {
                SparsePattern::Strided(4) // Default stride
            },
            crate::cross_attention::config::SparsePattern::Local => {
                SparsePattern::TopK(self.sparse_config.block_size.unwrap_or(64))
            },
            crate::cross_attention::config::SparsePattern::TopK => {
                SparsePattern::TopK(self.sparse_config.random_connections.unwrap_or(32))
            },
        };

        let sparse_mask = create_sparse_mask(
            query_len,
            key_len,
            self.sparse_config.sparsity_ratio,
            sparse_pattern,
        )?;

        // Combine with existing mask
        let combined_mask = if let Some(mask) = mask {
            Some(mask.add(&sparse_mask)?)
        } else {
            Some(sparse_mask)
        };

        // Apply sparse attention
        self.base_attention.forward(query, key, value, combined_mask)
    }
}

/// Hierarchical cross-attention layer
pub struct HierarchicalCrossAttention {
    #[allow(dead_code)]
    config: CrossAttentionConfig,
    hierarchical_config: HierarchicalAttentionConfig,
    attention_layers: Vec<MultiHeadCrossAttention>,
    pooling_layers: Vec<Linear>,
    output_projection: Linear,
}

impl HierarchicalCrossAttention {
    pub fn new(config: CrossAttentionConfig) -> Result<Self> {
        let hierarchical_config = config.hierarchical_config.clone().unwrap_or_default();

        let mut attention_layers = Vec::new();
        let mut pooling_layers = Vec::new();

        for _ in 0..hierarchical_config.num_levels {
            let layer_config = config.clone();
            attention_layers.push(MultiHeadCrossAttention::new(layer_config)?);

            if hierarchical_config.learnable_pooling {
                pooling_layers.push(Linear::new(config.hidden_size, config.hidden_size, false));
            }
        }

        let output_projection = Linear::new(
            config.hidden_size * hierarchical_config.num_levels,
            config.hidden_size,
            config.bias,
        );

        Ok(Self {
            config,
            hierarchical_config,
            attention_layers,
            pooling_layers,
            output_projection,
        })
    }

    pub fn forward(
        &self,
        query: Tensor,
        key: Tensor,
        value: Tensor,
        mask: Option<Tensor>,
    ) -> Result<CrossAttentionOutput> {
        let mut level_outputs = Vec::new();
        let mut current_key = key;
        let mut current_value = value;

        for level in 0..self.hierarchical_config.num_levels {
            // Apply attention at current level
            let attn_output = self.attention_layers[level].forward(
                query.clone(),
                current_key.clone(),
                current_value.clone(),
                mask.clone(),
            )?;

            level_outputs.push(attn_output.output);

            // Pool for next level
            if level < self.hierarchical_config.num_levels - 1 {
                let pooling_factor = self.hierarchical_config.pooling_factor;

                current_key = pool_tensor(current_key, pooling_factor, PoolingMethod::Average)?;
                current_value = pool_tensor(current_value, pooling_factor, PoolingMethod::Average)?;

                // Apply learnable pooling if configured
                if self.hierarchical_config.learnable_pooling && level < self.pooling_layers.len() {
                    current_key = self.pooling_layers[level].forward(current_key)?;
                    current_value = self.pooling_layers[level].forward(current_value)?;
                }
            }
        }

        // Aggregate multi-level outputs
        let output = match self.hierarchical_config.aggregation_method {
            crate::cross_attention::config::AggregationMethod::WeightedSum => {
                aggregate_weighted_sum(level_outputs)?
            },
            crate::cross_attention::config::AggregationMethod::Concatenation => {
                let first_output = &level_outputs[0];
                let concat_dim = first_output.shape().len().saturating_sub(1);
                let concatenated = Tensor::concat(&level_outputs, concat_dim)?;
                self.output_projection.forward(concatenated)?
            },
            crate::cross_attention::config::AggregationMethod::MaxPooling => {
                aggregate_max_pooling(level_outputs)?
            },
            crate::cross_attention::config::AggregationMethod::AvgPooling => {
                aggregate_avg_pooling(level_outputs)?
            },
        };

        Ok(CrossAttentionOutput {
            output,
            attention_weights: None,
            attention_stats: None,
        })
    }
}

/// Adaptive cross-attention layer
pub struct AdaptiveCrossAttention {
    #[allow(dead_code)]
    config: CrossAttentionConfig,
    adaptive_config: AdaptiveAttentionConfig,
    base_attention: MultiHeadCrossAttention,
    pattern_embeddings: Linear,
    pattern_selector: Linear,
    attention_patterns: Vec<Linear>,
}

impl AdaptiveCrossAttention {
    pub fn new(config: CrossAttentionConfig) -> Result<Self> {
        let adaptive_config = config.adaptive_config.clone().unwrap_or_default();
        let base_attention = MultiHeadCrossAttention::new(config.clone())?;

        let pattern_embeddings =
            Linear::new(config.hidden_size, adaptive_config.pattern_dim, false);

        let pattern_selector = Linear::new(
            adaptive_config.pattern_dim,
            adaptive_config.num_patterns,
            false,
        );

        let mut attention_patterns = Vec::new();
        for _ in 0..adaptive_config.num_patterns {
            attention_patterns.push(Linear::new(config.hidden_size, config.hidden_size, false));
        }

        Ok(Self {
            config,
            adaptive_config,
            base_attention,
            pattern_embeddings,
            pattern_selector,
            attention_patterns,
        })
    }

    pub fn forward(
        &self,
        query: Tensor,
        key: Tensor,
        value: Tensor,
        mask: Option<Tensor>,
    ) -> Result<CrossAttentionOutput> {
        // Compute pattern embeddings
        let pattern_emb = self.pattern_embeddings.forward(query.clone())?;
        let seq_len = pattern_emb.shape()[1] as f32;
        let pattern_emb = pattern_emb.sum(Some(vec![1]), false)?.div_scalar(seq_len)?; // Average over sequence length

        // Select attention pattern
        let pattern_logits = self.pattern_selector.forward(pattern_emb)?;
        let pattern_weights = if self.adaptive_config.hard_selection {
            // Hard selection - use argmax for discrete selection
            let _max_indices = pattern_logits.argmax(-1)?;
            // Create one-hot encoding by comparing with max indices
            // For now, return softmax of large logits to approximate one-hot
            let large_logits = pattern_logits.mul_scalar(100.0)?;
            large_logits.softmax(-1)?
        } else {
            // Soft selection with temperature-scaled softmax
            let scaled_logits = pattern_logits.div_scalar(self.adaptive_config.temperature)?;
            scaled_logits.softmax(-1)?
        };

        // Apply selected pattern(s)
        let mut pattern_outputs = Vec::new();
        for i in 0..self.adaptive_config.num_patterns {
            let pattern_query = self.attention_patterns[i].forward(query.clone())?;
            let pattern_output = self.base_attention.forward(
                pattern_query,
                key.clone(),
                value.clone(),
                mask.clone(),
            )?;
            pattern_outputs.push(pattern_output.output);
        }

        // Combine pattern outputs
        let output = combine_pattern_outputs(pattern_outputs, pattern_weights)?;

        Ok(CrossAttentionOutput {
            output,
            attention_weights: None,
            attention_stats: None,
        })
    }
}

/// Gated cross-attention layer
pub struct GatedCrossAttention {
    #[allow(dead_code)]
    config: CrossAttentionConfig,
    gated_config: GatedAttentionConfig,
    base_attention: MultiHeadCrossAttention,
    query_gate: Linear,
    key_gate: Option<Linear>,
    value_gate: Option<Linear>,
    gate_norm: LayerNorm,
}

impl GatedCrossAttention {
    pub fn new(config: CrossAttentionConfig) -> Result<Self> {
        let gated_config = config.gated_config.clone().unwrap_or_default();
        let base_attention = MultiHeadCrossAttention::new(config.clone())?;

        let gate_hidden_dim = gated_config.gate_hidden_dim.unwrap_or(config.hidden_size);

        let query_gate = Linear::new(config.hidden_size, gate_hidden_dim, gated_config.gate_bias);

        let key_gate = if gated_config.separate_gates {
            Some(Linear::new(
                config.hidden_size,
                gate_hidden_dim,
                gated_config.gate_bias,
            ))
        } else {
            None
        };

        let value_gate = if gated_config.separate_gates {
            Some(Linear::new(
                config.hidden_size,
                gate_hidden_dim,
                gated_config.gate_bias,
            ))
        } else {
            None
        };

        let gate_norm = LayerNorm::new(vec![gate_hidden_dim], 1e-5)?;

        Ok(Self {
            config,
            gated_config,
            base_attention,
            query_gate,
            key_gate,
            value_gate,
            gate_norm,
        })
    }

    pub fn forward(
        &self,
        query: Tensor,
        key: Tensor,
        value: Tensor,
        mask: Option<Tensor>,
    ) -> Result<CrossAttentionOutput> {
        // Compute gates
        let query_gate_out = self.query_gate.forward(query.clone())?;
        let query_gate_out = self.gate_norm.forward(query_gate_out)?;
        let query_gate_out =
            apply_gate_activation(&query_gate_out, &self.gated_config.gate_activation)?;

        let key_gate_out = if let Some(key_gate) = &self.key_gate {
            let gate_out = key_gate.forward(key.clone())?;
            let gate_out = self.gate_norm.forward(gate_out)?;
            apply_gate_activation(&gate_out, &self.gated_config.gate_activation)?
        } else {
            query_gate_out.clone()
        };

        let value_gate_out = if let Some(value_gate) = &self.value_gate {
            let gate_out = value_gate.forward(value.clone())?;
            let gate_out = self.gate_norm.forward(gate_out)?;
            apply_gate_activation(&gate_out, &self.gated_config.gate_activation)?
        } else {
            query_gate_out.clone()
        };

        // Apply gates
        let gated_query = query.mul(&query_gate_out)?;
        let gated_key = key.mul(&key_gate_out)?;
        let gated_value = value.mul(&value_gate_out)?;

        // Apply attention
        self.base_attention.forward(gated_query, gated_key, gated_value, mask)
    }
}

// Helper functions

fn apply_gate_activation(tensor: &Tensor, activation: &GateActivation) -> Result<Tensor> {
    match activation {
        GateActivation::Sigmoid => sigmoid(tensor),
        GateActivation::Tanh => tanh(tensor),
        GateActivation::ReLU => tensor.relu(),
        GateActivation::GELU => gelu(tensor),
        GateActivation::Swish => silu(tensor),
    }
}

fn aggregate_weighted_sum(outputs: Vec<Tensor>) -> Result<Tensor> {
    let mut result = outputs[0].clone();
    let weight = 1.0 / outputs.len() as f32;

    for output in outputs.iter().skip(1) {
        result = result.add(&output.mul_scalar(weight)?)?;
    }

    Ok(result)
}

fn aggregate_max_pooling(outputs: Vec<Tensor>) -> Result<Tensor> {
    if outputs.is_empty() {
        return Err(tensor_op_error(
            "tensor_operation",
            "Cannot perform max pooling on empty outputs".to_string(),
        ));
    }

    // Start with the first tensor and compute element-wise maximum with others
    let mut result = outputs[0].clone();
    for output in outputs.iter().skip(1) {
        result = result.max(output)?;
    }
    Ok(result)
}

fn aggregate_avg_pooling(outputs: Vec<Tensor>) -> Result<Tensor> {
    // Element-wise average across tensors
    let mut result = outputs[0].clone();
    for output in outputs.iter().skip(1) {
        result = result.add(output)?;
    }
    result.div_scalar(outputs.len() as f32)
}

fn combine_pattern_outputs(outputs: Vec<Tensor>, weights: Tensor) -> Result<Tensor> {
    if outputs.is_empty() {
        return Err(tensor_op_error(
            "tensor_operation",
            "Cannot combine empty outputs".to_string(),
        ));
    }

    // Weighted combination of pattern outputs
    // weights should have shape [batch_size, num_patterns]
    // outputs should be a vector of tensors with shape [batch_size, seq_len, hidden_size]
    let mut result = outputs[0].mul_scalar(0.0)?; // Initialize with zeros of the right shape

    for (i, output) in outputs.iter().enumerate() {
        // Extract weight for this pattern and expand to match output dimensions
        let pattern_weight = weights.select(1, i as i64)?; // Select weight for pattern i
        let expanded_weight = pattern_weight.unsqueeze(1)?.unsqueeze(2)?; // Expand to [batch, 1, 1]

        // Apply weight and add to result
        let weighted_output = output.mul(&expanded_weight)?;
        result = result.add(&weighted_output)?;
    }

    Ok(result)
}