Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Stopping Criteria

In the previous chapter, we learned to control the quality and diversity of sampling at inference. Now consider another question: when should the model stop generating?

By default, the model keeps generating until the maximum length. But often we want the model to stop at the right moment:

  • stop after one sentence;

  • stop after one paragraph;

  • stop when a specific token or sequence appears.

Stopping criteria

Stopping criteria are the rules that control when generation terminates. Common stopping criteria include:

  • Maximum length: stop after generating a fixed number of tokens. This is also the most basic default stopping criterion;

  • Stop tokens: stop after a specific token (for example a period, a newline, or EOF) appears;

  • Stop sequences: stop after a specific sequence (for example a blank line) appears.

How it works

After each token is generated, check whether any stopping criterion is met. If one is met, terminate early and do not generate any further.

GPT stopping criteria trainer

Hyperparameters

Model parameters

The model parameters must stay consistent with the pre-training stage.

Modeling

Loading the model

Maximum length

ROMEO:
Cut on Duke of York. Whence news
but thinkful should he divines

Stop tokens

ROMEO:
His many choses!

Stop sequences

ROMEO:
Ay, see your children will be frails
Tybalt's fear; for himself, here this duck?


In this chapter, we implemented stopping criteria: by a stop token (such as a period) or a stop sequence (such as a blank line), the model stops generating at the right moment.

Exercises

  1. Try adding a maximum repetition stopping criterion: if the same token appears more than 3 times in a row (for example aaaa), stop generating.