|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import importlib.util |
4 | 3 | import inspect |
5 | 4 | import os |
6 | 5 | from pathlib import Path |
7 | 6 |
|
8 | 7 | import pytest |
9 | 8 | import torch |
10 | 9 |
|
11 | | -from transformer_nuggets.export_autograd_triton import Specialization, export_autograd_triton |
| 10 | +from transformer_nuggets.export_autograd_triton import ( |
| 11 | + Specialization, |
| 12 | + export_autograd_triton, |
| 13 | + load_exported_module, |
| 14 | +) |
12 | 15 |
|
13 | 16 | os.environ.setdefault("TORCHINDUCTOR_COMPILE_THREADS", "1") |
14 | 17 |
|
@@ -98,11 +101,7 @@ def _requires_export_runtime(): |
98 | 101 |
|
99 | 102 |
|
100 | 103 | def _import_generated(path: Path): |
101 | | - spec = importlib.util.spec_from_file_location(path.stem, path) |
102 | | - module = importlib.util.module_from_spec(spec) |
103 | | - assert spec.loader is not None |
104 | | - spec.loader.exec_module(module) |
105 | | - return module |
| 104 | + return load_exported_module(path) |
106 | 105 |
|
107 | 106 |
|
108 | 107 | def _clone_tensor(tensor): |
@@ -521,13 +520,16 @@ def test_dynamic_batch_specialization_dispatches_across_batch_sizes(tmp_path): |
521 | 520 | ) |
522 | 521 | ], |
523 | 522 | generated_path, |
524 | | - source_backend="inductor", |
| 523 | + source_backend="clean_triton", |
525 | 524 | ) |
526 | 525 | module = _import_generated(generated_path) |
527 | | - assert "s" in "\n".join( |
| 526 | + artifact_source = "\n".join( |
528 | 527 | path.read_text() |
529 | 528 | for path in generated_path.with_name("generated_dynamic_artifacts").glob("*.py") |
530 | 529 | ) |
| 530 | + assert "@triton.jit" in artifact_source |
| 531 | + assert "async_compile.triton" not in artifact_source |
| 532 | + assert "triton.cdiv" in artifact_source |
531 | 533 |
|
532 | 534 | for batch_size in (1, 7, 16): |
533 | 535 | dynamic_x = torch.randn(batch_size, 8, device="cuda", requires_grad=True) |
@@ -560,11 +562,12 @@ def test_dynamic_shape_limitations_are_explicitly_guarded(tmp_path): |
560 | 562 | x = torch.randn(2, 4, device="cuda", requires_grad=True) |
561 | 563 | w = torch.randn(4, 5, device="cuda", requires_grad=True) |
562 | 564 |
|
563 | | - with pytest.raises(ValueError, match="source_backend='inductor'"): |
| 565 | + with pytest.raises(NotImplementedError, match="forward-only"): |
564 | 566 | export_autograd_triton( |
565 | | - affine_activation, |
566 | | - [Specialization(args=(x, w), dynamic_shapes={"x": {0: "batch"}})], |
567 | | - tmp_path / "generated_dynamic_clean.py", |
| 567 | + integer_tensor_output, |
| 568 | + [Specialization(args=(x,), dynamic_shapes={"x": {0: "batch"}})], |
| 569 | + tmp_path / "generated_dynamic_forward_only.py", |
| 570 | + source_backend="inductor", |
568 | 571 | ) |
569 | 572 |
|
570 | 573 | with pytest.raises(NotImplementedError, match="dynamic dim 0"): |
|
0 commit comments