|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +/* This file and macro is there to catch discrepancies on the different |
| 9 | + * compilers used in Faiss align and pack structures. This is a difficult to |
| 10 | + * catch bug. It works by declaring a structure and measuring its size in |
| 11 | + * diffetent compilers from a function. The function can be called from a test. |
| 12 | + * If the size as seen from different compilers changes, it's a guarantee for |
| 13 | + * fireworks at runtime. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace faiss { |
| 19 | +namespace struct_packing_test { |
| 20 | + |
| 21 | +/******************************************************* |
| 22 | + * Fake structure to detect structure packing/alignment |
| 23 | + * issues. |
| 24 | + *******************************************************/ |
| 25 | + |
| 26 | +struct StructPackingTestD { |
| 27 | + int a; |
| 28 | + bool b, c, d; |
| 29 | + long e; |
| 30 | + bool f, g, h, i, j; |
| 31 | +}; |
| 32 | + |
| 33 | +struct StructPackingTestC : StructPackingTestD { |
| 34 | + bool k; |
| 35 | + int l; |
| 36 | + bool m; |
| 37 | +}; |
| 38 | + |
| 39 | +struct StructPackingTestA { |
| 40 | + virtual void* operator()(int) { |
| 41 | + return nullptr; |
| 42 | + } |
| 43 | + |
| 44 | + virtual ~StructPackingTestA() {} |
| 45 | +}; |
| 46 | + |
| 47 | +struct StructPackingTestB : StructPackingTestA { |
| 48 | + StructPackingTestC options; |
| 49 | + std::vector<void*> vres; |
| 50 | + std::vector<int> devices; |
| 51 | + int ncall; |
| 52 | + |
| 53 | + void* operator()(int) override { |
| 54 | + return nullptr; |
| 55 | + } |
| 56 | + virtual ~StructPackingTestB() {} |
| 57 | +}; |
| 58 | + |
| 59 | +// This is the object hierachy of GpuProgressiveDimIndexFactory that initially |
| 60 | +// triggered this error (see PR #4135 and #4136) |
| 61 | + |
| 62 | +enum IndicesOptionsB { |
| 63 | + INDICES_CPU_B = 0, |
| 64 | + INDICES_IVF_B = 1, |
| 65 | + INDICES_32_BIT_B = 2, |
| 66 | + INDICES_64_BIT_B = 3, |
| 67 | +}; |
| 68 | + |
| 69 | +struct GpuClonerOptionsB { |
| 70 | + IndicesOptionsB indicesOptions = INDICES_64_BIT_B; |
| 71 | + |
| 72 | + bool useFloat16CoarseQuantizer = false; |
| 73 | + |
| 74 | + bool useFloat16 = false; |
| 75 | + |
| 76 | + bool usePrecomputed = false; |
| 77 | + |
| 78 | + long reserveVecs = 0; |
| 79 | + |
| 80 | + bool storeTransposed = false; |
| 81 | + |
| 82 | + bool verbose = false; |
| 83 | + |
| 84 | + bool use_cuvs = false; |
| 85 | + bool allowCpuCoarseQuantizer = false; |
| 86 | +}; |
| 87 | + |
| 88 | +struct GpuMultipleClonerOptionsB : public GpuClonerOptionsB { |
| 89 | + bool shard = false; |
| 90 | + int shard_type = 1; |
| 91 | + bool common_ivf_quantizer = false; |
| 92 | +}; |
| 93 | + |
| 94 | +struct ProgressiveDimIndexFactoryB { |
| 95 | + |
| 96 | + virtual void* operator()(int dim) { |
| 97 | + return nullptr; |
| 98 | + } |
| 99 | + |
| 100 | + virtual ~ProgressiveDimIndexFactoryB() {} |
| 101 | +}; |
| 102 | + |
| 103 | +struct GpuProgressiveDimIndexFactoryB : ProgressiveDimIndexFactoryB { |
| 104 | + GpuMultipleClonerOptionsB options; |
| 105 | + std::vector<void*> vres; |
| 106 | + std::vector<int> devices; |
| 107 | + int ncall; |
| 108 | + |
| 109 | + explicit GpuProgressiveDimIndexFactoryB(int ngpu) {} |
| 110 | + |
| 111 | + void* operator()(int dim) override { |
| 112 | + return nullptr; |
| 113 | + } |
| 114 | + |
| 115 | + virtual ~GpuProgressiveDimIndexFactoryB() override {} |
| 116 | +}; |
| 117 | + |
| 118 | +} // namespace struct_packing_test |
| 119 | + |
| 120 | +} // namespace faiss |
| 121 | + |
| 122 | +// body of function should be |
| 123 | +// int function_name (int q) STRUCT_PACKING_FUNCTION_BODY |
| 124 | + |
| 125 | +#define STRUCT_PACKING_FUNCTION_BODY \ |
| 126 | + { \ |
| 127 | + struct_packing_test::StructPackingTestB sb; \ |
| 128 | + switch (q) { \ |
| 129 | + case 0: \ |
| 130 | + return sizeof(struct_packing_test::StructPackingTestB); \ |
| 131 | + case 1: \ |
| 132 | + return (char*)&sb.ncall - (char*)&sb; \ |
| 133 | + case 2: \ |
| 134 | + return sizeof(struct_packing_test::StructPackingTestD); \ |
| 135 | + case 3: \ |
| 136 | + return sizeof(struct_packing_test::StructPackingTestC); \ |
| 137 | + case 4: \ |
| 138 | + return sizeof(struct_packing_test::StructPackingTestB); \ |
| 139 | + case 5: \ |
| 140 | + return sizeof(struct_packing_test::StructPackingTestA); \ |
| 141 | + case 6: \ |
| 142 | + return sizeof(struct_packing_test::IndicesOptionsB); \ |
| 143 | + case 7: \ |
| 144 | + return sizeof(struct_packing_test::GpuMultipleClonerOptionsB); \ |
| 145 | + case 8: \ |
| 146 | + return sizeof(struct_packing_test::GpuClonerOptionsB); \ |
| 147 | + case 9: \ |
| 148 | + return sizeof( \ |
| 149 | + struct_packing_test::ProgressiveDimIndexFactoryB); \ |
| 150 | + case 10: \ |
| 151 | + return sizeof( \ |
| 152 | + struct_packing_test::GpuProgressiveDimIndexFactoryB); \ |
| 153 | + default: \ |
| 154 | + return -1; \ |
| 155 | + } \ |
| 156 | + } |
0 commit comments