doc_vlm_batch_sampler.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 ....utils import logging
  15. from .base_batch_sampler import BaseBatchSampler
  16. class DocVLMBatchSampler(BaseBatchSampler):
  17. def __init__(self):
  18. """Initializes the BaseBatchSampler.
  19. Args:
  20. batch_size (int, optional): The size of each batch. Only support 1.
  21. """
  22. super().__init__()
  23. self.batch_size = 1
  24. def sample(self, inputs):
  25. """Generate list of input file path.
  26. Args:
  27. inputs (str): file path.
  28. Yields:
  29. list: list of file path.
  30. """
  31. if isinstance(inputs, dict):
  32. yield [inputs]
  33. elif isinstance(inputs, list) and all(isinstance(i, dict) for i in inputs):
  34. yield inputs
  35. else:
  36. raise TypeError(
  37. f"Not supported input data type! Only `dict` are supported, but got: {type(inputs)}."
  38. )
  39. @BaseBatchSampler.batch_size.setter
  40. def batch_size(self, batch_size):
  41. """Sets the batch size.
  42. Args:
  43. batch_size (int): The batch size to set.
  44. Raises:
  45. Warning: If the batch size is not equal 1.
  46. """
  47. # only support batch size 1
  48. if batch_size != 1:
  49. logging.warning(
  50. f"doc vlm batch sampler only support batch size 1, but got {batch_size}."
  51. )
  52. else:
  53. self._batch_size = batch_size