pytorch 3.3 KB

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