Skip to content

Commit

Permalink
Add missing op_builder.hpu component for HPU accelerator (#4963)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelyahu authored Jan 17, 2024
1 parent 7739c0a commit e278076
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
10 changes: 10 additions & 0 deletions op_builder/hpu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Habana Labs, Ltd. an Intel Company
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0

# DeepSpeed Team
'''Copyright The Microsoft DeepSpeed Team'''

from .cpu_adam import CPUAdamBuilder
from .fused_adam import FusedAdamBuilder
from .no_impl import NotImplementedBuilder
35 changes: 35 additions & 0 deletions op_builder/hpu/builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2023 Habana Labs, Ltd. an Intel Company
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0

# DeepSpeed Team

try:
# is op_builder from deepspeed or a 3p version? this should only succeed if it's deepspeed
# if successful this also means we're doing a local install and not JIT compile path
from op_builder import __deepspeed__ # noqa: F401 # type: ignore
from op_builder.builder import OpBuilder
except ImportError:
from deepspeed.ops.op_builder.builder import OpBuilder


class CPUOpBuilder(OpBuilder):

def builder(self):
from torch.utils.cpp_extension import CppExtension as ExtensionBuilder

compile_args = {'cxx': self.strip_empty_entries(self.cxx_args())}

cpp_ext = ExtensionBuilder(name=self.absolute_name(),
sources=self.strip_empty_entries(self.sources()),
include_dirs=self.strip_empty_entries(self.include_paths()),
libraries=self.strip_empty_entries(self.libraries_args()),
extra_compile_args=compile_args)

return cpp_ext

def cxx_args(self):
return ['-O3', '-g', '-Wno-reorder']

def libraries_args(self):
return []
33 changes: 33 additions & 0 deletions op_builder/hpu/cpu_adam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2023 Habana Labs, Ltd. an Intel Company
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0

# DeepSpeed Team

from .builder import CPUOpBuilder


class CPUAdamBuilder(CPUOpBuilder):
BUILD_VAR = "DS_BUILD_CPU_ADAM"
NAME = "cpu_adam"

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

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

def sources(self):
return ['csrc/adam/cpu_adam.cpp', 'csrc/adam/cpu_adam_impl.cpp']

def cxx_args(self):
args = super().cxx_args()
args += ['-DENABLE_BFLOAT16']
return args

def libraries_args(self):
args = super().libraries_args()
return args

def include_paths(self):
return ['csrc/includes']
29 changes: 29 additions & 0 deletions op_builder/hpu/fused_adam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2023 Habana Labs, Ltd. an Intel Company
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0

# DeepSpeed Team

from .builder import CPUOpBuilder


class FusedAdamBuilder(CPUOpBuilder):
BUILD_VAR = "DS_BUILD_FUSED_ADAM"
NAME = "fused_adam"

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

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

def sources(self):
return ['csrc/cpu/adam/fused_adam.cpp', 'csrc/adam/cpu_adam_impl.cpp']

def cxx_args(self):
args = super().cxx_args()
args += ['-DENABLE_BFLOAT16']
return args

def include_paths(self):
return ['csrc/includes']
24 changes: 24 additions & 0 deletions op_builder/hpu/no_impl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0

# DeepSpeed Team

from .builder import CPUOpBuilder


class NotImplementedBuilder(CPUOpBuilder):
BUILD_VAR = "DS_BUILD_NOT_IMPLEMENTED"
NAME = "deepspeed_not_implemented"

def __init__(self, name=None):
name = self.NAME if name is None else name
super().__init__(name=name)

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

def load(self, verbose=True):
raise ValueError("This op had not been implemented on HPU backend.")

def sources(self):
return []

0 comments on commit e278076

Please sign in to comment.