pytorch 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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
  16. }
  17. clean() {
  18. echo "pytorch clean"
  19. rm -rf "./third_party/env/pytorch"
  20. rm -rf "./third_party/source/pytorch"
  21. }
  22. sync() {
  23. echo "pytorch sync"
  24. [ -d "./third_party/source/pytorch" ] || git clone --quiet --recursive https://github.com/pytorch/pytorch.git "./third_party/source/pytorch"
  25. pushd "./third_party/source/pytorch" > /dev/null
  26. git pull --quiet --prune
  27. git submodule sync --quiet
  28. git submodule update --quiet --init --recursive
  29. popd > /dev/null
  30. }
  31. install() {
  32. echo "pytorch install"
  33. venv
  34. ${python} -m pip install --quiet --upgrade future protobuf
  35. if [[ "${OSTYPE}" == darwin* ]] && [[ "$(uname -sm)" = "Darwin arm64" ]] && [[ $(${python} -c "import platform; print(platform.uname().machine);") = "arm64" ]]; then
  36. [ -x "$(brew --prefix openblas)" ] || brew install openblas > /dev/null
  37. export OPENBLAS=$(brew --prefix openblas)
  38. ${python} -m pip install --upgrade --quiet pybind11 cython pyyaml
  39. ${python} -m pip install --upgrade --quiet numpy --no-use-pep517
  40. else
  41. ${python} -m pip install --quiet --upgrade scipy
  42. fi
  43. ${python} -m pip install --quiet --upgrade --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
  44. deactivate
  45. }
  46. schema() {
  47. echo "caffe2 schema"
  48. [[ $(grep -U $'\x0D' ./source/caffe2-proto.js) ]] && crlf=1
  49. node ./tools/protoc.js --text --root caffe2 --out ./source/caffe2-proto.js ./third_party/source/pytorch/caffe2/proto/caffe2.proto
  50. if [[ -n ${crlf} ]]; then
  51. unix2dos --quiet --newfile ./source/caffe2-proto.js ./source/caffe2-proto.js
  52. fi
  53. }
  54. metadata() {
  55. echo "pytorch metadata"
  56. venv
  57. export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
  58. if [[ $(grep -U $'\x0D' ./source/pytorch-metadata.json) ]]; then crlf=1; else crlf=; fi
  59. ${python} ./tools/pytorch-script.py metadata
  60. if [[ -n ${crlf} ]]; then
  61. unix2dos --quiet --newfile ./source/pytorch-metadata.json ./source/pytorch-metadata.json
  62. fi
  63. deactivate
  64. echo "caffe2 metadata"
  65. venv
  66. if [[ $(grep -U $'\x0D' ./source/caffe2-metadata.json) ]]; then crlf=1; else crlf=; fi
  67. ${python} ./tools/caffe2-script.py metadata
  68. if [[ -n ${crlf} ]]; then
  69. unix2dos --quiet --newfile ./source/caffe2-metadata.json ./source/caffe2-metadata.json
  70. fi
  71. deactivate
  72. }
  73. while [ "$#" != 0 ]; do
  74. command="$1" && shift
  75. case "${command}" in
  76. "clean") clean;;
  77. "sync") sync;;
  78. "install") install;;
  79. "schema") schema;;
  80. "metadata") metadata;;
  81. esac
  82. done