common.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2022, 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 namespace ML {
20 
21 // Dense input uses int64_t until FAISS is updated
22 typedef int64_t knn_indices_dense_t;
23 
25 
31 template <typename value_idx, typename value_t>
32 struct knn_graph {
33  knn_graph(value_idx n_rows_, int n_neighbors_)
34  : n_rows(n_rows_), n_neighbors(n_neighbors_), knn_indices{nullptr}, knn_dists{nullptr}
35  {
36  }
37 
38  knn_graph(value_idx n_rows_, int n_neighbors_, value_idx* knn_indices_, value_t* knn_dists_)
39  : n_rows(n_rows_), n_neighbors(n_neighbors_), knn_indices(knn_indices_), knn_dists(knn_dists_)
40  {
41  }
42 
43  value_idx* knn_indices;
44  value_t* knn_dists;
45 
46  value_idx n_rows;
48 };
49 
55 template <typename T>
57  T* y;
58  int n;
59  int d;
60 
61  manifold_inputs_t(T* y_, int n_, int d_) : y(y_), n(n_), d(d_) {}
62 
63  virtual bool alloc_knn_graph() const = 0;
64 };
65 
70 template <typename T>
72  T* X;
73 
74  manifold_dense_inputs_t(T* x_, T* y_, int n_, int d_) : manifold_inputs_t<T>(y_, n_, d_), X(x_) {}
75 
76  bool alloc_knn_graph() const { return true; }
77 };
78 
84 template <typename value_idx, typename T>
86  value_idx* indptr;
87  value_idx* indices;
88  T* data;
89 
90  size_t nnz;
91 
93  value_idx* indptr_, value_idx* indices_, T* data_, T* y_, size_t nnz_, int n_, int d_)
94  : manifold_inputs_t<T>(y_, n_, d_), indptr(indptr_), indices(indices_), data(data_), nnz(nnz_)
95  {
96  }
97 
98  bool alloc_knn_graph() const { return true; }
99 };
100 
106 template <typename value_idx, typename value_t>
109  value_idx* knn_indices_, value_t* knn_dists_, value_t* y_, int n_, int d_, int n_neighbors_)
110  : manifold_inputs_t<value_t>(y_, n_, d_), knn_graph(n_, n_neighbors_, knn_indices_, knn_dists_)
111  {
112  }
113 
115 
116  bool alloc_knn_graph() const { return false; }
117 };
118 
119 }; // end namespace ML
Definition: dbscan.hpp:30
int64_t knn_indices_dense_t
Definition: common.hpp:22
int knn_indices_sparse_t
Definition: common.hpp:24
Definition: common.hpp:32
int n_neighbors
Definition: common.hpp:47
value_idx n_rows
Definition: common.hpp:46
value_idx * knn_indices
Definition: common.hpp:43
knn_graph(value_idx n_rows_, int n_neighbors_, value_idx *knn_indices_, value_t *knn_dists_)
Definition: common.hpp:38
value_t * knn_dists
Definition: common.hpp:44
knn_graph(value_idx n_rows_, int n_neighbors_)
Definition: common.hpp:33
Definition: common.hpp:71
bool alloc_knn_graph() const
Definition: common.hpp:76
T * X
Definition: common.hpp:72
manifold_dense_inputs_t(T *x_, T *y_, int n_, int d_)
Definition: common.hpp:74
Definition: common.hpp:56
virtual bool alloc_knn_graph() const =0
manifold_inputs_t(T *y_, int n_, int d_)
Definition: common.hpp:61
T * y
Definition: common.hpp:57
int d
Definition: common.hpp:59
int n
Definition: common.hpp:58
Definition: common.hpp:107
bool alloc_knn_graph() const
Definition: common.hpp:116
knn_graph< value_idx, value_t > knn_graph
Definition: common.hpp:114
Definition: common.hpp:85
value_idx * indices
Definition: common.hpp:87
size_t nnz
Definition: common.hpp:90
manifold_sparse_inputs_t(value_idx *indptr_, value_idx *indices_, T *data_, T *y_, size_t nnz_, int n_, int d_)
Definition: common.hpp:92
bool alloc_knn_graph() const
Definition: common.hpp:98
value_idx * indptr
Definition: common.hpp:86
T * data
Definition: common.hpp:88