2023-03-31 03:14:38 +03:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
# DeepSpeed Team
|
2023-02-27 22:20:41 +03:00
|
|
|
|
2021-04-30 20:04:20 +03:00
|
|
|
# tests directory-specific settings - this file is run automatically by pytest before any tests are run
|
|
|
|
|
|
|
|
import sys
|
2022-06-21 18:42:55 +03:00
|
|
|
import pytest
|
2022-10-19 01:17:01 +03:00
|
|
|
import os
|
2021-04-30 20:04:20 +03:00
|
|
|
from os.path import abspath, dirname, join
|
2022-06-21 18:42:55 +03:00
|
|
|
import torch
|
|
|
|
import warnings
|
2021-04-30 20:04:20 +03:00
|
|
|
|
2022-10-19 01:17:01 +03:00
|
|
|
# Set this environment variable for the T5 inference unittest(s) (e.g. google/t5-v1_1-small)
|
|
|
|
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
|
|
|
|
|
2021-04-30 20:04:20 +03:00
|
|
|
# allow having multiple repository checkouts and not needing to remember to rerun
|
|
|
|
# 'pip install -e .[dev]' when switching between checkouts and running tests.
|
|
|
|
git_repo_path = abspath(join(dirname(dirname(__file__)), "src"))
|
|
|
|
sys.path.insert(1, git_repo_path)
|
2022-06-21 18:42:55 +03:00
|
|
|
|
|
|
|
|
2023-03-21 23:45:25 +03:00
|
|
|
def pytest_configure(config):
|
|
|
|
config.option.color = "yes"
|
|
|
|
config.option.durations = 0
|
|
|
|
config.option.durations_min = 1
|
|
|
|
config.option.verbose = True
|
|
|
|
|
|
|
|
|
2022-06-21 18:42:55 +03:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--torch_ver", default=None, type=str)
|
|
|
|
parser.addoption("--cuda_ver", default=None, type=str)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_version(expected, found):
|
|
|
|
version_depth = expected.count('.') + 1
|
|
|
|
found = '.'.join(found.split('.')[:version_depth])
|
|
|
|
return found == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def check_environment(pytestconfig):
|
|
|
|
expected_torch_version = pytestconfig.getoption("torch_ver")
|
|
|
|
expected_cuda_version = pytestconfig.getoption("cuda_ver")
|
|
|
|
if expected_torch_version is None:
|
|
|
|
warnings.warn(
|
2023-03-27 14:55:19 +03:00
|
|
|
"Running test without verifying torch version, please provide an expected torch version with --torch_ver")
|
2022-06-21 18:42:55 +03:00
|
|
|
elif not validate_version(expected_torch_version, torch.__version__):
|
|
|
|
pytest.exit(
|
|
|
|
f"expected torch version {expected_torch_version} did not match found torch version {torch.__version__}",
|
|
|
|
returncode=2)
|
|
|
|
if expected_cuda_version is None:
|
|
|
|
warnings.warn(
|
2023-03-27 14:55:19 +03:00
|
|
|
"Running test without verifying cuda version, please provide an expected cuda version with --cuda_ver")
|
2022-06-21 18:42:55 +03:00
|
|
|
elif not validate_version(expected_cuda_version, torch.version.cuda):
|
|
|
|
pytest.exit(
|
|
|
|
f"expected cuda version {expected_cuda_version} did not match found cuda version {torch.version.cuda}",
|
|
|
|
returncode=2)
|
2022-08-02 02:51:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Override of pytest "runtest" for DistributedTest class
|
|
|
|
# This hook is run before the default pytest_runtest_call
|
|
|
|
@pytest.hookimpl(tryfirst=True)
|
|
|
|
def pytest_runtest_call(item):
|
|
|
|
# We want to use our own launching function for distributed tests
|
|
|
|
if getattr(item.cls, "is_dist_test", False):
|
|
|
|
dist_test_class = item.cls()
|
2022-10-04 19:01:08 +03:00
|
|
|
dist_test_class(item._request)
|
2022-08-02 02:51:09 +03:00
|
|
|
item.runtest = lambda: True # Dummy function so test is not run twice
|
2022-10-04 19:01:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.hookimpl(tryfirst=True)
|
|
|
|
def pytest_fixture_setup(fixturedef, request):
|
|
|
|
if getattr(fixturedef.func, "is_dist_fixture", False):
|
|
|
|
#for val in dir(request):
|
|
|
|
# print(val.upper(), getattr(request, val), "\n")
|
|
|
|
dist_fixture_class = fixturedef.func()
|
|
|
|
dist_fixture_class(request)
|