pytensor icon indicating copy to clipboard operation
pytensor copied to clipboard

Add option to print inner graphs in debugprint function

Open Aarsh-Wankar opened this issue 9 months ago • 1 comments

I added an option print_inner_graphs to print inner graphs in the debugprint function.

Description

A boolean argument print_inner_graphs is provided to the debugprint function, which defaults to True. In case we don't want to print the inner graphs, we can set it to False. For example:

import pytensor
import pytensor.tensor as pt
from pytensor.compile.mode import get_default_mode

n = pt.iscalar("n")
x0 = pt.vector("x0")
xs, _ = pytensor.scan(lambda xtm1: xtm1 + 1, outputs_info=[x0], n_steps=n)

mode = get_default_mode().including("scan_save_mem")
fn = pytensor.function([n, x0], xs, mode=mode, on_unused_input="ignore")
fn.dprint()
# Output:
  Subtensor{start:stop} [id A] 9
   ├─ Scan{scan_fn, while_loop=False, inplace=all} [id B] 8
   │  ├─ n [id C]
   │  └─ SetSubtensor{:stop} [id D] 7
   │     ├─ AllocEmpty{dtype='float64'} [id E] 6
   │     │  ├─ Composite{...}.2 [id F] 0
   │     │  │  └─ n [id C]
   │     │  └─ Shape_i{0} [id G] 5
   │     │     └─ x0 [id H]
   │     ├─ Unbroadcast{0} [id I] 4
   │     │  └─ ExpandDims{axis=0} [id J] 3
   │     │     └─ x0 [id H]
   │     └─ 1 [id K]
   ├─ ScalarFromTensor [id L] 2
   │  └─ Composite{...}.1 [id F] 0
   │     └─ ···
   └─ ScalarFromTensor [id M] 1
      └─ Composite{...}.0 [id F] 0
         └─ ···
  
  Inner graphs:
  
  Scan{scan_fn, while_loop=False, inplace=all} [id B]
   ← Add [id N]
      ├─ [1.] [id O]
  ...
      │     ├─ maximum [id X] 't8'
      │     │  └─ ···
      │     └─ 1 [id W]
      └─ 1 [id U]
fn.dprint(print_inner_graphs = False)
# Output:
Subtensor{start:stop} [id A] 9
 ├─ Scan{scan_fn, while_loop=False, inplace=all} [id B] 8
 │  ├─ n [id C]
 │  └─ SetSubtensor{:stop} [id D] 7
 │     ├─ AllocEmpty{dtype='float64'} [id E] 6
 │     │  ├─ Composite{...}.2 [id F] 0
 │     │  │  └─ n [id C]
 │     │  └─ Shape_i{0} [id G] 5
 │     │     └─ x0 [id H]
 │     ├─ Unbroadcast{0} [id I] 4
 │     │  └─ ExpandDims{axis=0} [id J] 3
 │     │     └─ x0 [id H]
 │     └─ 1 [id K]
 ├─ ScalarFromTensor [id L] 2
 │  └─ Composite{...}.1 [id F] 0
 │     └─ ···
 └─ ScalarFromTensor [id M] 1
    └─ Composite{...}.0 [id F] 0
       └─ ···

Related Issue

  • [x] Closes #1285
  • [ ] Related to #

Checklist

Type of change

  • [ ] New feature / enhancement
  • [ ] Bug fix
  • [ ] Documentation
  • [x] Maintenance
  • [ ] Other (please specify):

📚 Documentation preview 📚: https://pytensor--1293.org.readthedocs.build/en/1293/

Aarsh-Wankar avatar Mar 13 '25 06:03 Aarsh-Wankar