macOS M1平台下编译使用LibTorch
For macOS
Building libTorch using CMake 1
1 | git clone -b master --recurse-submodule https://github.com/pytorch/pytorch.git |
1 | -- |
注意系统已有protobuf
可能会导致编译错误2。
1 | cmake --build . --target install --parallel 20 |
CMake测试程序 3
1 | set(TORCH_SRC /Users/luohanjie/Softwares/pytorch/pytorch-install) |
程序1:
1 |
|
输出:
1 | MPS? 1 |
程序2:
1 | #include <time.h> |
Device | Time |
---|---|
CPU | 15.36 |
MPS | 0.2671 |
生成TorchScript 4 5
A PyTorch model’s journey from Python to C++ is enabled by Torch Script, a representation of a PyTorch model that can be understood, compiled and serialized by the Torch Script compiler.
1 | import torch |
By default, for the CPU backend, optimize_for_mobile performs the following types of optimizations: * Conv2D and BatchNorm fusion which folds Conv2d-BatchNorm2d into Conv2d; * Insert and fold prepacked ops which rewrites the model graph to replace 2D convolutions and linear ops with their prepacked counterparts. * ReLU and hardtanh fusion which rewrites graph by finding ReLU/hardtanh ops and fuses them together. * Dropout removal which removes dropout nodes from this module when training is false. * Conv packed params hoisting which moves convolution packed params to the root module, so that the convolution structs can be deleted. This decreases model size without impacting numerics.
For the Vulkan backend,
optimize_for_mobile
performs the following type of optimization: * Automatic GPU transfer which rewrites the graph so that moving input and output data to and from the GPU becomes part of the model.
Optimization types can be disabled by passing an optimization blocklist as an argument to optimize_for_mobile.
c++中读取TorchScript并Inference
1 | #include <torch/script.h> // One-stop header. |
PyTorch to ONNX
1 | conda install onnx |
1 | import torch |
测试:
1 | import onnx |
For Android
Cross Compiling for Android NDK
修改/Users/luohanjie/Softwares/pytorch/scripts/build_android.sh
:
1 | CMAKE_ARGS+=("-DBUILD_SHARED_LIBS=ON") |
1 | brew install automake libtool |
1 | -- ******** Summary ******** |
Building PyTorch with Vulkan 6
Build PyTorch
PyTorch supports the ability to run model inference on GPUs that
support the Vulkan graphics and compute API. The primary target devices
are mobile GPUs on Android devices. Vulkan backend is not included by
default. The main switch to include Vulkan backend is cmake option
USE_VULKAN
, that can be set by environment variable
USE_VULKAN. To use PyTorch with Vulkan backend, we need to build it from
source with additional settings.
下载Vulkan Sdk,双击并且安装。
卸载方法:
sudo path_to_vulkan_sdk/uninstall.sh
。
1 | vulkaninfo |
Build PyTorch with Vulkan:
1 | conda create --name pytorch_vulkan python=3.10 |
修改CMakeLists.txt
:
1 | option(BUILD_CUSTOM_PROTOBUF "Build and use Caffe2's own protobuf under third_party" OFF) |
修改c10/CMakeLists.txt
:
1 | #add_subdirectory(benchmark) |
1 | export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"} |
1 | import torch |
生成TorchScript
1 | import torch |
If you see the error message: PytorchStreamReader failed locating file bytecode.pkl: file not found (), likely you are using a torch script model that requires the use of the PyTorch JIT interpreter (a version of our PyTorch interpreter that is not as size-efficient). In order to leverage our efficient interpreter, please regenerate the model by running: module._save_for_lite_interpreter(${model_path}).
If bytecode.pkl is missing, likely the model is generated with the api: module.save(${model_psth}).
The api _load_for_lite_interpreter(${model_psth}) can be helpful to validate model with the efficient mobile interpreter.
https://github.com/pytorch/pytorch/blob/master/docs/libtorch.rst↩︎
https://github.com/pytorch/pytorch/issues/64645↩︎
https://pytorch.org/cppdocs/installing.html↩︎
https://pytorch.org/tutorials/advanced/cpp_export.html↩︎
https://pytorch.org/tutorials/recipes/script_optimized.html↩︎
https://pytorch.org/tutorials/prototype/vulkan_workflow.html↩︎