Jit: Tried to access nonexistent attribute or method 'forward' of type 'Tensor'

I am new to TorchScript and I was trying to use torch.jit.script for one of my models and when I do so, I get the following:

RuntimeError: 
Tried to access nonexistent attribute or method 'forward' of type 'Tensor'.:
  File "custom_layers.py", line 32
    def forward(self, x):
        result = super().forward(x)
                 ~~~~~~~~~~~~~ <--- HERE
    
        if self._padding != 0:

The method that is causing issues is the forward() method of the nn.Conv1d instance that is called from the derived class as follows:

class CausalConv1d(nn.Conv1d):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size=1,
                 stride=1,
                 dilation=1,
                 groups=1,
                 bias=True):
        self._padding = (kernel_size - 1) * dilation

        super().__init__(
                 in_channels,
                 out_channels,
                 kernel_size=kernel_size,
                 stride=stride,
                 padding=self._padding,
                 dilation=dilation,
                 groups=groups,
                 bias=bias
                )

    def forward(self, x):
        result = super().forward(x)

        if self._padding != 0:
            return result[:, :, : - self._padding]

        return result

If I hint the input type x as suggested in this topic I still get an error.

Thanks for your help!

Hi I am noticing the same problem as you. Any chance you fixed it ?

I tried a few changes with no luck and had to move to something else so it is still unsolved

Looks like inheritance is not supported Unable to call `super` method with TorchScript · Issue #42885 · pytorch/pytorch · GitHub

Thanks for sharing it! That explains the issue…so apparently only tracing the model is an option in this case since it will only care about operations, but may not work too depending on the model’s logic.

My plan is make the subclass own an instance of the parent class and muck around with that instance’s state / call its forward method

@Prashant did this workaround work for you? It seems like as of today the issue still remains

Yes my workaround worked out for me.

Thanks!

I tried it and worked fine, although since my final code could be traced, I ended up using jit.trace for this section and jit.script for the rest.