



import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import CvtForImageClassification, CvtModel
					from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor






class 							lowercase__     (							lowercase       ):





	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       self.config_class(**self.inputs_dict			)
							self.parent.assertTrue(hasattr(lowerCamelCase__			,'embed_dim'			)			)
							self.parent.assertTrue(hasattr(lowerCamelCase__			,'num_heads'			)			)






class 							lowercase__     :





	def __init__(			self :    int			,lowerCamelCase__ :    str			,lowerCamelCase__ :    List[Any]=13			,lowerCamelCase__ :    Tuple=64			,lowerCamelCase__ :    Optional[int]=3			,lowerCamelCase__ :    List[str]=[16, 48, 96]			,lowerCamelCase__ :    Union[str, Any]=[1, 3, 6]			,lowerCamelCase__ :    Dict=[1, 2, 10]			,lowerCamelCase__ :    int=[7, 3, 3]			,lowerCamelCase__ :    Tuple=[4, 2, 2]			,lowerCamelCase__ :    int=[2, 1, 1]			,lowerCamelCase__ :    Optional[Any]=[2, 2, 2]			,lowerCamelCase__ :    Optional[int]=[False, False, True]			,lowerCamelCase__ :    int=[0.0, 0.0, 0.0]			,lowerCamelCase__ :    str=0.0_2			,lowerCamelCase__ :    Optional[int]=1E-12			,lowerCamelCase__ :    str=True			,lowerCamelCase__ :    List[str]=True			,lowerCamelCase__ :    Optional[Any]=2			,):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       parent
							_UpperCamelCase :		Any			    =       batch_size
							_UpperCamelCase :		Optional[int]			    =       image_size
							_UpperCamelCase :		List[str]			    =       patch_sizes
							_UpperCamelCase :		str			    =       patch_stride
							_UpperCamelCase :		Union[str, Any]			    =       patch_padding
							_UpperCamelCase :		List[str]			    =       is_training
							_UpperCamelCase :		List[str]			    =       use_labels
							_UpperCamelCase :		Tuple			    =       num_labels
							_UpperCamelCase :		List[str]			    =       num_channels
							_UpperCamelCase :		Optional[int]			    =       embed_dim
							_UpperCamelCase :		int			    =       num_heads
							_UpperCamelCase :		Dict			    =       stride_kv
							_UpperCamelCase :		Optional[Any]			    =       depth
							_UpperCamelCase :		str			    =       cls_token
							_UpperCamelCase :		Tuple			    =       attention_drop_rate
							_UpperCamelCase :		List[Any]			    =       initializer_range
							_UpperCamelCase :		Tuple			    =       layer_norm_eps





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

							_UpperCamelCase :		Optional[int]			    =       None
							if self.use_labels:
													_UpperCamelCase :		Optional[Any]			    =       ids_tensor([self.batch_size]			,self.num_labels			)

							_UpperCamelCase :		str			    =       self.get_config()
							return config, pixel_values, labels





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							return CvtConfig(
							    image_size=self.image_size			,num_labels=self.num_labels			,num_channels=self.num_channels			,embed_dim=self.embed_dim			,num_heads=self.num_heads			,patch_sizes=self.patch_sizes			,patch_padding=self.patch_padding			,patch_stride=self.patch_stride			,stride_kv=self.stride_kv			,depth=self.depth			,cls_token=self.cls_token			,attention_drop_rate=self.attention_drop_rate			,initializer_range=self.initializer_range			,)





	def      UpperCamelCase_  (			self :    Dict			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		str			    =       CvtModel(config=lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		Union[str, Any]			    =       model(lowerCamelCase__			)
							_UpperCamelCase :		Dict			    =       (self.image_size, self.image_size)
							_UpperCamelCase :		str			    =       image_size[0], image_size[1]
							for i in range(len(self.depth			)			):
													_UpperCamelCase :		Any			    =       floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1			)
													_UpperCamelCase :		Optional[int]			    =       floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1			)
							self.parent.assertEqual(result.last_hidden_state.shape			,(self.batch_size, self.embed_dim[-1], height, width)			)





	def      UpperCamelCase_  (			self :    int			,lowerCamelCase__ :    str			,lowerCamelCase__ :    str			,lowerCamelCase__ :    Dict			):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       self.num_labels
							_UpperCamelCase :		int			    =       CvtForImageClassification(lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		Optional[Any]			    =       model(lowerCamelCase__			,labels=lowerCamelCase__			)
							self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.num_labels)			)





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[Any]			    =       self.prepare_config_and_inputs()
							_UpperCamelCase :		List[Any]			    =       config_and_inputs
							_UpperCamelCase :		Optional[Any]			    =       {'pixel_values': pixel_values}
							return config, inputs_dict






@require_torch
class 							lowercase__     (							lowercase						, lowercase						, unittest.TestCase       ):
	lowercase__      =      (CvtModel, CvtForImageClassification) if is_torch_available() else ()
	lowercase__      =      (
	    {"""feature-extraction""": CvtModel, """image-classification""": CvtForImageClassification}
	    if is_torch_available()
	    else {}
	)

	lowercase__      =      False
	lowercase__      =      False
	lowercase__      =      False
	lowercase__      =      False
	lowercase__      =      False





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		str			    =       CvtModelTester(self			)
							_UpperCamelCase :		int			    =       ConfigTester(self			,config_class=lowerCamelCase__			,has_text_modality=lowerCamelCase__			,hidden_size=37			)





	def      UpperCamelCase_  (			self :    List[Any]			):







							'''simple docstring'''
							self.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							return





	@unittest.skip(reason='Cvt does not output attentions'			)
	def      UpperCamelCase_  (			self :    Optional[int]			):







							'''simple docstring'''
							pass





	@unittest.skip(reason='Cvt does not use inputs_embeds'			)
	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							pass





	@unittest.skip(reason='Cvt does not support input and output embeddings'			)
	def      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							pass





	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							_UpperCamelCase :		str			    =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase :		Tuple			    =       model_class(lowerCamelCase__			)
													_UpperCamelCase :		Any			    =       inspect.signature(model.forward			)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													_UpperCamelCase :		str			    =       [*signature.parameters.keys()]

													_UpperCamelCase :		Optional[int]			    =       ['pixel_values']
													self.assertListEqual(arg_names[:1]			,lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    Optional[int]			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							def check_hidden_states_output(lowerCamelCase__ :    Union[str, Any]			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    str			):
													_UpperCamelCase :		int			    =       model_class(lowerCamelCase__			)
													model.to(lowerCamelCase__			)
													model.eval()

													with torch.no_grad():
																			_UpperCamelCase :		Any			    =       model(**self._prepare_for_class(lowerCamelCase__			,lowerCamelCase__			)			)

													_UpperCamelCase :		Union[str, Any]			    =       outputs.hidden_states

													_UpperCamelCase :		str			    =       len(self.model_tester.depth			)
													self.assertEqual(len(lowerCamelCase__			)			,lowerCamelCase__			)

													# verify the first hidden states (first block)
													self.assertListEqual(
													    list(hidden_states[0].shape[-3:]			)			,[
													        self.model_tester.embed_dim[0],
													        self.model_tester.image_size // 4,
													        self.model_tester.image_size // 4,
													    ]			,)

							_UpperCamelCase :		str			    =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase :		List[Any]			    =       True
													check_hidden_states_output(lowerCamelCase__			,lowerCamelCase__			,lowerCamelCase__			)

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													_UpperCamelCase :		str			    =       True

													check_hidden_states_output(lowerCamelCase__			,lowerCamelCase__			,lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__			)





	@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'			)
	def      UpperCamelCase_  (			self :    Optional[int]			):







							'''simple docstring'''
							pass





	@slow
	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase :		List[Any]			    =       CvtModel.from_pretrained(lowerCamelCase__			)
													self.assertIsNotNone(lowerCamelCase__			)






def A__   (					):
						_UpperCamelCase :		Optional[Any]			    =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
						return image






@require_torch
@require_vision
class 							lowercase__     (							unittest.TestCase       ):





	@cached_property
	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]			)





	@slow
	def      UpperCamelCase_  (			self :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]			).to(lowerCamelCase__			)

							_UpperCamelCase :		Dict			    =       self.default_image_processor
							_UpperCamelCase :		List[str]			    =       prepare_img()
							_UpperCamelCase :		List[str]			    =       image_processor(images=lowerCamelCase__			,return_tensors='pt'			).to(lowerCamelCase__			)

							# forward pass
							with torch.no_grad():
													_UpperCamelCase :		Tuple			    =       model(**lowerCamelCase__			)

							# verify the logits
							_UpperCamelCase :		List[str]			    =       torch.Size((1, 1000)			)
							self.assertEqual(outputs.logits.shape			,lowerCamelCase__			)

							_UpperCamelCase :		Tuple			    =       torch.tensor([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0]			).to(lowerCamelCase__			)

							self.assertTrue(torch.allclose(outputs.logits[0, :3]			,lowerCamelCase__			,atol=1E-4			)			)







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


snake_case_			:     int  	=  logging.get_logger(__name__)

snake_case_			:     List[str]  	=  {
    'EleutherAI/gpt-j-6B': 'https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json',
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}






class 							lowercase__     (							lowercase       ):
	lowercase__      =      """gptj"""
	lowercase__      =      {
	    """max_position_embeddings""": """n_positions""",
	    """hidden_size""": """n_embd""",
	    """num_attention_heads""": """n_head""",
	    """num_hidden_layers""": """n_layer""",
	}





	def __init__(			self :    Any			,lowerCamelCase__ :    Optional[Any]=50400			,lowerCamelCase__ :    Tuple=2048			,lowerCamelCase__ :    Tuple=4096			,lowerCamelCase__ :    int=28			,lowerCamelCase__ :    Optional[Any]=16			,lowerCamelCase__ :    Optional[Any]=64			,lowerCamelCase__ :    List[Any]=None			,lowerCamelCase__ :    List[Any]="gelu_new"			,lowerCamelCase__ :    Optional[Any]=0.0			,lowerCamelCase__ :    List[str]=0.0			,lowerCamelCase__ :    List[Any]=0.0			,lowerCamelCase__ :    Tuple=1E-5			,lowerCamelCase__ :    int=0.0_2			,lowerCamelCase__ :    Any=True			,lowerCamelCase__ :    str=50256			,lowerCamelCase__ :    Any=50256			,lowerCamelCase__ :    Tuple=False			,**lowerCamelCase__ :    Optional[Any]			,):







							'''simple docstring'''
							_UpperCamelCase :		Optional[Any]			    =       vocab_size
							_UpperCamelCase :		Optional[Any]			    =       n_positions
							_UpperCamelCase :		Union[str, Any]			    =       n_embd
							_UpperCamelCase :		Any			    =       n_layer
							_UpperCamelCase :		Optional[int]			    =       n_head
							_UpperCamelCase :		List[str]			    =       n_inner
							_UpperCamelCase :		List[Any]			    =       rotary_dim
							_UpperCamelCase :		int			    =       activation_function
							_UpperCamelCase :		Dict			    =       resid_pdrop
							_UpperCamelCase :		Any			    =       embd_pdrop
							_UpperCamelCase :		Union[str, Any]			    =       attn_pdrop
							_UpperCamelCase :		Union[str, Any]			    =       layer_norm_epsilon
							_UpperCamelCase :		Optional[Any]			    =       initializer_range
							_UpperCamelCase :		str			    =       use_cache

							_UpperCamelCase :		Union[str, Any]			    =       bos_token_id
							_UpperCamelCase :		Any			    =       eos_token_id

							super().__init__(
							    bos_token_id=lowerCamelCase__			,eos_token_id=lowerCamelCase__			,tie_word_embeddings=lowerCamelCase__			,**lowerCamelCase__			)






class 							lowercase__     (							lowercase       ):





	def __init__(			self :    Tuple			,lowerCamelCase__ :    PretrainedConfig			,lowerCamelCase__ :    str = "default"			,lowerCamelCase__ :    List[PatchingSpec] = None			,lowerCamelCase__ :    bool = False			,):







							'''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?
													_UpperCamelCase :		int			    =       0





	@property
	def      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		List[str]			    =       OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}			)
							if self.use_past:
													self.fill_with_past_key_values_(lowerCamelCase__			,direction='inputs'			)
													_UpperCamelCase :		Tuple			    =       {0: 'batch', 1: 'past_sequence + sequence'}
							else:
													_UpperCamelCase :		Any			    =       {0: 'batch', 1: 'sequence'}

							return common_inputs





	@property
	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							return self._config.n_layer





	@property
	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							return self._config.n_head





	def      UpperCamelCase_  (			self :    List[str]			,lowerCamelCase__ :    PreTrainedTokenizer			,lowerCamelCase__ :    int = -1			,lowerCamelCase__ :    int = -1			,lowerCamelCase__ :    bool = False			,lowerCamelCase__ :    Optional[TensorType] = None			,):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       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()
							_UpperCamelCase :		Tuple			    =       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 :		str			    =       common_inputs['input_ids'].shape
																			# Not using the same length for past_key_values
																			_UpperCamelCase :		Optional[int]			    =       seqlen + 2
																			_UpperCamelCase :		List[Any]			    =       (
																			    batch,
																			    self.num_attention_heads,
																			    past_key_values_length,
																			    self._config.hidden_size // self.num_attention_heads,
																			)
																			_UpperCamelCase :		Optional[Any]			    =       [
																			    (torch.zeros(lowerCamelCase__			), torch.zeros(lowerCamelCase__			)) for _ in range(self.num_layers			)
																			]

							_UpperCamelCase :		Union[str, Any]			    =       common_inputs['attention_mask']
							if self.use_past:
													_UpperCamelCase :		Any			    =       ordered_inputs['attention_mask'].dtype
													_UpperCamelCase :		List[str]			    =       torch.cat(
													    [ordered_inputs['attention_mask'], torch.ones(lowerCamelCase__			,lowerCamelCase__			,dtype=lowerCamelCase__			)]			,dim=1			)

							return ordered_inputs





	@property
	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							return 13


