mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary: RFC: https://github.com/pytorch/rfcs/pull/40 This PR (re)introduces python codegen for unboxing wrappers. Given an entry of `native_functions.yaml` the codegen should be able to generate the corresponding C++ code to convert ivalues from the stack to their proper types. To trigger the codegen, run ``` tools/jit/gen_unboxing.py -d cg/torch/share/ATen ``` Merged changes on CI test. In https://github.com/pytorch/pytorch/issues/71782 I added an e2e test for static dispatch + codegen unboxing. The test exports a mobile model of mobilenetv2, load and run it on a new binary for lite interpreter: `test/mobile/custom_build/lite_predictor.cpp`. ## Lite predictor build specifics 1. Codegen: `gen.py` generates `RegisterCPU.cpp` and `RegisterSchema.cpp`. Now with this PR, once `static_dispatch` mode is enabled, `gen.py` will not generate `TORCH_LIBRARY` API calls in those cpp files, hence avoids interaction with the dispatcher. Once `USE_LIGHTWEIGHT_DISPATCH` is turned on, `cmake/Codegen.cmake` calls `gen_unboxing.py` which generates `UnboxingFunctions.h`, `UnboxingFunctions_[0-4].cpp` and `RegisterCodegenUnboxedKernels_[0-4].cpp`. 2. Build: `USE_LIGHTWEIGHT_DISPATCH` adds generated sources into `all_cpu_cpp` in `aten/src/ATen/CMakeLists.txt`. All other files remain unchanged. In reality all the `Operators_[0-4].cpp` are not necessary but we can rely on linker to strip them off. ## Current CI job test coverage update Created a new CI job `linux-xenial-py3-clang5-mobile-lightweight-dispatch-build` that enables the following build options: * `USE_LIGHTWEIGHT_DISPATCH=1` * `BUILD_LITE_INTERPRETER=1` * `STATIC_DISPATCH_BACKEND=CPU` This job triggers `test/mobile/lightweight_dispatch/build.sh` and builds `libtorch`. Then the script runs C++ tests written in `test_lightweight_dispatch.cpp` and `test_codegen_unboxing.cpp`. Recent commits added tests to cover as many C++ argument type as possible: in `build.sh` we installed PyTorch Python API so that we can export test models in `tests_setup.py`. Then we run C++ test binary to run these models on lightweight dispatch enabled runtime. Pull Request resolved: https://github.com/pytorch/pytorch/pull/69881 Reviewed By: iseeyuan Differential Revision: D33692299 Pulled By: larryliu0820 fbshipit-source-id: 211e59f2364100703359b4a3d2ab48ca5155a023 (cherry picked from commit 58e1c9a25e3d1b5b656282cf3ac2f548d98d530b)
199 lines
6.8 KiB
Bash
Executable File
199 lines
6.8 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
|
|
|
|
if [ -z "$PYTHON" ]; then
|
|
PYTHON=python
|
|
PYTHON_VERSION_MAJOR=$($PYTHON -c 'import sys; print(sys.version_info[0])')
|
|
if [ "${PYTHON_VERSION_MAJOR}" -le 2 ]; then
|
|
echo "Default python executable is Python-2, trying to use python3 alias"
|
|
PYTHON=python3
|
|
fi
|
|
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 "Python: $($PYTHON -c 'import sys; print(sys.version)')"
|
|
echo "Caffe2 path: $CAFFE2_ROOT"
|
|
echo "Using Android NDK at $ANDROID_NDK"
|
|
echo "Android NDK version: $ANDROID_NDK_VERSION"
|
|
|
|
CMAKE_ARGS=()
|
|
|
|
if [ -z "${BUILD_CAFFE2_MOBILE:-}" ]; then
|
|
# Build PyTorch mobile
|
|
CMAKE_ARGS+=("-DCMAKE_PREFIX_PATH=$($PYTHON -c 'import sysconfig; print(sysconfig.get_path("purelib"))')")
|
|
CMAKE_ARGS+=("-DPYTHON_EXECUTABLE=$($PYTHON -c 'import sys; print(sys.executable)')")
|
|
CMAKE_ARGS+=("-DBUILD_CUSTOM_PROTOBUF=OFF")
|
|
# custom build with selected ops
|
|
if [ -n "${SELECTED_OP_LIST}" ]; then
|
|
SELECTED_OP_LIST="$(cd $(dirname $SELECTED_OP_LIST); pwd -P)/$(basename $SELECTED_OP_LIST)"
|
|
echo "Choose SELECTED_OP_LIST file: $SELECTED_OP_LIST"
|
|
if [ ! -r ${SELECTED_OP_LIST} ]; then
|
|
echo "Error: SELECTED_OP_LIST file ${SELECTED_OP_LIST} not found."
|
|
exit 1
|
|
fi
|
|
CMAKE_ARGS+=("-DSELECTED_OP_LIST=${SELECTED_OP_LIST}")
|
|
fi
|
|
else
|
|
# Build Caffe2 mobile
|
|
CMAKE_ARGS+=("-DBUILD_CAFFE2_MOBILE=ON")
|
|
# 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")
|
|
|
|
if [ -z "$BUILD_MOBILE_BENCHMARK" ]; then
|
|
BUILD_MOBILE_BENCHMARK=0
|
|
fi
|
|
|
|
if [ -z "$BUILD_MOBILE_TEST" ]; then
|
|
BUILD_MOBILE_TEST=0
|
|
fi
|
|
# Don't build artifacts we don't need
|
|
CMAKE_ARGS+=("-DBUILD_TEST=OFF")
|
|
CMAKE_ARGS+=("-DBUILD_BINARY=OFF")
|
|
|
|
# If there exists env variable and it equals to 0, build full jit interpreter.
|
|
# Default behavior is to build lite interpreter
|
|
# cmd: BUILD_LITE_INTERPRETER=0 ./scripts/build_android.sh
|
|
if [ "${BUILD_LITE_INTERPRETER}" == 0 ]; then
|
|
CMAKE_ARGS+=("-DBUILD_LITE_INTERPRETER=OFF")
|
|
else
|
|
CMAKE_ARGS+=("-DBUILD_LITE_INTERPRETER=ON")
|
|
fi
|
|
if [ "${TRACING_BASED}" == 1 ]; then
|
|
CMAKE_ARGS+=("-DTRACING_BASED=ON")
|
|
else
|
|
CMAKE_ARGS+=("-DTRACING_BASED=OFF")
|
|
fi
|
|
if [ "${USE_LIGHTWEIGHT_DISPATCH}" == 1 ]; then
|
|
CMAKE_ARGS+=("-DUSE_LIGHTWEIGHT_DISPATCH=ON")
|
|
CMAKE_ARGS+=("-DSTATIC_DISPATCH_BACKEND=CPU")
|
|
else
|
|
CMAKE_ARGS+=("-DUSE_LIGHTWEIGHT_DISPATCH=OFF")
|
|
fi
|
|
|
|
CMAKE_ARGS+=("-DBUILD_MOBILE_BENCHMARK=$BUILD_MOBILE_BENCHMARK")
|
|
CMAKE_ARGS+=("-DBUILD_MOBILE_TEST=$BUILD_MOBILE_TEST")
|
|
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_STL_SHARED:-}" == '1' ]; then
|
|
CMAKE_ARGS+=("-DANDROID_STL=c++_shared")
|
|
fi
|
|
if [ "${ANDROID_DEBUG_SYMBOLS:-}" == '1' ]; then
|
|
CMAKE_ARGS+=("-DANDROID_DEBUG_SYMBOLS=1")
|
|
fi
|
|
|
|
if [ -n "${USE_VULKAN}" ]; then
|
|
CMAKE_ARGS+=("-DUSE_VULKAN=ON")
|
|
if [ -n "${USE_VULKAN_FP16_INFERENCE}" ]; then
|
|
CMAKE_ARGS+=("-DUSE_VULKAN_FP16_INFERENCE=ON")
|
|
fi
|
|
if [ -n "${USE_VULKAN_RELAXED_PRECISION}" ]; then
|
|
CMAKE_ARGS+=("-DUSE_VULKAN_RELAXED_PRECISION=ON")
|
|
fi
|
|
if [ -n "${USE_VULKAN_SHADERC_RUNTIME}" ]; then
|
|
CMAKE_ARGS+=("-DUSE_VULKAN_SHADERC_RUNTIME=ON")
|
|
fi
|
|
fi
|
|
|
|
# Use-specified CMake arguments go last to allow overridding defaults
|
|
CMAKE_ARGS+=($@)
|
|
|
|
# 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 "$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."
|