check-torchcuda.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.cuda".
  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.cuda", 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", "-e", r"torch\.cuda", "--and", "--not", "-e", "#ignore-cuda", *sys.argv[1:]],
  23. capture_output=True,
  24. )
  25. if res.returncode == 0:
  26. err('Error: The string "torch.cuda" was found.\nPlease replace all calls to torch.cuda with "get_accelerator()" and add the following import line:\n\n from deepspeed.accelerator import get_accelerator\n\nIf your code is mean to be cuda specific, please add the following comment in the line with torch.cuda:\n\n #ignore-cuda\n'
  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)
  34. res = subprocess.run(
  35. ["git", "grep", "-Hn", "--no-index", r"\.cuda()", *sys.argv[1:]],
  36. capture_output=True,
  37. )
  38. if res.returncode == 0:
  39. err('Error: The string ".cuda()" was found. This implies convert a tensor to cuda tensor. Please replace all calls to tensor.cuda() with "tensor.to(get_accelerator().device_name())" and add the following import line:\nfrom deepspeed.accelerator import get_accelerator'
  40. )
  41. err(res.stdout.decode("utf-8"))
  42. sys.exit(1)
  43. elif res.returncode == 2:
  44. err(f"Error invoking grep on {', '.join(sys.argv[1:])}:")
  45. err(res.stderr.decode("utf-8"))
  46. sys.exit(2)