Error using custom face alignment model on IOS

username: Lambda
cooked: <p>Im trying to build and run an IOS App for facial recognition using a custom face alignment model generated with pytorch.</p>
<p>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.<br>
Everything worked fine and the app run on an IOS simulator, I’m able detect faces on images.</p>
<p>But running the app on a real Iphone give the following error in xcode when calling for my model forward method:</p>
<blockquote>
<p><strong>libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: NNPACK SpatialConvolution_updateOutput failed</strong><br>
<strong>The above operation failed in interpreter.</strong><br>
<strong>Traceback (most recent call last):</strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py(342): conv2d_forward</strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/conv.py(345): forward</strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(525): _slow_forward</strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(539): <strong>call</strong></strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/face_alignment/detection/sfd/net_s3fd.py(97): forward</strong><br>
<strong>torch_compilo.py(47): forward</strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(525): _slow_forward</strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py(539): <strong>call</strong></strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/jit/<strong>init</strong>.py(997): trace_module</strong><br>
<strong>/usr/local/lib/python3.5/dist-packages/torch/jit/<strong>init</strong>.py(865): trace</strong><br>
<strong>torch_compilo.py(53): </strong><br>
<strong>Serialized   File “code/<strong>torch</strong>/face_alignment/detection/sfd/net_s3fd.py”, line 206</strong><br>
<strong>input29 = torch.relu(input28)</strong><br>
<strong>input30 = torch.max_pool2d(input29, [2, 2], [2, 2], [0, 0], [1, 1], False)</strong><br>
<strong>input31 = torch._convolution(input30, weight12, _27, [1, 1], [3, 3], [1, 1], False, [0, 0], 1, False, False, True)</strong><br>
<strong>~~~~~~~~~~~~~~~~~~ &lt;— HERE</strong><br>
<strong>input32 = torch.relu(input31)</strong><br>
<strong>input33 = torch._convolution(input32, weight13, _29, [1, 1], [0, 0], [1, 1], False, [0, 0], 1, False, False, True)</strong></p>
</blockquote>
<p>Any help regarding this issue will be appreciated!</p>

username: xta0
cooked: <p>Hey <a class="mention" href="/u/lambda">@Lambda</a>. Can you take a look at this one?  <a href="https://discuss.pytorch.org/t/cant-run-nn-conv1d-with-libtorch-on-ios/71719" class="inline-onebox">Can't run nn.Conv1d with libtorch on iOS</a>. I guess the error is the same, just disable NNPACK</p>

username: Lambda
cooked: <p>Hi <a class="mention" href="/u/xta0">@xta0</a> and thank you for your answer.</p>
<p>I followed your instructions, build from source code for mobile device withe NNPACK disabled</p>
<p>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 <a href="https://discuss.pytorch.org/t/pytorch-is-not-linked-with-support-for-cpu-devices/64441">this error</a> while loading my model in C++</p>

username: xta0
cooked: <p>Did you add <code>all_load</code> to your linker flags?</p>

username: Lambda
cooked: <p>That probably the part where I’m having an issue.</p>
<p>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.</p>
<p>It looks like that</p>
<blockquote>
<p>$(PROJECT_DIR)/pytorch/install/lib/libtorch.a<br>
$(PROJECT_DIR)/pytorch/install/lib/libc10.a<br>
$(PROJECT_DIR)/pytorch/install/lib/libcpuinfo.a<br>
$(PROJECT_DIR)/pytorch/install/lib/libpthreadpool.a $(PROJECT_DIR)/pytorch/install/lib/libpytorch_qnnpack.a $(PROJECT_DIR)/pytorch/install/lib/libtorch_cpu.a<br>
$(PROJECT_DIR)/pytorch/install/lib/libclog.a<br>
$(PROJECT_DIR)/pytorch/install/lib/libeigen_blas.a</p>
</blockquote>
<p>Adding -all_load flag bring me to this error</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>-force_load flag bring me to the previous issue</p>