|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import collections |
3 | 4 | import importlib
|
| 5 | +import inspect |
| 6 | +import os |
4 | 7 | from pathlib import Path
|
| 8 | +import re |
5 | 9 | import sys
|
6 | 10 | from typing import TYPE_CHECKING
|
7 | 11 | from typing import Callable
|
| 12 | +import unittest |
8 | 13 |
|
9 | 14 | import torch
|
10 | 15 | from triton.testing import do_bench
|
@@ -139,3 +144,139 @@ def check_example(
|
139 | 144 | )
|
140 | 145 | skip_accuracy or torch.testing.assert_close(result, expected, atol=1e-1, rtol=1e-2)
|
141 | 146 | return code
|
| 147 | + |
| 148 | + |
| 149 | +class AssertExpectedJournal: |
| 150 | + """ |
| 151 | + Manages a <testfile>.expected file that contains expected output for TestCase.assertExpectedJournal() calls. |
| 152 | +
|
| 153 | + This replaces the previous `expecttest` assertExpectedInline approach by storing expected output |
| 154 | + in external .expected files rather than inline strings in test files. This provides better |
| 155 | + organization and avoids cluttering test files with large code blocks. |
| 156 | +
|
| 157 | + The .expected file format uses sections like: |
| 158 | + --- assertExpectedJournal(TestClass.test_method) |
| 159 | + expected output here |
| 160 | +
|
| 161 | + --- assertExpectedJournal(TestClass.test_method) |
| 162 | + second expected output for same test |
| 163 | +
|
| 164 | + Environment variable EXPECTTEST_ACCEPT=1 can be used to update expected outputs. |
| 165 | + """ |
| 166 | + |
| 167 | + def __init__(self, cls: type[TestCase]) -> None: |
| 168 | + pyfile = os.path.abspath(inspect.getfile(cls)) |
| 169 | + assert "/test/" in pyfile |
| 170 | + assert pyfile.endswith(".py") |
| 171 | + self.filename: Path = Path(pyfile[:-3] + ".expected") |
| 172 | + self._cache: dict[str, list[str]] | None = None |
| 173 | + self.current_id: str | None = None |
| 174 | + self.current_index: int = 0 |
| 175 | + |
| 176 | + @property |
| 177 | + def cache(self) -> dict[str, list[str]]: |
| 178 | + if self._cache is None: |
| 179 | + return self.reload() |
| 180 | + return self._cache |
| 181 | + |
| 182 | + def reload(self) -> dict[str, list[str]]: |
| 183 | + if self.filename.exists(): |
| 184 | + data = self.filename.read_text() |
| 185 | + else: |
| 186 | + data = "" |
| 187 | + result = collections.defaultdict(list) |
| 188 | + for name, expected in re.findall( |
| 189 | + r"--- assertExpectedJournal\(([^)]*)\)\n(.*?)(?=^--- assertExpectedJournal\(|\Z)", |
| 190 | + data, |
| 191 | + re.MULTILINE | re.DOTALL, |
| 192 | + ): |
| 193 | + result[name].append(expected.strip()) |
| 194 | + self._cache = result |
| 195 | + return result |
| 196 | + |
| 197 | + def save(self) -> None: |
| 198 | + tmp = f"{self.filename}.tmp{os.getpid()}" |
| 199 | + with open(tmp, "w") as f: |
| 200 | + for name, expected_values in self.cache.items(): |
| 201 | + f.writelines( |
| 202 | + f"--- assertExpectedJournal({name})\n{expected}\n\n" |
| 203 | + for expected in expected_values |
| 204 | + ) |
| 205 | + os.rename(tmp, self.filename) |
| 206 | + |
| 207 | + @staticmethod |
| 208 | + def normalize_id(test_id: str) -> str: |
| 209 | + match = re.search(r"\b([^.]+\.[^.]+)$", test_id) |
| 210 | + assert match, f"Test ID '{test_id}' does not match expected format" |
| 211 | + return match.group(1) |
| 212 | + |
| 213 | + def lookup(self, test_id: str, value: str) -> tuple[str, str]: |
| 214 | + test_id = self.normalize_id(test_id) |
| 215 | + if self.current_id != test_id: |
| 216 | + self.current_id = test_id |
| 217 | + self.current_index = 0 |
| 218 | + |
| 219 | + expected_values = self.cache[test_id] |
| 220 | + if self.current_index < len(expected_values): |
| 221 | + expected = expected_values[self.current_index] |
| 222 | + else: |
| 223 | + assert self.current_index == len(expected_values) |
| 224 | + expected_values.append("") |
| 225 | + expected = "" |
| 226 | + |
| 227 | + value = value.strip() |
| 228 | + if value != expected and os.environ.get("EXPECTTEST_ACCEPT", "0") not in { |
| 229 | + "0", |
| 230 | + "false", |
| 231 | + "False", |
| 232 | + "", |
| 233 | + }: |
| 234 | + expected_values[self.current_index] = value |
| 235 | + # Reload to play nicer with other processes |
| 236 | + self.reload()[test_id] = expected_values |
| 237 | + self.save() |
| 238 | + expected = value |
| 239 | + print( |
| 240 | + f"Expected output for {test_id} updated: {len(expected)} => {len(value)} bytes", |
| 241 | + file=sys.stderr, |
| 242 | + ) |
| 243 | + self.current_index += 1 |
| 244 | + return value, expected |
| 245 | + |
| 246 | + |
| 247 | +class TestCase(unittest.TestCase): |
| 248 | + maxDiff = 16384 |
| 249 | + |
| 250 | + @classmethod |
| 251 | + def setUpClass(cls) -> None: |
| 252 | + cls._expected_journal = AssertExpectedJournal(cls) |
| 253 | + super().setUpClass() |
| 254 | + |
| 255 | + @classmethod |
| 256 | + def tearDownClass(cls) -> None: |
| 257 | + super().tearDownClass() |
| 258 | + del cls._expected_journal |
| 259 | + |
| 260 | + def assertExpectedJournal(self, value: str) -> None: |
| 261 | + """ |
| 262 | + Assert that the given value matches the expected output stored in <testfile>.expected. |
| 263 | +
|
| 264 | + This method replaces assertExpectedInline for code generation tests. Instead of storing |
| 265 | + expected output as inline strings in test files, it uses external .expected files for |
| 266 | + better organization. |
| 267 | +
|
| 268 | + Args: |
| 269 | + value: The actual output to compare (usually generated Triton code) |
| 270 | +
|
| 271 | + Raises: |
| 272 | + AssertionError: If value doesn't match expected output |
| 273 | +
|
| 274 | + Note: |
| 275 | + Use EXPECTTEST_ACCEPT=1 environment variable to update expected outputs. |
| 276 | + """ |
| 277 | + value, expected = self._expected_journal.lookup(self.id(), value) |
| 278 | + self.assertMultiLineEqual( |
| 279 | + value, |
| 280 | + expected, |
| 281 | + msg="To accept the new output, re-run test with env EXPECTTEST_ACCEPT=1", |
| 282 | + ) |
0 commit comments