Error running libtorch example program

I’ve downloaded the debug and release versions (current stable, v1.2) of libtorch and tried the example c++ program

#include <torch/torch.h>

int main() {
torch::Tensor tensor = torch::rand({ 2, 3 });
std::cout << tensor << std::endl;
return 0;

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)
After compiling in Visual Studio, I’m able to run the debug version of the program fine.
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.
The expression: _CrtlsValidHeapPointer(blk)

It runs fine if I comment out the line declaring the tensor and replace it with a default tensor constructor

// torch::Tensor tensor = torch::rand({ 2, 3 });
torch::Tensor tensor;

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.

I’ve also tried turning off all optimizations and in-lining. No help there either.

The only thing odd I noted is compiler warnings:

2>…\torch\libtorch-release\include\torch/csrc/jit/tracer.h(302): warning C4273: ‘torch::jit::tracer::addInputs’: inconsistent dll linkage

  1. 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)

  2. Any other ideas what this is all about?

cc @peterjc123 Do you have any suggestions?

It sounds a bit weird. Would you please upload your project so that I could have a look?

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?

here is a link to the zip file. If needed I could probably create a git repository.

@peterjc123. If you have time please take a look and see I missed. If you want me to upload something else please let me know.

I tried to build using the CMakeList.txt and example-app.cpp you provided, and there isn’t any problem.
Here is what I did.

# 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

For the debug configuration, you’ll only need to replace all occurences of Release into Debug.
You may also be interested in using Visual Studio for debugging, there may be some tricky points.

  1. The VS project defaults to x64 and Debug. (Make sure it matches the version of the libraries)
  2. You need to set the startup project to example-app.
  3. You’ll need to copy the DLLs from LibTorch manually.
1 Like

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)

  • Compilation was not the trouble I was having. After running the executable multiple time (usually >= 3) the program would throw an exception.

  • The example above doesn’t complete config step of cmake, because CUDA is only compatible with 64 bit builds.

CUDA support not available with 32-bit windows.  Did you forget to set
  Win64 in the generator target?

I solved this by adding -G “Visual Studio 15 2017 Win64” option on the command line to cmake

  • The example above does not build for me because it is looking for CUDA 9.2 during the cmake --build . --config Release command
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

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.

  • 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

  • On my 3rd execution of the program I got the same error pop up as before.


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.

  • 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.

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