UE5 and libtorch

I wanted to include libotorch inside unreal to run some models.
I’ve tried both by linking directly to libtorch, linking to my wrapper library that uses libtorch or even a plugin that delays the loading of the wrapped lib.
Eitherway the editor hangs.

By debugging I’ve noticed this:
Inside WindowsCallstackTrace.cpp
void FBacktracer::AddModule(UPTRINT ModuleBase, const TCHAR* Name);
This thing iterates over every imported module opcode. And when it does it over torch_cpu.dll the NumFunctions counter is too big to end. I smell a bug there!

Did someone get it running?

1 Like

Thank you for rasing up this issue.
I have UE 5.3.2 and trying to integrate libtorch 2.1 (also libtorch-win-shared-with-deps-debug-latest) and have the same issue.
I have recorded my debugging into a gif. There is a loop inside of the WindowsCallbackTrace.cpp, lines 332-342. An opcode with value 11 is not handled by the switch.
Will try to fix this or make a workaround.
devenv_ZzBKwpQqSM

The callstack is

UnrealEditor-Core.dll!FBacktracer::AddModule(unsigned __int64 ModuleBase, const wchar_t * Name) Line 332 C++
UnrealEditor-Core.dll!FModuleTrace::OnDllLoaded(const _UNICODE_STRING & Name, unsigned __int64 Base) Line 215 C++
[External Code]
> [Inline Frame] UnrealEditor-Core.dll!FWindowsPlatformProcess::LoadLibraryWithSearchPaths::__l13::() Line 2184 C++
UnrealEditor-Core.dll!FWindowsPlatformProcess::LoadLibraryWithSearchPaths(const FString & FileName, const TArray<FString,TSizedDefaultAllocator<32>> & SearchPaths) Line 2178 C++
UnrealEditor-Core.dll!FWindowsPlatformProcess::GetDllHandle(const wchar_t * FileName) Line 163 C++
UnrealEditor-Core.dll!FModuleManager::LoadModuleWithFailureReason(const FName InModuleName, EModuleLoadResult & OutFailureReason, ELoadModuleFlags InLoadModuleFlags) Line 607 C++
UnrealEditor-Projects.dll!FModuleDescriptor::LoadModulesForPhase(ELoadingPhase::Type LoadingPhase, const TArray<FModuleDescriptor,TSizedDefaultAllocator<32>> & Modules, TMap<FName,enum EModuleLoadResult,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<FName,enum EModuleLoadResult,0>> & ModuleLoadErrors) Line 696 C++
UnrealEditor-Projects.dll!FProjectManager::LoadModulesForProject(const ELoadingPhase::Type LoadingPhase) Line 62 C++
UnrealEditor.exe!FEngineLoop::LoadStartupModules() Line 4671 C++
UnrealEditor.exe!FEngineLoop::PreInitPostStartupScreen(const wchar_t * CmdLine) Line 3942 C++
[Inline Frame] UnrealEditor.exe!FEngineLoop::PreInit(const wchar_t *) Line 4414 C++
[Inline Frame] UnrealEditor.exe!EnginePreInit(const wchar_t *) Line 41 C++
UnrealEditor.exe!GuardedMain(const wchar_t * CmdLine) Line 140 C++
UnrealEditor.exe!LaunchWindowsStartup(HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, char * __formal, int nCmdShow, const wchar_t * CmdLine) Line 247 C++
UnrealEditor.exe!WinMain(HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, char * pCmdLine, int nCmdShow) Line 298 C++
[External Code]

falls infinit loop if switch to default.
image