chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
from .real_accelerator import get_accelerator, set_accelerator, is_current_accelerator_supported
|
||||
@@ -0,0 +1,303 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import abc
|
||||
from abc import ABC
|
||||
|
||||
|
||||
class DeepSpeedAccelerator(ABC):
|
||||
supports_nvtx_domain = False
|
||||
|
||||
def __init__(self):
|
||||
self._name = None
|
||||
self._communication_backend_name = None
|
||||
self._compile_backend = None
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_synchronized_device(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def use_host_timers(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def resolves_data_dependency(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def handles_memory_backpressure(self):
|
||||
...
|
||||
|
||||
# Device APIs
|
||||
@abc.abstractmethod
|
||||
def device_name(self, device_index):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def device(self, device_index):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def set_device(self, device_index):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def current_device(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def current_device_name(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def device_count(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def synchronize(self, device_index=None):
|
||||
...
|
||||
|
||||
# RNG APIs
|
||||
@abc.abstractmethod
|
||||
def random(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_rng_state(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def manual_seed(self, seed):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def manual_seed_all(self, seed):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def initial_seed(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def default_generator(self, device_index):
|
||||
...
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def Stream(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def stream(self, stream):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def current_stream(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def default_stream(self, device_index=None):
|
||||
...
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def Event(self):
|
||||
...
|
||||
|
||||
# Memory management
|
||||
@abc.abstractmethod
|
||||
def empty_cache(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def memory_allocated(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def memory_cached(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def max_memory_cached(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def memory_stats(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def memory_reserved(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def total_memory(self, device_index=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def available_memory(self, device_index=None):
|
||||
...
|
||||
|
||||
# Data types
|
||||
@abc.abstractmethod
|
||||
def is_bf16_supported(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_fp16_supported(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def supported_dtypes(self):
|
||||
...
|
||||
|
||||
# Misc
|
||||
@abc.abstractmethod
|
||||
def is_available(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def range_pop(self, domain=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def lazy_call(self, callback):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def communication_backend_name(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_triton_supported(self):
|
||||
...
|
||||
|
||||
# Graph operations
|
||||
@abc.abstractmethod
|
||||
def create_graph(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def replay_graph(self, graph):
|
||||
...
|
||||
|
||||
# Tensor operations
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def BFloat16Tensor(self):
|
||||
...
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def ByteTensor(self):
|
||||
...
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def DoubleTensor(self):
|
||||
...
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def FloatTensor(self):
|
||||
...
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def HalfTensor(self):
|
||||
...
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def IntTensor(self):
|
||||
...
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def LongTensor(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_pinned(self, tensor):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def on_accelerator(self, tensor):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def op_builder_dir(self):
|
||||
...
|
||||
|
||||
# create an instance of op builder, specified by class_name
|
||||
@abc.abstractmethod
|
||||
def create_op_builder(self, class_name):
|
||||
...
|
||||
|
||||
# return an op builder class, specified by class_name
|
||||
@abc.abstractmethod
|
||||
def get_op_builder(self, class_name):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def build_extension(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def export_envs(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def visible_devices_envs(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_compile_backend(self):
|
||||
...
|
||||
|
||||
@abc.abstractmethod
|
||||
def set_compile_backend(self, backend):
|
||||
...
|
||||
@@ -0,0 +1,358 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
|
||||
# During setup stage torch may not be installed, pass on no torch will
|
||||
# allow op builder related API to be executed.
|
||||
try:
|
||||
import torch
|
||||
except ImportError as e:
|
||||
pass
|
||||
|
||||
try:
|
||||
import oneccl_bindings_for_pytorch # noqa: F401 # type: ignore
|
||||
oneccl_imported_p = True
|
||||
except ImportError as e:
|
||||
oneccl_imported_p = False
|
||||
|
||||
import os
|
||||
|
||||
|
||||
# accelerator for Intel CPU
|
||||
class CPU_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
self._name = 'cpu'
|
||||
self._compile_backend = "inductor"
|
||||
if oneccl_imported_p:
|
||||
self._communication_backend_name = 'ccl'
|
||||
else:
|
||||
# fallback to gloo if oneccl_binding_for_pytorch is not installed
|
||||
self._communication_backend_name = 'gloo'
|
||||
try:
|
||||
import psutil
|
||||
mem = psutil.Process().memory_info().rss
|
||||
self.max_mem = mem
|
||||
except ImportError as e:
|
||||
self.max_mem = 0
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return True
|
||||
|
||||
def use_host_timers(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
return 'cpu'
|
||||
|
||||
def device(self, device_index=None):
|
||||
return None
|
||||
|
||||
def set_device(self, device_index):
|
||||
return
|
||||
|
||||
def current_device(self):
|
||||
return os.environ.get('LOCAL_RANK', 0)
|
||||
|
||||
def current_device_name(self):
|
||||
return 'cpu'
|
||||
|
||||
def device_count(self):
|
||||
device_count = int(os.environ.get('LOCAL_SIZE', 0))
|
||||
if device_count > 0:
|
||||
return device_count
|
||||
else:
|
||||
from deepspeed.utils.numa import get_numa_cores
|
||||
# Count NUMA node for number of cpu accelerators. On machine with HBM
|
||||
# In flat mode, HBM is in separate NUMA node with no cores on this node.
|
||||
# Ignore these NUMA nodes with no cores.
|
||||
numa_core_lists = get_numa_cores()
|
||||
if not numa_core_lists:
|
||||
return 1
|
||||
numa_count = 0
|
||||
prev_core_list = []
|
||||
for core_list in numa_core_lists:
|
||||
if len(core_list) > 0 and core_list != prev_core_list:
|
||||
numa_count += 1
|
||||
prev_core_list = core_list
|
||||
return numa_count
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.set_rng_state(new_state)
|
||||
return torch.set_rng_state(new_state, device_index)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
return torch.get_rng_state()
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.manual_seed(seed)
|
||||
|
||||
def initial_seed(self):
|
||||
return torch.initial_seed()
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return torch.default_generator
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return None
|
||||
|
||||
def stream(self, stream):
|
||||
from deepspeed.runtime.utils import noop_context
|
||||
return noop_context()
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return None
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return None
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return None
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return
|
||||
|
||||
def get_rss(self):
|
||||
import psutil
|
||||
mem = psutil.Process().memory_info().rss
|
||||
if mem > self.max_mem:
|
||||
self.max_mem = mem
|
||||
return mem
|
||||
|
||||
def reset_rss(self):
|
||||
import psutil
|
||||
mem = psutil.Process().memory_info().rss
|
||||
self.max_mem = mem
|
||||
return mem
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return self.get_rss()
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
self.get_rss()
|
||||
return self.max_mem
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
self.reset_rss()
|
||||
return
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return self.get_rss()
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
self.get_rss()
|
||||
return self.max_mem
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
self.reset_rss()
|
||||
return
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
mem = self.get_rss()
|
||||
mem_stat = {}
|
||||
mem_stat['allocated_bytes.all.current'] = mem
|
||||
mem_stat['allocated_bytes.all.peak'] = self.max_mem
|
||||
return mem_stat
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
self.reset_rss()
|
||||
return
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
return self.get_rss()
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
self.get_rss()
|
||||
return self.max_mem
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
import psutil
|
||||
return psutil.virtual_memory().total
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
import psutil
|
||||
return psutil.virtual_memory().available
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return True
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
# TODO itt is currently not supported yet
|
||||
# return torch.profiler.itt.range_push(msg)
|
||||
return
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
# TODO itt is currently not supported yet
|
||||
# return torch.profiler.itt.range_pop()
|
||||
return
|
||||
|
||||
def lazy_call(self, callback):
|
||||
return callback()
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return False
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return True
|
||||
|
||||
def is_fp16_supported(self):
|
||||
try:
|
||||
if torch.ops.mkldnn._is_mkldnn_fp16_supported():
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def supported_dtypes(self):
|
||||
supported_dtypes = [torch.float, torch.bfloat16]
|
||||
if self.is_fp16_supported():
|
||||
supported_dtypes.append(torch.float16)
|
||||
return supported_dtypes
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return None
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
from deepspeed.runtime.utils import noop_context
|
||||
return noop_context()
|
||||
|
||||
def replay_graph(self, graph):
|
||||
return
|
||||
|
||||
# Tensor operations
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return torch.BFloat16Tensor
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return torch.ByteTensor
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return torch.DoubleTensor
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return torch.FloatTensor
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return torch.HalfTensor
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return torch.IntTensor
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return torch.LongTensor
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
return tensor
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
return "op_builder.cpu"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder.cpu"
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith('cpu'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
# create an instance of op builder and return, name specified by class_name
|
||||
def create_op_builder(self, op_name):
|
||||
builder_class = self.get_op_builder(op_name)
|
||||
if builder_class is not None:
|
||||
return builder_class()
|
||||
return None
|
||||
|
||||
# return an op builder class, name specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
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.cpu import AsyncIOBuilder, CCLCommBuilder, ShareMemCommBuilder, FusedAdamBuilder, CPUAdamBuilder, NotImplementedBuilder
|
||||
except ImportError:
|
||||
from deepspeed.ops.op_builder.cpu import AsyncIOBuilder, CCLCommBuilder, ShareMemCommBuilder, FusedAdamBuilder, CPUAdamBuilder, NotImplementedBuilder
|
||||
|
||||
if class_name == "CCLCommBuilder":
|
||||
return CCLCommBuilder
|
||||
elif class_name == "ShareMemCommBuilder":
|
||||
return ShareMemCommBuilder
|
||||
elif class_name == "FusedAdamBuilder":
|
||||
return FusedAdamBuilder
|
||||
elif class_name == "CPUAdamBuilder":
|
||||
return CPUAdamBuilder
|
||||
elif class_name == "AsyncIOBuilder":
|
||||
return AsyncIOBuilder
|
||||
else:
|
||||
# return a NotImplementedBuilder to avoid get NoneType[Name] in unit tests
|
||||
return NotImplementedBuilder
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return []
|
||||
|
||||
# TODO: cpu's visible envs is confirmed, keep as CUDA_VISIBLE_DEVICES
|
||||
def visible_devices_envs(self):
|
||||
return ['CUDA_VISIBLE_DEVICES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends}")
|
||||
@@ -0,0 +1,404 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import functools
|
||||
import os
|
||||
import pkgutil
|
||||
import importlib
|
||||
import sys
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
# During setup stage torch may not be installed, pass on no torch will
|
||||
# allow op builder related API to be executed.
|
||||
try:
|
||||
import torch.cuda
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
import nvtx
|
||||
except ImportError:
|
||||
nvtx = None
|
||||
|
||||
# Delay import pynvml to avoid import error when CUDA is not available
|
||||
pynvml = None
|
||||
|
||||
|
||||
class CUDA_Accelerator(DeepSpeedAccelerator):
|
||||
supports_nvtx_domain = True
|
||||
|
||||
def __init__(self):
|
||||
self._name = 'cuda'
|
||||
self._communication_backend_name = 'nccl' if sys.platform != 'win32' else 'gloo'
|
||||
self._compile_backend = "inductor"
|
||||
self._nvtx_domains = {}
|
||||
if pynvml is None:
|
||||
self._init_pynvml()
|
||||
|
||||
def _init_pynvml(self):
|
||||
global pynvml
|
||||
try:
|
||||
import pynvml
|
||||
except ImportError:
|
||||
return
|
||||
try:
|
||||
pynvml.nvmlInit()
|
||||
except pynvml.NVMLError:
|
||||
pynvml = None
|
||||
return
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
if device_index is None:
|
||||
return 'cuda'
|
||||
return 'cuda:{}'.format(device_index)
|
||||
|
||||
def communication_backend_version(self):
|
||||
return torch.cuda.nccl.version()
|
||||
|
||||
def device(self, device_index=None):
|
||||
return torch.device('cuda', device_index)
|
||||
|
||||
def set_device(self, device_index):
|
||||
torch.cuda.set_device(device_index)
|
||||
|
||||
def current_device(self):
|
||||
return torch.cuda.current_device()
|
||||
|
||||
def current_device_name(self):
|
||||
return 'cuda:{}'.format(torch.cuda.current_device())
|
||||
|
||||
def device_count(self):
|
||||
return torch.cuda.device_count()
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return torch.cuda.synchronize(device_index)
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.cuda.set_rng_state(new_state)
|
||||
|
||||
return torch.cuda.set_rng_state(new_state, device_index)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.cuda.get_rng_state()
|
||||
|
||||
return torch.cuda.get_rng_state(device_index)
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.cuda.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.cuda.manual_seed_all(seed)
|
||||
|
||||
def initial_seed(self):
|
||||
return torch.cuda.initial_seed()
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return torch.cuda.default_generators[device_index]
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return torch.cuda.Stream
|
||||
|
||||
def stream(self, stream):
|
||||
return torch.cuda.stream(stream)
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return torch.cuda.current_stream(device_index)
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return torch.cuda.default_stream(device_index)
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return torch.cuda.Event
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return torch.cuda.empty_cache()
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return torch.cuda.memory_allocated(device_index)
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return torch.cuda.max_memory_allocated(device_index)
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return torch.cuda.reset_max_memory_allocated(device_index)
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return torch.cuda.memory_cached(device_index)
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return torch.cuda.max_memory_cached(device_index)
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return torch.cuda.reset_max_memory_cached(device_index)
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
if hasattr(torch.cuda, 'memory_stats'):
|
||||
return torch.cuda.memory_stats(device_index)
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
if hasattr(torch.cuda, 'reset_peak_memory_stats'):
|
||||
return torch.cuda.reset_peak_memory_stats(device_index)
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.cuda, 'memory_reserved'):
|
||||
return torch.cuda.memory_reserved(device_index)
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.cuda, 'max_memory_reserved'):
|
||||
return torch.cuda.max_memory_reserved(device_index)
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return torch.cuda.get_device_properties(device_index).total_memory
|
||||
|
||||
def _get_nvml_gpu_id(self, torch_gpu_id):
|
||||
"""
|
||||
credit: https://discuss.pytorch.org/t/making-pynvml-match-torch-device-ids-cuda-visible-devices/103020
|
||||
|
||||
Remap torch device id to nvml device id, respecting CUDA_VISIBLE_DEVICES.
|
||||
|
||||
If the latter isn't set return the same id
|
||||
"""
|
||||
# if CUDA_VISIBLE_DEVICES is used automagically remap the id since pynvml ignores this env var
|
||||
if "CUDA_VISIBLE_DEVICES" in os.environ:
|
||||
ids = list(map(int, os.environ.get("CUDA_VISIBLE_DEVICES", "").split(",")))
|
||||
return ids[torch_gpu_id] # remap
|
||||
else:
|
||||
return torch_gpu_id
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
if pynvml:
|
||||
if device_index is None:
|
||||
device_index = self.current_device()
|
||||
handle = pynvml.nvmlDeviceGetHandleByIndex(self._get_nvml_gpu_id(device_index))
|
||||
info = pynvml.nvmlDeviceGetMemoryInfo(handle)
|
||||
return info.free
|
||||
else:
|
||||
return self.total_memory(device_index) - self.memory_allocated(device_index)
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
if not torch.cuda.is_available():
|
||||
return True
|
||||
return torch.cuda.is_bf16_supported()
|
||||
|
||||
def is_fp16_supported(self):
|
||||
if not torch.cuda.is_available():
|
||||
return True
|
||||
# See https://docs.nvidia.com/deeplearning/tensorrt/support-matrix/index.html#hardware-precision-matrix
|
||||
# FP16 on compute capability 6.x is deprecated
|
||||
allow_deprecated_fp16 = os.environ.get('DS_ALLOW_DEPRECATED_FP16', '0') == '1'
|
||||
major, _ = torch.cuda.get_device_capability()
|
||||
if major >= 7:
|
||||
return True
|
||||
elif major == 6 and allow_deprecated_fp16:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def supported_dtypes(self):
|
||||
supported_dtypes = [torch.float]
|
||||
if self.is_fp16_supported():
|
||||
supported_dtypes.append(torch.half)
|
||||
if self.is_bf16_supported():
|
||||
supported_dtypes.append(torch.bfloat16)
|
||||
return supported_dtypes
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return torch.cuda.is_available()
|
||||
|
||||
def _get_nvtx_domain(self, domain):
|
||||
if nvtx is None or domain is None:
|
||||
return None
|
||||
if domain not in self._nvtx_domains:
|
||||
self._nvtx_domains[domain] = nvtx.get_domain(domain)
|
||||
return self._nvtx_domains[domain]
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
nvtx_domain = self._get_nvtx_domain(domain)
|
||||
if nvtx_domain is not None:
|
||||
return nvtx_domain.push_range(message=msg, category=category)
|
||||
torch_nvtx = getattr(torch.cuda, 'nvtx', None)
|
||||
if torch_nvtx is not None and hasattr(torch_nvtx, 'range_push'):
|
||||
return torch_nvtx.range_push(msg)
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
nvtx_domain = self._get_nvtx_domain(domain)
|
||||
if nvtx_domain is not None:
|
||||
return nvtx_domain.pop_range()
|
||||
torch_nvtx = getattr(torch.cuda, 'nvtx', None)
|
||||
if torch_nvtx is not None and hasattr(torch_nvtx, 'range_pop'):
|
||||
return torch_nvtx.range_pop()
|
||||
|
||||
def lazy_call(self, callback):
|
||||
return torch.cuda._lazy_call(callback)
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
if not self.is_available():
|
||||
return False
|
||||
major, _ = torch.cuda.get_device_capability()
|
||||
if major >= 8:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return torch.cuda.CUDAGraph()
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
return torch.cuda.graph(graph, pool, stream)
|
||||
|
||||
def replay_graph(self, graph):
|
||||
graph.replay()
|
||||
return
|
||||
|
||||
# Tensor operations
|
||||
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.bfloat16, device='cuda')
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.uint8, device='cuda')
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.double, device='cuda')
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.float, device='cuda')
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.half, device='cuda')
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.int, device='cuda')
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.long, device='cuda')
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
return tensor.pin_memory()
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith('cuda:'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
return "op_builder"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder"
|
||||
|
||||
# dict that holds class name <--> class type mapping i.e.
|
||||
# 'AsyncIOBuilder': <class 'op_builder.async_io.AsyncIOBuilder'>
|
||||
# this dict will be filled at init stage
|
||||
class_dict = None
|
||||
|
||||
def _lazy_init_class_dict(self):
|
||||
if self.class_dict is not None:
|
||||
return
|
||||
else:
|
||||
self.class_dict = {}
|
||||
# begin initialize for create_op_builder()
|
||||
# put all valid class name <--> class type mapping into class_dict
|
||||
op_builder_dir = self.op_builder_dir()
|
||||
op_builder_module = importlib.import_module(op_builder_dir)
|
||||
op_builder_absolute_path = os.path.dirname(op_builder_module.__file__)
|
||||
for _, module_name, _ in pkgutil.iter_modules([op_builder_absolute_path]):
|
||||
# avoid self references,
|
||||
# skip sub_directories which contains ops for other backend(cpu, npu, etc.).
|
||||
if module_name != 'all_ops' and module_name != 'builder' and not os.path.isdir(
|
||||
os.path.join(op_builder_absolute_path, module_name)):
|
||||
module = importlib.import_module("{}.{}".format(op_builder_dir, module_name))
|
||||
for member_name in module.__dir__():
|
||||
if member_name.endswith(
|
||||
'Builder'
|
||||
) and member_name != "OpBuilder" and member_name != "CUDAOpBuilder" and member_name != "TorchCPUOpBuilder": # avoid abstract classes
|
||||
if not member_name in self.class_dict:
|
||||
self.class_dict[member_name] = getattr(module, member_name)
|
||||
# end initialize for create_op_builder()
|
||||
|
||||
# create an instance of op builder and return, name specified by class_name
|
||||
def create_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]()
|
||||
else:
|
||||
return None
|
||||
|
||||
# return an op builder class, name specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]
|
||||
else:
|
||||
return None
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return ['NCCL']
|
||||
|
||||
def visible_devices_envs(self):
|
||||
return ['CUDA_VISIBLE_DEVICES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends}")
|
||||
@@ -0,0 +1,328 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import functools
|
||||
import os
|
||||
import pkgutil
|
||||
import importlib
|
||||
import torch
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
|
||||
|
||||
class HPU_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
self._name = 'hpu'
|
||||
self._communication_backend_name = 'hccl'
|
||||
self._compile_backend = "hpu_backend"
|
||||
self.apply_hpu_workarounds()
|
||||
try:
|
||||
import habana_frameworks.torch.hpu as hpu
|
||||
self.hpu = hpu
|
||||
torch.use_deterministic_algorithms(True)
|
||||
# TODO: remove this WA when memory mapping break is resolved.
|
||||
torch.utils.deterministic.fill_uninitialized_memory = False
|
||||
except ImportError as e:
|
||||
raise ValueError(
|
||||
"HPU_Accelerator requires habana_frameworks.torch.hpu, which is not installed on this system.")
|
||||
|
||||
self.fp16_supported = None
|
||||
|
||||
def apply_hpu_workarounds(self):
|
||||
|
||||
def update_wa_env_var(key, value):
|
||||
if key not in os.environ.keys():
|
||||
os.environ[key] = value
|
||||
|
||||
update_wa_env_var("PT_HPU_LAZY_ACC_PAR_MODE", "0")
|
||||
update_wa_env_var("PT_HPU_ENABLE_REFINE_DYNAMIC_SHAPES", "0")
|
||||
|
||||
# Device APIs
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
return False
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return True
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return True
|
||||
|
||||
def device_name(self, device_index=None):
|
||||
# ignoring device_index.
|
||||
return 'hpu'
|
||||
|
||||
def device(self, device_index=None):
|
||||
return torch.device(self.device_name(device_index))
|
||||
|
||||
def set_device(self, device_index):
|
||||
self.hpu.set_device(device_index)
|
||||
|
||||
def current_device(self):
|
||||
return (self.hpu.current_device())
|
||||
|
||||
def current_device_name(self):
|
||||
return 'hpu:{}'.format(self.current_device())
|
||||
|
||||
def device_count(self):
|
||||
return self.hpu.device_count()
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return self.hpu.synchronize()
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
self.hpu.random.set_rng_state(new_state)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
return self.hpu.random.get_rng_state()
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return self.hpu.random.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
self.hpu.random.manual_seed_all(seed)
|
||||
|
||||
def initial_seed(self):
|
||||
return self.hpu.random.initial_seed()
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return self.hpu.random.default_generators[device_index]
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return self.hpu.Stream
|
||||
|
||||
def stream(self, stream):
|
||||
return self.hpu.stream(stream)
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return self.hpu.current_stream()
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return self.hpu.default_stream()
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
import habana_frameworks.torch.core as htcore
|
||||
return htcore.hpu.Event
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return self.hpu.memory_allocated()
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return self.hpu.max_memory_allocated()
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return self.hpu.reset_max_memory_allocated()
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return self.hpu.memory_cached(device_index)
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return self.hpu.max_memory_cached(device_index)
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return None
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
return self.hpu.memory_stats(device_index)
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
self.hpu.reset_peak_memory_stats(device_index)
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
return self.hpu.memory_reserved(device_index)
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
return self.hpu.max_memory_reserved(device_index)
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return self.memory_stats(device_index)['Limit']
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
return self.total_memory(device_index) - self.memory_allocated(device_index)
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return True
|
||||
|
||||
def is_fp16_supported(self):
|
||||
if self.fp16_supported is None:
|
||||
import habana_frameworks.torch.utils.experimental as htexp
|
||||
self.fp16_supported = htexp._is_fp16_supported()
|
||||
return self.fp16_supported
|
||||
|
||||
def supported_dtypes(self):
|
||||
supported_dtypes = [torch.float, torch.bfloat16]
|
||||
if self.is_fp16_supported():
|
||||
supported_dtypes.append(torch.half)
|
||||
return supported_dtypes
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return self.hpu.is_available()
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
return
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
return
|
||||
|
||||
def lazy_call(self, callback):
|
||||
callback()
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return False
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return self.hpu.HPUGraph()
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
return self.hpu.graph(graph, stream=stream)
|
||||
|
||||
def replay_graph(self, graph):
|
||||
graph.replay()
|
||||
return
|
||||
|
||||
# Tensor operations
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.bfloat16, device='hpu')
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.uint8, device='hpu')
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.double, device='hpu')
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.float, device='hpu')
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.half, device='hpu')
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.int, device='hpu')
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.long, device='hpu')
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
return tensor.pin_memory(self.device())
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith('hpu:'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
return "op_builder.hpu"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder.hpu"
|
||||
|
||||
# dict that holds class name <--> class type mapping i.e.
|
||||
# 'AsyncIOBuilder': <class 'op_builder.async_io.AsyncIOBuilder'>
|
||||
# this dict will be filled at init stage
|
||||
class_dict = None
|
||||
|
||||
def _lazy_init_class_dict(self):
|
||||
if self.class_dict is not None:
|
||||
return
|
||||
else:
|
||||
self.class_dict = {}
|
||||
# begin initialize for create_op_builder()
|
||||
# put all valid class name <--> class type mapping into class_dict
|
||||
op_builder_dir = self.op_builder_dir()
|
||||
op_builder_module = importlib.import_module(op_builder_dir)
|
||||
op_builder_absolute_path = os.path.dirname(op_builder_module.__file__)
|
||||
for _, module_name, _ in pkgutil.iter_modules([op_builder_absolute_path]):
|
||||
# avoid self references,
|
||||
# skip sub_directories which contains ops for other backend(cpu, npu, etc.).
|
||||
if module_name != 'all_ops' and module_name != 'builder' and not os.path.isdir(
|
||||
os.path.join(op_builder_absolute_path, module_name)):
|
||||
module = importlib.import_module("{}.{}".format(op_builder_dir, module_name))
|
||||
for member_name in module.__dir__():
|
||||
if member_name.endswith(
|
||||
'Builder'
|
||||
) and member_name != "OpBuilder" and member_name != "CPUOpBuilder" and member_name != "TorchCPUOpBuilder": # avoid abstract classes
|
||||
if not member_name in self.class_dict:
|
||||
self.class_dict[member_name] = getattr(module, member_name)
|
||||
# end initialize for create_op_builder()
|
||||
|
||||
# create an instance of op builder and return, name specified by class_name
|
||||
def create_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]()
|
||||
else:
|
||||
return None
|
||||
|
||||
# return an op builder class, name specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]
|
||||
else:
|
||||
return self.class_dict['NotImplementedBuilder'] if 'NotImplementedBuilder' in self.class_dict else None
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return []
|
||||
|
||||
def visible_devices_envs(self):
|
||||
# Current way deepspeed set this env var is not applicable with all HPU instances
|
||||
# User has to follow instructions in:
|
||||
# https://docs.habana.ai/en/latest/PyTorch/Reference/PT_Multiple_Tenants_on_HPU/Multiple_Workloads_Single_Docker.html
|
||||
# keeping CUDA_VISIBLE_DEVICES
|
||||
return ['CUDA_VISIBLE_DEVICES'] #['HABANA_VISIBLE_MODULES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends}")
|
||||
@@ -0,0 +1,295 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
import importlib
|
||||
import inspect
|
||||
import functools
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
import torch
|
||||
# During setup stage torch may not be installed, pass on no torch will
|
||||
# allow op builder related API to be executed.
|
||||
|
||||
|
||||
class MLU_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
self._name = 'mlu'
|
||||
self._communication_backend_name = 'cncl'
|
||||
self._compile_backend = "inductor"
|
||||
self.class_dict = None
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
if device_index == None:
|
||||
return 'mlu'
|
||||
return 'mlu:{}'.format(device_index)
|
||||
|
||||
def device(self, device_index=None):
|
||||
return torch.mlu.device(device_index)
|
||||
|
||||
def set_device(self, device_index):
|
||||
torch.mlu.set_device(device_index)
|
||||
|
||||
def current_device(self):
|
||||
return torch.mlu.current_device()
|
||||
|
||||
def current_device_name(self):
|
||||
return 'mlu:{}'.format(torch.mlu.current_device())
|
||||
|
||||
def device_count(self):
|
||||
return torch.mlu.device_count()
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return torch.mlu.synchronize(device_index)
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.mlu.set_rng_state(new_state)
|
||||
|
||||
return torch.mlu.set_rng_state(new_state, device_index)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.mlu.get_rng_state()
|
||||
|
||||
return torch.mlu.get_rng_state(device_index)
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.mlu.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.mlu.manual_seed_all(seed)
|
||||
|
||||
def initial_seed(self, seed):
|
||||
return torch.mlu.initial_seed(seed)
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return torch.mlu.default_generators[device_index]
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return torch.mlu.Stream
|
||||
|
||||
def stream(self, stream):
|
||||
return torch.mlu.stream(stream)
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return torch.mlu.current_stream(device_index)
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return torch.mlu.default_stream(device_index)
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return torch.mlu.Event
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return torch.mlu.empty_cache()
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return torch.mlu.memory_allocated(device_index)
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return torch.mlu.max_memory_allocated(device_index)
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return torch.mlu.reset_max_memory_allocated(device_index)
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return torch.mlu.memory_cached(device_index)
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return torch.mlu.max_memory_cached(device_index)
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return torch.mlu.reset_max_memory_cached(device_index)
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
if hasattr(torch.mlu, 'memory_stats'):
|
||||
return torch.mlu.memory_stats(device_index)
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
if hasattr(torch.mlu, 'reset_peak_memory_stats'):
|
||||
return torch.mlu.reset_peak_memory_stats(device_index)
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.mlu, 'memory_reserved'):
|
||||
return torch.mlu.memory_reserved(device_index)
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.mlu, 'max_memory_reserved'):
|
||||
return torch.mlu.max_memory_reserved(device_index)
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return torch.mlu.get_device_properties(device_index).total_memory
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
return self.total_memory(device_index) - self.memory_allocated(device_index)
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return torch.mlu.is_bf16_supported()
|
||||
|
||||
def is_fp16_supported(self):
|
||||
return True
|
||||
|
||||
def supported_dtypes(self):
|
||||
supported_dtypes = [torch.float]
|
||||
if self.is_fp16_supported():
|
||||
supported_dtypes.append(torch.half)
|
||||
if self.is_bf16_supported():
|
||||
supported_dtypes.append(torch.bfloat16)
|
||||
return supported_dtypes
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return torch.mlu.is_available()
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
if hasattr(torch.mlu.cnpx, 'range_push'):
|
||||
return torch.mlu.cnpx.range_push(msg)
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
if hasattr(torch.mlu.cnpx, 'range_pop'):
|
||||
return torch.mlu.cnpx.range_pop()
|
||||
|
||||
def lazy_call(self, callback):
|
||||
return torch.mlu._lazy_call(callback)
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return True
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
torch.mlu.MLUGraph()
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
return torch.mlu.graph(graph, pool, stream)
|
||||
|
||||
def replay_graph(self, graph):
|
||||
graph.replay()
|
||||
return
|
||||
|
||||
# Tensor operations
|
||||
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.bfloat16, device='mlu')
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.uint8, device='mlu')
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.double, device='mlu')
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.float, device='mlu')
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.half, device='mlu')
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.int, device='mlu')
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.long, device='mlu')
|
||||
|
||||
def pin_memory(self, tensor):
|
||||
return tensor.pin_memory()
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith('mlu:'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
return "op_builder.mlu"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder.mlu"
|
||||
|
||||
def _lazy_init_class_dict(self):
|
||||
if self.class_dict:
|
||||
return
|
||||
|
||||
op_builder_module = importlib.import_module(self.op_builder_dir())
|
||||
|
||||
# get op builder class from op_builder/mlu/__init__.py
|
||||
self.class_dict = {}
|
||||
for class_name, class_obj in inspect.getmembers(op_builder_module, inspect.isclass):
|
||||
self.class_dict[class_name] = class_obj
|
||||
|
||||
# create an instance of op builder and return, name specified by class_name
|
||||
def create_op_builder(self, class_name):
|
||||
builder_class = self.get_op_builder(class_name)
|
||||
return builder_class()
|
||||
|
||||
# return an op builder class, name specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]
|
||||
else:
|
||||
return self.class_dict['NotImplementedBuilder']
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return ['NEUWARE_HOME', 'CNCL', 'LD_LIBRARY', 'PATH']
|
||||
|
||||
def visible_devices_envs(self):
|
||||
return ['MLU_VISIBLE_DEVICES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends }")
|
||||
@@ -0,0 +1,279 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import torch
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
|
||||
# During setup stage torch may not be installed, pass on no torch will
|
||||
# allow op builder related API to be executed.
|
||||
try:
|
||||
import torch.mps
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
class MPS_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
self._name = "mps"
|
||||
self._communication_backend_name = None
|
||||
self._compile_backend = "inductor"
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
# Event timers are not supported on MPS
|
||||
return True
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
if device_index is None:
|
||||
return "mps"
|
||||
return "mps:{}".format(device_index)
|
||||
|
||||
def device(self, device_index):
|
||||
return torch.device("mps", index=0)
|
||||
|
||||
def set_device(self, device_index):
|
||||
return
|
||||
|
||||
def current_device(self):
|
||||
return torch.device("mps", index=0)
|
||||
|
||||
def current_device_name(self):
|
||||
return "mps:0"
|
||||
|
||||
def device_count(self):
|
||||
return 1
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return torch.mps.synchronize()
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
return torch.mps.set_rng_state(new_state)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
return torch.mps.get_rng_state()
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.mps.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.mps.manual_seed(seed)
|
||||
|
||||
def seed(self):
|
||||
return torch.mps.seed()
|
||||
|
||||
def initial_seed(self):
|
||||
return
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return None
|
||||
|
||||
def stream(self, stream):
|
||||
return None
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return None
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return None
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return None
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return torch.mps.empty_cache()
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return torch.mps.current_allocated_memory()
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return torch.mps.driver_allocated_memory()
|
||||
|
||||
def set_per_process_memory_fraction(self, fraction):
|
||||
return torch.mps.set_per_process_memory_fraction(fraction)
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
return
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
return
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
return
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
return
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
return
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return False
|
||||
|
||||
def is_fp16_supported(self):
|
||||
return False
|
||||
|
||||
def supported_dtypes(self):
|
||||
return [torch.float]
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
return
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
return
|
||||
|
||||
def lazy_call(self, callback):
|
||||
return
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return False
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return None
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
from deepspeed.runtime.utils import noop_context
|
||||
return noop_context()
|
||||
|
||||
def replay_graph(self, graph):
|
||||
return
|
||||
|
||||
# Tensor operations
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
return tensor.pin_memory()
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith("mps"):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
|
||||
return "op_builder"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder"
|
||||
|
||||
# create an instance of op builder, specified by class_name
|
||||
def create_op_builder(self, op_name):
|
||||
builder_class = self.get_op_builder(op_name)
|
||||
if builder_class is not None:
|
||||
return builder_class()
|
||||
return None
|
||||
|
||||
# return an op builder class, specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
from deepspeed.ops.op_builder.cpu import NotImplementedBuilder
|
||||
|
||||
return NotImplementedBuilder
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return []
|
||||
|
||||
# TODO: mpu's visible envs is confirmed, keep as CUDA_VISIBLE_DEVICES
|
||||
def visible_devices_envs(self):
|
||||
# TODO: could not find visible devices env for mps
|
||||
return ['CUDA_VISIBLE_DEVICES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends}")
|
||||
@@ -0,0 +1,294 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
import importlib
|
||||
import inspect
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
# During setup stage torch may not be installed, pass on no torch will
|
||||
# allow op builder related API to be executed.
|
||||
try:
|
||||
import torch.npu
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
class NPU_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._name = 'npu'
|
||||
self._communication_backend_name = 'hccl'
|
||||
self._compile_backend = "inductor"
|
||||
# dict that holds class name <--> class type mapping i.e.
|
||||
# 'AsyncIOBuilder': <class 'op_builder.async_io.AsyncIOBuilder'>
|
||||
# this dict will be filled at init stage
|
||||
self.class_dict = None
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
if device_index is None:
|
||||
return 'npu'
|
||||
return 'npu:{}'.format(device_index)
|
||||
|
||||
def device(self, device_index=None):
|
||||
return torch.device('npu', device_index)
|
||||
|
||||
def set_device(self, device_index):
|
||||
torch.npu.set_device(device_index)
|
||||
|
||||
def current_device(self):
|
||||
return torch.npu.current_device()
|
||||
|
||||
def current_device_name(self):
|
||||
return 'npu:{}'.format(torch.npu.current_device())
|
||||
|
||||
def device_count(self):
|
||||
return torch.npu.device_count()
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return torch.npu.synchronize(device_index)
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.npu.set_rng_state(new_state)
|
||||
|
||||
return torch.npu.set_rng_state(new_state, device_index)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.npu.get_rng_state()
|
||||
|
||||
return torch.npu.get_rng_state(device_index)
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.npu.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.npu.manual_seed_all(seed)
|
||||
|
||||
def initial_seed(self):
|
||||
return torch.npu.initial_seed()
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return torch.npu.default_generators[device_index]
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return torch.npu.Stream
|
||||
|
||||
def stream(self, stream):
|
||||
return torch.npu.stream(stream)
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return torch.npu.current_stream(device_index)
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return torch.npu.default_stream(device_index)
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return torch.npu.Event
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return torch.npu.empty_cache()
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return torch.npu.memory_allocated(device_index)
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return torch.npu.max_memory_allocated(device_index)
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return torch.npu.reset_max_memory_allocated(device_index)
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return torch.npu.memory_cached(device_index)
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return torch.npu.max_memory_cached(device_index)
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return torch.npu.reset_max_memory_cached(device_index)
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
if hasattr(torch.npu, 'memory_stats'):
|
||||
return torch.npu.memory_stats(device_index)
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
if hasattr(torch.npu, 'reset_peak_memory_stats'):
|
||||
return torch.npu.reset_peak_memory_stats(device_index)
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.npu, 'memory_reserved'):
|
||||
return torch.npu.memory_reserved(device_index)
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.npu, 'max_memory_reserved'):
|
||||
return torch.npu.max_memory_reserved(device_index)
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return torch.npu.get_device_properties(device_index).total_memory
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
return self.total_memory(device_index) - self.memory_allocated(device_index)
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return torch.npu.is_bf16_supported()
|
||||
|
||||
def is_fp16_supported(self):
|
||||
return True
|
||||
|
||||
def supported_dtypes(self):
|
||||
return [torch.float, torch.half, torch.bfloat16]
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return torch.npu.is_available()
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
return
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
return
|
||||
|
||||
def lazy_call(self, callback):
|
||||
return torch.npu._lazy_call(callback)
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return False
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return None
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
from deepspeed.runtime.utils import noop_context
|
||||
return noop_context()
|
||||
|
||||
def replay_graph(self, graph):
|
||||
return
|
||||
|
||||
# Tensor operations
|
||||
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return torch.npu.BFloat16Tensor
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return torch.npu.ByteTensor
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return torch.npu.DoubleTensor
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return torch.npu.FloatTensor
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return torch.npu.HalfTensor
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return torch.npu.IntTensor
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return torch.npu.LongTensor
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
return tensor.pin_memory()
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith('npu:'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
return "op_builder.npu"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder.npu"
|
||||
|
||||
def _lazy_init_class_dict(self):
|
||||
if self.class_dict:
|
||||
return
|
||||
|
||||
op_builder_module = importlib.import_module(self.op_builder_dir())
|
||||
|
||||
# get op builder class from op_builder/npu/__init__.py
|
||||
self.class_dict = {}
|
||||
for class_name, class_obj in inspect.getmembers(op_builder_module, inspect.isclass):
|
||||
self.class_dict[class_name] = class_obj
|
||||
|
||||
# create an instance of op builder and return, name specified by class_name
|
||||
def create_op_builder(self, class_name):
|
||||
builder_class = self.get_op_builder(class_name)
|
||||
return None if builder_class is None else builder_class()
|
||||
|
||||
# return an op builder class, name specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]
|
||||
else:
|
||||
return self.class_dict['NotImplementedBuilder'] if 'NotImplementedBuilder' in self.class_dict else None
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return ['ASCEND', 'HCCL', 'LD_LIBRARY', 'PATH']
|
||||
|
||||
def visible_devices_envs(self):
|
||||
return ['ASCEND_RT_VISIBLE_DEVICES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends }")
|
||||
@@ -0,0 +1,299 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
import os
|
||||
|
||||
try:
|
||||
# Importing logger currently requires that torch is installed, hence the try...except
|
||||
# TODO: Remove logger dependency on torch.
|
||||
from deepspeed.utils import logger as accel_logger
|
||||
except ImportError as e:
|
||||
accel_logger = None
|
||||
|
||||
try:
|
||||
from accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa1
|
||||
except ImportError as e:
|
||||
dsa1 = None
|
||||
try:
|
||||
from deepspeed.accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa2
|
||||
except ImportError as e:
|
||||
dsa2 = None
|
||||
|
||||
SUPPORTED_ACCELERATOR_LIST = ['cuda', 'cpu', 'xpu', 'npu', 'mps', 'hpu', 'mlu', 'sdaa', 'supa']
|
||||
|
||||
ds_accelerator = None
|
||||
|
||||
|
||||
def _validate_accelerator(accel_obj):
|
||||
# because abstract_accelerator has different path during
|
||||
# build time (accelerator.abstract_accelerator)
|
||||
# and run time (deepspeed.accelerator.abstract_accelerator)
|
||||
# and extension would import the
|
||||
# run time abstract_accelerator/DeepSpeedAccelerator as its base
|
||||
# class, so we need to compare accel_obj with both base class.
|
||||
# if accel_obj is instance of DeepSpeedAccelerator in one of
|
||||
# accelerator.abstractor_accelerator
|
||||
# or deepspeed.accelerator.abstract_accelerator, consider accel_obj
|
||||
# is a conforming object
|
||||
if not ((dsa1 is not None and isinstance(accel_obj, dsa1)) or (dsa2 is not None and isinstance(accel_obj, dsa2))):
|
||||
raise AssertionError(f"{accel_obj.__class__.__name__} accelerator is not subclass of DeepSpeedAccelerator")
|
||||
|
||||
# TODO: turn off is_available test since this breaks tests
|
||||
# assert accel_obj.is_available(), \
|
||||
# f'{accel_obj.__class__.__name__} accelerator fails is_available() test'
|
||||
|
||||
|
||||
def is_current_accelerator_supported():
|
||||
return get_accelerator().device_name() in SUPPORTED_ACCELERATOR_LIST
|
||||
|
||||
|
||||
def get_accelerator():
|
||||
global ds_accelerator
|
||||
if ds_accelerator is not None:
|
||||
return ds_accelerator
|
||||
|
||||
accelerator_name = None
|
||||
ds_set_method = None
|
||||
# 1. Detect whether there is override of DeepSpeed accelerators from environment variable.
|
||||
if "DS_ACCELERATOR" in os.environ.keys():
|
||||
accelerator_name = os.environ["DS_ACCELERATOR"]
|
||||
if accelerator_name == "xpu":
|
||||
try:
|
||||
import torch
|
||||
assert hasattr(torch, 'xpu') and torch.xpu.is_available(), \
|
||||
"XPU_Accelerator requires PyTorch with XPU support (torch.xpu)."
|
||||
except (ImportError, AssertionError) as e:
|
||||
raise ValueError(f"XPU_Accelerator requires PyTorch with XPU support: {e}")
|
||||
elif accelerator_name == "cpu":
|
||||
pass
|
||||
elif accelerator_name == "npu":
|
||||
try:
|
||||
import torch_npu # noqa: F401 # type: ignore
|
||||
except ImportError as e:
|
||||
raise ValueError("NPU_Accelerator requires torch_npu, which is not installed on this system.")
|
||||
pass
|
||||
elif accelerator_name == "sdaa":
|
||||
try:
|
||||
import torch_sdaa # noqa: F401 # type: ignore
|
||||
except ImportError as e:
|
||||
raise ValueError("SDAA_Accelerator requires torch_sdaa, which is not installed on this system.")
|
||||
pass
|
||||
elif accelerator_name == "mps":
|
||||
try:
|
||||
import torch.mps
|
||||
|
||||
# should use torch.mps.is_available() if it exists someday but this is used as proxy
|
||||
torch.mps.current_allocated_memory()
|
||||
except (RuntimeError, ImportError) as e:
|
||||
raise ValueError("MPS_Accelerator requires torch.mps, which is not installed on this system.")
|
||||
elif accelerator_name == "hpu":
|
||||
try:
|
||||
import habana_frameworks.torch.hpu # noqa: F401
|
||||
except ImportError as e:
|
||||
raise ValueError(
|
||||
"HPU_Accelerator requires habana_frameworks.torch.hpu, which is not installed on this system.")
|
||||
elif accelerator_name == "mlu":
|
||||
try:
|
||||
import torch_mlu # noqa: F401
|
||||
except ImportError as e:
|
||||
raise ValueError("MLU_Accelerator requires torch_mlu, which is not installed on this system.")
|
||||
elif accelerator_name == "supa":
|
||||
try:
|
||||
import torch_supa # noqa: F401 # type: ignore
|
||||
except ImportError as e:
|
||||
raise ValueError("SUPA_Accelerator requires torch_supa, which is not installed on this system.")
|
||||
elif accelerator_name not in SUPPORTED_ACCELERATOR_LIST:
|
||||
raise ValueError(f'DS_ACCELERATOR must be one of {SUPPORTED_ACCELERATOR_LIST}. '
|
||||
f'Value "{accelerator_name}" is not supported')
|
||||
ds_set_method = "override"
|
||||
|
||||
# 2. If no override, detect which accelerator to use automatically
|
||||
if accelerator_name is None:
|
||||
# We need a way to choose among different accelerator types.
|
||||
# Currently we detect which accelerator extension is installed
|
||||
# in the environment and use it if the installing answer is True.
|
||||
# An alternative might be detect whether CUDA device is installed on
|
||||
# the system but this comes with two pitfalls:
|
||||
# 1. the system may not have torch pre-installed, so
|
||||
# get_accelerator().is_available() may not work.
|
||||
# 2. Some scenario like install on login node (without CUDA device)
|
||||
# and run on compute node (with CUDA device) may cause mismatch
|
||||
# between installation time and runtime.
|
||||
|
||||
try:
|
||||
import torch
|
||||
|
||||
# Detect XPU via PyTorch
|
||||
if hasattr(torch, 'xpu'):
|
||||
if torch.xpu.is_available():
|
||||
accelerator_name = "xpu"
|
||||
except ImportError as e:
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
try:
|
||||
import torch_npu # noqa: F401,F811 # type: ignore
|
||||
|
||||
accelerator_name = "npu"
|
||||
except ImportError as e:
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
try:
|
||||
import torch_sdaa # noqa: F401,F811 # type: ignore
|
||||
|
||||
accelerator_name = "sdaa"
|
||||
except ImportError as e:
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
try:
|
||||
import torch.mps
|
||||
|
||||
# should use torch.mps.is_available() if it exists someday but this is used as proxy
|
||||
torch.mps.current_allocated_memory()
|
||||
accelerator_name = "mps"
|
||||
except (RuntimeError, ImportError) as e:
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
try:
|
||||
import habana_frameworks.torch.hpu # noqa: F401,F811
|
||||
|
||||
accelerator_name = "hpu"
|
||||
except ImportError as e:
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
try:
|
||||
import torch_mlu # noqa: F401,F811
|
||||
|
||||
accelerator_name = "mlu"
|
||||
except ImportError as e:
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
try:
|
||||
# Detect Biren SUPA GPU. torch_supa spoofs torch.cuda so this #ignore-cuda
|
||||
# check must come before the CUDA detection below.
|
||||
import torch_supa # noqa: F401,F811 # type: ignore
|
||||
import torch
|
||||
|
||||
if hasattr(torch, 'supa') and torch.supa.is_available():
|
||||
accelerator_name = "supa"
|
||||
except ImportError as e:
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
try:
|
||||
import torch
|
||||
|
||||
# Determine if we are on a GPU or x86 CPU with torch.
|
||||
# "torch.cuda.is_available()" provides a stronger guarantee, #ignore-cuda
|
||||
# ensuring that we are free from CUDA initialization errors.
|
||||
# While "torch.cuda.device_count() > 0" check ensures that #ignore-cuda
|
||||
# we won't try to do any CUDA calls when no device is available
|
||||
# For reference: https://github.com/deepspeedai/DeepSpeed/pull/6810
|
||||
if torch.cuda.device_count() > 0 and torch.cuda.is_available(): #ignore-cuda
|
||||
accelerator_name = "cuda"
|
||||
except (RuntimeError, ImportError) as e:
|
||||
# TODO need a more decent way to detect which accelerator to use, consider using nvidia-smi command for detection
|
||||
pass
|
||||
if accelerator_name is None:
|
||||
# borrow this log from PR#5084
|
||||
if accel_logger is not None:
|
||||
accel_logger.warning(
|
||||
"Setting accelerator to CPU. If you have GPU or other accelerator, we were unable to detect it.")
|
||||
# cpu added as catch-all when accelerator detection fails
|
||||
accelerator_name = "cpu"
|
||||
|
||||
ds_set_method = "auto detect"
|
||||
|
||||
# 3. Set ds_accelerator accordingly
|
||||
if accelerator_name == "cuda":
|
||||
from .cuda_accelerator import CUDA_Accelerator
|
||||
|
||||
ds_accelerator = CUDA_Accelerator()
|
||||
elif accelerator_name == "cpu":
|
||||
from .cpu_accelerator import CPU_Accelerator
|
||||
|
||||
ds_accelerator = CPU_Accelerator()
|
||||
elif accelerator_name == "xpu":
|
||||
from .xpu_accelerator import XPU_Accelerator
|
||||
|
||||
ds_accelerator = XPU_Accelerator()
|
||||
elif accelerator_name == "npu":
|
||||
from .npu_accelerator import NPU_Accelerator
|
||||
|
||||
ds_accelerator = NPU_Accelerator()
|
||||
elif accelerator_name == "sdaa":
|
||||
from .sdaa_accelerator import SDAA_Accelerator
|
||||
|
||||
ds_accelerator = SDAA_Accelerator()
|
||||
elif accelerator_name == "mps":
|
||||
from .mps_accelerator import MPS_Accelerator
|
||||
|
||||
ds_accelerator = MPS_Accelerator()
|
||||
elif accelerator_name == 'hpu':
|
||||
from .hpu_accelerator import HPU_Accelerator
|
||||
|
||||
ds_accelerator = HPU_Accelerator()
|
||||
elif accelerator_name == 'mlu':
|
||||
from .mlu_accelerator import MLU_Accelerator
|
||||
|
||||
ds_accelerator = MLU_Accelerator()
|
||||
elif accelerator_name == 'supa':
|
||||
from .supa_accelerator import SUPA_Accelerator
|
||||
|
||||
ds_accelerator = SUPA_Accelerator()
|
||||
_validate_accelerator(ds_accelerator)
|
||||
if accel_logger is not None:
|
||||
accel_logger.info(f"Setting ds_accelerator to {ds_accelerator._name} ({ds_set_method})")
|
||||
return ds_accelerator
|
||||
|
||||
|
||||
def set_accelerator(accel_obj):
|
||||
global ds_accelerator
|
||||
_validate_accelerator(accel_obj)
|
||||
if accel_logger is not None and accel_obj is not None:
|
||||
accel_logger.info(f"Setting ds_accelerator to {accel_obj._name} (model specified)")
|
||||
ds_accelerator = accel_obj
|
||||
|
||||
|
||||
"""
|
||||
-----------[code] test_get.py -----------
|
||||
from deepspeed.accelerator import get_accelerator
|
||||
my_accelerator = get_accelerator()
|
||||
logger.info(f'{my_accelerator._name=}')
|
||||
logger.info(f'{my_accelerator._communication_backend=}')
|
||||
logger.info(f'{my_accelerator.HalfTensor().device=}')
|
||||
logger.info(f'{my_accelerator.total_memory()=}')
|
||||
-----------[code] test_get.py -----------
|
||||
|
||||
---[output] python test_get.py---------
|
||||
my_accelerator.name()='cuda'
|
||||
my_accelerator.communication_backend='nccl'
|
||||
my_accelerator.HalfTensor().device=device(type='cuda', index=0)
|
||||
my_accelerator.total_memory()=34089730048
|
||||
---[output] python test_get.py---------
|
||||
|
||||
**************************************************************************
|
||||
-----------[code] test_set.py -----------
|
||||
from deepspeed.accelerator.cuda_accelerator import CUDA_Accelerator
|
||||
cu_accel = CUDA_Accelerator()
|
||||
logger.info(f'{id(cu_accel)=}')
|
||||
from deepspeed.accelerator import set_accelerator, get_accelerator
|
||||
set_accelerator(cu_accel)
|
||||
|
||||
my_accelerator = get_accelerator()
|
||||
logger.info(f'{id(my_accelerator)=}')
|
||||
logger.info(f'{my_accelerator._name=}')
|
||||
logger.info(f'{my_accelerator._communication_backend=}')
|
||||
logger.info(f'{my_accelerator.HalfTensor().device=}')
|
||||
logger.info(f'{my_accelerator.total_memory()=}')
|
||||
-----------[code] test_set.py -----------
|
||||
|
||||
|
||||
---[output] python test_set.py---------
|
||||
id(cu_accel)=139648165478304
|
||||
my_accelerator=<deepspeed.accelerator.cuda_accelerator.CUDA_Accelerator object at 0x7f025f4bffa0>
|
||||
my_accelerator.name='cuda'
|
||||
my_accelerator.communication_backend='nccl'
|
||||
my_accelerator.HalfTensor().device=device(type='cuda', index=0)
|
||||
my_accelerator.total_memory()=34089730048
|
||||
---[output] python test_set.py---------
|
||||
"""
|
||||
Executable
+323
@@ -0,0 +1,323 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# BSD 3- Clause License Copyright (c) 2023, Tecorigin Co., Ltd. All rights
|
||||
# reserved.
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
# Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
# Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
# Neither the name of the copyright holder nor the names of its contributors
|
||||
# may be used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION)
|
||||
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
# STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
||||
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
# OF SUCH DAMAGE.
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import importlib
|
||||
import inspect
|
||||
import functools
|
||||
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
# During setup stage torch may not be installed, pass on no torch will
|
||||
# allow op builder related API to be executed.
|
||||
try:
|
||||
import torch.sdaa
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
class SDAA_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
self._name = 'sdaa'
|
||||
self._communication_backend_name = 'tccl'
|
||||
self._compile_backend = "inductor"
|
||||
self.class_dict = None
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
if device_index is None:
|
||||
return 'sdaa'
|
||||
return 'sdaa:{}'.format(device_index)
|
||||
|
||||
def device(self, device_index=None):
|
||||
return torch.sdaa.device(device_index)
|
||||
|
||||
def set_device(self, device_index):
|
||||
torch.sdaa.set_device(device_index)
|
||||
|
||||
def current_device(self):
|
||||
return torch.sdaa.current_device()
|
||||
|
||||
def current_device_name(self):
|
||||
return 'sdaa:{}'.format(torch.sdaa.current_device())
|
||||
|
||||
def device_count(self):
|
||||
return torch.sdaa.device_count()
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return torch.sdaa.synchronize(device_index)
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.sdaa.set_rng_state(new_state)
|
||||
|
||||
return torch.sdaa.set_rng_state(new_state, device_index)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.sdaa.get_rng_state()
|
||||
|
||||
return torch.sdaa.get_rng_state(device_index)
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.sdaa.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.sdaa.manual_seed_all(seed)
|
||||
|
||||
def initial_seed(self):
|
||||
return torch.sdaa.initial_seed()
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return torch.sdaa.default_generators[device_index]
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return torch.sdaa.Stream
|
||||
|
||||
def stream(self, stream):
|
||||
return torch.sdaa.stream(stream)
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return torch.sdaa.current_stream(device_index)
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return torch.sdaa.default_stream(device_index)
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return torch.sdaa.Event
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return torch.sdaa.empty_cache()
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return torch.sdaa.memory_allocated(device_index)
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return torch.sdaa.max_memory_allocated(device_index)
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return torch.sdaa.reset_max_memory_allocated(device_index)
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return torch.sdaa.memory_cached(device_index)
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return torch.sdaa.max_memory_cached(device_index)
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return torch.sdaa.reset_max_memory_cached(device_index)
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
if hasattr(torch.sdaa, 'memory_stats'):
|
||||
return torch.sdaa.memory_stats(device_index)
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
if hasattr(torch.sdaa, 'reset_peak_memory_stats'):
|
||||
return torch.sdaa.reset_peak_memory_stats(device_index)
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.sdaa, 'memory_reserved'):
|
||||
return torch.sdaa.memory_reserved(device_index)
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.sdaa, 'max_memory_reserved'):
|
||||
return torch.sdaa.max_memory_reserved(device_index)
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return torch.sdaa.get_device_properties(device_index).total_memory
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
return self.total_memory(device_index) - self.memory_allocated(device_index)
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return torch.sdaa.is_bf16_supported()
|
||||
|
||||
def is_fp16_supported(self):
|
||||
return True
|
||||
|
||||
def supported_dtypes(self):
|
||||
supported_dtypes = [torch.float]
|
||||
if self.is_fp16_supported():
|
||||
supported_dtypes.append(torch.half)
|
||||
if self.is_bf16_supported():
|
||||
supported_dtypes.append(torch.bfloat16)
|
||||
return supported_dtypes
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return torch.sdaa.is_available()
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
return
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
return
|
||||
|
||||
def lazy_call(self, callback):
|
||||
return torch.sdaa._lazy_call(callback)
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return False
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return None
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
from deepspeed.runtime.utils import noop_context
|
||||
return noop_context()
|
||||
|
||||
def replay_graph(self, graph):
|
||||
return
|
||||
|
||||
# Tensor operations
|
||||
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.bfloat16, device='sdaa')
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.uint8, device='sdaa')
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.double, device='sdaa')
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.float, device='sdaa')
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.half, device='sdaa')
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.int, device='sdaa')
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.long, device='sdaa')
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
return tensor.pin_memory()
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith('sdaa:'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
return "op_builder.sdaa"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder.sdaa"
|
||||
|
||||
def _lazy_init_class_dict(self):
|
||||
if self.class_dict:
|
||||
return
|
||||
|
||||
op_builder_module = importlib.import_module(self.op_builder_dir())
|
||||
|
||||
# get op builder class from op_builder/sdaa/__init__.py
|
||||
self.class_dict = {}
|
||||
for class_name, class_obj in inspect.getmembers(op_builder_module, inspect.isclass):
|
||||
self.class_dict[class_name] = class_obj
|
||||
|
||||
# create an instance of op builder and return, name specified by class_name
|
||||
def create_op_builder(self, class_name):
|
||||
builder_class = self.get_op_builder(class_name)
|
||||
return builder_class()
|
||||
|
||||
# return an op builder class, name specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]
|
||||
else:
|
||||
return self.class_dict['NotImplementedBuilder']
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return ['NCCL', 'LD_LIBRARY', 'PATH']
|
||||
|
||||
def visible_devices_envs(self):
|
||||
return ['SDAA_VISIBLE_DEVICES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends}")
|
||||
@@ -0,0 +1,293 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# DeepSpeed Team
|
||||
|
||||
import os
|
||||
import sys
|
||||
import pkgutil
|
||||
import importlib
|
||||
|
||||
import torch
|
||||
from .abstract_accelerator import DeepSpeedAccelerator
|
||||
|
||||
|
||||
class SUPA_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
self._name = 'supa'
|
||||
# Use BCCL on Linux, fall back to gloo on Windows (no BCCL support yet)
|
||||
self._communication_backend_name = 'bccl' if sys.platform != 'win32' else 'gloo'
|
||||
self._compile_backend = "inductor"
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
if device_index is None:
|
||||
return 'supa'
|
||||
return 'supa:{}'.format(device_index)
|
||||
|
||||
def communication_backend_version(self):
|
||||
# BCCL does not expose a version via torch.supa
|
||||
return (0, 0, 0)
|
||||
|
||||
def device(self, device_index=None):
|
||||
return torch.device('supa', device_index)
|
||||
|
||||
def set_device(self, device_index):
|
||||
torch.supa.set_device(device_index)
|
||||
|
||||
def current_device(self):
|
||||
return torch.supa.current_device()
|
||||
|
||||
def current_device_name(self):
|
||||
return 'supa:{}'.format(torch.supa.current_device())
|
||||
|
||||
def device_count(self):
|
||||
return torch.supa.device_count()
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return torch.supa.synchronize(device_index)
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.supa.set_rng_state(new_state)
|
||||
return torch.supa.set_rng_state(new_state, device_index)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
if device_index is None:
|
||||
return torch.supa.get_rng_state()
|
||||
return torch.supa.get_rng_state(device_index)
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.supa.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.supa.manual_seed_all(seed)
|
||||
|
||||
def initial_seed(self):
|
||||
return torch.supa.initial_seed()
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return torch.supa.default_generators[device_index]
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return torch.supa.Stream
|
||||
|
||||
def stream(self, stream):
|
||||
return torch.supa.stream(stream)
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return torch.supa.current_stream(device_index)
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
return torch.supa.default_stream(device_index)
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return torch.supa.Event
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return torch.supa.empty_cache()
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return torch.supa.memory_allocated(device_index)
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return torch.supa.max_memory_allocated(device_index)
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return torch.supa.reset_max_memory_allocated(device_index)
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return torch.supa.memory_cached(device_index)
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return torch.supa.max_memory_cached(device_index)
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return torch.supa.reset_max_memory_cached(device_index)
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
if hasattr(torch.supa, 'memory_stats'):
|
||||
return torch.supa.memory_stats(device_index)
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
if hasattr(torch.supa, 'reset_peak_memory_stats'):
|
||||
return torch.supa.reset_peak_memory_stats(device_index)
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.supa, 'memory_reserved'):
|
||||
return torch.supa.memory_reserved(device_index)
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
if hasattr(torch.supa, 'max_memory_reserved'):
|
||||
return torch.supa.max_memory_reserved(device_index)
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return torch.supa.get_device_properties(device_index).total_memory
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
return self.total_memory(device_index) - self.memory_allocated(device_index)
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return True
|
||||
|
||||
def is_fp16_supported(self):
|
||||
return True
|
||||
|
||||
def supported_dtypes(self):
|
||||
return [torch.float, torch.half, torch.bfloat16]
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return torch.supa.is_available()
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
return None
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
return None
|
||||
|
||||
def lazy_call(self, callback):
|
||||
return torch.supa._lazy_call(callback)
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return True
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return torch.supa.SUPAGraph()
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
return torch.supa.graph(graph, pool, stream)
|
||||
|
||||
def replay_graph(self, graph):
|
||||
graph.replay()
|
||||
|
||||
# Tensor operations
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return torch.supa.BFloat16Tensor
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return torch.supa.ByteTensor
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return torch.supa.DoubleTensor
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return torch.supa.FloatTensor
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return torch.supa.HalfTensor
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return torch.supa.IntTensor
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return torch.supa.LongTensor
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
return tensor.pin_memory()
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
return tensor.is_pinned()
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
return device_str.startswith('supa:')
|
||||
|
||||
def op_builder_dir(self):
|
||||
try:
|
||||
# Local install: op_builder is a top-level package
|
||||
from op_builder import __deepspeed__ # noqa: F401 # type: ignore
|
||||
return "op_builder.supa"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder.supa"
|
||||
|
||||
# dict that holds class name <--> class type mapping i.e.
|
||||
# 'FusedAdamBuilder': <class 'op_builder.supa.fused_adam.FusedAdamBuilder'>
|
||||
# populated lazily on first call to create_op_builder / get_op_builder
|
||||
class_dict = None
|
||||
|
||||
def _lazy_init_class_dict(self):
|
||||
if self.class_dict is not None:
|
||||
return
|
||||
self.class_dict = {}
|
||||
op_builder_dir = self.op_builder_dir()
|
||||
op_builder_module = importlib.import_module(op_builder_dir)
|
||||
op_builder_absolute_path = os.path.dirname(op_builder_module.__file__)
|
||||
for _, module_name, _ in pkgutil.iter_modules([op_builder_absolute_path]):
|
||||
if module_name in ('all_ops', 'builder') or os.path.isdir(
|
||||
os.path.join(op_builder_absolute_path, module_name)):
|
||||
continue
|
||||
module = importlib.import_module("{}.{}".format(op_builder_dir, module_name))
|
||||
for member_name in module.__dir__():
|
||||
if (member_name.endswith('Builder')
|
||||
and member_name not in ('OpBuilder', 'CUDAOpBuilder', 'TorchCPUOpBuilder', 'SUPAOpBuilder')):
|
||||
if member_name not in self.class_dict:
|
||||
self.class_dict[member_name] = getattr(module, member_name)
|
||||
|
||||
def create_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]()
|
||||
return None
|
||||
|
||||
def get_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]
|
||||
return None
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return ['BCCL', 'BIREN', 'SUPA', 'LD_LIBRARY', 'PATH']
|
||||
|
||||
def visible_devices_envs(self):
|
||||
return ['SUPA_VISIBLE_DEVICES']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(f"{backend} not supported by {self.device_name()}. "
|
||||
f"Supported backends: {supported_backends}")
|
||||
@@ -0,0 +1,315 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import torch
|
||||
from deepspeed.accelerator.abstract_accelerator import DeepSpeedAccelerator
|
||||
import functools
|
||||
import importlib
|
||||
import inspect
|
||||
|
||||
try:
|
||||
import oneccl_bindings_for_pytorch # noqa: F401 # type: ignore
|
||||
oneccl_imported_p = True
|
||||
except ImportError as e:
|
||||
oneccl_imported_p = False
|
||||
|
||||
|
||||
class XPU_Accelerator(DeepSpeedAccelerator):
|
||||
|
||||
def __init__(self):
|
||||
self._name = 'xpu'
|
||||
if oneccl_imported_p:
|
||||
self._communication_backend_name = 'ccl'
|
||||
else:
|
||||
# changed to xccl if not using torch-CCL on XPU device
|
||||
self._communication_backend_name = 'xccl'
|
||||
self._compile_backend = "inductor"
|
||||
self.aligned_tensors = []
|
||||
self.class_dict = None
|
||||
|
||||
def is_synchronized_device(self):
|
||||
return False
|
||||
|
||||
def use_host_timers(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def resolves_data_dependency(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
def handles_memory_backpressure(self):
|
||||
return self.is_synchronized_device()
|
||||
|
||||
# Device APIs
|
||||
def device_name(self, device_index=None):
|
||||
if device_index == None:
|
||||
return 'xpu'
|
||||
return 'xpu:{}'.format(device_index)
|
||||
|
||||
def device(self, device_index=None):
|
||||
return torch.device('xpu', device_index)
|
||||
|
||||
def set_device(self, device_index):
|
||||
torch.xpu.set_device(device_index)
|
||||
|
||||
def current_device(self):
|
||||
return torch.xpu.current_device()
|
||||
|
||||
def current_device_name(self):
|
||||
return 'xpu:{}'.format(torch.xpu.current_device())
|
||||
|
||||
def device_count(self):
|
||||
return torch.xpu.device_count()
|
||||
|
||||
def synchronize(self, device_index=None):
|
||||
return torch.xpu.synchronize(device_index)
|
||||
|
||||
# RNG APIs
|
||||
def random(self):
|
||||
return torch.xpu.random
|
||||
|
||||
def set_rng_state(self, new_state, device_index=None):
|
||||
if device_index == None:
|
||||
return torch.xpu.set_rng_state(new_state)
|
||||
return torch.xpu.set_rng_state(new_state, device_index)
|
||||
|
||||
def get_rng_state(self, device_index=None):
|
||||
if device_index == None:
|
||||
return torch.xpu.get_rng_state()
|
||||
return torch.xpu.get_rng_state(device_index)
|
||||
|
||||
def manual_seed(self, seed):
|
||||
return torch.xpu.manual_seed(seed)
|
||||
|
||||
def manual_seed_all(self, seed):
|
||||
return torch.xpu.manual_seed_all(seed)
|
||||
|
||||
def initial_seed(self):
|
||||
return torch.xpu.initial_seed()
|
||||
|
||||
def default_generator(self, device_index):
|
||||
return torch.xpu.default_generators[device_index]
|
||||
|
||||
# Streams/Events
|
||||
@property
|
||||
def Stream(self):
|
||||
return torch.xpu.Stream
|
||||
|
||||
def stream(self, stream):
|
||||
return torch.xpu.stream(stream)
|
||||
|
||||
def current_stream(self, device_index=None):
|
||||
return torch.xpu.current_stream(device_index)
|
||||
|
||||
def default_stream(self, device_index=None):
|
||||
# torch.xpu does not support the sync behavior of default stream as cuda
|
||||
# use current_stream as workaround
|
||||
# see https://pytorch.org/docs/stable/notes/cuda.html#cuda-streams
|
||||
return torch.xpu.current_stream(device_index)
|
||||
|
||||
@property
|
||||
def Event(self):
|
||||
return torch.xpu.Event
|
||||
|
||||
# Memory management
|
||||
def empty_cache(self):
|
||||
return torch.xpu.empty_cache()
|
||||
|
||||
def memory_allocated(self, device_index=None):
|
||||
return torch.xpu.memory_allocated(device_index)
|
||||
|
||||
def max_memory_allocated(self, device_index=None):
|
||||
return torch.xpu.max_memory_allocated(device_index)
|
||||
|
||||
def reset_max_memory_allocated(self, device_index=None):
|
||||
return torch.xpu.reset_max_memory_allocated(device_index)
|
||||
|
||||
def memory_cached(self, device_index=None):
|
||||
return torch.xpu.memory_reserved(device_index)
|
||||
|
||||
def max_memory_cached(self, device_index=None):
|
||||
return torch.xpu.max_memory_reserved(device_index)
|
||||
|
||||
def reset_max_memory_cached(self, device_index=None):
|
||||
return torch.xpu.reset_max_memory_reserved(device_index)
|
||||
|
||||
def memory_stats(self, device_index=None):
|
||||
return torch.xpu.memory_stats(device_index)
|
||||
|
||||
def reset_peak_memory_stats(self, device_index=None):
|
||||
return torch.xpu.reset_peak_memory_stats(device_index)
|
||||
|
||||
def memory_reserved(self, device_index=None):
|
||||
return torch.xpu.memory_reserved(device_index)
|
||||
|
||||
def max_memory_reserved(self, device_index=None):
|
||||
return torch.xpu.max_memory_reserved(device_index)
|
||||
|
||||
def total_memory(self, device_index=None):
|
||||
return torch.xpu.get_device_properties(device_index).total_memory
|
||||
|
||||
def available_memory(self, device_index=None):
|
||||
return self.total_memory(device_index) - self.memory_allocated(device_index)
|
||||
|
||||
# Misc
|
||||
def is_available(self):
|
||||
return torch.xpu.is_available()
|
||||
|
||||
def range_push(self, msg, domain=None, category=None):
|
||||
# TODO itt is currently not supported yet
|
||||
# return torch.profiler.itt.range_push(msg)
|
||||
return
|
||||
|
||||
def range_pop(self, domain=None):
|
||||
# TODO itt is currently not supported yet
|
||||
# return torch.profiler.itt.range_pop()
|
||||
return
|
||||
|
||||
def lazy_call(self, callback):
|
||||
if hasattr(torch.xpu, "_lazy_call"):
|
||||
return torch.xpu._lazy_call(callback)
|
||||
else:
|
||||
return torch.xpu.lazy_init._lazy_call(callback)
|
||||
|
||||
def communication_backend_name(self):
|
||||
return self._communication_backend_name
|
||||
|
||||
def is_triton_supported(self):
|
||||
return False
|
||||
|
||||
# Graph operations
|
||||
def create_graph(self):
|
||||
return None
|
||||
|
||||
def capture_to_graph(self, graph, pool=None, stream=None):
|
||||
from deepspeed.runtime.utils import noop_context
|
||||
return noop_context()
|
||||
|
||||
def replay_graph(self, graph):
|
||||
return
|
||||
|
||||
# Data types
|
||||
def is_bf16_supported(self):
|
||||
return True
|
||||
|
||||
def is_fp16_supported(self):
|
||||
return True
|
||||
|
||||
def supported_dtypes(self):
|
||||
return [torch.float, torch.half, torch.bfloat16]
|
||||
|
||||
# Tensor operations
|
||||
|
||||
@property
|
||||
def BFloat16Tensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.bfloat16, device=self._name)
|
||||
|
||||
@property
|
||||
def ByteTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.uint8, device=self._name)
|
||||
|
||||
@property
|
||||
def DoubleTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.double, device=self._name)
|
||||
|
||||
@property
|
||||
def FloatTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.float, device=self._name)
|
||||
|
||||
@property
|
||||
def HalfTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.half, device=self._name)
|
||||
|
||||
@property
|
||||
def IntTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.int, device=self._name)
|
||||
|
||||
@property
|
||||
def LongTensor(self):
|
||||
return functools.partial(torch.tensor, dtype=torch.long, device=self._name)
|
||||
|
||||
def pin_memory(self, tensor, align_bytes=1):
|
||||
if align_bytes == 1:
|
||||
return tensor.pin_memory(device=self.current_device_name())
|
||||
elif align_bytes == 0:
|
||||
from deepspeed.ops.op_builder.xpu import AsyncIOBuilder
|
||||
self.aio_handle = AsyncIOBuilder().load().aio_handle(128 * 1024, 8, False, False, False)
|
||||
aligned_t = self.aio_handle.new_cpu_locked_tensor(tensor.numel(), tensor)
|
||||
aligned_t = aligned_t[:tensor.numel()].copy_(tensor)
|
||||
self.aligned_tensors.append([aligned_t.data_ptr(), aligned_t[-1].data_ptr()])
|
||||
return aligned_t
|
||||
|
||||
def is_pinned(self, tensor):
|
||||
if tensor.is_pinned(device=self.current_device_name()):
|
||||
return True
|
||||
else:
|
||||
for begin, end in self.aligned_tensors:
|
||||
if begin <= tensor.data_ptr() and tensor.data_ptr() <= end:
|
||||
return True
|
||||
return False
|
||||
|
||||
def op_builder_dir(self):
|
||||
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
|
||||
return "op_builder.xpu"
|
||||
except ImportError:
|
||||
return "deepspeed.ops.op_builder.xpu"
|
||||
|
||||
def on_accelerator(self, tensor):
|
||||
device_str = str(tensor.device)
|
||||
if device_str.startswith('xpu:'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def _lazy_init_class_dict(self):
|
||||
if self.class_dict:
|
||||
return
|
||||
|
||||
op_builder_module = importlib.import_module(self.op_builder_dir())
|
||||
|
||||
# get op builder class from op_builder/xpu/__init__.py
|
||||
self.class_dict = {}
|
||||
for class_name, class_obj in inspect.getmembers(op_builder_module, inspect.isclass):
|
||||
self.class_dict[class_name] = class_obj
|
||||
|
||||
# create an instance of op builder and return, name specified by class_name
|
||||
def create_op_builder(self, class_name):
|
||||
builder_class = self.get_op_builder(class_name)
|
||||
return builder_class()
|
||||
|
||||
# return an op builder class, name specified by class_name
|
||||
def get_op_builder(self, class_name):
|
||||
self._lazy_init_class_dict()
|
||||
if class_name in self.class_dict:
|
||||
return self.class_dict[class_name]
|
||||
else:
|
||||
return self.class_dict['NotImplementedBuilder']
|
||||
|
||||
def build_extension(self):
|
||||
from torch.utils.cpp_extension import BuildExtension
|
||||
return BuildExtension
|
||||
|
||||
def export_envs(self):
|
||||
return []
|
||||
|
||||
def visible_devices_envs(self):
|
||||
return ['ZE_AFFINITY_MASK']
|
||||
|
||||
def set_visible_devices_envs(self, current_env, local_accelerator_ids):
|
||||
for env in self.visible_devices_envs():
|
||||
current_env[env] = ",".join(map(str, local_accelerator_ids))
|
||||
|
||||
def get_compile_backend(self):
|
||||
return self._compile_backend
|
||||
|
||||
def set_compile_backend(self, backend):
|
||||
supported_backends = torch._dynamo.list_backends(exclude_tags=())
|
||||
if backend in supported_backends:
|
||||
self._compile_backend = backend
|
||||
else:
|
||||
raise ValueError(
|
||||
f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends}")
|
||||
Reference in New Issue
Block a user