You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(embeddings): slice Matryoshka dimensions on final axis - #680
Matryoshka slicing currently treats the first axis of each output as its embedding width. For ColBERT-style outputs shaped (tokens, embedding_width), this truncates tokens, leaves the embedding width unchanged, and validates requested dimensions against the first input's token count.
Validate against shape[-1] and slice with [..., :matryoshka_dim]. Dense embeddings keep their existing behavior. Late-interaction embeddings retain every token while selecting the requested leading coordinates.
Related to #559: this addresses the reported dimension-axis bug only. Query/document-specific processing, projection, and reranking behavior remain outside this change. This PR does not close that issue.
The same tests on unchanged 040269b give 10 failed, 11 passed.
Coverage includes dense embeddings, variable-length token embeddings in both input orders, dimensions below and above token counts, full-width slicing, invalid dimensions, unchanged None/0 behavior, dtype preservation, and non-mutation of inputs.
The ColBERT fixture invokes Infinity's actual SentenceTransformerPatched.encode_post with deterministic PyTorch tensors and attention masks. A SimpleNamespace supplies only postprocessor configuration; it is not a loaded model instance. No model weights are loaded, and these tests make no pretrained-model or reduced-dimension retrieval-quality claim.
Adjacent checks:
DO_NOT_TRACK=1 HF_HUB_OFFLINE=1 python -m pytest \
tests/unit_test/inference/test_matryoshka.py \
tests/unit_test/test_engine.py::test_async_api_debug \
tests/unit_test/test_engine.py::test_args_between_array_and_engine_same \
tests/unit_test/test_sync_engine.py::test_sync_engine \
tests/unit_test/test_sync_engine.py::test_sync_engine_double \
tests/unit_test/inference/test_select_model.py \
-k 'not torch and not optimum and not ctranslate2' -q
python -m pytest tests/unit_test/test_args.py -q
mypy infinity_emb reports the same 22 missing-optional-dependency errors on this branch and unchanged main, with identical output and no batch_handler.py errors.
Full make precommit, pretrained-model, accelerator, and HTTP-server suites were not run in this minimal CPU environment.
Checklist
Read the contribution guidelines.
Added regression tests.
Documented embedding-dimension slicing.
AI assistance was used to prepare this contribution.
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.
Summary
Matryoshka slicing currently treats the first axis of each output as its embedding width. For ColBERT-style outputs shaped
(tokens, embedding_width), this truncates tokens, leaves the embedding width unchanged, and validates requested dimensions against the first input's token count.Validate against
shape[-1]and slice with[..., :matryoshka_dim]. Dense embeddings keep their existing behavior. Late-interaction embeddings retain every token while selecting the requested leading coordinates.Related to #559: this addresses the reported dimension-axis bug only. Query/document-specific processing, projection, and reranking behavior remain outside this change. This PR does not close that issue.
Validation
Linux, Python 3.12.14, NumPy 1.26.4, CPU-only PyTorch 2.8.0, sentence-transformers 3.3.1, and pytest 8.4.2.
From
libs/infinity_emb:040269bgive 10 failed, 11 passed.None/0behavior, dtype preservation, and non-mutation of inputs.SentenceTransformerPatched.encode_postwith deterministic PyTorch tensors and attention masks. ASimpleNamespacesupplies only postprocessor configuration; it is not a loaded model instance. No model weights are loaded, and these tests make no pretrained-model or reduced-dimension retrieval-quality claim.Adjacent checks:
DO_NOT_TRACK=1 HF_HUB_OFFLINE=1 python -m pytest \ tests/unit_test/inference/test_matryoshka.py \ tests/unit_test/test_engine.py::test_async_api_debug \ tests/unit_test/test_engine.py::test_args_between_array_and_engine_same \ tests/unit_test/test_sync_engine.py::test_sync_engine \ tests/unit_test/test_sync_engine.py::test_sync_engine_double \ tests/unit_test/inference/test_select_model.py \ -k 'not torch and not optimum and not ctranslate2' -q python -m pytest tests/unit_test/test_args.py -qruff check .passes. Changed-file formatting, codespell, andgit diff --checkpass.mypy infinity_embreports the same 22 missing-optional-dependency errors on this branch and unchanged main, with identical output and nobatch_handler.pyerrors.make precommit, pretrained-model, accelerator, and HTTP-server suites were not run in this minimal CPU environment.Checklist
AI assistance was used to prepare this contribution.