# How to get libtorch working?

**URL:** <https://forums.unrealengine.com/t/how-to-get-libtorch-working/490741>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 9, 2021, 10:39am UTC](https://forums.unrealengine.com/t/how-to-get-libtorch-working/490741 "2021-11-09T10:39:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheEath](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/theeath/32/88717_2.png) [@TheEath](https://forums.unrealengine.com/u/TheEath)\
**Post date:** [November 9, 2021, 10:39am UTC](https://forums.unrealengine.com/t/how-to-get-libtorch-working/490741/1 "2021-11-09T10:39:23Z")

</div>

Hi,  
I’m trying to get libtorch working with UE4. I have some code written that uses libtorch but now I can’t get it to work within unreal. When i try to open the engine I get either obscure errors (“The game module “ue4\_project” could not be loaded. There may be an operating system error, module may not properly set up, or a plugin which has been included into the build has not been turned on.”) or it crashes on the first time I try to use a function from libtorch.

I feel like I’m doing something wrong with loading the dll from libtorch.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![Amethystium77](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Amethystium77](https://forums.unrealengine.com/u/Amethystium77)\
**Post date:** [December 1, 2021, 11:21am UTC](https://forums.unrealengine.com/t/how-to-get-libtorch-working/490741/2 "2021-12-01T11:21:22Z")

</div>

Hi, I have only partially managed to run UE4 and Libtorch, but I still have a runtime error. In case it is of your help:

In order to replicate the progress:

Visual Studio 2019, Unreal Engine 4 (4.26.2) and Libtorch 1.10 (CPU and release version)

-Create a new Project in PyCharm  
-Install PyTorch: pip3 install torch torchvision torchaudio  
-Write the following code:

```
import torch
import torchvision

# An instance of your model.
model = torchvision.models.resnet18()

# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)

# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)

traced_script_module.save("D:\\resnet18.pt")

```

-Download Libtorch: Stable (1.10), Windows, LibTorch, C++/Java and CPU.  
[https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-1.10.0%2Bcpu.zip](https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-1.10.0%2Bcpu.zip)

-Extract the zip in your PATH.

-Create a project in Unreal Engine 4 named MyProject of type ThirdPerson and of type C++ (the default project is in mode Development Editor).

-Close the editor of UE4

-Open the properties in Project → MyProject Properties. Select the configuration Development\_Editor and VC++ Directories.

-Add the paths in External Include Directories:

(PATH)\libtorch-win-shared-with-deps-1.10.0+cpu\libtorch\include

(PATH)\libtorch-win-shared-with-deps-1.10.0+cpu\libtorch\include\torch\csrc\api\include

-Add the library int Library Directories:

(PATH)\libtorch-win-shared-with-deps-1.10.0+cpu\libtorch\lib

-Add PublicAdditionalLibraries int the MyProject.Build.cs:

```
public class MyProject : ModuleRules
{
	public MyProject(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay" });
		PublicAdditionalLibraries.Add("(PATH)\\libtorch-win-shared-with-deps-1.10.0+cpu\\libtorch\\lib\\torch_cpu.lib");
		PublicAdditionalLibraries.Add("(PATH)\\libtorch-win-shared-with-deps-1.10.0+cpu\\libtorch\\lib\\c10.lib");
		PublicAdditionalLibraries.Add("(PATH)\\libtorch-win-shared-with-deps-1.10.0+cpu\\libtorch\\lib\\torch.lib");
	}
}

```

-Paste the dynamic libraries (dlls) in the Win64 directory of the UE4 project: (PATH)\MyProject\Binaries\Win64  
Librarys: asmjit.dll, c10.dll. fbgemm.dll, libiomp5md.dll, libiompstubs5md.dll, torch.dll, torch\_cpu.dll and uv.dll

-Paste on the top of file MyProjectCharacter.h: `#include <torch/script.h>`. If you Build the project you get a lot of notes, warnings and errors.

-Replace : `#include <torch/script.h>` with:

```
#pragma warning( push )
#pragma warning(disable: 4582)
#pragma warning(disable: 4583)
#pragma warning(disable: 4458)
#pragma warning(disable: 4624)
#pragma warning(disable: 4800)
#pragma warning(disable: 4541)
#include <torch/script.h> // One-stop header.
#pragma warning( pop )

```

-Add the follow code in the constructor of your MyProjectCharacter.cpp

```
torch::device(torch::kCPU);
torch::jit::script::Module module = torch::jit::load("D:\\resnet18.pt");
assert(module != nullptr);
std::cout << "ok\n";

```

-Build the project, everything should run fine.

-Add the following code after the code above:

```
// Create a vector of inputs.
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({1, 3, 224, 224}));

// Execute the model and turn its output into a tensor.
at::Tensor output = module.forward(inputs).toTensor();

std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';

```

-Select Debug → Options and uncheck: Use the new Exception Helper

-Build and Run, then, you get an error in runt time in: at::Tensor output = module.forward(inputs).toTensor();

- If you continue you get a new runtime error:

Unhandled exception at 0x00007FFE8618F199 (ntdll.dll) in UE4Editor.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFE861F77F0).

-If you continue your project your project will run normally. (for example: [Question Answering: BERT(Bidirectional Encoder Representation with Transformers) with LibTorch & UE4 - YouTube](https://www.youtube.com/watch?v=RqELfDc8Rqc))

-I have tried for a couple of weeks to resolve the error at runtime (for example, disabling optimization with #pragma optimize ("", off)), but I have not succeeded. The error in the heap continues.

---

<div class="post-metadata">

**Author:** ![Amethystium77](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Amethystium77](https://forums.unrealengine.com/u/Amethystium77)\
**Post date:** [December 1, 2021, 11:22am UTC](https://forums.unrealengine.com/t/how-to-get-libtorch-working/490741/3 "2021-12-01T11:22:54Z")

</div>

I have managed to partially integrate Libtorch with unreal engine 4, but I have a runtime error.

I have used: Visual Studio 2019, Unreal Engine 4 (4.26.2) and Libtorch 1.10

In order to replicate the progrees:

1. Create a new Project in PyCharm

2. Install PyTorch: pip3 install torch torchvision torchaudio

3. Write the following code:

4. Download Libtorch: Stable (1.10), Windows, LibTorch, C++/Java and CPU.  
[https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-1.10.0%2Bcpu.zip](https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-1.10.0%2Bcpu.zip)

5. Extract the zip in your PATH.

6. Create a project in Unreal Engine 4 named MyProject of type ThirdPerson and of type C++ (the default project is in mode Development Editor).

7. Close the editor of UE4

8. Open the properties in Project → MyProject Properties. Select the configuration Development\_Editor and VC++ Directories.

9. Add the paths in External Include Directories:  
(PATH)\libtorch-win-shared-with-deps-1.10.0+cpu\libtorch\include,  
(PATH)\libtorch-win-shared-with-deps-1.10.0+cpu\libtorch\include\torch\csrc\api\include

10. Add the library int Library Directories:  
(PATH)\libtorch-win-shared-with-deps-1.10.0+cpu\libtorch\lib

11. Add PublicAdditionalLibraries int the MyProject.Build.cs:

12. Paste the dynamic libraries (dlls) in the Win64 directory of the UE4 project: (PATH)\MyProject\Binaries\Win64  
Librarys: asmjit.dll, c10.dll. fbgemm.dll, libiomp5md.dll, libiompstubs5md.dll, torch.dll, torch\_cpu.dll and uv.dll

13. Paste on the top of file MyProjectCharacter.h: `#include <torch/script.h>`. If you Build the project you get a lot of notes, warnings and errors.

14. Replace : `#include <torch/script.h>` with:

//code

```
#pragma warning( push )
#pragma warning(disable: 4582)
#pragma warning(disable: 4583)
#pragma warning(disable: 4458)
#pragma warning(disable: 4624)
#pragma warning(disable: 4800)
#pragma warning(disable: 4541)
#include <torch/script.h> // One-stop header.
#pragma warning( pop )

```

1. Add the follow code in the constructor of your MyProjectCharacter.cpp

2. Build and Run the project, everything should run fine.

3. Add the following code after the code above:

4. Select Debug → Options and uncheck: Use the new Exception Helper

5. Build and Run, then, you get an dialog box with the error in runt time in: `at::Tensor output = module.forward(inputs).toTensor();`

6. If you press Continue you get a new runtime error:  
Unhandled exception at 0x00007FFE8618F199 (ntdll.dll) in UE4Editor.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFE861F77F0).

7. If you press Continue your project will run normally (for example: [Question Answering: BERT(Bidirectional Encoder Representation with Transformers) with LibTorch & UE4 - YouTube](https://www.youtube.com/watch?v=RqELfDc8Rqc)).

I have tried for a couple of weeks to resolve the error at runtime (for example, disabling optimization with #pragma optimize ("", off)), but I have not succeeded. The error in the heap continues.
