Skip to content
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

[RLlib; Offline RL] Implement twin-Q net option for CQL. #47105

Merged
merged 4 commits into from
Aug 13, 2024

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Aug 13, 2024

Why are these changes needed?

This PR proposes the double Q trick for CQL to stabilize training. More specifically

  • it modifies the loss computation to also include loss terms for the twin Q-value function
  • it stores these additional loss terms to run a backward pass with optimizer for the twin Q-value networks

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@simonsays1980 simonsays1980 marked this pull request as ready for review August 13, 2024 12:24
@sven1977 sven1977 changed the title [RLlib; Offline RL] - Implemented double Q in CQL. [RLlib; Offline RL] Implement double-Q option for CQL. Aug 13, 2024
@sven1977 sven1977 changed the title [RLlib; Offline RL] Implement double-Q option for CQL. [RLlib; Offline RL] Implement twin-Q net option for CQL. Aug 13, 2024
@@ -90,8 +92,9 @@ def compute_loss_for_module(
# Use the actions sampled from the current policy.
Columns.ACTIONS: actions_curr,
}
# Note, if `twin_q` is `True`, `compute_q_values` computes the minimum
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

if config.twin_q:
td_error += torch.abs(q_twin_selected, q_selected_target)
# Rescale the TD error
td_error += 0.5
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be * 0.5?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great catch :D This should be multiplied by 0.5

@@ -144,15 +150,24 @@ def compute_loss_for_module(
# Calculate the TD error.
td_error = torch.abs(q_selected - q_selected_target)
# TODO (simon): Add the Twin TD error
if config.twin_q:
td_error += torch.abs(q_twin_selected, q_selected_target)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be torch.minimum?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, meant: Should this be torch.abs(q_twin_selected - q_selected_target)

@@ -144,15 +150,24 @@ def compute_loss_for_module(
# Calculate the TD error.
td_error = torch.abs(q_selected - q_selected_target)
# TODO (simon): Add the Twin TD error
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this TODO.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Another good catch :)

* config.min_q_weight
* config.temperature
)
cql_twin_loss - (q_twin_selected.mean()) * config.min_q_weight
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be -=?

Could you check all the math also once more? I don't want to miss anything important here :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh yes good catch. I hope I just missed the = key and its not my eyes :)

Copy link
Contributor

@sven1977 sven1977 left a comment

Choose a reason for hiding this comment

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

LGTM now. Thanks for this great PR and for double-checking @simonsays1980 .

@sven1977 sven1977 enabled auto-merge (squash) August 13, 2024 16:44
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Aug 13, 2024
@sven1977 sven1977 merged commit dfc6fad into ray-project:master Aug 13, 2024
7 checks passed
simonsays1980 added a commit to simonsays1980/ray that referenced this pull request Aug 14, 2024
simonsays1980 added a commit to simonsays1980/ray that referenced this pull request Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants