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
|
#include <iostream>
#include <fstream>
#include <queue>
#include <chrono>
#include "../../hnswlib/hnswlib.h"
#include <unordered_set>
using namespace std;
using namespace hnswlib;
class StopW {
std::chrono::steady_clock::time_point time_begin;
public:
StopW() {
time_begin = std::chrono::steady_clock::now();
}
float getElapsedTimeMicro() {
std::chrono::steady_clock::time_point time_end = std::chrono::steady_clock::now();
return (std::chrono::duration_cast<std::chrono::microseconds>(time_end - time_begin).count());
}
void reset() {
time_begin = std::chrono::steady_clock::now();
}
};
/*
* Author: David Robert Nadeau
* Site: http://NadeauSoftware.com/
* License: Creative Commons Attribution 3.0 Unported License
* http://creativecommons.org/licenses/by/3.0/deed.en_US
*/
#if defined(_WIN32)
#include <windows.h>
#include <psapi.h>
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/resource.h>
#if defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
#include <fcntl.h>
#include <procfs.h>
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
#endif
#else
#error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
#endif
/**
* Returns the peak (maximum so far) resident set size (physical
* memory use) measured in bytes, or zero if the value cannot be
* determined on this OS.
*/
static size_t getPeakRSS() {
#if defined(_WIN32)
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.PeakWorkingSetSize;
#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
/* AIX and Solaris ------------------------------------------ */
struct psinfo psinfo;
int fd = -1;
if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
return (size_t)0L; /* Can't open? */
if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
close(fd);
return (size_t)0L; /* Can't read? */
}
close(fd);
return (size_t)(psinfo.pr_rssize * 1024L);
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
/* BSD, Linux, and OSX -------------------------------------- */
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
#if defined(__APPLE__) && defined(__MACH__)
return (size_t)rusage.ru_maxrss;
#else
return (size_t) (rusage.ru_maxrss * 1024L);
#endif
#else
/* Unknown OS ----------------------------------------------- */
return (size_t)0L; /* Unsupported. */
#endif
}
/**
* Returns the current resident set size (physical memory use) measured
* in bytes, or zero if the value cannot be determined on this OS.
*/
static size_t getCurrentRSS() {
#if defined(_WIN32)
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.WorkingSetSize;
#elif defined(__APPLE__) && defined(__MACH__)
/* OSX ------------------------------------------------------ */
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
(task_info_t)&info, &infoCount) != KERN_SUCCESS)
return (size_t)0L; /* Can't access? */
return (size_t)info.resident_size;
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
/* Linux ---------------------------------------------------- */
long rss = 0L;
FILE *fp = NULL;
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
return (size_t) 0L; /* Can't open? */
if (fscanf(fp, "%*s%ld", &rss) != 1) {
fclose(fp);
return (size_t) 0L; /* Can't read? */
}
fclose(fp);
return (size_t) rss * (size_t) sysconf(_SC_PAGESIZE);
#else
/* AIX, BSD, Solaris, and Unknown OS ------------------------ */
return (size_t)0L; /* Unsupported. */
#endif
}
static void
get_gt(
unsigned int *massQA,
unsigned char *massQ,
unsigned char *mass,
size_t vecsize,
size_t qsize,
L2SpaceI &l2space,
size_t vecdim,
vector<std::priority_queue<std::pair<int, labeltype>>> &answers,
size_t k) {
(vector<std::priority_queue<std::pair<int, labeltype >>>(qsize)).swap(answers);
DISTFUNC<int> fstdistfunc_ = l2space.get_dist_func();
cout << qsize << "\n";
for (int i = 0; i < qsize; i++) {
for (int j = 0; j < k; j++) {
answers[i].emplace(0.0f, massQA[1000 * i + j]);
}
}
}
static float
test_approx(
unsigned char *massQ,
size_t vecsize,
size_t qsize,
HierarchicalNSW<int> &appr_alg,
size_t vecdim,
vector<std::priority_queue<std::pair<int, labeltype>>> &answers,
size_t k) {
size_t correct = 0;
size_t total = 0;
// uncomment to test in parallel mode:
//#pragma omp parallel for
for (int i = 0; i < qsize; i++) {
std::priority_queue<std::pair<int, labeltype >> result = appr_alg.searchKnn(massQ + vecdim * i, k);
std::priority_queue<std::pair<int, labeltype >> gt(answers[i]);
unordered_set<labeltype> g;
total += gt.size();
while (gt.size()) {
g.insert(gt.top().second);
gt.pop();
}
while (result.size()) {
if (g.find(result.top().second) != g.end()) {
correct++;
} else {
}
result.pop();
}
}
return 1.0f * correct / total;
}
static void
test_vs_recall(
unsigned char *massQ,
size_t vecsize,
size_t qsize,
HierarchicalNSW<int> &appr_alg,
size_t vecdim,
vector<std::priority_queue<std::pair<int, labeltype>>> &answers,
size_t k) {
vector<size_t> efs; // = { 10,10,10,10,10 };
for (int i = k; i < 30; i++) {
efs.push_back(i);
}
for (int i = 30; i < 100; i += 10) {
efs.push_back(i);
}
for (int i = 100; i < 500; i += 40) {
efs.push_back(i);
}
for (size_t ef : efs) {
appr_alg.setEf(ef);
StopW stopw = StopW();
float recall = test_approx(massQ, vecsize, qsize, appr_alg, vecdim, answers, k);
float time_us_per_query = stopw.getElapsedTimeMicro() / qsize;
cout << ef << "\t" << recall << "\t" << time_us_per_query << " us\n";
if (recall > 1.0) {
cout << recall << "\t" << time_us_per_query << " us\n";
break;
}
}
}
inline bool exists_test(const std::string &name) {
ifstream f(name.c_str());
return f.good();
}
void sift_test1B() {
int subset_size_milllions = 200;
int efConstruction = 40;
int M = 16;
size_t vecsize = subset_size_milllions * 1000000;
size_t qsize = 10000;
size_t vecdim = 128;
char path_index[1024];
char path_gt[1024];
const char *path_q = "../bigann/bigann_query.bvecs";
const char *path_data = "../bigann/bigann_base.bvecs";
snprintf(path_index, sizeof(path_index), "sift1b_%dm_ef_%d_M_%d.bin", subset_size_milllions, efConstruction, M);
snprintf(path_gt, sizeof(path_gt), "../bigann/gnd/idx_%dM.ivecs", subset_size_milllions);
unsigned char *massb = new unsigned char[vecdim];
cout << "Loading GT:\n";
ifstream inputGT(path_gt, ios::binary);
unsigned int *massQA = new unsigned int[qsize * 1000];
for (int i = 0; i < qsize; i++) {
int t;
inputGT.read((char *) &t, 4);
inputGT.read((char *) (massQA + 1000 * i), t * 4);
if (t != 1000) {
cout << "err";
return;
}
}
inputGT.close();
cout << "Loading queries:\n";
unsigned char *massQ = new unsigned char[qsize * vecdim];
ifstream inputQ(path_q, ios::binary);
for (int i = 0; i < qsize; i++) {
int in = 0;
inputQ.read((char *) &in, 4);
if (in != 128) {
cout << "file error";
exit(1);
}
inputQ.read((char *) massb, in);
for (int j = 0; j < vecdim; j++) {
massQ[i * vecdim + j] = massb[j];
}
}
inputQ.close();
unsigned char *mass = new unsigned char[vecdim];
ifstream input(path_data, ios::binary);
int in = 0;
L2SpaceI l2space(vecdim);
HierarchicalNSW<int> *appr_alg;
if (exists_test(path_index)) {
cout << "Loading index from " << path_index << ":\n";
appr_alg = new HierarchicalNSW<int>(&l2space, path_index, false);
cout << "Actual memory usage: " << getCurrentRSS() / 1000000 << " Mb \n";
} else {
cout << "Building index:\n";
appr_alg = new HierarchicalNSW<int>(&l2space, vecsize, M, efConstruction);
input.read((char *) &in, 4);
if (in != 128) {
cout << "file error";
exit(1);
}
input.read((char *) massb, in);
for (int j = 0; j < vecdim; j++) {
mass[j] = massb[j] * (1.0f);
}
appr_alg->addPoint((void *) (massb), (size_t) 0);
int j1 = 0;
StopW stopw = StopW();
StopW stopw_full = StopW();
size_t report_every = 100000;
#pragma omp parallel for
for (int i = 1; i < vecsize; i++) {
unsigned char mass[128];
int j2 = 0;
#pragma omp critical
{
input.read((char *) &in, 4);
if (in != 128) {
cout << "file error";
exit(1);
}
input.read((char *) massb, in);
for (int j = 0; j < vecdim; j++) {
mass[j] = massb[j];
}
j1++;
j2 = j1;
if (j1 % report_every == 0) {
cout << j1 / (0.01 * vecsize) << " %, "
<< report_every / (1000.0 * 1e-6 * stopw.getElapsedTimeMicro()) << " kips " << " Mem: "
<< getCurrentRSS() / 1000000 << " Mb \n";
stopw.reset();
}
}
appr_alg->addPoint((void *) (mass), (size_t) j2);
}
input.close();
cout << "Build time:" << 1e-6 * stopw_full.getElapsedTimeMicro() << " seconds\n";
appr_alg->saveIndex(path_index);
}
vector<std::priority_queue<std::pair<int, labeltype >>> answers;
size_t k = 1;
cout << "Parsing gt:\n";
get_gt(massQA, massQ, mass, vecsize, qsize, l2space, vecdim, answers, k);
cout << "Loaded gt\n";
for (int i = 0; i < 1; i++)
test_vs_recall(massQ, vecsize, qsize, *appr_alg, vecdim, answers, k);
cout << "Actual memory usage: " << getCurrentRSS() / 1000000 << " Mb \n";
return;
}
|