sparse_attn.py 2.94 KB
Newer Older
aiss's avatar
aiss committed
1
2
3
4
5
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0

# DeepSpeed Team

6
7
from .builder import OpBuilder

aiss's avatar
aiss committed
8
9
10
11
12
try:
    from packaging import version as pkg_version
except ImportError:
    pkg_version = None

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

class SparseAttnBuilder(OpBuilder):
    BUILD_VAR = "DS_BUILD_SPARSE_ATTN"
    NAME = "sparse_attn"

    def __init__(self):
        super().__init__(name=self.NAME)

    def absolute_name(self):
        return f'deepspeed.ops.sparse_attention.{self.NAME}_op'

    def sources(self):
        return ['csrc/sparse_attention/utils.cpp']

    def cxx_args(self):
        return ['-O2', '-fopenmp']

aiss's avatar
aiss committed
30
    def is_compatible(self, verbose=True):
31
        # Check to see if llvm and cmake are installed since they are dependencies
aiss's avatar
aiss committed
32
33
34
35
36
        #required_commands = ['llvm-config|llvm-config-9', 'cmake']
        #command_status = list(map(self.command_exists, required_commands))
        #deps_compatible = all(command_status)

        if self.is_rocm_pytorch():
aiss's avatar
aiss committed
37
            #self.warning(f'{self.NAME} is not compatible with ROCM')
aiss's avatar
aiss committed
38
#aiss debug
aiss's avatar
aiss committed
39
            return True
aiss's avatar
aiss committed
40

aiss's avatar
aiss committed
41
42
43
44
45
        try:
            import torch
        except ImportError:
            self.warning(f"unable to import torch, please install it first")
            return False
46

47
48
49
50
51
52
        # torch-cpu will not have a cuda version
        if torch.version.cuda is None:
            cuda_compatible = False
            self.warning(f"{self.NAME} cuda is not available from torch")
        else:
            major, minor = torch.version.cuda.split('.')[:2]
aiss's avatar
aiss committed
53
            cuda_compatible = (int(major) == 10 and int(minor) >= 1) or (int(major) >= 11)
54
            if not cuda_compatible:
aiss's avatar
aiss committed
55
                self.warning(f"{self.NAME} requires CUDA version 10.1+")
56

57
58
        TORCH_MAJOR = int(torch.__version__.split('.')[0])
        TORCH_MINOR = int(torch.__version__.split('.')[1])
aiss's avatar
aiss committed
59
        torch_compatible = (TORCH_MAJOR == 1 and TORCH_MINOR >= 5)
60
61
        if not torch_compatible:
            self.warning(
aiss's avatar
aiss committed
62
                f'{self.NAME} requires a torch version >= 1.5 and < 2.0 but detected {TORCH_MAJOR}.{TORCH_MINOR}')
63

aiss's avatar
aiss committed
64
65
66
67
68
        try:
            import triton
        except ImportError:
            # auto-install of triton is broken on some systems, reverting to manual install for now
            # see this issue: https://github.com/microsoft/DeepSpeed/issues/1710
aiss's avatar
aiss committed
69
            self.warning(f"please install triton==1.0.0 if you want to use sparse attention")
aiss's avatar
aiss committed
70
71
72
73
74
75
76
77
78
79
            return False

        if pkg_version:
            installed_triton = pkg_version.parse(triton.__version__)
            triton_mismatch = installed_triton != pkg_version.parse("1.0.0")
        else:
            installed_triton = triton.__version__
            triton_mismatch = installed_triton != "1.0.0"

        if triton_mismatch:
aiss's avatar
aiss committed
80
            self.warning(f"using untested triton version ({installed_triton}), only 1.0.0 is known to be compatible")
aiss's avatar
aiss committed
81
82
83
            return False

        return super().is_compatible(verbose) and torch_compatible and cuda_compatible