process_mesh.py 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2021 PaddlePaddle Authors. 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.

import copy
16 17 18

import numpy as np

19
import paddle
20
from paddle.framework import core
21

22 23 24
# Use to store the previous and current process mesh
_g_previous_process_mesh = None
_g_current_process_mesh = None
25 26
# {shape_process_ids : unique_id}
_g_unique_process_mesh_map = {}
27 28


29 30 31
def get_current_process_mesh():
    global _g_current_process_mesh
    return _g_current_process_mesh
32 33


34 35 36 37 38
def set_current_process_mesh(process_mesh):
    global _g_previous_process_mesh
    global _g_current_process_mesh
    _g_previous_process_mesh = _g_current_process_mesh
    _g_current_process_mesh = process_mesh
39 40


41 42 43 44
def reset_current_process_mesh():
    global _g_previous_process_mesh
    global _g_current_process_mesh
    _g_current_process_mesh = _g_previous_process_mesh
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
def get_unique_id_for_process_mesh(shape, process_ids):
    key = f"shape {shape}, process_ids {process_ids}"
    global _g_unique_process_mesh_map
    if key in _g_unique_process_mesh_map:
        unique_id = _g_unique_process_mesh_map[key]
    else:
        unique_id = len(_g_unique_process_mesh_map) + 1
        _g_unique_process_mesh_map[key] = unique_id

    return unique_id


def retrive_unique_id_for_process_mesh(shape, process_ids):
    key = f"shape {shape}, process_ids {process_ids}"
    global _g_unique_process_mesh_map
    assert key in _g_unique_process_mesh_map
    return _g_unique_process_mesh_map[key]


def get_unique_process_mesh_map():
    global _g_unique_process_mesh_map
    return _g_unique_process_mesh_map


71
class ProcessMesh(core.ProcessMesh):
72
    """
73
    The `ProcessMesh` object describes the Cartesian topology of the used processes.
74

75
    Args:
76
        mesh (list|numpy.array): an n-dimensional array describes the topology
77 78 79
            of the processes.
        dim_names (list, optional): the i-th element of this list gives the name of the
            i-th dimension of the mesh.
80

81 82 83 84
    Examples:
        .. code-block:: python

            import paddle
85
            import paddle.distributed as dist
86

87
            mesh = dist.ProcessMesh([[2, 4, 5], [0, 1, 3]], dim_names=["x", "y"])
88
            assert mesh.shape == [2, 3]
89
            assert mesh.process_ids == [2, 4, 5, 0, 1, 3]
90 91 92

    """

93 94 95 96 97 98 99 100
    def __init__(self, mesh=None, dim_names=None, shape=None, process_ids=None):
        # Use shape and process_ids just for compatibility
        # Users should not use these directly
        if mesh is None:
            assert shape is not None
            assert process_ids is not None
            mesh = np.array(process_ids).reshape(shape)

101
        if not isinstance(mesh, list) and not isinstance(mesh, np.ndarray):
102
            raise ValueError(
103 104
                'The mesh must be an instance of list or np.ndarray.'
            )
105 106 107
        if isinstance(mesh, list):
            mesh = np.array(mesh)

108 109 110
        if dim_names is not None and not isinstance(dim_names, list):
            raise ValueError('The dim_names must be an instance of list.')

111 112 113 114
        self._mesh = mesh
        self._shape = list(self._mesh.shape)
        self._process_ids = self._mesh.flatten().tolist()

115 116 117 118 119 120
        assert all(
            isinstance(p, int) for p in self._process_ids
        ), "All elements of the mesh must be integer"
        assert (
            min(self._process_ids) >= 0
        ), 'All elements of the mesh must be >= 0.'
121 122
        unique_process_ids = set(self._process_ids)
        assert len(unique_process_ids) == len(
123 124
            self._process_ids
        ), 'All elements of the mesh must be unique.'
125 126

        if dim_names is not None:
127 128 129
            assert len(dim_names) == len(
                self._shape
            ), "The length of dims_names must be same as the shape of the mesh."
130 131 132 133
            self._dim_names = copy.deepcopy(dim_names)
        else:
            self._dim_names = ["d" + str(i) for i in range(len(self._shape))]
        unique_dim_names = set(self._dim_names)
134 135
        assert len(unique_dim_names) == len(
            self._dim_names
136
        ), f'All dim_names {dim_names} must be unique.'
137

138 139 140 141 142
        # Follow the requirement for using pybind11
        core.ProcessMesh.__init__(
            self, self._shape, self._process_ids, self._dim_names
        )

143
        # Store all process meshes
144
        from .static.dist_context import get_default_distributed_context
145

146 147
        default_dist_cxt = get_default_distributed_context()
        default_dist_cxt.add_process_mesh(self)
148
        # Add new processes to process group 0
149
        from .static.process_group import get_process_group
150

151
        pg0 = get_process_group(0)
152
        pg0.add_ranks(self.process_ids)
153

154 155 156 157 158
        # Uniqe Mesh Id
        self._unique_id = get_unique_id_for_process_mesh(
            self._shape, self._process_ids
        )

159 160 161 162 163 164 165
    @property
    def mesh(self):
        """
        Get the underlying mesh of ProcessMesh.
        """
        return self._mesh

166 167 168 169 170 171 172
    @property
    def dim_names(self):
        """
        Get the underlying dimension names of ProcessMesh.
        """
        return self._dim_names

173 174 175 176 177 178 179 180 181 182
    @property
    def unique_id(self):
        """
        Get the unique id of ProcessMesh.
        NOTE
        Unique id only take process_ids and shape into account.
        Different ProcessMesh with same process_ids and shape have same unique id.
        """
        return self._unique_id

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    def __getitem__(self, index):
        if isinstance(index, tuple):
            new_dim_names = []
            for i, item in enumerate(index):
                if isinstance(item, slice):
                    new_dim_names.append(self._dim_names[i])
            new_mesh = self._mesh[index]
            if new_mesh.shape:
                return ProcessMesh(new_mesh, new_dim_names)
            else:
                # Wrap a scalar into a list but without dim_names
                return ProcessMesh([new_mesh])
        elif isinstance(index, slice):
            new_mesh = self._mesh[index]
            new_dim_names = self._dim_names
            return ProcessMesh(new_mesh, new_dim_names)
        else:
            new_mesh = self._mesh[index]
            new_dim_names = self._dim_names[1:]
202 203 204 205
            if new_mesh.shape:
                return ProcessMesh(new_mesh, new_dim_names)
            else:
                return ProcessMesh([new_mesh])
206 207 208

    def __enter__(self):
        set_current_process_mesh(self)
209
        default_prog = paddle.static.default_main_program()
210 211 212 213 214
        cur_block = default_prog.current_block()
        self._old_var_names = list(cur_block.vars.keys())
        self._old_op_size = len(cur_block.ops)

    def __exit__(self, exc_type, exc_value, exc_traceback):
215 216
        from .static.dist_op import DistributedOperator
        from .static.dist_tensor import DistributedTensor
217

218
        default_prog = paddle.static.default_main_program()
219 220 221
        cur_block = default_prog.current_block()
        new_var_names = list(cur_block.vars.keys())
        new_op_size = len(cur_block.ops)
222
        from .static.dist_context import get_default_distributed_context
223

224 225 226 227 228
        default_dist_ctx = get_default_distributed_context()
        for name in new_var_names:
            if name not in self._old_var_names:
                tensor = cur_block.vars[name]
                dist_tensor = default_dist_ctx.get_dist_tensor_for_program(
229 230
                    tensor
                )
231
                if dist_tensor is None:
232 233
                    dist_tensor = DistributedTensor(cur_block.vars[name])
                    dist_tensor.dist_attr.process_mesh = self
234 235 236 237 238 239 240 241 242 243 244
                    dist_tensor.dist_attr.mark_annotated("process_mesh")
                    default_dist_ctx.add_dist_tensor_for_program(dist_tensor)
                else:
                    if dist_tensor.dist_attr.process_mesh is None:
                        dist_tensor.dist_attr.process_mesh = self
                        dist_tensor.dist_attr.mark_annotated("process_mesh")

        for idx in range(self._old_op_size, new_op_size):
            op = cur_block.ops[idx]
            dist_op = default_dist_ctx.get_dist_op_for_program(op)
            if dist_op is None:
245 246
                dist_op = DistributedOperator(op)
                dist_op.dist_attr.process_mesh = self
247 248 249 250 251 252 253
                dist_op.dist_attr.mark_annotated("process_mesh")
                default_dist_ctx.add_dist_op_for_program(dist_op)
            else:
                if dist_op.dist_attr.process_mesh is None:
                    dist_op.dist_attr.process_mesh = self
                    dist_op.dist_attr.mark_annotated("process_mesh")
        reset_current_process_mesh()
254

255 256 257 258 259 260 261
    def __deepcopy__(self, memo):
        if id(self) in memo:
            return memo[id(self)]
        new_process_mesh = ProcessMesh(np.array(self.mesh), self.dim_names)
        memo[id(self)] = new_process_mesh
        return new_process_mesh

262
    def __eq__(self, other):
263
        if not isinstance(other, (ProcessMesh, core.ProcessMesh)):
264
            return False
265
        if self.shape != other.shape or self.process_ids != other.process_ids:
266 267 268 269 270 271 272
            return False
        return True

    def __ne__(self, other):
        return not self.__eq__(other)

    def __str__(self):
273
        str = "shape {}, process_ids {}, dim_nams {}".format(
274 275
            self.shape, self.process_ids, self.dim_names
        )
276
        return str
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324


def compute_compatible_process_mesh(process_mesh_list):
    """Compute the compatible process mesh given a list of process meshes."""
    if not process_mesh_list:
        return None

    def _compute_compatible_process_mesh_of_two(pm1, pm2):
        if pm1 is None:
            return True, pm2
        if pm2 is None:
            return True, pm1
        if pm1 == pm2:
            return True, pm1
        if pm1.process_ids == pm2.process_ids:
            if len(pm1.shape) >= len(pm2.shape):
                return True, pm1
            else:
                return True, pm2
        process_set1 = set(pm1.process_ids)
        process_set2 = set(pm2.process_ids)
        if process_set1.issubset(process_set2):
            return True, pm2
        if process_set2.issubset(process_set1):
            return True, pm1
        return False, None

    compatible_result = None
    for process_mesh in process_mesh_list:
        compatible, compatible_result = _compute_compatible_process_mesh_of_two(
            compatible_result, process_mesh
        )
        if not compatible:
            return None
    return copy.deepcopy(compatible_result)


def merge_process_meshes(process_meshes):
    """Merge a list of process meshes."""
    merged_process_mesh = None
    merged_process_ids = set()
    for process_mesh in process_meshes:
        if process_mesh is not None:
            process_ids = set(process_mesh.process_ids)
            merged_process_ids = merged_process_ids.union(process_ids)
    if len(merged_process_ids) != 0:
        merged_process_mesh = ProcessMesh(list(merged_process_ids))
    return merged_process_mesh