Python builtin forward is currently not supported in Torchscript

I have been puzzled by the inner workings of torch.jit.script and how it compiles the model code. My issue is to use a Python nn.Module class that calls C++/cuda functions during forward. For detailed overview I have created this Github repository: GitHub - maksad/torchscript-debug.

To clarify here is a simple code:

import torch.nn as nn
import torch
from carafe import CARAFEPack

class MyClass(nn.Module):
    def __init__(self):
        super(MyClass, self).__init__()

        self.conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=2)
        self.upsampling = CARAFEPack(channels=1, scale_factor=2)

    def forward(self, x):
        x = self.conv(x)
        x = self.upsampling(x)
        return x

and we can compile it like:

with torch.no_grad():
    my_nn = MyClass()
    my_nn = torch.jit.script(my_nn)

The CARAFEPack is simply nn.Module class, that calls carafe_ext.forward, which is a compiled C++/cuda code. carafe_ext is compiled to .so during installation with a setuptool.

My expectation would be torch.jit.script wouldn’t have any problem to compile it, but in fact it raises RuntimeError:

Python builtin <built-in method forward of PyCapsule object at 0x7fa48f8c4ed0> is currently not supported in Torchscript:
  File "/home/ubuntu/.virtualenvs/temp/lib/python3.6/site-packages/carafe/", line 109
        rmasks = masks.new_zeros(masks.size())
        if features.is_cuda:
            carafe_ext.forward(features, rfeatures, masks, rmasks, self.up_kernel,
            ~~~~~~~~~~~~~~~~~~ <--- HERE
                            self.up_group, self.scale_factor, routput, output)
'CARAFEPack.forward_carafe' is being compiled since it was called from 'CARAFEPack.feature_reassemble'
  File "/home/ubuntu/.virtualenvs/temp/lib/python3.6/site-packages/carafe/", line 92
    def feature_reassemble(self, x: Tensor, mask: Tensor):
        x = self.forward_carafe(x, mask, self.up_kernel, self.up_group, self.scale_factor)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        return x
'CARAFEPack.feature_reassemble' is being compiled since it was called from 'CARAFEPack.forward'
  File "/home/ubuntu/.virtualenvs/temp/lib/python3.6/site-packages/carafe/", line 121
        mask = self.kernel_normalizer(mask)
        x = self.feature_reassemble(x, mask)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        return x

Am I missing specifics of torch.jit.script?

Hi, take a look at Extending TorchScript with Custom C++ Operators — PyTorch Tutorials 1.7.1 documentation, that should answer your question.