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

Specifying Specific Cuda Device (PyTorch backend) #66

Open
MShinkle opened this issue Aug 22, 2024 · 5 comments
Open

Specifying Specific Cuda Device (PyTorch backend) #66

MShinkle opened this issue Aug 22, 2024 · 5 comments

Comments

@MShinkle
Copy link

Currently, specifying 'torch_cuda' as the backend appears to select the first CUDA device visible to PyTorch (cuda:0). However, in multi-gpu systems, it would be useful to specify a specific CUDA device through something like:
set_backend("torch_cuda:3")
which would tell Himalaya to use CUDA device 3. set_backend("torch_cuda") would still function equivalently to how it currently does.

Are there any interest or plans for this feature? From glancing through the Himalaya PyTorch backend I don't think implementing this would be too involved, but I could be mistaken.

@mvdoc
Copy link
Collaborator

mvdoc commented Aug 22, 2024 via email

@MShinkle
Copy link
Author

MShinkle commented Aug 22, 2024

PyTorch generally recommends against setting cuda device via this method, in favor of device('cuda:3') or device('cuda', index=3) syntax. Though I think the benefits of the latter (better support for using multiple CUDA devices within the same process) are unlikely to impact 99% of use cases, so maybe not worth the time to incorporate.

Thanks!

@mvdoc
Copy link
Collaborator

mvdoc commented Aug 22, 2024

I think that adding this option to himalaya will complicate the code unnecessarily.
But what happens if you manually push the features and data to the gpu that you want to use, and then those tensors are passed to the himalaya solvers? I wonder whether if this can be a workaround to avoid using the environment variable.

@MShinkle
Copy link
Author

That's an interesting idea, my expectation is that it would convert it to whatever cuda:0 is in the current environment, but I'll give it a test.

@MShinkle
Copy link
Author

Looks like backend.asarray moves tensors to cuda:0 regardless of the original device of the tensor. For example:

import torch
from himalaya.ridge import Ridge
from himalaya.backend import set_backend, get_backend

set_backend('torch_cuda')
backend = get_backend()

print(backend.asarray(torch.zeros(10, device='cuda:1')).device)

Prints cuda:0

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

No branches or pull requests

2 participants