Repository: pytorch
Issue: jit.script() fails to resolve/cast Optional[Tensor] fields of sub-modules or base classes of the object being scripted
Labels: oncall: jit

### 🐛 Describe the bug

Here: in scripted module, I can't use 'bias' of neither inherited nor contained Conv1d class, because script() fails to cast Optional[Tensor] to Tensor, even under 'is not None' condition.
Curiously enough, if I add an unrelated data member (slide_winsize: float below) - if fixes type resolution for base class. Unfortunately, I was not able to find a workaround to force it to properly handle self.c1d.bias below.

```
import torch
from torch import Tensor   
from torch.nn import Conv1d

class PartialConv1d(Conv1d):
    # Uncomment this to make script() work                                                                                                                                             
    # slide_winsize: float                                                                                                                                                             
    def __init__(self, *args, **kwargs):
        super(PartialConv1d, self).__init__(*args, **kwargs)
        self.c1d = Conv1d(*args, **kwargs)

    def forward(self, input, mask):

        input = torch.mul(input, mask)

        raw_out = self.c1d(input)
        # Can that somehow be made to work ?                                                                                                                                           
        # Or direct reference to Conv1d.forward(self, ...) as static method ?                                                                                                          
        # super(PartialConv1d, self).forward(input)                                                                                                                                    
        raw_out = self._conv_forward(input, self.weight, self.bias)

        if self.bias is not None:
            nch = self.out_channels # This always works                                                                                                                                
# Those two work with slide_winsize above uncommented, otherwise error out:                                                                                                            
            # 'Optional[Tensor]' object has no attribute or method 'view'.                                                                                                             
            my_view = self.bias.view(1,1,self.out_channels)
            # Variable 'my_bias' is annotated with type Tensor but is being assigned to a value of type Optional[Tensor]                                                               
            my_bias:Tensor = self.bias
# This fails even with slide_winsize uncommented                                                                                                                                       
            # my_bias2:Tensor = self.c1d.bias                                                                                                                                          
            bias_view = my_bias.view(1, self.out_channels, 1)
            output = torch.mul(raw_out - bias_view, 0.5) + bias_view

        return output


args =  {'kernel_size': 1, 'stride': 1, 'padding': 0, 'dilation': 1, 'bias': True}
c1 = PartialConv1d(1024, 80, **args)

w = c1.weight
s = w.shape
rand_in = torch.ones(1, s[1], s[2], dtype=w.dtype, device=w.device)
lens = torch.ones(1, 1, s[2], dtype=w.dtype, device=w.device)

c1.forward(rand_in, lens)
c2 = torch.jit.script(c1)
```

### Versions

PyTorch version: 1.13.0a0+d0d6b1f
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.5 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.22.2
Libc version: glibc-2.31

Python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10)  [GCC 10.3.0] (64-bit runtime)
Python platform: Linux-5.15.0-46-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: 11.8.89
GPU models and configuration: GPU 0: NVIDIA RTX A5000
Nvidia driver version: 515.65.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.6.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] functorch==0.3.0a0
[pip3] k2==1.21.dev20221109+cuda11.8.torch1.13.0a0
[pip3] numpy==1.22.2
[pip3] pytorch-lightning==1.7.7
[pip3] pytorch-quantization==2.1.2
[pip3] torch==1.13.0a0+d0d6b1f
[pip3] torch-tensorrt==1.3.0a0
[pip3] torchaudio==0.13.0
[pip3] torchmetrics==0.10.2
[pip3] torchvision==0.14.0a0
[conda] functorch                 0.3.0a0                  pypi_0    pypi
[conda] k2                        1.21.dev20221109+cuda11.8.torch1.13.0a0          pypi_0    pypi
[conda] mkl                       2020.4             h726a3e6_304    conda-forge
[conda] mkl-include               2020.4             h726a3e6_304    conda-forge
[conda] numpy                     1.22.2           py38h6ae9a64_0    conda-forge
[conda] pytorch-lightning         1.7.7                    pypi_0    pypi
[conda] pytorch-quantization      2.1.2                    pypi_0    pypi
[conda] torch                     1.13.0a0+d0d6b1f          pypi_0    pypi
[conda] torch-tensorrt            1.3.0a0                  pypi_0    pypi
[conda] torchaudio                0.13.0                   pypi_0    pypi
[conda] torchmetrics              0.10.2                   pypi_0    pypi
[conda] torchvision               0.14.0a0                 pypi_0    pypi
r

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel