cmake.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env bash
  2. set -euxo pipefail
  3. ./.github/scripts/setup-env.sh
  4. # Activate conda environment
  5. set +x && eval "$($(which conda) shell.bash hook)" && conda deactivate && conda activate ci && set -x
  6. # Setup the OS_TYPE environment variable that should be used for conditions involving the OS below.
  7. case $(uname) in
  8. Linux)
  9. OS_TYPE=linux
  10. ;;
  11. Darwin)
  12. OS_TYPE=macos
  13. ;;
  14. MSYS*)
  15. OS_TYPE=windows
  16. ;;
  17. *)
  18. echo "Unknown OS type:" $(uname)
  19. exit 1
  20. ;;
  21. esac
  22. if [[ $OS_TYPE == macos ]]; then
  23. JOBS=$(sysctl -n hw.logicalcpu)
  24. else
  25. JOBS=$(nproc)
  26. fi
  27. TORCH_PATH=$(python -c "import pathlib, torch; print(pathlib.Path(torch.__path__[0]))")
  28. if [[ $OS_TYPE == windows ]]; then
  29. PACKAGING_DIR="${PWD}/packaging"
  30. export PATH="${TORCH_PATH}/lib:${PATH}"
  31. fi
  32. Torch_DIR="${TORCH_PATH}/share/cmake/Torch"
  33. if [[ "${GPU_ARCH_TYPE}" == "cuda" ]]; then
  34. WITH_CUDA=1
  35. else
  36. WITH_CUDA=0
  37. fi
  38. echo '::group::Prepare CMake builds'
  39. mkdir -p cpp_build
  40. pushd test/tracing/frcnn
  41. python trace_model.py
  42. mkdir -p build
  43. mv fasterrcnn_resnet50_fpn.pt build
  44. popd
  45. pushd examples/cpp/hello_world
  46. python trace_model.py
  47. mkdir -p build
  48. mv resnet18.pt build
  49. popd
  50. # This was only needed for the tracing above
  51. pip uninstall -y torchvision
  52. echo '::endgroup::'
  53. echo '::group::Build and install libtorchvision'
  54. pushd cpp_build
  55. # On macOS, CMake is looking for the library (*.dylib) and the header (*.h) separately. By default, it prefers to load
  56. # the header from other packages that install the library. This easily leads to a mismatch if the library installed
  57. # from conda doesn't have the exact same version. Thus, we need to explicitly set CMAKE_FIND_FRAMEWORK=NEVER to force
  58. # it to not load anything from other installed frameworks. Resources:
  59. # https://stackoverflow.com/questions/36523911/osx-homebrew-cmake-libpng-version-mismatch-issue
  60. # https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_FRAMEWORK.html
  61. cmake .. -DTorch_DIR="${Torch_DIR}" -DWITH_CUDA="${WITH_CUDA}" \
  62. -DCMAKE_PREFIX_PATH="${CONDA_PREFIX}" \
  63. -DCMAKE_FIND_FRAMEWORK=NEVER \
  64. -DCMAKE_INSTALL_PREFIX="${CONDA_PREFIX}"
  65. if [[ $OS_TYPE == windows ]]; then
  66. "${PACKAGING_DIR}/windows/internal/vc_env_helper.bat" "${PACKAGING_DIR}/windows/internal/build_cmake.bat" $JOBS
  67. else
  68. make -j$JOBS
  69. make install
  70. fi
  71. popd
  72. echo '::endgroup::'
  73. echo '::group::Build and run project that uses Faster-RCNN'
  74. pushd test/tracing/frcnn/build
  75. cmake .. -DTorch_DIR="${Torch_DIR}" -DWITH_CUDA="${WITH_CUDA}" \
  76. -DCMAKE_PREFIX_PATH="${CONDA_PREFIX}" \
  77. -DCMAKE_FIND_FRAMEWORK=NEVER
  78. if [[ $OS_TYPE == windows ]]; then
  79. "${PACKAGING_DIR}/windows/internal/vc_env_helper.bat" "${PACKAGING_DIR}/windows/internal/build_frcnn.bat" $JOBS
  80. cd Release
  81. cp ../fasterrcnn_resnet50_fpn.pt .
  82. else
  83. make -j$JOBS
  84. fi
  85. ./test_frcnn_tracing
  86. popd
  87. echo '::endgroup::'
  88. echo '::group::Build and run C++ example'
  89. pushd examples/cpp/hello_world/build
  90. cmake .. -DTorch_DIR="${Torch_DIR}" \
  91. -DCMAKE_PREFIX_PATH="${CONDA_PREFIX}" \
  92. -DCMAKE_FIND_FRAMEWORK=NEVER
  93. if [[ $OS_TYPE == windows ]]; then
  94. "${PACKAGING_DIR}/windows/internal/vc_env_helper.bat" "${PACKAGING_DIR}/windows/internal/build_cpp_example.bat" $JOBS
  95. cd Release
  96. cp ../resnet18.pt .
  97. else
  98. make -j$JOBS
  99. fi
  100. ./hello-world
  101. popd
  102. echo '::endgroup::'