'''simple docstring'''


import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


snake_case__	:     str      =    logging.get_logger(__name__)

if is_tf_available():
 import tensorflow as tf






@dataclass
class 	__SCREAMING_SNAKE_CASE		(			lowerCamelCase_			):







  '''simple docstring'''






  lowerCamelCase_						:List[Any]		       =						'''tf'''
  lowerCamelCase_						:Optional[str]		       =						field(
      default=lowerCamelCase_		,      metadata={'''help''': '''Name of TPU'''}		,      )

  lowerCamelCase_						:Optional[str]		       =						field(
      default=lowerCamelCase_		,      metadata={'''help''': '''Zone of TPU'''}		,      )

  lowerCamelCase_						:Optional[str]		       =						field(
      default=lowerCamelCase_		,      metadata={'''help''': '''Name of Cloud TPU-enabled project'''}		,      )

  lowerCamelCase_						:float		       =						field(
      default=1.0		,      metadata={'''help''': '''Power for the Polynomial decay LR scheduler.'''}		,      )

  lowerCamelCase_						:bool		       =						field(default=lowerCamelCase_		,      metadata={'''help''': '''Whether to activate the XLA compilation or not'''}			)


  @cached_property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       requires_backends(self ,						['tf']		)
       logger.info('Tensorflow: setting up strategy'		)

       UpperCAmelCase_				:  List[str]  = tf.config.list_physical_devices('GPU'		)

       # Set to float16 at first
       if self.fpaa:
            tf.keras.mixed_precision.set_global_policy('mixed_float16'		)

       if self.no_cuda:
            UpperCAmelCase_				:  Tuple  = tf.distribute.OneDeviceStrategy(device='/cpu:0'		)
       else:
            try:
                 if self.tpu_name:
                      UpperCAmelCase_				:  List[Any]  = tf.distribute.cluster_resolver.TPUClusterResolver(
                          self.tpu_name ,						zone=self.tpu_zone ,						project=self.gcp_project		)
                 else:
                      UpperCAmelCase_				:  Any  = tf.distribute.cluster_resolver.TPUClusterResolver()
            except ValueError:
                 if self.tpu_name:
                      raise RuntimeError(F'''Couldn\'t connect to TPU {self.tpu_name}!'''		)
                 else:
                      UpperCAmelCase_				:  str  = None

            if tpu:
                 # Set to bfloat16 in case of TPU
                 if self.fpaa:
                      tf.keras.mixed_precision.set_global_policy('mixed_bfloat16'		)

                 tf.config.experimental_connect_to_cluster(snake_case_		)
                 tf.tpu.experimental.initialize_tpu_system(snake_case_		)

                 UpperCAmelCase_				:  str  = tf.distribute.TPUStrategy(snake_case_		)

            elif len(snake_case_		) == 0:
                 UpperCAmelCase_				:  Dict  = tf.distribute.OneDeviceStrategy(device='/cpu:0'		)
            elif len(snake_case_		) == 1:
                 UpperCAmelCase_				:  str  = tf.distribute.OneDeviceStrategy(device='/gpu:0'		)
            elif len(snake_case_		) > 1:
                 # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
                 UpperCAmelCase_				:  Dict  = tf.distribute.MirroredStrategy()
            else:
                 raise ValueError('Cannot find the proper strategy, please check your environment properties.'		)

       return strategy


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       requires_backends(self ,						['tf']		)
       return self._setup_strategy


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       requires_backends(self ,						['tf']		)
       return self._setup_strategy.num_replicas_in_sync


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       return False  # TF Logging is handled by Keras not the Trainer


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       if self.per_gpu_train_batch_size:
            logger.warning(
                'Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future '
                'version. Using `--per_device_train_batch_size` is preferred.'		)
       UpperCAmelCase_				:  Union[str, Any]  = self.per_gpu_train_batch_size or self.per_device_train_batch_size
       return per_device_batch_size * self.n_replicas


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       if self.per_gpu_eval_batch_size:
            logger.warning(
                'Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future '
                'version. Using `--per_device_eval_batch_size` is preferred.'		)
       UpperCAmelCase_				:  Union[str, Any]  = self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
       return per_device_batch_size * self.n_replicas






  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       requires_backends(self ,						['tf']		)
       warnings.warn(
           'The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead.' ,						snake_case_ ,						)
       return self._setup_strategy.num_replicas_in_sync














import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


lowerCamelCase					    =	threading.Lock()
lowerCamelCase					    =	None

lowerCamelCase					    =	{
    '''debug''': logging.DEBUG,
    '''info''': logging.INFO,
    '''warning''': logging.WARNING,
    '''error''': logging.ERROR,
    '''critical''': logging.CRITICAL,
}

lowerCamelCase					    =	logging.WARNING

lowerCamelCase					    =	True
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _lowercase     			=						os.getenv('DIFFUSERS_VERBOSITY',   _lowercase   )
    if env_level_str:
        if env_level_str in log_levels:
            return log_levels[env_level_str]
        else:
            logging.getLogger().warning(
                f'''Unknown option DIFFUSERS_VERBOSITY={env_level_str}, '''
                f'''has to be one of: { ", ".join(log_levels.keys()   ) }'''   )
    return _default_log_level
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    return __name__.split('.'   )[0]
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    return logging.getLogger(_get_library_name()   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    global _default_handler

    with _lock:
        if _default_handler:
            # This library has already configured the library root logger.
            return
        _lowercase     			=						logging.StreamHandler()  # Set sys.stderr as stream.
        _lowercase     			=						sys.stderr.flush

        # Apply our default configuration to the library root logger.
        _lowercase     			=						_get_library_root_logger()
        library_root_logger.addHandler(_default_handler   )
        library_root_logger.setLevel(_get_default_logging_level()   )
        _lowercase     			=						False
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    global _default_handler

    with _lock:
        if not _default_handler:
            return

        _lowercase     			=						_get_library_root_logger()
        library_root_logger.removeHandler(_default_handler   )
        library_root_logger.setLevel(logging.NOTSET   )
        _lowercase     			=						None
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    return log_levels
def 							UpperCAmelCase   ( _lowercase = None   ):


    '''simple docstring'''



    if name is None:
        _lowercase     			=						_get_library_name()

    _configure_library_root_logger()
    return logging.getLogger(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _configure_library_root_logger()
    return _get_library_root_logger().getEffectiveLevel()
def 							UpperCAmelCase   ( _lowercase   ):


    '''simple docstring'''



    _configure_library_root_logger()
    _get_library_root_logger().setLevel(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    return set_verbosity(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    return set_verbosity(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    return set_verbosity(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    return set_verbosity(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _configure_library_root_logger()

    assert _default_handler is not None
    _get_library_root_logger().removeHandler(_default_handler   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _configure_library_root_logger()

    assert _default_handler is not None
    _get_library_root_logger().addHandler(_default_handler   )
def 							UpperCAmelCase   ( _lowercase   ):


    '''simple docstring'''



    _configure_library_root_logger()

    assert handler is not None
    _get_library_root_logger().addHandler(_lowercase   )
def 							UpperCAmelCase   ( _lowercase   ):


    '''simple docstring'''



    _configure_library_root_logger()

    assert handler is not None and handler not in _get_library_root_logger().handlers
    _get_library_root_logger().removeHandler(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _configure_library_root_logger()
    _lowercase     			=						False
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _configure_library_root_logger()
    _lowercase     			=						True
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _lowercase     			=						_get_library_root_logger().handlers

    for handler in handlers:
        _lowercase     			=						logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'   )
        handler.setFormatter(_lowercase   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    _lowercase     			=						_get_library_root_logger().handlers

    for handler in handlers:
        handler.setFormatter(_lowercase   )
def 							UpperCAmelCase   ( self,   *_lowercase,   **_lowercase   ):


    '''simple docstring'''



    _lowercase     			=						os.getenv('DIFFUSERS_NO_ADVISORY_WARNINGS',   _lowercase   )
    if no_advisory_warnings:
        return
    self.warning(*_lowercase,   **_lowercase   )


lowerCamelCase					    =	warning_advice

class        snake_case_				:







   """simple docstring"""



   def __init__(     self:						int ,							*snake_case__:						str ,							**snake_case__:						Any						)	->		Any:  # pylint: disable=unused-argument


       """simple docstring"""



       _lowercase     			=						args[0] if args else None

   def __iter__(     self:						Union[str, Any]						)	->		str:


       """simple docstring"""



       return iter(self._iterator						)

   def __getattr__(     self:						int ,							snake_case__:						Any						)	->		List[Any]:


       """simple docstring"""



       def empty_fn(*snake_case__:						Optional[int] ,							**snake_case__:						Union[str, Any]						):  # pylint: disable=unused-argument
           return

       return empty_fn

   def __enter__(     self:						List[Any]						)	->		Tuple:


       """simple docstring"""



       return self

   def __exit__(     self:						List[str] ,							snake_case__:						Optional[Any] ,							snake_case__:						Dict ,							snake_case__:						List[str]						)	->		Optional[Any]:


       """simple docstring"""



       return






class        snake_case_				:







   """simple docstring"""



   def __call__(     self:						List[str] ,							*snake_case__:						Tuple ,							**snake_case__:						Union[str, Any]						)	->		List[str]:


       """simple docstring"""



       if _tqdm_active:
           return tqdm_lib.tqdm(*snake_case__ ,							**snake_case__						)
       else:
           return EmptyTqdm(*snake_case__ ,							**snake_case__						)

   def       SCREAMING_SNAKE_CASE__					(     self:						Tuple ,							*snake_case__:						Union[str, Any] ,							**snake_case__:						str						)	->		Optional[int]:


       """simple docstring"""



       _lowercase     			=						None
       if _tqdm_active:
           return tqdm_lib.tqdm.set_lock(*snake_case__ ,							**snake_case__						)

   def       SCREAMING_SNAKE_CASE__					(     self:						int						)	->		int:


       """simple docstring"""



       if _tqdm_active:
           return tqdm_lib.tqdm.get_lock()






lowerCamelCase					    =	_tqdm_cls()
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    global _tqdm_active
    return bool(_tqdm_active   )
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    global _tqdm_active
    _lowercase     			=						True
def 							UpperCAmelCase   ( ):


    '''simple docstring'''



    global _tqdm_active
    _lowercase     			=						False
