Skip to content

Commit adfea2c

Browse files
author
DvirDukhan
authored
M1 (#146)
* basic build pass on M1 * format * define m1 on spaces cmakelists * adjust tests to m1
1 parent e03ac92 commit adfea2c

File tree

8 files changed

+44
-14
lines changed

8 files changed

+44
-14
lines changed

cmake/cpu_features.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ option(CMAKE_POSITION_INDEPENDENT_CODE "" ON)
44
FetchContent_Declare(
55
cpu_features
66
GIT_REPOSITORY https://github.com/google/cpu_features.git
7-
GIT_TAG 2f5a7bf80ae4c7df3285e23341ec5f37a5254095
7+
GIT_TAG v0.7.0
88
)
99
FetchContent_MakeAvailable(cpu_features)

src/VecSim/spaces/CMakeLists.txt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ project(VectorSimilaritySpaces_no_optimization)
77
)
88

99
project(VectorSimilarity_Spaces)
10-
11-
include(${root}/cmake/cpu_features.cmake)
1210
include(CheckCXXCompilerFlag)
13-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
11+
12+
13+
# TODO: Remove this once cpu_features get support for M1
14+
if(NOT APPLE)
15+
include(${root}/cmake/cpu_features.cmake)
16+
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
17+
include(${root}/cmake/cpu_features.cmake)
18+
else()
19+
add_definitions(-DM1)
20+
endif()
21+
22+
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
1423
# build SSE/AVX* code only on x64 processors.
1524
# Check that the compiler supports instructions flag.
1625
# This will add the relevant flag both the the space selector and the optimization.
@@ -39,8 +48,16 @@ add_library(VectorSimilaritySpaces STATIC
3948
IP_space.cpp
4049
)
4150

42-
target_link_libraries(VectorSimilaritySpaces cpu_features VectorSimilaritySpaces_no_optimization)
43-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
51+
target_link_libraries(VectorSimilaritySpaces VectorSimilaritySpaces_no_optimization)
52+
53+
if(NOT APPLE)
54+
target_link_libraries(VectorSimilaritySpaces cpu_features)
55+
elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
56+
target_link_libraries(VectorSimilaritySpaces cpu_features)
57+
endif()
58+
59+
60+
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
4461
# Build and link the relevant optimization for X86.
4562
if(CXX_AVX512F)
4663
project(VectorSimilaritySpaces_avx512)

src/VecSim/spaces/IP_space.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
InnerProductSpace::InnerProductSpace(size_t dim, std::shared_ptr<VecSimAllocator> allocator)
66
: SpaceInterface(allocator) {
77
fstdistfunc_ = InnerProduct;
8-
#if defined(__x86_64__)
8+
#if defined(M1)
9+
10+
#elif defined(__x86_64__)
911
Arch_Optimization arch_opt = getArchitectureOptimization();
1012
if (arch_opt == ARCH_OPT_AVX512) {
1113
#ifdef __AVX512F__

src/VecSim/spaces/L2_space.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
L2Space::L2Space(size_t dim, std::shared_ptr<VecSimAllocator> allocator)
77
: SpaceInterface(allocator) {
88
fstdistfunc_ = L2Sqr;
9-
#if defined(__x86_64__)
9+
#if defined(M1)
10+
11+
#elif defined(__x86_64__)
1012
Arch_Optimization arch_opt = getArchitectureOptimization();
1113
if (arch_opt == ARCH_OPT_AVX512) {
1214
#ifdef __AVX512F__

src/VecSim/spaces/space_aux.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "space_aux.h"
2+
#if defined(M1)
3+
4+
#elif defined(__x86_64__)
25
#include "cpu_features_macros.h"
6+
#endif
37

48
#ifdef CPU_FEATURES_ARCH_X86_64
59
#include "cpuinfo_x86.h"

tests/unit/test_bruteforce.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ TEST_F(BruteForceTest, testCosine) {
12101210
((first_coordinate + (float)dim - 1.0f) /
12111211
(sqrtf((float)dim) * sqrtf((float)(dim - 1) + first_coordinate * first_coordinate)));
12121212
// Verify that abs difference between the actual and expected score is at most 1/10^6.
1213-
ASSERT_NEAR(score, expected_score, 1e-6);
1213+
ASSERT_NEAR(score, expected_score, 1e-5);
12141214
};
12151215
runTopKSearchTest(index, query, 10, verify_res);
12161216

@@ -1233,7 +1233,7 @@ TEST_F(BruteForceTest, testCosine) {
12331233
(sqrtf((float)dim) *
12341234
sqrtf((float)(dim - 1) + first_coordinate * first_coordinate)));
12351235
// Verify that abs difference between the actual and expected score is at most 1/10^6.
1236-
ASSERT_NEAR(score, expected_score, 1e-6);
1236+
ASSERT_NEAR(score, expected_score, 1e-5);
12371237
};
12381238
runBatchIteratorSearchTest(batchIterator, n_res, verify_res_batch);
12391239
iteration_num++;

tests/unit/test_hnswlib.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ TEST_F(HNSWLibTest, preferAdHocOptimization) {
12881288
}
12891289

12901290
TEST_F(HNSWLibTest, testCosine) {
1291-
size_t dim = 128;
1291+
size_t dim = 4;
12921292
size_t n = 100;
12931293

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

@@ -1344,7 +1344,7 @@ TEST_F(HNSWLibTest, testCosine) {
13441344
(sqrtf((float)dim) *
13451345
sqrtf((float)(dim - 1) + first_coordinate * first_coordinate)));
13461346
// Verify that abs difference between the actual and expected score is at most 1/10^6.
1347-
ASSERT_NEAR(score, expected_score, 1e-6);
1347+
ASSERT_NEAR(score, expected_score, 1e-5);
13481348
};
13491349
runBatchIteratorSearchTest(batchIterator, n_res, verify_res_batch);
13501350
iteration_num++;

tests/unit/test_spaces.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "gtest/gtest.h"
2-
#include "cpu_features_macros.h"
32
#include "VecSim/spaces/space_aux.h"
43
#include "VecSim/spaces/IP/IP.h"
54
#include "VecSim/spaces/IP/IP_SSE.h"
@@ -22,6 +21,10 @@ class SpacesTest : public ::testing::Test {
2221
void TearDown() override {}
2322
};
2423

24+
#if defined(M1)
25+
26+
#elif defined(__x86_64)
27+
#include "cpu_features_macros.h"
2528
#ifdef CPU_FEATURES_ARCH_X86_64
2629
// This test will trigger the "Residuals" function for dimension > 16, for each optimization.
2730
TEST_F(SpacesTest, l2_17) {
@@ -151,3 +154,5 @@ TEST_F(SpacesTest, ip_16) {
151154
}
152155
}
153156
#endif // CPU_FEATURES_ARCH_X86_64
157+
158+
#endif // M1/X86_64

0 commit comments

Comments
 (0)