callbacks.py 2.8 KB

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