tf 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/bash
  2. set -e
  3. root=$(cd $(dirname ${0})/..; pwd)
  4. build=${root}/build
  5. node_modules=${root}/node_modules
  6. src=${root}/src
  7. third_party=${root}/third_party
  8. tools=${root}/tools
  9. identifier=tensorflow
  10. virtualenv=${third_party}/virtualenv/${identifier}
  11. bold() {
  12. echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)"
  13. }
  14. git_sync () {
  15. mkdir -p "${third_party}"
  16. if [ -d "${third_party}/${1}" ]; then
  17. git -C "${third_party}/${1}" pull --quiet --prune
  18. else
  19. git -C "${third_party}" clone --quiet --recursive ${2}
  20. fi
  21. git -C "${third_party}/${1}" submodule sync --quiet
  22. git -C "${third_party}/${1}" submodule update --quiet --init --recursive
  23. }
  24. clean() {
  25. bold "tf clean"
  26. rm -rf ${virtualenv}
  27. rm -rf ${third_party}/${identifier}
  28. }
  29. sync() {
  30. bold "tf sync"
  31. git_sync tensorflow https://github.com/tensorflow/tensorflow.git
  32. }
  33. install() {
  34. bold "tf install"
  35. case "$(uname)" in
  36. "Linux")
  37. [ -n "$(which protoc)" ] || sudo apt install -y protobuf-compiler libprotoc-dev
  38. ;;
  39. "Darwin")
  40. brew list protobuf > /dev/null 2>&1 || brew install protobuf > /dev/null
  41. ;;
  42. esac
  43. [ -n "$(python3 -m pip list --format columns --disable-pip-version-check | grep -w virtualenv)" ] || python3 -m pip install --force-reinstall --user --quiet virtualenv
  44. [ -d "${virtualenv}" ] || virtualenv --quiet -p python3 ${virtualenv}
  45. source ${virtualenv}/bin/activate
  46. python3 -m pip install --quiet protobuf
  47. deactivate
  48. }
  49. schema() {
  50. bold "tf schema"
  51. ${node_modules}/protobufjs/bin/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 \
  52. ${third_party}/${identifier}/tensorflow/core/protobuf/saved_model.proto \
  53. ${third_party}/${identifier}/tensorflow/core/protobuf/meta_graph.proto \
  54. ${third_party}/${identifier}/tensorflow/core/protobuf/saver.proto \
  55. ${third_party}/${identifier}/tensorflow/core/framework/graph.proto \
  56. ${third_party}/${identifier}/tensorflow/core/framework/op_def.proto \
  57. ${third_party}/${identifier}/tensorflow/core/framework/tensor_shape.proto \
  58. ${third_party}/${identifier}/tensorflow/core/framework/types.proto \
  59. ${third_party}/${identifier}/tensorflow/core/framework/node_def.proto \
  60. ${third_party}/${identifier}/tensorflow/core/framework/versions.proto \
  61. ${third_party}/${identifier}/tensorflow/core/framework/function.proto \
  62. ${third_party}/${identifier}/tensorflow/core/framework/attr_value.proto \
  63. ${third_party}/${identifier}/tensorflow/core/framework/tensor.proto \
  64. ${third_party}/${identifier}/tensorflow/core/framework/variable.proto \
  65. ${third_party}/${identifier}/tensorflow/core/framework/resource_handle.proto \
  66. ${third_party}/${identifier}/tensorflow/core/protobuf/saved_object_graph.proto \
  67. ${third_party}/${identifier}/tensorflow/core/protobuf/trackable_object_graph.proto \
  68. ${third_party}/${identifier}/tensorflow/core/protobuf/struct.proto
  69. }
  70. metadata() {
  71. bold "tf metadata"
  72. source ${virtualenv}/bin/activate
  73. pushd ${tools} > /dev/null
  74. protoc --proto_path ${third_party}/${identifier} ${third_party}/${identifier}/tensorflow/core/framework/attr_value.proto --python_out=${tools}
  75. protoc --proto_path ${third_party}/${identifier} ${third_party}/${identifier}/tensorflow/core/framework/tensor.proto --python_out=${tools}
  76. protoc --proto_path ${third_party}/${identifier} ${third_party}/${identifier}/tensorflow/core/framework/types.proto --python_out=${tools}
  77. protoc --proto_path ${third_party}/${identifier} ${third_party}/${identifier}/tensorflow/core/framework/tensor_shape.proto --python_out=${tools}
  78. protoc --proto_path ${third_party}/${identifier} ${third_party}/${identifier}/tensorflow/core/framework/resource_handle.proto --python_out=${tools}
  79. protoc --proto_path ${third_party}/${identifier} ${third_party}/${identifier}/tensorflow/core/framework/api_def.proto --python_out=${tools}
  80. protoc --proto_path ${third_party}/${identifier} ${third_party}/${identifier}/tensorflow/core/framework/op_def.proto --python_out=${tools}
  81. touch ${tools}/tensorflow/__init__.py
  82. touch ${tools}/tensorflow/core/__init__.py
  83. touch ${tools}/tensorflow/core/framework/__init__.py
  84. python3 tf-script.py metadata
  85. rm -rf ${tools}/tensorflow
  86. popd > /dev/null
  87. deactivate
  88. }
  89. while [ "$#" != 0 ]; do
  90. command="$1" && shift
  91. case "${command}" in
  92. "clean") clean;;
  93. "sync") sync;;
  94. "install") install;;
  95. "schema") schema;;
  96. "metadata") metadata;;
  97. esac
  98. done