Fix for hooks with no name (#37785)
Summary:
Fix https://github.com/pytorch/pytorch/issues/37672
Make sure we only access fields that exist and handle python errors correctly.
Before the fix, the given test would throw:
```
AttributeError: 'MyHookClass' object has no attribute '__name__'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test_autograd.py", line 432, in test_hook_with_no_name
x.sum().backward()
File "/Users/albandes/workspace/pytorch_dev/torch/tensor.py", line 184, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/Users/albandes/workspace/pytorch_dev/torch/autograd/__init__.py", line 115, in backward
allow_unreachable=True) # allow_unreachable flag
SystemError: <built-in method run_backward of torch._C._EngineBase object at 0x112fd8100> returned a result with an error set
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/37785
Differential Revision: D21387946
Pulled By: albanD
fbshipit-source-id: dcb9afa37b3e10620dc9182d8aa410e7130ffb64