Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions funasr/models/campplus/cluster_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions tests/test_numpy_cluster_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest
import torch

from funasr.models.campplus.cluster_backend import ClusterBackend, UmapHdbscan

Expand Down Expand Up @@ -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)
Loading