Building from source, getting the procedure entry point ?mutable_grad@AutogradMeta could not be located in the dynamic link library error

Shisho_Sama: <p>I’m trying to build libtorch in debug configuration with <code>/MTd</code>. the release works just fine but the debug has been giving me headaches ever since. In my latest attempt I managed to get it to build just fine, and get rid of the damning 0xc0000142 error that I was getting.<br>
But now for some weird reasons that I can not fathom I get this linker like error at runtime!:</p>
<blockquote>
<p>The procedure entry point ?mutable_grad@AutogradMeta@autograd@torch@<span class="mention">@UEAAAEAVTensor</span>@at@<span class="mention">@XY</span> could not be located in the dynamic link library</p>
</blockquote>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d52d13a781f36e4a20fb2d645572676f9f862435.jpeg" alt="libtorch_debug" data-base62-sha1="upQhdFCiaFZUXX4nrXcD4i8WjRz" width="562" height="262"><br>
What am I missing here? Whats wrong?<br>
I could really use and greatly appreciate some help in this<br>
Thank you all in advance</p>

glaringlee: <p><a class="mention" href="/u/peterjc123">@peterjc123</a> Can you help?</p>

peterjc123: <p>Have you copied the DLLs to the directory of your target executable?</p>

Shisho_Sama: <p>Hi, you are right, <code>torch_cpu.dll</code> was missing and hence I was getting this error.<br>
When I rebuilt everything I went back to square one! <img src="https://discuss.pytorch.org/images/emoji/apple/pensive.png?v=12" title=":pensive:" class="emoji" alt=":pensive:" loading="lazy" width="20" height="20"><br>
That is I’m back at solving what is causing this :<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c5d2a1d8ffbc30a14359de9d84c5e27dff73610f.png" alt="image" data-base62-sha1="se1l1AuJsMCvDHnJa4ROMjnD3Sn" width="560" height="380"><br>
which pressing on the retry ultimately shows the infamous 0xc0000142 (DLL initialization failed):<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/c/c/cc5c9b467d798e467a05a65eeb9971259f006af5.png" alt="image" data-base62-sha1="t9ROny2NoraC8ZIn4Pyv47nYka9" width="485" height="241"><br>
Do you by any chance know how I can dig deeper and find the culprit?<br>
To give you an idea of how I went about making <code>/MD</code> into <code>MT</code>, first of all, I simply ran <code>cmake-gui</code><br>
and then under CMakegroup changed all MD flags to <code>/MT</code> like this :<br>
<img src="https://discourse.cmake.org/uploads/default/original/2X/a/a09f8a72aaea4d3d1e0a3ff6f24a1e1114d6096f.png" alt="" role="presentation" width="690" height="387"><br>
and then I went ahead and added this ugly hack to nearly all CMake files that without this hack would issue a build error (because of md vs mt mismatch) :</p>
<pre data-code-wrap="cmake"><code class="lang-cmake"># -- MT 
set(CompilerFlags
        CMAKE_CXX_FLAGS
        CMAKE_CXX_FLAGS_DEBUG
        CMAKE_CXX_FLAGS_RELEASE
        CMAKE_C_FLAGS
        CMAKE_C_FLAGS_DEBUG
        CMAKE_C_FLAGS_RELEASE
        )
foreach(CompilerFlag ${CompilerFlags})
  string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
</code></pre>
<p>This seemingly fixed the building process, but somewhere in there, there must be a mismatch happening, that causes c10.dll to fail. This doesn’t happen in release mode, only the debug has this issue and I’m clueless as to how to go about fixing it.<br>
Previously I had generated a VS project with everything in their defaults and then changed the properties through VisualStudio (i.e. changed all MDs to MTs from project properties) and managed to build just fine. but then again the debug would behave the same way at runtime. This was why I tried changing the cmake and see if it makes a change and obviously, it does not.</p>
<p>What could have caused this? a mismatch between the C++ versions? (they are built as C++14 but my own project uses C++17. if this was the case, why wouldn’t release have any issues then? if it’s not this, then what else could be wrong? where should I dig in next?</p>
<h2><a name="update-1-1" class="anchor" href="#update-1-1"></a>Update 1:</h2>
<p>Looking at the DLLs built with MD and MT respectively, I can see couple of differences.<br>
The import table and export tables are different:<br>
C10.dll (MDd) / C10.dll (MTd)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/4/24194026500410b7bca09ed5d42f7d1200a75cd1.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/24194026500410b7bca09ed5d42f7d1200a75cd1" title="c10.dll_imports"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24194026500410b7bca09ed5d42f7d1200a75cd1_2_690x358.jpeg" alt="c10.dll_imports" data-base62-sha1="59lhRjL3kQxAZy2Hrj5fCDnblKx" width="690" height="358" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24194026500410b7bca09ed5d42f7d1200a75cd1_2_690x358.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24194026500410b7bca09ed5d42f7d1200a75cd1_2_1035x537.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/24194026500410b7bca09ed5d42f7d1200a75cd1_2_1380x716.jpeg 2x" data-dominant-color="F8F8F8"><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">c10.dll_imports</span><span class="informations">1600×832 102 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><strong>Exports(684 vs 676)</strong>:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/a/8ab1dec000cbf03a0af4ab36416b104bc8b9bcc5.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/8ab1dec000cbf03a0af4ab36416b104bc8b9bcc5" title="c10.dll_exports"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/a/8ab1dec000cbf03a0af4ab36416b104bc8b9bcc5_2_690x361.jpeg" alt="c10.dll_exports" data-base62-sha1="jMWYvk3pjoXTFo5ipxfWffYqGdD" width="690" height="361" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/a/8ab1dec000cbf03a0af4ab36416b104bc8b9bcc5_2_690x361.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/8/a/8ab1dec000cbf03a0af4ab36416b104bc8b9bcc5_2_1035x541.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/a/8ab1dec000cbf03a0af4ab36416b104bc8b9bcc5_2_1380x722.jpeg 2x" data-dominant-color="E0E0E0"><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">c10.dll_exports</span><span class="informations">1596×836 622 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><strong>Headers:</strong><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/5/e5b305d3cb0f9a6c60e04b66e699a6e0116aa0f3.png" data-download-href="https://discuss.pytorch.org/uploads/default/e5b305d3cb0f9a6c60e04b66e699a6e0116aa0f3" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5b305d3cb0f9a6c60e04b66e699a6e0116aa0f3_2_690x342.png" alt="image" data-base62-sha1="wM0TvhdwBqUEmEgtUO3JMxfS799" width="690" height="342" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5b305d3cb0f9a6c60e04b66e699a6e0116aa0f3_2_690x342.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5b305d3cb0f9a6c60e04b66e699a6e0116aa0f3_2_1035x513.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e5b305d3cb0f9a6c60e04b66e699a6e0116aa0f3_2_1380x684.png 2x" data-dominant-color="F9F9F9"><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">1595×792 29 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><br>
In case its of any use, here are the full logs for each of them :<br>
C10.dll.txt info (MDd) : <a href="https://paste.ee/p/VYAXS" class="inline-onebox" rel="noopener nofollow ugc">Paste.ee - C10(MDd).dll info.txt</a><br>
C10.dll.txt info (MTd) : <a href="https://paste.ee/p/vq3wV" class="inline-onebox" rel="noopener nofollow ugc">Paste.ee - C10(MTd).dll info.txt</a></p>
<p>For comparison, in release configureation for example, despite the differences between the <code>MD</code> and <code>MT</code>builds, the exports are the same :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/c/1c1db7c905d35a6262cc822c43ead25e9022777a.png" data-download-href="https://discuss.pytorch.org/uploads/default/1c1db7c905d35a6262cc822c43ead25e9022777a" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/c/1c1db7c905d35a6262cc822c43ead25e9022777a.png" alt="image" data-base62-sha1="40J2rQMQAeVe1JaZk6z76XJ5ofM" width="690" height="344" data-dominant-color="E0E0E1"><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">1600×798 82.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>
<h2><a name="update-2-2" class="anchor" href="#update-2-2"></a>Update 2:</h2>
<p>I rebuilt the c10.dll separately and the MT version’s export now matches the MD’s but the exception still happens! <img src="https://discuss.pytorch.org/images/emoji/apple/pensive.png?v=12" title=":pensive:" class="emoji" alt=":pensive:" loading="lazy" width="20" height="20"><br>
What could be the cause here?</p>
<h2><a name="update-3-3" class="anchor" href="#update-3-3"></a>Update 3:</h2>
<p>I guess I might have found out the cause of all this. its because the shared_lib is on! it doesn’t issue any warnings/ apparent errors in release mode, but it doesn’t mean it’s fine either. the debug obviously fails because of this so release ought to have some issues as well.<br>
building statically has the issue that I get maximum size related error, so I need to find a way to exclude some libs I guess, not sure if that’s possible but will try my best and see how this goes.<br>
For the refs <a href="https://stackoverflow.com/a/3007915/2736559" rel="noopener nofollow ugc">this</a>, and <a href="https://github.com/rttrorg/rttr/issues/209" rel="noopener nofollow ugc">this</a> and <a href="https://stackoverflow.com/questions/35310117/debug-assertion-failed-expression-acrt-first-block-header" rel="noopener nofollow ugc">this</a> may be useful to others as well.</p>
<p>I’ll be working on this and see what I can do, but I could really use a helping hand in this. any kind of tips is greatly appreciated.<br>
Thank you very much in advance</p>

tik: <p>This could be due to non-compatiable package. I had kind of the same issue when importing torchtext. In fact, for me, it was like this</p>
<pre><code class="lang-auto">The procedure entry point ??0Error@c10@@QEAA@V?$basic_string@DU?$char_traits@D@ std@@V?$allocator@D@2@@std@@0PEBX@Z could not be located in the dynamic link library \.conda\envs\time\lib\site-packages\torchtextlib\libtorchtext.pyd.
</code></pre>
<p>I learned that this could be due to different versions of the underlying C++ libraries they depend on from here:<br>
<a href="https://ksml4.com/2024/10/18/fixed-cant-import-install-torchtext/" rel="noopener nofollow ugc">Fixed: TorchText install issue: The procedure entry point?? could not be located in the dynamic link library</a><br>
So, you may want to check the compatibility of the packages that you used.</p>