The no_repeat_ngram_size parameter blocks repetition at the phrase level: if set to an integer greater than 0, all n-grams of that size can only occur once in the generated text. It is a different tool from the repetition penalty, which discourages repeating single tokens: the n-gram block stops the model looping the same phrase, which is the failure mode of greedy decoding on long outputs. Use it when the output repeats whole phrases, not when it repeats a single word.
What you need
- A generation call where you control the sampling parameters
- A prompt that makes the model loop a phrase
- A note of the n-gram size you will try, for example 3
Step 1: Set no_repeat_ngram_size to 3
Set the parameter to 3, so no three-token phrase can appear twice. A size of 2 is stricter and can break natural repetition, while a size of 5 may miss short loops.
Step 2: Generate on the looping prompt
Run the prompt that looped before and read the output. The phrase that repeated should appear once, and the model should move on to new content.
Step 3: Check the output still reads naturally
Read the full output for awkward phrasing where the model avoided a natural repetition. If the text reads forced, raise the size to 4 or drop the block and use a repetition penalty instead.
Worked example: synthetic n-gram block check
The table below is a synthetic example. It shows the expected result of blocking repeated n-grams; no text was generated.
| Setting | Phrase repeated? | Reads naturally? |
|---|---|---|
| No block | Yes – same phrase 4 times | No |
| Size 3 | No | Yes |
| Size 2 | No | Stiff – natural repeats blocked |
| Decision | Size 3 kept | Loop gone, text natural |
Expected result (UNTESTED). size 3 stops the loop and the text still reads naturally, while size 2 makes it stiff. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- The block applies to the generated text only, not to the prompt, so a phrase in the prompt can still be echoed once.
- A size of 2 blocks common two-word phrases, which can damage natural text more than it helps.
- The n-gram block cannot stop a model repeating the same idea in different words; it only blocks identical phrases.
- Greedy decoding breaks down on long sequences because it begins to repeat itself, which is the failure this parameter addresses.
