


from __future__ import annotations

import unittest
import warnings

from transformers import AutoTokenizer, MarianConfig, MarianTokenizer, TranslationPipeline, 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, TFMarianModel, TFMarianMTModel







@require_tf
class     lowerCamelCase__		:






			"""simple docstring"""






			_UpperCamelCase						:   List[str]					=						MarianConfig
			_UpperCamelCase						:   List[str]					=						{}
			_UpperCamelCase						:   Union[str, Any]					=						'gelu'




			def __init__(							self ,  snake_case ,  snake_case=13 ,  snake_case=7 ,  snake_case=True ,  snake_case=False ,  snake_case=99 ,  snake_case=32 ,  snake_case=2 ,  snake_case=4 ,  snake_case=37 ,  snake_case=0.1 ,  snake_case=0.1 ,  snake_case=20 ,  snake_case=2 ,  snake_case=1 ,  snake_case=0 ,  ):

						'''simple docstring'''


						UpperCamelCase__   =    parent
						UpperCamelCase__   =    batch_size
						UpperCamelCase__   =    seq_length
						UpperCamelCase__   =    is_training
						UpperCamelCase__   =    use_labels
						UpperCamelCase__   =    vocab_size
						UpperCamelCase__   =    hidden_size
						UpperCamelCase__   =    num_hidden_layers
						UpperCamelCase__   =    num_attention_heads
						UpperCamelCase__   =    intermediate_size

						UpperCamelCase__   =    hidden_dropout_prob
						UpperCamelCase__   =    attention_probs_dropout_prob
						UpperCamelCase__   =    max_position_embeddings
						UpperCamelCase__   =    eos_token_id
						UpperCamelCase__   =    pad_token_id
						UpperCamelCase__   =    bos_token_id




			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    ids_tensor([self.batch_size, self.seq_length - 1] ,  self.vocab_size       )
						UpperCamelCase__   =    tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size       ) ,  1       )
						UpperCamelCase__   =    tf.concat([input_ids, eos_tensor] ,  axis=1       )

						UpperCamelCase__   =    ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size       )

						UpperCamelCase__   =    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__   =    prepare_marian_inputs_dict(snake_case ,  snake_case ,  snake_case       )
						return config, inputs_dict







			def 			snake_case__			(							self ,  snake_case ,  snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    TFMarianModel(config=snake_case       ).get_decoder()
						UpperCamelCase__   =    inputs_dict["input_ids"]

						UpperCamelCase__   =    input_ids[:1, :]
						UpperCamelCase__   =    inputs_dict["attention_mask"][:1, :]
						UpperCamelCase__   =    inputs_dict["head_mask"]
						UpperCamelCase__   =    1

						# first forward pass
						UpperCamelCase__   =    model(snake_case ,  attention_mask=snake_case ,  head_mask=snake_case ,  use_cache=snake_case       )

						UpperCamelCase__, UpperCamelCase__   =    outputs.to_tuple()

						# create hypothetical next token and extent to next_input_ids
						UpperCamelCase__   =    ids_tensor((self.batch_size, 3) ,  config.vocab_size       )
						UpperCamelCase__   =    tf.cast(ids_tensor((self.batch_size, 3) ,  2       ) ,  tf.inta       )

						# append to next input_ids and
						UpperCamelCase__   =    tf.concat([input_ids, next_tokens] ,  axis=-1       )
						UpperCamelCase__   =    tf.concat([attention_mask, next_attn_mask] ,  axis=-1       )

						UpperCamelCase__   =    model(snake_case ,  attention_mask=snake_case       )[0]
						UpperCamelCase__   =    model(snake_case ,  attention_mask=snake_case ,  past_key_values=snake_case       )[0]

						self.parent.assertEqual(next_tokens.shape[1] ,  output_from_past.shape[1]       )

						# select random slice
						UpperCamelCase__   =    int(ids_tensor((1,) ,  output_from_past.shape[-1]       )       )
						UpperCamelCase__   =    output_from_no_past[:, -3:, random_slice_idx]
						UpperCamelCase__   =    output_from_past[:, :, random_slice_idx]

						# test that outputs are equal for slice
						tf.debugging.assert_near(snake_case ,  snake_case ,  rtol=1E-3       )



def     UpperCamelCase_(			_A				:List[str]			,       _A				:Any			,       _A				:int			,       _A				:Union[str, Any]=None			,       _A				:str=None			,       _A				:Any=None			,       _A				:List[str]=None			,       _A				:Union[str, Any]=None			,       )->						Optional[int]:
			if attention_mask is None:
						UpperCamelCase__   =    tf.cast(tf.math.not_equal(_A			,       config.pad_token_id					)			,       tf.inta					)
			if decoder_attention_mask is None:
						UpperCamelCase__   =    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__   =    tf.ones((config.encoder_layers, config.encoder_attention_heads)					)
			if decoder_head_mask is None:
						UpperCamelCase__   =    tf.ones((config.decoder_layers, config.decoder_attention_heads)					)
			if cross_attn_head_mask is None:
						UpperCamelCase__   =    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__		(							UpperCAmelCase   ,		UpperCAmelCase   ,		unittest.TestCase ):






			"""simple docstring"""






			_UpperCamelCase						:   Dict					=						(TFMarianMTModel, TFMarianModel) if is_tf_available() else ()
			_UpperCamelCase						:   Union[str, Any]					=						(TFMarianMTModel,) if is_tf_available() else ()
			_UpperCamelCase						:   Optional[Any]					=						(
			    {
			        'conversational': TFMarianMTModel,
			        'feature-extraction': TFMarianModel,
			        'summarization': TFMarianMTModel,
			        'text2text-generation': TFMarianMTModel,
			        'translation': TFMarianMTModel,
			    }
			    if is_tf_available()
			    else {}
			)
			_UpperCamelCase						:   Optional[Any]					=						True
			_UpperCamelCase						:   Any					=						False
			_UpperCamelCase						:   List[Any]					=						False




			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    TFMarianModelTester(self       )
						UpperCamelCase__   =    ConfigTester(self ,  config_class=snake_case       )




			def 			snake_case__			(							self       ):

						'''simple docstring'''


						self.config_tester.run_common_tests()







			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    self.model_tester.prepare_config_and_inputs_for_common()
						self.model_tester.check_decoder_model_past_large_inputs(*snake_case       )







@require_tf
class     lowerCamelCase__		(							unittest.TestCase ):






			"""simple docstring"""






			_UpperCamelCase						:   Tuple					=						1000  # show more chars for failing integration tests




			@classmethod
			def 			snake_case__			(							cls       ):

						'''simple docstring'''


						UpperCamelCase__   =    F'''Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}'''
						return cls




			@cached_property
			def 			snake_case__			(							self       ):

						'''simple docstring'''


						return AutoTokenizer.from_pretrained(self.model_name       )




			@property
			def 			snake_case__			(							self       ):

						'''simple docstring'''


						return self.tokenizer.eos_token_id




			@cached_property
			def 			snake_case__			(							self       ):

						'''simple docstring'''


						warnings.simplefilter("error"       )
						UpperCamelCase__   =    TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name       )
						assert isinstance(snake_case ,  snake_case       )
						UpperCamelCase__   =    model.config
						self.assertListEqual(c.bad_words_ids ,  [[c.pad_token_id]]       )
						self.assertEqual(c.max_length ,  512       )
						self.assertEqual(c.decoder_start_token_id ,  c.pad_token_id       )
						return model




			def 			snake_case__			(							self ,  **snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    self.translate_src_text(**snake_case       )
						self.assertListEqual(self.expected_text ,  snake_case       )







			def 			snake_case__			(							self ,  **snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    self.tokenizer(self.src_text ,  **snake_case ,  padding=snake_case ,  return_tensors="tf"       )
						UpperCamelCase__   =    self.model.generate(
						    model_inputs.input_ids ,  attention_mask=model_inputs.attention_mask ,  num_beams=2 ,  max_length=128       )
						UpperCamelCase__   =    self.tokenizer.batch_decode(generated_ids.numpy() ,  skip_special_tokens=snake_case       )
						return generated_words







@require_sentencepiece
@require_tokenizers
@require_tf
class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			_UpperCamelCase						:   List[str]					=						'mt'
			_UpperCamelCase						:   Tuple					=						'en'
			_UpperCamelCase						:   str					=						['Billi messu b\'mod ġentili, Ġesù fejjaq raġel li kien milqut bil - marda kerha tal - ġdiem.']
			_UpperCamelCase						:   Optional[Any]					=						['Touching gently, Jesus healed a man who was affected by the sad disease of leprosy.']




			@unittest.skip("Skipping until #12647 is resolved."       )
			@slow
			def 			snake_case__			(							self       ):

						'''simple docstring'''


						self._assert_generated_batch_equal_expected()







@require_sentencepiece
@require_tokenizers
@require_tf
class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			_UpperCamelCase						:   str					=						'en'
			_UpperCamelCase						:   Dict					=						'zh'
			_UpperCamelCase						:   Dict					=						['My name is Wolfgang and I live in Berlin']
			_UpperCamelCase						:   int					=						['我叫沃尔夫冈 我住在柏林']




			@unittest.skip("Skipping until #12647 is resolved."       )
			@slow
			def 			snake_case__			(							self       ):

						'''simple docstring'''


						self._assert_generated_batch_equal_expected()







@require_sentencepiece
@require_tokenizers
@require_tf
class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			_UpperCamelCase						:   List[str]					=						'en'
			_UpperCamelCase						:   Optional[int]					=						'ROMANCE'
			_UpperCamelCase						:   Optional[int]					=						[
			    '>>fr<< Don\'t spend so much time watching TV.',
			    '>>pt<< Your message has been sent.',
			    '>>es<< He\'s two years older than me.',
			]
			_UpperCamelCase						:   Dict					=						[
			    'Ne passez pas autant de temps à regarder la télé.',
			    'A sua mensagem foi enviada.',
			    'Es dos años más viejo que yo.',
			]




			@unittest.skip("Skipping until #12647 is resolved."       )
			@slow
			def 			snake_case__			(							self       ):

						'''simple docstring'''


						self._assert_generated_batch_equal_expected()







			@unittest.skip("Skipping until #12647 is resolved."       )
			@slow
			def 			snake_case__			(							self       ):

						'''simple docstring'''


						UpperCamelCase__   =    TranslationPipeline(self.model ,  self.tokenizer ,  framework="tf"       )
						UpperCamelCase__   =    pipeline(self.src_text       )
						self.assertEqual(self.expected_text ,  [x["translation_text"] for x in output]       )






from __future__ import annotations
def     UpperCamelCase_(			_A				:list					)->						int | float:
			UpperCamelCase__   =    sorted(_A					)
			UpperCamelCase__   =    len(_A					)
			UpperCamelCase__   =    length >> 1
			return (
			    (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
			    if length % 2 == 0
			    else sorted_list[mid_index]
			)
def     UpperCamelCase_(			)->						List[Any]:
			import doctest

			doctest.testmod()


if __name__ == "__main__":
						main()


