xref: /aosp_15_r20/external/pytorch/scripts/compile_tests/common.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Workerimport functools
2*da0073e9SAndroid Build Coastguard Workerimport os
3*da0073e9SAndroid Build Coastguard Workerimport warnings
4*da0073e9SAndroid Build Coastguard Worker
5*da0073e9SAndroid Build Coastguard Worker
6*da0073e9SAndroid Build Coastguard Workertry:
7*da0073e9SAndroid Build Coastguard Worker    import lxml.etree
8*da0073e9SAndroid Build Coastguard Worker
9*da0073e9SAndroid Build Coastguard Worker    p = lxml.etree.XMLParser(huge_tree=True)
10*da0073e9SAndroid Build Coastguard Worker    parse = functools.partial(lxml.etree.parse, parser=p)
11*da0073e9SAndroid Build Coastguard Workerexcept ImportError:
12*da0073e9SAndroid Build Coastguard Worker    import xml.etree.ElementTree as ET
13*da0073e9SAndroid Build Coastguard Worker
14*da0073e9SAndroid Build Coastguard Worker    parse = ET.parse
15*da0073e9SAndroid Build Coastguard Worker    warnings.warn(
16*da0073e9SAndroid Build Coastguard Worker        "lxml was not found. `pip install lxml` to make this script run much faster"
17*da0073e9SAndroid Build Coastguard Worker    )
18*da0073e9SAndroid Build Coastguard Worker
19*da0073e9SAndroid Build Coastguard Worker
20*da0073e9SAndroid Build Coastguard Workerdef open_test_results(directory):
21*da0073e9SAndroid Build Coastguard Worker    xmls = []
22*da0073e9SAndroid Build Coastguard Worker    for root, _, files in os.walk(directory):
23*da0073e9SAndroid Build Coastguard Worker        for file in files:
24*da0073e9SAndroid Build Coastguard Worker            if file.endswith(".xml"):
25*da0073e9SAndroid Build Coastguard Worker                tree = parse(f"{root}/{file}")
26*da0073e9SAndroid Build Coastguard Worker                xmls.append(tree)
27*da0073e9SAndroid Build Coastguard Worker    return xmls
28*da0073e9SAndroid Build Coastguard Worker
29*da0073e9SAndroid Build Coastguard Worker
30*da0073e9SAndroid Build Coastguard Workerdef get_testcases(xmls):
31*da0073e9SAndroid Build Coastguard Worker    testcases = []
32*da0073e9SAndroid Build Coastguard Worker    for xml in xmls:
33*da0073e9SAndroid Build Coastguard Worker        root = xml.getroot()
34*da0073e9SAndroid Build Coastguard Worker        testcases.extend(list(root.iter("testcase")))
35*da0073e9SAndroid Build Coastguard Worker    return testcases
36*da0073e9SAndroid Build Coastguard Worker
37*da0073e9SAndroid Build Coastguard Worker
38*da0073e9SAndroid Build Coastguard Workerdef find(testcase, condition):
39*da0073e9SAndroid Build Coastguard Worker    children = list(testcase.iter())
40*da0073e9SAndroid Build Coastguard Worker    assert children[0] is testcase
41*da0073e9SAndroid Build Coastguard Worker    children = children[1:]
42*da0073e9SAndroid Build Coastguard Worker    return condition(children)
43*da0073e9SAndroid Build Coastguard Worker
44*da0073e9SAndroid Build Coastguard Worker
45*da0073e9SAndroid Build Coastguard Workerdef skipped_test(testcase):
46*da0073e9SAndroid Build Coastguard Worker    def condition(children):
47*da0073e9SAndroid Build Coastguard Worker        return "skipped" in {child.tag for child in children}
48*da0073e9SAndroid Build Coastguard Worker
49*da0073e9SAndroid Build Coastguard Worker    return find(testcase, condition)
50*da0073e9SAndroid Build Coastguard Worker
51*da0073e9SAndroid Build Coastguard Worker
52*da0073e9SAndroid Build Coastguard Workerdef passed_test(testcase):
53*da0073e9SAndroid Build Coastguard Worker    def condition(children):
54*da0073e9SAndroid Build Coastguard Worker        if len(children) == 0:
55*da0073e9SAndroid Build Coastguard Worker            return True
56*da0073e9SAndroid Build Coastguard Worker        tags = {child.tag for child in children}
57*da0073e9SAndroid Build Coastguard Worker        return "skipped" not in tags and "failed" not in tags
58*da0073e9SAndroid Build Coastguard Worker
59*da0073e9SAndroid Build Coastguard Worker    return find(testcase, condition)
60*da0073e9SAndroid Build Coastguard Worker
61*da0073e9SAndroid Build Coastguard Worker
62*da0073e9SAndroid Build Coastguard Workerdef key(testcase):
63*da0073e9SAndroid Build Coastguard Worker    file = testcase.attrib.get("file", "UNKNOWN")
64*da0073e9SAndroid Build Coastguard Worker    classname = testcase.attrib["classname"]
65*da0073e9SAndroid Build Coastguard Worker    name = testcase.attrib["name"]
66*da0073e9SAndroid Build Coastguard Worker    return "::".join([file, classname, name])
67*da0073e9SAndroid Build Coastguard Worker
68*da0073e9SAndroid Build Coastguard Worker
69*da0073e9SAndroid Build Coastguard Workerdef get_passed_testcases(xmls):
70*da0073e9SAndroid Build Coastguard Worker    testcases = get_testcases(xmls)
71*da0073e9SAndroid Build Coastguard Worker    passed_testcases = [testcase for testcase in testcases if passed_test(testcase)]
72*da0073e9SAndroid Build Coastguard Worker    return passed_testcases
73*da0073e9SAndroid Build Coastguard Worker
74*da0073e9SAndroid Build Coastguard Worker
75*da0073e9SAndroid Build Coastguard Workerdef get_excluded_testcases(xmls):
76*da0073e9SAndroid Build Coastguard Worker    testcases = get_testcases(xmls)
77*da0073e9SAndroid Build Coastguard Worker    excluded_testcases = [t for t in testcases if excluded_testcase(t)]
78*da0073e9SAndroid Build Coastguard Worker    return excluded_testcases
79*da0073e9SAndroid Build Coastguard Worker
80*da0073e9SAndroid Build Coastguard Worker
81*da0073e9SAndroid Build Coastguard Workerdef excluded_testcase(testcase):
82*da0073e9SAndroid Build Coastguard Worker    def condition(children):
83*da0073e9SAndroid Build Coastguard Worker        for child in children:
84*da0073e9SAndroid Build Coastguard Worker            if child.tag == "skipped":
85*da0073e9SAndroid Build Coastguard Worker                if "Policy: we don't run" in child.attrib["message"]:
86*da0073e9SAndroid Build Coastguard Worker                    return True
87*da0073e9SAndroid Build Coastguard Worker        return False
88*da0073e9SAndroid Build Coastguard Worker
89*da0073e9SAndroid Build Coastguard Worker    return find(testcase, condition)
90*da0073e9SAndroid Build Coastguard Worker
91*da0073e9SAndroid Build Coastguard Worker
92*da0073e9SAndroid Build Coastguard Workerdef is_unexpected_success(testcase):
93*da0073e9SAndroid Build Coastguard Worker    def condition(children):
94*da0073e9SAndroid Build Coastguard Worker        for child in children:
95*da0073e9SAndroid Build Coastguard Worker            if child.tag != "failure":
96*da0073e9SAndroid Build Coastguard Worker                continue
97*da0073e9SAndroid Build Coastguard Worker            is_unexpected_success = (
98*da0073e9SAndroid Build Coastguard Worker                "unexpected success" in child.attrib["message"].lower()
99*da0073e9SAndroid Build Coastguard Worker            )
100*da0073e9SAndroid Build Coastguard Worker            if is_unexpected_success:
101*da0073e9SAndroid Build Coastguard Worker                return True
102*da0073e9SAndroid Build Coastguard Worker        return False
103*da0073e9SAndroid Build Coastguard Worker
104*da0073e9SAndroid Build Coastguard Worker    return find(testcase, condition)
105*da0073e9SAndroid Build Coastguard Worker
106*da0073e9SAndroid Build Coastguard Worker
107*da0073e9SAndroid Build Coastguard WorkerMSG = "This test passed, maybe we can remove the skip from dynamo_test_failures.py"
108*da0073e9SAndroid Build Coastguard Worker
109*da0073e9SAndroid Build Coastguard Worker
110*da0073e9SAndroid Build Coastguard Workerdef is_passing_skipped_test(testcase):
111*da0073e9SAndroid Build Coastguard Worker    def condition(children):
112*da0073e9SAndroid Build Coastguard Worker        for child in children:
113*da0073e9SAndroid Build Coastguard Worker            if child.tag != "skipped":
114*da0073e9SAndroid Build Coastguard Worker                continue
115*da0073e9SAndroid Build Coastguard Worker            has_passing_skipped_test_msg = MSG in child.attrib["message"]
116*da0073e9SAndroid Build Coastguard Worker            if has_passing_skipped_test_msg:
117*da0073e9SAndroid Build Coastguard Worker                return True
118*da0073e9SAndroid Build Coastguard Worker        return False
119*da0073e9SAndroid Build Coastguard Worker
120*da0073e9SAndroid Build Coastguard Worker    return find(testcase, condition)
121*da0073e9SAndroid Build Coastguard Worker
122*da0073e9SAndroid Build Coastguard Worker
123*da0073e9SAndroid Build Coastguard Worker# NB: not an unexpected success
124*da0073e9SAndroid Build Coastguard Workerdef is_failure(testcase):
125*da0073e9SAndroid Build Coastguard Worker    def condition(children):
126*da0073e9SAndroid Build Coastguard Worker        for child in children:
127*da0073e9SAndroid Build Coastguard Worker            if child.tag != "failure":
128*da0073e9SAndroid Build Coastguard Worker                continue
129*da0073e9SAndroid Build Coastguard Worker            is_unexpected_success = (
130*da0073e9SAndroid Build Coastguard Worker                "unexpected success" in child.attrib["message"].lower()
131*da0073e9SAndroid Build Coastguard Worker            )
132*da0073e9SAndroid Build Coastguard Worker            if not is_unexpected_success:
133*da0073e9SAndroid Build Coastguard Worker                return True
134*da0073e9SAndroid Build Coastguard Worker        return False
135*da0073e9SAndroid Build Coastguard Worker
136*da0073e9SAndroid Build Coastguard Worker    return find(testcase, condition)
137