Repository: pytorch
Issue: Custom build of c++ libtorch for Android program or dynamic so lib does not reduce program or lib size
Labels: module: android,oncall: mobile

## 🐛 Bug

Steps to reproduce the behavior:

1. Get yaml from model
2.  run the following build command: 
BUILD_PYTORCH_MOBILE=1 SELECTED_OP_LIST=../android_test/simple.yaml  scripts/build_pytorch_android.sh armeabi-v7a
3. We try different yamls and not custom build and check the lib sizes
 not custom build
    19822270 Mar 24 09:55 libXNNPACK.a
    2755518 Mar 24 09:54 libc10.a
       7090 Mar 24 09:52 libclog.a
     411828 Mar 24 09:53 libcpuinfo.a
    5655228 Mar 24 09:53 libeigen_blas.a
    2221208 Mar 24 09:53 libnnpack.a
     234166 Mar 24 09:52 libpthreadpool.a
    1883140 Mar 24 09:53 libpytorch_qnnpack.a
        816 Mar 24 10:28 libtorch.a
  134633792 Mar 24 10:28 libtorch_cpu.a

yaml from a resnet
   19808734 Mar 29 10:39 libXNNPACK.a
    2755046 Apr 12 18:25 libc10.a
       7078 Mar 29 10:36 libclog.a
     411624 Mar 29 10:37 libcpuinfo.a
    5654988 Apr 12 16:13 libeigen_blas.a
    2220824 Mar 29 10:37 libnnpack.a
     234118 Mar 29 10:36 libpthreadpool.a
    1882312 Apr 12 18:25 libpytorch_qnnpack.a
        816 Apr 12 20:04 libtorch.a
  125607230 Apr 12 20:04 libtorch_cpu.a

only one op quantized::linear
   19822270 Jun 11 00:22 libXNNPACK.a
    2755046 Jun 11 00:21 libc10.a
       7090 Jun 11 00:19 libclog.a
     411828 Jun 11 00:20 libcpuinfo.a
    5655228 Jun 11 00:20 libeigen_blas.a
    2221208 Jun 11 00:20 libnnpack.a
     234166 Jun 11 00:19 libpthreadpool.a
    1883140 Jun 11 00:21 libpytorch_qnnpack.a
        816 Jun 11 00:53 libtorch.a
  124893632 Jun 11 00:53 libtorch_cpu.a
It looks like that the *.a files are not much different in size. BTW, we checked the generated VariableTypeEverything.cpp, and found they are the same regardless of different yaml files.

4. We further wrote a simple program predictior.cpp that just calls torch::jit::load to load a module and does module.forward, with the following command:

ndk_path=~/tools/Devolop_Tools/android-ndk-r21d
build_android_path=./

  $ndk_path/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ \
    --target=armv7-none-linux-androideabi21 \
    --gcc-toolchain=$ndk_path/toolchains/llvm/prebuilt/darwin-x86_64 \
    --sysroot=$ndk_path/toolchains/llvm/prebuilt/darwin-x86_64/sysroot   \
    -I$build_android_path/install/include \
    -I$build_android_path/install/include/torch/csrc/api/include  \
    -DANDROID \
    -frtti -fexceptions  \
    -fdata-sections \
    -ffunction-sections \
    -no-canonical-prefixes \
    -D_FORTIFY_SOURCE=2 \
    -march=armv7-a -mthumb \
    -Wformat \
    -Werror=format-security \
    -Oz \
    -DNDEBUG  \
    -std=gnu++14 \
    -o predictor.o \
    -c predictor.cpp

  $ndk_path/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ \
    --target=armv7-none-linux-androideabi21 \
    --gcc-toolchain=$ndk_path/toolchains/llvm/prebuilt/darwin-x86_64 \
    --sysroot=$ndk_path/toolchains/llvm/prebuilt/darwin-x86_64/sysroot   \
    -DANDROID \
    -no-canonical-prefixes \
    -D_FORTIFY_SOURCE=2 \
    -march=armv7-a \
    -mthumb -Wformat \
    -Werror=format-security \
    -Oz -DNDEBUG                    \
    -Wl,--exclude-libs,libgcc.a \
    -Wl,--exclude-libs,libgcc_real.a \
    -Wl,--exclude-libs,libatomic.a \
    -Wl,--build-id \
    -Wl,--fatal-warnings \
    -Wl,--exclude-libs,libunwind.a \
    -Wl,--no-undefined -Qunused-arguments \
    -Wl,--no-whole-archive \
    $build_android_path/install/lib/libtorch.a \
    $build_android_path/install/lib/libtorch_cpu.a \
    $build_android_path/install/lib/libc10.a \
    $build_android_path/install/lib/libnnpack.a \
    $build_android_path/install/lib/libpytorch_qnnpack.a \
    $build_android_path/install/lib/libXNNPACK.a \
    $build_android_path/install/lib/libclog.a \
    $build_android_path/install/lib/libcpuinfo.a \
    $build_android_path/install/lib/libpthreadpool.a \
    $build_android_path/install/lib/libeigen_blas.a \
    $build_android_path/install/lib/libVulkanWrapper.a \
    $predictor.o  -o predictor \
    -Wl,--no-whole-archive -llog -latomic -lm 

5. The predictor program runs well on Android with corresponding module, but no matter how we change the yaml file, the program size is always around 11MB.
6. We build dynamic .so files. Still, with different yaml files, the sizes of so files are almost the same and around 11MB.

## Expected behavior

the predictor programs or so files should have different size with different set of ops and without custom build

## Environment


 - PyTorch Version (e.g., 1.0): 1.8
 - OS (e.g., Linux): MACOS using android-ndk-r21d
 - How you installed PyTorch (`conda`, `pip`, source): source
 - Build command you used (if compiling from source):  BUILD_PYTORCH_MOBILE=1 SELECTED_OP_LIST=../android_test/simple.yaml  scripts/build_pytorch_android.sh armeabi-v7a