






"""simple docstring"""
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     UpperCamelCase_ :
	__magic_name__			   =       MBartConfig
	__magic_name__			   =       {}
	__magic_name__			   =       '''gelu'''





	def __init__(   self      :				List[Any]			,			lowerCAmelCase_      :				Tuple			,			lowerCAmelCase_      :				List[Any]=13			,			lowerCAmelCase_      :				Tuple=7			,			lowerCAmelCase_      :				Optional[Any]=True			,			lowerCAmelCase_      :				int=False			,			lowerCAmelCase_      :				Any=99			,			lowerCAmelCase_      :				Union[str, Any]=32			,			lowerCAmelCase_      :				Any=2			,			lowerCAmelCase_      :				List[str]=4			,			lowerCAmelCase_      :				Optional[Any]=37			,			lowerCAmelCase_      :				List[Any]=0.1			,			lowerCAmelCase_      :				Union[str, Any]=0.1			,			lowerCAmelCase_      :				Optional[int]=20			,			lowerCAmelCase_      :				List[str]=2			,			lowerCAmelCase_      :				Optional[Any]=1			,			lowerCAmelCase_      :				Tuple=0			,			)		->    List[str]:
				UpperCAmelCase_  :				Optional[int]      =      parent
				UpperCAmelCase_  :				Optional[int]      =      batch_size
				UpperCAmelCase_  :				Any      =      seq_length
				UpperCAmelCase_  :				Any      =      is_training
				UpperCAmelCase_  :				int      =      use_labels
				UpperCAmelCase_  :				int      =      vocab_size
				UpperCAmelCase_  :				int      =      hidden_size
				UpperCAmelCase_  :				List[Any]      =      num_hidden_layers
				UpperCAmelCase_  :				List[Any]      =      num_attention_heads
				UpperCAmelCase_  :				int      =      intermediate_size
				UpperCAmelCase_  :				str      =      hidden_dropout_prob
				UpperCAmelCase_  :				Union[str, Any]      =      attention_probs_dropout_prob
				UpperCAmelCase_  :				Tuple      =      max_position_embeddings
				UpperCAmelCase_  :				List[Any]      =      eos_token_id
				UpperCAmelCase_  :				Any      =      pad_token_id
				UpperCAmelCase_  :				List[Any]      =      bos_token_id





	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[Any]		)		->    Any:
				UpperCAmelCase_  :				Any      =      ids_tensor([self.batch_size, self.seq_length - 1]			,			self.vocab_size		)
				UpperCAmelCase_  :				Dict      =      tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size		)			,			1		)
				UpperCAmelCase_  :				Tuple      =      tf.concat([input_ids, eos_tensor]			,			axis=1		)

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

				UpperCAmelCase_  :				List[Any]      =      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			,			)
				UpperCAmelCase_  :				str      =      prepare_mbart_inputs_dict(lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_		)
				return config, inputs_dict





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]			,			lowerCAmelCase_      :				Dict			,			lowerCAmelCase_      :				List[Any]		)		->    Optional[int]:
				UpperCAmelCase_  :				List[str]      =      TFMBartModel(config=lowerCAmelCase_		).get_decoder()
				UpperCAmelCase_  :				int      =      inputs_dict["input_ids"]

				UpperCAmelCase_  :				Dict      =      input_ids[:1, :]
				UpperCAmelCase_  :				Any      =      inputs_dict["attention_mask"][:1, :]
				UpperCAmelCase_  :				Dict      =      inputs_dict["head_mask"]
				UpperCAmelCase_  :				List[Any]      =      1

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

				UpperCAmelCase_  ,			UpperCAmelCase_  :				Optional[int]      =      outputs.to_tuple()
				UpperCAmelCase_  :				Dict      =      past_key_values[1]





def        snake_case   (     A__			,A__			,A__			,A__=None			,A__=None			,A__=None			,A__=None			,A__=None			,):
			if attention_mask is None:
						UpperCAmelCase_  :				Tuple      =      tf.cast(tf.math.not_equal(A__			,config.pad_token_id		)			,tf.inta		)
			if decoder_attention_mask is None:
						UpperCAmelCase_  :				str      =      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:
						UpperCAmelCase_  :				Any      =      tf.ones((config.encoder_layers, config.encoder_attention_heads)		)
			if decoder_head_mask is None:
						UpperCAmelCase_  :				List[Any]      =      tf.ones((config.decoder_layers, config.decoder_attention_heads)		)
			if cross_attn_head_mask is None:
						UpperCAmelCase_  :				int      =      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     UpperCamelCase_ (__A       ,		__A       ,		unittest.TestCase  ):
	__magic_name__			   =       (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
	__magic_name__			   =       (TFMBartForConditionalGeneration,) if is_tf_available() else ()
	__magic_name__			   =       (
	    {
	        '''conversational''': TFMBartForConditionalGeneration,
	        '''feature-extraction''': TFMBartModel,
	        '''summarization''': TFMBartForConditionalGeneration,
	        '''text2text-generation''': TFMBartForConditionalGeneration,
	        '''translation''': TFMBartForConditionalGeneration,
	    }
	    if is_tf_available()
	    else {}
	)
	__magic_name__			   =       True
	__magic_name__			   =       False
	__magic_name__			   =       False





	def   _SCREAMING_SNAKE_CASE   (   self      :				Tuple			,			lowerCAmelCase_      :				List[Any]			,			lowerCAmelCase_      :				int			,			lowerCAmelCase_      :				str			,			lowerCAmelCase_      :				List[str]			,			lowerCAmelCase_      :				List[str]		)		->    Dict:
				if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
							# Exception encountered when calling layer '...'
							return True

				return False





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]		)		->    Optional[Any]:
				UpperCAmelCase_  :				Union[str, Any]      =      TFMBartModelTester(self		)
				UpperCAmelCase_  :				Union[str, Any]      =      ConfigTester(self			,			config_class=lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[Any]		)		->    str:
				self.config_tester.run_common_tests()





	def   _SCREAMING_SNAKE_CASE   (   self      :				Tuple		)		->    Any:
				UpperCAmelCase_  :				str      =      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     UpperCamelCase_ (unittest.TestCase  ):
	__magic_name__			   =       [
	    ''' UN Chief Says There Is No Military Solution in Syria''',
	]
	__magic_name__			   =       [
	    '''Şeful ONU declară că nu există o soluţie militară în Siria''',
	]
	__magic_name__			   =       '''facebook/mbart-large-en-ro'''





	@cached_property
	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]		)		->    Union[str, Any]:
				return AutoTokenizer.from_pretrained(self.model_name		)





	@cached_property
	def   _SCREAMING_SNAKE_CASE   (   self      :				str		)		->    Dict:
				UpperCAmelCase_  :				List[str]      =      TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name		)
				return model





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]			,			**lowerCAmelCase_      :				List[Any]		)		->    str:
				UpperCAmelCase_  :				List[Any]      =      self.translate_src_text(**lowerCAmelCase_		)
				self.assertListEqual(self.expected_text			,			lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any			,			**lowerCAmelCase_      :				Dict		)		->    List[str]:
				UpperCAmelCase_  :				Tuple      =      self.tokenizer(self.src_text			,			**lowerCAmelCase_			,			return_tensors="tf"		)
				UpperCAmelCase_  :				Tuple      =      self.model.generate(
				    model_inputs.input_ids			,			attention_mask=model_inputs.attention_mask			,			num_beams=2		)
				UpperCAmelCase_  :				List[str]      =      self.tokenizer.batch_decode(lowerCAmelCase_			,			skip_special_tokens=lowerCAmelCase_		)
				return generated_words





	@slow
	def   _SCREAMING_SNAKE_CASE   (   self      :				int		)		->    Optional[Any]:
				self._assert_generated_batch_equal_expected()











"""simple docstring"""
import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs







class     UpperCamelCase_ (__A  ):





	def __init__(   self      :				List[Any]			,			lowerCAmelCase_      :				List[str]			,			lowerCAmelCase_      :				List[Any]=None			,			lowerCAmelCase_      :				List[str]=True			,			lowerCAmelCase_      :				List[Any]=None			,			**lowerCAmelCase_      :				Tuple		)		->    Dict:
				UpperCAmelCase_  :				Tuple      =      parent
				UpperCAmelCase_  :				Optional[int]      =      config_class
				UpperCAmelCase_  :				List[str]      =      has_text_modality
				UpperCAmelCase_  :				Tuple      =      kwargs
				UpperCAmelCase_  :				int      =      common_properties





	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    Union[str, Any]:
				UpperCAmelCase_  :				Optional[int]      =      self.config_class(**self.inputs_dict		)
				UpperCAmelCase_  :				int      =      (
				    ["hidden_size", "num_attention_heads", "num_hidden_layers"]
				    if self.common_properties is None
				    else self.common_properties
				)

				# Add common fields for text models
				if self.has_text_modality:
							common_properties.extend(["vocab_size"]		)

				# Test that config has the common properties as getters
				for prop in common_properties:
							self.parent.assertTrue(hasattr(lowerCAmelCase_			,			lowerCAmelCase_		)			,			msg=f"""`{prop}` does not exist"""		)

				# Test that config has the common properties as setter
				for idx, name in enumerate(lowerCAmelCase_		):
							try:
										setattr(lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_		)
										self.parent.assertEqual(
										    getattr(lowerCAmelCase_			,			lowerCAmelCase_		)			,			lowerCAmelCase_			,			msg=f"""`{name} value {idx} expected, but was {getattr(lowerCAmelCase_			,			lowerCAmelCase_		)}"""		)
							except NotImplementedError:
										# Some models might not be able to implement setters for common_properties
										# In that case, a NotImplementedError is raised
										pass

        # Test if config class can be called with Config(prop_name=..)
				for idx, name in enumerate(lowerCAmelCase_		):
							try:
										UpperCAmelCase_  :				Optional[Any]      =      self.config_class(**{name: idx}		)
										self.parent.assertEqual(
										    getattr(lowerCAmelCase_			,			lowerCAmelCase_		)			,			lowerCAmelCase_			,			msg=f"""`{name} value {idx} expected, but was {getattr(lowerCAmelCase_			,			lowerCAmelCase_		)}"""		)
							except NotImplementedError:
										# Some models might not be able to implement setters for common_properties
										# In that case, a NotImplementedError is raised
										pass





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]		)		->    Tuple:
				UpperCAmelCase_  :				str      =      self.config_class(**self.inputs_dict		)
				UpperCAmelCase_  :				List[str]      =      json.loads(config.to_json_string()		)
				for key, value in self.inputs_dict.items():
							self.parent.assertEqual(obj[key]			,			lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[Any]		)		->    Any:
				UpperCAmelCase_  :				Optional[Any]      =      self.config_class(**self.inputs_dict		)

				with tempfile.TemporaryDirectory() as tmpdirname:
							UpperCAmelCase_  :				List[str]      =      os.path.join(lowerCAmelCase_			,			"config.json"		)
							config_first.to_json_file(lowerCAmelCase_		)
							UpperCAmelCase_  :				Dict      =      self.config_class.from_json_file(lowerCAmelCase_		)

				self.parent.assertEqual(config_second.to_dict()			,			config_first.to_dict()		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]		)		->    Dict:
				UpperCAmelCase_  :				Any      =      self.config_class(**self.inputs_dict		)

				with tempfile.TemporaryDirectory() as tmpdirname:
							config_first.save_pretrained(lowerCAmelCase_		)
							UpperCAmelCase_  :				Tuple      =      self.config_class.from_pretrained(lowerCAmelCase_		)

				self.parent.assertEqual(config_second.to_dict()			,			config_first.to_dict()		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				int		)		->    Optional[Any]:
				UpperCAmelCase_  :				Any      =      self.config_class(**self.inputs_dict		)

				UpperCAmelCase_  :				int      =      "test"
				with tempfile.TemporaryDirectory() as tmpdirname:
							UpperCAmelCase_  :				Optional[int]      =      os.path.join(lowerCAmelCase_			,			lowerCAmelCase_		)
							config_first.save_pretrained(lowerCAmelCase_		)
							UpperCAmelCase_  :				Union[str, Any]      =      self.config_class.from_pretrained(lowerCAmelCase_			,			subfolder=lowerCAmelCase_		)

				self.parent.assertEqual(config_second.to_dict()			,			config_first.to_dict()		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[Any]		)		->    Tuple:
				UpperCAmelCase_  :				List[str]      =      self.config_class(**self.inputs_dict			,			num_labels=5		)
				self.parent.assertEqual(len(config.idalabel		)			,			5		)
				self.parent.assertEqual(len(config.labelaid		)			,			5		)

				UpperCAmelCase_  :				List[Any]      =      3
				self.parent.assertEqual(len(config.idalabel		)			,			3		)
				self.parent.assertEqual(len(config.labelaid		)			,			3		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Tuple		)		->    Optional[Any]:
				if self.config_class.is_composition:
							return
				UpperCAmelCase_  :				str      =      self.config_class()
				self.parent.assertIsNotNone(lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Dict		)		->    List[str]:
				UpperCAmelCase_  :				Optional[int]      =      copy.deepcopy(lowerCAmelCase_		)
				UpperCAmelCase_  :				Optional[int]      =      self.config_class(**lowerCAmelCase_		)
				UpperCAmelCase_  :				Optional[Any]      =      []
				for key, value in config_common_kwargs.items():
							if key == "torch_dtype":
										if not is_torch_available():
													continue
										else:
													import torch

													if config.torch_dtype != torch.floataa:
																wrong_values.append(("torch_dtype", config.torch_dtype, torch.floataa)		)
							elif getattr(lowerCAmelCase_			,			lowerCAmelCase_		) != value:
										wrong_values.append((key, getattr(lowerCAmelCase_			,			lowerCAmelCase_		), value)		)

				if len(lowerCAmelCase_		) > 0:
							UpperCAmelCase_  :				Any      =      "\n".join([f"""- {v[0]}: got {v[1]} instead of {v[2]}""" for v in wrong_values]		)
							raise ValueError(f"""The following keys were not properly set in the config:\n{errors}"""		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[Any]		)		->    str:
				self.create_and_test_config_common_properties()
				self.create_and_test_config_to_json_string()
				self.create_and_test_config_to_json_file()
				self.create_and_test_config_from_and_save_pretrained()
				self.create_and_test_config_from_and_save_pretrained_subfolder()
				self.create_and_test_config_with_num_labels()
				self.check_config_can_be_init_without_params()
				self.check_config_arguments_init()



