onnx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "$(uname)" 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. ;;
  49. "Darwin")
  50. brew list cmake > /dev/null 2>&1 || brew install cmake > /dev/null
  51. brew list protobuf > /dev/null 2>&1 || brew install protobuf > /dev/null
  52. ;;
  53. esac
  54. venv
  55. ${python} -m pip install --quiet --upgrade protobuf
  56. export ONNX_ML=1
  57. export ONNX_NAMESPACE=onnx
  58. ${python} -m pip install --quiet ./third_party/onnx
  59. deactivate
  60. }
  61. schema() {
  62. bold "onnx schema"
  63. [[ $(grep -U $'\x0D' ./src/onnx-proto.js) ]] && crlf=1
  64. 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
  65. node ./tools/update_pbjs.js array ./src/onnx-proto.js float_data float 1
  66. node ./tools/update_pbjs.js array ./src/onnx-proto.js double_data double 1
  67. if [[ -n ${crlf} ]]; then
  68. unix2dos --quiet --newfile ./src/onnx-proto.js ./src/onnx-proto.js
  69. fi
  70. }
  71. metadata() {
  72. bold "onnx metadata"
  73. [[ $(grep -U $'\x0D' ./src/onnx-metadata.json) ]] && crlf=1
  74. venv
  75. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  76. ${python} ./tools/onnx-script.py metadata
  77. deactivate
  78. if [[ -n ${crlf} ]]; then
  79. unix2dos --quiet --newfile ./src/onnx-metadata.json ./src/onnx-metadata.json
  80. fi
  81. }
  82. convert() {
  83. bold "onnx convert"
  84. venv
  85. ${python} -m pip install --quiet ./third_party/onnxmltools
  86. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  87. ${python} ./tools/onnx-script.py convert ${1}
  88. deactivate
  89. }
  90. infer() {
  91. bold "onnx infer"
  92. venv
  93. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  94. ${python} ./tools/onnx-script.py infer ${1}
  95. deactivate
  96. }
  97. optimize() {
  98. bold "onnx optimize"
  99. venv
  100. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  101. ${python} ./tools/onnx-script.py optimize ${1}
  102. deactivate
  103. }
  104. while [ "$#" != 0 ]; do
  105. command="$1" && shift
  106. case "${command}" in
  107. "clean") clean;;
  108. "sync") sync;;
  109. "install") install;;
  110. "schema") schema;;
  111. "metadata") metadata;;
  112. "convert") convert ${1} && shift;;
  113. "infer") infer ${1} && shift;;
  114. "optimize") optimize ${1} && shift;;
  115. esac
  116. done