pytorch 3.3 KB

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