LibTorch C++ Windows exception in register_module()

Igor_Aherne: <p>Hello, Yesterday I’ve grabbed the stable version of Libtorch from <a href="https://pytorch.org/get-started/locally/" class="inline-onebox" rel="noopener nofollow ugc">Start Locally | PyTorch</a></p>
<p>it is “Stable (1.13.0) with Cuda 11.7 and with C++ for Windows”.</p>
<p>However getting an exception, when invoking <code>register_module()</code>:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/6/b64d923f8dfec857407e67b343b2c4cf16f61407.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b64d923f8dfec857407e67b343b2c4cf16f61407" title="screen0"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/6/b64d923f8dfec857407e67b343b2c4cf16f61407_2_554x500.jpeg" alt="screen0" data-base62-sha1="q0J5Urd9xVZviaTHqrkkgHYjwA7" width="554" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/6/b64d923f8dfec857407e67b343b2c4cf16f61407_2_554x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/6/b64d923f8dfec857407e67b343b2c4cf16f61407_2_831x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/6/b64d923f8dfec857407e67b343b2c4cf16f61407_2_1108x1000.jpeg 2x" data-dominant-color="262626"><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">screen0</span><span class="informations">1675×1510 225 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>
<p>I’ve first tried building using the usual Visual Studio solution,<br>
and then make a fresh project using a CMake file.</p>
<p>Strangely, getting the exact same behavior in either case. The libraries are connected, the include directories are specified, the code compiles. Until I try to construct the Net object</p>
<p>What might be happening? <img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=12" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>

Igor_Aherne: <p>if we go down the call stack we see that the <code>children_</code> OrderedDictionary has incorrect size and doesn’t appear to be initialized:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/9/09e6f5326d66fc1f8247e54a4b3f8277044b8dad.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/09e6f5326d66fc1f8247e54a4b3f8277044b8dad" title="screen1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/9/09e6f5326d66fc1f8247e54a4b3f8277044b8dad_2_546x500.jpeg" alt="screen1" data-base62-sha1="1pB7z7xonv91oXzx01WNZB0hRCd" width="546" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/9/09e6f5326d66fc1f8247e54a4b3f8277044b8dad_2_546x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/0/9/09e6f5326d66fc1f8247e54a4b3f8277044b8dad_2_819x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/9/09e6f5326d66fc1f8247e54a4b3f8277044b8dad_2_1092x1000.jpeg 2x" data-dominant-color="262727"><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">screen1</span><span class="informations">1697×1552 285 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>

Igor_Aherne: <p>When we access it, it throws this error:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/e/3e71d4a30e096d00bad811664e93ecc63ccf042d.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/3e71d4a30e096d00bad811664e93ecc63ccf042d" title="screen 3"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/e/3e71d4a30e096d00bad811664e93ecc63ccf042d_2_690x443.jpeg" alt="screen 3" data-base62-sha1="8Upu105eYekGzZHF2AjfevIQral" width="690" height="443" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/e/3e71d4a30e096d00bad811664e93ecc63ccf042d_2_690x443.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/e/3e71d4a30e096d00bad811664e93ecc63ccf042d_2_1035x664.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/e/3e71d4a30e096d00bad811664e93ecc63ccf042d_2_1380x886.jpeg 2x" data-dominant-color="242525"><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">screen 3</span><span class="informations">2674×1717 395 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>
<p>Sorry, I’m a new user so I couldn’t upload all 3 pictures into one post and had to split into several.</p>

Igor_Aherne: <p>Caused by downloading the wrong version of Libtorch.<br>
I was compiling stuff in Visual Studio solution in Debug mode, but downloaded Release mode of Libtorch from the website. Needed the Debug instead.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5cd4c31ada32fa248ea9c945b6ecc3957272b0c5.png" data-download-href="https://discuss.pytorch.org/uploads/default/5cd4c31ada32fa248ea9c945b6ecc3957272b0c5" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5cd4c31ada32fa248ea9c945b6ecc3957272b0c5_2_690x451.png" alt="image" data-base62-sha1="dfdLClgojtX7XArb7as1qkTcVGB" width="690" height="451" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5cd4c31ada32fa248ea9c945b6ecc3957272b0c5_2_690x451.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5cd4c31ada32fa248ea9c945b6ecc3957272b0c5_2_1035x676.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5cd4c31ada32fa248ea9c945b6ecc3957272b0c5_2_1380x902.png 2x" data-dominant-color="FAF4F3"><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">image</span><span class="informations">2330×1524 225 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>

vlvrz: <p>Did you achieve execute the program in release mode? i’m facing the same problem, it works fine in debug mode, but not in release when using the release distribution that you first downloaded. Tried with older release versions, but the problem persists</p>

Igor_Aherne: <p>Yes, all works ok in release. In Visual Studio I specify the following as “Additional Include Directories”:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/e/d/edefed7e4276999ffeb2d0df5010b85f9c58efb9.png" alt="image" data-base62-sha1="xWTcBfHURYnRMDN5Ak0I2EAbkJr" width="523" height="90"></p>
<p>And the libraries I’m linking are:<br>
torch.lib<br>
torch_cuda.lib<br>
c10.lib<br>
c10_cuda.lib<br>
torch_cuda_cpp.lib<br>
torch_cpu.lib<br>
caffe2_nvrtc.lib</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/d/1de5200f4e1946119c49f089d3809b01def0fcde.png" data-download-href="https://discuss.pytorch.org/uploads/default/1de5200f4e1946119c49f089d3809b01def0fcde" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/d/1de5200f4e1946119c49f089d3809b01def0fcde_2_690x378.png" alt="image" data-base62-sha1="4gsKcw5xhjNVV1ERqAcQ6krpsLA" width="690" height="378" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/d/1de5200f4e1946119c49f089d3809b01def0fcde_2_690x378.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/d/1de5200f4e1946119c49f089d3809b01def0fcde_2_1035x567.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/d/1de5200f4e1946119c49f089d3809b01def0fcde_2_1380x756.png 2x" data-dominant-color="EAEBEB"><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">image</span><span class="informations">2793×1533 221 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>
<p>Finally, I’m copying all the .dll files from C:\src\libtorch\Release\lib\ into the folder where my executable will be. I do it in Visual studio via Project Properties-&gt;Post-Build Events-&gt; Command Line:</p>
<p>xcopy /y “C:\src\libtorch\Release\lib*.dll” "C:_myDrive\repos\harvest\Harvest\Release".</p>
<p>I’m using C++20 and the toolset, which the Libtorch doesn’t quite like, because it’s made for C++14. So I’m using a precompiled header (header that is included before any other header), where I wrap the <span class="hashtag-raw">#include</span> &lt;torch/torch&gt; with several pragma-warning-disables.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/4/e4444633b9d6e14d10efa59f7e542444586a33cb.png" data-download-href="https://discuss.pytorch.org/uploads/default/e4444633b9d6e14d10efa59f7e542444586a33cb" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/4/e4444633b9d6e14d10efa59f7e542444586a33cb_2_690x328.png" alt="image" data-base62-sha1="wzl8GCbVCItClPHrlqZZLZHMVgT" width="690" height="328" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/4/e4444633b9d6e14d10efa59f7e542444586a33cb_2_690x328.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/4/e4444633b9d6e14d10efa59f7e542444586a33cb_2_1035x492.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/4/e4444633b9d6e14d10efa59f7e542444586a33cb_2_1380x656.png 2x" data-dominant-color="787C7B"><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">image</span><span class="informations">3130×1492 500 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>

liz517: <p>Have you solved this problem now? I have also encountered this problem now</p>