fix(pipelines): QA pipeline returns fewer than top_k results in batch mode
Author: Vixel2006
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes #38984

This PR fixes a bug in the `QuestionAnsweringPipeline` where it could return fewer than the requested top_k answers when processing long contexts or batched inputs. The original implementation processed each context chunk independently, asking for only the top_k best spans from each chunk before aggregation. This approach was flawed because if the best candidates within a chunk were later invalidated or identified as duplicates of an answer from another chunk, the pipeline had no other options to fall back on, resulting in an insufficient number of final answers. The fix implements a more robust strategy by first over-fetching a much larger pool of candidates from every chunk, then aggregating this global list, and only then sorting, merging, and selecting the final top_k answers, which guarantees a sufficient number of valid candidates to produce a reliable and complete result.

```py
import transformers

architecture = "csarron/mobilebert-uncased-squad-v2"
tokenizer = transformers.AutoTokenizer.from_pretrained(architecture, low_cpu_mem_usage=True)
model = transformers.MobileBertForQuestionAnswering.from_pretrained(
    architecture, low_cpu_mem_usage=True
)
pipeline = transformers.pipeline(task="question-answering", model=model, tokenizer=tokenizer)


data = [
    {'question': ['What color is it?', 'How do the people go?', "What does the 'wolf' howl at?"],
     'context': [
         "Some people said it was green but I know that it's pink.",
         'The people on the bus go up and down. Up and down.',
         "The pack of 'wolves' stood on the cliff and a 'lone wolf' howled at the moon for hours."
     ]}
]

# prediction result is wrong
pipeline(data, top_k=2, max_answer_len=5)
```

```
[[{'score': 0.5683297514915466, 'start': 51, 'end': 55, 'answer': 'pink'}, {'score': 0.028800610452890396, 'start': 51, 'end': 56, 'answer': 'pink.'}], [{'score': 0.3008899986743927, 'start': 25, 'end': 36, 'answer': 'up and down'}, {'score': 0.12070021033287048, 'start': 38, 'end': 49, 'answer': 'Up and down'}], [{'score': 0.8356598615646362, 'start': 68, 'end': 76, 'answer': 'the moon'}, {'score': 0.0971309095621109, 'start': 72, 'end': 76, 'answer': 'moon'}]]
```

## Who can review?
@Rocketknight1



Comments:
Leaving this to you @Rocketknight1 as you own this! 🤗 

Hi @Rocketknight1, The PR is ready for review.

Looks good, will review properly tomorrow. Sorry about the delay!

Hey @yushi2006, sorry for the delay! I just got around to reviewing, but I think we can do this fix with one line (setting a `pre_topk` value). I pushed the changes to your branch, but if you think your solution is better, let me know! We want to keep PRs as small as possible, but if the results from my approach are worse then we should revert to yours instead.

Hey @Rocketknight1, no worries and thanks for checking it out! I just tested your fix and yeah — it works perfectly, and it’s definitely cleaner than my original approach. Let’s go with yours. If anything weird ever pops up later, we can always revisit, but for now this looks solid. Appreciate the refinement!

cc @ydshieh can you force-merge this one? The failing tests are definitely unrelated

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_39193). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.