Implementation:Avhz RustQuant Graphviz
| Knowledge Sources | |
|---|---|
| Domains | Automatic_Differentiation, Mathematics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
The graphviz module generates DOT language output for visualizing the computational graph built during automatic differentiation.
Description
This module provides the graphviz function, which takes a reference to a Graph and a slice of Variable values (the input variables to highlight) and produces a String in the Graphviz DOT language. The generated DOT code can be rendered by any Graphviz-compatible viewer to produce a visual diagram of the computation graph.
The function works as follows:
- Graph initialization: It starts a directed graph (
digraph) with a transparent background, left-to-right layout (rankdir="LR"), and 3D box-shaped nodes. - Node definitions: Each vertex in the graph is emitted as a node. Variables that match the provided input variable indices are labeled in red with their input index and value (e.g.,
"Input: x_0, Value: 2.00"). All other nodes are labeled as operations with their graph index (e.g.,"Op: #5"). - Edge definitions: For each vertex, edges are drawn from each parent to the vertex, labeled with the partial derivative value for that edge (using the Unicode partial derivative symbol).
The module uses a HashSet for efficient lookup of which indices correspond to input variables. The output is a self-contained DOT string that can be piped to the dot command-line tool or pasted into a web-based Graphviz viewer.
This module is noted as a work in progress in the source code.
Usage
Use this function to debug or visualize the structure of a computation graph. This is especially useful for understanding how complex expressions are decomposed into elementary operations and verifying that the graph topology is correct. Generate the DOT output and render it with a Graphviz tool to see a diagram of the computation graph.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_autodiff/src/graphviz.rs
- Lines: 1-134
Signature
/// Graphviz dot string.
pub fn graphviz(graph: &Graph, vars: &[Variable]) -> String;
Import
use RustQuant::autodiff::graphviz;
use RustQuant::autodiff::{Graph, Variable};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| graph | &Graph |
Yes | A reference to the computational graph containing all vertices and edges from the recorded operations. |
| vars | &[Variable] |
Yes | A slice of input variables to highlight in the visualization. These are rendered in red with their values displayed. |
Outputs
| Name | Type | Description |
|---|---|---|
| dot | String |
A DOT-language string representing the directed graph. Nodes represent variables and operations; edges represent data flow annotated with partial derivative values. The string is ready to be rendered by Graphviz tools. |
Usage Examples
use RustQuant_autodiff::*;
let graph = Graph::new();
let x = graph.var(2.0);
let y = graph.var(3.0);
// Build a computation: z = x * y, then u = sin(exp(z))
let z = x * y;
let _u = (z.exp()).sin();
// Generate the DOT string for visualization
let dot_output = graphviz(&graph, &[x, y]);
// Print to stdout -- copy the output into a Graphviz viewer
// e.g., https://dreampuf.github.io/GraphvizOnline/
print!("{}", dot_output);
// Example output (abbreviated):
// digraph Graph {
// bgcolor="transparent";
// rankdir="LR";
// node [shape=box3d];
// 0 [label="Input: x_0, Value: 2.00", color="red"];
// 1 [label="Input: x_1, Value: 3.00", color="red"];
// 2 [label="Op: #2"];
// ...
// }
Complex Expression Example
use RustQuant_autodiff::*;
let graph = Graph::new();
let a = graph.var(1.0);
let b = graph.var(2.0);
let c = graph.var(3.0);
let f = (a.exp() + b.cbrt()).sin() + graph.var(5.0) * (c.sqrt() + graph.var(4.0).powf(2.0)).ln();
// Visualize the full computation graph
let dot = graphviz(&graph, &[a, b, c]);
print!("{}", dot);
// Combine with gradient computation
let grad = f.accumulate();
println!("Gradient: {:.4?}", grad.wrt(&[a, b, c]));