frdel commited on
Commit
d4dce78
·
1 Parent(s): 0e2ae0a

Update faiss_monkey_patch.py

Browse files
Files changed (1) hide show
  1. python/helpers/faiss_monkey_patch.py +28 -20
python/helpers/faiss_monkey_patch.py CHANGED
@@ -1,28 +1,36 @@
1
- import sys
2
- from types import ModuleType, SimpleNamespace
3
 
4
- import numpy # real numpy
5
 
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
- """
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)
19
 
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)
23
 
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
 
 
27
 
28
- hack_faiss_for_python_3_12()
 
 
 
 
 
 
 
 
 
 
1
+ # import sys
2
+ # from types import ModuleType, SimpleNamespace
3
 
4
+ # import numpy # real numpy
5
 
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
+ # """
12
 
13
+ # faiss_monkey_patch.py – import this before faiss -----------------
14
+ import sys, types, numpy as np
15
+ from types import SimpleNamespace
 
 
 
16
 
17
+ # fake numpy.distutils and numpy.distutils.cpuinfo packages
18
+ dist = types.ModuleType("numpy.distutils")
19
+ cpuinfo = types.ModuleType("numpy.distutils.cpuinfo")
20
 
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
 
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