Error using custom face alignment model on IOS

Im trying to build and run an IOS App for facial recognition using a custom face alignment model generated with pytorch.

I followed the tutorial to setup libtorch with cocoapod on my xcode project, and managed to load and use my custom model in C++ code.
Everything worked fine and the app run on an IOS simulator, I’m able detect faces on images.

But running the app on a real Iphone give the following error in xcode when calling for my model forward method:

libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: NNPACK SpatialConvolution_updateOutput failed
The above operation failed in interpreter.
Traceback (most recent call last):
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py(342): conv2d_forward
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py(345): forward
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(525): _slow_forward
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(539): call
/usr/local/lib/python3.5/dist-packages/face_alignment/detection/sfd/net_s3fd.py(97): forward
torch_compilo.py(47): forward
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(525): _slow_forward
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(539): call
/usr/local/lib/python3.5/dist-packages/torch/jit/init.py(997): trace_module
/usr/local/lib/python3.5/dist-packages/torch/jit/init.py(865): trace
torch_compilo.py(53):
Serialized File “code/torch/face_alignment/detection/sfd/net_s3fd.py”, line 206
input29 = torch.relu(input28)
input30 = torch.max_pool2d(input29, [2, 2], [2, 2], [0, 0], [1, 1], False)
input31 = torch._convolution(input30, weight12, _27, [1, 1], [3, 3], [1, 1], False, [0, 0], 1, False, False, True)
**~~~~~~~~~~~~~~~~~~ <— HERE**
input32 = torch.relu(input31)
input33 = torch._convolution(input32, weight13, _29, [1, 1], [0, 0], [1, 1], False, [0, 0], 1, False, False, True)

Any help regarding this issue will be appreciated!

Hey @Lambda. Can you take a look at this one? Can't run nn.Conv1d with libtorch on iOS. I guess the error is the same, just disable NNPACK

Hi @xta0 and thank you for your answer.

I followed your instructions, build from source code for mobile device withe NNPACK disabled

I setup headers and static libraries in my xcode project without using cocoapod, It does build and run on my Iphone but I’m back to this error while loading my model in C++

Did you add all_load to your linker flags?

That probably the part where I’m having an issue.

In other linker flags I need to add the path of each archive file in /install/lib in order to build on IOS instead of only adding the libtorch.a path as is it said in the IOS Pytorch Tutorial.

It looks like that

$(PROJECT_DIR)/pytorch/install/lib/libtorch.a
$(PROJECT_DIR)/pytorch/install/lib/libc10.a
$(PROJECT_DIR)/pytorch/install/lib/libcpuinfo.a
$(PROJECT_DIR)/pytorch/install/lib/libpthreadpool.a $(PROJECT_DIR)/pytorch/install/lib/libpytorch_qnnpack.a $(PROJECT_DIR)/pytorch/install/lib/libtorch_cpu.a
$(PROJECT_DIR)/pytorch/install/lib/libclog.a
$(PROJECT_DIR)/pytorch/install/lib/libeigen_blas.a

Adding -all_load flag bring me to this error

duplicate symbol '_pthreadpool_compute_4d_tiled' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-legacy.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool.cc.o)

duplicate symbol '_pthreadpool_compute_3d_tiled' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-legacy.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool.cc.o)

duplicate symbol '_pthreadpool_compute_2d_tiled' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-legacy.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool.cc.o)

duplicate symbol '_pthreadpool_compute_1d_tiled' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-legacy.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool.cc.o)

duplicate symbol '_pthreadpool_compute_2d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-legacy.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool.cc.o)

duplicate symbol '_pthreadpool_destroy' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_impl.cc.o)

duplicate symbol '_pthreadpool_get_threads_count' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_impl.cc.o)

duplicate symbol '_pthreadpool_create' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_impl.cc.o)

duplicate symbol '_pthreadpool_compute_1d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-legacy.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_impl.cc.o)

duplicate symbol '_pthreadpool_parallelize_2d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_6d_tile_2d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_5d_tile_2d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_4d_tile_2d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_3d_tile_2d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_2d_tile_2d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_1d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_2d_tile_1d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

duplicate symbol '_pthreadpool_parallelize_1d_tile_1d' in:

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libpthreadpool.a(threadpool-pthreads.c.o)

    /Users/root_anatoscope/Documents/cross-compile/opencvxcode/pytorch/install/lib/libtorch_cpu.a(pthreadpool_new_if_impl.c.o)

ld: 18 duplicate symbols for architecture arm64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

-force_load flag bring me to the previous issue