feat: add debugprint(file="rich") to return a rich.tree.Tree#2042
Open
williambdean wants to merge 8 commits intopymc-devs:v3from
Open
feat: add debugprint(file="rich") to return a rich.tree.Tree#2042williambdean wants to merge 8 commits intopymc-devs:v3from
williambdean wants to merge 8 commits intopymc-devs:v3from
Conversation
Contributor
Author
|
The Mermaid renderer sketchimport re
import pytensor.tensor as pt
from pytensor.printing import _build_label, _iter_graph_nodes
def build_mermaid(var) -> str:
done: dict = {}
used_ids: dict = {}
node_defs: dict[str, str] = {}
edges: list[tuple[str, str]] = []
apply_to_mid: dict[int, str] = {}
for gnode in _iter_graph_nodes(var, done=done):
label = _build_label(
gnode, done=done, used_ids=used_ids, id_type="CHAR",
print_type=False, print_shape=False, print_destroy_map=False,
print_view_map=False, print_op_info=False, op_information={},
)
mid = re.search(r"\[id ([^\]]+)\]", label).group(1)
if mid not in node_defs:
node_defs[mid] = label
if gnode.parent_node is not None:
parent_mid = apply_to_mid.get(id(gnode.parent_node))
if parent_mid is not None:
edges.append((parent_mid, mid))
if gnode.var.owner is not None:
apply_to_mid[id(gnode.var.owner)] = mid
lines = ["graph TD"]
for mid, label in node_defs.items():
lines.append(f' {mid}["{label.replace(chr(34), "#quot;")}"]')
for src, dst in edges:
lines.append(f" {src} --> {dst}")
return "\n".join(lines)
x = pt.dvector("x")
print(build_mermaid((x * 2).sum()))Output: graph TD
A["Sum{axes=None} [id A]"]
B["Mul [id B]"]
C["x [id C]"]
D["ExpandDims{axis=0} [id D]"]
E["2 [id E]"]
A --> B
B --> C
B --> D
D --> E
This would also address #1488. |
col_bars was iterating over all of ancestor_is_last, producing one extra 3-char column segment per node. The root-level ancestor entry should be skipped (ancestor_is_last[1:]) because the root contributes an empty prefix_child and column bar accumulation starts from depth-1 onwards. This restores byte-for-byte compatibility with the pre-refactor text output.
ricardoV94
reviewed
Apr 10, 2026
The old approach marked a repeat/stop_on_name node itself as is_repeat=True, replacing its label with ···. The new approach yields the node normally (label visible) and then yields a separate sentinel child with is_repeat=True one level deeper, so ··· appears indented below the node. Also moves done[node] = "" to before yield gnode so DAG-diamond marking is unconditional regardless of early generator abandonment.
Contributor
Author
|
Another one from the mermaid POC x = pt.dvector("x")
y = (x * 2).sum()
z = pt.stack([y.mean(), y.std()])graph TD
A["MakeVector{dtype='float64'} [id A]"]
B["True_div [id B] 'mean'"]
C["Sum{axes=None} [id C]"]
D["Sum{axes=None} [id D]"]
E["Mul [id E]"]
F["x [id F]"]
G["ExpandDims{axis=0} [id G]"]
H["2 [id H]"]
I["Cast{float64} [id I]"]
J["1 [id J]"]
K["Sqrt [id K] 'std'"]
L["True_div [id L] 'var'"]
M["Sum{axes=[]} [id M]"]
N["Pow [id N]"]
O["Sub [id O]"]
P["True_div [id P] 'mean'"]
Q["Sum{axes=[]} [id Q]"]
R["Cast{float64} [id R]"]
S["1 [id S]"]
T["2.0 [id T]"]
U["Cast{float64} [id U]"]
V["1 [id V]"]
A --> B
B --> C
C --> D
D --> E
E --> F
E --> G
G --> H
B --> I
I --> J
A --> K
K --> L
L --> M
M --> N
N --> O
O --> D
D --> D
O --> P
P --> Q
Q --> D
D --> D
P --> R
R --> S
N --> T
L --> U
U --> V
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1034
Adds
file="rich"as a new sentinel todebugprint, returning arich.tree.Treeinstead of printing text.richis added as a hard dependency. The refactor also extractsGraphNode,_assign_id,_build_label, and_iter_graph_nodesfromthe monolithic
_debugprintfunction, which both the text and richrenderers now consume.
The
"rich"sentinel slots in alongside"str"andNone— the textoutput path is unchanged.