doc_vlm_batch_sampler.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. else:
  34. logging.warning(
  35. f"Not supported input data type! Only `dict` are supported, but got: {input}."
  36. )
  37. @BaseBatchSampler.batch_size.setter
  38. def batch_size(self, batch_size):
  39. """Sets the batch size.
  40. Args:
  41. batch_size (int): The batch size to set.
  42. Raises:
  43. Warning: If the batch size is not equal 1.
  44. """
  45. # only support batch size 1
  46. if batch_size != 1:
  47. logging.warning(
  48. f"doc vlm batch sampler only support batch size 1, but got {batch_size}."
  49. )
  50. else:
  51. self._batch_size = batch_size