onnx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. venv_dir=./third_party/venv/onnx
  14. [ -d "${venv_dir}" ] || ${python} -m venv ${venv_dir}
  15. case "${OSTYPE}" in
  16. msys*) source ${venv_dir}/Scripts/activate;;
  17. *) source ${venv_dir}/bin/activate;;
  18. esac
  19. ${python} -m pip install --quiet --upgrade pip
  20. }
  21. git_sync() {
  22. mkdir -p "./third_party"
  23. if [ -d "./third_party/${1}" ]; then
  24. git -C "./third_party/${1}" pull --quiet --prune
  25. else
  26. git -C "./third_party" clone --quiet --recursive ${2}
  27. fi
  28. git -C "./third_party/${1}" submodule sync --quiet
  29. git -C "./third_party/${1}" submodule update --quiet --init --recursive
  30. }
  31. clean() {
  32. bold "onnx clean"
  33. rm -rf ./third_party/venv/onnx
  34. rm -rf ./third_party/onnx
  35. rm -rf ./third_party/onnxmltools
  36. }
  37. sync() {
  38. bold "onnx sync"
  39. git_sync onnx https://github.com/onnx/onnx.git
  40. git_sync onnxmltools https://github.com/onnx/onnxmltools.git
  41. }
  42. install() {
  43. bold "onnx install"
  44. case "${OSTYPE}" in
  45. linux*)
  46. [ -n "$(which cmake)" ] || sudo apt install -y cmake
  47. [ -n "$(which protoc)" ] || sudo apt install -y protobuf-compiler libprotoc-dev
  48. protobuf=protobuf
  49. ;;
  50. darwin*)
  51. brew list cmake > /dev/null 2>&1 || brew install cmake > /dev/null
  52. brew list [email protected] > /dev/null 2>&1 || brew install [email protected] > /dev/null
  53. export PATH=/usr/local/opt/[email protected]/bin:${PATH}
  54. protobuf="protobuf==3.7.1"
  55. ;;
  56. esac
  57. venv
  58. ${python} -m pip install --quiet --upgrade ${protobuf}
  59. export ONNX_ML=1
  60. export ONNX_NAMESPACE=onnx
  61. ${python} -m pip install --quiet ./third_party/onnx
  62. deactivate
  63. }
  64. schema() {
  65. bold "onnx schema"
  66. [[ $(grep -U $'\x0D' ./src/onnx-proto.js) ]] && crlf=1
  67. 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/onnx/onnx/onnx-ml.proto ./third_party/onnx/onnx/onnx-operators-ml.proto
  68. node ./tools/update_pbjs.js array ./src/onnx-proto.js float_data float 1
  69. node ./tools/update_pbjs.js array ./src/onnx-proto.js double_data double 1
  70. if [[ -n ${crlf} ]]; then
  71. unix2dos --quiet --newfile ./src/onnx-proto.js ./src/onnx-proto.js
  72. fi
  73. }
  74. metadata() {
  75. bold "onnx metadata"
  76. [[ $(grep -U $'\x0D' ./src/onnx-metadata.json) ]] && crlf=1
  77. venv
  78. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  79. ${python} ./tools/onnx-script.py metadata
  80. deactivate
  81. if [[ -n ${crlf} ]]; then
  82. unix2dos --quiet --newfile ./src/onnx-metadata.json ./src/onnx-metadata.json
  83. fi
  84. }
  85. convert() {
  86. bold "onnx convert"
  87. venv
  88. ${python} -m pip install --quiet ./third_party/onnxmltools
  89. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  90. ${python} ./tools/onnx-script.py convert ${1}
  91. deactivate
  92. }
  93. infer() {
  94. bold "onnx infer"
  95. venv
  96. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  97. ${python} ./tools/onnx-script.py infer ${1}
  98. deactivate
  99. }
  100. optimize() {
  101. bold "onnx optimize"
  102. venv
  103. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  104. ${python} ./tools/onnx-script.py optimize ${1}
  105. deactivate
  106. }
  107. while [ "$#" != 0 ]; do
  108. command="$1" && shift
  109. case "${command}" in
  110. "clean") clean;;
  111. "sync") sync;;
  112. "install") install;;
  113. "schema") schema;;
  114. "metadata") metadata;;
  115. "convert") convert ${1} && shift;;
  116. "infer") infer ${1} && shift;;
  117. "optimize") optimize ${1} && shift;;
  118. esac
  119. done