Skip to content

[Feature] Add TD-MPC2 planner - #4455

Merged
bsprenger merged 2 commits into
pytorch:mainfrom
bsprenger:feat/tdmpc2-planner
Oct 4, 2026
Merged

bsprenger merged 2 commits into
pytorch:mainfrom
bsprenger:feat/tdmpc2-planner

Conversation

@bsprenger

Copy link
Copy Markdown
Collaborator

Description

Note

This PR stacks on #4454 . I cannot find a good way to compare the diff against that base, since it is in my own fork.
Once it is merged, I will re-target this one to make the diff easier to read.

This PR introduces the central part of the TD-MPC2 algorithm: the planner module. The planner is responsible for rolling out candidate trajectories at inference time and planning which sequence of actions leads to the best outcome.

Motivation and Context

6th PR in the chain to address #4448 . The planning algorithm is described in depth in the original paper and associated implementation.

  • 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:

  • New feature (non-breaking change which adds core functionality)

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 Sep 21, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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 Sep 21, 2026
@github-actions github-actions Bot added Documentation Improvements or additions to documentation Modules Trainers Feature New feature labels Sep 21, 2026

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.

Let's use test classes whenever we can + short test names

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done, I refactored the whole chain of PRs to use test classes and short names. These changes are folded into the commits on the PRs that introduced each feature.

Comment thread torchrl/trainers/algorithms/tdmpc2.py Outdated

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.

not sure modules/functionals really belong here.

Also another finding from Codex:

Evaluation-time exploration cannot be disabled.

The planner always adds Gaussian noise to the selected action. The reference TD-MPC2 implementation skips this perturbation in evaluation mode. Add an explicit evaluation mode or respect TorchRL’s interaction type, with a regression test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I moved the modules and helpers -- does the new layout look better to you?

And nice find from Codex. I missed that because in the official implementation here, the evaluate.py script actually does not set this eval mode. So I thought it was deprecated/unused. Looking closer , it is used in a few other places, so I added that here. Thanks!

@bsprenger
bsprenger force-pushed the feat/tdmpc2-planner branch 3 times, most recently from 124bd3f to 5edbb7e Compare September 22, 2026 06:04
@bsprenger
bsprenger force-pushed the feat/tdmpc2-planner branch 3 times, most recently from c59c96f to 113f5d2 Compare September 22, 2026 07:42
@bsprenger
bsprenger force-pushed the feat/tdmpc2-planner branch from 113f5d2 to ac4f003 Compare October 1, 2026 14:25
@bsprenger
bsprenger requested a review from theap06 October 3, 2026 14:49

@theap06 theap06 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.

Overall LGTM! Just one nitpick

Comment thread torchrl/modules/planners/tdmpc2.py Outdated
1
)
action = selected[0]
from ...envs.utils import exploration_type, ExplorationType

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.

This import is made at every step. Let's move this to the hot path.

@bsprenger
bsprenger merged commit 65162f2 into pytorch:main Oct 4, 2026
115 of 123 checks passed
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 Feature New feature Integrations/torch_geometric Integrations Modules Objectives Trainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants