onnx 6.6 KB

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