





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



     '''simple docstring'''

     __snake_case       :      Any						      =     MarianConfig
     __snake_case       :      Optional[int]						      =     {}
     __snake_case       :      List[Any]						      =     "gelu"






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




           '''simple docstring'''


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

           SCREAMING_SNAKE_CASE						=		hidden_dropout_prob
           SCREAMING_SNAKE_CASE						=		attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE						=		max_position_embeddings
           SCREAMING_SNAKE_CASE						=		eos_token_id
           SCREAMING_SNAKE_CASE						=		pad_token_id
           SCREAMING_SNAKE_CASE						=		bos_token_id






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Union[str, Any]			)	->						str:




           '''simple docstring'''


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

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

           SCREAMING_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		,)
           SCREAMING_SNAKE_CASE						=		prepare_marian_inputs_dict(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__			)
           return config, inputs_dict






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[int]		,lowerCamelCase__		:       Tuple		,lowerCamelCase__		:       List[Any]			)	->						Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		TFMarianModel(config=lowerCamelCase__			).get_decoder()
           SCREAMING_SNAKE_CASE						=		inputs_dict["""input_ids"""]

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

           # first forward pass
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,attention_mask=lowerCamelCase__		,head_mask=lowerCamelCase__		,use_cache=lowerCamelCase__			)

           SCREAMING_SNAKE_CASE,   SCREAMING_SNAKE_CASE						=		outputs.to_tuple()

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

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

           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,attention_mask=lowerCamelCase__			)[0]
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,attention_mask=lowerCamelCase__		,past_key_values=lowerCamelCase__			)[0]

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

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

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



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE=None					,		_SCREAMING_SNAKE_CASE=None					,		_SCREAMING_SNAKE_CASE=None					,		_SCREAMING_SNAKE_CASE=None					,		_SCREAMING_SNAKE_CASE=None					,		)					->  int:


      '''simple docstring'''


      if attention_mask is None:
            SCREAMING_SNAKE_CASE						=		tf.cast(tf.math.not_equal(_SCREAMING_SNAKE_CASE					,		config.pad_token_id      )					,		tf.inta      )
      if decoder_attention_mask is None:
            SCREAMING_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:
            SCREAMING_SNAKE_CASE						=		tf.ones((config.encoder_layers, config.encoder_attention_heads)      )
      if decoder_head_mask is None:
            SCREAMING_SNAKE_CASE						=		tf.ones((config.decoder_layers, config.decoder_attention_heads)      )
      if cross_attn_head_mask is None:
            SCREAMING_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 						UpperCamelCase__					( lowerCAmelCase_				, lowerCAmelCase_				, unittest.TestCase  ):



     '''simple docstring'''

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






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Union[str, Any]			)	->						List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		TFMarianModelTester(self			)
           SCREAMING_SNAKE_CASE						=		ConfigTester(self		,config_class=lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Tuple			)	->						Optional[int]:




           '''simple docstring'''


           self.config_tester.run_common_tests()






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[str]			)	->						Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.check_decoder_model_past_large_inputs(*lowerCamelCase__			)

@require_tf
class 						UpperCamelCase__					( unittest.TestCase  ):



     '''simple docstring'''

     __snake_case       :      str						      =     1000  # show more chars for failing integration tests






     @classmethod
     def 							SCREAMING_SNAKE_CASE__    (				cls		:       Optional[int]			)	->						None:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		F"""Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}"""
           return cls






     @cached_property
     def 							SCREAMING_SNAKE_CASE__    (				self		:       str			)	->						MarianTokenizer:




           '''simple docstring'''


           return AutoTokenizer.from_pretrained(self.model_name			)






     @property
     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[str]			)	->						int:




           '''simple docstring'''


           return self.tokenizer.eos_token_id






     @cached_property
     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[int]			)	->						Any:




           '''simple docstring'''


           warnings.simplefilter("""error"""			)
           SCREAMING_SNAKE_CASE						=		TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name			)
           assert isinstance(lowerCamelCase__		,lowerCamelCase__			)
           SCREAMING_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 							SCREAMING_SNAKE_CASE__    (				self		:       Any		,**lowerCamelCase__		:       Tuple			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.translate_src_text(**lowerCamelCase__			)
           self.assertListEqual(self.expected_text		,lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       int		,**lowerCamelCase__		:       Optional[Any]			)	->						Dict:




           '''simple docstring'''


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

@require_sentencepiece
@require_tokenizers
@require_tf
class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

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






     @unittest.skip("""Skipping until #12647 is resolved."""			)
     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       Dict			)	->						Optional[int]:




           '''simple docstring'''


           self._assert_generated_batch_equal_expected()

@require_sentencepiece
@require_tokenizers
@require_tf
class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

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






     @unittest.skip("""Skipping until #12647 is resolved."""			)
     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[str]			)	->						Any:




           '''simple docstring'''


           self._assert_generated_batch_equal_expected()

@require_sentencepiece
@require_tokenizers
@require_tf
class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     __snake_case       :      List[str]						      =     "en"
     __snake_case       :      int						      =     "ROMANCE"
     __snake_case       :      List[Any]						      =     [
         ">>fr<< Don't spend so much time watching TV.",
         ">>pt<< Your message has been sent.",
         ">>es<< He's two years older than me.",
     ]
     __snake_case       :      Union[str, Any]						      =     [
         "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 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]			)	->						Optional[int]:




           '''simple docstring'''


           self._assert_generated_batch_equal_expected()






     @unittest.skip("""Skipping until #12647 is resolved."""			)
     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       Union[str, Any]			)	->						List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		TranslationPipeline(self.model		,self.tokenizer		,framework="""tf"""			)
           SCREAMING_SNAKE_CASE						=		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


SCREAMING_SNAKE_CASE_		  =			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_		  =			{
    """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 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     def __init__(				self		:       List[str]		,lowerCamelCase__		:       int=256		,lowerCamelCase__		:       List[Any]=1024		,lowerCamelCase__		:       Any=16		,lowerCamelCase__		:       Union[str, Any]=16		,lowerCamelCase__		:       int=4		,lowerCamelCase__		:       Tuple="gelu"		,lowerCamelCase__		:       Dict=1e-6		,**lowerCamelCase__		:       int		,)	->						List[str]:




           '''simple docstring'''


           super().__init__(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		hidden_size
           SCREAMING_SNAKE_CASE						=		image_size
           SCREAMING_SNAKE_CASE						=		patch_size
           SCREAMING_SNAKE_CASE						=		image_size // patch_size
           SCREAMING_SNAKE_CASE						=		mask_input_channels
           SCREAMING_SNAKE_CASE						=		num_point_embeddings
           SCREAMING_SNAKE_CASE						=		hidden_act
           SCREAMING_SNAKE_CASE						=		layer_norm_eps

class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     def __init__(				self		:       str		,lowerCamelCase__		:       str=256		,lowerCamelCase__		:       Optional[Any]="relu"		,lowerCamelCase__		:       str=2048		,lowerCamelCase__		:       Union[str, Any]=2		,lowerCamelCase__		:       int=8		,lowerCamelCase__		:       Union[str, Any]=2		,lowerCamelCase__		:       List[Any]=3		,lowerCamelCase__		:       int=3		,lowerCamelCase__		:       Optional[Any]=256		,lowerCamelCase__		:       Tuple=1e-6		,**lowerCamelCase__		:       List[str]		,)	->						Dict:




           '''simple docstring'''


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

class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     def __init__(				self		:       Union[str, Any]		,lowerCamelCase__		:       Optional[Any]=768		,lowerCamelCase__		:       Union[str, Any]=256		,lowerCamelCase__		:       Optional[int]=12		,lowerCamelCase__		:       str=12		,lowerCamelCase__		:       Tuple=3		,lowerCamelCase__		:       Optional[Any]=1024		,lowerCamelCase__		:       Optional[int]=16		,lowerCamelCase__		:       Optional[Any]="gelu"		,lowerCamelCase__		:       int=1e-0_6		,lowerCamelCase__		:       Optional[Any]=0.0		,lowerCamelCase__		:       Tuple=1e-1_0		,lowerCamelCase__		:       Any=True		,lowerCamelCase__		:       List[Any]=4.0		,lowerCamelCase__		:       Optional[int]=True		,lowerCamelCase__		:       Union[str, Any]=True		,lowerCamelCase__		:       List[str]=14		,lowerCamelCase__		:       List[str]=[2, 5, 8, 11]		,lowerCamelCase__		:       Tuple=128		,lowerCamelCase__		:       Dict=None		,**lowerCamelCase__		:       Optional[Any]		,)	->						str:




           '''simple docstring'''


           super().__init__(**lowerCamelCase__			)

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

class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     __snake_case       :      Any						      =     "sam"
     __snake_case       :      Optional[Any]						      =     True






     def __init__(				self		:       Union[str, Any]		,lowerCamelCase__		:       List[Any]=None		,lowerCamelCase__		:       int=None		,lowerCamelCase__		:       int=None		,lowerCamelCase__		:       int=0.02		,**lowerCamelCase__		:       int		,)	->						str:




           '''simple docstring'''


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

           if isinstance(lowerCamelCase__		,lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		vision_config.to_dict()
           if isinstance(lowerCamelCase__		,lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		prompt_encoder_config.to_dict()
           if isinstance(lowerCamelCase__		,lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		mask_decoder_config.to_dict()

           SCREAMING_SNAKE_CASE						=		SamVisionConfig(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		SamPromptEncoderConfig(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		SamMaskDecoderConfig(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		initializer_range






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Union[str, Any]			)	->						Optional[Any]:




           '''simple docstring'''


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


