Sentence Similarity
Safetensors
sentence-transformers
PyLate
lfm2
liquid
lfm2.5
edge
ColBERT
multi-vector
feature-extraction
custom_code
Instructions to use LiquidAI/LFM2.5-ColBERT-350M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use LiquidAI/LFM2.5-ColBERT-350M with sentence-transformers:
from pylate import models queries = [ "Which planet is known as the Red Planet?", "What is the largest planet in our solar system?", ] documents = [ ["Mars is the Red Planet.", "Venus is Earth's twin."], ["Jupiter is the largest planet.", "Saturn has rings."], ] model = models.ColBERT(model_name_or_path="LiquidAI/LFM2.5-ColBERT-350M") queries_emb = model.encode(queries, is_query=True) docs_emb = model.encode(documents, is_query=False) - Notebooks
- Google Colab
- Kaggle
Fix transformers 5.x loading and add Sentence Transformers usage
#3
by tomaarsen HF Staff - opened
- README.md +35 -4
- modeling_lfm2_bidirectional.py +4 -0
README.md
CHANGED
|
@@ -19,6 +19,7 @@ tags:
|
|
| 19 |
- ColBERT
|
| 20 |
- PyLate
|
| 21 |
- sentence-transformers
|
|
|
|
| 22 |
- sentence-similarity
|
| 23 |
- feature-extraction
|
| 24 |
pipeline_tag: sentence-similarity
|
|
@@ -100,15 +101,45 @@ We recommend LFM2.5-Embedding-350M and LFM2.5-ColBERT-350M for short-context ret
|
|
| 100 |
|
| 101 |
<a href="https://colab.research.google.com/drive/1uLswYrRTNw4P2P2qZ8JG-b8j-KDkQqwL?usp=sharing"><img src="https://cdn-uploads.huggingface.co/production/uploads/61b8e2ba285851687028d395/vlOyMEjwHa_b_LXysEu2E.png" width=120 alt="Colab link"></a>
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
|
| 105 |
```bash
|
| 106 |
-
pip install -
|
| 107 |
```
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
#### Indexing documents
|
| 114 |
|
|
|
|
| 19 |
- ColBERT
|
| 20 |
- PyLate
|
| 21 |
- sentence-transformers
|
| 22 |
+
- multi-vector
|
| 23 |
- sentence-similarity
|
| 24 |
- feature-extraction
|
| 25 |
pipeline_tag: sentence-similarity
|
|
|
|
| 101 |
|
| 102 |
<a href="https://colab.research.google.com/drive/1uLswYrRTNw4P2P2qZ8JG-b8j-KDkQqwL?usp=sharing"><img src="https://cdn-uploads.huggingface.co/production/uploads/61b8e2ba285851687028d395/vlOyMEjwHa_b_LXysEu2E.png" width=120 alt="Colab link"></a>
|
| 103 |
|
| 104 |
+
### Using Sentence Transformers
|
| 105 |
+
|
| 106 |
+
This model can be used as a multi-vector (ColBERT-style late interaction) retriever directly with [Sentence Transformers](https://www.sbert.net/) via the `MultiVectorEncoder`.
|
| 107 |
|
| 108 |
```bash
|
| 109 |
+
pip install "sentence-transformers>=6.0.0"
|
| 110 |
```
|
| 111 |
|
| 112 |
+
```python
|
| 113 |
+
from sentence_transformers import MultiVectorEncoder
|
| 114 |
+
|
| 115 |
+
model = MultiVectorEncoder("LiquidAI/LFM2.5-ColBERT-350M", trust_remote_code=True)
|
| 116 |
+
|
| 117 |
+
query = "Which planet is known as the Red Planet?"
|
| 118 |
+
documents = [
|
| 119 |
+
"Venus is often called Earth's twin because of its similar size and proximity.",
|
| 120 |
+
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
|
| 121 |
+
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
|
| 122 |
+
"Saturn, famous for its rings, is sometimes mistaken for the Red Planet.",
|
| 123 |
+
]
|
| 124 |
+
|
| 125 |
+
query_embeddings = model.encode_query([query])
|
| 126 |
+
document_embeddings = model.encode_document(documents)
|
| 127 |
+
print(query_embeddings[0].shape, document_embeddings[0].shape)
|
| 128 |
+
# (32, 128) (17, 128)
|
| 129 |
|
| 130 |
+
# MaxSim late-interaction scoring (the Mars document ranks highest)
|
| 131 |
+
scores = model.similarity(query_embeddings, document_embeddings)
|
| 132 |
+
print(scores)
|
| 133 |
+
# tensor([[27.1621, 28.2578, 27.7266, 28.1992]])
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
### Using PyLate
|
| 137 |
+
|
| 138 |
+
Use this model with PyLate to index and retrieve documents. The index uses [FastPLAID](https://github.com/lightonai/fast-plaid) for efficient similarity search. First, install PyLate and transformers:
|
| 139 |
+
|
| 140 |
+
```bash
|
| 141 |
+
pip install -U pylate
|
| 142 |
+
```
|
| 143 |
|
| 144 |
#### Indexing documents
|
| 145 |
|
modeling_lfm2_bidirectional.py
CHANGED
|
@@ -71,7 +71,11 @@ def _noncausal_shortconv_forward(
|
|
| 71 |
past_key_values=None,
|
| 72 |
cache_position=None,
|
| 73 |
attention_mask: Optional[torch.Tensor] = None,
|
|
|
|
| 74 |
) -> torch.Tensor:
|
|
|
|
|
|
|
|
|
|
| 75 |
# Only the flash_attention_2 path expects padding states zeroed before the
|
| 76 |
# conv. On eager/sdpa the checkpoints were trained WITHOUT zeroing: under
|
| 77 |
# transformers 4.56 the conv received the 4D additive mask, on which
|
|
|
|
| 71 |
past_key_values=None,
|
| 72 |
cache_position=None,
|
| 73 |
attention_mask: Optional[torch.Tensor] = None,
|
| 74 |
+
**kwargs,
|
| 75 |
) -> torch.Tensor:
|
| 76 |
+
# transformers >=5.x passes seq_idx (packed-sample conv-state reset) to the conv. This full
|
| 77 |
+
# sequence non-causal conv has no cache and no packing, so it is ignored, like the cache args
|
| 78 |
+
# above. **kwargs absorbs it and any future additions rather than breaking on each new one.
|
| 79 |
# Only the flash_attention_2 path expects padding states zeroed before the
|
| 80 |
# conv. On eager/sdpa the checkpoints were trained WITHOUT zeroing: under
|
| 81 |
# transformers 4.56 the conv received the 4D additive mask, on which
|