I want to run Libtorch(Pytorch C++ Frontend) in OpenFrameworks

HI, All.

I want to run Libtorch (Pytorch C++ Frontend) in OpenFraneworks.

LibTorch Site: https://pytorch.org/tutorials/advanced/cpp_frontend.html

If LibTorch is run in OpenFrameworks, It will be very powerful Deep Solution IDE(deep learning + GUI Production for OSX, Win10, & Linux).

The setup() + update() structure of OF is very suitable for trained model’s fast inference.

Like this:
setup() {
// trained deep model loading
// etc init
update() {
// fast inference repeatedly of inputed.

But I can’t include <torch/torch.h> in XCode.

At CMake method, CMakeLists.txt is like this:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

find_package(Torch REQUIRED)

add_executable(dcgan dcgan.cpp)
target_link_libraries(dcgan “${TORCH_LIBRARIES}”)
set_property(TARGET dcgan PROPERTY CXX_STANDARD 11)

and install libtorch for mac: https://download.pytorch.org/libtorch/cpu/libtorch-macos-latest.zip

How can I include libtorch header in XCode ??

Thanks in advance.

Simple Libtorch Code:

#include <torch/torch.h>
#include <iostream.h>

int main() {
torch::Tensor tensor = torch::eye(3);
std::cout << tensor << std::endl;

Hi @bemoregt, I just spent hours myself to get this simple example working.
My libtorch folder is next to the src folder of the OF project.
In the build settings of the targets (not the project) I had to add 2 paths to the header search path:

  • libtorch/include
  • libtorch/include/torch/csrc/api/include

I had to add libraries to link:

  • libtorch/lib/libtorch.dylib
  • libtorch/lib/libc10.dylib

After that the project builds, but will crash on execution because it can’t find the libraries.
I had to go to the build phases tab and copy the 2 libraries above and libiomp5.dylib during the build process. The destination is set to products directory.

I saw that there are more dylib files in the libtorch/lib folder and maybe the OF app will crash when you actually make something more complex than printing an identity matrix, but I think by then you know how to fix it.

I hope that helped. Let me know if you manage to make it run :slight_smile:


I just noticed that this only works when starting the app from within Xcode.
I’m trying to fix it so it can be started from the app bundle, but libiomp5 gives me a hard time with weird paths when executing…
I’ll post an update when I get it working.

AFAIK, linked dynamic libraries (.dylib) will only work when executing through Xcode. For standalone apps, it needs static (.a) libraries.

That’s good to know :sweat_smile:
Then I guess I have to build libtorch from source,
because the release only contains a dynamic library.
I’ll update after the build finishes.

what error messages were you getting, something rpath related?
I usually add a build phases in xcode, similar to the following code:

cp $OF_PATH/addons/ofxNDI/libs/libndi/lib/osx/libndi.dylib $TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/MacOS/


install_name_tool -change @rpath/libndi.3.dylib @executable_path/libndi.3.dylib $PRODUCT_NAME

Yes, I have something like that.

# Copy libtorch, libc10 and libiomp5
rsync -aved "./libtorch.dylib" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/Frameworks/";
rsync -aved "./libc10.dylib" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/Frameworks/";
rsync -aved "./libiomp5.dylib" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/Frameworks/";
# change path for execution
install_name_tool -change @rpath/libtorch.dylib @executable_path/../Frameworks/libtorch.dylib "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/MacOS/$PRODUCT_NAME";
install_name_tool -change @rpath/libc10.dylib @executable_path/../Frameworks/libc10.dylib "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/MacOS/$PRODUCT_NAME";
install_name_tool -change @rpath/libiomp5.dylib @executable_path/../Frameworks/libiomp5.dylib "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/MacOS/$PRODUCT_NAME";

But I still get an error for libiomp5:
dyld: Library not loaded: @rpath/libiomp5.dylib

maybe the otool helps to see whats going on.

1 Like

The output of otool shows the expected path after running the build phase script from Xcode, but the error remains the same.

    @executable_path/../Frameworks/libfmodex.dylib (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
	/System/Library/Frameworks/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1671.60.107)
	/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 50.1.0)
	/System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox (compatibility version 1.0.0, current version 492.0.0)
	/System/Library/Frameworks/AVFoundation.framework/Versions/A/AVFoundation (compatibility version 1.0.0, current version 2.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 23.0.0)
	/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1575.17.0)
	/System/Library/Frameworks/CoreMedia.framework/Versions/A/CoreMedia (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 946.0.0)
	/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
	/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
	/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore (compatibility version 1.2.0, current version 1.11.0)
	/System/Library/Frameworks/QTKit.framework/Versions/A/QTKit (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 58286.255.3)
	/System/Library/Frameworks/LDAP.framework/Versions/A/LDAP (compatibility version 1.0.0, current version 2.4.0)
	@executable_path/../Frameworks/libtorch.dylib (compatibility version 0.0.0, current version 0.0.0)
	@executable_path/../Frameworks/libc10.dylib (compatibility version 0.0.0, current version 0.0.0)
	@executable_path/../Frameworks/libiomp5.dylib (compatibility version 5.0.0, current version 5.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.250.1)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1265.9.0)
	/System/Library/Frameworks/CoreText.framework/Versions/A/CoreText (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1575.17.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)