Grammar-Constrained Token Generation in Generative Language Models

Resolve Bottlenecks,
Find Innovative Solutions
Generate Solutions

Solution Overview

Problem

Generative language models, such as large language models (LLMs), often produce syntactically-invalid or misinformed output, known as 'hallucination', due to incomplete training or fine-tuning, leading to performance issues and the need for computationally intensive methods to mitigate these errors.

Innovation Solution

The use of a grammar that defines valid sequences of output to constrain the token generation in the generative language model, where the probability of tokens not compliant with the grammar is reduced or zeroed, ensuring that only grammar-compliant tokens are output.

Engineering Contradictions & Design Principles

VSEngineering Contradiction Analysis

1Reliability

If additional fine-tuning training is used to minimize wrong correlations, then the model learns correct patterns better, but the computational cost and training complexity increase significantly

Engineering Contradiction:
Improveoutput correctnessVSAvoidcomputational cost
Core Design Contradiction:
ReliabilityVSUse of energy by stationary object

Solution Approach 1:

The grammar constraints are incorporated into the model architecture during the initial training phase, rather than requiring separate fine-tuning steps. The constrained decoder is pre-configured with grammar rules that guide token selection, allowing the model to learn correct patterns while inherently satisfying grammatical requirements from the start of training.

Inventive Principle:
Principle #10Preliminary action

Solution Approach 2:

A grammar-based constraint mechanism acts as an intermediary between the language model's predictive capabilities and the final output generation. This intermediary layer enforces grammatical rules during decoding without requiring the model to relearn patterns through additional fine-tuning, thereby reducing computational overhead while maintaining output correctness.

Inventive Principle:
Principle #24Intermediary (Mediator)

2Reliability

If post-process filtering is applied to flag or remove invalid output, then syntactically-invalid output is reduced, but the processing time and computational resources increase

Engineering Contradiction:
Improveoutput validityVSAvoidprocessing time
Core Design Contradiction:
ReliabilityVSLoss of time

Solution Approach 1:

Grammatical validity is ensured during the token generation process itself rather than through subsequent filtering. The constrained decoder applies grammar rules in real-time as each token is selected, preventing invalid sequences from being generated in the first place and eliminating the need for post-processing validation steps.

Inventive Principle:
Principle #10Preliminary action

Solution Approach 2:

The validation and filtering operations are extracted from the post-processing stage and integrated into the core decoding mechanism. By embedding grammar checking within the token selection process, the system eliminates separate validation steps that would otherwise consume additional processing time and resources.

Inventive Principle:
Principle #2Taking out (Extraction)

3Reliability

If a naïve approach of checking and regenerating output is used, then grammar compliance is achieved, but the generation speed and productivity decrease due to multiple iterations

Engineering Contradiction:
Improvegrammar complianceVSAvoidgeneration speed
Core Design Contradiction:
ReliabilityVSProductivity

Solution Approach 1:

The grammar constraints are built into the decoding process before generation begins. The constrained decoder is pre-configured with grammar rules that actively guide token selection throughout generation, ensuring compliance is maintained from the first token rather than requiring multiple regeneration attempts to achieve validity.

Inventive Principle:
Principle #10Preliminary action

Solution Approach 2:

The constrained decoder implements real-time feedback during token selection by evaluating which tokens satisfy grammar constraints at each step. This continuous feedback mechanism guides the generation process to produce valid sequences in a single pass, eliminating the need for iterative checking and regeneration that would slow down productivity.

Inventive Principle:
Principle #23Feedback

Data Source

PatentUS20250165711A1Constraining output of a generative language model to conform to a grammar
Publication Date: 2025.05.22 SHOPIFY INC
  • US20250165711A1 patent drawing
  • US20250165711A1 patent drawing
  • US20250165711A1 patent drawing

AI summary

One problem of a generative language model (e.g. a large language model) is the generation of syntactically-invalid or misinformed output. This may be mitigated by utilizing a grammar defining valid sequences of output. The grammar may constrain the token generation. A method may include obtaining values generated using the generative language model, where each value is indicative of a probability of a respective token being a next token in the token sequence. The method may further include obtaining a mask based on the token sequence already generated and the grammar. The method may further include applying the mask to the values. The mask may operate on each value that corresponds to a token not compliant with the grammar to reduce or zero the probability of the corresponding token being the next token. The next token is then determined based on the values after the mask is applied.