


'''simple docstring'''





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 	__a   :
		_a			:						Any    			=			MarianConfig
		_a			:						List[str]    			=			{}
		_a			:						str    			=			'gelu'
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=13    ,					_SCREAMING_SNAKE_CASE=7    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=False    ,					_SCREAMING_SNAKE_CASE=99    ,					_SCREAMING_SNAKE_CASE=32    ,					_SCREAMING_SNAKE_CASE=2    ,					_SCREAMING_SNAKE_CASE=4    ,					_SCREAMING_SNAKE_CASE=37    ,					_SCREAMING_SNAKE_CASE=0.1    ,					_SCREAMING_SNAKE_CASE=0.1    ,					_SCREAMING_SNAKE_CASE=20    ,					_SCREAMING_SNAKE_CASE=2    ,					_SCREAMING_SNAKE_CASE=1    ,					_SCREAMING_SNAKE_CASE=0    ,					)				-> Union[str, Any]:




									"""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 			UpperCAmelCase__       (    self		)				-> Dict:




									"""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(UpperCamelCase__    ,					UpperCamelCase__    ,					UpperCamelCase__		)
									return config, inputs_dict






		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase					=    TFMarianModel(config=UpperCamelCase__		).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(UpperCamelCase__    ,					attention_mask=UpperCamelCase__    ,					head_mask=UpperCamelCase__    ,					use_cache=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(UpperCamelCase__    ,					attention_mask=UpperCamelCase__		)[0]
									_UpperCAmelCase					=    model(UpperCamelCase__    ,					attention_mask=UpperCamelCase__    ,					past_key_values=UpperCamelCase__		)[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(UpperCamelCase__    ,					UpperCamelCase__    ,					rtol=1e-3		)



def  lowerCAmelCase__				(    a__:	Any    ,						a__:	Optional[Any]    ,						a__:	List[str]    ,						a__:	str=None    ,						a__:	Optional[int]=None    ,						a__:	str=None    ,						a__:	List[str]=None    ,						a__:	Optional[Any]=None    ,						)						->	Optional[Any]:


							'''simple docstring'''





							if attention_mask is None:
														_UpperCAmelCase					=    tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE__    ,						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 	__a   (       __SCREAMING_SNAKE_CASE					,   __SCREAMING_SNAKE_CASE					,   unittest.TestCase    ):
		_a			:						Optional[Any]    			=			(TFMarianMTModel, TFMarianModel) if is_tf_available() else ()
		_a			:						List[str]    			=			(TFMarianMTModel,) if is_tf_available() else ()
		_a			:						int    			=			(
		    {
		        'conversational': TFMarianMTModel,
		        'feature-extraction': TFMarianModel,
		        'summarization': TFMarianMTModel,
		        'text2text-generation': TFMarianMTModel,
		        'translation': TFMarianMTModel,
		    }
		    if is_tf_available()
		    else {}
		)
		_a			:						List[Any]    			=			True
		_a			:						List[str]    			=			False
		_a			:						Tuple    			=			False
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    TFMarianModelTester(self		)
									_UpperCAmelCase					=    ConfigTester(self    ,					config_class=UpperCamelCase__		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									self.config_tester.run_common_tests()






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




									"""simple docstring"""





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



@require_tf
class 	__a   (       unittest.TestCase    ):
		_a			:						Optional[Any]    			=			10_00  # show more chars for failing integration tests
		@classmethod
		def 			UpperCAmelCase__       (    cls		)				-> None:




									"""simple docstring"""





									_UpperCAmelCase					=    f'''Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}'''
									return cls
		@cached_property
		def 			UpperCAmelCase__       (    self		)				-> MarianTokenizer:




									"""simple docstring"""





									return AutoTokenizer.from_pretrained(self.model_name		)
		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									return self.tokenizer.eos_token_id
		@cached_property
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									warnings.simplefilter('error'		)
									_UpperCAmelCase					=    TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name		)
									assert isinstance(UpperCamelCase__    ,					UpperCamelCase__		)
									_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 			UpperCAmelCase__       (    self    ,					**_SCREAMING_SNAKE_CASE		)				-> int:




									"""simple docstring"""





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






		def 			UpperCAmelCase__       (    self    ,					**_SCREAMING_SNAKE_CASE		)				-> Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase					=    self.tokenizer(self.src_text    ,					**UpperCamelCase__    ,					padding=UpperCamelCase__    ,					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=UpperCamelCase__		)
									return generated_words



@require_sentencepiece
@require_tokenizers
@require_tf
class 	__a   (       __SCREAMING_SNAKE_CASE    ):
		_a			:						List[str]    			=			'mt'
		_a			:						List[Any]    			=			'en'
		_a			:						Optional[int]    			=			['Billi messu b\'mod ġentili, Ġesù fejjaq raġel li kien milqut bil - marda kerha tal - ġdiem.']
		_a			:						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 			UpperCAmelCase__       (    self		)				-> List[str]:




									"""simple docstring"""





									self._assert_generated_batch_equal_expected()



@require_sentencepiece
@require_tokenizers
@require_tf
class 	__a   (       __SCREAMING_SNAKE_CASE    ):
		_a			:						int    			=			'en'
		_a			:						Dict    			=			'zh'
		_a			:						Tuple    			=			['My name is Wolfgang and I live in Berlin']
		_a			:						Tuple    			=			['我叫沃尔夫冈 我住在柏林']
		@unittest.skip('Skipping until #12647 is resolved.'		)
		@slow
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									self._assert_generated_batch_equal_expected()



@require_sentencepiece
@require_tokenizers
@require_tf
class 	__a   (       __SCREAMING_SNAKE_CASE    ):
		_a			:						List[str]    			=			'en'
		_a			:						Union[str, Any]    			=			'ROMANCE'
		_a			:						Union[str, Any]    			=			[
		    '>>fr<< Don\'t spend so much time watching TV.',
		    '>>pt<< Your message has been sent.',
		    '>>es<< He\'s two years older than me.',
		]
		_a			:						str    			=			[
		    '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 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									self._assert_generated_batch_equal_expected()






		@unittest.skip('Skipping until #12647 is resolved.'		)
		@slow
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""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]		)




import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__						:int         =							logging.get_logger(__name__)

lowerCAmelCase__						:Optional[int]         =							{
    '''facebook/sam-vit-huge''': '''https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json''',
    '''facebook/sam-vit-large''': '''https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json''',
    '''facebook/sam-vit-base''': '''https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json''',
}

class 	__a   (       UpperCAmelCase    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE=1024    ,					_SCREAMING_SNAKE_CASE=16    ,					_SCREAMING_SNAKE_CASE=16    ,					_SCREAMING_SNAKE_CASE=4    ,					_SCREAMING_SNAKE_CASE="gelu"    ,					_SCREAMING_SNAKE_CASE=1e-6    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> Tuple:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    hidden_size
									_UpperCAmelCase					=    image_size
									_UpperCAmelCase					=    patch_size
									_UpperCAmelCase					=    image_size // patch_size
									_UpperCAmelCase					=    mask_input_channels
									_UpperCAmelCase					=    num_point_embeddings
									_UpperCAmelCase					=    hidden_act
									_UpperCAmelCase					=    layer_norm_eps



class 	__a   (       UpperCAmelCase    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE="relu"    ,					_SCREAMING_SNAKE_CASE=2048    ,					_SCREAMING_SNAKE_CASE=2    ,					_SCREAMING_SNAKE_CASE=8    ,					_SCREAMING_SNAKE_CASE=2    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE=1e-6    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> List[Any]:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    hidden_size
									_UpperCAmelCase					=    hidden_act
									_UpperCAmelCase					=    mlp_dim
									_UpperCAmelCase					=    num_hidden_layers
									_UpperCAmelCase					=    num_attention_heads
									_UpperCAmelCase					=    attention_downsample_rate
									_UpperCAmelCase					=    num_multimask_outputs
									_UpperCAmelCase					=    iou_head_depth
									_UpperCAmelCase					=    iou_head_hidden_dim
									_UpperCAmelCase					=    layer_norm_eps



class 	__a   (       UpperCAmelCase    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=768    ,					_SCREAMING_SNAKE_CASE=256    ,					_SCREAMING_SNAKE_CASE=12    ,					_SCREAMING_SNAKE_CASE=12    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=1024    ,					_SCREAMING_SNAKE_CASE=16    ,					_SCREAMING_SNAKE_CASE="gelu"    ,					_SCREAMING_SNAKE_CASE=1e-0_6    ,					_SCREAMING_SNAKE_CASE=0.0    ,					_SCREAMING_SNAKE_CASE=1e-1_0    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=4.0    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=14    ,					_SCREAMING_SNAKE_CASE=[2, 5, 8, 11]    ,					_SCREAMING_SNAKE_CASE=128    ,					_SCREAMING_SNAKE_CASE=None    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> List[str]:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase					=    hidden_size
									_UpperCAmelCase					=    output_channels
									_UpperCAmelCase					=    num_hidden_layers
									_UpperCAmelCase					=    num_attention_heads
									_UpperCAmelCase					=    num_channels
									_UpperCAmelCase					=    image_size
									_UpperCAmelCase					=    patch_size
									_UpperCAmelCase					=    hidden_act
									_UpperCAmelCase					=    layer_norm_eps
									_UpperCAmelCase					=    attention_dropout
									_UpperCAmelCase					=    initializer_range
									_UpperCAmelCase					=    qkv_bias
									_UpperCAmelCase					=    mlp_ratio
									_UpperCAmelCase					=    use_abs_pos
									_UpperCAmelCase					=    use_rel_pos
									_UpperCAmelCase					=    window_size
									_UpperCAmelCase					=    global_attn_indexes
									_UpperCAmelCase					=    num_pos_feats
									_UpperCAmelCase					=    int(hidden_size * mlp_ratio		) if mlp_dim is None else mlp_dim



class 	__a   (       UpperCAmelCase    ):
		_a			:						Any    			=			'sam'
		_a			:						Any    			=			True
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=0.02    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> Tuple:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    vision_config if vision_config is not None else {}
									_UpperCAmelCase					=    prompt_encoder_config if prompt_encoder_config is not None else {}
									_UpperCAmelCase					=    mask_decoder_config if mask_decoder_config is not None else {}

									if isinstance(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    vision_config.to_dict()
									if isinstance(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    prompt_encoder_config.to_dict()
									if isinstance(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    mask_decoder_config.to_dict()

									_UpperCAmelCase					=    SamVisionConfig(**_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    SamPromptEncoderConfig(**_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    SamMaskDecoderConfig(**_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    initializer_range






		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





									_UpperCAmelCase					=    copy.deepcopy(self.__dict__		)
									_UpperCAmelCase					=    self.vision_config.to_dict()
									_UpperCAmelCase					=    self.prompt_encoder_config.to_dict()
									_UpperCAmelCase					=    self.mask_decoder_config.to_dict()
									_UpperCAmelCase					=    self.__class__.model_type
									return output
