Incompatible std::string

lavoos: <p>I’m using MSVS 2022, UE 5.3.2, and libtorch - debug-2.2.2+cu121.<br>
I created a simple UE FPS project, added a code into a GameMode class, that loads libtorch.<br>
Added a code like:</p>
<blockquote>
<p>torch::jit::script::Module module_pt;<br>
module_pt = torch::jit::load(“C:/src/model_weights.pt”);</p>
</blockquote>
<p>This code compiled, linked, and I started the UEditor from MSVS. The UEditor has crashed on torch::jit::load with a runtime exception, going deep into the C runtime</p>
<div class="md-table">
<table>
<thead>
<tr>
<th></th>
<th>ucrtbased.dll!00007ffb27daa053()</th>
<th>Unknown</th>
<th>No symbols loaded.</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>ucrtbased.dll!00007ffb27daa627()</td>
<td>Unknown</td>
<td>No symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>msvcp140d.dll!00007ffb27f46c12()</td>
<td>Unknown</td>
<td>No symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>msvcp140d.dll!00007ffb27f469e4()</td>
<td>Unknown</td>
<td>No symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>msvcp140d.dll!00007ffb27f46b75()</td>
<td>Unknown</td>
<td>No symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>torch_cpu.dll!std::basic_filebuf&lt;char,std::char_traits&gt;::open(const char * _Filename, int _Mode, int _Prot) Line 277</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>torch_cpu.dll!std::basic_ifstream&lt;char,std::char_traits&gt;::basic_ifstream&lt;char,std::char_traits&gt;(const char * _Filename, int _Mode, int _Prot) Line 808</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>torch_cpu.dll!std::basic_ifstream&lt;char,std::char_traits&gt;::basic_ifstream&lt;char,std::char_traits&gt;(const std::string &amp; _Str, int _Mode, int _Prot) Line 815</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>torch_cpu.dll!torch::jit::getFileFormat(const std::string &amp; filename) Line 113</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>torch_cpu.dll!torch::jit::import_ir_module(std::shared_ptr<a>torch::jit::CompilationUnit</a> cu, const std::string &amp; filename, std::optional<a>c10::Device</a> device, std::unordered_map&lt;std::string,std::string,std::hash<a>std::string</a>,std::equal_to<a>std::string</a>,std::allocator&lt;std::pair&lt;std::string const ,std::string&gt;&gt;&gt; &amp; extra_files, bool load_debug_files, bool restore_shapes) Line 429</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>torch_cpu.dll!torch::jit::import_ir_module(std::shared_ptr<a>torch::jit::CompilationUnit</a> cu, const std::string &amp; filename, std::optional<a>c10::Device</a> device, bool load_debug_files) Line 416</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td>&gt;</td>
<td>torch_cpu.dll!torch::jit::load(const std::string &amp; filename, std::optional<a>c10::Device</a> device, bool load_debug_files) Line 498</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>UnrealEditor-RASample.dll!ARASampleGameMode::InitPytorch() Line 84</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
<tr>
<td></td>
<td>UnrealEditor-RASample.dll!ARASampleGameMode::ARASampleGameMode() Line 48</td>
<td>C++</td>
<td>Symbols loaded.</td>
</tr>
</tbody>
</table>
</div><p>After debugging I figured out that the torch::jit::load is not receiving the string value, that I’ve passed, but receives NULL.<br>
My C string value is converted to std::string on my side, but inside torch::jit::load it crosses dll boundary, and gets converted to NULL, probably because of different versions of std::string being used on my machine and machine used to build the libtorch distribution.</p>
<p>What could be done in this case?<br>
Build libtorch locally from sources?</p>

ptrblck: <p>I haven’t seen this issue before, but since you are concerned about the <code>std::string</code> definition, you might want to check if your installed <code>libtorch</code> version is using the cxx11 ABI or not and swap it for testing purposes.</p>

lavoos: <p>Thank you for reply.<br>
How can I do the check that you suggested?<br>
BTW, using dumpbin I was able to get linker version used to buid the libtorch, which is 14.29</p>

ptrblck: <p>Oh sorry, I see there is no cxx11 ABI - specific build for Windows and I’m also unsure how it’s handled in these builds, as I’m using linux mostly.</p>

lavoos: <p>I’ve followed steps from this article <a href="https://github.com/pytorch/pytorch/issues/69425" class="inline-onebox" rel="noopener nofollow ugc">Integrate Libtorch into Unreal Engine 4: _ivalue_INTERNAL ASSERT FAILED · Issue #69425 · pytorch/pytorch · GitHub</a>. Created dll project in MSVS, moved call of the module.forward() into the dll. That project produces .lib and .dll that I use in my UE5 project as described.</p>
<p>Now, call to module.forward() produces exception:<br>
UnrealEditor.exe: Microsoft C++ exception: c10::Error at memory location 0x000000F7AE178AB0<br>
This looks like the same issue, as in that article. Not fixed for years…</p>
<div class="md-table">
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Value</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="https://discuss.pytorch.org/images/emoji/apple/arrow_forward.png?v=12" title=":arrow_forward:" class="emoji only-emoji" alt=":arrow_forward:" loading="lazy" width="20" height="20"></td>
<td>what_without_backtrace_</td>
<td>"<em>ivalue</em> INTERNAL ASSERT FAILED at "C:\\actions-runner\\_work\\pytorch\\pytorch\\builder\\windows\\pytorch\\torch/csrc/jit/api/object.h":38, please report a bug to PyTorch. "</td>
<td>std::string</td>
</tr>
</tbody>
</table>
</div>

lavoos: <p>Ok, that article has an error, found it, fixed my issue with c10 error.<br>
The libtorch returns values for the input to a nn from simple .pt file prepared from py code. Yu-hOOO</p>