ONNX export using list of nn.Sequential/nn.ModuleList

Hello together,

I have the problem that, if using a list of nn.Sequential or nn.ModuleList the export of ONNX does not work and just prints out numbers. Spliiting all elements from the list to separate objects instead works as usual. Does someone know why this is actually happening and may explain it to me please? Is this a wanted behaviour?

Maybe one example code snippet to make more clear what my problem is right now.

Thanks in advance! :slight_smile:

import torch
import torch.nn as nn

class DoesNotWork(nn.Module):
    def __init__(self) -> None:
        super().__init__()

        self.seq = [nn.Sequential(*[
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
        ])]
    
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.seq[0](x)

class Works(nn.Module):
    def __init__(self) -> None:
        super().__init__()

        self.seq = nn.Sequential(*[
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
        ])
    
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.seq(x)

if __name__ == '__main__':
    ex_mod1 = Works().eval()
    ex_mod2 = DoesNotWork().eval()
    input   = torch.rand(1, 3, 960, 1280)
    y1      = ex_mod1(input) 
    y2      = ex_mod2(input) 

    torch.onnx.export(
        model           = ex_mod1,
        args            = (input),   # input
        f               = r".../mod.onnx",
        export_params   = True,         
        verbose         = False,
        input_names     = ["i_x"],
        output_names    = ["o_y"],
        opset_version   = 11            
    )

    torch.onnx.export(
        model           = ex_mod2,
        args            = (input),   # input
        f               = r".../mod.onnx",
        export_params   = True,          
        verbose         = False,
        input_names     = ["i_x"],
        output_names    = ["o_y"],
        opset_version   = 11            
    )

Ok,

I have found an explanation here if someone else is interested. I have overseen the actual error. :smiley:

Cannot insert a Tensor that requires grad as a constant - jit - PyTorch Forums.

1 Like