callbacks.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import gc
  2. from queue import Queue
  3. from threading import Thread
  4. import torch
  5. import transformers
  6. import modules.shared as shared
  7. # Copied from https://github.com/PygmalionAI/gradio-ui/
  8. class _SentinelTokenStoppingCriteria(transformers.StoppingCriteria):
  9. def __init__(self, sentinel_token_ids: list[torch.LongTensor], starting_idx: int):
  10. transformers.StoppingCriteria.__init__(self)
  11. self.sentinel_token_ids = sentinel_token_ids
  12. self.starting_idx = starting_idx
  13. def __call__(self, input_ids: torch.LongTensor, _scores: torch.FloatTensor) -> bool:
  14. for sample in input_ids:
  15. trimmed_sample = sample[self.starting_idx:]
  16. for i in range(len(self.sentinel_token_ids)):
  17. # Can't unfold, output is still too tiny. Skip.
  18. if trimmed_sample.shape[-1] < self.sentinel_token_ids[i].shape[-1]:
  19. continue
  20. for window in trimmed_sample.unfold(0, self.sentinel_token_ids[i].shape[-1], 1):
  21. if torch.all(torch.eq(self.sentinel_token_ids[i][0], window)):
  22. return True
  23. return False
  24. class Stream(transformers.StoppingCriteria):
  25. def __init__(self, callback_func=None):
  26. self.callback_func = callback_func
  27. def __call__(self, input_ids, scores) -> bool:
  28. if self.callback_func is not None:
  29. self.callback_func(input_ids[0])
  30. return False
  31. class Iteratorize:
  32. """
  33. Transforms a function that takes a callback
  34. into a lazy iterator (generator).
  35. """
  36. def __init__(self, func, kwargs={}, callback=None):
  37. self.mfunc=func
  38. self.c_callback=callback
  39. self.q = Queue()
  40. self.sentinel = object()
  41. self.kwargs = kwargs
  42. self.stop_now = False
  43. def _callback(val):
  44. if self.stop_now:
  45. raise ValueError
  46. self.q.put(val)
  47. def gentask():
  48. try:
  49. ret = self.mfunc(callback=_callback, **self.kwargs)
  50. except ValueError:
  51. pass
  52. clear_torch_cache()
  53. self.q.put(self.sentinel)
  54. if self.c_callback:
  55. self.c_callback(ret)
  56. self.thread = Thread(target=gentask)
  57. self.thread.start()
  58. def __iter__(self):
  59. return self
  60. def __next__(self):
  61. obj = self.q.get(True,None)
  62. if obj is self.sentinel:
  63. raise StopIteration
  64. else:
  65. return obj
  66. def __del__(self):
  67. clear_torch_cache()
  68. def __enter__(self):
  69. return self
  70. def __exit__(self, exc_type, exc_val, exc_tb):
  71. self.stop_now = True
  72. clear_torch_cache()
  73. def clear_torch_cache():
  74. gc.collect()
  75. if not shared.args.cpu:
  76. torch.cuda.empty_cache()