pytensor
pytensor copied to clipboard
Add option to print inner graphs in debugprint function
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
- [x] Checked that the pre-commit linting/style checks pass
- [ ] Included tests that prove the fix is effective or that the new feature works
- [x] Added necessary documentation (docstrings and/or example notebooks)
- [ ] If you are a pro: each commit corresponds to a relevant logical change
Type of change
- [ ] New feature / enhancement
- [ ] Bug fix
- [ ] Documentation
- [x] Maintenance
- [ ] Other (please specify):
📚 Documentation preview 📚: https://pytensor--1293.org.readthedocs.build/en/1293/