Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(core): accept a size of 1 before comparing it in KORNIA_CHECK_SAME_SIZE (#6094)

Dmytro Mishkinc7f032a94cb9
diff --git a/changelog.d/6094.fixed.md b/changelog.d/6094.fixed.md
new file mode 100644
index 000000000..339152403
--- /dev/null
+++ b/changelog.d/6094.fixed.md
@@ -0,0 +1 @@
+On torch 2.9.1, `torch.onnx.export(..., dynamo=True)` exports `pyrdown`, `build_pyramid` and `gaussian_blur2d` with a dynamic batch again. They used to fail with "Constraints violated (batch)": the batch check of `filter2d` and of `gaussian_blur2d`'s `sigma` compared a kernel or `sigma` batch of 1 with the input batch before it accepted the 1, and the size-oblivious `torch.export` that this exporter runs then guarded the batch against 1. `KORNIA_CHECK_SAME_SIZE` with `allow_one=True` now accepts a size of 1 before comparing it with the other size.
diff --git a/kornia/core/check.py b/kornia/core/check.py
index 32c2d8f9f..873f4b78b 100644
--- a/kornia/core/check.py
+++ b/kornia/core/check.py
@@ -660,7 +660,14 @@ def KORNIA_CHECK_SAME_SIZE(
x_shape, y_shape = x.shape, y.shape
x_ndim, y_ndim = len(x_shape), len(y_shape)
in_range = -x_ndim <= dim < x_ndim and -y_ndim <= dim < y_ndim
- if not in_range or not (x_shape[dim] == y_shape[dim] or (allow_one and x_shape[dim] == 1)):
+ # A plain 1 passes before it is compared with the other size: under a size-oblivious torch.export, which the
+ # Dynamo ONNX exporter of torch 2.9.1 runs, ``1 == s`` guards a dynamic ``s`` against 1, and an export whose
+ # dynamic batch admits 1 then fails.
+ if not in_range or not (
+ (allow_one and isinstance(x_shape[dim], int) and x_shape[dim] == 1)
+ or x_shape[dim] == y_shape[dim]
+ or (allow_one and x_shape[dim] == 1)
+ ):
if raises:
msg = _resolve_msg(msg)
x_shape_list = list(x_shape)
diff --git a/tests/core/test_check.py b/tests/core/test_check.py
index 0096652ab..465297365 100644
--- a/tests/core/test_check.py
+++ b/tests/core/test_check.py
@@ -48,7 +48,7 @@ from kornia.core.exceptions import (
ValueCheckError,
)
-from testing.base import BaseTester
+from testing.base import BaseTester, dynamo_is_available
class TestCheck:
@@ -661,6 +661,28 @@ class TestCheckSameSize:
assert torch.equal(compiled(x, y), x + y)
assert counter.frame_count == 1
+ @pytest.mark.skipif(not dynamo_is_available(), reason="torch.export is unavailable on this torch/python pair")
+ def test_size_oblivious_export_keeps_a_dynamic_size_against_a_size_of_one(self):
+ # The Dynamo ONNX exporter of torch 2.9.1 runs torch.export size-obliviously, where comparing a kernel batch of
+ # 1 with a dynamic batch first guarded the batch against 1, so a dynamic batch that admits 1 failed to export.
+ config = torch.fx.experimental._config
+ if not hasattr(config, "backed_size_oblivious"):
+ pytest.skip("torch.export has no size-oblivious mode in this torch release")
+
+ class Model(torch.nn.Module):
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
+ KORNIA_CHECK_SAME_SIZE(torch.ones(1, 3, 3), x, allow_one=True)
+ KORNIA_CHECK_SAME_SIZE(x, x, allow_one=True)
+ return x * 2
+
+ with config.patch(backed_size_oblivious=True):
+ program = torch.export.export(
+ Model(), (torch.rand(2, 3, 8, 8),), dynamic_shapes=({0: torch.export.Dim("batch")},)
+ )
+ for batch in (1, 3):
+ x = torch.rand(batch, 3, 8, 8)
+ assert torch.equal(program.module()(x), x * 2)
+
class TestCheckSizes:
@pytest.mark.parametrize("shape, sizes", [((5,), [5]), ((2, 3), [None, 3]), ((2, 3, 4), [2, None, None]), ((), [])])