Repository: pytorch
Issue: Cannot manually assign a tensor to .grad from TorchScript
Labels: triage review,oncall: jit,days

## 🐛 Bug

When a module is compiled via `torch.jit.script()`, it is not possible to bypass Autograd and manually assign a tensor to the `.grad` attribute of another tensor. This is necessary in specific situations where a gradient must be computed independently from the actual computation graph.

## To Reproduce

Steps to reproduce the behavior:

1. Create a nn module containing a parameter such as `self.W = nn.Parameter(W, requires_grad=False)` so that Autograd does not try to touch it
2. Later, assign a gradient via `self.W.grad = new_gradient`
3. Wrap a module instance within `torch.jit.script()`, then run it and observe the error.

```
RuntimeError:
Tried to set an attribute: grad on a non-class: Tensor:
  File "__main__.py", line 221

        self.W.grad = new_gradient
        ~~~~~~~~~~~~~~~~~~~ <--- HERE
```

## Expected behavior

The gradient should be assigned without error, as it correctly happens when the module is not compiled with TorchScript

## Environment

```
PyTorch version: 1.5.0
Is debug build: No
CUDA used to build PyTorch: 10.2

OS: CentOS Linux release 7.6.1810 (Core)
GCC version: (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
CMake version: version 2.8.12.2

Python version: 3.7
Is CUDA available: No
CUDA runtime version: Could not collect
GPU models and configuration:
GPU 0: Tesla V100-PCIE-16GB
GPU 1: Tesla V100-PCIE-16GB
GPU 2: Tesla V100-PCIE-16GB
GPU 3: Tesla V100-PCIE-16GB

Nvidia driver version: 418.39
cuDNN version: Could not collect

Versions of relevant libraries:
[pip3] botorch==0.1.0
[pip3] gpytorch==0.3.2
[pip3] numpy==1.18.1
[pip3] numpydoc==0.9.1
[pip3] pytorch-pretrained-bert==0.6.2
[pip3] pytorch-transformers==1.1.0
[pip3] torch==1.5.0
[pip3] torch-multi-head-attention==0.15.1
[pip3] torchfile==0.1.0
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.6.0
[pip3] torchvision==0.6.0
[pip3] torchviz==0.0.1
[conda] blas                      1.0                         mkl
[conda] botorch                   0.1.0                    pypi_0    pypi
[conda] cudatoolkit               10.0.130                      0
[conda] gpytorch                  0.3.2                    pypi_0    pypi
[conda] mkl                       2019.4                      243
[conda] mkl-service               2.3.0            py37he904b0f_0
[conda] mkl_fft                   1.0.15           py37ha843d7b_0
[conda] mkl_random                1.1.0            py37hd6b4f25_0
[conda] numpy                     1.17.4                   pypi_0    pypi
[conda] numpy-base                1.18.1           py37hde5b4d6_0
[conda] numpydoc                  0.9.1                    pypi_0    pypi
[conda] pytorch-pretrained-bert   0.6.2                    pypi_0    pypi
[conda] pytorch-transformers      1.1.0                    pypi_0    pypi
[conda] torch                     1.5.0                    pypi_0    pypi
[conda] torch-multi-head-attention 0.15.1                   pypi_0    pypi
[conda] torchfile                 0.1.0                    pypi_0    pypi
[conda] torchsummary              1.5.1                    pypi_0    pypi
[conda] torchtext                 0.6.0                    pypi_0    pypi
[conda] torchvision               0.6.0                    pypi_0    pypi
[conda] torchviz                  0.0.1                    pypi_0    pypi
```


cc @suo @gmagogsfm