This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

Best-First Beam Search

Clara Meister🟊   Tim Vieira   Ryan Cotterell✽,🟊
🟊ETH Zürich   Johns Hopkins University   University of Cambridge
[email protected]   [email protected]
[email protected]
Abstract

Decoding for many NLP tasks requires an effective heuristic algorithm for approximating exact search since the problem of searching the full output space is often intractable, or impractical in many settings. The default algorithm for this job is beam search—a pruned version of breadth-first search. Quite surprisingly, beam search often returns better results than exact inference due to beneficial search bias for NLP tasks. In this work, we show that the standard implementation of beam search can be made up to 10x faster in practice. Our method assumes that the scoring function is monotonic in the sequence length, which allows us to safely prune hypotheses that cannot be in the final set of hypotheses early on. We devise effective monotonic approximations to popular nonmonontic scoring functions, including length normalization and mutual information decoding. Lastly, we propose a memory-reduced variant of best-first beam search, which has a similar beneficial search bias in terms of downstream performance, but runs in a fraction of the time.

1 Introduction

Beam search is a common heuristic algorithm for decoding structured predictors, e.g., neural machine translation models and transition-based parsers. Due to the widespread adoption of recurrent neural networks and other non-Markov models, traditional dynamic programming solutions, such as the Viterbi algorithm Viterbi (1967), are prohibitively inefficient; this makes beam search a common component of many state-of-the-art NLP systems. Despite offering no formal guarantee of finding the highest-scoring hypothesis under the model, beam search yields impressive performance on a variety of tasks—unexpectedly providing a beneficial search bias over exact search for many tasks Stahlberg and Byrne (2019).

Within NLP, most research on beam search has focused on altering the log-probability scoring function to return improved results, e.g., higher bleu scores Wu et al. (2016); Murray and Chiang (2018); Shu and Nakayama (2018); Yang et al. (2018) or a more diverse set of outputs Vijayakumar et al. (2016). However, little work has been done to speed up beam search itself. Filling this gap, this paper focuses on reformulating beam search in order to make it faster. We propose best-first beam search, a prioritized version of traditional beam search which is up to an order of magnitude faster in practice while still returning the same set of results. We additionally discuss an even faster heuristic version of our algorithm which further limits the number of candidate solutions, leading to a smaller memory footprint while still finding good solutions.

Concretely, we offer a novel interpretation of beam search as an agenda-based algorithm where traditional beam search is recovered by employing a length-based prioritization scheme. We prove that a specific best-first prioritization scheme, as in classic A search Hart et al. (1968), allows for the elimination of paths that will necessarily fall off the beam; for many scoring functions, including standard log-probability scoring, we can still guarantee the same kk hypotheses as traditional beam search are returned. Indeed, our algorithm returns beam search’s top hypothesis the first time it encounters a complete hypothesis, allowing the program to stop early. Further, we discuss the application of best-first beam search to several popular scoring functions in the literature He et al. (2016); Li et al. (2016); this demonstrates that we have a general framework for adapting a variety of rescoring methods and alternate objectives to work with our algorithm.

Empirically, we compare best-first beam search to ordinary beam search on two NLP sequence-to-sequence tasks: neural machine translation (NMT) and abstractive summarization (AS). On NMT, we find that our algorithm achieves roughly a 30% speed-up over traditional beam search with increased gains for larger beams (e.g., 10\approx{10}x for a beam of 500). We find similar results hold for AS. Finally, we show that our memory-reduced version, which limits the number of active hypotheses, leads to additional speed-ups over best-first beam search across beam sizes while maintaining similar bleu scores. Our code is available online at https://github.com/rycolab/bfbs

2 Sequence Transduction

A core operation in structured prediction models is the determination of the highest-scoring output for a given input under a learned scoring model.

𝐲=defargmax𝐲𝒴(𝐱)score(𝐱,𝐲)\mathbf{y}^{\star}\overset{\mathrm{def}}{=}\operatorname*{argmax}_{\mathbf{y}\in\mathcal{Y}(\mathbf{x})}\ \mathrm{score}(\mathbf{x},\mathbf{y}) (1)

where 𝐱\mathbf{x} is an input and 𝒴(𝐱)\mathcal{Y}(\mathbf{x}) is a set of well-formed outputs for the input. An important example of 1 is maximum a posteriori (MAP),

𝐲MAP=defargmax𝐲𝒴(𝐱)p(𝐲𝐱).\mathbf{y}^{\mathrm{MAP}}\overset{\mathrm{def}}{=}\operatorname*{argmax}_{\mathbf{y}\in\mathcal{Y}(\mathbf{x})}\ p(\mathbf{y}\mid\mathbf{x}). (2)

Our work focuses on sequence-to-sequence transduction: predicting an output sequence given an input sequence. One such task is machine translation, wherein a source-language sentence is mapped (“transduced”) to a target-language sentence. While our exposition focuses on sequence-to-sequence prediction, our algorithms are directly applicable to any sequential structured prediction model, such as transition-based parsers Nivre et al. (2008) and sequence taggers McCallum et al. (2000); Lafferty et al. (2001).

Notation.

Let 𝐱=x1,,xN𝐱\mathbf{x}=\langle x_{1},\ldots,x_{N_{\mathbf{x}}}\rangle be an input sequence of length N𝐱N_{\mathbf{x}} and, likewise, let 𝐲=y1,,yN𝐲\mathbf{y}=\langle y_{1},\ldots,y_{N_{\mathbf{y}}}\rangle be an output sequence of length N𝐲N_{\mathbf{y}}. Each yty_{t} is an element of 𝒱\mathcal{V}, the set of output tokens. author=ryan,color=violet!40,size=,fancyline,caption=,]Are we consistent with input and output?author=clara,color=orange,size=,fancyline,caption=,]can’t find any inconsistencies so far but keeping this as a reminder to do another sweep Finally, let 𝒴(𝐱)\mathcal{Y}(\mathbf{x}) be the set of all valid output sequences (i.e., complete hypotheses). For the task of language generation, which we focus on experimentally, this set is defined as

𝒴(𝐱)=def{bos𝐯eos𝐯𝒱<nmax}\mathcal{Y}(\mathbf{x})\overset{\mathrm{def}}{=}\{\textsc{bos}\circ\mathbf{v}\circ\textsc{eos}\mid\mathbf{v}\in\mathcal{V}^{<n_{\textit{max}}}\} (3)

where \circ is string concatenation and 𝒱<nmax(𝐱)\mathcal{V}^{<n_{\textit{max}}(\mathbf{x})} is the set of all subsets of 𝒱\mathcal{V}^{\star} of size <nmax(𝐱)<n_{\textit{max}}(\mathbf{x}). In words, every valid sequence begins and ends with distinguished tokens (bos and eos, respectively).111bos and eos are typically members of 𝒱\mathcal{V}. Often, eos counts towards the nmaxn_{\textit{max}} length limit while bos does not. This is reflected in 3. Furthermore, each sequence has at most length nmax(𝐱)n_{\textit{max}}(\mathbf{x})—which is typically dependent on 𝐱\mathbf{x}—a restriction we impose to ensure termination. Some applications may require a stronger coupling between 𝒴(𝐱)\mathcal{Y}(\mathbf{x}) and 𝐱\mathbf{x} (e.g., |𝐱|=|𝐲||\mathbf{x}|=|\mathbf{y}|). We drop the dependence of 𝒴\mathcal{Y} and nmaxn_{\textit{max}} on 𝐱\mathbf{x} when it is clear from context.

Scoring.

We consider a general additively decomposable scoring model of the form

score(𝐱,𝐲)\displaystyle\mathrm{score}(\mathbf{x},\mathbf{y}) =t=1N𝐲score(𝐱,𝐲<tyt)\displaystyle=\sum_{t=1}^{N_{\mathbf{y}}}\mathrm{score}(\mathbf{x},\mathbf{y}_{<t}\circ y_{t}) (4)

This framework covers a variety of modeling methodologies including probabilistic transducers (both globally and locally normalized) and non-probabilistic models such as maximum-margin techniques Taskar et al. (2004). Most importantly, 4 covers MAP decoding 2 of neural sequence-to-sequence models à la Sutskever et al. (2014):222To see why, apply exp\exp (an order-preserving transformation): exp(scores2s(𝐱,𝐲))=exp(t=1N𝐲logp(yt𝐲<t,𝐱))=t=1N𝐲p(yt𝐲<t,𝐱)=p(𝐲𝐱)\exp(\mathrm{score}_{\textit{s2s}}(\mathbf{x},\mathbf{y}))=\exp\left(\sum_{t=1}^{N_{\mathbf{y}}}\log p(y_{t}\mid\mathbf{y}_{<t},\mathbf{x})\right)=\prod_{t=1}^{N_{\mathbf{y}}}p(y_{t}\mid\mathbf{y}_{<t},\mathbf{x})=p(\mathbf{y}\mid\mathbf{x}).

scores2s(𝐱,𝐲<tyt)=logp(yt𝐲<t,𝐱)\mathrm{score}_{\textit{s2s}}(\mathbf{x},\mathbf{y}_{<t}\circ y_{t})=\log p(y_{t}\mid\mathbf{y}_{<t},\mathbf{x}) (5)

We note that 5 is the scoring function used for decoding many language generation models. author=clara,color=orange,size=,fancyline,caption=,]citations

Beam search.

The worst-case running time of exactly computing 1 is exponential in nmaxn_{\textit{max}}; namely, 𝒪(|𝒱|nmax)\mathcal{O}(|\mathcal{V}|^{n_{\textit{max}}}).333This can be improved if, for example, score(,)\mathrm{score}(\cdot,\cdot) admits a low-order Markov factorization (Viterbi, 1967; Vieira et al., 2016). We do not discuss that setting in this paper because it limits the scoring model’s expressive power. Beam search is a commonly used approximation to 1 in NMT and language generation tasks. It is used in many (if not most) state-of-the-art NLP systems Wu et al. (2016); Serban et al. (2017); Edunov et al. (2018); Yang et al. (2019). Beam search may be understood as a pruned version of the classic path-search algorithm, breadth-first search (BFS), where the breadth is narrowed to the beam size kk. Pseudocode is given in footnote 5.

Although, beam search does not solve 1 exactly, it is a surprisingly useful approximation for NLP models. In many settings, beam search outperforms exact methods in terms of downstream evaluation Koehn and Knowles (2017); Stahlberg and Byrne (2019). For the remainder of this paper, we will pivot our attention away from exact solutions to 1 to exact solutions to the beam search output.

Definition 2.1.

kk-optimal hypothesis. We say that a hypothesis is kk-optimal if it is the top hypothesis returned by beam search with beam size kk.

Input: 𝐱\mathbf{x}: source sentence
          kk: maximum beam size
          nmaxn_{\textit{max}}: maximum hypothesis length
          score(,)\mathrm{score}(\cdot,\cdot): scoring function

1:B0{0,bos}B_{0}\leftarrow\{\langle 0,\textsc{bos}\rangle\}
2:for  t{1,,nmax}t\in\{1,\dots,n_{max}\}  :
3:   BB\leftarrow\emptyset
4:   for s,𝐲Bt1\langle s,\mathbf{y}\rangle\in B_{t-1} :
5:     if 𝐲.last()=eos\mathbf{y}.\mathrm{last}()=\textsc{eos} :
6:       B.add(s,𝐲)B.\mathrm{add}(\langle s,\mathbf{y}\rangle)
7:       continue      
8:     for y𝒱y\in\mathcal{V} :
9:       sscore(𝐱,𝐲y)s\leftarrow\mathrm{score}(\mathbf{x},\mathbf{y}\circ y)
10:       B.add(s,𝐲y)B.\mathrm{add}(\langle s,\mathbf{y}\circ y\rangle)         
11:   BtB.top(k)B_{t}\leftarrow B.\mathrm{top}(k)
12:return Bnmax.max()B_{n_{max}}.\mathrm{max}()
Algorithm 1 Standard beam search555Often, the score\mathrm{score} function is additively decomposable in tt, such as 5. Implementations can exploit this fact to make each score evaluation (line 9) 𝒪(1)\mathcal{O}(1) rather than 𝒪(t)\mathcal{O}(t). We did not make this implementation detail explicit in footnote 5 or footnote 7 for generality and simplicity.

3 A Beam Search

We develop a meta-algorithm that is parameterized by several choice points. Our general search algorithm for decoding (footnote 7) takes an arbitrary prioritization function, stopping criterion, and search heuristic. With certain values of these attributes, we recover many common search algorithms: greedy search, beam search, best-first search Dijkstra (1959), and A search Hart et al. (1968). We propose an alternate prioritization function for beam search that allows for faster decoding while still returning the same kk-optimal set of hypotheses.

Input: 𝐱\mathbf{x}: source sentence
          nmaxn_{\textit{max}}: maximum hypothesis length
          score(,)\mathrm{score}(\cdot,\cdot): scoring function
          \varogreaterthan: comparator 1
          stop()\mathrm{stop}(\cdot): stopping criterion 2
          kk: maximum beam size 3
          h(,)h(\cdot,\cdot): heuristic function 4

1:𝒬\mathcal{Q}\leftarrow priority_queue()\mathrm{priority\_queue}(\varogreaterthan)
2:𝒬.push(0,bos)\mathcal{Q}.\mathrm{push}(\langle 0,\textsc{bos}\rangle)
3:popscounter()\textsc{pops}\leftarrow\mathrm{counter}()
4:while not stop(𝒬)\mathrm{stop}(\mathcal{Q}) and not 𝒬.empty()\mathcal{Q}.\mathrm{empty}() :
5:   sh,𝐲𝒬.pop()\langle s_{h},\mathbf{y}\rangle\leftarrow\mathcal{Q}.\mathrm{pop}()
6:   if pops[|𝐲|]k\textsc{pops}[|\mathbf{y}|]\geq k or |𝐲|>nmax|\mathbf{y}|>n_{\textit{max}} : 7:     continue     8:   pops[|𝐲|]pops[|𝐲|]+1\textsc{pops}[|\mathbf{y}|]\leftarrow\textsc{pops}[|\mathbf{y}|]+1
9:   if 𝐲.last()=eos\mathbf{y}.\mathrm{last}()=\textsc{eos} :
10:     𝒬.push(sh,𝐲eos)\mathcal{Q}.\mathrm{push}(\langle s_{h},\mathbf{y}\circ\textsc{eos}\rangle)
11:   else:
12:     for y𝒱y\in\mathcal{V} :
13:       sscore(𝐱,𝐲y)s\leftarrow\mathrm{score}(\mathbf{x},\mathbf{y}\circ y)
14:       shs+s_{h}\leftarrow s+ h(𝐱,𝐲y)h(\mathbf{x},\mathbf{y}\circ y)
15:       𝒬.push(sh,𝐲y)\mathcal{Q}.\mathrm{push}(\langle s_{h},\mathbf{y}\circ y\rangle)         
16:return 𝒬.pop()\mathcal{Q}.\mathrm{pop}() if not 𝒬.empty()\mathcal{Q}.\mathrm{empty}() else null
Algorithm 2 General decoding scheme.footnotemark: ,777If the last token of 𝐲\mathbf{y}^{\prime} is the end symbol (e.g., eos), then 𝐲\mathbf{y}^{\prime} is not expanded any further. One can either regard 𝐲\mathbf{y}^{\prime} as any other hypothesis albeit with 𝐲yt=𝐲\mathbf{y}^{\prime}\circ y_{t}=\mathbf{y}^{\prime} or keep appending eos (i.e 𝐲yt=𝐲eos\mathbf{y}^{\prime}\circ y_{t}=\mathbf{y}^{\prime}\circ\textsc{eos} ) so that time step and length can be regarded as synonymous. We adopt the latter standard for comparability with subsequent algorithms. Highlighted sections are choice points in the algorithm for which values determine the search strategy. See § 3.1 for detailed explanation.

3.1 Choice Points of footnote 7

Here we review the components of our meta algorithm (the highlighted sections in footnote 7) that can be varied to recover different search strategies:

  • 1

    :𝐲×𝐲{True, False}\varogreaterthan:\mathbf{y}\times\mathbf{y}\to\{\text{True, False}\}.author=clara,color=orange,size=,fancyline,caption=,]doing binary T/F felt a bit strange A priority queue 𝒬\mathcal{Q} maintains the set of active hypotheses. Elements in this set are ordered according to a generic comparator \varogreaterthan. When its peek()\mathrm{peek}() (or pop()\mathrm{pop}()) methods are called, the first element ordered by \varogreaterthan is returned (or returned and removed).

  • 2

    stop():Collection𝐲{True, False}\mathrm{stop}(\cdot):\mathrm{Collection}\langle\mathbf{y}\rangle\to\{\text{True, False}\}. The algorithm terminates according to configurable stopping criterion based on the current set of elements in 𝒬\mathcal{Q}.

  • 3

    k>0k\in\mathbb{N}_{>0}. Only kk paths of a given length are considered. If the algorithm has already encountered kk paths of a given length, subsequent paths of that length are not evaluated. If we take k=k=\infty, we recover unpruned search algorithms.

  • 4

    h(,):𝐱×𝐲h(\cdot,\cdot):\mathbf{x}\times\mathbf{y}\to\mathbb{R}. A heuristic function h(𝐱,𝐲)h(\mathbf{x},\mathbf{y}) can be used during search to change the priority in which paths are evaluated. We note that with pruning, a heuristic may change the value of the kk-optimal hypothesis (see § 4.1).

Beam Search Best-First Beam Search A Beam Search 1 \tabularCenterstackcsh,𝐲sh,𝐲|𝐲|<|𝐲|\langle s_{h},\mathbf{y}\rangle\varogreaterthan\langle s_{h}^{\prime},\mathbf{y}^{\prime}\rangle\iff|\mathbf{y}|<|\mathbf{y}|^{\prime} or (|𝐲|=|𝐲| and shsh)\textbf{ or }\left(|\mathbf{y}|=|\mathbf{y}|^{\prime}\textbf{ and }s_{h}\geq s_{h}^{\prime}\right) \tabularCenterstackcsh,𝐲sh,𝐲sh>sh\langle s_{h},\mathbf{y}\rangle\varogreaterthan\langle s_{h}^{\prime},\mathbf{y}^{\prime}\rangle\iff s_{h}>s_{h}^{\prime} or (sh=sh and |𝐲|<|𝐲|)\textbf{ or }\left(s_{h}=s_{h}^{\prime}\textbf{ and }|\mathbf{y}|<|\mathbf{y}|^{\prime}\right) \tabularCenterstackcsh,𝐲sh,𝐲sh>sh\langle s_{h},\mathbf{y}\rangle\varogreaterthan\langle s_{h}^{\prime},\mathbf{y}^{\prime}\rangle\iff s_{h}>s_{h}^{\prime} or (sh=sh and |𝐲|<|𝐲|)\textbf{ or }\left(s_{h}=s_{h}^{\prime}\textbf{ and }|\mathbf{y}|<|\mathbf{y}|^{\prime}\right) 2 \tabularCenterstackcstop(𝒬)\mathrm{stop}{(\mathcal{Q})}\iff 𝐲.last()=eos𝐲𝒬\mathbf{y}.\mathrm{last}()=\textsc{eos}\quad\forall\mathbf{y}\in\mathcal{Q} \tabularCenterstackcstop(𝒬)\mathrm{stop}(\mathcal{Q})\iff 𝒬.peek().last()=eos\mathcal{Q}.\mathrm{peek}().\mathrm{last}()=\textsc{eos} \tabularCenterstackcstop(𝒬)\mathrm{stop}(\mathcal{Q})\iff 𝒬.peek().last()=eos\mathcal{Q}.\mathrm{peek}().\mathrm{last}()=\textsc{eos} 3 k=k= beam size k=k= beam size k=k= beam size 4 0 0 any admissible heuristic Breadth-First Search Best-First Search A Search 1 \tabularCenterstackcsh,𝐲sh,𝐲|𝐲|<|𝐲|\langle s_{h},\mathbf{y}\rangle\varogreaterthan\langle s_{h}^{\prime},\mathbf{y}^{\prime}\rangle\iff|\mathbf{y}|<|\mathbf{y}|^{\prime} or (|𝐲|=|𝐲| and shsh)\textbf{ or }\left(|\mathbf{y}|=|\mathbf{y}|^{\prime}\textbf{ and }s_{h}\geq s_{h}^{\prime}\right) \tabularCenterstackcsh,𝐲sh,𝐲sh>sh\langle s_{h},\mathbf{y}\rangle\varogreaterthan\langle s_{h}^{\prime},\mathbf{y}^{\prime}\rangle\iff s_{h}>s_{h}^{\prime} or (sh=sh and |𝐲|<|𝐲|)\textbf{ or }\left(s_{h}=s_{h}^{\prime}\textbf{ and }|\mathbf{y}|<|\mathbf{y}|^{\prime}\right) \tabularCenterstackcsh,𝐲sh,𝐲sh>sh\langle s_{h},\mathbf{y}\rangle\varogreaterthan\langle s_{h}^{\prime},\mathbf{y}^{\prime}\rangle\iff s_{h}>s_{h}^{\prime} or (sh=sh and |𝐲|<|𝐲|)\textbf{ or }\left(s_{h}=s_{h}^{\prime}\textbf{ and }|\mathbf{y}|<|\mathbf{y}|^{\prime}\right) 2 \tabularCenterstackcstop(𝒬)\mathrm{stop}{(\mathcal{Q})}\iff 𝐲.last()=eos𝐲𝒬\mathbf{y}.\mathrm{last}()=\textsc{eos}\quad\forall\mathbf{y}\in\mathcal{Q} \tabularCenterstackcstop(𝒬)\mathrm{stop}{(\mathcal{Q})}\iff 𝒬.peek().last()=eos\mathcal{Q}.\mathrm{peek}().\mathrm{last}()=\textsc{eos} \tabularCenterstackcstop(𝒬)\mathrm{stop}{(\mathcal{Q})}\iff 𝒬.peek().last()=eos\mathcal{Q}.\mathrm{peek}().\mathrm{last}()=\textsc{eos} 3 k=k=\infty k=k=\infty k=k=\infty 4 0 0 any admissible heuristic

Table 1: Values at choice points for various search algorithms. Note that any admissible heuristic may be used for variants of A\text{A}^{*} search.

Recovering Beam Search.

To recover beam search from footnote 7, we use the choice points from Tab. 1. Explicitly, the comparator prioritizes hypotheses from earlier time steps first, but breaks ties with the hypotheses’ scores under the model. We note that while the standard algorithm for beam search does not prioritize by score within a time step, variations of the algorithm use this strategy so they can employ early-stopping strategies Klein et al. (2017); Huang et al. (2017). Beam search terminates once either all hypotheses end in eos or the queue is empty (i.e., when the kk beams have been extended nmaxn_{\textit{max}} time steps but none end in eos). In the second case, no complete hypothesis is found. Finally, choosing the heuristic h(𝐱,𝐲)=0h(\mathbf{x},\mathbf{y})=0 makes the algorithm a case of standard best-first search.

Note that, while standard beam search returns a set, footnote 7 only returns the kk-optimal hypothesis. This behavior is sufficient for the majority of use cases for beam search. However, if the full set of kk hypotheses is desired, the stopping criterion can be changed to evaluate true only when kk hypotheses are complete. Under the other beam search settings, this would provably return the same set as beam search (see § 4.1).

Recovering A.

To recover the traditional A search algorithm, we use the comparator that prioritizes hypotheses with a higher score first; ties are broken by hypothesis length. The algorithm terminates when the first item of 𝒬\mathcal{Q} contains an eos. If we take k=k=\infty, best-first beam search recovers A. Any admissible heuristic may be used for h(𝐱,𝐲)h(\mathbf{x},\mathbf{y}).

Definition 3.1.

Admissible Heuristic. A heuristic hh is admissible if it never overestimates the future cost—or underestimates the future reward—of continuing down a path.

3.2 Best-First Beam Search

In its original form, A\text{A}^{*} search may traverse the entire 𝒪(|𝒱|nmax)\mathcal{O}(|\mathcal{V}|^{n_{\textit{max}}}) graph, which as discussed earlier, is intractable for many decoding problems. While standard beam search addresses this problem by limiting the search space, it still has computational inefficiencies—namely, we must analyze kk hypotheses of a given length (i.e., time step), regardless of how poor their scores may already be, before considering longer hypotheses. However, prioritization by length is not strictly necessary for finding a kk-optimal hypothesis. As is done in A\text{A}^{*}, we can use score as the prioritization scheme and still guarantee optimality–or kk-optimality–of the paths returned by the algorithm.

We define A\text{A}^{*} beam search as the A\text{A}^{*} algorithm where breadth is limited to size kk. Further, we define best-first beam search as the case of A\text{A}^{*} beam search when no heuristic is used (see Tab. 1 for algorithm settings). This formulation has two large advantages over standard beam search: (1) we gain the ability to remove paths from the queue that are guaranteed to fall off the beam and (2) we can terminate the algorithm the first time a complete hypothesis is encountered. We can therefore reduce the computation required for decoding while still returning the same set of results.

The mathematical property that makes this short-circuiting of computation possible is the monotonicity of the scoring function. Note that not all scoring functions are monotonic, but many important ones are, including log-probability 5. We discuss effective approximations for popular non-monotonic scoring functions in § 5.

Definition 3.2.

Monotonicity. A scoring function score(,)\mathrm{score}(\cdot,\cdot) is monotonic in tt if for all 𝐱\mathbf{x}, 𝐲<t=y1yt1\mathbf{y}_{<t}=\langle y_{1}\ldots y_{t-1}\rangle, yt𝒱, 1tnmaxy_{t}\in\mathcal{V},\,1\leq t\leq n_{\textit{max}}

score(𝐱,𝐲<t)score(𝐱,\displaystyle\mathrm{score}(\mathbf{x},\mathbf{y}_{<t})\geq\mathrm{score}(\mathbf{x}, 𝐲<tyt)\displaystyle\mathbf{y}_{<t}\circ y_{t})

Clearly, 5 is a monotonic scoring function in tt because scores2s0\mathrm{score}_{\textit{s2s}}\leq 0, that is, the score\mathrm{score} of a partial hypothesis 𝐲<t\mathbf{y}_{<t} can only decrease if we extend it by another symbol yty_{t}author=clara,color=orange,size=,fancyline,caption=,]flesh out a bit more. This implies we can order our search according to score(𝐱,𝐲<t)\mathrm{score}(\mathbf{x},\mathbf{y}_{<t}) without fear of overlooking a hypothesis whose score would increase over time. Furthermore, once kk hypotheses of a given length tt have been evaluated, we no longer need to consider any hypothesis where |𝐲|<t|\mathbf{y}|<t since such hypotheses would necessarily fall off the beam. We can therefore remove such hypotheses from the queue and avoid wasting computational power on their evaluation. We prove this formally in § 4.1.

Another implication of the monotonicity property of score\mathrm{score} is that we may terminate best-first beam search once a hypothesis containing eos is encountered (i.e., the end state is found). If the full set of kk complete hypotheses is desired, then we simply continue until kk hypotheses have reached eos. We prove the kk-optimality of these hypotheses under best-first beam search in § 4.1.

3.3 Implementation Details

Standard beam search forms a separate set of active hypotheses for each time step, i.e., each BtB_{t} is its own set. Once BtB_{t} has been narrowed down to the top kk, the previous B<tB_{<t} can be forgotten. However in best-first beam search, since hypotheses are not evaluated in order of time step, we may need to keep BtB_{t} from several time steps at any given point.

A naive implementation of best-first beam search is to keep a single priority queue with all the active hypotheses ordered by current score. However, each push to the queue would then require 𝒪(log(nmaxk|𝒱|))\mathcal{O}(\log(n_{\textit{max}}k|\mathcal{V}|)) time. We can reduce this runtime by instead keeping a priority queue of beams, where the priority queue is ordered by the highest-scoring hypothesis from each beam. Further, each beam can be represented by a min-max queue Atkinson et al. (1986); this allows us to limit the size of BtB_{t} to kk: we can check in 𝒪(1)\mathcal{O}(1) time if a hypothesis is in the top-kk before adding it to BtB_{t}.

A potential inefficiency, which we avoid, comes from updating Bt+1B_{t+1}, which we must do when evaluating a hypothesis from BtB_{t}. Since all beams are stored in a queue, there is no guarantee of the location in the queue of Bt+1B_{t+1}. To avoid 𝒪(nmax)\mathcal{O}(n_{\textit{max}}) lookup, we can keep a pointer to each beam, indexed by tt making the lookup 𝒪(1)\mathcal{O}(1). However, we acquire a 𝒪(lognmax)\mathcal{O}(\log n_{\textit{max}}) term to update the queue of beams as Bt+1B_{t+1} may change priority.

Memory-Reduced Best-First Beam Search.

A major drawback of the A\text{A}^{*} algorithm is its memory usage, which in the worst-case is 𝒪(bd)\mathcal{O}(b^{d}) for breadth width bb and maximum depth dd. In the A\text{A}^{*} formulation of beam search, where the breadth width is limited to the beam size, this amounts to worst-case 𝒪(knmax)\mathcal{O}(k\cdot{n_{max}}) memory usage, where standard beam search has 𝒪(k)\mathcal{O}(k) memory usage. While in many settings the multiplicative factor may be insignificant, for neural sequence models it can be prohibitive; this is due to the large amount of memory required to store each hypothesis (e.g., prior hidden states needed to compute subsequent scores for scoring functions parameterized by neural networks).

We propose a variant of best-first beam search that limits memory usage, i.e., the queue capacity. Specifically, if we reach the chosen queue capacity, we remove the worst scoring active hypothesis from the earliest active time step. This can easily be done in 𝒪(1)\mathcal{O}(1) time given our pointer to each beam.

4 Algorithm Analysis

4.1 Correctness

We show the equivalence of the top hypothesis888Best-first beam search is guaranteed to return the same set of kk hypotheses as beam search. We include the proof for only the top hypothesis for simplicity. The proof for set equality follows naturally. returned by beam search and best-first beam search when score(,)\mathrm{score}(\cdot,\cdot) is monotonically decreasing in tt, length-based prioritization is used and the beam size kk is the same for both algorithms. Without loss of generality, we hold 𝐱\mathbf{x} constant in all the following proofs.

Note that we take the terms pop and push from queue terminology. Specifically, “popping a hypothesis” refers to making it past line 7 of footnote 7, where a hypothesis 𝐲\mathbf{y} is expanded by yt𝒱y_{t}\in\mathcal{V}. In path search terminology, this would be equivalent to visiting a node and adding the edges from that node as potential paths to explore. Lastly, we refer to the priority queue used by beam search and best-first beam search as 𝒬BS\mathcal{Q}_{\mathrm{BS}} and 𝒬A\mathcal{Q}_{\mathrm{A}^{*}}, respectively.

Lemma 4.1.

Best-first beam search evaluates all hypotheses of a given length tt in order of their score.

Proof.

We prove the lemma by induction. The lemma holds trivially for the base case of hypotheses of length 0 because the only hypothesis of length 0 is bos\langle\textsc{bos}\rangle.

Now, by the inductive hypothesis, suppose Lemma 4.1 holds for all hypotheses of length <t<t. We will show it must also hold for hypotheses of length tt. Consider two competing hypotheses: 𝐲=𝐲<tyt\mathbf{y}=\mathbf{y}_{<t}\circ y_{t} and 𝐲=𝐲<tyt\mathbf{y}^{\prime}=\mathbf{y}_{<t}^{\prime}\circ y^{\prime}_{t}. Note that |𝐲<t|=|𝐲<t|=t1|\mathbf{y}_{<t}|=|\mathbf{y}_{<t}^{\prime}|=t-1. Suppose score(𝐱,𝐲)<score(𝐱,𝐲)\mathrm{score}(\mathbf{x},\mathbf{y}^{\prime})<\mathrm{score}(\mathbf{x},\mathbf{y}).

Case 1: score(𝐱,𝐲<t)<score(𝐱,𝐲<t)\mathrm{score}(\mathbf{x},\mathbf{y}_{<t}^{\prime})<\mathrm{score}(\mathbf{x},\mathbf{y}_{<t}). Then by induction, 𝐲<t\mathbf{y}_{<t} popped first and 𝐲\mathbf{y} is pushed to 𝒬\mathcal{Q} before 𝐲\mathbf{y}^{\prime}. Since score(𝐱,𝐲)<score(𝐱,𝐲)\mathrm{score}(\mathbf{x},\mathbf{y}^{\prime})<\mathrm{score}(\mathbf{x},\mathbf{y}), 𝐲\mathbf{y} will be popped before 𝐲\mathbf{y}^{\prime}.

Case 2: score(𝐱,𝐲<t)<score(𝐱,𝐲<t)\mathrm{score}(\mathbf{x},\mathbf{y}_{<t})<\mathrm{score}(\mathbf{x},\mathbf{y}_{<t}^{\prime}). Then by induction, 𝐲<t\mathbf{y}_{<t}^{\prime} is popped first and 𝐲\mathbf{y}^{\prime} is added to 𝒬\mathcal{Q} before 𝐲\mathbf{y}. But, since score(𝐱,𝐲)<score(𝐱,𝐲)score(𝐱,𝐲<t)\mathrm{score}(\mathbf{x},\mathbf{y}’)<\mathrm{score}(\mathbf{x},\mathbf{y})\leq\mathrm{score}(\mathbf{x},\mathbf{y}_{<t}) by monotonicity, then 𝐲<t\mathbf{y}_{<t} will be popped before 𝐲\mathbf{y}’. Consequently, 𝐲\mathbf{y} will be pushed to 𝒬\mathcal{Q} before 𝐲\mathbf{y}^{\prime} is evaluated. By the rules of the priority queue 𝐲\mathbf{y} will be evaluated before 𝐲\mathbf{y}^{\prime}.

Case 3: score(𝐱,𝐲)=score(𝐱,𝐲\mathrm{score}(\mathbf{x},\mathbf{y}’)=\mathrm{score}(\mathbf{x},\mathbf{y}). The lemma holds if either 𝐲\mathbf{y} or 𝐲\mathbf{y}^{\prime} is popped first.

By the principle of induction, Lemma 4.1 holds for all t>0t\in\mathbb{N}_{>0}. ∎

Lemma 4.2.

The first hypothesis that best-first beam search pops that ends in eos is k-optimal.

Proof.

Let 𝐲\mathbf{y} be the first hypothesis popped by best-first beam search ending in eos. By rules of the priority queue, no other active hypothesis has a higher score than 𝐲\mathbf{y}. Additionally, by monotonicity of the scoring function, no other hypothesis can subsequently have score greater than 𝐲\mathbf{y}. Therefore 𝐲\mathbf{y} must be k-optimal. ∎

Lemma 4.3.

If best-first beam search pops a hypothesis, then beam search necessarily pops that same hypothesis.

Proof.

We prove the lemma by induction on hypothesis length. The base case holds trivially: For hypotheses of length 0, both best-first beam search and beam search must pop the bos\langle\textsc{bos}\rangle as it is the only item in the queue after initialization.

By the inductive hypothesis, suppose Lemma 4.3 holds for hypotheses of length <t<t. Suppose best-first beam search pops a hypothesis 𝐲=𝐲<tyt\mathbf{y}=\mathbf{y}_{<t}\circ y_{t} of length tt.

Case 1: Best-first beam search pops kk hypotheses of length t1t-1 before popping 𝐲\mathbf{y}, which is of length tt. The sets of hypotheses of length t1t-1 that each algorithm pops are necessarily the same by the inductive hypothesis and the fact that they have the same cardinality. If best-first beam search pops 𝐲\mathbf{y}, which is of length tt, then it must be in the top-kk highest-scoring hypotheses of length tt in 𝒬A\mathcal{Q}_{\mathrm{A}^{*}} by the rules of the priority queue. Consequently, it must be in the top-kk in 𝒬BS\mathcal{Q}_{\mathrm{BS}}.

Case 2: Best-first beam search has popped fewer than kk hypotheses of length t1t-1 before popping 𝐲\mathbf{y}. Then, all remaining hypotheses of length t1t-1 in 𝒬A\mathcal{Q}_{\mathrm{A}^{*}} must have score(𝐱,𝐲<t)<score(𝐱,𝐲)\mathrm{score}(\mathbf{x},\mathbf{y}^{\prime}_{<t})<\mathrm{score}(\mathbf{x},\mathbf{y}) by the rules of the priority queue. By the monotonicity of the score function, all extensions of those 𝐲<t\mathbf{y}^{\prime}_{<t} will also have score(𝐱,𝐲<tyt)<score(𝐱,𝐲)\mathrm{score}(\mathbf{x},\mathbf{y}^{\prime}_{<t}\circ y^{\prime}_{t})<\mathrm{score}(\mathbf{x},\mathbf{y}). Because none of 𝐲<tyt\mathbf{y}_{<t}’\circ y_{t}^{\prime} has greater score than 𝐲\mathbf{y}, 𝐲\mathbf{y} must be in BtB_{t}. ∎

Corollary 4.3.1.

Best-first beam search will never pop more hypotheses than beam search.

Theorem 4.4.

author=ryan,color=violet!40,size=,fancyline,caption=,]Need to think and revisit Once best-first beam search has popped kk hypotheses of length tt, hypotheses from time steps <t<t do not need to be popped.

Proof.

This follows from Lemma 4.1. If kk hypotheses of length tt have been popped, then these must be the top-kk hypotheses from time step tt. Therefore no hypothesis from time step <t<t that is still in 𝒬A\mathcal{Q}_{\mathrm{A}^{*}} would be in the top-kk at time step tt. ∎

Theorem 4.5.

Let bs\mathcal{H}_{\textsc{bs}} and A\mathcal{H}_{\textsc{A}} be the set of kk hypotheses returned by beam search and best-first beam search, respectively. bs=A\mathcal{H}_{\textsc{bs}}=\mathcal{H}_{\textsc{A}}.

Proof.

Since |bs|=|A|=k|\mathcal{H}_{\textsc{bs}}|=|\mathcal{H}_{\textsc{A}}|=k, we only need to show 𝐲bs𝐲A\mathbf{y}\in\mathcal{H}_{\textsc{bs}}\Longrightarrow\mathbf{y}\in\mathcal{H}_{\textsc{A}}.

Suppose, by way of contradiction, there exists a hypothesis 𝐲bs\mathbf{y}\in\mathcal{H}_{\textsc{bs}} such that 𝐲A\mathbf{y}\not\in\mathcal{H}_{\textsc{A}}. If 𝐲A\mathbf{y}\not\in\mathcal{H}_{\textsc{A}} then we must not pop the prefix 𝐲<t\mathbf{y}_{<t} (where 𝐲=𝐲<t𝐲t:|𝐲|\mathbf{y}=\mathbf{y}_{<t}\circ\mathbf{y}_{t:|\mathbf{y}|}) for some time step t<|𝐲|t<|\mathbf{y}|.

Case 1: At some time step t+jt+j (j0j\geq 0), we pop kk partial hypotheses {𝐲t+j(1),,𝐲t+j(k)}\{\mathbf{y}_{\leq t+j}^{(1)},\dots,\mathbf{y}_{\leq t+j}^{(k)}\} where 𝐲t+j{𝐲t+j(1),,𝐲t+j(k)}\mathbf{y}_{\leq t+j}\not\in\{\mathbf{y}_{\leq t+j}^{(1)},\dots,\mathbf{y}_{\leq t+j}^{(k)}\}. By Lemma 4.1, it must be that score(𝐱,𝐲t+j(i))>score(𝐱,𝐲t+j\mathrm{score}(\mathbf{x},\mathbf{y}_{\leq t+j}^{(i)})>\mathrm{score}(\mathbf{x},\mathbf{y}_{\leq t+j}) i1,,k\forall i\in 1,\dots,k. This implies that for beam search, 𝐲t+j\mathbf{y}_{\leq t+j} would not be in the top-kk paths at time step t+jt+j since by Lemma 4.3, paths {𝐲t+j(1),,𝐲t+j(k)}\{\mathbf{y}_{\leq t+j}^{(1)},\dots,\mathbf{y}_{\leq t+j}^{(k)}\} would also be evaluated by beam search. Therefore 𝐲\mathbf{y} cannot be in bs\mathcal{H}_{\textsc{bs}}, which is a contradiction.

Case 2: For no time step t+jt+j (j0j\geq 0) do we pop kk paths. This can only happen if the algorithm stops early, i.e we have found kk complete hypotheses 𝐲(1),,𝐲(k)\mathbf{y}^{(1)},\dots,\mathbf{y}^{(k)}. If this is the case, then by rules of the priority queue, each 𝐲(1),,𝐲(k)\mathbf{y}^{(1)},\dots,\mathbf{y}^{(k)} must have score greater than score(𝐱,𝐲<t)\mathrm{score}(\mathbf{x},\mathbf{y}_{<t}). By monotonicity of the score function, score(𝐱,𝐲(i))>score(𝐱,𝐲\mathrm{score}(\mathbf{x},\mathbf{y}^{(i)})>\mathrm{score}(\mathbf{x},\mathbf{y}). This implies 𝐲\mathbf{y} cannot be in bs\mathcal{H}_{\textsc{bs}}, which is a contradiction. ∎

Non-monotonic Scoring Functions.

Non-monotonic scoring functions (definition 3.2) break the assumptions of § 4.1, in which case best-first beam search is not guaranteed to return a kk-optimal hypothesis. However, when the scoring function is boundable from above, we can alter the original stopping criterion ( 2 in footnote 7) such that kk-optimality is again guaranteed.

Given our assumed restriction on the search space—namely, |𝐲𝒴(𝐱)|nmax(𝐱)|\mathbf{y}^{\star}\in\mathcal{Y}(\mathbf{x})|\leq n_{\textit{max}}(\mathbf{x})—we can upper-bound the maximal score of any hypothesis under the scoring function in use. Formally, for any function score\mathrm{score} we have:

stop(𝒬)\displaystyle\mathrm{stop}{(\mathcal{Q})}\iff
score(𝐱,𝐲^)\displaystyle\mathrm{score}(\mathbf{x},\hat{\mathbf{y}})\geq\ score(𝐱,𝐲)+𝒰(𝐱,𝐲)\displaystyle\mathrm{score}(\mathbf{x},\mathbf{y}^{\prime})+\mathcal{U}(\mathbf{x},\mathbf{y}^{\prime})
𝐲𝒬\displaystyle\qquad\qquad\quad\forall\mathbf{y}^{\prime}\in\mathcal{Q} (6)

where 𝐲^\hat{\mathbf{y}} is the best complete hypothesis found so far and 𝒰(𝐱,𝐲)\mathcal{U}(\mathbf{x},\mathbf{y}^{\prime}) is the score\mathrm{score} function-dependent upper bound on how much the score of 𝐲\mathbf{y}^{\prime} can increase as 𝐲\mathbf{y}^{\prime} is expanded further.999For monotonic scoring functions, we have 𝒰(𝐱,𝐲)=0\mathcal{U}(\mathbf{x},\mathbf{y}^{\prime})=0. In this situation, best-first beam search only terminates once no other hypothesis in 𝒬\mathcal{Q} can have a score greater than the best finished hypothesis. We note that Huang et al. (2017) use a similar scheme for optimal stopping with bounded length normalization. We discuss examples of non-monotonic scoring functions in § 5.

A Note on Heuristics.

Our analysis shows the equivalence of beam search and best-first beam search, i.e., when h(𝐱,𝐲)=0h(\mathbf{x},\mathbf{y})=0. The analysis does not hold for arbitrary admissible heuristics. A poor heuristic, e.g., one that grossly overestimates the future score of continuing down one path, may cause other items to be pruned from best-first beam search that otherwise would have remained on the beam in standard beam search.

4.2 Runtime

Theorem 4.6.

The runtime of best-first beam search is 𝒪(nmaxk(|𝒱|log(k)+log(nmax)))\mathcal{O}(n_{\textit{max}}k\,(|\mathcal{V}|\log(k)+\log(n_{\textit{max}})))

Proof.

We pop at most nmaxkn_{\textit{max}}\cdot k items. Each pop requires us to push |𝒱||\mathcal{V}| items. Each push requires log(k)\log(k) time when the priority queue is implemented with a min–max heap Atkinson et al. (1986) and incrementally pruned so that it has no more than kk items. After pushing those |𝒱||\mathcal{V}| items, we have to perform a percolation in the priority queue of priority queues which requiers log(nmax)\log(n_{\textit{max}}) time. This yields 𝒪(nmaxk(|𝒱|log(k)+log(nmax)))\mathcal{O}(n_{\textit{max}}k\,(|\mathcal{V}|\log(k)+\log(n_{\textit{max}}))) time. ∎

Theorem 4.7.

The runtime of standard beam search is 𝒪(nmaxk|𝒱|log(k))\mathcal{O}(n_{\textit{max}}\,k\,|\mathcal{V}|\log(k)).

Proof.

The proof is the same as Theorem 4.6, but we can forgo the percolation step in the queue of queues because standard beam search proceeds in order of hypothesis length. This yields 𝒪(nmaxk|𝒱|log(k))\mathcal{O}(n_{\textit{max}}k|\mathcal{V}|\log(k)). ∎

While the theoretical bound of best-first beam search has an additional log factor compared to standard beam search, we find this to be negligible in practice. Rather, we find number of calls to score\mathrm{score}, the scoring function under our model (e.g., a neural network), is often the bottleneck operation when decoding neural networks (see § 6 for empirical evidence). In terms of this metric, the beam search algorithm makes 𝒪(knmax)\mathcal{O}(kn_{\textit{max}}) calls to score\mathrm{score}, as score\mathrm{score} is called once for each active hypothesis in BB and BB may evolve for nmaxn_{\textit{max}} rounds. The worst-case number of calls to score\mathrm{score} will be the same as for beam search, which follows from Lemma 4.3.

5 Scoring Functions

Even before the findings of Stahlberg and Byrne (2019), it was well known that the best-scoring hypothesis with respect to the traditional likelihood objective can be far from ideal in practice Wu et al. (2016); Murray and Chiang (2018); Yang et al. (2018). For language generation tasks specifically, the results returned by neural models using the standard scoring function are often short and default to high-frequency words Vinyals and Le (2015); Shen et al. (2016).

To alleviate such problems, methods that revise hypothesis scores to incorporate preferences for longer, less repetitive, or more diverse options have been introduced and are often used in practice. While most such techniques change the scoring function such that it is no longer monotonic, we can still guarantee the kk-optimality of the returned hypothesis for (upper) bounded scoring functions using the methods discussed in § 4.1. In the remainder of this section, we present alternate scoring schemes adapted to work with best-first beam search. Additionally, we present several heuristics which, while breaking the kk-optimality guarantee, provide another set of decoding strategies worth exploring.

Length Normalization.

Length normalization is a widely-used hypothesis scoring method that aims to counteract the propensity for shorter sequences to have higher scores under neural models; this is done by normalizing scores by hypothesis length (see Murray and Chiang (2018) for more detail).

For early stopping in beam search with length normalization, Huang et al. (2017) propose bounding the additive length reward as the minimum of a pre-determined optimal sequence length ratio rr and the final sequence length N𝐲N_{\mathbf{y}}:

scoreln(𝐱,𝐲)=score(𝐱,𝐲)+βmin{r|𝐱|,N𝐲}\begin{split}\mathrm{score}_{\textsc{ln}}(\mathbf{x},\mathbf{y})=\,&\mathrm{score}(\mathbf{x},\mathbf{y})\\ &\,\,+\,\beta\cdot\min\{r|\mathbf{x}|,N_{\mathbf{y}}\}\end{split} (7)

where β\beta is the scaling parameter for the reward. We note, however, that the same can be done with the maximum sequence length nmaxn_{max} such that the traditional length reward used by He et al. (2016) is recovered:

scoreln(𝐱,𝐲)\displaystyle\mathrm{score}_{\textsc{ln}}(\mathbf{x},\mathbf{y}) =score(𝐱,𝐲)+βmin{nmax,N𝐲}\displaystyle=\mathrm{score}(\mathbf{x},\mathbf{y})+\beta\min\{n_{max},N_{\mathbf{y}}\}
=score(𝐱,𝐲)+βN𝐲\displaystyle=\mathrm{score}(\mathbf{x},\mathbf{y})+\beta N_{\mathbf{y}} (8)

We formally propose two methods for length normalization. We use the scoring functions in 7 or 8 with either: (1) the following heuristic:

h(𝐱,𝐲)={0for 𝐲.last()=eosβmax{b|𝐲|,0}for 𝐲.last()eosh(\mathbf{x},\mathbf{y})=\begin{cases}0&\text{for }\mathbf{y}.\mathrm{last}()=\textsc{eos}\\ \beta\max\{b-|\mathbf{y}|,0\}&\text{for }\mathbf{y}.\mathrm{last}()\neq\textsc{eos}\end{cases} (9)

where bb can be r|𝐱|r|\mathbf{x}| or nmaxn_{\textit{max}};101010We enforce r|𝐱|<nmaxr|\mathbf{x}|<n_{\textit{max}}. or (2) stopping criterion as in 6 albeit with scoring function scoreln\mathrm{score}_{\textsc{ln}} and upper-bound function:

𝒰(𝐱,𝐲)=βmax{0,b|𝐲|}\mathcal{U}(\mathbf{x},\mathbf{y})=\beta\max\{0,b-|\mathbf{y}|\} (10)

Despite their similarities, these two methods are not guaranteed to return the same results. While the second method will return the same kk-optimal hypotheses as beam search, using a heuristic during pruned search means we can no longer guarantee the kk-optimality of the results with respect to the scoring function as the heuristic may push hypotheses off of the beam. We present experimental results for both methods in § 6.

IWSLT’14 De-En MTTT Fr-En CNN-DailyMail k=5k\!=\!5 k=10k\!=\!10 k=100k\!=\!100 k=500k\!=\!500 k=10k\!=\!10 k=100k\!=\!100 k=500k\!=\!500 k=5k\!=\!5 k=10k\!=\!10 k=100k\!=\!100 (35.6) (35.4) (34.7) (7.9) (33.0) (9.9) (1.2) (31.5) (30.9) (29.1) BF beam search 93 ​(24%) 169 ​(36%) 1275 ​(79%) 1168 ​(736%) 184 ​(16%) 867 ​(138%) 885 ​(836%) 200 ​(33%) 305 ​(43%) 2960 ​(92%) Beam search (ES) 107 ​(7%) 210 ​(9%) 2047 ​(12%) 7685 ​(27%) 196 ​(9%) 1310 ​(58%) 4182 ​(98%) 224 ​(19%) 357 ​(22%) 3942 ​(59%) Beam search 115 229 2286 9770 214 2066 8281 266 435 5673

Table 2: Average number of calls (rounded to nearest whole digit) to score\mathrm{score}, the sequence transduction model, per generated sequence when using different decoding algorithms. Green percentages are performance improvements over standard beam search. Beam search (ES) refers to the OpenNMT early-stopping method Klein et al. (2017). All methods provably return the same solution and thus, evaluation metrics (in dark blue) for a given beam size are identical.

author=timv,color=magenta!40,size=,fancyline,caption=,]Consider making a table that summarizes this section: original scoring rule name — citation — mathematical definition — monotonic approximation. author=clara,color=orange,size=,fancyline,caption=,]liking this…

Mutual Information.

Maximum mutual information decoding (Li et al., 2016) aims to alleviate the inherent preference of neural models for high-frequency tokens when using the log-probability decoding objective. Rather than choosing the hypothesis 𝐲\mathbf{y} to maximize conditional probability with respect to the input 𝐱\mathbf{x}, we instead choose 𝐲\mathbf{y} to maximize pointwise mutual information (PMI):

PMI(𝐱;𝐲)=logp(𝐱,𝐲)p(𝐱)p(𝐲)\mathrm{PMI}(\mathbf{x};\mathbf{y})=\log\frac{p(\mathbf{x},\mathbf{y})}{p(\mathbf{x})p(\mathbf{y})} (11)

Note that 11 is equivalent to logp(𝐲𝐱)p(𝐲)\log\frac{p(\mathbf{y}\mid\mathbf{x})}{p(\mathbf{y})}, which can be rewritten as logp(𝐲𝐱)logp(𝐲)\log p(\mathbf{y}\mid\mathbf{x})-\log p(\mathbf{y}) making the objective additive and thus 11 can conform to 4.

From this last form, we can see how mutual information decoding penalizes high-frequency and generic outputs; the negative p(𝐲)p(\mathbf{y}) term, as Li et al. (2016) point out, acts as an “anti-language model.” One unfortunate side effect of this objective is that ungrammatical and nonsensical outputs, which have probabilities close to 0 under a language model like p(𝐲)p(\mathbf{y}), end up with high scores due to the second term in the score function. To address this problem, and to upper-bound the scoring function, we propose lower-bounding the language model term by a hyperparameter 1ε>01\geq\varepsilon>0. We additionally use the strength hyperparameter λ\lambda employed by Li et al. (2016):

scorepmi(𝐱,𝐲)=\displaystyle\mathrm{score}_{\textsc{pmi}}(\mathbf{x},\mathbf{y})=\, logp(𝐲𝐱)\displaystyle\log p(\mathbf{y}\mid\mathbf{x})
λlogmax{p(𝐲),ε}\displaystyle\ \ \ -\lambda\log\max\{p(\mathbf{y}),\varepsilon\} (12)

Similarly to our methods for length normalization, we can use the scoring function in 12 either with the heuristic:

h(𝐱,𝐲)={0for 𝐲.last()=eosλlogε(nmax|𝐲|)for 𝐲.last()eosh(\mathbf{x},\mathbf{y})=\begin{cases}0&\!\text{for }\mathbf{y}.\mathrm{last}()=\textsc{eos}\\ -\lambda\log\varepsilon(n_{max}\!\!-\!|\mathbf{y}|)\!&\!\text{for }\mathbf{y}.\mathrm{last}()\neq\textsc{eos}\end{cases} (13)

or with stopping criterion as in 6 albeit with scorepmi\mathrm{score}_{\textsc{pmi}} and upper-bound function:

𝒰(𝐱,𝐲)=λlogε(nmax|𝐲|)\mathcal{U}(\mathbf{x},\mathbf{y})=-\lambda\log\varepsilon(n_{max}-|\mathbf{y}|) (14)

Since λlogε-\lambda\log\varepsilon is the best possible score at any given time step, clearly we can bound the increase in scorepmi\mathrm{score}_{\textsc{pmi}} by the above function. However, as with our length normalization strategy, we lose the kk-optimality guarantee with the heuristic method for mutual information decoding. We present experimental results for both methods in § 6.

6 Experiments

Refer to caption
Figure 1: Number of calls to scoring function score\mathrm{score} vs. total sequence generation time. Each point is a decoded sequence. Colors represent different model architectures and shapes signify the decoding algorithm used (beam sizes 3 and 10 are included for each). There is no notable difference in the overhead (time-wise) of best-first beam search and beam search.

We run our algorithm on several language-related tasks that typically use beam search for decoding: neural machine translation (NMT) and abstractive summarization (AS). Specifically, experiments are performed on IWSLT’14 De-En Cettolo et al. (2012), WMT’17 De-En Bojar et al. (2017), MTTT Fr-En Duh (2018), and CNN-DailyMail Hermann et al. (2015) using both Transformers Vaswani et al. (2017) and Convolutional sequence-to-sequence models Gehring et al. (2017).

For reproducibility, we use the data pre-processing scripts provided by fairseq Ott et al. (2019) and follow their methods for training sequence transduction models. Hyperparameter are set in accordance with previous works. Specifically, on IWSLT’14 and MTTT tasks, we follow the recommended Transformer settings for IWSLT’14 in fairseq,111111https://github.com/pytorch/fairseq/tree/master/examples/translation which are based on Vaswani et al. (2017) and Gehring et al. (2017). Hyperparameters for models trained on the WMT task are set following version 3 of the Tensor2Tensor toolkit Vaswani et al. (2018). We use byte-pair encoding (BPE; Sennrich et al. 2016) for all languages. Vocabulary sizes for WMT and IWSLT’14 are set from recommendations for the respective tasks in fairseq; for the MTTT tasks, vocabulary sizes are tuned on models trained with standard label-smoothing regularization. Similarly, the CNN/DailyMail dataset is pre-processed and uses BPE following the same steps as Lewis et al. (2019); model hyperparameters are likewise copied. Details are available on fairseq’s website.121212https://github.com/pytorch/fairseq/blob/master/examples/bart/README.cnn.md

We use bleu Papineni et al. (2002) (evaluated using SacreBLEU Post (2018)) for MT metrics and rouge-l Lin (2004) for abstractive summarization metrics. We build our decoding framework in SGNMT.131313https://github.com/ucam-smt/sgnmt

6.1 Running Time

In Tab. 2, we report values as the average number of calls to the scoring function per input; we do not use wall-clock time as this is heavily dependent on hardware. See Fig. 1 for empirical justification of the correlation between calls to the scoring function and runtime on the hardware our experiments were run on. For reference, in our experiments, the scoring function took on average >99%>99\% of the total computation time, even with larger beam sizes, when overhead of the search algorithm is most significant.

We find that best-first (BF) beam search leads to significant speed-ups over both traditional beam search and beam search with early stopping, with a performance increase141414Performance increase is defined as (oldnew)/new(\mathrm{old}-\mathrm{new})/\mathrm{new} of 8x\approx 8x for a beam size of 500. We likewise find that best-first beam search offers speed-ups over early stopping methods that are not guaranteed to return the same results as standard beam search (see Tab. 3).

IWSLT’14 De-En
kk method search error bleu # calls
10 shrinking 0% 35.4 229 ​(0%)
early 0% 35.4 225 ​(2%)
BF BS - 35.4 169 ​(36%)
100 shrinking 31.7% 13.2 2278 ​(0%)
early 31.7% 13.2 1738 ​(31%)
BF BS - 34.7 1275 ​(79%)
WMT’17 De-En
10 shrinking 0% 28.6 260 ​(0%)
early 0% 28.6 252 ​(3%)
BF BS - 28.6 230 ​(12%)
100 shrinking 1.7% 26.4 2587 ​(0%)
early 1.7% 26.4 2402 ​(8%)
BF BS - 26.9 2046 ​(26%)
Table 3: bleu, search error, and average number of calls to score\mathrm{score} for different stopping criterion. “shrinking” refers to the shrinking beam method of Bahdanau et al. (2015) and “early” refers to the stopping criterion of Huang et al. (2017). Note that neither method is guaranteed to return the same result as standard beam search. Search error and performance increases are with respect to standard beam search.

6.2 Length Normalization

We experiment with both forms of length normalization presented in § 5 and provide results in Tab. 4. We find that both methods, i.e., changing the stopping criterion and using a heuristic during search, provide improvements over baseline bleu scores albeit with different hyperparameter settings; increases are similar to improvements reported by Murray and Chiang (2018). Notably, using a heuristic causes a large percentage of search errors with respect to standard beam search using the same scoring function. However, the difference in results appears to be beneficial in terms of bleu.

IWSLT’14 De-En kk β\beta bb # calls search error bleu Heuristic 5 0.8 |𝐱||\mathbf{x}| 115 ​(0%) 40.6% 33.9 ​+0.3 10 1.2 |𝐱||\mathbf{x}| 229 ​(0%) 54.7% 33.8 ​+0.5 Stopping Criterion 5 0.5 nmaxn_{\textit{max}} 73 ​(58%) - 33.7 ​+0.1 10 0.5 nmaxn_{\textit{max}} 130 ​(76%) - 33.7 ​+0.4 MTTT Fr-En    Heuristic 5 0.8 .7|𝐱|.7|\mathbf{x}| 100 ​(8%) 16.2% 33.5 ​+0.2 10 1.0 .7|𝐱|.7|\mathbf{x}| 196 ​(9%) 25.2% 33.6 ​+0.6 Stopping Criterion 5 1.0 nmaxn_{\textit{max}} 65 ​(66%) - 34.1 ​+0.8 10 1.2 nmaxn_{\textit{max}} 88 ​(143%) - 34.1 ​+1.1

Table 4: bleu search error, and average number of calls to score\mathrm{score} for output obtained with length normalization scoring function on the IWSLT’14 De-En and MTTT Fr-En test sets. Increase in bleu is over baseline with no length normalization. Search error and performance increases are with respect to standard beam search decoding using the same scoring function.

6.3 Mutual Information

We train a language model on the IWSLT dataset and use it to calculate p(𝐲)p(\mathbf{y}) from 12 as marginalizing over 𝐲\mathbf{y} is intractable (see Li et al. (2016) for further justification). We run experiments using both of the methods discussed in § 5 and present results in Tab. 5. We find that both methods provide results of equivalent bleu score compared with the baseline output, i.e., results obtained with the unbounded PMI objective and beam search. Again, despite the high search error rate demonstrated by the heuristic method, evaluation metrics are still comparable.

6.4 Memory Usage

We conduct a set of experiments where we limit total queue capacity to kγk\cdot\gamma for γ{1,,nmax}\gamma\in\{1,\dots,n_{max}\}, as described in § 3.3, and report the bleu score of the resulting set of hypotheses.

As shown in Tab. 6, we find that restricting the queue capacity does not harm output quality and additionally, leads to even greater runtime performance increase. For example, runtime for decoding of IWSLT’14 with a beam size of 10 can be improved by >3x>\!3x while returning results with better evaluation metrics. We find that improvements are even more pronounced for larger beam sizes. Across beam widths and tasks, we find that search error (with respect to standard beam search) is quite low for γ=5\gamma=5. Additionally, for smaller γ\gamma, the change in bleu score demonstrates that search error in this context does not necessarily hurt the quality of results.

kk ε\varepsilon β\beta # calls search error bleu Baseline 5 - .05 115 - 33.2 10 - .05 229 - 33.0 Heuristic 5 .02 .05 129 ​(0%) 42.7% 33.2 10 .02 .05 256 ​(0%) 42.7% 33.0 Stopping Criterion 5 3e-43\mathrm{e}{\text{-}4} .05 114 ​(1%) 29.2% 33.2 10 5e-55\mathrm{e}{\text{-}5} .05 224 ​(2%) 26.6% 33.0

Table 5: bleu scores with mutual information scoring function on IWSLT’14 De-En. Baseline is PMI decoding with unbounded p(𝐲)p(\mathbf{y}), i.e., ε=0\varepsilon=0. Search error is with respect to beam search decoding of baseline with same β\beta.
IWSLT’14 De-En
kk γ\gamma search error bleu # calls
5 2 22.7% 35.7 ​+0.1 43.8 ​(163%)
5 4.4 % 35.8 ​+0.2 79.8 ​(44%)
nmaxn_{\textit{max}} - 35.6 93.0 ​(24%)
10 2 22.6% 35.7 ​+0.3 48.4 ​(374%)
5 4.5% 35.6 ​+0.2 126.9 ​(81%)
nmaxn_{\textit{max}} - 35.4 169.0 ​(36%)
WMT’17 De-En
5 2 29.0% 29.7 ​+0.2 77.5 ​(75%)
5 1.2% 29.5 ​+0.0 115.8 ​(12%)
nmaxn_{\textit{max}} - 29.5 118.8 ​(10%)
10 2 36.6% 29.5 ​+0.2 97.3 ​(165%)
5 2.6% 29.3 ​+0.0 230.0 ​(12%)
nmaxn_{\textit{max}} - 29.3 230.2 ​(12%)
Table 6: bleu scores and the number of calls to score\mathrm{score} on the IWSLT’14 De-En validation set and WMT’17 De-En test set with queue size restricted to nmaxkn_{\textit{max}}\cdot k. Note that γ=nmax\gamma\!=\!n_{\textit{max}} is the standard best-first beam search algorithm. Performance increases are over standard beam search. Search error is with respect to beam search with same beam width.

author=clara,color=orange,size=,fancyline,caption=,]I’d like to include a small section on search errors here. Just pointing to some research that they’re not necessarily bad for language generation tasks

7 Related Work

Our work is most similar to that of Zhou and Hansen (2005), who propose beam stack search. However, they are focused on exact inference and still evaluate hypotheses in breadth-first order. Additionally, their algorithm requires 𝒪(nmaxk)\mathcal{O}(n_{\textit{max}}k) memory; while best-first beam search has the same requirements, we introduce effective methods for reducing them, namely memory-reduced best-first beam search.

Huang et al. (2017) propose and prove the optimality of an early-stopping criterion for beam search. The authors find in practice though that reduction in computation from their algorithm was generally not significant. We build on this work and introduce additional methods for avoiding unnecessary computation. Our method leads to better performance, as shown in Tab. 2.

Klein and Manning (2003) use A\text{A}^{*} for PCFG parsing; however, they use the un-pruned version for exact search which is not applicable for NMT or AS as the memory requirements of the algorithm are far too large for these tasks. Subsequently, Pauls and Klein (2009) provide a method for pruning this search algorithm, albeit using a threshold rather than explicitly limiting the state space. Huang et al. (2012) also adapt A\text{A}^{*} for a kk-best decoding algorithm. While their methods differ notably from ours, they likewise employ pruning techniques that allow for substantial speedups.

Stahlberg and Byrne (2019) create an exact inference algorithm for decoding and use it to analyze the output of neural NMT models. While they likewise employ the monotonicity of the scoring function to make their method tractable, they do not focus on speed or mimicking the results of standard beam search.

8 Conclusion

We propose best-first beam search, an algorithm that allows for faster decoding while still guaranteeing kk-optimality. We provide results on several sequence-to-sequence transduction tasks that show the speed-ups our algorithm provides over standard beam search for decoding neural models. We adapt several popular alternate scoring functions to best-first beam search and provide a framework that can be used to adapt other scoring methods such as coverage normalization Wu et al. (2016) or diverse beam search Vijayakumar et al. (2016). We also provide a memory-reduced version of our algorithm, which returns competitive results in a fraction of the time needed for standard beam search.

References