Text Ranking
sentence-transformers
Safetensors
Transformers
multilingual
t5gemma2
text2text-generation
reranker
encoder-decoder
FBNL
Retrieval
RAG
lukann98 commited on
Commit
ac8d588
·
verified ·
1 Parent(s): e8eaadc

fix(reranker): avoid re-computing the first batch in predict()'s batch-size probe

Browse files

Existing Issue:
KaLMReranker.predict() runs a full forward pass on the first batch twice: once as a throwaway OOM-size probe, and once again for the real computation. The probe's result is computed but never used. This roughly doubles reranking latency for any call where the total number of documents fits within a single batch (the common case, since batch_size defaults to 32).


Proposed Fix:
Keep the probe's result and reuse it as the first batch's scores, starting the real loop after the first batch instead of from the beginning.

Files changed (1) hide show
  1. kalm_reranker.py +5 -3
kalm_reranker.py CHANGED
@@ -282,10 +282,12 @@ class KaLMReranker:
282
  )
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 +297,9 @@ class KaLMReranker:
295
  torch.cuda.empty_cache()
296
  tested_batch_size = max(1, tested_batch_size * 3 // 4)
297
 
298
- sorted_scores: List[float] = []
299
  try:
300
- for start in range(0, len(sorted_pairs), tested_batch_size):
301
  sorted_scores.extend(
302
  self._predict_batch(
303
  sorted_pairs[start : start + tested_batch_size],
 
282
  )
283
  sorted_pairs = [validated_pairs[index] for index in length_sorted_indices]
284
 
285
+
286
  tested_batch_size = effective_batch_size
287
+ first_batch_scores: List[float] = []
288
  while tested_batch_size > 1:
289
  try:
290
+ first_batch_scores = self._predict_batch(
291
  sorted_pairs[: min(len(sorted_pairs), tested_batch_size)],
292
  effective_instruction,
293
  )
 
297
  torch.cuda.empty_cache()
298
  tested_batch_size = max(1, tested_batch_size * 3 // 4)
299
 
300
+ sorted_scores: List[float] = list(first_batch_scores)
301
  try:
302
+ for start in range(tested_batch_size, len(sorted_pairs), tested_batch_size):
303
  sorted_scores.extend(
304
  self._predict_batch(
305
  sorted_pairs[start : start + tested_batch_size],