process_hook.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import inspect
  15. import functools
  16. from types import GeneratorType
  17. def batchable_method(func):
  18. """batchable"""
  19. @functools.wraps(func)
  20. def _wrapper(self, input_, *args, **kwargs):
  21. if isinstance(input_, list):
  22. output = []
  23. for ele in input_:
  24. out = func(self, ele, *args, **kwargs)
  25. output.append(out)
  26. return output
  27. else:
  28. return func(self, input_, *args, **kwargs)
  29. sig = inspect.signature(func)
  30. if not len(sig.parameters) >= 2:
  31. raise TypeError("The function to wrap should have at least two parameters.")
  32. return _wrapper
  33. def generatorable_method(func):
  34. """generatorable"""
  35. @functools.wraps(func)
  36. def _wrapper(self, input_, *args, **kwargs):
  37. if isinstance(input_, GeneratorType):
  38. for ele in input_:
  39. yield func(self, ele, *args, **kwargs)
  40. else:
  41. yield func(self, input_, *args, **kwargs)
  42. sig = inspect.signature(func)
  43. if not len(sig.parameters) >= 2:
  44. raise TypeError("The function to wrap should have at least two parameters.")
  45. return _wrapper