Skip to content

Add a function to create a tensor that represents the loop over the posterior #7885

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lucianopaz
Copy link
Member

@lucianopaz lucianopaz commented Aug 1, 2025

Description

This PR adds the loop_over_posterior function. It complements vectorize_over_posterior and can be used whenever vectorize_graph is not implemented for some nodes in the computational graph (i.e. some kinds of AdvancedSetSubtensor raise NotImplementedError when trying to vectorize them).

Related Issue

  • Closes #
  • Related to #

Checklist

Type of change

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

📚 Documentation preview 📚: https://pymc--7885.org.readthedocs.build/en/7885/

@lucianopaz lucianopaz changed the title Loop post Add a function to create a tensor that represents the loop over the posterior Aug 1, 2025
Copy link

codecov bot commented Aug 1, 2025

Codecov Report

❌ Patch coverage is 97.61905% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.96%. Comparing base (127ac30) to head (3012b17).

Files with missing lines Patch % Lines
pymc/sampling/forward.py 96.96% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #7885   +/-   ##
=======================================
  Coverage   92.95%   92.96%           
=======================================
  Files         116      116           
  Lines       18863    18904   +41     
=======================================
+ Hits        17534    17574   +40     
- Misses       1329     1330    +1     
Files with missing lines Coverage Δ
pymc/pytensorf.py 89.97% <100.00%> (+0.23%) ⬆️
pymc/sampling/forward.py 96.75% <96.96%> (+0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ricardoV94
Copy link
Member

I would rather work on pytensor to refactor AdvancedSubtensor. The problem is described in, pymc-devs/pytensor#541

And/or allow vectorize_graph to use scan, but keep the same API

Comment on lines +1167 to +1169
samples = clone_while_sharing_some_variables(
outputs, replace=replace, kept_variables=non_sequences
)
Copy link
Member

Choose a reason for hiding this comment

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

I know VI does something like this, either by passing things that shouldn't be replaced as keys:values, or by using graph_replace, can you check if the same approach works?

Copy link
Member

Choose a reason for hiding this comment

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

@@ -1083,3 +1090,122 @@ def vectorize_over_posterior(
f"The following random variables found in the extracted graph: {remaining_rvs}"
)
return vectorized_outputs


def loop_over_posterior(
Copy link
Member

Choose a reason for hiding this comment

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

instead of a separate function have an argument: use_scan: bool = False in the previous function? Most logic should be the same.

This flatten batch -> scan -> reshape logic should go in PyTensor, it's needed in general

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants