Repository navigation
feat(sparse): allow MiniCOIL sequence length limit #758
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
Open
Coding-Professional
wants to merge
3
commits into
qdrant:main
Choose a base branch
from
Coding-Professional:feat/544-minicoil-sequence-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| from types import SimpleNamespace | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from tokenizers import Tokenizer, models, pre_tokenizers, processors | ||
|
|
||
| from fastembed.sparse.minicoil import MiniCOIL | ||
| from fastembed.sparse.sparse_text_embedding import SparseTextEmbedding | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def local_minicoil(monkeypatch, tmp_path): | ||
| """Use a real tokenizer but avoid downloading model weights for sequence-length tests.""" | ||
|
|
||
| def load_tokenizer(model_dir): | ||
| vocab = {"[UNK]": 0, "[PAD]": 1} | ||
| vocab.update({f"word{i}": i + 2 for i in range(12)}) | ||
| tokenizer = Tokenizer(models.WordLevel(vocab, unk_token="[UNK]")) | ||
| tokenizer.pre_tokenizer = pre_tokenizers.Whitespace() | ||
| tokenizer.enable_truncation(max_length=8) | ||
| tokenizer.enable_padding(pad_id=1, pad_token="[PAD]") | ||
| return tokenizer, {} | ||
|
|
||
| monkeypatch.setattr("fastembed.text.onnx_text_model.load_tokenizer", load_tokenizer) | ||
| monkeypatch.setattr(MiniCOIL, "download_model", lambda *args, **kwargs: tmp_path) | ||
|
|
||
| def make_model(max_sequence_length=None): | ||
| kwargs = ( | ||
| {} if max_sequence_length is None else {"max_sequence_length": max_sequence_length} | ||
| ) | ||
| return SparseTextEmbedding("Qdrant/minicoil-v1", lazy_load=True, **kwargs).model | ||
|
|
||
| return make_model | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("max_sequence_length", "expected_length"), | ||
| [(None, 8), (4, 4), (20, 8)], | ||
| ) | ||
| def test_minicoil_limit_reaches_onnx_input( | ||
| local_minicoil, monkeypatch, max_sequence_length, expected_length | ||
| ) -> None: | ||
| model = local_minicoil(max_sequence_length) | ||
| model._ensure_tokenizer() | ||
| model.model = SimpleNamespace(get_inputs=lambda: [SimpleNamespace(name="input_ids")]) | ||
| monkeypatch.setattr( | ||
| model, | ||
| "_run_model", | ||
| lambda onnx_input, onnx_output_names=None: np.zeros( | ||
| (*onnx_input["input_ids"].shape, 1), dtype=np.float32 | ||
| ), | ||
| ) | ||
|
|
||
| text = " ".join(f"word{i}" for i in range(12)) | ||
| output = model.onnx_embed([text]) | ||
|
|
||
| assert output.input_ids.shape == (1, expected_length) | ||
| assert model.token_count(text) == expected_length | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("invalid_length", [0, -1, True, 2.5]) | ||
| def test_minicoil_rejects_invalid_sequence_limit(local_minicoil, invalid_length) -> None: | ||
| with pytest.raises(ValueError, match="max_sequence_length must be a positive integer"): | ||
| local_minicoil(invalid_length) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("max_sequence_length", [1, 2]) | ||
| @pytest.mark.parametrize("operation", ["token_count", "embed", "query_embed"]) | ||
| def test_minicoil_limit_must_leave_room_for_text_and_special_tokens( | ||
| local_minicoil, monkeypatch, max_sequence_length, operation | ||
| ) -> None: | ||
| tokenizer = Tokenizer( | ||
| models.WordLevel({"[UNK]": 0, "[CLS]": 1, "[SEP]": 2, "word": 3}, unk_token="[UNK]") | ||
| ) | ||
| tokenizer.pre_tokenizer = pre_tokenizers.Whitespace() | ||
| tokenizer.post_processor = processors.TemplateProcessing( | ||
| single="[CLS] $A [SEP]", special_tokens=[("[CLS]", 1), ("[SEP]", 2)] | ||
| ) | ||
| tokenizer.enable_truncation(max_length=8) | ||
| monkeypatch.setattr( | ||
| "fastembed.text.onnx_text_model.load_tokenizer", | ||
| lambda model_dir: (tokenizer, {"[CLS]": 1, "[SEP]": 2}), | ||
| ) | ||
| model = local_minicoil(max_sequence_length) | ||
| monkeypatch.setattr( | ||
| model, | ||
| "_load_onnx_model", | ||
| lambda **kwargs: pytest.fail("invalid sequence limit must fail before loading ONNX"), | ||
| ) | ||
|
|
||
| for _ in range(2): | ||
| with pytest.raises(ValueError, match="max_sequence_length must be at least 3"): | ||
| result = getattr(model, operation)("word word") | ||
| if operation != "token_count": | ||
| list(result) | ||
|
|
||
|
|
||
| def test_minicoil_parallel_workers_receive_sequence_limit(local_minicoil, monkeypatch) -> None: | ||
| model = local_minicoil(4) | ||
| captured = {} | ||
|
|
||
| class RecordingPool: | ||
| def __init__(self, **kwargs): | ||
| pass | ||
|
|
||
| def ordered_map(self, batches, **kwargs): | ||
| captured.update(kwargs) | ||
| return iter(()) | ||
|
|
||
| monkeypatch.setattr("fastembed.text.onnx_text_model.ParallelWorkerPool", RecordingPool) | ||
|
|
||
| assert list(model.embed(["word0", "word1"], batch_size=1, parallel=2)) == [] | ||
| assert captured["max_sequence_length"] == 4 |
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.