|
| 1 | +from collections import Counter |
| 2 | +from dataclasses import dataclass |
| 3 | +from enum import IntEnum, nonmember |
| 4 | + |
| 5 | + |
| 6 | +class TestResult(IntEnum): |
| 7 | + """Represents the result of a test case run, indicating success or a specific failure reason.""" |
| 8 | + |
| 9 | + SUCCESS = 0 |
| 10 | + """ The test succeeded with the backend delegate part or all of the graph. """ |
| 11 | + |
| 12 | + SUCCESS_UNDELEGATED = 1 |
| 13 | + """ The test succeeded without the backend delegating anything. """ |
| 14 | + |
| 15 | + EAGER_FAIL = 2 |
| 16 | + """ The test failed due to the model failing to run in eager mode. """ |
| 17 | + |
| 18 | + EXPORT_FAIL = 3 |
| 19 | + """ The test failed due to the model failing to export. """ |
| 20 | + |
| 21 | + LOWER_FAIL = 4 |
| 22 | + """ The test failed due to a failure in partitioning or lowering. """ |
| 23 | + |
| 24 | + PTE_LOAD_FAIL = 5 |
| 25 | + """ The test failed due to the resulting PTE failing to load. """ |
| 26 | + |
| 27 | + PTE_RUN_FAIL = 6 |
| 28 | + """ The test failed due to the resulting PTE failing to run. """ |
| 29 | + |
| 30 | + OUTPUT_MISMATCH_FAIL = 7 |
| 31 | + """ The test failed due to a mismatch between runtime and reference outputs. """ |
| 32 | + |
| 33 | + UNKNOWN_FAIL = 8 |
| 34 | + """ The test failed in an unknown or unexpected manner. """ |
| 35 | + |
| 36 | + @nonmember |
| 37 | + def is_success(self): |
| 38 | + return self in {TestResult.SUCCESS, TestResult.SUCCESS_UNDELEGATED} |
| 39 | + |
| 40 | + @nonmember |
| 41 | + def is_non_backend_failure(self): |
| 42 | + return self in {TestResult.EAGER_FAIL, TestResult.EAGER_FAIL} |
| 43 | + |
| 44 | + @nonmember |
| 45 | + def is_backend_failure(self): |
| 46 | + return not self.is_success() and not self.is_non_backend_failure() |
| 47 | + |
| 48 | + @nonmember |
| 49 | + def display_name(self): |
| 50 | + if self == TestResult.SUCCESS: |
| 51 | + return "Success (Delegated)" |
| 52 | + elif self == TestResult.SUCCESS_UNDELEGATED: |
| 53 | + return "Success (Undelegated)" |
| 54 | + elif self == TestResult.EAGER_FAIL: |
| 55 | + return "Fail (Eager)" |
| 56 | + elif self == TestResult.EXPORT_FAIL: |
| 57 | + return "Fail (Export)" |
| 58 | + elif self == TestResult.LOWER_FAIL: |
| 59 | + return "Fail (Lowering)" |
| 60 | + elif self == TestResult.PTE_LOAD_FAIL: |
| 61 | + return "Fail (PTE Load)" |
| 62 | + elif self == TestResult.PTE_RUN_FAIL: |
| 63 | + return "Fail (PTE Run)" |
| 64 | + elif self == TestResult.OUTPUT_MISMATCH_FAIL: |
| 65 | + return "Fail (Output Mismatch)" |
| 66 | + elif self == TestResult.UNKNOWN_FAIL: |
| 67 | + return "Fail (Other)" |
| 68 | + else: |
| 69 | + raise ValueError(f"Invalid TestResult value: {self}.") |
| 70 | + |
| 71 | + |
| 72 | +@dataclass |
| 73 | +class TestCaseSummary: |
| 74 | + """ |
| 75 | + Contains summary results for the execution of a single test case. |
| 76 | + """ |
| 77 | + |
| 78 | + name: str |
| 79 | + """ The qualified name of the test, not including the flow suffix. """ |
| 80 | + |
| 81 | + flow: str |
| 82 | + """ The backend-specific flow name. Corresponds to flows registered in backends/test/suite/__init__.py. """ |
| 83 | + |
| 84 | + params: dict | None |
| 85 | + """ Test-specific parameters, such as dtype. """ |
| 86 | + |
| 87 | + result: TestResult |
| 88 | + """ The top-level result, such as SUCCESS or LOWER_FAIL. """ |
| 89 | + |
| 90 | + error: Exception | None |
| 91 | + """ The Python exception object, if any. """ |
| 92 | + |
| 93 | + |
| 94 | +class TestSessionState: |
| 95 | + test_case_summaries: list[TestCaseSummary] |
| 96 | + |
| 97 | + def __init__(self): |
| 98 | + self.test_case_summaries = [] |
| 99 | + |
| 100 | + |
| 101 | +@dataclass |
| 102 | +class RunSummary: |
| 103 | + aggregated_results: dict[TestResult, int] |
| 104 | + num_test_cases: int |
| 105 | + test_case_summaries: list[TestCaseSummary] |
| 106 | + total_failed: int |
| 107 | + total_passed: int |
| 108 | + total_skipped: int |
| 109 | + |
| 110 | + @staticmethod |
| 111 | + def from_session(session: TestSessionState) -> "RunSummary": |
| 112 | + # Total each outcome type. |
| 113 | + aggregated_results = dict( |
| 114 | + sorted(Counter(s.result for s in session.test_case_summaries).items()) |
| 115 | + ) |
| 116 | + |
| 117 | + total_failed = 0 |
| 118 | + total_passed = 0 |
| 119 | + total_skipped = 0 |
| 120 | + |
| 121 | + for k, v in aggregated_results.items(): |
| 122 | + if k.is_success(): |
| 123 | + total_passed += v |
| 124 | + elif k.is_backend_failure(): |
| 125 | + total_failed += v |
| 126 | + else: |
| 127 | + total_skipped += v |
| 128 | + |
| 129 | + return RunSummary( |
| 130 | + aggregated_results=aggregated_results, |
| 131 | + num_test_cases=len(session.test_case_summaries), |
| 132 | + test_case_summaries=session.test_case_summaries, |
| 133 | + total_failed=total_failed, |
| 134 | + total_passed=total_passed, |
| 135 | + total_skipped=total_skipped, |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +_active_session: TestSessionState | None = None |
| 140 | + |
| 141 | + |
| 142 | +def begin_test_session(): |
| 143 | + global _active_session |
| 144 | + |
| 145 | + assert _active_session is None, "A test session is already active." |
| 146 | + _active_session = TestSessionState() |
| 147 | + |
| 148 | + |
| 149 | +def log_test_summary(summary: TestCaseSummary): |
| 150 | + global _active_session |
| 151 | + |
| 152 | + if _active_session is not None: |
| 153 | + _active_session.test_case_summaries.append(summary) |
| 154 | + |
| 155 | + |
| 156 | +def complete_test_session() -> RunSummary: |
| 157 | + global _active_session |
| 158 | + |
| 159 | + assert _active_session is not None, "No test session is active." |
| 160 | + summary = RunSummary.from_session(_active_session) |
| 161 | + _active_session = None |
| 162 | + |
| 163 | + return summary |
0 commit comments