Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Replace jit test method in favor of dynamo in `BaseTester` (#2120)

João Gustavo A. Amorim9def16475e76
diff --git a/.github/workflows/pr_test_cpu.yml b/.github/workflows/pr_test_cpu.yml
index 2003aeb80..d1a113087 100644
--- a/.github/workflows/pr_test_cpu.yml
+++ b/.github/workflows/pr_test_cpu.yml
@@ -48,7 +48,7 @@ jobs:
strategy:
fail-fast: false
matrix:
- os: ['Ubuntu-latest', 'Windows-latest', 'MacOS-latest']
+ os: ['Ubuntu-latest', 'Windows-latest'] #, 'MacOS-latest'] add it when https://github.com/pytorch/pytorch/pull/89262 be merged
pytorch-dtype: ['float32', 'float64']
uses: ./.github/workflows/tests.yml
with:
diff --git a/conftest.py b/conftest.py
index a6575dff0..1d48dad26 100644
--- a/conftest.py
+++ b/conftest.py
@@ -63,6 +63,18 @@ def dtype(dtype_name) -> torch.dtype:
return TEST_DTYPES[dtype_name]
[email protected](scope='session')
+def torch_optimizer():
+ try:
+ import torch._dynamo as dynamo
+
+ torch.set_float32_matmul_precision('high')
+ return dynamo.optimize('inductor')
+ except Exception as er:
+ del er
+ pytest.skip(f"skipped because {torch.__version__} may not have dynamo available! Failed to setup dynamo.")
+
+
def pytest_generate_tests(metafunc):
device_names = None
dtype_names = None
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index 58566fafc..9ff65c361 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -119,9 +119,9 @@ class BaseTester(ABC):
def test_cardinality(self, device, dtype):
raise NotImplementedError("Implement a stupid routine.")
- @abstractmethod
- def test_jit(self, device, dtype):
- raise NotImplementedError("Implement a stupid routine.")
+ # TODO: add @abstractmethod
+ def test_dynamo(self, device, dtype, torch_optimizer):
+ pass
@abstractmethod
def test_gradcheck(self, device):
diff --git a/test/color/test_gray.py b/test/color/test_gray.py
index 90e40967a..15b2edc72 100644
--- a/test/color/test_gray.py
+++ b/test/color/test_gray.py
@@ -87,13 +87,12 @@ class TestGrayscaleToRgb(BaseTester):
img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
assert gradcheck(kornia.color.grayscale_to_rgb, (img,), raise_exception=True, fast_mode=True)
- @pytest.mark.jit
- def test_jit(self, device, dtype):
+ def test_dynamo(self, device, dtype, torch_optimizer):
B, C, H, W = 2, 1, 4, 4
img = torch.ones(B, C, H, W, device=device, dtype=dtype)
op = kornia.color.grayscale_to_rgb
- op_jit = torch.jit.script(op)
- assert_close(op(img), op_jit(img))
+ op_optimized = torch_optimizer(op)
+ assert_close(op(img), op_optimized(img))
def test_module(self, device, dtype):
B, C, H, W = 2, 1, 4, 4
@@ -202,13 +201,12 @@ class TestRgbToGrayscale(BaseTester):
img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
assert gradcheck(kornia.color.rgb_to_grayscale, (img,), raise_exception=True, fast_mode=True)
- @pytest.mark.jit
- def test_jit(self, device, dtype):
+ def test_dynamo(self, device, dtype, torch_optimizer):
B, C, H, W = 2, 3, 4, 4
img = torch.ones(B, C, H, W, device=device, dtype=dtype)
op = kornia.color.rgb_to_grayscale
- op_jit = torch.jit.script(op)
- assert_close(op(img), op_jit(img))
+ op_optimized = torch_optimizer(op)
+ assert_close(op(img), op_optimized(img))
def test_module(self, device, dtype):
B, C, H, W = 2, 3, 4, 4
@@ -294,13 +292,12 @@ class TestBgrToGrayscale(BaseTester):
img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
assert gradcheck(kornia.color.bgr_to_grayscale, (img,), raise_exception=True, fast_mode=True)
- @pytest.mark.jit
- def test_jit(self, device, dtype):
+ def test_dynamo(self, device, dtype, torch_optimizer):
B, C, H, W = 2, 3, 4, 4
img = torch.ones(B, C, H, W, device=device, dtype=dtype)
op = kornia.color.rgb_to_grayscale
- op_jit = torch.jit.script(op)
- assert_close(op(img), op_jit(img))
+ op_optimized = torch_optimizer(op)
+ assert_close(op(img), op_optimized(img))
def test_module(self, device, dtype):
B, C, H, W = 2, 3, 4, 4