|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# DeepSpeed Team |
| 5 | + |
| 6 | +import torch |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from deepspeed.runtime.engine import DeepSpeedEngine |
| 11 | + |
| 12 | + |
| 13 | +def _flatten_dense_tensors(tensors): |
| 14 | + """Flatten dense tensors into a contiguous 1D buffer. Assume tensors are of |
| 15 | + same dense type. |
| 16 | +
|
| 17 | + Since inputs are dense, the resulting tensor will be a concatenated 1D |
| 18 | + buffer. Element-wise operation on this buffer will be equivalent to |
| 19 | + operating individually. |
| 20 | +
|
| 21 | + Args: |
| 22 | + tensors (Iterable[Tensor]): dense tensors to flatten. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + A contiguous 1D buffer containing input tensors. |
| 26 | + """ |
| 27 | + transposed_tensors = [t.transpose(0, 1).contiguous() if t.dim() == 2 else t for t in tensors] |
| 28 | + return torch._C._nn.flatten_dense_tensors(transposed_tensors) |
| 29 | + |
| 30 | + |
| 31 | +def _unflatten_dense_tensors(flat, tensors): |
| 32 | + """View a flat buffer using the sizes of tensors. Assume that tensors are of |
| 33 | + same dense type, and that flat is given by _flatten_dense_tensors. |
| 34 | +
|
| 35 | + Args: |
| 36 | + flat (Tensor): flattened dense tensors to unflatten. |
| 37 | + tensors (Iterable[Tensor]): dense tensors whose sizes will be used to |
| 38 | + unflatten flat. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + Unflattened dense tensors with sizes same as tensors and values from |
| 42 | + flat. |
| 43 | + """ |
| 44 | + transposed_tensors = [t.transpose(0, 1) if t.dim() == 2 else t for t in tensors] |
| 45 | + unflat = torch._C._nn.unflatten_dense_tensors(flat, transposed_tensors) |
| 46 | + return [t.transpose(0, 1) if t.dim() == 2 else t for t in unflat] |
| 47 | + |
| 48 | + |
| 49 | +def configure_zenflow(engine: "DeepSpeedEngine") -> None: |
| 50 | + zenflow_config = engine.zenflow_config() |
| 51 | + if zenflow_config == None: |
| 52 | + engine.zenflow = False |
| 53 | + return |
| 54 | + |
| 55 | + engine.zenflow = True |
| 56 | + select_strategy = zenflow_config.select_strategy |
| 57 | + |
| 58 | + if select_strategy == 'auto': |
| 59 | + select_strategy = "epoch" |
| 60 | + if isinstance(zenflow_config.select_interval, int): |
| 61 | + raise Warning( |
| 62 | + "If use auto select strategy, select_interval will be set to 1 and select_strategy will be set to epoch, thus select_interval would be overwritten." |
| 63 | + ) |
| 64 | + engine.select_interval = 1 |
| 65 | + else: |
| 66 | + if isinstance(zenflow_config.select_interval, str): |
| 67 | + raise ValueError("If don't use auto select strategy, select_interval must be a number.") |
| 68 | + engine.select_interval = zenflow_config.select_interval |
| 69 | + |
| 70 | + if isinstance(zenflow_config.update_interval, str): |
| 71 | + engine.auto_update = True |
| 72 | + engine.update_interval = 0 |
| 73 | + else: |
| 74 | + engine.auto_update = False |
| 75 | + engine.update_interval = int(zenflow_config.update_interval) |
| 76 | + |
| 77 | + if select_strategy == 'epoch': |
| 78 | + zenflow_config.steps_per_epoch = len(engine.training_dataloader) |
| 79 | + engine.select_interval = engine.select_interval * len(engine.training_dataloader) |
| 80 | + |
| 81 | + if not engine.auto_update and engine.select_interval != 0 and engine.select_interval < engine.update_interval: |
| 82 | + raise ValueError("Select interval must be greater or equal to update interval") |
| 83 | + |
| 84 | + engine.overlap_step = zenflow_config.overlap_step |
| 85 | + |
| 86 | + engine.full_warm_up_rounds = zenflow_config.full_warm_up_rounds |
| 87 | + |
| 88 | + engine._config.gradient_accumulation_steps = engine.update_interval |
0 commit comments