





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


if is_vision_available():
   import PIL


lowerCamelCase_	:		List[Any]         =   logging.get_logger(__name__)

class 					_UpperCAmelCase    (  UpperCAmelCase__						):



      '''simple docstring'''
      lowercase_ :					str             =  ["""pixel_values"""]

      def __init__(	self   , snake_case_ = True   , snake_case_ = None   , snake_case_ = True   , snake_case_ = None   , snake_case_ = PILImageResampling.BICUBIC   , snake_case_ = True   , snake_case_ = 1 / 2_5_5   , snake_case_ = True   , snake_case_ = None   , snake_case_ = None   , **snake_case_   , ):

        """simple docstring"""

        super().__init__(**snake_case_    )
        A_    :			List[Any]									=    crop_size if crop_size is not None else {'height': 2_5_6, 'width': 2_5_6}
        A_    :			str									=    get_size_dict(snake_case_   , param_name='crop_size'    )
        A_    :			Any									=    size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
        A_    :			Any									=    get_size_dict(snake_case_    )

        A_    :			List[str]									=    do_center_crop
        A_    :			Optional[int]									=    crop_size
        A_    :			str									=    do_resize
        A_    :			str									=    size
        A_    :			List[Any]									=    resample
        A_    :			Optional[int]									=    do_rescale
        A_    :			Dict									=    rescale_factor
        A_    :			Optional[int]									=    do_normalize
        A_    :			List[Any]									=    image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
        A_    :			Tuple									=    image_std if image_std is not None else IMAGENET_DEFAULT_STD

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_   , snake_case_ = None   , snake_case_ = None   , **snake_case_   , ):

        """simple docstring"""

        A_    :			Optional[int]									=    self.size if size is None else size
        A_    :			Optional[Any]									=    get_size_dict(snake_case_    )
        A_    :			Union[str, Any]									=    get_size_dict(snake_case_   , param_name='crop_size'    )

        A_		,			A_    :			Union[str, Any]									=    get_image_size(snake_case_    )
        A_    :			str									=    min(snake_case_   , snake_case_    )
        A_    :			Tuple									=    (size['height'] / crop_size['height']) * min_dim
        A_    :			Any									=    (size['width'] / crop_size['width']) * min_dim
        return center_crop(snake_case_   , size=(cropped_height, cropped_width)   , data_format=snake_case_   , **snake_case_    )

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_   , snake_case_ = PIL.Image.BICUBIC   , snake_case_ = None   , **snake_case_   , ):

        """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    lowerCamelCase_     (	self   , snake_case_   , snake_case_   , snake_case_ = None   , **snake_case_   , ):

        """simple docstring"""

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

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_   , snake_case_   , snake_case_ = None   , **snake_case_   , ):

        """simple docstring"""

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

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = None   , snake_case_ = ChannelDimension.FIRST   , **snake_case_   , ):

        """simple docstring"""

        A_    :			List[Any]									=    do_center_crop if do_center_crop is not None else self.do_center_crop
        A_    :			List[str]									=    crop_size if crop_size is not None else self.crop_size
        A_    :			str									=    get_size_dict(snake_case_   , param_name='crop_size'    )
        A_    :			Union[str, Any]									=    do_resize if do_resize is not None else self.do_resize
        A_    :			Any									=    size if size is not None else self.size
        A_    :			Dict									=    get_size_dict(snake_case_    )
        A_    :			int									=    resample if resample is not None else self.resample
        A_    :			Any									=    do_rescale if do_rescale is not None else self.do_rescale
        A_    :			List[str]									=    rescale_factor if rescale_factor is not None else self.rescale_factor
        A_    :			List[Any]									=    do_normalize if do_normalize is not None else self.do_normalize
        A_    :			int									=    image_mean if image_mean is not None else self.image_mean
        A_    :			Tuple									=    image_std if image_std is not None else self.image_std

        A_    :			Tuple									=    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_center_crop and crop_size is None:
          raise ValueError('If `do_center_crop` is set to `True`, `crop_size` must be provided.'    )

        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.'    )

        if do_normalize and (image_mean is None or image_std is None):
          raise ValueError('Image mean and image standard deviation must be specified if do_normalize is True.'    )

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

        if do_center_crop:
          A_    :			Tuple									=    [self.center_crop(snake_case_   , snake_case_   , size=snake_case_    ) for image in images]

        if do_resize:
          A_    :			Any									=    [self.resize(image=snake_case_   , size=snake_case_   , resample=snake_case_    ) for image in images]

        if do_rescale:
          A_    :			Any									=    [self.rescale(image=snake_case_   , scale=snake_case_    ) for image in images]

        if do_normalize:
          A_    :			Any									=    [self.normalize(image=snake_case_   , mean=snake_case_   , std=snake_case_    ) for image in images]

        A_    :			Optional[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_    )




'''simple docstring'''


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

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


lowercase											=      logging.get_logger(__name__)

lowercase											=      {
    '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/config.json''',
    '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/config.json''',
    '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/config.json''',
    '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/config.json''',
    '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/config.json''',
}







class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 __magic_name__       :			str	=   "gpt2"
 __magic_name__       :			str	=   ["past_key_values"]
 __magic_name__       :			Dict	=   {
     "hidden_size": "n_embd",
     "max_position_embeddings": "n_positions",
     "num_attention_heads": "n_head",
     "num_hidden_layers": "n_layer",
 }




 def __init__( self				,	lowerCAmelCase_=5_0_2_5_7				,	lowerCAmelCase_=1_0_2_4				,	lowerCAmelCase_=7_6_8				,	lowerCAmelCase_=1_2				,	lowerCAmelCase_=1_2				,	lowerCAmelCase_=None				,	lowerCAmelCase_="gelu_new"				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=1e-5				,	lowerCAmelCase_=0.0_2				,	lowerCAmelCase_="cls_index"				,	lowerCAmelCase_=True				,	lowerCAmelCase_=None				,	lowerCAmelCase_=True				,	lowerCAmelCase_=0.1				,	lowerCAmelCase_=True				,	lowerCAmelCase_=True				,	lowerCAmelCase_=5_0_2_5_6				,	lowerCAmelCase_=5_0_2_5_6				,	lowerCAmelCase_=False				,	lowerCAmelCase_=False				,	**lowerCAmelCase_				,	)			->   Dict:



      """simple docstring"""







      a_									=vocab_size
      a_									=n_positions
      a_									=n_embd
      a_									=n_layer
      a_									=n_head
      a_									=n_inner
      a_									=activation_function
      a_									=resid_pdrop
      a_									=embd_pdrop
      a_									=attn_pdrop
      a_									=layer_norm_epsilon
      a_									=initializer_range
      a_									=summary_type
      a_									=summary_use_proj
      a_									=summary_activation
      a_									=summary_first_dropout
      a_									=summary_proj_to_labels
      a_									=scale_attn_weights
      a_									=use_cache
      a_									=scale_attn_by_inverse_layer_idx
      a_									=reorder_and_upcast_attn

      a_									=bos_token_id
      a_									=eos_token_id

      super().__init__(bos_token_id=lowerCAmelCase_				,	eos_token_id=lowerCAmelCase_				,	**lowerCAmelCase_)







class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 def __init__( self				,	lowerCAmelCase_				,	lowerCAmelCase_ = "default"				,	lowerCAmelCase_ = None				,	lowerCAmelCase_ = False				,	)			->   List[str]:



      """simple docstring"""







      super().__init__(lowerCAmelCase_				,	task=lowerCAmelCase_				,	patching_specs=lowerCAmelCase_				,	use_past=lowerCAmelCase_)
      if not getattr(self._config				,	"pad_token_id"				,	lowerCAmelCase_):
           # TODO: how to do that better?
           a_									=0




 @property
 def    lowercase_	( self)			->   Mapping[str, Mapping[int, str]]:



      """simple docstring"""







      a_									=OrderedDict({"input_ids": {0: "batch", 1: "sequence"}})
      if self.use_past:
           self.fill_with_past_key_values_(lowerCAmelCase_				,	direction="inputs")
           a_									={0: "batch", 1: "past_sequence + sequence"}
      else:
           a_									={0: "batch", 1: "sequence"}

      return common_inputs




 @property
 def    lowercase_	( self)			->   int:



      """simple docstring"""







      return self._config.n_layer




 @property
 def    lowercase_	( self)			->   int:



      """simple docstring"""







      return self._config.n_head




 def    lowercase_	( self				,	lowerCAmelCase_				,	lowerCAmelCase_ = -1				,	lowerCAmelCase_ = -1				,	lowerCAmelCase_ = False				,	lowerCAmelCase_ = None				,	)			->   Mapping[str, Any]:



      """simple docstring"""







      a_									=super(lowerCAmelCase_				,	self).generate_dummy_inputs(
          lowerCAmelCase_				,	batch_size=lowerCAmelCase_				,	seq_length=lowerCAmelCase_				,	is_pair=lowerCAmelCase_				,	framework=lowerCAmelCase_)

      # We need to order the input in the way they appears in the forward()
      a_									=OrderedDict({"input_ids": common_inputs["input_ids"]})

      # Need to add the past_keys
      if self.use_past:
           if not is_torch_available():
                raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")
           else:
                import torch

                a_		,    a_									=common_inputs["input_ids"].shape
                # Not using the same length for past_key_values
                a_									=seqlen + 2
                a_									=(
                    batch,
                    self.num_attention_heads,
                    past_key_values_length,
                    self._config.hidden_size // self.num_attention_heads,
                )
                a_									=[
                    (torch.zeros(lowerCAmelCase_), torch.zeros(lowerCAmelCase_)) for _ in range(self.num_layers)
                ]

      a_									=common_inputs["attention_mask"]
      if self.use_past:
           a_									=ordered_inputs["attention_mask"].dtype
           a_									=torch.cat(
               [ordered_inputs["attention_mask"], torch.ones(lowerCAmelCase_				,	lowerCAmelCase_				,	dtype=lowerCAmelCase_)]				,	dim=1)

      return ordered_inputs




 @property
 def    lowercase_	( self)			->   int:



      """simple docstring"""







      return 1_3


