Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Preserve KMeans accumulation precision under autocast (#6597)

A Aswanth Rajf50e332913e6
diff --git a/changelog.d/6597.fixed.md b/changelog.d/6597.fixed.md
new file mode 100644
index 000000000..2183b2e1b
--- /dev/null
+++ b/changelog.d/6597.fixed.md
@@ -0,0 +1 @@
+`KMeans.fit` preserves its promoted accumulation dtype under autocast, preventing infinite or biased cluster centers for unit-range data.
diff --git a/kornia/contrib/kmeans.py b/kornia/contrib/kmeans.py
index 94beeebd0..fe32e3708 100644
--- a/kornia/contrib/kmeans.py
+++ b/kornia/contrib/kmeans.py
@@ -24,6 +24,7 @@ import operator
import torch
from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_SIZE, KORNIA_CHECK_SHAPE
+from kornia.core.utils import _autocast_disabled
from kornia.geometry.linalg import euclidean_distance
@@ -259,7 +260,8 @@ class KMeans:
one_hot = torch.nn.functional.one_hot(cluster_assignment, num_classes=self.num_clusters)
cluster_counts = one_hot.sum(0)
if work_dtype.is_floating_point:
- cluster_sums = one_hot.to(work_dtype).t() @ X_work
+ with _autocast_disabled(X.device.type):
+ cluster_sums = one_hot.to(work_dtype).t() @ X_work
else:
# integer sums are exact with scatter_add_; MPS lacks integer matmul and index_add_ on torch 2.5
index = cluster_assignment[:, None].expand(-1, X_work.shape[1])
diff --git a/tests/contrib/test_kmeans.py b/tests/contrib/test_kmeans.py
index 0ea64edd3..bd25158b0 100644
--- a/tests/contrib/test_kmeans.py
+++ b/tests/contrib/test_kmeans.py
@@ -231,6 +231,21 @@ class TestKMeans(BaseTester):
# pre-fix bfloat16 error (0.25, well under BaseTester's default bfloat16 tolerance) pass
self.assert_close(kmeans.cluster_centers, expected.to(device=device, dtype=dtype), rtol=0.0, atol=0.0)
+ @pytest.mark.parametrize("autocast_dtype", [torch.float16, torch.bfloat16])
+ def test_autocast_preserves_cluster_sum_precision(self, device, dtype, autocast_dtype):
+ """Keep centroid accumulation in the work dtype under autocast."""
+ if device.type not in ("cpu", "cuda"):
+ pytest.skip("autocast precision regression is exercised on CPU and CUDA")
+ if device.type == "cuda" and autocast_dtype == torch.bfloat16 and not torch.cuda.is_bf16_supported():
+ pytest.skip("CUDA device does not support bfloat16 autocast")
+ x = torch.full((100000, 3), 0.75, device=device, dtype=dtype)
+ kmeans = kornia.contrib.KMeans(1, torch.zeros_like(x[:1]), tolerance=None, max_iterations=1)
+
+ with torch.autocast(device_type=device.type, dtype=autocast_dtype):
+ kmeans.fit(x)
+
+ self.assert_close(kmeans.cluster_centers, x[:1], rtol=0.0, atol=0.0)
+
@staticmethod
def _create_data(device, dtype):
# create example dataset