From af698a7edebb532f4b541a18315de66712d98eb4 Mon Sep 17 00:00:00 2001 From: Aarsh-Wankar <23110003@iitgn.ac.in> Date: Thu, 13 Mar 2025 12:10:03 +0530 Subject: [PATCH 1/4] Add option to print inner graphs in debugprint function --- pytensor/printing.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pytensor/printing.py b/pytensor/printing.py index b7b71622e8..1d64667578 100644 --- a/pytensor/printing.py +++ b/pytensor/printing.py @@ -101,6 +101,7 @@ def debugprint( print_view_map: bool = False, print_memory_map: bool = False, print_fgraph_inputs: bool = False, + print_inner_graphs: bool = True, ) -> str | TextIO: r"""Print a graph as text. @@ -161,6 +162,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 ------- @@ -322,7 +325,7 @@ def debugprint( print_view_map=print_view_map, ) - if len(inner_graph_vars) > 0: + if len(inner_graph_vars) > 0 and print_inner_graphs: print("", file=_file) prefix = "" new_prefix = prefix + " ← " From 66432ece7d6c3f8b5d7d965730dd4160acbdf9b7 Mon Sep 17 00:00:00 2001 From: Aarsh-Wankar <23110003@iitgn.ac.in> Date: Fri, 14 Mar 2025 16:06:00 +0530 Subject: [PATCH 2/4] Add inner_depth parameter to debugprint for depth controlled inner graph printing --- pytensor/printing.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pytensor/printing.py b/pytensor/printing.py index 1d64667578..c228f831d9 100644 --- a/pytensor/printing.py +++ b/pytensor/printing.py @@ -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, @@ -299,7 +300,7 @@ def debugprint( 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: + ) and not 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)) @@ -325,7 +326,7 @@ def debugprint( print_view_map=print_view_map, ) - if len(inner_graph_vars) > 0 and print_inner_graphs: + if len(inner_graph_vars) > 0 and inner_depth: print("", file=_file) prefix = "" new_prefix = prefix + " ← " @@ -377,7 +378,7 @@ def debugprint( _debugprint( ig_var, prefix=prefix, - depth=depth, + depth=inner_depth, done=done, print_type=print_type, print_shape=print_shape, @@ -400,7 +401,7 @@ def debugprint( _debugprint( inp, prefix=" → ", - depth=depth, + depth=inner_depth, done=done, print_type=print_type, print_shape=print_shape, @@ -435,7 +436,7 @@ def debugprint( _debugprint( out, prefix=new_prefix, - depth=depth, + depth=inner_depth, done=done, print_type=print_type, print_shape=print_shape, From c7d0a54bff762f287bf5ec466ce16311a9cf22f0 Mon Sep 17 00:00:00 2001 From: Aarsh-Wankar <23110003@iitgn.ac.in> Date: Fri, 14 Mar 2025 16:09:40 +0530 Subject: [PATCH 3/4] fix ruff format --- pytensor/printing.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pytensor/printing.py b/pytensor/printing.py index c228f831d9..4d563a0626 100644 --- a/pytensor/printing.py +++ b/pytensor/printing.py @@ -297,10 +297,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 not inner_depth 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 not 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)) From 04ebe0a19eb7e331faeaf1d23466a91e2e7aad13 Mon Sep 17 00:00:00 2001 From: Aarsh-Wankar <23110003@iitgn.ac.in> Date: Fri, 14 Mar 2025 17:11:03 +0530 Subject: [PATCH 4/4] Fix logical flaw in inner_var list appends --- pytensor/printing.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pytensor/printing.py b/pytensor/printing.py index 4d563a0626..5bdac3e7fd 100644 --- a/pytensor/printing.py +++ b/pytensor/printing.py @@ -102,7 +102,6 @@ def debugprint( print_view_map: bool = False, print_memory_map: bool = False, print_fgraph_inputs: bool = False, - print_inner_graphs: bool = True, ) -> str | TextIO: r"""Print a graph as text. @@ -125,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 @@ -302,7 +303,7 @@ def debugprint( or hasattr(var.owner.op, "scalar_op") and isinstance(var.owner.op.scalar_op, HasInnerGraph) ) - and not inner_depth + and inner_depth and var not in inner_graph_vars ): inner_graph_vars.append(var)