onnx 6.6 KB

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