fast-clustering.cc
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// sherpa-onnx/python/csrc/fast-clustering.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/python/csrc/fast-clustering.h"
#include <sstream>
#include <vector>
#include "sherpa-onnx/csrc/fast-clustering.h"
#define C_CONTIGUOUS py::detail::npy_api::constants::NPY_ARRAY_C_CONTIGUOUS_
namespace sherpa_onnx {
static void PybindFastClusteringConfig(py::module *m) {
using PyClass = FastClusteringConfig;
py::class_<PyClass>(*m, "FastClusteringConfig")
.def(py::init<int32_t, float>(), py::arg("num_clusters") = -1,
py::arg("threshold") = 0.5)
.def_readwrite("num_clusters", &PyClass::num_clusters)
.def_readwrite("threshold", &PyClass::threshold)
.def("__str__", &PyClass::ToString)
.def("validate", &PyClass::Validate);
}
void PybindFastClustering(py::module *m) {
PybindFastClusteringConfig(m);
using PyClass = FastClustering;
py::class_<PyClass>(*m, "FastClustering")
.def(py::init<const FastClusteringConfig &>(), py::arg("config"))
.def(
"__call__",
[](const PyClass &self,
py::array_t<float> features) -> std::vector<int32_t> {
if (!(C_CONTIGUOUS == (features.flags() & C_CONTIGUOUS))) {
throw py::value_error(
"input features should be contiguous. Please use "
"np.ascontiguousarray(features)");
}
int num_dim = features.ndim();
if (num_dim != 2) {
std::ostringstream os;
os << "Expect an array of 2 dimensions. Given dim: " << num_dim
<< "\n";
throw py::value_error(os.str());
}
int32_t num_rows = features.shape(0);
int32_t num_cols = features.shape(1);
float *p = features.mutable_data();
py::gil_scoped_release release;
return self.Cluster(p, num_rows, num_cols);
},
py::arg("features"));
}
} // namespace sherpa_onnx