# Implimentation of Stopping Criteria List

**URL:** <https://discuss.huggingface.co/t/implimentation-of-stopping-criteria-list/20040>\
**Category:** Beginners\
**Created:** [July 5, 2022, 9:52pm UTC](https://discuss.huggingface.co/t/implimentation-of-stopping-criteria-list/20040 "2022-07-05T21:52:06Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![berkecr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/berkecr/32/32071_2.png) [@berkecr](https://discuss.huggingface.co/u/berkecr)\
**Post date:** [September 15, 2023, 9:37am UTC](https://discuss.huggingface.co/t/implimentation-of-stopping-criteria-list/20040/9 "2023-09-15T09:37:10Z")

</div>

In addition to @hatimbr ‘s comment, sometimes the same string may be mapped to different ids by the tokenizer due to preceding tokens.  
Example:  
In the context of given text,  
{  
“text”: "\n’pizza’,\n’calzone’,\n’stromboli’,\n’focaccia’,\n’flatbread’,\n’naan’,\n’roti’,\n’paratha’]"  
}  
last `']` maps to tensor([525, 29962]) while my given stop sequence `']` maps to tensor(2033)

As a workaround,

```auto
class StoppingCriteriaSub(StoppingCriteria):
    def __init__ (self, stops = [], encounters=1):
        super(). __init__ ()
        self.stops = [stop.to("cuda") for stop in stops]

    def __call__ (self, input_ids: torch.LongTensor, scores: torch.FloatTensor):
        last_token = input_ids[0][-1]
        for stop in self.stops:
            if tokenizer.decode(stop) == tokenizer.decode(last_token):
                return True
        return False

```

to use it,

```auto
stop_words = ["]", "']", "']\n", "]\n", "\n\n", "']\n\n"]
stop_words_ids = [tokenizer(stop_word, return_tensors='pt', add_special_tokens=False)['input_ids'].squeeze() for stop_word in stop_words]
stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])

```

This may be slowing down text generation, so if anyone has better suggestions, I’m eager to listen.

I got Llama 2 to produce a parsable list with this.

---

_[View the full topic](https://discuss.huggingface.co/t/implimentation-of-stopping-criteria-list/20040)._
