tqdm.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright 2022 The HuggingFace Team. 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 .imports import is_tqdm_available
  15. if is_tqdm_available():
  16. from tqdm.auto import tqdm as _tqdm
  17. from ..state import PartialState
  18. def tqdm(*args, main_process_only: bool = True, **kwargs):
  19. """
  20. Wrapper around `tqdm.tqdm` that optionally displays only on the main process.
  21. Args:
  22. main_process_only (`bool`, *optional*):
  23. Whether to display the progress bar only on the main process
  24. """
  25. if not is_tqdm_available():
  26. raise ImportError("Accelerate's `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.")
  27. if len(args) > 0 and isinstance(args[0], bool):
  28. raise ValueError(
  29. "Passing `True` or `False` as the first argument to Accelerate's `tqdm` wrapper is unsupported. "
  30. "Please use the `main_process_only` keyword argument instead."
  31. )
  32. disable = kwargs.pop("disable", False)
  33. if main_process_only and not disable:
  34. disable = PartialState().local_process_index != 0
  35. return _tqdm(*args, **kwargs, disable=disable)