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

mamsds: <p>I have a model that is largely based on VGG16 but simpler as follows:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Following the instructions <a href="https://pytorch.org/tutorials/advanced/cpp_export.html#converting-to-torch-script-via-tracing" rel="noopener nofollow ugc">here</a>, I instantiate the model and save it by:</p>
<pre><code class="lang-auto">v16 = MyVGG16(num_classes=2)
v16_ts = torch.jit.script(v16)
v16_ts.save("v16_ts.pt")
</code></pre>
<p>The above code does not complain anything and I use the <a href="https://pytorch.org/tutorials/advanced/cpp_export.html#a-minimal-c-application" rel="noopener nofollow ugc">sample C++ program</a> to load the model. I compile the sample program on Ubuntu 22.04.3 LTS with g++ 11.4.0 using the following command:</p>
<pre><code class="lang-auto">g++ model.cpp -o model.out -ltorch_cpu -lc10 -g

</code></pre>
<p>Then I run the program <code>./model.out model_ts.pt </code>, it prints:</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'torch::jit::ErrorReport'
terminate called recursively
Aborted (core dumped)
</code></pre>
<p>Running the program with <code>gdb --args ./model.out model_ts.pt</code> shows:</p>
<pre><code class="lang-auto">(gdb) run
Starting program: &lt;root&gt;/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.
</code></pre>
<p>Any idea on the reason of error? (It appears to me that the code’s catch{} block is not trigger altogether)</p>

ptrblck: <p>You could rerun the code via <code>gdb</code> and check the stacktrace a to narrow down what exactly is failing.</p>

mamsds: <p>Updated my post with gdb’s output</p>

mamsds: <p>After some trial-and-error, it turns out that I cant use the <code>libtorch</code> development package (<code>libtorch-dev</code>) 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.</p>