Skip to content

Expose kmeans to python #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: branch-25.06
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7ce62f9
Expose kmeans to python
benfred Feb 26, 2025
5e85c0c
add missing cython bindings
benfred Feb 26, 2025
092d965
Merge branch 'branch-25.04' into python_kmeans
cjnolet Feb 27, 2025
bf0cbd2
fix cluster_cost
benfred Feb 27, 2025
1295983
Merge branch 'python_kmeans' of https://github.com/benfred/cuvs into …
benfred Feb 27, 2025
599caac
Expose CAGRA persistent mode to python
benfred Feb 27, 2025
d1a2803
add missing file
benfred Feb 27, 2025
5b2f6d9
Merge branch 'branch-25.04' into python_kmeans
benfred Feb 27, 2025
8be3373
.
benfred Feb 27, 2025
2054c87
Merge branch 'python_kmeans' of https://github.com/benfred/cuvs into …
benfred Feb 27, 2025
13b21d3
fix c-api for cagra persistent
benfred Feb 27, 2025
42ace71
Merge branch 'branch-25.04' into python_kmeans
cjnolet Feb 28, 2025
0708c21
fix docs
benfred Mar 2, 2025
18a1645
Merge branch 'python_kmeans' of https://github.com/benfred/cuvs into …
benfred Mar 2, 2025
61abd07
Merge branch 'branch-25.04' into python_kmeans
benfred Mar 4, 2025
3ff978d
Merge branch 'branch-25.04' into python_kmeans
cjnolet Apr 2, 2025
42d74bc
Merge branch 'branch-25.04' into python_kmeans
benfred Apr 10, 2025
762cc17
Merge branch 'branch-25.06' into python_kmeans
benfred Apr 10, 2025
d5cdccc
.
benfred Apr 10, 2025
8ef77bf
Merge branch 'python_kmeans' of https://github.com/benfred/cuvs into …
benfred Apr 10, 2025
6d06239
Merge remote-tracking branch 'origin/branch-25.06' into python_kmeans
benfred Apr 22, 2025
c2723d8
Merge branch 'branch-25.06' into python_kmeans
cjnolet Apr 22, 2025
ef55faf
Merge branch 'python_kmeans' of https://github.com/benfred/cuvs into …
benfred Apr 23, 2025
2e32325
Merge branch 'branch-25.06' into python_kmeans
benfred Apr 23, 2025
2cc1212
address code review feedback
benfred Apr 24, 2025
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 cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ if(BUILD_SHARED_LIBS)
add_library(
cuvs_objs OBJECT
src/cluster/kmeans_balanced_fit_float.cu
src/cluster/kmeans_cluster_cost.cu
src/cluster/kmeans_fit_mg_float.cu
src/cluster/kmeans_fit_mg_double.cu
src/cluster/kmeans_fit_double.cu
Expand Down Expand Up @@ -696,6 +697,7 @@ target_compile_definitions(cuvs::cuvs INTERFACE $<$<BOOL:${CUVS_NVTX}>:NVTX_ENAB
add_library(
cuvs_c SHARED
src/core/c_api.cpp
src/cluster/kmeans_c.cpp
src/neighbors/brute_force_c.cpp
src/neighbors/ivf_flat_c.cpp
src/neighbors/ivf_pq_c.cpp
Expand Down
217 changes: 217 additions & 0 deletions cpp/include/cuvs/cluster/kmeans.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*
* Copyright (c) 2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <cuvs/core/c_api.h>
#include <cuvs/distance/distance.h>
#include <dlpack/dlpack.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @defgroup kmeans_c_params k-means hyperparameters
* @{
*/

enum cuvsKMeansInitMethod {
/**
* Sample the centroids using the kmeans++ strategy
*/
KMeansPlusPlus,

/**
* Sample the centroids uniformly at random
*/
Random,

/**
* User provides the array of initial centroids
*/
Array
};

/**
* @brief Hyper-parameters for the kmeans algorithm
*/
struct cuvsKMeansParams {
cuvsDistanceType metric;

/**
* The number of clusters to form as well as the number of centroids to generate (default:8).
*/
int n_clusters;

/**
* Method for initialization, defaults to k-means++:
* - cuvsKMeansInitMethod::KMeansPlusPlus (k-means++): Use scalable k-means++ algorithm
* to select the initial cluster centers.
* - cuvsKMeansInitMethod::Random (random): Choose 'n_clusters' observations (rows) at
* random from the input data for the initial centroids.
* - cuvsKMeansInitMethod::Array (ndarray): Use 'centroids' as initial cluster centers.
*/
cuvsKMeansInitMethod init;

/**
* Maximum number of iterations of the k-means algorithm for a single run.
*/
int max_iter;

/**
* Relative tolerance with regards to inertia to declare convergence.
*/
double tol;

/**
* Number of instance k-means algorithm will be run with different seeds.
*/
int n_init;

/**
* Oversampling factor for use in the k-means|| algorithm
*/
double oversampling_factor;

/**
* batch_samples and batch_centroids are used to tile 1NN computation which is
* useful to optimize/control the memory footprint
* Default tile is [batch_samples x n_clusters] i.e. when batch_centroids is 0
* then don't tile the centroids
*/
int batch_samples;

/**
* if 0 then batch_centroids = n_clusters
*/
int batch_centroids;

bool inertia_check;
};

typedef struct cuvsKMeansParams* cuvsKMeansParams_t;

/**
* @brief Allocate KMeans params, and populate with default values
*
* @param[in] params cuvsKMeansParams_t to allocate
* @return cuvsError_t
*/
cuvsError_t cuvsKMeansParamsCreate(cuvsKMeansParams_t* params);

/**
* @brief De-allocate KMeans params
*
* @param[in] params
* @return cuvsError_t
*/
cuvsError_t cuvsKMeansParamsDestroy(cuvsKMeansParams_t params);

/**
* @}
*/

/**
* @defgroup kmeans_c k-means clustering APIs
* @{
*/

/**
* @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.
*
* @param[in] res opaque C 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.
*/
cuvsError_t cuvsKMeansFit(cuvsResources_t res,
cuvsKMeansParams_t params,
DLManagedTensor* X,
DLManagedTensor* sample_weight,
DLManagedTensor* centroids,
double* inertia,
int* n_iter);

/**
* @brief Predict the closest cluster each sample in X belongs to.
*
* @param[in] res opaque C 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.
*/
cuvsError_t cuvsKMeansPredict(cuvsResources_t res,
cuvsKMeansParams_t params,
DLManagedTensor* X,
DLManagedTensor* sample_weight,
DLManagedTensor* centroids,
DLManagedTensor* labels,
bool normalize_weight,
double* inertia);

/**
* @brief Compute cluster cost
*
* @param[in] res opaque C 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
*
*/
cuvsError_t cuvsKMeansClusterCost(cuvsResources_t res,
DLManagedTensor* X,
DLManagedTensor* centroids,
double* cost);
/**
* @}
*/

#ifdef __cplusplus
}
#endif
47 changes: 43 additions & 4 deletions cpp/include/cuvs/cluster/kmeans.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ struct params : base_params {
*/
double oversampling_factor = 2.0;

// batch_samples and batch_centroids are used to tile 1NN computation which is
// useful to optimize/control the memory footprint
// Default tile is [batch_samples x n_clusters] i.e. when batch_centroids is 0
// then don't tile the centroids
/**
* batch_samples and batch_centroids are used to tile 1NN computation which is
* useful to optimize/control the memory footprint
* Default tile is [batch_samples x n_clusters] i.e. when batch_centroids is 0
* then don't tile the centroids
*/
int batch_samples = 1 << 15;

/**
Expand Down Expand Up @@ -1089,6 +1091,43 @@ void transform(raft::resources const& handle,
raft::device_matrix_view<const double, int> X,
raft::device_matrix_view<const double, int> centroids,
raft::device_matrix_view<double, int> X_new);

/**
* @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
*
*/
void cluster_cost(const raft::resources& handle,
raft::device_matrix_view<const float, int> X,
raft::device_matrix_view<const float, int> centroids,
raft::host_scalar_view<float> cost);

/**
* @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
*
*/
void cluster_cost(const raft::resources& handle,
raft::device_matrix_view<const double, int> X,
raft::device_matrix_view<const double, int> centroids,
raft::host_scalar_view<double> cost);

/**
* @}
*/
Expand Down
24 changes: 24 additions & 0 deletions cpp/include/cuvs/neighbors/cagra.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,30 @@ struct cuvsCagraSearchParams {
uint32_t num_random_samplings;
/** Bit mask used for initial random seed node selection. */
uint64_t rand_xor_mask;

/** Whether to use the persistent version of the kernel (only SINGLE_CTA is supported a.t.m.) */
bool persistent;
/** Persistent kernel: time in seconds before the kernel stops if no requests received. */
float persistent_lifetime;
/**
* Set the fraction of maximum grid size used by persistent kernel.
* Value 1.0 means the kernel grid size is maximum possible for the selected device.
* The value must be greater than 0.0 and not greater than 1.0.
*
* One may need to run other kernels alongside this persistent kernel. This parameter can
* be used to reduce the grid size of the persistent kernel to leave a few SMs idle.
* Note: running any other work on GPU alongside with the persistent kernel makes the setup
* fragile.
* - Running another kernel in another thread usually works, but no progress guaranteed
* - Any CUDA allocations block the context (this issue may be obscured by using pools)
* - Memory copies to not-pinned host memory may block the context
*
* Even when we know there are no other kernels working at the same time, setting
* kDeviceUsage to 1.0 surprisingly sometimes hurts performance. Proceed with care.
* If you suspect this is an issue, you can reduce this number to ~0.9 without a significant
* impact on the throughput.
*/
float persistent_device_usage;
};

typedef struct cuvsCagraSearchParams* cuvsCagraSearchParams_t;
Expand Down
54 changes: 54 additions & 0 deletions cpp/src/cluster/kmeans.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,60 @@ void min_cluster_distance(raft::resources const& handle,
workspace);
}

template <typename DataT, typename IndexT>
void cluster_cost(raft::resources const& handle,
raft::device_matrix_view<const DataT, IndexT> X,
raft::device_matrix_view<const DataT, IndexT> centroids,
raft::host_scalar_view<DataT> cost)
{
auto stream = raft::resource::get_cuda_stream(handle);

auto n_clusters = centroids.extent(0);
auto n_samples = X.extent(0);
auto n_features = X.extent(1);

rmm::device_uvector<char> workspace(n_samples * sizeof(IndexT), stream);

auto x_norms = raft::make_device_vector<DataT>(handle, n_samples);

raft::linalg::rowNorm(x_norms.data_handle(),
X.data_handle(),
n_features,
n_samples,
raft::linalg::L2Norm,
true,
stream);

auto min_cluster_distance = raft::make_device_vector<DataT>(handle, n_samples);
rmm::device_uvector<DataT> l2_norm_or_distance_buffer(0, stream);

auto metric = cuvs::distance::DistanceType::L2Expanded;

cuvs::cluster::kmeans::min_cluster_distance<DataT, IndexT>(
handle,
X,
raft::make_device_matrix_view<DataT, IndexT>(
const_cast<DataT*>(centroids.data_handle()), n_clusters, n_features),
min_cluster_distance.view(),
x_norms.view(),
l2_norm_or_distance_buffer,
metric,
n_samples,
n_clusters,
workspace);

rmm::device_scalar<DataT> device_cost(0, stream);

cuvs::cluster::kmeans::cluster_cost(handle,
min_cluster_distance.view(),
workspace,
raft::make_device_scalar_view<DataT>(device_cost.data()),
raft::add_op{});
raft::update_host(cost.data_handle(), device_cost.data(), 1, stream);

raft::resource::sync_stream(handle);
}

/**
* @brief Calculates a <key, value> pair for every sample in input 'X' where key is an
* index of one of the 'centroids' (index of the nearest centroid) and 'value'
Expand Down
Loading