| 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 |
from accelerate import debug_launcher |
| 16 |
from accelerate.test_utils import ( |
| 17 |
DEFAULT_LAUNCH_COMMAND, |
| 18 |
device_count, |
| 19 |
execute_subprocess_async, |
| 20 |
path_in_accelerate_package, |
| 21 |
require_cpu, |
| 22 |
require_multi_device, |
| 23 |
require_non_cpu, |
| 24 |
run_first, |
| 25 |
test_sync, |
| 26 |
) |
| 27 |
from accelerate.test_utils.testing import AccelerateTestCase |
| 28 |
from accelerate.utils import patch_environment |
| 29 |
|
| 30 |
|
| 31 |
class SyncScheduler(AccelerateTestCase): |
| 32 |
test_file_path = path_in_accelerate_package("test_utils", "scripts", "test_sync.py") |
| 33 |
|
| 34 |
@require_cpu |
| 35 |
def test_gradient_sync_cpu_noop(self): |
| 36 |
debug_launcher(test_sync.main, num_processes=1) |
| 37 |
|
| 38 |
@require_cpu |
| 39 |
def test_gradient_sync_cpu_multi(self): |
| 40 |
debug_launcher(test_sync.main) |
| 41 |
|
| 42 |
@require_non_cpu |
| 43 |
def test_gradient_sync_gpu(self): |
| 44 |
test_sync.main() |
| 45 |
|
| 46 |
@run_first |
| 47 |
@require_multi_device |
| 48 |
def test_gradient_sync_gpu_multi(self): |
| 49 |
print(f"Found {device_count} devices.") |
| 50 |
cmd = DEFAULT_LAUNCH_COMMAND + [self.test_file_path] |
| 51 |
with patch_environment(omp_num_threads=1): |
| 52 |
execute_subprocess_async(cmd) |