Spaces:
Paused
Paused
frdel commited on
Commit ·
d4dce78
1
Parent(s): 0e2ae0a
Update faiss_monkey_patch.py
Browse files
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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|