已合并
Fix testing init. #16166
yuhaiyan8创建于 2024年11月20日
Fix testing init. #16166
已合并
yuhaiyan8创建于 2024年11月20日
refs/pull/16166/head合入到master
2 个文件变更+44-42
@@ -31,7 +31,7 @@ from torch.testing._internal.common_modules import modules, module_db, ModuleInf
31from torch.testing._internal.opinfo.core import SampleInput, DecorateInfo, OpInfo31from torch.testing._internal.opinfo.core import SampleInput, DecorateInfo, OpInfo
32 32 
33 33 
34-# For testing TestCase methods and torch.testing functions34+# For testing TestCase methods and torch.testing functions
35class TestTesting(TestCase):35class TestTesting(TestCase):
36 # Ensure that assertEqual handles numpy arrays properly36 # Ensure that assertEqual handles numpy arrays properly
37 @dtypes(*all_types_and_complex_and(torch.bool, torch.half))37 @dtypes(*all_types_and_complex_and(torch.bool, torch.half))
@@ -16,56 +16,58 @@ from torch_npu.testing._npu_testing_utils import update_skip_list, get_decorator
16__all__ = []16__all__ = []
17 17 
18 18 
19-def _filter_json(data):19+def _get_tests_dict():
20- return {key: val for key, val in data.items() if len(val) > 1 and not(val[1] and "A2" in val[1])}
21 20 
21+ def _filter_json(data):
22+ return {key: val for key, val in data.items() if len(val) > 1 and not(val[1] and "A2" in val[1])}
22 23 
23-def _is_910A():24+ def _is_910A():
24- device_name = torch_npu.npu.get_device_name(0)25+ device_name = torch_npu.npu.get_device_name(0)
25- if "Ascend910A" in device_name or "Ascend910P" in device_name:26+ if "Ascend910A" in device_name or "Ascend910P" in device_name:
26- return True27+ return True
27- return False28+ return False
28 29 
30+ def _load_disabled_json(filename):
31+ if os.path.isfile(filename):
32+ with open(filename) as fp0:
33+ if _is_910A():
34+ disabled_dict = json.load(fp0, object_hook=_filter_json)
35+ else:
36+ disabled_dict = json.load(fp0)
37+ return disabled_dict
38+ warnings.warn("Attempted to load json file '%s' but it does not exist.", filename)
39+ return {}
29 40 
30-def _load_disabled_json(filename):41+ # import test files
31- if os.path.isfile(filename):42+ disabled_tests_dict = {}
32- with open(filename) as fp0:43+ slow_tests_dict = {}
33- if _is_910A():44+ # set them here in case the tests are running in a subprocess that doesn't call run_tests
34- disabled_dict = json.load(fp0, object_hook=_filter_json)45+ if os.getenv("SLOW_TESTS_FILE", ""):
35- else:46+ slow_tests_dict = maybe_load_json(os.getenv("SLOW_TESTS_FILE", ""))
36- disabled_dict = json.load(fp0)47+ if os.getenv("DISABLED_TESTS_FILE", ""):
37- return disabled_dict48+ disabled_tests_dict = _load_disabled_json(os.getenv("DISABLED_TESTS_FILE", ""))
38- warnings.warn("Attempted to load json file '%s' but it does not exist.", filename)49+ if SLOW_TESTS_FILE:
39- return {}50+ if os.path.exists(SLOW_TESTS_FILE):
51+ with open(SLOW_TESTS_FILE) as fp:
52+ slow_tests_dict = json.load(fp)
53+ # use env vars so pytest-xdist subprocesses can still access them
54+ os.environ['SLOW_TESTS_FILE'] = SLOW_TESTS_FILE
55+ else:
56+ warnings.warn(f'slow test file provided but not found: {SLOW_TESTS_FILE}')
40 57 
58+ if DISABLED_TESTS_FILE:
59+ if os.path.exists(DISABLED_TESTS_FILE):
60+ disabled_tests_dict = _load_disabled_json(DISABLED_TESTS_FILE)
61+ os.environ['DISABLED_TESTS_FILE'] = DISABLED_TESTS_FILE
62+ else:
63+ warnings.warn(f'disabled test file provided but not found: {DISABLED_TESTS_FILE}')
41 64 
42-# import test files65+ return disabled_tests_dict, slow_tests_dict
43-disabled_tests_dict = {}
44-slow_tests_dict = {}
45-# set them here in case the tests are running in a subprocess that doesn't call run_tests
46-if os.getenv("SLOW_TESTS_FILE", ""):
47- slow_tests_dict = maybe_load_json(os.getenv("SLOW_TESTS_FILE", ""))
48-if os.getenv("DISABLED_TESTS_FILE", ""):
49- disabled_tests_dict = _load_disabled_json(os.getenv("DISABLED_TESTS_FILE", ""))
50-if SLOW_TESTS_FILE:
51- if os.path.exists(SLOW_TESTS_FILE):
52- with open(SLOW_TESTS_FILE) as fp:
53- slow_tests_dict = json.load(fp)
54- # use env vars so pytest-xdist subprocesses can still access them
55- os.environ['SLOW_TESTS_FILE'] = SLOW_TESTS_FILE
56- else:
57- warnings.warn(f'slow test file provided but not found: {SLOW_TESTS_FILE}')
58- 
59- 
60-if DISABLED_TESTS_FILE:
61- if os.path.exists(DISABLED_TESTS_FILE):
62- disabled_tests_dict = _load_disabled_json(DISABLED_TESTS_FILE)
63- os.environ['DISABLED_TESTS_FILE'] = DISABLED_TESTS_FILE
64- else:
65- warnings.warn(f'disabled test file provided but not found: {DISABLED_TESTS_FILE}')
66 66 
67 67 
68def _check_if_enable_npu(test: unittest.TestCase):68def _check_if_enable_npu(test: unittest.TestCase):
69+ disabled_tests_dict, slow_tests_dict = _get_tests_dict()
70+ 
69 classname = str(test.__class__).split("'")[1].split(".")[-1]71 classname = str(test.__class__).split("'")[1].split(".")[-1]
70 sanitized_testname = remove_device_and_dtype_suffixes(test._testMethodName)72 sanitized_testname = remove_device_and_dtype_suffixes(test._testMethodName)
71 73