Error in torchScript based on Pytorch tutorial

Capdi: <p>I’m new to Pytorch and it C++ API. I followed the <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="noopener nofollow ugc">this tutorial</a> in order to learn how to load and process a .pt file in C++.</p>
<p>Unfortunately, while the code is built correctly, and loads the pt file without errors, when I try to execute something from the saved module, it raises the following error:</p>
<pre><code class="lang-auto">
terminate called after throwing an instance of 'std::runtime_error'
  what():  The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__.py", line 11, in forward
    if bool(torch.gt(torch.sum(input), 0)):
      weight = self.weight
      output = torch.mv(weight, input)
               ~~~~~~~~ &lt;--- HERE
    else:
      weight0 = self.weight

Traceback of TorchScript, original code (most recent call last):
  File "torchScript.py", line 10, in forward
    def forward(self, input):
        if input.sum() &gt; 0:
            output = self.weight.mv(input)
                     ~~~~~~~~~~~~~~ &lt;--- HERE
        else:
            output = self.weight + input
RuntimeError: vector + matrix @ vector expected, got 1, 2, 4

Aborted (core dumped)
</code></pre>
<p>How can I fix it ?<br>
Thank you in advance</p>

ptrblck: <p>Did you just rerun the entire tutorial or did you change the inputs to another structure?</p>

Capdi: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thank you for your answer. I followed the tutorial without any change. I converted the torch script via annotation and not via tracing.<br>
My torchscript:</p>
<pre><code class="lang-auto">import torch

class MyModule(torch.nn.Module):
    def __init__(self, N, M):
        super(MyModule, self).__init__()
        self.weight = torch.nn.Parameter(torch.rand(N, M))

    def forward(self, input):
        if input.sum() &gt; 0:
            output = self.weight.mv(input)
        else:
            output = self.weight + input
        return output

my_module = MyModule(10,20)
sm = torch.jit.script(my_module)

sm.save("my_module_model.pt")
</code></pre>
<p>My cpp code:</p>
<pre><code class="lang-auto">#include &lt;torch/script.h&gt; // One-stop header.

#include &lt;iostream&gt;
#include &lt;memory&gt;

int main(int argc, const char* argv[]) {
  if (argc != 2) {
    std::cerr &lt;&lt; "usage: example-app &lt;path-to-exported-script-module&gt;\n";
    return -1;
  }


  torch::jit::script::Module module;
  try {
    // Deserialize the ScriptModule from a file using torch::jit::load().
    module = torch::jit::load(argv[1]);
  }
  catch (const c10::Error&amp; e) {
    std::cerr &lt;&lt; "error loading the model\n";
    return -1;
  }

  // Create a vector of inputs.
  std::vector&lt;torch::jit::IValue&gt; inputs;
  inputs.push_back(torch::ones({1, 3, 224, 224}));

  // Execute the model and turn its output into a tensor.
  at::Tensor output = module.forward(inputs).toTensor();
  std::cout &lt;&lt; output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) &lt;&lt; '\n';

  std::cout &lt;&lt; "ok\n";
}
</code></pre>
<p>My setup:<br>
Pytorch: 1.9.0 (building from source)<br>
Cuda: 10.2<br>
Cudnn: 8.2.2<br>
g++: 8.4<br>
Python 3.6 (Anaconda)</p>

ptrblck: <p>Thanks for raising this issue! Could you create an issue on <a href="https://github.com/pytorch/pytorch/issues">GitHub</a> so that we could track and fix it, please?</p>

Capdi: <p>Yes, of course, I’ ll create the issue as soon as possible.</p>