-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathvec_sim_debug.cpp
More file actions
200 lines (194 loc) · 9.66 KB
/
vec_sim_debug.cpp
File metadata and controls
200 lines (194 loc) · 9.66 KB
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
/*
* Copyright (c) 2006-Present, Redis Ltd.
* All rights reserved.
*
* Licensed under your choice of the Redis Source Available License 2.0
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
* GNU Affero General Public License v3 (AGPLv3).
*/
#include "vec_sim_debug.h"
#include "VecSim/vec_sim_index.h"
#include "VecSim/algorithms/hnsw/hnsw.h"
#include "VecSim/algorithms/hnsw/hnsw_tiered.h"
#include "VecSim/types/bfloat16.h"
extern "C" int VecSimDebug_GetElementNeighborsInHNSWGraph(VecSimIndex *index, size_t label,
int ***neighborsData) {
// Set as if we return an error, and upon success we will set the pointers appropriately.
*neighborsData = nullptr;
VecSimIndexBasicInfo info = index->basicInfo();
if (info.algo != VecSimAlgo_HNSWLIB) {
return VecSimDebugCommandCode_BadIndex;
}
if (!info.isTiered) {
if (info.type == VecSimType_FLOAT32) {
return dynamic_cast<HNSWIndex<float, float> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else if (info.type == VecSimType_FLOAT64) {
return dynamic_cast<HNSWIndex<double, double> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else if (info.type == VecSimType_BFLOAT16) {
return dynamic_cast<HNSWIndex<vecsim_types::bfloat16, float> *>(index)
->getHNSWElementNeighbors(label, neighborsData);
} else if (info.type == VecSimType_FLOAT16) {
return dynamic_cast<HNSWIndex<vecsim_types::float16, float> *>(index)
->getHNSWElementNeighbors(label, neighborsData);
} else if (info.type == VecSimType_INT8) {
return dynamic_cast<HNSWIndex<int8_t, float> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else if (info.type == VecSimType_UINT8) {
return dynamic_cast<HNSWIndex<uint8_t, float> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else {
assert(false && "Invalid data type");
}
} else {
if (info.type == VecSimType_FLOAT32) {
return dynamic_cast<TieredHNSWIndex<float, float> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else if (info.type == VecSimType_FLOAT64) {
return dynamic_cast<TieredHNSWIndex<double, double> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else if (info.type == VecSimType_BFLOAT16) {
return dynamic_cast<TieredHNSWIndex<vecsim_types::bfloat16, float> *>(index)
->getHNSWElementNeighbors(label, neighborsData);
} else if (info.type == VecSimType_FLOAT16) {
return dynamic_cast<TieredHNSWIndex<vecsim_types::float16, float> *>(index)
->getHNSWElementNeighbors(label, neighborsData);
} else if (info.type == VecSimType_INT8) {
return dynamic_cast<TieredHNSWIndex<int8_t, float> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else if (info.type == VecSimType_UINT8) {
return dynamic_cast<TieredHNSWIndex<uint8_t, float> *>(index)->getHNSWElementNeighbors(
label, neighborsData);
} else {
assert(false && "Invalid data type");
}
}
return VecSimDebugCommandCode_BadIndex;
}
extern "C" void VecSimDebug_ReleaseElementNeighborsInHNSWGraph(int **neighborsData) {
if (neighborsData == nullptr) {
return;
}
size_t i = 0;
while (neighborsData[i] != nullptr) {
delete[] neighborsData[i];
i++;
}
delete[] neighborsData;
}
extern "C" int VecSimDebug_GetElementIncomingEdgesInHNSWGraph(VecSimIndex *index, size_t label,
int **incomingEdgesCounts) {
// Set as if we return an error, and upon success we will set the pointers appropriately.
*incomingEdgesCounts = nullptr;
VecSimIndexBasicInfo info = index->basicInfo();
if (info.algo != VecSimAlgo_HNSWLIB) {
return VecSimDebugCommandCode_BadIndex;
}
if (!info.isTiered) {
if (info.type == VecSimType_FLOAT32) {
return dynamic_cast<HNSWIndex<float, float> *>(index)->getHNSWElementIncomingEdges(
label, incomingEdgesCounts);
} else if (info.type == VecSimType_FLOAT64) {
return dynamic_cast<HNSWIndex<double, double> *>(index)->getHNSWElementIncomingEdges(
label, incomingEdgesCounts);
} else if (info.type == VecSimType_BFLOAT16) {
return dynamic_cast<HNSWIndex<vecsim_types::bfloat16, float> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else if (info.type == VecSimType_FLOAT16) {
return dynamic_cast<HNSWIndex<vecsim_types::float16, float> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else if (info.type == VecSimType_INT8) {
return dynamic_cast<HNSWIndex<int8_t, float> *>(index)->getHNSWElementIncomingEdges(
label, incomingEdgesCounts);
} else if (info.type == VecSimType_UINT8) {
return dynamic_cast<HNSWIndex<uint8_t, float> *>(index)->getHNSWElementIncomingEdges(
label, incomingEdgesCounts);
} else {
assert(false && "Invalid data type");
}
} else {
if (info.type == VecSimType_FLOAT32) {
return dynamic_cast<TieredHNSWIndex<float, float> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else if (info.type == VecSimType_FLOAT64) {
return dynamic_cast<TieredHNSWIndex<double, double> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else if (info.type == VecSimType_BFLOAT16) {
return dynamic_cast<TieredHNSWIndex<vecsim_types::bfloat16, float> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else if (info.type == VecSimType_FLOAT16) {
return dynamic_cast<TieredHNSWIndex<vecsim_types::float16, float> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else if (info.type == VecSimType_INT8) {
return dynamic_cast<TieredHNSWIndex<int8_t, float> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else if (info.type == VecSimType_UINT8) {
return dynamic_cast<TieredHNSWIndex<uint8_t, float> *>(index)
->getHNSWElementIncomingEdges(label, incomingEdgesCounts);
} else {
assert(false && "Invalid data type");
}
}
return VecSimDebugCommandCode_BadIndex;
}
extern "C" void VecSimDebug_ReleaseElementIncomingEdgesInHNSWGraph(int *incomingEdgesCounts) {
if (incomingEdgesCounts == nullptr) {
return;
}
delete[] incomingEdgesCounts;
}
extern "C" int VecSimDebug_ShrinkIncomingEdgesInHNSWGraph(VecSimIndex *index, size_t *memorySaved) {
*memorySaved = 0;
VecSimIndexBasicInfo info = index->basicInfo();
if (info.algo != VecSimAlgo_HNSWLIB) {
return VecSimDebugCommandCode_BadIndex;
}
if (!info.isTiered) {
if (info.type == VecSimType_FLOAT32) {
*memorySaved = dynamic_cast<HNSWIndex<float, float> *>(index)->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_FLOAT64) {
*memorySaved =
dynamic_cast<HNSWIndex<double, double> *>(index)->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_BFLOAT16) {
*memorySaved = dynamic_cast<HNSWIndex<vecsim_types::bfloat16, float> *>(index)
->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_FLOAT16) {
*memorySaved = dynamic_cast<HNSWIndex<vecsim_types::float16, float> *>(index)
->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_INT8) {
*memorySaved =
dynamic_cast<HNSWIndex<int8_t, float> *>(index)->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_UINT8) {
*memorySaved =
dynamic_cast<HNSWIndex<uint8_t, float> *>(index)->shrinkAllIncomingEdges();
} else {
assert(false && "Invalid data type");
return VecSimDebugCommandCode_BadIndex;
}
} else {
if (info.type == VecSimType_FLOAT32) {
*memorySaved =
dynamic_cast<TieredHNSWIndex<float, float> *>(index)->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_FLOAT64) {
*memorySaved =
dynamic_cast<TieredHNSWIndex<double, double> *>(index)->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_BFLOAT16) {
*memorySaved = dynamic_cast<TieredHNSWIndex<vecsim_types::bfloat16, float> *>(index)
->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_FLOAT16) {
*memorySaved = dynamic_cast<TieredHNSWIndex<vecsim_types::float16, float> *>(index)
->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_INT8) {
*memorySaved =
dynamic_cast<TieredHNSWIndex<int8_t, float> *>(index)->shrinkAllIncomingEdges();
} else if (info.type == VecSimType_UINT8) {
*memorySaved =
dynamic_cast<TieredHNSWIndex<uint8_t, float> *>(index)->shrinkAllIncomingEdges();
} else {
assert(false && "Invalid data type");
return VecSimDebugCommandCode_BadIndex;
}
}
return VecSimDebugCommandCode_OK;
}