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


__snake_case         =     logging.get_logger(__name__)

__snake_case         =     {
    '''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       __snake_case (  lowerCamelCase__ ):
 __lowerCamelCase     : Union[str, Any] 	=   """gpt2"""
 __lowerCamelCase     : str 	=   ["""past_key_values"""]
 __lowerCamelCase     : Optional[int] 	=   {
     """hidden_size""": """n_embd""",
     """max_position_embeddings""": """n_positions""",
     """num_attention_heads""": """n_head""",
     """num_hidden_layers""": """n_layer""",
 }
 def __init__(	self		, snake_case__=5_0257		, snake_case__=1024		, snake_case__=768		, snake_case__=12		, snake_case__=12		, snake_case__=None		, snake_case__="gelu_new"		, snake_case__=0.1		, snake_case__=0.1		, snake_case__=0.1		, snake_case__=1e-5		, snake_case__=0.02		, snake_case__="cls_index"		, snake_case__=True		, snake_case__=None		, snake_case__=True		, snake_case__=0.1		, snake_case__=True		, snake_case__=True		, snake_case__=5_0256		, snake_case__=5_0256		, snake_case__=False		, snake_case__=False		, **snake_case__		, )						->				List[Any]:
       '''simple docstring'''





       UpperCAmelCase     :			int     	=vocab_size
       UpperCAmelCase     :			int     	=n_positions
       UpperCAmelCase     :			Union[str, Any]     	=n_embd
       UpperCAmelCase     :			Tuple     	=n_layer
       UpperCAmelCase     :			Optional[int]     	=n_head
       UpperCAmelCase     :			str     	=n_inner
       UpperCAmelCase     :			List[Any]     	=activation_function
       UpperCAmelCase     :			Tuple     	=resid_pdrop
       UpperCAmelCase     :			Any     	=embd_pdrop
       UpperCAmelCase     :			Tuple     	=attn_pdrop
       UpperCAmelCase     :			Optional[Any]     	=layer_norm_epsilon
       UpperCAmelCase     :			List[str]     	=initializer_range
       UpperCAmelCase     :			Optional[Any]     	=summary_type
       UpperCAmelCase     :			Union[str, Any]     	=summary_use_proj
       UpperCAmelCase     :			Dict     	=summary_activation
       UpperCAmelCase     :			str     	=summary_first_dropout
       UpperCAmelCase     :			List[Any]     	=summary_proj_to_labels
       UpperCAmelCase     :			Optional[int]     	=scale_attn_weights
       UpperCAmelCase     :			str     	=use_cache
       UpperCAmelCase     :			Tuple     	=scale_attn_by_inverse_layer_idx
       UpperCAmelCase     :			List[str]     	=reorder_and_upcast_attn

       UpperCAmelCase     :			Any     	=bos_token_id
       UpperCAmelCase     :			Optional[Any]     	=eos_token_id

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







class       __snake_case (  lowerCamelCase__ ):
 def __init__(	self		, snake_case__		, snake_case__ = "default"		, snake_case__ = None		, snake_case__ = False		, )						->				Optional[int]:
       '''simple docstring'''





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





       UpperCAmelCase     :			str     	=OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}    )
       if self.use_past:
             self.fill_with_past_key_values_(snake_case__		, direction='''inputs'''    )
             UpperCAmelCase     :			List[str]     	={0: '''batch''', 1: '''past_sequence + sequence'''}
       else:
             UpperCAmelCase     :			int     	={0: '''batch''', 1: '''sequence'''}

       return common_inputs
 @property
 def 						UpperCAmelCase__							(	self    )						->				int:
       '''simple docstring'''





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





       return self._config.n_head
 def 						UpperCAmelCase__							(	self		, snake_case__		, snake_case__ = -1		, snake_case__ = -1		, snake_case__ = False		, snake_case__ = None		, )						->				Mapping[str, Any]:
       '''simple docstring'''





       UpperCAmelCase     :			Tuple     	=super(snake_case__		, self    ).generate_dummy_inputs(
           snake_case__		, batch_size=snake_case__		, seq_length=snake_case__		, is_pair=snake_case__		, framework=snake_case__    )

       # We need to order the input in the way they appears in the forward()
       UpperCAmelCase     :			List[str]     	=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     :			Optional[int]     	=common_inputs['''input_ids'''].shape
                   # Not using the same length for past_key_values
                   UpperCAmelCase     :			Union[str, Any]     	=seqlen + 2
                   UpperCAmelCase     :			Dict     	=(
                       batch,
                       self.num_attention_heads,
                       past_key_values_length,
                       self._config.hidden_size // self.num_attention_heads,
                   )
                   UpperCAmelCase     :			List[Any]     	=[
                       (torch.zeros(snake_case__    ), torch.zeros(snake_case__    )) for _ in range(self.num_layers    )
                   ]

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

       return ordered_inputs






 @property
 def 						UpperCAmelCase__							(	self    )						->				int:
       '''simple docstring'''





       return 13

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 EfficientNetImageProcessor







class       __snake_case (  unittest.TestCase ):
 def __init__(	self		, snake_case__		, snake_case__=13		, snake_case__=3		, snake_case__=18		, snake_case__=30		, snake_case__=400		, snake_case__=True		, snake_case__=None		, snake_case__=True		, snake_case__=[0.5, 0.5, 0.5]		, snake_case__=[0.5, 0.5, 0.5]		, )						->				Any:
       '''simple docstring'''





       UpperCAmelCase     :			int     	=size if size is not None else {'''height''': 18, '''width''': 18}
       UpperCAmelCase     :			Tuple     	=parent
       UpperCAmelCase     :			List[str]     	=batch_size
       UpperCAmelCase     :			int     	=num_channels
       UpperCAmelCase     :			List[str]     	=image_size
       UpperCAmelCase     :			str     	=min_resolution
       UpperCAmelCase     :			Optional[int]     	=max_resolution
       UpperCAmelCase     :			Optional[int]     	=do_resize
       UpperCAmelCase     :			int     	=size
       UpperCAmelCase     :			Union[str, Any]     	=do_normalize
       UpperCAmelCase     :			Dict     	=image_mean
       UpperCAmelCase     :			List[Any]     	=image_std






 def 						UpperCAmelCase__							(	self    )						->				int:
       '''simple docstring'''





       return {
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_normalize": self.do_normalize,
           "do_resize": self.do_resize,
           "size": self.size,
       }







@require_torch
@require_vision
class       __snake_case (  lowerCamelCase__       , unittest.TestCase ):
 __lowerCamelCase     : Any 	=   EfficientNetImageProcessor if is_vision_available() else None
 def 						UpperCAmelCase__							(	self    )						->				Tuple:
       '''simple docstring'''





       UpperCAmelCase     :			Dict     	=EfficientNetImageProcessorTester(self    )
 @property
 def 						UpperCAmelCase__							(	self    )						->				int:
       '''simple docstring'''





       return self.image_processor_tester.prepare_image_processor_dict()
 def 						UpperCAmelCase__							(	self    )						->				List[str]:
       '''simple docstring'''





       UpperCAmelCase     :			Tuple     	=self.image_processing_class(**self.image_processor_dict    )
       self.assertTrue(hasattr(snake_case__		, '''image_mean'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''image_std'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''do_normalize'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''do_resize'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''size'''    )    )
 def 						UpperCAmelCase__							(	self    )						->				Tuple:
       '''simple docstring'''





       UpperCAmelCase     :			str     	=self.image_processing_class.from_dict(self.image_processor_dict    )
       self.assertEqual(image_processor.size		, {'''height''': 18, '''width''': 18}    )

       UpperCAmelCase     :			List[str]     	=self.image_processing_class.from_dict(self.image_processor_dict		, size=42    )
       self.assertEqual(image_processor.size		, {'''height''': 42, '''width''': 42}    )
 def 						UpperCAmelCase__							(	self    )						->				Dict:
       '''simple docstring'''





       UpperCAmelCase     :			List[Any]     	=self.image_processing_class(**self.image_processor_dict    )
       # create random PIL images
       UpperCAmelCase     :			Optional[Any]     	=prepare_image_inputs(self.image_processor_tester		, equal_resolution=snake_case__    )
       for image in image_inputs:
             self.assertIsInstance(snake_case__		, Image.Image    )

       # Test not batched input
       UpperCAmelCase     :			int     	=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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

       # Test batched
       UpperCAmelCase     :			int     	=image_processing(snake_case__		, 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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )
 def 						UpperCAmelCase__							(	self    )						->				Dict:
       '''simple docstring'''





       UpperCAmelCase     :			Tuple     	=self.image_processing_class(**self.image_processor_dict    )
       # create random numpy tensors
       UpperCAmelCase     :			List[str]     	=prepare_image_inputs(self.image_processor_tester		, equal_resolution=snake_case__		, numpify=snake_case__    )
       for image in image_inputs:
             self.assertIsInstance(snake_case__		, np.ndarray    )

       # Test not batched input
       UpperCAmelCase     :			str     	=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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

       # Test batched
       UpperCAmelCase     :			Union[str, Any]     	=image_processing(snake_case__		, 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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )






 def 						UpperCAmelCase__							(	self    )						->				str:
       '''simple docstring'''





       UpperCAmelCase     :			int     	=self.image_processing_class(**self.image_processor_dict    )
       # create random PyTorch tensors
       UpperCAmelCase     :			int     	=prepare_image_inputs(self.image_processor_tester		, equal_resolution=snake_case__		, torchify=snake_case__    )
       for image in image_inputs:
             self.assertIsInstance(snake_case__		, torch.Tensor    )

       # Test not batched input
       UpperCAmelCase     :			List[Any]     	=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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

       # Test batched
       UpperCAmelCase     :			List[str]     	=image_processing(snake_case__		, 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.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )
