



"""simple docstring"""







import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import MBartConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow
from transformers.utils import cached_property

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
		import os

		# The slow tests are often failing with OOM error on GPU
		# This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
		# but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
		UpperCAmelCase:  int			     =				"""platform"""

		import jax
		import jax.numpy as jnp

		from transformers import AutoTokenizer
		from transformers.models.mbart.modeling_flax_mbart import (
		    FlaxMBartForConditionalGeneration,
		    FlaxMBartForQuestionAnswering,
		    FlaxMBartForSequenceClassification,
		    FlaxMBartModel,
		    shift_tokens_right,
		)


def  __SCREAMING_SNAKE_CASE (  __UpperCAmelCase   ,				__UpperCAmelCase   ,				__UpperCAmelCase=None   ,				__UpperCAmelCase=None   ,				__UpperCAmelCase=None   ,				__UpperCAmelCase=None   ,				__UpperCAmelCase=None   ,				__UpperCAmelCase=None   ,				):
		if attention_mask is None:
				_lowercase				: int     =   np.where(input_ids != config.pad_token_id   ,				1   ,				0      )
		if decoder_attention_mask is None:
				_lowercase				: Any     =   np.where(decoder_input_ids != config.pad_token_id   ,				1   ,				0      )
		if head_mask is None:
				_lowercase				: Tuple     =   np.ones((config.encoder_layers, config.encoder_attention_heads)      )
		if decoder_head_mask is None:
				_lowercase				: Tuple     =   np.ones((config.decoder_layers, config.decoder_attention_heads)      )
		if cross_attn_head_mask is None:
				_lowercase				: str     =   np.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,
		}





class   UpperCamelCase	(     unittest.TestCase       ):




	"""simple docstring"""






	def __init__(     self				,UpperCAmelCase_				,UpperCAmelCase_=13				,UpperCAmelCase_=7				,UpperCAmelCase_=True				,UpperCAmelCase_=False				,UpperCAmelCase_=99				,UpperCAmelCase_=16				,UpperCAmelCase_=2				,UpperCAmelCase_=4				,UpperCAmelCase_=4				,UpperCAmelCase_="gelu"				,UpperCAmelCase_=0.1				,UpperCAmelCase_=0.1				,UpperCAmelCase_=32				,UpperCAmelCase_=2				,UpperCAmelCase_=1				,UpperCAmelCase_=0				,UpperCAmelCase_=2				,UpperCAmelCase_=0.02				,):
			_lowercase				: Optional[int]     =   parent
			_lowercase				: Optional[Any]     =   batch_size
			_lowercase				: Optional[int]     =   seq_length
			_lowercase				: Tuple     =   is_training
			_lowercase				: Any     =   use_labels
			_lowercase				: str     =   vocab_size
			_lowercase				: Any     =   hidden_size
			_lowercase				: Dict     =   num_hidden_layers
			_lowercase				: Any     =   num_attention_heads
			_lowercase				: Optional[Any]     =   intermediate_size
			_lowercase				: Optional[int]     =   hidden_act
			_lowercase				: Optional[int]     =   hidden_dropout_prob
			_lowercase				: Optional[Any]     =   attention_probs_dropout_prob
			_lowercase				: Tuple     =   max_position_embeddings
			_lowercase				: int     =   eos_token_id
			_lowercase				: int     =   pad_token_id
			_lowercase				: Any     =   bos_token_id
			_lowercase				: List[str]     =   decoder_start_token_id
			_lowercase				: Tuple     =   initializer_range






	def    lowerCamelCase__					(     self   ):
			_lowercase				: str     =   np.clip(ids_tensor([self.batch_size, self.seq_length - 1]				,self.vocab_size   )				,3				,self.vocab_size   )
			_lowercase				: int     =   np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)				,dtype=np.intaa   ))				,-1   )

			_lowercase				: List[str]     =   shift_tokens_right(UpperCAmelCase_				,1   )

			_lowercase				: List[str]     =   MBartConfig(
			    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_id=self.eos_token_id				,bos_token_id=self.bos_token_id				,pad_token_id=self.pad_token_id				,decoder_start_token_id=self.decoder_start_token_id				,initializer_range=self.initializer_range				,use_cache=UpperCAmelCase_				,)
			_lowercase				: Tuple     =   prepare_mbart_inputs_dict(UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   )
			return config, inputs_dict






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase				: Optional[Any]     =   self.prepare_config_and_inputs()
			return config, inputs_dict






	def    lowerCamelCase__					(     self				,UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   ):
			_lowercase				: Optional[int]     =   20
			_lowercase				: Dict     =   model_class_name(UpperCAmelCase_   )

			_lowercase				: str     =   model.encode(inputs_dict["""input_ids"""]   )

			_lowercase     ,  _lowercase				: List[str]     =   (
			    inputs_dict["""decoder_input_ids"""],
			    inputs_dict["""decoder_attention_mask"""],
			)

			_lowercase				: Union[str, Any]     =   model.init_cache(decoder_input_ids.shape[0]				,UpperCAmelCase_				,UpperCAmelCase_   )
			_lowercase				: int     =   jnp.ones((decoder_input_ids.shape[0], max_decoder_length)				,dtype="""i4"""   )

			_lowercase				: Dict     =   jnp.broadcast_to(
			    jnp.arange(decoder_input_ids.shape[-1] - 1   )[None, :]				,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1)				,)
			_lowercase				: int     =   model.decode(
			    decoder_input_ids[:, :-1]				,UpperCAmelCase_				,decoder_attention_mask=UpperCAmelCase_				,past_key_values=UpperCAmelCase_				,decoder_position_ids=UpperCAmelCase_				,)

			_lowercase				: List[Any]     =   jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]]				,dtype="""i4"""   )
			_lowercase				: Optional[Any]     =   model.decode(
			    decoder_input_ids[:, -1:]				,UpperCAmelCase_				,decoder_attention_mask=UpperCAmelCase_				,past_key_values=outputs_cache.past_key_values				,decoder_position_ids=UpperCAmelCase_				,)

			_lowercase				: str     =   model.decode(UpperCAmelCase_				,UpperCAmelCase_   )

			_lowercase				: List[str]     =   np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])   )   )
			self.parent.assertTrue(diff < 1E-3				,msg=f"""Max diff is {diff}"""   )






	def    lowerCamelCase__					(     self				,UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   ):
			_lowercase				: Tuple     =   20
			_lowercase				: List[Any]     =   model_class_name(UpperCAmelCase_   )

			_lowercase				: Union[str, Any]     =   model.encode(inputs_dict["""input_ids"""]   )

			_lowercase     ,  _lowercase				: Any     =   (
			    inputs_dict["""decoder_input_ids"""],
			    inputs_dict["""decoder_attention_mask"""],
			)

			_lowercase				: List[str]     =   jnp.concatenate(
			    [
			        decoder_attention_mask,
			        jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1])   ),
			    ]				,axis=-1				,)

			_lowercase				: Union[str, Any]     =   model.init_cache(decoder_input_ids.shape[0]				,UpperCAmelCase_				,UpperCAmelCase_   )
			_lowercase				: List[str]     =   jnp.broadcast_to(
			    jnp.arange(decoder_input_ids.shape[-1] - 1   )[None, :]				,(decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1)				,)

			_lowercase				: List[Any]     =   model.decode(
			    decoder_input_ids[:, :-1]				,UpperCAmelCase_				,decoder_attention_mask=UpperCAmelCase_				,past_key_values=UpperCAmelCase_				,decoder_position_ids=UpperCAmelCase_				,)
			_lowercase				: List[Any]     =   jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]]				,dtype="""i4"""   )
			_lowercase				: Optional[Any]     =   model.decode(
			    decoder_input_ids[:, -1:]				,UpperCAmelCase_				,past_key_values=outputs_cache.past_key_values				,decoder_attention_mask=UpperCAmelCase_				,decoder_position_ids=UpperCAmelCase_				,)

			_lowercase				: Union[str, Any]     =   model.decode(UpperCAmelCase_				,UpperCAmelCase_				,decoder_attention_mask=UpperCAmelCase_   )

			_lowercase				: List[str]     =   np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])   )   )
			self.parent.assertTrue(diff < 1E-3				,msg=f"""Max diff is {diff}"""   )







@require_flax
class   UpperCamelCase	(     unittest.TestCase       ):




	"""simple docstring"""
	SCREAMING_SNAKE_CASE_							:       List[Any]						       =  9_9






	def    lowerCamelCase__					(     self   ):
			_lowercase				: List[str]     =   np.array(
			    [
			        [71, 82, 18, 33, 46, 91, 2],
			        [68, 34, 26, 58, 30, 82, 2],
			        [5, 97, 17, 39, 94, 40, 2],
			        [76, 83, 94, 25, 70, 78, 2],
			        [87, 59, 41, 35, 48, 66, 2],
			        [55, 13, 16, 58, 5, 2, 1],  # note padding
			        [64, 27, 31, 51, 12, 75, 2],
			        [52, 64, 86, 17, 83, 39, 2],
			        [48, 61, 9, 24, 71, 82, 2],
			        [26, 1, 60, 48, 22, 13, 2],
			        [21, 5, 62, 28, 14, 76, 2],
			        [45, 98, 37, 86, 59, 48, 2],
			        [70, 70, 50, 9, 28, 0, 2],
			    ]				,dtype=np.intaa				,)

			_lowercase				: List[str]     =   input_ids.shape[0]
			_lowercase				: List[Any]     =   MBartConfig(
			    vocab_size=self.vocab_size				,d_model=24				,encoder_layers=2				,decoder_layers=2				,encoder_attention_heads=2				,decoder_attention_heads=2				,encoder_ffn_dim=32				,decoder_ffn_dim=32				,max_position_embeddings=48				,eos_token_id=2				,pad_token_id=1				,bos_token_id=0				,)
			return config, input_ids, batch_size






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase     ,  _lowercase				: int     =   self._get_config_and_data()
			_lowercase				: Optional[Any]     =   FlaxMBartForSequenceClassification(UpperCAmelCase_   )
			_lowercase				: Any     =   model(input_ids=UpperCAmelCase_				,decoder_input_ids=UpperCAmelCase_   )
			_lowercase				: Any     =   (batch_size, config.num_labels)
			self.assertEqual(outputs["""logits"""].shape				,UpperCAmelCase_   )






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase     ,  _lowercase				: Optional[int]     =   self._get_config_and_data()
			_lowercase				: Tuple     =   FlaxMBartForQuestionAnswering(UpperCAmelCase_   )
			_lowercase				: Optional[int]     =   model(input_ids=UpperCAmelCase_   )

			self.assertEqual(outputs["""start_logits"""].shape				,input_ids.shape   )
			self.assertEqual(outputs["""end_logits"""].shape				,input_ids.shape   )






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase     ,  _lowercase				: Any     =   self._get_config_and_data()
			_lowercase				: List[str]     =   FlaxMBartForConditionalGeneration(UpperCAmelCase_   )
			_lowercase				: Dict     =   lm_model(input_ids=UpperCAmelCase_   )
			_lowercase				: Any     =   (batch_size, input_ids.shape[1], config.vocab_size)
			self.assertEqual(outputs["""logits"""].shape				,UpperCAmelCase_   )






	def    lowerCamelCase__					(     self   ):
			_lowercase				: List[str]     =   MBartConfig(
			    vocab_size=self.vocab_size				,d_model=14				,encoder_layers=2				,decoder_layers=2				,encoder_attention_heads=2				,decoder_attention_heads=2				,encoder_ffn_dim=8				,decoder_ffn_dim=8				,max_position_embeddings=48				,)
			_lowercase				: Optional[int]     =   FlaxMBartForConditionalGeneration(UpperCAmelCase_   )
			_lowercase				: str     =   np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]]				,dtype=np.intaa   )
			_lowercase				: Dict     =   np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]]				,dtype=np.intaa   )
			_lowercase				: str     =   lm_model(input_ids=UpperCAmelCase_				,decoder_input_ids=UpperCAmelCase_   )
			_lowercase				: Optional[int]     =   (*summary.shape, config.vocab_size)
			self.assertEqual(outputs["""logits"""].shape				,UpperCAmelCase_   )






	def    lowerCamelCase__					(     self   ):
			_lowercase				: Any     =   np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]]				,dtype=np.intaa   )
			_lowercase				: Any     =   shift_tokens_right(UpperCAmelCase_				,1   )
			_lowercase				: List[str]     =   np.equal(UpperCAmelCase_				,1   ).astype(np.floataa   ).sum()
			_lowercase				: Dict     =   np.equal(UpperCAmelCase_				,1   ).astype(np.floataa   ).sum()
			self.assertEqual(shifted.shape				,input_ids.shape   )
			self.assertEqual(UpperCAmelCase_				,n_pad_before - 1   )
			self.assertTrue(np.equal(shifted[:, 0]				,2   ).all()   )







@require_flax
class   UpperCamelCase	(     snake_case					,   unittest.TestCase					,   snake_case       ):




	"""simple docstring"""
	SCREAMING_SNAKE_CASE_							:       Dict						       =  True
	SCREAMING_SNAKE_CASE_							:       int						       =  (
	    (
	        FlaxMBartModel,
	        FlaxMBartForConditionalGeneration,
	        FlaxMBartForSequenceClassification,
	        FlaxMBartForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE_							:       Dict						       =  (FlaxMBartForConditionalGeneration,) if is_flax_available() else ()






	def    lowerCamelCase__					(     self   ):
			_lowercase				: Any     =   FlaxMBartModelTester(self   )






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase				: List[str]     =   self.model_tester.prepare_config_and_inputs()
			for model_class in self.all_model_classes:
					self.model_tester.check_use_cache_forward(UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   )






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase				: Optional[int]     =   self.model_tester.prepare_config_and_inputs()
			for model_class in self.all_model_classes:
					self.model_tester.check_use_cache_forward_with_attn_mask(UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   )






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase				: Dict     =   self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					with self.subTest(model_class.__name__   ):
							_lowercase				: Any     =   self._prepare_for_class(UpperCAmelCase_				,UpperCAmelCase_   )
							_lowercase				: Union[str, Any]     =   model_class(UpperCAmelCase_   )

							@jax.jit
							def encode_jitted(UpperCAmelCase_				,UpperCAmelCase_=None				,**UpperCAmelCase_   ):
									return model.encode(input_ids=UpperCAmelCase_				,attention_mask=UpperCAmelCase_   )

							with self.subTest("""JIT Enabled"""   ):
									_lowercase				: str     =   encode_jitted(**UpperCAmelCase_   ).to_tuple()

							with self.subTest("""JIT Disabled"""   ):
									with jax.disable_jit():
											_lowercase				: Optional[Any]     =   encode_jitted(**UpperCAmelCase_   ).to_tuple()

							self.assertEqual(len(UpperCAmelCase_   )				,len(UpperCAmelCase_   )   )
							for jitted_output, output in zip(UpperCAmelCase_				,UpperCAmelCase_   ):
									self.assertEqual(jitted_output.shape				,output.shape   )






	def    lowerCamelCase__					(     self   ):
			_lowercase     ,  _lowercase				: Any     =   self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					with self.subTest(model_class.__name__   ):
							_lowercase				: List[Any]     =   model_class(UpperCAmelCase_   )
							_lowercase				: List[Any]     =   model.encode(inputs_dict["""input_ids"""]				,inputs_dict["""attention_mask"""]   )

							_lowercase				: Optional[int]     =   {
							    """decoder_input_ids""": inputs_dict["""decoder_input_ids"""],
							    """decoder_attention_mask""": inputs_dict["""decoder_attention_mask"""],
							    """encoder_outputs""": encoder_outputs,
							}

							@jax.jit
							def decode_jitted(UpperCAmelCase_				,UpperCAmelCase_				,UpperCAmelCase_   ):
									return model.decode(
									    decoder_input_ids=UpperCAmelCase_				,decoder_attention_mask=UpperCAmelCase_				,encoder_outputs=UpperCAmelCase_				,)

							with self.subTest("""JIT Enabled"""   ):
									_lowercase				: List[Any]     =   decode_jitted(**UpperCAmelCase_   ).to_tuple()

							with self.subTest("""JIT Disabled"""   ):
									with jax.disable_jit():
											_lowercase				: int     =   decode_jitted(**UpperCAmelCase_   ).to_tuple()

							self.assertEqual(len(UpperCAmelCase_   )				,len(UpperCAmelCase_   )   )
							for jitted_output, output in zip(UpperCAmelCase_				,UpperCAmelCase_   ):
									self.assertEqual(jitted_output.shape				,output.shape   )






	@slow
	def    lowerCamelCase__					(     self   ):
			for model_class_name in self.all_model_classes:
					_lowercase				: Tuple     =   model_class_name.from_pretrained("""facebook/mbart-large-cc25"""				,from_pt=UpperCAmelCase_   )
					# FlaxMBartForSequenceClassification expects eos token in input_ids
					_lowercase				: Any     =   np.ones((1, 1)   ) * model.config.eos_token_id
					_lowercase				: Union[str, Any]     =   model(UpperCAmelCase_   )
					self.assertIsNotNone(UpperCAmelCase_   )







@require_flax
@require_sentencepiece
@require_tokenizers
class   UpperCamelCase	(     unittest.TestCase       ):




	"""simple docstring"""
	SCREAMING_SNAKE_CASE_							:       Union[str, Any]						       =  [
	    " UN Chief Says There Is No Military Solution in Syria",
	]
	SCREAMING_SNAKE_CASE_							:       List[Any]						       =  [
	    "Şeful ONU declară că nu există o soluţie militară în Siria",
	]
	SCREAMING_SNAKE_CASE_							:       List[Any]						       =  "facebook/mbart-large-en-ro"






	@cached_property
	def    lowerCamelCase__					(     self   ):
			return AutoTokenizer.from_pretrained(self.model_name   )






	@cached_property
	def    lowerCamelCase__					(     self   ):
			_lowercase				: Any     =   FlaxMBartForConditionalGeneration.from_pretrained(self.model_name				,from_pt=UpperCAmelCase_   )
			return model






	def    lowerCamelCase__					(     self				,**UpperCAmelCase_   ):
			_lowercase				: Optional[int]     =   self.translate_src_text(**UpperCAmelCase_   )
			self.assertListEqual(self.expected_text				,UpperCAmelCase_   )






	def    lowerCamelCase__					(     self				,**UpperCAmelCase_   ):
			_lowercase				: Tuple     =   self.tokenizer(self.src_text				,**UpperCAmelCase_				,return_tensors="""np"""   )
			_lowercase				: Optional[Any]     =   self.model.generate(
			    model_inputs.input_ids				,attention_mask=model_inputs.attention_mask				,decoder_start_token_id=self.tokenizer.lang_code_to_id["""ro_RO"""]				,early_stopping=UpperCAmelCase_				,num_beams=2				,).sequences
			_lowercase				: Optional[Any]     =   self.tokenizer.batch_decode(UpperCAmelCase_				,skip_special_tokens=UpperCAmelCase_   )
			return generated_words






	@slow
	def    lowerCamelCase__					(     self   ):
			self._assert_generated_batch_equal_expected()








from __future__ import annotations

import copy
import os
import tempfile
from importlib import import_module
from math import isnan

from transformers import is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import _tf_gpu_memory_limit, require_tf, slow

from ..test_modeling_tf_common import ids_tensor


if is_tf_available():
     import numpy as np
     import tensorflow as tf

     from transformers import (
         TF_MODEL_FOR_CAUSAL_LM_MAPPING,
         TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
         TF_MODEL_FOR_MASKED_LM_MAPPING,
         TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
         TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING,
         TF_MODEL_FOR_PRETRAINING_MAPPING,
         TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
         TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
         TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
         TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
         TFSharedEmbeddings,
     )

     if _tf_gpu_memory_limit is not None:
          A_		:  List[Any]										=  tf.config.list_physical_devices('''GPU''')
          for gpu in gpus:
               # Restrict TensorFlow to only allocate x GB of memory on the GPUs
               try:
                    tf.config.set_logical_device_configuration(
                        gpu, [tf.config.LogicalDeviceConfiguration(memory_limit=_tf_gpu_memory_limit)]
                    )
                    A_		:  List[Any]										=  tf.config.list_logical_devices('''GPU''')
                    print('''Logical GPUs''', logical_gpus)
               except RuntimeError as e:
                    # Virtual devices must be set before GPUs have been initialized
                    print(e)



@require_tf
class 		_lowercase		:
   _UpperCAmelCase  						=      None
   _UpperCAmelCase  						=      ()
   _UpperCAmelCase  						=      ()
   _UpperCAmelCase  						=      True
   _UpperCAmelCase  						=      True
   _UpperCAmelCase  						=      True
   _UpperCAmelCase  						=      False

   def 		A				(   self      :							Dict				, __lowerCAmelCase      :							List[str]				, __lowerCAmelCase      :							List[Any]				, __lowerCAmelCase      :							Union[str, Any]=False				)							->					dict:


    """simple docstring"""






    a							=		copy.deepcopy(__lowerCAmelCase				)

    if model_class in get_values(__lowerCAmelCase				):
     a							=		{
         k: tf.tile(tf.expand_dims(__lowerCAmelCase				, 1				)				, (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)				)
         if isinstance(__lowerCAmelCase				, tf.Tensor				) and v.ndim > 0
         else v
         for k, v in inputs_dict.items()
     }

    if return_labels:
     if model_class in get_values(__lowerCAmelCase				):
      a							=		tf.ones(self.model_tester.batch_size				, dtype=tf.intaa				)
     elif model_class in get_values(__lowerCAmelCase				):
      a							=		tf.zeros(self.model_tester.batch_size				, dtype=tf.intaa				)
      a							=		tf.zeros(self.model_tester.batch_size				, dtype=tf.intaa				)
     elif model_class in [
         *get_values(__lowerCAmelCase				),
         *get_values(__lowerCAmelCase				),
     ]:
      a							=		tf.zeros(self.model_tester.batch_size				, dtype=tf.intaa				)
     elif model_class in get_values(__lowerCAmelCase				):
      a							=		tf.zeros(self.model_tester.batch_size				, dtype=tf.intaa				)
     elif model_class in [
         *get_values(__lowerCAmelCase				),
         *get_values(__lowerCAmelCase				),
         *get_values(__lowerCAmelCase				),
         *get_values(__lowerCAmelCase				),
         *get_values(__lowerCAmelCase				),
     ]:
      a							=		tf.zeros(
          (self.model_tester.batch_size, self.model_tester.seq_length)				, dtype=tf.intaa				)
    return inputs_dict

   @slow
   def 		A				(   self      :							List[str]				)							->					str:


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
    for model_class in self.all_model_classes[:2]:
     a							=		self._prepare_for_class(__lowerCAmelCase				, __lowerCAmelCase				)
     a							=		model_class(__lowerCAmelCase				)

     @tf.function
     def run_in_graph_mode():
      return model(__lowerCAmelCase				)

     a							=		run_in_graph_mode()
     self.assertIsNotNone(__lowerCAmelCase				)

   @slow
   def 		A				(   self      :							Optional[int]				)							->					Union[str, Any]:


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
    for model_class in self.all_model_classes[:2]:
     a							=		self._prepare_for_class(__lowerCAmelCase				, __lowerCAmelCase				)
     a							=		model_class(__lowerCAmelCase				)

     @tf.function(experimental_compile=__lowerCAmelCase				)
     def run_in_graph_mode():
      return model(__lowerCAmelCase				)

     a							=		run_in_graph_mode()
     self.assertIsNotNone(__lowerCAmelCase				)

   @slow
   def 		A				(   self      :							str				)							->					List[Any]:


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
    for model_class in self.all_model_classes[:2]:
     a							=		model_class(__lowerCAmelCase				)
     if getattr(__lowerCAmelCase				, "hf_compute_loss"				, __lowerCAmelCase				):
      # Test that model correctly compute the loss with kwargs
      a							=		self._prepare_for_class(inputs_dict.copy()				, __lowerCAmelCase				, return_labels=__lowerCAmelCase				)
      # Is there a better way to remove these decoder inputs?
      a							=		{
          key: val
          for key, val in prepared_for_class.items()
          if key not in ("head_mask", "decoder_head_mask", "cross_attn_head_mask", "decoder_input_ids")
      }

      a							=		{
          "labels",
          "label",
          "label_ids",
          "start_positions",
          "start_position",
          "end_positions",
          "end_position",
          "next_sentence_label",
      }
      a							=		possible_label_cols.intersection(set(__lowerCAmelCase				)				)
      self.assertGreater(len(__lowerCAmelCase				)				, 0				, msg="No matching label names found!"				)
      a							=		{key: val for key, val in prepared_for_class.items() if key in label_names}
      a							=		{key: val for key, val in prepared_for_class.items() if key not in label_names}
      self.assertGreater(len(__lowerCAmelCase				)				, 0				)

      # Make sure it works with XLA!
      model.compile(optimizer=tf.keras.optimizers.SGD(0.0				)				, jit_compile=__lowerCAmelCase				)
      # Make sure the model fits without crashing regardless of where we pass the labels
      a							=		model.fit(
          __lowerCAmelCase				, validation_data=__lowerCAmelCase				, steps_per_epoch=1				, validation_steps=1				, shuffle=__lowerCAmelCase				, verbose=0				, )
      a							=		history.history["loss"][0]
      self.assertTrue(not isnan(__lowerCAmelCase				)				)
      a							=		history.history["val_loss"][0]
      self.assertTrue(not isnan(__lowerCAmelCase				)				)

      # Now test it with separate labels, to make sure that path works in XLA too.
      a							=		model_class(__lowerCAmelCase				)
      model.compile(optimizer=tf.keras.optimizers.SGD(0.0				)				, jit_compile=__lowerCAmelCase				)
      a							=		model.fit(
          __lowerCAmelCase				, __lowerCAmelCase				, validation_data=(inputs_minus_labels, labels)				, steps_per_epoch=1				, validation_steps=1				, shuffle=__lowerCAmelCase				, verbose=0				, )

      a							=		history.history["loss"][0]
      self.assertTrue(not isnan(__lowerCAmelCase				)				)
      a							=		history.history["val_loss"][0]
      self.assertTrue(not isnan(__lowerCAmelCase				)				)

   @slow
   def 		A				(   self      :							int				)							->					List[str]:


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
    a							=		True
    a							=		True

    if hasattr(__lowerCAmelCase				, "use_cache"				):
     a							=		True

    a							=		getattr(self.model_tester				, "encoder_seq_length"				, self.model_tester.seq_length				)
    a							=		getattr(self.model_tester				, "key_length"				, __lowerCAmelCase				)

    for model_class in self.all_model_classes[:2]:
     a							=		self._prepare_for_class(__lowerCAmelCase				, __lowerCAmelCase				)
     a							=		model_class(__lowerCAmelCase				)
     model.build()
     a							=		len(model(__lowerCAmelCase				)				)

     for key in list(class_inputs_dict.keys()				):
      # Remove keys not in the serving signature, as the SavedModel will not be compiled to deal with them
      if key not in model.input_signature:
       del class_inputs_dict[key]
      # Check it's a tensor, in case the inputs dict has some bools in it too
      elif isinstance(class_inputs_dict[key]				, tf.Tensor				) and class_inputs_dict[key].dtype.is_integer:
       a							=		tf.cast(class_inputs_dict[key]				, tf.intaa				)

     if set(class_inputs_dict.keys()				) != set(model.input_signature.keys()				):
      continue  # Some models have inputs that the preparation functions don't create, we skip those

     with tempfile.TemporaryDirectory() as tmpdirname:
      model.save_pretrained(__lowerCAmelCase				, saved_model=__lowerCAmelCase				)
      a							=		os.path.join(__lowerCAmelCase				, "saved_model"				, "1"				)
      a							=		tf.keras.models.load_model(__lowerCAmelCase				)
      a							=		model(__lowerCAmelCase				)

      if self.is_encoder_decoder:
       a							=		outputs["encoder_hidden_states"]
       a							=		outputs["encoder_attentions"]
      else:
       a							=		outputs["hidden_states"]
       a							=		outputs["attentions"]

      self.assertEqual(len(__lowerCAmelCase				)				, __lowerCAmelCase				)

      a							=		getattr(
          self.model_tester				, "expected_num_hidden_layers"				, self.model_tester.num_hidden_layers + 1				)

      self.assertEqual(len(__lowerCAmelCase				)				, __lowerCAmelCase				)
      self.assertListEqual(
          list(output_hidden_states[0].shape[-2:]				)				, [self.model_tester.seq_length, self.model_tester.hidden_size]				, )

      self.assertEqual(len(__lowerCAmelCase				)				, self.model_tester.num_hidden_layers				)
      self.assertListEqual(
          list(output_attentions[0].shape[-3:]				)				, [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]				, )

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


    """simple docstring"""






    tf.keras.mixed_precision.set_global_policy("mixed_float16"				)

    # try/finally block to ensure subsequent tests run in float32
    try:
     a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
     for model_class in self.all_model_classes[:2]:
      a							=		self._prepare_for_class(__lowerCAmelCase				, __lowerCAmelCase				)
      a							=		model_class(__lowerCAmelCase				)
      a							=		model(__lowerCAmelCase				)

      self.assertIsNotNone(__lowerCAmelCase				)
    finally:
     tf.keras.mixed_precision.set_global_policy("float32"				)

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


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()
    # head_mask and decoder_head_mask has different shapes than other input args
    if "head_mask" in inputs_dict:
     del inputs_dict["head_mask"]
    if "decoder_head_mask" in inputs_dict:
     del inputs_dict["decoder_head_mask"]
    if "cross_attn_head_mask" in inputs_dict:
     del inputs_dict["cross_attn_head_mask"]
    a							=		{
        module_member
        for model_class in self.all_model_classes
        for module in (import_module(model_class.__module__				),)
        for module_member_name in dir(__lowerCAmelCase				)
        if module_member_name.endswith("MainLayer"				)
        for module_member in (getattr(__lowerCAmelCase				, __lowerCAmelCase				),)
        if isinstance(__lowerCAmelCase				, __lowerCAmelCase				)
        and tf.keras.layers.Layer in module_member.__bases__
        and getattr(__lowerCAmelCase				, "_keras_serializable"				, __lowerCAmelCase				)
    }

    for main_layer_class in tf_main_layer_classes:
     # T5MainLayer needs an embed_tokens parameter when called without the inputs_embeds parameter
     if "T5" in main_layer_class.__name__:
      # Take the same values than in TFT5ModelTester for this shared layer
      a							=		TFSharedEmbeddings(self.model_tester.vocab_size				, self.model_tester.hidden_size				, name="shared"				)
      a							=		False
      a							=		main_layer_class(__lowerCAmelCase				, embed_tokens=__lowerCAmelCase				)
     else:
      a							=		main_layer_class(__lowerCAmelCase				)

     a							=		{
         name: tf.keras.Input(tensor.shape[1:]				, dtype=tensor.dtype				) for name, tensor in inputs_dict.items()
     }

     if hasattr(self.model_tester				, "num_labels"				):
      a							=		self.model_tester.num_labels
     else:
      a							=		2

     a							=		tf.data.Dataset.from_tensor_slices(
         (inputs_dict, np.ones((self.model_tester.batch_size, self.model_tester.seq_length, num_labels, 1)				))				).batch(1				)

     a							=		main_layer(__lowerCAmelCase				)[0]
     a							=		tf.keras.layers.Dense(__lowerCAmelCase				, activation="softmax"				, name="outputs"				)(__lowerCAmelCase				)
     a							=		tf.keras.models.Model(inputs=__lowerCAmelCase				, outputs=[outputs]				)

     model.compile(loss="binary_crossentropy"				, optimizer="adam"				, metrics=["binary_accuracy"]				)
     model.fit(__lowerCAmelCase				, epochs=1				)

     with tempfile.TemporaryDirectory() as tmpdirname:
      a							=		os.path.join(__lowerCAmelCase				, "keras_model.h5"				)
      model.save(__lowerCAmelCase				)
      if "T5" in main_layer_class.__name__:
       a							=		tf.keras.models.load_model(
           __lowerCAmelCase				, custom_objects={
               main_layer_class.__name__: main_layer_class,
               "TFSharedEmbeddings": TFSharedEmbeddings,
           }				, )
      else:
       a							=		tf.keras.models.load_model(
           __lowerCAmelCase				, custom_objects={main_layer_class.__name__: main_layer_class}				)
      assert isinstance(__lowerCAmelCase				, tf.keras.Model				)
      model(__lowerCAmelCase				)

   @slow
   def 		A				(   self      :							Any				)							->					Union[str, Any]:


    """simple docstring"""






    a		,	a							=		self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes[:2]:
     a							=		model_class(__lowerCAmelCase				)

     a							=		copy.deepcopy(__lowerCAmelCase				)

     if not self.is_encoder_decoder:
      a							=		inputs["input_ids"]
      del inputs["input_ids"]
     else:
      a							=		inputs["input_ids"]
      a							=		inputs.get("decoder_input_ids"				, __lowerCAmelCase				)
      del inputs["input_ids"]
      inputs.pop("decoder_input_ids"				, __lowerCAmelCase				)

     if not self.is_encoder_decoder:
      a							=		model.get_input_embeddings()(__lowerCAmelCase				)
     else:
      a							=		model.get_input_embeddings()(__lowerCAmelCase				)
      a							=		model.get_input_embeddings()(__lowerCAmelCase				)

     a							=		self._prepare_for_class(__lowerCAmelCase				, __lowerCAmelCase				)

     @tf.function
     def run_in_graph_mode():
      return model(__lowerCAmelCase				)

     a							=		run_in_graph_mode()
     self.assertIsNotNone(__lowerCAmelCase				)

   def 		A				(   self      :							Union[str, Any]				, __lowerCAmelCase      :							Dict				, __lowerCAmelCase      :							List[str]				)							->					List[str]:


    """simple docstring"""






    a							=		[]
    if model.config.bos_token_id is not None:
     special_tokens.append(model.config.bos_token_id				)
    if model.config.pad_token_id is not None:
     special_tokens.append(model.config.pad_token_id				)
    if model.config.eos_token_id is not None:
     special_tokens.append(model.config.eos_token_id				)

    # create random bad tokens that are not special tokens
    a							=		[]
    while len(__lowerCAmelCase				) < num_bad_tokens:
     a							=		tf.squeeze(ids_tensor((1, 1)				, self.model_tester.vocab_size				)				, 0				).numpy()[0]
     if token not in special_tokens:
      bad_tokens.append(__lowerCAmelCase				)
    return bad_tokens

   def 		A				(   self      :							int				, __lowerCAmelCase      :							int				)							->					int:


    """simple docstring"""






    for token_id in output_ids[0].numpy().tolist():
     self.assertGreaterEqual(__lowerCAmelCase				, 0				)
     self.assertLess(__lowerCAmelCase				, self.model_tester.vocab_size				)




   def 		A				(   self      :							Optional[Any]				, __lowerCAmelCase      :							List[str]				, __lowerCAmelCase      :							Any				)							->					int:


    """simple docstring"""






    for bad_word_ids in bad_words_ids:
     # for all slices in batch
     for generated_ids_slice in generated_ids:
      # for all word idx
      for i in range(len(__lowerCAmelCase				)				, len(__lowerCAmelCase				)				):
       # if tokens match
       if generated_ids_slice[i - len(__lowerCAmelCase				) : i] == bad_word_ids:
        return True
    return False

