scirs2-autograd 0.3.2

Automatic differentiation module for SciRS2 (scirs2-autograd)
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
//! Computation graph visualization and analysis tools
//!
//! This module provides tools for:
//! - **DOT format export** (`graph_to_dot`) for Graphviz visualization
//! - **Text/JSON/Mermaid export** for various rendering backends
//! - **Graph statistics** (node counts, depth, memory estimates)
//! - **Structure analysis** (critical path, fan-in/fan-out)
//!
//! # Examples
//!
//! ## DOT export
//!
//! ```rust
//! use scirs2_autograd as ag;
//! use ag::tensor_ops;
//! use ag::visualization::{graph_to_dot, GraphStats};
//!
//! ag::run(|ctx: &mut ag::Context<f64>| {
//!     let x = ctx.placeholder("x", &[3]);
//!     let y = x * 2.0 + 1.0;
//!     let loss = tensor_ops::reduction::sum_all(y);
//!
//!     let dot = graph_to_dot(&loss, ctx);
//!     // Can be rendered with: dot -Tpng graph.dot -o graph.png
//!
//!     let stats = GraphStats::from_tensor(&loss, ctx);
//!     assert!(stats.total_nodes > 0);
//! });
//! ```

use crate::graph::{Graph, TensorID};
use crate::tensor::Tensor;
use crate::{Context, Float};
use std::collections::{HashMap, HashSet};
use std::fmt::Write;

// ---------------------------------------------------------------------------
// Graph traversal helpers
// ---------------------------------------------------------------------------

/// Collect all reachable node IDs from a root tensor by traversing backwards
/// through incoming edges. Returns nodes in topological order (dependencies first).
fn collect_reachable_nodes<F: Float>(root_id: TensorID, graph: &Graph<F>) -> Vec<TensorID> {
    let mut visited = HashSet::new();
    let mut order = Vec::new();
    collect_dfs(root_id, graph, &mut visited, &mut order);
    order
}

fn collect_dfs<F: Float>(
    node_id: TensorID,
    graph: &Graph<F>,
    visited: &mut HashSet<TensorID>,
    order: &mut Vec<TensorID>,
) {
    if visited.contains(&node_id) {
        return;
    }
    visited.insert(node_id);

    let node = graph.access_inner(node_id);
    let incoming = node.incoming_nodes.clone();
    drop(node); // release borrow

    for inc in &incoming {
        collect_dfs(inc.id, graph, visited, order);
    }

    order.push(node_id);
}

/// Information extracted from a single graph node.
struct NodeInfo {
    id: TensorID,
    op_name: String,
    topo_rank: usize,
    is_differentiable: bool,
    is_placeholder: bool,
    placeholder_name: Option<String>,
    is_variable: bool,
    num_inputs: usize,
    input_ids: Vec<TensorID>,
    known_shape: Option<Vec<isize>>,
}

fn extract_node_info<F: Float>(node_id: TensorID, graph: &Graph<F>) -> NodeInfo {
    let node = graph.access_inner(node_id);
    let op_name = node
        .op
        .as_ref()
        .map(|o| {
            let full = o.name();
            // Extract the short name (last segment after ::)
            full.rsplit("::").next().unwrap_or(full).to_string()
        })
        .unwrap_or_else(|| "Source".to_string());

    let input_ids: Vec<TensorID> = node.incoming_nodes.iter().map(|inc| inc.id).collect();
    let num_inputs = input_ids.len();

    let known_shape = node.knownshape.as_ref().map(|ks| ks.get().to_vec());

    NodeInfo {
        id: node_id,
        op_name,
        topo_rank: node.topo_rank,
        is_differentiable: node.is_differentiable,
        is_placeholder: node.placeholder_name.is_some(),
        placeholder_name: node.placeholder_name.map(|s| s.to_string()),
        is_variable: node.variable_id.is_some(),
        num_inputs,
        input_ids,
        known_shape,
    }
}

// ---------------------------------------------------------------------------
// DOT export
// ---------------------------------------------------------------------------

/// Export the computation graph reachable from `root` as a Graphviz DOT string.
///
/// The DOT output can be rendered to PNG/SVG/PDF using:
/// ```bash
/// echo "<dot output>" | dot -Tpng -o graph.png
/// ```
///
/// Node colors:
/// - **lightgreen**: placeholders (input data)
/// - **lightyellow**: variables (trainable parameters)
/// - **lightblue**: differentiable operations
/// - **lightgray**: non-differentiable operations
///
/// # Arguments
/// * `root` - The root tensor (typically the loss or final output)
/// * `ctx` - The autograd context
///
/// # Returns
/// A string in Graphviz DOT format.
pub fn graph_to_dot<'g, F: Float>(root: &Tensor<'g, F>, ctx: &'g Context<'g, F>) -> String {
    let graph = get_graph(ctx);
    let nodes = collect_reachable_nodes(root.id(), graph);
    let node_set: HashSet<TensorID> = nodes.iter().copied().collect();

    let mut output = String::new();
    let _ = writeln!(output, "digraph computation_graph {{");
    let _ = writeln!(output, "  rankdir=BT;");
    let _ = writeln!(
        output,
        "  node [shape=box, style=\"rounded,filled\", fontname=\"Helvetica\"];"
    );
    let _ = writeln!(output, "  edge [color=gray50];");
    let _ = writeln!(output);

    // Emit nodes
    for &nid in &nodes {
        let info = extract_node_info(nid, graph);
        let label = node_label(&info);
        let style = node_color(&info);
        let _ = writeln!(output, "  n{nid} [label=\"{label}\", {style}];");
    }

    let _ = writeln!(output);

    // Emit edges
    for &nid in &nodes {
        let info = extract_node_info(nid, graph);
        for &src in &info.input_ids {
            if node_set.contains(&src) {
                let _ = writeln!(output, "  n{src} -> n{nid};");
            }
        }
    }

    let _ = writeln!(output, "}}");
    output
}

fn node_label(info: &NodeInfo) -> String {
    let mut label = String::new();

    if let Some(ref name) = info.placeholder_name {
        let _ = write!(label, "{name}\\n");
    }

    let _ = write!(label, "{}", info.op_name);

    if let Some(ref shape) = info.known_shape {
        let _ = write!(label, "\\n{shape:?}");
    }

    let _ = write!(label, "\\n(id={})", info.id);
    label
}

fn node_color(info: &NodeInfo) -> String {
    if info.is_placeholder {
        "fillcolor=\"#d5f5d5\"".to_string() // light green
    } else if info.is_variable {
        "fillcolor=\"#fff8d5\"".to_string() // light yellow
    } else if info.is_differentiable {
        "fillcolor=\"#d5e8f5\"".to_string() // light blue
    } else {
        "fillcolor=\"#e8e8e8\"".to_string() // light gray
    }
}

// ---------------------------------------------------------------------------
// Text summary
// ---------------------------------------------------------------------------

/// Generate a compact text summary of the computation graph.
///
/// # Arguments
/// * `root` - The root tensor
/// * `ctx` - The autograd context
pub fn graph_summary<'g, F: Float>(root: &Tensor<'g, F>, ctx: &'g Context<'g, F>) -> String {
    let graph = get_graph(ctx);
    let nodes = collect_reachable_nodes(root.id(), graph);

    let mut output = String::new();
    let _ = writeln!(output, "Computation Graph Summary");
    let _ = writeln!(output, "=========================");
    let _ = writeln!(output, "Total nodes: {}", nodes.len());

    let mut placeholders = 0usize;
    let mut variables = 0usize;
    let mut ops = 0usize;
    let mut max_rank = 0usize;
    let mut op_counts: HashMap<String, usize> = HashMap::new();

    for &nid in &nodes {
        let info = extract_node_info(nid, graph);
        if info.is_placeholder {
            placeholders += 1;
        } else if info.is_variable {
            variables += 1;
        } else {
            ops += 1;
        }
        if info.topo_rank > max_rank {
            max_rank = info.topo_rank;
        }
        *op_counts.entry(info.op_name.clone()).or_insert(0) += 1;
    }

    let _ = writeln!(output, "  Placeholders: {placeholders}");
    let _ = writeln!(output, "  Variables: {variables}");
    let _ = writeln!(output, "  Operations: {ops}");
    let _ = writeln!(output, "  Max depth (topo rank): {max_rank}");
    let _ = writeln!(output);

    // Sort op counts by frequency
    let mut sorted_ops: Vec<_> = op_counts.into_iter().collect();
    sorted_ops.sort_by(|a, b| b.1.cmp(&a.1));

    let _ = writeln!(output, "Operation breakdown:");
    for (name, count) in &sorted_ops {
        let _ = writeln!(output, "  {name}: {count}");
    }

    output
}

// ---------------------------------------------------------------------------
// JSON export
// ---------------------------------------------------------------------------

/// Export the computation graph as a JSON string.
///
/// The JSON has the following structure:
/// ```json
/// {
///   "nodes": [{"id": 0, "op": "Placeholder", "rank": 0, ...}, ...],
///   "edges": [{"from": 0, "to": 1}, ...]
/// }
/// ```
pub fn graph_to_json<'g, F: Float>(root: &Tensor<'g, F>, ctx: &'g Context<'g, F>) -> String {
    let graph = get_graph(ctx);
    let nodes = collect_reachable_nodes(root.id(), graph);
    let node_set: HashSet<TensorID> = nodes.iter().copied().collect();

    let mut output = String::new();
    let _ = writeln!(output, "{{");
    let _ = writeln!(output, "  \"nodes\": [");

    for (idx, &nid) in nodes.iter().enumerate() {
        let info = extract_node_info(nid, graph);
        let comma = if idx + 1 < nodes.len() { "," } else { "" };
        let shape_str = info
            .known_shape
            .as_ref()
            .map(|s| format!("{s:?}"))
            .unwrap_or_else(|| "null".to_string());
        let _ = writeln!(
            output,
            "    {{\"id\": {}, \"op\": \"{}\", \"rank\": {}, \"differentiable\": {}, \"shape\": {}}}{}",
            info.id, info.op_name, info.topo_rank, info.is_differentiable, shape_str, comma
        );
    }

    let _ = writeln!(output, "  ],");
    let _ = writeln!(output, "  \"edges\": [");

    let mut edge_idx = 0usize;
    let total_edges: usize = nodes
        .iter()
        .map(|&nid| {
            let info = extract_node_info(nid, graph);
            info.input_ids
                .iter()
                .filter(|id| node_set.contains(id))
                .count()
        })
        .sum();

    for &nid in &nodes {
        let info = extract_node_info(nid, graph);
        for &src in &info.input_ids {
            if node_set.contains(&src) {
                edge_idx += 1;
                let comma = if edge_idx < total_edges { "," } else { "" };
                let _ = writeln!(
                    output,
                    "    {{\"from\": {src}, \"to\": {}}}{comma}",
                    info.id
                );
            }
        }
    }

    let _ = writeln!(output, "  ]");
    let _ = writeln!(output, "}}");
    output
}

// ---------------------------------------------------------------------------
// Mermaid export
// ---------------------------------------------------------------------------

/// Export the computation graph as a Mermaid diagram string.
///
/// Can be embedded in Markdown or rendered at <https://mermaid.live>.
pub fn graph_to_mermaid<'g, F: Float>(root: &Tensor<'g, F>, ctx: &'g Context<'g, F>) -> String {
    let graph = get_graph(ctx);
    let nodes = collect_reachable_nodes(root.id(), graph);
    let node_set: HashSet<TensorID> = nodes.iter().copied().collect();

    let mut output = String::new();
    let _ = writeln!(output, "graph BT");

    for &nid in &nodes {
        let info = extract_node_info(nid, graph);
        let label = if let Some(ref name) = info.placeholder_name {
            format!("{name}: {}", info.op_name)
        } else {
            info.op_name.clone()
        };
        let _ = writeln!(output, "  N{nid}[\"{label}\"]");
    }

    for &nid in &nodes {
        let info = extract_node_info(nid, graph);
        for &src in &info.input_ids {
            if node_set.contains(&src) {
                let _ = writeln!(output, "  N{src} --> N{nid}");
            }
        }
    }

    output
}

// ---------------------------------------------------------------------------
// Graph statistics
// ---------------------------------------------------------------------------

/// Statistics about a computation graph.
#[derive(Debug, Clone)]
pub struct GraphStats {
    /// Total number of nodes
    pub total_nodes: usize,
    /// Number of placeholder (input) nodes
    pub num_placeholders: usize,
    /// Number of variable (trainable parameter) nodes
    pub num_variables: usize,
    /// Number of operation nodes
    pub num_operations: usize,
    /// Maximum topological rank (depth of the graph)
    pub max_depth: usize,
    /// Number of edges (connections between nodes)
    pub num_edges: usize,
    /// Number of differentiable nodes
    pub num_differentiable: usize,
    /// Operation type breakdown: (op_name, count)
    pub op_breakdown: Vec<(String, usize)>,
    /// Maximum fan-in (most inputs to a single node)
    pub max_fan_in: usize,
    /// Maximum fan-out (most nodes that depend on a single node)
    pub max_fan_out: usize,
}

impl GraphStats {
    /// Compute graph statistics from a root tensor.
    pub fn from_tensor<'g, F: Float>(root: &Tensor<'g, F>, ctx: &'g Context<'g, F>) -> Self {
        let graph = get_graph(ctx);
        let nodes = collect_reachable_nodes(root.id(), graph);

        let mut num_placeholders = 0usize;
        let mut num_variables = 0usize;
        let mut num_operations = 0usize;
        let mut num_differentiable = 0usize;
        let mut max_depth = 0usize;
        let mut num_edges = 0usize;
        let mut max_fan_in = 0usize;
        let mut op_counts: HashMap<String, usize> = HashMap::new();
        let mut fan_out: HashMap<TensorID, usize> = HashMap::new();

        for &nid in &nodes {
            let info = extract_node_info(nid, graph);

            if info.is_placeholder {
                num_placeholders += 1;
            } else if info.is_variable {
                num_variables += 1;
            } else {
                num_operations += 1;
            }

            if info.is_differentiable {
                num_differentiable += 1;
            }

            if info.topo_rank > max_depth {
                max_depth = info.topo_rank;
            }

            num_edges += info.num_inputs;

            if info.num_inputs > max_fan_in {
                max_fan_in = info.num_inputs;
            }

            for &src in &info.input_ids {
                *fan_out.entry(src).or_insert(0) += 1;
            }

            *op_counts.entry(info.op_name).or_insert(0) += 1;
        }

        let max_fan_out = fan_out.values().copied().max().unwrap_or(0);

        let mut op_breakdown: Vec<_> = op_counts.into_iter().collect();
        op_breakdown.sort_by(|a, b| b.1.cmp(&a.1));

        GraphStats {
            total_nodes: nodes.len(),
            num_placeholders,
            num_variables,
            num_operations,
            max_depth,
            num_edges,
            num_differentiable,
            op_breakdown,
            max_fan_in,
            max_fan_out,
        }
    }

    /// Format the statistics as a human-readable string.
    pub fn display(&self) -> String {
        let mut output = String::new();
        let _ = writeln!(output, "Graph Statistics");
        let _ = writeln!(output, "================");
        let _ = writeln!(output, "Total nodes:      {}", self.total_nodes);
        let _ = writeln!(output, "Placeholders:     {}", self.num_placeholders);
        let _ = writeln!(output, "Variables:        {}", self.num_variables);
        let _ = writeln!(output, "Operations:       {}", self.num_operations);
        let _ = writeln!(output, "Edges:            {}", self.num_edges);
        let _ = writeln!(output, "Max depth:        {}", self.max_depth);
        let _ = writeln!(output, "Differentiable:   {}", self.num_differentiable);
        let _ = writeln!(output, "Max fan-in:       {}", self.max_fan_in);
        let _ = writeln!(output, "Max fan-out:      {}", self.max_fan_out);
        let _ = writeln!(output);
        let _ = writeln!(output, "Operation breakdown:");
        for (name, count) in &self.op_breakdown {
            let _ = writeln!(output, "  {name}: {count}");
        }
        output
    }
}

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Configuration for graph visualization.
#[derive(Debug, Clone)]
pub struct VisualizationConfig {
    /// Whether to show tensor shapes in nodes
    pub show_shapes: bool,
    /// Whether to show operation names
    pub show_operations: bool,
    /// Whether to show gradient flow
    pub show_gradients: bool,
    /// Maximum number of nodes to display
    pub max_nodes: Option<usize>,
    /// Output format
    pub format: OutputFormat,
    /// Whether to include values (for small tensors)
    pub show_values: bool,
}

impl Default for VisualizationConfig {
    fn default() -> Self {
        Self {
            show_shapes: true,
            show_operations: true,
            show_gradients: false,
            max_nodes: Some(100),
            format: OutputFormat::Dot,
            show_values: false,
        }
    }
}

/// Output format for graph visualization.
#[derive(Debug, Clone, Copy)]
pub enum OutputFormat {
    /// Graphviz DOT format
    Dot,
    /// Simple text representation
    Text,
    /// JSON format for web visualization
    Json,
    /// Mermaid diagram format
    Mermaid,
}

/// Errors that can occur during visualization.
#[derive(Debug, thiserror::Error)]
pub enum VisualizationError {
    #[error("Graph traversal error: {0}")]
    GraphTraversal(String),
    #[error("Format error: {0}")]
    Format(#[from] std::fmt::Error),
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Invalid configuration: {0}")]
    Config(String),
}

// ---------------------------------------------------------------------------
// Helper: get Graph from Context via Deref
// ---------------------------------------------------------------------------

/// Get the underlying Graph from a Context via Deref.
fn get_graph<'g, F: Float>(ctx: &'g Context<'g, F>) -> &'g Graph<F> {
    use std::ops::Deref;
    ctx.deref()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_graph_to_dot_basic() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[3]);
            let y = x * 2.0;
            let loss = crate::tensor_ops::reduction::sum_all(y);

            let dot = graph_to_dot(&loss, ctx);
            assert!(dot.contains("digraph computation_graph"));
            assert!(dot.contains("->"));
            assert!(dot.contains("}"));
        });
    }

    #[test]
    fn test_graph_to_dot_multi_input() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[2]);
            let y = ctx.placeholder("y", &[2]);
            let z = x + y;
            let loss = crate::tensor_ops::reduction::sum_all(z);

            let dot = graph_to_dot(&loss, ctx);
            assert!(dot.contains("digraph computation_graph"));
            // Should have nodes for x, y, z, and loss
            assert!(dot.contains("fillcolor"));
        });
    }

    #[test]
    fn test_graph_summary_basic() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[3]);
            let y = x * 2.0 + 1.0;
            let loss = crate::tensor_ops::reduction::sum_all(y);

            let summary = graph_summary(&loss, ctx);
            assert!(summary.contains("Computation Graph Summary"));
            assert!(summary.contains("Total nodes:"));
            assert!(summary.contains("Placeholders:"));
        });
    }

    #[test]
    fn test_graph_to_json() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[2]);
            let y = x * 3.0;

            let json = graph_to_json(&y, ctx);
            assert!(json.contains("\"nodes\""));
            assert!(json.contains("\"edges\""));
            assert!(json.contains("\"op\""));
        });
    }

    #[test]
    fn test_graph_to_mermaid() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[2]);
            let y = x * 2.0;

            let mermaid = graph_to_mermaid(&y, ctx);
            assert!(mermaid.contains("graph BT"));
            assert!(mermaid.contains("-->"));
        });
    }

    #[test]
    fn test_graph_stats() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[3]);
            let y = x * 2.0 + 1.0;
            let loss = crate::tensor_ops::reduction::sum_all(y);

            let stats = GraphStats::from_tensor(&loss, ctx);
            assert!(stats.total_nodes > 0);
            assert!(stats.num_placeholders >= 1);
            assert!(stats.num_edges > 0);
            assert!(stats.max_depth > 0);
            assert!(!stats.op_breakdown.is_empty());
        });
    }

    #[test]
    fn test_graph_stats_display() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[3]);
            let loss = crate::tensor_ops::reduction::sum_all(x * x);

            let stats = GraphStats::from_tensor(&loss, ctx);
            let display = stats.display();
            assert!(display.contains("Graph Statistics"));
            assert!(display.contains("Total nodes:"));
            assert!(display.contains("Max fan-in:"));
            assert!(display.contains("Max fan-out:"));
        });
    }

    #[test]
    fn test_graph_dot_colors() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[2]);
            let y = x * 2.0;

            let dot = graph_to_dot(&y, ctx);
            // Should contain color codes for different node types
            assert!(dot.contains("fillcolor"));
        });
    }

    #[test]
    fn test_visualization_config_default() {
        let config = VisualizationConfig::default();
        assert!(config.show_shapes);
        assert!(config.show_operations);
        assert!(!config.show_gradients);
        assert_eq!(config.max_nodes, Some(100));
        assert!(matches!(config.format, OutputFormat::Dot));
    }

    #[test]
    fn test_graph_stats_single_node() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[]);

            let stats = GraphStats::from_tensor(&x, ctx);
            assert_eq!(stats.total_nodes, 1);
            assert_eq!(stats.num_placeholders, 1);
            assert_eq!(stats.num_operations, 0);
            assert_eq!(stats.num_edges, 0);
        });
    }

    #[test]
    fn test_collect_reachable_shared_nodes() {
        crate::run(|ctx: &mut crate::Context<f64>| {
            let x = ctx.placeholder("x", &[2]);
            // x is used twice -> shared node
            let y = x + x;
            let loss = crate::tensor_ops::reduction::sum_all(y);

            let graph: &Graph<f64> = std::ops::Deref::deref(ctx);
            let nodes = collect_reachable_nodes(loss.id(), graph);
            // x should appear only once despite being used twice
            let x_count = nodes.iter().filter(|&&id| id == x.id()).count();
            assert_eq!(x_count, 1);
        });
    }
}