


from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel






@require_tf
class _lowerCamelCase:
       lowercase_					:      Tuple       					=     MBartConfig
       lowercase_					:      Optional[Any]       					=     {}
       lowercase_					:      List[Any]       					=     """gelu"""
       def __init__(   self, lowerCamelCase, lowerCamelCase=13, lowerCamelCase=7, lowerCamelCase=True, lowerCamelCase=False, lowerCamelCase=99, lowerCamelCase=32, lowerCamelCase=2, lowerCamelCase=4, lowerCamelCase=37, lowerCamelCase=0.1, lowerCamelCase=0.1, lowerCamelCase=20, lowerCamelCase=2, lowerCamelCase=1, lowerCamelCase=0, )			->  Optional[int]:




           """simple docstring"""

           _lowercase			:				str 						=						parent
           _lowercase			:				str 						=						batch_size
           _lowercase			:				Union[str, Any] 						=						seq_length
           _lowercase			:				Dict 						=						is_training
           _lowercase			:				str 						=						use_labels
           _lowercase			:				List[Any] 						=						vocab_size
           _lowercase			:				Dict 						=						hidden_size
           _lowercase			:				Tuple 						=						num_hidden_layers
           _lowercase			:				int 						=						num_attention_heads
           _lowercase			:				int 						=						intermediate_size
           _lowercase			:				Tuple 						=						hidden_dropout_prob
           _lowercase			:				int 						=						attention_probs_dropout_prob
           _lowercase			:				Union[str, Any] 						=						max_position_embeddings
           _lowercase			:				Optional[Any] 						=						eos_token_id
           _lowercase			:				Optional[int] 						=						pad_token_id
           _lowercase			:				Any 						=						bos_token_id
       def      UpperCamelCase	(   self)			->  Optional[Any]:




           """simple docstring"""

           _lowercase			:				Optional[Any] 						=						ids_tensor([self.batch_size, self.seq_length - 1], self.vocab_size)
           _lowercase			:				List[str] 						=						tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size), 1)
           _lowercase			:				Optional[int] 						=						tf.concat([input_ids, eos_tensor], axis=1)

           _lowercase			:				Optional[int] 						=						ids_tensor([self.batch_size, self.seq_length], self.vocab_size)

           _lowercase			:				Dict 						=						self.config_cls(
               vocab_size=self.vocab_size, d_model=self.hidden_size, encoder_layers=self.num_hidden_layers, decoder_layers=self.num_hidden_layers, encoder_attention_heads=self.num_attention_heads, decoder_attention_heads=self.num_attention_heads, encoder_ffn_dim=self.intermediate_size, decoder_ffn_dim=self.intermediate_size, dropout=self.hidden_dropout_prob, attention_dropout=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, eos_token_ids=[2], bos_token_id=self.bos_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.pad_token_id, **self.config_updates, )
           _lowercase			:				str 						=						prepare_mbart_inputs_dict(lowerCamelCase, lowerCamelCase, lowerCamelCase)
           return config, inputs_dict




       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase)			->  Any:




           """simple docstring"""

           _lowercase			:				str 						=						TFMBartModel(config=lowerCamelCase).get_decoder()
           _lowercase			:				Union[str, Any] 						=						inputs_dict['input_ids']

           _lowercase			:				str 						=						input_ids[:1, :]
           _lowercase			:				Optional[int] 						=						inputs_dict['attention_mask'][:1, :]
           _lowercase			:				Tuple 						=						inputs_dict['head_mask']
           _lowercase			:				Tuple 						=						1

           # first forward pass
           _lowercase			:				Union[str, Any] 						=						model(lowerCamelCase, attention_mask=lowerCamelCase, head_mask=lowerCamelCase, use_cache=lowerCamelCase)

           _lowercase						,							_lowercase			:				Union[str, Any] 						=						outputs.to_tuple()
           _lowercase			:				Optional[int] 						=						past_key_values[1]



def 							UpperCamelCase_( lowerCamelCase_				,					lowerCamelCase_				,					lowerCamelCase_				,					lowerCamelCase_=None				,					lowerCamelCase_=None				,					lowerCamelCase_=None				,					lowerCamelCase_=None				,					lowerCamelCase_=None				,					)       ->		Optional[Any]:
    if attention_mask is None:
        _lowercase			:				Dict 						=						tf.cast(tf.math.not_equal(lowerCamelCase_				,					config.pad_token_id					)				,					tf.inta					)
    if decoder_attention_mask is None:
        _lowercase			:				List[Any] 						=						tf.concat(
            [
                tf.ones(decoder_input_ids[:, :1].shape				,					dtype=tf.inta					),
                tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]				,					config.pad_token_id					)				,					tf.inta					),
            ]				,					axis=-1				,					)
    if head_mask is None:
        _lowercase			:				List[Any] 						=						tf.ones((config.encoder_layers, config.encoder_attention_heads)					)
    if decoder_head_mask is None:
        _lowercase			:				int 						=						tf.ones((config.decoder_layers, config.decoder_attention_heads)					)
    if cross_attn_head_mask is None:
        _lowercase			:				Tuple 						=						tf.ones((config.decoder_layers, config.decoder_attention_heads)					)
    return {
        "input_ids": input_ids,
        "decoder_input_ids": decoder_input_ids,
        "attention_mask": attention_mask,
        "decoder_attention_mask": decoder_attention_mask,
        "head_mask": head_mask,
        "decoder_head_mask": decoder_head_mask,
        "cross_attn_head_mask": cross_attn_head_mask,
    }






@require_tf
class _lowerCamelCase(		_a,					_a,					unittest.TestCase  ):
       lowercase_					:      Optional[int]       					=     (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
       lowercase_					:      Optional[Any]       					=     (TFMBartForConditionalGeneration,) if is_tf_available() else ()
       lowercase_					:      Optional[int]       					=     (
           {
               """conversational""": TFMBartForConditionalGeneration,
               """feature-extraction""": TFMBartModel,
               """summarization""": TFMBartForConditionalGeneration,
               """text2text-generation""": TFMBartForConditionalGeneration,
               """translation""": TFMBartForConditionalGeneration,
           }
           if is_tf_available()
           else {}
       )
       lowercase_					:      Optional[Any]       					=     True
       lowercase_					:      Union[str, Any]       					=     False
       lowercase_					:      Dict       					=     False
       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase)			->  List[Any]:




           """simple docstring"""

           if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
               # Exception encountered when calling layer '...'
               return True

           return False
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase			:				List[str] 						=						TFMBartModelTester(self)
           _lowercase			:				List[Any] 						=						ConfigTester(self, config_class=lowerCamelCase)
       def      UpperCamelCase	(   self)			->  Union[str, Any]:




           """simple docstring"""

           self.config_tester.run_common_tests()




       def      UpperCamelCase	(   self)			->  Tuple:




           """simple docstring"""

           _lowercase			:				Optional[int] 						=						self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.check_decoder_model_past_large_inputs(*lowerCamelCase)






@require_sentencepiece
@require_tokenizers
@require_tf
class _lowerCamelCase(		unittest.TestCase  ):
       lowercase_					:      Tuple       					=     [
           """ UN Chief Says There Is No Military Solution in Syria""",
       ]
       lowercase_					:      int       					=     [
           """Şeful ONU declară că nu există o soluţie militară în Siria""",
       ]
       lowercase_					:      Optional[Any]       					=     """facebook/mbart-large-en-ro"""
       @cached_property
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           return AutoTokenizer.from_pretrained(self.model_name)
       @cached_property
       def      UpperCamelCase	(   self)			->  List[Any]:




           """simple docstring"""

           _lowercase			:				Union[str, Any] 						=						TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name)
           return model
       def      UpperCamelCase	(   self, **lowerCamelCase)			->  List[str]:




           """simple docstring"""

           _lowercase			:				str 						=						self.translate_src_text(**lowerCamelCase)
           self.assertListEqual(self.expected_text, lowerCamelCase)
       def      UpperCamelCase	(   self, **lowerCamelCase)			->  List[str]:




           """simple docstring"""

           _lowercase			:				List[Any] 						=						self.tokenizer(self.src_text, **lowerCamelCase, return_tensors='tf')
           _lowercase			:				int 						=						self.model.generate(
               model_inputs.input_ids, attention_mask=model_inputs.attention_mask, num_beams=2)
           _lowercase			:				List[Any] 						=						self.tokenizer.batch_decode(lowerCamelCase, skip_special_tokens=lowerCamelCase)
           return generated_words




       @slow
       def      UpperCamelCase	(   self)			->  Optional[int]:




           """simple docstring"""

           self._assert_generated_batch_equal_expected()








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


a__		     =     get_tests_dir('''fixtures/dummy-config.json''')


class    SCREAMING_SNAKE_CASE_				(       unittest.TestCase   ):



	"""simple docstring"""


	def 					lowerCamelCase__     (					self		:						str	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[Any]             =      0






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







						"""simple docstring"""







						self.assertIsNotNone(transformers.models.auto.__spec__	)
						self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""	)	)






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	int             =      AutoConfig.from_pretrained("""bert-base-uncased"""	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






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







						"""simple docstring"""







						__UpperCamelCase					:	Dict             =      AutoConfig.from_pretrained(lowerCAmelCase	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






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







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      AutoConfig.from_pretrained(lowerCAmelCase	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Tuple	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	List[Any]             =      AutoConfig.for_model("""roberta"""	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






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







						"""simple docstring"""







						with tempfile.TemporaryDirectory() as tmp_dir:
											# This model name contains bert and roberta, but roberta ends up being picked.
											__UpperCamelCase					:	Union[str, Any]             =      os.path.join(lowerCAmelCase					,     """fake-roberta"""	)
											os.makedirs(lowerCAmelCase					,     exist_ok=lowerCAmelCase	)
											with open(os.path.join(lowerCAmelCase					,     """config.json"""	)					,     """w"""	) as f:
																f.write(json.dumps({}	)	)
											__UpperCamelCase					:	Dict             =      AutoConfig.from_pretrained(lowerCAmelCase	)
											self.assertEqual(type(lowerCAmelCase	)					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int	)	->       str:







						"""simple docstring"""







						try:
											AutoConfig.register("""custom"""					,     lowerCAmelCase	)
											# Wrong model type will raise an error
											with self.assertRaises(lowerCAmelCase	):
																AutoConfig.register("""model"""					,     lowerCAmelCase	)
											# Trying to register something existing in the Transformers library will raise an error
											with self.assertRaises(lowerCAmelCase	):
																AutoConfig.register("""bert"""					,     lowerCAmelCase	)

											# Now that the config is registered, it can be used as any other config with the auto-API
											__UpperCamelCase					:	Optional[Any]             =      CustomConfig()
											with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(lowerCAmelCase	)
																__UpperCamelCase					:	List[str]             =      AutoConfig.from_pretrained(lowerCAmelCase	)
																self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)

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






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







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     """bert-base is not a local folder and is not a valid model identifier"""	):
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""bert-base"""	)






	def 					lowerCamelCase__     (					self		:						Dict	)	->       Any:







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""	):
											__UpperCamelCase					:	Optional[Any]             =      AutoConfig.from_pretrained(lowerCAmelCase					,     revision="""aaaaaa"""	)






	def 					lowerCamelCase__     (					self		:						str	)	->       List[str]:







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     """hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""					,     ):
											__UpperCamelCase					:	Union[str, Any]             =      AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""	)






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







						"""simple docstring"""







						with self.assertRaises(lowerCAmelCase	):
											__UpperCamelCase					:	str             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""	)
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(lowerCAmelCase	):
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)

						__UpperCamelCase					:	List[str]             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
						self.assertEqual(config.__class__.__name__					,     """NewModelConfig"""	)

						# Test config can be reloaded.
						with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(lowerCAmelCase	)
											__UpperCamelCase					:	str             =      AutoConfig.from_pretrained(lowerCAmelCase					,     trust_remote_code=lowerCAmelCase	)
						self.assertEqual(reloaded_config.__class__.__name__					,     """NewModelConfig"""	)






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       str:







						"""simple docstring"""







						class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



							"""simple docstring"""


							__magic_name__   :       int      	=		'new-model'

						try:
											AutoConfig.register("""new-model"""					,     lowerCAmelCase	)
											# If remote code is not set, the default is to use local
											__UpperCamelCase					:	Tuple             =      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					:	Any             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfigLocal"""	)

											# If remote is enabled, we load from the Hub
											__UpperCamelCase					:	List[Any]             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfig"""	)

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






