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
use trustformers_core::{errors::Result, tensor::Tensor};

/// Output structure for cross-attention operations
#[derive(Debug, Clone)]
pub struct CrossAttentionOutput {
    /// Attention output tensor
    pub output: Tensor,
    /// Attention weights (optional)
    pub attention_weights: Option<Tensor>,
    /// Attention statistics (optional)
    pub attention_stats: Option<AttentionStats>,
}

/// Statistics about attention patterns
#[derive(Debug, Clone)]
pub struct AttentionStats {
    /// Average attention entropy
    pub entropy: f32,
    /// Maximum attention weight
    pub max_weight: f32,
    /// Minimum attention weight
    pub min_weight: f32,
    /// Attention sparsity ratio
    pub sparsity: f32,
    /// Head-wise statistics
    pub head_stats: Vec<HeadStats>,
}

/// Per-head attention statistics
#[derive(Debug, Clone)]
pub struct HeadStats {
    /// Head index
    pub head_idx: usize,
    /// Head entropy
    pub entropy: f32,
    /// Head sparsity
    pub sparsity: f32,
    /// Most attended positions
    pub top_positions: Vec<usize>,
}

/// Create attention mask for cross-attention
pub fn create_attention_mask(
    query_len: usize,
    key_len: usize,
    mask_type: MaskType,
) -> Result<Tensor> {
    match mask_type {
        MaskType::None => {
            // No masking - all positions visible
            Ok(Tensor::zeros(&[query_len, key_len])?)
        },
        MaskType::Causal => {
            // Causal mask - can only attend to previous positions
            let mut mask = vec![vec![0.0f32; key_len]; query_len];
            for (i, row) in mask.iter_mut().enumerate() {
                for j in (i + 1)..key_len.min(query_len) {
                    row[j] = f32::NEG_INFINITY;
                }
            }
            let flattened: Vec<f32> = mask.into_iter().flatten().collect();
            Ok(Tensor::from_vec(flattened, &[query_len, key_len])?)
        },
        MaskType::Local(window_size) => {
            // Local attention mask - only attend to nearby positions
            let mut mask = vec![vec![f32::NEG_INFINITY; key_len]; query_len];
            for (i, row) in mask.iter_mut().enumerate() {
                let start = i.saturating_sub(window_size / 2);
                let end = (i + window_size / 2 + 1).min(key_len);
                for j in start..end {
                    row[j] = 0.0;
                }
            }
            let flattened: Vec<f32> = mask.into_iter().flatten().collect();
            Ok(Tensor::from_vec(flattened, &[query_len, key_len])?)
        },
        MaskType::Custom(custom_mask) => Ok(custom_mask),
    }
}

/// Types of attention masks
#[derive(Debug, Clone)]
pub enum MaskType {
    /// No masking
    None,
    /// Causal masking for autoregressive models
    Causal,
    /// Local window masking
    Local(usize),
    /// Custom mask tensor
    Custom(Tensor),
}

/// Create sparse attention mask
pub fn create_sparse_mask(
    query_len: usize,
    key_len: usize,
    sparsity_ratio: f32,
    pattern: SparsePattern,
) -> Result<Tensor> {
    match pattern {
        SparsePattern::Random => create_random_sparse_mask(query_len, key_len, sparsity_ratio),
        SparsePattern::Block(block_size) => {
            create_block_sparse_mask(query_len, key_len, block_size)
        },
        SparsePattern::Strided(stride) => create_strided_sparse_mask(query_len, key_len, stride),
        SparsePattern::TopK(k) => create_topk_sparse_mask(query_len, key_len, k),
    }
}

/// Sparse attention patterns
#[derive(Debug, Clone)]
pub enum SparsePattern {
    /// Random sparse connections
    Random,
    /// Block-based sparse connections
    Block(usize),
    /// Strided sparse connections
    Strided(usize),
    /// Top-k sparse connections
    TopK(usize),
}

fn create_random_sparse_mask(
    query_len: usize,
    key_len: usize,
    sparsity_ratio: f32,
) -> Result<Tensor> {
    let mut mask = vec![vec![f32::NEG_INFINITY; key_len]; query_len];
    let keep_ratio = 1.0 - sparsity_ratio;

    for (i, row) in mask.iter_mut().enumerate() {
        for (j, val) in row.iter_mut().enumerate() {
            // Simplified random selection - in practice would use proper RNG
            if (i + j) % 10 < (keep_ratio * 10.0) as usize {
                *val = 0.0;
            }
        }
    }

    let flattened: Vec<f32> = mask.into_iter().flatten().collect();
    Tensor::from_vec(flattened, &[query_len, key_len])
}

fn create_block_sparse_mask(query_len: usize, key_len: usize, block_size: usize) -> Result<Tensor> {
    let mut mask = vec![vec![f32::NEG_INFINITY; key_len]; query_len];

    for (i, row) in mask.iter_mut().enumerate() {
        for (j, val) in row.iter_mut().enumerate() {
            let qi = i / block_size;
            let kj = j / block_size;

            // Allow attention within blocks and to adjacent blocks
            if qi == kj || qi.abs_diff(kj) <= 1 {
                *val = 0.0;
            }
        }
    }

    let flattened: Vec<f32> = mask.into_iter().flatten().collect();
    Tensor::from_vec(flattened, &[query_len, key_len])
}

fn create_strided_sparse_mask(query_len: usize, key_len: usize, stride: usize) -> Result<Tensor> {
    let mut mask = vec![vec![f32::NEG_INFINITY; key_len]; query_len];

    for (i, row) in mask.iter_mut().enumerate() {
        for (j, val) in row.iter_mut().enumerate() {
            // Allow attention to positions at regular intervals
            if j % stride == i % stride {
                *val = 0.0;
            }
        }
    }

    let flattened: Vec<f32> = mask.into_iter().flatten().collect();
    Tensor::from_vec(flattened, &[query_len, key_len])
}

fn create_topk_sparse_mask(query_len: usize, key_len: usize, k: usize) -> Result<Tensor> {
    let mut mask = vec![vec![f32::NEG_INFINITY; key_len]; query_len];

    for (i, row) in mask.iter_mut().enumerate() {
        // Allow attention to k nearest positions
        let start = i.saturating_sub(k / 2);
        let end = (i + k / 2 + 1).min(key_len);

        for j in start..end {
            row[j] = 0.0;
        }
    }

    let flattened: Vec<f32> = mask.into_iter().flatten().collect();
    Tensor::from_vec(flattened, &[query_len, key_len])
}

/// Create hierarchical attention mask
pub fn create_hierarchical_mask(
    query_len: usize,
    key_len: usize,
    num_levels: usize,
    pooling_factor: usize,
) -> Result<Vec<Tensor>> {
    let mut masks = Vec::new();

    for level in 0..num_levels {
        let level_pooling = pooling_factor.pow(level as u32);
        let level_query_len = query_len.div_ceil(level_pooling);
        let level_key_len = key_len.div_ceil(level_pooling);

        let mask = create_attention_mask(level_query_len, level_key_len, MaskType::None)?;
        masks.push(mask);
    }

    Ok(masks)
}

/// Compute attention statistics
pub fn compute_attention_stats(
    attention_weights: &Tensor,
    num_heads: usize,
) -> Result<AttentionStats> {
    let shape = attention_weights.shape();
    let _batch_size = shape[0];
    let _seq_len = shape[2];

    // Compute overall statistics
    let entropy = compute_entropy(attention_weights)?;
    let (min_weight, max_weight) = compute_min_max(attention_weights)?;
    let sparsity = compute_sparsity(attention_weights, 1e-6)?;

    // Compute per-head statistics
    let mut head_stats = Vec::new();
    for head in 0..num_heads {
        // Select the specific head weights from the attention tensor
        // Shape: [batch_size, num_heads, seq_len, seq_len] -> [batch_size, seq_len, seq_len]
        let head_weights = attention_weights.select(1, head as i64)?;
        let head_entropy = compute_entropy(&head_weights)?;
        let head_sparsity = compute_sparsity(&head_weights, 1e-6)?;
        let top_positions = compute_top_positions(&head_weights, 5)?;

        head_stats.push(HeadStats {
            head_idx: head,
            entropy: head_entropy,
            sparsity: head_sparsity,
            top_positions,
        });
    }

    Ok(AttentionStats {
        entropy,
        max_weight,
        min_weight,
        sparsity,
        head_stats,
    })
}

fn compute_entropy(_tensor: &Tensor) -> Result<f32> {
    // Simplified entropy computation
    // In practice, this would use proper entropy calculation
    Ok(0.5) // Placeholder
}

fn compute_min_max(_tensor: &Tensor) -> Result<(f32, f32)> {
    // Simplified min/max computation
    Ok((0.0, 1.0)) // Placeholder
}

fn compute_sparsity(_tensor: &Tensor, _threshold: f32) -> Result<f32> {
    // Simplified sparsity computation
    Ok(0.1) // Placeholder
}

fn compute_top_positions(_tensor: &Tensor, _k: usize) -> Result<Vec<usize>> {
    // Simplified top-k computation
    Ok(vec![0, 1, 2, 3, 4]) // Placeholder
}

/// Apply attention dropout
pub fn apply_attention_dropout(
    attention_weights: Tensor,
    dropout_rate: f32,
    training: bool,
) -> Result<Tensor> {
    if training && dropout_rate > 0.0 {
        attention_weights.dropout(dropout_rate)
    } else {
        Ok(attention_weights)
    }
}

/// Compute scaled dot-product attention
pub fn scaled_dot_product_attention(
    query: &Tensor,
    key: &Tensor,
    value: &Tensor,
    mask: Option<&Tensor>,
    scale: f32,
    dropout_rate: f32,
    training: bool,
) -> Result<CrossAttentionOutput> {
    // Compute attention scores
    let key_shape = key.shape();
    let dim0 = key_shape.len().saturating_sub(2);
    let dim1 = key_shape.len().saturating_sub(1);
    let scores = query.matmul(&key.transpose(dim0, dim1)?)?;
    let scores = scores.mul_scalar(scale)?;

    // Apply mask if provided
    let scores = if let Some(mask) = mask { scores.add(mask)? } else { scores };

    // Apply softmax
    let attention_weights = scores.softmax(-1)?;

    // Apply dropout
    let attention_weights = apply_attention_dropout(attention_weights, dropout_rate, training)?;

    // Apply attention to values
    let output = attention_weights.matmul(value)?;

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

/// Reshape tensor for multi-head attention
pub fn reshape_for_multihead(tensor: Tensor, num_heads: usize, head_dim: usize) -> Result<Tensor> {
    let shape = tensor.shape();
    let batch_size = shape[0];
    let seq_len = shape[1];

    tensor.reshape(&[batch_size, seq_len, num_heads, head_dim])?.transpose(1, 2)
}

/// Reshape tensor back from multi-head attention
pub fn reshape_from_multihead(tensor: Tensor, hidden_size: usize) -> Result<Tensor> {
    let shape = tensor.shape();
    let batch_size = shape[0];
    let seq_len = shape[2];

    tensor.transpose(1, 2)?.reshape(&[batch_size, seq_len, hidden_size])
}

/// Pool tensor for hierarchical attention
pub fn pool_tensor(tensor: Tensor, pooling_factor: usize, method: PoolingMethod) -> Result<Tensor> {
    match method {
        PoolingMethod::Average => average_pool_1d(tensor, pooling_factor),
        PoolingMethod::Max => max_pool_1d(tensor, pooling_factor),
        PoolingMethod::Learnable => {
            // Placeholder for learnable pooling
            average_pool_1d(tensor, pooling_factor)
        },
    }
}

/// Pooling methods for hierarchical attention
#[derive(Debug, Clone)]
pub enum PoolingMethod {
    /// Average pooling
    Average,
    /// Max pooling
    Max,
    /// Learnable pooling
    Learnable,
}

fn average_pool_1d(tensor: Tensor, _pooling_factor: usize) -> Result<Tensor> {
    // Simplified average pooling
    // In practice, this would use proper pooling operations
    Ok(tensor)
}

fn max_pool_1d(tensor: Tensor, _pooling_factor: usize) -> Result<Tensor> {
    // Simplified max pooling
    // In practice, this would use proper pooling operations
    Ok(tensor)
}

/// Interpolate tensor for hierarchical attention
pub fn interpolate_tensor(
    tensor: Tensor,
    target_length: usize,
    method: InterpolationMethod,
) -> Result<Tensor> {
    match method {
        InterpolationMethod::Linear => linear_interpolate(tensor, target_length),
        InterpolationMethod::Nearest => nearest_interpolate(tensor, target_length),
    }
}

/// Interpolation methods
#[derive(Debug, Clone)]
pub enum InterpolationMethod {
    /// Linear interpolation
    Linear,
    /// Nearest neighbor interpolation
    Nearest,
}

fn linear_interpolate(tensor: Tensor, _target_length: usize) -> Result<Tensor> {
    // Simplified linear interpolation
    Ok(tensor)
}

fn nearest_interpolate(tensor: Tensor, _target_length: usize) -> Result<Tensor> {
    // Simplified nearest neighbor interpolation
    Ok(tensor)
}