27 #include <raft/cluster/detail/agglomerative.cuh>
28 #include <raft/cluster/detail/mst.cuh>
29 #include <raft/core/handle.hpp>
30 #include <raft/core/kvp.hpp>
31 #include <raft/core/resource/thrust_policy.hpp>
32 #include <raft/sparse/coo.hpp>
33 #include <raft/util/cudart_utils.hpp>
35 #include <rmm/device_uvector.hpp>
37 #include <thrust/device_ptr.h>
38 #include <thrust/extrema.h>
39 #include <thrust/gather.h>
40 #include <thrust/scatter.h>
41 #include <thrust/transform.h>
43 #include <cuvs/neighbors/reachability.hpp>
55 template <
typename value_
idx,
typename value_t>
64 typedef typename raft::KeyValuePair<value_idx, value_t>
KVP;
69 value_t core_dist_other =
max(core_dist_rit,
max(
core_dists[other.key], other.value));
71 value_t core_dist_out;
75 core_dist_out = out->value;
78 bool smaller = core_dist_other < core_dist_out;
79 out->key = smaller ? other.key : out->key;
80 out->value = smaller ? core_dist_other : core_dist_out;
86 if (rit < m && a.key > -1) {
94 core_dist_b = b.value;
97 return core_dist_a < core_dist_b ?
KVP(a.key, core_dist_a) :
KVP(b.key, core_dist_b);
103 DI
void init(value_t* out, value_t maxVal)
const { *out = maxVal; }
110 DI
void init_key(value_t& out, value_idx idx)
const {
return; }
111 DI
void init_key(
KVP& out, value_idx idx)
const { out.key = idx; }
114 DI value_t
get_value(value_t& out)
const {
return out; }
116 void gather(
const raft::resources& handle, value_idx* map)
118 auto tmp_core_dists = raft::make_device_vector<value_t>(handle,
m);
119 thrust::gather(raft::resource::get_thrust_policy(handle),
123 tmp_core_dists.data_handle());
125 core_dists, tmp_core_dists.data_handle(),
m, raft::resource::get_cuda_stream(handle));
128 void scatter(
const raft::resources& handle, value_idx* map)
130 auto tmp_core_dists = raft::make_device_vector<value_t>(handle,
m);
131 thrust::scatter(raft::resource::get_thrust_policy(handle),
135 tmp_core_dists.data_handle());
137 core_dists, tmp_core_dists.data_handle(),
m, raft::resource::get_cuda_stream(handle));
157 template <
typename value_
idx =
int64_t,
typename value_t =
float>
162 raft::distance::DistanceType metric,
167 auto stream = handle.get_stream();
172 rmm::device_uvector<value_idx> mutual_reachability_indptr(m + 1, stream);
176 raft::sparse::COO<value_t, value_idx> mutual_reachability_coo(stream,
177 (
params.min_samples + 1) * m * 2);
179 cuvs::neighbors::reachability::mutual_reachability_graph(
181 raft::make_device_matrix_view<const value_t, int64_t>(X, m, n),
183 raft::make_device_vector_view<value_idx>(mutual_reachability_indptr.data(), m + 1),
184 raft::make_device_vector_view<value_t>(core_dists, m),
185 mutual_reachability_coo,
186 static_cast<cuvs::distance::DistanceType
>(metric),
193 rmm::device_uvector<value_idx> color(m, stream);
196 raft::cluster::detail::build_sorted_mst(handle,
198 mutual_reachability_indptr.data(),
199 mutual_reachability_coo.cols(),
200 mutual_reachability_coo.vals(),
207 mutual_reachability_coo.nnz,
215 size_t n_edges = m - 1;
217 raft::cluster::detail::build_dendrogram_host(handle,
227 template <
typename value_
idx =
int64_t,
typename value_t =
float>
232 raft::distance::DistanceType metric,
238 auto stream = handle.get_stream();
239 auto exec_policy = handle.get_thrust_policy();
241 int min_cluster_size =
params.min_cluster_size;
260 rmm::device_uvector<value_t> tree_stabilities(out.
get_condensed_tree().get_n_clusters(),
261 handle.get_stream());
264 handle.get_stream());
265 value_idx n_selected_clusters =
266 detail::Extract::extract_clusters(handle,
270 tree_stabilities.data(),
273 params.cluster_selection_method,
275 params.allow_single_cluster,
277 params.cluster_selection_epsilon);
281 auto lambdas_ptr = thrust::device_pointer_cast(out.
get_condensed_tree().get_lambdas());
282 value_t max_lambda = *(thrust::max_element(
285 detail::Stability::get_stability_scores(handle,
287 tree_stabilities.data(),
303 [label_map = label_map.data()] __device__(value_idx label) {
304 if (label != -1)
return label_map[label];
Definition: hdbscan.hpp:151
Definition: hdbscan.hpp:253
CondensedHierarchy< value_idx, value_t > & get_condensed_tree()
Definition: hdbscan.hpp:295
value_t * get_stabilities()
Definition: hdbscan.hpp:278
rmm::device_uvector< value_idx > & _get_inverse_label_map()
Definition: hdbscan.hpp:281
void set_n_clusters(int n_clusters_)
Definition: hdbscan.hpp:288
value_t * get_probabilities()
Definition: hdbscan.hpp:277
Definition: hdbscan.hpp:163
value_idx * get_sizes()
Definition: hdbscan.hpp:204
value_idx * get_mst_src()
Definition: hdbscan.hpp:206
value_t * get_mst_weights()
Definition: hdbscan.hpp:208
value_idx * get_mst_dst()
Definition: hdbscan.hpp:207
value_idx * get_labels()
Definition: hdbscan.hpp:202
value_t * get_deltas()
Definition: hdbscan.hpp:205
value_idx * get_children()
Definition: hdbscan.hpp:203
Definition: params.hpp:34
void build_linkage(const raft::handle_t &handle, const value_t *X, size_t m, size_t n, raft::distance::DistanceType metric, Common::HDBSCANParams ¶ms, value_t *core_dists, Common::robust_single_linkage_output< value_idx, value_t > &out)
Definition: runner.h:158
void _fit_hdbscan(const raft::handle_t &handle, const value_t *X, size_t m, size_t n, raft::distance::DistanceType metric, Common::HDBSCANParams ¶ms, value_idx *labels, value_t *core_dists, Common::hdbscan_output< value_idx, value_t > &out)
Definition: runner.h:228
math_t max(math_t a, math_t b)
Definition: learning_rate.h:27
void transform(const raft::handle_t &handle, const KMeansParams ¶ms, const float *centroids, const float *X, int n_samples, int n_features, float *X_new)
Transform X to a cluster-distance space.
Definition: dbscan.hpp:30
void build_condensed_hierarchy(const raft::handle_t &handle, const int *children, const float *delta, const int *sizes, int min_cluster_size, int n_leaves, HDBSCAN::Common::CondensedHierarchy< int, float > &condensed_tree)
value_t * core_dists
Definition: runner.h:57
void gather(const raft::resources &handle, value_idx *map)
Definition: runner.h:116
DI value_t get_value(value_t &out) const
Definition: runner.h:114
value_idx m
Definition: runner.h:58
FixConnectivitiesRedOp(value_t *core_dists_, value_idx m_)
Definition: runner.h:62
DI void init_key(KVP &out, value_idx idx) const
Definition: runner.h:111
DI void init_key(value_t &out, value_idx idx) const
Definition: runner.h:110
DI FixConnectivitiesRedOp()
Definition: runner.h:60
DI value_t get_value(KVP &out) const
Definition: runner.h:113
DI KVP operator()(value_idx rit, const KVP &a, const KVP &b) const
Definition: runner.h:84
DI void init(KVP *out, value_t maxVal) const
Definition: runner.h:104
DI void init(value_t *out, value_t maxVal) const
Definition: runner.h:103
DI void operator()(value_idx rit, KVP *out, const KVP &other) const
Definition: runner.h:65
void scatter(const raft::resources &handle, value_idx *map)
Definition: runner.h:128
raft::KeyValuePair< value_idx, value_t > KVP
Definition: runner.h:62