Torch::jit::load() failed after throwing an instance of 'torch::jit::ErrorReport'

I have a model that is largely based on VGG16 but simpler as follows:

class MyVGG16(nn.Module):

    def __init__(self, num_classes=10):
        super(VGG16MinusMinus, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU())
        self.layer2 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
...
        self.fc = nn.Sequential(
            nn.Linear(int(224 / 32) * int(426 / 32) * 512, int(4096 / 4)),
            nn.Dropout(self.dropout),
            nn.ReLU())
        self.fc1 = nn.Sequential(
            nn.Linear(int(4096 / 4), int(4096 / 4)),
            nn.Dropout(self.dropout),
            nn.ReLU())
        self.fc2 = nn.Sequential(
            nn.Linear(int(4096 / 4), num_classes))

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
...
        x = self.layer13(x)
        x = x.reshape(x.size(0), -1)
        x = self.fc(x)
        x = self.fc1(x)
        x = self.fc2(x)
        return x

Following the instructions here, I instantiate the model and save it by:

v16 = MyVGG16(num_classes=2)
v16_ts = torch.jit.script(v16)
v16_ts.save("v16_ts.pt")

The above code does not complain anything and I use the sample C++ program to load the model. I compile the sample program on Ubuntu 22.04.3 LTS with g++ 11.4.0 using the following command:

g++ model.cpp -o model.out -ltorch_cpu -lc10 -g

Then I run the program ./model.out model_ts.pt , it prints:

terminate called after throwing an instance of 'torch::jit::ErrorReport'
terminate called recursively
Aborted (core dumped)

Running the program with gdb --args ./model.out model_ts.pt shows:

(gdb) run
Starting program: <root>/model.out model_ts.pt
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffda959640 (LWP 11342)]
[New Thread 0x7fffd2158640 (LWP 11343)]
[New Thread 0x7fffd1957640 (LWP 11344)]
terminate called after throwing an instance of 'torch::jit::ErrorReport'
terminate called recursively

Thread 1 "model.out" received signal SIGABRT, Aborted.
__pthread_kill_implementation (no_tid=0, signo=6, threadid=140736860777472) at ./nptl/pthread_kill.c:44
44	./nptl/pthread_kill.c: No such file or directory.

Any idea on the reason of error? (It appears to me that the code’s catch{} block is not trigger altogether)

You could rerun the code via gdb and check the stacktrace a to narrow down what exactly is failing.

Updated my post with gdb’s output

After some trial-and-error, it turns out that I cant use the libtorch development package (libtorch-dev) provided by Ubuntu 22.04 LTS and have to download the files from PyTorch’s official site. After using the library from the official download page, it works as expected.