mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary:
Reason:
To have one-step build for test android application based on the current code state that is ready for profiling with simpleperf, systrace etc. to profile performance inside the application.
## Parameters to control debug symbols stripping
Introducing /CMakeLists parameter `ANDROID_DEBUG_SYMBOLS` to be able not to strip symbols for pytorch (not add linker flag `-s`)
which is checked in `scripts/build_android.sh`
On gradle side stripping happens by default, and to prevent it we have to specify
```
android {
packagingOptions {
doNotStrip "**/*.so"
}
}
```
which is now controlled by new gradle property `nativeLibsDoNotStrip `
## Test_App
`android/test_app` - android app with one MainActivity that does inference in cycle
`android/build_test_app.sh` - script to build libtorch with debug symbols for specified android abis and adds `NDK_DEBUG=1` and `-PnativeLibsDoNotStrip=true` to keep all debug symbols for profiling.
Script assembles all debug flavors:
```
└─ $ find . -type f -name *apk
./test_app/app/build/outputs/apk/mobilenetQuant/debug/test_app-mobilenetQuant-debug.apk
./test_app/app/build/outputs/apk/resnet/debug/test_app-resnet-debug.apk
```
## Different build configurations
Module for inference can be set in `android/test_app/app/build.gradle` as a BuildConfig parameters:
```
productFlavors {
mobilenetQuant {
dimension "model"
applicationIdSuffix ".mobilenetQuant"
buildConfigField ("String", "MODULE_ASSET_NAME", buildConfigProps('MODULE_ASSET_NAME_MOBILENET_QUANT'))
addManifestPlaceholders([APP_NAME: "PyMobileNetQuant"])
buildConfigField ("String", "LOGCAT_TAG", "\"pytorch-mobilenet\"")
}
resnet {
dimension "model"
applicationIdSuffix ".resnet"
buildConfigField ("String", "MODULE_ASSET_NAME", buildConfigProps('MODULE_ASSET_NAME_RESNET18'))
addManifestPlaceholders([APP_NAME: "PyResnet"])
buildConfigField ("String", "LOGCAT_TAG", "\"pytorch-resnet\"")
}
```
In that case we can setup several apps on the same device for comparison, to separate packages `applicationIdSuffix`: 'org.pytorch.testapp.mobilenetQuant' and different application names and logcat tags as `manifestPlaceholder` and another BuildConfig parameter:
```
─ $ adb shell pm list packages | grep pytorch
package:org.pytorch.testapp.mobilenetQuant
package:org.pytorch.testapp.resnet
```
In future we can add another BuildConfig params e.g. single/multi threads and other configuration for profiling.
At the moment 2 flavors - for resnet18 and for mobilenetQuantized
which can be installed on connected device:
```
cd android
```
```
gradle test_app:installMobilenetQuantDebug
```
```
gradle test_app:installResnetDebug
```
## Testing:
```
cd android
sh build_test_app.sh
adb install -r test_app/app/build/outputs/apk/mobilenetQuant/debug/test_app-mobilenetQuant-debug.apk
```
```
cd $ANDROID_NDK
python simpleperf/run_simpleperf_on_device.py record --app org.pytorch.testapp.mobilenetQuant -g --duration 10 -o /data/local/tmp/perf.data
adb pull /data/local/tmp/perf.data
python simpleperf/report_html.py
```
Simpleperf report has all symbols:

Pull Request resolved: https://github.com/pytorch/pytorch/pull/28406
Differential Revision: D18386622
Pulled By: IvanKobzarev
fbshipit-source-id: 3a751192bbc4bc3c6d7f126b0b55086b4d586e7a
133 lines
4.7 KiB
Bash
Executable File
133 lines
4.7 KiB
Bash
Executable File
#!/bin/bash
|
|
##############################################################################
|
|
# Example command to build the android target.
|
|
##############################################################################
|
|
#
|
|
# This script shows how one can build a Caffe2 binary for the Android platform
|
|
# using android-cmake. A few notes:
|
|
#
|
|
# (1) This build also does a host build for protobuf. You will need autoconf
|
|
# to carry out this. If autoconf is not possible, you will need to provide
|
|
# a pre-built protoc binary that is the same version as the protobuf
|
|
# version under third_party.
|
|
# If you are building on Mac, you might need to install autotool and
|
|
# libtool. The easiest way is via homebrew:
|
|
# brew install automake
|
|
# brew install libtool
|
|
# (2) You will need to have android ndk installed. The current script assumes
|
|
# that you set ANDROID_NDK to the location of ndk.
|
|
# (3) The toolchain and the build target platform can be specified with the
|
|
# cmake arguments below. For more details, check out android-cmake's doc.
|
|
|
|
set -e
|
|
|
|
# Android specific flags
|
|
if [ -z "$ANDROID_ABI" ]; then
|
|
ANDROID_ABI="armeabi-v7a with NEON"
|
|
fi
|
|
ANDROID_NATIVE_API_LEVEL="21"
|
|
echo "Build with ANDROID_ABI[$ANDROID_ABI], ANDROID_NATIVE_API_LEVEL[$ANDROID_NATIVE_API_LEVEL]"
|
|
|
|
CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)"
|
|
if [ -z "$ANDROID_NDK" ]; then
|
|
echo "ANDROID_NDK not set; please set it to the Android NDK directory"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -d "$ANDROID_NDK" ]; then
|
|
echo "ANDROID_NDK not a directory; did you install it under $ANDROID_NDK?"
|
|
exit 1
|
|
fi
|
|
|
|
ANDROID_NDK_PROPERTIES="$ANDROID_NDK/source.properties"
|
|
[ -f "$ANDROID_NDK_PROPERTIES" ] && ANDROID_NDK_VERSION=$(sed -n 's/^Pkg.Revision[^=]*= *\([0-9]*\)\..*$/\1/p' "$ANDROID_NDK_PROPERTIES")
|
|
|
|
echo "Bash: $(/bin/bash --version | head -1)"
|
|
echo "Caffe2 path: $CAFFE2_ROOT"
|
|
echo "Using Android NDK at $ANDROID_NDK"
|
|
echo "Android NDK version: $ANDROID_NDK_VERSION"
|
|
|
|
# Now, actually build the Android target.
|
|
BUILD_ROOT=${BUILD_ROOT:-"$CAFFE2_ROOT/build_android"}
|
|
INSTALL_PREFIX=${BUILD_ROOT}/install
|
|
mkdir -p $BUILD_ROOT
|
|
cd $BUILD_ROOT
|
|
|
|
CMAKE_ARGS=()
|
|
|
|
if [ -n "${BUILD_PYTORCH_MOBILE:-}" ]; then
|
|
CMAKE_ARGS+=("-DBUILD_CAFFE2_MOBILE=OFF")
|
|
CMAKE_ARGS+=("-DCMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')")
|
|
CMAKE_ARGS+=("-DPYTHON_EXECUTABLE=$(python -c 'import sys; print(sys.executable)')")
|
|
CMAKE_ARGS+=("-DBUILD_CUSTOM_PROTOBUF=OFF")
|
|
else
|
|
# Build protobuf from third_party so we have a host protoc binary.
|
|
echo "Building protoc"
|
|
$CAFFE2_ROOT/scripts/build_host_protoc.sh
|
|
# Use locally built protoc because we'll build libprotobuf for the
|
|
# target architecture and need an exact version match.
|
|
CMAKE_ARGS+=("-DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=$CAFFE2_ROOT/build_host_protoc/bin/protoc")
|
|
fi
|
|
|
|
# If Ninja is installed, prefer it to Make
|
|
if [ -x "$(command -v ninja)" ]; then
|
|
CMAKE_ARGS+=("-GNinja")
|
|
fi
|
|
|
|
# Use android-cmake to build Android project from CMake.
|
|
CMAKE_ARGS+=("-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake")
|
|
|
|
# Don't build artifacts we don't need
|
|
CMAKE_ARGS+=("-DBUILD_TEST=OFF")
|
|
CMAKE_ARGS+=("-DBUILD_BINARY=OFF")
|
|
CMAKE_ARGS+=("-DBUILD_PYTHON=OFF")
|
|
CMAKE_ARGS+=("-DBUILD_SHARED_LIBS=OFF")
|
|
if (( "${ANDROID_NDK_VERSION:-0}" < 18 )); then
|
|
CMAKE_ARGS+=("-DANDROID_TOOLCHAIN=gcc")
|
|
else
|
|
CMAKE_ARGS+=("-DANDROID_TOOLCHAIN=clang")
|
|
fi
|
|
# Disable unused dependencies
|
|
CMAKE_ARGS+=("-DUSE_CUDA=OFF")
|
|
CMAKE_ARGS+=("-DUSE_GFLAGS=OFF")
|
|
CMAKE_ARGS+=("-DUSE_OPENCV=OFF")
|
|
CMAKE_ARGS+=("-DUSE_LMDB=OFF")
|
|
CMAKE_ARGS+=("-DUSE_LEVELDB=OFF")
|
|
CMAKE_ARGS+=("-DUSE_MPI=OFF")
|
|
CMAKE_ARGS+=("-DUSE_OPENMP=OFF")
|
|
# Only toggle if VERBOSE=1
|
|
if [ "${VERBOSE:-}" == '1' ]; then
|
|
CMAKE_ARGS+=("-DCMAKE_VERBOSE_MAKEFILE=1")
|
|
fi
|
|
|
|
# Android specific flags
|
|
CMAKE_ARGS+=("-DANDROID_NDK=$ANDROID_NDK")
|
|
CMAKE_ARGS+=("-DANDROID_ABI=$ANDROID_ABI")
|
|
CMAKE_ARGS+=("-DANDROID_NATIVE_API_LEVEL=$ANDROID_NATIVE_API_LEVEL")
|
|
CMAKE_ARGS+=("-DANDROID_CPP_FEATURES=rtti exceptions")
|
|
|
|
if [ "${ANDROID_DEBUG_SYMBOLS:-}" == '1' ]; then
|
|
CMAKE_ARGS+=("-DANDROID_DEBUG_SYMBOLS=1")
|
|
fi
|
|
|
|
# Use-specified CMake arguments go last to allow overridding defaults
|
|
CMAKE_ARGS+=($@)
|
|
|
|
cmake "$CAFFE2_ROOT" \
|
|
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
|
|
-DCMAKE_BUILD_TYPE=Release \
|
|
"${CMAKE_ARGS[@]}"
|
|
|
|
# Cross-platform parallel build
|
|
if [ -z "$MAX_JOBS" ]; then
|
|
if [ "$(uname)" == 'Darwin' ]; then
|
|
MAX_JOBS=$(sysctl -n hw.ncpu)
|
|
else
|
|
MAX_JOBS=$(nproc)
|
|
fi
|
|
fi
|
|
|
|
echo "Will install headers and libs to $INSTALL_PREFIX for further Android project usage."
|
|
cmake --build . --target install -- "-j${MAX_JOBS}"
|
|
echo "Installation completed, now you can copy the headers/libs from $INSTALL_PREFIX to your Android project directory."
|