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] Add CQLLearner and CQLTorchLearner. #46969

Merged
merged 8 commits into from
Aug 12, 2024

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Aug 5, 2024

Why are these changes needed?

This PR proposes the Learner classes for CQL in the new API stack. It relies on the SACTorchModule (i.e. having a Q-network and Q-target-network) and implements the loss logic. Because three loss terms are optimized the gradients are collected inside the compute_loss_for_module method and are then just returned from compute_gradients. The reason for this are multiple passes through the networks where a follow-up path would erase the gradients computed in the pass before.

This PR is one of several PRs that should implement CQL in the new API stack.

Related issue number

Closes #37779

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

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…oss and switched in actor loss from selected actions to sampled actions from the current policy.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>

# Get the current batch size. Note, this size might vary in case the
# last batch contains less than `train_batch_size_per_learner` examples.
batch_size = batch[Columns.OBS].shape[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we explain this logic here and why we defend against different batch sizes coming in?

Don't we expect always the same batch size from the data pipeline?

If not, we should:
a) explain here why we are expecting various batch sizes
b) probably fix this logic here. What if we call compute_loss_for_module 10x with a batch of train_batch_size_per_learner - 1 (accumulating gradients for these, but not applying these) and then one batch of size train_batch_size_per_learner. In this case, the effective batch size would be roughly 11x the user configured one, correct?

c) Also, what if the incoming batch is larger than train_batch_size_per_learner?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@sven1977 I fully agree on your comment.

a) This was also part of the old stack algorithm. The reason behind this is that when iterating over a dataset the last batch could have less than train_batch_size_per_learner samples in it.

b) The offline logic here is that after each call to compute_loss_for_module a compute_gradients and apply_gradients will occur. No SGD is run on the offline algorithms (yet). So the train batch size should be always as large as configured (without the last one being smaller).

c) This case should also not happen. iter_batches takes care of this and ensures that always the batch size is sampled, without the last one which can be avoided by setting a flag, but neglects data.

# for `alpha`` and the target entropy defined.
super().build()

# Set up the gradient buffer to store gradients to apply
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add more explanation here as to why caching any grads is necessary?

Maybe I don't understand the data pipelines properly yet, but should we try to fix these to always produce the exact same batch sizes (or number of episode timesteps) per iteration?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is not really caused by the batch sizes, but will only be executed if the batch size is as configured. The reason for the grad caching is merely to enable multiple passes through the network during loss calculation.

# the policy.
# TODO (simon, sven): Add upstream information pieces into this timesteps
# call arg to Learner.update_...().
self.metrics.log_value(
Copy link
Contributor

Choose a reason for hiding this comment

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

Add: reduce="sum" here to make sure iterations are not EMA'd :) (the default).

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, you don't need this here anymore. B/c you are logging it below, this entry will be created automatically.


# TODO (simon, sven): Add upstream information pieces into this timesteps
# call arg to Learner.update_...().
self.metrics.log_value(
Copy link
Contributor

@sven1977 sven1977 Aug 9, 2024

Choose a reason for hiding this comment

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

New that reduce="sum" is already defined above, you can just do:

log_value(
    (ALL_MODULES, TRAINING_ITERATION),
    1,
    reduce="sum",
)

alpha = torch.exp(self.curr_log_alpha[module_id])
# Start training with behavior cloning and turn to the classic Soft-Actor Critic
# after `bc_iters` of training iterations.
if self.metrics.peek((ALL_MODULES, TRAINING_ITERATION)) >= config.bc_iters:
Copy link
Contributor

@sven1977 sven1977 Aug 9, 2024

Choose a reason for hiding this comment

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

Change to:

self.metrics.peek(([your key]), default=0)

Copy link
Contributor

Choose a reason for hiding this comment

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

To cover the case, in which this entry does not exist yet.
This way, you don't have to define it up front.

Just log into it as reduce="sum" (and value=1) and all will be good.

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.

Just some nits left, otherwise good to go! Awesome work @simonsays1980 .

@sven1977 sven1977 changed the title [RLlib | Offline RL] - Add CQLLearner and CQLTorchLearner [RLlib; Offline RL] Add CQLLearner and CQLTorchLearner. Aug 12, 2024
@sven1977 sven1977 enabled auto-merge (squash) August 12, 2024 10:33
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Aug 12, 2024
@sven1977 sven1977 disabled auto-merge August 12, 2024 10:33
Signed-off-by: sven1977 <svenmika1977@gmail.com>
@sven1977 sven1977 enabled auto-merge (squash) August 12, 2024 10:39
@sven1977 sven1977 merged commit eecf122 into ray-project:master Aug 12, 2024
6 checks passed
@simonsays1980 simonsays1980 changed the title [RLlib; Offline RL] Add CQLLearner and CQLTorchLearner. [RLlib; Offline RL] Add CQLLearner and CQLTorchLearner. Aug 12, 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.

[RLlib] Enabling RLModule by default on CQL
2 participants