forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhf_stopping.py
33 lines (29 loc) · 1.12 KB
/
hf_stopping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
from tokenizers import Tokenizer
from transformers import StoppingCriteria
class SequenceStoppingCriteria(StoppingCriteria):
"""Enables automatic stopping of model text generation when specific text sequences are generated."""
def __init__(
self,
tokenizer: Tokenizer,
stop_texts: list[str],
input_prompt: str,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.stop_texts = stop_texts
self.tokenizer = tokenizer
self.input_length = len(tokenizer.encode(input_prompt))
def __call__(
self,
input_ids: torch.LongTensor,
scores: torch.FloatTensor,
**kwargs,
) -> bool:
# Assumes batch size 1, sufficient for our use case
generated_ids = input_ids[0, self.input_length :].tolist()
# TODO: optimise this. Inefficient to decode whole sequence every time
# but can't encode stop sequences as they don't always tokenize the same
generated_text = self.tokenizer.decode(generated_ids)
return any(text in generated_text for text in self.stop_texts)