tf 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. set -e
  3. pushd $(cd $(dirname ${0})/..; pwd) > /dev/null
  4. [[ "$(python3 --version 2> /dev/null)" =~ "Python 3" ]] && python=python3 || python=python
  5. bold() {
  6. echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)"
  7. }
  8. venv() {
  9. venv_dir=./third_party/venv/tensorflow
  10. [ -d "${venv_dir}" ] || ${python} -m venv ${venv_dir}
  11. case "${OSTYPE}" in
  12. msys*) source ${venv_dir}/Scripts/activate;;
  13. *) source ${venv_dir}/bin/activate;;
  14. esac
  15. ${python} -m pip install --quiet --upgrade pip
  16. }
  17. clean() {
  18. bold "tf clean"
  19. rm -rf "./third_party/venv/tensorflow"
  20. rm -rf "./third_party/tensorflow"
  21. }
  22. sync() {
  23. bold "tf sync"
  24. mkdir -p "./third_party"
  25. if [ -d "./third_party/tensorflow" ]; then
  26. git -C "./third_party/tensorflow" pull --quiet --prune
  27. else
  28. git -C "./third_party" clone --quiet --recursive https://github.com/tensorflow/tensorflow.git
  29. fi
  30. git -C "./third_party/tensorflow" submodule sync --quiet
  31. git -C "./third_party/tensorflow" submodule update --quiet --init --recursive
  32. }
  33. install() {
  34. bold "tf install"
  35. venv
  36. ${python} -m pip install --quiet --upgrade protobuf
  37. deactivate
  38. }
  39. schema() {
  40. bold "tf schema"
  41. [[ $(grep -U $'\x0D' ./src/tf-proto.js) ]] && crlf=1
  42. npx pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case --decode-text -r tf -o ./src/tf-proto.js \
  43. ./third_party/tensorflow/tensorflow/core/protobuf/saved_model.proto \
  44. ./third_party/tensorflow/tensorflow/core/protobuf/meta_graph.proto \
  45. ./third_party/tensorflow/tensorflow/core/protobuf/saver.proto \
  46. ./third_party/tensorflow/tensorflow/core/framework/graph.proto \
  47. ./third_party/tensorflow/tensorflow/core/framework/op_def.proto \
  48. ./third_party/tensorflow/tensorflow/core/framework/tensor_shape.proto \
  49. ./third_party/tensorflow/tensorflow/core/framework/types.proto \
  50. ./third_party/tensorflow/tensorflow/core/framework/node_def.proto \
  51. ./third_party/tensorflow/tensorflow/core/framework/versions.proto \
  52. ./third_party/tensorflow/tensorflow/core/framework/function.proto \
  53. ./third_party/tensorflow/tensorflow/core/framework/attr_value.proto \
  54. ./third_party/tensorflow/tensorflow/core/framework/tensor.proto \
  55. ./third_party/tensorflow/tensorflow/core/framework/variable.proto \
  56. ./third_party/tensorflow/tensorflow/core/framework/resource_handle.proto \
  57. ./third_party/tensorflow/tensorflow/core/protobuf/saved_object_graph.proto \
  58. ./third_party/tensorflow/tensorflow/core/protobuf/trackable_object_graph.proto \
  59. ./third_party/tensorflow/tensorflow/core/protobuf/struct.proto \
  60. ./third_party/tensorflow/tensorflow/core/protobuf/tensor_bundle.proto \
  61. ./third_party/tensorflow/tensorflow/core/framework/tensor_slice.proto
  62. if [[ -n ${crlf} ]]; then
  63. unix2dos --quiet --newfile ./src/tf-proto.js ./src/tf-proto.js
  64. fi
  65. }
  66. metadata() {
  67. bold "tf metadata"
  68. [[ $(grep -U $'\x0D' ./src/tf-metadata.json) ]] && crlf=1
  69. venv
  70. case "${OSTYPE}" in
  71. linux*)
  72. [ -n "$(which protoc)" ] || sudo apt install -y protobuf-compiler libprotoc-dev
  73. protoc=protoc
  74. ;;
  75. darwin*)
  76. brew list protobuf > /dev/null 2>&1 || brew install protobuf > /dev/null
  77. protoc=protoc
  78. ;;
  79. msys*)
  80. protoc_version=$(curl -s https://api.github.com/repos/protocolbuffers/protobuf/releases/latest | grep tag_name | cut -f 2 -d : | cut -f 2 -d '"' | cut -f 2 -d v)
  81. protoc_dir=$(dirname $(mktemp -u))/protobuf/v${protoc_version}
  82. if [ ! -f "${protoc_dir}/bin/protoc.exe" ]; then
  83. mkdir -p "${protoc_dir}"
  84. pushd "${protoc_dir}" > /dev/null
  85. curl -sL -O https://github.com/protocolbuffers/protobuf/releases/download/v${protoc_version}/protoc-${protoc_version}-win32.zip
  86. unzip protoc-${protoc_version}-win32.zip > /dev/null
  87. popd > /dev/null
  88. fi
  89. protoc=${protoc_dir}/bin/protoc
  90. ;;
  91. esac
  92. ${protoc} --proto_path ./third_party/tensorflow ./third_party/tensorflow/tensorflow/core/framework/attr_value.proto --python_out=./tools
  93. ${protoc} --proto_path ./third_party/tensorflow ./third_party/tensorflow/tensorflow/core/framework/tensor.proto --python_out=./tools
  94. ${protoc} --proto_path ./third_party/tensorflow ./third_party/tensorflow/tensorflow/core/framework/types.proto --python_out=./tools
  95. ${protoc} --proto_path ./third_party/tensorflow ./third_party/tensorflow/tensorflow/core/framework/tensor_shape.proto --python_out=./tools
  96. ${protoc} --proto_path ./third_party/tensorflow ./third_party/tensorflow/tensorflow/core/framework/resource_handle.proto --python_out=./tools
  97. ${protoc} --proto_path ./third_party/tensorflow ./third_party/tensorflow/tensorflow/core/framework/api_def.proto --python_out=./tools
  98. ${protoc} --proto_path ./third_party/tensorflow ./third_party/tensorflow/tensorflow/core/framework/op_def.proto --python_out=./tools
  99. touch ./tools/tensorflow/__init__.py
  100. touch ./tools/tensorflow/core/__init__.py
  101. touch ./tools/tensorflow/core/framework/__init__.py
  102. ${python} ./tools/tf-script.py metadata
  103. rm -rf ./tools/tensorflow
  104. deactivate
  105. if [[ -n ${crlf} ]]; then
  106. unix2dos --quiet --newfile ./src/tf-metadata.json ./src/tf-metadata.json
  107. fi
  108. }
  109. while [ "$#" != 0 ]; do
  110. command="$1" && shift
  111. case "${command}" in
  112. "clean") clean;;
  113. "sync") sync;;
  114. "install") install;;
  115. "schema") schema;;
  116. "metadata") metadata;;
  117. esac
  118. done