# Stop generation while using past in GPT-2

**URL:** <https://discuss.huggingface.co/t/stop-generation-while-using-past-in-gpt-2/2073>\
**Category:** Beginners\
**Created:** [November 15, 2020, 10:10pm UTC](https://discuss.huggingface.co/t/stop-generation-while-using-past-in-gpt-2/2073 "2020-11-15T22:10:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Speedeo](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Speedeo](https://discuss.huggingface.co/u/Speedeo)\
**Post date:** [November 15, 2020, 10:10pm UTC](https://discuss.huggingface.co/t/stop-generation-while-using-past-in-gpt-2/2073/1 "2020-11-15T22:10:17Z")

</div>

I am writing custom backend support for a game using GPT-2. I am try to tokenizing \n to stop generating when we reach a new line. The token is a blank token with nothing in it. Is there a way while using past to stop generation at a certain token?

This is modified example

Code 1 Main.py:

```
`EndTokens = enc.encode(EndToken)`

```

EndToken is \n

Code 2 sample.py:

```
        if len(EndTokens) > 0 and prev[0, 0] == EndTokens[0] : 

                        break

```

Prev is a output from gpt-2. This code is in a loop generating a set amount of tokens.
