2023-03-24 02:16:08 +01:00
|
|
|
import gc
|
2023-03-29 00:20:50 +02:00
|
|
|
import traceback
|
2023-03-08 06:46:35 +01:00
|
|
|
from queue import Queue
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import transformers
|
2023-10-27 04:39:51 +02:00
|
|
|
from transformers import is_torch_xpu_available
|
2023-03-08 06:46:35 +01:00
|
|
|
|
2023-03-24 02:19:01 +01:00
|
|
|
import modules.shared as shared
|
|
|
|
|
2023-03-17 15:42:25 +01:00
|
|
|
|
2024-01-07 14:36:26 +01:00
|
|
|
class StopNowException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-06-24 16:19:16 +02:00
|
|
|
class _StopEverythingStoppingCriteria(transformers.StoppingCriteria):
|
|
|
|
def __init__(self):
|
|
|
|
transformers.StoppingCriteria.__init__(self)
|
|
|
|
|
|
|
|
def __call__(self, input_ids: torch.LongTensor, _scores: torch.FloatTensor) -> bool:
|
|
|
|
return shared.stop_everything
|
|
|
|
|
|
|
|
|
2023-03-08 06:46:35 +01:00
|
|
|
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])
|
2023-08-23 05:18:16 +02:00
|
|
|
|
2023-03-08 06:46:35 +01:00
|
|
|
return False
|
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-03-08 06:46:35 +01:00
|
|
|
class Iteratorize:
|
|
|
|
|
|
|
|
"""
|
|
|
|
Transforms a function that takes a callback
|
|
|
|
into a lazy iterator (generator).
|
2023-05-04 02:49:55 +02:00
|
|
|
|
|
|
|
Adapted from: https://stackoverflow.com/a/9969000
|
2023-03-08 06:46:35 +01:00
|
|
|
"""
|
|
|
|
|
2023-06-17 01:35:38 +02:00
|
|
|
def __init__(self, func, args=None, kwargs=None, callback=None):
|
2023-04-07 05:15:45 +02:00
|
|
|
self.mfunc = func
|
|
|
|
self.c_callback = callback
|
2023-03-12 03:14:49 +01:00
|
|
|
self.q = Queue()
|
2023-03-08 06:46:35 +01:00
|
|
|
self.sentinel = object()
|
2023-06-17 01:35:38 +02:00
|
|
|
self.args = args or []
|
2023-05-09 03:55:41 +02:00
|
|
|
self.kwargs = kwargs or {}
|
2023-03-12 06:04:28 +01:00
|
|
|
self.stop_now = False
|
2023-03-08 06:46:35 +01:00
|
|
|
|
|
|
|
def _callback(val):
|
2023-03-27 18:23:59 +02:00
|
|
|
if self.stop_now or shared.stop_everything:
|
2024-01-07 14:36:26 +01:00
|
|
|
raise StopNowException
|
2023-03-08 06:46:35 +01:00
|
|
|
self.q.put(val)
|
|
|
|
|
|
|
|
def gentask():
|
2023-03-12 06:04:28 +01:00
|
|
|
try:
|
2023-06-17 01:35:38 +02:00
|
|
|
ret = self.mfunc(callback=_callback, *args, **self.kwargs)
|
2024-01-07 14:36:26 +01:00
|
|
|
except StopNowException:
|
2023-03-12 06:04:28 +01:00
|
|
|
pass
|
2023-03-29 00:20:50 +02:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
pass
|
|
|
|
|
2023-03-12 06:53:08 +01:00
|
|
|
clear_torch_cache()
|
2023-03-08 06:46:35 +01:00
|
|
|
self.q.put(self.sentinel)
|
|
|
|
if self.c_callback:
|
|
|
|
self.c_callback(ret)
|
|
|
|
|
2023-03-12 06:04:28 +01:00
|
|
|
self.thread = Thread(target=gentask)
|
|
|
|
self.thread.start()
|
2023-03-08 06:46:35 +01:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
2023-04-07 05:15:45 +02:00
|
|
|
obj = self.q.get(True, None)
|
2023-03-08 06:46:35 +01:00
|
|
|
if obj is self.sentinel:
|
|
|
|
raise StopIteration
|
|
|
|
else:
|
|
|
|
return obj
|
2023-03-12 03:14:49 +01:00
|
|
|
|
|
|
|
def __del__(self):
|
2023-03-12 06:04:28 +01: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()
|
2023-03-24 02:12:24 +01:00
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-03-24 02:12:24 +01:00
|
|
|
def clear_torch_cache():
|
|
|
|
gc.collect()
|
|
|
|
if not shared.args.cpu:
|
2023-10-27 04:39:51 +02:00
|
|
|
if is_torch_xpu_available():
|
|
|
|
torch.xpu.empty_cache()
|
|
|
|
else:
|
|
|
|
torch.cuda.empty_cache()
|