-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtesting_utils.py
129 lines (104 loc) · 3.88 KB
/
testing_utils.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import dataclasses
import enum
import logging
import os
import unittest
from subprocess import PIPE, STDOUT, run
from typing import List, Optional, Union
import yaml
from tests.data import CustomTestConfig, TestConfig
# TODO: probably makes sense to move this type of function to a more central place,
# which can be used by __init__.py as well
def is_torch_available():
try:
import torch # noqa: F401
return True
except ImportError:
return False
def is_gpu_available():
"""
Check for GPU and warn if not found
"""
try:
import torch # noqa: F401
return torch.cuda.device_count() > 0
except ImportError:
return False
def requires_torch(test_case):
return unittest.skipUnless(is_torch_available(), "test requires PyTorch")(test_case)
def requires_gpu(test_case):
return unittest.skipUnless(is_gpu_available(), "test requires GPU")(test_case)
def _load_yaml(configs_directory, file):
if file.endswith(".yaml") or file.endswith(".yml"):
config_path = os.path.join(configs_directory, file)
# reads the yaml file
with open(config_path, "r") as f:
config = yaml.safe_load(f)
return config
return None
def _validate_test_config(config: dict):
for f in dataclasses.fields(TestConfig):
if f.name not in config:
return False
config_value = config.get(f.name)
if issubclass(f.type, enum.Enum):
try:
f.type(config_value)
except ValueError:
raise False
return True
# Set cadence in the config. The environment must set if nightly, weekly or commit
# tests are running
def parse_params(
configs_directory: str, type: Optional[str] = None
) -> List[Union[dict, CustomTestConfig]]:
# parses the config file provided
assert os.path.isdir(
configs_directory
), f"Config_directory {configs_directory} is not a directory"
config_dicts = []
for file in os.listdir(configs_directory):
config = _load_yaml(configs_directory, file)
if not config:
continue
cadence = os.environ.get("CADENCE", "commit")
expected_cadence = config.get("cadence")
if not isinstance(expected_cadence, list):
expected_cadence = [expected_cadence]
if cadence in expected_cadence:
if type == "custom":
config = CustomTestConfig(**config)
else:
if not _validate_test_config(config):
raise ValueError(
"The config provided does not comply with the expected "
"structure. See tests.data.TestConfig for the expected "
"fields."
)
config_dicts.append(config)
else:
logging.info(
f"Skipping testing model: {file} for cadence: {config['cadence']}"
)
return config_dicts
def run_cli_command(cmd: List[str]):
"""
Run a cli command and return the response. The cli command is launched through a new
subprocess.
:param cmd: cli command provided as a list of arguments where each argument
should be a string
"""
return run(cmd, stdout=PIPE, stderr=STDOUT, check=False, encoding="utf-8")