Skip to content

Commit

Permalink
M1 (#146)
Browse files Browse the repository at this point in the history
* basic build pass on M1

* format

* define m1 on spaces cmakelists

* adjust tests to m1
  • Loading branch information
DvirDukhan authored Mar 14, 2022
1 parent e03ac92 commit adfea2c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmake/cpu_features.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ option(CMAKE_POSITION_INDEPENDENT_CODE "" ON)
FetchContent_Declare(
cpu_features
GIT_REPOSITORY https://github.com/google/cpu_features.git
GIT_TAG 2f5a7bf80ae4c7df3285e23341ec5f37a5254095
GIT_TAG v0.7.0
)
FetchContent_MakeAvailable(cpu_features)
27 changes: 22 additions & 5 deletions src/VecSim/spaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ project(VectorSimilaritySpaces_no_optimization)
)

project(VectorSimilarity_Spaces)

include(${root}/cmake/cpu_features.cmake)
include(CheckCXXCompilerFlag)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")


# TODO: Remove this once cpu_features get support for M1
if(NOT APPLE)
include(${root}/cmake/cpu_features.cmake)
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
include(${root}/cmake/cpu_features.cmake)
else()
add_definitions(-DM1)
endif()

if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
# build SSE/AVX* code only on x64 processors.
# Check that the compiler supports instructions flag.
# This will add the relevant flag both the the space selector and the optimization.
Expand Down Expand Up @@ -39,8 +48,16 @@ add_library(VectorSimilaritySpaces STATIC
IP_space.cpp
)

target_link_libraries(VectorSimilaritySpaces cpu_features VectorSimilaritySpaces_no_optimization)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
target_link_libraries(VectorSimilaritySpaces VectorSimilaritySpaces_no_optimization)

if(NOT APPLE)
target_link_libraries(VectorSimilaritySpaces cpu_features)
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
target_link_libraries(VectorSimilaritySpaces cpu_features)
endif()


if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
# Build and link the relevant optimization for X86.
if(CXX_AVX512F)
project(VectorSimilaritySpaces_avx512)
Expand Down
4 changes: 3 additions & 1 deletion src/VecSim/spaces/IP_space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
InnerProductSpace::InnerProductSpace(size_t dim, std::shared_ptr<VecSimAllocator> allocator)
: SpaceInterface(allocator) {
fstdistfunc_ = InnerProduct;
#if defined(__x86_64__)
#if defined(M1)

#elif defined(__x86_64__)
Arch_Optimization arch_opt = getArchitectureOptimization();
if (arch_opt == ARCH_OPT_AVX512) {
#ifdef __AVX512F__
Expand Down
4 changes: 3 additions & 1 deletion src/VecSim/spaces/L2_space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
L2Space::L2Space(size_t dim, std::shared_ptr<VecSimAllocator> allocator)
: SpaceInterface(allocator) {
fstdistfunc_ = L2Sqr;
#if defined(__x86_64__)
#if defined(M1)

#elif defined(__x86_64__)
Arch_Optimization arch_opt = getArchitectureOptimization();
if (arch_opt == ARCH_OPT_AVX512) {
#ifdef __AVX512F__
Expand Down
4 changes: 4 additions & 0 deletions src/VecSim/spaces/space_aux.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "space_aux.h"
#if defined(M1)

#elif defined(__x86_64__)
#include "cpu_features_macros.h"
#endif

#ifdef CPU_FEATURES_ARCH_X86_64
#include "cpuinfo_x86.h"
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_bruteforce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ TEST_F(BruteForceTest, testCosine) {
((first_coordinate + (float)dim - 1.0f) /
(sqrtf((float)dim) * sqrtf((float)(dim - 1) + first_coordinate * first_coordinate)));
// Verify that abs difference between the actual and expected score is at most 1/10^6.
ASSERT_NEAR(score, expected_score, 1e-6);
ASSERT_NEAR(score, expected_score, 1e-5);
};
runTopKSearchTest(index, query, 10, verify_res);

Expand All @@ -1233,7 +1233,7 @@ TEST_F(BruteForceTest, testCosine) {
(sqrtf((float)dim) *
sqrtf((float)(dim - 1) + first_coordinate * first_coordinate)));
// Verify that abs difference between the actual and expected score is at most 1/10^6.
ASSERT_NEAR(score, expected_score, 1e-6);
ASSERT_NEAR(score, expected_score, 1e-5);
};
runBatchIteratorSearchTest(batchIterator, n_res, verify_res_batch);
iteration_num++;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_hnswlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ TEST_F(HNSWLibTest, preferAdHocOptimization) {
}

TEST_F(HNSWLibTest, testCosine) {
size_t dim = 128;
size_t dim = 4;
size_t n = 100;

VecSimParams params{.algo = VecSimAlgo_HNSWLIB,
Expand Down Expand Up @@ -1321,7 +1321,7 @@ TEST_F(HNSWLibTest, testCosine) {
((first_coordinate + (float)dim - 1.0f) /
(sqrtf((float)dim) * sqrtf((float)(dim - 1) + first_coordinate * first_coordinate)));
// Verify that abs difference between the actual and expected score is at most 1/10^6.
ASSERT_NEAR(score, expected_score, 1e-6);
ASSERT_NEAR(score, expected_score, 1e-5);
};
runTopKSearchTest(index, query, 10, verify_res);

Expand All @@ -1344,7 +1344,7 @@ TEST_F(HNSWLibTest, testCosine) {
(sqrtf((float)dim) *
sqrtf((float)(dim - 1) + first_coordinate * first_coordinate)));
// Verify that abs difference between the actual and expected score is at most 1/10^6.
ASSERT_NEAR(score, expected_score, 1e-6);
ASSERT_NEAR(score, expected_score, 1e-5);
};
runBatchIteratorSearchTest(batchIterator, n_res, verify_res_batch);
iteration_num++;
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/test_spaces.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "gtest/gtest.h"
#include "cpu_features_macros.h"
#include "VecSim/spaces/space_aux.h"
#include "VecSim/spaces/IP/IP.h"
#include "VecSim/spaces/IP/IP_SSE.h"
Expand All @@ -22,6 +21,10 @@ class SpacesTest : public ::testing::Test {
void TearDown() override {}
};

#if defined(M1)

#elif defined(__x86_64)
#include "cpu_features_macros.h"
#ifdef CPU_FEATURES_ARCH_X86_64
// This test will trigger the "Residuals" function for dimension > 16, for each optimization.
TEST_F(SpacesTest, l2_17) {
Expand Down Expand Up @@ -151,3 +154,5 @@ TEST_F(SpacesTest, ip_16) {
}
}
#endif // CPU_FEATURES_ARCH_X86_64

#endif // M1/X86_64

0 comments on commit adfea2c

Please sign in to comment.