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] CQL: Support multi-GPU/CPU setup and different learning rates for actor, critic, and alpha. #47402

Merged

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Aug 29, 2024

Why are these changes needed?

This PR introduces - similar to SAC - multiple learning rates for CQL, namely one learning rate for each of the three optimizers (i.e. for actor, critic, and the hyperparameter alpha). Furthermore, it moves all forward passes from the learner into the module and therewith enables multi-learner setups.
While SAC had already all forward passes moved into _forward_train, CQL missed the ones that were used for the CQL loss. This PR provides a complete setup.

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

…rted to rewrite CQL loss.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…tes for actor, critic, and alpha. Multi-learner setups works.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@simonsays1980 simonsays1980 marked this pull request as ready for review August 29, 2024 14:41
@sven1977 sven1977 changed the title [RLlib; Offline RL] - Multi-learner setup and learning rates for actor, critic, and alpha. [RLlib; Offline RL] CQL: Support multi-GPU/CPU setup and different learning rates for actor, critic, and alpha. Aug 29, 2024
@@ -84,6 +85,12 @@ def __init__(self, algo_class=None):
self.lagrangian_thresh = 5.0
self.min_q_weight = 5.0
self.lr = 3e-4
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, so for the new stack, users have to set this to None, manually? I guess this is ok (explicit is always good).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, exactly. We discussed this in the other PR concerning SAC.

def _model_config_auto_includes(self):
return super()._model_config_auto_includes | {
"num_actions": self.num_actions,
"_deterministic_loss": self._deterministic_loss,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Let's remove this deterministic loss thing. It's a relic from a long time ago (2020) when I was trying to debug SAC on torch vs our old SAC on tf. It serves no real purpose and just bloats the code.

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!! That saves us many lines of code!

# here). This is different from doing `.detach()` or `with torch.no_grads()`,
# as these two methds would fully block all gradient recordings, including
# the needed policy ones.
all_params = list(self.pi_encoder.parameters()) + list(self.pi.parameters())
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, why pi? We need to block the q-net gradients (same as in SAC), correct?

Copy link
Contributor

Choose a reason for hiding this comment

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

        all_params = (
            list(self.qf.parameters())
            + list(self.qf_encoder.parameters())
            + list(self.qf_twin.parameters())
            + list(self.qf_twin_encoder.parameters())
        )
        for param in all_params:
            param.requires_grad = False
        output["q_curr"] = self.compute_q_values(q_batch_curr)
        for param in all_params:
            param.requires_grad = True

Copy link
Contributor

Choose a reason for hiding this comment

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

^ from SAC

Copy link
Collaborator Author

@simonsays1980 simonsays1980 Aug 29, 2024

Choose a reason for hiding this comment

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

Well, I don't think so. That was my first impression of it. But using this kind of backward pass let's the CQL loss rise without limit. Which makes sense: The additional loss term is now merely an added constant which is different from time to time, but does not change in regard to its own amount. Removing in this forward pass the requires_grads=False does make the CQL loss term decrease by time - as it should.

Note, the SAC loss is computed earlier and uses in the super()._forward_train exactly the logic you posted above.

+ list(self.qf_twin.parameters())
+ list(self.qf_twin_encoder.parameters())
)
all_params = list(self.qf.parameters()) + list(self.qf_encoder.parameters())
Copy link
Contributor

Choose a reason for hiding this comment

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

great catch!!

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…dy sampled log-probabilities.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…l one published by Kumar et al. (2020).

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@sven1977 sven1977 enabled auto-merge (squash) August 30, 2024 16:09
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Aug 30, 2024
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! Thanks @simonsays1980 !!

@sven1977 sven1977 merged commit eedb407 into ray-project:master Aug 30, 2024
6 of 7 checks passed
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 12, 2024
…arning rates for actor, critic, and alpha. (ray-project#47402)

Signed-off-by: ujjawal-khare <ujjawal.khare@dream11.com>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
…arning rates for actor, critic, and alpha. (ray-project#47402)

Signed-off-by: ujjawal-khare <ujjawal.khare@dream11.com>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
…arning rates for actor, critic, and alpha. (ray-project#47402)

Signed-off-by: ujjawal-khare <ujjawal.khare@dream11.com>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
…arning rates for actor, critic, and alpha. (ray-project#47402)

Signed-off-by: ujjawal-khare <ujjawal.khare@dream11.com>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
…arning rates for actor, critic, and alpha. (ray-project#47402)

Signed-off-by: ujjawal-khare <ujjawal.khare@dream11.com>
@simonsays1980 simonsays1980 deleted the adapt-cql-learning-rates-to-sac-ones branch November 22, 2024 10:57
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