check-torchcuda.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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)
  47. files = []
  48. for file in sys.argv[1:]:
  49. if not file.endswith(".cpp"):
  50. files.append(file)
  51. res = subprocess.run(
  52. ["git", "grep", "-Hn", "--no-index", r"\.is_cuda", *files],
  53. capture_output=True,
  54. )
  55. if res.returncode == 0:
  56. err('''
  57. Error: The string ".is_cuda" was found. This implies checking if a tensor is a cuda tensor.
  58. Please replace all calls to "tensor.is_cuda" with "get_accelerator().on_accelerator(tensor)",
  59. and add the following import line:
  60. 'from deepspeed.accelerator import get_accelerator'
  61. ''')
  62. err(res.stdout.decode("utf-8"))
  63. sys.exit(1)
  64. elif res.returncode == 2:
  65. err(f"Error invoking grep on {', '.join(files)}:")
  66. err(res.stderr.decode("utf-8"))
  67. sys.exit(2)