|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import contextlib |
| 4 | +import io |
3 | 5 | import itertools |
4 | 6 | from typing import Callable |
5 | 7 | import unittest |
@@ -288,6 +290,141 @@ def bmm(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: |
288 | 290 | expected = torch.bmm(A, B).to(result.dtype) * 2 |
289 | 291 | torch.testing.assert_close(result, expected, atol=1e-2, rtol=1e-2) |
290 | 292 |
|
| 293 | + def _assert_warning_in_stderr( |
| 294 | + self, kernel, args, expected_result, warning_str, *, atol=1e-2, rtol=1e-2 |
| 295 | + ): |
| 296 | + stderr_buffer = io.StringIO() |
| 297 | + with contextlib.redirect_stderr(stderr_buffer): |
| 298 | + _, out = code_and_output(kernel, args) |
| 299 | + |
| 300 | + torch.testing.assert_close(out, expected_result, atol=atol, rtol=rtol) |
| 301 | + |
| 302 | + warning_text = stderr_buffer.getvalue() |
| 303 | + self.assertIn(warning_str, warning_text) |
| 304 | + |
| 305 | + @skipIfRefEager("Warning emitted in compile mode only") |
| 306 | + def test_augassign_at_operator_warning(self): |
| 307 | + @helion.kernel(static_shapes=True) |
| 308 | + def warn_kernel(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 309 | + m, k = x.shape |
| 310 | + k2, n = y.shape |
| 311 | + assert k == k2 |
| 312 | + out = torch.empty([m, n], dtype=x.dtype, device=x.device) |
| 313 | + for tile_m, tile_n in hl.tile([m, n]): |
| 314 | + acc = hl.zeros([tile_m, tile_n], dtype=torch.float32) |
| 315 | + for tile_k in hl.tile(k): |
| 316 | + lhs = x[tile_m, tile_k] |
| 317 | + rhs = y[tile_k, tile_n] |
| 318 | + acc += lhs @ rhs |
| 319 | + out[tile_m, tile_n] = acc |
| 320 | + return out |
| 321 | + |
| 322 | + x = torch.randn(32, 16, device=DEVICE, dtype=torch.float32) |
| 323 | + y = torch.randn(16, 32, device=DEVICE, dtype=torch.float32) |
| 324 | + |
| 325 | + self._assert_warning_in_stderr( |
| 326 | + warn_kernel, (x, y), x @ y, "WARNING[TiledKMatmulAccumulationWarning]" |
| 327 | + ) |
| 328 | + |
| 329 | + @skipIfRefEager("Warning emitted in compile mode only") |
| 330 | + def test_augassign_torch_matmul_warning(self): |
| 331 | + @helion.kernel(static_shapes=True) |
| 332 | + def warn_kernel(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 333 | + m, k = x.shape |
| 334 | + k2, n = y.shape |
| 335 | + assert k == k2 |
| 336 | + out = torch.empty([m, n], dtype=x.dtype, device=x.device) |
| 337 | + for tile_m, tile_n in hl.tile([m, n]): |
| 338 | + acc = hl.zeros([tile_m, tile_n], dtype=torch.float32) |
| 339 | + for tile_k in hl.tile(k): |
| 340 | + lhs = x[tile_m, tile_k] |
| 341 | + rhs = y[tile_k, tile_n] |
| 342 | + acc += torch.matmul(lhs, rhs) |
| 343 | + out[tile_m, tile_n] = acc |
| 344 | + return out |
| 345 | + |
| 346 | + x = torch.randn(32, 16, device=DEVICE, dtype=torch.float32) |
| 347 | + y = torch.randn(16, 32, device=DEVICE, dtype=torch.float32) |
| 348 | + |
| 349 | + self._assert_warning_in_stderr( |
| 350 | + warn_kernel, (x, y), x @ y, "WARNING[TiledKMatmulAccumulationWarning]" |
| 351 | + ) |
| 352 | + |
| 353 | + @skipIfRefEager("Warning emitted in compile mode only") |
| 354 | + def test_augassign_torch_mm_warning(self): |
| 355 | + @helion.kernel(static_shapes=True) |
| 356 | + def warn_kernel(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 357 | + m, k = x.shape |
| 358 | + k2, n = y.shape |
| 359 | + assert k == k2 |
| 360 | + out = torch.empty([m, n], dtype=x.dtype, device=x.device) |
| 361 | + for tile_m, tile_n in hl.tile([m, n]): |
| 362 | + acc = hl.zeros([tile_m, tile_n], dtype=torch.float32) |
| 363 | + for tile_k in hl.tile(k): |
| 364 | + lhs = x[tile_m, tile_k] |
| 365 | + rhs = y[tile_k, tile_n] |
| 366 | + acc += torch.mm(lhs, rhs) |
| 367 | + out[tile_m, tile_n] = acc |
| 368 | + return out |
| 369 | + |
| 370 | + x = torch.randn(32, 16, device=DEVICE, dtype=torch.float32) |
| 371 | + y = torch.randn(16, 32, device=DEVICE, dtype=torch.float32) |
| 372 | + |
| 373 | + self._assert_warning_in_stderr( |
| 374 | + warn_kernel, (x, y), x @ y, "WARNING[TiledKMatmulAccumulationWarning]" |
| 375 | + ) |
| 376 | + |
| 377 | + @skipIfRefEager("Warning emitted in compile mode only") |
| 378 | + def test_augassign_torch_bmm_warning(self): |
| 379 | + @helion.kernel(static_shapes=True) |
| 380 | + def warn_kernel(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 381 | + b, m, k = x.shape |
| 382 | + b2, k2, n = y.shape |
| 383 | + assert b == b2 and k == k2 |
| 384 | + out = torch.empty([b, m, n], dtype=x.dtype, device=x.device) |
| 385 | + for tile_b, tile_m, tile_n in hl.tile([b, m, n]): |
| 386 | + acc = hl.zeros([tile_b, tile_m, tile_n], dtype=torch.float32) |
| 387 | + for tile_k in hl.tile(k): |
| 388 | + lhs = x[tile_b, tile_m, tile_k] |
| 389 | + rhs = y[tile_b, tile_k, tile_n] |
| 390 | + acc += torch.bmm(lhs, rhs) |
| 391 | + out[tile_b, tile_m, tile_n] = acc |
| 392 | + return out |
| 393 | + |
| 394 | + x = torch.randn(4, 32, 16, device=DEVICE, dtype=torch.float32) |
| 395 | + y = torch.randn(4, 16, 32, device=DEVICE, dtype=torch.float32) |
| 396 | + |
| 397 | + self._assert_warning_in_stderr( |
| 398 | + warn_kernel, |
| 399 | + (x, y), |
| 400 | + torch.bmm(x, y), |
| 401 | + "WARNING[TiledKMatmulAccumulationWarning]", |
| 402 | + ) |
| 403 | + |
| 404 | + @skipIfRefEager("Warning emitted in compile mode only") |
| 405 | + def test_augassign_hl_dot_warning(self): |
| 406 | + @helion.kernel(static_shapes=True) |
| 407 | + def no_warn_kernel(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 408 | + m, k = x.shape |
| 409 | + k2, n = y.shape |
| 410 | + assert k == k2 |
| 411 | + out = torch.empty([m, n], dtype=x.dtype, device=x.device) |
| 412 | + for tile_m, tile_n in hl.tile([m, n]): |
| 413 | + acc = hl.zeros([tile_m, tile_n], dtype=torch.float32) |
| 414 | + for tile_k in hl.tile(k): |
| 415 | + lhs = x[tile_m, tile_k] |
| 416 | + rhs = y[tile_k, tile_n] |
| 417 | + acc += hl.dot(lhs, rhs) |
| 418 | + out[tile_m, tile_n] = acc |
| 419 | + return out |
| 420 | + |
| 421 | + x = torch.randn(32, 16, device=DEVICE, dtype=torch.float32) |
| 422 | + y = torch.randn(16, 32, device=DEVICE, dtype=torch.float32) |
| 423 | + |
| 424 | + self._assert_warning_in_stderr( |
| 425 | + no_warn_kernel, (x, y), x @ y, "WARNING[TiledKMatmulAccumulationWarning]" |
| 426 | + ) |
| 427 | + |
291 | 428 | # Note: numerical behavior for differing acc dtype is covered by existing dot tests; here we focus on codegen shape |
292 | 429 |
|
293 | 430 | # torch.baddbmm codegen shape is covered indirectly by broader matmul tests; skipping a brittle code-inspection here |
|
0 commit comments