Skip to content

Sparse - #423

Closed
stephantul wants to merge 6 commits into
mainfrom
sparse
Closed

Sparse#423
stephantul wants to merge 6 commits into
mainfrom
sparse

Conversation

@stephantul

Copy link
Copy Markdown
Contributor

No description provided.

@stephantul stephantul closed this Dec 1, 2026
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
model2vec/base.py 100.00% <100.00%> (ø)
model2vec/model.py 99.32% <100.00%> (+1.61%) ⬆️
model2vec/persistence/__init__.py 100.00% <100.00%> (ø)
model2vec/persistence/persistence.py 99.02% <100.00%> (+0.16%) ⬆️
model2vec/sentence-transformers/sparse_encoder.py 100.00% <100.00%> (ø)
model2vec/sparse.py 100.00% <100.00%> (ø)
model2vec/train/base.py 100.00% <100.00%> (ø)
model2vec/types.py 100.00% <100.00%> (ø)
model2vec/utils.py 94.11% <100.00%> (+0.36%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5 · The PR appears safe to merge, with non-blocking improvements to adapter memory use and installation instructions.RetriggerView in GreptileConfidence Score: 4/5

[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())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 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

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.

1 participant