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 sure SlateQ works with GPU. #22738

Merged
merged 6 commits into from
Mar 4, 2022

Conversation

gjoliver
Copy link
Member

@gjoliver gjoliver commented Mar 1, 2022

Why are these changes needed?

Create models and variables on proper device so SlateQ works with GPU training.

Related issue number

Checks

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

@@ -41,14 +41,15 @@ def build_slateq_model_and_distribution(
Returns:
Tuple consisting of 1) Q-model and 2) an action distribution class.
"""
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, the TorchPolicy will take care of all this.

I think we only have to make sure all tensors in the loss function are on the right device. ...

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, I understand everything now.
it's the target_model that was the issue. we just need to use the correct target_model out of policy.target_models for things to work.
thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yeah, sorry, I should have thought about this. Yes, you can always do:

correct_target_model_to_use = policy.target_models[model]
...

@@ -154,7 +155,7 @@ def build_slateq_losses(

clicked = torch.sum(click_indicator, dim=1)
mask_clicked_slates = clicked > 0
clicked_indices = torch.arange(batch_size)
clicked_indices = torch.arange(batch_size).to(policy.device)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, this seems (almost) correct.

clicked_indices = torch.arange(batch_size).to(clicked.device). # <- some tensor that we know is already on one of the GPU.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep. this was actually how I did it originally. :)
updated.

@@ -320,7 +321,10 @@ def score_documents(
torch.multiply(user_obs.unsqueeze(1), torch.stack(doc_obs, dim=1)), dim=2
)
# Compile a constant no-click score tensor.
score_no_click = torch.full(size=[user_obs.shape[0], 1], fill_value=no_click_score)
# Make sure it lives on the same device as scores_per_candidate.
score_no_click = torch.full(
Copy link
Contributor

Choose a reason for hiding this comment

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

looks great.

# [1, AxS] Useful for torch.take_along_dim()
policy.slates_indices = policy.slates.reshape(-1).unsqueeze(0).to(policy.device)

setup_mixins(policy)
Copy link
Contributor

Choose a reason for hiding this comment

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

setup_late_mixins()

??

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, reverted. I was trying to move policy.slates_indices to the correct device during late_setup.
but I am doing this the correct way now.

@sven1977 sven1977 changed the title [RLlib] make sure SlateQ works with GPU [RLlib] Make sure SlateQ works with GPU. Mar 2, 2022
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! Thanks for this important fix @gjoliver !

@sven1977 sven1977 merged commit e765915 into ray-project:master Mar 4, 2022
@gjoliver gjoliver deleted the slateq_gpu branch April 1, 2022 07:00
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.

2 participants