sparse_attn.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Copyright 2020 The Microsoft DeepSpeed Team
  3. """
  4. import warnings
  5. from .builder import OpBuilder
  6. class SparseAttnBuilder(OpBuilder):
  7. BUILD_VAR = "DS_BUILD_SPARSE_ATTN"
  8. NAME = "sparse_attn"
  9. def __init__(self):
  10. super().__init__(name=self.NAME)
  11. def absolute_name(self):
  12. return f'deepspeed.ops.sparse_attention.{self.NAME}_op'
  13. def sources(self):
  14. return ['csrc/sparse_attention/utils.cpp']
  15. def cxx_args(self):
  16. return ['-O2', '-fopenmp']
  17. def is_compatible(self):
  18. # Check to see if llvm and cmake are installed since they are dependencies
  19. #required_commands = ['llvm-config|llvm-config-9', 'cmake']
  20. #command_status = list(map(self.command_exists, required_commands))
  21. #deps_compatible = all(command_status)
  22. try:
  23. import torch
  24. except ImportError:
  25. self.warning(f"unable to import torch, please install it first")
  26. return False
  27. # torch-cpu will not have a cuda version
  28. if torch.version.cuda is None:
  29. cuda_compatible = False
  30. self.warning(f"{self.NAME} cuda is not available from torch")
  31. else:
  32. major, minor = torch.version.cuda.split('.')[:2]
  33. cuda_compatible = (int(major) == 10
  34. and int(minor) >= 1) or (int(major) >= 11)
  35. if not cuda_compatible:
  36. self.warning(f"{self.NAME} requires CUDA version 10.1+")
  37. TORCH_MAJOR = int(torch.__version__.split('.')[0])
  38. TORCH_MINOR = int(torch.__version__.split('.')[1])
  39. torch_compatible = TORCH_MAJOR == 1 and TORCH_MINOR >= 5
  40. if not torch_compatible:
  41. self.warning(
  42. f'{self.NAME} requires a torch version >= 1.5 but detected {TORCH_MAJOR}.{TORCH_MINOR}'
  43. )
  44. return super().is_compatible() and torch_compatible and cuda_compatible