trustformers-debug 0.1.2

Advanced debugging tools for TrustformeRS models
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
//! Neural Network Debugging Demo
#![allow(unused_variables)]
//!
//! This example demonstrates the advanced neural network debugging capabilities
//! for transformer models and attention mechanisms in TrustformeRS Debug.

use anyhow::Result;
use scirs2_core::ndarray::{Array2, ArrayD};
use trustformers_debug::*;

#[tokio::main]
async fn main() -> Result<()> {
    println!("🚀 TrustformeRS Neural Network Debugging Demo");
    println!("==============================================");

    // Demo 1: Basic attention debugging with convenience macro
    demo_attention_debugging().await?;

    // Demo 2: Full transformer debugging
    demo_transformer_debugging().await?;

    // Demo 3: Integration with main debugging session
    demo_integrated_debugging().await?;

    println!("✅ Neural network debugging demo completed successfully!");
    Ok(())
}

/// Demonstrate attention mechanism debugging
#[allow(clippy::excessive_nesting)]
async fn demo_attention_debugging() -> Result<()> {
    println!("\n📊 Demo 1: Attention Mechanism Debugging");
    println!("----------------------------------------");

    // Create sample attention weights for a single layer with 8 heads
    let mut attention_weights = Vec::new();
    for head in 0..8 {
        // Create a 64x64 attention matrix with different patterns for each head
        let mut weights = Array2::<f32>::zeros((64, 64));

        match head {
            0 | 1 => {
                // Local attention pattern (diagonal)
                for i in 0..64 {
                    for j in 0..64 {
                        let distance = (i as i32 - j as i32).abs();
                        if distance <= 3 {
                            weights[[i, j]] = 1.0 / (1.0 + distance as f32);
                        }
                    }
                }
            },
            2 | 3 => {
                // Long-range attention pattern
                for i in 0..64 {
                    for j in 0..64 {
                        let distance = (i as i32 - j as i32).abs();
                        if distance > 16 {
                            weights[[i, j]] = 0.8 / (1.0 + (distance as f32 / 10.0));
                        }
                    }
                }
            },
            4 | 5 => {
                // Block-structured attention
                let block_size = 16;
                for i in 0..64 {
                    for j in 0..64 {
                        if (i / block_size) == (j / block_size) {
                            weights[[i, j]] = 0.9;
                        }
                    }
                }
            },
            _ => {
                // Sparse attention pattern
                for i in 0..64 {
                    for j in 0..64 {
                        if (i + j) % 8 == 0 {
                            weights[[i, j]] = 0.7;
                        }
                    }
                }
            },
        }

        // Normalize attention weights (softmax-like)
        let row_sums: Vec<f32> = weights.rows().into_iter().map(|row| row.sum()).collect();

        for i in 0..64 {
            let sum = row_sums[i];
            if sum > 0.0 {
                for j in 0..64 {
                    weights[[i, j]] /= sum;
                }
            }
        }

        // Convert to ArrayD for the debugger
        let weights_d = weights.into_dyn();
        attention_weights.push(weights_d);
    }

    // Create attention debugger
    let mut debugger = AttentionDebugger::new(AttentionDebugConfig::default());

    // Analyze the attention layer
    let layer_analysis = debugger.analyze_attention_layer(0, &attention_weights)?;

    println!("Layer Analysis Results:");
    println!("  • Layer Index: {}", layer_analysis.layer_index);
    println!("  • Number of Heads: {}", layer_analysis.num_heads);
    println!(
        "  • Layer Diversity Score: {:.3}",
        layer_analysis.layer_diversity_score
    );
    println!(
        "  • Overall Redundancy Score: {:.3}",
        layer_analysis.redundancy_analysis.overall_redundancy_score
    );

    println!("\nHead Specializations:");
    for (i, head_analysis) in layer_analysis.head_analyses.iter().enumerate() {
        println!(
            "  • Head {}: {:?} (Importance: {:.3})",
            i, head_analysis.specialization_type, head_analysis.importance_score
        );
    }

    println!("\nAttention Patterns Detected:");
    for (i, attention_map) in layer_analysis.attention_maps.iter().enumerate() {
        println!(
            "  • Head {}: {:?} (Entropy: {:.3}, Sparsity: {:.3})",
            i,
            attention_map.attention_pattern,
            attention_map.attention_entropy,
            attention_map.sparsity_ratio
        );
    }

    // Using the convenience macro
    println!("\nUsing debug_attention! macro:");
    match debug_attention!(&attention_weights) {
        Ok(analysis) => {
            println!(
                "  • Quick analysis completed for {} heads",
                analysis.num_heads
            );
        },
        Err(e) => println!("  • Error in quick analysis: {}", e),
    }

    Ok(())
}

/// Demonstrate full transformer debugging
#[allow(clippy::excessive_nesting)]
async fn demo_transformer_debugging() -> Result<()> {
    println!("\n🔍 Demo 2: Full Transformer Debugging");
    println!("-------------------------------------");

    // Create sample attention weights for multiple layers (simulating a 6-layer transformer)
    let mut model_attention_weights = Vec::new();

    for layer in 0..6 {
        let mut layer_weights = Vec::new();

        // Each layer has 12 attention heads
        for head in 0..12 {
            let mut weights = Array2::<f32>::zeros((32, 32)); // Smaller for demo

            // Create different attention patterns across layers
            match layer {
                0 | 1 => {
                    // Early layers: more local attention
                    for i in 0..32 {
                        for j in 0..32 {
                            let distance = (i as i32 - j as i32).abs();
                            if distance <= 2 {
                                weights[[i, j]] = 1.0 / (1.0 + distance as f32);
                            }
                        }
                    }
                },
                2 | 3 => {
                    // Middle layers: mixed patterns
                    for i in 0..32 {
                        for j in 0..32 {
                            let distance = (i as i32 - j as i32).abs();
                            if head < 6 {
                                // Half heads do local attention
                                if distance <= 3 {
                                    weights[[i, j]] = 0.8 / (1.0 + distance as f32);
                                }
                            } else {
                                // Half heads do long-range attention
                                if distance > 8 {
                                    weights[[i, j]] = 0.6 / (1.0 + (distance as f32 / 5.0));
                                }
                            }
                        }
                    }
                },
                _ => {
                    // Later layers: more global attention
                    for i in 0..32 {
                        for j in 0..32 {
                            weights[[i, j]] = 0.5 + 0.3 * ((i * j) as f32 / 1024.0).sin();
                        }
                    }
                },
            }

            // Normalize
            for i in 0..32 {
                let sum: f32 = (0..32).map(|j| weights[[i, j]]).sum();
                if sum > 0.0 {
                    for j in 0..32 {
                        weights[[i, j]] /= sum;
                    }
                }
            }

            layer_weights.push(weights.into_dyn());
        }

        model_attention_weights.push(layer_weights);
    }

    // Create transformer debugger
    let mut transformer_debugger = TransformerDebugger::new(TransformerDebugConfig::default());

    // Analyze the entire transformer
    let transformer_analysis =
        transformer_debugger.analyze_transformer_attention(&model_attention_weights)?;

    println!("Transformer Analysis Results:");
    println!("  • Total Layers: {}", transformer_analysis.num_layers);
    println!(
        "  • Model Attention Health: {:?}",
        transformer_analysis.model_attention_summary.model_attention_health
    );
    println!(
        "  • Total Heads: {}",
        transformer_analysis.model_attention_summary.total_heads
    );
    println!(
        "  • Average Diversity Score: {:.3}",
        transformer_analysis.model_attention_summary.average_diversity_score
    );
    println!(
        "  • Average Redundancy Score: {:.3}",
        transformer_analysis.model_attention_summary.average_redundancy_score
    );

    if let Some(cross_layer) = &transformer_analysis.cross_layer_analysis {
        println!("\nCross-Layer Analysis:");
        println!(
            "  • Attention Evolution: {:?}",
            cross_layer.attention_evolution.evolution_type
        );
        println!(
            "  • Head Consistency Score: {:.3}",
            cross_layer.head_consistency.consistency_score
        );

        println!("  • Entropy Trend Across Layers:");
        for (layer, entropy) in cross_layer.attention_evolution.entropy_trend.iter().enumerate() {
            println!("    - Layer {}: {:.3}", layer, entropy);
        }

        println!("  • Dominant Pattern Sequence:");
        for (layer, pattern) in
            cross_layer.pattern_progression.dominant_pattern_sequence.iter().enumerate()
        {
            println!("    - Layer {}: {:?}", layer, pattern);
        }
    }

    // Using the convenience macro
    println!("\nUsing debug_transformer! macro:");
    match debug_transformer!(&model_attention_weights) {
        Ok(analysis) => {
            println!(
                "  • Quick transformer analysis completed for {} layers",
                analysis.num_layers
            );
        },
        Err(e) => println!("  • Error in quick transformer analysis: {}", e),
    }

    Ok(())
}

/// Demonstrate integration with main debugging session
async fn demo_integrated_debugging() -> Result<()> {
    println!("\n🔧 Demo 3: Integrated Debugging Session");
    println!("--------------------------------------");

    // Create a debug session with transformer debugging enabled
    let mut session = debug_session_with_transformer();

    println!("Created debug session with transformer debugging enabled");

    // Start the session
    session.start().await?;
    println!("Debug session started");

    // Check if transformer debugger is available
    if let Some(transformer_debugger) = session.transformer_debugger() {
        println!("✅ Transformer debugger is available and configured");

        // You can now access the transformer debugger through the session
        println!(
            "  • Max layers to analyze: {}",
            transformer_debugger.config.max_layers_to_analyze
        );
        println!(
            "  • Max heads to analyze: {}",
            transformer_debugger.config.attention_config.max_heads_to_analyze
        );
        println!(
            "  • Cross-layer analysis enabled: {}",
            transformer_debugger.config.enable_cross_layer_analysis
        );
    } else {
        println!("❌ Transformer debugger not available");
    }

    // For demonstration, let's create some sample data and analyze it
    let sample_layer_weights = vec![
        Array2::<f32>::eye(16).into_dyn(),
        Array2::<f32>::zeros((16, 16)).into_dyn(),
    ];

    if let Some(transformer_debugger) = session.transformer_debugger_mut() {
        let sample_model_weights = vec![sample_layer_weights];

        match transformer_debugger.analyze_transformer_attention(&sample_model_weights) {
            Ok(analysis) => {
                println!("✅ Successfully analyzed sample transformer model");
                println!(
                    "  • Health status: {:?}",
                    analysis.model_attention_summary.model_attention_health
                );
            },
            Err(e) => {
                println!("❌ Error analyzing sample model: {}", e);
            },
        }
    }

    // Stop the session and generate report
    let report = session.stop().await?;
    println!("Debug session completed");

    // The report now includes all debugging components
    let summary = report.summary();
    println!("Session Summary:");
    println!("  • Session ID: {}", summary.session_id);
    println!("  • Total Issues: {}", summary.total_issues);
    println!("  • Critical Issues: {}", summary.critical_issues);

    Ok(())
}

/// Helper function to create sample attention weights with specific patterns
#[allow(dead_code)]
fn create_sample_attention_weights(seq_len: usize, pattern: &str) -> ArrayD<f32> {
    let mut weights = Array2::<f32>::zeros((seq_len, seq_len));

    match pattern {
        "diagonal" => {
            for i in 0..seq_len {
                for j in 0..seq_len {
                    let distance = (i as i32 - j as i32).abs();
                    if distance <= 2 {
                        weights[[i, j]] = 1.0 / (1.0 + distance as f32);
                    }
                }
            }
        },
        "uniform" => {
            weights.fill(1.0 / seq_len as f32);
        },
        "sparse" => {
            for i in 0..seq_len {
                for j in 0..seq_len {
                    if (i + j) % 4 == 0 {
                        weights[[i, j]] = 1.0;
                    }
                }
            }
        },
        _ => {
            // Random pattern
            for i in 0..seq_len {
                for j in 0..seq_len {
                    weights[[i, j]] = ((i * j) as f32).sin().abs();
                }
            }
        },
    }

    // Normalize rows
    for mut row in weights.rows_mut() {
        let sum: f32 = row.sum();
        if sum > 0.0 {
            row /= sum;
        }
    }

    weights.into_dyn()
}