Linking errors with Pytorch Lite 1.9.0 in Android native app

Aurelien: <p>Hi everyone, hoping to get some help on this, I’ve tried a few things but I can’t work out what is going on.</p>
<p>I have an Android app using Pytorch 1.9.0 routines in native C++. My <code>build.gradle</code> is essentially set up just like the example NativeApp (<a href="https://github.com/pytorch/android-demo-app/blob/master/NativeApp/app/build.gradle" class="inline-onebox" rel="noopener nofollow ugc">android-demo-app/build.gradle at master · pytorch/android-demo-app · GitHub</a>) and it currently builds fine and works as expected using the following in <code>build.gradle</code>:</p>
<pre><code class="lang-auto">dependencies {
    implementation 'org.pytorch:pytorch_android:1.9.0'
    extractForNativeBuild 'org.pytorch:pytorch_android:1.9.0'
}
</code></pre>
<p>Now, I would like to use Pytorch 1.9.0 <code>lite</code>. So I’ve updated the <code>build.gradle</code> section above to:</p>
<pre><code class="lang-auto">dependencies {
    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    extractForNativeBuild 'org.pytorch:pytorch_android_lite:1.9.0'
}
</code></pre>
<p>And I’ve changed my native CMakeLists.txt to point the linking to <code>libpytorch_jni_lite.so</code> (instead of previously <code>libpytorch_jni.so</code>).</p>
<p>Should be pretty straight forward, but I run into the following linking issues. Note that 2 errors point to methods in torch headers (<code>module.h</code> and <code>object.h</code>) and a couple point to the my own file <code>torch_module.cpp</code> that’s making calls to <code>torch::jit::load</code>.</p>
<pre><code class="lang-auto">In function `torch::jit::Module::forward(std::__ndk1::vector&lt;c10::IValue, std::__ndk1::allocator&lt;c10::IValue&gt; &gt;)': /include/torch/csrc/jit/api/module.h:114: undefined reference to `torch::jit::Method::operator()(std::__ndk1::vector&lt;c10::IValue, std::__ndk1::allocator&lt;c10::IValue&gt; &gt;, std::__ndk1::unordered_map&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt;, c10::IValue, std::__ndk1::hash&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt;, std::__ndk1::equal_to&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; &gt;, std::__ndk1::allocator&lt;std::__ndk1::pair&lt;std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const, c10::IValue&gt; &gt; &gt; const&amp;)'
In function `torch::jit::Object::get_method(std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;) const':
/include/torch/csrc/jit/api/object.h:100: undefined reference to `torch::jit::Object::find_method(std::__ndk1::basic_string&lt;char, std::__ndk1::char_traits&lt;char&gt;, std::__ndk1::allocator&lt;char&gt; &gt; const&amp;) const'
In function `TorchModule': 
torch_module.cpp:18: undefined reference to `torch::jit::load(std::__ndk1::basic_istream&lt;char, std::__ndk1::char_traits&lt;char&gt; &gt;&amp;, c10::optional&lt;c10::Device&gt;)'
torch_module.cpp:27: undefined reference to `torch::jit::Module::to(c10::Device, bool)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
</code></pre>
<p>Am I missing something? Thanks heaps for your help.</p>
<p>Aurelien</p>

Linbin: <p>You need to use different C++ APIs for pytorch lite. For example, use torch::jit::mobile::Module instead of torch::jit::Module. Use torch::jit::_load_for_mobile to load a lite model.</p>

Aurelien: <p>Amazing, thanks Linbin. I had totally missed that! I made the changes and I am now able to build the app and linking with PT Lite.</p>
<p>Not quite there yet as I’m running into issues with getting <code>torchvision::nms</code> ops to load properly (even with adding <code>'org.pytorch:pytorch_android_torchvision:1.9.0'</code> to <code>build.gradle</code> and linking with <code>libpytorch_vision_jni.so</code>. I get <code>c10::Error: Following ops cannot be found. Check fburl.com/missing_ops for the fix.{torchvision::nms, }</code> at runtime).</p>
<p>And aside from that my <code>ptl</code> models don’t seem to run properly using NNAPI (<code>c10::Error: The implementation of class __torch__.torch.classes._nnapi.Compilation cannot be found.</code>). I still need to investigate a bit more, will post back with progress or ask for help if I’m still stuck.</p>