| 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 warnings |
| 37 | with warnings.catch_warnings(): |
| 38 | warnings.simplefilter("ignore", DeprecationWarning) |
| 39 | import faiss |