Skip to content

feat: Add faiss cache backend#3402

Merged
Samoed merged 15 commits into
v2.0.0from
move_cache_wrapper
Oct 19, 2025
Merged

feat: Add faiss cache backend#3402
Samoed merged 15 commits into
v2.0.0from
move_cache_wrapper

Conversation

@Samoed

@Samoed Samoed commented Oct 17, 2025

Copy link
Copy Markdown
Member

Close #3192

Added FAISS to cache backend. Now CacheEmbeddingsWrapper can support multiple backends that can be passed directly.

from mteb.models.cache_wrappers.cache_backends.faiss_cache import FaissCache

model = mteb.get_model(...)
cachedmodel = CachedEmbeddingWrapper(model, cache_dir, cache_backend=FaissCache)

Probably something similar can be done for Retrieval tasks

@KennethEnevoldsen KennethEnevoldsen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great changes - only a few minor things

Comment thread tests/test_models/test_cached_embedding_wrapper.py
logger = logging.getLogger(__name__)


class VectorCacheMap:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename this to NumpyCache?



@runtime_checkable
class CacheBackendProtocol(Protocol):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loving it - this makes it very easy to change + test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, this is very nice!

Comment thread mteb/models/cache_wrappers/cache_backend_protocol.py Outdated
self.cache_dict[task_name] = _VectorCacheMap(
self.cache_path / task_name
)
self.cache_dict[task_name] = self.backend(self.cache_path / task_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, a whole new cache PR task - I am fine with this, but it prevents re-use across

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agree. Can remove it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the check include prompt (if not we probably have to handle that)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe this is the reason behind different indexes for different tasks. I think the simplest solution would be to keep cache per task to ensure that prompts will apply correctly, because they can change in model.encode

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah propably the best choice

Comment thread mteb/models/cache_wrappers/cache_wrapper.py Outdated
Samoed and others added 3 commits October 17, 2025 17:00

@orionw orionw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Excited to have this in 🙌

Comment thread pyproject.toml Outdated
xet = ["huggingface_hub>=0.32.0"]
youtu = ["tencentcloud-sdk-python-common>=3.0.1454", "tencentcloud-sdk-python-lkeap>=3.0.1451"]
faiss-cpu = ["faiss-cpu>=1.12.0"]
faiss-gpu = ["faiss-gpu>=1.12.0"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test with cpu and gpu? It's been a minute since I used them, but IIRC the gpu one has some quirks.

@Samoed Samoed Oct 17, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I faiss-gpu is deprecated on pypi, but exists only on conda. I'll leave only cpu version then



@runtime_checkable
class CacheBackendProtocol(Protocol):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, this is very nice!

@Samoed

Samoed commented Oct 19, 2025

Copy link
Copy Markdown
Member Author

@KennethEnevoldsen Is it good to merge?

Comment thread docs/advanced_usage/cache_embeddings.md Outdated
Samoed and others added 3 commits October 19, 2025 23:31
Co-authored-by: Kenneth Enevoldsen <kennethcenevoldsen@gmail.com>
# Conflicts:
#	pyproject.toml
@Samoed Samoed enabled auto-merge (squash) October 19, 2025 20:55
@Samoed Samoed merged commit aa88b98 into v2.0.0 Oct 19, 2025
11 checks passed
@Samoed Samoed deleted the move_cache_wrapper branch October 19, 2025 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants