Fast Lexically Constrained Decoding with Dynamic Beam Allocation for Neural Machine Translation

Matt Post, David Vilar

Abstract: The end-to-end nature of neural machine translation (NMT) removes many ways of manually guiding the translation process that were available in older paradigms. Recent work, however, has introduced a new capability: lexically constrained or guided decoding, a modification to beam search that forces the inclusion of pre-specified words and phrases in the output. However, while theoretically sound, existing approaches have computational complexities that are either linear (Hokamp and Liu, 2017) or exponential (Anderson et al., 2017) in the number of constraints. We present an algorithm for lexically constrained decoding with a complexity of O(1) in the number of constraints. We demonstrate the algorithm's remarkable ability to properly place these constraints, and use it to explore the shaky relationship between model and BLEU scores. Our implementation is available as part of Sockeye.

Introduction

Neural Machine Translation (NMT) models are often treated as "black boxes," making it difficult for users to intervene when specific terminology is required. In professional translation workflows, it is often necessary to force the system to use specific words—such as brand names, technical terms, or pre-approved glossary entries—regardless of the model's internal probability distribution. This process is known as lexically constrained decoding.

Examples of lexically constrained decoding where specific German words like "errichten" or "Keiner" are forced into the translation of a famous quote.

Previous methods, such as Grid Beam Search (GBS), managed these constraints by expanding the search space (the beam) for every constraint added. This resulted in a runtime complexity that scaled linearly with the number of constraints ($O(N)$), making real-time translation slow and difficult to optimize for parallel GPU processing.

Dynamic Beam Allocation (DBA)

The core innovation of this work is Dynamic Beam Allocation (DBA). Instead of increasing the beam size to accommodate constraints, DBA maintains a fixed-size beam ($k$) and dynamically divides it into "banks" based on the number of constraints satisfied by each hypothesis.

The DBA process at time step $t$, showing how hypotheses are grouped into banks based on satisfied constraints and how beam slots are redistributed to ensure progress.

The algorithm operates through three primary mechanisms:

  1. Constraint Banking: Hypotheses are grouped into banks (e.g., Bank 0 for no constraints met, Bank 2 for two constraints met).
  2. Dynamic Allocation: At each time step, the fixed beam slots are divided among these banks. If a bank is empty or has fewer viable candidates than its allocation, those slots are redistributed to other banks, ensuring the beam is always fully utilized.
  3. Candidate Generation: To guarantee that constraints are found, the algorithm explicitly adds the "best" token for every unmet constraint to the candidate list at each step, alongside the standard top-$k$ predictions.

By fixing the beam size, DBA achieves a complexity of $O(1)$ relative to the number of constraints, allowing for efficient batching and high throughput on GPUs.

Key Insights and Results

Efficiency and Placement

On a Tesla V100 GPU, DBA maintains a consistent speed of approximately 0.6 seconds per sentence, regardless of whether one or ten constraints are applied. This is significantly faster than GBS, which slows down linearly as constraints increase. Furthermore, the algorithm demonstrates high "syntactic intelligence"; even without explicit alignment data, the correlation between the position of a constraint in a human reference and its position in the DBA output is remarkably high (Pearson’s $r = 0.82$).

The "Reference Aversion" Phenomenon

A significant finding of this research is that NMT models often assign lower internal probability to human reference translations than to their own generated outputs. Forcing a reference word into the output often "hurts" the model's internal score even as it improves the BLEU score. This highlights a disconnect between model optimization and human-perceived quality.

Pruning the "Garbage"

When forced off its preferred path by a constraint, the model often generates low-probability sequences while "waiting" for the next constraint. The authors implemented a large pruning threshold to discard these "garbage" hypotheses, which was found to be more effective for speed and quality than standard early-stopping techniques.

Conclusion

Dynamic Beam Allocation makes lexically constrained decoding feasible for production environments by providing constant-time overhead. The algorithm is now a standard feature in the Sockeye NMT framework, enabling users to guide neural models with high-quality terminology without sacrificing translation throughput.

Summary updated

BibTeX
@inproceedings{post2018fast,
  author = {Matt Post and David Vilar},
  title = {{Fast Lexically Constrained Decoding with Dynamic Beam Allocation for Neural Machine Translation}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2018},
  url = {https://openreview.net/forum?id=ByNhv7ZO-S},
}
Figure