Skip to content

Commit

Permalink
Clean knowhere build warnings
Browse files Browse the repository at this point in the history
Signed-off-by: CaiYudong <[email protected]>
  • Loading branch information
cydrain committed Feb 18, 2025
1 parent 385964a commit ef7ad89
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 135 deletions.
2 changes: 1 addition & 1 deletion benchmark/hdf5/gen_fbin_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Create_FBIN : public Benchmark_hdf5, public ::testing::Test {
// convert golden_ids to int32
auto elem_cnt = result.value()->GetLims()[nq];
std::vector<uint32_t> gt_ids_int(elem_cnt);
for (int32_t i = 0; i < elem_cnt; i++) {
for (size_t i = 0; i < elem_cnt; i++) {
gt_ids_int[i] = result.value()->GetIds()[i];
}

Expand Down
1 change: 1 addition & 0 deletions cmake/utils/compile_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if(WITH_ASAN)
endif()

set(CMAKE_CXX_FLAGS "-Wall -fPIC ${CMAKE_CXX_FLAGS}")
#set(CMAKE_CXX_FLAGS "-Wall -Werror -fPIC ${CMAKE_CXX_FLAGS}")

if(__X86_64)
set(CMAKE_CXX_FLAGS "-msse4.2 ${CMAKE_CXX_FLAGS}")
Expand Down
5 changes: 3 additions & 2 deletions include/knowhere/comp/knowhere_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

namespace knowhere {
namespace KnowhereCheck {
static bool

__attribute__((unused)) static bool
IndexTypeAndDataTypeCheck(const std::string& index_name, VecType data_type) {
auto& static_index_table = std::get<0>(IndexFactory::StaticIndexTableInstance());
auto key = std::pair<std::string, VecType>(index_name, data_type);
Expand All @@ -30,7 +31,7 @@ IndexTypeAndDataTypeCheck(const std::string& index_name, VecType data_type) {
}
}

static bool
__attribute__((unused)) static bool
SupportMmapIndexTypeCheck(const std::string& index_name) {
auto& mmap_index_table = std::get<1>(IndexFactory::StaticIndexTableInstance());
if (mmap_index_table.find(index_name) != mmap_index_table.end()) {
Expand Down
1 change: 0 additions & 1 deletion include/knowhere/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ data_type_conversion(const DataSet& src, const std::optional<int64_t> start = st

// map
auto* des_data = new OutType[out_dim * count_rows];
std::memset(des_data, 0, sizeof(OutType) * out_dim * count_rows);
auto* src_data = (const InType*)src.GetTensor();
for (auto i = 0; i < count_rows; i++) {
for (auto d = 0; d < in_dim; d++) {
Expand Down
40 changes: 39 additions & 1 deletion src/simd/distances_avx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <cassert>

#include "faiss/impl/platform_macros.h"
#include "simd_util.h"

namespace faiss {

Expand All @@ -41,6 +40,45 @@ masked_read(int d, const float* x) {
// cannot use AVX2 _mm_mask_set1_epi32
}

static inline __m128i
mm_masked_read_short(int d, const uint16_t* x) {
assert(0 <= d && d < 8);
ALIGNED(16) uint16_t buf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
switch (d) {
case 7:
buf[6] = x[6];
case 6:
buf[5] = x[5];
case 5:
buf[4] = x[4];
case 4:
buf[3] = x[3];
case 3:
buf[2] = x[2];
case 2:
buf[1] = x[1];
case 1:
buf[0] = x[0];
}
return _mm_loadu_si128((__m128i*)buf);
}

static inline __m256
_mm256_bf16_to_fp32(const __m128i& a) {
__m256i o = _mm256_slli_epi32(_mm256_cvtepu16_epi32(a), 16);
return _mm256_castsi256_ps(o);
}

static inline float
_mm256_reduce_add_ps(const __m256 res) {
const __m128 sum = _mm_add_ps(_mm256_castps256_ps128(res), _mm256_extractf128_ps(res, 1));
const __m128 v0 = _mm_shuffle_ps(sum, sum, _MM_SHUFFLE(0, 0, 3, 2));
const __m128 v1 = _mm_add_ps(sum, v0);
__m128 v2 = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(0, 0, 0, 1));
const __m128 v3 = _mm_add_ps(v1, v2);
return _mm_cvtss_f32(v3);
}

// trust the compiler to unroll this properly
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float
Expand Down
6 changes: 5 additions & 1 deletion src/simd/distances_avx512.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <string>

#include "faiss/impl/platform_macros.h"
#include "simd_util.h"

namespace faiss {

Expand All @@ -40,6 +39,11 @@ masked_read(int d, const float* x) {
// cannot use AVX2 _mm_mask_set1_epi32
}

static inline __m512
_mm512_bf16_to_fp32(const __m256i& x) {
return _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(x), 16));
}

// trust the compiler to unroll this properly
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float
Expand Down
42 changes: 41 additions & 1 deletion src/simd/distances_neon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,49 @@
#include <arm_neon.h>
#include <math.h>

#include "simd_util.h"
namespace faiss {

static inline float32x4x4_t
vcvt4_f32_f16(const float16x4x4_t a) {
float32x4x4_t c;
c.val[0] = vcvt_f32_f16(a.val[0]);
c.val[1] = vcvt_f32_f16(a.val[1]);
c.val[2] = vcvt_f32_f16(a.val[2]);
c.val[3] = vcvt_f32_f16(a.val[3]);
return c;
}

static inline float32x4x2_t
vcvt2_f32_f16(const float16x4x2_t a) {
float32x4x2_t c;
c.val[0] = vcvt_f32_f16(a.val[0]);
c.val[1] = vcvt_f32_f16(a.val[1]);
return c;
}

static inline float32x4x4_t
vcvt4_f32_half(const uint16x4x4_t x) {
float32x4x4_t c;
c.val[0] = vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(x.val[0]), 16));
c.val[1] = vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(x.val[1]), 16));
c.val[2] = vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(x.val[2]), 16));
c.val[3] = vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(x.val[3]), 16));
return c;
}

static inline float32x4x2_t
vcvt2_f32_half(const uint16x4x2_t x) {
float32x4x2_t c;
c.val[0] = vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(x.val[0]), 16));
c.val[1] = vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(x.val[1]), 16));
return c;
}

static inline float32x4_t
vcvt_f32_half(const uint16x4_t x) {
return vreinterpretq_f32_u32(vshlq_n_u32(vmovl_u16(x), 16));
}

// The main goal is to reduce the original precision of floats to maintain consistency with the distance result
// precision of the cardinal index.
__attribute__((always_inline)) inline float32x4_t
Expand Down
30 changes: 29 additions & 1 deletion src/simd/distances_sse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <cstdint>

#include "distances_ref.h"
#include "simd_util.h"

namespace faiss {

Expand All @@ -45,6 +44,35 @@ masked_read(int d, const float* x) {
// cannot use AVX2 _mm_mask_set1_epi32
}

static inline __m128i
mm_masked_read_short(int d, const uint16_t* x) {
assert(0 <= d && d < 8);
ALIGNED(16) uint16_t buf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
switch (d) {
case 7:
buf[6] = x[6];
case 6:
buf[5] = x[5];
case 5:
buf[4] = x[4];
case 4:
buf[3] = x[3];
case 3:
buf[2] = x[2];
case 2:
buf[1] = x[1];
case 1:
buf[0] = x[0];
}
return _mm_loadu_si128((__m128i*)buf);
}

static inline __m128
_mm_bf16_to_fp32(const __m128i& a) {
auto o = _mm_slli_epi32(_mm_cvtepu16_epi32(a), 16);
return _mm_castsi128_ps(o);
}

float
fvec_norm_L2sqr_sse(const float* x, size_t d) {
__m128 mx;
Expand Down
123 changes: 0 additions & 123 deletions src/simd/simd_util.h

This file was deleted.

3 changes: 3 additions & 0 deletions tests/ut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

knowhere_file_glob(GLOB_RECURSE KNOWHERE_UT_SRCS *.cc)

# remove this test temporarily
list(REMOVE_ITEM KNOWHERE_UT_SRCS test_data_view_index.cc)

if(NOT WITH_DISKANN)
knowhere_file_glob(GLOB_RECURSE KNOWHERE_DISKANN_TESTS test_diskann.cc)
list(REMOVE_ITEM KNOWHERE_UT_SRCS ${KNOWHERE_DISKANN_TESTS})
Expand Down
1 change: 0 additions & 1 deletion tests/ut/test_data_view_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ BaseTest(const knowhere::DataSetPtr train_ds, const knowhere::DataSetPtr query_d
auto base = knowhere::ConvertToDataTypeIfNeeded<DataType>(train_ds);
auto query = knowhere::ConvertToDataTypeIfNeeded<DataType>(query_ds);
auto dim = base->GetDim();
auto nb = base->GetRows();
auto nq = query->GetRows();

auto knn_gt = knowhere::BruteForce::Search<DataType>(base, query, conf, nullptr);
Expand Down
3 changes: 3 additions & 0 deletions tests/ut/test_index_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ TEST_CASE("Test index node") {
DataSetPtr ds = std::make_shared<DataSet>();
BinarySet binset;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable" // to ignore build warnings
SECTION("Test IndexNode") {
KNOWHERE_SIMPLE_REGISTER_GLOBAL(BASE_FLAT, BaseFlatIndexNode, fp32, knowhere::feature::FLOAT32);
auto index = IndexFactory::Instance().Create<fp32>("BASE_FLAT", version).value();
Expand Down Expand Up @@ -202,4 +204,5 @@ TEST_CASE("Test index node") {
REQUIRE(index.Count() == 0);
REQUIRE(index.Type() == INDEX_BASE_FLAT);
}
#pragma GCC diagnostic pop
}
6 changes: 3 additions & 3 deletions thirdparty/faiss/faiss/impl/ResultHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ struct HeapBlockResultHandler : BlockResultHandler<C> {
void add_results(size_t j0, size_t j1, const T* dis_tab,
const IDSelector* sel) final {
#pragma omp parallel for
for (int64_t i = i0; i < i1; i++) {
for (size_t i = i0; i < i1; i++) {
T* heap_dis = heap_dis_tab + i * k;
TI* heap_ids = heap_ids_tab + i * k;
const T* dis_tab_i = dis_tab + (j1 - j0) * (i - i0) - j0;
Expand Down Expand Up @@ -350,7 +350,7 @@ struct ReservoirTopN : ResultHandler<C> {
}

void to_result(T* heap_dis, TI* heap_ids) const {
for (int j = 0; j < std::min(i, n); j++) {
for (size_t j = 0; j < std::min(i, n); j++) {
heap_push<C>(j + 1, heap_dis, heap_ids, vals[j], ids[j]);
}

Expand Down Expand Up @@ -468,7 +468,7 @@ struct ReservoirBlockResultHandler : BlockResultHandler<C> {
void add_results(size_t j0, size_t j1, const T* dis_tab,
const IDSelector* sel) {
#pragma omp parallel for
for (int64_t i = i0; i < i1; i++) {
for (size_t i = i0; i < i1; i++) {
ReservoirTopN<C>& reservoir = reservoirs[i - i0];
const T* dis_tab_i = dis_tab + (j1 - j0) * (i - i0) - j0;
for (size_t j = j0; j < j1; j++) {
Expand Down

0 comments on commit ef7ad89

Please sign in to comment.