Repository navigation
Sparse - #423
Sparse#423stephantul wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
[Medium impact] The PR adds a bounded sparse-embedding workflow to Model2Vec and Sentence Transformers. The PR appears safe to merge, with non-blocking improvements to adapter memory use and installation instructions. Reviews (1) · Last reviewed commit: "add tests" · Reviewed by Greptile |
| :return: The features, with the sparse vectors as a dense tensor under `sentence_embedding`. | ||
| """ | ||
| encode = self.model.encode_query if features["task"] == "query" else self.model.encode_document | ||
| features["sentence_embedding"] = torch.from_numpy(encode(features["texts"]).toarray()) |
There was a problem hiding this comment.
Sparse batches use dense memory
forward expands every sparse result with .toarray(). Memory use then grows with batch size times the full vocabulary, even when each text activates only a few tokens. For example, 256 texts over 250,000 dimensions need about 256 MB for this array alone at float32. Preserve the sparse form when creating the PyTorch tensor.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| scores = queries @ documents.T | ||
| ``` | ||
|
|
||
| This model can also be loaded as a [Sentence Transformers](https://github.com/huggingface/sentence-transformers) `SparseEncoder`, which requires `model2vec[sparse]` to be installed: |
There was a problem hiding this comment.
Example lacks a required package
The installation command installs model2vec[sparse], but that extra adds only scipy. A fresh environment following these instructions cannot run the example importing SparseEncoder from sentence_transformers. Add a separate installation command for Sentence Transformers before the example.
Knowledge Base Used: Model card generation
No description provided.