Skip to content

Add vectorize_over_posterior to pymc.sampling.forward #7841

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 4, 2025

Conversation

lucianopaz
Copy link
Member

@lucianopaz lucianopaz commented Jul 3, 2025

This PR adds the function pymc.sampling.forward.vectorize_over_posterior.

This function basically vectorizes the computation of a list of output variables over possible values of random variables or deterministics that are stored in an xarray.Dataset. Any extra random variable remaining in the graph will be vectorized as well, either by changing its size explicitly to match the batch of chain * draw dimensions, or implicitly given its inputs.

Type of change

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

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

Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

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

Looks good, some minor suggestions and complaints. This PR reminds me that I don't think we have vectorize_node implemented for SymbolicRandomVariables. Shoud be easy to implement, at least for those we have a signature (it's how we know how to resize them automatically)

@_change_dist_size.register(SymbolicRandomVariable)
def change_symbolic_rv_size(op: SymbolicRandomVariable, rv, new_size, expand) -> TensorVariable:
extended_signature = op.extended_signature
if extended_signature is None:
raise NotImplementedError(
f"SymbolicRandomVariable {op} without signature requires custom `_change_dist_size` implementation."
)
size = op.size_param(rv.owner)
if size is None:
raise NotImplementedError(
f"SymbolicRandomVariable {op} without [size] in extended_signature requires custom `_change_dist_size` implementation."
)
params = op.dist_params(rv.owner)
if expand and not rv_size_is_none(size):
new_size = tuple(new_size) + tuple(size)
return op.rebuild_rv(*params, size=new_size)

@ricardoV94
Copy link
Member

ricardoV94 commented Jul 3, 2025

Looks good, some minor suggestions and complaints. This PR reminds me that I don't think we have vectorize_node implemented for SymbolicRandomVariables. Shoud be easy to implement, at least for those we have a signature (it's how we know how to resize them automatically)

I'm not suggesting we should do it in this PR, just reminded me

Copy link

codecov bot commented Jul 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.92%. Comparing base (3ae5095) to head (b2af12f).
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7841      +/-   ##
==========================================
+ Coverage   92.91%   92.92%   +0.01%     
==========================================
  Files         107      107              
  Lines       18286    18313      +27     
==========================================
+ Hits        16991    17018      +27     
  Misses       1295     1295              
Files with missing lines Coverage Δ
pymc/sampling/forward.py 96.73% <100.00%> (+0.35%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lucianopaz lucianopaz force-pushed the vectorize_post branch 3 times, most recently from 20972c9 to b2af12f Compare July 4, 2025 04:40
@ricardoV94
Copy link
Member

Looks great, failing test is unrelated I've seen it fail sporadically

@lucianopaz lucianopaz merged commit 794b5b8 into pymc-devs:main Jul 4, 2025
21 of 23 checks passed
@lucianopaz lucianopaz deleted the vectorize_post branch July 4, 2025 14:12
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