2
0

onnx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. set -e
  3. pushd $(cd $(dirname ${0})/..; pwd) > /dev/null
  4. identifier=onnx
  5. case "${OSTYPE}" in
  6. msys*) python="winpty python";;
  7. *) python=python3;;
  8. esac
  9. bold() {
  10. echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)"
  11. }
  12. venv() {
  13. env_dir=./third_party/env/onnx
  14. [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
  15. case "${OSTYPE}" in
  16. msys*) source ${env_dir}/Scripts/activate;;
  17. *) source ${env_dir}/bin/activate;;
  18. esac
  19. ${python} -m pip install --quiet --upgrade pip
  20. }
  21. git_sync() {
  22. [ -d "./third_party/src/${1}" ] || git clone --quiet --recursive ${2} "./third_party/src/${1}"
  23. pushd "./third_party/src/${1}" > /dev/null
  24. git pull --quiet --prune
  25. git submodule sync --quiet
  26. git submodule update --quiet --init --recursive
  27. popd > /dev/null
  28. }
  29. clean() {
  30. bold "onnx clean"
  31. rm -rf "./third_party/env/onnx"
  32. rm -rf "./third_party/src/onnx"
  33. rm -rf "./third_party/src/onnxmltools"
  34. }
  35. sync() {
  36. bold "onnx sync"
  37. git_sync onnx https://github.com/onnx/onnx.git
  38. git_sync onnxmltools https://github.com/onnx/onnxmltools.git
  39. }
  40. install() {
  41. bold "onnx install"
  42. case "${OSTYPE}" in
  43. linux*)
  44. [ -x "$(command -v cmake)" ] || sudo apt install -y cmake
  45. [ -x "$(command -v protoc)" ] || sudo apt install -y protobuf-compiler libprotoc-dev
  46. protobuf=protobuf
  47. ;;
  48. darwin*)
  49. brew list cmake > /dev/null 2>&1 || brew install cmake > /dev/null
  50. brew list protobuf > /dev/null 2>&1 || brew install protobuf > /dev/null
  51. protobuf=protobuf
  52. ;;
  53. msys*)
  54. [ ! -z "$(choco list --local-only --exacty --limit-output visualstudio2017-workload-vctools)" ] || $(choco install -yes visualstudio2017-workload-vctools) > /dev/null
  55. protoc_version=3.9.x
  56. protoc_dir="$(pwd)/third_party/bin/protobuf/v${protoc_version}"
  57. programfiles_x86_dir=$(env | grep "^ProgramFiles(x86)=" | cut -d '=' -f 2)
  58. cmake_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
  59. msbuild_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin"
  60. if [ ! -f "${protoc_dir}/bin/protoc.exe" ]; then
  61. rm -rf ${protoc_dir}
  62. git clone --quiet --branch ${protoc_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  63. pushd "${protoc_dir}/src/cmake" > /dev/null
  64. "${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
  65. "${msbuild_dir}\MSBuild.exe" protobuf.sln //m //p:Configuration=Release > /dev/null
  66. "${msbuild_dir}\MSBuild.exe" INSTALL.vcxproj //p:Configuration=Release > /dev/null
  67. popd > /dev/null
  68. fi
  69. export PATH="${protoc_dir}\bin":"$(cygpath -u "${cmake_dir}")":${PATH}
  70. export USE_MSVC_STATIC_RUNTIME=0
  71. protobuf="protobuf==3.9.2"
  72. ;;
  73. esac
  74. venv
  75. ${python} -m pip install --quiet --upgrade ${protobuf}
  76. export ONNX_ML=1
  77. export ONNX_NAMESPACE=onnx
  78. ${python} -m pip install --quiet "./third_party/src/onnx"
  79. deactivate
  80. }
  81. schema() {
  82. bold "onnx schema"
  83. [[ $(grep -U $'\x0D' ./src/onnx-proto.js) ]] && crlf=1
  84. npx pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case --decode-text -r onnx -o ./src/onnx-proto.js ./third_party/src/onnx/onnx/onnx-ml.proto ./third_party/src/onnx/onnx/onnx-operators-ml.proto
  85. node ./tools/update_pbjs.js array ./src/onnx-proto.js float_data float 1
  86. node ./tools/update_pbjs.js array ./src/onnx-proto.js double_data double 1
  87. if [[ -n ${crlf} ]]; then
  88. unix2dos --quiet --newfile ./src/onnx-proto.js ./src/onnx-proto.js
  89. fi
  90. }
  91. metadata() {
  92. bold "onnx metadata"
  93. [[ $(grep -U $'\x0D' ./src/onnx-metadata.json) ]] && crlf=1
  94. venv
  95. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  96. ${python} ./tools/onnx-script.py metadata
  97. deactivate
  98. if [[ -n ${crlf} ]]; then
  99. unix2dos --quiet --newfile ./src/onnx-metadata.json ./src/onnx-metadata.json
  100. fi
  101. }
  102. convert() {
  103. bold "onnx convert"
  104. venv
  105. ${python} -m pip install --quiet ./third_party/src/onnxmltools
  106. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  107. ${python} ./tools/onnx-script.py convert ${1}
  108. deactivate
  109. }
  110. infer() {
  111. bold "onnx infer"
  112. venv
  113. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  114. ${python} ./tools/onnx-script.py infer ${1}
  115. deactivate
  116. }
  117. optimize() {
  118. bold "onnx optimize"
  119. venv
  120. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  121. ${python} ./tools/onnx-script.py optimize ${1}
  122. deactivate
  123. }
  124. while [ "$#" != 0 ]; do
  125. command="$1" && shift
  126. case "${command}" in
  127. "clean") clean;;
  128. "sync") sync;;
  129. "install") install;;
  130. "schema") schema;;
  131. "metadata") metadata;;
  132. "convert") convert ${1} && shift;;
  133. "infer") infer ${1} && shift;;
  134. "optimize") optimize ${1} && shift;;
  135. esac
  136. done