| 1 |
# Copyright 2021 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 unittest |
| 16 |
from functools import partial |
| 17 |
|
| 18 |
import torch |
| 19 |
|
| 20 |
from accelerate import Accelerator, debug_launcher |
| 21 |
from accelerate.state import AcceleratorState, GradientState |
| 22 |
from accelerate.test_utils import require_cpu, require_huggingface_suite |
| 23 |
from accelerate.utils import GradientAccumulationPlugin |
| 24 |
|
| 25 |
|
| 26 |
def one_cycle_test(num_processes=2, step_scheduler_with_optimizer=True, split_batches=False): |
| 27 |
accelerator = Accelerator(step_scheduler_with_optimizer=step_scheduler_with_optimizer, split_batches=split_batches) |
| 28 |
model = torch.nn.Linear(2, 4) |
| 29 |
optimizer = torch.optim.AdamW(model.parameters(), lr=1.0) |
| 30 |
scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr=0.01, steps_per_epoch=2, epochs=1) |
| 31 |
model, optimizer, scheduler = accelerator.prepare(model, optimizer, scheduler) |
| 32 |
|
| 33 |
# Optimizer has stepped |
| 34 |
scheduler.step() |
| 35 |
if step_scheduler_with_optimizer or (num_processes == 1): |
| 36 |
assert scheduler.scheduler.last_epoch == num_processes, ( |
| 37 |
f"Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})" |
| 38 |
) |
| 39 |
else: |
| 40 |
assert scheduler.scheduler.last_epoch != num_processes, ( |
| 41 |
f"Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})" |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
def lambda_test(num_processes=2, step_scheduler_with_optimizer=True, split_batches=False): |
| 46 |
accelerator = Accelerator(step_scheduler_with_optimizer=step_scheduler_with_optimizer, split_batches=split_batches) |
| 47 |
model = torch.nn.Linear(2, 4) |
| 48 |
optimizer = torch.optim.AdamW(model.parameters(), lr=1.0) |
| 49 |
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda n: 1 - n / 10) |
| 50 |
model, optimizer, scheduler = accelerator.prepare(model, optimizer, scheduler) |
| 51 |
|
| 52 |
# Optimizer has stepped |
| 53 |
optimizer._is_overflow = False |
| 54 |
scheduler.step() |
| 55 |
expected_lr = 1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10 |
| 56 |
assert scheduler.get_last_lr()[0] == expected_lr, ( |
| 57 |
f"Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}" |
| 58 |
) |
| 59 |
|
| 60 |
# Optimizer has not stepped |
| 61 |
optimizer._is_overflow = True |
| 62 |
scheduler.step() |
| 63 |
if not step_scheduler_with_optimizer: |
| 64 |
expected_lr = 1 - 2 / 10 |
| 65 |
assert scheduler.get_last_lr()[0] == expected_lr, ( |
| 66 |
f"Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}" |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
def accumulation_test(num_processes: int = 2): |
| 71 |
""" |
| 72 |
With this test, an observed batch size of 64 should result in negligible |
| 73 |
differences in the scheduler after going through the correct number of steps. |
| 74 |
|
| 75 |
Uses single, two, and four steps to test. |
| 76 |
""" |
| 77 |
from transformers import get_linear_schedule_with_warmup |
| 78 |
|
| 79 |
steps = [1, 2, 4] |
| 80 |
for num_steps in steps: |
| 81 |
plugin = GradientAccumulationPlugin(num_steps=num_steps, adjust_scheduler=num_steps > 1) |
| 82 |
accelerator = Accelerator(gradient_accumulation_plugin=plugin) |
| 83 |
model = torch.nn.Linear(2, 4) |
| 84 |
optimizer = torch.optim.AdamW(model.parameters(), lr=10.0) |
| 85 |
scheduler = get_linear_schedule_with_warmup(optimizer=optimizer, num_warmup_steps=0, num_training_steps=20) |
| 86 |
|
| 87 |
model, optimizer, scheduler = accelerator.prepare(model, optimizer, scheduler) |
| 88 |
|
| 89 |
for i in range(10 * num_steps): |
| 90 |
with accelerator.accumulate(model): |
| 91 |
optimizer.step() |
| 92 |
scheduler.step() |
| 93 |
|
| 94 |
if i == (10 * num_steps - 2): |
| 95 |
assert scheduler.get_last_lr()[0] != 0, ( |
| 96 |
f"Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}" |
| 97 |
) |
| 98 |
assert scheduler.get_last_lr()[0] == 0, ( |
| 99 |
f"Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}" |
| 100 |
) |
| 101 |
GradientState._reset_state() |
| 102 |
|
| 103 |
|
| 104 |
@require_cpu |
| 105 |
class SchedulerTester(unittest.TestCase): |
| 106 |
def test_lambda_scheduler_steps_with_optimizer_single_process(self): |
| 107 |
debug_launcher(partial(lambda_test, num_processes=1), num_processes=1) |
| 108 |
debug_launcher(partial(lambda_test, num_processes=1, split_batches=True), num_processes=1) |
| 109 |
|
| 110 |
def test_one_cycle_scheduler_steps_with_optimizer_single_process(self): |
| 111 |
debug_launcher(partial(one_cycle_test, num_processes=1), num_processes=1) |
| 112 |
debug_launcher(partial(one_cycle_test, num_processes=1, split_batches=True), num_processes=1) |
| 113 |
|
| 114 |
def test_lambda_scheduler_not_step_with_optimizer_single_process(self): |
| 115 |
debug_launcher(partial(lambda_test, num_processes=1, step_scheduler_with_optimizer=False), num_processes=1) |
| 116 |
|
| 117 |
def test_one_cycle_scheduler_not_step_with_optimizer_single_process(self): |
| 118 |
debug_launcher(partial(one_cycle_test, num_processes=1, step_scheduler_with_optimizer=False), num_processes=1) |
| 119 |
|
| 120 |
def test_lambda_scheduler_steps_with_optimizer_multiprocess(self): |
| 121 |
AcceleratorState._reset_state(True) |
| 122 |
debug_launcher(lambda_test) |
| 123 |
debug_launcher(partial(lambda_test, num_processes=1, split_batches=True), num_processes=1) |
| 124 |
|
| 125 |
def test_one_cycle_scheduler_steps_with_optimizer_multiprocess(self): |
| 126 |
AcceleratorState._reset_state(True) |
| 127 |
debug_launcher(one_cycle_test) |
| 128 |
debug_launcher(partial(one_cycle_test, num_processes=1, split_batches=True), num_processes=1) |
| 129 |
|
| 130 |
def test_lambda_scheduler_not_step_with_optimizer_multiprocess(self): |
| 131 |
AcceleratorState._reset_state(True) |
| 132 |
debug_launcher(partial(lambda_test, step_scheduler_with_optimizer=False)) |
| 133 |
|
| 134 |
def test_one_cycle_scheduler_not_step_with_optimizer_multiprocess(self): |
| 135 |
AcceleratorState._reset_state(True) |
| 136 |
debug_launcher(partial(one_cycle_test, step_scheduler_with_optimizer=False)) |
| 137 |
|
| 138 |
@require_huggingface_suite |
| 139 |
def test_accumulation(self): |
| 140 |
AcceleratorState._reset_state(True) |
| 141 |
debug_launcher(partial(accumulation_test, num_processes=1)) |
| 142 |
debug_launcher(accumulation_test) |