-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf.hpp
213 lines (194 loc) · 7.1 KB
/
bf.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <queue>
#include <vector>
#include "distance.hpp"
namespace avs {
class KNNSearch {
int32_t _dim;
int32_t _batch_size;
avs::matf32_t _dataset;
dnnl::engine engine;
dnnl::stream stream;
public:
void init_onednn() {
engine = dnnl::engine(dnnl::engine::kind::cpu, 0);
stream = dnnl::stream(engine);
}
KNNSearch(int32_t dim, int32_t batch_size)
: _dim(dim), _batch_size(batch_size) {
init_onednn();
if (!avs::is_amxbf16_supported()) {
std::cout << "Intel AMX unavailable" << std::endl;
}
}
void add(avs::vecf32_t point) { _dataset.push_back(point); }
std::pair<int32_t, int32_t> shape() {
return std::make_pair(_dataset.size(), _dataset[0].size());
}
// Intel AMX versions
avs::matf32_t search_ip_amx(matf32_t queries, int32_t top_k) {
std::vector<std::vector<float>> results(queries.size(),
std::vector<float>(top_k, 0.0f));
std::unordered_map<int32_t, std::priority_queue<float, std::vector<float>,
std::greater<float>>>
map;
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::matf32_t distances =
avs::ip_distance_amx(queries, curr_batch, engine, stream);
for (int32_t i = 0; i < distances.size(); i++) {
for (int32_t j = 0; j < distances[0].size(); j++) {
map[i].push(distances[i][j]);
}
}
idx += curr_batch_size;
}
for (int i = 0; i < queries.size(); i++) {
int32_t k_idx = 0;
while (k_idx < top_k) {
results[i][k_idx++] = map[i].top();
map[i].pop();
}
}
return results;
}
void search_ip_amx_perf(matf32_t queries, int32_t top_k) {
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::matf32_t distances =
avs::ip_distance_amx(queries, curr_batch, engine, stream);
idx += curr_batch_size;
}
}
avs::matf32_t search_l2_amx(matf32_t queries, int32_t top_k) {
std::vector<std::vector<float>> results(queries.size(),
std::vector<float>(top_k, 0.0f));
for (int i = 0; i < queries.size(); i++) {
std::priority_queue<float, std::vector<float>, std::greater<float>> pq;
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::vecf32_t distances =
avs::l2_distance_amx(queries[i], curr_batch, engine, stream);
for (auto const &d : distances) {
pq.push(d);
}
idx += curr_batch_size;
}
int32_t k_idx = 0;
while (k_idx < top_k) {
results[i][k_idx++] = pq.top();
pq.pop();
}
}
return results;
}
void search_l2_amx_perf(matf32_t queries, int32_t top_k) {
for (int i = 0; i < queries.size(); i++) {
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::vecf32_t distances =
avs::l2_distance_amx(queries[i], curr_batch, engine, stream);
idx += curr_batch_size;
}
}
}
// Vanilla versions
avs::matf32_t search_l2_vanilla(matf32_t queries, int32_t top_k) {
std::vector<std::vector<float>> results(queries.size(),
std::vector<float>(top_k, 0.0f));
for (int i = 0; i < queries.size(); i++) {
std::priority_queue<float, std::vector<float>, std::greater<float>> pq;
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::vecf32_t distances =
avs::l2_distance_vanilla(queries[i], curr_batch, engine, stream);
for (auto const &d : distances) {
pq.push(d);
}
idx += curr_batch_size;
}
int32_t k_idx = 0;
while (k_idx < top_k) {
results[i][k_idx++] = pq.top();
pq.pop();
}
}
return results;
}
void search_l2_vanilla_perf(matf32_t queries, int32_t top_k) {
for (int i = 0; i < queries.size(); i++) {
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::vecf32_t distances =
avs::l2_distance_vanilla(queries[i], curr_batch, engine, stream);
idx += curr_batch_size;
}
}
}
void search_l2_avx512_perf(matf32_t queries, int32_t top_k) {
for (int i = 0; i < queries.size(); i++) {
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::vecf32_t distances =
avs::l2_distance_avx512(queries[i], curr_batch, engine, stream);
idx += curr_batch_size;
}
}
}
void search_ip_vanilla_perf(matf32_t queries, int32_t top_k) {
for (int i = 0; i < queries.size(); i++) {
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::vecf32_t distances =
avs::ip_distance_vanilla(queries[i], curr_batch, engine, stream);
idx += curr_batch_size;
}
}
}
void search_ip_avx512_perf(matf32_t queries, int32_t top_k) {
for (int i = 0; i < queries.size(); i++) {
int32_t idx = 0;
while (idx < _dataset.size()) {
int32_t curr_batch_size =
std::min(_batch_size, (int32_t)_dataset.size() - idx);
std::vector<std::vector<float>> curr_batch(
_dataset.begin() + idx, _dataset.begin() + idx + curr_batch_size);
avs::vecf32_t distances =
avs::ip_distance_avx512(queries[i], curr_batch, engine, stream);
idx += curr_batch_size;
}
}
}
};
} // namespace avs