check-torchdist.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. # Copyright (c) Microsoft Corporation.
  3. # SPDX-License-Identifier: Apache-2.0
  4. # DeepSpeed Team
  5. from __future__ import annotations
  6. '''Copyright The Microsoft DeepSpeed Team'''
  7. """
  8. Checks each file in sys.argv for the string "torch.distributed".
  9. Modified from https://github.com/jlebar/pre-commit-hooks/blob/master/check_do_not_submit.py
  10. """
  11. import subprocess
  12. import sys
  13. def err(s: str) -> None:
  14. print(s, file=sys.stderr)
  15. # There are many ways we could search for the string "torch.distributed", but `git
  16. # grep --no-index` is nice because
  17. # - it's very fast (as compared to iterating over the file in Python)
  18. # - we can reasonably assume it's available on all machines
  19. # - unlike plain grep, which is slower and has different flags on MacOS versus
  20. # Linux, git grep is always the same.
  21. res = subprocess.run(
  22. ["git", "grep", "-Hn", "--no-index", r"torch\.distributed", *sys.argv[1:]],
  23. capture_output=True,
  24. )
  25. if res.returncode == 0:
  26. err('Error: The string "torch.distributed" was found. Please replace all calls to torch.distributed with "deepspeed.comm"'
  27. )
  28. err(res.stdout.decode("utf-8"))
  29. sys.exit(1)
  30. elif res.returncode == 2:
  31. err(f"Error invoking grep on {', '.join(sys.argv[1:])}:")
  32. err(res.stderr.decode("utf-8"))
  33. sys.exit(2)