



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



      """simple docstring"""






      def      UpperCAmelCase__					( self			:List[str]       ,  lowercase_			:str       ,  lowercase_			:List[Any]       ,  lowercase_			:int )    ->      Union[str, Any]:
            self.assertEqual(len(lowercase_ )       ,  len(lowercase_ ) )
            for a, b in zip(lowercase_       ,  lowercase_ ):
                  self.assertAlmostEqual(lowercase_       ,  lowercase_       ,  delta=lowercase_ )






      def      UpperCAmelCase__					( self			:Union[str, Any] )    ->      int:
            UpperCAmelCase					=			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      UpperCAmelCase__					( self			:Union[str, Any] )    ->      List[Any]:
            UpperCAmelCase					=			None
            ops.enable_eager_execution_internal()
            UpperCAmelCase					=			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()] )
            UpperCAmelCase					=			tf.config.list_logical_devices(device_type='CPU' )
            UpperCAmelCase					=			tf.distribute.MirroredStrategy(devices=devices[:2] )

            with strategy.scope():
                  UpperCAmelCase					=			GradientAccumulator()
                  UpperCAmelCase					=			tf.Variable([4.0, 3.0] )
                  UpperCAmelCase				,   UpperCAmelCase					=			create_optimizer(5E-5       ,  10       ,  5 )
                  UpperCAmelCase					=			tf.Variable([0.0, 0.0]       ,  trainable=lowercase_ )

            def accumulate_on_replica(lowercase_			:Optional[Any] ):
                  accumulator([gradient] )

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

            @tf.function
            def accumulate(lowercase_			:int       ,  lowercase_			:int ):
                  with strategy.scope():
                        UpperCAmelCase					=			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_			:Optional[int]       ,  lowercase_			:List[str] ):
                  UpperCAmelCase					=			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] )










"""simple docstring"""


import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402


snake_case_    					=	get_tests_dir("""fixtures/dummy-config.json""")







class 				A_      (					unittest.TestCase    ):



						"""simple docstring"""






						def      UpperCAmelCase__					( self			:int )    ->      Optional[Any]:
												UpperCAmelCase					=			0






						def      UpperCAmelCase__					( self			:List[str] )    ->      str:
												self.assertIsNotNone(transformers.models.auto.__spec__ )
												self.assertIsNotNone(importlib.util.find_spec('transformers.models.auto' ) )






						def      UpperCAmelCase__					( self			:List[Any] )    ->      List[str]:
												UpperCAmelCase					=			AutoConfig.from_pretrained('bert-base-uncased' )
												self.assertIsInstance(lowercase_       ,  lowercase_ )






						def      UpperCAmelCase__					( self			:Optional[int] )    ->      int:
												UpperCAmelCase					=			AutoConfig.from_pretrained(lowercase_ )
												self.assertIsInstance(lowercase_       ,  lowercase_ )






						def      UpperCAmelCase__					( self			:int )    ->      Any:
												UpperCAmelCase					=			AutoConfig.from_pretrained(lowercase_ )
												self.assertIsInstance(lowercase_       ,  lowercase_ )






						def      UpperCAmelCase__					( self			:Optional[Any] )    ->      List[str]:
												UpperCAmelCase					=			AutoConfig.for_model('roberta' )
												self.assertIsInstance(lowercase_       ,  lowercase_ )






						def      UpperCAmelCase__					( self			:str )    ->      List[str]:
												with tempfile.TemporaryDirectory() as tmp_dir:
																		# This model name contains bert and roberta, but roberta ends up being picked.
																		UpperCAmelCase					=			os.path.join(lowercase_       ,  'fake-roberta' )
																		os.makedirs(lowercase_       ,  exist_ok=lowercase_ )
																		with open(os.path.join(lowercase_       ,  'config.json' )       ,  'w' ) as f:
																								f.write(json.dumps({} ) )
																		UpperCAmelCase					=			AutoConfig.from_pretrained(lowercase_ )
																		self.assertEqual(type(lowercase_ )       ,  lowercase_ )






						def      UpperCAmelCase__					( self			:int )    ->      Union[str, Any]:
												try:
																		AutoConfig.register('custom'       ,  lowercase_ )
																		# Wrong model type will raise an error
																		with self.assertRaises(lowercase_ ):
																								AutoConfig.register('model'       ,  lowercase_ )
																		# Trying to register something existing in the Transformers library will raise an error
																		with self.assertRaises(lowercase_ ):
																								AutoConfig.register('bert'       ,  lowercase_ )

																		# Now that the config is registered, it can be used as any other config with the auto-API
																		UpperCAmelCase					=			CustomConfig()
																		with tempfile.TemporaryDirectory() as tmp_dir:
																								config.save_pretrained(lowercase_ )
																								UpperCAmelCase					=			AutoConfig.from_pretrained(lowercase_ )
																								self.assertIsInstance(lowercase_       ,  lowercase_ )

												finally:
																		if "custom" in CONFIG_MAPPING._extra_content:
																								del CONFIG_MAPPING._extra_content["custom"]






						def      UpperCAmelCase__					( self			:str )    ->      Dict:
												with self.assertRaisesRegex(
												    lowercase_       ,  'bert-base is not a local folder and is not a valid model identifier' ):
																		UpperCAmelCase					=			AutoConfig.from_pretrained('bert-base' )






						def      UpperCAmelCase__					( self			:List[Any] )    ->      Optional[int]:
												with self.assertRaisesRegex(
												    lowercase_       ,  R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ):
																		UpperCAmelCase					=			AutoConfig.from_pretrained(lowercase_       ,  revision='aaaaaa' )






						def      UpperCAmelCase__					( self			:List[str] )    ->      str:
												with self.assertRaisesRegex(
												    lowercase_       ,  'hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'       ,  ):
																		UpperCAmelCase					=			AutoConfig.from_pretrained('hf-internal-testing/no-config-test-repo' )






						def      UpperCAmelCase__					( self			:str )    ->      int:
												# If remote code is not set, we will time out when asking whether to load the model.
												with self.assertRaises(lowercase_ ):
																		UpperCAmelCase					=			AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' )
												# If remote code is disabled, we can't load this config.
												with self.assertRaises(lowercase_ ):
																		UpperCAmelCase					=			AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'       ,  trust_remote_code=lowercase_ )

												UpperCAmelCase					=			AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'       ,  trust_remote_code=lowercase_ )
												self.assertEqual(config.__class__.__name__       ,  'NewModelConfig' )

												# Test config can be reloaded.
												with tempfile.TemporaryDirectory() as tmp_dir:
																		config.save_pretrained(lowercase_ )
																		UpperCAmelCase					=			AutoConfig.from_pretrained(lowercase_       ,  trust_remote_code=lowercase_ )
												self.assertEqual(reloaded_config.__class__.__name__       ,  'NewModelConfig' )






						def      UpperCAmelCase__					( self			:List[Any] )    ->      Optional[int]:







												class 				A_      (					SCREAMING_SNAKE_CASE_    ):



																		"""simple docstring"""
																		__UpperCamelCase		    =							"""new-model"""

												try:
																		AutoConfig.register('new-model'       ,  lowercase_ )
																		# If remote code is not set, the default is to use local
																		UpperCAmelCase					=			AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' )
																		self.assertEqual(config.__class__.__name__       ,  'NewModelConfigLocal' )

																		# If remote code is disabled, we load the local one.
																		UpperCAmelCase					=			AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'       ,  trust_remote_code=lowercase_ )
																		self.assertEqual(config.__class__.__name__       ,  'NewModelConfigLocal' )

																		# If remote is enabled, we load from the Hub
																		UpperCAmelCase					=			AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'       ,  trust_remote_code=lowercase_ )
																		self.assertEqual(config.__class__.__name__       ,  'NewModelConfig' )

												finally:
																		if "new-model" in CONFIG_MAPPING._extra_content:
																								del CONFIG_MAPPING._extra_content["new-model"]





