Incompatible std::string

I’m using MSVS 2022, UE 5.3.2, and libtorch - debug-2.2.2+cu121.
I created a simple UE FPS project, added a code into a GameMode class, that loads libtorch.
Added a code like:

torch::jit::script::Module module_pt;
module_pt = torch::jit::load(“C:/src/model_weights.pt”);

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

ucrtbased.dll!00007ffb27daa053() Unknown No symbols loaded.
ucrtbased.dll!00007ffb27daa627() Unknown No symbols loaded.
msvcp140d.dll!00007ffb27f46c12() Unknown No symbols loaded.
msvcp140d.dll!00007ffb27f469e4() Unknown No symbols loaded.
msvcp140d.dll!00007ffb27f46b75() Unknown No symbols loaded.
torch_cpu.dll!std::basic_filebuf<char,std::char_traits>::open(const char * _Filename, int _Mode, int _Prot) Line 277 C++ Symbols loaded.
torch_cpu.dll!std::basic_ifstream<char,std::char_traits>::basic_ifstream<char,std::char_traits>(const char * _Filename, int _Mode, int _Prot) Line 808 C++ Symbols loaded.
torch_cpu.dll!std::basic_ifstream<char,std::char_traits>::basic_ifstream<char,std::char_traits>(const std::string & _Str, int _Mode, int _Prot) Line 815 C++ Symbols loaded.
torch_cpu.dll!torch::jit::getFileFormat(const std::string & filename) Line 113 C++ Symbols loaded.
torch_cpu.dll!torch::jit::import_ir_module(std::shared_ptrtorch::jit::CompilationUnit cu, const std::string & filename, std::optionalc10::Device device, std::unordered_map<std::string,std::string,std::hashstd::string,std::equal_tostd::string,std::allocator<std::pair<std::string const ,std::string>>> & extra_files, bool load_debug_files, bool restore_shapes) Line 429 C++ Symbols loaded.
torch_cpu.dll!torch::jit::import_ir_module(std::shared_ptrtorch::jit::CompilationUnit cu, const std::string & filename, std::optionalc10::Device device, bool load_debug_files) Line 416 C++ Symbols loaded.
> torch_cpu.dll!torch::jit::load(const std::string & filename, std::optionalc10::Device device, bool load_debug_files) Line 498 C++ Symbols loaded.
UnrealEditor-RASample.dll!ARASampleGameMode::InitPytorch() Line 84 C++ Symbols loaded.
UnrealEditor-RASample.dll!ARASampleGameMode::ARASampleGameMode() Line 48 C++ Symbols loaded.

After debugging I figured out that the torch::jit::load is not receiving the string value, that I’ve passed, but receives NULL.
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.

What could be done in this case?
Build libtorch locally from sources?

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

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

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.

I’ve followed steps from this article Integrate Libtorch into Unreal Engine 4: _ivalue_INTERNAL ASSERT FAILED · Issue #69425 · pytorch/pytorch · GitHub. 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.

Now, call to module.forward() produces exception:
UnrealEditor.exe: Microsoft C++ exception: c10::Error at memory location 0x000000F7AE178AB0
This looks like the same issue, as in that article. Not fixed for years…

Name Value Type
:arrow_forward: what_without_backtrace_ "ivalue 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. " std::string

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

1 Like