Beam Decoding with Controlled Patience

Jungo Kasai, Keisuke Sakaguchi, Ronan Le Bras, Dragomir R. Radev, Yejin Choi, Noah A. Smith

Abstract

Text generation with beam search has proven successful in a wide range of applications. We point out that, though largely overlooked in the literature, the commonly-used implementation of beam decoding (e.g., Hugging Face Transformers and fairseq) uses a first come, first served (FCFS) heuristic: it keeps a set of already completed sequences over time steps and stops when the size of this set reaches the beam size. Based on this finding, we introduce a patience factor, a simple modification to this beam decoding implementation, that generalizes the stopping criterion and provides flexibility to the depth of search. Empirical results demonstrate that adjusting this patience factor improves decoding performance of strong pretrained models on news text summarization and machine translation over diverse language pairs, with a negligible inference slowdown. Our approach only modifies one line of code and can be thus readily incorporated in any implementation. Further, we find that different versions of beam decoding result in large performance differences in summarization, demonstrating the need for clarity in specifying the beam search implementation in research work.

Introduction

Beam search is the foundational algorithm for modern text generation, powering systems from large language models to machine translation engines. However, a significant discrepancy exists between the "vanilla" beam search described in academic textbooks and the actual implementations found in industry-standard libraries like Hugging Face Transformers and fairseq.

While academic literature often assumes a vanilla version where search continues until the top-$k$ candidates are finalized, practical implementations typically use a First Come, First Served (FCFS) heuristic. This heuristic stops the search as soon as $k$ finished sequences are found, regardless of whether potentially superior sequences might emerge in subsequent steps. This implementation detail creates an implicit link between search breadth (beam size) and search depth (stopping criterion), which is rarely acknowledged in research papers. This lack of clarity can lead to inconsistent results and overlooked optimization opportunities.

Methodology: Controlled Patience

To address this, we propose Controlled Patience, a simple yet effective modification to the standard FCFS implementation. By introducing a patience factor ($p$), we decouple the stopping criterion from the beam size ($k$). The modified stopping condition becomes:

if |F_t| ≥ k · p : return best sequence

where $|F_t|$ is the number of finished sequences at time step $t$. This allows for two distinct search behaviors:

  • High Patience ($p > 1$): The model searches deeper, allowing more candidates to complete before selecting the best result. This is beneficial when the model needs to explore more possibilities to find a higher-quality sequence.
  • Low Patience ($p < 1$): The model terminates earlier, favoring speed and potentially shorter, more concise outputs.
Visualizing the difference between Vanilla beam search, the standard FCFS heuristic, and the proposed FCFS with patience factor p.

This approach is particularly attractive for production environments as it requires changing only a single line of code and allows for independent tuning of search depth.

Experimental Setup

We evaluated Controlled Patience using strong pretrained models (BART and mBART) across two primary generation tasks:

  1. Machine Translation: Utilizing WMT 2020 and 2021 datasets for four language pairs: English to/from Polish (PL), German (DE), Japanese (JA), and Chinese (ZH).
  2. Text Summarization: Utilizing the CNN/Dailymail (CNNDM) and XSUM datasets to evaluate the generation of concise news summaries.

Key Results and Insights

Our experiments revealed that the optimal search depth is highly task-dependent. In machine translation, increasing patience ($p=2$) consistently improved performance across various language pairs. For instance, on the English→Polish task, the COMET score rose from 52.1 to 53.0. Conversely, in text summarization, lower patience ($p=0.5$) proved superior. On the CNNDM dataset, stopping earlier improved the ROUGE-L score from 30.3 to 31.2. This suggests that for summarization, searching too deep can lead to undesirable verbosity or hallucinations, often referred to as the "beam search curse."

Comprehensive results table comparing Greedy, Vanilla, FCFS, and FCFS with controlled patience across machine translation and summarization benchmarks.

Notably, the FCFS implementation with controlled patience often outperformed the "vanilla" beam search assumed in theoretical work. Furthermore, increasing the patience factor to $p=2$ resulted in a negligible slowdown in inference speed, making it a highly efficient optimization for real-world applications.

Conclusion

This work highlights the importance of implementation details in deep learning. By simply adjusting the "patience" of the beam search stopping criterion, we can achieve significant performance gains in both translation and summarization. Future research may explore dynamic patience factors that adapt based on model confidence or specific input characteristics to further optimize the balance between search depth and generation quality.

Summary updated

Abstract

Text generation with beam search has proven successful in a wide range of applications. We point out that, though largely overlooked in the literature, the commonly-used implementation of beam decoding (e.g., Hugging Face Transformers and fairseq) uses a first come, first served heuristic: it keeps a set of already completed sequences over time steps and stops when the size of this set reaches the beam size. Based on this finding, we introduce a patience factor, a simple modification to this beam decoding implementation, that generalizes the stopping criterion and provides flexibility to the depth of search. Empirical results demonstrate that adjusting this patience factor improves decoding performance of strong pretrained models on news text summarization and machine translation over diverse language pairs, with a negligible inference slowdown. Our approach only modifies one line of code and can be thus readily incorporated in any implementation. Further, we find that different versions of beam decoding result in large performance differences in summarization, demonstrating the need for clarity in specifying the beam search implementation in research work. Our code will be available upon publication.
BibTeX
@inproceedings{kasai2024beam,
  author = {Jungo Kasai and Keisuke Sakaguchi and Ronan Le Bras and Dragomir R. Radev and Yejin Choi and Noah A. Smith},
  title = {{Beam Decoding with Controlled Patience}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=ITCJRZUMLE},
}
Figure