trustformers-debug 0.1.1

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
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
//! Computation graph visualization tools
//!
//! This module provides tools to visualize the computation graph of neural networks,
//! including layer connections, tensor shapes, and operation flows.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Graph visualizer for computation graphs
#[derive(Debug)]
pub struct GraphVisualizer {
    /// Graph definition
    graph: ComputationGraph,
    /// Visualization config
    config: GraphVisualizerConfig,
}

/// Configuration for graph visualization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphVisualizerConfig {
    /// Include tensor shapes in visualization
    pub show_shapes: bool,
    /// Include data types in visualization
    pub show_dtypes: bool,
    /// Include operation attributes
    pub show_attributes: bool,
    /// Layout direction (TB=top-to-bottom, LR=left-to-right)
    pub layout_direction: LayoutDirection,
    /// Maximum depth to visualize (-1 for unlimited)
    pub max_depth: i32,
    /// Color scheme
    pub color_scheme: GraphColorScheme,
}

/// Layout direction for graph visualization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LayoutDirection {
    /// Top to bottom
    TopToBottom,
    /// Left to right
    LeftToRight,
    /// Bottom to top
    BottomToTop,
    /// Right to left
    RightToLeft,
}

/// Color scheme for graph nodes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GraphColorScheme {
    /// Default colors
    Default,
    /// By layer type
    ByLayerType,
    /// By computational cost
    ByCost,
    /// By data flow
    ByDataFlow,
}

impl Default for GraphVisualizerConfig {
    fn default() -> Self {
        Self {
            show_shapes: true,
            show_dtypes: true,
            show_attributes: false,
            layout_direction: LayoutDirection::TopToBottom,
            max_depth: -1,
            color_scheme: GraphColorScheme::ByLayerType,
        }
    }
}

/// Computation graph definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComputationGraph {
    /// Graph name
    pub name: String,
    /// Graph nodes
    pub nodes: Vec<GraphNode>,
    /// Graph edges
    pub edges: Vec<GraphEdge>,
    /// Input nodes
    pub inputs: Vec<String>,
    /// Output nodes
    pub outputs: Vec<String>,
}

/// Graph node representing an operation or tensor
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphNode {
    /// Node ID
    pub id: String,
    /// Node label (display name)
    pub label: String,
    /// Operation type
    pub op_type: String,
    /// Tensor shape (if applicable)
    pub shape: Option<Vec<i64>>,
    /// Data type
    pub dtype: Option<String>,
    /// Node attributes
    pub attributes: HashMap<String, String>,
    /// Node depth in the graph
    pub depth: usize,
}

/// Graph edge representing data flow
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphEdge {
    /// Source node ID
    pub from: String,
    /// Target node ID
    pub to: String,
    /// Edge label (optional)
    pub label: Option<String>,
    /// Tensor shape along this edge
    pub shape: Option<Vec<i64>>,
}

impl GraphVisualizer {
    /// Create a new graph visualizer
    ///
    /// # Arguments
    ///
    /// * `graph_name` - Name of the computation graph
    ///
    /// # Example
    ///
    /// ```
    /// use trustformers_debug::GraphVisualizer;
    ///
    /// let visualizer = GraphVisualizer::new("my_model");
    /// ```
    pub fn new(graph_name: &str) -> Self {
        let graph = ComputationGraph {
            name: graph_name.to_string(),
            nodes: Vec::new(),
            edges: Vec::new(),
            inputs: Vec::new(),
            outputs: Vec::new(),
        };

        Self {
            graph,
            config: GraphVisualizerConfig::default(),
        }
    }

    /// Create a graph visualizer with custom configuration
    pub fn with_config(graph_name: &str, config: GraphVisualizerConfig) -> Self {
        let graph = ComputationGraph {
            name: graph_name.to_string(),
            nodes: Vec::new(),
            edges: Vec::new(),
            inputs: Vec::new(),
            outputs: Vec::new(),
        };

        Self { graph, config }
    }

    /// Add a node to the graph
    ///
    /// # Example
    ///
    /// ```
    /// # use trustformers_debug::GraphVisualizer;
    /// # use std::collections::HashMap;
    /// # let mut visualizer = GraphVisualizer::new("model");
    /// visualizer.add_node(
    ///     "layer1",
    ///     "Linear Layer 1",
    ///     "Linear",
    ///     Some(vec![10, 20]),
    ///     Some("float32".to_string()),
    ///     HashMap::new(),
    /// );
    /// ```
    pub fn add_node(
        &mut self,
        id: &str,
        label: &str,
        op_type: &str,
        shape: Option<Vec<i64>>,
        dtype: Option<String>,
        attributes: HashMap<String, String>,
    ) {
        let node = GraphNode {
            id: id.to_string(),
            label: label.to_string(),
            op_type: op_type.to_string(),
            shape,
            dtype,
            attributes,
            depth: 0, // Will be computed later
        };

        self.graph.nodes.push(node);
    }

    /// Add an edge to the graph
    pub fn add_edge(
        &mut self,
        from: &str,
        to: &str,
        label: Option<String>,
        shape: Option<Vec<i64>>,
    ) {
        let edge = GraphEdge {
            from: from.to_string(),
            to: to.to_string(),
            label,
            shape,
        };

        self.graph.edges.push(edge);
    }

    /// Mark a node as an input
    pub fn mark_input(&mut self, node_id: &str) {
        if !self.graph.inputs.contains(&node_id.to_string()) {
            self.graph.inputs.push(node_id.to_string());
        }
    }

    /// Mark a node as an output
    pub fn mark_output(&mut self, node_id: &str) {
        if !self.graph.outputs.contains(&node_id.to_string()) {
            self.graph.outputs.push(node_id.to_string());
        }
    }

    /// Compute node depths in the graph
    fn compute_depths(&mut self) {
        // Build adjacency list
        let mut adjacency: HashMap<String, Vec<String>> = HashMap::new();
        for edge in &self.graph.edges {
            adjacency.entry(edge.from.clone()).or_default().push(edge.to.clone());
        }

        // BFS from input nodes
        let mut depths: HashMap<String, usize> = HashMap::new();
        let mut queue: Vec<(String, usize)> = Vec::new();

        for input in &self.graph.inputs {
            queue.push((input.clone(), 0));
            depths.insert(input.clone(), 0);
        }

        while let Some((node_id, depth)) = queue.pop() {
            if let Some(neighbors) = adjacency.get(&node_id) {
                for neighbor in neighbors {
                    let new_depth = depth + 1;
                    if !depths.contains_key(neighbor) || depths[neighbor] < new_depth {
                        depths.insert(neighbor.clone(), new_depth);
                        queue.push((neighbor.clone(), new_depth));
                    }
                }
            }
        }

        // Update node depths
        for node in &mut self.graph.nodes {
            node.depth = *depths.get(&node.id).unwrap_or(&0);
        }
    }

    /// Export graph to GraphViz DOT format
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use trustformers_debug::GraphVisualizer;
    /// # let mut visualizer = GraphVisualizer::new("model");
    /// visualizer.export_to_dot("model.dot").unwrap();
    /// ```
    pub fn export_to_dot<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        self.compute_depths();

        let mut dot = String::from("digraph {\n");

        // Graph attributes
        let direction = match self.config.layout_direction {
            LayoutDirection::TopToBottom => "TB",
            LayoutDirection::LeftToRight => "LR",
            LayoutDirection::BottomToTop => "BT",
            LayoutDirection::RightToLeft => "RL",
        };
        dot.push_str(&format!("  rankdir={};\n", direction));
        dot.push_str("  node [shape=box, style=rounded];\n\n");

        // Add nodes
        for node in &self.graph.nodes {
            if self.config.max_depth >= 0 && node.depth > self.config.max_depth as usize {
                continue;
            }

            let color = self.get_node_color(node);
            let mut label = node.label.to_string();

            if self.config.show_shapes {
                if let Some(ref shape) = node.shape {
                    label.push_str(&format!("\\nshape: {:?}", shape));
                }
            }

            if self.config.show_dtypes {
                if let Some(ref dtype) = node.dtype {
                    label.push_str(&format!("\\ndtype: {}", dtype));
                }
            }

            dot.push_str(&format!(
                "  \"{}\" [label=\"{}\", fillcolor=\"{}\", style=\"filled,rounded\"];\n",
                node.id, label, color
            ));
        }

        dot.push('\n');

        // Add edges
        for edge in &self.graph.edges {
            let mut edge_label = String::new();

            if let Some(ref label) = edge.label {
                edge_label = label.clone();
            } else if self.config.show_shapes {
                if let Some(ref shape) = edge.shape {
                    edge_label = format!("{:?}", shape);
                }
            }

            if !edge_label.is_empty() {
                dot.push_str(&format!(
                    "  \"{}\" -> \"{}\" [label=\"{}\"];\n",
                    edge.from, edge.to, edge_label
                ));
            } else {
                dot.push_str(&format!("  \"{}\" -> \"{}\";\n", edge.from, edge.to));
            }
        }

        dot.push_str("}\n");

        fs::write(path, dot)?;
        Ok(())
    }

    /// Get color for a node based on color scheme
    fn get_node_color(&self, node: &GraphNode) -> &'static str {
        match self.config.color_scheme {
            GraphColorScheme::Default => "lightblue",
            GraphColorScheme::ByLayerType => match node.op_type.as_str() {
                "Linear" | "Dense" => "lightblue",
                "Conv2d" | "Conv1d" => "lightgreen",
                "BatchNorm" | "LayerNorm" => "lightyellow",
                "ReLU" | "GELU" | "Softmax" => "lightcoral",
                "Dropout" => "lightgray",
                "Attention" | "MultiHeadAttention" => "plum",
                _ => "white",
            },
            GraphColorScheme::ByCost => {
                // Simplified: use depth as proxy for computational cost
                if node.depth > 10 {
                    "darkred"
                } else if node.depth > 5 {
                    "orange"
                } else {
                    "lightgreen"
                }
            },
            GraphColorScheme::ByDataFlow => {
                if self.graph.inputs.contains(&node.id) {
                    "lightgreen"
                } else if self.graph.outputs.contains(&node.id) {
                    "lightcoral"
                } else {
                    "lightblue"
                }
            },
        }
    }

    /// Export graph to JSON format
    pub fn export_to_json<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let json = serde_json::to_string_pretty(&self.graph)?;
        fs::write(path, json)?;
        Ok(())
    }

    /// Get statistics about the graph
    pub fn statistics(&self) -> GraphStatistics {
        let num_nodes = self.graph.nodes.len();
        let num_edges = self.graph.edges.len();

        let op_type_counts: HashMap<String, usize> =
            self.graph.nodes.iter().fold(HashMap::new(), |mut acc, node| {
                *acc.entry(node.op_type.clone()).or_insert(0) += 1;
                acc
            });

        let max_depth = self.graph.nodes.iter().map(|n| n.depth).max().unwrap_or(0);

        GraphStatistics {
            num_nodes,
            num_edges,
            num_inputs: self.graph.inputs.len(),
            num_outputs: self.graph.outputs.len(),
            max_depth,
            op_type_counts,
        }
    }

    /// Print a summary of the graph
    pub fn summary(&self) -> String {
        let stats = self.statistics();

        let mut output = String::new();
        output.push_str(&format!("Computation Graph: {}\n", self.graph.name));
        output.push_str(&"=".repeat(60));
        output.push('\n');
        output.push_str(&format!("Nodes: {}\n", stats.num_nodes));
        output.push_str(&format!("Edges: {}\n", stats.num_edges));
        output.push_str(&format!("Inputs: {}\n", stats.num_inputs));
        output.push_str(&format!("Outputs: {}\n", stats.num_outputs));
        output.push_str(&format!("Max Depth: {}\n", stats.max_depth));

        output.push_str("\nOperation Types:\n");
        for (op_type, count) in &stats.op_type_counts {
            output.push_str(&format!("  {}: {}\n", op_type, count));
        }

        output
    }
}

/// Graph statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphStatistics {
    /// Number of nodes
    pub num_nodes: usize,
    /// Number of edges
    pub num_edges: usize,
    /// Number of input nodes
    pub num_inputs: usize,
    /// Number of output nodes
    pub num_outputs: usize,
    /// Maximum depth
    pub max_depth: usize,
    /// Operation type counts
    pub op_type_counts: HashMap<String, usize>,
}

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

    #[test]
    fn test_graph_visualizer_creation() {
        let visualizer = GraphVisualizer::new("test_graph");
        assert_eq!(visualizer.graph.name, "test_graph");
        assert_eq!(visualizer.graph.nodes.len(), 0);
    }

    #[test]
    fn test_add_node() {
        let mut visualizer = GraphVisualizer::new("test");

        visualizer.add_node(
            "node1",
            "Layer 1",
            "Linear",
            Some(vec![10, 20]),
            Some("float32".to_string()),
            HashMap::new(),
        );

        assert_eq!(visualizer.graph.nodes.len(), 1);
        assert_eq!(visualizer.graph.nodes[0].id, "node1");
    }

    #[test]
    fn test_add_edge() {
        let mut visualizer = GraphVisualizer::new("test");

        visualizer.add_node("node1", "N1", "Linear", None, None, HashMap::new());
        visualizer.add_node("node2", "N2", "ReLU", None, None, HashMap::new());
        visualizer.add_edge("node1", "node2", None, Some(vec![10, 20]));

        assert_eq!(visualizer.graph.edges.len(), 1);
        assert_eq!(visualizer.graph.edges[0].from, "node1");
        assert_eq!(visualizer.graph.edges[0].to, "node2");
    }

    #[test]
    fn test_mark_input_output() {
        let mut visualizer = GraphVisualizer::new("test");

        visualizer.add_node("input", "Input", "Input", None, None, HashMap::new());
        visualizer.add_node("output", "Output", "Output", None, None, HashMap::new());

        visualizer.mark_input("input");
        visualizer.mark_output("output");

        assert_eq!(visualizer.graph.inputs.len(), 1);
        assert_eq!(visualizer.graph.outputs.len(), 1);
    }

    #[test]
    fn test_export_to_dot() {
        let temp_dir = env::temp_dir();
        let output_path = temp_dir.join("test_graph.dot");

        let mut visualizer = GraphVisualizer::new("test");

        visualizer.add_node("input", "Input", "Input", None, None, HashMap::new());
        visualizer.add_node(
            "layer1",
            "Linear",
            "Linear",
            Some(vec![10, 20]),
            None,
            HashMap::new(),
        );
        visualizer.add_edge("input", "layer1", None, None);

        visualizer.mark_input("input");

        visualizer.export_to_dot(&output_path).expect("operation failed in test");
        assert!(output_path.exists());

        // Clean up
        let _ = fs::remove_file(output_path);
    }

    #[test]
    fn test_export_to_json() {
        let temp_dir = env::temp_dir();
        let output_path = temp_dir.join("test_graph.json");

        let mut visualizer = GraphVisualizer::new("test");
        visualizer.add_node("node1", "N1", "Linear", None, None, HashMap::new());

        visualizer.export_to_json(&output_path).expect("operation failed in test");
        assert!(output_path.exists());

        // Clean up
        let _ = fs::remove_file(output_path);
    }

    #[test]
    fn test_statistics() {
        let mut visualizer = GraphVisualizer::new("test");

        visualizer.add_node("n1", "N1", "Linear", None, None, HashMap::new());
        visualizer.add_node("n2", "N2", "Linear", None, None, HashMap::new());
        visualizer.add_node("n3", "N3", "ReLU", None, None, HashMap::new());

        visualizer.add_edge("n1", "n2", None, None);
        visualizer.add_edge("n2", "n3", None, None);

        visualizer.mark_input("n1");
        visualizer.mark_output("n3");

        let stats = visualizer.statistics();

        assert_eq!(stats.num_nodes, 3);
        assert_eq!(stats.num_edges, 2);
        assert_eq!(stats.num_inputs, 1);
        assert_eq!(stats.num_outputs, 1);
    }

    #[test]
    fn test_summary() {
        let mut visualizer = GraphVisualizer::new("test_model");

        visualizer.add_node("input", "Input", "Input", None, None, HashMap::new());
        visualizer.add_node("layer1", "Linear", "Linear", None, None, HashMap::new());

        let summary = visualizer.summary();
        assert!(summary.contains("test_model"));
        assert!(summary.contains("Nodes: 2"));
    }

    #[test]
    fn test_compute_depths() {
        let mut visualizer = GraphVisualizer::new("test");

        visualizer.add_node("input", "Input", "Input", None, None, HashMap::new());
        visualizer.add_node("layer1", "L1", "Linear", None, None, HashMap::new());
        visualizer.add_node("layer2", "L2", "ReLU", None, None, HashMap::new());

        visualizer.add_edge("input", "layer1", None, None);
        visualizer.add_edge("layer1", "layer2", None, None);

        visualizer.mark_input("input");

        visualizer.compute_depths();

        assert_eq!(visualizer.graph.nodes[0].depth, 0);
        assert_eq!(visualizer.graph.nodes[1].depth, 1);
        assert_eq!(visualizer.graph.nodes[2].depth, 2);
    }
}