| 1 |
# Copyright 2024 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 |
import subprocess |
| 15 |
import sys |
| 16 |
|
| 17 |
from accelerate.test_utils import require_transformer_engine |
| 18 |
from accelerate.test_utils.testing import TempDirTestCase, require_import_timer |
| 19 |
from accelerate.utils import is_import_timer_available |
| 20 |
|
| 21 |
|
| 22 |
if is_import_timer_available(): |
| 23 |
from import_timer import calculate_total_time, read_import_profile |
| 24 |
from import_timer.core import get_paths_above_threshold, sort_nodes_by_total_time |
| 25 |
|
| 26 |
|
| 27 |
def convert_list_to_string(data): |
| 28 |
end_result = "" |
| 29 |
arrow_right = "->" |
| 30 |
for path in data: |
| 31 |
end_result += f"{arrow_right.join(path[0])} {path[1]:.3f}s\n" |
| 32 |
return end_result |
| 33 |
|
| 34 |
|
| 35 |
def run_import_time(command: str): |
| 36 |
output = subprocess.run([sys.executable, "-X", "importtime", "-c", command], capture_output=True, text=True) |
| 37 |
return output.stderr |
| 38 |
|
| 39 |
|
| 40 |
@require_import_timer |
| 41 |
class ImportSpeedTester(TempDirTestCase): |
| 42 |
""" |
| 43 |
Test suite which checks if imports have seen slowdowns |
| 44 |
based on a particular baseline. |
| 45 |
|
| 46 |
If the error messages are not clear enough to get a |
| 47 |
full view of what is slowing things down (or to |
| 48 |
figure out how deep the initial depth should be), |
| 49 |
please view the profile with the `tuna` framework: |
| 50 |
`tuna import.log`. |
| 51 |
""" |
| 52 |
|
| 53 |
clear_on_setup = False |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def setUpClass(cls): |
| 57 |
super().setUpClass() |
| 58 |
output = run_import_time("import torch") |
| 59 |
data = read_import_profile(output) |
| 60 |
total_time = calculate_total_time(data) |
| 61 |
cls.pytorch_time = total_time |
| 62 |
|
| 63 |
def test_base_import(self): |
| 64 |
output = run_import_time("import accelerate") |
| 65 |
data = read_import_profile(output) |
| 66 |
total_time = calculate_total_time(data) |
| 67 |
pct_more = (total_time - self.pytorch_time) / self.pytorch_time * 100 |
| 68 |
# Base import should never be more than 20% slower than raw torch import |
| 69 |
err_msg = f"Base import is more than 20% slower than raw torch import ({pct_more:.2f}%), please check the attached `tuna` profile:\n" |
| 70 |
sorted_data = sort_nodes_by_total_time(data) |
| 71 |
paths_above_threshold = get_paths_above_threshold(sorted_data, 0.05, max_depth=7) |
| 72 |
err_msg += f"\n{convert_list_to_string(paths_above_threshold)}" |
| 73 |
self.assertLess(pct_more, 20, err_msg) |
| 74 |
|
| 75 |
def test_cli_import(self): |
| 76 |
output = run_import_time("from accelerate.commands.launch import launch_command_parser") |
| 77 |
data = read_import_profile(output) |
| 78 |
total_time = calculate_total_time(data) |
| 79 |
pct_more = (total_time - self.pytorch_time) / self.pytorch_time * 100 |
| 80 |
# Base import should never be more than 20% slower than raw torch import |
| 81 |
err_msg = f"Base import is more than 20% slower than raw torch import ({pct_more:.2f}%), please check the attached `tuna` profile:\n" |
| 82 |
sorted_data = sort_nodes_by_total_time(data) |
| 83 |
paths_above_threshold = get_paths_above_threshold(sorted_data, 0.05, max_depth=7) |
| 84 |
err_msg += f"\n{convert_list_to_string(paths_above_threshold)}" |
| 85 |
self.assertLess(pct_more, 20, err_msg) |
| 86 |
|
| 87 |
|
| 88 |
@require_transformer_engine |
| 89 |
class LazyImportTester(TempDirTestCase): |
| 90 |
""" |
| 91 |
Test suite which checks if specific packages are lazy-loaded. |
| 92 |
|
| 93 |
Eager-import will trigger circular import in some case, |
| 94 |
e.g. in huggingface/accelerate#3056. |
| 95 |
""" |
| 96 |
|
| 97 |
def test_te_import(self): |
| 98 |
output = run_import_time("import accelerate, accelerate.utils.transformer_engine") |
| 99 |
|
| 100 |
self.assertFalse(" transformer_engine" in output, "`transformer_engine` should not be imported on import") |