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