tensorlogic-ir 0.1.0

Intermediate representation (IR) and AST types for TensorLogic
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
//! DOT format export for graph visualization.
//!
//! This module provides utilities to export `EinsumGraph` to DOT format
//! for visualization with Graphviz and similar tools.
//!
//! # Example
//!
//! ```
//! use tensorlogic_ir::{EinsumGraph, EinsumNode};
//!
//! let mut graph = EinsumGraph::new();
//! let t0 = graph.add_tensor("input".to_string());
//! let t1 = graph.add_tensor("output".to_string());
//! let node = EinsumNode::elem_unary("relu", t0, t1);
//! graph.add_node(node).expect("unwrap");
//!
//! let dot = tensorlogic_ir::export_to_dot(&graph);
//! println!("{}", dot);
//! ```

use crate::graph::{EinsumGraph, EinsumNode, OpType};
use std::collections::{HashMap, HashSet};
use std::fmt::Write as FmtWrite;

/// Export an `EinsumGraph` to DOT format.
///
/// The resulting DOT string can be rendered with Graphviz:
/// ```bash
/// echo "..." | dot -Tpng > graph.png
/// echo "..." | dot -Tsvg > graph.svg
/// ```
///
/// # Layout Options
///
/// The generated DOT uses the following attributes:
/// - **Tensor nodes**: Boxes with blue color
/// - **Operation nodes**: Ellipses with green color
/// - **Edges**: Show data flow from inputs to operations to outputs
///
/// # Example
///
/// ```
/// use tensorlogic_ir::{EinsumGraph, EinsumNode, export_to_dot};
///
/// let mut graph = EinsumGraph::new();
/// let input = graph.add_tensor("x".to_string());
/// let output = graph.add_tensor("y".to_string());
///
/// let node = EinsumNode::elem_unary("relu", input, output);
/// graph.add_node(node).expect("unwrap");
///
/// let dot = export_to_dot(&graph);
/// assert!(dot.contains("digraph"));
/// assert!(dot.contains("relu"));
/// ```
pub fn export_to_dot(graph: &EinsumGraph) -> String {
    let mut output = String::new();
    export_to_dot_writer(graph, &mut output).expect("String write should not fail");
    output
}

/// Export an `EinsumGraph` to DOT format with custom options.
///
/// # Options
///
/// - `show_tensor_ids`: Show tensor indices in labels
/// - `show_node_ids`: Show node indices in labels
/// - `show_metadata`: Include metadata in node labels
/// - `cluster_by_operation`: Group operations by type
/// - `horizontal_layout`: Use left-to-right layout instead of top-to-bottom
///
/// # Example
///
/// ```
/// use tensorlogic_ir::{EinsumGraph, EinsumNode, DotExportOptions, export_to_dot_with_options};
///
/// let mut graph = EinsumGraph::new();
/// let t0 = graph.add_tensor("input".to_string());
/// let t1 = graph.add_tensor("output".to_string());
/// let node = EinsumNode::elem_unary("sigmoid", t0, t1);
/// graph.add_node(node).expect("unwrap");
///
/// let options = DotExportOptions {
///     show_tensor_ids: true,
///     show_node_ids: true,
///     horizontal_layout: true,
///     ..Default::default()
/// };
///
/// let dot = export_to_dot_with_options(&graph, &options);
/// assert!(dot.contains("rankdir=LR"));
/// ```
pub fn export_to_dot_with_options(graph: &EinsumGraph, options: &DotExportOptions) -> String {
    let mut output = String::new();
    export_to_dot_writer_with_options(graph, &mut output, options)
        .expect("String write should not fail");
    output
}

/// Options for DOT export customization.
#[derive(Debug, Clone, Default)]
pub struct DotExportOptions {
    /// Show tensor indices in labels (e.g., "tensor_0 \[0\]")
    pub show_tensor_ids: bool,
    /// Show node indices in labels (e.g., "op_0")
    pub show_node_ids: bool,
    /// Include metadata in node labels
    pub show_metadata: bool,
    /// Group operations by type (einsum, elem_unary, elem_binary, reduce)
    pub cluster_by_operation: bool,
    /// Use horizontal (left-to-right) layout instead of vertical
    pub horizontal_layout: bool,
    /// Include tensor shapes in labels (if available)
    pub show_shapes: bool,
    /// Highlight specific tensors (by name or index)
    pub highlight_tensors: Vec<String>,
    /// Highlight specific operations (by index)
    pub highlight_nodes: Vec<usize>,
}

/// Export to DOT format writing to a generic writer.
pub fn export_to_dot_writer<W: FmtWrite>(graph: &EinsumGraph, writer: &mut W) -> std::fmt::Result {
    let options = DotExportOptions::default();
    export_to_dot_writer_with_options(graph, writer, &options)
}

/// Export to DOT format with options, writing to a generic writer.
pub fn export_to_dot_writer_with_options<W: FmtWrite>(
    graph: &EinsumGraph,
    writer: &mut W,
    options: &DotExportOptions,
) -> std::fmt::Result {
    writeln!(writer, "digraph EinsumGraph {{")?;

    // Graph attributes
    writeln!(writer, "  // Graph styling")?;
    writeln!(writer, "  graph [fontname=\"Helvetica\", fontsize=10];")?;
    writeln!(writer, "  node [fontname=\"Helvetica\", fontsize=10];")?;
    writeln!(writer, "  edge [fontname=\"Helvetica\", fontsize=9];")?;

    if options.horizontal_layout {
        writeln!(writer, "  rankdir=LR;")?;
    }

    writeln!(writer)?;

    // Group operations by type if requested
    let mut op_clusters: HashMap<String, Vec<usize>> = HashMap::new();
    if options.cluster_by_operation {
        for (idx, node) in graph.nodes.iter().enumerate() {
            let cluster_name = match &node.op {
                OpType::Einsum { .. } => "einsum",
                OpType::ElemUnary { .. } => "elem_unary",
                OpType::ElemBinary { .. } => "elem_binary",
                OpType::Reduce { .. } => "reduce",
            };
            op_clusters
                .entry(cluster_name.to_string())
                .or_default()
                .push(idx);
        }
    }

    // Collect input and output tensors
    let mut used_tensors = HashSet::new();
    for node in &graph.nodes {
        for &input in &node.inputs {
            used_tensors.insert(input);
        }
        for &output in &node.outputs {
            used_tensors.insert(output);
        }
    }

    // Write tensor nodes
    writeln!(writer, "  // Tensor nodes")?;
    for (idx, tensor_name) in graph.tensors.iter().enumerate() {
        if !used_tensors.contains(&idx) && !graph.inputs.contains(&idx) {
            continue; // Skip unused tensors
        }

        let label = if options.show_tensor_ids {
            format!("{} [{}]", escape_label(tensor_name), idx)
        } else {
            escape_label(tensor_name)
        };

        let is_input = graph.inputs.contains(&idx);
        let is_output = graph.outputs.contains(&idx);
        let is_highlighted = options.highlight_tensors.contains(tensor_name)
            || options
                .highlight_tensors
                .contains(&format!("tensor_{}", idx));

        let color = if is_highlighted {
            "red"
        } else if is_input && is_output {
            "purple"
        } else if is_input {
            "lightblue"
        } else if is_output {
            "lightgreen"
        } else {
            "lightyellow"
        };

        writeln!(
            writer,
            "  tensor_{} [label=\"{}\", shape=box, style=filled, fillcolor={}];",
            idx, label, color
        )?;
    }

    writeln!(writer)?;

    // Write operation nodes, possibly clustered
    if options.cluster_by_operation && !op_clusters.is_empty() {
        for (cluster_name, node_indices) in &op_clusters {
            writeln!(
                writer,
                "  subgraph cluster_{} {{",
                cluster_name.replace('.', "_")
            )?;
            writeln!(writer, "    label=\"{}\";", cluster_name)?;
            writeln!(writer, "    style=dashed;")?;

            for &node_idx in node_indices {
                write_operation_node(writer, &graph.nodes[node_idx], node_idx, options)?;
            }

            writeln!(writer, "  }}")?;
            writeln!(writer)?;
        }
    } else {
        writeln!(writer, "  // Operation nodes")?;
        for (idx, node) in graph.nodes.iter().enumerate() {
            write_operation_node(writer, node, idx, options)?;
        }
        writeln!(writer)?;
    }

    // Write edges
    writeln!(writer, "  // Data flow edges")?;
    for (node_idx, node) in graph.nodes.iter().enumerate() {
        // Input edges
        for &input_tensor in &node.inputs {
            writeln!(writer, "  tensor_{} -> op_{};", input_tensor, node_idx)?;
        }

        // Output edges
        for &output_tensor in &node.outputs {
            writeln!(writer, "  op_{} -> tensor_{};", node_idx, output_tensor)?;
        }
    }

    writeln!(writer, "}}")?;

    Ok(())
}

/// Write a single operation node to the DOT output.
fn write_operation_node<W: FmtWrite>(
    writer: &mut W,
    node: &EinsumNode,
    idx: usize,
    options: &DotExportOptions,
) -> std::fmt::Result {
    let (op_type, op_label) = match &node.op {
        OpType::Einsum { spec } => ("einsum", format!("einsum\\n{}", escape_label(spec))),
        OpType::ElemUnary { op } => ("elem_unary", format!("{}(·)", escape_label(op))),
        OpType::ElemBinary { op } => ("elem_binary", format!("{}(·,·)", escape_label(op))),
        OpType::Reduce { op, axes } => ("reduce", format!("{}(axes={:?})", escape_label(op), axes)),
    };

    let label = if options.show_node_ids {
        format!("{}\\n[op_{}]", op_label, idx)
    } else {
        op_label
    };

    let is_highlighted = options.highlight_nodes.contains(&idx);
    let color = if is_highlighted {
        "orange"
    } else {
        match op_type {
            "einsum" => "lightcyan",
            "elem_unary" => "lightgreen",
            "elem_binary" => "lightyellow",
            "reduce" => "lightpink",
            _ => "white",
        }
    };

    writeln!(
        writer,
        "  op_{} [label=\"{}\", shape=ellipse, style=filled, fillcolor={}];",
        idx, label, color
    )?;

    Ok(())
}

/// Escape special characters in DOT labels.
fn escape_label(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{EinsumGraph, EinsumNode};

    #[test]
    fn test_export_empty_graph() {
        let graph = EinsumGraph::new();
        let dot = export_to_dot(&graph);
        assert!(dot.contains("digraph EinsumGraph"));
    }

    #[test]
    fn test_export_simple_operation() {
        let mut graph = EinsumGraph::new();
        let t0 = graph.add_tensor("input".to_string());
        let t1 = graph.add_tensor("output".to_string());

        let node = EinsumNode::elem_unary("relu", t0, t1);
        graph.add_node(node).expect("unwrap");

        let dot = export_to_dot(&graph);
        assert!(dot.contains("relu"));
        assert!(dot.contains("tensor_0"));
        assert!(dot.contains("tensor_1"));
        assert!(dot.contains("op_0"));
    }

    #[test]
    fn test_export_with_einsum() {
        let mut graph = EinsumGraph::new();
        let t0 = graph.add_tensor("A".to_string());
        let t1 = graph.add_tensor("B".to_string());
        let t2 = graph.add_tensor("C".to_string());

        let node = EinsumNode::einsum("ij,jk->ik", vec![t0, t1], vec![t2]);
        graph.add_node(node).expect("unwrap");

        let dot = export_to_dot(&graph);
        assert!(dot.contains("einsum"));
        assert!(dot.contains("ij,jk->ik"));
    }

    #[test]
    fn test_export_with_options() {
        let mut graph = EinsumGraph::new();
        let t0 = graph.add_tensor("x".to_string());
        let t1 = graph.add_tensor("y".to_string());

        let node = EinsumNode::elem_unary("sigmoid", t0, t1);
        graph.add_node(node).expect("unwrap");

        let options = DotExportOptions {
            show_tensor_ids: true,
            show_node_ids: true,
            horizontal_layout: true,
            ..Default::default()
        };

        let dot = export_to_dot_with_options(&graph, &options);
        assert!(dot.contains("rankdir=LR"));
        assert!(dot.contains("[0]")); // Tensor ID
        assert!(dot.contains("[op_0]")); // Node ID
    }

    #[test]
    fn test_export_with_clustering() {
        let mut graph = EinsumGraph::new();
        let t0 = graph.add_tensor("a".to_string());
        let t1 = graph.add_tensor("b".to_string());
        let t2 = graph.add_tensor("c".to_string());
        let t3 = graph.add_tensor("d".to_string());

        graph
            .add_node(EinsumNode::elem_unary("relu", t0, t1))
            .expect("unwrap");
        graph
            .add_node(EinsumNode::elem_unary("sigmoid", t1, t2))
            .expect("unwrap");
        graph
            .add_node(EinsumNode::elem_binary("add", t2, t0, t3))
            .expect("unwrap");

        let options = DotExportOptions {
            cluster_by_operation: true,
            ..Default::default()
        };

        let dot = export_to_dot_with_options(&graph, &options);
        assert!(dot.contains("subgraph cluster_elem_unary"));
        assert!(dot.contains("subgraph cluster_elem_binary"));
    }

    #[test]
    fn test_export_with_highlights() {
        let mut graph = EinsumGraph::new();
        let t0 = graph.add_tensor("input".to_string());
        let t1 = graph.add_tensor("hidden".to_string());
        let t2 = graph.add_tensor("output".to_string());

        graph
            .add_node(EinsumNode::elem_unary("relu", t0, t1))
            .expect("unwrap");
        graph
            .add_node(EinsumNode::elem_unary("softmax", t1, t2))
            .expect("unwrap");

        let options = DotExportOptions {
            highlight_tensors: vec!["output".to_string()],
            highlight_nodes: vec![0],
            ..Default::default()
        };

        let dot = export_to_dot_with_options(&graph, &options);
        assert!(dot.contains("red")); // Highlighted tensor
        assert!(dot.contains("orange")); // Highlighted operation
    }

    #[test]
    fn test_label_escaping() {
        assert_eq!(escape_label("hello\"world"), "hello\\\"world");
        assert_eq!(escape_label("line1\nline2"), "line1\\nline2");
        assert_eq!(escape_label("path\\to\\file"), "path\\\\to\\\\file");
    }

    #[test]
    fn test_complex_graph_export() {
        let mut graph = EinsumGraph::new();

        // Build a more complex graph: (a + b) * c
        let a = graph.add_tensor("a".to_string());
        let b = graph.add_tensor("b".to_string());
        let c = graph.add_tensor("c".to_string());
        let sum = graph.add_tensor("sum".to_string());
        let result = graph.add_tensor("result".to_string());

        graph.inputs = vec![a, b, c];
        graph.outputs = vec![result];

        graph
            .add_node(EinsumNode::elem_binary("add", a, b, sum))
            .expect("unwrap");
        graph
            .add_node(EinsumNode::elem_binary("multiply", sum, c, result))
            .expect("unwrap");

        let dot = export_to_dot(&graph);

        // Verify structure
        assert!(dot.contains("tensor_0")); // a
        assert!(dot.contains("tensor_1")); // b
        assert!(dot.contains("tensor_2")); // c
        assert!(dot.contains("tensor_3")); // sum
        assert!(dot.contains("tensor_4")); // result
        assert!(dot.contains("op_0")); // add
        assert!(dot.contains("op_1")); // multiply

        // Verify edges
        assert!(dot.contains("tensor_0 -> op_0")); // a -> add
        assert!(dot.contains("tensor_1 -> op_0")); // b -> add
        assert!(dot.contains("op_0 -> tensor_3")); // add -> sum
        assert!(dot.contains("tensor_3 -> op_1")); // sum -> multiply
        assert!(dot.contains("tensor_2 -> op_1")); // c -> multiply
        assert!(dot.contains("op_1 -> tensor_4")); // multiply -> result
    }
}