Error running libtorch example program

mboedigh: <p>I’ve downloaded the debug and release versions (current stable, v1.2) of libtorch and tried the example c++ program</p>
<blockquote>
<p><span class="hashtag-raw">#include</span> &lt;torch/torch.h&gt;<br>
<span class="hashtag-raw">#include</span> </p>
<p>int main() {<br>
torch::Tensor tensor = torch::rand({ 2, 3 });<br>
std::cout &lt;&lt; tensor &lt;&lt; std::endl;<br>
return 0;<br>
}</p>
</blockquote>
<p>I successfully built debug and release versions of solution files for visual studio 2017 using cmake (I needed to set CMAKE_PREFIX_PATH in the CMakeLists.txt file to make it work though, because cmake failed work if I passed the argument to cmake on the command line)<br>
After compiling in Visual Studio, I’m able to run the debug version of the program fine.<br>
However, running the release version (sometimes) throws an exception.  It appears after the main function returns. An error dialog pops up with the text “Debug Assertion Failed!” The File is in a runtime library minkernel\crts\ucrt\src\apcrt\heap\debug_heap.cpp line:904.<br>
The expression: _CrtlsValidHeapPointer(blk)</p>
<p>It runs fine if I comment out the line declaring the tensor and replace it with a default tensor constructor</p>
<blockquote>
<p>// torch::Tensor tensor = torch::rand({ 2, 3 });<br>
torch::Tensor tensor;</p>
</blockquote>
<p>It does not seem to matter whether I build with v141 or v142 of MSVC toolchain or use 10.0.18362.0 or 10.0.17763.0 versions of the SDK.</p>
<p>I’ve also tried turning off all optimizations and in-lining. No help there either.</p>
<p>The only thing odd I noted is compiler warnings:</p>
<blockquote>
<p>2&gt;…\torch\libtorch-release\include\torch/csrc/jit/tracer.h(302): warning C4273: ‘torch::jit::tracer::addInputs’: inconsistent dll linkage</p>
</blockquote>
<ol>
<li>
<p>I don’t know why I have a debug assertion at all, since this is the release version of the app (I’ve confirmed it is compiled with /MD, not /MDd, which is for the multi-threaded dynamic link runtime)</p>
</li>
<li>
<p>Any other ideas what this is all about?</p>
</li>
</ol>

yf225: <p>cc <a class="mention" href="/u/peterjc123">@peterjc123</a>  Do you have any suggestions?</p>

peterjc123: <p>It sounds a bit weird. Would you please upload your project so that I could have a look?</p>

mboedigh: <p>sure. I’m not sure what will be most helpful. Here is the whole directory. It includes the sln, code, intermediates. Let me know if you want something else. Wait. how do I upload a zip? Or how do I do upload a project?</p>

mboedigh: <p>here is a link to the zip file. If needed I could probably create a git repository.<br>
<a href="http://example" rel="nofollow noopener">https://drive.google.com/open?id=1SexWYHC8Wy6g5-TzZ95XMGjfqDqfsNIt</a></p>

mboedigh: <p><a class="mention" href="/u/peterjc123">@peterjc123</a>. If you have time please take a look and see I missed. If you want me to upload something else please let me know.</p>

peterjc123: <p>I tried to build using the CMakeList.txt and example-app.cpp you provided, and there isn’t any problem.<br>
Here is what I did.</p>
<pre><code class="lang-auto"># Suppose you store libtorch in `C:\\Users\\Mike\\Libs\\torch\\libtorch-release`
# And the cmake project is stored in `C:\\Users\\Mike\\Libs\\torch\\example-app`
cd C:\Users\Mike\Libs\torch\example-app
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH="C:\\Users\\Mike\\Libs\\torch\\libtorch-release" ..
cmake --build . --config Release
copy /Y C:\Users\Mike\Libs\torch\libtorch-release\lib\*.dll C:\Users\Mike\Libs\torch\example-app\build\Release\
cd Release
example-app.exe
</code></pre>
<p>For the debug configuration, you’ll only need to replace all occurences of <code>Release</code> into <code>Debug</code>.<br>
You may also be interested in using Visual Studio for debugging, there may be some tricky points.</p>
<ol>
<li>The VS project defaults to x64 and Debug. (Make sure it matches the version of the libraries)</li>
<li>You need to set the startup project to <code>example-app</code>.</li>
<li>You’ll need to copy the DLLs from LibTorch manually.</li>
</ol>

mboedigh: <p>First. Thanks for taking a look and showing a clear example. It differs from the instructions in the Getting Started web-page. In the end, I think you fixed the problem, but not without the notes below and I still think there is a configuration bug (mentioned in 4th bullet)</p>
<ul>
<li>
<p>Compilation was not the trouble I was having. After running the executable multiple time (usually &gt;= 3) the program would throw an exception.</p>
</li>
<li>
<p>The example above doesn’t complete config step of cmake, because CUDA is only compatible with 64 bit builds.</p>
</li>
</ul>
<pre><code class="lang-auto">CUDA support not available with 32-bit windows.  Did you forget to set
  Win64 in the generator target?
</code></pre>
<p>I solved this by adding -G “Visual Studio 15 2017 Win64” option on the command line to cmake</p>
<ul>
<li>The example above does not build for me because it is looking for CUDA 9.2 during the <code>cmake --build . --config Release</code> command</li>
</ul>
<pre><code class="lang-auto">LINK : fatal error LNK1181: cannot open input file 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\lib\x64\cudart_static.lib' [C:\Users\Mik
e\Libs\torch\example-app\build\example-app.vcxproj]
</code></pre>
<p>I don’t know why it is doing this. I never  had CUDA 9.2 installed. My CUDA_PATH is set correctly, so cmake should find the correct CUDA. Also, CMakeCache.txt only mentions CUDA 10.0.</p>
<ul>
<li>
<p>In the generated example-app.vcxproj, cudart_static.lib is listed twice for each build type (Release, Debug, …)  I deleted mention of non-existent cudart_static.lib and the build worked</p>
</li>
<li>
<p>On my 3rd execution of the program I got the same error pop up as before.</p>
</li>
</ul>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/c/c7460b218184202de33c7cfe80e842ad5b879e4e.png" alt="Annotation%202019-08-24%20075318" data-base62-sha1="sqR5ddniIDgneFTrUU7PBOa6gJU" width="404" height="292"></p>
<p>Then I noticed I didn’t copy the dlls over to my release directory. The program usually still ran, and I never get a message about not having them available.</p>
<ul>
<li>Why does the program need the dlls? My program seems to only links .lib files. I initially skipped this step. THIS IS THE KEY STEP. now it runs every time.</li>
</ul>

peterjc123: <p>The <code>.lib</code> files are useful even when doing dynamic linking in MSVC, at which time they serve as the import libraries. It differs from GCC that <code>.so</code> is only used for dynamic linking, while <code>.a</code> is only for static linking. For the pre-compiled LibTorch binaries, they are built dynamically, so you need the DLL files.</p>