
from ctypes import c_float, sizeof
from enum import Enum
from typing import TYPE_CHECKING, Optional, Union


if TYPE_CHECKING:
			from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore




class      SCREAMING_SNAKE_CASE__  (			UpperCAmelCase      ):





	'''simple docstring'''



	_UpperCAmelCase :	List[str]					     =      c_float



	@property
	def 				A							(						self			:					Union[str, Any]			):


				'''simple docstring'''


				return sizeof(self.value			)






def  a_	( __lowercase     :			int ,      __lowercase     :			int ,      __lowercase     :			int = 0				)				->						int:
			# < 0 is possible if using a dynamic axis
			if dimension <= 0:
						_snake_case						=    fixed_dimension

			dimension -= num_token_to_add
			return dimension






def  a_	( __lowercase     :			int ,      __lowercase     :			ParameterFormat				)				->						int:
			return num_parameters * dtype.size






def  a_	( __lowercase     :			str				)				->						Optional[Union["AutoTokenizer", "AutoFeatureExtractor", "AutoProcessor"]]:
			from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore

			try:
						return AutoProcessor.from_pretrained(__lowercase				)
			except (ValueError, OSError, KeyError):
						_snake_case			,      _snake_case						=    None, None
						try:
									_snake_case						=    AutoTokenizer.from_pretrained(__lowercase				)
						except (OSError, KeyError):
									pass
						try:
									_snake_case						=    AutoFeatureExtractor.from_pretrained(__lowercase				)
						except (OSError, KeyError):
									pass

						if tokenizer is not None and feature_extractor is not None:
									raise ValueError(
									    f'''Couldn\'t auto-detect preprocessor for {model_name}. Found both a tokenizer and a feature extractor.'''				)
						elif tokenizer is None and feature_extractor is None:
									return None
						elif tokenizer is not None:
									return tokenizer
						else:
									return feature_extractor

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      SCREAMING_SNAKE_CASE__  :





	'''simple docstring'''



	_UpperCAmelCase :	Optional[int]					     =      MarianConfig
	_UpperCAmelCase :	str					     =      {}
	_UpperCAmelCase :	Union[str, Any]					     =      "gelu"



	def __init__(						self			:					List[Any]    ,						lowercase			:					List[str]    ,						lowercase			:					str=13    ,						lowercase			:					Union[str, Any]=7    ,						lowercase			:					List[Any]=True    ,						lowercase			:					Tuple=False    ,						lowercase			:					Dict=99    ,						lowercase			:					Optional[Any]=32    ,						lowercase			:					Optional[int]=2    ,						lowercase			:					int=4    ,						lowercase			:					List[str]=37    ,						lowercase			:					Tuple=0.1    ,						lowercase			:					Union[str, Any]=0.1    ,						lowercase			:					Optional[int]=20    ,						lowercase			:					List[Any]=2    ,						lowercase			:					Dict=1    ,						lowercase			:					str=0    ,						):


				'''simple docstring'''


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

				_snake_case						=    hidden_dropout_prob
				_snake_case						=    attention_probs_dropout_prob
				_snake_case						=    max_position_embeddings
				_snake_case						=    eos_token_id
				_snake_case						=    pad_token_id
				_snake_case						=    bos_token_id



	def 				A							(						self			:					List[Any]			):


				'''simple docstring'''


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

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

				_snake_case						=    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    ,						)
				_snake_case						=    prepare_marian_inputs_dict(lowercase    ,						lowercase    ,						lowercase			)
				return config, inputs_dict





	def 				A							(						self			:					List[Any]    ,						lowercase			:					List[str]    ,						lowercase			:					Optional[Any]			):


				'''simple docstring'''


				_snake_case						=    TFMarianModel(config=lowercase			).get_decoder()
				_snake_case						=    inputs_dict['input_ids']

				_snake_case						=    input_ids[:1, :]
				_snake_case						=    inputs_dict['attention_mask'][:1, :]
				_snake_case						=    inputs_dict['head_mask']
				_snake_case						=    1

				# first forward pass
				_snake_case						=    model(lowercase    ,						attention_mask=lowercase    ,						head_mask=lowercase    ,						use_cache=lowercase			)

				_snake_case			,      _snake_case						=    outputs.to_tuple()

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

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

				_snake_case						=    model(lowercase    ,						attention_mask=lowercase			)[0]
				_snake_case						=    model(lowercase    ,						attention_mask=lowercase    ,						past_key_values=lowercase			)[0]

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

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

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






def  a_	( __lowercase     :			List[str] ,      __lowercase     :			List[str] ,      __lowercase     :			Any ,      __lowercase     :			int=None ,      __lowercase     :			Tuple=None ,      __lowercase     :			str=None ,      __lowercase     :			Tuple=None ,      __lowercase     :			str=None ,      )				->						List[str]:
			if attention_mask is None:
						_snake_case						=    tf.cast(tf.math.not_equal(__lowercase ,      config.pad_token_id				) ,      tf.inta				)
			if decoder_attention_mask is None:
						_snake_case						=    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:
						_snake_case						=    tf.ones((config.encoder_layers, config.encoder_attention_heads)				)
			if decoder_head_mask is None:
						_snake_case						=    tf.ones((config.decoder_layers, config.decoder_attention_heads)				)
			if cross_attn_head_mask is None:
						_snake_case						=    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      SCREAMING_SNAKE_CASE__  (			UpperCAmelCase							,UpperCAmelCase							,unittest.TestCase      ):





	'''simple docstring'''



	_UpperCAmelCase :	int					     =      (TFMarianMTModel, TFMarianModel) if is_tf_available() else ()
	_UpperCAmelCase :	Tuple					     =      (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 :	Union[str, Any]					     =      True
	_UpperCAmelCase :	Union[str, Any]					     =      False
	_UpperCAmelCase :	Tuple					     =      False



	def 				A							(						self			:					int			):


				'''simple docstring'''


				_snake_case						=    TFMarianModelTester(self			)
				_snake_case						=    ConfigTester(self    ,						config_class=lowercase			)



	def 				A							(						self			:					Any			):


				'''simple docstring'''


				self.config_tester.run_common_tests()





	def 				A							(						self			:					Tuple			):


				'''simple docstring'''


				_snake_case						=    self.model_tester.prepare_config_and_inputs_for_common()
				self.model_tester.check_decoder_model_past_large_inputs(*lowercase			)




@require_tf
class      SCREAMING_SNAKE_CASE__  (			unittest.TestCase      ):





	'''simple docstring'''



	_UpperCAmelCase :	List[Any]					     =      1_0_0_0  # show more chars for failing integration tests



	@classmethod
	def 				A							(						cls			:					Tuple			):


				'''simple docstring'''


				_snake_case						=    f'''Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}'''
				return cls



	@cached_property
	def 				A							(						self			:					List[str]			):


				'''simple docstring'''


				return AutoTokenizer.from_pretrained(self.model_name			)



	@property
	def 				A							(						self			:					Tuple			):


				'''simple docstring'''


				return self.tokenizer.eos_token_id



	@cached_property
	def 				A							(						self			:					List[Any]			):


				'''simple docstring'''


				warnings.simplefilter('error'			)
				_snake_case						=    TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name			)
				assert isinstance(lowercase    ,						lowercase			)
				_snake_case						=    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 				A							(						self			:					Optional[Any]    ,						**lowercase			:					int			):


				'''simple docstring'''


				_snake_case						=    self.translate_src_text(**lowercase			)
				self.assertListEqual(self.expected_text    ,						lowercase			)





	def 				A							(						self			:					Optional[int]    ,						**lowercase			:					List[Any]			):


				'''simple docstring'''


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




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





	'''simple docstring'''



	_UpperCAmelCase :	Union[str, Any]					     =      "mt"
	_UpperCAmelCase :	List[str]					     =      "en"
	_UpperCAmelCase :	str					     =      ["Billi messu b'mod ġentili, Ġesù fejjaq raġel li kien milqut bil - marda kerha tal - ġdiem."]
	_UpperCAmelCase :	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 				A							(						self			:					Union[str, Any]			):


				'''simple docstring'''


				self._assert_generated_batch_equal_expected()




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





	'''simple docstring'''



	_UpperCAmelCase :	Tuple					     =      "en"
	_UpperCAmelCase :	Any					     =      "zh"
	_UpperCAmelCase :	int					     =      ["My name is Wolfgang and I live in Berlin"]
	_UpperCAmelCase :	List[str]					     =      ["我叫沃尔夫冈 我住在柏林"]



	@unittest.skip('Skipping until #12647 is resolved.'			)
	@slow
	def 				A							(						self			:					Optional[Any]			):


				'''simple docstring'''


				self._assert_generated_batch_equal_expected()




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





	'''simple docstring'''



	_UpperCAmelCase :	Any					     =      "en"
	_UpperCAmelCase :	Any					     =      "ROMANCE"
	_UpperCAmelCase :	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 :	Tuple					     =      [
	    "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 				A							(						self			:					List[str]			):


				'''simple docstring'''


				self._assert_generated_batch_equal_expected()





	@unittest.skip('Skipping until #12647 is resolved.'			)
	@slow
	def 				A							(						self			:					List[Any]			):


				'''simple docstring'''


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