
"""simple docstring"""
from collections import OrderedDict
from typing import Any, Mapping, Optional

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


lowercase		:						int =					logging.get_logger(__name__)

lowercase		:						Tuple =					{
    'facebook/mbart-large-cc25': 'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json',
    # See all MBART models at https://huggingface.co/models?filter=mbart
}




class    _snake_case  (						_lowercase       ):
       SCREAMING_SNAKE_CASE		     =						'''mbart'''
       SCREAMING_SNAKE_CASE		     =						['''past_key_values''']
       SCREAMING_SNAKE_CASE		     =						{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






       def __init__(					self						:			List[Any]						, snake_case__						:			Union[str, Any]=5_0_2_6_5						, snake_case__						:			Tuple=1_0_2_4						, snake_case__						:			Union[str, Any]=1_2						, snake_case__						:			Dict=4_0_9_6						, snake_case__						:			Optional[int]=1_6						, snake_case__						:			Optional[int]=1_2						, snake_case__						:			Optional[int]=4_0_9_6						, snake_case__						:			Optional[int]=1_6						, snake_case__						:			str=0.0						, snake_case__						:			Union[str, Any]=0.0						, snake_case__						:			List[Any]=True						, snake_case__						:			Tuple=True						, snake_case__						:			Dict="gelu"						, snake_case__						:			Dict=1_0_2_4						, snake_case__						:			Optional[Any]=0.1						, snake_case__						:			Any=0.0						, snake_case__						:			List[Any]=0.0						, snake_case__						:			int=0.02						, snake_case__						:			List[str]=0.0						, snake_case__						:			Any=False						, snake_case__						:			Any=1						, snake_case__						:			Dict=0						, snake_case__						:			Any=2						, snake_case__						:			Optional[int]=2						, **snake_case__						:			Dict						, ):






            """simple docstring"""



            __a:						Optional[Any]		    =					vocab_size
            __a:						int		    =					max_position_embeddings
            __a:						Optional[Any]		    =					d_model
            __a:						List[str]		    =					encoder_ffn_dim
            __a:						Any		    =					encoder_layers
            __a:						Optional[Any]		    =					encoder_attention_heads
            __a:						Optional[Any]		    =					decoder_ffn_dim
            __a:						Any		    =					decoder_layers
            __a:						Any		    =					decoder_attention_heads
            __a:						List[Any]		    =					dropout
            __a:						Optional[int]		    =					attention_dropout
            __a:						List[str]		    =					activation_dropout
            __a:						Any		    =					activation_function
            __a:						Any		    =					init_std
            __a:						Dict		    =					encoder_layerdrop
            __a:						Dict		    =					decoder_layerdrop
            __a:						Optional[Any]		    =					classifier_dropout
            __a:						Tuple		    =					use_cache
            __a:						Dict		    =					encoder_layers
            __a:						Optional[Any]		    =					scale_embedding  # scale factor will be sqrt(d_model) if True
            super().__init__(
                pad_token_id=snake_case__						, bos_token_id=snake_case__						, eos_token_id=snake_case__						, is_encoder_decoder=snake_case__						, forced_eos_token_id=snake_case__						, **snake_case__						, )






class    _snake_case  (						_lowercase       ):






       @property
       def 							__lowercase  (					self						:			Optional[Any]    ):






            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                 __a:						Tuple		    =					OrderedDict(
                     [
                         ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                         ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                     ]    )

                 if self.use_past:
                      __a:						Dict		    =					{0: """batch"""}
                      __a:						List[str]		    =					{0: """batch""", 1: """past_decoder_sequence + sequence"""}
                 else:
                      __a:						Optional[Any]		    =					{0: """batch""", 1: """decoder_sequence"""}
                      __a:						Any		    =					{0: """batch""", 1: """decoder_sequence"""}

                 if self.use_past:
                      self.fill_with_past_key_values_(snake_case__						, direction="""inputs"""    )
            elif self.task == "causal-lm":
                 # TODO: figure this case out.
                 __a:						Any		    =					OrderedDict(
                     [
                         ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                         ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                     ]    )
                 if self.use_past:
                      __a:						List[Any]		    =					self.num_layers
                      for i in range(snake_case__    ):
                           __a:						List[str]		    =					{0: """batch""", 2: """past_sequence + sequence"""}
                           __a:						Optional[int]		    =					{0: """batch""", 2: """past_sequence + sequence"""}
            else:
                 __a:						Optional[Any]		    =					OrderedDict(
                     [
                         ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                         ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                         ("""decoder_input_ids""", {0: """batch""", 1: """decoder_sequence"""}),
                         ("""decoder_attention_mask""", {0: """batch""", 1: """decoder_sequence"""}),
                     ]    )

            return common_inputs






       @property
       def 							__lowercase  (					self						:			List[str]    ):






            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                 __a:						str		    =					super().outputs
            else:
                 __a:						List[Any]		    =					super(snake_case__						, self    ).outputs
                 if self.use_past:
                      __a:						Optional[int]		    =					self.num_layers
                      for i in range(snake_case__    ):
                           __a:						Any		    =					{0: """batch""", 2: """past_sequence + sequence"""}
                           __a:						str		    =					{0: """batch""", 2: """past_sequence + sequence"""}
            return common_outputs






       def 							__lowercase  (					self						:			Any						, snake_case__						:			PreTrainedTokenizer						, snake_case__						:			int = -1						, snake_case__						:			int = -1						, snake_case__						:			bool = False						, snake_case__						:			Optional[TensorType] = None						, ):






            """simple docstring"""



            __a:						int		    =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                snake_case__						, snake_case__						, snake_case__						, snake_case__						, snake_case__    )

            # Generate decoder inputs
            __a:						Dict		    =					seq_length if not self.use_past else 1
            __a:						int		    =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                snake_case__						, snake_case__						, snake_case__						, snake_case__						, snake_case__    )
            __a:						List[str]		    =					{f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
            __a:						str		    =					dict(**snake_case__						, **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:						Tuple		    =					common_inputs["""input_ids"""].shape
                 __a:						int		    =					common_inputs["""decoder_input_ids"""].shape[1]
                 __a:						List[str]		    =					self.num_attention_heads
                 __a:						Union[str, Any]		    =					(
                     batch,
                     num_encoder_attention_heads,
                     encoder_seq_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )
                 __a:						Union[str, Any]		    =					decoder_seq_length + 3
                 __a:						int		    =					(
                     batch,
                     num_decoder_attention_heads,
                     decoder_past_length,
                     self._config.hidden_size // num_decoder_attention_heads,
                 )

                 __a:						Optional[Any]		    =					torch.cat(
                     [common_inputs["""decoder_attention_mask"""], torch.ones(snake_case__						, snake_case__    )]						, dim=1    )

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

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






       def 							__lowercase  (					self						:			Tuple						, snake_case__						:			PreTrainedTokenizer						, snake_case__						:			int = -1						, snake_case__						:			int = -1						, snake_case__						:			bool = False						, snake_case__						:			Optional[TensorType] = None						, ):






            """simple docstring"""



            __a:						List[str]		    =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                snake_case__						, snake_case__						, snake_case__						, snake_case__						, 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:						List[Any]		    =					common_inputs["""input_ids"""].shape
                 # Not using the same length for past_key_values
                 __a:						List[Any]		    =					seqlen + 2
                 __a:						List[str]		    =					self.num_layers
                 __a:						Dict		    =					self.num_attention_heads
                 __a:						Union[str, Any]		    =					(
                     batch,
                     num_encoder_attention_heads,
                     past_key_values_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )

                 __a:						List[str]		    =					common_inputs["""attention_mask"""].dtype
                 __a:						Any		    =					torch.cat(
                     [common_inputs["""attention_mask"""], torch.ones(snake_case__						, snake_case__						, dtype=snake_case__    )]						, dim=1    )
                 __a:						Any		    =					[
                     (torch.zeros(snake_case__    ), torch.zeros(snake_case__    )) for _ in range(snake_case__    )
                 ]
            return common_inputs






       def 							__lowercase  (					self						:			Optional[int]						, snake_case__						:			PreTrainedTokenizer						, snake_case__						:			int = -1						, snake_case__						:			int = -1						, snake_case__						:			bool = False						, snake_case__						:			Optional[TensorType] = None						, ):






            """simple docstring"""



            __a:						str		    =					compute_effective_axis_dimension(
                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:						Dict		    =					tokenizer.num_special_tokens_to_add(snake_case__    )
            __a:						Any		    =					compute_effective_axis_dimension(
                snake_case__						, fixed_dimension=OnnxConfig.default_fixed_sequence						, num_token_to_add=snake_case__    )

            # Generate dummy inputs according to compute batch and sequence
            __a:						str		    =					[""" """.join([tokenizer.unk_token]    ) * seq_length] * batch_size
            __a:						Dict		    =					dict(tokenizer(snake_case__						, return_tensors=snake_case__    )    )
            return common_inputs






       def 							__lowercase  (					self						:			str						, snake_case__						:			PreTrainedTokenizer						, snake_case__						:			int = -1						, snake_case__						:			int = -1						, snake_case__						:			bool = False						, snake_case__						:			Optional[TensorType] = None						, ):






            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                 __a:						Any		    =					self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                     snake_case__						, batch_size=snake_case__						, seq_length=snake_case__						, is_pair=snake_case__						, framework=snake_case__    )

            elif self.task == "causal-lm":
                 __a:						Any		    =					self._generate_dummy_inputs_for_causal_lm(
                     snake_case__						, batch_size=snake_case__						, seq_length=snake_case__						, is_pair=snake_case__						, framework=snake_case__    )
            else:
                 __a:						Optional[int]		    =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                     snake_case__						, batch_size=snake_case__						, seq_length=snake_case__						, is_pair=snake_case__						, framework=snake_case__    )

            return common_inputs






       def 							__lowercase  (					self						:			str						, snake_case__						:			List[Any]						, snake_case__						:			List[Any]						, snake_case__						:			List[str]						, snake_case__						:			Union[str, Any]    ):






            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                 __a:						Union[str, Any]		    =					super()._flatten_past_key_values_(snake_case__						, snake_case__						, snake_case__						, snake_case__    )
            else:
                 __a:						List[Any]		    =					super(snake_case__						, self    )._flatten_past_key_values_(
                     snake_case__						, snake_case__						, snake_case__						, snake_case__    )


"""simple docstring"""
from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowercase		:						Tuple =					logging.get_logger(__name__)




class    _snake_case  (						_lowercase       ):
							SCREAMING_SNAKE_CASE		     =						['''pixel_values''']






							def __init__(					self						:			Union[str, Any]						, snake_case__						:			bool = True						, snake_case__						:			Optional[Dict[str, int]] = None						, snake_case__						:			PILImageResampling = PILImageResampling.BILINEAR						, snake_case__						:			bool = True						, snake_case__						:			Union[int, float] = 1 / 2_5_5						, snake_case__						:			bool = True						, snake_case__						:			Optional[Union[float, List[float]]] = None						, snake_case__						:			Optional[Union[float, List[float]]] = None						, **snake_case__						:			Optional[int]						, ):






												"""simple docstring"""



												super().__init__(**snake_case__    )
												__a:						Dict		    =					size if size is not None else {"""height""": 2_2_4, """width""": 2_2_4}
												__a:						Optional[Any]		    =					get_size_dict(snake_case__    )
												__a:						Union[str, Any]		    =					do_resize
												__a:						str		    =					do_rescale
												__a:						Tuple		    =					do_normalize
												__a:						Dict		    =					size
												__a:						str		    =					resample
												__a:						Any		    =					rescale_factor
												__a:						Optional[int]		    =					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
												__a:						int		    =					image_std if image_std is not None else IMAGENET_STANDARD_STD






							def 							__lowercase  (					self						:			Optional[Any]						, snake_case__						:			np.ndarray						, snake_case__						:			Dict[str, int]						, snake_case__						:			PILImageResampling = PILImageResampling.BILINEAR						, snake_case__						:			Optional[Union[str, ChannelDimension]] = None						, **snake_case__						:			List[str]						, ):






												"""simple docstring"""



												__a:						int		    =					get_size_dict(snake_case__    )
												if "height" not in size or "width" not in size:
																	raise ValueError(f"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"""    )
												return resize(
												    snake_case__						, size=(size["""height"""], size["""width"""])						, resample=snake_case__						, data_format=snake_case__						, **snake_case__    )






							def 							__lowercase  (					self						:			List[str]						, snake_case__						:			np.ndarray						, snake_case__						:			float						, snake_case__						:			Optional[Union[str, ChannelDimension]] = None						, **snake_case__						:			Union[str, Any]    ):






												"""simple docstring"""



												return rescale(snake_case__						, scale=snake_case__						, data_format=snake_case__						, **snake_case__    )






							def 							__lowercase  (					self						:			Dict						, snake_case__						:			np.ndarray						, snake_case__						:			Union[float, List[float]]						, snake_case__						:			Union[float, List[float]]						, snake_case__						:			Optional[Union[str, ChannelDimension]] = None						, **snake_case__						:			Union[str, Any]						, ):






												"""simple docstring"""



												return normalize(snake_case__						, mean=snake_case__						, std=snake_case__						, data_format=snake_case__						, **snake_case__    )






							def 							__lowercase  (					self						:			Optional[int]						, snake_case__						:			ImageInput						, snake_case__						:			Optional[bool] = None						, snake_case__						:			Dict[str, int] = None						, snake_case__						:			PILImageResampling = None						, snake_case__						:			Optional[bool] = None						, snake_case__						:			Optional[float] = None						, snake_case__						:			Optional[bool] = None						, snake_case__						:			Optional[Union[float, List[float]]] = None						, snake_case__						:			Optional[Union[float, List[float]]] = None						, snake_case__						:			Optional[Union[str, TensorType]] = None						, snake_case__						:			Union[str, ChannelDimension] = ChannelDimension.FIRST						, **snake_case__						:			List[str]						, ):






												"""simple docstring"""



												__a:						Any		    =					do_resize if do_resize is not None else self.do_resize
												__a:						Any		    =					do_rescale if do_rescale is not None else self.do_rescale
												__a:						int		    =					do_normalize if do_normalize is not None else self.do_normalize
												__a:						Any		    =					resample if resample is not None else self.resample
												__a:						Any		    =					rescale_factor if rescale_factor is not None else self.rescale_factor
												__a:						Dict		    =					image_mean if image_mean is not None else self.image_mean
												__a:						List[str]		    =					image_std if image_std is not None else self.image_std

												__a:						int		    =					size if size is not None else self.size
												__a:						Any		    =					get_size_dict(snake_case__    )

												__a:						List[str]		    =					make_list_of_images(snake_case__    )

												if not valid_images(snake_case__    ):
																	raise ValueError(
																	    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
																	    """torch.Tensor, tf.Tensor or jax.ndarray."""    )

												if do_resize and size is None:
																	raise ValueError("""Size must be specified if do_resize is True."""    )

												if do_rescale and rescale_factor is None:
																	raise ValueError("""Rescale factor must be specified if do_rescale is True."""    )

												# All transformations expect numpy arrays.
												__a:						str		    =					[to_numpy_array(snake_case__    ) for image in images]

												if do_resize:
																	__a:						Dict		    =					[self.resize(image=snake_case__						, size=snake_case__						, resample=snake_case__    ) for image in images]

												if do_rescale:
																	__a:						str		    =					[self.rescale(image=snake_case__						, scale=snake_case__    ) for image in images]

												if do_normalize:
																	__a:						Dict		    =					[self.normalize(image=snake_case__						, mean=snake_case__						, std=snake_case__    ) for image in images]

												__a:						int		    =					[to_channel_dimension_format(snake_case__						, snake_case__    ) for image in images]

												__a:						str		    =					{"""pixel_values""": images}
												return BatchFeature(data=snake_case__						, tensor_type=snake_case__    )
