| 1 |
# Copyright 2024 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 |
|
| 15 |
import inspect |
| 16 |
import json |
| 17 |
from functools import partial |
| 18 |
from pathlib import Path |
| 19 |
|
| 20 |
import torch |
| 21 |
from transformers import AutoModelForCausalLM |
| 22 |
|
| 23 |
from accelerate import Accelerator, DeepSpeedPlugin |
| 24 |
from accelerate.commands.launch import launch_command, launch_command_parser |
| 25 |
from accelerate.test_utils.testing import ( |
| 26 |
AccelerateTestCase, |
| 27 |
path_in_accelerate_package, |
| 28 |
require_deepspeed, |
| 29 |
require_huggingface_suite, |
| 30 |
require_multi_device, |
| 31 |
require_non_cpu, |
| 32 |
run_first, |
| 33 |
slow, |
| 34 |
) |
| 35 |
from accelerate.test_utils.training import RegressionDataset |
| 36 |
from accelerate.utils import patch_environment |
| 37 |
from accelerate.utils.deepspeed import DummyOptim, DummyScheduler, get_active_deepspeed_plugin |
| 38 |
|
| 39 |
|
| 40 |
GPT2_TINY = "hf-internal-testing/tiny-random-gpt2" |
| 41 |
|
| 42 |
|
| 43 |
@require_deepspeed |
| 44 |
@require_non_cpu |
| 45 |
class DeepSpeedConfigIntegration(AccelerateTestCase): |
| 46 |
parser = launch_command_parser() |
| 47 |
test_scripts_folder = path_in_accelerate_package("test_utils", "scripts", "external_deps") |
| 48 |
|
| 49 |
def setUp(self): |
| 50 |
super().setUp() |
| 51 |
|
| 52 |
self.dist_env = dict( |
| 53 |
ACCELERATE_USE_DEEPSPEED="true", |
| 54 |
MASTER_ADDR="localhost", |
| 55 |
MASTER_PORT="10999", |
| 56 |
RANK="0", |
| 57 |
LOCAL_RANK="0", |
| 58 |
WORLD_SIZE="1", |
| 59 |
) |
| 60 |
|
| 61 |
self._test_file_path = inspect.getfile(self.__class__) |
| 62 |
path = Path(self._test_file_path).resolve() |
| 63 |
self.test_file_dir_str = str(path.parents[0]) |
| 64 |
|
| 65 |
self.ds_config_file = dict( |
| 66 |
zero2=f"{self.test_file_dir_str}/ds_config_zero2.json", |
| 67 |
zero3_inference=f"{self.test_file_dir_str}/ds_config_zero3_model_only.json", |
| 68 |
zero3_training=f"{self.test_file_dir_str}/ds_config_zero3.json", |
| 69 |
) |
| 70 |
|
| 71 |
with open(self.ds_config_file["zero2"], encoding="utf-8") as f: |
| 72 |
self.config_zero2 = json.load(f) |
| 73 |
with open(self.ds_config_file["zero3_training"], encoding="utf-8") as f: |
| 74 |
self.config_zero3 = json.load(f) |
| 75 |
with open(self.ds_config_file["zero3_inference"], encoding="utf-8") as f: |
| 76 |
self.config_zero3_inference = json.load(f) |
| 77 |
|
| 78 |
self.model_init = partial(AutoModelForCausalLM.from_pretrained, GPT2_TINY) |
| 79 |
|
| 80 |
def get_ds_plugins(self, zero3_inference=False): |
| 81 |
ds_zero2 = DeepSpeedPlugin( |
| 82 |
hf_ds_config=self.config_zero2, |
| 83 |
) |
| 84 |
ds_zero3 = DeepSpeedPlugin( |
| 85 |
hf_ds_config=self.config_zero3 if not zero3_inference else self.config_zero3_inference, |
| 86 |
) |
| 87 |
return {"zero2": ds_zero2, "zero3": ds_zero3} |
| 88 |
|
| 89 |
def test_select_plugin(self): |
| 90 |
ds_plugins = self.get_ds_plugins() |
| 91 |
ds_zero2, ds_zero3 = ds_plugins.values() |
| 92 |
accelerator = Accelerator( |
| 93 |
deepspeed_plugin=ds_plugins, |
| 94 |
) |
| 95 |
# Accelerator's constructor should automatically enable the first plugin |
| 96 |
assert ds_zero2.selected |
| 97 |
assert not ds_zero3.selected |
| 98 |
assert get_active_deepspeed_plugin(accelerator.state) == ds_zero2 |
| 99 |
assert accelerator.deepspeed_plugin == ds_zero2 |
| 100 |
assert accelerator.state.get_deepspeed_plugin("zero2") == ds_zero2 |
| 101 |
accelerator.state.select_deepspeed_plugin("zero3") |
| 102 |
assert not ds_zero2.selected |
| 103 |
assert ds_zero3.selected |
| 104 |
assert get_active_deepspeed_plugin(accelerator.state) == ds_zero3 |
| 105 |
assert accelerator.deepspeed_plugin == ds_zero3 |
| 106 |
assert accelerator.state.get_deepspeed_plugin("zero3") == ds_zero3 |
| 107 |
accelerator.state.select_deepspeed_plugin("zero2") |
| 108 |
assert not ds_zero3.selected |
| 109 |
assert ds_zero2.selected |
| 110 |
assert get_active_deepspeed_plugin(accelerator.state) == ds_zero2 |
| 111 |
assert accelerator.deepspeed_plugin == ds_zero2 |
| 112 |
assert accelerator.state.get_deepspeed_plugin("zero2") == ds_zero2 |
| 113 |
|
| 114 |
@require_huggingface_suite |
| 115 |
def test_config_reference_update(self): |
| 116 |
# Make sure that the transformers weakref is updating when we update the config |
| 117 |
ds_plugins = self.get_ds_plugins(zero3_inference=True) |
| 118 |
zero2, zero3 = ds_plugins.values() |
| 119 |
accelerator = Accelerator(deepspeed_plugin=ds_plugins) |
| 120 |
from transformers.integrations.deepspeed import deepspeed_config |
| 121 |
|
| 122 |
# Note that these have `auto` values being set so we need to adjust |
| 123 |
assert accelerator.deepspeed_plugin is zero2 |
| 124 |
zero2.deepspeed_config["train_micro_batch_size_per_gpu"] = 1 |
| 125 |
zero2.deepspeed_config.pop("train_batch_size") |
| 126 |
assert deepspeed_config() == accelerator.deepspeed_plugin.hf_ds_config.config |
| 127 |
|
| 128 |
accelerator.state.select_deepspeed_plugin("zero3") |
| 129 |
assert accelerator.deepspeed_plugin is zero3 |
| 130 |
assert deepspeed_config() == accelerator.deepspeed_plugin.hf_ds_config.config |
| 131 |
|
| 132 |
def test_enable_disable_manually_set(self): |
| 133 |
ds_plugins = self.get_ds_plugins() |
| 134 |
ds_zero2, _ = ds_plugins.values() |
| 135 |
with self.assertRaises(ValueError): |
| 136 |
ds_zero2.select() |
| 137 |
accelerator = Accelerator(deepspeed_plugin=ds_plugins) |
| 138 |
accelerator.state.select_deepspeed_plugin("zero2") |
| 139 |
with self.assertRaises(NotImplementedError): |
| 140 |
ds_zero2.selected = False |
| 141 |
assert ds_zero2.selected |
| 142 |
|
| 143 |
def test_multiple_accelerators(self): |
| 144 |
ds_plugins = self.get_ds_plugins() |
| 145 |
ds_zero2, ds_zero3 = ds_plugins.values() |
| 146 |
_ = Accelerator( |
| 147 |
deepspeed_plugin=ds_zero2, |
| 148 |
) |
| 149 |
with self.assertRaises(NotImplementedError): |
| 150 |
_ = Accelerator(deepspeed_plugin=ds_zero3) |
| 151 |
|
| 152 |
def test_prepare_multiple_models_zero3_inference(self): |
| 153 |
with patch_environment(**self.dist_env): |
| 154 |
ds_plugins = self.get_ds_plugins(zero3_inference=True) |
| 155 |
accelerator = Accelerator(deepspeed_plugin=ds_plugins) |
| 156 |
# Using Zero-2 first |
| 157 |
model1 = self.model_init() |
| 158 |
optimizer = DummyOptim(model1.parameters()) |
| 159 |
scheduler = DummyScheduler(optimizer) |
| 160 |
|
| 161 |
dataset = RegressionDataset() |
| 162 |
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1) |
| 163 |
model1, optimizer, scheduler, dataloader = accelerator.prepare(model1, optimizer, scheduler, dataloader) |
| 164 |
accelerator.state.select_deepspeed_plugin("zero3") |
| 165 |
model2 = self.model_init() |
| 166 |
with self.assertLogs(level="WARNING") as captured: |
| 167 |
model2 = accelerator.prepare(model2) |
| 168 |
self.assertIn( |
| 169 |
"A wrapped DeepSpeed engine reference is currently tied for this `Accelerator()` instance.", |
| 170 |
captured.output[0], |
| 171 |
) |
| 172 |
|
| 173 |
assert accelerator.deepspeed_engine_wrapped.engine is model1 |
| 174 |
|
| 175 |
@run_first |
| 176 |
@require_huggingface_suite |
| 177 |
@require_multi_device |
| 178 |
@slow |
| 179 |
def test_train_multiple_models(self): |
| 180 |
self.test_file_path = self.test_scripts_folder / "test_ds_multiple_model.py" |
| 181 |
args = ["--num_processes=2", "--num_machines=1", str(self.test_file_path)] |
| 182 |
args = self.parser.parse_args(args) |
| 183 |
launch_command(args) |