| 1 |
# Copyright 2022 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 |
|
| 16 |
import os |
| 17 |
import unittest |
| 18 |
|
| 19 |
from accelerate.test_utils.testing import ( |
| 20 |
TempDirTestCase, |
| 21 |
device_count, |
| 22 |
execute_subprocess_async, |
| 23 |
get_launch_command, |
| 24 |
path_in_accelerate_package, |
| 25 |
require_multi_device, |
| 26 |
require_non_torch_xla, |
| 27 |
require_tp, |
| 28 |
require_transformers, |
| 29 |
run_first, |
| 30 |
slow, |
| 31 |
) |
| 32 |
from accelerate.utils import patch_environment |
| 33 |
|
| 34 |
|
| 35 |
@require_non_torch_xla |
| 36 |
@require_multi_device |
| 37 |
@require_transformers |
| 38 |
@require_tp |
| 39 |
@run_first |
| 40 |
@slow |
| 41 |
class TPIntegrationTest(TempDirTestCase): |
| 42 |
test_scripts_folder = path_in_accelerate_package("test_utils", "scripts", "external_deps") |
| 43 |
|
| 44 |
def setUp(self): |
| 45 |
super().setUp() |
| 46 |
self.test_tp_size = 2 |
| 47 |
self.model_name_or_path = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" |
| 48 |
self.batch_size = 1 |
| 49 |
from accelerate.utils import set_seed |
| 50 |
|
| 51 |
set_seed(42) |
| 52 |
|
| 53 |
def test_working_of_tp(self): |
| 54 |
self.test_file_path = self.test_scripts_folder / "test_performance.py" |
| 55 |
cmd = get_launch_command(num_processes=self.test_tp_size, num_machines=1, machine_rank=0) |
| 56 |
cmd.extend( |
| 57 |
[ |
| 58 |
self.test_file_path, |
| 59 |
f"--output_dir={self.tmpdir}", |
| 60 |
f"--model_name_or_path={self.model_name_or_path}", |
| 61 |
"--add_pad_token=true", |
| 62 |
"--tp_plan=auto", |
| 63 |
f"--tp_size={self.test_tp_size}", |
| 64 |
] |
| 65 |
) |
| 66 |
with patch_environment(omp_num_threads=1): |
| 67 |
execute_subprocess_async(cmd) |
| 68 |
|
| 69 |
@unittest.skipUnless(device_count >= 4, "TP (2) x FSDP dp_shard (2) requires at least 4 hardware accelerators") |
| 70 |
def test_working_of_tp_and_fsdp(self): |
| 71 |
current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 72 |
self.test_file_path = os.path.join(current_dir, "fsdp2_tp_preparation.py") |
| 73 |
self.test_config_path = os.path.join(current_dir, "fsdp2_tp_preparation_config.yaml") |
| 74 |
cmd = get_launch_command(num_processes=device_count, num_machines=1, machine_rank=0) |
| 75 |
cmd.extend( |
| 76 |
[ |
| 77 |
f"--config_file={self.test_config_path}", |
| 78 |
self.test_file_path, |
| 79 |
] |
| 80 |
) |
| 81 |
with patch_environment(omp_num_threads=4): |
| 82 |
execute_subprocess_async(cmd) |