base_batch_sampler.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. from typing import Union, Tuple, List, Dict, Any, Iterator
  15. from abc import ABC, abstractmethod
  16. from ....utils.flags import (
  17. INFER_BENCHMARK,
  18. INFER_BENCHMARK_ITER,
  19. INFER_BENCHMARK_DATA_SIZE,
  20. )
  21. class BaseBatchSampler:
  22. """BaseBatchSampler"""
  23. def __init__(self, batch_size: int = 1) -> None:
  24. """Initializes the BaseBatchSampler.
  25. Args:
  26. batch_size (int, optional): The size of each batch. Defaults to 1.
  27. """
  28. super().__init__()
  29. self._batch_size = batch_size
  30. self._benchmark = INFER_BENCHMARK
  31. self._benchmark_iter = INFER_BENCHMARK_ITER
  32. self._benchmark_data_size = INFER_BENCHMARK_DATA_SIZE
  33. @property
  34. def batch_size(self) -> int:
  35. """Gets the batch size."""
  36. return self._batch_size
  37. @batch_size.setter
  38. def batch_size(self, batch_size: int) -> None:
  39. """Sets the batch size.
  40. Args:
  41. batch_size (int): The batch size to set.
  42. Raises:
  43. AssertionError: If the batch size is not greater than 0.
  44. """
  45. assert batch_size > 0
  46. self._batch_size = batch_size
  47. def __call__(self, input: Any) -> Iterator[List[Any]]:
  48. """
  49. Sample batch data with the specified input.
  50. If input is None and benchmarking is enabled, it will yield batches
  51. of random data for the specified number of iterations.
  52. Otherwise, it will yield from the apply() function.
  53. Args:
  54. input (Any): The input data to sampled.
  55. Yields:
  56. Iterator[List[Any]]: An iterator yielding the batch data.
  57. """
  58. if input is None and self._benchmark:
  59. for _ in range(self._benchmark_iter):
  60. yield self._rand_batch(self._benchmark_data_size)
  61. else:
  62. yield from self.sample(input)
  63. @abstractmethod
  64. def sample(self, *args: Tuple[Any], **kwargs: Dict[str, Any]) -> Iterator[list]:
  65. """sample batch data"""
  66. raise NotImplementedError
  67. @abstractmethod
  68. def _rand_batch(self, batch_size: int) -> List[Any]:
  69. """rand batch data
  70. Args:
  71. batch_size (int): batch size
  72. """
  73. raise NotImplementedError