tomaarsen HF Staff commited on
Commit
8b16d8f
·
verified ·
1 Parent(s): 59633c2

Fix transformers 5.x loading and add Sentence Transformers usage

Browse files
Files changed (2) hide show
  1. README.md +35 -4
  2. 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
- First, install the PyLate and transformers libraries:
 
 
104
 
105
  ```bash
106
- pip install -U pylate
107
  ```
108
 
109
- ### Retrieval
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- Use this model with PyLate to index and retrieve documents. The index uses [FastPLAID](https://github.com/lightonai/fast-plaid) for efficient similarity search.
 
 
 
 
 
 
 
 
 
 
 
 
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.1628, 28.2510, 27.7368, 28.2090]])
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