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 // 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
60 void compareIndices(faiss::Index& refIndex,
61  faiss::Index& testIndex,
62  int numQuery, int dim, int k,
63  const std::string& configMsg,
64  float maxRelativeError = 6e-5f,
65  float pctMaxDiff1 = 0.1f,
66  float pctMaxDiffN = 0.005f);
67 
68 /// Display specific differences in the two (distance, index) lists
69 void compareLists(const float* refDist,
70  const faiss::Index::idx_t* refInd,
71  const float* testDist,
72  const faiss::Index::idx_t* testInd,
73  int dim1, int dim2,
74  const std::string& configMsg,
75  bool printBasicStats, bool printDiffs, bool assertOnErr,
76  float maxRelativeError = 6e-5f,
77  float pctMaxDiff1 = 0.1f,
78  float pctMaxDiffN = 0.005f);
79 
80 } }
long idx_t
all indices are this type
Definition: Index.h:62