Skip to content

[Doc] Fold corrected TRL interoperability end-to-end section into trl_interop tutorial - #4161

Open
coder-jayp wants to merge 4 commits into
pytorch:mainfrom
coder-jayp:docs/trl-interop-tutorial
Open

[Doc] Fold corrected TRL interoperability end-to-end section into trl_interop tutorial#4161
coder-jayp wants to merge 4 commits into
pytorch:mainfrom
coder-jayp:docs/trl-interop-tutorial

Conversation

@coder-jayp

@coder-jayp coder-jayp commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

This PR extends the existing trl_interop.py tutorial with a corrected
end-to-end section (Part 4) that demonstrates the complete TorchRL ↔ TRL
user journey.

Specific changes:

  • Adds Part 4 to trl_interop.py covering the full preference-pair pipeline:
    collect (prompt + chosen + rejected) into a ReplayBuffer, bridge to a trl
    trainer via TorchRLBufferDataset, and score rollouts with HFRewardModelWrapper
    using the canonical ("tokens", "full") / ("masks", "all_attention_mask") keys.
  • Shows LLMCollector as the production data-collection path.
  • Removes the standalone coding_grpo_trl.py file.
  • Removes the coding_grpo_trl entry from index.rst and llms.rst.

Motivation and Context:

Addresses review feedback on the previous version. The new section:

  • Preserves full conversational context (prompt is no longer discarded).

  • Exercises the actual TorchRL-to-TRL boundary as described in the RFC.

  • Runs in CI using toy models only (no network access or GPU required).

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Documentation (update in the documentation)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

@pytorch-bot

pytorch-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4161

Note: Links to docs will display an error until the docs builds have been completed.

❌ 4 New Failures

As of commit d9d3a53 with merge base 60b1d01 (image):

NEW FAILURES - The following jobs have failed:

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 26, 2026
@coder-jayp

Copy link
Copy Markdown
Contributor Author

@torchrlbot reviewer @vmoens

@github-actions github-actions Bot added Documentation Improvements or additions to documentation tutorials/ labels Aug 26, 2026
@github-actions
github-actions Bot requested a review from vmoens August 26, 2026 17:22
@github-actions

Copy link
Copy Markdown
Contributor

Requested review from @vmoens (requested by @coder-jayp).

if _has_trl:
# Enable TensorDict to store Python lists (strings) transparently.
# Scoped here so it only applies when trl is available.
set_list_to_stack(True).set()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

set() changes TensorDict's process-global mode and LIST_TO_STACK. the change lasts for the rest of the process. can we scope it to buffer construction?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated. Scoped using with set_list_to_stack(True): around the ReplayBuffer population loop so it doesn't leak globally

Comment thread tutorials/sphinx-tutorials/coding_grpo_trl.py Outdated
@vmoens

vmoens commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Can you rebase?

@coder-jayp
coder-jayp force-pushed the docs/trl-interop-tutorial branch from 24a4f32 to 8c59f65 Compare August 29, 2026 14:50
@coder-jayp

Copy link
Copy Markdown
Contributor Author

Done @vmoens

@coder-jayp

Copy link
Copy Markdown
Contributor Author

cc @vmoens

@vmoens vmoens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The real-TRL example is useful and fits the interoperability RFC, but the current tutorial does not yet demonstrate the user journey that motivates TorchRL.

It round-trips a static HF dataset through TorchRL, discards the prompt when constructing reward-model examples, skips the preference-pair creation step attributed to LLMCollector, and finally scores random tokens outside the canonical History/Tokens representation.

The real path is also skipped by both docs and tutorial CI. Could we either fold a corrected end-to-end section into the existing interop tutorial, or position this within a coherent LLM tutorial track, while preserving conversational context and exercising the actual TorchRL-to-TRL boundary?

BATCH_SIZE = 8
MODEL_NAME = "distilbert-base-uncased"

if _has_trl:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

that's not amazing for a tutorial, how do we avoid that kind of guard and block?


# %%
# 2. Bridging TorchRL → TRL with TorchRLBufferDataset
# ----------------------------------------------------

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# ----------------------------------------------------
# ---------------------------------------------------

@coder-jayp

coder-jayp commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@vmoens Removed the standalone file and folded a corrected end-to-end section (Part 4) into the existing trl_interop.py. Full prompt context included, LLMCollector shown as the production path, and HFRewardModelWrapper uses the canonical ("tokens", "full") / ("masks", "all_attention_mask") keys. Runs in CI with toy models only.

@coder-jayp coder-jayp changed the title [Doc] Add end-to-end tutorial for TRL reward model interoperability [Doc] Fold corrected TRL interoperability end-to-end section into trl_interop tutorial Sep 4, 2026
@coder-jayp

coder-jayp commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

cc @vmoens @theap06

@vmoens

vmoens commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks for iterating on this. I pushed a small cleanup to make the scope and contracts accurate: LLMCollector produces rollouts rather than preference labels, RewardTrainer and GRPOTrainer have different dataset requirements, and the toy reward model was not actually trained by the preceding example. The final section now presents the preference buffer as an interoperability boundary rather than claiming a complete end-to-end pipeline.

At a higher level, I still think this raises an organizational question for TorchRL. This is useful as a focused recipe, but our LLM story is still forming and I would rather see it become part of a broader sequence covering collection/conversation state, preference construction, training, and scoring. In particular, a follow-up should explore how this composes with History and the existing conversational abstractions instead of establishing standalone string fields as the apparent workflow. I don’t think that necessarily needs to block this PR once CI is green, but I would like us to agree on that broader direction so this does not remain an isolated pattern.

cc @theap06: we need and RFC on our LLM APIs to clean it up, make it more suitable for large scale training (incl. distributed APIs, titan/megatron integration, etc). One thing I'm particularly worried/thinking about is how do we guarantee that we have one way and only one way of doing things such that it's not confusing for users.

I think the tutorials should be the last step of our LLM post training integration.

@coder-jayp

Copy link
Copy Markdown
Contributor Author

@vmoens I completely agree. This tutorial serves as a focused recipe for bridging to TRL right now, but it makes total sense that it should eventually integrate with the History abstractions.

Once the larger LLM API RFC is finalized (especially around distributed APIs and unified conversational state), we can revisit and update these tutorials so they demonstrate the single, canonical workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Documentation Improvements or additions to documentation tutorials/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants