Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion FlagEmbedding/abc/inference/AbsReranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ def encode_multi_process(
[output_queue.get() for _ in trange(last_chunk_id, desc="Chunks")],
key=lambda x: x[0],
)
scores = np.concatenate([result[1] for result in results_list])
# Scores are flat or layer-major; the last axis always indexes pairs.
scores = np.concatenate([result[1] for result in results_list], axis=-1)
return scores

# copied from https://github.com/UKPLab/sentence-transformers/blob/1802076d4eae42ff0a5629e1b04e75785d4e193b/sentence_transformers/SentenceTransformer.py#L857
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This directory contains tests for the FlagEmbedding library, including compatibi
- `test_tokenizer_pad_compat.py`: Tests normalization of sorted per-example tokenized records before `tokenizer.pad`, including the local BGE reranker tokenizer when available.
- `test_infer_embedder_basic.py`: Tests basic functionality of BGE embedder models with a small public checkpoint.
- `test_infer_reranker_basic.py`: Tests basic functionality of reranker models.
- `test_reranker_multi_process_scores.py`: Tests worker-score aggregation for flat and layerwise reranker outputs, without model downloads.

## Running Tests

Expand Down
60 changes: 60 additions & 0 deletions tests/test_reranker_multi_process_scores.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Worker-score aggregation must preserve the single-device output layout."""

import math
from queue import Queue

import numpy as np
import pytest

from FlagEmbedding.abc.inference import AbsReranker


class AggregationReranker(AbsReranker):
"""Exercise aggregation with precomputed worker results, without model downloads."""

def compute_score_single_gpu(self, sentence_pairs, **kwargs):
raise AssertionError(
"The aggregation test must use the supplied worker results"
)


@pytest.mark.parametrize("pair_count", [1, 3, 4, 7])
@pytest.mark.parametrize("layer_count", [1, 2, 3])
def test_worker_scores_preserve_layer_and_pair_axes(pair_count, layer_count):
reranker = AggregationReranker("unused", devices=["cpu", "cpu"])
sentence_pairs = [(f"query {i}", f"passage {i}") for i in range(pair_count)]
expected = np.arange(layer_count * pair_count, dtype=float).reshape(
layer_count, pair_count
)
# Both layerwise and lightweight rerankers collapse a single score layer.
if layer_count == 1:
expected = expected[0]

chunk_size = math.ceil(pair_count / len(reranker.target_devices))
chunks = [
(chunk_id, start, min(start + chunk_size, pair_count))
for chunk_id, start in enumerate(range(0, pair_count, chunk_size))
]
input_queue, output_queue = Queue(), Queue()
# Complete out of order, as real workers may. Their scores are Python lists.
for chunk_id, start, end in reversed(chunks):
output_queue.put([chunk_id, expected[..., start:end].tolist()])
pool = {
"input": input_queue,
"output": output_queue,
"processes": [None] * len(reranker.target_devices),
}

scores = reranker.encode_multi_process(sentence_pairs, pool, normalize=True)

assert isinstance(scores, np.ndarray)
assert scores.shape == expected.shape
np.testing.assert_array_equal(scores, expected)
assert output_queue.empty()
for chunk_id, start, end in chunks:
assert input_queue.get_nowait() == [
chunk_id,
sentence_pairs[start:end],
{"normalize": True},
]
assert input_queue.empty()