-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathconftest.py
87 lines (67 loc) · 2.51 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""conftest.py.
Pytest fixtures.
"""
import sys
import pytest
from qibo.backends import construct_backend
# backends to be tested
BACKENDS = [
"numpy",
"tensorflow",
"pytorch",
"qibojit-numba",
"qibojit-cupy",
"qibojit-cuquantum",
]
# multigpu configurations to be tested (only with qibojit-cupy)
ACCELERATORS = [
{"/GPU:0": 1, "/GPU:1": 1},
{"/GPU:0": 2, "/GPU:1": 2},
{"/GPU:0": 1, "/GPU:1": 1, "/GPU:2": 1, "/GPU:3": 1},
]
def get_backend(backend_name):
if "-" in backend_name:
name, platform = backend_name.split("-")
else:
name, platform = backend_name, None
return construct_backend(name, platform=platform)
# ignore backends that are not available in the current testing environment
AVAILABLE_BACKENDS = []
MULTIGPU_BACKENDS = []
for backend_name in BACKENDS:
try:
_backend = get_backend(backend_name)
AVAILABLE_BACKENDS.append(backend_name)
if _backend.supports_multigpu: # pragma: no cover
MULTIGPU_BACKENDS.append(backend_name)
except (ModuleNotFoundError, ImportError):
pass
def pytest_runtest_setup(item):
ALL = {"darwin", "linux"}
supported_platforms = ALL.intersection(mark.name for mark in item.iter_markers())
plat = sys.platform
if supported_platforms and plat not in supported_platforms: # pragma: no cover
# case not covered by workflows
pytest.skip(f"Cannot run test on platform {plat}.")
def pytest_configure(config):
config.addinivalue_line("markers", "linux: mark test to run only on linux")
@pytest.fixture
def backend(backend_name):
yield get_backend(backend_name)
def pytest_generate_tests(metafunc):
module_name = metafunc.module.__name__
if module_name == "tests.test_models_distcircuit_execution":
config = [(bk, acc) for acc in ACCELERATORS for bk in MULTIGPU_BACKENDS]
metafunc.parametrize("backend_name,accelerators", config)
else:
if "backend_name" in metafunc.fixturenames:
if "accelerators" in metafunc.fixturenames:
config = [(backend, None) for backend in AVAILABLE_BACKENDS]
config.extend(
(bk, acc) for acc in ACCELERATORS for bk in MULTIGPU_BACKENDS
)
metafunc.parametrize("backend_name,accelerators", config)
else:
metafunc.parametrize("backend_name", AVAILABLE_BACKENDS)
elif "accelerators" in metafunc.fixturenames:
metafunc.parametrize("accelerators", ACCELERATORS)