| 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 single GPU training. |
| 19 |
""" |
| 20 |
|
| 21 |
import evaluate |
| 22 |
import msamp |
| 23 |
import torch |
| 24 |
from fp8_utils import evaluate_model, get_training_utilities |
| 25 |
|
| 26 |
from accelerate import Accelerator |
| 27 |
from accelerate.state import AcceleratorState |
| 28 |
from accelerate.utils import FP8RecipeKwargs, get_grad_scaler, set_seed |
| 29 |
|
| 30 |
|
| 31 |
MODEL_NAME = "bert-base-cased" |
| 32 |
METRIC = evaluate.load("glue", "mrpc") |
| 33 |
|
| 34 |
|
| 35 |
def train_baseline(opt_level="O2"): |
| 36 |
set_seed(42) |
| 37 |
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = get_training_utilities(MODEL_NAME) |
| 38 |
|
| 39 |
model, optimizer = msamp.initialize(model, optimizer, opt_level=opt_level) |
| 40 |
model.to("cuda") |
| 41 |
|
| 42 |
base_model_results = evaluate_model(model, eval_dataloader, METRIC) |
| 43 |
model.train() |
| 44 |
scaler = get_grad_scaler() |
| 45 |
|
| 46 |
for batch in train_dataloader: |
| 47 |
batch = batch.to("cuda") |
| 48 |
with torch.autocast(device_type="cuda", dtype=torch.bfloat16): |
| 49 |
outputs = model(**batch) |
| 50 |
loss = outputs.loss |
| 51 |
loss = scaler.scale(loss) |
| 52 |
loss.backward() |
| 53 |
optimizer.step() |
| 54 |
optimizer.zero_grad() |
| 55 |
lr_scheduler.step() |
| 56 |
|
| 57 |
trained_model_results = evaluate_model(model, eval_dataloader, METRIC) |
| 58 |
|
| 59 |
assert trained_model_results["accuracy"] > base_model_results["accuracy"], ( |
| 60 |
f"Accuracy should be higher for the trained model: {trained_model_results['accuracy']} > {base_model_results['accuracy']}" |
| 61 |
) |
| 62 |
assert trained_model_results["f1"] > base_model_results["f1"], ( |
| 63 |
f"F1 score should be higher for the trained model: {trained_model_results['f1']} > {base_model_results['f1']}" |
| 64 |
) |
| 65 |
|
| 66 |
return base_model_results, trained_model_results |
| 67 |
|
| 68 |
|
| 69 |
def train_integration(opt_level="O2"): |
| 70 |
kwargs_handlers = [FP8RecipeKwargs(backend="msamp", opt_level=opt_level)] |
| 71 |
AcceleratorState()._reset_state(True) |
| 72 |
accelerator = Accelerator(mixed_precision="fp8", kwargs_handlers=kwargs_handlers) |
| 73 |
set_seed(42) |
| 74 |
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = get_training_utilities( |
| 75 |
MODEL_NAME, accelerator=accelerator |
| 76 |
) |
| 77 |
|
| 78 |
model, optimizer, lr_scheduler = accelerator.prepare(model, optimizer, lr_scheduler) |
| 79 |
base_model_results = evaluate_model(model, eval_dataloader, METRIC) |
| 80 |
model.train() |
| 81 |
|
| 82 |
for batch in train_dataloader: |
| 83 |
outputs = model(**batch) |
| 84 |
loss = outputs.loss |
| 85 |
accelerator.backward(loss) |
| 86 |
optimizer.step() |
| 87 |
optimizer.zero_grad() |
| 88 |
lr_scheduler.step() |
| 89 |
|
| 90 |
trained_model_results = evaluate_model(model, eval_dataloader, METRIC) |
| 91 |
|
| 92 |
assert trained_model_results["accuracy"] > base_model_results["accuracy"], ( |
| 93 |
f"Accuracy should be higher for the trained model: {trained_model_results['accuracy']} > {base_model_results['accuracy']}" |
| 94 |
) |
| 95 |
assert trained_model_results["f1"] > base_model_results["f1"], ( |
| 96 |
f"F1 score should be higher for the trained model: {trained_model_results['f1']} > {base_model_results['f1']}" |
| 97 |
) |
| 98 |
|
| 99 |
return base_model_results, trained_model_results |
| 100 |
|
| 101 |
|
| 102 |
if __name__ == "__main__": |
| 103 |
for opt_level in ["O1", "O2"]: |
| 104 |
baseline_not_trained, baseline_trained = train_baseline(opt_level) |
| 105 |
accelerator_not_trained, accelerator_trained = train_integration(opt_level) |
| 106 |
|
| 107 |
assert baseline_not_trained["accuracy"] == accelerator_not_trained["accuracy"], ( |
| 108 |
f"Accuracy should be the same for the baseline and accelerator: {baseline_not_trained['accuracy']} == {accelerator_not_trained['accuracy']}" |
| 109 |
) |
| 110 |
assert baseline_not_trained["f1"] == accelerator_not_trained["f1"], ( |
| 111 |
f"F1 score should be the same for the baseline and accelerator: {baseline_not_trained['f1']} == {accelerator_not_trained['f1']}" |
| 112 |
) |
| 113 |
assert baseline_trained["accuracy"] == accelerator_trained["accuracy"], ( |
| 114 |
f"Accuracy should be the same for the baseline and accelerator: {baseline_trained['accuracy']} == {accelerator_trained['accuracy']}" |
| 115 |
) |
| 116 |
assert baseline_trained["f1"] == accelerator_trained["f1"], ( |
| 117 |
f"F1 score should be the same for the baseline and accelerator: {baseline_trained['f1']} == {accelerator_trained['f1']}" |
| 118 |
) |