This CMakeLists.txt manages the building of a minimal PyTorch library from PyTorch 1.7.0 by setting environment variables and calling pytorch/setup.py
- Clone PyTorch 1.7.0 and adjust the CMakeLists.txt variable
PYTORCH_SRC_DIRto point to the local repository, for exampleset(PYTORCH_SRC_DIR ../pytorch) - Install the PyTorch prerequisites
Equivalent bash script inspired by C++ development tips
cd pytorch
export DEBUG=1
export BUILD_TEST=0
export USE_CUDA=0
export USE_DISTRIBUTED=0
export USE_MKLDNN=0
export USE_FBGEMM=0
export USE_NNPACK=0
export USE_QNNPACK=0
export USE_XNNPACK=0
export BUILD_CAFFE2_OPS=0
[see below for minor edits to PyTorch tree]
python setup.py build --cmake-only
cmake --build build
cmake -S . -B build
CMakeLists.txt adjusts the following in ${PYTORCH_SRC_DIR}/CMakeLists.txt
-Og enables optimizations that do not interfere with debugging. This replaces -O2 in CMAKE_CXX_FLAGS
CMakeLists.txt adjusts the following in ${PYTORCH_SRC_DIR}/cmake/Dependencies.cmake
This avoids the following error -
CMake Error at cmake/Dependencies.cmake:926 (if):
if given arguments:
"VERSION_LESS" "3"
Unknown arguments specified
The environmental variable DEBUG=1, sets CMAKE_BUILD_TYPE=Debug to build libraries with debug symbols
The build generates shared libraries by default. This can be disabled by setting BUILD_SHARED_LIBS=OFF
cmake --build ${PYTORCH_SRC_DIR}/build
cmake ---S . -B build
cmake --build ../pytorch/build
pytorch/build/lib
rm build/CMakeCache.txt
rm -rf build
del build/CMakeCache.txt
rmdir /s /q build
mklink /d build d:\build
git clean -dfx