diff --git a/c/src/cluster/kmeans.cpp b/c/src/cluster/kmeans.cpp index a84cd50259..96760dc179 100644 --- a/c/src/cluster/kmeans.cpp +++ b/c/src/cluster/kmeans.cpp @@ -83,7 +83,7 @@ void _fit(cuvsResources_t res, } T inertia_temp; - IdxT n_iter_temp; + int n_iter_temp; auto kmeans_params = convert_params(params); cuvs::cluster::kmeans::fit(*res_ptr, @@ -92,7 +92,7 @@ void _fit(cuvsResources_t res, sample_weight, centroids_view, raft::make_host_scalar_view(&inertia_temp), - raft::make_host_scalar_view(&n_iter_temp)); + raft::make_host_scalar_view(&n_iter_temp)); *inertia = inertia_temp; *n_iter = n_iter_temp; @@ -118,7 +118,7 @@ void _fit(cuvsResources_t res, } } else { T inertia_temp; - IdxT n_iter_temp; + int n_iter_temp; std::optional> sample_weight; if (sample_weight_tensor != NULL) { @@ -133,14 +133,14 @@ void _fit(cuvsResources_t res, sample_weight, cuvs::core::from_dlpack(centroids_tensor), raft::make_host_scalar_view(&inertia_temp), - raft::make_host_scalar_view(&n_iter_temp)); + raft::make_host_scalar_view(&n_iter_temp)); *inertia = inertia_temp; *n_iter = n_iter_temp; } } } -template +template void _predict(cuvsResources_t res, const cuvsKMeansParams& params, DLManagedTensor* X_tensor, diff --git a/cpp/include/cuvs/cluster/kmeans.hpp b/cpp/include/cuvs/cluster/kmeans.hpp index d299d9f483..c93bdd5a13 100644 --- a/cpp/include/cuvs/cluster/kmeans.hpp +++ b/cpp/include/cuvs/cluster/kmeans.hpp @@ -220,7 +220,7 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); /** * @brief Find clusters with k-means algorithm using batched processing of host data. @@ -231,56 +231,6 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); - -/** - * @brief Find clusters with k-means algorithm. - * Initial centroids are chosen with k-means++ algorithm. Empty - * clusters are reinitialized by choosing new centroids with - * k-means++ algorithm. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::params params; - * int n_features = 15, inertia, n_iter; - * auto centroids = raft::make_device_matrix(handle, params.n_clusters, n_features); - * - * kmeans::fit(handle, - * params, - * X, - * std::nullopt, - * centroids, - * raft::make_scalar_view(&inertia), - * raft::make_scalar_view(&n_iter)); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X Training instances to cluster. The data must - * be in row-major format. - * [dim = n_samples x n_features] - * @param[in] sample_weight Optional weights for each observation in X. - * [len = n_samples] - * @param[inout] centroids [in] When init is InitMethod::Array, use - * centroids as the initial cluster centers. - * [out] The generated centroids from the - * kmeans algorithm are stored at the address - * pointed by 'centroids'. - * [dim = n_clusters x n_features] - * @param[out] inertia Sum of squared distances of samples to their - * closest cluster center. - * @param[out] n_iter Number of iterations run. - */ -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, raft::host_scalar_view n_iter); /** @@ -332,56 +282,6 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); - -/** - * @brief Find clusters with k-means algorithm. - * Initial centroids are chosen with k-means++ algorithm. Empty - * clusters are reinitialized by choosing new centroids with - * k-means++ algorithm. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::params params; - * int n_features = 15, inertia, n_iter; - * auto centroids = raft::make_device_matrix(handle, params.n_clusters, n_features); - * - * kmeans::fit(handle, - * params, - * X, - * std::nullopt, - * centroids, - * raft::make_scalar_view(&inertia), - * raft::make_scalar_view(&n_iter)); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X Training instances to cluster. The data must - * be in row-major format. - * [dim = n_samples x n_features] - * @param[in] sample_weight Optional weights for each observation in X. - * [len = n_samples] - * @param[inout] centroids [in] When init is InitMethod::Array, use - * centroids as the initial cluster centers. - * [out] The generated centroids from the - * kmeans algorithm are stored at the address - * pointed by 'centroids'. - * [dim = n_clusters x n_features] - * @param[out] inertia Sum of squared distances of samples to their - * closest cluster center. - * @param[out] n_iter Number of iterations run. - */ -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, raft::host_scalar_view n_iter); /** @@ -433,7 +333,7 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); /** * @brief Find clusters with k-means algorithm. @@ -634,70 +534,12 @@ void fit(const raft::resources& handle, raft::device_matrix_view centroids, std::optional> inertia = std::nullopt); -/** - * @brief Predict the closest cluster each sample in X belongs to. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::params params; - * int n_features = 15, inertia, n_iter; - * auto centroids = raft::make_device_matrix(handle, params.n_clusters, n_features); - * - * kmeans::fit(handle, - * params, - * X, - * std::nullopt, - * centroids.view(), - * raft::make_scalar_view(&inertia), - * raft::make_scalar_view(&n_iter)); - * ... - * auto labels = raft::make_device_vector(handle, X.extent(0)); - * - * kmeans::predict(handle, - * params, - * X, - * std::nullopt, - * centroids.view(), - * false, - * labels.view(), - * raft::make_scalar_view(&inertia)); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X New data to predict. - * [dim = n_samples x n_features] - * @param[in] sample_weight Optional weights for each observation in X. - * [len = n_samples] - * @param[in] centroids Cluster centroids. The data must be in - * row-major format. - * [dim = n_clusters x n_features] - * @param[in] normalize_weight True if the weights should be normalized - * @param[out] labels Index of the cluster each sample in X - * belongs to. - * [len = n_samples] - * @param[out] inertia Sum of squared distances of samples to - * their closest cluster center. - */ -void predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::device_vector_view labels, - bool normalize_weight, - raft::host_scalar_view inertia); - void predict(raft::resources const& handle, const kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia); @@ -722,7 +564,7 @@ void predict(raft::resources const& handle, * raft::make_scalar_view(&inertia), * raft::make_scalar_view(&n_iter)); * ... - * auto labels = raft::make_device_vector(handle, X.extent(0)); + * auto labels = raft::make_device_vector(handle, X.extent(0)); * * kmeans::predict(handle, * params, @@ -755,7 +597,7 @@ void predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia); @@ -780,65 +622,7 @@ void predict(raft::resources const& handle, * raft::make_scalar_view(&inertia), * raft::make_scalar_view(&n_iter)); * ... - * auto labels = raft::make_device_vector(handle, X.extent(0)); - * - * kmeans::predict(handle, - * params, - * X, - * std::nullopt, - * centroids.view(), - * false, - * labels.view(), - * raft::make_scalar_view(&inertia)); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X New data to predict. - * [dim = n_samples x n_features] - * @param[in] sample_weight Optional weights for each observation in X. - * [len = n_samples] - * @param[in] centroids Cluster centroids. The data must be in - * row-major format. - * [dim = n_clusters x n_features] - * @param[in] normalize_weight True if the weights should be normalized - * @param[out] labels Index of the cluster each sample in X - * belongs to. - * [len = n_samples] - * @param[out] inertia Sum of squared distances of samples to - * their closest cluster center. - */ -void predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::device_vector_view labels, - bool normalize_weight, - raft::host_scalar_view inertia); - -/** - * @brief Predict the closest cluster each sample in X belongs to. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::params params; - * int n_features = 15, inertia, n_iter; - * auto centroids = raft::make_device_matrix(handle, params.n_clusters, n_features); - * - * kmeans::fit(handle, - * params, - * X, - * std::nullopt, - * centroids.view(), - * raft::make_scalar_view(&inertia), - * raft::make_scalar_view(&n_iter)); - * ... - * auto labels = raft::make_device_vector(handle, X.extent(0)); + * auto labels = raft::make_device_vector(handle, X.extent(0)); * * kmeans::predict(handle, * params, @@ -871,7 +655,7 @@ void predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia); @@ -919,94 +703,6 @@ void predict(const raft::resources& handle, raft::device_matrix_view centroids, raft::device_vector_view labels); -/** - * @brief Predict the closest cluster each sample in X belongs to. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::balanced_params params; - * int64_t n_features = 15, n_clusters = 8; - * auto centroids = raft::make_device_matrix(handle, n_clusters, n_features); - * - * kmeans::fit(handle, - * params, - * X, - * centroids.view()); - * ... - * auto labels = raft::make_device_vector(handle, X.extent(0)); - * - * kmeans::predict(handle, - * params, - * X, - * centroids.view(), - * labels.view()); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X New data to predict. - * [dim = n_samples x n_features] - * @param[in] centroids Cluster centroids. The data must be in - * row-major format. - * [dim = n_clusters x n_features] - * @param[out] labels Index of the cluster each sample in X - * belongs to. - * [len = n_samples] - */ -void predict(const raft::resources& handle, - cuvs::cluster::kmeans::balanced_params const& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_vector_view labels); - -/** - * @brief Predict the closest cluster each sample in X belongs to. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::balanced_params params; - * int64_t n_features = 15, n_clusters = 8; - * auto centroids = raft::make_device_matrix(handle, n_clusters, n_features); - * - * kmeans::fit(handle, - * params, - * X, - * centroids.view()); - * ... - * auto labels = raft::make_device_vector(handle, X.extent(0)); - * - * kmeans::predict(handle, - * params, - * X, - * centroids.view(), - * labels.view()); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X New data to predict. - * [dim = n_samples x n_features] - * @param[in] centroids Cluster centroids. The data must be in - * row-major format. - * [dim = n_clusters x n_features] - * @param[out] labels Index of the cluster each sample in X - * belongs to. - * [len = n_samples] - */ -void predict(const raft::resources& handle, - cuvs::cluster::kmeans::balanced_params const& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_vector_view labels); - /** * @brief Predict the closest cluster each sample in X belongs to. * @@ -1139,61 +835,6 @@ void predict(const raft::resources& handle, raft::device_matrix_view centroids, raft::device_vector_view labels); -/** - * @brief Compute k-means clustering and predicts cluster index for each sample - * in the input. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::params params; - * int n_features = 15, inertia, n_iter; - * auto centroids = raft::make_device_matrix(handle, params.n_clusters, n_features); - * auto labels = raft::make_device_vector(handle, X.extent(0)); - * - * kmeans::fit_predict(handle, - * params, - * X, - * std::nullopt, - * centroids.view(), - * labels.view(), - * raft::make_scalar_view(&inertia), - * raft::make_scalar_view(&n_iter)); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X Training instances to cluster. The data must be - * in row-major format. - * [dim = n_samples x n_features] - * @param[in] sample_weight Optional weights for each observation in X. - * [len = n_samples] - * @param[inout] centroids Optional - * [in] When init is InitMethod::Array, use - * centroids as the initial cluster centers - * [out] The generated centroids from the - * kmeans algorithm are stored at the address - * pointed by 'centroids'. - * [dim = n_clusters x n_features] - * @param[out] labels Index of the cluster each sample in X belongs - * to. - * [len = n_samples] - * @param[out] inertia Sum of squared distances of samples to their - * closest cluster center. - * @param[out] n_iter Number of iterations run. - */ -void fit_predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - std::optional> centroids, - raft::device_vector_view labels, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); - /** * @brief Compute k-means clustering and predicts cluster index for each sample * in the input. @@ -1207,7 +848,7 @@ void fit_predict(raft::resources const& handle, * cuvs::cluster::kmeans::params params; * int64_t n_features = 15, inertia, n_iter; * auto centroids = raft::make_device_matrix(handle, params.n_clusters, - * n_features); auto labels = raft::make_device_vector(handle, X.extent(0)); + * n_features); auto labels = raft::make_device_vector(handle, X.extent(0)); * * kmeans::fit_predict(handle, * params, @@ -1245,63 +886,8 @@ void fit_predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, std::optional> centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); - -/** - * @brief Compute k-means clustering and predicts cluster index for each sample - * in the input. - * - * @code{.cpp} - * #include - * #include - * using namespace cuvs::cluster; - * ... - * raft::resources handle; - * cuvs::cluster::kmeans::params params; - * int n_features = 15, inertia, n_iter; - * auto centroids = raft::make_device_matrix(handle, params.n_clusters, n_features); - * auto labels = raft::make_device_vector(handle, X.extent(0)); - * - * kmeans::fit_predict(handle, - * params, - * X, - * std::nullopt, - * centroids.view(), - * labels.view(), - * raft::make_scalar_view(&inertia), - * raft::make_scalar_view(&n_iter)); - * @endcode - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X Training instances to cluster. The data must be - * in row-major format. - * [dim = n_samples x n_features] - * @param[in] sample_weight Optional weights for each observation in X. - * [len = n_samples] - * @param[inout] centroids Optional - * [in] When init is InitMethod::Array, use - * centroids as the initial cluster centers - * [out] The generated centroids from the - * kmeans algorithm are stored at the address - * pointed by 'centroids'. - * [dim = n_clusters x n_features] - * @param[out] labels Index of the cluster each sample in X belongs - * to. - * [len = n_samples] - * @param[out] inertia Sum of squared distances of samples to their - * closest cluster center. - * @param[out] n_iter Number of iterations run. - */ -void fit_predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - std::optional> centroids, - raft::device_vector_view labels, - raft::host_scalar_view inertia, raft::host_scalar_view n_iter); /** @@ -1317,7 +903,7 @@ void fit_predict(raft::resources const& handle, * cuvs::cluster::kmeans::params params; * int64_t n_features = 15, inertia, n_iter; * auto centroids = raft::make_device_matrix(handle, params.n_clusters, - * n_features); auto labels = raft::make_device_vector(handle, X.extent(0)); + * n_features); auto labels = raft::make_device_vector(handle, X.extent(0)); * * kmeans::fit_predict(handle, * params, @@ -1355,9 +941,9 @@ void fit_predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, std::optional> centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); /** * @brief Compute balanced k-means clustering and predicts cluster index for each sample @@ -1372,7 +958,7 @@ void fit_predict(raft::resources const& handle, * cuvs::cluster::kmeans::balanced_params params; * int64_t n_features = 15, n_clusters = 8; * auto centroids = raft::make_device_matrix(handle, n_clusters, n_features); - * auto labels = raft::make_device_vector(handle, X.extent(0)); + * auto labels = raft::make_device_vector(handle, X.extent(0)); * * kmeans::fit_predict(handle, * params, @@ -1416,7 +1002,7 @@ void fit_predict(const raft::resources& handle, * cuvs::cluster::kmeans::balanced_params params; * int64_t n_features = 15, n_clusters = 8; * auto centroids = raft::make_device_matrix(handle, n_clusters, n_features); - * auto labels = raft::make_device_vector(handle, X.extent(0)); + * auto labels = raft::make_device_vector(handle, X.extent(0)); * * kmeans::fit_predict(handle, * params, @@ -1447,87 +1033,6 @@ void fit_predict(const raft::resources& handle, raft::device_matrix_view centroids, raft::device_vector_view labels); -/** - * @brief Transform X to a cluster-distance space. - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X Training instances to cluster. The data must - * be in row-major format - * [dim = n_samples x n_features] - * @param[in] centroids Cluster centroids. The data must be in row-major format. - * [dim = n_clusters x n_features] - * @param[out] X_new X transformed in the new space. - * [dim = n_samples x n_features] - */ -void transform(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_matrix_view X_new); - -/** - * @brief Transform X to a cluster-distance space. - * - * @param[in] handle The raft handle. - * @param[in] params Parameters for KMeans model. - * @param[in] X Training instances to cluster. The data must - * be in row-major format - * [dim = n_samples x n_features] - * @param[in] centroids Cluster centroids. The data must be in row-major format. - * [dim = n_clusters x n_features] - * @param[out] X_new X transformed in the new space. - * [dim = n_samples x n_features] - */ -void transform(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_matrix_view X_new); - -/** - * @brief Compute (optionally weighted) cluster cost - * - * @param[in] handle The raft handle - * @param[in] X Training instances to cluster. The data must - * be in row-major format. - * [dim = n_samples x n_features] - * @param[in] centroids Cluster centroids. The data must be in - * row-major format. - * [dim = n_clusters x n_features] - * @param[out] cost Resulting cluster cost - * @param[in] sample_weight Optional per-sample weights. - * [len = n_samples] - * - */ -void cluster_cost( - const raft::resources& handle, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::host_scalar_view cost, - std::optional> sample_weight = std::nullopt); - -/** - * @brief Compute cluster cost - * - * @param[in] handle The raft handle - * @param[in] X Training instances to cluster. The data must - * be in row-major format. - * [dim = n_samples x n_features] - * @param[in] centroids Cluster centroids. The data must be in - * row-major format. - * [dim = n_clusters x n_features] - * @param[out] cost Resulting cluster cost - * @param[in] sample_weight Optional per-sample weights. - * [len = n_samples] - */ -void cluster_cost( - const raft::resources& handle, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::host_scalar_view cost, - std::optional> sample_weight = std::nullopt); - /** * @brief Compute (optionally weighted) cluster cost * @@ -1617,7 +1122,7 @@ namespace helpers { * @param tol tolerance for early stopping convergence */ void find_k(raft::resources const& handle, - raft::device_matrix_view X, + raft::device_matrix_view X, raft::host_scalar_view best_k, raft::host_scalar_view inertia, raft::host_scalar_view n_iter, diff --git a/cpp/include/cuvs/cluster/spectral.hpp b/cpp/include/cuvs/cluster/spectral.hpp index f1ed2fd7ff..e81906efec 100644 --- a/cpp/include/cuvs/cluster/spectral.hpp +++ b/cpp/include/cuvs/cluster/spectral.hpp @@ -84,7 +84,7 @@ struct params { void fit_predict(raft::resources const& handle, params config, raft::device_coo_matrix_view connectivity_graph, - raft::device_vector_view labels); + raft::device_vector_view labels); /** * @brief Perform spectral clustering on a connectivity graph @@ -122,7 +122,7 @@ void fit_predict(raft::resources const& handle, void fit_predict(raft::resources const& handle, params config, raft::device_coo_matrix_view connectivity_graph, - raft::device_vector_view labels); + raft::device_vector_view labels); /** * @brief Perform spectral clustering on a dense dataset @@ -155,7 +155,7 @@ void fit_predict(raft::resources const& handle, void fit_predict(raft::resources const& handle, params config, raft::device_matrix_view dataset, - raft::device_vector_view labels); + raft::device_vector_view labels); /** * @} */ diff --git a/cpp/src/cluster/detail/kmeans.cuh b/cpp/src/cluster/detail/kmeans.cuh index 5a35f203b3..faf4e2c989 100644 --- a/cpp/src/cluster/detail/kmeans.cuh +++ b/cpp/src/cluster/detail/kmeans.cuh @@ -311,7 +311,7 @@ void kmeans_fit_main(raft::resources const& handle, raft::device_vector_view weight, raft::device_matrix_view centroidsRawData, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter, + raft::host_scalar_view n_iter, rmm::device_uvector& workspace) { raft::common::nvtx::range fun_scope("kmeans_fit_main"); @@ -650,7 +650,7 @@ void initScalableKMeansPlusPlus(raft::resources const& handle, handle, params, potentialCentroids, centroidsRawData, workspace); auto inertia = raft::make_host_scalar(0); - auto n_iter = raft::make_host_scalar(0); + auto n_iter = raft::make_host_scalar(0); cuvs::cluster::kmeans::params default_params; default_params.n_clusters = params.n_clusters; @@ -727,7 +727,7 @@ void kmeans_fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { raft::common::nvtx::range fun_scope("kmeans_fit"); auto n_samples = X.extent(0); @@ -798,8 +798,8 @@ void kmeans_fit(raft::resources const& handle, cuvs::cluster::kmeans::params iter_params = pams; iter_params.rng_state.seed = gen(); - DataT iter_inertia = std::numeric_limits::max(); - IndexT n_current_iter = 0; + DataT iter_inertia = std::numeric_limits::max(); + int n_current_iter = 0; if (iter_params.init == cuvs::cluster::kmeans::params::InitMethod::Random) { // initializing with random samples from input dataset RAFT_LOG_DEBUG( @@ -844,7 +844,7 @@ void kmeans_fit(raft::resources const& handle, weight.view(), centroidsRawData.view(), raft::make_host_scalar_view(&iter_inertia), - raft::make_host_scalar_view(&n_current_iter), + raft::make_host_scalar_view(&n_current_iter), workspace); if (iter_inertia < inertia[0]) { inertia[0] = iter_inertia; @@ -872,7 +872,7 @@ void kmeans_fit(raft::resources const& handle, IndexT n_samples, IndexT n_features, DataT& inertia, - IndexT& n_iter) + int& n_iter) { auto XView = raft::make_device_matrix_view(X, n_samples, n_features); auto centroidsView = @@ -894,7 +894,7 @@ void kmeans_predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia) { @@ -981,8 +981,10 @@ void kmeans_predict(raft::resources const& handle, raft::value_op{}, raft::add_op{}); - raft::linalg::map( - handle, labels, raft::key_op{}, raft::make_const_mdspan(minClusterAndDistance.view())); + raft::linalg::map(handle, + labels, + raft::compose_op(raft::cast_op{}, raft::key_op{}), + raft::make_const_mdspan(minClusterAndDistance.view())); inertia[0] = clusterCostD.value(stream); } @@ -995,7 +997,7 @@ void kmeans_predict(raft::resources const& handle, const DataT* centroids, IndexT n_samples, IndexT n_features, - IndexT* labels, + uint32_t* labels, bool normalize_weight, DataT& inertia) { @@ -1006,7 +1008,7 @@ void kmeans_predict(raft::resources const& handle, if (sample_weight) sample_weightView.emplace( raft::make_device_vector_view(sample_weight, n_samples)); - auto labelsView = raft::make_device_vector_view(labels, n_samples); + auto labelsView = raft::make_device_vector_view(labels, n_samples); auto inertiaView = raft::make_host_scalar_view(&inertia); cuvs::cluster::kmeans::detail::kmeans_predict(handle, diff --git a/cpp/src/cluster/detail/kmeans_auto_find_k.cuh b/cpp/src/cluster/detail/kmeans_auto_find_k.cuh index 594a63e8da..e8c88978be 100644 --- a/cpp/src/cluster/detail/kmeans_auto_find_k.cuh +++ b/cpp/src/cluster/detail/kmeans_auto_find_k.cuh @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2023-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2023-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -25,13 +25,13 @@ void compute_dispersion(raft::resources const& handle, raft::device_matrix_view X, cuvs::cluster::kmeans::params& params, raft::device_matrix_view centroids_view, - raft::device_vector_view labels, - raft::device_vector_view clusterSizes, + raft::device_vector_view labels, + raft::device_vector_view clusterSizes, rmm::device_uvector& workspace, raft::host_vector_view clusterDispertionView, raft::host_vector_view resultsView, raft::host_scalar_view residual, - raft::host_scalar_view n_iter, + raft::host_scalar_view n_iter, int val, idx_t n, idx_t d) @@ -39,32 +39,36 @@ void compute_dispersion(raft::resources const& handle, auto centroids_const_view = raft::make_device_matrix_view(centroids_view.data_handle(), val, d); - idx_t* clusterSizes_ptr = clusterSizes.data_handle(); auto cluster_sizes_view = - raft::make_device_vector_view(clusterSizes_ptr, val); + raft::make_device_vector_view(clusterSizes.data_handle(), val); params.n_clusters = val; cuvs::cluster::kmeans::fit_predict( handle, params, X, std::nullopt, std::make_optional(centroids_view), labels, residual, n_iter); - detail::countLabels(handle, labels.data_handle(), clusterSizes.data_handle(), n, val, workspace); + detail::countLabels( + handle, labels.data_handle(), clusterSizes.data_handle(), n, idx_t(val), workspace); - resultsView[val] = residual[0]; - clusterDispertionView[val] = raft::stats::cluster_dispersion( - handle, centroids_const_view, cluster_sizes_view, std::nullopt, n); + resultsView[val] = residual[0]; + clusterDispertionView[val] = + raft::stats::cluster_dispersion(handle, + centroids_const_view, + raft::make_const_mdspan(cluster_sizes_view), + std::nullopt, + n); } template void find_k(raft::resources const& handle, raft::device_matrix_view X, - raft::host_scalar_view best_k, + raft::host_scalar_view best_k, raft::host_scalar_view residual, - raft::host_scalar_view n_iter, - idx_t kmax, - idx_t kmin = 1, - idx_t maxiter = 100, - value_t tol = 1e-2) + raft::host_scalar_view n_iter, + int kmax, + int kmin = 1, + int maxiter = 100, + value_t tol = 1e-2) { idx_t n = X.extent(0); idx_t d = X.extent(1); @@ -79,13 +83,11 @@ void find_k(raft::resources const& handle, // Device memory auto centroids = raft::make_device_matrix(handle, kmax, X.extent(1)); - auto clusterSizes = raft::make_device_vector(handle, kmax); - auto labels = raft::make_device_vector(handle, n); + auto clusterSizes = raft::make_device_vector(handle, kmax); + auto labels = raft::make_device_vector(handle, n); rmm::device_uvector workspace(0, raft::resource::get_cuda_stream(handle)); - idx_t* clusterSizes_ptr = clusterSizes.data_handle(); - // Host memory auto results = raft::make_host_vector(kmax + 1); auto clusterDispersion = raft::make_host_vector(kmax + 1); @@ -109,9 +111,9 @@ void find_k(raft::resources const& handle, auto centroids_view = raft::make_device_matrix_view(centroids.data_handle(), left, d); - auto labels_view = raft::make_device_vector_view(labels.data_handle(), n); + auto labels_view = raft::make_device_vector_view(labels.data_handle(), n); auto clusterSizes_view = - raft::make_device_vector_view(clusterSizes.data_handle(), kmax); + raft::make_device_vector_view(clusterSizes.data_handle(), kmax); compute_dispersion(handle, X, params, diff --git a/cpp/src/cluster/detail/kmeans_batched.cuh b/cpp/src/cluster/detail/kmeans_batched.cuh index 93888490a4..d31d8cf835 100644 --- a/cpp/src/cluster/detail/kmeans_batched.cuh +++ b/cpp/src/cluster/detail/kmeans_batched.cuh @@ -227,7 +227,7 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { cudaStream_t stream = raft::resource::get_cuda_stream(handle); auto n_samples = X.extent(0); @@ -272,7 +272,7 @@ void fit(raft::resources const& handle, ? raft::make_device_matrix(handle, n_clusters, n_features) : raft::make_device_matrix(handle, 0, 0); T best_inertia = std::numeric_limits::max(); - IdxT best_n_iter = 0; + int best_n_iter = 0; std::mt19937 gen(params.rng_state.seed); diff --git a/cpp/src/cluster/detail/kmeans_common.cuh b/cpp/src/cluster/detail/kmeans_common.cuh index 250563dd12..09f14085e8 100644 --- a/cpp/src/cluster/detail/kmeans_common.cuh +++ b/cpp/src/cluster/detail/kmeans_common.cuh @@ -383,9 +383,7 @@ void minClusterAndDistanceCompute( rmm::device_uvector& workspace); EXTERN_TEMPLATE_MIN_CLUSTER_AND_DISTANCE(float, int64_t) -EXTERN_TEMPLATE_MIN_CLUSTER_AND_DISTANCE(float, int) EXTERN_TEMPLATE_MIN_CLUSTER_AND_DISTANCE(double, int64_t) -EXTERN_TEMPLATE_MIN_CLUSTER_AND_DISTANCE(double, int) #undef EXTERN_TEMPLATE_MIN_CLUSTER_AND_DISTANCE @@ -416,8 +414,6 @@ void minClusterDistanceCompute(raft::resources const& handle, EXTERN_TEMPLATE_MIN_CLUSTER_DISTANCE(float, int64_t) EXTERN_TEMPLATE_MIN_CLUSTER_DISTANCE(double, int64_t) -EXTERN_TEMPLATE_MIN_CLUSTER_DISTANCE(float, int) -EXTERN_TEMPLATE_MIN_CLUSTER_DISTANCE(double, int) #undef EXTERN_TEMPLATE_MIN_CLUSTER_DISTANCE diff --git a/cpp/src/cluster/detail/kmeans_mg.cuh b/cpp/src/cluster/detail/kmeans_mg.cuh index fdec2bdd73..59d6cceb4d 100644 --- a/cpp/src/cluster/detail/kmeans_mg.cuh +++ b/cpp/src/cluster/detail/kmeans_mg.cuh @@ -408,7 +408,7 @@ void initKMeansPlusPlus(const raft::resources& handle, handle, params, const_centroids, centroidsRawData, workspace); auto inertia = raft::make_host_scalar(0); - auto n_iter = raft::make_host_scalar(0); + auto n_iter = raft::make_host_scalar(0); auto weight_view = raft::make_device_vector_view(weight.data_handle(), weight.extent(0)); cuvs::cluster::kmeans::params params_copy = params; @@ -504,7 +504,7 @@ void fit(const raft::resources& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter, + raft::host_scalar_view n_iter, rmm::device_uvector& workspace) { const auto& comm = raft::resource::get_comms(handle); diff --git a/cpp/src/cluster/detail/minClusterDistanceCompute.cu b/cpp/src/cluster/detail/minClusterDistanceCompute.cu index 8370ff922f..569d389b41 100644 --- a/cpp/src/cluster/detail/minClusterDistanceCompute.cu +++ b/cpp/src/cluster/detail/minClusterDistanceCompute.cu @@ -158,8 +158,6 @@ void minClusterAndDistanceCompute( INSTANTIATE_MIN_CLUSTER_AND_DISTANCE(float, int64_t) INSTANTIATE_MIN_CLUSTER_AND_DISTANCE(double, int64_t) -INSTANTIATE_MIN_CLUSTER_AND_DISTANCE(float, int) -INSTANTIATE_MIN_CLUSTER_AND_DISTANCE(double, int) #undef INSTANTIATE_MIN_CLUSTER_AND_DISTANCE @@ -294,8 +292,6 @@ void minClusterDistanceCompute(raft::resources const& handle, INSTANTIATE_MIN_CLUSTER_DISTANCE(float, int64_t) INSTANTIATE_MIN_CLUSTER_DISTANCE(double, int64_t) -INSTANTIATE_MIN_CLUSTER_DISTANCE(float, int) -INSTANTIATE_MIN_CLUSTER_DISTANCE(double, int) #undef INSTANTIATE_MIN_CLUSTER_DISTANCE diff --git a/cpp/src/cluster/detail/spectral.cuh b/cpp/src/cluster/detail/spectral.cuh index 52513afe26..9a39f6d991 100644 --- a/cpp/src/cluster/detail/spectral.cuh +++ b/cpp/src/cluster/detail/spectral.cuh @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -18,7 +18,7 @@ template void fit_predict(raft::resources const& handle, params config, raft::device_coo_matrix_view connectivity_graph, - raft::device_vector_view labels) + raft::device_vector_view labels) { int n_samples = connectivity_graph.structure_view().get_n_rows(); DataT inertia; @@ -51,12 +51,16 @@ void fit_predict(raft::resources const& handle, config.n_components, raft::resource::get_cuda_stream(handle)); + auto X_i64 = raft::make_device_matrix_view( + embedding_row_major.data_handle(), int64_t(n_samples), int64_t(config.n_components)); + auto labels_i64 = raft::make_device_vector_view( + labels.data_handle(), int64_t(n_samples)); cuvs::cluster::kmeans::fit_predict(handle, kmeans_config, - embedding_row_major.view(), + raft::make_const_mdspan(X_i64), std::nullopt, std::nullopt, - labels, + labels_i64, raft::make_host_scalar_view(&inertia), raft::make_host_scalar_view(&n_iter)); } @@ -64,7 +68,7 @@ void fit_predict(raft::resources const& handle, void fit_predict(raft::resources const& handle, params config, raft::device_matrix_view dataset, - raft::device_vector_view labels) + raft::device_vector_view labels) { int n_samples = dataset.extent(0); diff --git a/cpp/src/cluster/kmeans.cuh b/cpp/src/cluster/kmeans.cuh index e4f9821990..309f8f8836 100644 --- a/cpp/src/cluster/kmeans.cuh +++ b/cpp/src/cluster/kmeans.cuh @@ -64,7 +64,7 @@ void fit_main(raft::resources const& handle, raft::device_vector_view sample_weights, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter, + raft::host_scalar_view n_iter, rmm::device_uvector& workspace); #define EXTERN_TEMPLATE_FIT_MAIN(DataT, IndexT) \ @@ -75,13 +75,11 @@ void fit_main(raft::resources const& handle, raft::device_vector_view sample_weights, \ raft::device_matrix_view centroids, \ raft::host_scalar_view inertia, \ - raft::host_scalar_view n_iter, \ + raft::host_scalar_view n_iter, \ rmm::device_uvector& workspace); -EXTERN_TEMPLATE_FIT_MAIN(double, int) EXTERN_TEMPLATE_FIT_MAIN(double, int64_t) EXTERN_TEMPLATE_FIT_MAIN(float, int64_t) -EXTERN_TEMPLATE_FIT_MAIN(float, int) #undef EXTERN_TEMPLATE_FIT_MAIN /** @@ -136,7 +134,7 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); #define EXTERN_TEMPLATE_FIT(DataT, IndexT) \ extern template void fit( \ @@ -146,11 +144,9 @@ void fit(raft::resources const& handle, std::optional> sample_weight, \ raft::device_matrix_view centroids, \ raft::host_scalar_view inertia, \ - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); -EXTERN_TEMPLATE_FIT(double, int) EXTERN_TEMPLATE_FIT(double, int64_t) -EXTERN_TEMPLATE_FIT(float, int) EXTERN_TEMPLATE_FIT(float, int64_t) #undef EXTERN_TEMPLATE_FIT @@ -212,7 +208,7 @@ void predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia); @@ -223,13 +219,11 @@ void predict(raft::resources const& handle, raft::device_matrix_view X, \ std::optional> sample_weight, \ raft::device_matrix_view centroids, \ - raft::device_vector_view labels, \ + raft::device_vector_view labels, \ bool normalize_weight, \ raft::host_scalar_view inertia); -EXTERN_TEMPLATE_PREDICT(double, int) EXTERN_TEMPLATE_PREDICT(double, int64_t) -EXTERN_TEMPLATE_PREDICT(float, int) EXTERN_TEMPLATE_PREDICT(float, int64_t) #undef EXTERN_TEMPLATE_PREDICT diff --git a/cpp/src/cluster/kmeans_auto_find_k_float.cu b/cpp/src/cluster/kmeans_auto_find_k_float.cu index e54f515acb..de5223121f 100644 --- a/cpp/src/cluster/kmeans_auto_find_k_float.cu +++ b/cpp/src/cluster/kmeans_auto_find_k_float.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -11,7 +11,7 @@ namespace cuvs::cluster::kmeans::helpers { void find_k(raft::resources const& handle, - raft::device_matrix_view X, + raft::device_matrix_view X, raft::host_scalar_view best_k, raft::host_scalar_view inertia, raft::host_scalar_view n_iter, @@ -20,7 +20,7 @@ void find_k(raft::resources const& handle, int maxiter, float tol) { - cuvs::cluster::kmeans::detail::find_k( + cuvs::cluster::kmeans::detail::find_k( handle, X, best_k, inertia, n_iter, kmax, kmin, maxiter, tol); } } // namespace cuvs::cluster::kmeans::helpers diff --git a/cpp/src/cluster/kmeans_balanced_fit_predict_int8.cu b/cpp/src/cluster/kmeans_balanced_fit_predict_int8.cu index 5cf4eab59c..cb4166f3d8 100644 --- a/cpp/src/cluster/kmeans_balanced_fit_predict_int8.cu +++ b/cpp/src/cluster/kmeans_balanced_fit_predict_int8.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -19,13 +19,4 @@ void fit_predict(const raft::resources& handle, { cuvs::cluster::kmeans_balanced::fit_predict(handle, params, X, centroids, labels); } - -void fit_predict(const raft::resources& handle, - cuvs::cluster::kmeans::balanced_params const& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_vector_view labels) -{ - cuvs::cluster::kmeans_balanced::fit_predict(handle, params, X, centroids, labels); -} } // namespace cuvs::cluster::kmeans diff --git a/cpp/src/cluster/kmeans_balanced_predict_float.cu b/cpp/src/cluster/kmeans_balanced_predict_float.cu index 5a08b19c3d..b9d4e72490 100644 --- a/cpp/src/cluster/kmeans_balanced_predict_float.cu +++ b/cpp/src/cluster/kmeans_balanced_predict_float.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -21,16 +21,6 @@ void predict(const raft::resources& handle, cuvs::cluster::kmeans_balanced::predict( handle, params, X, centroids, labels, cuvs::spatial::knn::detail::utils::mapping{}); } - -void predict(const raft::resources& handle, - cuvs::cluster::kmeans::balanced_params const& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_vector_view labels) -{ - cuvs::cluster::kmeans_balanced::predict( - handle, params, X, centroids, labels, cuvs::spatial::knn::detail::utils::mapping{}); -} } // namespace cuvs::cluster::kmeans namespace cuvs::cluster::kmeans_balanced::helpers { diff --git a/cpp/src/cluster/kmeans_balanced_predict_int8.cu b/cpp/src/cluster/kmeans_balanced_predict_int8.cu index 4a62fbdbcd..7751da98bc 100644 --- a/cpp/src/cluster/kmeans_balanced_predict_int8.cu +++ b/cpp/src/cluster/kmeans_balanced_predict_int8.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -20,14 +20,4 @@ void predict(const raft::resources& handle, cuvs::cluster::kmeans_balanced::predict( handle, params, X, centroids, labels, cuvs::spatial::knn::detail::utils::mapping{}); } - -void predict(const raft::resources& handle, - cuvs::cluster::kmeans::balanced_params const& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_vector_view labels) -{ - cuvs::cluster::kmeans_balanced::predict( - handle, params, X, centroids, labels, cuvs::spatial::knn::detail::utils::mapping{}); -} } // namespace cuvs::cluster::kmeans diff --git a/cpp/src/cluster/kmeans_cluster_cost.cu b/cpp/src/cluster/kmeans_cluster_cost.cu index 0cdc182fb9..e5180cc89d 100644 --- a/cpp/src/cluster/kmeans_cluster_cost.cu +++ b/cpp/src/cluster/kmeans_cluster_cost.cu @@ -8,23 +8,6 @@ #include namespace cuvs::cluster::kmeans { -void cluster_cost(const raft::resources& handle, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::host_scalar_view cost, - std::optional> sample_weight) -{ - cuvs::cluster::kmeans::cluster_cost(handle, X, centroids, cost, sample_weight); -} - -void cluster_cost(const raft::resources& handle, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::host_scalar_view cost, - std::optional> sample_weight) -{ - cuvs::cluster::kmeans::cluster_cost(handle, X, centroids, cost, sample_weight); -} void cluster_cost(const raft::resources& handle, raft::device_matrix_view X, diff --git a/cpp/src/cluster/kmeans_fit_double.cu b/cpp/src/cluster/kmeans_fit_double.cu index d7e4748e33..8a9f1f7dbd 100644 --- a/cpp/src/cluster/kmeans_fit_double.cu +++ b/cpp/src/cluster/kmeans_fit_double.cu @@ -18,7 +18,7 @@ namespace cuvs::cluster::kmeans { raft::device_vector_view sample_weights, \ raft::device_matrix_view centroids, \ raft::host_scalar_view inertia, \ - raft::host_scalar_view n_iter, \ + raft::host_scalar_view n_iter, \ rmm::device_uvector& workspace); #define INSTANTIATE_FIT(DataT, IndexT) \ @@ -29,36 +29,21 @@ namespace cuvs::cluster::kmeans { std::optional> sample_weight, \ raft::device_matrix_view centroids, \ raft::host_scalar_view inertia, \ - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); -INSTANTIATE_FIT_MAIN(double, int) INSTANTIATE_FIT_MAIN(double, int64_t) - -INSTANTIATE_FIT(double, int) INSTANTIATE_FIT(double, int64_t) #undef INSTANTIATE_FIT_MAIN #undef INSTANTIATE_FIT -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) -{ - cuvs::cluster::kmeans::fit( - handle, params, X, sample_weight, centroids, inertia, n_iter); -} - void fit(raft::resources const& handle, const cuvs::cluster::kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { cuvs::cluster::kmeans::fit( handle, params, X, sample_weight, centroids, inertia, n_iter); @@ -70,7 +55,7 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { cuvs::cluster::kmeans::detail::fit( handle, params, X, sample_weight, centroids, inertia, n_iter); diff --git a/cpp/src/cluster/kmeans_fit_float.cu b/cpp/src/cluster/kmeans_fit_float.cu index f86fabcfbd..faa85ee51b 100644 --- a/cpp/src/cluster/kmeans_fit_float.cu +++ b/cpp/src/cluster/kmeans_fit_float.cu @@ -18,7 +18,7 @@ namespace cuvs::cluster::kmeans { raft::device_vector_view sample_weights, \ raft::device_matrix_view centroids, \ raft::host_scalar_view inertia, \ - raft::host_scalar_view n_iter, \ + raft::host_scalar_view n_iter, \ rmm::device_uvector& workspace); #define INSTANTIATE_FIT(DataT, IndexT) \ @@ -29,36 +29,21 @@ namespace cuvs::cluster::kmeans { std::optional> sample_weight, \ raft::device_matrix_view centroids, \ raft::host_scalar_view inertia, \ - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); -INSTANTIATE_FIT_MAIN(float, int) INSTANTIATE_FIT_MAIN(float, int64_t) - -INSTANTIATE_FIT(float, int) INSTANTIATE_FIT(float, int64_t) #undef INSTANTIATE_FIT_MAIN #undef INSTANTIATE_FIT -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) -{ - cuvs::cluster::kmeans::fit( - handle, params, X, sample_weight, centroids, inertia, n_iter); -} - void fit(raft::resources const& handle, const cuvs::cluster::kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { cuvs::cluster::kmeans::fit( handle, params, X, sample_weight, centroids, inertia, n_iter); @@ -70,7 +55,7 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { cuvs::cluster::kmeans::detail::fit( handle, params, X, sample_weight, centroids, inertia, n_iter); diff --git a/cpp/src/cluster/kmeans_fit_mg_double.cu b/cpp/src/cluster/kmeans_fit_mg_double.cu index bd7f8453c1..96ebf1c4ff 100644 --- a/cpp/src/cluster/kmeans_fit_mg_double.cu +++ b/cpp/src/cluster/kmeans_fit_mg_double.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -9,31 +9,17 @@ namespace cuvs::cluster::kmeans::mg { -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) -{ - rmm::device_uvector workspace(0, raft::resource::get_cuda_stream(handle)); - - cuvs::cluster::kmeans::mg::detail::fit( - handle, params, X, sample_weight, centroids, inertia, n_iter, workspace); -} - void fit(raft::resources const& handle, const cuvs::cluster::kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { rmm::device_uvector workspace(0, raft::resource::get_cuda_stream(handle)); - cuvs::cluster::kmeans::mg::detail::fit( handle, params, X, sample_weight, centroids, inertia, n_iter, workspace); } + } // namespace cuvs::cluster::kmeans::mg diff --git a/cpp/src/cluster/kmeans_fit_mg_float.cu b/cpp/src/cluster/kmeans_fit_mg_float.cu index ae7c5722b7..6a3a96ace2 100644 --- a/cpp/src/cluster/kmeans_fit_mg_float.cu +++ b/cpp/src/cluster/kmeans_fit_mg_float.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -9,31 +9,17 @@ namespace cuvs::cluster::kmeans::mg { -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) -{ - rmm::device_uvector workspace(0, raft::resource::get_cuda_stream(handle)); - - cuvs::cluster::kmeans::mg::detail::fit( - handle, params, X, sample_weight, centroids, inertia, n_iter, workspace); -} - void fit(raft::resources const& handle, const cuvs::cluster::kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { rmm::device_uvector workspace(0, raft::resource::get_cuda_stream(handle)); - cuvs::cluster::kmeans::mg::detail::fit( handle, params, X, sample_weight, centroids, inertia, n_iter, workspace); } + } // namespace cuvs::cluster::kmeans::mg diff --git a/cpp/src/cluster/kmeans_fit_predict_double.cu b/cpp/src/cluster/kmeans_fit_predict_double.cu index b38f2b2327..117308c419 100644 --- a/cpp/src/cluster/kmeans_fit_predict_double.cu +++ b/cpp/src/cluster/kmeans_fit_predict_double.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -8,29 +8,14 @@ namespace cuvs::cluster::kmeans { -void fit_predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - std::optional> centroids, - raft::device_vector_view labels, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) - -{ - cuvs::cluster::kmeans::fit_predict( - handle, params, X, sample_weight, centroids, labels, inertia, n_iter); -} - void fit_predict(raft::resources const& handle, const kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, std::optional> centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) - + raft::host_scalar_view n_iter) { cuvs::cluster::kmeans::fit_predict( handle, params, X, sample_weight, centroids, labels, inertia, n_iter); diff --git a/cpp/src/cluster/kmeans_fit_predict_float.cu b/cpp/src/cluster/kmeans_fit_predict_float.cu index 253e93f4aa..0bb128205a 100644 --- a/cpp/src/cluster/kmeans_fit_predict_float.cu +++ b/cpp/src/cluster/kmeans_fit_predict_float.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -8,29 +8,14 @@ namespace cuvs::cluster::kmeans { -void fit_predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - std::optional> centroids, - raft::device_vector_view labels, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) - -{ - cuvs::cluster::kmeans::fit_predict( - handle, params, X, sample_weight, centroids, labels, inertia, n_iter); -} - void fit_predict(raft::resources const& handle, const kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, std::optional> centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) - + raft::host_scalar_view n_iter) { cuvs::cluster::kmeans::fit_predict( handle, params, X, sample_weight, centroids, labels, inertia, n_iter); diff --git a/cpp/src/cluster/kmeans_impl.cuh b/cpp/src/cluster/kmeans_impl.cuh index 437aa16c76..7ae49832e2 100644 --- a/cpp/src/cluster/kmeans_impl.cuh +++ b/cpp/src/cluster/kmeans_impl.cuh @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ #pragma once @@ -15,7 +15,7 @@ void fit_main(raft::resources const& handle, raft::device_vector_view sample_weights, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter, + raft::host_scalar_view n_iter, rmm::device_uvector& workspace) { cuvs::cluster::kmeans::detail::kmeans_fit_main( @@ -29,7 +29,7 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { // use the mnmg kmeans fit if we have comms initialize, single gpu otherwise if (raft::resource::comms_initialized(handle)) { @@ -46,7 +46,7 @@ void predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia) { diff --git a/cpp/src/cluster/kmeans_impl_fit_predict.cuh b/cpp/src/cluster/kmeans_impl_fit_predict.cuh index e350b7d6ed..8726eb7727 100644 --- a/cpp/src/cluster/kmeans_impl_fit_predict.cuh +++ b/cpp/src/cluster/kmeans_impl_fit_predict.cuh @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ #pragma once @@ -16,9 +16,9 @@ void fit_predict(raft::resources const& handle, raft::device_matrix_view X, std::optional> sample_weight, std::optional> centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter) + raft::host_scalar_view n_iter) { if (!centroids.has_value()) { auto n_features = X.extent(1); diff --git a/cpp/src/cluster/kmeans_mg.hpp b/cpp/src/cluster/kmeans_mg.hpp index 77cceff962..bc97781fda 100644 --- a/cpp/src/cluster/kmeans_mg.hpp +++ b/cpp/src/cluster/kmeans_mg.hpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ #pragma once @@ -30,28 +30,12 @@ namespace cuvs::cluster::kmeans::mg { * closest cluster center. * @param[out] n_iter Number of iterations run. */ -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); - void fit(raft::resources const& handle, const cuvs::cluster::kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); - -void fit(raft::resources const& handle, - const cuvs::cluster::kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::host_scalar_view inertia, raft::host_scalar_view n_iter); void fit(raft::resources const& handle, @@ -60,5 +44,6 @@ void fit(raft::resources const& handle, std::optional> sample_weight, raft::device_matrix_view centroids, raft::host_scalar_view inertia, - raft::host_scalar_view n_iter); + raft::host_scalar_view n_iter); + } // namespace cuvs::cluster::kmeans::mg diff --git a/cpp/src/cluster/kmeans_predict_double.cu b/cpp/src/cluster/kmeans_predict_double.cu index 52d120a232..d69c8d688b 100644 --- a/cpp/src/cluster/kmeans_predict_double.cu +++ b/cpp/src/cluster/kmeans_predict_double.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -16,38 +16,22 @@ namespace cuvs::cluster::kmeans { raft::device_matrix_view X, \ std::optional> sample_weight, \ raft::device_matrix_view centroids, \ - raft::device_vector_view labels, \ + raft::device_vector_view labels, \ bool normalize_weight, \ raft::host_scalar_view inertia); -INSTANTIATE_PREDICT(double, int) INSTANTIATE_PREDICT(double, int64_t) #undef INSTANTIATE_PREDICT -void predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::device_vector_view labels, - bool normalize_weight, - raft::host_scalar_view inertia) - -{ - cuvs::cluster::kmeans::predict( - handle, params, X, sample_weight, centroids, labels, normalize_weight, inertia); -} - void predict(raft::resources const& handle, const kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia) - { cuvs::cluster::kmeans::predict( handle, params, X, sample_weight, centroids, labels, normalize_weight, inertia); diff --git a/cpp/src/cluster/kmeans_predict_float.cu b/cpp/src/cluster/kmeans_predict_float.cu index 30812aa141..a2868d9e5f 100644 --- a/cpp/src/cluster/kmeans_predict_float.cu +++ b/cpp/src/cluster/kmeans_predict_float.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -16,38 +16,22 @@ namespace cuvs::cluster::kmeans { raft::device_matrix_view X, \ std::optional> sample_weight, \ raft::device_matrix_view centroids, \ - raft::device_vector_view labels, \ + raft::device_vector_view labels, \ bool normalize_weight, \ raft::host_scalar_view inertia); -INSTANTIATE_PREDICT(float, int) INSTANTIATE_PREDICT(float, int64_t) #undef INSTANTIATE_PREDICT -void predict(raft::resources const& handle, - const kmeans::params& params, - raft::device_matrix_view X, - std::optional> sample_weight, - raft::device_matrix_view centroids, - raft::device_vector_view labels, - bool normalize_weight, - raft::host_scalar_view inertia) - -{ - cuvs::cluster::kmeans::predict( - handle, params, X, sample_weight, centroids, labels, normalize_weight, inertia); -} - void predict(raft::resources const& handle, const kmeans::params& params, raft::device_matrix_view X, std::optional> sample_weight, raft::device_matrix_view centroids, - raft::device_vector_view labels, + raft::device_vector_view labels, bool normalize_weight, raft::host_scalar_view inertia) - { cuvs::cluster::kmeans::predict( handle, params, X, sample_weight, centroids, labels, normalize_weight, inertia); diff --git a/cpp/src/cluster/kmeans_transform_double.cu b/cpp/src/cluster/kmeans_transform_double.cu index c51789c2a2..a4ad23a10e 100644 --- a/cpp/src/cluster/kmeans_transform_double.cu +++ b/cpp/src/cluster/kmeans_transform_double.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -10,11 +10,10 @@ namespace cuvs::cluster::kmeans { void transform(raft::resources const& handle, const kmeans::params& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_matrix_view X_new) - + raft::device_matrix_view X, + raft::device_matrix_view centroids, + raft::device_matrix_view X_new) { - cuvs::cluster::kmeans::transform(handle, params, X, centroids, X_new); + cuvs::cluster::kmeans::transform(handle, params, X, centroids, X_new); } } // namespace cuvs::cluster::kmeans diff --git a/cpp/src/cluster/kmeans_transform_float.cu b/cpp/src/cluster/kmeans_transform_float.cu index 9f0f934ca0..ccfd233858 100644 --- a/cpp/src/cluster/kmeans_transform_float.cu +++ b/cpp/src/cluster/kmeans_transform_float.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -10,10 +10,10 @@ namespace cuvs::cluster::kmeans { void transform(raft::resources const& handle, const kmeans::params& params, - raft::device_matrix_view X, - raft::device_matrix_view centroids, - raft::device_matrix_view X_new) + raft::device_matrix_view X, + raft::device_matrix_view centroids, + raft::device_matrix_view X_new) { - cuvs::cluster::kmeans::transform(handle, params, X, centroids, X_new); + cuvs::cluster::kmeans::transform(handle, params, X, centroids, X_new); } } // namespace cuvs::cluster::kmeans diff --git a/cpp/src/cluster/spectral.cu b/cpp/src/cluster/spectral.cu index 7aa5cc22fc..b90e21bf74 100644 --- a/cpp/src/cluster/spectral.cu +++ b/cpp/src/cluster/spectral.cu @@ -13,7 +13,7 @@ namespace cuvs::cluster::spectral { void fit_predict(raft::resources const& handle, \ params config, \ raft::device_coo_matrix_view connectivity_graph, \ - raft::device_vector_view labels) \ + raft::device_vector_view labels) \ { \ detail::fit_predict(handle, config, connectivity_graph, labels); \ } @@ -26,7 +26,7 @@ CUVS_INST_SPECTRAL(double); void fit_predict(raft::resources const& handle, params config, raft::device_matrix_view dataset, - raft::device_vector_view labels) + raft::device_vector_view labels) { detail::fit_predict(handle, config, dataset, labels); } diff --git a/cpp/src/neighbors/detail/vpq_dataset.cuh b/cpp/src/neighbors/detail/vpq_dataset.cuh index a186ec6188..55f4e9ca18 100644 --- a/cpp/src/neighbors/detail/vpq_dataset.cuh +++ b/cpp/src/neighbors/detail/vpq_dataset.cuh @@ -109,14 +109,18 @@ void train_pq_centers( std::optional> sample_weight = std::nullopt; MathT inertia; - IdxT n_iter; + int n_iter; + auto pq_centers_i64 = raft::make_device_matrix_view( + pq_centers_view.data_handle(), + static_cast(pq_centers_view.extent(0)), + static_cast(pq_centers_view.extent(1))); cuvs::cluster::kmeans::fit(res, kmeans_params, pq_trainset_view, sample_weight, - pq_centers_view, + pq_centers_i64, raft::make_host_scalar_view(&inertia), - raft::make_host_scalar_view(&n_iter)); + raft::make_host_scalar_view(&n_iter)); } } diff --git a/cpp/tests/cluster/kmeans.cu b/cpp/tests/cluster/kmeans.cu index 1ef8d07623..c152a33104 100644 --- a/cpp/tests/cluster/kmeans.cu +++ b/cpp/tests/cluster/kmeans.cu @@ -71,10 +71,10 @@ class KmeansTest : public ::testing::TestWithParam> { // // raft::random::RngState rng(params.rng_state.seed, params.rng_state.type); // - // auto X = raft::make_device_matrix(handle, n_samples, n_features); - // auto labels = raft::make_device_vector(handle, n_samples); + // auto X = raft::make_device_matrix(handle, n_samples, n_features); + // auto labels = raft::make_device_vector(handle, n_samples); // - // raft::random::make_blobs(X.data_handle(), + // raft::random::make_blobs(X.data_handle(), // labels.data_handle(), // n_samples, // n_features, @@ -97,7 +97,7 @@ class KmeansTest : public ::testing::TestWithParam> { // thrust::cuda::par.on(stream), d_sample_weight.data(), d_sample_weight.data() + n_samples, // 1); // auto weight_view = - // raft::make_device_vector_view(d_sample_weight.data(), n_samples); + // raft::make_device_vector_view(d_sample_weight.data(), n_samples); // // T inertia = 0; // int n_iter = 0; @@ -106,14 +106,15 @@ class KmeansTest : public ::testing::TestWithParam> { // rmm::device_uvector inRankCp(0, stream); // auto X_view = raft::make_const_mdspan(X.view()); // auto centroids_view = - // raft::make_device_matrix_view(d_centroids.data(), params.n_clusters, n_features); - // auto miniX = raft::make_device_matrix(handle, n_samples / 4, n_features); + // raft::make_device_matrix_view(d_centroids.data(), params.n_clusters, + // n_features); + // auto miniX = raft::make_device_matrix(handle, n_samples / 4, n_features); // // // Initialize kmeans on a portion of X // raft::cluster::kmeans::shuffle_and_gather( // handle, // X_view, - // raft::make_device_matrix_view(miniX.data_handle(), miniX.extent(0), + // raft::make_device_matrix_view(miniX.data_handle(), miniX.extent(0), // miniX.extent(1)), miniX.extent(0), params.rng_state.seed); // // raft::cluster::kmeans::init_plus_plus( @@ -181,7 +182,7 @@ class KmeansTest : public ::testing::TestWithParam> { // raft::cluster::kmeans::min_cluster_and_distance( // handle, // X_view, - // raft::make_device_matrix_view( + // raft::make_device_matrix_view( // d_centroids.data(), params.n_clusters, n_features), // minClusterAndDistance.view(), // L2NormX.view(), @@ -197,11 +198,11 @@ class KmeansTest : public ::testing::TestWithParam> { // // auto sampleCountInCluster = raft::make_device_vector(handle, params.n_clusters); // auto weigthInCluster = raft::make_device_vector(handle, params.n_clusters); - // auto newCentroids = raft::make_device_matrix(handle, params.n_clusters, n_features); - // raft::cluster::kmeans::update_centroids(handle, + // auto newCentroids = raft::make_device_matrix(handle, params.n_clusters, + // n_features); raft::cluster::kmeans::update_centroids(handle, // X_view, // weight_view, - // raft::make_device_matrix_view( + // raft::make_device_matrix_view( // d_centroids.data(), params.n_clusters, // n_features), // itr, @@ -233,45 +234,45 @@ class KmeansTest : public ::testing::TestWithParam> { params.rng_state.seed = 1; params.oversampling_factor = 0; - auto X = raft::make_device_matrix(handle, n_samples, n_features); - auto labels = raft::make_device_vector(handle, n_samples); + auto X = raft::make_device_matrix(handle, n_samples, n_features); + auto labels = raft::make_device_vector(handle, n_samples); auto stream = raft::resource::get_cuda_stream(handle); - raft::random::make_blobs(X.data_handle(), - labels.data_handle(), - n_samples, - n_features, - params.n_clusters, - stream, - true, - nullptr, - nullptr, - T(1.0), - false, - (T)-10.0f, - (T)10.0f, - (uint64_t)1234); + raft::random::make_blobs(X.data_handle(), + labels.data_handle(), + n_samples, + n_features, + params.n_clusters, + stream, + true, + nullptr, + nullptr, + T(1.0), + false, + (T)-10.0f, + (T)10.0f, + (uint64_t)1234); d_labels.resize(n_samples, stream); d_labels_ref.resize(n_samples, stream); d_centroids.resize(params.n_clusters * n_features, stream); - std::optional> d_sw = std::nullopt; + std::optional> d_sw = std::nullopt; auto d_centroids_view = - raft::make_device_matrix_view(d_centroids.data(), params.n_clusters, n_features); + raft::make_device_matrix_view(d_centroids.data(), params.n_clusters, n_features); if (testparams.weighted) { d_sample_weight.resize(n_samples, stream); d_sw = std::make_optional( - raft::make_device_vector_view(d_sample_weight.data(), n_samples)); + raft::make_device_vector_view(d_sample_weight.data(), n_samples)); raft::matrix::fill( - handle, raft::make_device_vector_view(d_sample_weight.data(), n_samples), T(1)); + handle, raft::make_device_vector_view(d_sample_weight.data(), n_samples), T(1)); } raft::copy(d_labels_ref.data(), labels.data_handle(), n_samples, stream); - T inertia = 0; - int n_iter = 0; - auto X_view = raft::make_const_mdspan(X.view()); + T inertia = 0; + int64_t n_iter = 0; + auto X_view = raft::make_const_mdspan(X.view()); cuvs::cluster::kmeans::fit_predict( handle, @@ -279,9 +280,9 @@ class KmeansTest : public ::testing::TestWithParam> { X_view, d_sw, d_centroids_view, - raft::make_device_vector_view(d_labels.data(), n_samples), + raft::make_device_vector_view(d_labels.data(), n_samples), raft::make_host_scalar_view(&inertia), - raft::make_host_scalar_view(&n_iter)); + raft::make_host_scalar_view(&n_iter)); raft::resource::sync_stream(handle, stream); @@ -308,8 +309,8 @@ class KmeansTest : public ::testing::TestWithParam> { protected: raft::resources handle; KmeansInputs testparams; - rmm::device_uvector d_labels; - rmm::device_uvector d_labels_ref; + rmm::device_uvector d_labels; + rmm::device_uvector d_labels_ref; rmm::device_uvector d_centroids; rmm::device_uvector d_sample_weight; double score; @@ -381,23 +382,23 @@ class KmeansFitBatchedTest : public ::testing::TestWithParam(handle, n_samples, n_features); - auto labels = raft::make_device_vector(handle, n_samples); - - raft::random::make_blobs(X.data_handle(), - labels.data_handle(), - n_samples, - n_features, - params.n_clusters, - stream, - true, - nullptr, - nullptr, - T(1.0), - false, - (T)-10.0f, - (T)10.0f, - (uint64_t)1234); + auto X = raft::make_device_matrix(handle, n_samples, n_features); + auto labels = raft::make_device_vector(handle, n_samples); + + raft::random::make_blobs(X.data_handle(), + labels.data_handle(), + n_samples, + n_features, + params.n_clusters, + stream, + true, + nullptr, + nullptr, + T(1.0), + false, + (T)-10.0f, + (T)10.0f, + (uint64_t)1234); // Copy X to host for batched API std::vector h_X(n_samples * n_features); @@ -420,32 +421,32 @@ class KmeansFitBatchedTest : public ::testing::TestWithParam(d_centroids.data(), params.n_clusters, n_features); - std::optional> d_sw = std::nullopt; + std::optional> d_sw = std::nullopt; rmm::device_uvector d_sample_weight(0, stream); if (testparams.weighted) { d_sample_weight.resize(n_samples, stream); d_sw = std::make_optional( - raft::make_device_vector_view(d_sample_weight.data(), n_samples)); + raft::make_device_vector_view(d_sample_weight.data(), n_samples)); raft::matrix::fill( - handle, raft::make_device_vector_view(d_sample_weight.data(), n_samples), T(1)); + handle, raft::make_device_vector_view(d_sample_weight.data(), n_samples), T(1)); } - auto d_centroids_ref_view = - raft::make_device_matrix_view(d_centroids_ref.data(), params.n_clusters, n_features); + auto d_centroids_ref_view = raft::make_device_matrix_view( + d_centroids_ref.data(), params.n_clusters, n_features); params.init = cuvs::cluster::kmeans::params::Array; params.inertia_check = true; params.max_iter = 20; T ref_inertia = 0; - int ref_n_iter = 0; + int64_t ref_n_iter = 0; cuvs::cluster::kmeans::fit(handle, params, raft::make_const_mdspan(X.view()), d_sw, d_centroids_ref_view, raft::make_host_scalar_view(&ref_inertia), - raft::make_host_scalar_view(&ref_n_iter)); + raft::make_host_scalar_view(&ref_n_iter)); cuvs::cluster::kmeans::params batched_params = params; batched_params.inertia_check = true; @@ -485,9 +486,9 @@ class KmeansFitBatchedTest : public ::testing::TestWithParam( + raft::make_device_matrix_view( d_centroids_ref.data(), params.n_clusters, n_features), - raft::make_device_vector_view(d_labels_ref.data(), n_samples), + raft::make_device_vector_view(d_labels_ref.data(), n_samples), true, raft::make_host_scalar_view(&ref_pred_inertia)); @@ -497,9 +498,9 @@ class KmeansFitBatchedTest : public ::testing::TestWithParam( + raft::make_device_matrix_view( d_centroids.data(), params.n_clusters, n_features), - raft::make_device_vector_view(d_labels.data(), n_samples), + raft::make_device_vector_view(d_labels.data(), n_samples), true, raft::make_host_scalar_view(&pred_inertia)); @@ -530,8 +531,8 @@ class KmeansFitBatchedTest : public ::testing::TestWithParam testparams; - rmm::device_uvector d_labels; - rmm::device_uvector d_labels_ref; + rmm::device_uvector d_labels; + rmm::device_uvector d_labels_ref; rmm::device_uvector d_centroids; rmm::device_uvector d_centroids_ref; double score; diff --git a/cpp/tests/cluster/kmeans_balanced.cu b/cpp/tests/cluster/kmeans_balanced.cu index b84ab5a7ff..d3af406b5e 100644 --- a/cpp/tests/cluster/kmeans_balanced.cu +++ b/cpp/tests/cluster/kmeans_balanced.cu @@ -193,33 +193,33 @@ const auto inputsf_i64 = get_kmeans_balanced_inputs(); * First set of tests: no conversion */ -KB_TEST((KmeansBalancedTest), +KB_TEST((KmeansBalancedTest), KmeansBalancedTestFFU32I32, - inputsf_i32); -KB_TEST((KmeansBalancedTest), + inputsf_i64); +KB_TEST((KmeansBalancedTest), KmeansBalancedTestFFI32I32_SEP, - inputsf_i32); -// KB_TEST((KmeansBalancedTest), + inputsf_i64); +// KB_TEST((KmeansBalancedTest), // KmeansBalancedTestDDU32I32, // inputsd_i32); KB_TEST((KmeansBalancedTest), KmeansBalancedTestFFU32I64, inputsf_i64); -KB_TEST((KmeansBalancedTest), +KB_TEST((KmeansBalancedTest), KmeansBalancedTestFFI32I64_SEP, inputsf_i64); // KB_TEST((KmeansBalancedTest), // KmeansBalancedTestDDU32I64, // inputsd_i64); -// KB_TEST((KmeansBalancedTest), +// KB_TEST((KmeansBalancedTest), // KmeansBalancedTestFFI32I32, -// inputsf_i32); -// KB_TEST((KmeansBalancedTest), +// inputsf_i64); +// KB_TEST((KmeansBalancedTest), // KmeansBalancedTestFFI32I64, // inputsf_i64); // KB_TEST((KmeansBalancedTest), // KmeansBalancedTestFFI64I32, -// inputsf_i32); +// inputsf_i64); // KB_TEST((KmeansBalancedTest), // KmeansBalancedTestFFI64I64, // inputsf_i64); @@ -240,11 +240,11 @@ struct i2f_scaler { RAFT_INLINE_FUNCTION auto operator()(const DataT& x) const { return op(x); }; }; -KB_TEST((KmeansBalancedTest, false>), +KB_TEST((KmeansBalancedTest, false>), KmeansBalancedTestFI8U32I32, - inputsf_i32); -KB_TEST((KmeansBalancedTest, true>), + inputsf_i64); +KB_TEST((KmeansBalancedTest, true>), KmeansBalancedTestFI8I32I32_SEP, - inputsf_i32); + inputsf_i64); } // namespace cuvs diff --git a/cpp/tests/cluster/kmeans_mg.cu b/cpp/tests/cluster/kmeans_mg.cu index 1c75a6a7f1..110b463066 100644 --- a/cpp/tests/cluster/kmeans_mg.cu +++ b/cpp/tests/cluster/kmeans_mg.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2022-2024, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -74,44 +74,44 @@ class KmeansTest : public ::testing::TestWithParam> { rmm::device_uvector X(n_samples * n_features, stream); rmm::device_uvector labels(n_samples, stream); - raft::random::make_blobs(handle, - X.data(), - labels.data(), - n_samples, - n_features, - params.n_clusters, - true, - nullptr, - nullptr, - 1.0, - false, - -10.0f, - 10.0f, - 1234ULL); + raft::random::make_blobs(handle, + X.data(), + labels.data(), + n_samples, + n_features, + params.n_clusters, + true, + nullptr, + nullptr, + 1.0, + false, + -10.0f, + 10.0f, + 1234ULL); d_labels.resize(n_samples, stream); d_labels_ref.resize(n_samples, stream); d_centroids.resize(params.n_clusters * n_features, stream); - std::optional> d_sw = std::nullopt; + std::optional> d_sw = std::nullopt; if (testparams.weighted) { d_sample_weight.resize(n_samples, stream); thrust::fill(thrust::cuda::par.on(stream), d_sample_weight.data(), d_sample_weight.data() + n_samples, 1); - d_sw = raft::make_device_vector_view(d_sample_weight.data(), n_samples); + d_sw = raft::make_device_vector_view(d_sample_weight.data(), n_samples); } raft::copy(d_labels_ref.data(), labels.data(), n_samples, stream); handle.sync_stream(stream); - T inertia = 0; - int n_iter = 0; + T inertia = 0; + int64_t n_iter = 0; - auto X_view = raft::make_device_matrix_view(X.data(), n_samples, n_features); + auto X_view = raft::make_device_matrix_view(X.data(), n_samples, n_features); auto centroids_view = - raft::make_device_matrix_view(d_centroids.data(), params.n_clusters, n_features); + raft::make_device_matrix_view(d_centroids.data(), params.n_clusters, n_features); cuvs::cluster::kmeans::fit(handle, params, @@ -119,7 +119,7 @@ class KmeansTest : public ::testing::TestWithParam> { d_sw, centroids_view, raft::make_host_scalar_view(&inertia), - raft::make_host_scalar_view(&n_iter)); + raft::make_host_scalar_view(&n_iter)); cuvs::cluster::kmeans::predict( handle, @@ -127,7 +127,7 @@ class KmeansTest : public ::testing::TestWithParam> { X_view, d_sw, d_centroids.data(), - raft::make_device_vector_view(d_labels.data(), n_samples), + raft::make_device_vector_view(d_labels.data(), n_samples), true, raft::make_host_scalar_view(&inertia)); score = raft::stats::adjusted_rand_index( @@ -150,8 +150,8 @@ class KmeansTest : public ::testing::TestWithParam> { raft::handle_t handle; cudaStream_t stream; KmeansInputs testparams; - rmm::device_uvector d_labels; - rmm::device_uvector d_labels_ref; + rmm::device_uvector d_labels; + rmm::device_uvector d_labels_ref; rmm::device_uvector d_centroids; rmm::device_uvector d_sample_weight; double score; diff --git a/cpp/tests/cluster/spectral.cu b/cpp/tests/cluster/spectral.cu index 9c55cef766..7229516ff7 100644 --- a/cpp/tests/cluster/spectral.cu +++ b/cpp/tests/cluster/spectral.cu @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -59,7 +59,7 @@ class SpectralClusteringTest : public ::testing::TestWithParam(handle, n_samples, n_features); - auto labels = raft::make_device_vector(handle, n_samples); + auto labels = raft::make_device_vector(handle, n_samples); auto stream = raft::resource::get_cuda_stream(handle); raft::random::make_blobs(X.data_handle(), @@ -121,13 +121,13 @@ class SpectralClusteringTest : public ::testing::TestWithParam(d_labels.data(), n_samples)); + raft::make_device_vector_view(d_labels.data(), n_samples)); } else { cluster::spectral::fit_predict( handle, params, connectivity_graph_float.view(), - raft::make_device_vector_view(d_labels.data(), n_samples)); + raft::make_device_vector_view(d_labels.data(), n_samples)); } raft::resource::sync_stream(handle, stream); @@ -151,8 +151,8 @@ class SpectralClusteringTest : public ::testing::TestWithParam d_labels; - rmm::device_uvector d_labels_ref; + rmm::device_uvector d_labels; + rmm::device_uvector d_labels_ref; double score; cluster::spectral::params params; }; diff --git a/python/cuvs/cuvs/cluster/kmeans/kmeans.pyx b/python/cuvs/cuvs/cluster/kmeans/kmeans.pyx index b267c908c9..cfa1f2964a 100644 --- a/python/cuvs/cuvs/cluster/kmeans/kmeans.pyx +++ b/python/cuvs/cuvs/cluster/kmeans/kmeans.pyx @@ -400,10 +400,10 @@ def predict( sample_weight_dlpack = cydlpack.dlpack_c(wrap_array(sample_weights)) if labels is None: - labels = device_ndarray.empty((x_ai.shape[0]), dtype='int32') + labels = device_ndarray.empty((x_ai.shape[0]), dtype='uint32') labels_ai = wrap_array(labels) - _check_input_array(labels_ai, [np.dtype('int32')]) + _check_input_array(labels_ai, [np.dtype('uint32')]) cdef cydlpack.DLManagedTensor * labels_dlpack = \ cydlpack.dlpack_c(labels_ai)