Learned Sequence Termination for Dynamic Output Lengths

40 concepts35 papers1 proposal

Sequence generation models face a fundamental tension between structure and flexibility. Traditional architectures often treat output length as an external constraint—either fixed in advance, bounded by rigid heuristics, or predicted by a separate module. However, real-world data is inherently variable: a document might contain three key ideas or thirty, a chemical reaction might yield two products or five, and a paragraph might contain a variable number of lines.

Forcing a model to conform to a pre-determined length introduces noise, truncation errors, or computational waste. The core insight of learned sequence termination is that sequence length should not be a structural hyperparameter; it should be a semantic property learned directly by the model. By embedding termination decisions into the vocabulary or latent space transitions, models can organically stop generating when the underlying information density of the task has been fully satisfied.

Determining Cardinality in Set and Autoregressive Generation

How do we know when a set or sequence is complete? In multi-label classification or keyphrase generation, traditional approaches use fixed top-$K$ cutoffs or manual probability thresholds. This introduces a dilemma: set the threshold too high, and you miss valid labels; set it too low, and you introduce irrelevant noise.

To solve this, Jinseok Nam et al. reimagined multi-label classification as a sequence-to-sequence problem in Maximizing Subset Accuracy with Recurrent Neural Networks in Multi-label Classification. By introducing a virtual end-of-sequence token into the label generation process, the model learns to stop predicting labels naturally once all true positive labels are exhausted. This eliminates the need for secondary classification layers or manual thresholding.

This intuition extends directly to generating sets of text. For instance, Xingdi Yuan et al. developed CatSeqD in One Size Does Not Fit All: Generating and Evaluating Variable Number of Keyphrases, which trains a model to emit a termination token only when it has achieved sufficient "semantic coverage" of the source text. Similarly, Hou Pong Chan et al. explicitly modeled keyphrase generation as a set of sequences in Exclusive Hierarchical Decoding for Deep Keyphrase Generation, allowing the network to learn a natural stopping condition based on input complexity rather than relying on post-processing filters.

This mechanism is highly effective across diverse domains:

  • Clinical Prediction: Mingyu Derek Ma et al. demonstrated in Memorize and Rank: Elevating Large Language Models for Clinical Diagnosis Prediction that a dedicated termination token allows large language models to dynamically adjust the number of predicted diagnoses based on internal confidence.
  • Spatial Modeling: Shuhan Tan et al. utilized a learned stopping token in SceneGen: Learning To Generate Realistic Traffic Scenes to terminate the sequential addition of actors onto a map, dynamically adapting scene density to the environmental context.

Overcoming the Fixed Canvas in Parallel Generation

While autoregressive models can generate sequentially until they hit a stop token, parallel generation frameworks—such as non-autoregressive translation (NAT) and diffusion models—face a steeper challenge. Because they predict all tokens simultaneously, they typically require an initial length prediction. If this initial guess is wrong, the model cannot easily recover, leading to duplication, omission, or rigid constraints.

To bypass this "fixed canvas" problem, researchers have integrated dynamic length control directly into parallel decoding. For non-autoregressive text generation, Yixuan Su demonstrated in Non-Autoregressive Text Generation with Pre-trained Language Models that a model can be trained to emit a dedicated end-of-sentence token. Crucially, once this token is predicted at any position during parallel decoding, all subsequent positions are constrained to the same terminal token, allowing the model to determine the effective length on-the-fly without an external length predictor.

For multi-segment generation, Qiu Ran et al. introduced RecoverSAT in Learning to Recover from Multi-Modality Errors for Non-Autoregressive Neural Machine Translation, which allows parallel segments to terminate early via specialized end-of-sentence tokens. This flexibility ensures that if one segment finishes early, the subsequent segment can begin at the correct semantic point, preventing information loss. Similarly, Yosuke Higuchi et al. introduced Dynamic Length Prediction (DLP) in Improved Mask-CTC for Non-Autoregressive End-to-End ASR, training the model to predict the number of tokens represented by a masked region to dynamically handle insertion and deletion errors.

Other parallel architectures leverage continuous spaces, iterative insertion, or flow matching:

Diffusion models have similarly evolved past fixed-size constraints. In code infilling, Zirui Wu et al. introduced DreamOn in DreamOn: Diffusion Language Models For Code Infilling Beyond Fixed-size Canvas, which incorporates special [expand] and [delete] tokens to let the diffusion model autonomously resize its output canvas.

For tabular time series, Fabrizio Garuti et al. trained models in Diffusion Transformers for Tabular Data Time Series Generation to predict specific end-of-sequence tokens within a padded structure, allowing the model to learn the underlying length distribution. In chemical retrosynthesis, Sean Current et al. used random padding during training in tt DiffER: categorical diffusion ensembles for single-step chemical retrosynthesis to teach a classifier to predict length differences and padding tokens, dynamically adjusting the final sequence length. Similarly, for image generation, Vittorio Pippi et al. trained a decoder to emit specific padding embeddings upon sequence completion in Zero-Shot Styled Text Image Generation, but Make It Autoregressive, enabling autonomous end-point determination for text images.

Adaptive Context and Streaming Processing

Dynamic termination is equally vital for managing input context and streaming data, where the optimal amount of information cannot be known beforehand.

In real-time applications, waiting for an entire input sequence destroys low-latency guarantees. To enable online processing, Navdeep Jaitly et al. proposed the Neural Transducer in An Online Sequence-to-Sequence Model Using Partial Conditioning. It processes data in chunks and introduces a dedicated terminal symbol to the output vocabulary; when emitted, the model halts generation for the current block and requests the next input segment, enabling dynamic, variable-length chunking.

When retrieving information or constructing prompts, fixed-size strategies often introduce noise. Jiangui Chen et al. demonstrated in GERE: Generative Evidence Retrieval for Fact Verification that generative retrieval models can use an end-of-sequence token to dynamically determine the exact number of evidence pieces to retrieve, avoiding the noise of fixed top-$K$ strategies.

For in-context learning, Xiang Gao et al. implemented a self-termination mechanism in Learning to Search Effective Example Sequences for In-Context Learning to find the ideal number of few-shot examples for a query, preventing performance degradation from excessive context. Similarly, Fuzhao Xue et al. introduced an adaptive halting mechanism in Adaptive Computation with Elastic Input Sequence to stop retrieving tokens from a memory bank once sufficient information is gathered.

This principle also applies to structural document processing. Clement Chatelain showed in End-to-end Handwritten Paragraph Text Recognition Using a Vertical Attention Network that training a recurrent decoder to predict a specific termination token allows the model to adapt to paragraphs with a variable number of lines without a fixed iteration count. In reading comprehension, Junjie Yang et al. introduced a virtual span token in Multi-span Style Extraction for Generative Reading Comprehension to signal the end of multi-span extraction, dynamically adjusting answer lengths based on content.

Robustness, Heuristics, and Vulnerabilities

Relying on learned termination tokens introduces a critical dependency: if the model fails to emit the stop token, it can enter catastrophic failure states.

For example, Phillip Keung et al. discovered that attentional speech recognition models frequently fail on out-of-domain audio, getting trapped in infinite loops. In Attentional Speech Recognition Models Misbehave on Out-of-domain Utterances, they implemented a structural truncation heuristic that terminates generation when the output length exceeds a predefined multiple of the predicted length, safeguarding the system against loop failures.

Conversely, this dependency can be exploited adversarially. Xiasi Wang et al. developed VLMInferSlow in VLMInferSlow: Evaluating the Efficiency Robustness of Large Vision-Language Models as a Service, an attack that applies a dynamic weight decay strategy to the end-of-sequence token. By penalizing termination as the sequence grows, the attack forces the model to generate excessively long, rambling descriptions, doubling latency and computational costs.

These vulnerabilities highlight why rigid heuristics or fixed step counts are insufficient. As argued by Xuekang Zhu et al. in Does the Manipulation Process Matter? RITA: Reasoning Composite Image Manipulations via Reversely-Ordered Incremental-Transition Autoregression, predefined thresholds limit flexibility; building robust sequential models requires refining how models learn a natural, context-aware stopping signal.

Key Foundational Work

Annotation Order Matters: Recurrent Image Annotator for Arbitrary Length Image Tagging (2016) — contributed 1 concept elements to this direction.
Maximizing Subset Accuracy with Recurrent Neural Networks in Multi-label Classification (2017) — contributed 1 concept elements to this direction.
An Online Sequence-to-Sequence Model Using Partial Conditioning (2016) — contributed 1 concept elements to this direction.
History & citationVersion 1 · 2026-10-04

Current · Permanent link

View frozen version 1

Cite this direction

Latest version (a living page):

@misc{lacuna_dynamic_sequence_termination_78767692,
  title = {dynamic sequence termination},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/dynamic-sequence-termination/rdr_7876769271d547c6babb02de0565812c},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-26},
}

This exact version (version 1, frozen text):

@misc{lacuna_dynamic_sequence_termination_78767692_v1,
  title = {dynamic sequence termination},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/dynamic-sequence-termination/rdr_7876769271d547c6babb02de0565812c/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-26},
}
About

These directions overlap with this one through shared papers.

See something wrong? Report a mistake