| 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 |
|
| 17 |
import numpy as np |
| 18 |
from packaging import version |
| 19 |
|
| 20 |
from accelerate import debug_launcher |
| 21 |
from accelerate.test_utils import ( |
| 22 |
DEFAULT_LAUNCH_COMMAND, |
| 23 |
device_count, |
| 24 |
execute_subprocess_async, |
| 25 |
path_in_accelerate_package, |
| 26 |
require_cpu, |
| 27 |
require_huggingface_suite, |
| 28 |
require_multi_device, |
| 29 |
require_single_device, |
| 30 |
run_first, |
| 31 |
) |
| 32 |
from accelerate.utils import patch_environment |
| 33 |
|
| 34 |
|
| 35 |
@require_huggingface_suite |
| 36 |
@unittest.skipIf(version.parse(np.__version__) >= version.parse("2.0"), "Test requires numpy version < 2.0") |
| 37 |
class MetricTester(unittest.TestCase): |
| 38 |
def setUp(self): |
| 39 |
self.test_file_path = path_in_accelerate_package("test_utils", "scripts", "external_deps", "test_metrics.py") |
| 40 |
|
| 41 |
from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 |
| 42 |
|
| 43 |
self.test_metrics = test_metrics |
| 44 |
|
| 45 |
@require_cpu |
| 46 |
def test_metric_cpu_noop(self): |
| 47 |
debug_launcher(self.test_metrics.main, num_processes=1) |
| 48 |
|
| 49 |
@require_cpu |
| 50 |
def test_metric_cpu_multi(self): |
| 51 |
debug_launcher(self.test_metrics.main) |
| 52 |
|
| 53 |
@require_single_device |
| 54 |
def test_metric_accelerator(self): |
| 55 |
self.test_metrics.main() |
| 56 |
|
| 57 |
@run_first |
| 58 |
@require_multi_device |
| 59 |
def test_metric_accelerator_multi(self): |
| 60 |
print(f"Found {device_count} devices.") |
| 61 |
cmd = DEFAULT_LAUNCH_COMMAND + [self.test_file_path] |
| 62 |
with patch_environment(omp_num_threads=1, ACCELERATE_LOG_LEVEL="INFO"): |
| 63 |
execute_subprocess_async(cmd) |