Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
TestUtils.h
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // Copyright 2004-present Facebook. All Rights Reserved.
10
11 #pragma once
12
13 #include "../../FaissAssert.h"
14 #include "../../Index.h"
15 #include <initializer_list>
16 #include <memory>
17 #include <string>
18 #include <vector>
19
20 namespace faiss { namespace gpu {
21
22 /// Generates and displays a new seed for the test
23 void newTestSeed();
24
25 /// Uses an explicit seed for the test
26 void setTestSeed(long seed);
27
28 /// Returns the relative error in difference between a and b
29 /// (|a - b| / (0.5 * (|a| + |b|))
30 float relativeError(float a, float b);
31
32 /// Generates a random integer in the range [a, b]
33 int randVal(int a, int b);
34
35 /// Generates a random bool
36 bool randBool();
37
38 /// Select a random value from the given list of values provided as an
39 /// initializer_list
40 template <typename T>
41 T randSelect(std::initializer_list<T> vals) {
42 FAISS_ASSERT(vals.size() > 0);
43 int sel = randVal(0, vals.size());
44
45 int i = 0;
46 for (auto v : vals) {
47 if (i++ == sel) {
48 return v;
49 }
50 }
51
52 // should not get here
53 return *vals.begin();
54 }
55
56 /// Generates a collection of random vectors in the range [0, 1]
57 std::vector<float> randVecs(size_t num, size_t dim);
58
59 /// Compare two indices via query for similarity, with a user-specified set of
60 /// query vectors
61 void compareIndices(const std::vector<float>& queryVecs,
62 faiss::Index& refIndex,
63 faiss::Index& testIndex,
64 int numQuery, int dim, int k,
65 const std::string& configMsg,
66 float maxRelativeError = 6e-5f,
67 float pctMaxDiff1 = 0.1f,
68 float pctMaxDiffN = 0.005f);
69
70 /// Compare two indices via query for similarity, generating random query
71 /// vectors
72 void compareIndices(faiss::Index& refIndex,
73 faiss::Index& testIndex,
74 int numQuery, int dim, int k,
75 const std::string& configMsg,
76 float maxRelativeError = 6e-5f,
77 float pctMaxDiff1 = 0.1f,
78 float pctMaxDiffN = 0.005f);
79
80 /// Display specific differences in the two (distance, index) lists
81 void compareLists(const float* refDist,
82 const faiss::Index::idx_t* refInd,
83 const float* testDist,
84 const faiss::Index::idx_t* testInd,
85 int dim1, int dim2,
86 const std::string& configMsg,
87 bool printBasicStats, bool printDiffs, bool assertOnErr,
88 float maxRelativeError = 6e-5f,
89 float pctMaxDiff1 = 0.1f,
90 float pctMaxDiffN = 0.005f);
91
92 } }
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::Index
Definition: Index.h:60