Trouble Building with C++ TorchScript

ChrisH: <p>Hi there,</p>
<p>I have a PyTorch model that I’ve trained in a python project that I’d like to load into a C++ project. From what I understand, my best option is to trace my model using TorchScript and then use the C++ library to load the model. I’m having trouble including TorchScript in my Visual Studio 2017 project, though.</p>
<p>After adding the path for the “\Python310\Lib\site-packages\torch\include” folder and “\torch\lib” to the project properties, the project won’t compile. I get a million notes and warnings, and one single error:</p>
<p><code>...\python310\lib\site-packages\torch\include\c10\util\safe_numerics.h(26): error C3861: '_addcarry_u64': identifier not found</code></p>
<p>That file contains this:</p>
<pre><code class="lang-auto">#if defined(_M_IX86) || defined(_M_X64)
  auto carry = _addcarry_u64(0, a, b, &amp;tmp);
#else
  tmp = a + b;
  unsigned long long vector = (a &amp; b) ^ ((a ^ b) &amp; ~tmp);
  auto carry = vector &gt;&gt; 63;
#endif
  *out = tmp;
  return carry;
</code></pre>
<p>After some googling, it seems the recommended work around is to simply comment out the part of the file that references <code>_addcarry_u64</code> so that it always falls down into the <code>#else</code> section. If I do that and try building again, I now get 96 unresolved externals. I don’t want to list them all but here’s the first few:</p>
<pre><code class="lang-auto">error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall c10::ValueError::ValueError(struct c10::SourceLocation,class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;)" (__imp_??0ValueError@c10@@QAE@USourceLocation@1@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: static void __cdecl c10::ivalue::Future::ensureIsSubsetOfDevices(class std::vector&lt;struct c10::Device,class std::allocator&lt;struct c10::Device&gt; &gt; const &amp;,class std::vector&lt;struct c10::Device,class std::allocator&lt;struct c10::Device&gt; &gt; const &amp;)" (?ensureIsSubsetOfDevices@Future@ivalue@c10@@CAXABV?$vector@UDevice@c10@@V?$allocator@UDevice@c10@@@std@@@std@@0@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) char const * __cdecl c10::detail::torchCheckMsgImpl(char const *)" (__imp_?torchCheckMsgImpl@detail@c10@@YAPBDPBD@Z) referenced in function "public: void __thiscall c10::ivalue::Object::unsafeRemoveSlot(unsigned int)" (?unsafeRemoveSlot@Object@ivalue@c10@@QAEXI@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) char const * __cdecl c10::detail::torchCheckMsgImpl(char const *,char const *)" (__imp_?torchCheckMsgImpl@detail@c10@@YAPBDPBD0@Z) referenced in function __catch$?markCompleted@Future@ivalue@c10@@QAEXUIValue@3@V?$optional@V?$vector@V?$weak_intrusive_ptr@UStorageImpl@c10@@U?$intrusive_target_default_null_type@UStorageImpl@c10@@@detail@2@@c10@@V?$allocator@V?$weak_intrusive_ptr@UStorageImpl@c10@@U?$intrusive_target_default_null_type@UStorageImpl@c10@@@detail@2@@c10@@@std@@@std@@@3@@Z$0
</code></pre>
<p>Would anyone have any clues as to why this is happening and what I can do? I’m on windows 10 using VS 2017 and the project I’m working from needs to build in 32 bit, would I need to download/build a 32 bit version of PyTorch? I see nothing suggesting that if I try looking that up.</p>
<p>Thanks in advance for any help.</p>

ptrblck: <p>I’m not familiar enough with Windows builds to know why the original error is raised, but</p>
<aside class="quote no-group" data-username="ChrisH" data-post="1" data-topic="182443">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/6de8d8/48.png" class="avatar"> ChrisH:</div>
<blockquote>
<p>I’m working from needs to build in 32 bit, would I need to download/build a 32 bit version of PyTorch?</p>
</blockquote>
</aside>
<p>PyTorch does not support 32bit builds if I’m not mistaken.</p>

ChrisH: <p>Ah okay, I guess I’ll have to find some other method then.</p>