pytorch 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. set -e
  3. pushd $(cd $(dirname ${0})/..; pwd) > /dev/null
  4. case "${OSTYPE}" in
  5. msys*) python="winpty python";;
  6. *) python="python";;
  7. esac
  8. venv() {
  9. env_dir=./third_party/env/pytorch
  10. [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
  11. case "${OSTYPE}" in
  12. msys*) source ${env_dir}/Scripts/activate;;
  13. *) source ${env_dir}/bin/activate;;
  14. esac
  15. ${python} -m pip install --quiet --upgrade pip requests
  16. }
  17. clean() {
  18. echo "pytorch clean"
  19. rm -rf "./third_party/source/caffe2"
  20. rm -rf "./third_party/source/pytorch"
  21. }
  22. sync() {
  23. echo "pytorch sync"
  24. [ -d "./third_party/source/pytorch" ] || git clone --quiet https://github.com/pytorch/pytorch.git "./third_party/source/pytorch"
  25. git -C "./third_party/source/pytorch" pull --quiet --prune
  26. mkdir -p "./third_party/source/caffe2/proto"
  27. curl --silent --show-error --location --output "./third_party/source/caffe2/proto/caffe2.proto" "https://github.com/pytorch/pytorch/raw/5e69e11d098a2cfccc8a59377c431e9c71cab9a8/caffe2/proto/caffe2.proto"
  28. curl --silent --show-error --location --output "./third_party/source/caffe2/proto/torch.proto" "https://github.com/pytorch/pytorch/raw/5e69e11d098a2cfccc8a59377c431e9c71cab9a8/caffe2/proto/torch.proto"
  29. }
  30. install() {
  31. echo "pytorch install"
  32. venv
  33. ${python} -m pip install --quiet --upgrade wheel
  34. ${python} -m pip install --quiet --upgrade torch torchvision torchaudio --pre --index-url https://download.pytorch.org/whl/nightly/cpu
  35. # ${python} -m pip install --quiet --upgrade torch-neuron --index-url https://pip.repos.neuron.amazonaws.com
  36. deactivate
  37. }
  38. schema() {
  39. echo "pytorch schema"
  40. [[ $(grep -U $'\x0D' ./source/caffe2-proto.js) ]] && crlf=1
  41. node ./tools/protoc.js --binary --text --root caffe2 --out ./source/caffe2-proto.js ./third_party/source/caffe2/proto/caffe2.proto
  42. if [[ -n ${crlf} ]]; then
  43. unix2dos --quiet --newfile ./source/caffe2-proto.js ./source/caffe2-proto.js
  44. fi
  45. [[ $(grep -U $'\x0D' ./source/pytorch-proto.js) ]] && crlf=1
  46. node ./tools/protoc.js --json --root pytorch --out ./source/pytorch-proto.js --path ./third_party/source ./third_party/source/caffe2/proto/torch.proto
  47. if [[ -n ${crlf} ]]; then
  48. unix2dos --quiet --newfile ./source/pytorch-proto.js ./source/pytorch-proto.js
  49. fi
  50. [[ $(grep -U $'\x0D' ./source/pytorch-schema.js) ]] && crlf=1
  51. node ./tools/flatc.js --root torch --out ./source/pytorch-schema.js ./third_party/source/pytorch/torch/csrc/jit/serialization/mobile_bytecode.fbs
  52. if [[ -n ${crlf} ]]; then
  53. unix2dos --quiet --newfile ./source/pytorch-schema.js ./source/pytorch-schema.js
  54. fi
  55. }
  56. metadata() {
  57. echo "pytorch metadata"
  58. [[ $(grep -U $'\x0D' ./source/pytorch-metadata.json) ]] && crlf=1
  59. venv
  60. ${python} ./tools/pytorch_script.py
  61. deactivate
  62. if [[ -n ${crlf} ]]; then
  63. unix2dos --quiet --newfile ./source/pytorch-metadata.json ./source/pytorch-metadata.json
  64. fi
  65. }
  66. while [ "$#" != 0 ]; do
  67. command="$1" && shift
  68. case "${command}" in
  69. "clean") clean;;
  70. "install") install;;
  71. "sync") sync;;
  72. "schema") schema;;
  73. "metadata") metadata;;
  74. esac
  75. done