From 087e4c3dcff27604bfc28e4bba2d1b35c78a5908 Mon Sep 17 00:00:00 2001 From: carey-bk <180496115+carey-bk@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:40:07 +0800 Subject: [PATCH] fix(campplus): convert tensors before UMAP clustering --- funasr/models/campplus/cluster_backend.py | 2 ++ tests/test_numpy_cluster_compatibility.py | 26 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/funasr/models/campplus/cluster_backend.py b/funasr/models/campplus/cluster_backend.py index e6c4ca64dc..9055f659f3 100644 --- a/funasr/models/campplus/cluster_backend.py +++ b/funasr/models/campplus/cluster_backend.py @@ -263,6 +263,8 @@ def forward(self, X, **params): elif k is not None: labels = self.kmeans_cluster(X, k) else: + if isinstance(X, torch.Tensor): + X = X.detach().cpu().numpy() labels = self.umap_hdbscan_cluster(X) labels = self.merge_to_max_spks(labels, X, self.spectral_cluster.max_num_spks) diff --git a/tests/test_numpy_cluster_compatibility.py b/tests/test_numpy_cluster_compatibility.py index 6389847c16..34b83af7e1 100644 --- a/tests/test_numpy_cluster_compatibility.py +++ b/tests/test_numpy_cluster_compatibility.py @@ -2,6 +2,7 @@ import numpy as np import pytest +import torch from funasr.models.campplus.cluster_backend import ClusterBackend, UmapHdbscan @@ -79,3 +80,28 @@ def test_umap_hdbscan_preserves_separated_speakers(): numba.set_num_threads(previous_threads) _assert_same_partition(labels, expected) np.testing.assert_array_equal(embeddings, original) + + +@pytest.mark.parametrize("input_kind", ["numpy", "tensor", "tensor_with_grad"]) +def test_large_backend_umap_accepts_numpy_and_tensor(input_kind): + import numba + + # Reach the production UMAP dispatcher with four well-separated speakers. + expected = np.repeat(np.arange(4), 512) + rng = np.random.default_rng(0) + embeddings = np.eye(4, 192, dtype=np.float32)[expected] + embeddings += rng.normal(0, 0.01, embeddings.shape).astype(np.float32) + original = embeddings.copy() + inputs = embeddings + if input_kind != "numpy": + inputs = torch.from_numpy(embeddings).requires_grad_(input_kind == "tensor_with_grad") + + previous_threads = numba.get_num_threads() + numba.set_num_threads(1) + try: + labels = ClusterBackend()(inputs) + finally: + numba.set_num_threads(previous_threads) + + _assert_same_partition(labels, expected) + np.testing.assert_array_equal(embeddings, original)