Compilation error in 'jit/ir/ir.h'

Hi,
I’m trying to include PyTorch library in my CPP project.
I’m using ndk21 and C++14, torch version 1.5.0 for CPU.

I get the following compilation errors, all from csrc/jit/ir/ir.h and regarding c10 namespace:

C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:67:17: error: no
namespace named ‘prim’ in namespace ‘c10’; did you mean simply ‘prim’?
using namespace ::c10::prim;
^~~~~~~~~~~
prim
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:66:11: note: namespace
‘prim’ defined here
namespace prim {
^
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:70:17: error: no
namespace named ‘attr’ in namespace ‘c10’; did you mean simply ‘attr’?
using namespace ::c10::attr;
^~~~~~~~~~~
attr
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:69:11: note: namespace
‘attr’ defined here
namespace attr {
^
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:73:17: error: no
namespace named ‘aten’ in namespace ‘c10’; did you mean simply ‘aten’?
using namespace ::c10::aten;
^~~~~~~~~~~
aten
C:\REPOSCPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:72:11: note: namespace
‘aten’ defined here
namespace aten {
^
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:1309:34: error: no
member named ‘prim’ in namespace ‘c10’; did you mean simply ‘prim’?
static constexpr Symbol Kind = ::c10::prim::profile;
^~~~~~~~~~~
prim
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:66:11: note: ‘prim’
declared here
namespace prim {
^
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:1309:47: error: no
member named ‘profile’ in namespace ‘torch::jit::prim’
static constexpr Symbol Kind = ::c10::prim::profile;
^
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:1311:21: error: no
member named ‘prim’ in namespace ‘c10’; did you mean simply ‘prim’?
: Node(graph, ::c10::prim::profile), callback_(callback) {}
^
~~~~
prim
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:66:11: note: ‘prim’
declared here
namespace prim {
^
C:\REPOS\CPU\libtorch-shared-with-deps-latest\libtorch/include\torch/csrc/jit/ir/ir.h:1311:34: error: no
member named ‘profile’ in namespace ‘torch::jit::prim’
: Node(graph, ::c10::prim::profile), callback_(callback) {}

Can someone please help me to overcome this?
Thanks.

More information:

  1. I have a CPP android project. I’m compiling it using ndk-build (ndk21 and C++14).
  2. I have a file named “main.cpp” and I’m trying to #include “torch/script.h” .
  3. I downloaded libtorch using this command:
    wget https://github.com/Kitware/CMake/releases/download/v3.15.2/cmake-3.15.2.tar.gz
  4. In my Android.mk file, I’m giving the path to:
    LOCAL_C_INCLUDES += $(TORCH_REPO_PATH)
    $(TORCH_REPO_PATH)/include
    $(TORCH_REPO_PATH)/include/torch
    $(TORCH_REPO_PATH)/include/torch/csrc/api/include
  5. When I’m trying to compile, I get the errors demonstrated above.
    How can I overcome this issue?

This question seems to be related to https://github.com/pytorch/pytorch/issues/38319

Copy the options from there:

  1. You can use the prebuilt binaries we released for mobile, see details: https://pytorch.org/mobile/home/
  2. You can build it from source on your own for Android using the following script:
scripts/build_pytorch_android.sh
  1. If you don’t want to use the JNI wrapper, you can also use the C++ API directly from your Android C++ code. This is not officially supported yet, but here is a simple example: https://github.com/ljk53/pytorch-android-cpp-demo

For option 1 & option 2, you will get libtorch built in the form of Android AAR (https://stackoverflow.com/questions/23915619/android-archive-library-aar-vs-standard-jar), which provides Java API and can be called from your Java code on Android.

If you want to call libtorch C++ API in your mobile app directly, you can try Option 3 (which is not well supported yet) - please check out the example code and follow README to see how it works.

Hello @ljk53,
Thank you for your help.

  • I was able to run my model using the JAVA example.
  • I was able to execute the “pytorch-android-cpp-demo” on my Android device. Now I’m trying to use the generated binaries in my cpp android project.

Can you please tell me what can be done to compile the binaries for arm64? I would like to get .so files and not static libraries.

Hi, I got exactly the same error as this post. I am using Pytorch 1.6. The codes I include works well on Mac, Linux, Windows, but fail on NDK. Any hint? Thanks.