Skip to content

Conversation

@pritamdamania87
Copy link
Contributor

@pritamdamania87 pritamdamania87 commented Aug 26, 2020

Stack from ghstack:

This PR attempts to address #42560. We add a "Python Traceback"
component to the error message recording the original location from where the
exception was thrown.

This approach isn't ideal since we're extending the exception message itself.
The alternative I considered was extending our Future to record this
information in addition to just the message. The tricky part about this was
avoiding python dependency. Ideally, I'd like to avoid a python dependency in
our Future class. Even if we do avoid the python depedency in our base class by
creating a subclass for python, we would still end up having a python
dependency in things like autograd engine's GraphTask.

For the example in #42560, the exception trace would now look like:

> Traceback (most recent call last):
>   File "test_autograd.py", line 6914, in test_preserve_backtrace
>     Foo.apply(t).sum().backward()
>   File "torch/tensor.py", line 214, in backward
>     torch.autograd.backward(self, gradient, retain_graph, create_graph)
>   File "autograd/__init__.py", line 127, in backward
>     allow_unreachable=True)  # allow_unreachable flag
> RuntimeError: something
> Python Traceback (most recent call last):
>   File "torch/autograd/function.py", line 87, in apply
>     return self._forward_cls.backward(self, *args)
>   File "test_autograd.py", line 6909, in backward
>     raise ValueError("something")

Differential Revision: D23337371

This PR attempts to address
#42560. We add a "Python Traceback"
component to the error message recording the original location from where the
exception was thrown.

This approach isn't ideal since we're extending the exception message itself.
The alternative I considered was extending our Future to record this
information in addition to just the message. The tricky part about this was
avoiding python dependency. Ideally, I'd like to avoid a python dependency in
our Future class. Even if we do avoid the python depedency in our base class by
creating a subclass for python, we would still end up having a python
dependency in things like autograd engine's GraphTask.

For the example in #42560, the
exception trace would now look like:

```
> Traceback (most recent call last):
>   File "test_autograd.py", line 6914, in test_preserve_backtrace
>     Foo.apply(t).sum().backward()
>   File "torch/tensor.py", line 214, in backward
>     torch.autograd.backward(self, gradient, retain_graph, create_graph)
>   File "autograd/__init__.py", line 127, in backward
>     allow_unreachable=True)  # allow_unreachable flag
> RuntimeError: something
> Python Traceback (most recent call last):
>   File "torch/autograd/function.py", line 87, in apply
>     return self._forward_cls.backward(self, *args)
>   File "test_autograd.py", line 6909, in backward
>     raise ValueError("something")
```

Differential Revision: [D23337371](https://our.internmc.facebook.com/intern/diff/D23337371/)

[ghstack-poisoned]
pritamdamania87 pushed a commit that referenced this pull request Aug 26, 2020
This PR attempts to address
#42560. We add a "Python Traceback"
component to the error message recording the original location from where the
exception was thrown.

This approach isn't ideal since we're extending the exception message itself.
The alternative I considered was extending our Future to record this
information in addition to just the message. The tricky part about this was
avoiding python dependency. Ideally, I'd like to avoid a python dependency in
our Future class. Even if we do avoid the python depedency in our base class by
creating a subclass for python, we would still end up having a python
dependency in things like autograd engine's GraphTask.

For the example in #42560, the
exception trace would now look like:

```
> Traceback (most recent call last):
>   File "test_autograd.py", line 6914, in test_preserve_backtrace
>     Foo.apply(t).sum().backward()
>   File "torch/tensor.py", line 214, in backward
>     torch.autograd.backward(self, gradient, retain_graph, create_graph)
>   File "autograd/__init__.py", line 127, in backward
>     allow_unreachable=True)  # allow_unreachable flag
> RuntimeError: something
> Python Traceback (most recent call last):
>   File "torch/autograd/function.py", line 87, in apply
>     return self._forward_cls.backward(self, *args)
>   File "test_autograd.py", line 6909, in backward
>     raise ValueError("something")
```

Differential Revision: [D23337371](https://our.internmc.facebook.com/intern/diff/D23337371/)

ghstack-source-id: 110722744
Pull Request resolved: #43608
@pritamdamania87 pritamdamania87 requested a review from albanD August 26, 2020 04:28
@albanD
Copy link
Collaborator

albanD commented Aug 26, 2020

Hi,

I don't think that match the issue. For example, the c10::Error exceptions contains information about the c++ stack trace as well that are very useful.
But more generally, these exception objects could contain any user-needed information.
Is there a reason for not propagating the original exception?

@pritamdamania87
Copy link
Contributor Author

Is there a reason for not propagating the original exception?

As I mentioned in the PR summary, to propagate the original exception we need to store this information as part of the Future which would incur a python dependency:

"This approach isn't ideal since we're extending the exception message itself.
The alternative I considered was extending our Future to record this
information in addition to just the message. The tricky part about this was
avoiding python dependency. Ideally, I'd like to avoid a python dependency in
our Future class. Even if we do avoid the python depedency in our base class by
creating a subclass for python, we would still end up having a python
dependency in things like autograd engine's GraphTask."

@albanD
Copy link
Collaborator

albanD commented Aug 26, 2020

As I mentioned in the PR summary, to propagate the original exception we need to store this information as part of the Future which would incur a python dependency

What I had in mind is to save it as a std::exception and then re-trowing it like we used to do in the engine (from a random old commit:

exception_ = std::move(eptr);
). That would not require a python dependency right? And the re-thrown exception will then be handled properly depending on its actual type.

@pritamdamania87
Copy link
Contributor Author

What I had in mind is to save it as a std::exception and then re-trowing it like we used to do in the engine (from a random old commit:

We probably need to save the exception_ptr in the Future itself for this right? Let me look into this to see if we can modify the Future to save an exception_ptr instead of just the exception message.

@pritamdamania87
Copy link
Contributor Author

Abandoning in favor of #43684.

@facebook-github-bot facebook-github-bot deleted the gh/pritamdamania87/154/head branch September 26, 2020 14:15
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.

4 participants