-
Notifications
You must be signed in to change notification settings - Fork 179
Cross encoders parallelism #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fba23ed
Merge master
I8dNLo 0a3f616
rerank_pairs interface + parallelism support
I8dNLo 3536be7
remove test notebook
I8dNLo 8a1901e
Removed unused code
I8dNLo b034d8e
New tests for cross encoders and new interface
I8dNLo 1dbcb07
Importing Self fix. We will need it for mypy support in newer versions
I8dNLo c894380
Removed Self typing
I8dNLo 4bceaa4
Removed non-needed changes from text
I8dNLo 13acf23
Isort + black
I8dNLo 42b8f4e
wip: start reviewing (#420)
joein fdb4681
Test fix
I8dNLo 2e86028
Update fastembed/rerank/cross_encoder/text_cross_encoder.py
I8dNLo f886a3b
Update fastembed/rerank/cross_encoder/text_cross_encoder.py
I8dNLo a05653f
Update fastembed/rerank/cross_encoder/text_cross_encoder.py
I8dNLo 1eb809b
Update fastembed/rerank/cross_encoder/text_cross_encoder_base.py
I8dNLo a180a41
Test for parallel processing + bugfix of PosixPath passing
I8dNLo eb30d0e
Removed non-needed import and added docstring
I8dNLo f4cabd3
Typing fix + argument passing
I8dNLo 870d94a
Test parametrization
I8dNLo e14857e
Run base test on all models
I8dNLo 5dfec6d
Typing fix + improvement of input_names check
I8dNLo b5d2abc
nit: fix post process, update docstring, update tokenize, remove redu…
joein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,12 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Iterable, Any, Sequence, Optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Iterable, Any, Sequence, Optional, Self, Type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
joein marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from loguru import logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fastembed.common import OnnxProvider | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fastembed.rerank.cross_encoder.onnx_text_model import OnnxCrossEncoderModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fastembed.rerank.cross_encoder.onnx_text_model import OnnxCrossEncoderModel, TextRerankerWorker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fastembed.rerank.cross_encoder.text_cross_encoder_base import TextCrossEncoderBase | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fastembed.common.utils import define_cache_dir | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from fastembed.common.onnx_model import OnnxOutputContext | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| supported_onnx_models = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -82,7 +83,7 @@ def list_supported_models(cls) -> list[dict[str, Any]]: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return supported_onnx_models | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self: Self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model_name: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cache_dir: Optional[str] = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| threads: Optional[int] = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -91,7 +92,7 @@ def __init__( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| device_ids: Optional[list[int]] = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lazy_load: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| device_id: Optional[int] = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs: Any, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -155,11 +156,11 @@ def load_onnx_model(self) -> None: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def rerank( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self: Self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| documents: Iterable[str], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| batch_size: int = 64, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs: Any, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Iterable[float]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Reranks documents based on their relevance to a given query. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -175,3 +176,35 @@ def rerank( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| yield from self._rerank_documents( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query=query, documents=documents, batch_size=batch_size, **kwargs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def rerank_pairs( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self: Self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pairs: Iterable[tuple[str, str]], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| batch_size: int = 64, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs: Any, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Iterable[float]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| yield from self._rerank_pairs( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pairs=pairs, batch_size=batch_size, **kwargs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
185
to
202
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I8dNLo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _get_worker_class(cls) -> Type[TextRerankerWorker]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TextCrossEncoderWorker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _post_process_onnx_output(self, output: OnnxOutputContext) -> Iterable[np.ndarray]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return output.model_output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TextCrossEncoderWorker(TextRerankerWorker): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def init_embedding( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model_name: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cache_dir: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> OnnxTextCrossEncoder: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return OnnxTextCrossEncoder( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model_name=model_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cache_dir=cache_dir, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| threads=1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.