Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

perf(contrib): stop the MPS distance_transform cascade once it settles (#6274)

Dmytro Mishkin160245b7ff1e
diff --git a/changelog.d/6274.fixed.md b/changelog.d/6274.fixed.md
new file mode 100644
index 000000000..efd5872ee
--- /dev/null
+++ b/changelog.d/6274.fixed.md
@@ -0,0 +1,4 @@
+`distance_transform` on MPS now stops once its cascade settles, as on CPU and CUDA: an 8 x 1 x 256 x 256 blob mask takes
+9 ms on an M1 Pro, against 79 ms before and 12 ms in 0.8.3. Reading whether the cascade has settled costs MPS a few
+rounds' work, so it reads less often there; a cascade that cannot stop early, such as one from a single corner source,
+takes 5 % (512 x 512) to 20 % (64 x 64) longer than before. CPU and CUDA are unchanged.
diff --git a/kornia/contrib/distance_transform.py b/kornia/contrib/distance_transform.py
index 5e6e4fe2a..6f39e4559 100644
--- a/kornia/contrib/distance_transform.py
+++ b/kornia/contrib/distance_transform.py
@@ -18,6 +18,7 @@
from __future__ import annotations
import math
+from collections.abc import Collection
import torch
from torch import nn
@@ -107,15 +108,35 @@ def _can_stop_cascade(image: torch.Tensor) -> bool:
return False
if image.requires_grad and torch.is_grad_enabled():
return False
- # Only CPU and CUDA hold readable data here; meta, fake and lazy tensors do not, and reading a
- # flag would synchronize MPS. A CUDA graph capture cannot synchronize either.
- if image.device.type not in ("cpu", "cuda") or is_fake(image):
+ # Read the flag only on the eager backends measured here. Meta and fake tensors hold no data, and a lazy backend
+ # such as XLA would run its pending graph at every read. A CUDA graph capture cannot synchronize.
+ if image.device.type not in ("cpu", "cuda", "mps") or is_fake(image):
return False
if image.is_cuda and torch.cuda.is_current_stream_capturing():
return False
return torch.autograd.forward_ad.unpack_dual(image).tangent is None
+def _stop_check_rounds(n_iters: int, device_type: str) -> Collection[int]:
+ """Rounds after which an eager cascade on ``device_type`` reads whether it has settled."""
+ if device_type != "mps":
+ # Every eighth round amortizes the read, which synchronizes CUDA. A range, so that membership costs constant
+ # memory however long the image is.
+ return range(8, n_iters + 1, 8)
+ # On MPS a read drains the command queue, which cost an M1 0.6 to 0.8 ms, the work of two or three rounds. Read
+ # every eighth round, a cascade that cannot stop early took 23 % longer than with no reads at 8 x 1 x 256 x 256 and
+ # 30 % at 1 x 1 x 512 x 512. There the gap between reads is the larger of eight rounds and a quarter of the rounds
+ # done, which also bounds how far a settled cascade runs on, and no read falls in the last eight rounds, where it
+ # cannot save its cost: those cascades take 10 % and 5 % longer. Outside the last eight rounds, the reads up to
+ # round 40 fall where the CPU's do.
+ rounds = []
+ done = 8
+ while n_iters - done >= 8:
+ rounds.append(done)
+ done += max(8, done // 4)
+ return frozenset(rounds)
+
+
def _distance_transform_2d_impl(image: torch.Tensor, kernel_size: int, h: float) -> torch.Tensor:
device = image.device
dtype = image.dtype
@@ -137,7 +158,7 @@ def _distance_transform_2d_impl(image: torch.Tensor, kernel_size: int, h: float)
image > 1, torch.ones_like(image), torch.where(image < 0, torch.zeros_like(image), image)
).to(compute_dtype)
signal_ones = torch.ones_like(boundary)
- can_stop = _can_stop_cascade(image)
+ stop_checks = _stop_check_rounds(n_iters, image.device.type) if _can_stop_cascade(image) else ()
for i in range(n_iters):
cdt = filter2d(boundary, kernel, border_type="replicate")
@@ -148,8 +169,8 @@ def _distance_transform_2d_impl(image: torch.Tensor, kernel_size: int, h: float)
mask = cdt > 0
# Once the mask is empty the boundary cannot change, so every later iteration adds zero.
- # Check only every eight rounds to amortize the device synchronization in eager inference.
- if can_stop and (i + 1) % 8 == 0 and not bool(mask.any()):
+ # Check only at a few rounds to amortize the device synchronization in eager inference.
+ if i + 1 in stop_checks and not bool(mask.any()):
break
offset: int = i * k_half
out = out + (offset + cdt) * mask.to(dtype=out.dtype)
@@ -176,7 +197,7 @@ def _distance_transform_3d_impl(image: torch.Tensor, kernel_size: int, h: float)
image > 1, torch.ones_like(image), torch.where(image < 0, torch.zeros_like(image), image)
).to(compute_dtype)
signal_ones = torch.ones_like(boundary)
- can_stop = _can_stop_cascade(image)
+ stop_checks = _stop_check_rounds(n_iters, image.device.type) if _can_stop_cascade(image) else ()
for i in range(n_iters):
cdt = filter3d(boundary, kernel, border_type="replicate")
@@ -187,7 +208,7 @@ def _distance_transform_3d_impl(image: torch.Tensor, kernel_size: int, h: float)
mask = cdt > 0
# As in 2-D, retain a static loop for capture and differentiation, and amortize eager checks.
- if can_stop and (i + 1) % 8 == 0 and not bool(mask.any()):
+ if i + 1 in stop_checks and not bool(mask.any()):
break
offset: int = i * k_half
out = out + (offset + cdt) * mask.to(dtype=out.dtype)
diff --git a/tests/contrib/test_conv_distance_transformer.py b/tests/contrib/test_conv_distance_transformer.py
index 1d3b4de65..25f6f50b5 100644
--- a/tests/contrib/test_conv_distance_transformer.py
+++ b/tests/contrib/test_conv_distance_transformer.py
@@ -17,6 +17,7 @@
import importlib
import math
+import sys
import pytest
import torch
@@ -45,7 +46,7 @@ class TestConvDistanceTransform(BaseTester):
sample = torch.ones((1, 1) + (32,) * ndim, device=device, dtype=dtype)
output = kornia.contrib.distance_transform(sample)
self.assert_close(output, torch.zeros_like(sample))
- expected_calls = 8 if device.type in ("cpu", "cuda") else 32
+ expected_calls = 8 if device.type in ("cpu", "cuda", "mps") else 32
assert len(calls) == expected_calls
@pytest.mark.parametrize("ndim", [2, 3])
@@ -109,9 +110,25 @@ class TestConvDistanceTransform(BaseTester):
with torch.no_grad():
output = kornia.contrib.distance_transform(sample)
self.assert_close(output, torch.zeros_like(sample))
- expected_calls = 8 if device.type in ("cpu", "cuda") else 32
+ expected_calls = 8 if device.type in ("cpu", "cuda", "mps") else 32
assert len(calls) == expected_calls
+ @pytest.mark.device_agnostic
+ def test_settled_check_rounds(self):
+ # Reading whether the cascade settled synchronizes the device. CPU and CUDA read every eighth round; MPS, where
+ # a read costs a few rounds' work, reads at most a quarter of the rounds done apart and not in the last eight.
+ module = importlib.import_module("kornia.contrib.distance_transform")
+ assert sorted(module._stop_check_rounds(32, "cpu")) == [8, 16, 24, 32]
+ assert sorted(module._stop_check_rounds(17, "cuda")) == [8, 16]
+ assert sorted(module._stop_check_rounds(15, "mps")) == []
+ assert sorted(module._stop_check_rounds(17, "mps")) == [8]
+ assert sorted(module._stop_check_rounds(32, "mps")) == [8, 16, 24]
+ assert sorted(module._stop_check_rounds(256, "mps")) == [8, 16, 24, 32, 40, 50, 62, 77, 96, 120, 150, 187, 233]
+ # The schedule takes memory for the rounds it reads, not for every possible one: a set of all 131,072 reads of a
+ # 1 x 1 x 1 x 1,048,576 image took 7.9 MB.
+ assert sys.getsizeof(module._stop_check_rounds(2**20, "cpu")) < 1024
+ assert len(module._stop_check_rounds(2**20, "mps")) < 100
+
@pytest.mark.device_agnostic
@pytest.mark.parametrize("ndim", [2, 3])
def test_shape_only_tensors(self, ndim):