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

RecursionError when used on subclass of Tensor #65

Closed
Varal7 opened this issue Jun 17, 2021 · 5 comments
Closed

RecursionError when used on subclass of Tensor #65

Varal7 opened this issue Jun 17, 2021 · 5 comments

Comments

@Varal7
Copy link
Contributor

Varal7 commented Jun 17, 2021

When using subclasses of Tensor, make_dot crashed with RecursionError.

class DummyTensor(torch.Tensor):
    pass

a = DummyTensor(torch.ones(5, requires_grad=True))
make_dot(a)

Output:

/usr/local/lib/python3.7/dist-packages/torchviz/dot.py in add_base_tensor(var, color)
    152             dot.edge(str(id(var.grad_fn)), str(id(var)))
    153         if var._is_view():
--> 154             add_base_tensor(var._base, color='darkolivegreen3')
    155             dot.edge(str(id(var._base)), str(id(var)), style="dotted")
    156 

RecursionError: maximum recursion depth exceeded in __instancecheck__

The reason is because a is a view, so is a._base, so is a._base._base, so is a._base._base._base, etc.

https://colab.research.google.com/drive/1VlAEQY5lOtp3MsYfgyCkrNmkjzuKoRX1?usp=sharing

@albanD
Copy link
Contributor

albanD commented Jun 17, 2021

I'm confused, why is a a view here?

@Varal7
Copy link
Contributor Author

Varal7 commented Jun 17, 2021

So the answer is: a is a view of torch.ones(5).

This actually seems like a Pytorch bug.

a = torch.ones(1)
b = torch.Tensor(a)
print(b._is_view()) # True
print(b._base is a) # True
a = torch.ones(1)
c = DummyTensor(a)
print(c._is_view()) # True
print(c._base is a) # False
print(type(c._base)) # <class '__main__.DummyTensor'>

c._base here creates a new DummyTensor, as per the docs

As of version 1.7.0, methods on torch.Tensor and functions in public torch.* namespaces applied on torch.Tensor subclasses will return subclass instances instead of torch.Tensor instances:

@ezyang
Copy link

ezyang commented Jun 21, 2021

Yeah, agreed this is a bug.

@ezyang
Copy link

ezyang commented Jun 21, 2021

As a workaround, do the _base object within the DisableTorchFunction context manager:

with torch._C.DisableTorchFunction():
  base = c._base

ezyang added a commit to pytorch/pytorch that referenced this issue Jun 22, 2021
Fixes szagoruyko/pytorchviz#65

An alternate implementation of this PR would be to remove the
__torch_function__ interposition points for these accessors entirely.
In the end, I decided to opt for extra expressivity.  See
torch.overrides for the criterion on how I decided which accessors
should get the nowrap treatment.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

[ghstack-poisoned]
ezyang added a commit to pytorch/pytorch that referenced this issue Jun 22, 2021
Fixes szagoruyko/pytorchviz#65

An alternate implementation of this PR would be to remove the
__torch_function__ interposition points for these accessors entirely.
In the end, I decided to opt for extra expressivity.  See
torch.overrides for the criterion on how I decided which accessors
should get the nowrap treatment.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

ghstack-source-id: 92c718fcc1e2ef32fad11d1e33cde48875c2945a
Pull Request resolved: #60464
facebook-github-bot pushed a commit to pytorch/pytorch that referenced this issue Jun 22, 2021
Summary:
Pull Request resolved: #60464

Fixes szagoruyko/pytorchviz#65

An alternate implementation of this PR would be to remove the
__torch_function__ interposition points for these accessors entirely.
In the end, I decided to opt for extra expressivity.  See
torch.overrides for the criterion on how I decided which accessors
should get the nowrap treatment.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Test Plan: Imported from OSS

Reviewed By: albanD

Differential Revision: D29302835

Pulled By: ezyang

fbshipit-source-id: fbe0ac4530a6cc9d6759a3fdf5514d4d7b1f7690
@Varal7
Copy link
Contributor Author

Varal7 commented Jun 30, 2021

This is indeed fixed by pytorch/pytorch#60464.

image

@Varal7 Varal7 closed this as completed Jun 30, 2021
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

3 participants