Skip to content

[BugFix] Subtract the max before exp in MPPIPlanner weights - #4721

Open
Aarav500 wants to merge 1 commit into
pytorch:mainfrom
Aarav500:fix-mppi-weight-overflow
Open

Aarav500 wants to merge 1 commit into
pytorch:mainfrom
Aarav500:fix-mppi-weight-overflow

Conversation

@Aarav500

Copy link
Copy Markdown

Description

MPPIPlanner.planning computed the top-k weights as (self.temperature * vals).exp() and then normalized them by their sum. In float32, exp overflows above about 88.7 and underflows below about -103.9, so the normalization became inf/inf or 0/0 and the planner returned NaN actions.

This subtracts the max of temperature * vals over the top-k dimension before exp. The weights are normalized right after, so their values don't change when nothing overflows. TdMpc2Planner already does the same.

Test: test_MPPI_advantage_offset_invariant shifts every advantage by +200 and by -200 and checks that the plan equals the unshifted one, since MPPI weights are a softmax and can't depend on a constant offset. Before the fix both cases return NaN; after it they match.

The issue's Pendulum reproduction, before and after:

Case before after
horizon 5 -1.9868 -1.9868
horizon 12 nan 1.9245
horizon 30 nan -1.7440
temperature 3, horizon 5 nan -1.9974
offset 1000, horizon 5 nan -1.9868

pytest test/modules/test_planners.py: 24 passed, 6 skipped (Hydra not installed), CPU only. ufmt check and flake8 --config=setup.cfg pass on both files with the versions pinned in .pre-commit-config.yaml.

AI disclosure: the change, the test and this description were written by Claude (an AI assistant) working for me, and run on my machine.

Motivation and Context

close #4717 (an existing issue, filed by someone else)

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

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

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

exp(temperature * advantage) overflows float32 above ~88.7 and underflows
below ~-103.9, so the normalized weights became inf/inf or 0/0 and the
planner returned NaN actions. Subtracting the max leaves the normalized
weights unchanged. Fixes pytorch#4717.
@pytorch-bot

pytorch-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 16 Awaiting Approval

As of commit 4a314a2 with merge base a711ef9 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Nov 11, 2026

Copy link
Copy Markdown

Hi @Aarav500!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@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 Nov 11, 2026
@meta-cla

meta-cla Bot commented Nov 11, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
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. Integrations/torch_geometric Integrations Modules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MPPIPlanner returns NaN actions when temperature times advantage is large in magnitude

1 participant