English (unofficial) translations of posts at kexue.fm
Source

How to Deal with the ``Never-Ending'' Problem in Seq2Seq?

Translated by DeepSeek V4 Pro. Translations can be inaccurate, please refer to the original post for important stuff.

In the decoding process of Seq2Seq, we generate tokens recursively one by one until the <eos> tag appears; this is the so-called “autoregressive” generation model. However, readers who have studied Seq2Seq may have noticed that this autoregressive decoding occasionally exhibits a “never-ending” phenomenon. This primarily manifests as a certain fragment appearing repeatedly—for example, “The weather today is nice is nice is nice is nice is nice...” or “Do you think what I said is right is right is right is right...”—but the <eos> tag simply never appears. The ICML 2020 paper “Consistency of a Recurrent Language Model With Respect to Incomplete Decoding” systematically discusses this phenomenon and proposes some countermeasures. This article provides a brief introduction to the main content of that paper.

Decoding Algorithms

For autoregressive models, we build a conditional language model as follows: p(y_t|y_{< t}, x)\label{eq:p} The decoding algorithm then outputs the corresponding y=(y_1,y_2,\dots,y_T) given x when the above model is known. Decoding algorithms can be roughly divided into two categories: deterministic decoding algorithms and stochastic decoding algorithms. The original paper discusses the “never-ending” problem for both categories, so we need to understand them first.

Deterministic Decoding

Deterministic decoding algorithms ensure that once the input text is fixed, the decoded output text is also fixed. This category includes Greedy Search and Beam Search. In fact, Greedy Search is a special case of Beam Search, so we only need to discuss the latter.

In Beam Search, we fix a “beam size” k and decode tokens one by one from left to right, keeping only the k sequences with the highest total scores at each step. For example, if k=2 and the token space is V=\{a,b,c,d\}, the decoding process might look like this:

Step 1: Calculate p(y_1|y_0,x) (y_0 is the fixed start tag <bos>), then keep the two largest, e.g., \{a,b\}, and record their scores (log-probabilities).

Step 2: Calculate p(y_2|y_0,a,x) and p(y_2|y_0,b,x). At this point, there are k \times |V| = 8 candidate sequences. Keep the two with the highest total scores (current token score plus the score of a or b itself), e.g., \{(a,c),(b,d)\}, and record their total scores.

Step 3: Calculate p(y_3|y_0,a,c,x) and p(y_3|y_0,b,d,x). There are again k \times |V| = 8 candidate sequences. Keep the two with the highest total scores, e.g., \{(a,c,d),(a,c,c)\}, and record their total scores.

...

By parity of reasoning, each sequence stops when <eos> appears. Finally, the best one is selected from the k completed sequences. There are generally two choices: outputting the one with the highest total score, or the one with the highest average score (divided by the number of tokens). Sometimes, length penalties are added based on actual needs.

Stochastic Decoding

Stochastic decoding algorithms, as the name suggests, mean that even if the input text is fixed, the decoded output is not. For example, random sampling from a trained language model is such an algorithm (refer to “Playing with Chinese GPT2 using Keras”). For Seq2Seq, we often want deterministic results, so Beam Search is used in most scenarios. However, Beam Search results may be too generic (e.g., “Okay,” “I don’t know,” “Thank you”), or sometimes we want to increase output diversity (e.g., the SimBERT model we open-sourced for similarity sentence generation). In these cases, stochastic decoding is needed, which includes three types: Pure Random Sampling, Top-k Sampling, and Nucleus Sampling.

Pure Random Sampling is simple: at each step, a token is randomly sampled according to its probability. For example, step one calculates p(y_1|y_0,x) and samples a token, say c; step two calculates p(y_2|y_0,c,x) and samples a; step three calculates p(y_3|y_0,c,a,x), and so on, until <eos> is sampled.

Top-k Sampling comes from the paper “Hierarchical Neural Story Generation”. It adds a truncation to pure random sampling: at each step, only the k tokens with the highest probabilities are kept, and then sampling is performed after re-normalization. This aims to strike a balance between “high score” and “diversity.” Obviously, when k=1, it is equivalent to Greedy Search.

Nucleus Sampling comes from the paper “The Curious Case of Neural Text Degeneration”. Similar to top-k, it truncates the sampling space: fix p \in (0, 1), and keep only the top tokens whose cumulative probability just exceeds p. This is also called top-p sampling.

Besides top-k and top-p, there are adaptive truncation methods. For example, the paper “Sparse Sequence-to-Sequence Models” replaces the softmax function with a sparse version, which automatically sets the probability of most impossible tokens to 0 without needing to manually choose k or p.

Knowing When to Stop

From the design of Seq2Seq models and the decoding algorithms described above, there is no theoretical guarantee that the decoding process will definitely stop. That is, nothing ensures the <eos> tag will appear; this relies entirely on the model learning it. When the model doesn’t learn well enough, the “never-ending” phenomenon occurs. The original paper analyzes different decoding algorithms and proposes strategies to make the decoding process “know when to stop.”

Bounded Hidden Vectors

The classic way to model the probability in \eqref{eq:p} is: p(y_t|y_{< t}, x)=\text{softmax}(Wh_t+b),\quad h_t=f(y_{< t}, x) That is, a hidden vector h_t is calculated, followed by a fully connected layer and softmax activation. In this form, the original paper states:

If \|h_t\| is bounded for any t, then pure random sampling can “know when to stop.”

This sounds like a very strong and practical conclusion, doesn’t it? Making \|h_t\| bounded is easy—for example, by adding Layer Norm. Does this mean adding Layer Norm solves everything? Not quite. The conclusion is theoretically correct, and the reasoning is: because \|h_t\| is bounded, for any t and any token, p(y_t|y_{< t}, x) has a positive lower bound (since Wh_t won’t be infinitely large, e^{Wh_t} won’t be infinitely large, and after normalization, it won’t be infinitely close to 0). This means there exists an \epsilon > 0 such that p(\text{\texttt{<eos>}}|y_{< t}, x) \geq \epsilon. Since the probability is a positive constant, as long as you sample enough steps, you will eventually sample <eos>, so it won’t run forever.

Isn’t this reasoning a bit laughable? Yes, it can stop, but it might require sampling a huge number of steps. It’s like saying “as long as you buy enough lottery tickets, you will eventually win the jackpot”—it lacks practical value. By the time it stops after many steps, the looping or repeating tokens might have already appeared many times. Even if it stops, the output might be meaningless, perhaps even worse than simple length truncation.

Actively Adding <eos>

Note that the above conclusion only holds for pure random sampling. For top-k and Nucleus sampling, it doesn’t necessarily hold because <eos> might not appear in the truncated sampling space. Of course, we can manually add <eos> back into the sampling space, leading to the following conclusion:

If \|h_t\| is bounded for any t, and we add <eos> to the sampling space, then top-k and Nucleus sampling can “know when to stop.”

However, this feels a bit like a truism...

Self-Truncating Design

Note that the two conclusions above only apply to stochastic decoding. For deterministic decoding, since there is no randomness, we cannot guarantee hitting <eos>. To this end, the original paper proposes a self-truncating design: find a way to make p(\text{\texttt{<eos>}}|y_{< t}, x) have a positive lower bound that increases with t and eventually approaches 1.

This self-truncating design is not complicated. It defines p(\text{\texttt{<eos>}}|y_{< t}, x) = 1 - \alpha(h_t), where: \begin{aligned} \alpha(h_0)=&\,\sigma\left(w_{\text{\texttt{<eos>}}}^{\top} h_0 + b_{\text{\texttt{<eos>}}}\right) \\ \alpha(h_t)=&\,\sigma\left(w_{\text{\texttt{<eos>}}}^{\top} h_t + b_{\text{\texttt{<eos>}}}\right)\left[1 - p(\text{\texttt{<eos>}}|y_{< {t-1}}, x)\right] \end{aligned} Here \sigma(\cdot) maps \mathbb{R} to [0, 1-\epsilon]; for example, one could use \sigma(\cdot)=(1-\epsilon)\text{sigmoid}(\cdot). After designing p(\text{\texttt{<eos>}}|y_{< t}, x), the probabilities of the remaining tokens are calculated using the original softmax and then multiplied by \alpha(h_t).

Now we have: \begin{aligned} p(\text{\texttt{<eos>}}|y_{< t}, x)=&\,1 - \sigma\left(w_{\text{\texttt{<eos>}}}^{\top} h_t + b_{\text{\texttt{<eos>}}}\right)\left[1 - p(\text{\texttt{<eos>}}|y_{< {t-1}}, x)\right]\\ =&\,1 - \prod_{i=0}^t\sigma\left(w_{\text{\texttt{<eos>}}}^{\top} h_i + b_{\text{\texttt{<eos>}}}\right)\\ \geq&\, 1 - (1 - \epsilon)^{t+1} \end{aligned} Clearly, as long as t > -\ln 2/\ln (1-\epsilon), p(\text{\texttt{<eos>}}|y_{< t}, x) > 0.5. This means Greedy Search must stop within -\ln 2/\ln (1-\epsilon) steps. As p(\text{\texttt{<eos>}}|y_{< t}, x) approaches 1, Beam Search will also stop within finite steps.

Personal Evaluation

The main content of the original paper is roughly as described. Overall, it does provide some new insights into decoding algorithms and offers effective strategies to mitigate the “never-ending” problem. However, for an ICML paper, I feel the perspective is not very high and the overall content is somewhat shallow.

A large portion of the paper is spent using mathematical language to restate existing concepts—such as what a decoding algorithm is, what top-k sampling is, what Beam Search is, and what the “never-ending” problem is. While giving these mathematical definitions isn’t meaningless, it doesn’t add much value to the core problem the paper explores. Once these parts are removed, not much remains. Furthermore, the conclusions are weak. I’ve already commented on the strategies for stochastic decoding: the conclusions are correct but have little practical value. As for the self-truncating design for deterministic decoding, it feels quite forced and lacks elegance, appearing more like a crude truncation.

The most critical issue is that regarding the “never-ending” problem, the paper focuses entirely on answering “what it is” and “how to handle it,” without exploring “why it happens.” It fails to provide useful information for understanding the essence of the “never-ending” phenomenon, and thus fails to derive strategies that are closer to the root cause. This is something I find quite difficult to accept.

Summary

This article introduced Seq2Seq decoding algorithms, discussed the “never-ending” phenomenon that can occur during decoding, and presented the strategies provided in an ICML 2020 paper.

When reposting, please include the original address: https://kexue.fm/archives/7500

For more details on reposting, please refer to: “Scientific Space FAQ”