Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to print inner graphs in debugprint function #1293

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions pytensor/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def debugprint(
| FunctionGraph
| Sequence[Variable | Apply | Function | FunctionGraph],
depth: int = -1,
inner_depth: int = -1,
print_type: bool = False,
print_shape: bool = False,
file: Literal["str"] | TextIO | None = None,
Expand Down Expand Up @@ -123,6 +124,8 @@ def debugprint(
The object(s) to be printed.
depth
Print graph to this depth (``-1`` for unlimited).
inner_depth
Print inner graph to this depth (``-1`` for unlimited).
print_type
If ``True``, print the `Type`\s of each `Variable` in the graph.
print_shape
Expand Down Expand Up @@ -161,6 +164,8 @@ def debugprint(
Whether to set both `print_destroy_map` and `print_view_map` to ``True``.
print_fgraph_inputs
Print the inputs of `FunctionGraph`\s.
print_inner_graphs
Whether to print the inner graphs of `Op`\s

Returns
-------
Expand Down Expand Up @@ -293,10 +298,14 @@ def debugprint(
):
if hasattr(var.owner, "op"):
if (
isinstance(var.owner.op, HasInnerGraph)
or hasattr(var.owner.op, "scalar_op")
and isinstance(var.owner.op.scalar_op, HasInnerGraph)
) and var not in inner_graph_vars:
(
isinstance(var.owner.op, HasInnerGraph)
or hasattr(var.owner.op, "scalar_op")
and isinstance(var.owner.op.scalar_op, HasInnerGraph)
)
and inner_depth
and var not in inner_graph_vars
):
inner_graph_vars.append(var)
if print_op_info:
op_information.update(op_debug_information(var.owner.op, var.owner))
Expand All @@ -322,7 +331,7 @@ def debugprint(
print_view_map=print_view_map,
)

if len(inner_graph_vars) > 0:
if len(inner_graph_vars) > 0 and inner_depth:
print("", file=_file)
prefix = ""
new_prefix = prefix + " ← "
Expand Down Expand Up @@ -374,7 +383,7 @@ def debugprint(
_debugprint(
ig_var,
prefix=prefix,
depth=depth,
depth=inner_depth,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depth and inner_depth are not the same. Depth still makes sense alongside inner_depth. Inner depth tells how many inner graphs to step into, depth tells how many ops in a graph (or inner garph) to step into

done=done,
print_type=print_type,
print_shape=print_shape,
Expand All @@ -397,7 +406,7 @@ def debugprint(
_debugprint(
inp,
prefix=" → ",
depth=depth,
depth=inner_depth,
done=done,
print_type=print_type,
print_shape=print_shape,
Expand Down Expand Up @@ -432,7 +441,7 @@ def debugprint(
_debugprint(
out,
prefix=new_prefix,
depth=depth,
depth=inner_depth,
done=done,
print_type=print_type,
print_shape=print_shape,
Expand Down