Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): match full-path exception type on empty-dsize warp paths (#4076)

Himesh Rupchandani36c13592af18
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e30dd2bd..e31ded146 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* Raise `NotImplementedError` instead of `RuntimeError` for an integral `src` on the empty-`dsize` paths of
+ `warp_affine`, `warp_perspective`, `remap`, `warp_affine3d` and `warp_perspective3d`, matching what the non-empty path already raises from
+ `grid_sample` (#4031). The degenerate path had its own explicit guard that fired first with a different exception
+ type, so the exception a caller saw for the same invalid input depended on whether `dsize` had a zero dimension.
+ The message is unchanged. `NotImplementedError` derives from `RuntimeError`, so `except RuntimeError` around these
+ functions still catches it; only code matching on the exact type sees a difference.
+
* Make `yuv_to_rgb` the exact inverse of `rgb_to_yuv`, so an RGB β†’ YUV β†’ RGB round trip is now limited only by the
input dtype instead of losing up to `1.36e-3` (in B, at `rgb = (1, 1, 0)`) at every precision, `float64` included
(#4044). The inverse kernel was a separately rounded copy of the published BT.470-5 M/PAL inverse relations rather
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 1f5d6e550..034c6ec4d 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -120,7 +120,7 @@ def _empty_warp_output_2d(
# rejecting it. Matching that would mean bilinear-sampling into an integer output, so the
# cpu/cuda contract is the one enforced here.)
if not src.is_floating_point():
- raise RuntimeError(f"Expected a floating point src, got {src.dtype}.")
+ raise NotImplementedError(f"Expected a floating point src, got {src.dtype}.")
# ``grid_sample`` requires the sampling grid in ``src.dtype``. ``grid_dtype`` is the dtype the
# caller's non-empty pipeline would actually produce from ``transform``, so checking it here
# makes a zero-sized ``dsize`` neither stricter nor laxer than a non-empty one.
@@ -162,7 +162,7 @@ def _empty_warp_output_3d(
# As in the 2-D helper: an integral ``src`` cannot reach cpu/cuda ``grid_sample`` on either
# path, so reject it by name instead of via the transform-dtype message below.
if not src.is_floating_point():
- raise RuntimeError(f"Expected a floating point src, got {src.dtype}.")
+ raise NotImplementedError(f"Expected a floating point src, got {src.dtype}.")
if src.dtype != transform.dtype:
raise RuntimeError(f"Expected src and transform with the same dtype, got {src.dtype} and {transform.dtype}.")
grid_zero = transform.reshape(-1)[:1].sum() * 0.0
diff --git a/tests/geometry/transform/test_imgwarp.py b/tests/geometry/transform/test_imgwarp.py
index 37dcfd36c..d422ff7cc 100644
--- a/tests/geometry/transform/test_imgwarp.py
+++ b/tests/geometry/transform/test_imgwarp.py
@@ -140,11 +140,14 @@ def test_empty_destination_blames_an_integral_src_by_name(op_name, device):
src = torch.zeros(1, 3, 3, 4, device=device, dtype=torch.int64)
matrix = torch.eye(3, device=device)[:rows].unsqueeze(0)
- with pytest.raises(RuntimeError, match="floating point src"):
- op(src, matrix, (0, 4))
- # The non-empty path rejects it too, so the empty guard is not stricter.
- with pytest.raises((RuntimeError, NotImplementedError)):
+ # Both paths must reject it with the same exception type. ``grid_sample`` raises
+ # ``NotImplementedError`` on newer torch and ``RuntimeError`` on older ones, so pin the
+ # parity rather than a fixed class: catch whatever the full path raises, then require the
+ # empty path to raise something the same ``except`` clause would catch.
+ with pytest.raises(RuntimeError) as full:
op(src, matrix, (1, 4))
+ with pytest.raises(type(full.value), match="floating point src"):
+ op(src, matrix, (0, 4))
@pytest.mark.parametrize("op_name", ["warp_affine", "warp_perspective"])
diff --git a/tests/geometry/transform/test_imgwarp3d.py b/tests/geometry/transform/test_imgwarp3d.py
index f2f4cf145..a6d0b0f0d 100644
--- a/tests/geometry/transform/test_imgwarp3d.py
+++ b/tests/geometry/transform/test_imgwarp3d.py
@@ -76,6 +76,27 @@ def test_negative_destination_raises(op_name, device, dtype):
op(src, transform, (-1, 4, 5))
[email protected]("op_name", ["warp_affine3d", "warp_perspective3d"])
+def test_empty_destination_blames_an_integral_src_by_name(op_name, device):
+ """``grid_sample`` rejects an integral volume on both paths; the message must name ``src``.
+
+ Scoped away from MPS, whose ``grid_sample`` accepts an integral image and samples it back into
+ ``int64`` instead of rejecting it, so the non-empty half of the pairing does not hold there.
+ """
+ if device.type == "mps":
+ pytest.skip("MPS grid_sample accepts an integral image instead of rejecting it")
+ op = getattr(proj, op_name)
+ matrix_size = (3, 4) if op_name == "warp_affine3d" else (4, 4)
+ src = torch.zeros(1, 2, 3, 4, 5, device=device, dtype=torch.int64)
+ matrix = torch.eye(*matrix_size, device=device).unsqueeze(0)
+
+ # The non-empty path rejects it too, so the empty guard is not stricter.
+ with pytest.raises(RuntimeError) as full:
+ op(src, matrix, (1, 4, 5))
+ with pytest.raises(type(full.value), match="floating point src"):
+ op(src, matrix, (0, 4, 5))
+
+
@pytest.mark.parametrize("op_name", ["warp_affine3d", "warp_perspective3d"])
def test_empty_destination_keeps_grid_sample_validation(op_name, device, dtype):
src = torch.rand(2, 2, 3, 4, 5, device=device, dtype=dtype)