Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\accelerate\local_sgd.py with huggingface_hub
Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//accelerate//local_sgd.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
import torch
|
| 15 |
+
|
| 16 |
+
from accelerate import Accelerator, DistributedType
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class LocalSGD:
|
| 20 |
+
"""
|
| 21 |
+
A helper class to support local SGD on top of Accelerator. It simply runs a given number of updates independently
|
| 22 |
+
on each device, and averages model weights every K synchronization step.
|
| 23 |
+
|
| 24 |
+
It should be used only in the multi-GPU (or multi-CPU) setup without extensions such as DeepSpeed. In particular,
|
| 25 |
+
this is a simple implementation that cannot support scenarios such as model parallelism.
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
Although we are not aware of the true origins of this simple approach, the idea of local SGD is quite old and goes
|
| 29 |
+
back to at least:
|
| 30 |
+
|
| 31 |
+
Zhang, J., De Sa, C., Mitliagkas, I., & Ré, C. (2016). [Parallel SGD: When does averaging help?. arXiv preprint
|
| 32 |
+
arXiv:1606.07365.](https://huggingface.co/papers/1606.07365)
|
| 33 |
+
|
| 34 |
+
We credit the term Local SGD to the following paper (but there might be earlier references we are not aware of).
|
| 35 |
+
|
| 36 |
+
Stich, Sebastian Urban. ["Local SGD Converges Fast and Communicates Little." ICLR 2019-International Conference on
|
| 37 |
+
Learning Representations. No. CONF. 2019.](https://huggingface.co/papers/1805.09767)
|
| 38 |
+
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
def __enter__(self):
|
| 42 |
+
if self.enabled:
|
| 43 |
+
self.model_sync_obj = self.model.no_sync()
|
| 44 |
+
self.model_sync_obj.__enter__()
|
| 45 |
+
|
| 46 |
+
return self
|
| 47 |
+
|
| 48 |
+
def __exit__(self, type, value, tb):
|
| 49 |
+
if self.enabled:
|
| 50 |
+
# Average all models on exit
|
| 51 |
+
self._sync_and_avg_model_params()
|
| 52 |
+
self.model_sync_obj.__exit__(type, value, tb)
|
| 53 |
+
|
| 54 |
+
def __init__(self, accelerator: Accelerator, model: torch.nn.Module, local_sgd_steps: int, enabled: bool = True):
|
| 55 |
+
"""
|
| 56 |
+
Constructor.
|
| 57 |
+
|
| 58 |
+
Args:
|
| 59 |
+
model (`torch.nn.Module):
|
| 60 |
+
The model whose parameters we need to average.
|
| 61 |
+
accelerator (`Accelerator`):
|
| 62 |
+
Accelerator object.
|
| 63 |
+
local_sgd_steps (`int`):
|
| 64 |
+
A number of local SGD steps (before model parameters are synchronized).
|
| 65 |
+
enabled (`bool):
|
| 66 |
+
Local SGD is disabled if this parameter set to `False`.
|
| 67 |
+
"""
|
| 68 |
+
if accelerator.distributed_type not in [
|
| 69 |
+
DistributedType.NO,
|
| 70 |
+
DistributedType.MULTI_CPU,
|
| 71 |
+
DistributedType.MULTI_GPU,
|
| 72 |
+
DistributedType.MULTI_XPU,
|
| 73 |
+
DistributedType.MULTI_MLU,
|
| 74 |
+
DistributedType.MULTI_HPU,
|
| 75 |
+
DistributedType.MULTI_SDAA,
|
| 76 |
+
DistributedType.MULTI_MUSA,
|
| 77 |
+
DistributedType.MULTI_NPU,
|
| 78 |
+
]:
|
| 79 |
+
raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)")
|
| 80 |
+
self.enabled = enabled and accelerator.distributed_type != DistributedType.NO
|
| 81 |
+
self.num_steps = 0
|
| 82 |
+
if self.enabled:
|
| 83 |
+
self.accelerator = accelerator
|
| 84 |
+
self.model = model
|
| 85 |
+
self.local_sgd_steps = local_sgd_steps
|
| 86 |
+
|
| 87 |
+
def step(self):
|
| 88 |
+
"""
|
| 89 |
+
This function makes a "step" and synchronizes model parameters if necessary.
|
| 90 |
+
"""
|
| 91 |
+
self.num_steps += 1
|
| 92 |
+
if not self.enabled:
|
| 93 |
+
return
|
| 94 |
+
|
| 95 |
+
if self.num_steps % self.local_sgd_steps == 0:
|
| 96 |
+
self._sync_and_avg_model_params()
|
| 97 |
+
|
| 98 |
+
def _sync_and_avg_model_params(self):
|
| 99 |
+
"""
|
| 100 |
+
Synchronize + Average model parameters across all GPUs
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
self.accelerator.wait_for_everyone()
|
| 104 |
+
with self.accelerator.autocast():
|
| 105 |
+
for param in self.model.parameters():
|
| 106 |
+
param.data = self.accelerator.reduce(param.data, reduction="mean")
|