


"""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_video_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import VideoMAEImageProcessor
class snake_case   (	unittest.TestCase):







		def __init__(       self   :							int     , a__   :							str     , a__   :							List[Any]=7     , a__   :							Union[str, Any]=3     , a__   :							List[str]=10     , a__   :							Dict=18     , a__   :							List[Any]=30     , a__   :							Optional[int]=4_00     , a__   :							int=True     , a__   :							List[str]=None     , a__   :							Union[str, Any]=True     , a__   :							Any=[0.5, 0.5, 0.5]     , a__   :							str=[0.5, 0.5, 0.5]     , a__   :							Union[str, Any]=None     , )							->    Union[str, Any]:





						'''simple docstring'''







						_A       	=      size if size is not None else {"shortest_edge": 18}
						_A       	=      crop_size if crop_size is not None else {"height": 18, "width": 18}

						_A       	=      parent
						_A       	=      batch_size
						_A       	=      num_channels
						_A       	=      num_frames
						_A       	=      image_size
						_A       	=      min_resolution
						_A       	=      max_resolution
						_A       	=      do_resize
						_A       	=      size
						_A       	=      do_normalize
						_A       	=      image_mean
						_A       	=      image_std
						_A       	=      crop_size







		def 			a_			(       self   :							List[Any]		)							->    Dict:





						'''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,
						    "crop_size": self.crop_size,
						}





@require_torch
@require_vision
class snake_case   (	lowercase_  ,					unittest.TestCase):
		__UpperCamelCase							     =      VideoMAEImageProcessor if is_vision_available() else None







		def 			a_			(       self   :							Union[str, Any]		)							->    Dict:





						'''simple docstring'''







						_A       	=      VideoMAEImageProcessingTester(self		)







		@property
		def 			a_			(       self   :							Optional[int]		)							->    Any:





						'''simple docstring'''







						return self.image_processor_tester.prepare_image_processor_dict()







		def 			a_			(       self   :							str		)							->    Optional[int]:





						'''simple docstring'''







						_A       	=      self.image_processing_class(**self.image_processor_dict		)
						self.assertTrue(hasattr(a__     , "image_mean"		)		)
						self.assertTrue(hasattr(a__     , "image_std"		)		)
						self.assertTrue(hasattr(a__     , "do_normalize"		)		)
						self.assertTrue(hasattr(a__     , "do_resize"		)		)
						self.assertTrue(hasattr(a__     , "do_center_crop"		)		)
						self.assertTrue(hasattr(a__     , "size"		)		)







		def 			a_			(       self   :							Optional[int]		)							->    Any:





						'''simple docstring'''







						_A       	=      self.image_processing_class.from_dict(self.image_processor_dict		)
						self.assertEqual(image_processor.size     , {"shortest_edge": 18}		)
						self.assertEqual(image_processor.crop_size     , {"height": 18, "width": 18}		)

						_A       	=      self.image_processing_class.from_dict(self.image_processor_dict     , size=42     , crop_size=84		)
						self.assertEqual(image_processor.size     , {"shortest_edge": 42}		)
						self.assertEqual(image_processor.crop_size     , {"height": 84, "width": 84}		)







		def 			a_			(       self   :							List[str]		)							->    Union[str, Any]:





						'''simple docstring'''







						pass







		def 			a_			(       self   :							Optional[Any]		)							->    Optional[Any]:





						'''simple docstring'''







						_A       	=      self.image_processing_class(**self.image_processor_dict		)
						# create random PIL videos
						_A       	=      prepare_video_inputs(self.image_processor_tester     , equal_resolution=a__		)
						for video in video_inputs:
										self.assertIsInstance(a__     , a__		)
										self.assertIsInstance(video[0]     , Image.Image		)

						# Test not batched input
						_A       	=      image_processing(video_inputs[0]     , return_tensors="pt"		).pixel_values
						self.assertEqual(
						    encoded_videos.shape     , (
						        1,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )     , )

						# Test batched
						_A       	=      image_processing(a__     , return_tensors="pt"		).pixel_values
						self.assertEqual(
						    encoded_videos.shape     , (
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )     , )







		def 			a_			(       self   :							Optional[int]		)							->    Optional[Any]:





						'''simple docstring'''







						_A       	=      self.image_processing_class(**self.image_processor_dict		)
						# create random numpy tensors
						_A       	=      prepare_video_inputs(self.image_processor_tester     , equal_resolution=a__     , numpify=a__		)
						for video in video_inputs:
										self.assertIsInstance(a__     , a__		)
										self.assertIsInstance(video[0]     , np.ndarray		)

						# Test not batched input
						_A       	=      image_processing(video_inputs[0]     , return_tensors="pt"		).pixel_values
						self.assertEqual(
						    encoded_videos.shape     , (
						        1,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )     , )

						# Test batched
						_A       	=      image_processing(a__     , return_tensors="pt"		).pixel_values
						self.assertEqual(
						    encoded_videos.shape     , (
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )     , )







		def 			a_			(       self   :							int		)							->    Dict:





						'''simple docstring'''







						_A       	=      self.image_processing_class(**self.image_processor_dict		)
						# create random PyTorch tensors
						_A       	=      prepare_video_inputs(self.image_processor_tester     , equal_resolution=a__     , torchify=a__		)
						for video in video_inputs:
										self.assertIsInstance(a__     , a__		)
										self.assertIsInstance(video[0]     , torch.Tensor		)

						# Test not batched input
						_A       	=      image_processing(video_inputs[0]     , return_tensors="pt"		).pixel_values
						self.assertEqual(
						    encoded_videos.shape     , (
						        1,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )     , )

						# Test batched
						_A       	=      image_processing(a__     , return_tensors="pt"		).pixel_values
						self.assertEqual(
						    encoded_videos.shape     , (
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size["height"],
						        self.image_processor_tester.crop_size["width"],
						    )     , )

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


_UpperCAmelCase    			=					logging.get_logger(__name__)

_UpperCAmelCase    			=					{
    "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 		__magic_name__				( lowercase_					):


  """simple docstring"""





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


  def __init__(					self	,		a__=5_02_57	,		a__=10_24	,		a__=7_68	,		a__=12	,		a__=12	,		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_02_56	,		a__=5_02_56	,		a__=False	,		a__=False	,		**a__	,		):
    _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      =						summary_type
    _lowerCamelCase      =						summary_use_proj
    _lowerCamelCase      =						summary_activation
    _lowerCamelCase      =						summary_first_dropout
    _lowerCamelCase      =						summary_proj_to_labels
    _lowerCamelCase      =						scale_attn_weights
    _lowerCamelCase      =						use_cache
    _lowerCamelCase      =						scale_attn_by_inverse_layer_idx
    _lowerCamelCase      =						reorder_and_upcast_attn

    _lowerCamelCase      =						bos_token_id
    _lowerCamelCase      =						eos_token_id

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







class 		__magic_name__				( lowercase_					):


  """simple docstring"""





  def __init__(					self	,		a__	,		a__ = "default"	,		a__ = None	,		a__ = False	,		):
    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?
      _lowerCamelCase      =						0


  @property
  def     _UpperCAmelCase  (					self		):
    _lowerCamelCase      =						OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}		)
    if self.use_past:
      self.fill_with_past_key_values_(a__	,		direction='''inputs'''		)
      _lowerCamelCase      =						{0: '''batch''', 1: '''past_sequence + sequence'''}
    else:
      _lowerCamelCase      =						{0: '''batch''', 1: '''sequence'''}

    return common_inputs


  @property
  def     _UpperCAmelCase  (					self		):
    return self._config.n_layer


  @property
  def     _UpperCAmelCase  (					self		):
    return self._config.n_head


  def     _UpperCAmelCase  (					self	,		a__	,		a__ = -1	,		a__ = -1	,		a__ = False	,		a__ = None	,		):
    _lowerCamelCase      =						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()
    _lowerCamelCase      =						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

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

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

    return ordered_inputs


  @property
  def     _UpperCAmelCase  (					self		):
    return 13




