Skip to content

[Feature,BugFix] Fix composite entropy for nested keys #3101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2025

Conversation

juandelos
Copy link
Contributor

@juandelos juandelos commented Jul 30, 2025

Description

Updated the function _weighted_loss_entropy to enable setting the entropy coefficient for every head for different nested keys definitions. Now it uses the nested keys and goes through the leaves. If the entropy coefficient is defined for the leave, it uses that. If not, it goes one step up until it finds a key that is defined in the entropy coefficient map.

Motivation and Context

Before, the function had problems when we had nested keys of the type:

  • agents
    • agent1
      • action
    • agent2
      • action

This would result in a problem as it would loop through the keys and try to find the name 'agents' in the entropy coefficient map.

Also, it was not possible to define different entropy coefficients for the same agent, but different action heads like in this example:

  • agent1

    • action1
    • action2
  • agent2

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of examples)

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.

Copy link

pytorch-bot bot commented Jul 30, 2025

🔗 Helpful Links

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

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

⏳ No Failures, 32 Pending

As of commit 1adc46f with merge base 978424e (image):
💚 Looks good so far! There are no failures yet. 💚

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

Copy link

meta-cla bot commented Jul 30, 2025

Hi @juandelos!

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 [email protected]. Thanks!

@amalvaidya
Copy link

If the entropy coefficient is defined for the leave, it uses that. If not, it goes one step up until it finds a key that is defined in the entropy coefficient map.

I think that overall this logic probably isn't necessary. I think it's okay for the user to always have to provide the full, nested key.

@vmoens vmoens changed the title Fix composite entropy for nested keys [Feature,BugFix] Fix composite entropy for nested keys Jul 30, 2025
@vmoens
Copy link
Collaborator

vmoens commented Jul 30, 2025

@juandelos thanks for this PR! Let me know when I should review it.
Can you sign the CLA when ready?

@@ -918,15 +919,37 @@ def _weighted_loss_entropy(
return -self.entropy_coeff * entropy

Copy link
Contributor

Choose a reason for hiding this comment

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

General comment: I feel like this method could use a bit more inline comments to guide the reader.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep agree on that
I can give it a shot

@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 Jul 30, 2025
@juandelos juandelos marked this pull request as ready for review July 31, 2025 07:27
@vmoens vmoens added bug Something isn't working enhancement New feature or request labels Jul 31, 2025
Copy link
Collaborator

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

Giving it a look now.

We're getting

FAILED test/test_cost.py::TestPPO::test_ppo_composite_dists - KeyError: "Missing entropy coeff for head '('agent0', 'action', 'action1', 'sub_action1_log_prob')'"
FAILED test/test_cost.py::TestPPO::test_weighted_entropy_mapping - KeyError: "Missing entropy coeff for head '('head_0', 'action_log_prob')'"

I will write bunch of comments on the _weighted_loss_entropy function to keep track of everything it's doing - and push it to your branch is that works for you!

@@ -918,15 +919,37 @@ def _weighted_loss_entropy(
return -self.entropy_coeff * entropy

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep agree on that
I can give it a shot

@@ -1075,7 +1079,7 @@ def __init__(
clip_epsilon: float = 0.2,
entropy_bonus: bool = True,
samples_mc_entropy: int = 1,
entropy_coeff: float | Mapping[str, float] | None = None,
entropy_coeff: float | Mapping[str | tuple | list, float] | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

@louisfaury NestedKey here too right?

Suggested change
entropy_coeff: float | Mapping[str | tuple | list, float] | None = None,
entropy_coeff: float | Mapping[NestedKey | tuple | list, float] | None = None,

@vmoens vmoens force-pushed the fix/composite_entropy_nested_keys branch from 4db6eed to 1adc46f Compare August 1, 2025 13:12
@vmoens vmoens merged commit d34dbb2 into pytorch:main Aug 1, 2025
69 of 90 checks passed
@vmoens
Copy link
Collaborator

vmoens commented Aug 1, 2025

Follow-up: #3109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants