batch.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
  2. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import inspect
  16. import functools
  17. import itertools
  18. __all__ = ['batchable_method', 'apply_batch', 'Batcher']
  19. def batchable_method(func):
  20. """ batchable """
  21. @functools.wraps(func)
  22. def _wrapper(self, input_, *args, **kwargs):
  23. if isinstance(input_, list):
  24. output = []
  25. for ele in input_:
  26. out = func(self, ele, *args, **kwargs)
  27. output.append(out)
  28. return output
  29. else:
  30. return func(self, input_, *args, **kwargs)
  31. sig = inspect.signature(func)
  32. if not len(sig.parameters) >= 2:
  33. raise TypeError(
  34. "The function to wrap should have at least two parameters.")
  35. return _wrapper
  36. def apply_batch(batch, callable_, *args, **kwargs):
  37. """ apply batch """
  38. output = []
  39. for ele in batch:
  40. out = callable_(ele, *args, **kwargs)
  41. output.append(out)
  42. return output
  43. class Batcher(object):
  44. """ Batcher """
  45. def __init__(self, iterable, batch_size=None):
  46. super().__init__()
  47. self.iterable = iterable
  48. self.batch_size = batch_size
  49. def __iter__(self):
  50. if self.batch_size is None:
  51. all_data = list(self.iterable)
  52. yield all_data
  53. else:
  54. iterator = iter(self.iterable)
  55. while True:
  56. batch = list(itertools.islice(iterator, self.batch_size))
  57. if not batch:
  58. break
  59. yield batch