Repository navigation
Return an empty list from batch_decode for an empty batch - #50873
Conversation
|
Thank you for your contribution 🤗! CI Security Gate — waiting for maintainer approvalCI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it. |
|
This PR might need docs. Beyond the empty-batch fix, this PR makes Pages that might need an update:
Posted automatically after approval. Docs live in |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. Pages changed in this PR: |
|
Thanks for the review and the cleanup commit. I updated the Mistral |
batch_decode()returns['']instead of[]when given an empty batch. I reproduced this with a list, NumPy array and PyTorch tensor.This is a regression from v4.57. I checked the old implementation, where
batch_decode()used a list comprehension and correctly returned an empty list. In v5, it passes the whole input todecode(), which causes the issue.This can cause prediction and reference counts to mismatch, which can lead to incorrect evaluation results. It also affects multimodal processors that use the tokenizer's
batch_decode().Minimal reproduction
Reproduction
I also created a small evaluation example using
hf-internal-testing/tiny-random-gpt2. It generates sequences and keeps only rows ending with EOS. When no rows match, the resulting tensor has shape(0, 6).preds: [''],len(preds)=1,len(refs)=0preds: [], counts match.This is a reproduction I put together to show how the bug can happen in an evaluation loop, not a reported user issue.
Fix
Added an early check in
tokenization_utils_base.pyto return[]when the batch is empty.Other behaviour stays the same:
decode([])still returns''.[[], []](shape(2, 0)) still returns['', ''].Tests
Added two tests in
tests/tokenization/test_tokenization_utils.py. Both fail on main and pass with this change.Full module: 31 passed, 1 skipped.
I also read the Mistral-common and Marian implementations. Both call
super().batch_decode(), so they get the fix too.cc @ArthurZucker @itazap (tokenizers)