2023-03-08 00:46:35 -05:00
|
|
|
from queue import Queue
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import transformers
|
|
|
|
|
2023-03-23 20:56:26 -04:00
|
|
|
from modules.text_generation import clear_torch_cache
|
2023-03-08 00:46:35 -05:00
|
|
|
|
2023-03-17 10:42:25 -04:00
|
|
|
|
2023-03-08 00:46:35 -05:00
|
|
|
# Copied from https://github.com/PygmalionAI/gradio-ui/
|
|
|
|
class _SentinelTokenStoppingCriteria(transformers.StoppingCriteria):
|
|
|
|
|
2023-03-23 20:38:20 -04:00
|
|
|
def __init__(self, sentinel_token_ids: list[torch.LongTensor], starting_idx: int):
|
2023-03-08 00:46:35 -05:00
|
|
|
transformers.StoppingCriteria.__init__(self)
|
|
|
|
self.sentinel_token_ids = sentinel_token_ids
|
|
|
|
self.starting_idx = starting_idx
|
|
|
|
|
2023-03-23 20:38:20 -04:00
|
|
|
def __call__(self, input_ids: torch.LongTensor, _scores: torch.FloatTensor) -> bool:
|
2023-03-08 00:46:35 -05:00
|
|
|
for sample in input_ids:
|
|
|
|
trimmed_sample = sample[self.starting_idx:]
|
2023-03-23 20:38:20 -04:00
|
|
|
|
|
|
|
for i in range(len(self.sentinel_token_ids)):
|
|
|
|
# Can't unfold, output is still too tiny. Skip.
|
|
|
|
if trimmed_sample.shape[-1] < self.sentinel_token_ids[i].shape[-1]:
|
|
|
|
continue
|
|
|
|
for window in trimmed_sample.unfold(0, self.sentinel_token_ids[i].shape[-1], 1):
|
|
|
|
if torch.all(torch.eq(self.sentinel_token_ids[i], window)):
|
|
|
|
return True
|
2023-03-08 00:46:35 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
class Stream(transformers.StoppingCriteria):
|
|
|
|
def __init__(self, callback_func=None):
|
|
|
|
self.callback_func = callback_func
|
|
|
|
|
|
|
|
def __call__(self, input_ids, scores) -> bool:
|
|
|
|
if self.callback_func is not None:
|
|
|
|
self.callback_func(input_ids[0])
|
|
|
|
return False
|
|
|
|
|
|
|
|
class Iteratorize:
|
|
|
|
|
|
|
|
"""
|
|
|
|
Transforms a function that takes a callback
|
|
|
|
into a lazy iterator (generator).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, func, kwargs={}, callback=None):
|
|
|
|
self.mfunc=func
|
|
|
|
self.c_callback=callback
|
2023-03-11 21:14:49 -05:00
|
|
|
self.q = Queue()
|
2023-03-08 00:46:35 -05:00
|
|
|
self.sentinel = object()
|
|
|
|
self.kwargs = kwargs
|
2023-03-12 00:04:28 -05:00
|
|
|
self.stop_now = False
|
2023-03-08 00:46:35 -05:00
|
|
|
|
|
|
|
def _callback(val):
|
2023-03-12 00:04:28 -05:00
|
|
|
if self.stop_now:
|
|
|
|
raise ValueError
|
2023-03-08 00:46:35 -05:00
|
|
|
self.q.put(val)
|
|
|
|
|
|
|
|
def gentask():
|
2023-03-12 00:04:28 -05:00
|
|
|
try:
|
|
|
|
ret = self.mfunc(callback=_callback, **self.kwargs)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2023-03-12 00:53:08 -05:00
|
|
|
clear_torch_cache()
|
2023-03-08 00:46:35 -05:00
|
|
|
self.q.put(self.sentinel)
|
|
|
|
if self.c_callback:
|
|
|
|
self.c_callback(ret)
|
|
|
|
|
2023-03-12 00:04:28 -05:00
|
|
|
self.thread = Thread(target=gentask)
|
|
|
|
self.thread.start()
|
2023-03-08 00:46:35 -05:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
obj = self.q.get(True,None)
|
|
|
|
if obj is self.sentinel:
|
|
|
|
raise StopIteration
|
|
|
|
else:
|
|
|
|
return obj
|
2023-03-11 21:14:49 -05:00
|
|
|
|
|
|
|
def __del__(self):
|
2023-03-12 00:04:28 -05:00
|
|
|
clear_torch_cache()
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
self.stop_now = True
|
|
|
|
clear_torch_cache()
|