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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
//! Computation graph analysis tools for debugging deep learning models.
//!
//! This module provides comprehensive analysis tools for computation graphs,
//! including node analysis, dependency tracking, optimization opportunities,
//! bottleneck detection, and graph visualization capabilities.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt;
use uuid::Uuid;

/// Represents a computation graph for analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComputationGraph {
    /// Unique identifier for this graph
    pub id: Uuid,
    /// Map of node ID to node information
    pub nodes: HashMap<String, GraphNode>,
    /// Adjacency list representing edges (dependencies)
    pub edges: HashMap<String, Vec<String>>,
    /// Root nodes (inputs to the computation)
    pub root_nodes: HashSet<String>,
    /// Leaf nodes (outputs of the computation)
    pub leaf_nodes: HashSet<String>,
    /// Metadata about the graph
    pub metadata: GraphMetadata,
}

/// Metadata about the computation graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphMetadata {
    /// Name of the model/graph
    pub name: String,
    /// Total number of nodes
    pub node_count: usize,
    /// Total number of edges
    pub edge_count: usize,
    /// Maximum depth of the graph
    pub max_depth: usize,
    /// Memory usage estimate in bytes
    pub estimated_memory_usage: u64,
    /// FLOP count estimate
    pub estimated_flops: u64,
    /// Timestamp when graph was created
    pub created_at: chrono::DateTime<chrono::Utc>,
}

/// Represents a single node in the computation graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphNode {
    /// Unique identifier for this node
    pub id: String,
    /// Human-readable name
    pub name: String,
    /// Type of operation (e.g., "MatMul", "Add", "ReLU")
    pub operation_type: OperationType,
    /// Input tensor shapes
    pub input_shapes: Vec<Vec<usize>>,
    /// Output tensor shapes
    pub output_shapes: Vec<Vec<usize>>,
    /// Computational complexity (FLOPs)
    pub flop_count: u64,
    /// Memory usage estimate in bytes
    pub memory_usage: u64,
    /// Execution time in microseconds (if profiled)
    pub execution_time_us: Option<u64>,
    /// Number of parameters (for parameterized operations)
    pub parameter_count: Option<u64>,
    /// Position in topological ordering
    pub topo_order: Option<usize>,
    /// Depth in the graph (distance from inputs)
    pub depth: usize,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Types of operations in the computation graph
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OperationType {
    // Arithmetic operations
    Add,
    Subtract,
    Multiply,
    Divide,
    MatMul,
    Dot,

    // Activation functions
    ReLU,
    Sigmoid,
    Tanh,
    GELU,
    Softmax,

    // Normalization
    LayerNorm,
    BatchNorm,
    RMSNorm,

    // Convolution operations
    Conv1D,
    Conv2D,
    Conv3D,
    ConvTranspose,

    // Pooling operations
    MaxPool,
    AvgPool,
    AdaptivePool,

    // Tensor operations
    Reshape,
    Transpose,
    Concat,
    Split,
    Slice,
    Gather,
    Scatter,

    // Reduction operations
    Sum,
    Mean,
    Max,
    Min,

    // Attention operations
    Attention,
    MultiHeadAttention,
    SelfAttention,
    CrossAttention,

    // Embedding operations
    Embedding,
    PositionalEmbedding,

    // Loss functions
    CrossEntropyLoss,
    MSELoss,
    L1Loss,

    // Control flow
    If,
    While,
    Loop,

    // Custom operations
    Custom(String),
}

/// Configuration for computation graph analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphAnalysisConfig {
    /// Whether to perform memory analysis
    pub enable_memory_analysis: bool,
    /// Whether to perform FLOP analysis
    pub enable_flop_analysis: bool,
    /// Whether to detect optimization opportunities
    pub enable_optimization_analysis: bool,
    /// Whether to perform bottleneck detection
    pub enable_bottleneck_detection: bool,
    /// Whether to analyze data flow patterns
    pub enable_dataflow_analysis: bool,
    /// Threshold for considering a node a bottleneck (microseconds)
    pub bottleneck_threshold_us: u64,
    /// Memory threshold for large operations (bytes)
    pub large_memory_threshold: u64,
}

impl Default for GraphAnalysisConfig {
    fn default() -> Self {
        Self {
            enable_memory_analysis: true,
            enable_flop_analysis: true,
            enable_optimization_analysis: true,
            enable_bottleneck_detection: true,
            enable_dataflow_analysis: true,
            bottleneck_threshold_us: 1000,             // 1ms
            large_memory_threshold: 1024 * 1024 * 100, // 100MB
        }
    }
}

/// Main computation graph analyzer
#[derive(Debug)]
pub struct ComputationGraphAnalyzer {
    config: GraphAnalysisConfig,
    graphs: HashMap<Uuid, ComputationGraph>,
    analysis_results: HashMap<Uuid, GraphAnalysisResult>,
}

/// Comprehensive analysis result for a computation graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphAnalysisResult {
    /// Graph being analyzed
    pub graph_id: Uuid,
    /// Memory analysis results
    pub memory_analysis: Option<MemoryAnalysis>,
    /// FLOP analysis results
    pub flop_analysis: Option<FlopAnalysis>,
    /// Optimization opportunities
    pub optimization_opportunities: Vec<OptimizationOpportunity>,
    /// Bottleneck analysis
    pub bottleneck_analysis: Option<BottleneckAnalysis>,
    /// Data flow analysis
    pub dataflow_analysis: Option<DataFlowAnalysis>,
    /// Critical path analysis
    pub critical_path: Vec<String>,
    /// Graph statistics
    pub statistics: GraphStatistics,
    /// Recommendations for improvement
    pub recommendations: Vec<String>,
}

/// Memory usage analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryAnalysis {
    /// Total memory usage in bytes
    pub total_memory_usage: u64,
    /// Peak memory usage in bytes
    pub peak_memory_usage: u64,
    /// Memory usage by operation type
    pub memory_by_operation: HashMap<OperationType, u64>,
    /// Nodes with highest memory usage
    pub memory_hotspots: Vec<(String, u64)>,
    /// Memory fragmentation estimate
    pub fragmentation_ratio: f64,
    /// Suggested memory optimizations
    pub optimization_suggestions: Vec<String>,
}

/// FLOP (Floating Point Operations) analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlopAnalysis {
    /// Total FLOP count
    pub total_flops: u64,
    /// FLOP count by operation type
    pub flops_by_operation: HashMap<OperationType, u64>,
    /// Nodes with highest FLOP count
    pub compute_hotspots: Vec<(String, u64)>,
    /// Arithmetic intensity (FLOPs per byte)
    pub arithmetic_intensity: f64,
    /// Computational complexity analysis
    pub complexity_analysis: ComplexityAnalysis,
}

/// Complexity analysis of the computation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComplexityAnalysis {
    /// Time complexity estimate
    pub time_complexity: String,
    /// Space complexity estimate
    pub space_complexity: String,
    /// Parallelization potential (0.0 to 1.0)
    pub parallelization_potential: f64,
    /// Sequential dependencies
    pub sequential_dependencies: usize,
}

/// Optimization opportunity detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationOpportunity {
    /// Type of optimization
    pub optimization_type: OptimizationType,
    /// Description of the opportunity
    pub description: String,
    /// Nodes involved in this optimization
    pub affected_nodes: Vec<String>,
    /// Estimated performance improvement
    pub estimated_improvement: EstimatedImprovement,
    /// Implementation difficulty (1-5)
    pub implementation_difficulty: u8,
    /// Priority level
    pub priority: OptimizationPriority,
}

/// Types of optimizations that can be applied
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationType {
    /// Fuse multiple operations into one
    OperationFusion,
    /// Eliminate redundant computations
    RedundancyElimination,
    /// Optimize memory layout
    MemoryLayoutOptimization,
    /// Use more efficient algorithms
    AlgorithmicOptimization,
    /// Parallelize sequential operations
    Parallelization,
    /// Optimize data access patterns
    DataAccessOptimization,
    /// Reduce precision where safe
    PrecisionOptimization,
    /// Cache intermediate results
    Memoization,
    /// Optimize control flow
    ControlFlowOptimization,
}

/// Priority levels for optimizations
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum OptimizationPriority {
    Low,
    Medium,
    High,
    Critical,
}

/// Estimated improvement from an optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EstimatedImprovement {
    /// Estimated speedup (multiplicative factor)
    pub speedup_factor: f64,
    /// Estimated memory reduction in bytes
    pub memory_reduction: u64,
    /// Estimated energy savings (0.0 to 1.0)
    pub energy_savings: f64,
}

/// Bottleneck analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BottleneckAnalysis {
    /// Nodes that are bottlenecks
    pub bottleneck_nodes: Vec<String>,
    /// Critical path through the graph
    pub critical_path_nodes: Vec<String>,
    /// Total critical path time
    pub critical_path_time_us: u64,
    /// Nodes that could benefit from parallelization
    pub parallelizable_nodes: Vec<String>,
    /// Scheduling suggestions
    pub scheduling_suggestions: Vec<String>,
}

/// Data flow analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataFlowAnalysis {
    /// Data dependencies between nodes
    pub data_dependencies: HashMap<String, Vec<String>>,
    /// Live variables at each node
    pub live_variables: HashMap<String, HashSet<String>>,
    /// Variable lifetime analysis
    pub variable_lifetimes: HashMap<String, VariableLifetime>,
    /// Memory reuse opportunities
    pub memory_reuse_opportunities: Vec<MemoryReuseOpportunity>,
}

/// Lifetime information for a variable
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableLifetime {
    /// Node where variable is created
    pub birth_node: String,
    /// Node where variable is last used
    pub death_node: String,
    /// All nodes that use this variable
    pub usage_nodes: Vec<String>,
    /// Memory footprint in bytes
    pub memory_footprint: u64,
}

/// Memory reuse opportunity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryReuseOpportunity {
    /// Variables that can share memory
    pub reusable_variables: Vec<String>,
    /// Memory that can be saved
    pub memory_savings: u64,
    /// Implementation complexity
    pub complexity: u8,
}

/// Graph statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphStatistics {
    /// Number of nodes by operation type
    pub nodes_by_type: HashMap<OperationType, usize>,
    /// Average node fan-in
    pub average_fan_in: f64,
    /// Average node fan-out
    pub average_fan_out: f64,
    /// Graph diameter (longest shortest path)
    pub diameter: usize,
    /// Clustering coefficient
    pub clustering_coefficient: f64,
    /// Number of strongly connected components
    pub strongly_connected_components: usize,
}

impl ComputationGraphAnalyzer {
    /// Create a new computation graph analyzer
    pub fn new(config: GraphAnalysisConfig) -> Self {
        Self {
            config,
            graphs: HashMap::new(),
            analysis_results: HashMap::new(),
        }
    }

    /// Add a computation graph for analysis
    pub fn add_graph(&mut self, graph: ComputationGraph) -> Result<()> {
        let graph_id = graph.id;
        self.graphs.insert(graph_id, graph);
        Ok(())
    }

    /// Create a computation graph from operations
    pub fn create_graph(
        &mut self,
        name: String,
        operations: Vec<(String, OperationType, Vec<String>)>, // (node_id, op_type, dependencies)
    ) -> Result<Uuid> {
        let graph_id = Uuid::new_v4();
        let mut nodes = HashMap::new();
        let mut edges = HashMap::new();
        let mut root_nodes = HashSet::new();
        let mut leaf_nodes = HashSet::new();

        // Create nodes
        for (node_id, op_type, dependencies) in &operations {
            let node = GraphNode {
                id: node_id.clone(),
                name: node_id.clone(),
                operation_type: op_type.clone(),
                input_shapes: vec![],
                output_shapes: vec![],
                flop_count: self.estimate_flops(op_type, &[]),
                memory_usage: self.estimate_memory(op_type, &[]),
                execution_time_us: None,
                parameter_count: self.estimate_parameters(op_type),
                topo_order: None,
                depth: 0,
                metadata: HashMap::new(),
            };
            nodes.insert(node_id.clone(), node);

            // Track dependencies
            if dependencies.is_empty() {
                root_nodes.insert(node_id.clone());
            }
            edges.insert(node_id.clone(), dependencies.clone());
        }

        // Identify leaf nodes
        let all_dependencies: HashSet<String> = edges.values().flatten().cloned().collect();
        for node_id in nodes.keys() {
            if !all_dependencies.contains(node_id) {
                leaf_nodes.insert(node_id.clone());
            }
        }

        // Calculate depth and topological order
        self.calculate_depth_and_topo_order(&mut nodes, &edges)?;

        let metadata = GraphMetadata {
            name,
            node_count: nodes.len(),
            edge_count: edges.values().map(|deps| deps.len()).sum(),
            max_depth: nodes.values().map(|n| n.depth).max().unwrap_or(0),
            estimated_memory_usage: nodes.values().map(|n| n.memory_usage).sum(),
            estimated_flops: nodes.values().map(|n| n.flop_count).sum(),
            created_at: chrono::Utc::now(),
        };

        let graph = ComputationGraph {
            id: graph_id,
            nodes,
            edges,
            root_nodes,
            leaf_nodes,
            metadata,
        };

        self.graphs.insert(graph_id, graph);
        Ok(graph_id)
    }

    /// Analyze a computation graph
    pub fn analyze_graph(&mut self, graph_id: Uuid) -> Result<GraphAnalysisResult> {
        let graph = self
            .graphs
            .get(&graph_id)
            .ok_or_else(|| anyhow::anyhow!("Graph not found: {}", graph_id))?;

        let mut result = GraphAnalysisResult {
            graph_id,
            memory_analysis: None,
            flop_analysis: None,
            optimization_opportunities: Vec::new(),
            bottleneck_analysis: None,
            dataflow_analysis: None,
            critical_path: Vec::new(),
            statistics: self.calculate_statistics(graph)?,
            recommendations: Vec::new(),
        };

        // Perform different types of analysis based on configuration
        if self.config.enable_memory_analysis {
            result.memory_analysis = Some(self.analyze_memory_usage(graph)?);
        }

        if self.config.enable_flop_analysis {
            result.flop_analysis = Some(self.analyze_flop_usage(graph)?);
        }

        if self.config.enable_optimization_analysis {
            result.optimization_opportunities = self.detect_optimization_opportunities(graph)?;
        }

        if self.config.enable_bottleneck_detection {
            result.bottleneck_analysis = Some(self.analyze_bottlenecks(graph)?);
        }

        if self.config.enable_dataflow_analysis {
            result.dataflow_analysis = Some(self.analyze_dataflow(graph)?);
        }

        result.critical_path = self.find_critical_path(graph)?;
        result.recommendations = self.generate_recommendations(&result)?;

        self.analysis_results.insert(graph_id, result.clone());
        Ok(result)
    }

    /// Get analysis results for a graph
    pub fn get_analysis_result(&self, graph_id: Uuid) -> Option<&GraphAnalysisResult> {
        self.analysis_results.get(&graph_id)
    }

    /// Export graph analysis to DOT format for visualization
    pub fn export_to_dot(&self, graph_id: Uuid) -> Result<String> {
        let graph = self
            .graphs
            .get(&graph_id)
            .ok_or_else(|| anyhow::anyhow!("Graph not found: {}", graph_id))?;

        let mut dot = String::new();
        dot.push_str(&format!("digraph \"{}\" {{\n", graph.metadata.name));
        dot.push_str("  rankdir=TB;\n");
        dot.push_str("  node [shape=box, style=filled];\n\n");

        // Add nodes with styling based on operation type
        for node in graph.nodes.values() {
            let color = self.get_node_color(&node.operation_type);
            let label = format!(
                "{}\\n{}\\n{:.1} GFLOP\\n{:.1} MB",
                node.name,
                format!("{:?}", node.operation_type),
                node.flop_count as f64 / 1e9,
                node.memory_usage as f64 / (1024.0 * 1024.0)
            );

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

        dot.push('\n');

        // Add edges
        for (node_id, dependencies) in &graph.edges {
            for dep in dependencies {
                dot.push_str(&format!("  \"{}\" -> \"{}\";\n", dep, node_id));
            }
        }

        dot.push_str("}\n");
        Ok(dot)
    }

    // Private helper methods

    fn calculate_depth_and_topo_order(
        &self,
        nodes: &mut HashMap<String, GraphNode>,
        edges: &HashMap<String, Vec<String>>,
    ) -> Result<()> {
        // Topological sort and depth calculation
        let mut in_degree: HashMap<String, usize> = HashMap::new();
        let mut adj_list: HashMap<String, Vec<String>> = HashMap::new();

        // Initialize in-degrees and adjacency list
        for node_id in nodes.keys() {
            in_degree.insert(node_id.clone(), 0);
            adj_list.insert(node_id.clone(), Vec::new());
        }

        for (node_id, dependencies) in edges {
            in_degree.insert(node_id.clone(), dependencies.len());
            for dep in dependencies {
                if let Some(adj) = adj_list.get_mut(dep) {
                    adj.push(node_id.clone());
                }
            }
        }

        // Kahn's algorithm for topological sorting and depth calculation
        let mut queue = VecDeque::new();
        let mut topo_order = 0;

        // Find all nodes with no incoming edges
        for (node_id, &degree) in &in_degree {
            if degree == 0 {
                queue.push_back((node_id.clone(), 0)); // (node_id, depth)
            }
        }

        while let Some((node_id, depth)) = queue.pop_front() {
            // Update node
            if let Some(node) = nodes.get_mut(&node_id) {
                node.depth = depth;
                node.topo_order = Some(topo_order);
                topo_order += 1;
            }

            // Process neighbors
            if let Some(neighbors) = adj_list.get(&node_id) {
                for neighbor in neighbors {
                    if let Some(degree) = in_degree.get_mut(neighbor) {
                        *degree -= 1;
                        if *degree == 0 {
                            queue.push_back((neighbor.clone(), depth + 1));
                        }
                    }
                }
            }
        }

        Ok(())
    }

    fn estimate_flops(&self, op_type: &OperationType, shapes: &[Vec<usize>]) -> u64 {
        // Simplified FLOP estimation
        match op_type {
            OperationType::MatMul => {
                if shapes.len() >= 2 {
                    let a_shape = &shapes[0];
                    let b_shape = &shapes[1];
                    if a_shape.len() >= 2 && b_shape.len() >= 2 {
                        let m = a_shape[a_shape.len() - 2];
                        let k = a_shape[a_shape.len() - 1];
                        let n = b_shape[b_shape.len() - 1];
                        return (2 * m * k * n) as u64;
                    }
                }
                1000000 // Default estimate
            },
            OperationType::Add | OperationType::Subtract | OperationType::Multiply => {
                shapes.first().map(|s| s.iter().product::<usize>() as u64).unwrap_or(1000)
            },
            OperationType::ReLU | OperationType::Sigmoid | OperationType::Tanh => {
                shapes.first().map(|s| s.iter().product::<usize>() as u64).unwrap_or(1000)
            },
            OperationType::LayerNorm | OperationType::BatchNorm => {
                shapes.first().map(|s| (s.iter().product::<usize>() * 5) as u64).unwrap_or(5000)
            },
            _ => 1000, // Default estimate
        }
    }

    fn estimate_memory(&self, op_type: &OperationType, shapes: &[Vec<usize>]) -> u64 {
        // Simplified memory estimation (assuming float32 = 4 bytes)
        let element_size = 4u64;
        match op_type {
            OperationType::MatMul => {
                shapes
                    .iter()
                    .map(|s| s.iter().product::<usize>() as u64 * element_size)
                    .sum::<u64>()
                    .max(1024) // Minimum 1KB
            },
            _ => shapes
                .first()
                .map(|s| s.iter().product::<usize>() as u64 * element_size)
                .unwrap_or(1024),
        }
    }

    fn estimate_parameters(&self, op_type: &OperationType) -> Option<u64> {
        match op_type {
            OperationType::MatMul => Some(1000000), // Example: 1M parameters
            OperationType::Conv2D => Some(500000),
            OperationType::Embedding => Some(2000000),
            OperationType::LayerNorm => Some(1000),
            _ => None,
        }
    }

    fn analyze_memory_usage(&self, graph: &ComputationGraph) -> Result<MemoryAnalysis> {
        let total_memory_usage = graph.nodes.values().map(|n| n.memory_usage).sum();

        let mut memory_by_operation: HashMap<OperationType, u64> = HashMap::new();
        for node in graph.nodes.values() {
            *memory_by_operation.entry(node.operation_type.clone()).or_insert(0) +=
                node.memory_usage;
        }

        let mut memory_hotspots: Vec<(String, u64)> =
            graph.nodes.values().map(|n| (n.id.clone(), n.memory_usage)).collect();
        memory_hotspots.sort_by_key(|item| std::cmp::Reverse(item.1));
        memory_hotspots.truncate(10); // Top 10

        let peak_memory_usage = total_memory_usage; // Simplified
        let fragmentation_ratio = 0.1; // Simplified estimate

        let optimization_suggestions = vec![
            "Consider memory pooling for frequently allocated tensors".to_string(),
            "Implement in-place operations where possible".to_string(),
            "Use gradient checkpointing for memory-intensive layers".to_string(),
        ];

        Ok(MemoryAnalysis {
            total_memory_usage,
            peak_memory_usage,
            memory_by_operation,
            memory_hotspots,
            fragmentation_ratio,
            optimization_suggestions,
        })
    }

    fn analyze_flop_usage(&self, graph: &ComputationGraph) -> Result<FlopAnalysis> {
        let total_flops = graph.nodes.values().map(|n| n.flop_count).sum();

        let mut flops_by_operation: HashMap<OperationType, u64> = HashMap::new();
        for node in graph.nodes.values() {
            *flops_by_operation.entry(node.operation_type.clone()).or_insert(0) += node.flop_count;
        }

        let mut compute_hotspots: Vec<(String, u64)> =
            graph.nodes.values().map(|n| (n.id.clone(), n.flop_count)).collect();
        compute_hotspots.sort_by_key(|item| std::cmp::Reverse(item.1));
        compute_hotspots.truncate(10); // Top 10

        let total_memory = graph.nodes.values().map(|n| n.memory_usage).sum::<u64>();
        let arithmetic_intensity =
            if total_memory > 0 { total_flops as f64 / total_memory as f64 } else { 0.0 };

        let complexity_analysis = ComplexityAnalysis {
            time_complexity: "O(n)".to_string(),  // Simplified
            space_complexity: "O(n)".to_string(), // Simplified
            parallelization_potential: 0.7,       // Simplified estimate
            sequential_dependencies: graph.metadata.max_depth,
        };

        Ok(FlopAnalysis {
            total_flops,
            flops_by_operation,
            compute_hotspots,
            arithmetic_intensity,
            complexity_analysis,
        })
    }

    fn detect_optimization_opportunities(
        &self,
        graph: &ComputationGraph,
    ) -> Result<Vec<OptimizationOpportunity>> {
        let mut opportunities = Vec::new();

        // Look for operation fusion opportunities
        opportunities.extend(self.detect_fusion_opportunities(graph)?);

        // Look for redundant operations
        opportunities.extend(self.detect_redundancy_opportunities(graph)?);

        // Look for memory optimization opportunities
        opportunities.extend(self.detect_memory_optimizations(graph)?);

        Ok(opportunities)
    }

    fn detect_fusion_opportunities(
        &self,
        graph: &ComputationGraph,
    ) -> Result<Vec<OptimizationOpportunity>> {
        let mut opportunities = Vec::new();

        // Look for patterns like MatMul + Add (bias addition)
        for node in graph.nodes.values() {
            if let OperationType::Add = node.operation_type {
                let empty_deps = vec![];
                let dependencies = graph.edges.get(&node.id).unwrap_or(&empty_deps);
                for dep in dependencies {
                    if let Some(dep_node) = graph.nodes.get(dep) {
                        if let OperationType::MatMul = dep_node.operation_type {
                            opportunities.push(OptimizationOpportunity {
                                optimization_type: OptimizationType::OperationFusion,
                                description:
                                    "Fuse MatMul and Add operations into a single GEMM operation"
                                        .to_string(),
                                affected_nodes: vec![dep.clone(), node.id.clone()],
                                estimated_improvement: EstimatedImprovement {
                                    speedup_factor: 1.2,
                                    memory_reduction: 1024 * 1024, // 1MB
                                    energy_savings: 0.1,
                                },
                                implementation_difficulty: 2,
                                priority: OptimizationPriority::Medium,
                            });
                        }
                    }
                }
            }
        }

        Ok(opportunities)
    }

    fn detect_redundancy_opportunities(
        &self,
        _graph: &ComputationGraph,
    ) -> Result<Vec<OptimizationOpportunity>> {
        // Simplified - in real implementation would detect common subexpressions
        Ok(vec![])
    }

    fn detect_memory_optimizations(
        &self,
        graph: &ComputationGraph,
    ) -> Result<Vec<OptimizationOpportunity>> {
        let mut opportunities = Vec::new();

        // Look for large memory operations
        for node in graph.nodes.values() {
            if node.memory_usage > self.config.large_memory_threshold {
                opportunities.push(OptimizationOpportunity {
                    optimization_type: OptimizationType::MemoryLayoutOptimization,
                    description: format!(
                        "Optimize memory layout for large operation: {}",
                        node.name
                    ),
                    affected_nodes: vec![node.id.clone()],
                    estimated_improvement: EstimatedImprovement {
                        speedup_factor: 1.1,
                        memory_reduction: node.memory_usage / 4, // 25% reduction
                        energy_savings: 0.05,
                    },
                    implementation_difficulty: 3,
                    priority: OptimizationPriority::Medium,
                });
            }
        }

        Ok(opportunities)
    }

    fn analyze_bottlenecks(&self, graph: &ComputationGraph) -> Result<BottleneckAnalysis> {
        let mut bottleneck_nodes = Vec::new();
        let mut parallelizable_nodes = Vec::new();

        for node in graph.nodes.values() {
            if let Some(exec_time) = node.execution_time_us {
                if exec_time > self.config.bottleneck_threshold_us {
                    bottleneck_nodes.push(node.id.clone());
                }
            }

            // Check if node can be parallelized (simplified heuristic)
            match node.operation_type {
                OperationType::MatMul | OperationType::Conv2D | OperationType::Add => {
                    parallelizable_nodes.push(node.id.clone());
                },
                _ => {},
            }
        }

        let critical_path_nodes = self.find_critical_path(graph)?;
        let critical_path_time_us = critical_path_nodes
            .iter()
            .filter_map(|id| graph.nodes.get(id))
            .filter_map(|node| node.execution_time_us)
            .sum();

        let scheduling_suggestions = vec![
            "Consider parallel execution of independent operations".to_string(),
            "Use asynchronous execution for I/O operations".to_string(),
            "Implement pipeline parallelism for sequential operations".to_string(),
        ];

        Ok(BottleneckAnalysis {
            bottleneck_nodes,
            critical_path_nodes,
            critical_path_time_us,
            parallelizable_nodes,
            scheduling_suggestions,
        })
    }

    fn analyze_dataflow(&self, graph: &ComputationGraph) -> Result<DataFlowAnalysis> {
        let mut data_dependencies = HashMap::new();
        let mut live_variables = HashMap::new();
        let mut variable_lifetimes = HashMap::new();

        // Simplified dataflow analysis
        for (node_id, dependencies) in &graph.edges {
            data_dependencies.insert(node_id.clone(), dependencies.clone());
            live_variables.insert(node_id.clone(), dependencies.iter().cloned().collect());

            // Create variable lifetimes for dependencies
            for dep in dependencies {
                if !variable_lifetimes.contains_key(dep) {
                    variable_lifetimes.insert(
                        dep.clone(),
                        VariableLifetime {
                            birth_node: dep.clone(),
                            death_node: node_id.clone(),
                            usage_nodes: vec![node_id.clone()],
                            memory_footprint: graph
                                .nodes
                                .get(dep)
                                .map(|n| n.memory_usage)
                                .unwrap_or(0),
                        },
                    );
                } else {
                    let lifetime = variable_lifetimes
                        .get_mut(dep)
                        .expect("variable lifetime should exist for previously seen dependency");
                    lifetime.death_node = node_id.clone();
                    lifetime.usage_nodes.push(node_id.clone());
                }
            }
        }

        let memory_reuse_opportunities = vec![MemoryReuseOpportunity {
            reusable_variables: vec!["var1".to_string(), "var2".to_string()],
            memory_savings: 1024 * 1024, // 1MB
            complexity: 2,
        }];

        Ok(DataFlowAnalysis {
            data_dependencies,
            live_variables,
            variable_lifetimes,
            memory_reuse_opportunities,
        })
    }

    fn find_critical_path(&self, graph: &ComputationGraph) -> Result<Vec<String>> {
        // Simplified critical path finding - uses depth as proxy
        let mut path = Vec::new();
        let mut current_depth = graph.metadata.max_depth;

        while current_depth > 0 {
            // Find a node at the current depth
            for node in graph.nodes.values() {
                if node.depth == current_depth {
                    path.push(node.id.clone());
                    current_depth -= 1;
                    break;
                }
            }
            current_depth = current_depth.saturating_sub(1);
        }

        path.reverse();
        Ok(path)
    }

    fn calculate_statistics(&self, graph: &ComputationGraph) -> Result<GraphStatistics> {
        let mut nodes_by_type: HashMap<OperationType, usize> = HashMap::new();
        for node in graph.nodes.values() {
            *nodes_by_type.entry(node.operation_type.clone()).or_insert(0) += 1;
        }

        let total_fan_in: usize = graph.edges.values().map(|deps| deps.len()).sum();
        let total_fan_out = total_fan_in; // In a DAG, total fan-in equals total fan-out
        let average_fan_in = total_fan_in as f64 / graph.nodes.len() as f64;
        let average_fan_out = total_fan_out as f64 / graph.nodes.len() as f64;

        Ok(GraphStatistics {
            nodes_by_type,
            average_fan_in,
            average_fan_out,
            diameter: graph.metadata.max_depth,
            clustering_coefficient: 0.0, // Simplified - DAGs have clustering coefficient of 0
            strongly_connected_components: graph.nodes.len(), // Each node is its own SCC in a DAG
        })
    }

    fn generate_recommendations(&self, analysis: &GraphAnalysisResult) -> Result<Vec<String>> {
        let mut recommendations = Vec::new();

        // Memory-based recommendations
        if let Some(ref memory_analysis) = analysis.memory_analysis {
            if memory_analysis.total_memory_usage > 1024 * 1024 * 1024 {
                // > 1GB
                recommendations.push(
                    "Consider using gradient checkpointing to reduce memory usage".to_string(),
                );
            }
            if memory_analysis.fragmentation_ratio > 0.2 {
                recommendations
                    .push("Implement memory pooling to reduce fragmentation".to_string());
            }
        }

        // FLOP-based recommendations
        if let Some(ref flop_analysis) = analysis.flop_analysis {
            if flop_analysis.arithmetic_intensity < 1.0 {
                recommendations
                    .push("Consider kernel fusion to improve arithmetic intensity".to_string());
            }
            if flop_analysis.complexity_analysis.parallelization_potential > 0.5 {
                recommendations.push(
                    "Explore parallelization opportunities for compute-intensive operations"
                        .to_string(),
                );
            }
        }

        // Optimization opportunities
        if analysis.optimization_opportunities.len() > 3 {
            recommendations.push(
                "Multiple optimization opportunities detected - prioritize by estimated impact"
                    .to_string(),
            );
        }

        // Bottleneck recommendations
        if let Some(ref bottleneck_analysis) = analysis.bottleneck_analysis {
            if !bottleneck_analysis.bottleneck_nodes.is_empty() {
                recommendations.push(
                    "Address bottleneck operations through optimization or parallelization"
                        .to_string(),
                );
            }
        }

        Ok(recommendations)
    }

    fn get_node_color(&self, op_type: &OperationType) -> &'static str {
        match op_type {
            OperationType::MatMul | OperationType::Dot => "lightblue",
            OperationType::Add
            | OperationType::Subtract
            | OperationType::Multiply
            | OperationType::Divide => "lightgreen",
            OperationType::ReLU
            | OperationType::Sigmoid
            | OperationType::Tanh
            | OperationType::GELU => "orange",
            OperationType::LayerNorm | OperationType::BatchNorm | OperationType::RMSNorm => {
                "yellow"
            },
            OperationType::Conv1D | OperationType::Conv2D | OperationType::Conv3D => "lightcoral",
            OperationType::Attention | OperationType::MultiHeadAttention => "purple",
            OperationType::Embedding | OperationType::PositionalEmbedding => "pink",
            _ => "lightgray",
        }
    }
}

impl fmt::Display for OperationType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OperationType::Custom(name) => write!(f, "Custom({})", name),
            _ => write!(f, "{:?}", self),
        }
    }
}

impl Default for ComputationGraphAnalyzer {
    fn default() -> Self {
        Self::new(GraphAnalysisConfig::default())
    }
}

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

    #[test]
    fn test_computation_graph_creation() {
        let mut analyzer = ComputationGraphAnalyzer::default();

        let operations = vec![
            (
                "input".to_string(),
                OperationType::Custom("Input".to_string()),
                vec![],
            ),
            (
                "linear1".to_string(),
                OperationType::MatMul,
                vec!["input".to_string()],
            ),
            (
                "relu1".to_string(),
                OperationType::ReLU,
                vec!["linear1".to_string()],
            ),
            (
                "linear2".to_string(),
                OperationType::MatMul,
                vec!["relu1".to_string()],
            ),
            (
                "output".to_string(),
                OperationType::Custom("Output".to_string()),
                vec!["linear2".to_string()],
            ),
        ];

        let graph_id = analyzer
            .create_graph("test_model".to_string(), operations)
            .expect("operation failed in test");
        let analysis = analyzer.analyze_graph(graph_id).expect("operation failed in test");

        assert_eq!(analysis.statistics.nodes_by_type.len(), 4); // MatMul, ReLU, Custom("Input"), Custom("Output")
        assert!(!analysis.critical_path.is_empty());
    }

    #[test]
    fn test_optimization_detection() {
        let mut analyzer = ComputationGraphAnalyzer::default();

        let operations = vec![
            (
                "input".to_string(),
                OperationType::Custom("Input".to_string()),
                vec![],
            ),
            (
                "matmul".to_string(),
                OperationType::MatMul,
                vec!["input".to_string()],
            ),
            (
                "add".to_string(),
                OperationType::Add,
                vec!["matmul".to_string()],
            ),
        ];

        let graph_id = analyzer
            .create_graph("fusion_test".to_string(), operations)
            .expect("operation failed in test");
        let analysis = analyzer.analyze_graph(graph_id).expect("operation failed in test");

        assert!(analysis
            .optimization_opportunities
            .iter()
            .any(|op| op.optimization_type == OptimizationType::OperationFusion));
    }

    #[test]
    fn test_dot_export() {
        let mut analyzer = ComputationGraphAnalyzer::default();

        let operations = vec![
            ("a".to_string(), OperationType::MatMul, vec![]),
            ("b".to_string(), OperationType::ReLU, vec!["a".to_string()]),
        ];

        let graph_id = analyzer
            .create_graph("simple".to_string(), operations)
            .expect("operation failed in test");
        let dot = analyzer.export_to_dot(graph_id).expect("operation failed in test");

        assert!(dot.contains("digraph"));
        assert!(dot.contains("MatMul"));
        assert!(dot.contains("ReLU"));
    }
}