Skip to content

[Feature] Add joint sample-and-score distribution adapter - #4097

Open
vmoens wants to merge 9 commits into
mainfrom
feature/joint-sampling-distribution-adapter
Open

[Feature] Add joint sample-and-score distribution adapter#4097
vmoens wants to merge 9 commits into
mainfrom
feature/joint-sampling-distribution-adapter

Conversation

@vmoens

@vmoens vmoens commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add ensure_rsample_and_log_prob() as an identity-preserving capability adapter
  • expose sample_and_log_prob() and rsample_and_log_prob() on arbitrary PyTorch and third-party distributions through a lightweight proxy
  • adapt distributions at TorchRL probabilistic-module and objective boundaries, allowing consumers to use the method contract without concrete-distribution branches
  • add unwrap_distribution() for unavoidable exact-type dispatch such as torch.distributions.kl_divergence
  • document the method-oriented API and preserve the existing free-function fallback
  • cover native identity, wrapped gradients, torch.compile, serialization, composites, non-reparameterizable distributions, actor/objective integration, and sampling overhead

Motivation

#4080 introduces atomic sample-and-score operations for TanhNormal and 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:

action, log_prob = dist.rsample_and_log_prob()

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.distributions namespace.

Compatibility

The proxy delegates ordinary distribution behavior, including sampling, scoring, entropy, support, statistics, expansion, enumeration, mapping behavior for composite distributions, and distribution-specific attributes. isinstance checks remain transparent. APIs that dispatch on exact concrete types must use unwrap_distribution(); the PPO KL path does this explicitly.

Structured CompositeDistribution samples continue to use the centralized component aggregation in the existing free functions. Distributions with has_rsample=False expose sample_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 skipped
  • pytest -q test/objectives/test_cql.py: 408 passed, 3 skipped
  • representative SAC, CrossQ, REDQ, A2C, OnlineDT, and V-trace regressions passed
  • a direct non-skipped KLPENPPOLoss execution passed, covering explicit unwrapping for KL dispatch
  • sampling microbenchmark: adapted method 19.5 us mean; free function 20.0 us mean in the local run
  • pinned ufmt, flake8, pydocstyle, and git diff --check passed

@pytorch-bot

pytorch-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

🔗 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 Failure

As of commit 2373262 with merge base c5ab3c9 (image):

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 12, 2026
@github-actions github-actions Bot added Documentation Improvements or additions to documentation Benchmarks rl/benchmark changes Objectives distributions llm/ LLM-related PR, triggers LLM CI tests Modules Integrations/torch_geometric Integrations Feature New feature labels Aug 12, 2026
@vmoens
vmoens marked this pull request as ready for review August 12, 2026 17:25
@vmoens
vmoens force-pushed the feature/joint-sampling-distribution-adapter branch from 49a8826 to b6e293f Compare August 12, 2026 17:28
@vmoens
vmoens force-pushed the feature/joint-sampling-distribution-adapter branch from b6e293f to 2373262 Compare August 12, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Benchmarks rl/benchmark changes CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. distributions Documentation Improvements or additions to documentation Feature New feature Integrations/torch_geometric Integrations llm/ LLM-related PR, triggers LLM CI tests Modules Objectives

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants