# US 2025/0165711 A1 · Constraining output of a generative language model to conform to a grammar

> Agents: this data is available as JSON from the patentgrep API and MCP server — see https://patentgrep.com/llms.txt

- Number: US 2025/0165711 A1
- Document ID: US20250165711A1
- Status: Published application
- Kind: A1
- Application number: 18/649,251
- Filed: 2024-04-29
- Published: 2025-05-22
- Earliest claimed: 2023-11-17
- Assignees: Shopify Inc.
- Applicants: Shopify Inc.
- Inventors: David Libbey; Neil Leonard Padgett
- Web page: https://patentgrep.com/patent/US20250165711A1

## CPC

- G06F 40/284: Lexical analysis, e.g. tokenisation or collocates
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

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.

## Claims

### Claim 1

A computer-implemented method comprising:
obtaining a plurality of values generated using a generative language model, each of the values indicative of a probability of a respective token being a next token in a token sequence generated by the generative language model;
obtaining a mask based on at least one token of the token sequence already generated by the generative language model and based on a grammar defining valid sequences of output; and
applying the mask to the plurality of values, the mask operating 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;
wherein the next token is determined based on the plurality of values after the mask is applied.

### Claim 2 (depends on 1)

The computer-implemented method of claim 1, wherein obtaining the mask comprises:
determining a set of valid next tokens based on the at least one token of the token sequence already generated by the generative language model and based on the grammar, wherein the set of valid next tokens consists of one or more tokens any one of which, when appended to the token sequence, results in the token sequence still being compliant with the grammar; and
generating the mask by, for each token not in the set of valid next tokens, generating a corresponding masking value that, when applied, reduces or zeros the probability of the token being the next token.

### Claim 3 (depends on 2)

The computer-implemented method of claim 2,
wherein obtaining the plurality of values comprises generating a first tensor in a neural network of the generative language model, the first tensor including the plurality of values;
wherein the mask is a second tensor; and
wherein applying the mask comprises performing a tensor product of the first tensor and the second tensor.

### Claim 4 (depends on 3)

The computer-implemented method of claim 3, wherein tokens not in the set of valid next tokens are invalid next tokens, and wherein:
at each position in the second tensor that corresponds to a valid next token there is an identity element that does not modify the value in the first tensor corresponding to the valid next token when the tensor product is performed; and
at each position in the second tensor that corresponds to an invalid next token there is the corresponding masking value that does modify the value in the first tensor corresponding to the invalid next token when the tensor product is performed to reduce or zero the probability of the invalid next token being the next token.

### Claim 5 (depends on 1)

The computer-implemented method of claim 1, wherein the plurality of values is a plurality of normalized probability values output from a softmax function of the generative language model, and wherein applying the mask comprises reducing or setting to zero probability each of the normalized probability values that corresponds to a token not compliant with the grammar.

### Claim 6 (depends on 2)

The computer-implemented method of claim 2,
wherein generating the plurality of values using the generative language model and the applying the mask is implemented on a first processing unit;
wherein the determining the set of valid next tokens and the generating the mask is implemented on a second processing unit; and
wherein the method further comprises transmitting the mask from the second processing unit to the first processing unit.

### Claim 7 (depends on 6)

The computer-implemented method of claim 6, wherein the first processing unit is a graphics processing unit (GPU) and the second processing unit is a general purpose processor.

### Claim 8 (depends on 2)

The computer-implemented method of claim 2, comprising determining, for each token of a plurality of tokens, whether that token is in the set of valid next tokens.

### Claim 9 (depends on 8)

The computer-implemented method of claim 8, wherein the plurality of tokens is a set of tokens containing fewer than all possible tokens that can be generated by the generative language model, and wherein the set of tokens is determined by retrieving all tokens having a prefix equal to a start of a next possible valid token.

### Claim 10 (depends on 9)

The computer-implemented method of claim 9, wherein all possible tokens that can be generated by the generative language model are stored in the form of a tree, and wherein the set of tokens corresponds to at least one branch of the tree and fewer than all branches of the tree.

### Claim 11 (depends on 1)

The computer-implemented method of claim 1, wherein the grammar defines terminal symbols and one or more rules that define how the terminal symbols can be arranged in relation to each other to define the valid sequences of output.

### Claim 12 (depends on 11)

The computer-implemented method of claim 11, wherein for each terminal symbol of one or more of the terminal symbols, the terminal symbol is divided into two or more constituent symbols, each constituent symbol corresponding to a respective possible output token that can be generated by the generative language model; and wherein the valid sequences of output defined by the grammar include valid sequences of output tokens based on the constituent symbols.

### Claim 13 (depends on 12)

The computer-implemented method of claim 12, wherein the grammar before the terminal symbol is divided into the two or more constituent symbols is an original grammar, wherein the grammar after the terminal symbol is divided into the two or more constituent symbols is a tokenized grammar, and wherein generating the tokenized grammar comprises:
obtaining the original grammar and obtaining an indication of the tokens that can be output by the generative language model; and
modifying the original grammar to result in the tokenized grammar by, for each terminal symbol of one or more of the terminal symbols of the original grammar, dividing the terminal symbol into the two or more constituent symbols, each constituent symbol corresponding to a respective possible output token that can be generated by the generative language model.

### Claim 14

A system comprising:
a memory to store a grammar defining valid sequences of output; and
at least one processing unit to:
obtain a plurality of values generated using a generative language model, each of the values indicative of a probability of a respective token being a next token in a token sequence generated by the generative language model;
obtain a mask based on at least one token of the token sequence already generated by the generative language model and based on the grammar defining the valid sequences of output; and
apply the mask to the plurality of values, the mask operating 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;
wherein the next token is determined based on the plurality of values after the mask is applied.

### Claim 15 (depends on 14)

The system of claim 14, wherein the at least one processing unit is further to:
determine a set of valid next tokens based on the at least one token of the token sequence already generated by the generative language model and based on the grammar, wherein the set of valid next tokens consists of one or more tokens any one of which, when appended to the token sequence, results in the token sequence still being compliant with the grammar; and
generate the mask by, for each token not in the set of valid next tokens, generating a corresponding masking value that, when applied, reduces or zeros the probability of the token being the next token.

### Claim 16 (depends on 15)

The system of claim 15,
wherein the at least one processing unit is to obtain the plurality of values by generating a first tensor in a neural network of the generative language model, the first tensor including the plurality of values;
wherein the mask is a second tensor; and
wherein the at least one processing unit is to apply the mask by performing a tensor product of the first tensor and the second tensor.

### Claim 17 (depends on 16)

The system of claim 16, wherein tokens not in the set of valid next tokens are invalid next tokens, and wherein:
at each position in the second tensor that corresponds to a valid next token there is an identity element that does not modify the value in the first tensor corresponding to the valid next token when the tensor product is performed; and
at each position in the second tensor that corresponds to an invalid next token there is the corresponding masking value that does modify the value in the first tensor corresponding to the invalid next token when the tensor product is performed to reduce or zero the probability of the invalid next token being the next token.

### Claim 18 (depends on 14)

The system of claim 14, wherein the plurality of values is a plurality of normalized probability values output from a softmax function of the generative language model, and wherein the at least one processor is to apply the mask by reducing or setting to zero probability each of the normalized probability values that corresponds to a token not compliant with the grammar.

### Claim 19 (depends on 15)

The system of claim 15,
wherein the at least one processing unit comprises a first processing unit and a second processing unit;
wherein the first processing unit is to generate the plurality of values using the generative language model and apply the mask;
wherein the second processing unit is to determine the set of valid next tokens, generate the mask, and transmit the mask to the first processing unit.

### Claim 20 (depends on 19)

The system of claim 19, wherein the first processing unit is a graphics processing unit (GPU) and the second processing unit is a general purpose processor.

### Claim 21 (depends on 15)

The system of claim 15, wherein the at least one processor is to determine, for each token of a plurality of tokens, whether that token is in the set of valid next tokens, wherein the plurality of tokens is a set of tokens containing fewer than all possible tokens that can be generated by the generative language model, and wherein the at least one processor is to determine the set of tokens by retrieving all tokens having a prefix equal to a start of a next possible valid token.

### Claim 22 (depends on 21)

The system of claim 21, wherein all possible tokens that can be generated by the generative language model are stored in the form of a tree, and wherein the set of tokens corresponds to at least one branch of the tree and fewer than all branches of the tree.

### Claim 23 (depends on 14)

The system of claim 14, wherein the grammar defines terminal symbols and one or more rules that define how the terminal symbols can be arranged in relation to each other to define the valid sequences of output.

### Claim 24 (depends on 14)

The system of claim 14, wherein for each terminal symbol of one or more of the terminal symbols, the terminal symbol is divided into two or more constituent symbols, each constituent symbol corresponding to a respective possible output token that can be generated by the generative language model; and wherein the valid sequences of output defined by the grammar include valid sequences of output tokens based on the constituent symbols.

### Claim 25

One or more non-transitory computer-readable storage media having stored thereon computer-executable instructions that, when executed by at least one processing unit, cause the at least one processing unit to perform operations comprising:
obtaining a plurality of values generated using a generative language model, each of the values indicative of a probability of a respective token being a next token in a token sequence generated by the generative language model;
obtaining a mask based on at least one token of the token sequence already generated by the generative language model and based on a grammar defining valid sequences of output; and
applying the mask to the plurality of values, the mask operating 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;
wherein the next token is determined based on the plurality of values after the mask is applied.
