

'''simple docstring'''



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 lowerCamelCase			(  lowercase_						):



			'''simple docstring'''






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





									'''simple docstring'''
									A__					:       List[Any]     =self.config_class(**self.inputs_dict			)
									self.parent.assertTrue(hasattr(lowerCAmelCase_		,      """embed_dim"""			)			)
									self.parent.assertTrue(hasattr(lowerCAmelCase_		,      """num_heads"""			)			)



class lowerCamelCase			:



			'''simple docstring'''






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





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


			def lowercase__						(				self	:						Tuple			)      ->		Optional[int]:





									'''simple docstring'''
									A__					:       Tuple     =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

									A__					:       Optional[int]     =self.get_config()
									return config, pixel_values, labels


			def lowercase__						(				self	:						Union[str, Any]			)      ->		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 lowercase__						(				self	:						Optional[int]		,      lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						Dict		,      lowerCAmelCase_	:						Optional[Any]			)      ->		int:





									'''simple docstring'''
									A__					:       Union[str, Any]     =CvtModel(config=lowerCAmelCase_			)
									model.to(lowerCAmelCase_			)
									model.eval()
									A__					:       Optional[Any]     =model(lowerCAmelCase_			)
									A__					:       Any     =(self.image_size, self.image_size)
									A__   ,						A__					:       List[str]     =image_size[0], image_size[1]
									for i in range(len(self.depth			)			):
															A__					:       Optional[Any]     =floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1			)
															A__					:       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 lowercase__						(				self	:						List[str]		,      lowerCAmelCase_	:						Optional[int]		,      lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						Union[str, Any]			)      ->		Any:





									'''simple docstring'''
									A__					:       Dict     =self.num_labels
									A__					:       int     =CvtForImageClassification(lowerCAmelCase_			)
									model.to(lowerCAmelCase_			)
									model.eval()
									A__					:       List[str]     =model(lowerCAmelCase_		,      labels=lowerCAmelCase_			)
									self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)			)



			def lowercase__						(				self	:						str			)      ->		str:





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



@require_torch
class lowerCamelCase			(  lowercase_	,						lowercase_	,						unittest.TestCase						):



			'''simple docstring'''






			__snake_case        =	(CvtModel, CvtForImageClassification) if is_torch_available() else ()
			__snake_case        =	(
			    {'feature-extraction': CvtModel, 'image-classification': CvtForImageClassification}
			    if is_torch_available()
			    else {}
			)

			__snake_case        =	False
			__snake_case        =	False
			__snake_case        =	False
			__snake_case        =	False
			__snake_case        =	False


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





									'''simple docstring'''
									A__					:       List[str]     =CvtModelTester(self			)
									A__					:       int     =ConfigTester(self		,      config_class=lowerCAmelCase_		,      has_text_modality=lowerCAmelCase_		,      hidden_size=37			)


			def lowercase__						(				self	:						Union[str, Any]			)      ->		int:





									'''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 lowercase__						(				self	:						str			)      ->		str:





									'''simple docstring'''
									return


			@unittest.skip(reason="""Cvt does not output attentions"""			)
			def lowercase__						(				self	:						Tuple			)      ->		Optional[Any]:





									'''simple docstring'''
									pass


			@unittest.skip(reason="""Cvt does not use inputs_embeds"""			)
			def lowercase__						(				self	:						Dict			)      ->		int:





									'''simple docstring'''
									pass


			@unittest.skip(reason="""Cvt does not support input and output embeddings"""			)
			def lowercase__						(				self	:						Any			)      ->		Optional[int]:





									'''simple docstring'''
									pass


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





									'''simple docstring'''
									A__   ,						A__					:       Optional[Any]     =self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															A__					:       Optional[int]     =model_class(lowerCAmelCase_			)
															A__					:       int     =inspect.signature(model.forward			)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															A__					:       str     =[*signature.parameters.keys()]

															A__					:       int     =["""pixel_values"""]
															self.assertListEqual(arg_names[:1]		,      lowerCAmelCase_			)


			def lowercase__						(				self	:						int			)      ->		List[str]:





									'''simple docstring'''
									A__					:       Optional[int]     =self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCAmelCase_			)


			def lowercase__						(				self	:						Any			)      ->		Tuple:





									'''simple docstring'''
									def check_hidden_states_output(lowerCAmelCase_	:						Any		,      lowerCAmelCase_	:						Dict		,      lowerCAmelCase_	:						Any			):
															A__					:       List[str]     =model_class(lowerCAmelCase_			)
															model.to(lowerCAmelCase_			)
															model.eval()

															with torch.no_grad():
																					A__					:       str     =model(**self._prepare_for_class(lowerCAmelCase_		,      lowerCAmelCase_			)			)

															A__					:       Any     =outputs.hidden_states

															A__					:       Dict     =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,
															    ]		,      )

									A__   ,						A__					:       List[Any]     =self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															A__					:       List[str]     =True
															check_hidden_states_output(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)

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

															check_hidden_states_output(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)


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





									'''simple docstring'''
									A__					:       Optional[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 lowercase__						(				self	:						Optional[Any]			)      ->		int:





									'''simple docstring'''
									pass



			@slow
			def lowercase__						(				self	:						Optional[int]			)      ->		List[Any]:





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


def 						__lowerCamelCase	(       )	->							Optional[int]:






						"""simple docstring"""





						A__					:       Any     =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
						return image



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



			'''simple docstring'''






			@cached_property
			def lowercase__						(				self	:						Optional[int]			)      ->		Dict:





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



			@slow
			def lowercase__						(				self	:						int			)      ->		Optional[Any]:





									'''simple docstring'''
									A__					:       Dict     =CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]			).to(lowerCAmelCase_			)

									A__					:       List[str]     =self.default_image_processor
									A__					:       Optional[int]     =prepare_img()
									A__					:       str     =image_processor(images=lowerCAmelCase_		,      return_tensors="""pt"""			).to(lowerCAmelCase_			)

									# forward pass
									with torch.no_grad():
															A__					:       Dict     =model(**lowerCAmelCase_			)

									# verify the logits
									A__					:       List[Any]     =torch.Size((1, 10_00)			)
									self.assertEqual(outputs.logits.shape		,      lowerCAmelCase_			)

									A__					:       Optional[Any]     =torch.tensor([0.9285, 0.9015, -0.3150]			).to(lowerCAmelCase_			)

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




'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging


__snake_case			:     Union[str, Any]										=      logging.get_logger(__name__)

__snake_case			:     Optional[int]										=      {
    'bigcode/gpt_bigcode-santacoder': 'https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json',
}



class lowerCamelCase			(  lowercase_						):



			'''simple docstring'''






			__snake_case        =	'gpt_bigcode'
			__snake_case        =	['past_key_values']
			__snake_case        =	{
			    'hidden_size': 'n_embd',
			    'max_position_embeddings': 'n_positions',
			    'num_attention_heads': 'n_head',
			    'num_hidden_layers': 'n_layer',
			}


			def __init__(				self	:						Optional[Any]		,      lowerCAmelCase_	:						List[str]=5_02_57		,      lowerCAmelCase_	:						str=10_24		,      lowerCAmelCase_	:						str=7_68		,      lowerCAmelCase_	:						str=12		,      lowerCAmelCase_	:						int=12		,      lowerCAmelCase_	:						Dict=None		,      lowerCAmelCase_	:						List[Any]="gelu_pytorch_tanh"		,      lowerCAmelCase_	:						Union[str, Any]=0.1		,      lowerCAmelCase_	:						Optional[Any]=0.1		,      lowerCAmelCase_	:						Optional[int]=0.1		,      lowerCAmelCase_	:						Dict=1e-5		,      lowerCAmelCase_	:						str=0.02		,      lowerCAmelCase_	:						List[Any]=True		,      lowerCAmelCase_	:						str=True		,      lowerCAmelCase_	:						str=5_02_56		,      lowerCAmelCase_	:						Dict=5_02_56		,      lowerCAmelCase_	:						Union[str, Any]=True		,      lowerCAmelCase_	:						str=True		,      lowerCAmelCase_	:						Optional[Any]=True		,      **lowerCAmelCase_	:						Optional[Any]		,      )      ->		Tuple:





									'''simple docstring'''
									A__					:       Optional[Any]     =vocab_size
									A__					:       Optional[Any]     =n_positions
									A__					:       List[str]     =n_embd
									A__					:       str     =n_layer
									A__					:       Optional[int]     =n_head
									A__					:       Optional[int]     =n_inner
									A__					:       int     =activation_function
									A__					:       int     =resid_pdrop
									A__					:       int     =embd_pdrop
									A__					:       Dict     =attn_pdrop
									A__					:       Any     =layer_norm_epsilon
									A__					:       List[Any]     =initializer_range
									A__					:       Dict     =scale_attn_weights
									A__					:       Any     =use_cache
									A__					:       List[Any]     =attention_softmax_in_fpaa
									A__					:       Optional[int]     =scale_attention_softmax_in_fpaa
									A__					:       Dict     =multi_query

									A__					:       List[str]     =bos_token_id
									A__					:       Any     =eos_token_id

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

