Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): bin Otsu pixels with a correctly rounded division under CUDA torch.compile (#6585)

Dmytro Mishkin1c84c52dbb4d
diff --git a/changelog.d/6585.fixed.md b/changelog.d/6585.fixed.md
new file mode 100644
index 000000000..2443c42d4
--- /dev/null
+++ b/changelog.d/6585.fixed.md
@@ -0,0 +1,3 @@
+`otsu_threshold` and `OtsuThreshold` under `torch.compile` on CUDA now put a pixel that lies on a bin edge in the same
+bin as eager. Triton's approximate float32 division used to place it in the bin below, which changed the mask at that
+pixel and could move the threshold by a whole bin.
diff --git a/kornia/filters/otsu_thresholding.py b/kornia/filters/otsu_thresholding.py
index 859531f7a..0f178adb2 100644
--- a/kornia/filters/otsu_thresholding.py
+++ b/kornia/filters/otsu_thresholding.py
@@ -80,7 +80,12 @@ class OtsuThreshold(torch.nn.Module):
min_values, max_values = min_values / scale, max_values / scale
widths = max_values - min_values
safe_widths = torch.where(widths > 0, widths, torch.ones_like(widths))
- indices = ((values - min_values) * bins / safe_widths).to(torch.int64).clamp(0, bins - 1)
+ # A pixel on a bin edge has a coordinate that is an integer only when the division is correctly rounded.
+ # Triton lowers float32 `/` to an approximate division, so under torch.compile on CUDA such a pixel can fall
+ # into the bin below; Inductor lowers addcdiv's division correctly rounded, and adding 0 is exact, so eager
+ # and compiled bin it alike.
+ coordinates = torch.addcdiv(values.new_zeros(()), (values - min_values) * bins, safe_widths)
+ indices = coordinates.to(torch.int64).clamp(0, bins - 1)
histograms = values.new_zeros((values.shape[0], bins)).scatter_add(1, indices, torch.ones_like(values))
# Match linspace's symmetric interpolation and exact end points with the arithmetic of `_upper_edge`'s