Text Ranking
sentence-transformers
Safetensors
Transformers
multilingual
t5gemma2
text2text-generation
reranker
encoder-decoder
FBNL
matryoshka
retrieval
RAG
cosyy commited on
Commit
a0c15f0
·
verified ·
1 Parent(s): 7b9c7a5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +115 -0
README.md CHANGED
@@ -157,6 +157,121 @@ On LMEB-Dialogue, a compact embedding model paired with our Nano reranker, which
157
  ![lmeb](./assets/lmeb.jpg)
158
  ![lmeb_emb](./assets/lmeb_emb.jpg)
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  #### Ablation on multi-stage training
161
 
162
  Across all three model sizes and all seven compression ratios, performance on BEIR and MIRACL improves consistently from Stage 1 to Stage 3, demonstrating the effectiveness of our multi-stage training pipeline. Concretely, Stage 1 establishes a robust foundation for document reranking, distillation in Stage 2 substantially improves performance, and Stage 3 yields further modest gains. More importantly, robustness to compression generally improves across the three training stages. For example, from Stage 1 to Stage 3, the performance retention of KaLM-Reranker-V1-Nano at r = 128 relative to r = 2 increases from 92.88% to 93.80% on BEIR and from 90.93% to 92.15% on MIRACL.
 
157
  ![lmeb](./assets/lmeb.jpg)
158
  ![lmeb_emb](./assets/lmeb_emb.jpg)
159
 
160
+ ## Usage
161
+ ### Using transformers
162
+ ```python
163
+ import argparse
164
+ from typing import Optional
165
+
166
+
167
+ def optional_positive_int(value: str) -> Optional[int]:
168
+ if value.lower() == "none":
169
+ return None
170
+ try:
171
+ parsed = int(value)
172
+ except ValueError as error:
173
+ raise argparse.ArgumentTypeError(
174
+ "must be a positive integer or 'none'"
175
+ ) from error
176
+ if parsed <= 0:
177
+ raise argparse.ArgumentTypeError("must be a positive integer or 'none'")
178
+ return parsed
179
+
180
+
181
+ def build_parser() -> argparse.ArgumentParser:
182
+ parser = argparse.ArgumentParser(
183
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter,
184
+ )
185
+ parser.add_argument(
186
+ "--model",
187
+ default="KaLM-Embedding/KaLM-Reranker-V1-Nano-R2",
188
+ help="Hugging Face model ID or local checkpoint path.",
189
+ )
190
+ parser.add_argument(
191
+ "--device",
192
+ default=None,
193
+ help="Inference device, such as 'cuda', 'cuda:0', or 'cpu'.",
194
+ )
195
+ parser.add_argument(
196
+ "--dtype",
197
+ default=None,
198
+ choices=("bfloat16", "bf16", "float16", "fp16", "float32", "fp32"),
199
+ help="Model parameter dtype. By default, use BF16 on CUDA and FP32 on CPU.",
200
+ )
201
+ parser.add_argument(
202
+ "--batch-size",
203
+ type=int,
204
+ default=32,
205
+ help="Number of query-document pairs scored per inference batch.",
206
+ )
207
+ parser.add_argument(
208
+ "--query-max-length",
209
+ type=int,
210
+ default=512,
211
+ help=(
212
+ "Maximum tokens in the raw query before it is inserted into the "
213
+ "decoder prompt; prompt tokens are not included in this limit."
214
+ ),
215
+ )
216
+ parser.add_argument(
217
+ "--reranker-max-length",
218
+ type=int,
219
+ default=1024,
220
+ help=(
221
+ "Maximum encoder tokens for '<Document>: {passage}'. This is not a "
222
+ "combined query-document context limit."
223
+ ),
224
+ )
225
+ parser.add_argument(
226
+ "--chunk-size",
227
+ type=optional_positive_int,
228
+ default=4,
229
+ metavar="N|none",
230
+ help=(
231
+ "Number of encoder token hidden states per mean-pooled chunk; use "
232
+ "'none' to disable encoder chunk pooling."
233
+ ),
234
+ )
235
+ return parser
236
+
237
+
238
+ def main() -> None:
239
+ args = build_parser().parse_args()
240
+
241
+ from kalm_reranker import KaLMReranker
242
+
243
+ reranker = KaLMReranker(
244
+ args.model,
245
+ device=args.device,
246
+ dtype=args.dtype,
247
+ batch_size=args.batch_size,
248
+ query_max_length=args.query_max_length,
249
+ max_length=args.reranker_max_length,
250
+ chunk_size=args.chunk_size,
251
+ )
252
+ query = "What is the capital of China?"
253
+ documents = [
254
+ "The capital of China is Beijing.",
255
+ "Gravity attracts bodies toward one another.",
256
+ ]
257
+ instruction = "Given a query, retrieve documents that answer the query."
258
+
259
+ pairs = [(query, document) for document in documents]
260
+ print("scores:", reranker.predict(pairs, instruction=instruction))
261
+ print("rankings:", reranker.rank(query, documents, instruction=instruction))
262
+
263
+
264
+ if __name__ == "__main__":
265
+ main()
266
+
267
+ '''
268
+ scores: [0.985496461391449, 0.00017952796770259738]
269
+ rankings: [{'corpus_id': 0, 'score': 0.985496461391449}, {'corpus_id': 1, 'score': 0.00017952796770259738}]
270
+ '''
271
+
272
+ ```
273
+
274
+
275
  #### Ablation on multi-stage training
276
 
277
  Across all three model sizes and all seven compression ratios, performance on BEIR and MIRACL improves consistently from Stage 1 to Stage 3, demonstrating the effectiveness of our multi-stage training pipeline. Concretely, Stage 1 establishes a robust foundation for document reranking, distillation in Stage 2 substantially improves performance, and Stage 3 yields further modest gains. More importantly, robustness to compression generally improves across the three training stages. For example, from Stage 1 to Stage 3, the performance retention of KaLM-Reranker-V1-Nano at r = 128 relative to r = 2 increases from 92.88% to 93.80% on BEIR and from 90.93% to 92.15% on MIRACL.