pytorch/scripts
Ashkan Aliabadi 6aecfd1e80 Mobile Backend: NHWC memory layout + XNNPACK integration. (#33722)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/33722

In order to improve CPU performance on floating-point models on mobile, this PR introduces a new CPU backend for mobile that implements the most common mobile operators with NHWC memory layout support through integration with XNNPACK.

XNNPACK itself, and this codepath, are currently only included in the build, but the actual integration is gated with USE_XNNPACK preprocessor guards.  This preprocessor symbol is intentionally not passed on to the compiler, so as to enable this rollout in multiple stages in follow up PRs.  This changeset will build XNNPACK as part of the build if the identically named USE_XNNPACK CMAKE variable, defaulted to ON, is enabled, but will not actually expose or enable this code path in any other way.

Furthermore, it is worth pointing out that in order to efficiently map models to these operators, some front-end method of exposing this backend to the user is needed.  The less efficient implementation would be to hook these operators into their corresponding native implementations, granted that a series of XNNPACK-specific conditions are met, much like how NNPACK is integrated with PyTorch today for instance.

Having said that, while the above implementation is still expected to outperform NNPACK based on the benchmarks I ran, the above integration would be leave a considerable gap between the performance achieved and the maximum performance potential XNNPACK enables, as it does not provide a way to compute and factor out one-time operations out of the inner most forward() loop.

The more optimal solution, and one we will  decide on soon, would involve either providing a JIT pass that maps nn operators onto these newly introduced operators, while allowing one-time calculations to be factored out, much like quantized mobile models.  Alternatively, new eager-mode modules can also be introduced that would directly call into these implementations either through c10 or some other mechanism, also allowing for decoupling of op creation from op execution.

This PR does not include any of the front end changes  mentioned above.  Neither does it include the mobile threadpool unification present in the original https://github.com/pytorch/pytorch/issues/30644.  Furthermore, this codepath seems to be faster than NNPACK in a good number of use cases, which can potentially allow us to remove NNPACK from aten to make the codebase a little simpler, granted that there is widespread support for such a move.

Regardless, these changes will be introduced gradually and in a more controlled way in subsequent PRs.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/32509

Test Plan:
Build: CI
Functionality: Not exposed

Reviewed By: dreiss

Differential Revision: D20069796

Pulled By: AshkanAliabadi

fbshipit-source-id: d46c1c91d4bea91979ea5bd46971ced5417d309c
2020-02-24 21:58:56 -08:00
..
appveyor move flags to c10 (#12144) 2018-10-04 02:09:56 -07:00
fbcode-dev-setup Fix typos (#30606) 2019-12-02 20:17:42 -08:00
model_zoo Bump up opset version to 7 in Caffe2 ONNX exporter (#8854) 2018-06-28 07:39:02 -07:00
onnx Skip same tests in ONNX Python3 CI as in Python2 (#31827) 2020-01-03 12:42:42 -08:00
add_apache_header.sh Re-license to Apache 2017-09-28 16:22:00 -07:00
apache_header.txt Re-license to Apache 2017-09-28 16:22:00 -07:00
apache_python.txt Add script for removing Apache header. 2018-03-27 13:10:18 -07:00
build_android.sh custom build script (#30144) 2019-11-20 13:16:02 -08:00
build_host_protoc.sh Port all PyTorch and Caffe2 jobs to CircleCI (#11264) 2018-09-05 16:28:11 -07:00
build_ios.sh [iOS] Add watchOS support (#33318) 2020-02-14 14:02:22 -08:00
build_local.sh Only set CCACHE_WRAPPER_PATH in the build scripts if it is not already passed in. 2019-11-01 18:39:12 -07:00
build_mobile.sh op dependency analysis bash driver 2019-12-04 00:12:24 -08:00
build_pytorch_android.sh Script for full android build to aars; script to run android tests (#26833) 2019-09-30 14:25:00 -07:00
build_raspbian.sh Fix target name. 2019-02-22 17:27:16 -08:00
build_tegra_x1.sh Allow specifying cmake build directory in the build scripts 2017-11-20 20:32:30 -08:00
build_tizen.sh Allow specifying cmake build directory in the build scripts 2017-11-20 20:32:30 -08:00
build_windows.bat Turn off warnings on Windows CI. (#24331) 2019-08-30 07:11:07 -07:00
diagnose_protobuf.py Fix several DeprecationWarning: invalid escape sequence (#15733) 2019-01-05 08:53:35 -08:00
get_python_cmake_flags.py [caffe2][build] Update python cmake flag print script (#7306) 2018-05-08 00:34:42 -07:00
proto.ps1 Sync fbcode/caffe2 and xplat/caffe2 (1) (#19218) 2019-04-13 21:45:52 -07:00
read_conda_versions.sh Adding gcc4 conda builds (#2283) 2018-03-17 17:26:37 -07:00
README.md Enhance build_ios.sh to be consistent with build_android.sh (#18564) 2019-03-28 21:37:55 -07:00
remove_apache_header.sh Add script for removing Apache header. 2018-03-27 13:10:18 -07:00
run_mobilelab.py Sync fbcode/caffe2 and xplat/caffe2 (1) (#19218) 2019-04-13 21:45:52 -07:00
temp.sh Add script for removing Apache header. 2018-03-27 13:10:18 -07:00
xcode_build.rb Mobile Backend: NHWC memory layout + XNNPACK integration. (#33722) 2020-02-24 21:58:56 -08:00

This directory contains the useful tools.

build_android.sh

This script is to build PyTorch/Caffe2 library for Android. Take the following steps to start the build:

  • set ANDROID_NDK to the location of ndk
export ANDROID_NDK=YOUR_NDK_PATH
  • run build_android.sh
#in your PyTorch root directory
bash scripts/build_android.sh

If succeeded, the libraries and headers would be generated to build_android/install directory. You can then copy these files from build_android/install to your Android project for further usage.

You can also override the cmake flags via command line, e.g., following command will also compile the executable binary files:

bash scripts/build_android.sh -DBUILD_BINARY=ON

build_ios.sh

This script is to build PyTorch/Caffe2 library for iOS, and can only be performed on macOS. Take the following steps to start the build:

  • Install Xcode from App Store, and configure "Command Line Tools" properly on Xcode.
  • Install the dependencies:
brew install cmake automake libtool
  • run build_ios.sh
#in your PyTorch root directory
bash scripts/build_ios.sh

If succeeded, the libraries and headers would be generated to build_ios/install directory. You can then copy these files to your Xcode project for further usage.