Anyway to use c++ api on mobile to do inference task?

As the title mentioned, anyway to compile the c++ api and use them on android/ios? Or there exist prebuild binary?

Hi,
We do have a binary that you can build in order to test/run your model on an android device.
Here is a link to the file (in C++) - https://github.com/pytorch/pytorch/blob/master/binaries/speed_benchmark_torch.cc

You can build the binary by running

./scripts/build_android.sh \
-DBUILD_BINARY=ON \
-DBUILD_CAFFE2_MOBILE=OFF \
-DCMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())') \
-DPYTHON_EXECUTABLE=$(python -c 'import sys; print(sys.executable)')

To execute you can run

./speed_benchmark_torch --model test.pt --input_dims="1,3,224,224" --input_type=float --warmup=5 --iter 20
1 Like

Any plan to release prebuild lib like linux, mac and windows?

@ngap_wei_Tham
Pytorch mobile is moving towards providing tooling for building custom mobile builds which include only operators that particular model needs. As all-ops default build maybe overshoot by lib size.

1 Like

We have prebuilt libraries for Android and iOS. The iOS tutorial uses the C++ API: https://pytorch.org/mobile/ios/ . We will probably support the C++ API on Android eventually, but for now the easiest path is to use the Java API.

1 Like

I have an example app here showing how to use a pytorch model in C++:

2 Likes

It would be much easier to create cross-platform app if we could use c++ api, I have tried tensorflow-lite, but old of the tutorials of how to build the c++ api are too old, now trying with caffe2, hope it work.

ps : Hope one day there will have a c++ library which could run the inference part of deep learning models efficiently on major platforms(desktop, mobiles and embedded devices), today we have to use x-tools for y platforms for performance(and other) sake.

Thanks, could you run object detection model by c++ api?mask-rcnn do not support yet(even it is, maybe too slow for mobile), have you tried with mobileNet quantized?Thanks

When I build the pytorch from source, I always get the error message

src.cxx:1:10: fatal error: 'glog/stl_logging.h' file not found

os : ubuntu18.04.3 LTS 64bits
ndk : r18b
sdk : 29(minimum 21)
gradle: 4.1.6
python : 3.6, install pytorch1.3 with conda

Do you know how to solve it? Thanks

You probably need to install glog: sudo apt install libgoogle-glog-dev

Don’t forget you have to build pytorch for android and set TORCHPATH correctly in the CMakeLists.txt file!

Let me know if it works.

Thanks, don’t work for 1.3.0, haven’t tried with 1.4.0 yet