Skip to content

Allow debug evaling IR logp graphs #7666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pymc/logprob/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,16 @@ class ValuedRV(Op):
and breaking the dependency of `b` on `a`. The new nodes isolate the graphs between conditioning points.
"""

view_map = {0: [0]}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs view_map so PyTensor knows not to do inplace operations on this. The other Op already had the view_map defined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that you have to use view_map to do that. Somewhat hidden functionality? I thought pytensor only does inplace on things that proactively define a destroy_map.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't say this output returns a view of the input, and this output is not itself a graph output, PyTensor will assume it can be safely destroyed as it is just an intermediate operation.


def make_node(self, rv, value):
assert isinstance(rv, Variable)
assert isinstance(value, Variable)
return Apply(self, [rv, value], [rv.type(name=rv.name)])

def perform(self, node, inputs, out):
raise NotImplementedError("ValuedVar should not be present in the final graph!")
warnings.warn("ValuedVar should not be present in the final graph!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be logging instead of warning so people can more easily turn it off?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I want this to be loud, because it shouldn't be in the final graph.

out[0][0] = inputs[0]

def infer_shape(self, fgraph, node, input_shapes):
return [input_shapes[0]]
Expand Down
4 changes: 3 additions & 1 deletion pymc/logprob/transform_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import warnings

from collections.abc import Sequence

Expand Down Expand Up @@ -40,7 +41,8 @@ def make_node(self, tran_value: TensorVariable, value: TensorVariable):
return Apply(self, [tran_value, value], [tran_value.type()])

def perform(self, node, inputs, outputs):
raise NotImplementedError("These `Op`s should be removed from graphs used for computation.")
warnings.warn("TransformedValue should not be present in the final graph!")
outputs[0][0] = inputs[0]

def infer_shape(self, fgraph, node, input_shapes):
return [input_shapes[0]]
Expand Down
23 changes: 23 additions & 0 deletions tests/logprob/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,26 @@ def test_ir_rewrite_does_not_disconnect_valued_rvs():
logp_b.eval({a_value: np.pi, b_value: np.e}),
stats.norm.logpdf(np.e, np.pi * 8, 1),
)


def test_ir_ops_can_be_evaluated_with_warning():
_eval_values = [None, None]

def my_logp(value, lam):
nonlocal _eval_values
_eval_values[0] = value.eval()
_eval_values[1] = lam.eval({"lam_log__": -1.5})
return value * lam

with pm.Model() as m:
lam = pm.Exponential("lam")
pm.CustomDist("y", lam, logp=my_logp, observed=[0, 1, 2])

with pytest.warns(
UserWarning, match="TransformedValue should not be present in the final graph"
):
with pytest.warns(UserWarning, match="ValuedVar should not be present in the final graph"):
m.logp()

assert _eval_values[0].sum() == 3
assert _eval_values[1] == np.exp(-1.5)