| 1 |
# Copyright 2024 The HuggingFace Inc. 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 |
""" |
| 16 |
This script tests to ensure that `accelerate` performs at the same level as raw `MS-AMP`. |
| 17 |
|
| 18 |
This particular script verifies this for DeepSpeed training. |
| 19 |
|
| 20 |
NOTE: MS-AMP does *not* support ZeRO-3. |
| 21 |
""" |
| 22 |
|
| 23 |
# import msamp.deepspeed as msamp_deepspeed |
| 24 |
import evaluate |
| 25 |
import torch |
| 26 |
from fp8_utils import evaluate_model, get_training_utilities |
| 27 |
from msamp import deepspeed as msamp_deepspeed |
| 28 |
|
| 29 |
from accelerate import Accelerator, DeepSpeedPlugin |
| 30 |
from accelerate.state import AcceleratorState |
| 31 |
from accelerate.utils import set_seed |
| 32 |
|
| 33 |
|
| 34 |
MODEL_NAME = "bert-base-cased" |
| 35 |
METRIC = evaluate.load("glue", "mrpc") |
| 36 |
|
| 37 |
|
| 38 |
def train_baseline(zero_stage: int = 1, opt_level: str = "O1"): |
| 39 |
set_seed(42) |
| 40 |
accelerator = Accelerator() |
| 41 |
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = get_training_utilities( |
| 42 |
MODEL_NAME, accelerator=accelerator |
| 43 |
) |
| 44 |
|
| 45 |
import numpy as np |
| 46 |
|
| 47 |
config = { |
| 48 |
"train_batch_size": 32, |
| 49 |
"train_micro_batch_size_per_gpu": 16, |
| 50 |
"gradient_accumulation_steps": 1, |
| 51 |
"zero_optimization": { |
| 52 |
"stage": zero_stage, |
| 53 |
"offload_optimizer": {"device": "none", "nvme_path": None}, |
| 54 |
"offload_param": {"device": "none", "nvme_path": None}, |
| 55 |
}, |
| 56 |
"gradient_clipping": 1.0, |
| 57 |
"steps_per_print": np.inf, |
| 58 |
"bf16": {"enabled": True}, |
| 59 |
"fp16": {"enabled": False}, |
| 60 |
"zero_allow_untested_optimizer": True, |
| 61 |
"msamp": { |
| 62 |
"enabled": True, |
| 63 |
"opt_level": opt_level, |
| 64 |
}, |
| 65 |
} |
| 66 |
( |
| 67 |
model, |
| 68 |
optimizer, |
| 69 |
_, |
| 70 |
_, |
| 71 |
) = msamp_deepspeed.initialize( |
| 72 |
model=model, |
| 73 |
optimizer=optimizer, |
| 74 |
config_params=config, |
| 75 |
) |
| 76 |
|
| 77 |
base_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) |
| 78 |
model.train() |
| 79 |
|
| 80 |
for _ in range(2): |
| 81 |
for batch in train_dataloader: |
| 82 |
outputs = model(**batch) |
| 83 |
loss = outputs.loss |
| 84 |
model.backward(loss) |
| 85 |
model.step() |
| 86 |
for _ in range(accelerator.num_processes): |
| 87 |
lr_scheduler.step() |
| 88 |
|
| 89 |
trained_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) |
| 90 |
model.destroy() |
| 91 |
torch.cuda.empty_cache() |
| 92 |
AcceleratorState()._reset_state(True) |
| 93 |
assert trained_model_results["accuracy"] > base_model_results["accuracy"], ( |
| 94 |
f"Accuracy should be higher for the trained model: {trained_model_results['accuracy']} > {base_model_results['accuracy']}" |
| 95 |
) |
| 96 |
assert trained_model_results["f1"] > base_model_results["f1"], ( |
| 97 |
f"F1 score should be higher for the trained model: {trained_model_results['f1']} > {base_model_results['f1']}" |
| 98 |
) |
| 99 |
|
| 100 |
return base_model_results, trained_model_results |
| 101 |
|
| 102 |
|
| 103 |
def train_integration(zero_stage: int = 1, opt_level: str = "O1"): |
| 104 |
set_seed(42) |
| 105 |
deepspeed_plugin = DeepSpeedPlugin( |
| 106 |
zero_stage=zero_stage, |
| 107 |
enable_msamp=True, |
| 108 |
msamp_opt_level=opt_level, |
| 109 |
) |
| 110 |
accelerator = Accelerator(mixed_precision="fp8", deepspeed_plugin=deepspeed_plugin) |
| 111 |
accelerator.state.deepspeed_plugin.deepspeed_config["train_micro_batch_size_per_gpu"] = 16 |
| 112 |
|
| 113 |
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = get_training_utilities( |
| 114 |
MODEL_NAME, accelerator=accelerator |
| 115 |
) |
| 116 |
|
| 117 |
model, optimizer, lr_scheduler = accelerator.prepare(model, optimizer, lr_scheduler) |
| 118 |
base_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) |
| 119 |
model.train() |
| 120 |
for _ in range(2): |
| 121 |
for batch in train_dataloader: |
| 122 |
outputs = model(**batch) |
| 123 |
loss = outputs.loss |
| 124 |
accelerator.backward(loss) |
| 125 |
optimizer.step() |
| 126 |
lr_scheduler.step() |
| 127 |
optimizer.zero_grad() |
| 128 |
|
| 129 |
trained_model_results = evaluate_model(model, eval_dataloader, METRIC, accelerator=accelerator) |
| 130 |
model.destroy() |
| 131 |
torch.cuda.empty_cache() |
| 132 |
assert trained_model_results["accuracy"] > base_model_results["accuracy"], ( |
| 133 |
f"Accuracy should be higher for the trained model: {trained_model_results['accuracy']} > {base_model_results['accuracy']}" |
| 134 |
) |
| 135 |
assert trained_model_results["f1"] > base_model_results["f1"], ( |
| 136 |
f"F1 score should be higher for the trained model: {trained_model_results['f1']} > {base_model_results['f1']}" |
| 137 |
) |
| 138 |
|
| 139 |
AcceleratorState()._reset_state(True) |
| 140 |
return base_model_results, trained_model_results |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__": |
| 144 |
for zero_stage in [1, 2]: |
| 145 |
for opt_level in ["O1", "O2", "O3"]: |
| 146 |
baseline_not_trained, baseline_trained = train_baseline(zero_stage, opt_level) |
| 147 |
accelerator_not_trained, accelerator_trained = train_integration(zero_stage, opt_level) |
| 148 |
assert baseline_not_trained["accuracy"] == accelerator_not_trained["accuracy"], ( |
| 149 |
f"ZERO stage {zero_stage}, opt_level={opt_level}:\nAccuracy should be the same for the baseline and accelerator: {baseline_not_trained['accuracy']} == {accelerator_not_trained['accuracy']}" |
| 150 |
) |
| 151 |
assert baseline_not_trained["f1"] == accelerator_not_trained["f1"], ( |
| 152 |
f"ZERO stage {zero_stage}, opt_level={opt_level}:\nF1 score should be the same for the baseline and accelerator: {baseline_not_trained['f1']} == {accelerator_not_trained['f1']}" |
| 153 |
) |
| 154 |
assert baseline_trained["accuracy"] == accelerator_trained["accuracy"], ( |
| 155 |
f"ZERO stage {zero_stage}, opt_level={opt_level}:\nAccuracy should be the same for the baseline and accelerator: {baseline_trained['accuracy']} == {accelerator_trained['accuracy']}" |
| 156 |
) |
| 157 |
assert baseline_trained["f1"] == accelerator_trained["f1"], ( |
| 158 |
f"ZERO stage {zero_stage}, opt_level={opt_level}:\nF1 score should be the same for the baseline and accelerator: {baseline_trained['f1']} == {accelerator_trained['f1']}" |
| 159 |
) |
| 160 |
|
| 161 |
torch.distributed.destroy_process_group() |