-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmConfig.cpp
More file actions
382 lines (362 loc) · 12.7 KB
/
Copy pathAlgorithmConfig.cpp
File metadata and controls
382 lines (362 loc) · 12.7 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "AlgorithmConfig.h"
#include <cerrno>
#include <cmath>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <limits>
#include <string>
#include <unordered_set>
namespace algorithmconfig {
namespace {
namespace fs = std::filesystem;
void setError(std::string* error, const std::string& message) {
if (error != nullptr) {
*error = message;
}
}
std::string trim(const std::string& value) {
const std::size_t begin = value.find_first_not_of(" \t\r\n");
if (begin == std::string::npos) {
return {};
}
const std::size_t end = value.find_last_not_of(" \t\r\n");
return value.substr(begin, end - begin + 1);
}
bool parseSize(const std::string& text, std::size_t& value) {
if (text.empty() || text.front() == '-') {
return false;
}
errno = 0;
char* end = nullptr;
const unsigned long long parsed = std::strtoull(text.c_str(), &end, 10);
if (errno == ERANGE || end == text.c_str() || *end != '\0' ||
parsed > std::numeric_limits<std::size_t>::max()) {
return false;
}
value = static_cast<std::size_t>(parsed);
return true;
}
bool parseUint32(const std::string& text, std::uint32_t& value) {
std::size_t parsed = 0;
if (!parseSize(text, parsed) ||
parsed > std::numeric_limits<std::uint32_t>::max()) {
return false;
}
value = static_cast<std::uint32_t>(parsed);
return true;
}
bool parseFloat(const std::string& text, float& value) {
if (text.empty()) {
return false;
}
errno = 0;
char* end = nullptr;
const float parsed = std::strtof(text.c_str(), &end);
if (errno == ERANGE || end == text.c_str() || *end != '\0' ||
!std::isfinite(parsed)) {
return false;
}
value = parsed;
return true;
}
bool assignValue(
const std::string& section,
const std::string& key,
const std::string& value,
AlgorithmConfig& config) {
if (section == "search") {
if (key == "result_count") {
return parseSize(value, config.search.result_count);
}
if (key == "gamma") {
return parseFloat(value, config.search.gamma);
}
if (key == "entry_point_count") {
return parseSize(value, config.search.entry_point_count);
}
return false;
}
if (section == "quantization") {
if (key == "centroid_count") {
return parseSize(value, config.quantization.centroid_count);
}
if (key == "total_bits") {
return parseSize(value, config.quantization.total_bits);
}
if (key == "navigation_bits") {
return parseSize(value, config.quantization.navigation_bits);
}
return false;
}
if (section == "graph") {
if (key == "graph_degree") {
return parseSize(value, config.graph.graph_degree);
}
if (key == "random_seed") {
return parseUint32(value, config.graph.random_seed);
}
if (key == "ef_construction") {
return parseSize(value, config.graph.ef_construction);
}
if (key == "onng_max_outdegree") {
return parseSize(value, config.graph.onng_max_outdegree);
}
if (key == "onng_max_indegree") {
return parseSize(value, config.graph.onng_max_indegree);
}
if (key == "onng_min_edges") {
return parseSize(value, config.graph.onng_min_edges);
}
if (key == "vamana_refinement_degree") {
return parseSize(value, config.graph.vamana_refinement_degree);
}
if (key == "vamana_refinement_alpha") {
return parseFloat(value, config.graph.vamana_refinement_alpha);
}
if (key == "cache_affinity_window") {
return parseSize(value, config.graph.cache_affinity_window);
}
if (key == "build_thread_limit") {
return parseSize(value, config.graph.build_thread_limit);
}
return false;
}
return false;
}
const std::unordered_set<std::string>& requiredKeys() {
static const std::unordered_set<std::string> keys{
"search.result_count",
"search.gamma",
"search.entry_point_count",
"quantization.centroid_count",
"quantization.total_bits",
"quantization.navigation_bits",
"graph.graph_degree",
"graph.random_seed",
"graph.ef_construction",
"graph.onng_max_outdegree",
"graph.onng_max_indegree",
"graph.onng_min_edges",
"graph.vamana_refinement_degree",
"graph.vamana_refinement_alpha",
"graph.cache_affinity_window",
"graph.build_thread_limit"};
return keys;
}
} // namespace
AlgorithmConfig defaults() {
return AlgorithmConfig{};
}
bool validate(const AlgorithmConfig& config, std::string* error) {
setError(error, "");
const GraphConfig& graph = config.graph;
const SearchConfig& search = config.search;
const QuantizationConfig& quantization = config.quantization;
if (graph.graph_degree == 0 ||
graph.graph_degree > std::numeric_limits<unsigned short>::max()) {
setError(error, "graph.graph_degree must be in [1, 65535]");
return false;
}
if (graph.ef_construction == 0 ||
graph.ef_construction > std::numeric_limits<std::uint32_t>::max()) {
setError(error, "graph.ef_construction must be in [1, UINT32_MAX]");
return false;
}
if (graph.onng_max_outdegree == 0 ||
graph.onng_max_outdegree > graph.graph_degree) {
setError(error, "graph.onng_max_outdegree must be in [1, graph_degree]");
return false;
}
if (graph.onng_max_indegree == 0 ||
graph.onng_max_indegree > std::numeric_limits<std::uint32_t>::max()) {
setError(error, "graph.onng_max_indegree must be in [1, UINT32_MAX]");
return false;
}
if (graph.onng_min_edges > graph.onng_max_outdegree) {
setError(error, "graph.onng_min_edges cannot exceed onng_max_outdegree");
return false;
}
if (graph.vamana_refinement_degree == 0 ||
graph.vamana_refinement_degree > graph.graph_degree) {
setError(error, "graph.vamana_refinement_degree must be in [1, graph_degree]");
return false;
}
if (!std::isfinite(graph.vamana_refinement_alpha) ||
graph.vamana_refinement_alpha < 1.0f) {
setError(error, "graph.vamana_refinement_alpha must be finite and at least 1");
return false;
}
if (graph.cache_affinity_window == 0 ||
graph.cache_affinity_window > std::numeric_limits<unsigned short>::max()) {
setError(error, "graph.cache_affinity_window must be in [1, 65535]");
return false;
}
if (graph.build_thread_limit == 0 ||
graph.build_thread_limit > std::numeric_limits<std::uint32_t>::max()) {
setError(error, "graph.build_thread_limit must be in [1, UINT32_MAX]");
return false;
}
if (search.result_count == 0 ||
search.result_count > static_cast<std::size_t>(std::numeric_limits<int>::max())) {
setError(error, "search.result_count must be in [1, INT_MAX]");
return false;
}
if (!std::isfinite(search.gamma) || search.gamma < 0.0f) {
setError(error, "search.gamma must be finite and non-negative");
return false;
}
if (search.entry_point_count == 0) {
setError(error, "search.entry_point_count must be positive");
return false;
}
if (quantization.centroid_count == 0 ||
quantization.centroid_count > std::numeric_limits<std::uint32_t>::max()) {
setError(error, "quantization.centroid_count must be in [1, UINT32_MAX]");
return false;
}
if (quantization.total_bits < 1 || quantization.total_bits > 9) {
setError(error, "quantization.total_bits must be in [1, 9]");
return false;
}
if (quantization.navigation_bits != 1 &&
quantization.navigation_bits != 4) {
setError(error, "quantization.navigation_bits must be 1 or 4");
return false;
}
if (quantization.navigation_bits > quantization.total_bits) {
setError(error, "quantization.navigation_bits cannot exceed total_bits");
return false;
}
return true;
}
bool load(
const std::string& path,
AlgorithmConfig& config,
std::string* error) {
setError(error, "");
std::ifstream input(path);
if (!input.is_open()) {
setError(error, "cannot open algorithm config: " + path);
return false;
}
AlgorithmConfig parsed = defaults();
std::unordered_set<std::string> seen;
std::string section;
std::string line;
std::size_t line_number = 0;
while (std::getline(input, line)) {
++line_number;
const std::size_t comment = line.find_first_of("#;");
if (comment != std::string::npos) {
line.erase(comment);
}
line = trim(line);
if (line.empty()) {
continue;
}
if (line.front() == '[' && line.back() == ']') {
section = trim(line.substr(1, line.size() - 2));
if (section != "search" && section != "quantization" &&
section != "graph") {
setError(
error,
"line " + std::to_string(line_number) +
": unknown section [" + section + "]");
return false;
}
continue;
}
if (section.empty()) {
setError(
error,
"line " + std::to_string(line_number) +
": key appears before a section");
return false;
}
const std::size_t separator = line.find('=');
if (separator == std::string::npos || line.find('=', separator + 1) != std::string::npos) {
setError(
error,
"line " + std::to_string(line_number) +
": expected key = value");
return false;
}
const std::string key = trim(line.substr(0, separator));
const std::string value = trim(line.substr(separator + 1));
const std::string qualified = section + "." + key;
if (requiredKeys().count(qualified) == 0) {
setError(
error,
"line " + std::to_string(line_number) +
": unknown key " + qualified);
return false;
}
if (!seen.insert(qualified).second) {
setError(
error,
"line " + std::to_string(line_number) +
": duplicate key " + qualified);
return false;
}
if (!assignValue(section, key, value, parsed)) {
setError(
error,
"line " + std::to_string(line_number) +
": invalid value for " + qualified);
return false;
}
}
if (input.bad()) {
setError(error, "failed while reading algorithm config: " + path);
return false;
}
for (const std::string& key : requiredKeys()) {
if (seen.count(key) == 0) {
setError(error, "missing required key " + key);
return false;
}
}
if (!validate(parsed, error)) {
return false;
}
config = parsed;
return true;
}
std::string defaultPathForExecutable(const char* executable_path) {
auto find_config_root = [](fs::path directory) {
while (!directory.empty()) {
std::error_code algorithm_error;
std::error_code dataset_error;
const fs::path algorithm = directory / "AlgorithmConfig.ini";
const fs::path dataset = directory / "DatasetConfig.ini";
if (fs::exists(algorithm, algorithm_error) && !algorithm_error &&
fs::exists(dataset, dataset_error) && !dataset_error) {
return algorithm.lexically_normal();
}
const fs::path parent = directory.parent_path();
if (parent == directory) {
break;
}
directory = parent;
}
return fs::path{};
};
const fs::path from_working_directory =
find_config_root(fs::current_path());
if (!from_working_directory.empty()) {
return from_working_directory.string();
}
const fs::path executable = executable_path == nullptr
? fs::path{}
: fs::absolute(fs::path(executable_path));
const fs::path directory = executable.has_parent_path()
? executable.parent_path()
: fs::current_path();
const fs::path from_executable = find_config_root(directory);
if (!from_executable.empty()) {
return from_executable.string();
}
return (directory / "AlgorithmConfig.ini").lexically_normal().string();
}
} // namespace algorithmconfig