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)
157 lines
5.3 KiB
Bash
Executable File
157 lines
5.3 KiB
Bash
Executable File
#!/bin/bash -xe
|
|
##############################################################################
|
|
# Example command to build the iOS target.
|
|
##############################################################################
|
|
#
|
|
# This script shows how one can build a Caffe2 binary for the iOS platform
|
|
# using ios-cmake. This is very similar to the android-cmake - see
|
|
# build_android.sh for more details.
|
|
|
|
CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)"
|
|
|
|
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
|
|
# bitcode
|
|
if [ "${ENABLE_BITCODE:-}" == '1' ]; then
|
|
CMAKE_ARGS+=("-DCMAKE_C_FLAGS=-fembed-bitcode")
|
|
CMAKE_ARGS+=("-DCMAKE_CXX_FLAGS=-fembed-bitcode")
|
|
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"
|
|
BITCODE_FLAGS="-DCMAKE_C_FLAGS=-fembed-bitcode -DCMAKE_CXX_FLAGS=-fembed-bitcode "
|
|
$CAFFE2_ROOT/scripts/build_host_protoc.sh --other-flags $BITCODE_FLAGS
|
|
# 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")
|
|
# Bitcode is enabled by default for caffe2
|
|
CMAKE_ARGS+=("-DCMAKE_C_FLAGS=-fembed-bitcode")
|
|
CMAKE_ARGS+=("-DCMAKE_CXX_FLAGS=-fembed-bitcode")
|
|
fi
|
|
|
|
# Use ios-cmake to build iOS project from CMake.
|
|
# This projects sets CMAKE_C_COMPILER to /usr/bin/gcc and
|
|
# CMAKE_CXX_COMPILER to /usr/bin/g++. In order to use ccache (if it is available) we
|
|
# must override these variables via CMake arguments.
|
|
CMAKE_ARGS+=("-DCMAKE_TOOLCHAIN_FILE=$CAFFE2_ROOT/cmake/iOS.cmake")
|
|
if [ -n "${CCACHE_WRAPPER_PATH:-}"]; then
|
|
CCACHE_WRAPPER_PATH=/usr/local/opt/ccache/libexec
|
|
fi
|
|
if [ -d "$CCACHE_WRAPPER_PATH" ]; then
|
|
CMAKE_ARGS+=("-DCMAKE_C_COMPILER=$CCACHE_WRAPPER_PATH/gcc")
|
|
CMAKE_ARGS+=("-DCMAKE_CXX_COMPILER=$CCACHE_WRAPPER_PATH/g++")
|
|
fi
|
|
|
|
# IOS_PLATFORM controls type of iOS platform (see ios-cmake)
|
|
if [ -n "${IOS_PLATFORM:-}" ]; then
|
|
CMAKE_ARGS+=("-DIOS_PLATFORM=${IOS_PLATFORM}")
|
|
if [ "${IOS_PLATFORM}" == "WATCHOS" ]; then
|
|
# enable bitcode by default for watchos
|
|
CMAKE_ARGS+=("-DCMAKE_C_FLAGS=-fembed-bitcode")
|
|
CMAKE_ARGS+=("-DCMAKE_CXX_FLAGS=-fembed-bitcode")
|
|
# disable the QNNPACK
|
|
CMAKE_ARGS+=("-DUSE_PYTORCH_QNNPACK=OFF")
|
|
fi
|
|
else
|
|
# IOS_PLATFORM is not set, default to OS, which builds iOS.
|
|
CMAKE_ARGS+=("-DIOS_PLATFORM=OS")
|
|
fi
|
|
|
|
if [ -n "${IOS_ARCH:-}" ]; then
|
|
CMAKE_ARGS+=("-DIOS_ARCH=${IOS_ARCH}")
|
|
fi
|
|
|
|
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+=("-DUSE_LITE_INTERPRETER_PROFILER=OFF")
|
|
|
|
# Don't build binaries or tests (only the library)
|
|
CMAKE_ARGS+=("-DBUILD_TEST=OFF")
|
|
CMAKE_ARGS+=("-DBUILD_BINARY=OFF")
|
|
CMAKE_ARGS+=("-DBUILD_PYTHON=OFF")
|
|
|
|
# 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_NUMPY=OFF")
|
|
CMAKE_ARGS+=("-DUSE_NNPACK=OFF")
|
|
CMAKE_ARGS+=("-DUSE_MKLDNN=OFF")
|
|
|
|
# Metal
|
|
if [ "${USE_PYTORCH_METAL:-}" == "1" ]; then
|
|
CMAKE_ARGS+=("-DUSE_PYTORCH_METAL=ON")
|
|
fi
|
|
|
|
# Core ML
|
|
if [ "${USE_COREML_DELEGATE}" == "1" ]; then
|
|
CMAKE_ARGS+=("-DUSE_COREML_DELEGATE=ON")
|
|
fi
|
|
|
|
# pthreads
|
|
CMAKE_ARGS+=("-DCMAKE_THREAD_LIBS_INIT=-lpthread")
|
|
CMAKE_ARGS+=("-DCMAKE_HAVE_THREADS_LIBRARY=1")
|
|
CMAKE_ARGS+=("-DCMAKE_USE_PTHREADS_INIT=1")
|
|
|
|
# Only toggle if VERBOSE=1
|
|
if [ "${VERBOSE:-}" == '1' ]; then
|
|
CMAKE_ARGS+=("-DCMAKE_VERBOSE_MAKEFILE=1")
|
|
fi
|
|
|
|
# enable ARC
|
|
CMAKE_ARGS+=("-DCMAKE_CXX_FLAGS=-fobjc-arc")
|
|
|
|
# Now, actually build the iOS target.
|
|
BUILD_ROOT=${BUILD_ROOT:-"$CAFFE2_ROOT/build_ios"}
|
|
INSTALL_PREFIX=${BUILD_ROOT}/install
|
|
mkdir -p $BUILD_ROOT
|
|
cd $BUILD_ROOT
|
|
cmake "$CAFFE2_ROOT" \
|
|
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
|
|
-DCMAKE_BUILD_TYPE=MinSizeRel \
|
|
-DBUILD_SHARED_LIBS=OFF \
|
|
${CMAKE_ARGS[@]} \
|
|
$@
|
|
|
|
cmake --build . -- "-j$(sysctl -n hw.ncpu)"
|
|
|
|
# copy headers and libs to install directory
|
|
echo "Will install headers and libs to $INSTALL_PREFIX for further Xcode project usage."
|
|
make install
|
|
echo "Installation completed, now you can copy the headers/libs from $INSTALL_PREFIX to your Xcode project directory."
|