Skip to content

Commit 41998c9

Browse files
authored
Merge pull request #357 from tony-kuo/avx_runtime
AVX runtime check
2 parents 07c68cc + 324426d commit 41998c9

File tree

4 files changed

+135
-18
lines changed

4 files changed

+135
-18
lines changed

hnswlib/hnswlib.h

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,25 @@
1515
#ifdef _MSC_VER
1616
#include <intrin.h>
1717
#include <stdexcept>
18+
#include "cpu_x86.h"
19+
void cpu_x86::cpuid(int32_t out[4], int32_t eax, int32_t ecx) {
20+
__cpuidex(out, eax, ecx);
21+
}
22+
__int64 xgetbv(unsigned int x) {
23+
return _xgetbv(x);
24+
}
1825
#else
1926
#include <x86intrin.h>
27+
#include <cpuid.h>
28+
#include <stdint.h>
29+
void cpuid(int32_t cpuInfo[4], int32_t eax, int32_t ecx) {
30+
__cpuid_count(eax, ecx, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]);
31+
}
32+
uint64_t xgetbv(unsigned int index) {
33+
uint32_t eax, edx;
34+
__asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
35+
return ((uint64_t)edx << 32) | eax;
36+
}
2037
#endif
2138

2239
#if defined(USE_AVX512)
@@ -30,6 +47,65 @@
3047
#define PORTABLE_ALIGN32 __declspec(align(32))
3148
#define PORTABLE_ALIGN64 __declspec(align(64))
3249
#endif
50+
51+
// Adapted from https://github.com/Mysticial/FeatureDetector
52+
#define _XCR_XFEATURE_ENABLED_MASK 0
53+
54+
bool AVXCapable() {
55+
int cpuInfo[4];
56+
57+
// CPU support
58+
cpuid(cpuInfo, 0, 0);
59+
int nIds = cpuInfo[0];
60+
61+
bool HW_AVX = false;
62+
if (nIds >= 0x00000001) {
63+
cpuid(cpuInfo, 0x00000001, 0);
64+
HW_AVX = (cpuInfo[2] & ((int)1 << 28)) != 0;
65+
}
66+
67+
// OS support
68+
cpuid(cpuInfo, 1, 0);
69+
70+
bool osUsesXSAVE_XRSTORE = (cpuInfo[2] & (1 << 27)) != 0;
71+
bool cpuAVXSuport = (cpuInfo[2] & (1 << 28)) != 0;
72+
73+
bool avxSupported = false;
74+
if (osUsesXSAVE_XRSTORE && cpuAVXSuport) {
75+
uint64_t xcrFeatureMask = xgetbv(_XCR_XFEATURE_ENABLED_MASK);
76+
avxSupported = (xcrFeatureMask & 0x6) == 0x6;
77+
}
78+
return HW_AVX && avxSupported;
79+
}
80+
81+
bool AVX512Capable() {
82+
if (!AVXCapable()) return false;
83+
84+
int cpuInfo[4];
85+
86+
// CPU support
87+
cpuid(cpuInfo, 0, 0);
88+
int nIds = cpuInfo[0];
89+
90+
bool HW_AVX512F = false;
91+
if (nIds >= 0x00000007) { // AVX512 Foundation
92+
cpuid(cpuInfo, 0x00000007, 0);
93+
HW_AVX512F = (cpuInfo[1] & ((int)1 << 16)) != 0;
94+
}
95+
96+
// OS support
97+
cpuid(cpuInfo, 1, 0);
98+
99+
bool osUsesXSAVE_XRSTORE = (cpuInfo[2] & (1 << 27)) != 0;
100+
bool cpuAVXSuport = (cpuInfo[2] & (1 << 28)) != 0;
101+
102+
bool avx512Supported = false;
103+
if (osUsesXSAVE_XRSTORE && cpuAVXSuport) {
104+
uint64_t xcrFeatureMask = xgetbv(_XCR_XFEATURE_ENABLED_MASK);
105+
avx512Supported = (xcrFeatureMask & 0xe6) == 0xe6;
106+
}
107+
return HW_AVX512F && avx512Supported;
108+
}
33109
#endif
34110

35111
#include <queue>
@@ -108,7 +184,6 @@ namespace hnswlib {
108184

109185
return result;
110186
}
111-
112187
}
113188

114189
#include "space_l2.h"

hnswlib/space_ip.h

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace hnswlib {
1818

1919
// Favor using AVX if available.
2020
static float
21-
InnerProductSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
21+
InnerProductSIMD4ExtAVX(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
2222
float PORTABLE_ALIGN32 TmpRes[8];
2323
float *pVect1 = (float *) pVect1v;
2424
float *pVect2 = (float *) pVect2v;
@@ -64,10 +64,12 @@ namespace hnswlib {
6464
return 1.0f - sum;
6565
}
6666

67-
#elif defined(USE_SSE)
67+
#endif
68+
69+
#if defined(USE_SSE)
6870

6971
static float
70-
InnerProductSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
72+
InnerProductSIMD4ExtSSE(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
7173
float PORTABLE_ALIGN32 TmpRes[8];
7274
float *pVect1 = (float *) pVect1v;
7375
float *pVect2 = (float *) pVect2v;
@@ -128,7 +130,7 @@ namespace hnswlib {
128130
#if defined(USE_AVX512)
129131

130132
static float
131-
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
133+
InnerProductSIMD16ExtAVX512(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
132134
float PORTABLE_ALIGN64 TmpRes[16];
133135
float *pVect1 = (float *) pVect1v;
134136
float *pVect2 = (float *) pVect2v;
@@ -157,10 +159,12 @@ namespace hnswlib {
157159
return 1.0f - sum;
158160
}
159161

160-
#elif defined(USE_AVX)
162+
#endif
163+
164+
#if defined(USE_AVX)
161165

162166
static float
163-
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
167+
InnerProductSIMD16ExtAVX(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
164168
float PORTABLE_ALIGN32 TmpRes[8];
165169
float *pVect1 = (float *) pVect1v;
166170
float *pVect2 = (float *) pVect2v;
@@ -195,10 +199,12 @@ namespace hnswlib {
195199
return 1.0f - sum;
196200
}
197201

198-
#elif defined(USE_SSE)
202+
#endif
203+
204+
#if defined(USE_SSE)
199205

200206
static float
201-
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
207+
InnerProductSIMD16ExtSSE(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
202208
float PORTABLE_ALIGN32 TmpRes[8];
203209
float *pVect1 = (float *) pVect1v;
204210
float *pVect2 = (float *) pVect2v;
@@ -245,6 +251,9 @@ namespace hnswlib {
245251
#endif
246252

247253
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
254+
DISTFUNC<float> InnerProductSIMD16Ext = InnerProductSIMD16ExtSSE;
255+
DISTFUNC<float> InnerProductSIMD4Ext = InnerProductSIMD4ExtSSE;
256+
248257
static float
249258
InnerProductSIMD16ExtResiduals(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
250259
size_t qty = *((size_t *) qty_ptr);
@@ -283,6 +292,20 @@ namespace hnswlib {
283292
InnerProductSpace(size_t dim) {
284293
fstdistfunc_ = InnerProduct;
285294
#if defined(USE_AVX) || defined(USE_SSE) || defined(USE_AVX512)
295+
#if defined(USE_AVX512)
296+
if (AVX512Capable())
297+
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX512;
298+
else if (AVXCapable())
299+
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX;
300+
#elif defined(USE_AVX)
301+
if (AVXCapable())
302+
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX;
303+
#endif
304+
#if defined(USE_AVX)
305+
if (AVXCapable())
306+
InnerProductSIMD4Ext = InnerProductSIMD4ExtAVX;
307+
#endif
308+
286309
if (dim % 16 == 0)
287310
fstdistfunc_ = InnerProductSIMD16Ext;
288311
else if (dim % 4 == 0)

hnswlib/space_l2.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace hnswlib {
2323

2424
// Favor using AVX512 if available.
2525
static float
26-
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
26+
L2SqrSIMD16ExtAVX512(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
2727
float *pVect1 = (float *) pVect1v;
2828
float *pVect2 = (float *) pVect2v;
2929
size_t qty = *((size_t *) qty_ptr);
@@ -52,12 +52,13 @@ namespace hnswlib {
5252

5353
return (res);
5454
}
55+
#endif
5556

56-
#elif defined(USE_AVX)
57+
#if defined(USE_AVX)
5758

5859
// Favor using AVX if available.
5960
static float
60-
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
61+
L2SqrSIMD16ExtAVX(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
6162
float *pVect1 = (float *) pVect1v;
6263
float *pVect2 = (float *) pVect2v;
6364
size_t qty = *((size_t *) qty_ptr);
@@ -89,10 +90,12 @@ namespace hnswlib {
8990
return TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7];
9091
}
9192

92-
#elif defined(USE_SSE)
93+
#endif
94+
95+
#if defined(USE_SSE)
9396

9497
static float
95-
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
98+
L2SqrSIMD16ExtSSE(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
9699
float *pVect1 = (float *) pVect1v;
97100
float *pVect2 = (float *) pVect2v;
98101
size_t qty = *((size_t *) qty_ptr);
@@ -141,6 +144,8 @@ namespace hnswlib {
141144
#endif
142145

143146
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
147+
DISTFUNC<float> L2SqrSIMD16Ext = L2SqrSIMD16ExtSSE;
148+
144149
static float
145150
L2SqrSIMD16ExtResiduals(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
146151
size_t qty = *((size_t *) qty_ptr);
@@ -156,7 +161,7 @@ namespace hnswlib {
156161
#endif
157162

158163

159-
#ifdef USE_SSE
164+
#if defined(USE_SSE)
160165
static float
161166
L2SqrSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
162167
float PORTABLE_ALIGN32 TmpRes[8];
@@ -208,7 +213,17 @@ namespace hnswlib {
208213
public:
209214
L2Space(size_t dim) {
210215
fstdistfunc_ = L2Sqr;
211-
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
216+
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
217+
#if defined(USE_AVX512)
218+
if (AVX512Capable())
219+
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX512;
220+
else if (AVXCapable())
221+
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX;
222+
#elif defined(USE_AVX)
223+
if (AVXCapable())
224+
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX;
225+
#endif
226+
212227
if (dim % 16 == 0)
213228
fstdistfunc_ = L2SqrSIMD16Ext;
214229
else if (dim % 4 == 0)
@@ -217,7 +232,7 @@ namespace hnswlib {
217232
fstdistfunc_ = L2SqrSIMD16ExtResiduals;
218233
else if (dim > 4)
219234
fstdistfunc_ = L2SqrSIMD4ExtResiduals;
220-
#endif
235+
#endif
221236
dim_ = dim;
222237
data_size_ = dim * sizeof(float);
223238
}

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ class BuildExt(build_ext):
7474
"""A custom build extension for adding compiler-specific options."""
7575
c_opts = {
7676
'msvc': ['/EHsc', '/openmp', '/O2'],
77-
'unix': ['-O3', '-march=native'], # , '-w'
77+
#'unix': ['-O3', '-march=native'], # , '-w'
78+
'unix': ['-O3'], # , '-w'
7879
}
80+
if not os.environ.get("HNSWLIB_NO_NATIVE"):
81+
c_opts['unix'].append('-march=native')
82+
7983
link_opts = {
8084
'unix': [],
8185
'msvc': [],

0 commit comments

Comments
 (0)