19 #include <cub/cub.cuh>
21 #include <raft/util/cudart_utils.hpp>
23 #include <raft/sparse/convert/csr.cuh>
24 #include <raft/sparse/op/sort.cuh>
28 #include <raft/core/device_mdspan.hpp>
29 #include <raft/label/classlabels.cuh>
30 #include <raft/linalg/matrix_vector_op.cuh>
31 #include <raft/linalg/norm.cuh>
35 #include "../condensed_hierarchy.cu"
38 #include <thrust/copy.h>
39 #include <thrust/execution_policy.h>
40 #include <thrust/for_each.h>
41 #include <thrust/functional.h>
42 #include <thrust/iterator/zip_iterator.h>
43 #include <thrust/reduce.h>
44 #include <thrust/sort.h>
45 #include <thrust/transform.h>
46 #include <thrust/transform_reduce.h>
47 #include <thrust/tuple.h>
49 #include <rmm/device_uvector.hpp>
50 #include <rmm/exec_policy.hpp>
70 template <
typename value_
idx,
typename value_t,
typename CUBReduceFunc>
74 const value_idx* offsets,
76 CUBReduceFunc cub_reduce_func)
78 rmm::device_uvector<char> d_temp_storage(0, stream);
79 size_t temp_storage_bytes = 0;
81 nullptr, temp_storage_bytes, in, out, n_segments, offsets, offsets + 1, stream,
false);
82 d_temp_storage.resize(temp_storage_bytes, stream);
84 cub_reduce_func(d_temp_storage.data(),
104 template <
typename value_
idx,
typename value_t>
108 auto stream = handle.get_stream();
109 auto thrust_policy = handle.get_thrust_policy();
115 value_idx cluster_tree_edges = thrust::transform_reduce(
119 [=] __device__(value_idx a) {
return a > 1; },
121 thrust::plus<value_idx>());
124 rmm::device_uvector<value_idx> cluster_parents(cluster_tree_edges, stream);
125 rmm::device_uvector<value_idx> cluster_children(cluster_tree_edges, stream);
126 rmm::device_uvector<value_t> cluster_lambdas(cluster_tree_edges, stream);
127 rmm::device_uvector<value_idx> cluster_sizes(cluster_tree_edges, stream);
129 auto in = thrust::make_zip_iterator(thrust::make_tuple(parents, children, lambdas, sizes));
131 auto out = thrust::make_zip_iterator(thrust::make_tuple(
132 cluster_parents.data(), cluster_children.data(), cluster_lambdas.data(), cluster_sizes.data()));
134 thrust::copy_if(thrust_policy,
139 [=] __device__(value_idx a) {
return a > 1; });
143 cluster_parents.begin(),
144 cluster_parents.end(),
145 cluster_parents.begin(),
146 [n_leaves] __device__(value_idx a) {
return a - n_leaves; });
148 cluster_children.begin(),
149 cluster_children.end(),
150 cluster_children.begin(),
151 [n_leaves] __device__(value_idx a) {
return a - n_leaves; });
157 std::move(cluster_parents),
158 std::move(cluster_children),
159 std::move(cluster_lambdas),
160 std::move(cluster_sizes));
172 template <
typename value_
idx,
typename value_t>
175 value_idx* sorted_parents,
178 auto stream = handle.get_stream();
179 auto thrust_policy = handle.get_thrust_policy();
188 auto index_op = [n_leaves] __device__(
const auto& x) {
return x - n_leaves; };
190 thrust_policy, sorted_parents, sorted_parents + n_edges, sorted_parents, index_op);
192 raft::sparse::convert::sorted_coo_to_csr(sorted_parents, n_edges, indptr, n_clusters + 1, stream);
195 template <
typename value_
idx,
typename value_t>
196 void normalize(value_t* data, value_idx n,
size_t m, cudaStream_t stream)
198 rmm::device_uvector<value_t> sums(m, stream);
201 raft::linalg::rowNorm<value_t, size_t>(
202 sums.data(), data, (
size_t)n, m, raft::linalg::L1Norm,
true, stream);
205 raft::linalg::matrixVectorOp(
207 const_cast<value_t*
>(data),
213 [] __device__(value_t mat_in, value_t vec_in) {
return mat_in / vec_in; },
227 template <
typename value_
idx,
typename value_t>
228 void softmax(
const raft::handle_t& handle, value_t* data, value_idx n,
size_t m)
230 rmm::device_uvector<value_t> linf_norm(m, handle.get_stream());
232 auto data_const_view =
233 raft::make_device_matrix_view<const value_t, value_idx, raft::row_major>(data, (
int)m, n);
235 raft::make_device_matrix_view<value_t, value_idx, raft::row_major>(data, (
int)m, n);
236 auto linf_norm_const_view =
237 raft::make_device_vector_view<const value_t, value_idx>(linf_norm.data(), (
int)m);
238 auto linf_norm_view = raft::make_device_vector_view<value_t, value_idx>(linf_norm.data(), (
int)m);
240 raft::linalg::norm(handle,
243 raft::linalg::LinfNorm,
244 raft::linalg::Apply::ALONG_ROWS);
246 raft::linalg::matrix_vector_op(
249 linf_norm_const_view,
251 raft::linalg::Apply::ALONG_COLUMNS,
252 [] __device__(value_t mat_in, value_t vec_in) {
return exp(mat_in - vec_in); });
Definition: hdbscan.hpp:40
value_idx * get_sizes()
Definition: hdbscan.hpp:118
value_t * get_lambdas()
Definition: hdbscan.hpp:117
value_idx get_n_leaves() const
Definition: hdbscan.hpp:121
value_idx get_n_edges()
Definition: hdbscan.hpp:119
value_idx * get_children()
Definition: hdbscan.hpp:116
int get_n_clusters()
Definition: hdbscan.hpp:120
value_idx * get_parents()
Definition: hdbscan.hpp:115
Common::CondensedHierarchy< value_idx, value_t > make_cluster_tree(const raft::handle_t &handle, Common::CondensedHierarchy< value_idx, value_t > &condensed_tree)
Definition: utils.h:105
void softmax(const raft::handle_t &handle, value_t *data, value_idx n, size_t m)
Definition: utils.h:228
void normalize(value_t *data, value_idx n, size_t m, cudaStream_t stream)
Definition: utils.h:196
void cub_segmented_reduce(const value_t *in, value_t *out, int n_segments, const value_idx *offsets, cudaStream_t stream, CUBReduceFunc cub_reduce_func)
Definition: utils.h:71
void parent_csr(const raft::handle_t &handle, Common::CondensedHierarchy< value_idx, value_t > &condensed_tree, value_idx *sorted_parents, value_idx *indptr)
Definition: utils.h:173
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:27