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

ProvenanceTensor bug when used with torch.set_default_device #3218

Closed
ordabayevy opened this issue May 21, 2023 · 3 comments
Closed

ProvenanceTensor bug when used with torch.set_default_device #3218

ordabayevy opened this issue May 21, 2023 · 3 comments
Labels

Comments

@ordabayevy
Copy link
Member

ordabayevy commented May 21, 2023

When trying to debug the code from the forum post I came across this bug:

import torch
from pyro.ops.provenance import ProvenanceTensor

device = torch.device("cuda")
torch.set_default_device(device)
x = torch.tensor([1., 2., 3.])
y = ProvenanceTensor(x, frozenset(["x"]))
print(torch.as_tensor(y))

returns tensor([], device='cuda:0')

Two observations:

  1. This doesn't happen if torch.set_default_device is not used: print(torch.as_tensor(y.cuda())) works fine
  2. When torch.set_default_device is used then the following code gets invoked
    https://github.com/pytorch/pytorch/blob/main/torch/utils/_device.py#L72-L76
    where func(*args, **kwargs) returns tensor([], device='cuda:0'). This doesn't happen when .cuda() is used like in 1).
@ordabayevy ordabayevy added the bug label May 21, 2023
@ilia-kats
Copy link
Contributor

I don't think it's related to set_default_device, but rather more generally to device handling in as_tensor:

import torch
from pyro.ops.provenance import ProvenanceTensor

device = torch.device("cuda")
x = torch.tensor([1., 2., 3.])
y = ProvenanceTensor(x, frozenset(["x"]))
print(torch.as_tensor(y, device=device))

also returns an empty tensor

@ilia-kats
Copy link
Contributor

I think device handing in ProvenanceTensor is generally buggy:

import torch
from pyro.ops.provenance import ProvenanceTensor

device = torch.device("cuda")
x = torch.tensor([1., 2., 3.])
y = ProvenanceTensor(x, frozenset(["x"]))
print(torch.as_tensor(y.cuda(), device="cpu").device)

returns device(type='cuda', index=0)

but

print(torch.as_tensor(torch.arange(5).cuda(), device="cpu").device)

returns device(type='cpu') as expected

@ilia-kats
Copy link
Contributor

Apparently ProvenanceTensor.__torch_function__ is not being called in as_tensor. It is being called for y.cuda(), though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants