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] Make movement of tensors to device only happen once #36091

Merged
merged 13 commits into from
Jun 8, 2023

Conversation

avnishn
Copy link
Member

@avnishn avnishn commented Jun 5, 2023

Signed-off-by: avnishn [email protected]

Our current minibatching logic in the learner stack forces individual minibatches to be moved to the gpu after they have been sliced. This is wasteful since it creates unnecessary copies of batches, and adds unnecessary movements of the batch over to gpu. This pr addresses this by moving the whole batch to the gpu first, then doing any minibatching operations on it.

Why are these changes needed?

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

@avnishn avnishn changed the title [RLLib] Make movement of tensors to device only happen once [WIP][RLLib] Make movement of tensors to device only happen once Jun 5, 2023
for minibatch in batch_iter(batch, minibatch_size, num_iters):
# Convert minibatch into a tensor batch (NestedDict).
tensor_minibatch = self._convert_batch_type(minibatch)
# Convert minibatch into a tensor batch (NestedDict).
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: "... on the correct device (e.g. GPU)"? This would clarify further.
Also note then that "we only perform copying to the correct device once so we do not have to move data in each minibatch iteration below". something like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -1,5 +1,6 @@
import math


Copy link
Contributor

Choose a reason for hiding this comment

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

two empty lines?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can get rid of these, they're left over from previous commits

@@ -1631,7 +1635,22 @@ def _concat_values(*values, time_major=None) -> TensorType:
time_major: Whether to concatenate along the first axis
(time_major=False) or the second axis (time_major=True).
"""
return np.concatenate(list(values), axis=1 if time_major else 0)
if torch and isinstance(values[0], torch.Tensor):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain why we need these changes (add comment)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, let's use torch.is_tensor().

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

return torch.cat(values, dim=1 if time_major else 0)
elif isinstance(values[0], np.ndarray):
return np.concatenate(values, axis=1 if time_major else 0)
elif tf and isinstance(values[0], tf.Tensor):
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 use tf.is_tensor() instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

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.

Looks great. Just a few nits and questions. Thanks for this important enhancement @avnishn !

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.

Looks great. Just a few nits and questions. Thanks for this important enhancement @avnishn !

@avnishn avnishn changed the title [WIP][RLLib] Make movement of tensors to device only happen once [RLLib] Make movement of tensors to device only happen once Jun 7, 2023
Co-authored-by: Sven Mika <[email protected]>
Signed-off-by: Avnish Narayan <[email protected]>
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. Let's wait for all tests to pass again, then, I'll merge. ...

rllib/core/learner/learner.py Outdated Show resolved Hide resolved
avnishn and others added 3 commits June 7, 2023 17:13
Co-authored-by: kourosh hakhamaneshi <[email protected]>
Signed-off-by: Avnish Narayan <[email protected]>
Signed-off-by: Avnish <[email protected]>
Signed-off-by: Avnish <[email protected]>
@kouroshHakha kouroshHakha merged commit d950281 into ray-project:master Jun 8, 2023
2 checks passed
arvind-chandra pushed a commit to lmco/ray that referenced this pull request Aug 31, 2023
…ect#36091)

Signed-off-by: Avnishn <[email protected]>
Co-authored-by: Sven Mika <[email protected]>
Co-authored-by: kourosh hakhamaneshi <[email protected]>
Signed-off-by: e428265 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants