Skip to content

fix(pipelines): QA pipeline returns fewer than top_k results in batch mode #39193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
12 changes: 11 additions & 1 deletion src/transformers/pipelines/question_answering.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,18 @@ def postprocess(
output["attention_mask"].numpy() if output.get("attention_mask", None) is not None else None
)

pre_topk = (
top_k * 2 + 10 if align_to_words else top_k
) # Some candidates may be deleted if we align to words
starts, ends, scores, min_null_score = select_starts_ends(
start_, end_, p_mask, attention_mask, min_null_score, top_k, handle_impossible_answer, max_answer_len
start_,
end_,
p_mask,
attention_mask,
min_null_score,
pre_topk,
handle_impossible_answer,
max_answer_len,
)

if not self.tokenizer.is_fast:
Expand Down
24 changes: 15 additions & 9 deletions tests/pipelines/test_pipelines_question_answering.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ def test_small_model_pt(self):
)

outputs = question_answerer(
question="Where was HuggingFace founded ?", context="HuggingFace was founded in Paris."
question="Where was HuggingFace founded ?",
context="HuggingFace was founded in Paris.",
)

self.assertEqual(nested_simplify(outputs), {"score": 0.01, "start": 0, "end": 11, "answer": "HuggingFace"})
self.assertEqual(nested_simplify(outputs), {"score": 0.063, "start": 0, "end": 11, "answer": "HuggingFace"})

@require_torch
def test_small_model_pt_fp16(self):
Expand All @@ -182,10 +183,11 @@ def test_small_model_pt_fp16(self):
)

outputs = question_answerer(
question="Where was HuggingFace founded ?", context="HuggingFace was founded in Paris."
question="Where was HuggingFace founded ?",
context="HuggingFace was founded in Paris.",
)

self.assertEqual(nested_simplify(outputs), {"score": 0.01, "start": 0, "end": 11, "answer": "HuggingFace"})
self.assertEqual(nested_simplify(outputs), {"score": 0.063, "start": 0, "end": 11, "answer": "HuggingFace"})

@require_torch
def test_small_model_pt_bf16(self):
Expand All @@ -196,10 +198,11 @@ def test_small_model_pt_bf16(self):
)

outputs = question_answerer(
question="Where was HuggingFace founded ?", context="HuggingFace was founded in Paris."
question="Where was HuggingFace founded ?",
context="HuggingFace was founded in Paris.",
)

self.assertEqual(nested_simplify(outputs), {"score": 0.01, "start": 0, "end": 11, "answer": "HuggingFace"})
self.assertEqual(nested_simplify(outputs), {"score": 0.063, "start": 0, "end": 11, "answer": "HuggingFace"})

@require_torch
def test_small_model_pt_iterator(self):
Expand All @@ -211,7 +214,9 @@ def data():
yield {"question": "Where was HuggingFace founded ?", "context": "HuggingFace was founded in Paris."}

for outputs in pipe(data()):
self.assertEqual(nested_simplify(outputs), {"score": 0.01, "start": 0, "end": 11, "answer": "HuggingFace"})
self.assertEqual(
nested_simplify(outputs), {"score": 0.063, "start": 0, "end": 11, "answer": "HuggingFace"}
)

@require_torch
def test_small_model_pt_softmax_trick(self):
Expand Down Expand Up @@ -242,10 +247,11 @@ def ensure_large_logits_postprocess(
question_answerer.postprocess = ensure_large_logits_postprocess

outputs = question_answerer(
question="Where was HuggingFace founded ?", context="HuggingFace was founded in Paris."
question="Where was HuggingFace founded ?",
context="HuggingFace was founded in Paris.",
)

self.assertEqual(nested_simplify(outputs), {"score": 0.028, "start": 0, "end": 11, "answer": "HuggingFace"})
self.assertEqual(nested_simplify(outputs), {"score": 0.111, "start": 0, "end": 11, "answer": "HuggingFace"})

@slow
@require_torch
Expand Down