onnx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if [ ! -d "./third_party/source/onnx" ]; then
  26. git clone --quiet --recursive https://github.com/onnx/onnx.git "./third_party/source/onnx"
  27. fi
  28. pushd "./third_party/source/onnx" > /dev/null
  29. git pull --quiet --prune
  30. git submodule sync --quiet
  31. git submodule update --quiet --init --recursive
  32. popd > /dev/null
  33. mkdir -p "./third_party/source/onnxruntime/core/flatbuffers/schema"
  34. curl --silent --show-error --location --output "./third_party/source/onnxruntime/core/flatbuffers/schema/ort.fbs" "https://github.com/microsoft/onnxruntime/raw/main/onnxruntime/core/flatbuffers/schema/ort.fbs"
  35. }
  36. install() {
  37. echo "onnx install"
  38. venv
  39. protobuf_version=$(grep "protobuf" ./third_party/source/onnx/requirements.txt | cut -d'=' -f2)
  40. protoc_dir="$(pwd)/third_party/bin/protobuf/v${protobuf_version}"
  41. case "${OSTYPE}" in
  42. # linux*)
  43. # [ -x "$(command -v cmake)" ] || sudo apt install -y cmake
  44. # if [ ! -f "${protoc_dir}/bin/protoc" ]; then
  45. # rm -rf ${protoc_dir}
  46. # git clone --quiet -c advice.detachedHead=false --quiet --recursive --depth=1 --branch v${protobuf_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  47. # pushd "${protoc_dir}/src/cmake" > /dev/null
  48. # 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
  49. # make -j$(nproc) > /dev/null
  50. # make install > /dev/null
  51. # popd > /dev/null
  52. # fi
  53. # export PATH="${protoc_dir}\bin":${PATH}
  54. # ;;
  55. # darwin*)
  56. # [ -x "$(command -v cmake)" ] || brew install cmake > /dev/null
  57. # if [ ! -f "${protoc_dir}/bin/protoc" ]; then
  58. # rm -rf ${protoc_dir}
  59. # git clone --quiet -c advice.detachedHead=false --quiet --recursive --depth=1 --branch v${protobuf_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  60. # pushd "${protoc_dir}/src/cmake" > /dev/null
  61. # 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
  62. # make > /dev/null
  63. # make install > /dev/null
  64. # popd > /dev/null
  65. # fi
  66. # export PATH="${protoc_dir}\bin":${PATH}
  67. # ;;
  68. msys*)
  69. # winget install -e --id=Microsoft.VisualStudio.2019.BuildTools --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended"
  70. programfiles_x86_dir=$(env | grep "^ProgramFiles(x86)=" | cut -d '=' -f 2)
  71. build_tools_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2019\BuildTools"
  72. cmake_dir="${build_tools_dir}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
  73. protobuf="protobuf==${protobuf_version}"
  74. ${python} -m pip install --quiet --upgrade ${protobuf}
  75. export PATH="$(cygpath -u "${cmake_dir}")":${PATH}
  76. msbuild_dir="${build_tools_dir}\MSBuild\Current\Bin"
  77. if [ ! -f "${protoc_dir}/bin/protoc.exe" ]; then
  78. rm -rf ${protoc_dir}
  79. git clone --quiet -c advice.detachedHead=false -b v${protobuf_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
  80. pushd "${protoc_dir}/src/cmake" > /dev/null
  81. "${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
  82. "${msbuild_dir}\MSBuild.exe" protobuf.sln //m //p:Configuration=Release > /dev/null
  83. "${msbuild_dir}\MSBuild.exe" INSTALL.vcxproj //p:Configuration=Release > /dev/null
  84. popd > /dev/null
  85. fi
  86. export PATH="${protoc_dir}\bin":${PATH}
  87. export USE_MSVC_STATIC_RUNTIME=0
  88. ;;
  89. esac
  90. ${python} -m pip install --quiet "./third_party/source/onnx"
  91. ${python} -m pip install --quiet --upgrade flatbuffers numpy packaging protobuf sympy
  92. ${python} -m pip install --quiet --upgrade onnxruntime --pre --extra-index https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/
  93. deactivate
  94. }
  95. schema() {
  96. echo "onnx schema"
  97. [[ $(grep -U $'\x0D' ./source/onnx-proto.js) ]] && crlf=1
  98. node ./tools/protoc.js --binary --text --json --root onnx --out ./source/onnx-proto.js --path ./third_party/source/onnx onnx/onnx-ml.proto onnx/onnx-operators-ml.proto
  99. if [[ -n ${crlf} ]]; then
  100. unix2dos --quiet --newfile ./source/onnx-proto.js ./source/onnx-proto.js
  101. fi
  102. [[ $(grep -U $'\x0D' ./source/onnx-schema.js) ]] && crlf=1
  103. node ./tools/flatc.js --root ort --out ./source/onnx-schema.js ./third_party/source/onnxruntime/core/flatbuffers/schema/ort.fbs
  104. if [[ -n ${crlf} ]]; then
  105. unix2dos --quiet --newfile ./source/onnx-schema.js ./source/onnx-schema.js
  106. fi
  107. }
  108. metadata() {
  109. echo "onnx metadata"
  110. [[ $(grep -U $'\x0D' ./source/onnx-metadata.json) ]] && crlf=1
  111. venv
  112. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  113. ${python} ./tools/onnx_script.py
  114. deactivate
  115. if [[ -n ${crlf} ]]; then
  116. unix2dos --quiet --newfile ./source/onnx-metadata.json ./source/onnx-metadata.json
  117. fi
  118. }
  119. while [ "$#" != 0 ]; do
  120. command="$1" && shift
  121. case "${command}" in
  122. "clean") clean;;
  123. "sync") sync;;
  124. "install") install;;
  125. "schema") schema;;
  126. "metadata") metadata;;
  127. esac
  128. done