-
Notifications
You must be signed in to change notification settings - Fork 659
Refactor kernel_density to use less memory #7833
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
Merged
rapids-bot
merged 11 commits into
NVIDIA:release/26.06
from
Intron7:refactor-kernel-density
May 26, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d52a211
add refactor
Intron7 4a6006e
update kernel
Intron7 0d1a397
Update cpp/src/kde/kde.cu
Intron7 103c1b2
update test and adress coderabbit
Intron7 5243ecf
move kernel to cuvs
Intron7 96490a8
update for new cuvs API
Intron7 f4f1784
add docstring and int64_t
Intron7 5d6e337
add russellrao
Intron7 1e673e2
fix build and linking
Intron7 abe1a4b
Address KDE review feedback
Intron7 e93e492
Fixups
jcrist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <cuml/common/distance_type.hpp> | ||
|
|
||
| #include <raft/core/resources.hpp> | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace ML::KDE { | ||
|
|
||
| enum class DensityKernelType : int { | ||
| Gaussian = 0, | ||
| Tophat = 1, | ||
| Epanechnikov = 2, | ||
| Exponential = 3, | ||
| Linear = 4, | ||
| Cosine = 5 | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Compute normalized log-density scores for query samples. | ||
| * | ||
| * The query and training arrays must be dense row-major (C-contiguous) | ||
| * device arrays with shapes `(n_query, n_features)` and | ||
| * `(n_train, n_features)`, respectively. | ||
| * | ||
| * @tparam T floating point type, either float or double | ||
| * @param[in] handle raft resources used to launch work | ||
| * @param[in] query device pointer to query samples in row-major order | ||
| * @param[in] train device pointer to training samples in row-major order | ||
| * @param[in] weights optional device pointer to sample weights of length | ||
| * `n_train`, or nullptr for uniform weights | ||
| * @param[out] output device pointer to log-density scores of length `n_query` | ||
| * @param[in] n_query number of query samples | ||
| * @param[in] n_train number of training samples | ||
| * @param[in] n_features number of features per sample | ||
| * @param[in] bandwidth positive KDE bandwidth | ||
| * @param[in] sum_weights sum of `weights`, or `n_train` when weights is null | ||
| * @param[in] kernel density kernel to evaluate | ||
| * @param[in] metric distance metric used between query and training samples | ||
| * @param[in] metric_arg metric-specific argument, such as p for Minkowski | ||
| */ | ||
| template <typename T> | ||
| void score_samples(raft::resources const& handle, | ||
| const T* query, | ||
| const T* train, | ||
| const T* weights, | ||
| T* output, | ||
| std::int64_t n_query, | ||
| std::int64_t n_train, | ||
| std::int64_t n_features, | ||
| T bandwidth, | ||
| T sum_weights, | ||
| DensityKernelType kernel, | ||
| ML::distance::DistanceType metric, | ||
| T metric_arg); | ||
|
|
||
| extern template void score_samples<float>(raft::resources const&, | ||
| const float*, | ||
| const float*, | ||
| const float*, | ||
| float*, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| float, | ||
| float, | ||
| DensityKernelType, | ||
| ML::distance::DistanceType, | ||
| float); | ||
|
|
||
| extern template void score_samples<double>(raft::resources const&, | ||
| const double*, | ||
| const double*, | ||
| const double*, | ||
| double*, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| double, | ||
| double, | ||
| DensityKernelType, | ||
| ML::distance::DistanceType, | ||
| double); | ||
|
|
||
| } // namespace ML::KDE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <cuml/neighbors/kde.hpp> | ||
|
|
||
| #include <raft/core/device_mdspan.hpp> | ||
|
|
||
| #include <cuvs/distance/kde.hpp> | ||
|
|
||
| #include <optional> | ||
|
|
||
| namespace ML::KDE { | ||
|
|
||
| template <typename T> | ||
| void score_samples(raft::resources const& handle, | ||
| const T* query, | ||
| const T* train, | ||
| const T* weights, | ||
| T* output, | ||
| std::int64_t n_query, | ||
| std::int64_t n_train, | ||
| std::int64_t n_features, | ||
| T bandwidth, | ||
| T sum_weights, | ||
| DensityKernelType kernel, | ||
| ML::distance::DistanceType metric, | ||
| T metric_arg) | ||
| { | ||
| auto query_view = | ||
| raft::make_device_matrix_view<const T, std::int64_t>(query, n_query, n_features); | ||
| auto train_view = | ||
| raft::make_device_matrix_view<const T, std::int64_t>(train, n_train, n_features); | ||
| auto output_view = raft::make_device_vector_view<T, std::int64_t>(output, n_query); | ||
| auto weights_view = | ||
| weights | ||
| ? std::make_optional(raft::make_device_vector_view<const T, std::int64_t>(weights, n_train)) | ||
| : std::nullopt; | ||
| auto cuvs_kernel = static_cast<cuvs::distance::DensityKernelType>(kernel); | ||
| auto cuvs_metric = static_cast<cuvs::distance::DistanceType>(metric); | ||
|
|
||
| cuvs::distance::kde(handle, | ||
| query_view, | ||
| train_view, | ||
| weights_view, | ||
| output_view, | ||
| bandwidth, | ||
| sum_weights, | ||
| cuvs_kernel, | ||
| cuvs_metric, | ||
| metric_arg); | ||
| } | ||
|
|
||
| template void score_samples<float>(raft::resources const&, | ||
| const float*, | ||
| const float*, | ||
| const float*, | ||
| float*, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| float, | ||
| float, | ||
| DensityKernelType, | ||
| ML::distance::DistanceType, | ||
| float); | ||
|
|
||
| template void score_samples<double>(raft::resources const&, | ||
| const double*, | ||
| const double*, | ||
| const double*, | ||
| double*, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| std::int64_t, | ||
| double, | ||
| double, | ||
| DensityKernelType, | ||
| ML::distance::DistanceType, | ||
| double); | ||
|
|
||
| } // namespace ML::KDE | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.