Fix compute_marginals and sample_posterior for plates of size 1 - #3484
Open
MohammadHijjawi97 wants to merge 1 commit into
Open
MohammadHijjawi97 wants to merge 1 commit into
MohammadHijjawi97 wants to merge 1 commit into
Conversation
TraceEnum_ELBO.compute_marginals() and sample_posterior() raised a KeyError when an enumerated site lived in a plate of size 1. Packed tensors drop size-1 dims, so the default LogRing had no size to broadcast the result back to the site's plate. Pass a ring that knows the site's plate sizes. Fixes pyro-ppl#3102
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3102
Proposed changes
TraceEnum_ELBO.compute_marginals()andTraceEnum_ELBO.sample_posterior()raiseKeyErrorwhen an enumerated site is inside a plate of size 1. Packed tensors drop size-1 dims, so the plate's dim never appears in any factor;contract_to_tensor()was called with only a cache, so its defaultLogRinghad an emptydim_to_sizeandRing.broadcast()could not look up the plate size when broadcasting the result back to the site's ordinal.This passes a
LogRingwhosedim_to_sizeis filled from the site's plate frames, in both_compute_marginalsandBackwardSampleMessenger.Tests
test_marginals_plate_3102[1,2,3]intests/infer/test_enum.pychecks the marginals against the analytic posterior and runssample_posterior;size=1fails withKeyError: 'a'before this change. The script from the issue now printstensor([0.3000, 0.7000]).pytest tests/infer/test_enum.py(678 passed) andpytest tests/infer/test_discrete.py.ruff check/ruff format --check/mypyon the changed files.