






"""simple docstring"""





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


_SCREAMING_SNAKE_CASE   :							Optional[int]          =		threading.Lock()
_SCREAMING_SNAKE_CASE   :							Optional[logging.Handler]          =		None

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

_SCREAMING_SNAKE_CASE   :							Any          =		logging.WARNING

_SCREAMING_SNAKE_CASE   :							Optional[int]          =		True







def 	lowerCamelCase__			(				) ->							List[Any]:
  lowerCamelCase_       	=		os.getenv('TRANSFORMERS_VERBOSITY'						,							_lowerCamelCase					)
  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 	lowerCamelCase__			(				) ->							str:
  return __name__.split('.'					)[0]







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







def 	lowerCamelCase__			(				) ->							None:
  global _default_handler

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

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







def 	lowerCamelCase__			(				) ->							None:
  global _default_handler

  with _lock:
    if not _default_handler:
      return

    lowerCamelCase_       	=		_get_library_root_logger()
    library_root_logger.removeHandler(_default_handler					)
    library_root_logger.setLevel(logging.NOTSET					)
    lowerCamelCase_       	=		None







def 	lowerCamelCase__			(				) ->							int:
  return log_levels







def 	lowerCamelCase__			(				_lowerCamelCase			:						Optional[str] = None					) ->							logging.Logger:

  if name is None:
    lowerCamelCase_       	=		_get_library_name()

  _configure_library_root_logger()
  return logging.getLogger(_lowerCamelCase					)







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







def 	lowerCamelCase__			(				_lowerCamelCase			:						int					) ->							None:
  _configure_library_root_logger()
  _get_library_root_logger().setLevel(_lowerCamelCase					)







def 	lowerCamelCase__			(				) ->							Dict:
  return set_verbosity(_lowerCamelCase					)







def 	lowerCamelCase__			(				) ->							Optional[Any]:
  return set_verbosity(_lowerCamelCase					)







def 	lowerCamelCase__			(				) ->							Optional[int]:
  return set_verbosity(_lowerCamelCase					)







def 	lowerCamelCase__			(				) ->							str:
  return set_verbosity(_lowerCamelCase					)







def 	lowerCamelCase__			(				) ->							None:
  _configure_library_root_logger()

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







def 	lowerCamelCase__			(				) ->							None:
  _configure_library_root_logger()

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







def 	lowerCamelCase__			(				_lowerCamelCase			:						logging.Handler					) ->							None:
  _configure_library_root_logger()

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







def 	lowerCamelCase__			(				_lowerCamelCase			:						logging.Handler					) ->							None:
  _configure_library_root_logger()

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







def 	lowerCamelCase__			(				) ->							None:
  _configure_library_root_logger()
  lowerCamelCase_       	=		False







def 	lowerCamelCase__			(				) ->							None:
  _configure_library_root_logger()
  lowerCamelCase_       	=		True







def 	lowerCamelCase__			(				) ->							None:
  lowerCamelCase_       	=		_get_library_root_logger().handlers

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







def 	lowerCamelCase__			(				) ->							None:
  lowerCamelCase_       	=		_get_library_root_logger().handlers

  for handler in handlers:
    handler.setFormatter(_lowerCamelCase					)







def 	lowerCamelCase__			(				self			:						Union[str, Any]						,							*_lowerCamelCase			:						Dict						,							**_lowerCamelCase			:						List[Any]					) ->							List[str]:
  lowerCamelCase_       	=		os.getenv('TRANSFORMERS_NO_ADVISORY_WARNINGS'						,							_lowerCamelCase					)
  if no_advisory_warnings:
    return
  self.warning(*_lowerCamelCase						,							**_lowerCamelCase					)


_SCREAMING_SNAKE_CASE   :							Dict          =		warning_advice







@functools.lru_cache(_lowerCamelCase					)
def 	lowerCamelCase__			(				self			:						Tuple						,							*_lowerCamelCase			:						Dict						,							**_lowerCamelCase			:						Any					) ->							Tuple:
  self.warning(*_lowerCamelCase						,							**_lowerCamelCase					)


_SCREAMING_SNAKE_CASE   :							Any          =		warning_once






class 	a		:






    def __init__(			self	:				Dict			,		*__SCREAMING_SNAKE_CASE	:				Any			,		**__SCREAMING_SNAKE_CASE	:				Optional[Any]   )	->						List[Any]:  # pylint: disable=unused-argument
      lowerCamelCase_       	=		args[0] if args else None






    def __iter__(			self	:				Optional[Any]   )	->						Optional[Any]:
      return iter(self._iterator   )






    def __getattr__(			self	:				Dict			,		__SCREAMING_SNAKE_CASE	:				Optional[Any]   )	->						str:

      def empty_fn(*__SCREAMING_SNAKE_CASE	:				str			,		**__SCREAMING_SNAKE_CASE	:				Tuple   ):  # pylint: disable=unused-argument
        return

      return empty_fn






    def __enter__(			self	:				Optional[Any]   )	->						Optional[Any]:
      return self






    def __exit__(			self	:				List[Any]			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]			,		__SCREAMING_SNAKE_CASE	:				Optional[int]   )	->						Any:
      return






class 	a		:






    def __call__(			self	:				Optional[Any]			,		*__SCREAMING_SNAKE_CASE	:				Optional[int]			,		**__SCREAMING_SNAKE_CASE	:				List[str]   )	->						Optional[int]:
      if _tqdm_active:
        return tqdm_lib.tqdm(*__SCREAMING_SNAKE_CASE			,		**__SCREAMING_SNAKE_CASE   )
      else:
        return EmptyTqdm(*__SCREAMING_SNAKE_CASE			,		**__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				Tuple			,		*__SCREAMING_SNAKE_CASE	:				int			,		**__SCREAMING_SNAKE_CASE	:				Optional[int]   )	->						str:
      lowerCamelCase_       	=		None
      if _tqdm_active:
        return tqdm_lib.tqdm.set_lock(*__SCREAMING_SNAKE_CASE			,		**__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				int   )	->						List[Any]:
      if _tqdm_active:
        return tqdm_lib.tqdm.get_lock()
_SCREAMING_SNAKE_CASE   :							Any          =		_tqdm_cls()







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







def 	lowerCamelCase__			(				) ->							List[str]:
  global _tqdm_active
  lowerCamelCase_       	=		True
  hf_hub_utils.enable_progress_bars()







def 	lowerCamelCase__			(				) ->							Union[str, Any]:
  global _tqdm_active
  lowerCamelCase_       	=		False
  hf_hub_utils.disable_progress_bars()












"""simple docstring"""





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						):






    def   UpperCamelCase			(			self	:				int			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Any			,		__SCREAMING_SNAKE_CASE	:				Dict   )	->						str:
      self.assertEqual(len(__SCREAMING_SNAKE_CASE   )			,		len(__SCREAMING_SNAKE_CASE   )   )
      for a, b in zip(__SCREAMING_SNAKE_CASE			,		__SCREAMING_SNAKE_CASE   ):
        self.assertAlmostEqual(__SCREAMING_SNAKE_CASE			,		__SCREAMING_SNAKE_CASE			,		delta=__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				List[Any]   )	->						int:
      lowerCamelCase_       	=		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(__SCREAMING_SNAKE_CASE   ):
        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   UpperCamelCase			(			self	:				str   )	->						Any:
      lowerCamelCase_       	=		None
      ops.enable_eager_execution_internal()
      lowerCamelCase_       	=		tf.config.list_physical_devices('CPU'   )
      if len(__SCREAMING_SNAKE_CASE   ) == 1:
        tf.config.set_logical_device_configuration(
            physical_devices[0]			,		[tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]   )
      lowerCamelCase_       	=		tf.config.list_logical_devices(device_type='CPU'   )
      lowerCamelCase_       	=		tf.distribute.MirroredStrategy(devices=devices[:2]   )

      with strategy.scope():
        lowerCamelCase_       	=		GradientAccumulator()
        lowerCamelCase_       	=		tf.Variable([4.0, 3.0]   )
        lowerCamelCase_	,      lowerCamelCase_       	=		create_optimizer(5e-5			,		10			,		5   )
        lowerCamelCase_       	=		tf.Variable([0.0, 0.0]			,		trainable=__SCREAMING_SNAKE_CASE   )

      def accumulate_on_replica(__SCREAMING_SNAKE_CASE	:				Union[str, Any]   ):
        accumulator([gradient]   )

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

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

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

      def _check_local_values(__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				List[str]   ):
        lowerCamelCase_       	=		strategy.experimental_local_results(accumulator._gradients[0]   )
        self.assertListAlmostEqual(values[0].value()			,		__SCREAMING_SNAKE_CASE			,		tol=1e-2   )
        self.assertListAlmostEqual(values[1].value()			,		__SCREAMING_SNAKE_CASE			,		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]   )




