π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): support empty accelerator transforms (#4185)
Yusef Syedacb861c31e51
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 051f17490..04eacaf3c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -365,6 +365,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0* `VisualPrompter.predict()` called without any prompt (the "run the prediction without prompts" example on theSegment Anything page) raised `TypeError: object of type 'NoneType' has no len()` from the prompt augmentationcontainer; it now predicts from the image embedding alone, as documented. (#4178)++* Preserve empty color tensors on accelerator backends instead of asking `reshape` to infer an ambiguous batch+ dimension. This lets the YUV420 and YUV422 empty-input conversions return their documented empty RGB output on+ MPS, matching CPU behavior (#4185).+* Follow-up fixes to the documentation revamp (#4155): the landing page's filtering card quoted the combined`filters`/`color`/`enhance`/`morphology` count next to a `kornia.filters` label, the "Why Kornia?" hero carouselresumed after a visitor picked a tab, the Community page linked to the now-unregistered `librecv.org`, and thediff --git a/kornia/color/utils.py b/kornia/color/utils.pyindex f48904945..a6e57d980 100644--- a/kornia/color/utils.py+++ b/kornia/color/utils.py@@ -49,8 +49,9 @@ def _apply_linear_transformation(# Empirical benchmarks show that einsum is faster on CPU for this specific pattern,# while conv2d offers significant speedups on GPU/CUDA.# We branch to ensure optimal performance on both devices.- # BRANCH 1: CPU (Einsum)- if image_compute.device.type == "cpu":+ # BRANCH 1: CPU and empty tensors (Einsum)+ # Einsum handles every empty shape and keeps image, kernel, and bias in the graph.+ if image_compute.device.type == "cpu" or image_compute.numel() == 0:out = torch.einsum("oi, ...ihw -> ...ohw", kernel_compute, image_compute)if bias_compute is not None:diff --git a/testing/known_failure_xfails/mps_float32.txt b/testing/known_failure_xfails/mps_float32.txtindex bbd84a495..670444fe1 100644--- a/testing/known_failure_xfails/mps_float32.txt+++ b/testing/known_failure_xfails/mps_float32.txt@@ -1,20 +1,17 @@# Exact failure baseline recorded from# https://github.com/kornia/kornia/actions/runs/33808269822/job/100824208527 -- that job ran-# without --xfail-known-failures, so it reports these 14 as failures: it is the run the baseline+# without --xfail-known-failures, so it reports these 12 as failures: it is the run the baseline# was recorded from, not a replay of this file.# macos-15-arm64, Python 3.13, PyTorch 2.14.0, MPS float32, no CPU fallback,# one pytest process for the full suite (tests marked device_agnostic are deselected on an mps-only run).# Remove an entry when it becomes a strict XPASS; fix or explicitly add any new failure. Tracked in #4159.# torch 2.14.0 added MPS kernels for the eigen/QR/LU/SVD family and for both grid_sample backward-# passes, which took this baseline from 200 entries to 14. Two causes remain:+# passes, which took this baseline from 200 entries to 12. One cause remains:# NotImplementedError aten::_linalg_eigvals has no MPS kernel (the 5-point essential solver).-# RuntimeError yuv420/yuv422 empty-input reshape, a kornia degenerate-shape defect.# tests/geometry/test_ransac.py is skipped on MPS, not pinned: it aborts the process (#4204).# Keep the entries sorted by node ID: KnownFailureTracker prints its regeneration output sorted,# so an unsorted file turns the next re-record into a whole-file diff.# Format: <exact exception type>\t<pytest node ID>-RuntimeError tests/color/test_yuv.py::TestYuv420ToRgb::test_empty_input[mps-float32]-RuntimeError tests/color/test_yuv.py::TestYuv422ToRgb::test_empty_input[mps-float32]NotImplementedError tests/geometry/epipolar/test_essential.py::TestFindEssential::test_degenerate_case[mps-float32-10-5]NotImplementedError tests/geometry/epipolar/test_essential.py::TestFindEssential::test_degenerate_case[mps-float32-5-5]NotImplementedError tests/geometry/epipolar/test_essential.py::TestFindEssential::test_epipolar_constraint[mps-float32]diff --git a/tests/color/test_utils.py b/tests/color/test_utils.pyindex b81095189..c7cb68b0e 100644--- a/tests/color/test_utils.py+++ b/tests/color/test_utils.py@@ -41,3 +41,20 @@ class TestApplyLinearTransformation(BaseTester):assert actual.dtype == torch.float32assert actual.is_contiguous()self.assert_close(actual, expected, atol=0.0, rtol=0.0)++ def test_empty_input_autograd(self, device):+ for shape in ((0, 3, 4, 4), (2, 3, 0, 4), (2, 0, 3, 4, 4)):+ image = torch.zeros(shape, device=device, dtype=torch.float32, requires_grad=True)+ kernel = torch.eye(3, device=device, dtype=torch.float32, requires_grad=True)+ bias = torch.zeros(3, device=device, dtype=torch.float32, requires_grad=True)++ actual = _apply_linear_transformation(image, kernel, bias)+ actual.sum().backward()++ assert actual.shape == image.shape+ assert actual.is_contiguous()+ assert image.grad is not None+ assert kernel.grad is not None+ assert bias.grad is not None+ self.assert_close(kernel.grad, torch.zeros_like(kernel))+ self.assert_close(bias.grad, torch.zeros_like(bias))