Loading [MathJax]/extensions/tex2jax.js
cuML C++ API  24.04
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
runner.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "detail/condense.cuh"
20 #include "detail/extract.cuh"
21 #include "detail/reachability.cuh"
23 
24 #include <cuml/cluster/hdbscan.hpp>
25 #include <cuml/common/logger.hpp>
26 
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>
34 
35 #include <rmm/device_uvector.hpp>
36 
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>
42 
43 namespace ML {
44 namespace HDBSCAN {
45 
53 template <typename value_idx, typename value_t>
55  value_t* core_dists;
56  value_idx m;
57 
59 
60  FixConnectivitiesRedOp(value_t* core_dists_, value_idx m_) : core_dists(core_dists_), m(m_){};
61 
62  typedef typename raft::KeyValuePair<value_idx, value_t> KVP;
63  DI void operator()(value_idx rit, KVP* out, const KVP& other) const
64  {
65  if (rit < m && other.value < std::numeric_limits<value_t>::max()) {
66  value_t core_dist_rit = core_dists[rit];
67  value_t core_dist_other = max(core_dist_rit, max(core_dists[other.key], other.value));
68 
69  value_t core_dist_out;
70  if (out->key > -1) {
71  core_dist_out = max(core_dist_rit, max(core_dists[out->key], out->value));
72  } else {
73  core_dist_out = out->value;
74  }
75 
76  bool smaller = core_dist_other < core_dist_out;
77  out->key = smaller ? other.key : out->key;
78  out->value = smaller ? core_dist_other : core_dist_out;
79  }
80  }
81 
82  DI KVP operator()(value_idx rit, const KVP& a, const KVP& b) const
83  {
84  if (rit < m && a.key > -1) {
85  value_t core_dist_rit = core_dists[rit];
86  value_t core_dist_a = max(core_dist_rit, max(core_dists[a.key], a.value));
87 
88  value_t core_dist_b;
89  if (b.key > -1) {
90  core_dist_b = max(core_dist_rit, max(core_dists[b.key], b.value));
91  } else {
92  core_dist_b = b.value;
93  }
94 
95  return core_dist_a < core_dist_b ? KVP(a.key, core_dist_a) : KVP(b.key, core_dist_b);
96  }
97 
98  return b;
99  }
100 
101  DI void init(value_t* out, value_t maxVal) const { *out = maxVal; }
102  DI void init(KVP* out, value_t maxVal) const
103  {
104  out->key = -1;
105  out->value = maxVal;
106  }
107 
108  DI void init_key(value_t& out, value_idx idx) const { return; }
109  DI void init_key(KVP& out, value_idx idx) const { out.key = idx; }
110 
111  DI value_t get_value(KVP& out) const { return out.value; }
112  DI value_t get_value(value_t& out) const { return out; }
113 
114  void gather(const raft::resources& handle, value_idx* map)
115  {
116  auto tmp_core_dists = raft::make_device_vector<value_t>(handle, m);
117  thrust::gather(raft::resource::get_thrust_policy(handle),
118  map,
119  map + m,
120  core_dists,
121  tmp_core_dists.data_handle());
122  raft::copy_async(
123  core_dists, tmp_core_dists.data_handle(), m, raft::resource::get_cuda_stream(handle));
124  }
125 
126  void scatter(const raft::resources& handle, value_idx* map)
127  {
128  auto tmp_core_dists = raft::make_device_vector<value_t>(handle, m);
129  thrust::scatter(raft::resource::get_thrust_policy(handle),
130  core_dists,
131  core_dists + m,
132  map,
133  tmp_core_dists.data_handle());
134  raft::copy_async(
135  core_dists, tmp_core_dists.data_handle(), m, raft::resource::get_cuda_stream(handle));
136  }
137 };
138 
155 template <typename value_idx = int64_t, typename value_t = float>
156 void build_linkage(const raft::handle_t& handle,
157  const value_t* X,
158  size_t m,
159  size_t n,
160  raft::distance::DistanceType metric,
162  value_t* core_dists,
164 {
165  auto stream = handle.get_stream();
166 
170  rmm::device_uvector<value_idx> mutual_reachability_indptr(m + 1, stream);
171  // Note that (min_samples+1) is parsed while allocating space for the COO matrix and to the
172  // mutual_reachability_graph function. This was done to account for self-loops in the knn graph
173  // and be consistent with Scikit learn Contrib.
174  raft::sparse::COO<value_t, value_idx> mutual_reachability_coo(stream,
175  (params.min_samples + 1) * m * 2);
176 
177  detail::Reachability::mutual_reachability_graph(handle,
178  X,
179  (size_t)m,
180  (size_t)n,
181  metric,
182  params.min_samples + 1,
183  params.alpha,
184  mutual_reachability_indptr.data(),
185  core_dists,
186  mutual_reachability_coo);
187 
192  rmm::device_uvector<value_idx> color(m, stream);
193  FixConnectivitiesRedOp<value_idx, value_t> red_op(core_dists, m);
194  // during knn graph connection
195  raft::cluster::detail::build_sorted_mst(handle,
196  X,
197  mutual_reachability_indptr.data(),
198  mutual_reachability_coo.cols(),
199  mutual_reachability_coo.vals(),
200  m,
201  n,
202  out.get_mst_src(),
203  out.get_mst_dst(),
204  out.get_mst_weights(),
205  color.data(),
206  mutual_reachability_coo.nnz,
207  red_op,
208  metric,
209  (size_t)10);
210 
214  size_t n_edges = m - 1;
215 
216  raft::cluster::detail::build_dendrogram_host(handle,
217  out.get_mst_src(),
218  out.get_mst_dst(),
219  out.get_mst_weights(),
220  n_edges,
221  out.get_children(),
222  out.get_deltas(),
223  out.get_sizes());
224 }
225 
226 template <typename value_idx = int64_t, typename value_t = float>
227 void _fit_hdbscan(const raft::handle_t& handle,
228  const value_t* X,
229  size_t m,
230  size_t n,
231  raft::distance::DistanceType metric,
233  value_idx* labels,
234  value_t* core_dists,
236 {
237  auto stream = handle.get_stream();
238  auto exec_policy = handle.get_thrust_policy();
239 
240  int min_cluster_size = params.min_cluster_size;
241 
242  build_linkage(handle, X, m, n, metric, params, core_dists, out);
243 
248  out.get_children(),
249  out.get_deltas(),
250  out.get_sizes(),
251  min_cluster_size,
252  m,
253  out.get_condensed_tree());
254 
259  rmm::device_uvector<value_t> tree_stabilities(out.get_condensed_tree().get_n_clusters(),
260  handle.get_stream());
261 
262  rmm::device_uvector<value_idx> label_map(out.get_condensed_tree().get_n_clusters(),
263  handle.get_stream());
264  value_idx n_selected_clusters =
265  detail::Extract::extract_clusters(handle,
266  out.get_condensed_tree(),
267  m,
268  labels,
269  tree_stabilities.data(),
270  out.get_probabilities(),
271  label_map.data(),
272  params.cluster_selection_method,
274  params.allow_single_cluster,
275  params.max_cluster_size,
276  params.cluster_selection_epsilon);
277 
278  out.set_n_clusters(n_selected_clusters);
279 
280  auto lambdas_ptr = thrust::device_pointer_cast(out.get_condensed_tree().get_lambdas());
281  value_t max_lambda = *(thrust::max_element(
282  exec_policy, lambdas_ptr, lambdas_ptr + out.get_condensed_tree().get_n_edges()));
283 
284  detail::Stability::get_stability_scores(handle,
285  labels,
286  tree_stabilities.data(),
287  out.get_condensed_tree().get_n_clusters(),
288  max_lambda,
289  m,
290  out.get_stabilities(),
291  label_map.data());
292 
298  thrust::transform(exec_policy,
299  labels,
300  labels + m,
301  out.get_labels(),
302  [label_map = label_map.data()] __device__(value_idx label) {
303  if (label != -1) return label_map[label];
304  return -1;
305  });
306 }
307 
308 }; // end namespace HDBSCAN
309 }; // end namespace ML
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
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 &params, value_t *core_dists, Common::robust_single_linkage_output< value_idx, value_t > &out)
Definition: runner.h:156
void _fit_hdbscan(const raft::handle_t &handle, const value_t *X, size_t m, size_t n, raft::distance::DistanceType metric, Common::HDBSCANParams &params, value_idx *labels, value_t *core_dists, Common::hdbscan_output< value_idx, value_t > &out)
Definition: runner.h:227
math_t max(math_t a, math_t b)
Definition: learning_rate.h:27
void transform(const raft::handle_t &handle, const KMeansParams &params, 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:55
void gather(const raft::resources &handle, value_idx *map)
Definition: runner.h:114
DI value_t get_value(value_t &out) const
Definition: runner.h:112
value_idx m
Definition: runner.h:56
FixConnectivitiesRedOp(value_t *core_dists_, value_idx m_)
Definition: runner.h:60
DI void init_key(KVP &out, value_idx idx) const
Definition: runner.h:109
DI void init_key(value_t &out, value_idx idx) const
Definition: runner.h:108
DI FixConnectivitiesRedOp()
Definition: runner.h:58
DI value_t get_value(KVP &out) const
Definition: runner.h:111
DI KVP operator()(value_idx rit, const KVP &a, const KVP &b) const
Definition: runner.h:82
DI void init(KVP *out, value_t maxVal) const
Definition: runner.h:102
DI void init(value_t *out, value_t maxVal) const
Definition: runner.h:101
DI void operator()(value_idx rit, KVP *out, const KVP &other) const
Definition: runner.h:63
void scatter(const raft::resources &handle, value_idx *map)
Definition: runner.h:126
raft::KeyValuePair< value_idx, value_t > KVP
Definition: runner.h:60