

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


_a				: Optional[int] 							=						logging.get_logger(__name__)

_a				: Optional[Any] 							=						{
    '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        lowercase_	(		a ):

  '''simple docstring'''





  __lowerCAmelCase					: int      =   "gpt2"
  __lowerCAmelCase					: List[str]      =   ["past_key_values"]
  __lowerCAmelCase					: List[str]      =   {
      "hidden_size": "n_embd",
      "max_position_embeddings": "n_positions",
      "num_attention_heads": "n_head",
      "num_hidden_layers": "n_layer",
  }
  def __init__(     self   , a_=5_0_2_5_7   , a_=1_0_2_4   , a_=7_6_8   , a_=1_2   , a_=1_2   , a_=None   , a_="gelu_new"   , a_=0.1   , a_=0.1   , a_=0.1   , a_=1E-5   , a_=0.02   , a_="cls_index"   , a_=True   , a_=None   , a_=True   , a_=0.1   , a_=True   , a_=True   , a_=5_0_2_5_6   , a_=5_0_2_5_6   , a_=False   , a_=False   , **a_   , )							->				Tuple:


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

    UpperCAmelCase													=	bos_token_id
    UpperCAmelCase													=	eos_token_id

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




class        lowercase_	(		a ):

  '''simple docstring'''





  def __init__(     self   , a_   , a_ = "default"   , a_ = None   , a_ = False   , )							->				Optional[int]:


    """simple docstring"""
    super().__init__(a_   , task=a_   , patching_specs=a_   , use_past=a_     )
    if not getattr(self._config   , 'pad_token_id'   , a_     ):
      # TODO: how to do that better?
      UpperCAmelCase													=	0
  @property
  def 						snake_case_			(     self     )							->				Mapping[str, Mapping[int, str]]:


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

    return common_inputs
  @property
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    return self._config.n_layer
  @property
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    return self._config.n_head
  def 						snake_case_			(     self   , a_   , a_ = -1   , a_ = -1   , a_ = False   , a_ = None   , )							->				Mapping[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	super(a_   , self     ).generate_dummy_inputs(
        a_   , batch_size=a_   , seq_length=a_   , is_pair=a_   , framework=a_     )

    # We need to order the input in the way they appears in the forward()
    UpperCAmelCase													=	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

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

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

    return ordered_inputs





  @property
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    return 1_3








'''simple docstring'''




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import MobileNetVaImageProcessor



class        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





  def __init__(     self   , a_   , a_=7   , a_=3   , a_=1_8   , a_=3_0   , a_=4_0_0   , a_=True   , a_=None   , a_=True   , a_=None   , )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	size if size is not None else {'shortest_edge': 2_0}
    UpperCAmelCase													=	crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}
    UpperCAmelCase													=	parent
    UpperCAmelCase													=	batch_size
    UpperCAmelCase													=	num_channels
    UpperCAmelCase													=	image_size
    UpperCAmelCase													=	min_resolution
    UpperCAmelCase													=	max_resolution
    UpperCAmelCase													=	do_resize
    UpperCAmelCase													=	size
    UpperCAmelCase													=	do_center_crop
    UpperCAmelCase													=	crop_size





  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    return {
        "do_resize": self.do_resize,
        "size": self.size,
        "do_center_crop": self.do_center_crop,
        "crop_size": self.crop_size,
    }




@require_torch
@require_vision
class        lowercase_	(		a ,       unittest.TestCase ):

  '''simple docstring'''





  __lowerCAmelCase					: Union[str, Any]      =   MobileNetVaImageProcessor if is_vision_available() else None
  def 						snake_case_			(     self     )							->				Dict:


    """simple docstring"""
    UpperCAmelCase													=	MobileNetVaImageProcessingTester(self     )
  @property
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    return self.image_processor_tester.prepare_image_processor_dict()
  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    self.assertTrue(hasattr(a_   , 'do_resize'     )     )
    self.assertTrue(hasattr(a_   , 'size'     )     )
    self.assertTrue(hasattr(a_   , 'do_center_crop'     )     )
    self.assertTrue(hasattr(a_   , 'center_crop'     )     )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	self.image_processing_class.from_dict(self.image_processor_dict     )
    self.assertEqual(image_processor.size   , {'shortest_edge': 2_0}     )
    self.assertEqual(image_processor.crop_size   , {'height': 1_8, 'width': 1_8}     )

    UpperCAmelCase													=	self.image_processing_class.from_dict(self.image_processor_dict   , size=4_2   , crop_size=8_4     )
    self.assertEqual(image_processor.size   , {'shortest_edge': 4_2}     )
    self.assertEqual(image_processor.crop_size   , {'height': 8_4, 'width': 8_4}     )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    pass
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random PIL images
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , Image.Image     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random numpy tensors
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_   , numpify=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , np.ndarray     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )





  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random PyTorch tensors
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_   , torchify=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , torch.Tensor     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )


