2
0

onnx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/blob/main/onnxruntime/core/flatbuffers/schema/ort.fbs?raw=true"
  33. }
  34. install() {
  35. echo "onnx install"
  36. case "${OSTYPE}" in
  37. linux*)
  38. [ -x "$(command -v cmake)" ] || sudo apt install -y cmake
  39. protoc_version=3.16.0
  40. protoc_dir="$(pwd)/third_party/bin/protobuf/v${protoc_version}"
  41. if [ ! -f "${protoc_dir}/bin/protoc" ]; then
  42. rm -rf ${protoc_dir}
  43. git -c advice.detachedHead=false clone --quiet --recursive --depth=1 --branch v${protoc_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  44. pushd "${protoc_dir}/src/cmake" > /dev/null
  45. 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
  46. make -j$(nproc) > /dev/null
  47. make install > /dev/null
  48. popd > /dev/null
  49. fi
  50. protobuf="protobuf==${protoc_version}"
  51. export PATH="${protoc_dir}\bin":${PATH}
  52. ;;
  53. darwin*)
  54. [ -x "$(command -v cmake)" ] || brew install cmake > /dev/null
  55. [ -x "$(command -v protoc)" ] || brew install protobuf > /dev/null
  56. protobuf="protobuf==3.20.1"
  57. ;;
  58. msys*)
  59. protoc_version=3.16.0
  60. protoc_dir="$(pwd)/third_party/bin/protobuf/v${protoc_version}"
  61. programfiles_x86_dir=$(env | grep "^ProgramFiles(x86)=" | cut -d '=' -f 2)
  62. cmake_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
  63. msbuild_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin"
  64. if [ ! -f "${protoc_dir}/bin/protoc.exe" ]; then
  65. rm -rf ${protoc_dir}
  66. git clone --quiet https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  67. git -C "${protoc_dir}/src" checkout v${protoc_version}
  68. pushd "${protoc_dir}/src/cmake" > /dev/null
  69. "${cmake_dir}\cmake.exe" -G "Visual Studio 15 2017 Win64" -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF -DCMAKE_INSTALL_PREFIX="..\.." > /dev/null
  70. "${msbuild_dir}\MSBuild.exe" protobuf.sln //m //p:Configuration=Release > /dev/null
  71. "${msbuild_dir}\MSBuild.exe" INSTALL.vcxproj //p:Configuration=Release > /dev/null
  72. popd > /dev/null
  73. fi
  74. export PATH="${protoc_dir}\bin":"$(cygpath -u "${cmake_dir}")":${PATH}
  75. export USE_MSVC_STATIC_RUNTIME=0
  76. protobuf="protobuf==3.9.2"
  77. ;;
  78. esac
  79. venv
  80. ${python} -m pip install --quiet --upgrade ${protobuf}
  81. ${python} -m pip install --quiet "./third_party/source/onnx"
  82. deactivate
  83. }
  84. schema() {
  85. echo "onnx schema"
  86. [[ $(grep -U $'\x0D' ./source/onnx-proto.js) ]] && crlf=1
  87. 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
  88. if [[ -n ${crlf} ]]; then
  89. unix2dos --quiet --newfile ./source/onnx-proto.js ./source/onnx-proto.js
  90. fi
  91. [[ $(grep -U $'\x0D' ./source/onnx-schema.js) ]] && crlf=1
  92. node ./tools/flatc.js --root ort --out ./source/onnx-schema.js ./third_party/source/onnxruntime/core/flatbuffers/schema/ort.fbs
  93. if [[ -n ${crlf} ]]; then
  94. unix2dos --quiet --newfile ./source/onnx-schema.js ./source/onnx-schema.js
  95. fi
  96. }
  97. metadata() {
  98. echo "onnx metadata"
  99. [[ $(grep -U $'\x0D' ./source/onnx-metadata.json) ]] && crlf=1
  100. venv
  101. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  102. ${python} ./tools/onnx_script.py metadata
  103. deactivate
  104. if [[ -n ${crlf} ]]; then
  105. unix2dos --quiet --newfile ./source/onnx-metadata.json ./source/onnx-metadata.json
  106. fi
  107. }
  108. infer() {
  109. echo "onnx infer"
  110. venv
  111. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  112. ${python} ./tools/onnx_script.py infer ${1}
  113. deactivate
  114. }
  115. optimize() {
  116. echo "onnx optimize"
  117. venv
  118. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  119. ${python} ./tools/onnx_script.py optimize ${1}
  120. deactivate
  121. }
  122. while [ "$#" != 0 ]; do
  123. command="$1" && shift
  124. case "${command}" in
  125. "clean") clean;;
  126. "sync") sync;;
  127. "install") install;;
  128. "schema") schema;;
  129. "metadata") metadata;;
  130. "infer") infer ${1} && shift;;
  131. "optimize") optimize ${1} && shift;;
  132. esac
  133. done