DeepSpeed
Зеркало из https://github.com/microsoft/DeepSpeed
300 строк · 9.1 Кб
1# Copyright (c) Microsoft Corporation.
2# SPDX-License-Identifier: Apache-2.0
3
4# DeepSpeed Team
5import importlib6import inspect7import functools8
9from .abstract_accelerator import DeepSpeedAccelerator10import torch11# During setup stage torch may not be installed, pass on no torch will
12# allow op builder related API to be executed.
13
14
15class MLU_Accelerator(DeepSpeedAccelerator):16
17def __init__(self):18self._name = 'mlu'19self._communication_backend_name = 'cncl'20self._compile_backend = "inductor"21self.class_dict = None22
23def is_synchronized_device(self):24return False25
26def use_host_timers(self):27return self.is_synchronized_device()28
29def resolves_data_dependency(self):30return self.is_synchronized_device()31
32def handles_memory_backpressure(self):33return self.is_synchronized_device()34
35# Device APIs36def device_name(self, device_index=None):37if device_index == None:38return 'mlu'39return 'mlu:{}'.format(device_index)40
41def device(self, device_index=None):42return torch.mlu.device(device_index)43
44def set_device(self, device_index):45torch.mlu.set_device(device_index)46
47def current_device(self):48return torch.mlu.current_device()49
50def current_device_name(self):51return 'mlu:{}'.format(torch.mlu.current_device())52
53def device_count(self):54return torch.mlu.device_count()55
56def synchronize(self, device_index=None):57return torch.mlu.synchronize(device_index)58
59# RNG APIs60def random(self):61return torch.random62
63def set_rng_state(self, new_state, device_index=None):64if device_index is None:65return torch.mlu.set_rng_state(new_state)66
67return torch.mlu.set_rng_state(new_state, device_index)68
69def get_rng_state(self, device_index=None):70if device_index is None:71return torch.mlu.get_rng_state()72
73return torch.mlu.get_rng_state(device_index)74
75def manual_seed(self, seed):76return torch.mlu.manual_seed(seed)77
78def manual_seed_all(self, seed):79return torch.mlu.manual_seed_all(seed)80
81def initial_seed(self, seed):82return torch.mlu.initial_seed(seed)83
84def default_generator(self, device_index):85return torch.mlu.default_generators[device_index]86
87# Streams/Events88@property89def Stream(self):90return torch.mlu.Stream91
92def stream(self, stream):93return torch.mlu.stream(stream)94
95def current_stream(self, device_index=None):96return torch.mlu.current_stream(device_index)97
98def default_stream(self, device_index=None):99return torch.mlu.default_stream(device_index)100
101@property102def Event(self):103return torch.mlu.Event104
105# Memory management106def empty_cache(self):107return torch.mlu.empty_cache()108
109def memory_allocated(self, device_index=None):110return torch.mlu.memory_allocated(device_index)111
112def max_memory_allocated(self, device_index=None):113return torch.mlu.max_memory_allocated(device_index)114
115def reset_max_memory_allocated(self, device_index=None):116return torch.mlu.reset_max_memory_allocated(device_index)117
118def memory_cached(self, device_index=None):119return torch.mlu.memory_cached(device_index)120
121def max_memory_cached(self, device_index=None):122return torch.mlu.max_memory_cached(device_index)123
124def reset_max_memory_cached(self, device_index=None):125return torch.mlu.reset_max_memory_cached(device_index)126
127def memory_stats(self, device_index=None):128if hasattr(torch.mlu, 'memory_stats'):129return torch.mlu.memory_stats(device_index)130
131def reset_peak_memory_stats(self, device_index=None):132if hasattr(torch.mlu, 'reset_peak_memory_stats'):133return torch.mlu.reset_peak_memory_stats(device_index)134
135def memory_reserved(self, device_index=None):136if hasattr(torch.mlu, 'memory_reserved'):137return torch.mlu.memory_reserved(device_index)138
139def max_memory_reserved(self, device_index=None):140if hasattr(torch.mlu, 'max_memory_reserved'):141return torch.mlu.max_memory_reserved(device_index)142
143def total_memory(self, device_index=None):144return torch.mlu.get_device_properties(device_index).total_memory145
146def available_memory(self, device_index=None):147return self.total_memory(device_index) - self.memory_allocated(device_index)148
149# Data types150def is_bf16_supported(self):151return torch.mlu.is_bf16_supported()152
153def is_fp16_supported(self):154return True155
156def supported_dtypes(self):157supported_dtypes = [torch.float]158if self.is_fp16_supported():159supported_dtypes.append(torch.half)160if self.is_bf16_supported():161supported_dtypes.append(torch.bfloat16)162return supported_dtypes163
164# Misc165def amp(self):166if hasattr(torch.mlu, 'amp'):167return torch.mlu.amp168return None169
170def is_available(self):171return torch.mlu.is_available()172
173def range_push(self, msg):174if hasattr(torch.mlu.cnpx, 'range_push'):175return torch.mlu.cnpx.range_push(msg)176
177def range_pop(self):178if hasattr(torch.mlu.cnpx, 'range_pop'):179return torch.mlu.cnpx.range_pop()180
181def lazy_call(self, callback):182return torch.mlu._lazy_call(callback)183
184def communication_backend_name(self):185return self._communication_backend_name186
187def is_triton_supported(self):188return True189
190# Graph operations191def create_graph(self):192torch.mlu.MLUGraph()193
194def capture_to_graph(self, graph, pool=None, stream=None):195return torch.mlu.graph(graph, pool, stream)196
197def replay_graph(self, graph):198graph.replay()199return200
201# Tensor operations202
203@property204def BFloat16Tensor(self):205return functools.partial(torch.tensor, dtype=torch.bfloat16, device='mlu')206
207@property208def ByteTensor(self):209return functools.partial(torch.tensor, dtype=torch.uint8, device='mlu')210
211@property212def DoubleTensor(self):213return functools.partial(torch.tensor, dtype=torch.double, device='mlu')214
215@property216def FloatTensor(self):217return functools.partial(torch.tensor, dtype=torch.float, device='mlu')218
219@property220def HalfTensor(self):221return functools.partial(torch.tensor, dtype=torch.half, device='mlu')222
223@property224def IntTensor(self):225return functools.partial(torch.tensor, dtype=torch.int, device='mlu')226
227@property228def LongTensor(self):229return functools.partial(torch.tensor, dtype=torch.long, device='mlu')230
231def pin_memory(self, tensor):232return tensor.pin_memory()233
234def is_pinned(self, tensor):235return tensor.is_pinned()236
237def on_accelerator(self, tensor):238device_str = str(tensor.device)239if device_str.startswith('mlu:'):240return True241else:242return False243
244def op_builder_dir(self):245try:246# is op_builder from deepspeed or a 3p version? this should only succeed if it's deepspeed247# if successful this also means we're doing a local install and not JIT compile path248from op_builder import __deepspeed__ # noqa: F401 # type: ignore249return "op_builder.mlu"250except ImportError:251return "deepspeed.ops.op_builder.mlu"252
253def _lazy_init_class_dict(self):254if self.class_dict:255return256
257op_builder_module = importlib.import_module(self.op_builder_dir())258
259# get op builder class from op_builder/mlu/__init__.py260self.class_dict = {}261for class_name, class_obj in inspect.getmembers(op_builder_module, inspect.isclass):262self.class_dict[class_name] = class_obj263
264# create an instance of op builder and return, name specified by class_name265def create_op_builder(self, class_name):266builder_class = self.get_op_builder(class_name)267return builder_class()268
269# return an op builder class, name specified by class_name270def get_op_builder(self, class_name):271self._lazy_init_class_dict()272if class_name in self.class_dict:273return self.class_dict[class_name]274else:275return self.class_dict['NotImplementedBuilder']276
277def build_extension(self):278from torch.utils.cpp_extension import BuildExtension279return BuildExtension280
281def export_envs(self):282return ['NEUWARE_HOME', 'CNCL', 'LD_LIBRARY', 'PATH']283
284def visible_devices_envs(self):285return ['MLU_VISIBLE_DEVICES']286
287def set_visible_devices_envs(self, current_env, local_accelerator_ids):288for env in self.visible_devices_envs():289current_env[env] = ",".join(map(str, local_accelerator_ids))290
291def get_compile_backend(self):292return self._compile_backend293
294def set_compile_backend(self, backend):295supported_backends = torch._dynamo.list_backends(exclude_tags=())296if backend in supported_backends:297self._compile_backend = backend298else:299raise ValueError(300f"{backend} not supported by {self.device_name()}. Supported Backends are {supported_backends }")301