cpu_adam.py 1.47 KB
Newer Older
Samyam Rajbhandari's avatar
Samyam Rajbhandari committed
1
2
3
"""
Copyright 2020 The Microsoft DeepSpeed Team
"""
4
import os
aiss's avatar
aiss committed
5
from .builder import TorchCPUOpBuilder
6
7


aiss's avatar
aiss committed
8
class CPUAdamBuilder(TorchCPUOpBuilder):
9
10
    BUILD_VAR = "DS_BUILD_CPU_ADAM"
    NAME = "cpu_adam"
aiss's avatar
aiss committed
11
     
12
13
14
15
16
17
18
    def __init__(self):
        super().__init__(name=self.NAME)

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

    def sources(self):
aiss's avatar
aiss committed
19
20
21
        if self.build_for_cpu:
            return ['csrc/adam/cpu_adam.cpp']
   
aiss's avatar
aiss committed
22
23
24
25
        return ['csrc/adam/cpu_adam.cpp', 'csrc/common/custom_cuda_kernel.cu']

    def libraries_args(self):
        args = super().libraries_args()
aiss's avatar
aiss committed
26
27
28
29
30
        if self.build_for_cpu:
            return args

        if not self.is_rocm_pytorch():
            args += ['curand']
aiss's avatar
aiss committed
31
        return args
32
33

    def include_paths(self):
aiss's avatar
aiss committed
34
        import torch
aiss's avatar
aiss committed
35
36
37
        if self.build_for_cpu:
            CUDA_INCLUDE = []
        elif not self.is_rocm_pytorch():
aiss's avatar
aiss committed
38
39
40
41
42
43
44
45
46
47
48
49
50
            CUDA_INCLUDE = [os.path.join(torch.utils.cpp_extension.CUDA_HOME, "include")]
        else:
            CUDA_INCLUDE = [
                os.path.join(torch.utils.cpp_extension.ROCM_HOME,
                             "include"),
                os.path.join(torch.utils.cpp_extension.ROCM_HOME,
                             "include",
                             "rocrand"),
                os.path.join(torch.utils.cpp_extension.ROCM_HOME,
                             "include",
                             "hiprand"),
            ]
        return ['csrc/includes'] + CUDA_INCLUDE