From 034f3774130c82253eadfc4189f8aa409daa423a Mon Sep 17 00:00:00 2001 From: Max Buckley Date: Mon, 26 Jan 2026 21:24:28 +0100 Subject: [PATCH 1/2] Fix uninitialized class/struct members in public headers Initialize POD members with sensible defaults to prevent undefined behavior when users create instances without explicitly setting all fields: - cluster/agglomerative.hpp: min_samples = 5 - distance/distance.hpp: KernelParams defaults (LINEAR, degree=3, gamma=1.0, coef0=0.0) - distance/grammian.hpp: cublas_handle = nullptr, fix empty constructor syntax - neighbors/ball_cover.hpp: index_trained = false - neighbors/common.hpp: make base_filter destructor virtual, num_ranks_ = 0 - neighbors/hnsw.hpp: ef = 200 - neighbors/vamana.hpp: pq_codebook_size = 0, pq_dim = 0 - preprocessing/spectral_embedding.hpp: n_components=2, n_neighbors=15, etc. Also fixes base_filter to have a virtual destructor for safe polymorphic deletion. Co-Authored-By: Claude Opus 4.5 --- cpp/include/cuvs/cluster/agglomerative.hpp | 2 +- cpp/include/cuvs/distance/distance.hpp | 8 ++++---- cpp/include/cuvs/distance/grammian.hpp | 18 +++++++++--------- cpp/include/cuvs/neighbors/ball_cover.hpp | 2 +- cpp/include/cuvs/neighbors/common.hpp | 4 ++-- cpp/include/cuvs/neighbors/hnsw.hpp | 2 +- cpp/include/cuvs/neighbors/vamana.hpp | 4 ++-- .../cuvs/preprocessing/spectral_embedding.hpp | 8 ++++---- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cpp/include/cuvs/cluster/agglomerative.hpp b/cpp/include/cuvs/cluster/agglomerative.hpp index 817826856a..cf6a81dada 100644 --- a/cpp/include/cuvs/cluster/agglomerative.hpp +++ b/cpp/include/cuvs/cluster/agglomerative.hpp @@ -129,7 +129,7 @@ struct distance_params { /** Specialized parameters to build the Mutual Reachability graph */ struct mutual_reachability_params { /** this neighborhood will be selected for core distances. */ - int min_samples; + int min_samples = 5; /** weight applied when internal distance is chosen for mutual reachability (value of 1.0 disables * the weighting) */ diff --git a/cpp/include/cuvs/distance/distance.hpp b/cpp/include/cuvs/distance/distance.hpp index 13c8c7bd7e..1d0bf25071 100644 --- a/cpp/include/cuvs/distance/distance.hpp +++ b/cpp/include/cuvs/distance/distance.hpp @@ -93,10 +93,10 @@ enum KernelType { LINEAR, POLYNOMIAL, RBF, TANH }; */ struct KernelParams { // Kernel function parameters - KernelType kernel; //!< Type of the kernel function - int degree; //!< Degree of polynomial kernel (ignored by others) - double gamma; //!< multiplier in the - double coef0; //!< additive constant in poly and tanh kernels + KernelType kernel = KernelType::LINEAR; //!< Type of the kernel function + int degree = 3; //!< Degree of polynomial kernel (ignored by others) + double gamma = 1.0; //!< multiplier in the + double coef0 = 0.0; //!< additive constant in poly and tanh kernels }; } // end namespace kernels diff --git a/cpp/include/cuvs/distance/grammian.hpp b/cpp/include/cuvs/distance/grammian.hpp index 4cb3aa47b6..4be50ee9d4 100644 --- a/cpp/include/cuvs/distance/grammian.hpp +++ b/cpp/include/cuvs/distance/grammian.hpp @@ -36,15 +36,15 @@ using csr_input_matrix_view_t = raft::device_csr_matrix_view class GramMatrixBase { protected: - cublasHandle_t cublas_handle; + cublasHandle_t cublas_handle = nullptr; bool legacy_interface; public: - GramMatrixBase() : legacy_interface(false) {}; + GramMatrixBase() : legacy_interface(false) {} [[deprecated]] GramMatrixBase(cublasHandle_t cublas_handle) - : cublas_handle(cublas_handle), legacy_interface(true) {}; + : cublas_handle(cublas_handle), legacy_interface(true) {} - virtual ~GramMatrixBase() {}; + virtual ~GramMatrixBase() = default; /** Convenience function to evaluate the Gram matrix for two vector sets. * Vector sets are provided in Matrix format @@ -320,10 +320,10 @@ class PolynomialKernel : public GramMatrixBase { * @param offset */ PolynomialKernel(exp_t exponent, math_t gain, math_t offset) - : GramMatrixBase(), exponent(exponent), gain(gain), offset(offset) {}; + : GramMatrixBase(), exponent(exponent), gain(gain), offset(offset) {} [[deprecated]] PolynomialKernel(exp_t exponent, math_t gain, math_t offset, cublasHandle_t handle) - : GramMatrixBase(handle), exponent(exponent), gain(gain), offset(offset) {}; + : GramMatrixBase(handle), exponent(exponent), gain(gain), offset(offset) {} /** Evaluate kernel matrix using polynomial kernel. * @@ -436,7 +436,7 @@ class TanhKernel : public GramMatrixBase { TanhKernel(math_t gain, math_t offset) : GramMatrixBase(), gain(gain), offset(offset) {} [[deprecated]] TanhKernel(math_t gain, math_t offset, cublasHandle_t handle) - : GramMatrixBase(handle), gain(gain), offset(offset) {}; + : GramMatrixBase(handle), gain(gain), offset(offset) {} /** Evaluate kernel matrix using tanh kernel. * @@ -551,10 +551,10 @@ class RBFKernel : public GramMatrixBase { * @tparam math_t floating point type * @param gain */ - RBFKernel(math_t gain) : GramMatrixBase(), gain(gain) {}; + RBFKernel(math_t gain) : GramMatrixBase(), gain(gain) {} [[deprecated]] RBFKernel(math_t gain, cublasHandle_t handle) - : GramMatrixBase(handle), gain(gain) {}; + : GramMatrixBase(handle), gain(gain) {} void matrixRowNormL2(raft::resources const& handle, dense_input_matrix_view_t matrix, diff --git a/cpp/include/cuvs/neighbors/ball_cover.hpp b/cpp/include/cuvs/neighbors/ball_cover.hpp index 17fa94bfd7..77a1bc1d7c 100644 --- a/cpp/include/cuvs/neighbors/ball_cover.hpp +++ b/cpp/include/cuvs/neighbors/ball_cover.hpp @@ -135,7 +135,7 @@ struct index : cuvs::neighbors::index { raft::device_matrix X_reordered; protected: - bool index_trained; + bool index_trained = false; }; /** @} */ diff --git a/cpp/include/cuvs/neighbors/common.hpp b/cpp/include/cuvs/neighbors/common.hpp index 39967999ed..9d99b800fb 100644 --- a/cpp/include/cuvs/neighbors/common.hpp +++ b/cpp/include/cuvs/neighbors/common.hpp @@ -502,7 +502,7 @@ namespace filtering { enum class FilterType { None, Bitmap, Bitset }; struct base_filter { - ~base_filter() = default; + virtual ~base_filter() = default; virtual FilterType get_filter_type() const = 0; }; @@ -976,7 +976,7 @@ struct mg_index { auto operator=(mg_index&&) -> mg_index& = default; distribution_mode mode_; - int num_ranks_; + int num_ranks_ = 0; std::vector> ann_interfaces_; // for load balancing mechanism diff --git a/cpp/include/cuvs/neighbors/hnsw.hpp b/cpp/include/cuvs/neighbors/hnsw.hpp index 9896924cb3..b30cf423de 100644 --- a/cpp/include/cuvs/neighbors/hnsw.hpp +++ b/cpp/include/cuvs/neighbors/hnsw.hpp @@ -738,7 +738,7 @@ void extend(raft::resources const& res, */ struct search_params : cuvs::neighbors::search_params { - int ef; // size of the candidate list + int ef = 200; // size of the candidate list int num_threads = 0; // number of host threads to use for concurrent searches. Value of 0 // automatically maximizes parallelism }; diff --git a/cpp/include/cuvs/neighbors/vamana.hpp b/cpp/include/cuvs/neighbors/vamana.hpp index 6c0fc94c1e..5e4f9db19a 100644 --- a/cpp/include/cuvs/neighbors/vamana.hpp +++ b/cpp/include/cuvs/neighbors/vamana.hpp @@ -33,8 +33,8 @@ namespace cuvs::neighbors::vamana { */ template struct codebook_params { - int pq_codebook_size; - int pq_dim; + int pq_codebook_size = 0; + int pq_dim = 0; std::vector pq_encoding_table; std::vector rotation_matrix; }; diff --git a/cpp/include/cuvs/preprocessing/spectral_embedding.hpp b/cpp/include/cuvs/preprocessing/spectral_embedding.hpp index e7a578d2ab..5575b1bb38 100644 --- a/cpp/include/cuvs/preprocessing/spectral_embedding.hpp +++ b/cpp/include/cuvs/preprocessing/spectral_embedding.hpp @@ -24,10 +24,10 @@ namespace cuvs::preprocessing::spectral_embedding { */ struct params { /** @brief The number of components to reduce the data to. */ - int n_components; + int n_components = 2; /** @brief The number of neighbors to use for the nearest neighbors graph. */ - int n_neighbors; + int n_neighbors = 15; /** * @brief Whether to normalize the Laplacian matrix. @@ -36,7 +36,7 @@ struct params { * If false, uses the unnormalized graph Laplacian (L = D - W). * Normalized Laplacian often leads to better results for clustering tasks. */ - bool norm_laplacian; + bool norm_laplacian = true; /** * @brief Whether to drop the first eigenvector. @@ -45,7 +45,7 @@ struct params { * uninformative. Setting this to true drops it from the embedding. * This is typically set to true when norm_laplacian is true. */ - bool drop_first; + bool drop_first = true; /** * @brief Tolerance for the eigenvalue solver. From 986dd69e6ed32639381ca9e438edbcc734c26479 Mon Sep 17 00:00:00 2001 From: aamijar Date: Mon, 23 Feb 2026 15:19:13 -0800 Subject: [PATCH 2/2] fix style --- cpp/include/cuvs/cluster/agglomerative.hpp | 2 +- cpp/include/cuvs/distance/distance.hpp | 8 ++++---- cpp/include/cuvs/distance/grammian.hpp | 22 ++++++++++++++++------ cpp/include/cuvs/neighbors/ball_cover.hpp | 2 +- cpp/include/cuvs/neighbors/hnsw.hpp | 6 +++--- cpp/include/cuvs/neighbors/vamana.hpp | 4 ++-- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/cpp/include/cuvs/cluster/agglomerative.hpp b/cpp/include/cuvs/cluster/agglomerative.hpp index cf6a81dada..d80086e218 100644 --- a/cpp/include/cuvs/cluster/agglomerative.hpp +++ b/cpp/include/cuvs/cluster/agglomerative.hpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2021-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ diff --git a/cpp/include/cuvs/distance/distance.hpp b/cpp/include/cuvs/distance/distance.hpp index 1d0bf25071..b5454f00a3 100644 --- a/cpp/include/cuvs/distance/distance.hpp +++ b/cpp/include/cuvs/distance/distance.hpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION. + * SPDX-FileCopyrightText: Copyright (c) 2021-2026, NVIDIA CORPORATION. * SPDX-License-Identifier: Apache-2.0 */ @@ -94,9 +94,9 @@ enum KernelType { LINEAR, POLYNOMIAL, RBF, TANH }; struct KernelParams { // Kernel function parameters KernelType kernel = KernelType::LINEAR; //!< Type of the kernel function - int degree = 3; //!< Degree of polynomial kernel (ignored by others) - double gamma = 1.0; //!< multiplier in the - double coef0 = 0.0; //!< additive constant in poly and tanh kernels + int degree = 3; //!< Degree of polynomial kernel (ignored by others) + double gamma = 1.0; //!< multiplier in the + double coef0 = 0.0; //!< additive constant in poly and tanh kernels }; } // end namespace kernels diff --git a/cpp/include/cuvs/distance/grammian.hpp b/cpp/include/cuvs/distance/grammian.hpp index 4be50ee9d4..a179ae80f8 100644 --- a/cpp/include/cuvs/distance/grammian.hpp +++ b/cpp/include/cuvs/distance/grammian.hpp @@ -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 */ @@ -42,7 +42,9 @@ class GramMatrixBase { public: GramMatrixBase() : legacy_interface(false) {} [[deprecated]] GramMatrixBase(cublasHandle_t cublas_handle) - : cublas_handle(cublas_handle), legacy_interface(true) {} + : cublas_handle(cublas_handle), legacy_interface(true) + { + } virtual ~GramMatrixBase() = default; @@ -320,10 +322,14 @@ class PolynomialKernel : public GramMatrixBase { * @param offset */ PolynomialKernel(exp_t exponent, math_t gain, math_t offset) - : GramMatrixBase(), exponent(exponent), gain(gain), offset(offset) {} + : GramMatrixBase(), exponent(exponent), gain(gain), offset(offset) + { + } [[deprecated]] PolynomialKernel(exp_t exponent, math_t gain, math_t offset, cublasHandle_t handle) - : GramMatrixBase(handle), exponent(exponent), gain(gain), offset(offset) {} + : GramMatrixBase(handle), exponent(exponent), gain(gain), offset(offset) + { + } /** Evaluate kernel matrix using polynomial kernel. * @@ -436,7 +442,9 @@ class TanhKernel : public GramMatrixBase { TanhKernel(math_t gain, math_t offset) : GramMatrixBase(), gain(gain), offset(offset) {} [[deprecated]] TanhKernel(math_t gain, math_t offset, cublasHandle_t handle) - : GramMatrixBase(handle), gain(gain), offset(offset) {} + : GramMatrixBase(handle), gain(gain), offset(offset) + { + } /** Evaluate kernel matrix using tanh kernel. * @@ -554,7 +562,9 @@ class RBFKernel : public GramMatrixBase { RBFKernel(math_t gain) : GramMatrixBase(), gain(gain) {} [[deprecated]] RBFKernel(math_t gain, cublasHandle_t handle) - : GramMatrixBase(handle), gain(gain) {} + : GramMatrixBase(handle), gain(gain) + { + } void matrixRowNormL2(raft::resources const& handle, dense_input_matrix_view_t matrix, diff --git a/cpp/include/cuvs/neighbors/ball_cover.hpp b/cpp/include/cuvs/neighbors/ball_cover.hpp index 77a1bc1d7c..039df42672 100644 --- a/cpp/include/cuvs/neighbors/ball_cover.hpp +++ b/cpp/include/cuvs/neighbors/ball_cover.hpp @@ -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 */ diff --git a/cpp/include/cuvs/neighbors/hnsw.hpp b/cpp/include/cuvs/neighbors/hnsw.hpp index b30cf423de..f35f7e5e82 100644 --- a/cpp/include/cuvs/neighbors/hnsw.hpp +++ b/cpp/include/cuvs/neighbors/hnsw.hpp @@ -738,9 +738,9 @@ void extend(raft::resources const& res, */ struct search_params : cuvs::neighbors::search_params { - int ef = 200; // size of the candidate list - int num_threads = 0; // number of host threads to use for concurrent searches. Value of 0 - // automatically maximizes parallelism + int ef = 200; // size of the candidate list + int num_threads = 0; // number of host threads to use for concurrent searches. Value of 0 + // automatically maximizes parallelism }; /** diff --git a/cpp/include/cuvs/neighbors/vamana.hpp b/cpp/include/cuvs/neighbors/vamana.hpp index 5e4f9db19a..224d3bd97d 100644 --- a/cpp/include/cuvs/neighbors/vamana.hpp +++ b/cpp/include/cuvs/neighbors/vamana.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 */ @@ -34,7 +34,7 @@ namespace cuvs::neighbors::vamana { template struct codebook_params { int pq_codebook_size = 0; - int pq_dim = 0; + int pq_dim = 0; std::vector pq_encoding_table; std::vector rotation_matrix; };