File size of dynamic libraries for android are very large

The file size of dynamic libraries for android generated under pytorch/build_android/install/lib are very large. Here is the detail:

-rw-r–r-- 1 ultraz ultraz 5.7M Jun 30 23:17 build_android/install/lib/libc10.so
-rw-r–r-- 1 ultraz ultraz 82M Jun 30 23:19 build_android/install/lib/libtorch_cpu.so
-rw-r–r-- 1 ultraz ultraz 7.4K Jun 30 23:17 build_android/install/lib/libtorch_global_deps.so
-rw-r–r-- 1 ultraz ultraz 7.4K Jun 30 23:19 build_android/install/lib/libtorch.so

For libtorch_cpu, it is 82M, which is considerably large once integrated into a mobile app. I see the script/build_android.sh has already disabled a lot of features. I would like to ask if it is possible to further reduce the size of libs?

1 Like

I also met this issue when compiling mobile version. 100M+ is too big for smartphones…

Use selective build to only include the used operators in the final library, see

https://pytorch.org/tutorials/recipes/mobile_interpreter.html#how-to-use-mobile-interpreter-custom-build
and
(prototype) Tracing-based Selective Build Mobile Interpreter in Android and iOS — PyTorch Tutorials 1.12.0+cu102 documentation (even smaller size)

Thank you and I will try it.

[quote="leigao97, post:1, topic:155576"]
-rw-r–r-- 1 ultraz ultraz 82M Jun 30 23:19 build_android/install/lib/libtorch_cpu.so
[/quote]
  1. Looks like it’s the size of an old interpreter. The size should be smaller if you check build_android_lite/install/lib/libtorch_cpu.so. See (beta) Efficient mobile interpreter in Android and iOS — PyTorch Tutorials 1.12.0+cu102 documentation

  2. From the number, this libtorch_cpu.so includes all 4 android abis (armeabi-v7a, arm64-v8a, x86, x86_64). For one device, only one abi is needed and size should be much less. Select build is another way to bring down. the size.

I have tested a couple of cases for ANDROID_ABI=arm64-v8a, and here is the result:

  1. BUILD_LITE_INTERPRETER=0 ./script/build_android.shlibtorch_cpu.so is 89M
  2. BUILD_LITE_INTERPRETER=1 ./script/build_android.shlibtorch_cpu.so is 81M
  3. BUILD_LITE_INTERPRETER=1 .SELECTED_OP_LIST=scripted_lenet_model.yaml /script/build_android.shlibtorch_cpu.so is 75M

@cccclai Can you clarify that I am using the new interpreter? For your second point, I think it only builds one specific ABI because I have declared before compiling.

Try to use

BUILD_LITE_INTERPRETER=1 ./scripts/build_pytorch_android.sh x86

to build for x86.

The one with smaller size is the new interpreter. BUILD_LITE_INTERPRETER=1 is the correct environement setting.