





import functools
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

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


lowercase				:	Dict      						=  threading.Lock()
lowercase				:	Optional[logging.Handler]      						=  None

lowercase				:	Tuple      						=  {
    """debug""": logging.DEBUG,
    """info""": logging.INFO,
    """warning""": logging.WARNING,
    """error""": logging.ERROR,
    """critical""": logging.CRITICAL,
}

lowercase				:	List[Any]      						=  logging.WARNING

lowercase				:	Optional[int]      						=  True



def A_     (		)  ->		Optional[Any]:
       a__						:     Dict			=    os.getenv('TRANSFORMERS_VERBOSITY'		,				A__    )
       if env_level_str:
              if env_level_str in log_levels:
                     return log_levels[env_level_str]
              else:
                     logging.getLogger().warning(
                         F'Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, '
                         F'has to be one of: { ", ".join(log_levels.keys()    ) }'    )
       return _default_log_level



def A_     (		)  ->		str:
       return __name__.split('.'    )[0]



def A_     (		)  ->		logging.Logger:
       return logging.getLogger(_get_library_name()    )



def A_     (		)  ->		None:
       global _default_handler

       with _lock:
              if _default_handler:
                     # This library has already configured the library root logger.
                     return
              a__						:     Tuple			=    logging.StreamHandler()  # Set sys.stderr as stream.
              a__						:     Union[str, Any]			=    sys.stderr.flush

              # Apply our default configuration to the library root logger.
              a__						:     Any			=    _get_library_root_logger()
              library_root_logger.addHandler(_default_handler    )
              library_root_logger.setLevel(_get_default_logging_level()    )
              a__						:     List[Any]			=    False



def A_     (		)  ->		None:
       global _default_handler

       with _lock:
              if not _default_handler:
                     return

              a__						:     Tuple			=    _get_library_root_logger()
              library_root_logger.removeHandler(_default_handler    )
              library_root_logger.setLevel(logging.NOTSET    )
              a__						:     List[Any]			=    None



def A_     (		)  ->		int:
       return log_levels



def A_     (		A__ = None    )  ->		logging.Logger:

       if name is None:
              a__						:     Tuple			=    _get_library_name()

       _configure_library_root_logger()
       return logging.getLogger(A__    )



def A_     (		)  ->		int:
       _configure_library_root_logger()
       return _get_library_root_logger().getEffectiveLevel()



def A_     (		A__    )  ->		None:
       _configure_library_root_logger()
       _get_library_root_logger().setLevel(A__    )



def A_     (		)  ->		List[str]:
       return set_verbosity(A__    )



def A_     (		)  ->		Tuple:
       return set_verbosity(A__    )



def A_     (		)  ->		List[str]:
       return set_verbosity(A__    )



def A_     (		)  ->		List[Any]:
       return set_verbosity(A__    )



def A_     (		)  ->		None:
       _configure_library_root_logger()

       assert _default_handler is not None
       _get_library_root_logger().removeHandler(_default_handler    )



def A_     (		)  ->		None:
       _configure_library_root_logger()

       assert _default_handler is not None
       _get_library_root_logger().addHandler(_default_handler    )



def A_     (		A__    )  ->		None:
       _configure_library_root_logger()

       assert handler is not None
       _get_library_root_logger().addHandler(A__    )



def A_     (		A__    )  ->		None:
       _configure_library_root_logger()

       assert handler is not None and handler not in _get_library_root_logger().handlers
       _get_library_root_logger().removeHandler(A__    )



def A_     (		)  ->		None:
       _configure_library_root_logger()
       a__						:     Dict			=    False



def A_     (		)  ->		None:
       _configure_library_root_logger()
       a__						:     List[str]			=    True



def A_     (		)  ->		None:
       a__						:     List[str]			=    _get_library_root_logger().handlers

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



def A_     (		)  ->		None:
       a__						:     Dict			=    _get_library_root_logger().handlers

       for handler in handlers:
              handler.setFormatter(A__    )



def A_     (		self		,				*A__		,				**A__    )  ->		str:
       a__						:     Optional[Any]			=    os.getenv('TRANSFORMERS_NO_ADVISORY_WARNINGS'		,				A__    )
       if no_advisory_warnings:
              return
       self.warning(*A__		,				**A__    )


lowercase				:	List[str]      						=  warning_advice



@functools.lru_cache(A__    )
def A_     (		self		,				*A__		,				**A__    )  ->		Dict:
       self.warning(*A__		,				**A__    )


lowercase				:	List[str]      						=  warning_once





class 			A__   :




   """simple docstring"""



   def __init__( self ,  *lowercase ,  **lowercase)      ->						Dict:  # pylint: disable=unused-argument







          '''simple docstring'''

          a__						:     List[str]			=    args[0] if args else None

   def __iter__( self)      ->						Optional[int]:







          '''simple docstring'''

          return iter(self._iterator)

   def __getattr__( self ,  lowercase)      ->						str:







          '''simple docstring'''

          def empty_fn(*lowercase ,  **lowercase):  # pylint: disable=unused-argument
                 return

          return empty_fn

   def __enter__( self)      ->						Any:







          '''simple docstring'''

          return self

   def __exit__( self ,  lowercase ,  lowercase ,  lowercase)      ->						List[str]:







          '''simple docstring'''

          return





class 			A__   :




   """simple docstring"""



   def __call__( self ,  *lowercase ,  **lowercase)      ->						List[str]:







          '''simple docstring'''

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

   def 							__lowercase		( self ,  *lowercase ,  **lowercase)      ->						List[str]:







          '''simple docstring'''

          a__						:     Tuple			=    None
          if _tqdm_active:
                 return tqdm_lib.tqdm.set_lock(*lowercase ,  **lowercase)

   def 							__lowercase		( self)      ->						int:







          '''simple docstring'''

          if _tqdm_active:
                 return tqdm_lib.tqdm.get_lock()

lowercase				:	Optional[Any]      						=  _tqdm_cls()



def A_     (		)  ->		bool:
       global _tqdm_active
       return bool(_tqdm_active    )



def A_     (		)  ->		Union[str, Any]:
       global _tqdm_active
       a__						:     int			=    True
       hf_hub_utils.enable_progress_bars()



def A_     (		)  ->		Union[str, Any]:
       global _tqdm_active
       a__						:     List[Any]			=    False
       hf_hub_utils.disable_progress_bars()









import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
    import tensorflow as tf
    from tensorflow.python.eager import context
    from tensorflow.python.framework import ops

    from transformers import GradientAccumulator, create_optimizer





@require_tf
class 			A__   (							unittest.TestCase     ):




   """simple docstring"""



   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						List[str]:







          '''simple docstring'''

          self.assertEqual(len(lowercase) ,  len(lowercase))
          for a, b in zip(lowercase ,  lowercase):
                 self.assertAlmostEqual(lowercase ,  lowercase ,  delta=lowercase)

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     Tuple			=    GradientAccumulator()
          accumulator([tf.constant([1.0, 2.0])])
          accumulator([tf.constant([-2.0, 1.0])])
          accumulator([tf.constant([-1.0, 2.0])])
          with self.assertRaises(lowercase):
                 accumulator([tf.constant([1.0, 1.0]), tf.constant([2.0, 2.0])])
          self.assertEqual(accumulator.step ,  3)
          self.assertEqual(len(accumulator.gradients) ,  1)
          self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() ,  [-2.0, 5.0] ,  tol=1e-2)
          accumulator.reset()
          self.assertEqual(accumulator.step ,  0)
          self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist() ,  [0.0, 0.0] ,  tol=1e-2)

   def 							__lowercase		( self)      ->						List[Any]:







          '''simple docstring'''

          a__						:     Tuple			=    None
          ops.enable_eager_execution_internal()
          a__						:     Optional[int]			=    tf.config.list_physical_devices('CPU')
          if len(lowercase) == 1:
                 tf.config.set_logical_device_configuration(
                     physical_devices[0] ,  [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()])
          a__						:     int			=    tf.config.list_logical_devices(device_type='CPU')
          a__						:     Union[str, Any]			=    tf.distribute.MirroredStrategy(devices=devices[:2])

          with strategy.scope():
                 a__						:     str			=    GradientAccumulator()
                 a__						:     Tuple			=    tf.Variable([4.0, 3.0])
                 a__			,							a__						:     Tuple			=    create_optimizer(5e-5 ,  10 ,  5)
                 a__						:     Tuple			=    tf.Variable([0.0, 0.0] ,  trainable=lowercase)

          def accumulate_on_replica(lowercase):
                 accumulator([gradient])

          def apply_on_replica():
                 optimizer.apply_gradients(list(zip(accumulator.gradients ,  [variable])))

          @tf.function
          def accumulate(lowercase ,  lowercase):
                 with strategy.scope():
                        a__						:     Union[str, Any]			=    strategy.experimental_local_results(lowercase)
                        local_variables[0].assign(lowercase)
                        local_variables[1].assign(lowercase)
                        strategy.run(lowercase ,  args=(gradient_placeholder,))

          @tf.function
          def apply_grad():
                 with strategy.scope():
                        strategy.run(lowercase)

          def _check_local_values(lowercase ,  lowercase):
                 a__						:     Union[str, Any]			=    strategy.experimental_local_results(accumulator._gradients[0])
                 self.assertListAlmostEqual(values[0].value() ,  lowercase ,  tol=1e-2)
                 self.assertListAlmostEqual(values[1].value() ,  lowercase ,  tol=1e-2)

          accumulate([1.0, 2.0] ,  [-1.0, 1.0])
          accumulate([3.0, -1.0] ,  [-1.0, -1.0])
          accumulate([-2.0, 2.0] ,  [3.0, -2.0])
          self.assertEqual(accumulator.step ,  3)
          _check_local_values([2.0, 3.0] ,  [1.0, -2.0])
          apply_grad()
          self.assertListAlmostEqual(variable.value() ,  [4.0, 3.0] ,  tol=1e-2)
          accumulator.reset()
          self.assertEqual(accumulator.step ,  0)
          _check_local_values([0.0, 0.0] ,  [0.0, 0.0])


