Skip to content

Commit 5ab9bf4

Browse files
committed
Short-circuit givens validation when it's empty
1 parent d5c4f5c commit 5ab9bf4

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

pytensor/compile/function/pfunc.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -546,26 +546,27 @@ def construct_pfunc_ins_and_outs(
546546
"variables in the inputs list."
547547
)
548548

549-
# Check that we are not using `givens` to replace input variables, because
550-
# this typically does nothing, contrary to what one may expect.
551-
in_var_set = set(in_variables)
552-
try:
553-
givens_pairs = list(givens.items())
554-
except AttributeError:
555-
givens_pairs = givens
556-
for x, y in givens_pairs:
557-
if x in in_var_set:
558-
raise RuntimeError(
559-
f"You are trying to replace variable '{x}' through the "
560-
"`givens` parameter, but this variable is an input to your "
561-
"function. Replacing inputs is currently forbidden because it "
562-
"has no effect. One way to modify an input `x` to a function "
563-
"evaluating f(x) is to define a new input `y` and use "
564-
"`pytensor.function([y], f(x), givens={x: g(y)})`. Another "
565-
"solution consists in using `pytensor.clone_replace`, e.g. like this: "
566-
"`pytensor.function([x], "
567-
"pytensor.clone_replace(f(x), replace={x: g(x)}))`."
568-
)
549+
if givens:
550+
# Check that we are not using `givens` to replace input variables, because
551+
# this typically does nothing, contrary to what one may expect.
552+
in_var_set = set(in_variables)
553+
try:
554+
givens_pairs = list(givens.items())
555+
except AttributeError:
556+
givens_pairs = givens
557+
for x, y in givens_pairs:
558+
if x in in_var_set:
559+
raise RuntimeError(
560+
f"You are trying to replace variable '{x}' through the "
561+
"`givens` parameter, but this variable is an input to your "
562+
"function. Replacing inputs is currently forbidden because it "
563+
"has no effect. One way to modify an input `x` to a function "
564+
"evaluating f(x) is to define a new input `y` and use "
565+
"`pytensor.function([y], f(x), givens={x: g(y)})`. Another "
566+
"solution consists in using `pytensor.clone_replace`, e.g. like this: "
567+
"`pytensor.function([x], "
568+
"pytensor.clone_replace(f(x), replace={x: g(x)}))`."
569+
)
569570

570571
if not fgraph:
571572
# Extend the outputs with the updates on input variables so they are

0 commit comments

Comments
 (0)