Update faiss_monkey_patch.py

frdel committed Jun 11, 2025 at 12:49 UTC 2e43eaf204fe522fc1fc8283eed2abe486d40eeb
1 file changed +28 -20
python/helpers/faiss_monkey_patch.py
+28 -20
@@ -1,28 +1,36 @@
1 -import sys
2 -from types import ModuleType, SimpleNamespace
1 +# import sys
2 +# from types import ModuleType, SimpleNamespace
3
4 -import numpy # real numpy
4 +# import numpy # real numpy
5
6 -# for python 3.12 on arm, faiss needs a fake cpuinfo module
6 +# # for python 3.12 on arm, faiss needs a fake cpuinfo module
7
8
9 -""" This disgusting hack was brought to you by:
10 -https://github.com/facebookresearch/faiss/issues/3936
11 -"""
9 +# """ This disgusting hack was brought to you by:
10 +# https://github.com/facebookresearch/faiss/issues/3936
11 +# """
12
13 -def hack_faiss_for_python_3_12() -> None:
14 - # Create a fake 'cpuinfo' module
15 - fake_cpuinfo_module = ModuleType("numpy.distutils.cpuinfo")
16 - fake_cpu_module = SimpleNamespace()
17 - fake_cpu_module.info = [{"Features": "asimd fp"}] # No 'sve'
18 - setattr(fake_cpuinfo_module, "cpu", fake_cpu_module)
13 +# faiss_monkey_patch.py – import this before faiss -----------------
14 +import sys, types, numpy as np
15 +from types import SimpleNamespace
16
20 - # Create a fake 'distutils' module and assign the cpuinfo module
21 - fake_distutils_module = ModuleType("numpy.distutils")
22 - setattr(fake_distutils_module, "cpuinfo", fake_cpuinfo_module)
17 +# fake numpy.distutils and numpy.distutils.cpuinfo packages
18 +dist = types.ModuleType("numpy.distutils")
19 +cpuinfo = types.ModuleType("numpy.distutils.cpuinfo")
20
24 - # Inject the fake modules into sys.modules
25 - sys.modules["numpy.distutils"] = fake_distutils_module
26 - sys.modules["numpy.distutils.cpuinfo"] = fake_cpuinfo_module
21 +# cpu attribute that looks like the real one
22 +cpuinfo.cpu = SimpleNamespace( # type: ignore
23 + # FAISS only does .info[0].get('Features', '')
24 + info=[{}]
25 +)
26
28 -hack_faiss_for_python_3_12()
\ No newline at end of file
27 +# register in sys.modules
28 +dist.cpuinfo = cpuinfo # type: ignore
29 +sys.modules["numpy.distutils"] = dist
30 +sys.modules["numpy.distutils.cpuinfo"] = cpuinfo
31 +
32 +# crucial: expose it as an *attribute* of the already-imported numpy package
33 +np.distutils = dist # type: ignore
34 +# -------------------------------------------------------------------
35 +
36 +import faiss
\ No newline at end of file