Text Ranking
sentence-transformers
Safetensors
Transformers
multilingual
t5gemma2
text2text-generation
reranker
encoder-decoder
FBNL
Retrieval
RAG
Instructions to use KaLM-Embedding/KaLM-Reranker-V1-Large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use KaLM-Embedding/KaLM-Reranker-V1-Large with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("KaLM-Embedding/KaLM-Reranker-V1-Large") query = "Which planet is known as the Red Planet?" passages = [ "Venus is often called Earth's twin because of its similar size and proximity.", "Mars, known for its reddish appearance, is often referred to as the Red Planet.", "Jupiter, the largest planet in our solar system, has a prominent red spot.", "Saturn, famous for its rings, is sometimes mistaken for the Red Planet." ] scores = model.predict([(query, passage) for passage in passages]) print(scores) - Transformers
How to use KaLM-Embedding/KaLM-Reranker-V1-Large with Transformers:
# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("KaLM-Embedding/KaLM-Reranker-V1-Large") model = AutoModelForMultimodalLM.from_pretrained("KaLM-Embedding/KaLM-Reranker-V1-Large", device_map="auto") - Notebooks
- Google Colab
- Kaggle
fix(reranker): avoid re-computing the first batch in predict()'s batch-size probe to reduce additional computational effort
#1
by cosyy - opened
- kalm_reranker.py +15 -4
kalm_reranker.py
CHANGED
|
@@ -283,9 +283,10 @@ class KaLMReranker:
|
|
| 283 |
sorted_pairs = [validated_pairs[index] for index in length_sorted_indices]
|
| 284 |
|
| 285 |
tested_batch_size = effective_batch_size
|
|
|
|
| 286 |
while tested_batch_size > 1:
|
| 287 |
try:
|
| 288 |
-
self._predict_batch(
|
| 289 |
sorted_pairs[: min(len(sorted_pairs), tested_batch_size)],
|
| 290 |
effective_instruction,
|
| 291 |
)
|
|
@@ -295,9 +296,19 @@ class KaLMReranker:
|
|
| 295 |
torch.cuda.empty_cache()
|
| 296 |
tested_batch_size = max(1, tested_batch_size * 3 // 4)
|
| 297 |
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
try:
|
| 300 |
-
for start in range(
|
| 301 |
sorted_scores.extend(
|
| 302 |
self._predict_batch(
|
| 303 |
sorted_pairs[start : start + tested_batch_size],
|
|
@@ -346,4 +357,4 @@ class KaLMReranker:
|
|
| 346 |
return rankings if top_k is None else rankings[:top_k]
|
| 347 |
|
| 348 |
|
| 349 |
-
__all__ = ["KaLMReranker"]
|
|
|
|
| 283 |
sorted_pairs = [validated_pairs[index] for index in length_sorted_indices]
|
| 284 |
|
| 285 |
tested_batch_size = effective_batch_size
|
| 286 |
+
first_batch_scores: Optional[List[float]] = None
|
| 287 |
while tested_batch_size > 1:
|
| 288 |
try:
|
| 289 |
+
first_batch_scores = self._predict_batch(
|
| 290 |
sorted_pairs[: min(len(sorted_pairs), tested_batch_size)],
|
| 291 |
effective_instruction,
|
| 292 |
)
|
|
|
|
| 296 |
torch.cuda.empty_cache()
|
| 297 |
tested_batch_size = max(1, tested_batch_size * 3 // 4)
|
| 298 |
|
| 299 |
+
# The while loop's condition (`> 1`) means batch size 1 is never
|
| 300 |
+
# actually probed. If every size down to 2 OOMs, it exits without a
|
| 301 |
+
# successful probe. Only skip ahead to `tested_batch_size` when the
|
| 302 |
+
# probe actually ran; otherwise fall back to starting at 0 like the
|
| 303 |
+
# loop below always did originally, or the first item(s) get dropped.
|
| 304 |
+
if first_batch_scores is None:
|
| 305 |
+
sorted_scores: List[float] = []
|
| 306 |
+
loop_start = 0
|
| 307 |
+
else:
|
| 308 |
+
sorted_scores = list(first_batch_scores)
|
| 309 |
+
loop_start = tested_batch_size
|
| 310 |
try:
|
| 311 |
+
for start in range(loop_start, len(sorted_pairs), tested_batch_size):
|
| 312 |
sorted_scores.extend(
|
| 313 |
self._predict_batch(
|
| 314 |
sorted_pairs[start : start + tested_batch_size],
|
|
|
|
| 357 |
return rankings if top_k is None else rankings[:top_k]
|
| 358 |
|
| 359 |
|
| 360 |
+
__all__ = ["KaLMReranker"]
|