Document-Level and Context-Aware Translation › Context-Aware Decoding
Language AI HandbookAll chapters You can intervene at decoding time without retraining when the decoder exposes the controls you need. Grid beam search requires control of candidate token expansion and scoring. A hosted service can enforce the constraints if it provides such a feature. When an endpoint accepts text and returns text without exposing search controls, use an external placeholder pipeline if the engine can preserve its markers.
Grid beam search in more detail
The requirement that the output contain is a regular language. A finite-state recognizer can track whether a model prefix has met the requirement. A hard-constrained decoder can use that state to prevent accepting an end-of-sequence token before the term appears.
For a single required phrase of model tokens, use states through . Before a full match, the state is the length of the longest suffix of the output so far that matches a prefix of the phrase. State means no prefix matches. The intermediate states correspond to nonempty proper prefixes. If a token breaks a partial match, keep any shorter prefix that still matches a suffix. State means the phrase has appeared, and all later tokens leave the recognizer in that satisfied state. A one-token requirement therefore has two states.
For multiple phrases, a product recognizer can track a tuple of these states and accept when every phrase has appeared. One token can advance more than one recognizer. For example, the output tokens A B C satisfy both substring requirements A B and B C because they share the token B. The original grid beam search credits each emitted token to at most one constraint position, so separately covering those two phrases takes four emitted constraint tokens. Even at the same coverage level, hypotheses can have covered different phrases. Each hypothesis therefore also tracks which constraints it has covered and whether it is in the middle of emitting one.
You can also allow soft constraints by penalizing missing terms in the score. This lets a decoder prefer the glossary form while still accepting a different form if it scores well enough. The penalty can be term-specific. A legal term might be mandatory, while a stylistic preference carries a small penalty.
Source-side terminology tagging
Another approach is to train the model to use inline terminology annotations. Dinu et al. (2019) augmented the source with the desired translation and used an auxiliary stream to mark token roles. They selected training examples whose target reference already contained the desired term. At inference, the annotations guide generation without adding a separate grid search.
This setup requires glossary-annotated training data. Evaluate term use and fluency separately, including terms held out from the annotations. Compare runtime against your configured constrained decoder under the same hardware and decoding settings.
Placeholder protection as a decoding substitute
The placeholder pipeline enforces its replacement rule after generation, wherever a recognizable marker survives. Constrained decoding instead requires the specified token sequence in an accepted output. If either approach uses a fixed form, check whether that form is grammatical in context.
Use placeholders when you only control the text sent to the engine and the marker survival rate is acceptable. If you control decoding, test in-search constraints for terms that must appear in the output. You can combine the two for different terms, then check which requirements each stage satisfied.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!