pytensor icon indicating copy to clipboard operation
pytensor copied to clipboard

Support for graph to mermaid

Open williambdean opened this issue 6 months ago • 4 comments

Similar to https://github.com/pymc-devs/pymc/pull/7826 but for the pytensor graph.

What are the different types of nodes in the graph? Do plates / subgraphs exist (would this be each op)? What is the idea here?

CC: @ricardoV94

williambdean avatar Jun 20 '25 11:06 williambdean

Syntax details:

https://mermaid.js.org/syntax/flowchart.html

williambdean avatar Jun 20 '25 11:06 williambdean

plates may be useful for Ops with inner graphs (OpFromGraph / Composite / Scan). Otherwise pytensor has two types of nodes: Apply and Variable. Variable(s) -> Apply(Op) -> Variable(s). You may want to distinguish root inputs/outputs visually as well.

You may also want to distinguish visually different variable types (Tensor, Scalar, RNG, Slice, None, ...)

ricardoV94 avatar Jun 20 '25 12:06 ricardoV94

Any preferences on the node shapes?

https://mermaid.js.org/syntax/flowchart.html#node-shapes

Also the different arrow types

Is there a similar function to get nodes, edges? What are the main traversal functions?

williambdean avatar Jun 20 '25 12:06 williambdean

Is there a similar function to get nodes, edges? What are the main traversal functions?

You can go from outputs to inputs via x.owner.inputs, the other way around you need to crate a FunctionGraph, and you have a dictionary clients that goes in the other direction. x is a variable, owner is the apply. Root variables have no owner

ricardoV94 avatar Jun 20 '25 13:06 ricardoV94