

'''simple docstring'''

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

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


lowercase__         =      logging.get_logger(__name__)

lowercase__         =      {
    "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json",
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}




class 	A_			(    _snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:     List[str]  		=  """blenderbot-small"""
     UpperCAmelCase_						:     Union[str, Any]  		=  ["""past_key_values"""]
     UpperCAmelCase_						:     Any  		=  {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}


     def __init__(		self			:					Optional[Any]					,      lowercase_			:					int=50_265					,      lowercase_			:					Optional[int]=512					,      lowercase_			:					Union[str, Any]=8					,      lowercase_			:					List[Any]=2_048					,      lowercase_			:					str=16					,      lowercase_			:					Any=8					,      lowercase_			:					int=2_048					,      lowercase_			:					Dict=16					,      lowercase_			:					Tuple=0.0					,      lowercase_			:					Optional[int]=0.0					,      lowercase_			:					List[Any]=True					,      lowercase_			:					List[str]=True					,      lowercase_			:					Any="gelu"					,      lowercase_			:					Union[str, Any]=512					,      lowercase_			:					Union[str, Any]=0.1					,      lowercase_			:					str=0.0					,      lowercase_			:					List[Any]=0.0					,      lowercase_			:					Dict=0.02					,      lowercase_			:					Any=1					,      lowercase_			:					str=False					,      lowercase_			:					Any=0					,      lowercase_			:					Optional[int]=1					,      lowercase_			:					Tuple=2					,      lowercase_			:					Optional[int]=2					,      **lowercase_			:					Union[str, Any]					,      )						->     List[str]:
      UpperCAmelCase	:     int						 =			vocab_size
      UpperCAmelCase	:     Union[str, Any]						 =			max_position_embeddings
      UpperCAmelCase	:     Any						 =			d_model
      UpperCAmelCase	:     Union[str, Any]						 =			encoder_ffn_dim
      UpperCAmelCase	:     Optional[Any]						 =			encoder_layers
      UpperCAmelCase	:     List[Any]						 =			encoder_attention_heads
      UpperCAmelCase	:     int						 =			decoder_ffn_dim
      UpperCAmelCase	:     Optional[Any]						 =			decoder_layers
      UpperCAmelCase	:     Dict						 =			decoder_attention_heads
      UpperCAmelCase	:     Any						 =			dropout
      UpperCAmelCase	:     Optional[Any]						 =			attention_dropout
      UpperCAmelCase	:     Tuple						 =			activation_dropout
      UpperCAmelCase	:     Any						 =			activation_function
      UpperCAmelCase	:     List[Any]						 =			init_std
      UpperCAmelCase	:     Union[str, Any]						 =			encoder_layerdrop
      UpperCAmelCase	:     Union[str, Any]						 =			decoder_layerdrop
      UpperCAmelCase	:     Optional[Any]						 =			use_cache
      UpperCAmelCase	:     Tuple						 =			encoder_layers
      UpperCAmelCase	:     List[str]						 =			scale_embedding  # scale factor will be sqrt(d_model) if True

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




class 	A_			(    _snake_case							):






     '''simple docstring'''



     @property
     def        UpperCAmelCase_	(		self			:					Any	)						->     Mapping[str, Mapping[int, str]]:
      if self.task in ["default", "seq2seq-lm"]:
       UpperCAmelCase	:     List[Any]						 =			OrderedDict(
           [
               ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
               ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
           ]	)

       if self.use_past:
        UpperCAmelCase	:     int						 =			{0: 'batch'}
        UpperCAmelCase	:     List[str]						 =			{0: 'batch', 1: 'past_decoder_sequence + sequence'}
       else:
        UpperCAmelCase	:     str						 =			{0: 'batch', 1: 'decoder_sequence'}
        UpperCAmelCase	:     Optional[Any]						 =			{0: 'batch', 1: 'decoder_sequence'}

       if self.use_past:
        self.fill_with_past_key_values_(lowercase_					,      direction='inputs'	)
      elif self.task == "causal-lm":
       # TODO: figure this case out.
       UpperCAmelCase	:     Union[str, Any]						 =			OrderedDict(
           [
               ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
               ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
           ]	)
       if self.use_past:
        UpperCAmelCase ,			UpperCAmelCase	:     int						 =			self.num_layers
        for i in range(lowercase_	):
         UpperCAmelCase	:     List[Any]						 =			{0: 'batch', 2: 'past_sequence + sequence'}
         UpperCAmelCase	:     Tuple						 =			{0: 'batch', 2: 'past_sequence + sequence'}
      else:
       UpperCAmelCase	:     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        UpperCAmelCase_	(		self			:					Tuple	)						->     Mapping[str, Mapping[int, str]]:
      if self.task in ["default", "seq2seq-lm"]:
       UpperCAmelCase	:     int						 =			super().outputs
      else:
       UpperCAmelCase	:     Optional[Any]						 =			super(lowercase_					,      self	).outputs
       if self.use_past:
        UpperCAmelCase ,			UpperCAmelCase	:     List[str]						 =			self.num_layers
        for i in range(lowercase_	):
         UpperCAmelCase	:     Tuple						 =			{0: 'batch', 2: 'past_sequence + sequence'}
         UpperCAmelCase	:     Optional[Any]						 =			{0: 'batch', 2: 'past_sequence + sequence'}
      return common_outputs


     def        UpperCAmelCase_	(		self			:					Optional[int]					,      lowercase_			:					PreTrainedTokenizer					,      lowercase_			:					int = -1					,      lowercase_			:					int = -1					,      lowercase_			:					bool = False					,      lowercase_			:					Optional[TensorType] = None					,      )						->     Mapping[str, Any]:
      UpperCAmelCase	:     Any						 =			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          lowercase_					,      lowercase_					,      lowercase_					,      lowercase_					,      lowercase_	)

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

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

       UpperCAmelCase	:     Optional[Any]						 =			torch.cat(
           [common_inputs['decoder_attention_mask'], torch.ones(lowercase_					,      lowercase_	)]					,      dim=1	)

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

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


     def        UpperCAmelCase_	(		self			:					Dict					,      lowercase_			:					PreTrainedTokenizer					,      lowercase_			:					int = -1					,      lowercase_			:					int = -1					,      lowercase_			:					bool = False					,      lowercase_			:					Optional[TensorType] = None					,      )						->     Mapping[str, Any]:
      UpperCAmelCase	:     Any						 =			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          lowercase_					,      lowercase_					,      lowercase_					,      lowercase_					,      lowercase_	)

      if self.use_past:
       if not is_torch_available():
        raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'	)
       else:
        import torch
       UpperCAmelCase ,			UpperCAmelCase	:     Optional[Any]						 =			common_inputs['input_ids'].shape
       # Not using the same length for past_key_values
       UpperCAmelCase	:     Tuple						 =			seqlen + 2
       UpperCAmelCase ,			UpperCAmelCase	:     Dict						 =			self.num_layers
       UpperCAmelCase ,			UpperCAmelCase	:     str						 =			self.num_attention_heads
       UpperCAmelCase	:     Tuple						 =			(
           batch,
           num_encoder_attention_heads,
           past_key_values_length,
           self._config.hidden_size // num_encoder_attention_heads,
       )

       UpperCAmelCase	:     Optional[Any]						 =			common_inputs['attention_mask'].dtype
       UpperCAmelCase	:     str						 =			torch.cat(
           [common_inputs['attention_mask'], torch.ones(lowercase_					,      lowercase_					,      dtype=lowercase_	)]					,      dim=1	)
       UpperCAmelCase	:     Union[str, Any]						 =			[
           (torch.zeros(lowercase_	), torch.zeros(lowercase_	)) for _ in range(lowercase_	)
       ]
      return common_inputs


     def        UpperCAmelCase_	(		self			:					Union[str, Any]					,      lowercase_			:					PreTrainedTokenizer					,      lowercase_			:					int = -1					,      lowercase_			:					int = -1					,      lowercase_			:					bool = False					,      lowercase_			:					Optional[TensorType] = None					,      )						->     Mapping[str, Any]:
      # Copied from OnnxConfig.generate_dummy_inputs
      # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
      # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
      UpperCAmelCase	:     Any						 =			compute_effective_axis_dimension(
          lowercase_					,      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
      UpperCAmelCase	:     Any						 =			tokenizer.num_special_tokens_to_add(lowercase_	)
      UpperCAmelCase	:     Optional[Any]						 =			compute_effective_axis_dimension(
          lowercase_					,      fixed_dimension=OnnxConfig.default_fixed_sequence					,      num_token_to_add=lowercase_	)

      # Generate dummy inputs according to compute batch and sequence
      UpperCAmelCase	:     Tuple						 =			[' '.join([tokenizer.unk_token]	) * seq_length] * batch_size
      UpperCAmelCase	:     Dict						 =			dict(tokenizer(lowercase_					,      return_tensors=lowercase_	)	)
      return common_inputs


     def        UpperCAmelCase_	(		self			:					Optional[int]					,      lowercase_			:					PreTrainedTokenizer					,      lowercase_			:					int = -1					,      lowercase_			:					int = -1					,      lowercase_			:					bool = False					,      lowercase_			:					Optional[TensorType] = None					,      )						->     Mapping[str, Any]:
      if self.task in ["default", "seq2seq-lm"]:
       UpperCAmelCase	:     List[Any]						 =			self._generate_dummy_inputs_for_default_and_seqaseq_lm(
           lowercase_					,      batch_size=lowercase_					,      seq_length=lowercase_					,      is_pair=lowercase_					,      framework=lowercase_	)

      elif self.task == "causal-lm":
       UpperCAmelCase	:     Union[str, Any]						 =			self._generate_dummy_inputs_for_causal_lm(
           lowercase_					,      batch_size=lowercase_					,      seq_length=lowercase_					,      is_pair=lowercase_					,      framework=lowercase_	)
      else:
       UpperCAmelCase	:     Tuple						 =			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           lowercase_					,      batch_size=lowercase_					,      seq_length=lowercase_					,      is_pair=lowercase_					,      framework=lowercase_	)

      return common_inputs




     def        UpperCAmelCase_	(		self			:					List[str]					,      lowercase_			:					Union[str, Any]					,      lowercase_			:					Dict					,      lowercase_			:					List[str]					,      lowercase_			:					Optional[Any]	)						->     Any:
      if self.task in ["default", "seq2seq-lm"]:
       UpperCAmelCase	:     Optional[int]						 =			super()._flatten_past_key_values_(lowercase_					,      lowercase_					,      lowercase_					,      lowercase_	)
      else:
       UpperCAmelCase	:     Tuple						 =			super(lowercase_					,      self	)._flatten_past_key_values_(
           lowercase_					,      lowercase_					,      lowercase_					,      lowercase_	)







'''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 rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
 import PIL


lowercase__         =      logging.get_logger(__name__)




def 				UpperCamelCase(					UpperCAmelCase_  ,	UpperCAmelCase_     ):
 UpperCAmelCase	:     Optional[Any]						 =			b.T
 UpperCAmelCase	:     Optional[int]						 =			np.sum(np.square(UpperCAmelCase_     )  ,	axis=1     )
 UpperCAmelCase	:     List[Any]						 =			np.sum(np.square(UpperCAmelCase_     )  ,	axis=0     )
 UpperCAmelCase	:     List[str]						 =			np.matmul(UpperCAmelCase_  ,	UpperCAmelCase_     )
 UpperCAmelCase	:     Union[str, Any]						 =			aa[:, None] - 2 * ab + ba[None, :]
 return d




def 				UpperCamelCase(					UpperCAmelCase_  ,	UpperCAmelCase_     ):
 UpperCAmelCase	:     int						 =			x.reshape(-1  ,	3     )
 UpperCAmelCase	:     Optional[int]						 =			squared_euclidean_distance(UpperCAmelCase_  ,	UpperCAmelCase_     )
 return np.argmin(UpperCAmelCase_  ,	axis=1     )




class 	A_			(    _snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:     List[Any]  		=  ["""pixel_values"""]


     def __init__(		self			:					List[Any]					,      lowercase_			:					Optional[Union[List[List[int]], np.ndarray]] = None					,      lowercase_			:					bool = True					,      lowercase_			:					Dict[str, int] = None					,      lowercase_			:					PILImageResampling = PILImageResampling.BILINEAR					,      lowercase_			:					bool = True					,      lowercase_			:					bool = True					,      **lowercase_			:					Optional[Any]					,      )						->     None:
      super().__init__(**lowercase_	)
      UpperCAmelCase	:     Any						 =			size if size is not None else {'height': 256, 'width': 256}
      UpperCAmelCase	:     List[Any]						 =			get_size_dict(lowercase_	)
      UpperCAmelCase	:     str						 =			np.array(lowercase_	) if clusters is not None else None
      UpperCAmelCase	:     Any						 =			do_resize
      UpperCAmelCase	:     List[Any]						 =			size
      UpperCAmelCase	:     Any						 =			resample
      UpperCAmelCase	:     Dict						 =			do_normalize
      UpperCAmelCase	:     List[Any]						 =			do_color_quantize


     def        UpperCAmelCase_	(		self			:					int					,      lowercase_			:					np.ndarray					,      lowercase_			:					Dict[str, int]					,      lowercase_			:					PILImageResampling = PILImageResampling.BILINEAR					,      lowercase_			:					Optional[Union[str, ChannelDimension]] = None					,      **lowercase_			:					Any					,      )						->     np.ndarray:
      UpperCAmelCase	:     Dict						 =			get_size_dict(lowercase_	)
      if "height" not in size or "width" not in size:
       raise ValueError(f"""Size dictionary must contain both height and width keys. Got {size.keys()}"""	)
      return resize(
          lowercase_					,      size=(size['height'], size['width'])					,      resample=lowercase_					,      data_format=lowercase_					,      **lowercase_	)


     def        UpperCAmelCase_	(		self			:					Optional[int]					,      lowercase_			:					np.ndarray					,      lowercase_			:					Optional[Union[str, ChannelDimension]] = None					,      )						->     np.ndarray:
      UpperCAmelCase	:     int						 =			rescale(image=lowercase_					,      scale=1 / 127.5					,      data_format=lowercase_	)
      UpperCAmelCase	:     Dict						 =			image - 1
      return image




     def        UpperCAmelCase_	(		self			:					str					,      lowercase_			:					ImageInput					,      lowercase_			:					bool = None					,      lowercase_			:					Dict[str, int] = None					,      lowercase_			:					PILImageResampling = None					,      lowercase_			:					bool = None					,      lowercase_			:					Optional[bool] = None					,      lowercase_			:					Optional[Union[List[List[int]], np.ndarray]] = None					,      lowercase_			:					Optional[Union[str, TensorType]] = None					,      lowercase_			:					Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST					,      **lowercase_			:					List[str]					,      )						->     PIL.Image.Image:
      UpperCAmelCase	:     Optional[int]						 =			do_resize if do_resize is not None else self.do_resize
      UpperCAmelCase	:     Optional[Any]						 =			size if size is not None else self.size
      UpperCAmelCase	:     Optional[int]						 =			get_size_dict(lowercase_	)
      UpperCAmelCase	:     Any						 =			resample if resample is not None else self.resample
      UpperCAmelCase	:     Optional[Any]						 =			do_normalize if do_normalize is not None else self.do_normalize
      UpperCAmelCase	:     str						 =			do_color_quantize if do_color_quantize is not None else self.do_color_quantize
      UpperCAmelCase	:     Optional[int]						 =			clusters if clusters is not None else self.clusters
      UpperCAmelCase	:     List[str]						 =			np.array(lowercase_	)

      UpperCAmelCase	:     int						 =			make_list_of_images(lowercase_	)

      if not valid_images(lowercase_	):
       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 or resample is None:
       raise ValueError('Size and resample must be specified if do_resize is True.'	)

      if do_color_quantize and clusters is None:
       raise ValueError('Clusters must be specified if do_color_quantize is True.'	)

      # All transformations expect numpy arrays.
      UpperCAmelCase	:     Dict						 =			[to_numpy_array(lowercase_	) for image in images]

      if do_resize:
       UpperCAmelCase	:     List[Any]						 =			[self.resize(image=lowercase_					,      size=lowercase_					,      resample=lowercase_	) for image in images]

      if do_normalize:
       UpperCAmelCase	:     Tuple						 =			[self.normalize(image=lowercase_	) for image in images]

      if do_color_quantize:
       UpperCAmelCase	:     List[str]						 =			[to_channel_dimension_format(lowercase_					,      ChannelDimension.LAST	) for image in images]
       # color quantize from (batch_size, height, width, 3) to (batch_size, height, width)
       UpperCAmelCase	:     int						 =			np.array(lowercase_	)
       UpperCAmelCase	:     str						 =			color_quantize(lowercase_					,      lowercase_	).reshape(images.shape[:-1]	)

       # flatten to (batch_size, height*width)
       UpperCAmelCase	:     Optional[int]						 =			images.shape[0]
       UpperCAmelCase	:     Union[str, Any]						 =			images.reshape(lowercase_					,      -1	)

       # We need to convert back to a list of images to keep consistent behaviour across processors.
       UpperCAmelCase	:     int						 =			list(lowercase_	)
      else:
       UpperCAmelCase	:     Dict						 =			[to_channel_dimension_format(lowercase_					,      lowercase_	) for image in images]

      UpperCAmelCase	:     Any						 =			{'input_ids': images}
      return BatchFeature(data=lowercase_					,      tensor_type=lowercase_	)




