onnx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. set -e
  3. pushd $(cd $(dirname ${0})/..; pwd) > /dev/null
  4. case "${OSTYPE}" in
  5. msys*) python="winpty python";;
  6. *) python="python";;
  7. esac
  8. venv() {
  9. env_dir=./third_party/env/onnx
  10. [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
  11. case "${OSTYPE}" in
  12. msys*) source ${env_dir}/Scripts/activate;;
  13. *) source ${env_dir}/bin/activate;;
  14. esac
  15. ${python} -m pip install --quiet --upgrade pip setuptools wheel
  16. }
  17. clean() {
  18. echo "onnx clean"
  19. rm -rf "./third_party/env/onnx"
  20. rm -rf "./third_party/source/onnx"
  21. rm -rf "./third_party/source/onnxruntime"
  22. }
  23. sync() {
  24. echo "onnx sync"
  25. [ -d "./third_party/source/onnx" ] || git clone --quiet --recursive https://github.com/onnx/onnx.git "./third_party/source/onnx"
  26. pushd "./third_party/source/onnx" > /dev/null
  27. git pull --quiet --prune
  28. git submodule sync --quiet
  29. git submodule update --quiet --init --recursive
  30. popd > /dev/null
  31. mkdir -p "./third_party/source/onnxruntime/core/flatbuffers/schema"
  32. curl --silent --location --output "./third_party/source/onnxruntime/core/flatbuffers/schema/ort.fbs" "https://github.com/microsoft/onnxruntime/raw/main/onnxruntime/core/flatbuffers/schema/ort.fbs"
  33. }
  34. install() {
  35. echo "onnx install"
  36. protoc_version=3.20.2
  37. protobuf="protobuf==${protoc_version}"
  38. protoc_dir="$(pwd)/third_party/bin/protobuf/v${protoc_version}"
  39. case "${OSTYPE}" in
  40. linux*)
  41. [ -x "$(command -v cmake)" ] || sudo apt install -y cmake
  42. if [ ! -f "${protoc_dir}/bin/protoc" ]; then
  43. rm -rf ${protoc_dir}
  44. git clone --quiet -c advice.detachedHead=false --quiet --recursive --depth=1 --branch v${protoc_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  45. pushd "${protoc_dir}/src/cmake" > /dev/null
  46. cmake ../cmake -DCMAKE_CXX_FLAGS="-w" -Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="..\.." -DCMAKE_INSTALL_SYSCONFDIR=/etc -DCMAKE_POSITION_INDEPENDENT_CODE=ON -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release > /dev/null
  47. make -j$(nproc) > /dev/null
  48. make install > /dev/null
  49. popd > /dev/null
  50. fi
  51. export PATH="${protoc_dir}\bin":${PATH}
  52. ;;
  53. darwin*)
  54. [ -x "$(command -v cmake)" ] || brew install cmake > /dev/null
  55. if [ ! -f "${protoc_dir}/bin/protoc" ]; then
  56. rm -rf ${protoc_dir}
  57. git clone --quiet -c advice.detachedHead=false --quiet --recursive --depth=1 --branch v${protoc_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  58. pushd "${protoc_dir}/src/cmake" > /dev/null
  59. cmake ../cmake -DCMAKE_CXX_FLAGS="-w" -Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="..\.." -DCMAKE_INSTALL_SYSCONFDIR=/etc -DCMAKE_POSITION_INDEPENDENT_CODE=ON -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release > /dev/null
  60. make > /dev/null
  61. make install > /dev/null
  62. popd > /dev/null
  63. fi
  64. export PATH="${protoc_dir}\bin":${PATH}
  65. ;;
  66. msys*)
  67. # winget install -e --id=Microsoft.VisualStudio.2019.BuildTools --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended"
  68. programfiles_x86_dir=$(env | grep "^ProgramFiles(x86)=" | cut -d '=' -f 2)
  69. build_tools_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2019\BuildTools"
  70. cmake_dir="${build_tools_dir}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
  71. msbuild_dir="${build_tools_dir}\MSBuild\Current\Bin"
  72. if [ ! -f "${protoc_dir}/bin/protoc.exe" ]; then
  73. rm -rf ${protoc_dir}
  74. git clone --quiet -c advice.detachedHead=false -b v${protoc_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  75. pushd "${protoc_dir}/src/cmake" > /dev/null
  76. "${cmake_dir}\cmake.exe" -G "Visual Studio 16 2019" -A x64 -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF -DCMAKE_INSTALL_PREFIX="..\.." > /dev/null
  77. "${msbuild_dir}\MSBuild.exe" protobuf.sln //m //p:Configuration=Release > /dev/null
  78. "${msbuild_dir}\MSBuild.exe" INSTALL.vcxproj //p:Configuration=Release > /dev/null
  79. popd > /dev/null
  80. fi
  81. export PATH="${protoc_dir}\bin":"$(cygpath -u "${cmake_dir}")":${PATH}
  82. export USE_MSVC_STATIC_RUNTIME=0
  83. ;;
  84. esac
  85. venv
  86. ${python} -m pip install --quiet --upgrade ${protobuf} numpy
  87. ${python} -m pip install --quiet "./third_party/source/onnx"
  88. deactivate
  89. }
  90. schema() {
  91. echo "onnx schema"
  92. [[ $(grep -U $'\x0D' ./source/onnx-proto.js) ]] && crlf=1
  93. node ./tools/protoc.js --text --root onnx --out ./source/onnx-proto.js --path ./third_party/source/onnx onnx/onnx-ml.proto onnx/onnx-operators-ml.proto
  94. if [[ -n ${crlf} ]]; then
  95. unix2dos --quiet --newfile ./source/onnx-proto.js ./source/onnx-proto.js
  96. fi
  97. [[ $(grep -U $'\x0D' ./source/onnx-schema.js) ]] && crlf=1
  98. node ./tools/flatc.js --root ort --out ./source/onnx-schema.js ./third_party/source/onnxruntime/core/flatbuffers/schema/ort.fbs
  99. if [[ -n ${crlf} ]]; then
  100. unix2dos --quiet --newfile ./source/onnx-schema.js ./source/onnx-schema.js
  101. fi
  102. }
  103. metadata() {
  104. echo "onnx metadata"
  105. [[ $(grep -U $'\x0D' ./source/onnx-metadata.json) ]] && crlf=1
  106. venv
  107. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  108. ${python} ./tools/onnx_metadata.py
  109. deactivate
  110. if [[ -n ${crlf} ]]; then
  111. unix2dos --quiet --newfile ./source/onnx-metadata.json ./source/onnx-metadata.json
  112. fi
  113. }
  114. while [ "$#" != 0 ]; do
  115. command="$1" && shift
  116. case "${command}" in
  117. "clean") clean;;
  118. "sync") sync;;
  119. "install") install;;
  120. "schema") schema;;
  121. "metadata") metadata;;
  122. esac
  123. done