


from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


A__:   Any    =	logging.get_logger(__name__)

A__:   List[str]    =	{
    '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json''',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}







class A__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  Union[str, Any]     	=							"m2m_100"
      __UpperCamelCase	:  List[Any]     	=							["past_key_values"]
      __UpperCamelCase	:  Optional[int]     	=							{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}






      def __init__(			self      :str					,						SCREAMING_SNAKE_CASE      :Any=1_2_8_1_1_2					,						SCREAMING_SNAKE_CASE      :Optional[Any]=1_0_2_4					,						SCREAMING_SNAKE_CASE      :List[str]=1_2					,						SCREAMING_SNAKE_CASE      :List[str]=4_0_9_6					,						SCREAMING_SNAKE_CASE      :str=1_6					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=1_2					,						SCREAMING_SNAKE_CASE      :List[Any]=4_0_9_6					,						SCREAMING_SNAKE_CASE      :Any=1_6					,						SCREAMING_SNAKE_CASE      :List[str]=0.05					,						SCREAMING_SNAKE_CASE      :Optional[int]=0.05					,						SCREAMING_SNAKE_CASE      :Dict=True					,						SCREAMING_SNAKE_CASE      :Tuple=True					,						SCREAMING_SNAKE_CASE      :Dict="relu"					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=1_0_2_4					,						SCREAMING_SNAKE_CASE      :Optional[Any]=0.1					,						SCREAMING_SNAKE_CASE      :int=0.1					,						SCREAMING_SNAKE_CASE      :str=0.0					,						SCREAMING_SNAKE_CASE      :List[Any]=0.02					,						SCREAMING_SNAKE_CASE      :int=2					,						SCREAMING_SNAKE_CASE      :Optional[int]=True					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=1					,						SCREAMING_SNAKE_CASE      :Optional[int]=0					,						SCREAMING_SNAKE_CASE      :List[str]=2					,						**SCREAMING_SNAKE_CASE      :Tuple					,						)	-> Any:




             '''simple docstring'''
             _a   :			Dict					 =vocab_size
             _a   :			Any					 =max_position_embeddings
             _a   :			str					 =d_model
             _a   :			Any					 =encoder_ffn_dim
             _a   :			Optional[int]					 =encoder_layers
             _a   :			Dict					 =encoder_attention_heads
             _a   :			Any					 =decoder_ffn_dim
             _a   :			Optional[int]					 =decoder_layers
             _a   :			Dict					 =decoder_attention_heads
             _a   :			List[Any]					 =dropout
             _a   :			Optional[int]					 =attention_dropout
             _a   :			str					 =activation_dropout
             _a   :			Optional[Any]					 =activation_function
             _a   :			Optional[Any]					 =init_std
             _a   :			List[Any]					 =encoder_layerdrop
             _a   :			Union[str, Any]					 =decoder_layerdrop
             _a   :			str					 =use_cache
             _a   :			Optional[int]					 =encoder_layers
             _a   :			Optional[Any]					 =scale_embedding  # scale factor will be sqrt(d_model) if True

             super().__init__(
                 pad_token_id=SCREAMING_SNAKE_CASE					,						bos_token_id=SCREAMING_SNAKE_CASE					,						eos_token_id=SCREAMING_SNAKE_CASE					,						is_encoder_decoder=SCREAMING_SNAKE_CASE					,						decoder_start_token_id=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE					,						)







class A__	(						UpperCAmelCase__				):






      @property
      def __UpperCAmelCase	(			self      :List[Any]     )	-> Mapping[str, Mapping[int, str]]:




             '''simple docstring'''
             _a   :			Any					 =OrderedDict(
                 [
                     ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                     ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                 ]     )

             if self.use_past:
                    _a   :			List[Any]					 ={0: """batch"""}
                    _a   :			Tuple					 ={0: """batch""", 1: """past_decoder_sequence + sequence"""}
             else:
                    _a   :			Union[str, Any]					 ={0: """batch""", 1: """decoder_sequence"""}
                    _a   :			int					 ={0: """batch""", 1: """decoder_sequence"""}

             if self.use_past:
                    self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE					,						direction="""inputs"""     )
             return common_inputs






      def __UpperCAmelCase	(			self      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :PreTrainedTokenizer					,						SCREAMING_SNAKE_CASE      :int = -1					,						SCREAMING_SNAKE_CASE      :int = -1					,						SCREAMING_SNAKE_CASE      :bool = False					,						SCREAMING_SNAKE_CASE      :Optional[TensorType] = None					,						)	-> Mapping[str, Any]:




             '''simple docstring'''
             _a   :			Any					 =compute_effective_axis_dimension(
                 SCREAMING_SNAKE_CASE					,						fixed_dimension=OnnxConfig.default_fixed_batch					,						num_token_to_add=0     )

             # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
             _a   :			Any					 =tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE     )
             _a   :			Union[str, Any]					 =compute_effective_axis_dimension(
                 SCREAMING_SNAKE_CASE					,						fixed_dimension=OnnxConfig.default_fixed_sequence					,						num_token_to_add=SCREAMING_SNAKE_CASE     )

             # Generate dummy inputs according to compute batch and sequence
             _a   :			Optional[int]					 =[""" """.join([tokenizer.unk_token]     ) * seq_length] * batch_size
             _a   :			Any					 =dict(tokenizer(SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE     )     )
             return common_inputs






      def __UpperCAmelCase	(			self      :Tuple					,						SCREAMING_SNAKE_CASE      :PreTrainedTokenizer					,						SCREAMING_SNAKE_CASE      :int = -1					,						SCREAMING_SNAKE_CASE      :int = -1					,						SCREAMING_SNAKE_CASE      :bool = False					,						SCREAMING_SNAKE_CASE      :Optional[TensorType] = None					,						)	-> Mapping[str, Any]:




             '''simple docstring'''
             _a   :			Dict					 =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                 SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )

             # Generate decoder inputs
             _a   :			str					 =seq_length if not self.use_past else 1
             _a   :			int					 =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                 SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )
             _a   :			List[str]					 ={f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
             _a   :			Union[str, Any]					 =dict(**SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if self.use_past:
                    if not is_torch_available():
                           raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""     )
                    else:
                           import torch
                    _a   :			Optional[int]					 =common_inputs["""input_ids"""].shape
                    _a   :			Any					 =common_inputs["""decoder_input_ids"""].shape[1]
                    _a   :			Tuple					 =self.num_attention_heads
                    _a   :			Tuple					 =(
                        batch,
                        num_encoder_attention_heads,
                        encoder_seq_length,
                        self._config.hidden_size // num_encoder_attention_heads,
                    )
                    _a   :			List[Any]					 =decoder_seq_length + 3
                    _a   :			List[str]					 =(
                        batch,
                        num_decoder_attention_heads,
                        decoder_past_length,
                        self._config.hidden_size // num_decoder_attention_heads,
                    )

                    _a   :			Any					 =torch.cat(
                        [common_inputs["""decoder_attention_mask"""], torch.ones(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )]					,						dim=1     )

                    _a   :			str					 =[]
                    # If the number of encoder and decoder layers are present in the model configuration, both are considered
                    _a   :			Dict					 =self.num_layers
                    _a   :			Tuple					 =min(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )
                    _a   :			Optional[Any]					 =max(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     ) - min_num_layers
                    _a   :			Dict					 ="""encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

                    for _ in range(SCREAMING_SNAKE_CASE     ):
                           common_inputs["past_key_values"].append(
                               (
                                   torch.zeros(SCREAMING_SNAKE_CASE     ),
                                   torch.zeros(SCREAMING_SNAKE_CASE     ),
                                   torch.zeros(SCREAMING_SNAKE_CASE     ),
                                   torch.zeros(SCREAMING_SNAKE_CASE     ),
                               )     )
                    # TODO: test this.
                    _a   :			Any					 =encoder_shape if remaining_side_name == """encoder""" else decoder_shape
                    for _ in range(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     ):
                           common_inputs["past_key_values"].append((torch.zeros(SCREAMING_SNAKE_CASE     ), torch.zeros(SCREAMING_SNAKE_CASE     ))     )
             return common_inputs



      __UpperCamelCase	:  Any     	=							_generate_dummy_inputs_for_default_and_seqaseq_lm






'''simple docstring'''







from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class A__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  Optional[Any]     	=							["image_processor", "tokenizer"]
      __UpperCamelCase	:  Optional[Any]     	=							"EfficientNetImageProcessor"
      __UpperCamelCase	:  Union[str, Any]     	=							("BertTokenizer", "BertTokenizerFast")






      def __init__(			self      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :Optional[int]     )	-> List[str]:




             '''simple docstring'''
             super().__init__(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )






      def __call__(			self      :str					,						SCREAMING_SNAKE_CASE      :List[Any]=None					,						SCREAMING_SNAKE_CASE      :Dict=None					,						SCREAMING_SNAKE_CASE      :Union[str, Any]="max_length"					,						SCREAMING_SNAKE_CASE      :Any=6_4					,						SCREAMING_SNAKE_CASE      :List[str]=None					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Optional[int]:




             '''simple docstring'''
             if text is None and images is None:
                    raise ValueError("""You have to specify either text or images. Both cannot be none."""     )

             if text is not None:
                    _a   :			Dict					 =self.tokenizer(
                        SCREAMING_SNAKE_CASE					,						padding=SCREAMING_SNAKE_CASE					,						max_length=SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if images is not None:
                    _a   :			Any					 =self.image_processor(SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if text is not None and images is not None:
                    _a   :			Dict					 =image_features.pixel_values
                    return encoding
             elif text is not None:
                    return encoding
             else:
                    return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE     )					,						tensor_type=SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Any					,						*SCREAMING_SNAKE_CASE      :Optional[int]					,						**SCREAMING_SNAKE_CASE      :str     )	-> Dict:




             '''simple docstring'''
             return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :int					,						*SCREAMING_SNAKE_CASE      :List[str]					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Union[str, Any]:




             '''simple docstring'''
             return self.tokenizer.decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      @property
      def __UpperCAmelCase	(			self      :Tuple     )	-> Tuple:




             '''simple docstring'''
             _a   :			int					 =self.tokenizer.model_input_names
             _a   :			Union[str, Any]					 =self.image_processor.model_input_names
             return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )


