-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
no_sync
context wrapper + clean up some more warnings for…
… DDP (#428)
- Loading branch information
Showing
10 changed files
with
245 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Copyright 2022 The HuggingFace Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from copy import deepcopy | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
from torch.utils.data import DataLoader | ||
|
||
from accelerate import Accelerator | ||
from accelerate.test_utils import RegressionDataset, RegressionModel | ||
from accelerate.utils import DistributedType, set_seed | ||
|
||
|
||
def step_model(model, input, target, accelerator): | ||
model.train() | ||
output = model(input) | ||
loss = F.mse_loss(output, target.to(output.device)) | ||
accelerator.backward(loss) | ||
|
||
|
||
def get_training_setup(accelerator): | ||
"Returns everything needed to perform basic training" | ||
set_seed(42) | ||
model = RegressionModel() | ||
model.to(accelerator.device) | ||
dset = RegressionDataset() | ||
dataloader = DataLoader(dset, batch_size=16) | ||
# Make a copy of `model` | ||
ddp_model, dataloader = accelerator.prepare(deepcopy(model), dataloader) | ||
# Use a single batch for all of the tests | ||
ddp_input, ddp_target = next(iter(dataloader)).values() | ||
return model, ddp_model, ddp_input, ddp_target | ||
|
||
|
||
def test_noop_sync(accelerator): | ||
# Test when on a single CPU or GPU that the context manager does nothing | ||
model, ddp_model, ddp_input, ddp_target = get_training_setup(accelerator) | ||
for iteration in range(3): | ||
# Gather the distributed inputs and targs for the base model | ||
input, target = accelerator.gather((ddp_input, ddp_target)) | ||
input, target = input.to(accelerator.device), target.to(accelerator.device) | ||
# Perform our initial ground truth step in non "DDP" | ||
step_model(model, input, target, accelerator) | ||
# Do "gradient accumulation" (noop) | ||
if iteration % 2 == 0: | ||
# Accumulate grads locally | ||
with accelerator.no_sync(ddp_model): | ||
step_model(ddp_model, ddp_input, ddp_target, accelerator) | ||
else: | ||
# Sync grads | ||
step_model(ddp_model, ddp_input, ddp_target, accelerator) | ||
|
||
# Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync | ||
for param, ddp_param in zip(model.parameters(), ddp_model.parameters()): | ||
if not param.requires_grad: | ||
continue | ||
assert torch.allclose( | ||
param.grad, ddp_param.grad | ||
), f"Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" | ||
|
||
# Shuffle ddp_input on each iteration | ||
torch.manual_seed(1337 + iteration) | ||
ddp_input = ddp_input[torch.randperm(16)] | ||
|
||
|
||
def test_distributed_sync(accelerator): | ||
# Test on distributed setup that context manager behaves properly | ||
model, ddp_model, ddp_input, ddp_target = get_training_setup(accelerator) | ||
for iteration in range(3): | ||
# Gather the distributed inputs and targs for the base model | ||
input, target = accelerator.gather((ddp_input, ddp_target)) | ||
input, target = input.to(accelerator.device), target.to(accelerator.device) | ||
# Perform our initial ground truth step in non "DDP" | ||
step_model(model, input, target, accelerator) | ||
# Do "gradient accumulation" (noop) | ||
if iteration % 2 == 0: | ||
# Accumulate grads locally | ||
with accelerator.no_sync(ddp_model): | ||
step_model(ddp_model, ddp_input, ddp_target, accelerator) | ||
else: | ||
# Sync grads | ||
step_model(ddp_model, ddp_input, ddp_target, accelerator) | ||
|
||
# DDP model and model should only be in sync when not (iteration % 2 == 0) | ||
for param, ddp_param in zip(model.parameters(), ddp_model.parameters()): | ||
if not param.requires_grad: | ||
continue | ||
if iteration % 2 == 0: | ||
# Grads should not be in sync | ||
assert ( | ||
torch.allclose(param.grad, ddp_param.grad) is False | ||
), f"Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})" | ||
else: | ||
# Grads should be in sync | ||
assert ( | ||
torch.allclose(param.grad, ddp_param.grad) is True | ||
), f"Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" | ||
|
||
# Shuffle ddp_input on each iteration | ||
torch.manual_seed(1337 + iteration) | ||
ddp_input = ddp_input[torch.randperm(16)] | ||
|
||
|
||
def main(): | ||
accelerator = Accelerator() | ||
state = accelerator.state | ||
if state.distributed_type == DistributedType.NO: | ||
if state.local_process_index == 0: | ||
print("**NOOP `no_sync` gradient accumulation**") | ||
test_noop_sync(accelerator) | ||
if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU): | ||
if state.local_process_index == 0: | ||
print("**Distributed `no_sync` gradient accumulation**") | ||
test_distributed_sync(accelerator) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2021 The HuggingFace Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import inspect | ||
import os | ||
import unittest | ||
|
||
import torch | ||
|
||
import accelerate | ||
from accelerate import debug_launcher | ||
from accelerate.test_utils import ( | ||
execute_subprocess_async, | ||
require_cpu, | ||
require_multi_gpu, | ||
require_single_gpu, | ||
test_sync, | ||
) | ||
from accelerate.utils import get_launch_prefix, patch_environment | ||
|
||
|
||
class SyncScheduler(unittest.TestCase): | ||
def setUp(self): | ||
mod_file = inspect.getfile(accelerate.test_utils) | ||
self.test_file_path = os.path.sep.join(mod_file.split(os.path.sep)[:-1] + ["scripts", "test_sync.py"]) | ||
|
||
@require_cpu | ||
def test_gradient_sync_single_cpu_noop(self): | ||
debug_launcher(test_sync.main) | ||
debug_launcher(test_sync.main, num_processes=1) | ||
|
||
@require_cpu | ||
def test_gradient_sync_multi_cpu(self): | ||
debug_launcher(test_sync.main) | ||
|
||
@require_single_gpu | ||
def test_gradient_sync_single_gpu(self): | ||
debug_launcher(test_sync.main, num_processes=1) | ||
|
||
@require_multi_gpu | ||
def test_gradient_sync_multi_gpu(self): | ||
print(f"Found {torch.cuda.device_count()} devices.") | ||
cmd = get_launch_prefix() + [f"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path] | ||
with patch_environment(omp_num_threads=1): | ||
execute_subprocess_async(cmd, env=os.environ.copy()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters