Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- lib/python3.12/site-packages/__pycache__/decorator.cpython-312.pycAdded
- lib/python3.12/site-packages/__pycache__/ipython_pygments_lexers.cpython-312.pycAdded
- lib/python3.12/site-packages/__pycache__/isympy.cpython-312.pycAdded
- lib/python3.12/site-packages/__pycache__/pylab.cpython-312.pycAdded
- lib/python3.12/site-packages/__pycache__/six.cpython-312.pycAdded
- lib/python3.12/site-packages/click-8.2.1.dist-info/INSTALLERAdded+1
- lib/python3.12/site-packages/click-8.2.1.dist-info/METADATAAdded+82
- lib/python3.12/site-packages/click-8.2.1.dist-info/RECORDAdded+38
- lib/python3.12/site-packages/click-8.2.1.dist-info/WHEELAdded+4
- lib/python3.12/site-packages/click-8.2.1.dist-info/licenses/LICENSE.txtAdded+28
- lib/python3.12/site-packages/deepspeed/__init__.pyAdded+398
- lib/python3.12/site-packages/deepspeed/accelerator/__init__.pyAdded+7
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/abstract_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/cpu_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/cuda_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/hpu_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/mlu_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/mps_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/npu_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/real_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/sdaa_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/xpu_accelerator.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/accelerator/abstract_accelerator.pyAdded+306
- lib/python3.12/site-packages/deepspeed/accelerator/cpu_accelerator.pyAdded+361
- lib/python3.12/site-packages/deepspeed/accelerator/cuda_accelerator.pyAdded+382
- lib/python3.12/site-packages/deepspeed/accelerator/hpu_accelerator.pyAdded+331
- lib/python3.12/site-packages/deepspeed/accelerator/mlu_accelerator.pyAdded+300
- lib/python3.12/site-packages/deepspeed/accelerator/mps_accelerator.pyAdded+281
- lib/python3.12/site-packages/deepspeed/accelerator/npu_accelerator.pyAdded+299
- lib/python3.12/site-packages/deepspeed/accelerator/real_accelerator.pyAdded+308
- lib/python3.12/site-packages/deepspeed/accelerator/sdaa_accelerator.pyAdded+328
- lib/python3.12/site-packages/deepspeed/accelerator/xpu_accelerator.pyAdded+337
- lib/python3.12/site-packages/deepspeed/constants.pyAdded+21
- lib/python3.12/site-packages/deepspeed/env_report.pyAdded+195
- lib/python3.12/site-packages/deepspeed/git_version_info.pyAdded+31
- lib/python3.12/site-packages/deepspeed/git_version_info_installed.pyAdded+6
- lib/python3.12/site-packages/deepspeed/inference/__init__.pyAdded+7
- lib/python3.12/site-packages/deepspeed/inference/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/__pycache__/config.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/__pycache__/engine.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/config.pyAdded+323
- lib/python3.12/site-packages/deepspeed/inference/engine.pyAdded+625
- lib/python3.12/site-packages/deepspeed/inference/quantization/__init__.pyAdded+4
- lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/layers.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/quantization.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/quantization_context.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/utils.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/quantization/layers.pyAdded+114
1pip12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182Metadata-Version: 2.4Name: clickVersion: 8.2.1Summary: Composable command line interface toolkitMaintainer-email: Pallets <contact@palletsprojects.com>Requires-Python: >=3.10Description-Content-Type: text/markdownLicense-Expression: BSD-3-ClauseClassifier: Development Status :: 5 - Production/StableClassifier: Intended Audience :: DevelopersClassifier: Operating System :: OS IndependentClassifier: Programming Language :: PythonClassifier: Typing :: TypedLicense-File: LICENSE.txtRequires-Dist: colorama; platform_system == 'Windows'Project-URL: Changes, https://click.palletsprojects.com/page/changes/Project-URL: Chat, https://discord.gg/palletsProject-URL: Documentation, https://click.palletsprojects.com/Project-URL: Donate, https://palletsprojects.com/donateProject-URL: Source, https://github.com/pallets/click/
# $ click_
Click is a Python package for creating beautiful command line interfacesin a composable way with as little code as necessary. It's the "CommandLine Interface Creation Kit". It's highly configurable but comes withsensible defaults out of the box.
It aims to make the process of writing command line tools quick and funwhile also preventing any frustration caused by the inability toimplement an intended CLI API.
Click in three points:
- Arbitrary nesting of commands- Automatic help page generation- Supports lazy loading of subcommands at runtime
## A Simple Example
```pythonimport click
@click.command()@click.option("--count", default=1, help="Number of greetings.")@click.option("--name", prompt="Your name", help="The person to greet.")def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for _ in range(count): click.echo(f"Hello, {name}!")
if __name__ == '__main__': hello()```
```$ python hello.py --count=3Your name: ClickHello, Click!Hello, Click!Hello, Click!```
## Donate
The Pallets organization develops and supports Click and other popularpackages. In order to grow the community of contributors and users, andallow the maintainers to devote more time to the projects, [pleasedonate today][].
[please donate today]: https://palletsprojects.com/donate
## Contributing
See our [detailed contributing documentation][contrib] for many ways tocontribute, including reporting issues, requesting features, asking or answeringquestions, and making PRs.
[contrib]: https://palletsprojects.com/contributing/
1234567891011121314151617181920212223242526272829303132333435363738click-8.2.1.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
click-8.2.1.dist-info/METADATA,sha256=dI1MbhHTLoKD2tNCCGnx9rK2gok23HDNylFeLKdLSik,2471
click-8.2.1.dist-info/RECORD,,
click-8.2.1.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
click-8.2.1.dist-info/licenses/LICENSE.txt,sha256=morRBqOU6FO_4h9C9OctWSgZoigF2ZG18ydQKSkrZY0,1475
click/__init__.py,sha256=6YyS1aeyknZ0LYweWozNZy0A9nZ_11wmYIhv3cbQrYo,4473
click/__pycache__/__init__.cpython-312.pyc,,
click/__pycache__/_compat.cpython-312.pyc,,
click/__pycache__/_termui_impl.cpython-312.pyc,,
click/__pycache__/_textwrap.cpython-312.pyc,,
click/__pycache__/_winconsole.cpython-312.pyc,,
click/__pycache__/core.cpython-312.pyc,,
click/__pycache__/decorators.cpython-312.pyc,,
click/__pycache__/exceptions.cpython-312.pyc,,
click/__pycache__/formatting.cpython-312.pyc,,
click/__pycache__/globals.cpython-312.pyc,,
click/__pycache__/parser.cpython-312.pyc,,
click/__pycache__/shell_completion.cpython-312.pyc,,
click/__pycache__/termui.cpython-312.pyc,,
click/__pycache__/testing.cpython-312.pyc,,
click/__pycache__/types.cpython-312.pyc,,
click/__pycache__/utils.cpython-312.pyc,,
click/_compat.py,sha256=v3xBZkFbvA1BXPRkFfBJc6-pIwPI7345m-kQEnpVAs4,18693
click/_termui_impl.py,sha256=ASXhLi9IQIc0Js9KQSS-3-SLZcPet3VqysBf9WgbbpI,26712
click/_textwrap.py,sha256=BOae0RQ6vg3FkNgSJyOoGzG1meGMxJ_ukWVZKx_v-0o,1400
click/_winconsole.py,sha256=_vxUuUaxwBhoR0vUWCNuHY8VUefiMdCIyU2SXPqoF-A,8465
click/core.py,sha256=gUhpNS9cFBGdEXXdisGVG-eRvGf49RTyFagxulqwdFw,117343
click/decorators.py,sha256=5P7abhJtAQYp_KHgjUvhMv464ERwOzrv2enNknlwHyQ,18461
click/exceptions.py,sha256=1rdtXgHJ1b3OjGkN-UpXB9t_HCBihJvh_DtpmLmwn9s,9891
click/formatting.py,sha256=Bhqx4QXdKQ9W4WKknIwj5KPKFmtduGOuGq1yw_THLZ8,9726
click/globals.py,sha256=gM-Nh6A4M0HB_SgkaF5M4ncGGMDHc_flHXu9_oh4GEU,1923
click/parser.py,sha256=nU1Ah2p11q29ul1vNdU9swPo_PUuKrxU6YXToi71q1c,18979
click/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
click/shell_completion.py,sha256=CQSGdjgun4ORbOZrXP0CVhEtPx4knsufOkRsDiK64cM,19857
click/termui.py,sha256=vAYrKC2a7f_NfEIhAThEVYfa__ib5XQbTSCGtJlABRA,30847
click/testing.py,sha256=2eLdAaCJCGToP5Tw-XN8JjrDb3wbJIfARxg3d0crW5M,18702
click/types.py,sha256=KBTRxN28cR1VZ5mb9iJX98MQSw_p9SGzljqfEI8z5Tw,38389
click/utils.py,sha256=b1Mm-usEDBHtEwcPltPIn3zSK4nw2KTp5GC7_oSTlLo,202451234Wheel-Version: 1.0Generator: flit 3.12.0Root-Is-Purelib: trueTag: py3-none-any12345678910111213141516171819202122232425262728Copyright 2014 Pallets
Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. 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.
3. 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 NOTLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR APARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTHOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITEDTO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import sysimport typesimport jsonfrom typing import Optional, Unionimport torchfrom torch.optim import Optimizerfrom torch.optim.lr_scheduler import _LRSchedulerfrom packaging import version as pkg_version
# Skip Triton import for AMD due to pytorch-triton-rocm module breaking device API in DeepSpeedif not (hasattr(torch.version, 'hip') and torch.version.hip is not None): try: import triton # noqa: F401 # type: ignore HAS_TRITON = True except ImportError: HAS_TRITON = Falseelse: HAS_TRITON = False
from . import opsfrom . import module_inject
from .accelerator import get_acceleratorfrom .constants import TORCH_DISTRIBUTED_DEFAULT_PORTfrom .runtime.engine import DeepSpeedEngine, DeepSpeedOptimizerCallable, DeepSpeedSchedulerCallablefrom .runtime.engine import ADAM_OPTIMIZER, LAMB_OPTIMIZERfrom .runtime.hybrid_engine import DeepSpeedHybridEnginefrom .runtime.pipe.engine import PipelineEnginefrom .inference.engine import InferenceEnginefrom .inference.config import DeepSpeedInferenceConfigfrom .runtime.lr_schedules import add_tuning_argumentsfrom .runtime.config import DeepSpeedConfig, DeepSpeedConfigErrorfrom .runtime.activation_checkpointing import checkpointingfrom .ops.transformer import DeepSpeedTransformerLayer, DeepSpeedTransformerConfigfrom .module_inject import replace_transformer_layer, revert_transformer_layer, set_autotp_mode
from .utils import log_dist, OnDevice, loggerfrom .comm.comm import init_distributed
from .runtime import zero, dominofrom .runtime.compiler import is_compile_supported
from .pipe import PipelineModule
from .git_version_info import version, git_hash, git_branch
def _parse_version(version_str): '''Parse a version string and extract the major, minor, and patch versions.''' ver = pkg_version.parse(version_str) return ver.major, ver.minor, ver.micro
# Export version information__version__ = version__version_major__, __version_minor__, __version_patch__ = _parse_version(__version__)__git_hash__ = git_hash__git_branch__ = git_branch
# Set to torch's distributed package or deepspeed.comm based inside DeepSpeedEngine initdist = None
def initialize(args=None, model: torch.nn.Module = None, optimizer: Optional[Union[Optimizer, DeepSpeedOptimizerCallable]] = None, model_parameters: Optional[torch.nn.Module] = None, training_data: Optional[torch.utils.data.Dataset] = None, lr_scheduler: Optional[Union[_LRScheduler, DeepSpeedSchedulerCallable]] = None, distributed_port: int = TORCH_DISTRIBUTED_DEFAULT_PORT, mpu=None, dist_init_required: Optional[bool] = None, collate_fn=None, config=None, mesh_param=None, config_params=None): """Initialize the DeepSpeed Engine.
Arguments: args: an object containing local_rank and deepspeed_config fields. This is optional if `config` is passed.
model: Required: nn.module class before apply any wrappers
optimizer: Optional: a user defined Optimizer or Callable that returns an Optimizer object. This overrides any optimizer definition in the DeepSpeed json config.
model_parameters: Optional: An iterable of torch.Tensors or dicts. Specifies what Tensors should be optimized.
training_data: Optional: Dataset of type torch.utils.data.Dataset
lr_scheduler: Optional: Learning Rate Scheduler Object or a Callable that takes an Optimizer and returns a Scheduler object. The scheduler object should define a get_lr(), step(), state_dict(), and load_state_dict() methods
distributed_port: Optional: Master node (rank 0)'s free port that needs to be used for communication during distributed training
mpu: Optional: A model parallelism unit object that implements get_{model,data}_parallel_{rank,group,world_size}()
dist_init_required: Optional: None will auto-initialize torch distributed if needed, otherwise the user can force it to be initialized or not via boolean.
collate_fn: Optional: Merges a list of samples to form a mini-batch of Tensor(s). Used when using batched loading from a map-style dataset.
config: Optional: Instead of requiring args.deepspeed_config you can pass your deepspeed config as an argument instead, as a path or a dictionary.
config_params: Optional: Same as `config`, kept for backwards compatibility.
Returns: A tuple of ``engine``, ``optimizer``, ``training_dataloader``, ``lr_scheduler``
* ``engine``: DeepSpeed runtime engine which wraps the client model for distributed training.
* ``optimizer``: Wrapped optimizer if a user defined ``optimizer`` is supplied, or if optimizer is specified in json config else ``None``.
* ``training_dataloader``: DeepSpeed dataloader if ``training_data`` was supplied, otherwise ``None``.
* ``lr_scheduler``: Wrapped lr scheduler if user ``lr_scheduler`` is passed, or if ``lr_scheduler`` specified in JSON configuration. Otherwise ``None``. """ log_dist("DeepSpeed info: version={}, git-hash={}, git-branch={}".format(__version__, __git_hash__, __git_branch__), ranks=[0])
# Disable zero.Init context if it's currently enabled zero.partition_parameters.shutdown_init_context()
assert model is not None, "deepspeed.initialize requires a model"
global dist from deepspeed import comm as dist dist_backend = get_accelerator().communication_backend_name() dist.init_distributed(dist_backend=dist_backend, distributed_port=distributed_port, dist_init_required=dist_init_required)
##TODO: combine reuse mpu as mesh device and vice versa # Set config using config_params for backwards compat if config is None and config_params is not None: config = config_params
mesh_device = None if mesh_param: logger.info(f"mesh_param to Initialize mesh device: {mesh_param}") mesh_device = dist.initialize_mesh_device(mesh_param, ("data_parallel", "sequence_parallel")) #if config file has sequence parallelize and data parallelize, then use them to initialize mesh device elif config is not None: if "sequence_parallel_size" in config and "data_parallel_size" in config: logger.info(f"config to Initialize mesh device: {config}") mesh_device = dist.initialize_mesh_device((config["data_parallel_size"], config["sequence_parallel_size"]), \ ("data_parallel", "sequence_parallel"))
# Check for deepscale_config for backwards compat if hasattr(args, "deepscale_config") and args.deepscale_config is not None: logger.warning("************ --deepscale_config is deprecated, please use --deepspeed_config ************") if hasattr(args, "deepspeed_config"): assert (args.deepspeed_config is None), "Not sure how to proceed, we were given both a deepscale_config and deepspeed_config" args.deepspeed_config = args.deepscale_config args.deepscale_config = None
# Check that we have only one config passed if hasattr(args, "deepspeed_config") and args.deepspeed_config is not None: assert config is None, "Not sure how to proceed, we were given deepspeed configs in the deepspeed arguments and deepspeed.initialize() function call" config = args.deepspeed_config assert config is not None, "DeepSpeed requires --deepspeed_config to specify configuration file" if not isinstance(model, PipelineModule): config_class = DeepSpeedConfig(config, mpu, mesh_device=mesh_device) if config_class.hybrid_engine.enabled: engine = DeepSpeedHybridEngine(args=args, model=model, optimizer=optimizer, model_parameters=model_parameters, training_data=training_data, lr_scheduler=lr_scheduler, mpu=mpu, dist_init_required=dist_init_required, collate_fn=collate_fn, config=config, config_class=config_class) else: engine = DeepSpeedEngine(args=args, model=model, optimizer=optimizer, model_parameters=model_parameters, training_data=training_data, lr_scheduler=lr_scheduler, mpu=mpu, dist_init_required=dist_init_required, collate_fn=collate_fn, config=config, mesh_device=mesh_device, config_class=config_class) else: assert mpu is None, "mpu must be None with pipeline parallelism" mpu = model.mpu() config_class = DeepSpeedConfig(config, mpu) engine = PipelineEngine(args=args, model=model, optimizer=optimizer, model_parameters=model_parameters, training_data=training_data, lr_scheduler=lr_scheduler, mpu=mpu, dist_init_required=dist_init_required, collate_fn=collate_fn, config=config, config_class=config_class)
# Restore zero.Init context if necessary zero.partition_parameters.restore_init_context()
return_items = [ engine, engine.optimizer, engine.training_dataloader, engine.lr_scheduler, ] return tuple(return_items)
def _add_core_arguments(parser): r"""Helper (internal) function to update an argument parser with an argument group of the core DeepSpeed arguments. The core set of DeepSpeed arguments include the following: 1) --deepspeed: boolean flag to enable DeepSpeed 2) --deepspeed_config <json file path>: path of a json configuration file to configure DeepSpeed runtime.
This is a helper function to the public add_config_arguments()
Arguments: parser: argument parser Return: parser: Updated Parser """ group = parser.add_argument_group('DeepSpeed', 'DeepSpeed configurations')
group.add_argument('--deepspeed', default=False, action='store_true', help='Enable DeepSpeed (helper flag for user code, no impact on DeepSpeed backend)')
group.add_argument('--deepspeed_config', default=None, type=str, help='DeepSpeed json configuration file.')
group.add_argument('--deepscale', default=False, action='store_true', help='Deprecated enable DeepSpeed (helper flag for user code, no impact on DeepSpeed backend)')
group.add_argument('--deepscale_config', default=None, type=str, help='Deprecated DeepSpeed json configuration file.')
return parser
def add_config_arguments(parser): r"""Update the argument parser to enabling parsing of DeepSpeed command line arguments. The set of DeepSpeed arguments include the following: 1) --deepspeed: boolean flag to enable DeepSpeed 2) --deepspeed_config <json file path>: path of a json configuration file to configure DeepSpeed runtime.
Arguments: parser: argument parser Return: parser: Updated Parser """ parser = _add_core_arguments(parser)
return parser
def default_inference_config(): """ Return a default DeepSpeed inference configuration dictionary. """ return DeepSpeedInferenceConfig().dict()
def init_inference(model, config=None, **kwargs): """Initialize the DeepSpeed InferenceEngine.
Description: all four cases are valid and supported in DS init_inference() API.
# Case 1: user provides no config and no kwargs. Default config will be used.
.. code-block:: python
generator.model = deepspeed.init_inference(generator.model) string = generator("DeepSpeed is") print(string)
# Case 2: user provides a config and no kwargs. User supplied config will be used.
.. code-block:: python
generator.model = deepspeed.init_inference(generator.model, config=config) string = generator("DeepSpeed is") print(string)
# Case 3: user provides no config and uses keyword arguments (kwargs) only.
.. code-block:: python
generator.model = deepspeed.init_inference(generator.model, tensor_parallel={"tp_size": world_size}, dtype=torch.half, replace_with_kernel_inject=True) string = generator("DeepSpeed is") print(string)
# Case 4: user provides config and keyword arguments (kwargs). Both config and kwargs are merged and kwargs take precedence.
.. code-block:: python
generator.model = deepspeed.init_inference(generator.model, config={"dtype": torch.half}, replace_with_kernel_inject=True) string = generator("DeepSpeed is") print(string)
Arguments: model: Required: original nn.module object without any wrappers
config: Optional: instead of arguments, you can pass in a DS inference config dict or path to JSON file
Returns: A deepspeed.InferenceEngine wrapped model. """ log_dist("DeepSpeed info: version={}, git-hash={}, git-branch={}".format(__version__, __git_hash__, __git_branch__), ranks=[0])
# Load config_dict from config first if config is None: config = {} if isinstance(config, str): with open(config, "r") as f: config_dict = json.load(f) elif isinstance(config, dict): config_dict = config else: raise ValueError(f"'config' argument expected string or dictionary, got {type(config)}")
# Update with values from kwargs, ensuring no conflicting overlap between config and kwargs overlap_keys = set(config_dict.keys()).intersection(kwargs.keys()) # If there is overlap, error out if values are different for key in overlap_keys: if config_dict[key] != kwargs[key]: raise ValueError(f"Conflicting argument '{key}' in 'config':{config_dict[key]} and kwargs:{kwargs[key]}") config_dict.update(kwargs)
ds_inference_config = DeepSpeedInferenceConfig(**config_dict)
engine = InferenceEngine(model, config=ds_inference_config)
return engine
def tp_model_init(model, tp_size, dtype, config=None, **kwargs): """ Initialize the model for tensor parallelism.
Args: model (torch.nn.Module): The model to be initialized. tp_size (int): The tensor parallelism size. dtype (torch.dtype): The data type to be used for the model.
Returns: torch.nn.Module: The initialized model with tensor parallelism. """ # avoid re-entry if hasattr(model, 'ds_autotp_parsed'): logger.warning("ds_autotp_parsed' attribute already exists in the model, re-entry is not allowed.") return
set_autotp_mode(training=True)
from deepspeed.runtime.tensor_parallel import TpTrainingManager # The expected usage here is for it to be invoked by transformers package.
#TODO: We should provide a custom TP mapping solution without using autoTP #as modifying the autoTP logic may be more difficult for users compared to configuring it
model = TpTrainingManager(model=model, tp_size=tp_size, dtype=dtype).module
setattr(model, 'ds_autotp_parsed', True)
return model1234567# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from .abstract_accelerator import DeepSpeedAcceleratorfrom .real_accelerator import get_accelerator, set_accelerator, is_current_accelerator_supported123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import abcfrom abc import ABC
class DeepSpeedAccelerator(ABC):
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 amp(self): ...
@abc.abstractmethod def is_available(self): ...
@abc.abstractmethod def range_push(self, msg): ...
@abc.abstractmethod def range_pop(self): ...
@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): ...123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361# 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 torchexcept ImportError as e: pass
try: import oneccl_bindings_for_pytorch # noqa: F401 # type: ignore oneccl_imported_p = Trueexcept ImportError as e: oneccl_imported_p = False
import os
# accelerator for Intel CPUclass 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 amp(self): return torch.cpu.amp
def is_available(self): return True
def range_push(self, msg): # TODO itt is currently not supported yet # return torch.profiler.itt.range_push(msg) return
def range_pop(self): # 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: 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}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import functoolsimport osimport pkgutilimport importlibimport 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.cudaexcept ImportError: pass
# Delay import pynvml to avoid import error when CUDA is not availablepynvml = None
class CUDA_Accelerator(DeepSpeedAccelerator):
def __init__(self): self._name = 'cuda' self._communication_backend_name = 'nccl' if sys.platform != 'win32' else 'gloo' self._compile_backend = "inductor" 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 device(self, device_index=None): return torch.cuda.device(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 amp(self): if hasattr(torch.cuda, 'amp'): return torch.cuda.amp return None
def is_available(self): return torch.cuda.is_available()
def range_push(self, msg): if hasattr(torch.cuda.nvtx, 'range_push'): return torch.cuda.nvtx.range_push(msg)
def range_pop(self): if hasattr(torch.cuda.nvtx, 'range_pop'): return torch.cuda.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): 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}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import functoolsimport osimport pkgutilimport importlibimport 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( f"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 amp(self): return None
def is_available(self): return self.hpu.is_available()
def range_push(self, msg): return
def range_pop(self): 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}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Teamimport importlibimport inspectimport functools
from .abstract_accelerator import DeepSpeedAcceleratorimport 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 amp(self): if hasattr(torch.mlu, 'amp'): return torch.mlu.amp return None
def is_available(self): return torch.mlu.is_available()
def range_push(self, msg): if hasattr(torch.mlu.cnpx, 'range_push'): return torch.mlu.cnpx.range_push(msg)
def range_pop(self): 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 }")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281# 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.mpsexcept 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): 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 "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 amp(self): return
def is_available(self): return hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
def range_push(self, msg): return
def range_pop(self): 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}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Teamimport importlibimport 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.npuexcept 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.npu.device(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 amp(self): if hasattr(torch.npu, 'amp'): return torch.npu.amp return None
def is_available(self): return torch.npu.is_available()
def range_push(self, msg): return
def range_pop(self): 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 }")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Teamimport 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_loggerexcept ImportError as e: accel_logger = None
try: from accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa1except ImportError as e: dsa1 = Nonetry: from deepspeed.accelerator.abstract_accelerator import DeepSpeedAccelerator as dsa2except ImportError as e: dsa2 = None
SUPPORTED_ACCELERATOR_LIST = ['cuda', 'cpu', 'xpu', 'xpu.external', 'npu', 'mps', 'hpu', 'mlu', 'sdaa']
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 intel_extension_for_pytorch as ipex assert ipex._C._has_xpu(), "XPU_Accelerator requires an intel_extension_for_pytorch that supports XPU." except ImportError as e: raise ValueError( f"XPU_Accelerator requires intel_extension_for_pytorch, which is not installed on this system.") elif accelerator_name == "xpu.external": try: import intel_extension_for_deepspeed # noqa: F401 # type: ignore except ImportError as e: raise ValueError( f"XPU_Accelerator external requires intel_extension_for_deepspeed, which is not installed on this system." ) elif accelerator_name == "cpu": pass elif accelerator_name == "npu": try: import torch_npu # noqa: F401 # type: ignore except ImportError as e: raise ValueError(f"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(f"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(f"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( f"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(f"MLU_Accelerator requires torch_mlu, 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: from intel_extension_for_deepspeed import XPU_Accelerator # noqa: F401,F811 # type: ignore accelerator_name = "xpu.external" except ImportError as e: pass if accelerator_name is None: try: import intel_extension_for_pytorch as ipex
if ipex._C._has_xpu(): accelerator_name = "xpu" except ImportError as e: pass if accelerator_name is None: try: import torch
# torch.xpu will be supported in upstream pytorch-2.8. # Currently we can run on xpu device only using pytorch, # also reserve the old path using ipex when the torch version is old. if hasattr(torch, 'xpu'): if torch.cuda.device_count() == 0: #ignore-cuda if torch.xpu.device_count() > 0 and torch.xpu.is_available(): accelerator_name = "xpu" else: pass 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: 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.external": # XPU_Accelerator is already imported in detection stage ds_accelerator = XPU_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() _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: 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_acceleratormy_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_Acceleratorcu_accel = CUDA_Accelerator()logger.info(f'{id(cu_accel)=}')from deepspeed.accelerator import set_accelerator, get_acceleratorset_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)=139648165478304my_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---------"""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328# 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 importlibimport inspectimport 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.sdaaexcept 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 amp(self): if hasattr(torch.sdaa, 'amp'): return torch.sdaa.amp return None
def is_available(self): return torch.sdaa.is_available()
def range_push(self, msg): return
def range_pop(self): 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}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torchfrom deepspeed.accelerator.abstract_accelerator import DeepSpeedAcceleratorimport functoolsimport importlibimport inspect
try: import oneccl_bindings_for_pytorch # noqa: F401 # type: ignore oneccl_imported_p = Trueexcept ImportError as e: oneccl_imported_p = False
try: import intel_extension_for_pytorch as ipex # noqa: F401 # type: ignore ipex_imported_p = Trueexcept ImportError as e: ipex_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): if not ipex_imported_p: return self.is_synchronized_device() else: # WA XPU event will be consolidated in 2.6 if ipex.__version__ < '2.6': return True else: 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.xpu.device(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 amp(self): return torch.xpu.amp
def is_available(self): return torch.xpu.is_available()
def range_push(self, msg): # TODO itt is currently not supported yet # return torch.profiler.itt.range_push(msg) return
def range_pop(self): # 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): if ipex_imported_p: try: from intel_extension_for_pytorch.xpu.cpp_extension import DpcppBuildExtension except ImportError: from intel_extension_for_pytorch.xpu.utils import DpcppBuildExtension else: from torch.utils.cpp_extension import DpcppBuildExtension return DpcppBuildExtension
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}")123456789101112131415161718192021# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import osfrom datetime import timedelta
############################################## Torch distributed constants#############################################TORCH_DISTRIBUTED_DEFAULT_PORT = 29500
# Default process group wide timeout, if applicable.# This only applies to the gloo and nccl backends# (only if NCCL_BLOCKING_WAIT or NCCL_ASYNC_ERROR_HANDLING is set to 1).# To make an attempt at backwards compatibility with THD, we use an# extraordinarily high default timeout, given that THD did not have timeouts.default_pg_timeout = timedelta(minutes=int(os.getenv("DEEPSPEED_TIMEOUT", default=30)))INFERENCE_GENERIC_MODE = 'generic'INFERENCE_SPECIALIZED_MODE = 'specialized'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import osimport torchimport deepspeedimport subprocessimport argparsefrom .ops.op_builder.all_ops import ALL_OPSfrom .git_version_info import installed_ops, torch_info, accelerator_namefrom deepspeed.accelerator import get_accelerator
GREEN = '\033[92m'RED = '\033[91m'YELLOW = '\033[93m'END = '\033[0m'SUCCESS = f"{GREEN} [SUCCESS] {END}"OKAY = f"{GREEN}[OKAY]{END}"WARNING = f"{YELLOW}[WARNING]{END}"FAIL = f'{RED}[FAIL]{END}'INFO = '[INFO]'
color_len = len(GREEN) + len(END)okay = f"{GREEN}[OKAY]{END}"warning = f"{YELLOW}[WARNING]{END}"
def op_report(verbose=True): max_dots = 23 max_dots2 = 11 h = ["op name", "installed", "compatible"] print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1]))) print("DeepSpeed C++/CUDA extension op report") print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
print("NOTE: Ops not installed will be just-in-time (JIT) compiled at\n" " runtime if needed. Op compatibility means that your system\n" " meet the required dependencies to JIT install the op.")
print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1]))) print("JIT compiled ops requires ninja") ninja_status = OKAY if ninja_installed() else FAIL print('ninja', "." * (max_dots - 5), ninja_status) print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1]))) print(h[0], "." * (max_dots - len(h[0])), h[1], "." * (max_dots2 - len(h[1])), h[2]) print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1]))) installed = f"{GREEN}[YES]{END}" no = f"{YELLOW}[NO]{END}" for op_name, builder in ALL_OPS.items(): dots = "." * (max_dots - len(op_name)) is_compatible = OKAY if builder.is_compatible(verbose) else no is_installed = installed if installed_ops.get(op_name, False) and accelerator_name == get_accelerator()._name else no dots2 = '.' * ((len(h[1]) + (max_dots2 - len(h[1]))) - (len(is_installed) - color_len)) print(op_name, dots, is_installed, dots2, is_compatible) print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
def ninja_installed(): try: import ninja # noqa: F401 # type: ignore except ImportError: return False return True
def nvcc_version(): import torch.utils.cpp_extension cuda_home = torch.utils.cpp_extension.CUDA_HOME if cuda_home is None: return f"{RED} [FAIL] cannot find CUDA_HOME via torch.utils.cpp_extension.CUDA_HOME={torch.utils.cpp_extension.CUDA_HOME} {END}" try: output = subprocess.check_output([cuda_home + "/bin/nvcc", "-V"], universal_newlines=True) except FileNotFoundError: return f"{RED} [FAIL] nvcc missing {END}" output_split = output.split() release_idx = output_split.index("release") release = output_split[release_idx + 1].replace(',', '').split(".") return ".".join(release)
def installed_cann_path(): if "ASCEND_HOME_PATH" in os.environ or os.path.exists(os.environ["ASCEND_HOME_PATH"]): return os.environ["ASCEND_HOME_PATH"] return None
def installed_cann_version(): import re ascend_path = installed_cann_path() if ascend_path is None: return f"CANN_HOME does not exist, unable to compile NPU op(s)" cann_version = "" for dirpath, _, filenames in os.walk(os.path.realpath(ascend_path)): if cann_version: break install_files = [file for file in filenames if re.match(r"ascend_.*_install\.info", file)] if install_files: filepath = os.path.join(dirpath, install_files[0]) with open(filepath, "r") as f: for line in f: if line.find("version") != -1: cann_version = line.strip().split("=")[-1] break return cann_version
def get_shm_size(): try: shm_stats = os.statvfs('/dev/shm') except (OSError, FileNotFoundError, ValueError, AttributeError): return "UNKNOWN", None
shm_size = shm_stats.f_frsize * shm_stats.f_blocks shm_hbytes = human_readable_size(shm_size) warn = [] if shm_size < 512 * 1024**2: warn.append( f" {YELLOW} [WARNING] /dev/shm size might be too small, if running in docker increase to at least --shm-size='1gb' {END}" ) if get_accelerator().communication_backend_name() == "nccl": warn.append( f" {YELLOW} [WARNING] see more details about NCCL requirements: https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/troubleshooting.html#sharing-data {END}" ) return shm_hbytes, warn
def human_readable_size(size): units = ['B', 'KB', 'MB', 'GB', 'TB'] i = 0 while size >= 1024 and i < len(units) - 1: size /= 1024 i += 1 return f'{size:.2f} {units[i]}'
def debug_report(): max_dots = 33
report = [("torch install path", torch.__path__), ("torch version", torch.__version__), ("deepspeed install path", deepspeed.__path__), ("deepspeed info", f"{deepspeed.__version__}, {deepspeed.__git_hash__}, {deepspeed.__git_branch__}")] if get_accelerator().device_name() == 'cuda': hip_version = getattr(torch.version, "hip", None) report.extend([("torch cuda version", torch.version.cuda), ("torch hip version", hip_version), ("nvcc version", (None if hip_version else nvcc_version())), ("deepspeed wheel compiled w.", f"torch {torch_info['version']}, " + (f"hip {torch_info['hip_version']}" if hip_version else f"cuda {torch_info['cuda_version']}")) ]) elif get_accelerator().device_name() == 'npu': import torch_npu report.extend([("deepspeed wheel compiled w.", f"torch {torch_info['version']}"), ("torch_npu install path", torch_npu.__path__), ("torch_npu version", torch_npu.__version__), ("ascend_cann version", installed_cann_version())]) else: report.extend([("deepspeed wheel compiled w.", f"torch {torch_info['version']} ")])
report.append(("shared memory (/dev/shm) size", get_shm_size()))
print("DeepSpeed general environment info:") for name, value in report: warns = [] if isinstance(value, tuple): value, warns = value print(name, "." * (max_dots - len(name)), value) if warns: for warn in warns: print(warn)
def parse_arguments(): parser = argparse.ArgumentParser() parser.add_argument('--hide_operator_status', action='store_true', help='Suppress display of installation and compatibility statuses of DeepSpeed operators. ') parser.add_argument('--hide_errors_and_warnings', action='store_true', help='Suppress warning and error messages.') args = parser.parse_args() return args
def main(hide_operator_status=False, hide_errors_and_warnings=False): if not hide_operator_status: op_report(verbose=not hide_errors_and_warnings) debug_report()
def cli_main(): args = parse_arguments() main(hide_operator_status=args.hide_operator_status, hide_errors_and_warnings=args.hide_errors_and_warnings)
if __name__ == "__main__": main()12345678910111213141516171819202122232425262728293031# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
try: # This is populated by setup.py from .git_version_info_installed import * # noqa: F401 # type: ignoreexcept ModuleNotFoundError: import os if os.path.isfile('version.txt'): # Will be missing from checkouts that haven't been installed (e.g., readthedocs) version = open('version.txt', 'r').read().strip() else: version = "0.0.0" git_hash = '[none]' git_branch = '[none]'
from .ops.op_builder.all_ops import ALL_OPS installed_ops = dict.fromkeys(ALL_OPS.keys(), False) accelerator_name = "" torch_info = {'version': "0.0", "cuda_version": "0.0", "hip_version": "0.0"}
# compatible_ops list is recreated for each launchfrom .ops.op_builder.all_ops import ALL_OPS
compatible_ops = dict.fromkeys(ALL_OPS.keys(), False)for op_name, builder in ALL_OPS.items(): op_compatible = builder.is_compatible() compatible_ops[op_name] = op_compatible compatible_ops["deepspeed_not_implemented"] = False123456version='0.17.0'git_hash='unknown'git_branch='unknown'installed_ops={'deepspeed_not_implemented': False, 'async_io': False, 'deepspeed_ccl_comm': False, 'deepspeed_shm_comm': False, 'cpu_adam': False, 'fused_adam': False}accelerator_name='cpu'torch_info={'version': '0.0', 'bf16_support': False, 'cuda_version': '0.0', 'nccl_version': '0.0', 'hip_version': '0.0'}1234567# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Teamfrom .v2 import RaggedInferenceEngineConfig, DeepSpeedTPConfigfrom .v2.engine_v2 import InferenceEngineV2from .v2 import build_hf_engine, build_engine_from_ds_checkpoint123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torchimport deepspeedfrom pydantic import Field, field_validatorfrom deepspeed.runtime.config_utils import DeepSpeedConfigModelfrom deepspeed.runtime.zero.config import DeepSpeedZeroConfigfrom typing import Dict, Union, Optionalfrom enum import Enum
class DtypeEnum(Enum): fp16 = (torch.float16, "torch.float16", "fp16", "float16", "half") fp32 = (torch.float32, "torch.float32", "fp32", "float32", "float") bf16 = (torch.bfloat16, "torch.bfloat16", "bf16", "bfloat16", "bfloat") int8 = (torch.int8, "torch.int8", "int8")
@classmethod def from_str(cls, value: str): for dtype in cls: if value in dtype.value: return dtype raise ValueError(f"'{value}' is not a valid DtypeEnum")
class MoETypeEnum(str, Enum): residual = "residual" standard = "standard"
class DeepSpeedTPConfig(DeepSpeedConfigModel): """ Configure tensor parallelism settings """
enabled: bool = True """ Turn tensor parallelism on/off. """
tp_size: int = 1 """ Number of devices to split the model across using tensor parallelism. """
tp_grain_size: int = 64 "Desired MLP/lm_head tp size granularity. DNN library favors tensor size in granularity of power of 2, we pick 64 as a default size."
mpu: object = None """ A model parallelism unit object that implements ``get_{model,data}_parallel_{rank,group,world_size}()``. """
tp_group: object = None
class DeepSpeedMoEConfig(DeepSpeedConfigModel): """ Sets parameters for MoE """
enabled: bool = True ep_size: int = 1 """ The expert-parallelism size which is used for partitioning the experts across the GPUs in the expert-parallel group. """
moe_experts: list = Field([1], alias="num_experts") """ The global number of experts used in an MoE layer. """
type: MoETypeEnum = MoETypeEnum.standard """ Specify the type of MoE layer. We have two types of MoE layer: 'Standard' and 'Residual'. """
ep_mp_group: object = None ep_group: object = Field(None, alias="expert_group")
class QuantTypeEnum(str, Enum): asym = "asymmetric" sym = "symmetric"
class BaseQuantConfig(DeepSpeedConfigModel): enabled: bool = True num_bits: int = 8 q_type: QuantTypeEnum = QuantTypeEnum.sym q_groups: int = 1
class WeightQuantConfig(BaseQuantConfig): enabled: bool = True quantized_initialization: Dict = {} post_init_quant: Dict = {}
class ActivationQuantConfig(BaseQuantConfig): enabled: bool = True
class QKVQuantConfig(DeepSpeedConfigModel): enabled: bool = True
class QuantizationConfig(DeepSpeedConfigModel): enabled: bool = True activation: ActivationQuantConfig = ActivationQuantConfig() weight: WeightQuantConfig = WeightQuantConfig() qkv: QKVQuantConfig = QKVQuantConfig()
# todo: brainstorm on how to do ckpt loading for DS inferenceclass InferenceCheckpointConfig(DeepSpeedConfigModel): checkpoint_dir: Optional[str] = None save_mp_checkpoint_path: Optional[str] = None base_dir: Optional[str] = None
class DeepSpeedInferenceConfig(DeepSpeedConfigModel): """ Sets parameters for DeepSpeed Inference Engine. """
replace_with_kernel_inject: bool = Field(False, alias="kernel_inject") """ Set to true to inject inference kernels for models such as, Bert, GPT2, GPT-Neo and GPT-J. Otherwise, the injection_dict provides the names of two linear layers as a tuple: `(attention_output projection, transformer output projection)` """
dtype: torch.dtype = torch.float16 """ Desired model data type, will convert model to this type. Supported target types: `torch.half`, `torch.int8`, `torch.float` """
tensor_parallel: DeepSpeedTPConfig = Field({}, alias="tp") """ Configuration for tensor parallelism used to split the model across several GPUs. Expects a dictionary containing values for :any:`DeepSpeedTPConfig`. """
enable_cuda_graph: bool = False """ Use this flag for capturing the CUDA-Graph of the inference ops, so that it can run faster using the graph replay method. """
use_triton: bool = False """ Use this flag to use triton kernels for inference ops. """
triton_autotune: bool = False """ Use this flag to enable triton autotuning. Turning it on is better for performance but increase the 1st runtime for autotuning. """
zero: DeepSpeedZeroConfig = {} """ ZeRO configuration to use with the Inference Engine. Expects a dictionary containing values for :any:`DeepSpeedZeroConfig`. """
triangular_masking: bool = Field(True, alias="tm") """ Controls the type of masking for attention scores in transformer layer. Note that the masking is application specific. """
moe: Union[bool, DeepSpeedMoEConfig] = {} """ Specify if the type of Transformer is MoE. Expects a dictionary containing values for :any:`DeepSpeedMoEConfig`. """
keep_module_on_host: bool = False """ When loading checkpoints to model parameters, they are moved to the device. In very large models this might fill the device and cause OOM. Setting this flag to true, will keep checkpoints on host and not move them directly to the device (giving an option to quantize checkpoint data before moving it to the device for example). Set only for models with injection policies and auto TP. """
quant: QuantizationConfig = {} """ NOTE: only works for int8 dtype. Quantization settings used for quantizing your model using the MoQ. The setting can be one element or a tuple. If one value is passed in, we consider it as the number of groups used in quantization. A tuple is passed in if we want to mention that there is extra-grouping for the MLP part of a Transformer layer (e.g. (True, 8) shows we quantize the model using 8 groups for all the network except the MLP part that we use 8 extra grouping). Expects a dictionary containing values for :any:`QuantizationConfig`. """
#todo: refactor the following 3 into the new checkpoint_config checkpoint: Optional[Union[str, Dict]] = None """ Path to deepspeed compatible checkpoint or path to JSON with load policy. """
base_dir: str = "" """ This shows the root directory under which all the checkpoint files exists. This can be passed through the json config too. """
set_empty_params: bool = False """ specifying whether the inference-module is created with empty or real Tensor """
save_mp_checkpoint_path: Optional[str] = None """ The path for which we want to save the loaded model with a checkpoint. This feature is used for adjusting the parallelism degree to help alleviate the model loading overhead. It does not save any new checkpoint if no path is passed. """
checkpoint_config: InferenceCheckpointConfig = Field({}, alias="ckpt_config") """ TODO: Add docs. Expects a dictionary containing values for :any:`InferenceCheckpointConfig`. """
return_tuple: bool = True """ Specify whether or not the transformer layers need to return a tuple or a Tensor. """
training_mp_size: int = 1 """ If loading a checkpoint this is the mp size that it was trained with, it may be different than what the mp size that you want to use during inference. """
replace_method: str = Field( "auto", json_schema_extra={ "deprecated": True, "deprecated_msg": "This parameter is no longer needed, please remove from your call to DeepSpeed-inference" })
injection_policy: Optional[Dict] = Field(None, alias="injection_dict") """ Dictionary mapping a client nn.Module to its corresponding injection policy. e.g., `{BertLayer : deepspeed.inference.HFBertLayerPolicy}` """
injection_policy_tuple: Optional[tuple] = None """ TODO: Add docs """
config: Optional[Dict] = Field(None, alias="args") # todo: really no need for this field if we can refactor
max_out_tokens: int = Field(1024, alias="max_tokens") """ This argument shows the maximum number of tokens inference-engine can work with, including the input and output tokens. Please consider increasing it to the required token-length required for your use-case. """
min_out_tokens: int = Field(1, alias="min_tokens") """ This argument communicates to the runtime the minimum number of tokens you expect you will need to generate. This will cause the runtime to error if it unable to provide this and provide context on the memory pressure rather than seg-faulting or providing corrupted output. """
transposed_mode: bool = Field(False, alias="transposed_mode")
mp_size: int = Field(1, json_schema_extra={"deprecated": True, "new_param": "tensor_parallel.tp_size"}) """ Desired model parallel size, default is 1 meaning no model parallelism. Deprecated, please use the ``tensor_parallel` config to control model parallelism. """ mpu: object = Field(None, json_schema_extra={"deprecated": True, "new_param": "tensor_parallel.mpu"}) ep_size: int = Field(1, json_schema_extra={"deprecated": True, "new_param": "moe.ep_size"}) ep_group: object = Field(None, alias="expert_group", json_schema_extra={ "deprecated": True, "new_param": "moe.ep_group" }) ep_mp_group: object = Field(None, alias="expert_mp_group", json_schema_extra={ "deprecated": True, "new_param": "moe.ep_mp_group" }) moe_experts: list = Field([1], json_schema_extra={"deprecated": True, "new_param": "moe.moe_experts"}) moe_type: MoETypeEnum = Field(MoETypeEnum.standard, json_schema_extra={ "deprecated": True, "new_param": "moe.type" })
@field_validator("dtype", mode="before") def validate_dtype(cls, field_value, values): if isinstance(field_value, str): return DtypeEnum.from_str(field_value).value[0] if isinstance(field_value, torch.dtype): return field_value raise TypeError(f"Invalid type for dtype: {type(field_value)}")
@field_validator("moe") def moe_backward_compat(cls, field_value, values): if isinstance(field_value, bool): return DeepSpeedMoEConfig(moe=field_value) return field_value
@field_validator("use_triton") def has_triton(cls, field_value, values): if field_value and not deepspeed.HAS_TRITON: raise ValueError('Triton needs to be installed to use deepspeed with triton kernels') return field_value123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torchimport timeimport osimport deepspeedfrom deepspeed import comm as distfrom deepspeed.utils.logging import log_dist
from torch.nn.modules import Modulefrom packaging import version as pkg_versionfrom deepspeed.runtime.checkpoint_engine.torch_checkpoint_engine import TorchCheckpointEnginefrom deepspeed.utils.timer import SynchronizedWallClockTimerfrom deepspeed.runtime.compiler import is_compile_supportedfrom ..runtime.state_dict_factory import SDLoaderFactoryfrom ..runtime.weight_quantizer import WeightQuantizationfrom ..module_inject import replace_transformer_layer, generic_injectionfrom ..comm.comm import init_distributedfrom ..pipe import PipelineModulefrom ..moe.utils import has_moe_layersfrom ..module_inject import LinearAllreduce, LinearLayer, Normalize, ReplaceWithTensorSlicingfrom deepspeed.accelerator import get_acceleratorfrom ..module_inject.policy import TransformerPolicyfrom ..module_inject.auto_tp import AutoTP
from ..module_inject.replace_policy import generic_policiesfrom ..module_inject.auto_tp_model_utils import build_bloom_alibi_tensor, build_mpt_atten_bias_tensor, build_mpt_alibi_tensor, get_alibi_maskfrom ..ops.transformer.inference.ds_attention import DeepSpeedSelfAttentionfrom ..model_implementations.transformers.ds_transformer import DeepSpeedTransformerInference
DS_INFERENCE_ENABLED = Falsefrom torch import nn
INFERENCE_MODEL_TIMER = "model-forward-inference"
class InferenceEngine(Module): inference_mp_group = None inference_ep_group = None expert_mp_group = None
def __init__(self, model, config): """ Args: model: torch.nn.Module config: DeepSpeedInferenceConfig """ global DS_INFERENCE_ENABLED DS_INFERENCE_ENABLED = True
super().__init__() if DeepSpeedTransformerInference.workspace is not None: self.destroy()
self.module = model self._config = config
self._get_model_config_generate(config) # keep for weird backward compatibility
# patch model generate with ours if model uses it if hasattr(self.module, "generate"): self.generate = self._generate
if hasattr(self.module, "config"): TransformerPolicy.hf_model_config = self.module.config
if config.dtype not in get_accelerator().supported_dtypes(): raise ValueError( f"Data type {config.dtype} is not supported by {get_accelerator().device_name()} accelerator")
# todo: keep this self.injection_dict because we don't use to change config.injection_policy API # todo: this will get changed when Molly's PR on auto injection dict is merged self.injection_dict = config.injection_policy
# todo: refactor the mp_group and mp_size related in the next refactor self.mp_group = config.tensor_parallel.tp_group self.mpu = config.tensor_parallel.mpu
self.quantize_merge_count = 1 self.quantization_scales = None
# these are not needed in the config as we are creating them ourselves in the inference engine self.ep_group = None # config.moe.ep_group self.expert_mp_group = None # config.moe.ep_mp_group
self.cuda_graph_created = False self.checkpoint_engine = TorchCheckpointEngine() quantization_setting = None self._init_quantization_setting( quantization_setting) # todo: update with the new quant config for weight quant self.model_profile_enabled = False self._model_times = []
if not self.injection_dict and config.replace_with_kernel_inject: # This is a hack to remove the prepare_mask function on HF side for BLOOM architecture self.remove_mask_prepare_for_bloom()
if self.injection_dict or not config.replace_with_kernel_inject: # This is a hack to redefine the alibi func due to TP if config.tensor_parallel.tp_size > 1: self.build_alibi_tensor() self.build_attn_bias()
if get_accelerator().device_name() == 'cuda' and config.enable_cuda_graph: assert pkg_version.parse(torch.__version__) >= pkg_version.parse("1.10"), \ "If you want to use cuda graph, please upgrade torch to at least v1.10"
# convert model to intended dtype if config.dtype: self._convert_to_dtype(config)
if self.mpu: config.tensor_parallel.tp_size = dist.get_world_size(group=self.mpu.get_model_parallel_group()) self.mp_group = self.mpu.get_model_parallel_group() elif config.tensor_parallel.tp_size > 1: self._create_model_parallel_group(config) config.tensor_parallel.tp_group = self.mp_group
if isinstance(self.module, torch.nn.Module): moe, _ = has_moe_layers(self.module) else: moe = False
if moe and dist.get_world_size() > 1: self._create_ep_parallel_group(config.moe.moe_experts)
# We only support three modes: 1) user specified policy for tensor-parallelism, 2) kernel injection (replace_with_kernel_inject), and 3) automatic tensor parallelism if tp_size > 1. if self.injection_dict: # 1. User specified Tensor Parallelism assert not config.replace_with_kernel_inject, "Cannot use both user specified injection policy and kernel injection" for client_module, injection_policy in self.injection_dict.items():
assert issubclass(client_module, torch.nn.Module), f"{client_module} is not a subclass of torch.nn.Module"
# construct the tuple and pass that instead of a string or dict. if isinstance(injection_policy, str): config.injection_policy_tuple = (injection_policy, ) else: config.injection_policy_tuple = injection_policy
layer_names = [name for name, _ in self.module.named_modules()] for policy in config.injection_policy_tuple: if not any(name.endswith(policy) for name in layer_names): raise ValueError(f"Injection policy layer'{policy}' not valid.")
self._apply_injection_policy(config, client_module) else: if config.replace_with_kernel_inject: # 2. DeepSpeed Kernel Injection self._apply_injection_policy(config) elif config.tensor_parallel.tp_size > 1: # 3. Automatic Tensor Parallelism parser_dict = AutoTP.tp_parser(model) print("AutoTP: ", parser_dict) for client_module, injection_policy in parser_dict: if isinstance(injection_policy, str): config.injection_policy_tuple = (injection_policy, ) else: config.injection_policy_tuple = injection_policy self._apply_injection_policy(config, client_module)
device = get_accelerator().current_device_name() # NOTE: This check assumes a Hugging Face hierarchy for the device type i.e. module.device.type is_meta_device = hasattr(self.module, "device") and self.module.device.type == 'meta' if is_meta_device: self.module.to_empty(device=device) elif not config.keep_module_on_host: self.module.to(device)
if config.tensor_parallel.tp_size > 1: _rng_state = get_accelerator().get_rng_state().to(get_accelerator().current_device_name()) dist.broadcast(_rng_state, 0) get_accelerator().set_rng_state(_rng_state.cpu())
if config.tensor_parallel.tp_size > 1: assert not config.enable_cuda_graph, "Cuda graph is not supported for model parallelism"
# Check if local CUDA graphs can be created in replacement modules self.local_cuda_graph = self._local_cuda_graph_used(self.module) self._is_compiled = False
def destroy(self): DeepSpeedTransformerInference.layer_id = 0 DeepSpeedSelfAttention.num_layers = 0 if DeepSpeedTransformerInference.workspace.is_allocated(): DeepSpeedTransformerInference.workspace.release_workspace() DeepSpeedTransformerInference.workspace = None
def profile_model_time(self, use_cuda_events=True): if not self.model_profile_enabled and not self._config.enable_cuda_graph: self.module.register_forward_pre_hook(self._pre_forward_hook) self.module.register_forward_hook(self._post_forward_hook) self.model_profile_enabled = True self.use_cuda_events = use_cuda_events if self.use_cuda_events: self.timers = SynchronizedWallClockTimer()
# todo: remove this once all the config dicts are centralized from top level pydantic config def _get_model_config_generate(self, config): # this is being passed to replace_transformer_layer(config=self.user_model_config_dict) self.config = getattr(self.module, 'config', None) if config.config is None else config.config
def remove_mask_prepare_for_bloom(self): if hasattr(self.module, 'transformer'): if hasattr(self.module.transformer, '_prepare_attn_mask'): self.module.transformer._prepare_attn_mask = lambda attention_mask, *args, **kwargs: attention_mask
def build_alibi_tensor(self): if hasattr(self.module, 'transformer'): if hasattr(self.module.transformer, 'build_alibi_tensor'): self.module.transformer.build_alibi_tensor = build_bloom_alibi_tensor if hasattr(self.module.transformer, 'build_mpt_alibi_tensor'): self.module.transformer.build_mpt_alibi_tensor_orig = self.module.transformer.build_mpt_alibi_tensor self.module.transformer.__class__.build_mpt_alibi_tensor = build_mpt_alibi_tensor if hasattr(self.module, 'model'): if hasattr(self.module.model, 'get_alibi_mask'): self.module.model.get_alibi_mask_orig = self.module.model.get_alibi_mask self.module.model.__class__.get_alibi_mask = get_alibi_mask
def build_attn_bias(self): if hasattr(self.module, 'transformer'): if hasattr(self.module.transformer, '_attn_bias'): self.module.transformer._attn_bias_orig = self.module.transformer._attn_bias self.module.transformer.__class__._attn_bias = build_mpt_atten_bias_tensor
def _pre_forward_hook(self, module, *inputs, **kwargs): if self.use_cuda_events: self.timers(INFERENCE_MODEL_TIMER).start() else: get_accelerator().synchronize() self._start = time.time()
def _post_forward_hook(self, module, input, output): if self.use_cuda_events: self.timers(INFERENCE_MODEL_TIMER).stop() elapsed_time = self.timers(INFERENCE_MODEL_TIMER).elapsed(reset=True) else: get_accelerator().synchronize() self._end = time.time() elapsed_time = (self._end - self._start) * 1e3 # convert seconds to ms self._model_times.append(elapsed_time)
def _create_model_parallel_group(self, config): # Call the init process if InferenceEngine.inference_mp_group is None: init_distributed() local_rank = int(os.getenv('LOCAL_RANK', '0')) get_accelerator().set_device(local_rank)
ranks = [i for i in range(config.tensor_parallel.tp_size)] self.mp_group = dist.new_group(ranks) InferenceEngine.inference_mp_group = self.mp_group else: self.mp_group = InferenceEngine.inference_mp_group
def _create_ep_parallel_group(self, moe_experts): # Call the init process self.ep_group = {} self.expert_mp_group = {} moe_experts = moe_experts if type(moe_experts) is list else [moe_experts] for e in moe_experts: self.ep_group.update({e: None}) self.expert_mp_group.update({e: None}) for moe_ep_size in self.ep_group.keys(): num_ep_groups = dist.get_world_size() // moe_ep_size for i in range(num_ep_groups): ep_cnt = i * moe_ep_size size = dist.get_world_size() if moe_ep_size > dist.get_world_size() else moe_ep_size ranks = list(range(ep_cnt, ep_cnt + size)) _ep_group = dist.new_group(ranks) if dist.get_rank() in ranks: self.ep_group.update({moe_ep_size: _ep_group})
if dist.get_world_size() > moe_ep_size: num_expert_mp_groups = dist.get_world_size() // num_ep_groups expert_mp_size = dist.get_world_size() // moe_ep_size for i in range(num_expert_mp_groups): expert_mp_comm_ranks = [i + nr * moe_ep_size for nr in range(expert_mp_size)] _expert_mp_group = dist.new_group(expert_mp_comm_ranks) if dist.get_rank() in expert_mp_comm_ranks: self.expert_mp_group.update({moe_ep_size: _expert_mp_group})
def _init_quantization_setting(self, quantization_setting): self.quantize_bits = 8 self.mlp_extra_grouping = False self.quantize_groups = 1 if type(quantization_setting) is tuple: self.mlp_extra_grouping, \ self.quantize_groups = quantization_setting elif quantization_setting is not None: self.quantize_groups = quantization_setting log_dist( f"quantize_bits = {self.quantize_bits} " f"mlp_extra_grouping = {self.mlp_extra_grouping}, " f"quantize_groups = {self.quantize_groups}", [0])
def load_model_with_checkpoint(self, r_module): self.mp_replace = ReplaceWithTensorSlicing( mp_group=self.mp_group, mp_size=self._config.tensor_parallel.tp_size) #, out_dim=0, in_dim=1) error_msgs = []
def load(module, state_dict, prefix): args = (state_dict, prefix, {}, True, [], [], error_msgs) if hasattr(module, 'weight'): if module.weight.data.is_meta: # meta tensor cannot be casted or copied to, so we need to replace it with a normal tensor here module.weight = torch.nn.parameter.Parameter(data=torch.empty_like(module.weight.data, device="cpu"), requires_grad=module.weight.data.requires_grad) if 'query_key_value' in prefix: module.weight = self.mp_replace.strided_copy(module.weight.data, state_dict[prefix + 'weight'], num_splits=3) else: module.weight = self.mp_replace.copy(module.weight.data, state_dict[prefix + 'weight']) else: if module.norm.weight.data.is_meta: # meta tensor cannot be casted or copied to, so we need to replace it with a normal tensor here module.norm.weight = torch.nn.parameter.Parameter( data=torch.empty_like(module.norm.weight.data, device="cpu"), requires_grad=module.norm.weight.data.requires_grad) module.norm.weight = self.mp_replace.copy(module.norm.weight.data, state_dict[prefix + 'weight']) if prefix + 'bias' in self.key_list: if hasattr(module, 'norm'): if module.norm.bias.data.is_meta: # meta tensor cannot be casted or copied to, so we need to replace it with a normal tensor here module.norm.bias = torch.nn.parameter.Parameter( data=torch.empty_like(module.norm.bias.data, device="cpu"), requires_grad=module.norm.bias.data.requires_grad) module.norm.bias = self.mp_replace.copy(module.norm.bias, state_dict[prefix + 'bias']) else: if module.bias.data.is_meta: # meta tensor cannot be casted or copied to, so we need to replace it with a normal tensor here module.bias = torch.nn.parameter.Parameter(data=torch.empty_like(module.bias.data, device="cpu"), requires_grad=module.bias.data.requires_grad) data = state_dict[prefix + 'bias'] data = data.to(get_accelerator().current_device_name()) module.bias = self.mp_replace.copy(module.bias, data)
layer_policies = { nn.Linear: load, nn.Embedding: load, nn.LayerNorm: load, LinearLayer: load, LinearAllreduce: load }
def load_module_recursive(module, prefix='', level=0): for name, child in module.named_children(): if child.__class__ in layer_policies: checking_key = prefix + name + '.' if not any(checking_key in item for item in self.key_list): continue if len(list(child.parameters())) > 0 and list(child.parameters())[0].numel() == 0: if len(child.weight.ds_shape) == 1: child = Normalize(dim=child.weight.ds_shape[-1], dtype=child.weight.dtype, eps=child.eps) setattr(module, name, child) load(child, self.sd, prefix + name + '.') else: load_module_recursive(child, prefix if level == 0 else prefix + name + '.', level + 1)
load_module_recursive(r_module)
embedding_weight = None
for n, p in r_module.named_parameters(): if "word_embeddings." in n or "embed_tokens." in n or "wte." in n: embedding_weight = p if embedding_weight is not None and hasattr(r_module, "lm_head") and hasattr( r_module.lm_head, "weight") and r_module.lm_head.weight.is_meta: r_module.lm_head.weight = embedding_weight
def _apply_injection_policy(self, config, client_module=None): # client_module is only passed when using the injection_dict method. checkpoint_dir = config.checkpoint checkpoint = SDLoaderFactory.get_sd_loader_json(checkpoint_dir, self.checkpoint_engine) if checkpoint_dir is not None else None
generic_injection(self.module, dtype=config.dtype, enable_cuda_graph=config.enable_cuda_graph)
if isinstance(self.module, torch.nn.Module): # config is our DeepSpeedInferenceConfig and self.config is the HF model config replace_transformer_layer(client_module, self.module, checkpoint, config, self.config)
def _get_all_ckpt_names(self, checkpoints_path, tag): ckpt_file_pattern = self._get_ckpt_name(checkpoints_path, tag, mp_placeholder="*") import glob
ckpt_files = glob.glob(ckpt_file_pattern) ckpt_files.sort() return ckpt_files
def _get_ckpt_name(self, checkpoints_path, tag, mp_placeholder=None): if mp_placeholder is not None: mp_rank_str = mp_placeholder else: mp_rank = 0 if self.mpu is None else self.mpu.get_model_parallel_rank() mp_rank_str = "{:02d}".format(mp_rank)
ckpt_name = os.path.join( checkpoints_path, "mp_rank_" + mp_rank_str + "_model_states.pt", ) return ckpt_name
def _load_checkpoint(self, load_dir, load_module_strict=True, tag=None): is_pipe_parallel = isinstance(self.module, PipelineModule) if is_pipe_parallel: raise RuntimeError('pipeline parallelism is currently not supported in inference.') if not isinstance(load_dir, dict) and os.path.isdir(load_dir): if tag is None: latest_path = os.path.join(load_dir, "latest") if os.path.isfile(latest_path): with open(latest_path, "r") as fd: tag = fd.read().strip()
ckpt_list = self._get_all_ckpt_names(load_dir, tag) sd_loader = SDLoaderFactory.get_sd_loader(ckpt_list, self.checkpoint_engine) else: sd_loader = SDLoaderFactory.get_sd_loader_json(load_dir, self.checkpoint_engine)
checkpoint = sd_loader['checkpoints']
if type(checkpoint) is list: self.sd = torch.load(checkpoint[0], map_location='cpu', weights_only=False) self.key_list = list(self.sd.keys())
self.load_model_with_checkpoint(self.module)
for i in range(1, len(checkpoint)): if not dist.is_initialized() or dist.get_rank() == 0: print(f"loading checkpoint ({i})") self.sd = torch.load(checkpoint[i], map_location=get_accelerator().device_name(), weights_only=False) self.key_list = list(self.sd.keys()) self.load_model_with_checkpoint(self.module) else: mp_rank = 0 if self.mpu is None else self.mpu.get_model_parallel_rank()
load_path, checkpoint, quantize_config = sd_loader.load(self._config.tensor_parallel.tp_size, mp_rank, is_pipe_parallel=is_pipe_parallel, quantize=(self._config.dtype is torch.int8), quantize_groups=self.quantize_groups, mlp_extra_grouping=self.mlp_extra_grouping)
self.quantization_scales, self.quantize_merge_count = quantize_config
moe, _ = has_moe_layers(self.module) if moe: from deepspeed.runtime.engine import DeepSpeedEngine old_moe_load = False if not isinstance(checkpoint['num_experts'], list): old_moe_load = True DeepSpeedEngine.load_moe_state_dict(load_dir, tag, state_dict=checkpoint[self._choose_module_key(checkpoint)], old_moe_load=old_moe_load, model=self.module, mpu=self.mpu, checkpoint_engine=self.checkpoint_engine)
self.module.load_state_dict(state_dict=checkpoint[self._choose_module_key(checkpoint)], strict=load_module_strict)
def _choose_module_key(self, sd): assert not ('module' in sd and 'model' in sd), "checkpoint has both 'model' and 'module' keys, not sure how to proceed" assert 'module' in sd or 'model' in sd, "checkpoint contains neither 'model' or 'module' keys, not sure how to proceed" if 'module' in sd: return 'module' elif 'model' in sd: return 'model'
def _convert_to_dtype(self, config): if not isinstance(self.module, torch.nn.Module): return
if False: #config.dtype is torch.int8 and self.quantization_scales is None: quantizer = WeightQuantization(mlp_extra_grouping=self.mlp_extra_grouping) model, self.quantization_scales = quantizer.model_quantize(self.module, self.injection_dict, self.quantize_bits, self.quantize_groups) elif config.dtype == torch.half: self.module.half() elif config.dtype == torch.bfloat16: self.module.bfloat16() elif config.dtype == torch.float: self.module.float()
def _create_cuda_graph(self, *inputs, **kwargs): # warmup to create the workspace and cublas handle cuda_stream = get_accelerator().Stream() cuda_stream.wait_stream(get_accelerator().current_stream()) with get_accelerator().stream(cuda_stream): for i in range(3): ret = self.module(*inputs, **kwargs) get_accelerator().current_stream().wait_stream(cuda_stream)
# create cuda_graph and assign static_inputs and static_outputs self._cuda_graphs = get_accelerator().create_graph() self.static_inputs = inputs self.static_kwargs = kwargs
with get_accelerator().capture_to_graph(self._cuda_graphs): self.static_output = self.module(*self.static_inputs, **self.static_kwargs)
self.cuda_graph_created = True
def _graph_replay(self, *inputs, **kwargs): for i in range(len(inputs)): if torch.is_tensor(inputs[i]): self.static_inputs[i].copy_(inputs[i]) for k in kwargs: if torch.is_tensor(kwargs[k]): self.static_kwargs[k].copy_(kwargs[k]) get_accelerator().replay_graph(self._cuda_graphs) return self.static_output
def model_times(self): assert self.model_profile_enabled, "model profiling is not enabled" model_times = self._model_times if self._config.enable_cuda_graph and len(self._model_times) == 0: raise ValueError("Model times are empty and cuda graph is enabled. If " "this is a GPT-style model this combo is not supported. If this is a " "BERT-style model this is a bug, please report it. " f"Model type is: {type(self.module)}") self._model_times = [] return model_times
def _module_match(self, module): for policy in generic_policies: policy = policy() if policy.match_replaced(module): return True return False
def _local_cuda_graph_used(self, module): if isinstance(module, torch.nn.Module): return False else: sub_module_cuda_graph = False for name in module.__dict__.keys(): sub_module = getattr(module, name)
if self._module_match(sub_module) and hasattr(sub_module, "enable_cuda_graph"): sub_module_cuda_graph = True
return sub_module_cuda_graph
def forward(self, *inputs, **kwargs): """Execute forward propagation
Arguments: *inputs: Variable length input list **kwargs: variable length keyword arguments """ start = None if self.model_profile_enabled and get_accelerator().device_name() == 'cuda' and self._config.enable_cuda_graph: get_accelerator().synchronize() start = time.time()
if get_accelerator().device_name() == 'cuda' and self._config.enable_cuda_graph and not self.local_cuda_graph: if self.cuda_graph_created: outputs = self._graph_replay(*inputs, **kwargs) else: self._create_cuda_graph(*inputs, **kwargs) outputs = self._graph_replay(*inputs, **kwargs)
else: outputs = self.module(*inputs, **kwargs)
if self.model_profile_enabled and self._config.enable_cuda_graph: get_accelerator().synchronize() duration = (time.time() - start) * 1e3 # convert seconds to ms self._model_times.append(duration)
return outputs
def _generate(self, *inputs, **kwargs): # Reset KV-cache at the beginning of generate if hasattr(self.module, 'reset_cache'): self.module.reset_cache() num_beams = 1 if "generation_config" in kwargs: gen_config = kwargs["generation_config"] num_beams = getattr(gen_config, "num_beams", 1) if "num_beams" in kwargs: num_beams = kwargs["num_beams"]
if num_beams > 1: raise NotImplementedError("DeepSpeed does not support `num_beams` > 1, if this is important to you please " "add your request to: https://github.com/deepspeedai/DeepSpeed/issues/2506")
if ("input_ids" in kwargs) and (kwargs["input_ids"].dim() == 2): for input_tensor in kwargs["input_ids"]: tensor_length = input_tensor.shape[-1] if tensor_length > self._config.max_out_tokens: raise RuntimeError( f"Input with size {tensor_length} exceeds maximum length of {self._config.max_out_tokens}. Please increase max_tokens in the DeepSpeed Inference Config." )
return self.module.generate(*inputs, **kwargs)
def compile(self, backend=get_accelerator().get_compile_backend(), compile_kwargs={}) -> None: """ Compile the module using the specified backend and kwargs. """ if not is_compile_supported(): raise RuntimeError("compile is not supported in your version of PyTorch.")
if self._is_compiled: return
# Avoid graph breaks deepspeed.utils.nvtx.enable_nvtx = False self.module.compile(backend=backend, **compile_kwargs) self._is_compiled = True
@property def is_compiled(self) -> bool: return self._is_compiled1234# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torch
from torch import nnfrom torch import Tensorfrom torch.nn import functional as Ffrom .utils import Quantizer, DeQuantizer, concat_to_compat_paramfrom typing import Tuple, Callable, Dictfrom deepspeed.runtime.zero import register_external_parameter
quantized_weight_registry = {}is_zero3_enabled = False
# deal with weight sharingdef get_quantized_weight_wrapper(model, pre_quant_weight: nn.Parameter, quantize_weight_fn: Callable) -> nn.Parameter: if id(pre_quant_weight) in quantized_weight_registry: compat_tensor = quantized_weight_registry[id(pre_quant_weight)] if is_zero3_enabled: register_external_parameter(model, compat_tensor)
return quantized_weight_registry[id(pre_quant_weight)] else: quantized_weights, quant_scale, quant_min = quantize_weight_fn() quantized_weight_registry[id(pre_quant_weight)] = concat_to_compat_param(quantized_weights, quant_scale, quant_min) return quantized_weight_registry[id(pre_quant_weight)]
def get_quantize_weight_fn(quantizer: Quantizer, pre_quant_weight: nn.Parameter) -> Callable:
def func() -> Tuple[nn.Parameter, Tensor, Tensor]: quantized_weights, quant_scale, quant_min = quantizer.quantize(pre_quant_weight.data) # A temporary hack as zero Zero3 assume all model weights has the same type. in all_gather_coalesced.get_only_unique_item quantized_weights = quantized_weights.view(pre_quant_weight.dtype) quant_scale = quant_scale.type(pre_quant_weight.dtype) quant_min = quant_min.type(pre_quant_weight.dtype) return quantized_weights, quant_scale, quant_min
return func
class QuantizedLinear(nn.Linear):
def __init__(self, config: Dict, pre_quant_layer: nn.Linear) -> None: super(QuantizedLinear, self).__init__(in_features=pre_quant_layer.in_features, out_features=pre_quant_layer.out_features, bias=pre_quant_layer.bias is not None, device=pre_quant_layer.weight.device, dtype=pre_quant_layer.weight.dtype) self.config = config
self.quantizer = Quantizer(config=config) self.bias = pre_quant_layer.bias self.weight = get_quantized_weight_wrapper(self, pre_quant_layer.weight, get_quantize_weight_fn(self.quantizer, pre_quant_layer.weight))
self.weight.dequantizer = DeQuantizer(config, pre_quant_layer.weight.dtype)
def forward(self, input: Tensor) -> Tensor: quantized_weight, quant_scale, quant_min = self.weight.deconcat(self.weight) temp_dequantized_weight = self.weight.dequantizer.dequantize(quantized_weight.view(torch.uint8), quant_scale, quant_min)
# !!! Do not use torch.functional.linear(input, temp_dequantized_weight, self.bias) here as in zero3 torch.functional.linear is # replaced by LinearFunctionForZeroStage3. Which assume weight is non-temporary. # If weight is temp buffer there will be memory leak. return torch._C._nn.linear(input, temp_dequantized_weight, self.bias)
class QuantizedEmbedding(nn.Embedding):
def __init__(self, config: Dict, pre_quant_layer: nn.Embedding) -> None: super(QuantizedEmbedding, self).__init__(num_embeddings=pre_quant_layer.num_embeddings, embedding_dim=pre_quant_layer.embedding_dim, padding_idx=pre_quant_layer.padding_idx, max_norm=pre_quant_layer.max_norm, norm_type=pre_quant_layer.norm_type, scale_grad_by_freq=pre_quant_layer.scale_grad_by_freq, sparse=pre_quant_layer.sparse, _weight=pre_quant_layer.weight, device=pre_quant_layer.weight.device, dtype=pre_quant_layer.weight.dtype)
assert pre_quant_layer.max_norm is None, 'Not supported' assert pre_quant_layer.norm_type == 2, 'Not supported' assert pre_quant_layer.scale_grad_by_freq == False, 'Not supported' assert pre_quant_layer.sparse == False, 'Not supported'
self.config = config quantizer = Quantizer(config=config)
self.weight = get_quantized_weight_wrapper(self, pre_quant_layer.weight, get_quantize_weight_fn(quantizer, pre_quant_layer.weight))
self.weight.dequantizer = DeQuantizer(config, pre_quant_layer.weight.dtype)
def forward(self, input: Tensor) -> Tensor: quantized_weight, quant_scale, quant_min = self.weight.deconcat(self.weight) temp_dequantized_weight = self.weight.dequantizer.dequantize(quantized_weight.view(torch.uint8), quant_scale, quant_min)
return F.embedding(input, temp_dequantized_weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse)
QUANTIZATION_LAYER_MAPPINGS = { nn.Linear: QuantizedLinear, nn.Embedding: QuantizedEmbedding,}lib/python3.12/site-packages/__pycache__/decorator.cpython-312.pycAdded
lib/python3.12/site-packages/__pycache__/ipython_pygments_lexers.cpython-312.pycAdded
lib/python3.12/site-packages/__pycache__/isympy.cpython-312.pycAdded
lib/python3.12/site-packages/__pycache__/pylab.cpython-312.pycAdded
lib/python3.12/site-packages/__pycache__/six.cpython-312.pycAdded
lib/python3.12/site-packages/click-8.2.1.dist-info/INSTALLERAdded+1
lib/python3.12/site-packages/click-8.2.1.dist-info/METADATAAdded+82
lib/python3.12/site-packages/click-8.2.1.dist-info/RECORDAdded+38
lib/python3.12/site-packages/click-8.2.1.dist-info/WHEELAdded+4
lib/python3.12/site-packages/click-8.2.1.dist-info/licenses/LICENSE.txtAdded+28
lib/python3.12/site-packages/deepspeed/__init__.pyAdded+398
lib/python3.12/site-packages/deepspeed/accelerator/__init__.pyAdded+7
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/abstract_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/cpu_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/cuda_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/hpu_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/mlu_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/mps_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/npu_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/real_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/sdaa_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/__pycache__/xpu_accelerator.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/accelerator/abstract_accelerator.pyAdded+306
lib/python3.12/site-packages/deepspeed/accelerator/cpu_accelerator.pyAdded+361
lib/python3.12/site-packages/deepspeed/accelerator/cuda_accelerator.pyAdded+382
lib/python3.12/site-packages/deepspeed/accelerator/hpu_accelerator.pyAdded+331
lib/python3.12/site-packages/deepspeed/accelerator/mlu_accelerator.pyAdded+300
lib/python3.12/site-packages/deepspeed/accelerator/mps_accelerator.pyAdded+281
lib/python3.12/site-packages/deepspeed/accelerator/npu_accelerator.pyAdded+299
lib/python3.12/site-packages/deepspeed/accelerator/real_accelerator.pyAdded+308
lib/python3.12/site-packages/deepspeed/accelerator/sdaa_accelerator.pyAdded+328
lib/python3.12/site-packages/deepspeed/accelerator/xpu_accelerator.pyAdded+337
lib/python3.12/site-packages/deepspeed/constants.pyAdded+21
lib/python3.12/site-packages/deepspeed/env_report.pyAdded+195
lib/python3.12/site-packages/deepspeed/git_version_info.pyAdded+31
lib/python3.12/site-packages/deepspeed/git_version_info_installed.pyAdded+6
lib/python3.12/site-packages/deepspeed/inference/__init__.pyAdded+7
lib/python3.12/site-packages/deepspeed/inference/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/__pycache__/config.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/__pycache__/engine.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/config.pyAdded+323
lib/python3.12/site-packages/deepspeed/inference/engine.pyAdded+625
lib/python3.12/site-packages/deepspeed/inference/quantization/__init__.pyAdded+4
lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/layers.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/quantization.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/quantization_context.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/quantization/__pycache__/utils.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/quantization/layers.pyAdded+114