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/torch/distributed/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/__pycache__/_state_dict_utils.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/__pycache__/argparse_util.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/__pycache__/launch.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/__pycache__/logging_handlers.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/__pycache__/remote_device.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/__pycache__/run.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/__pycache__/utils.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_composable/__init__.pyAdded+3
- lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/checkpoint_activation.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/contract.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/replicate.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_composable/checkpoint_activation.pyAdded+126
- lib/python3.12/site-packages/torch/distributed/_composable/contract.pyAdded+224
- lib/python3.12/site-packages/torch/distributed/_composable/fsdp/__init__.pyAdded+3
- lib/python3.12/site-packages/torch/distributed/_composable/fsdp/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_composable/fsdp/__pycache__/fully_shard.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/_composable/fsdp/fully_shard.pyAdded+8
- lib/python3.12/site-packages/torch/distributed/_composable/replicate.pyAdded+254
- lib/python3.12/site-packages/torch/distributed/_sharded_tensor/__init__.pyAdded+21
- lib/python3.12/site-packages/torch/distributed/_sharded_tensor/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/__init__.pyAdded+1
- lib/python3.12/site-packages/torch/distributed/algorithms/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/__pycache__/join.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/__init__.pyAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/__pycache__/checkpoint_wrapper.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/checkpoint_wrapper.pyAdded+323
- lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/__init__.pyAdded+7
- lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/__pycache__/default_hooks.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/default_hooks.pyAdded+192
- lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/__init__.pyAdded+1
- lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/__pycache__/optimizer_overlap.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/optimizer_overlap.pyAdded+97
- lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/__init__.pyAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/__pycache__/quantization.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/quantization.pyAdded+150
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__init__.pyAdded+110
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/ddp_zero_hook.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/debugging_hooks.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/default_hooks.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/mixed_precision_hooks.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/optimizer_overlap_hooks.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/post_localSGD_hook.cpython-312.pycAdded
- lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/powerSGD_hook.cpython-312.pycAdded
123from .checkpoint_activation import checkpointfrom .contract import _get_registry, contractfrom .replicate import replicate123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126# mypy: allow-untyped-decorators# mypy: allow-untyped-defsfrom contextlib import contextmanager, nullcontextfrom typing import Any, ContextManager, Dict, Optional, Tuple
import torchimport torch.nn as nnfrom torch.utils.checkpoint import ( _checkpoint_without_reentrant_generator, _DEFAULT_DETERMINISM_MODE,)
from .contract import contract
@contextmanagerdef _no_hook(module: nn.Module, user_ctx: Optional[ContextManager] = None): r""" Disable hooks installed by checkpoint to avoid unintentional recursion during backward recomputation. """
with user_ctx if user_ctx else nullcontext(): orig_enable_hook = checkpoint.state(module).enable_hook checkpoint.state(module).enable_hook = False try: yield finally: checkpoint.state(module).enable_hook = orig_enable_hook
@contract()def checkpoint(module: nn.Module, **kwargs) -> nn.Module: r""" This is a composable activation checkpointing API. Unlike functional activation checkpointing APIs, this one does not require changing model source code. Unlike ``nn.Module`` wrapper activation checkpointing APIs, this one does not modify model structure or fully-qualified names either. Under the hood, it registers activation checkpointing logic as pre- and post-forward hooks. Hence, this API can be easily applied to any model or sub-modules in the model.
Args: module (nn.Module): the target model or sub-module to apply activation checkpointing.
Example:: >>> # xdoctest: +SKIP >>> import torch.nn as nn >>> >>> class MyModel(nn.Module): >>> def __init__(self) -> None: >>> super().__init__() >>> self.l1 = nn.Linear(10, 10) >>> self.l2 = nn.Linear(10, 10) >>> >>> def forward(self, x): >>> return self.l2(self.l1(x)) >>> >>> model = MyModel() >>> checkpoint(model.l1) # apply activation checkpointing only to l1 >>> model(torch.zeros(2, 10)).sum().backward()
""" torch._C._log_api_usage_once("torch.distributed.checkpoint")
use_reentrant = kwargs.pop("use_reentrant", False) if use_reentrant: raise NotImplementedError( "use_reentrant=True is not supported in composable checkpoint. " "Please use torch.utils.checkpoint.checkpoint instead." ) preserve_rng_state = kwargs.pop("preserve_rng_state", True) user_context_fns = kwargs.pop("context_fn", None) determinism_check = kwargs.pop("determinism_check", _DEFAULT_DETERMINISM_MODE) debug = kwargs.pop("debug", False)
if kwargs: raise ValueError( "Unexpected keyword arguments: " + ",".join(arg for arg in kwargs) )
def forward_pre_hook( module: nn.Module, args: Tuple[Any, ...], kwargs: Dict[str, Any] ) -> None: if checkpoint.state(module).enable_hook:
def context_fns(): if user_context_fns is not None: ctx1, ctx2 = user_context_fns() return ctx1, _no_hook(module, ctx2) else: return nullcontext(), _no_hook(module)
checkpoint.state( module )._ac_generator = _checkpoint_without_reentrant_generator( module, preserve_rng_state, context_fns, determinism_check, debug, *args, **kwargs, ) next(checkpoint.state(module)._ac_generator)
def forward_hook(module: nn.Module, inputs: Tuple[Any, ...], output: Any) -> Any: if checkpoint.state(module).enable_hook: try: next(checkpoint.state(module)._ac_generator) except StopIteration: pass else: raise RuntimeError( "Expected non-reentrant activation checkpoint generator to be exhausted, but it was not!" )
# Ensure that we no longer hold on to the generator. always_call=True helps ensure we # clear this even in the case of exception in fwd pass. checkpoint.state(module)._ac_generator = None
checkpoint.state(module).enable_hook = True module.register_forward_pre_hook(forward_pre_hook, with_kwargs=True) module.register_forward_hook(forward_hook, prepend=True, always_call=True) return module123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224# mypy: allow-untyped-defsimport uuidfrom collections import OrderedDictfrom functools import wrapsfrom typing import Callable, Dict, List, Optional, Sequence, Type, Union
import torchimport torch.nn as nnfrom torch.distributed._composable_state import _Statefrom torch.distributed.utils import _get_root_modules
def generate_state_key(string="__composable_api_state_key"): return f"{string}_{str(uuid.uuid4())}"
STATE_KEY = generate_state_key()REGISTRY_KEY = generate_state_key()
# TODO: we can add additional info to RegistryItem to share across APIs. E.g.,# we can add args and kwargs here, and then we can detect whether fully_shard# is combined with reentrant activation checkpointing and error out with a clear# message.class RegistryItem: pass
def contract(state_cls: Type[_State] = _State): r""" Decorate a function as a composable distributed API, where the first argument of the function must be an :class:`nn.Module` instance or sequence of :class:`nn.Module` instances.
The decorator verifies that the decorated function does not modify fully-qualified names (FQNs) for parameters, buffers, or modules. The decorated function can return different module instances than the input modules; the FQN invariant will be enforced following the input order.
When a function ``func`` is decorated by ``@contract()``, a ``.state(module: nn.Module)`` method will be installed to the decorated function. Then you can retrieve and modify the state on a module by calling ``func.state(module)``.
Example:: >>> # xdoctest: +SKIP >>> import torch.nn as nn >>> >>> class MyModel(nn.Module): >>> def __init__(self) -> None: >>> super().__init__() >>> self.l1 = nn.Linear(10, 10) >>> self.l2 = nn.Linear(10, 10) >>> >>> def forward(self, x): >>> return self.l2(self.l1(x)) >>> >>> @contract() >>> def my_feature(module: nn.Module) -> nn.Module: >>> my_feature.state(module).some_state = "any value" >>> return module >>> >>> model = MyModel() >>> my_feature(model.l1) >>> assert my_feature.state(model.l1).some_state == "any value" >>> my_feature(model.l2) >>> model(torch.randn(2, 10)).sum().backward() """
# wraps will make functions decorated with contract() pickleable - needed for integration with torch.package @wraps(state_cls) def inner(func): @wraps(func) def wrapper( module: Union[nn.Module, Sequence[nn.Module]], *args, **kwargs ) -> Optional[nn.Module]: inp_module = module if isinstance(module, nn.Module): modules = [module] else: # If the user passes a sequence of modules, then we assume that # we only need to insert the state object on the root modules # (i.e. those without a parent) among the passed-in modules. modules = _get_root_modules(list(module)) state = state_cls() # shared across all modules registry_item = RegistryItem() # shared across all modules
# `func` is allowed to return different module instances than the # input modules as long as FQNs are preserved following the input # module order all_orig_named_params: List[Dict[str, nn.Parameter]] = [] all_orig_named_buffers: List[Dict[str, torch.Tensor]] = [] all_orig_named_modules: List[Dict[str, nn.Module]] = []
for module in modules: default_all_state: Dict[Callable, _State] = OrderedDict() default_registry: Dict[str, RegistryItem] = OrderedDict() all_state: Dict[Callable, _State] = module.__dict__.setdefault( # type: ignore[call-overload] STATE_KEY, default_all_state ) if not isinstance(all_state, dict): raise AssertionError( f"Distributed composable API states corrupted: {all_state}" ) registry: Dict[str, RegistryItem] = module.__dict__.setdefault( # type: ignore[call-overload] REGISTRY_KEY, default_registry ) if not isinstance(registry, dict): raise AssertionError( f"Distributed composable API registry corrupted: {registry}" ) if func in all_state or func.__name__ in registry: raise AssertionError( "Each distinct composable distributed API can only be applied to a " f"module once. {func.__name__} has already been applied to the " f"following module:\n{module}" ) all_state.setdefault(func, state) registry.setdefault(func.__name__, registry_item)
all_orig_named_params.append(OrderedDict(module.named_parameters())) all_orig_named_buffers.append(OrderedDict(module.named_buffers())) all_orig_named_modules.append(OrderedDict(module.named_modules()))
updated = func(inp_module, *args, **kwargs) if updated is None: updated = inp_module if isinstance(updated, nn.Module): updated_modules = [updated] else: updated_modules = _get_root_modules(list(inp_module)) # type: ignore[arg-type]
all_new_named_params: List[Dict[str, nn.Parameter]] = [] all_new_named_buffers: List[Dict[str, torch.Tensor]] = [] all_new_named_modules: List[Dict[str, nn.Module]] = [] for module in updated_modules: all_new_named_params.append(OrderedDict(module.named_parameters())) all_new_named_buffers.append(OrderedDict(module.named_buffers())) all_new_named_modules.append(OrderedDict(module.named_modules()))
num_orig_modules = len(all_orig_named_modules) num_new_modules = len(all_new_named_modules) if num_orig_modules != num_new_modules: raise AssertionError( f"{func.__name__} should return the same number of modules as input modules" f"Inputs: {num_orig_modules} modules\n" f"Outputs: {num_new_modules} modules" )
def check_fqn(orig_fqns: List[str], new_fqns: List[str], check_key: str): if orig_fqns == new_fqns: return
orig_fqn_set, new_fqn_set = set(orig_fqns), set(new_fqns) orig_only = orig_fqn_set - new_fqn_set new_only = new_fqn_set - orig_fqn_set if len(orig_only) or len(new_only): raise RuntimeError( f"{check_key}" "Composable distributed API implementations cannot modify FQNs.\n" f"FQNs only in original: {orig_only}\n" f"FQNs only in new: {new_only}" ) else: raise RuntimeError( f"{check_key}" "Composable distributed API implementations cannot modify " "the order of FQNs.\n" f"Original FQNs: {orig_only}\n" f"New FQNs: {new_only}" )
for orig_named_params, new_named_params in zip( all_orig_named_params, all_new_named_params ): check_fqn( list(orig_named_params.keys()), list(new_named_params.keys()), "Checking parameters: ", ) for orig_named_buffers, new_named_buffers in zip( all_orig_named_buffers, all_new_named_buffers ): check_fqn( list(orig_named_buffers.keys()), list(new_named_buffers.keys()), "Checking buffers: ", ) for orig_named_modules, new_named_modules in zip( all_orig_named_modules, all_new_named_modules ): check_fqn( list(orig_named_modules.keys()), list(new_named_modules.keys()), "Checking modules: ", )
# TODO: verify that installed distributed paradigms are compatible with # each other.
return updated
def get_state(module: nn.Module) -> Optional[_State]: return module.__dict__.setdefault( # type: ignore[call-overload] STATE_KEY, {}, # TODO(@yhcharles): this is a temporary fix, need a better way ).get( func ) # type: ignore[call-overload]
wrapper.state = get_state # type: ignore[attr-defined]
return wrapper
return inner
def _get_registry(module: nn.Module) -> Optional[Dict[str, RegistryItem]]: r""" Get an ``OrderedDict`` of composable APIs that have been applied to the ``module``, indexed by the API name. If no API has been applied, then this returns ``None``. """ return getattr(module, REGISTRY_KEY, None)123from torch.distributed.fsdp import CPUOffloadPolicy, MixedPrecisionPolicy, OffloadPolicy
from .fully_shard import FSDPModule, fully_shard, register_fsdp_forward_method12345678# TODO: For backward compatibility, we are importing the public objects# originally from this file.from torch.distributed.fsdp import ( # noqa: F401 FSDPModule, fully_shard, register_fsdp_forward_method, UnshardHandle,)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254# mypy: allow-untyped-decorators# mypy: allow-untyped-defsimport weakreffrom typing import Any, cast, Dict, Iterable, List, NoReturn, Optional, Set, Tuple
import torchimport torch.nn as nnfrom torch.distributed._composable_state import _Statefrom torch.nn.parallel import DistributedDataParallel
from .contract import _get_registry, contract
_ROOT_MODULE_PREFIX = ""
class _ReplicateState(_State): def __init__(self) -> None: super().__init__() self.module: nn.Module = nn.ParameterList() self.has_initialized: bool = False self._param_list: nn.ParameterList = nn.ParameterList() # TODO(@fegin): this variable is originally create for testing, we # should remove this if possible. self._orig_module = self.module self._param_names: List[str] = [] self._no_sync: bool = False self._init_args: Optional[Tuple[Any, ...]] = None self._init_kwargs: Dict[str, Any] = {} self._comm_hook_args: List[Any] = []
def _collect_params( self, module: nn.Module, ignored_modules: Set[nn.Module], ignored_params: Set[nn.Parameter], prefix: str = _ROOT_MODULE_PREFIX, ) -> None: # skip if managed by fully_sharded API if _is_fully_sharded(module): return
# if a module is ignored, all descendants of the module are ignored. if module in ignored_modules: return
recurse_prefix = ( f"{prefix}." if prefix != _ROOT_MODULE_PREFIX else _ROOT_MODULE_PREFIX )
for n, p in module.named_parameters(recurse=False): if p not in ignored_params: self._param_list.append(p) self._param_names.append(f"{recurse_prefix}{n}")
for name, child_module in module.named_children(): self._collect_params( child_module, ignored_modules, ignored_params, prefix=f"{recurse_prefix}{name}", )
def lazy_init(self) -> None: @torch._disable_dynamo(recursive=True) def _lazy_init(): assert self._init_args is not None self.init(*self._init_args, **self._init_kwargs) self.register_comm_hook() self._init_args = () self._init_kwargs = {}
_lazy_init()
def init( self, module: nn.Module, ignored_modules: Set[nn.Module], **kwargs, ) -> None: if self.has_initialized: return
self.has_initialized = True self.module = module ignored_params = {p for m in ignored_modules for p in m.parameters()} for submodule in module.modules(): if _is_fully_sharded(submodule): ignored_params.update(submodule.parameters()) from torch.distributed.tensor.parallel.ddp import _localize_dtensor
_localize_dtensor(module, ignored_params=ignored_params) self._collect_params(module, ignored_modules, ignored_params)
if "device_id" in kwargs: # replicate() supports a small usability enhancement where # user can pass in device_id as a Union[int, torch.device] even for # CPU devices so users don't have to change code for CPU/GPU runs. # We derive the right device_ids to feed into DDP to support this. if kwargs["device_id"] is not None: device_id = kwargs["device_id"] # Convert to device_ids that DDP expects. if isinstance(device_id, torch.device) and device_id.type == "cpu": # CPU modules receive device_ids None kwargs["device_ids"] = None else: # GPU modules expect device_ids=[cuda_device] kwargs["device_ids"] = [device_id] else: kwargs["device_ids"] = None kwargs.pop("device_id")
self._ddp = DistributedDataParallel(self._param_list, **kwargs) # Weakref to the DDP instance is currently only used for testing. replicate.state(self.module)._ddp_weakref = weakref.ref(self._ddp)
def register_comm_hook(self) -> None: for comm_args, comm_kwargs in self._comm_hook_args: self._ddp.register_comm_hook(*comm_args, **comm_kwargs) self._comm_hook_args.clear()
def record_init_args(self, *args, **kwargs) -> None: self._init_args = args self._init_kwargs = kwargs
def forward_pre_hook( self, module: nn.Module, args: Tuple[Any, ...], kwargs: Dict[str, Any] ) -> Any: if self._init_args or self._init_kwargs: self.lazy_init() self._ddp.require_backward_grad_sync = not self._no_sync return self._ddp._pre_forward(*args, **kwargs)
def forward_post_hook( self, module: nn.Module, input: Tuple[torch.Tensor], output: torch.Tensor, ) -> torch.Tensor: return self._ddp._post_forward(output)
def unimplemented_deepcopy(*args: Any, **kwargs: Any) -> NoReturn: raise AssertionError( "DDP does not support deepcopy. Please use state dict for serialization." )
# Follow the same pattern as FSDP/fully_shardclass DDP: def __new__(cls, *args, **kwargs): """ Override ``__new__`` to remove the DDP class and directly construct the original class for cases like indexing into a container module. """ # Use index 2 since 0 is the dynamically constructed `DDP<...>` class # and index 1 is the `DDP` class itself orig_cls = cls.__mro__[2] return orig_cls.__new__(orig_cls, *args, **kwargs)
def set_requires_gradient_sync(self, requires_gradient_sync: bool) -> None: """ Sets if the module should sync gradients. This can be used to implement gradient accumulation without communication.
Args: requires_gradient_sync (bool): Whether to reduce gradients for the module's parameters. """ replicate.state(self)._no_sync = not requires_gradient_sync
def register_comm_hook(self, *args, **kwargs) -> None: replicate.state(self)._comm_hook_args.append((args, kwargs))
@contract(state_cls=_ReplicateState)def replicate( module: nn.Module, ignored_modules: Optional[Iterable[torch.nn.Module]] = None, **kwargs,) -> nn.Module: r"""Replicates a module
Args: module (torch.nn.Module): module to replicate
Example:: >>> # xdoctest: +REQUIRES(module:torch._C._distributed_c10d) >>> module = nn.Linear(3, 3) >>> replicate(module) """ torch._C._log_api_usage_once("torch.distributed.replicate")
# TODO(fegin): using kwargs is not a good idea if we would like to make # replicate a formal API to replace DDP. if "device_id" in kwargs: if not isinstance(kwargs["device_id"], (int, torch.device)): raise RuntimeError( "Expected device_id to be int or torch.device, " f"but got {type(kwargs['device_id'])}" )
if _is_fully_sharded(module): raise RuntimeError( "Cannot apply `replicate()` on a Module already managed by `fully_shard`" )
if ignored_modules is None: ignored_modules = {} else: ignored_modules = set(ignored_modules)
state = cast(_ReplicateState, replicate.state(module)) module.register_forward_pre_hook(state.forward_pre_hook, with_kwargs=True) device_mesh = kwargs.get("device_mesh", None) if device_mesh is not None: from torch.distributed.device_mesh import _mesh_resources
root_mesh = _mesh_resources.get_root_mesh(device_mesh) # if a root mesh is not the same as device_mesh, # meaning the device_mesh is sliced out from the root mesh. if root_mesh != device_mesh: # TODO: This is a temporary work around to enable DDP + TP. # We should do the logic in DDP so that the 2D implementation is # sound and the state_dict works out of the box. # # This won't conflict with what is done in DDP class as the module # replicate is going to pass is NOT the original module. from torch.distributed.tensor.parallel.ddp import ( _localize_dtensor, _reconstruct_dtensor, )
module.register_forward_pre_hook(_reconstruct_dtensor) module.register_forward_hook(_localize_dtensor)
module.register_forward_hook(state.forward_post_hook) # type: ignore[arg-type]
state.record_init_args(module, ignored_modules, **kwargs)
# Place DDP leftmost for highest priority in the method resolution order cls = module.__class__ dct = {"__deepcopy__": unimplemented_deepcopy} new_cls = type(f"DDP{cls.__name__}", (DDP, cls), dct) module.__class__ = new_cls return module
def _is_fully_sharded(module: nn.Module) -> bool: r"""Check if module is marked with fully_shard.""" registry = _get_registry(module) if registry is None: return False return "fully_shard" in registry123456789101112131415161718192021# Keep old package for BC purposes, this file should be removed once# everything moves to the `torch.distributed._shard` package.import sysimport warnings
import torchfrom torch.distributed._shard.sharded_tensor import * # noqa: F403
with warnings.catch_warnings(): warnings.simplefilter("always") warnings.warn( "`torch.distributed._sharded_tensor` will be deprecated, " "use `torch.distributed._shard.sharded_tensor` instead", DeprecationWarning, stacklevel=2, )
sys.modules[ "torch.distributed._sharded_tensor"] = torch.distributed._shard.sharded_tensor1from .join import Join, Joinable, JoinHook123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323# mypy: allow-untyped-defsimport warningsfrom abc import ABC, abstractmethodfrom enum import auto, Enumfrom functools import partialfrom typing import Any, Callable, Dict, Iterator, Optional, Tuple
import torchimport torch.nn as nnfrom torch.autograd.graph import save_on_cpufrom torch.distributed.utils import _pack_kwargs, _replace_by_prefix, _unpack_kwargsfrom torch.utils.checkpoint import checkpoint as torch_utils_checkpoint
_CHECKPOINT_WRAPPED_MODULE = "_checkpoint_wrapped_module"_CHECKPOINT_PREFIX = _CHECKPOINT_WRAPPED_MODULE + "."
class CheckpointImpl(Enum): REENTRANT = auto() NO_REENTRANT = auto()
class ActivationWrapper(torch.nn.Module, ABC): """ Base class for Activation Checkpoint and Activation Offload.
Not meant to be instantiated directly. """
def __init__(self, mod): super().__init__() self._checkpoint_wrapped_module = mod # state_dict post hook to remove prefix to allow loading into a # non-checkpoint wrapped module. self._register_state_dict_hook(self._post_state_dict_hook) # load_state_dict pre-hook to allow loading back into # checkpoint-wrapped module. self.register_load_state_dict_pre_hook(self._pre_load_state_dict_hook)
@abstractmethod def forward(self, *args, **kwargs): raise ValueError("Subclasses should implement forward().")
def __getattr__(self, name: str) -> Any: """Forward missing attributes to wrapped module.""" try: return super().__getattr__(name) # defer to nn.Module's logic except AttributeError: return getattr(self._checkpoint_wrapped_module, name)
def __getitem__(self, key: int) -> Any: """Forward indexing calls in case the module is a nn.Sequential.""" return self._checkpoint_wrapped_module.__getitem__(key) # type: ignore[operator]
def named_parameters( self, *args, **kwargs, ) -> Iterator[Tuple[str, torch.nn.Parameter]]: """ Override :meth:`named_parameters()` to intercept parameter names.
remove all occurrences of ``_CHECKPOINT_PREFIX``. """ for param_name, param in super().named_parameters(*args, **kwargs): yield param_name.replace(_CHECKPOINT_PREFIX, ""), param
@staticmethod def _post_state_dict_hook( module: nn.Module, state_dict: Dict[str, Any], prefix: str, *args: Any, ) -> Dict[str, Any]: """ _post_state_dict_hook() is called after the state_dict() of this FSDP module is executed.
For ``checkpoint_wrapper``, it will strip checkpoint-wrapped module prefix, so that this module can be loaded into non-checkpointed modules. It would still be able to be loaded into checkpoint-wrapped modules as this class, adds the prefix back before loading the state_dict. """ _replace_by_prefix(state_dict, f"{prefix}{_CHECKPOINT_PREFIX}", prefix) return state_dict
@staticmethod def _pre_load_state_dict_hook( module: nn.Module, state_dict: Dict[str, Any], prefix: str, *args: Any, ) -> None: """ ``_pre_state_dict_hook` is called before ``self._load_from_state_dict()`` is called.
For ``checkpoint_wrapper``, it will add back the module prefix so that non-checkpointed modules can be loaded into checkpoint_wrapper modules properly. """ _replace_by_prefix(state_dict, prefix, prefix + f"{_CHECKPOINT_PREFIX}")
class OffloadWrapper(ActivationWrapper): def __init__(self, mod): super().__init__(mod)
def forward(self, *args, **kwargs): with save_on_cpu(pin_memory=True): return self._checkpoint_wrapped_module(*args, **kwargs)
class CheckpointWrapper(ActivationWrapper): """ An ``nn.Module`` that wraps another ``nn.Module`` with checkpointing.
Note that this module is not meant to be used directly but instead, it is to be used through the ``checkpoint_wrapper`` function. """
def __init__( self, mod: torch.nn.Module, checkpoint_impl: CheckpointImpl = CheckpointImpl.NO_REENTRANT, checkpoint_fn=None, **checkpoint_fn_kwargs, ): super().__init__(mod) self.checkpoint_impl = checkpoint_impl if checkpoint_fn is None: # use torch.utils.checkpoint self.checkpoint_fn = partial( torch_utils_checkpoint, use_reentrant=(self.checkpoint_impl == CheckpointImpl.REENTRANT), **checkpoint_fn_kwargs, ) else: # Construct user-specified checkpoint function. self.checkpoint_fn = partial( checkpoint_fn, **checkpoint_fn_kwargs, )
def forward(self, *args, **kwargs): # Support keyword arguments for reentrant checkpoint. Note that this # only works if user has specified self.checkpoint_impl and is not # using their own custom checkpoint_fn. if self.checkpoint_impl == CheckpointImpl.REENTRANT and kwargs != {}: # Pack the args and kwargs flat_args, kwarg_keys = _pack_kwargs(*args, **kwargs)
# Function that only takes (packed) args, but can unpack them # into the original args and kwargs for the checkpointed # function, and runs that function. def my_function(*inputs): # unpack back into args and kwargs unpacked_args, unpacked_kwargs = _unpack_kwargs(inputs, kwarg_keys) # run original module return self._checkpoint_wrapped_module( *unpacked_args, **unpacked_kwargs )
# Pass the function that only takes packed args into reentrant # checkpoint API. return self.checkpoint_fn( # type: ignore[misc] my_function, *flat_args, ) else: return self.checkpoint_fn( # type: ignore[misc] self._checkpoint_wrapped_module, *args, **kwargs )
def offload_wrapper(module: torch.nn.Module) -> torch.nn.Module: """ Wrap a module for activation offloading to CPU.
Offloads intermediate activations to the CPU for modules wrapped with this function. Wrappers with activation offload can be composed with ones that do recomputation-based checkpoint to trade off increased compute versus increased CPU memory usage and additional H2D transfers.
Usage:: offloaded_module = offload_wrapper(module) outputs = checkpointed_module(inputs) Args: module (nn.Module): The module to be wrapped Returns: (nn.Module): Wrapped module """ return OffloadWrapper(module)
def checkpoint_wrapper( module: torch.nn.Module, checkpoint_impl: CheckpointImpl = CheckpointImpl.NO_REENTRANT, checkpoint_fn=None, **checkpoint_fn_kwargs,) -> torch.nn.Module: """ Wrap a module for activation checkpointing.
If the module is wrapped with this function, all subsequent calls to the module will, automatically perform checkpointing without the user having to explicitly call ``checkpoint`` function.
Usage:: checkpointed_module = checkpoint_wrapper(module) outputs = checkpointed_module(inputs) Args: module (nn.Module): The module to be wrapped checkpoint_impl (Optional[CheckpointImpl]): The checkpointing implementation to use. Note that this will only be passed into the ``torch.utils.checkpoint.checkpoint`` implementation, and is ignored if a custom ``checkpoint_fn`` is specified. Note that for implementations using reentrant checkpoint from ``torch.utils.checkpoint``, keyword arguments will only be supported if ``checkpoint_impl`` is passed as ``CheckpointImpl.REENTRANT`. checkpoint_fn (Optional[Callable]): Functional checkpoint implementation to use. If this is specified, it will be used over the default ``torch.utils.checkpoint.checkpoint`` implementation and the `checkpoint_impl` argument will be ignored. **checkpoint_fn_kwargs: (Dict[str, Any]): Keyword arguments to pass into `checkpoint_fn`.
Returns: (nn.Module): Wrapped module """
if checkpoint_impl == CheckpointImpl.REENTRANT: warnings.warn( f"Please specify {CheckpointImpl.NO_REENTRANT} as " f"{CheckpointImpl.REENTRANT} will soon be removed as " "the default and eventually deprecated.", FutureWarning, stacklevel=2, ) return CheckpointWrapper( module, checkpoint_impl, checkpoint_fn, **checkpoint_fn_kwargs, )
def apply_activation_checkpointing( model, checkpoint_wrapper_fn=checkpoint_wrapper, check_fn=lambda _: True, auto_wrap_policy: Optional[Callable[[nn.Module, bool, int], bool]] = None,): """ Apply :func:`checkpoint_wrapper` to modules within `model` based on a user-defined configuration.
For each module within `model`, the `check_fn` is used to decide whether `module` should be wrapped with :func:`checkpoint_wrapper` or not.
Note:: This function modifies `model` in place and replaces appropriate layers with their checkpoint-wrapped modules. Note:: This function will not wrap the overall root module. If this is needed, please directly use :func:`checkpoint_wrapper` or :func:`offload_wrapper`. Usage:: model = nn.Sequential( nn.Linear(10, 10), nn.Linear(10, 10), nn.Linear(10, 10) ) check_fn = lambda l: isinstance(l, nn.Linear) # checkpoint activations apply_activation_checkpointing(model, checkpoint_wrapper_fn=checkpoint_wrapper, check_fn=check_fn) # Or offload activations to CPU apply_activation_checkpointing(model, checkpoint_wrapper_fn=offload_wrapper, check_fn=check_fn) Args: model (nn.Module): The model whose submodules should be wrapped with activation checkpointing. checkpoint_wrapper_fn (Optional[Callable[nn.Module]]) A ``Callable`` which will wrap modules check_fn (Optional[Callable[nn.Module, nn.Module]]) A lambda function which will be passed each child submodule of ``model`` and returns ``True`` or ``False`` depending on whether the submodule should be wrapped. auto_wrap_policy (Optional[Callable[[nn.Module, bool, int], bool]]): A policy to wrap model's submodules with AC. Note that if this is specified, it takes precedence over ``check_fn``. Returns: None (`model` is modified inplace) """ # TODO: Importing inside function to avoid circular import issue between FSDP and # checkpoint_wrapper. This can be resolved once wrap() APIs are decoupled from FSDP code. from torch.distributed.fsdp._wrap_utils import _construct_wrap_fn, _post_order_apply from torch.distributed.fsdp.wrap import ( _Policy, _recursive_wrap, lambda_auto_wrap_policy, )
policy = ( auto_wrap_policy if auto_wrap_policy is not None else partial(lambda_auto_wrap_policy, lambda_fn=check_fn) ) if not callable(policy): if not isinstance(policy, _Policy): raise ValueError( f"Expected {policy} to be callable or be a pre-defined wrap policy" ) target_module_to_kwargs = policy._run_policy( model, ignored_modules=set(), root_kwargs={} ) wrap_fn = _construct_wrap_fn( model, target_module_to_kwargs, checkpoint_wrapper_fn ) _post_order_apply(model, wrap_fn) return
_recursive_wrap( module=model, auto_wrap_policy=policy, # type: ignore[arg-type] wrapper_cls=checkpoint_wrapper_fn, ignored_modules=set(), ignored_params=set(), only_wrap_children=True, )1234567from . import default_hooks as default
LOW_PRECISION_HOOKS = [ default.fp16_compress_hook, default.bf16_compress_hook,]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192# mypy: allow-untyped-defsimport functoolsfrom typing import Optional
import torchimport torch.distributed as dist
class DefaultState: r""" Stores state needed to perform the default communication algorithm within a communication hook.
Args: process_group (ProcessGroup): The process group to be used. """
__slots__ = [ "process_group", "world_size", "gradient_predivide_factor", "gradient_postdivide_factor", ]
def __init__(self, process_group: dist.ProcessGroup): if process_group is None: raise ValueError(f"Expected to pass in an explicit ProcessGroup to {self}.") self.process_group = process_group self.world_size = dist.get_world_size(process_group) # Setting two factors `self.gradient_predivide_factor` # and `self.gradient_postdivide_factor` to avoid underflow and overflow self.gradient_predivide_factor = self._get_gradient_predivide_factor( self.world_size ) self.gradient_postdivide_factor = ( self.world_size / self.gradient_predivide_factor )
@staticmethod def _get_gradient_predivide_factor(world_size: int) -> float: factor: int = 1 while world_size % factor == 0 and world_size / factor > factor: factor *= 2 return float(factor)
class LowPrecisionState(DefaultState): r""" Stores state needed to perform gradient communication in a lower precision within a communication hook.
Communication hook will cast gradients back to the original parameter precision specified by ``parameter_type`` (default: torch.float32). Builds on top of the :class:`DefaultState`.
Args: parameter_type (torch.dtype): The precision of model's parameters. Required for a hook to cast gradients back to a parameter's precision. """
__slots__ = [ "parameter_type", ]
def __init__( self, process_group, parameter_type=torch.float32, ): super().__init__(process_group) self.parameter_type = parameter_type
def _decompress(state: LowPrecisionState, grad: torch.Tensor): """ Casts gradients back to full parameter precision so that further computation happens in full precision. """ orig_grad_data = grad.data grad.data = grad.data.to(state.parameter_type) device_type = "" try: if grad.device.type == "privateuse1": device_type = torch._C._get_privateuse1_backend_name() else: device_type = grad.device.type backend = getattr(torch, device_type) except AttributeError as e: raise AttributeError( f"Device {grad.device} does not have a \ corresponding backend registered as 'torch.device_type'." ) from e
# Don't let this memory get reused until after the transfer. orig_grad_data.record_stream(backend.current_stream()) # type: ignore[arg-type]
def allreduce_hook(state: DefaultState, grad: torch.Tensor): r""" Implement the FSDP communication hook for ``all_reduce`` algorithm and a necessary pre- and post-division of gradients.
Args: state (DefaultState): State information, configures pre- and post-division factors. grad (torch.Tensor): A gradient for the local batch that needs to be communicated across ranks. """ # Average grad by pre-division factor. Together pre- and post-division factors # lead to an overall averaging by world_size, required for consistency with PyTorch DDP. # This is a two-step process to avoid potential underflow and overflow. if state.gradient_predivide_factor > 1: grad.div_(state.gradient_predivide_factor) dist.all_reduce(grad, group=state.process_group) # Average grad by post-division factor. if state.gradient_postdivide_factor > 1: grad.div_(state.gradient_postdivide_factor)
def reduce_scatter_hook(state: DefaultState, grad: torch.Tensor, output: torch.Tensor): r""" Implement the FSDP communication hook for ``reduce_scatter`` algorithm.
For sharded FSDP strategies and a necessary pre- and post-division of gradients.
Args: state (DefaultState): State information, configures pre- and post-division factors. grad (torch.Tensor): An unsharded gradient for the local batch that needs to be communicated across ranks. output (torch.Tensor): Stores a single shard of the gradient after ``reduce_scatter``. """ # Average grad by pre-division factor. if state.gradient_predivide_factor > 1: grad.div_(state.gradient_predivide_factor) dist.reduce_scatter_tensor(output, grad, group=state.process_group) # Average grad's shard by post-division factor. if state.gradient_postdivide_factor > 1: output.div_(state.gradient_postdivide_factor)
def _low_precision_hook( prec: torch.dtype, state: LowPrecisionState, grad: torch.Tensor, output: Optional[torch.Tensor],): if grad.dtype != prec: grad.data = grad.data.to(prec) if output is not None: if output.dtype != prec: output.data = output.data.to(prec) reduce_scatter_hook(state, grad, output) _decompress(state, output) else: allreduce_hook(state, grad) _decompress(state, grad)
def fp16_compress_hook( state: LowPrecisionState, grad: torch.Tensor, output: Optional[torch.Tensor] = None): r""" Implement FSDP communication hook for a simple gradient compression approach. Casts ``grad`` to half-precision floating-point format (``torch.float16``).
It also averages gradients by ``world_size`` in two steps: first it pre-divides gradients by a ``state.gradient_predivide_factor``, and after a communication step (``all_reduce`` or ``reduce_scatter``) gradients are averaged by a ``state.gradient_postdivide_factor``. Once post-division is done, compressed gradients are casted back to parameters' precision.
Args: state (LowPrecisionState): State information, configures pre- and post-division factors, parameters' precision. grad (torch.Tensor): A gradient for the local batch that needs to be communicated across ranks in a lower precision. output (torch.Tensor): Stores a single shard of the gradient after ``reduce_scatter``. """ fp16_hook = functools.partial(_low_precision_hook, torch.float16) return fp16_hook(state, grad, output)
def bf16_compress_hook( state: LowPrecisionState, grad: torch.Tensor, output: Optional[torch.Tensor] = None): r""" Implement FSDP communication hook for a simple gradient compression approach . Casts ``grad`` to half-precision floating-point format.
It also averages gradients by ``world_size`` in two steps: first it pre-divides gradients by a ``state.gradient_predivide_factor``, and after a communication step (``all_reduce`` or ``reduce_scatter``) gradients are averaged by a ``state.gradient_postdivide_factor``. Once post-division is done, compressed gradients are casted back to parameters' precision.
Args: state (LowPrecisionState): State information, configures pre- and post-division factors, parameters' precision. grad (torch.Tensor): A gradient for the local batch that needs to be communicated across ranks in a lower precision. output (torch.Tensor): Stores a single shard of the gradient after ``reduce_scatter``. """ bf16_hook = functools.partial(_low_precision_hook, torch.bfloat16) return bf16_hook(state, grad, output)1from .optimizer_overlap import _as_overlapped_optim12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697# mypy: allow-untyped-defsimport inspectfrom abc import ABC, abstractmethodfrom typing import Dict, Type
from torch.distributed.algorithms.ddp_comm_hooks.default_hooks import allreduce_hookfrom torch.distributed.algorithms.ddp_comm_hooks.optimizer_overlap_hooks import ( _hook_then_optimizer, _OptimizerHookState,)from torch.distributed.fsdp import FullyShardedDataParallelfrom torch.distributed.optim import as_functional_optimfrom torch.nn.parallel import DistributedDataParallelfrom torch.optim import Optimizer
# Contains the mappings between the regular and overlapped optimizer types._registered_overlapped_optims: Dict[Type, Type] = {}
def register_overlapped(optim_cls): def decorator(target_overlapped_optim_cls): if target_overlapped_optim_cls in _registered_overlapped_optims: raise ValueError( f"{target_overlapped_optim_cls} already registered with optim_cls " f"{_registered_overlapped_optims[optim_cls]} {optim_cls}, trying to" f"re-register it for {optim_cls} is not supported." ) _registered_overlapped_optims[optim_cls] = target_overlapped_optim_cls return target_overlapped_optim_cls
return decorator
class OverlappedOptimizer(ABC): def __init__(self, optim_cls: Type) -> None: """ Initialize the OverlappedOptimizer.
Overlappedoptimizer is a base class that child classes can implement to specify how different optimizers will register themselves with DDP. """ self.optim_cls = optim_cls
@abstractmethod def register_ddp(self, ddp: DistributedDataParallel) -> None: """Registers the overlapped optimizer with DDP.""" raise NotImplementedError( f"{self.__class__.__name__} does not support overlapped DDP." )
@abstractmethod def register_fsdp(self, fsdp: FullyShardedDataParallel) -> None: """Registers the overlapped optimizer with FSDP.""" raise NotImplementedError( f"{self.__class__.__name__} does not support overlapped FSDP." )
@register_overlapped(Optimizer)class _OverlappedStandardOptimizer(OverlappedOptimizer): """Overlaps a regular ``Optimizer``."""
def __init__(self, optim_cls: Type, params, *optim_args, **optim_kwargs) -> None: super().__init__(optim_cls) f_optim = as_functional_optim(self.optim_cls, *optim_args, **optim_kwargs) self._opt_hook_state = _OptimizerHookState(f_optim, params)
def register_ddp(self, ddp_inst: DistributedDataParallel): # NOTE: using a custom communication hook and fused optimizer is not # yet supported. ddp_inst.register_comm_hook( # type: ignore[operator] None, # wrapped hook state _hook_then_optimizer(allreduce_hook, self._opt_hook_state), )
# TODO: register_fsdp once FSDP supports communication hook. def register_fsdp(self, fsdp: FullyShardedDataParallel) -> None: """Register the overlapped optimizer with FSDP.""" raise NotImplementedError( f"{self.__class__.__name__} does not support overlapped FSDP." )
def _as_overlapped_optim(optim_cls: Type, params, *args, **kwargs): """Return a new ``OverlappedOptimizer`` instance that supports ``optim_cls``.""" for clz in inspect.getmro(optim_cls): try: return _registered_overlapped_optims[clz]( optim_cls, params, *args, **kwargs ) except KeyError: pass
# Fallback to standard overlapped optimizer, which will raise errors if user # is attempting to use an unsupported optimizer. return _OverlappedStandardOptimizer(optim_cls, params, *args, **kwargs)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150# mypy: allow-untyped-defsimport functoolsfrom enum import Enum
import torchimport torch.distributed as dist
TORCH_HALF_MIN = torch.finfo(torch.float16).minTORCH_HALF_MAX = torch.finfo(torch.float16).max
class DQuantType(Enum): """ Different quantization methods for auto_quantize API are identified here.
auto_quantize API currently supports fp16 and bfp16 methods. """
FP16 = ("fp16",) BFP16 = "bfp16"
def __str__(self) -> str: return self.value
def _fp32_to_fp16_with_clamp(tensor: torch.Tensor) -> torch.Tensor: return torch.clamp(tensor, TORCH_HALF_MIN, TORCH_HALF_MAX).half()
def _quantize_tensor(tensor, qtype): if not isinstance(tensor, torch.Tensor): raise RuntimeError( f"_quantize_tensor expecting torch.Tensor as input but found {type(tensor)}" ) if qtype == DQuantType.FP16: return _fp32_to_fp16_with_clamp(tensor) elif qtype == DQuantType.BFP16: return torch.ops.quantization._FloatToBfloat16Quantized(tensor) else: raise RuntimeError(f"Quantization type {qtype} is not supported")
def _quantize_tensor_list(tensor_list, qtype): if not isinstance(tensor_list, list) or not all( isinstance(p, torch.Tensor) for p in tensor_list ): raise RuntimeError( f"_quantize_tensor_list expecting list of torch.Tensor as input but found {type(tensor_list)}" ) quantized_tensor_list = [_quantize_tensor(t, qtype) for t in tensor_list] return quantized_tensor_list
def _dequantize_tensor(tensor, qtype, quant_loss=None): if not isinstance(tensor, torch.Tensor): raise RuntimeError( f"_dequantize_tensor expecting torch.Tensor as input but found {type(tensor)}" ) if qtype == DQuantType.FP16: if tensor.dtype != torch.float16: raise RuntimeError( f"tensor dtype is {tensor.dtype} while expected to be FP16." ) elif tensor.dtype == torch.float16 and quant_loss is None: return tensor.float() else: return tensor.float() / quant_loss elif qtype == DQuantType.BFP16: if tensor.dtype != torch.float16: raise RuntimeError( f"tensor dtype is {tensor.dtype} while expected to be FP16." ) else: return torch.ops.quantization._Bfloat16QuantizedToFloat(tensor) else: raise RuntimeError(f"Quantization type {qtype} is not supported")
def _dequantize_tensor_list(tensor_list, qtype, quant_loss=None): if not isinstance(tensor_list, list) or not all( isinstance(p, torch.Tensor) for p in tensor_list ): raise RuntimeError( f"_dequantize_tensor_list expecting list of torch.Tensor as input but found {type(tensor_list)}" ) dequantized_tensor_list = [_dequantize_tensor(t, qtype) for t in tensor_list] return dequantized_tensor_list
def auto_quantize(func, qtype, quant_loss=None): """ Quantize the input tensors, choose the precision types, and pass other necessary arguments and then dequantizes the output.
Currently it only supports: . FP16 and BFP16 quantization method supported for gloo and nccl backends . all_gather, all_to_all collective ops Note: BFP16 only supports 2D tensors. Args: func (Callable): A function representing collective operations. qtype (QuantType): Quantization method quant_loss (float, optional): This can be used to improve accuracy in the dequantization. Returns: (Callable): the same collective as func but enables automatic quantization/dequantization. """
@functools.wraps(func) def wrapper(*args, **kwargs): group = kwargs.get("group", None) async_op = kwargs.get("async_op", False) if async_op is True: raise RuntimeError("The async_op=True mode is not supported yet.") if func == dist.all_gather: tensors = args[0] input_tensors = _quantize_tensor(args[1], qtype) out_tensors = _quantize_tensor_list(tensors, qtype) dist.all_gather(out_tensors, input_tensors, group=group, async_op=async_op) for i, t in enumerate( _dequantize_tensor_list(out_tensors, qtype, quant_loss=quant_loss) ): tensors[i] = t
elif func == dist.all_to_all: tensors = args[0] input_tensors = _quantize_tensor_list(args[1], qtype) out_tensors = _quantize_tensor_list(tensors, qtype) dist.all_to_all(out_tensors, input_tensors, group=group, async_op=async_op) for i, t in enumerate( _dequantize_tensor_list(out_tensors, qtype, quant_loss=quant_loss) ): tensors[i] = t
elif func == dist.all_to_all_single: tensors = args[0] out_splits = kwargs.get("out_splits", None) in_splits = kwargs.get("in_splits", None) # Quantizing the input/output tensor input_tensors = _quantize_tensor(args[1], qtype) out_tensors = _quantize_tensor(tensors, qtype) dist.all_to_all_single( out_tensors, input_tensors, out_splits, in_splits, group=group ) for i, t in enumerate( _dequantize_tensor(out_tensors, qtype, quant_loss=quant_loss) ): tensors[i] = t else: raise RuntimeError(f"The collective op {func} is not supported yet")
return wrapper123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110# mypy: allow-untyped-defsfrom enum import Enumfrom functools import partial
import torch.distributed as dist
from . import ( debugging_hooks as debugging, default_hooks as default, optimizer_overlap_hooks as optimizer_overlap, powerSGD_hook as powerSGD, quantization_hooks as quantization,)
__all__ = ["DDPCommHookType", "register_ddp_comm_hook"]
def _ddp_comm_hook_wrapper(comm_hook, model, state): model.register_comm_hook(state, comm_hook)
def _powerSGD_comm_hook_wrapper( comm_hook, model, state, matrix_approximation_rank, start_powerSGD_iter=1_000,): """ Wrap PowerSGD communication hook.
To be consistent with the wrappers of other DDP comm hooks, the input state only needs to be a process group, which will be wrapped up with other state info. """ powerSGD_state = powerSGD.PowerSGDState( process_group=state, matrix_approximation_rank=matrix_approximation_rank, start_powerSGD_iter=start_powerSGD_iter, ) model.register_comm_hook(powerSGD_state, comm_hook)
class DDPCommHookType(Enum): """ Enumerate ``ddp_comm_hooks`` and ``ddp_comm_hook_wrapper`` communucation hook types.
DDPCommHookType enumerates the hooks of ``torch.distributed.algorithms.ddp_comm_hooks`` as names and ``ddp_comm_hook_wrapper`` partials with hook specified. As an example, you can register allreduce hook by ``DDPCommHookType.ALLREDUCE.value(model=model, state=process_group)``. """
ALLREDUCE = partial(_ddp_comm_hook_wrapper, comm_hook=default.allreduce_hook) FP16_COMPRESS = partial( _ddp_comm_hook_wrapper, comm_hook=default.fp16_compress_hook ) BF16_COMPRESS = partial( _ddp_comm_hook_wrapper, comm_hook=default.bf16_compress_hook ) QUANTIZE_PER_TENSOR = partial( _ddp_comm_hook_wrapper, comm_hook=quantization.quantization_pertensor_hook ) QUANTIZE_PER_CHANNEL = partial( _ddp_comm_hook_wrapper, comm_hook=quantization.quantization_perchannel_hook ) POWER_SGD = partial( _powerSGD_comm_hook_wrapper, comm_hook=powerSGD.powerSGD_hook, matrix_approximation_rank=1, ) # Rank-2 PowerSGD can give a higher accuracy than the default rank-1 version, # but it runs slower and consumes more memory. POWER_SGD_RANK2 = partial( _powerSGD_comm_hook_wrapper, comm_hook=powerSGD.powerSGD_hook, matrix_approximation_rank=2, ) # Batching can lead to a faster training at the cost of accuracy. BATCHED_POWER_SGD = partial( _powerSGD_comm_hook_wrapper, comm_hook=powerSGD.batched_powerSGD_hook, matrix_approximation_rank=1, ) BATCHED_POWER_SGD_RANK2 = partial( _powerSGD_comm_hook_wrapper, comm_hook=powerSGD.batched_powerSGD_hook, matrix_approximation_rank=2, ) NOOP = partial( _ddp_comm_hook_wrapper, comm_hook=debugging.noop_hook, )
def register_ddp_comm_hook(comm_hook_type: DDPCommHookType, model, state=None): """ Register ``ddp_comm_hooks`` to DDP model.
Registers the hooks of ``torch.distributed.algorithms.ddp_comm_hooks`` to the DDP model. User can specify the type of hook as an enum ``DDPCommHookType`` type using ``comm_hook_type`` input. State input will be passed to the model. Uses Python comm hook implementations.
Example:: >>> # xdoctest: +SKIP >>> register_ddp_comm_hook(DDPCommHookType.FP16_COMPRESS, model, state) """ comm_hook_type.value(model=model, state=state)lib/python3.12/site-packages/torch/distributed/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/__pycache__/_state_dict_utils.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/__pycache__/argparse_util.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/__pycache__/launch.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/__pycache__/logging_handlers.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/__pycache__/remote_device.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/__pycache__/run.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/__pycache__/utils.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_composable/__init__.pyAdded+3
lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/checkpoint_activation.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/contract.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_composable/__pycache__/replicate.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_composable/checkpoint_activation.pyAdded+126
lib/python3.12/site-packages/torch/distributed/_composable/contract.pyAdded+224
lib/python3.12/site-packages/torch/distributed/_composable/fsdp/__init__.pyAdded+3
lib/python3.12/site-packages/torch/distributed/_composable/fsdp/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_composable/fsdp/__pycache__/fully_shard.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/_composable/fsdp/fully_shard.pyAdded+8
lib/python3.12/site-packages/torch/distributed/_composable/replicate.pyAdded+254
lib/python3.12/site-packages/torch/distributed/_sharded_tensor/__init__.pyAdded+21
lib/python3.12/site-packages/torch/distributed/_sharded_tensor/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/__init__.pyAdded+1
lib/python3.12/site-packages/torch/distributed/algorithms/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/__pycache__/join.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/__init__.pyAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/__pycache__/checkpoint_wrapper.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_checkpoint/checkpoint_wrapper.pyAdded+323
lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/__init__.pyAdded+7
lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/__pycache__/default_hooks.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_comm_hooks/default_hooks.pyAdded+192
lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/__init__.pyAdded+1
lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/__pycache__/optimizer_overlap.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_optimizer_overlap/optimizer_overlap.pyAdded+97
lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/__init__.pyAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/__pycache__/quantization.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/_quantization/quantization.pyAdded+150
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__init__.pyAdded+110
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/ddp_zero_hook.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/debugging_hooks.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/default_hooks.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/mixed_precision_hooks.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/optimizer_overlap_hooks.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/post_localSGD_hook.cpython-312.pycAdded
lib/python3.12/site-packages/torch/distributed/algorithms/ddp_comm_hooks/__pycache__/powerSGD_hook.cpython-312.pycAdded