faiss monkey patch

frdel committed Jun 11, 2025 at 12:24 UTC 8e5b108f58279de509bb96ae4c8135e6b3360e5e
4 files changed +40 -2
python/helpers/faiss_monkey_patch.py new
+28
@@ -0,0 +1,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 +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()
\ No newline at end of file
python/helpers/memory.py
+5
@@ -5,7 +5,12 @@ from langchain.embeddings import CacheBackedEmbeddings
5
6 # from langchain_chroma import Chroma
7 from langchain_community.vectorstores import FAISS
8 +
9 +# faiss needs to be patched for python 3.12 on arm #TODO remove once not needed
10 +from python.helpers import faiss_monkey_patch
11 import faiss
12 +
13 +
14 from langchain_community.docstore.in_memory import InMemoryDocstore
15 from langchain_community.vectorstores.utils import (
16 DistanceStrategy,
python/helpers/vector_db.py
+5
@@ -1,7 +1,12 @@
1 from typing import Any, List, Sequence
2 import uuid
3 from langchain_community.vectorstores import FAISS
4 +
5 +# faiss needs to be patched for python 3.12 on arm #TODO remove once not needed
6 +from python.helpers import faiss_monkey_patch
7 import faiss
8 +
9 +
10 from langchain_core.documents import Document
11 from langchain.storage import InMemoryByteStore
12 from langchain_community.docstore.in_memory import InMemoryDocstore
requirements.txt
+2 -2
@@ -4,7 +4,7 @@ beautifulsoup4==4.13.4
4 browser-use==0.2.5
5 docker==7.1.0
6 duckduckgo-search==6.1.12
7 -faiss-cpu==1.9.0
7 +faiss-cpu==1.11.0
8 fastmcp==2.3.4
9 flask[async]==3.0.3
10 flask-basicauth==0.2.0
@@ -35,4 +35,4 @@ unstructured==0.15.13
35 unstructured-client==0.25.9
36 webcolors==24.6.0
37 nest-asyncio==1.6.0
38 -crontab==1.0.1
38 +crontab==1.0.1
\ No newline at end of file