Skip to content

Commit

Permalink
turn instruction_set into set (review beauby)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-vetinari committed Jan 29, 2021
1 parent 95d60df commit 68d0d0c
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions faiss/python/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@

def instruction_set():
"""
Returns a dictionary for supported instruction sets, see
Returns the set of supported instruction sets, see
https://github.com/numpy/numpy/blob/master/numpy/core/src/common/npy_cpu_features.h
for the list of features that this dictionary contains per architecture.
for the list of features that this set may contain per architecture.
Example:
>>> instruction_set() # for x86
{"SSE2": True, "AVX2": False, ...}
{"SSE2", "AVX2", ...}
>>> instruction_set() # for PPC
{"VSX": True, "VSX2": False, ...}
{"VSX", "VSX2", ...}
>>> instruction_set() # for ARM
{"NEON": True, "ASIMD": False, ...}
{"NEON", "ASIMD", ...}
"""
import numpy
if LooseVersion(numpy.__version__) >= "1.19":
# use private API as next-best thing until numpy/numpy#18058 is solved
from numpy.core._multiarray_umath import __cpu_features__
return __cpu_features__
# __cpu_features__ is a dictionary with CPU features
# as keys, and True / False as values
supported = {k for k, v in __cpu_features__.items() if v}
return supported

# platform-dependent legacy fallback before numpy 1.19, no windows
if platform.system() == "Darwin":
if subprocess.check_output(["/usr/sbin/sysctl", "hw.optional.avx2_0"])[-1] == '1':
return {"AVX2": True}
return {"AVX2"}
elif platform.system() == "Linux":
import numpy.distutils.cpuinfo
if "avx2" in numpy.distutils.cpuinfo.cpu.info[0].get('flags', ""):
return {"AVX2": True}
return {"AVX2": False}
return {"AVX2"}
return set()


logger = logging.getLogger(__name__)

try:
instr_set = instruction_set()
# dict-values of instr_set are True or False, but do not have
# uniform keys across arches -> use fallback value of False
has_AVX2 = instr_set.get("AVX2", False)
has_AVX2 = "AVX2" in instruction_set()
if has_AVX2:
logger.info("Loading faiss with AVX2 support.")
from .swigfaiss_avx2 import *
Expand Down

0 comments on commit 68d0d0c

Please sign in to comment.