[Feature] [LLM] Add TRL interoperability adapters - #4070
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4070
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New FailuresAs of commit b5247d3 with merge base d7659c7 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
| Prefix | Label Applied | Example |
|---|---|---|
[Algorithm] |
new algo | [Algorithm] Add new RL objective |
[BE] |
BE | [BE] Improve error messages |
[Benchmark] or [Benchmarks] |
Benchmarks | [Benchmark] Add collector benchmark |
[BugFix] |
BugFix | [BugFix] Fix memory leak in collector |
[Example] or [Examples] |
Examples | [Example] Add training script |
[Feature] |
Feature | [Feature] Add new optimizer |
[Doc] or [Docs] |
Documentation | [Doc] Update installation guide |
[Refactor] |
Refactoring | [Refactor] Clean up module imports |
[CI] |
CI | [CI] Fix workflow permissions |
[Test] or [Tests] |
Tests | [Tests] Add unit tests for buffer |
[Trainer] or [Trainers] |
Trainers | [Trainer] Add trainer config |
[Environment] or [Environments] |
Environments | [Environments] Add Gymnasium support |
[Data] |
Data | [Data] Fix replay buffer sampling |
[LLM] |
llm/ | [LLM] Add reward model integration |
[Minor] |
small change | [Minor] Fix typo in error message |
[Performance] or [Perf] |
Performance | [Performance] Optimize tensor ops |
[BC-Breaking] |
bc breaking | [BC-Breaking] Remove deprecated API |
[Deprecation] |
Deprecation | [Deprecation] Mark old function |
[Algorithm] or [Algorithms] |
new algo | [Algorithm] Add new objective |
[Quality] |
Quality | [Quality] Fix typos and add codespell |
[Versioning] |
versioning | [Versioning] Bump release version |
[WIP] |
WIP | [WIP] Draft implementation |
Note: Common variations like singular/plural are supported (e.g., [Doc] or [Docs]).
|
@vmoens could you take a look when you have a moment? |
Introduces optional adapters to allow seamless mixing of TorchRL and HuggingFace trl components without adoption friction. - Add TorchRLBufferDataset: wraps ReplayBuffer as an IterableDataset yielding flat dicts for direct consumption by HF trainers. - Add HFRewardModelWrapper: wraps HF reward models as a TensorDictModuleBase for native use in TorchRL loops. - Add lazy loading in torchrl.modules.llm to keep TRL/Transformers as strictly optional dependencies. - Add 17 unit tests validating both adapters and E2E round-trips. - Add comprehensive Sphinx tutorial and documentation. Closes pytorch#4058
e29ccdd to
a6eb3fe
Compare
|
Reviewed rebased head One blocking issue: Targeted result: |
Description
This PR introduces zero-overhead interoperability adapters for Hugging Face
trl.TorchRLBufferDataset(TorchRL -> TRL)ReplayBufferas atorch.utils.data.IterableDataset.DataCollatorWithPaddingand standard HFTrainer/trlpipelines.NestedKeyserialization.HFRewardModelWrapper(TRL -> TorchRL)AutoModelForSequenceClassificationmodels as aTensorDictModuleBase.logits,rewards, or raw tensors) into a squeezed[B]shape reward tensor written back to the TensorDict in-place.inference_mode.Documentation & Validation
__getattr__intorchrl.modules.llm.__init__.pyto ensuretrl/transformersremain strictly optional.TestTRLInteropachieving complete coverage, including E2E round-trip data flow.tutorials/sphinx-tutorials/trl_interop.py).Motivation and Context
Currently, many teams use Hugging Face trl (e.g., PPOTrainer, GRPOTrainer) but face bottlenecks with rollout storage and sampling at scale. Conversely, teams using TorchRL's highly efficient collectors and recipes often want to utilize the vast ecosystem of Hugging Face Reward Models without writing tedious custom wrappers.
This PR implements Workstream 2 of the Post-training efforts RFC (#3948), allowing users to seamlessly mix and match TorchRL and TRL components without adopting a full stack at once, significantly lowering adoption friction.
close #4058
Types of changes
What types of changes does your code introduce? Remove all that do not apply:
Checklist
Go over all the following points, and put an
xin all the boxes that apply.If you are unsure about any of these, don't hesitate to ask. We are here to help!