



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

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
    from ... import FeatureExtractionMixin, TensorType

UpperCAmelCase_									=							logging.get_logger(__name__)

UpperCAmelCase_									=							{
    "openai/imagegpt-small": "",
    "openai/imagegpt-medium": "",
    "openai/imagegpt-large": "",
}





class 		UpperCAmelCase						(						snake_case_				):
   SCREAMING_SNAKE_CASE__							=     '''imagegpt'''
   SCREAMING_SNAKE_CASE__							=     ['''past_key_values''']
   SCREAMING_SNAKE_CASE__							=     {
       '''hidden_size''': '''n_embd''',
       '''max_position_embeddings''': '''n_positions''',
       '''num_attention_heads''': '''n_head''',
       '''num_hidden_layers''': '''n_layer''',
   }





   def __init__(	self       ,     _lowerCAmelCase=512 + 1       ,     _lowerCAmelCase=32 * 32       ,     _lowerCAmelCase=512       ,     _lowerCAmelCase=24       ,     _lowerCAmelCase=8       ,     _lowerCAmelCase=None       ,     _lowerCAmelCase="quick_gelu"       ,     _lowerCAmelCase=0.1       ,     _lowerCAmelCase=0.1       ,     _lowerCAmelCase=0.1       ,     _lowerCAmelCase=1E-5       ,     _lowerCAmelCase=0.02       ,     _lowerCAmelCase=True       ,     _lowerCAmelCase=True       ,     _lowerCAmelCase=False       ,     _lowerCAmelCase=False       ,     _lowerCAmelCase=False       ,     **_lowerCAmelCase       ,     ):
         _lowerCAmelCase 			=  vocab_size
         _lowerCAmelCase 			=  n_positions
         _lowerCAmelCase 			=  n_embd
         _lowerCAmelCase 			=  n_layer
         _lowerCAmelCase 			=  n_head
         _lowerCAmelCase 			=  n_inner
         _lowerCAmelCase 			=  activation_function
         _lowerCAmelCase 			=  resid_pdrop
         _lowerCAmelCase 			=  embd_pdrop
         _lowerCAmelCase 			=  attn_pdrop
         _lowerCAmelCase 			=  layer_norm_epsilon
         _lowerCAmelCase 			=  initializer_range
         _lowerCAmelCase 			=  scale_attn_weights
         _lowerCAmelCase 			=  use_cache
         _lowerCAmelCase 			=  scale_attn_by_inverse_layer_idx
         _lowerCAmelCase 			=  reorder_and_upcast_attn
         _lowerCAmelCase 			=  tie_word_embeddings

         super().__init__(tie_word_embeddings=_lowerCAmelCase       ,     **_lowerCAmelCase			)






class 		UpperCAmelCase						(						snake_case_				):





   @property
   def 	__lowerCAmelCase							(	self			):
         return OrderedDict(
             [
                 ('''input_ids''', {0: '''batch''', 1: '''sequence'''}),
             ]			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase       ,     _lowerCAmelCase = 1       ,     _lowerCAmelCase = -1       ,     _lowerCAmelCase = False       ,     _lowerCAmelCase = None       ,     _lowerCAmelCase = 3       ,     _lowerCAmelCase = 32       ,     _lowerCAmelCase = 32       ,     ):
         _lowerCAmelCase 			=  self._generate_dummy_images(_lowerCAmelCase       ,     _lowerCAmelCase       ,     _lowerCAmelCase       ,     _lowerCAmelCase			)
         _lowerCAmelCase 			=  dict(preprocessor(images=_lowerCAmelCase       ,     return_tensors=_lowerCAmelCase			)			)

         return inputs











import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available
def 							UpperCAmelCase__		(				*_SCREAMING_SNAKE_CASE   :	Tuple				)->List[Any]:
      if not isinstance(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				):
            _lowerCAmelCase 			=  list(_SCREAMING_SNAKE_CASE				)
      for i in range(len(_SCREAMING_SNAKE_CASE				)				):
            _lowerCAmelCase 			=  None
      gc.collect()
      if is_xpu_available():
            torch.xpu.empty_cache()
      elif is_npu_available():
            torch.npu.empty_cache()
      else:
            torch.cuda.empty_cache()
      return objects
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	Exception				)->bool:
      _lowerCAmelCase 			=  [
          '''CUDA out of memory.''',  # CUDA OOM
          '''cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.''',  # CUDNN SNAFU
          '''DefaultCPUAllocator: can\'t allocate memory''',  # CPU OOM
      ]
      if isinstance(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				) and len(exception.args				) == 1:
            return any(err in exception.args[0] for err in _statements				)
      return False


def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	callable = None     ,				_SCREAMING_SNAKE_CASE   :	int = 1_2_8				)->Optional[int]:
      if function is None:
            return functools.partial(_SCREAMING_SNAKE_CASE     ,				starting_batch_size=_SCREAMING_SNAKE_CASE				)

      _lowerCAmelCase 			=  starting_batch_size

      def decorator(*_SCREAMING_SNAKE_CASE   :	Optional[int]     ,				**_SCREAMING_SNAKE_CASE   :	Optional[Any]				):
            nonlocal batch_size
            gc.collect()
            if is_xpu_available():
                  torch.xpu.empty_cache()
            elif is_npu_available():
                  torch.npu.empty_cache()
            else:
                  torch.cuda.empty_cache()
            _lowerCAmelCase 			=  list(inspect.signature(_SCREAMING_SNAKE_CASE				).parameters.keys()				)
            # Guard against user error
            if len(_SCREAMING_SNAKE_CASE				) < (len(_SCREAMING_SNAKE_CASE				) + 1):
                  _lowerCAmelCase 			=  ''', '''.join([f'''{arg}={value}''' for arg, value in zip(params[1:]     ,				args[1:]				)]				)
                  raise TypeError(
                      f'''Batch size was passed into `{function.__name__}` as the first argument when called.'''
                      f'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`'''				)
            while True:
                  if batch_size == 0:
                        raise RuntimeError('''No executable batch size found, reached zero.'''				)
                  try:
                        return function(_SCREAMING_SNAKE_CASE     ,				*_SCREAMING_SNAKE_CASE     ,				**_SCREAMING_SNAKE_CASE				)
                  except Exception as e:
                        if should_reduce_batch_size(_SCREAMING_SNAKE_CASE				):
                              gc.collect()
                              if is_xpu_available():
                                    torch.xpu.empty_cache()
                              elif is_npu_available():
                                    torch.npu.empty_cache()
                              else:
                                    torch.cuda.empty_cache()
                              batch_size //= 2
                        else:
                              raise

      return decorator