trustformers-mobile 0.1.1

Mobile deployment support for TrustformeRS (iOS, Android)
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
//! Operator Fusion Module
//!
//! Fuses multiple operators into single optimized kernels for mobile execution.
//! Common fusion patterns include:
//! - Conv + BatchNorm + ReLU
//! - Linear + Activation
//! - Multi-head Attention components

use super::{ComputationGraph, GraphOperator, KernelType};
use crate::MobileBackend;
use std::collections::{HashMap, HashSet};
use trustformers_core::error::Result;
use trustformers_core::Tensor;

/// Fusion pattern types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FusionPattern {
    ConvBatchNorm,
    ConvBatchNormRelu,
    LinearActivation,
    MultiHeadAttention,
    LayerNormLinear,
    GeLULinear,
    ResidualBlock,
    TransformerBlock,
}

/// Fusion context for tracking fusion opportunities
#[derive(Debug)]
pub struct FusionContext {
    pub fused_operators: HashMap<usize, FusedOperator>,
    pub fusion_groups: Vec<Vec<usize>>,
    pub performance_gains: HashMap<FusionPattern, f32>,
}

impl Default for FusionContext {
    fn default() -> Self {
        let mut performance_gains = HashMap::new();
        // Estimated performance gains for each fusion pattern
        performance_gains.insert(FusionPattern::ConvBatchNorm, 1.2);
        performance_gains.insert(FusionPattern::ConvBatchNormRelu, 1.4);
        performance_gains.insert(FusionPattern::LinearActivation, 1.15);
        performance_gains.insert(FusionPattern::MultiHeadAttention, 1.5);
        performance_gains.insert(FusionPattern::LayerNormLinear, 1.25);
        performance_gains.insert(FusionPattern::GeLULinear, 1.2);
        performance_gains.insert(FusionPattern::ResidualBlock, 1.3);
        performance_gains.insert(FusionPattern::TransformerBlock, 1.6);

        Self {
            fused_operators: HashMap::new(),
            fusion_groups: Vec::new(),
            performance_gains,
        }
    }
}

/// Fused operator representation
#[derive(Debug, Clone)]
pub struct FusedOperator {
    pub pattern: FusionPattern,
    pub operators: Vec<usize>,
    pub fused_kernel: KernelType,
    pub estimated_speedup: f32,
}

/// Operator fusion engine
pub struct OperatorFusion {
    backend: MobileBackend,
    context: FusionContext,
    enable_aggressive_fusion: bool,
}

impl OperatorFusion {
    /// Create new fusion engine for specific backend
    pub fn new(backend: MobileBackend) -> Self {
        Self {
            backend,
            context: FusionContext::default(),
            enable_aggressive_fusion: true,
        }
    }

    /// Detect fusion opportunities in the graph
    pub fn detect_fusion_opportunities(
        &mut self,
        graph: &ComputationGraph,
    ) -> Result<Vec<FusionPattern>> {
        let mut patterns = Vec::new();
        let mut visited = HashSet::new();

        for (idx, operator) in graph.operators.iter().enumerate() {
            if visited.contains(&idx) {
                continue;
            }

            // Check Conv + BatchNorm patterns
            if matches!(operator.kernel, KernelType::Conv2d) {
                if let Some(pattern) = self.check_conv_patterns(graph, idx, &mut visited) {
                    patterns.push(pattern);
                }
            }

            // Check Linear + Activation patterns
            if matches!(operator.kernel, KernelType::Linear) {
                if let Some(pattern) = self.check_linear_patterns(graph, idx, &mut visited) {
                    patterns.push(pattern);
                }
            }

            // Check Attention patterns
            if matches!(operator.kernel, KernelType::Attention) {
                if let Some(pattern) = self.check_attention_patterns(graph, idx, &mut visited) {
                    patterns.push(pattern);
                }
            }

            // Check Transformer block patterns
            if self.enable_aggressive_fusion {
                if let Some(pattern) = self.check_transformer_patterns(graph, idx, &mut visited) {
                    patterns.push(pattern);
                }
            }
        }

        Ok(patterns)
    }

    /// Fuse Conv + BatchNorm operators
    pub fn fuse_conv_batchnorm(&mut self, graph: &mut ComputationGraph) -> Result<()> {
        let mut fusion_groups = Vec::new();

        // Find Conv->BatchNorm sequences
        for (idx, operator) in graph.operators.iter().enumerate() {
            if matches!(operator.kernel, KernelType::Conv2d) {
                if let Some(next_idx) = self.find_next_operator(graph, idx) {
                    if matches!(graph.operators[next_idx].kernel, KernelType::BatchNorm) {
                        // Check if we can also fuse ReLU
                        if let Some(relu_idx) = self.find_next_operator(graph, next_idx) {
                            if matches!(graph.operators[relu_idx].kernel, KernelType::Activation) {
                                fusion_groups.push(vec![idx, next_idx, relu_idx]);
                            } else {
                                fusion_groups.push(vec![idx, next_idx]);
                            }
                        } else {
                            fusion_groups.push(vec![idx, next_idx]);
                        }
                    }
                }
            }
        }

        // Apply fusion
        for group in fusion_groups {
            self.apply_fusion(graph, group, FusionPattern::ConvBatchNormRelu)?;
        }

        Ok(())
    }

    /// Fuse Linear + Activation operators
    pub fn fuse_linear_activation(&mut self, graph: &mut ComputationGraph) -> Result<()> {
        let mut fusion_groups = Vec::new();

        // Find Linear->Activation sequences
        for (idx, operator) in graph.operators.iter().enumerate() {
            if matches!(operator.kernel, KernelType::Linear) {
                if let Some(next_idx) = self.find_next_operator(graph, idx) {
                    if matches!(graph.operators[next_idx].kernel, KernelType::Activation) {
                        fusion_groups.push(vec![idx, next_idx]);
                    }
                }
            }
        }

        // Apply fusion
        for group in fusion_groups {
            self.apply_fusion(graph, group, FusionPattern::LinearActivation)?;
        }

        Ok(())
    }

    /// Fuse multi-head attention components
    pub fn fuse_attention(&mut self, graph: &mut ComputationGraph) -> Result<()> {
        let mut fusion_groups = Vec::new();

        // Find attention patterns (Q, K, V projections + attention)
        for (idx, operator) in graph.operators.iter().enumerate() {
            if matches!(operator.kernel, KernelType::Linear) {
                // Look for Q, K, V projection pattern
                if let Some(pattern_ops) = self.detect_qkv_pattern(graph, idx) {
                    fusion_groups.push(pattern_ops);
                }
            }
        }

        // Apply fusion
        for group in fusion_groups {
            self.apply_fusion(graph, group, FusionPattern::MultiHeadAttention)?;
        }

        Ok(())
    }

    // Private helper methods

    fn check_conv_patterns(
        &self,
        graph: &ComputationGraph,
        idx: usize,
        visited: &mut HashSet<usize>,
    ) -> Option<FusionPattern> {
        if let Some(next_idx) = self.find_next_operator(graph, idx) {
            if matches!(graph.operators[next_idx].kernel, KernelType::BatchNorm) {
                visited.insert(idx);
                visited.insert(next_idx);

                // Check for ReLU after BatchNorm
                if let Some(relu_idx) = self.find_next_operator(graph, next_idx) {
                    if matches!(graph.operators[relu_idx].kernel, KernelType::Activation) {
                        visited.insert(relu_idx);
                        return Some(FusionPattern::ConvBatchNormRelu);
                    }
                }

                return Some(FusionPattern::ConvBatchNorm);
            }
        }
        None
    }

    fn check_linear_patterns(
        &self,
        graph: &ComputationGraph,
        idx: usize,
        visited: &mut HashSet<usize>,
    ) -> Option<FusionPattern> {
        if let Some(next_idx) = self.find_next_operator(graph, idx) {
            match &graph.operators[next_idx].kernel {
                KernelType::Activation => {
                    visited.insert(idx);
                    visited.insert(next_idx);

                    // Check if it's specifically GeLU
                    if self.is_gelu_activation(&graph.operators[next_idx]) {
                        return Some(FusionPattern::GeLULinear);
                    }

                    return Some(FusionPattern::LinearActivation);
                },
                KernelType::Custom(name) if name == "LayerNorm" => {
                    visited.insert(idx);
                    visited.insert(next_idx);
                    return Some(FusionPattern::LayerNormLinear);
                },
                _ => {},
            }
        }
        None
    }

    fn check_attention_patterns(
        &self,
        graph: &ComputationGraph,
        idx: usize,
        visited: &mut HashSet<usize>,
    ) -> Option<FusionPattern> {
        // Simple check for attention operator
        if matches!(graph.operators[idx].kernel, KernelType::Attention) {
            visited.insert(idx);
            return Some(FusionPattern::MultiHeadAttention);
        }
        None
    }

    fn check_transformer_patterns(
        &self,
        graph: &ComputationGraph,
        idx: usize,
        visited: &mut HashSet<usize>,
    ) -> Option<FusionPattern> {
        // Look for transformer block pattern:
        // LayerNorm -> Attention -> Residual -> LayerNorm -> FFN -> Residual
        if let Some(name) = self.get_custom_kernel_name(&graph.operators[idx].kernel) {
            if name == "LayerNorm" {
                // This is a simplified check - in practice would be more sophisticated
                let block_size = 6; // Typical transformer block has ~6 operators
                if idx + block_size < graph.operators.len() {
                    // Mark all operators in the block as visited
                    for i in idx..idx + block_size {
                        visited.insert(i);
                    }
                    return Some(FusionPattern::TransformerBlock);
                }
            }
        }
        None
    }

    fn find_next_operator(&self, graph: &ComputationGraph, current_idx: usize) -> Option<usize> {
        // Find the operator that uses the output of current operator
        let current_output = &graph.operators[current_idx].outputs[0];

        for (idx, operator) in graph.operators.iter().enumerate() {
            if operator.inputs.contains(current_output) {
                return Some(idx);
            }
        }

        None
    }

    fn detect_qkv_pattern(&self, graph: &ComputationGraph, start_idx: usize) -> Option<Vec<usize>> {
        // Simplified QKV pattern detection
        // Look for three consecutive linear layers with similar shapes
        if start_idx + 2 >= graph.operators.len() {
            return None;
        }

        let mut indices = vec![start_idx];

        for i in 1..3 {
            if matches!(graph.operators[start_idx + i].kernel, KernelType::Linear) {
                indices.push(start_idx + i);
            } else {
                return None;
            }
        }

        Some(indices)
    }

    fn apply_fusion(
        &mut self,
        graph: &mut ComputationGraph,
        operator_indices: Vec<usize>,
        pattern: FusionPattern,
    ) -> Result<()> {
        // Create fused operator
        let fused_kernel = self.create_fused_kernel(&pattern, &operator_indices, graph)?;

        let fused_op = FusedOperator {
            pattern: pattern.clone(),
            operators: operator_indices.clone(),
            fused_kernel: fused_kernel.clone(),
            estimated_speedup: self.context.performance_gains[&pattern],
        };

        // Replace operators in graph with fused version
        // This is simplified - in practice would need careful graph manipulation
        let first_idx = operator_indices[0];
        graph.operators[first_idx].kernel = fused_kernel;

        // Mark other operators for removal (simplified)
        for &idx in &operator_indices[1..] {
            graph.operators[idx].kernel = KernelType::Custom("_removed_".to_string());
        }

        // Store fusion info
        self.context.fused_operators.insert(first_idx, fused_op);
        self.context.fusion_groups.push(operator_indices);

        Ok(())
    }

    fn create_fused_kernel(
        &self,
        pattern: &FusionPattern,
        indices: &[usize],
        graph: &ComputationGraph,
    ) -> Result<KernelType> {
        match pattern {
            FusionPattern::ConvBatchNorm => Ok(KernelType::Custom("ConvBN".to_string())),
            FusionPattern::ConvBatchNormRelu => Ok(KernelType::Custom("ConvBNReLU".to_string())),
            FusionPattern::LinearActivation => Ok(KernelType::Custom("LinearAct".to_string())),
            FusionPattern::GeLULinear => Ok(KernelType::Custom("LinearGeLU".to_string())),
            FusionPattern::MultiHeadAttention => Ok(KernelType::Custom("MHA".to_string())),
            FusionPattern::LayerNormLinear => Ok(KernelType::Custom("LNLinear".to_string())),
            FusionPattern::ResidualBlock => Ok(KernelType::Custom("ResBlock".to_string())),
            FusionPattern::TransformerBlock => {
                Ok(KernelType::Custom("TransformerBlock".to_string()))
            },
        }
    }

    fn is_gelu_activation(&self, operator: &GraphOperator) -> bool {
        // Check if the activation is GELU
        // This is simplified - would check operator attributes
        matches!(operator.kernel, KernelType::Activation)
    }

    fn get_custom_kernel_name<'a>(&self, kernel: &'a KernelType) -> Option<&'a str> {
        match kernel {
            KernelType::Custom(name) => Some(name),
            _ => None,
        }
    }
}

/// Conv + BatchNorm fusion implementation
pub struct ConvBatchNormFusion;

impl ConvBatchNormFusion {
    /// Fuse Conv2D weights with BatchNorm parameters
    pub fn fuse_weights(
        conv_weight: &Tensor,
        conv_bias: Option<&Tensor>,
        bn_scale: &Tensor,
        bn_bias: &Tensor,
        bn_mean: &Tensor,
        bn_var: &Tensor,
        epsilon: f32,
    ) -> Result<(Tensor, Tensor)> {
        let channels = bn_scale.shape()[0];

        // Compute fused weights
        let mut fused_weight = conv_weight.clone();
        let mut fused_bias = if let Some(bias) = conv_bias {
            bias.clone()
        } else {
            Tensor::zeros(&[channels])?
        };

        // Fuse parameters: W' = W * scale / sqrt(var + eps)
        // b' = (bias - mean) * scale / sqrt(var + eps) + bn_bias
        let scale_data = bn_scale.data()?;
        let var_data = bn_var.data()?;
        let mean_data = bn_mean.data()?;
        let bias_data = bn_bias.data()?;

        for c in 0..channels {
            let scale = scale_data[c];
            let var = var_data[c];
            let mean = mean_data[c];
            let bn_b = bias_data[c];

            let factor = scale / (var + epsilon).sqrt();

            // Update weights for this channel
            // This is simplified - would need proper tensor indexing
            let mut weight_data = fused_weight.data()?;
            for i in 0..weight_data.len() / channels {
                weight_data[c + i * channels] *= factor;
            }
            fused_weight = Tensor::from_vec(weight_data, &fused_weight.shape())?;

            // Update bias
            let mut bias_data = fused_bias.data()?;
            bias_data[c] = (bias_data[c] - mean) * factor + bn_b;
            fused_bias = Tensor::from_vec(bias_data, &fused_bias.shape())?;
        }

        Ok((fused_weight, fused_bias))
    }
}

/// Linear + Activation fusion implementation
pub struct LinearActivationFusion;

impl LinearActivationFusion {
    /// Create fused linear + activation kernel
    pub fn create_fused_kernel(activation_type: &str) -> KernelType {
        match activation_type {
            "relu" => KernelType::Custom("LinearReLU".to_string()),
            "gelu" => KernelType::Custom("LinearGeLU".to_string()),
            "silu" => KernelType::Custom("LinearSiLU".to_string()),
            _ => KernelType::Custom(format!("Linear{}", activation_type)),
        }
    }
}

/// Attention fusion implementation
pub struct AttentionFusion;

impl AttentionFusion {
    /// Fuse QKV projections into single operation
    pub fn fuse_qkv_projections(
        q_weight: &Tensor,
        k_weight: &Tensor,
        v_weight: &Tensor,
        q_bias: Option<&Tensor>,
        k_bias: Option<&Tensor>,
        v_bias: Option<&Tensor>,
    ) -> Result<(Tensor, Option<Tensor>)> {
        // Concatenate weights along output dimension
        let weights = vec![q_weight.clone(), k_weight.clone(), v_weight.clone()];
        let fused_weight = Tensor::concat(&weights, 0)?;

        // Concatenate biases if present
        let fused_bias = if let (Some(qb), Some(kb), Some(vb)) = (q_bias, k_bias, v_bias) {
            let biases = vec![qb.clone(), kb.clone(), vb.clone()];
            Some(Tensor::concat(&biases, 0)?)
        } else {
            None
        };

        Ok((fused_weight, fused_bias))
    }
}

/// Fusion statistics
#[derive(Debug)]
pub struct FusionStats {
    pub patterns_detected: HashMap<FusionPattern, usize>,
    pub operators_fused: usize,
    pub estimated_speedup: f32,
    pub memory_saved_bytes: usize,
}

impl Default for FusionStats {
    fn default() -> Self {
        Self {
            patterns_detected: HashMap::new(),
            operators_fused: 0,
            estimated_speedup: 1.0, // Start at baseline (no speedup)
            memory_saved_bytes: 0,
        }
    }
}

impl FusionStats {
    /// Update statistics with new fusion
    pub fn record_fusion(&mut self, pattern: &FusionPattern, operators_count: usize) {
        *self.patterns_detected.entry(pattern.clone()).or_insert(0) += 1;
        self.operators_fused += operators_count;

        // Estimate speedup (simplified)
        let pattern_speedup = match pattern {
            FusionPattern::ConvBatchNormRelu => 1.4,
            FusionPattern::MultiHeadAttention => 1.5,
            FusionPattern::TransformerBlock => 1.6,
            _ => 1.2,
        };

        // Multiplicative speedup accumulation (more realistic for independent optimizations)
        self.estimated_speedup *= pattern_speedup;

        // Estimate memory saved by eliminating intermediate tensors
        self.memory_saved_bytes += operators_count * 1024 * 1024; // 1MB per op (estimate)
    }

    /// Get summary of fusion optimizations
    pub fn summary(&self) -> String {
        format!(
            "Fusion Statistics:\n\
             - Patterns detected: {:?}\n\
             - Operators fused: {}\n\
             - Estimated speedup: {:.1}x\n\
             - Memory saved: {} MB",
            self.patterns_detected,
            self.operators_fused,
            self.estimated_speedup,
            self.memory_saved_bytes / (1024 * 1024)
        )
    }
}

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

    #[test]
    fn test_fusion_pattern_detection() {
        let fusion = OperatorFusion::new(MobileBackend::CPU);

        // Create simple graph
        let graph = ComputationGraph {
            operators: vec![
                GraphOperator {
                    id: 0,
                    kernel: KernelType::Conv2d,
                    inputs: vec!["input".to_string()],
                    outputs: vec!["conv_out".to_string()],
                    input_shapes: vec![vec![1, 3, 224, 224]],
                    output_shape: vec![1, 64, 112, 112],
                    cache_hints: None,
                },
                GraphOperator {
                    id: 1,
                    kernel: KernelType::BatchNorm,
                    inputs: vec!["conv_out".to_string()],
                    outputs: vec!["bn_out".to_string()],
                    input_shapes: vec![vec![1, 64, 112, 112]],
                    output_shape: vec![1, 64, 112, 112],
                    cache_hints: None,
                },
            ],
            edges: vec![],
        };

        let mut fusion_engine = OperatorFusion::new(MobileBackend::CPU);
        let patterns = fusion_engine.detect_fusion_opportunities(&graph).expect("Operation failed");

        assert!(!patterns.is_empty());
        assert!(patterns.contains(&FusionPattern::ConvBatchNorm));
    }

    #[test]
    fn test_conv_bn_weight_fusion() {
        let conv_weight = Tensor::ones(&[64, 3, 3, 3]).expect("Operation failed");
        let conv_bias = Some(Tensor::zeros(&[64]).expect("Operation failed"));
        let bn_scale = Tensor::ones(&[64]).expect("Operation failed");
        let bn_bias = Tensor::zeros(&[64]).expect("Operation failed");
        let bn_mean = Tensor::zeros(&[64]).expect("Operation failed");
        let bn_var = Tensor::ones(&[64]).expect("Operation failed");

        let (fused_weight, fused_bias) = ConvBatchNormFusion::fuse_weights(
            &conv_weight,
            conv_bias.as_ref(),
            &bn_scale,
            &bn_bias,
            &bn_mean,
            &bn_var,
            1e-5,
        )
        .expect("Operation failed");

        assert_eq!(fused_weight.shape(), conv_weight.shape());
        assert_eq!(fused_bias.shape(), &[64]);
    }

    #[test]
    fn test_fusion_stats() {
        let mut stats = FusionStats::default();

        stats.record_fusion(&FusionPattern::ConvBatchNormRelu, 3);
        stats.record_fusion(&FusionPattern::LinearActivation, 2);

        assert_eq!(stats.operators_fused, 5);
        assert!(stats.estimated_speedup > 1.0);
        assert!(stats.memory_saved_bytes > 0);

        let summary = stats.summary();
        assert!(summary.contains("Operators fused: 5"));
    }
}