File size: 3,157 Bytes
2961be3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2020-2026 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Distributed training backend abstraction."""

from contextlib import contextmanager


class DistributedBackend:
    """Abstracts distributed backend specifics (DeepSpeed ZeRO, FSDP) behind a uniform API.

    Detects the active backend once at construction from ``accelerator.state``, then provides context managers that are
    no-ops on backends where the operation is not needed.

    Args:
        accelerator ([`~accelerate.Accelerator`]):
            The accelerator instance managing the distributed state.

    Example:

    ```python
    >>> dist = DistributedBackend(accelerator)
    >>> with dist.gather_params(list(model.parameters())):
    ...     model.merge_adapter()
    >>> with dist.summon_full_params(model, recurse=False):
    ...     outputs = model.generate(inputs)
    ```
    """

    def __init__(self, accelerator):
        ds_plugin = accelerator.state.deepspeed_plugin
        fsdp_plugin = getattr(accelerator.state, "fsdp_plugin", None)
        self.zero_stage = ds_plugin.zero_stage if ds_plugin else 0
        self.fsdp_version = getattr(fsdp_plugin, "fsdp_version", None) if fsdp_plugin else None

    @property
    def is_zero3(self) -> bool:
        """Whether DeepSpeed ZeRO Stage 3 is active."""
        return self.zero_stage == 3

    @property
    def is_fsdp(self) -> bool:
        """Whether FSDP (any version) is active."""
        return self.fsdp_version is not None

    @contextmanager
    def gather_params(self, params):
        """Gather sharded parameters under DeepSpeed ZeRO-3; no-op otherwise.

        Args:
            params (iterable of `torch.nn.Parameter`):
                Parameters to gather.
        """
        if self.is_zero3:
            import deepspeed

            with deepspeed.zero.GatheredParameters(params):
                yield
        else:
            yield

    @contextmanager
    def summon_full_params(self, module, **kwargs):
        """Materialize full FSDP v1 parameters; no-op for FSDP v2 or non-FSDP backends.

        FSDP v2 parameters are always accessible and require no explicit materialization.

        Args:
            module (`torch.nn.Module`):
                The FSDP-wrapped module.
            **kwargs:
                Forwarded to ``FSDP.summon_full_params`` (e.g. ``recurse``, ``writeback``).
        """
        if self.fsdp_version == 1:
            from torch.distributed.fsdp import FullyShardedDataParallel as FSDP

            with FSDP.summon_full_params(module, **kwargs):
                yield
        else:
            yield