The problem about Loading a Pytorch Model in C++

username: kevinhaoliu
cooked: <p>I follow the tutorial and use the same code but it comes up a problem</p>
<h4>*** Error in `./example-app': free(): invalid pointer: 0x00007ffe733866b0 ***</h4>
I run them in the centos, How can I solve this problem?

username: ptrblck
cooked: <p>Could you post the complete stack trace, if you have it?<br>
You are apparently trying to free something which isn’t pointing to a “freeable” memory address.</p>

username: kevinhaoliu
cooked: <p>stack trace:<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/25865c544fdb1287e9e94e172cf88e0aadc67150.png" data-download-href="https://discuss.pytorch.org/uploads/default/25865c544fdb1287e9e94e172cf88e0aadc67150" title="stack%20trace"><img src="https://discuss.pytorch.org/uploads/default/original/2X/2/25865c544fdb1287e9e94e172cf88e0aadc67150.png" alt="stack%20trace" data-base62-sha1="5lXx24gotjSuzjtjX8ySd15CADe" width="690" height="158" data-dominant-color="111111"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">stack%20trace</span><span class="informations">1694×390 20.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: kevinhaoliu
cooked: <p>But I only run the same code by following the tutorial.</p>

username: kevinhaoliu
cooked: <p>I update the g++ from 4.8.5 to 4.9.2 and it works.</p>

username: zaynabhabibi
cooked: <p>I update the g++ too, but still have the same problem.</p>

username: kevinhaoliu
cooked: <p>What’s your g++ version?</p>

username: zaynabhabibi
cooked: <p>gcc version 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC)</p>

username: kevinhaoliu
cooked: <p>You can go to <a href="https://github.com/pytorch/pytorch/issues/21627" rel="nofollow noopener">this</a> to explore possible solutions.</p>

username: zaynabhabibi
cooked: <p>Sorry, I re-compile the project and  no more free() problem.<br>
It is another one <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>operation failed in interpreter:<br>
op_version_set = 0<br>
def forward(self,<br>
input: Tensor) -&gt; Tensor:<br>
if bool(torch.gt(torch.sum(input), 0)):<br>
output = torch.mv(self.weight, input)<br>
~~~~~~~~ &lt;— HERE<br>
else:<br>
output_2 = torch.add(self.weight, input, alpha=1)<br>
output = output_2<br>
return output</p>
<p>Abandon (core dumped)</p>

username: kevinhaoliu
cooked: <p>Could you please show me your C++ code?</p>

username: zaynabhabibi
cooked: <p>I use the tutorial code :</p>
<p><span class="hashtag">#include</span> &lt;torch/script.h&gt; // One-stop header.</p>
<p><span class="hashtag">#include</span> <br>
<span class="hashtag">#include</span> </p>
<p>int main(int argc, const char* argv[]) {<br>
if (argc != 2) {<br>
std::cerr &lt;&lt; “usage: example-app \n”;<br>
return -1;<br>
}</p>
<p>// Deserialize the ScriptModule from a file using torch::jit::load().<br>
std::shared_ptr<a>torch::jit::script::Module</a> module = torch::jit::load(argv[1]);</p>
<p>assert(module != nullptr);<br>
std::cout &lt;&lt; “ok\n”;<br>
/ Create a vector of inputs.<br>
std::vector<a>torch::jit::IValue</a> inputs;<br>
inputs.push_back(torch::ones({1, 3, 224, 224}));</p>
<p>// Execute the model and turn its output into a tensor.<br>
at::Tensor output = module-&gt;forward(inputs).toTensor();</p>
<p>std::cout &lt;&lt; output.slice(/<em>dim=</em>/1, /<em>start=</em>/0, /<em>end=</em>/5) &lt;&lt; ‘\n’;<br>
}</p>

username: zaynabhabibi
cooked: <p>And I use this code to create the “model.pt”</p>
<p>import torch</p>
<p>import torchvision</p>
<h1>An instance of your model.</h1>
<p>model = torchvision.models.resnet18()</p>
<h1>An example input you would normally provide to your model’s forward() method.</h1>
<p>example = torch.rand(1, 3, 224, 224)</p>
<p>print(example)</p>
<h1>Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.</h1>
<p>traced_script_module = torch.jit.trace(model, example)</p>
<p>traced_script_module.save(“model.pt”)</p>

username: kevinhaoliu
cooked: <p><a href="https://discuss.pytorch.org/t/loading-batchnorm1d-with-jit/37552">It</a> has the similar problem with you, you can see this for solution.</p>

username: zaynabhabibi
cooked: <p>Thank you. I have the version 1.1.0 but still have the same problem with “torch.mv” function :</p>
<p>terminate called after throwing an instance of ‘std::runtime_error’<br>
what():<br>
mv: Expected 1-D argument vec, but got 4-D (check_1d at /pytorch/aten/src/ATen/native/LinearAlgebra.cpp:143)<br>
frame <span class="hashtag">#0:</span> std::function&lt;std::string ()&gt;::operator()() const + 0x11 (0x7f30c5a46441 in /home/pixur/Documents/Zaynab/TestLibtorch/libtorch/lib/libc10.so)</p>
<p>I didn’t mke any chnge in the code, I just follow the tutorial</p>

username: zaynabhabibi
cooked: <p>I used the wrong model.pt. It working now. Thank you!</p>