[Feature] Add joint sample-and-score distribution adapter - #4097
Open
vmoens wants to merge 9 commits into
Open
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4097
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 1 Unrelated FailureAs of commit 2373262 with merge base c5ab3c9 ( NEW FAILURE - The following job has failed:
BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
vmoens
marked this pull request as ready for review
August 12, 2026 17:25
vmoens
force-pushed
the
feature/joint-sampling-distribution-adapter
branch
from
August 12, 2026 17:28
49a8826 to
b6e293f
Compare
vmoens
force-pushed
the
feature/joint-sampling-distribution-adapter
branch
from
August 12, 2026 17:29
b6e293f to
2373262
Compare
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.
Summary
ensure_rsample_and_log_prob()as an identity-preserving capability adaptersample_and_log_prob()andrsample_and_log_prob()on arbitrary PyTorch and third-party distributions through a lightweight proxyunwrap_distribution()for unavoidable exact-type dispatch such astorch.distributions.kl_divergencetorch.compile, serialization, composites, non-reparameterizable distributions, actor/objective integration, and sampling overheadMotivation
#4080 introduces atomic sample-and-score operations for
TanhNormaland generic free-function fallbacks. Distribution-consuming modules are modular, however, and cannot assume which concrete distribution they will receive. This follow-up establishes a uniform internal method contract:Distributions that already provide the method retain object identity. Other distributions are adapted without monkey-patching PyTorch classes or maintaining TorchRL copies of the
torch.distributionsnamespace.Compatibility
The proxy delegates ordinary distribution behavior, including sampling, scoring, entropy, support, statistics, expansion, enumeration, mapping behavior for composite distributions, and distribution-specific attributes.
isinstancechecks remain transparent. APIs that dispatch on exact concrete types must useunwrap_distribution(); the PPO KL path does this explicitly.Structured
CompositeDistributionsamples continue to use the centralized component aggregation in the existing free functions. Distributions withhas_rsample=Falseexposesample_and_log_prob()and continue to reject reparameterized sampling.Stacking
This draft is stacked on #4080 and currently includes its commits. Once #4080 lands, this branch should be rebased onto
main, leaving only the adapter follow-up in the diff.Validation
pytest -q test/test_distributions.py test/modules/test_actor.py: 1087 passed, 1 skippedpytest -q test/objectives/test_cql.py: 408 passed, 3 skippedKLPENPPOLossexecution passed, covering explicit unwrapping for KL dispatchufmt,flake8,pydocstyle, andgit diff --checkpassed