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

[BUG] TensorDict.to moves all tensors to cuda:0 if index not specified #1158

Open
3 tasks done
ctongfei opened this issue Jan 2, 2025 · 0 comments
Open
3 tasks done
Assignees
Labels
bug Something isn't working

Comments

@ctongfei
Copy link

ctongfei commented Jan 2, 2025

Describe the bug

When moving a TensorDict to a CUDA device whose index is not specified, tensordict moves it to cuda:0, where it should be moved to the current device as selected by torch.cuda.current_device() or torch.cuda.get_node_local_rank().

To Reproduce

import tensordict
import torch.cuda

torch.cuda.set_device(1)
# sets the current device index to be 1

assert torch.tensor(1).to(device=torch.device('cuda')).device.index == 1  
# passes assertion

assert TensorDict({'key': torch.tensor(1)}).to(device=torch.device('cuda')).device.index == 1  
# fails assertion! returned index == 0 instead of 1 

Reason and Possible fixes

The bug appears at

device = torch.device("cuda:0")
:

It should be changed to

    if device and device.type == "cuda" and device.index is None:
        device = torch.device("cuda", index=torch.cuda.current_device())

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)
@ctongfei ctongfei added the bug Something isn't working label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants