Linking errors with Pytorch Lite 1.9.0 in Android native app

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.

I have an Android app using Pytorch 1.9.0 routines in native C++. My build.gradle is essentially set up just like the example NativeApp (android-demo-app/build.gradle at master · pytorch/android-demo-app · GitHub) and it currently builds fine and works as expected using the following in build.gradle:

dependencies {
    implementation 'org.pytorch:pytorch_android:1.9.0'
    extractForNativeBuild 'org.pytorch:pytorch_android:1.9.0'

Now, I would like to use Pytorch 1.9.0 lite. So I’ve updated the build.gradle section above to:

dependencies {
    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
    extractForNativeBuild 'org.pytorch:pytorch_android_lite:1.9.0'

And I’ve changed my native CMakeLists.txt to point the linking to (instead of previously

Should be pretty straight forward, but I run into the following linking issues. Note that 2 errors point to methods in torch headers (module.h and object.h) and a couple point to the my own file torch_module.cpp that’s making calls to torch::jit::load.

In function `torch::jit::Module::forward(std::__ndk1::vector<c10::IValue, std::__ndk1::allocator<c10::IValue> >)': /include/torch/csrc/jit/api/module.h:114: undefined reference to `torch::jit::Method::operator()(std::__ndk1::vector<c10::IValue, std::__ndk1::allocator<c10::IValue> >, std::__ndk1::unordered_map<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, c10::IValue, std::__ndk1::hash<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::equal_to<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > >, std::__ndk1::allocator<std::__ndk1::pair<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const, c10::IValue> > > const&)'
In function `torch::jit::Object::get_method(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&) const':
/include/torch/csrc/jit/api/object.h:100: undefined reference to `torch::jit::Object::find_method(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&) const'
In function `TorchModule': 
torch_module.cpp:18: undefined reference to `torch::jit::load(std::__ndk1::basic_istream<char, std::__ndk1::char_traits<char> >&, c10::optional<c10::Device>)'
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)

Am I missing something? Thanks heaps for your help.


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.

1 Like

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.

Not quite there yet as I’m running into issues with getting torchvision::nms ops to load properly (even with adding 'org.pytorch:pytorch_android_torchvision:1.9.0' to build.gradle and linking with I get c10::Error: Following ops cannot be found. Check for the fix.{torchvision::nms, } at runtime).

And aside from that my ptl models don’t seem to run properly using NNAPI (c10::Error: The implementation of class __torch__.torch.classes._nnapi.Compilation cannot be found.). I still need to investigate a bit more, will post back with progress or ask for help if I’m still stuck.

1 Like