Skip to content

Commit 7f03125

Browse files
committed
Allow ignoring printing of specific rewrites from optimizer_verbose
1 parent 5008fab commit 7f03125

File tree

6 files changed

+77
-8
lines changed

6 files changed

+77
-8
lines changed

pytensor/configdefaults.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,18 @@ def add_compile_configvars():
418418

419419
config.add(
420420
"optimizer_verbose",
421-
"If True, we print all optimization being applied",
421+
"Print information about rewrites that are applied during a graph transformation.",
422422
BoolParam(False),
423423
in_c_key=False,
424424
)
425425

426+
config.add(
427+
"optimizer_verbose_ignore",
428+
"Do not print information for rewrites with these names when `optimizer_verbose` is `True`. Separate names with ','",
429+
StrParam(""),
430+
in_c_key=False,
431+
)
432+
426433
config.add(
427434
"on_opt_error",
428435
(

pytensor/configparser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class PyTensorConfigParser:
8181
allow_gc: bool
8282
optimizer: str
8383
optimizer_verbose: bool
84+
optimizer_verbose_ignore: str
8485
on_opt_error: str
8586
nocleanup: bool
8687
on_unused_input: str

pytensor/graph/features.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ def replace_all_validate(
567567
if verbose is None:
568568
verbose = config.optimizer_verbose
569569

570+
if verbose:
571+
print_reason = True
572+
if config.optimizer_verbose_ignore:
573+
print_reason = str(reason) not in config.optimizer_verbose_ignore.split(
574+
","
575+
)
576+
570577
for r, new_r in replacements:
571578
try:
572579
fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
@@ -608,7 +615,7 @@ def replace_all_validate(
608615
)
609616
raise
610617

611-
if verbose:
618+
if verbose and print_reason:
612619
print( # noqa: T201
613620
f"rewriting: rewrite {reason} replaces {r} of {r.owner} with {new_r} of {new_r.owner}"
614621
)

pytensor/graph/fg.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,18 @@ def replace(
490490
"""
491491
if verbose is None:
492492
verbose = config.optimizer_verbose
493+
493494
if verbose:
494-
print( # noqa: T201
495-
f"rewriting: rewrite {reason} replaces {var} of {var.owner} with {new_var} of {new_var.owner}"
496-
)
495+
print_reason = True
496+
if config.optimizer_verbose_ignore:
497+
print_reason = str(reason) not in config.optimizer_verbose_ignore.split(
498+
","
499+
)
500+
501+
if print_reason:
502+
print( # noqa: T201
503+
f"rewriting: rewrite {reason} replaces {var} of {var.owner} with {new_var} of {new_var.owner}"
504+
)
497505

498506
new_var = var.type.filter_variable(new_var, allow_convert=True)
499507

pytensor/graph/rewriting/basic.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,9 +1305,15 @@ def transform(self, fgraph, node):
13051305
new_vars = list(new_repl.values())
13061306

13071307
if config.optimizer_verbose:
1308-
print( # noqa: T201
1309-
f"rewriting: rewrite {rewrite} replaces node {node} with {new_repl}"
1310-
)
1308+
print_reason = True
1309+
if config.optimizer_verbose_ignore:
1310+
print_reason = str(
1311+
rewrite
1312+
) not in config.optimizer_verbose_ignore.split(",")
1313+
if print_reason:
1314+
print( # noqa: T201
1315+
f"rewriting: rewrite {rewrite} replaces node {node} with {new_repl}"
1316+
)
13111317

13121318
if self.profile:
13131319
self.node_created[rewrite] += len(

tests/graph/test_fg.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,43 @@ def test_dprint(self):
731731
o1 = op1(r1, r2)
732732
fg = FunctionGraph([r1, r2], [o1], clone=False)
733733
assert fg.dprint(file="str") == debugprint(fg, file="str")
734+
735+
def test_optimizer_verbose(self, capsys):
736+
x = MyVariable("x")
737+
y = MyVariable("y")
738+
z = MyVariable("z")
739+
740+
o1 = op1(x, y)
741+
fgraph = FunctionGraph([x, y, z], [o1], clone=False)
742+
743+
with config.change_flags(optimizer_verbose=False):
744+
fgraph.replace(y, z, reason="y->z")
745+
746+
cap_out = capsys.readouterr().out
747+
assert cap_out == ""
748+
749+
with config.change_flags(optimizer_verbose=True):
750+
fgraph.replace(z, y, reason="z->y")
751+
752+
cap_out = capsys.readouterr().out
753+
assert "z->y" in cap_out
754+
755+
with config.change_flags(
756+
optimizer_verbose=True, optimizer_verbose_ignore="y->z"
757+
):
758+
fgraph.replace(y, z, reason="y->z")
759+
fgraph.replace(z, y, reason="z->y")
760+
761+
cap_out = capsys.readouterr().out
762+
assert "y->z" not in cap_out
763+
assert "z->y" in cap_out
764+
765+
with config.change_flags(
766+
optimizer_verbose=True, optimizer_verbose_ignore="y->z,z->y"
767+
):
768+
fgraph.replace(y, z, reason="y->z")
769+
fgraph.replace(z, y, reason="z->y")
770+
771+
cap_out = capsys.readouterr().out
772+
assert "y->z" not in cap_out
773+
assert "z->y" not in cap_out

0 commit comments

Comments
 (0)