Repository navigation
Expand file tree
/
Copy pathonnx
More file actions
executable file
·144 lines (133 loc) · 5.7 KB
/
Copy pathonnx
File metadata and controls
executable file
·144 lines (133 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null
case "${OSTYPE}" in
msys*) python="winpty python";;
*) python="python";;
esac
venv() {
env_dir=./third_party/env/onnx
[ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
case "${OSTYPE}" in
msys*) source ${env_dir}/Scripts/activate;;
*) source ${env_dir}/bin/activate;;
esac
${python} -m pip install --quiet --upgrade pip setuptools wheel
}
clean() {
echo "onnx clean"
rm -rf "./third_party/env/onnx"
rm -rf "./third_party/source/onnx"
rm -rf "./third_party/source/onnxruntime"
}
sync() {
echo "onnx sync"
[ -d "./third_party/source/onnx" ] || git clone --quiet --recursive https://github.com/onnx/onnx.git "./third_party/source/onnx"
pushd "./third_party/source/onnx" > /dev/null
git pull --quiet --prune
git submodule sync --quiet
git submodule update --quiet --init --recursive
popd > /dev/null
mkdir -p "./third_party/source/onnxruntime/core/flatbuffers/schema"
curl --silent --location --output "./third_party/source/onnxruntime/core/flatbuffers/schema/ort.fbs" "https://github.com/microsoft/onnxruntime/blob/master/onnxruntime/core/flatbuffers/schema/ort.fbs?raw=true"
}
install() {
echo "onnx install"
case "${OSTYPE}" in
linux*)
[ -x "$(command -v cmake)" ] || sudo apt install -y cmake
protoc_version=3.16.0
protoc_dir="$(pwd)/third_party/bin/protobuf/v${protoc_version}"
if [ ! -f "${protoc_dir}/bin/protoc" ]; then
rm -rf ${protoc_dir}
git -c advice.detachedHead=false clone --quiet --recursive --depth=1 --branch v${protoc_version} https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
pushd "${protoc_dir}/src/cmake" > /dev/null
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
make -j$(nproc) > /dev/null
make install > /dev/null
popd > /dev/null
fi
protobuf="protobuf==${protoc_version}"
export PATH="${protoc_dir}\bin":${PATH}
;;
darwin*)
[ -x "$(command -v cmake)" ] || brew install cmake > /dev/null
[ -x "$(command -v protoc)" ] || brew install protobuf > /dev/null
protobuf=protobuf
;;
msys*)
protoc_version=3.16.0
protoc_dir="$(pwd)/third_party/bin/protobuf/v${protoc_version}"
programfiles_x86_dir=$(env | grep "^ProgramFiles(x86)=" | cut -d '=' -f 2)
cmake_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
msbuild_dir="${programfiles_x86_dir}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin"
if [ ! -f "${protoc_dir}/bin/protoc.exe" ]; then
rm -rf ${protoc_dir}
git clone --quiet https://github.com/protocolbuffers/protobuf.git ${protoc_dir}/src
git -C "${protoc_dir}/src" checkout v${protoc_version}
pushd "${protoc_dir}/src/cmake" > /dev/null
"${cmake_dir}\cmake.exe" -G "Visual Studio 15 2017 Win64" -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF -DCMAKE_INSTALL_PREFIX="..\.." > /dev/null
"${msbuild_dir}\MSBuild.exe" protobuf.sln //m //p:Configuration=Release > /dev/null
"${msbuild_dir}\MSBuild.exe" INSTALL.vcxproj //p:Configuration=Release > /dev/null
popd > /dev/null
fi
export PATH="${protoc_dir}\bin":"$(cygpath -u "${cmake_dir}")":${PATH}
export USE_MSVC_STATIC_RUNTIME=0
protobuf="protobuf==3.9.2"
;;
esac
venv
${python} -m pip install --quiet --upgrade ${protobuf}
${python} -m pip install --quiet "./third_party/source/onnx"
deactivate
}
schema() {
echo "onnx schema"
[[ $(grep -U $'\x0D' ./source/onnx-proto.js) ]] && crlf=1
node ./tools/protoc.js --text --root onnx --out ./source/onnx-proto.js --path ./third_party/source/onnx onnx/onnx-ml.proto onnx/onnx-operators-ml.proto
if [[ -n ${crlf} ]]; then
unix2dos --quiet --newfile ./source/onnx-proto.js ./source/onnx-proto.js
fi
[[ $(grep -U $'\x0D' ./source/onnx-schema.js) ]] && crlf=1
node ./tools/flatc.js --root ort --out ./source/onnx-schema.js ./third_party/source/onnxruntime/core/flatbuffers/schema/ort.fbs
if [[ -n ${crlf} ]]; then
unix2dos --quiet --newfile ./source/onnx-schema.js ./source/onnx-schema.js
fi
}
metadata() {
echo "onnx metadata"
[[ $(grep -U $'\x0D' ./source/onnx-metadata.json) ]] && crlf=1
venv
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
${python} ./tools/onnx-script.py metadata
deactivate
if [[ -n ${crlf} ]]; then
unix2dos --quiet --newfile ./source/onnx-metadata.json ./source/onnx-metadata.json
fi
}
infer() {
echo "onnx infer"
venv
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
${python} ./tools/onnx-script.py infer ${1}
deactivate
}
optimize() {
echo "onnx optimize"
venv
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
${python} ./tools/onnx-script.py optimize ${1}
deactivate
}
while [ "$#" != 0 ]; do
command="$1" && shift
case "${command}" in
"clean") clean;;
"sync") sync;;
"install") install;;
"schema") schema;;
"metadata") metadata;;
"infer") infer ${1} && shift;;
"optimize") optimize ${1} && shift;;
esac
done