tf 4.7 KB

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