Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): make Resize compute_transformation fullgraph-safe (#3808)

Edgar Riba865a598c003b
diff --git a/kornia/augmentation/_2d/geometric/resize.py b/kornia/augmentation/_2d/geometric/resize.py
index 250825726..ab6618b5b 100644
--- a/kornia/augmentation/_2d/geometric/resize.py
+++ b/kornia/augmentation/_2d/geometric/resize.py
@@ -22,7 +22,6 @@ import torch
from kornia.augmentation import random_generator as rg
from kornia.augmentation._2d.geometric.base import GeometricAugmentationBase2D
from kornia.constants import Resample
-from kornia.core.ops import eye_like
from kornia.geometry.transform import crop_by_transform_mat, get_perspective_transform, resize
@@ -64,9 +63,12 @@ class Resize(GeometricAugmentationBase2D):
def compute_transformation(
self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]
) -> torch.Tensor:
- if params["output_size"] == input.shape[-2:]:
- return eye_like(3, input)
-
+ # NOTE: a former `if params["output_size"] == input.shape[-2:]: return eye_like(...)`
+ # short-circuit was dead code β€” comparing a tensor to a ``torch.Size`` falls back to
+ # identity ``==`` and is *always* Python ``False``, so the branch never ran. It also
+ # graph-broke torch.compile (a Python ``if`` on a would-be tensor). Dropped; the
+ # perspective transform below already yields identity when src == dst, so behaviour is
+ # byte-identical.
transform: torch.Tensor = torch.as_tensor(
get_perspective_transform(params["src"], params["dst"]), dtype=input.dtype, device=input.device
)
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index 41f016ffa..e07fb3c07 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -4677,12 +4677,9 @@ class TestResize:
assert out.shape == (1, 1, 4, 5)
assert aug.inverse(out).shape == (1, 1, 4, 6)
- @pytest.mark.xfail(
- reason="Resize.apply_transform still reads output_size via `.tolist()`, which breaks "
- "fullgraph under inductor on torch<2.10. Needs a static-size path (follow-up).",
- strict=False,
- )
def test_dynamo(self, device, dtype):
+ # A tuple output size resizes the whole batch in one call and the transform matrix is
+ # built without a data-dependent branch, so Resize is fullgraph-safe and matches eager.
img = torch.rand(3, 3, 20, 24, device=device, dtype=dtype)
aug = Resize(size=(16, 16))
torch._dynamo.reset()