





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   _UpperCamelCase  (       UpperCAmelCase_  ):







					'''simple docstring'''

					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[int]		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.config_class(**self.inputs_dict		)
						self.parent.assertTrue(hasattr(_lowercase ,					"""embed_dim"""		)		)
						self.parent.assertTrue(hasattr(_lowercase ,					"""num_heads"""		)		)







class   _UpperCamelCase  :







					'''simple docstring'''

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






						'''simple docstring'''







						UpperCAmelCase_								=     parent
						UpperCAmelCase_								=     batch_size
						UpperCAmelCase_								=     image_size
						UpperCAmelCase_								=     patch_sizes
						UpperCAmelCase_								=     patch_stride
						UpperCAmelCase_								=     patch_padding
						UpperCAmelCase_								=     is_training
						UpperCAmelCase_								=     use_labels
						UpperCAmelCase_								=     num_labels
						UpperCAmelCase_								=     num_channels
						UpperCAmelCase_								=     embed_dim
						UpperCAmelCase_								=     num_heads
						UpperCAmelCase_								=     stride_kv
						UpperCAmelCase_								=     depth
						UpperCAmelCase_								=     cls_token
						UpperCAmelCase_								=     attention_drop_rate
						UpperCAmelCase_								=     initializer_range
						UpperCAmelCase_								=     layer_norm_eps





					def        SCREAMING_SNAKE_CASE							(						self    :  Any		):






						'''simple docstring'''







						UpperCAmelCase_								=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

						UpperCAmelCase_								=     None
						if self.use_labels:
							UpperCAmelCase_								=     ids_tensor([self.batch_size] ,					self.num_labels		)

						UpperCAmelCase_								=     self.get_config()
						return config, pixel_values, labels





					def        SCREAMING_SNAKE_CASE							(						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        SCREAMING_SNAKE_CASE							(						self    :  Union[str, Any] ,					__lowercase    :  Any ,					__lowercase    :  List[Any] ,					__lowercase    :  Optional[int]		):






						'''simple docstring'''







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






						'''simple docstring'''







						UpperCAmelCase_								=     self.num_labels
						UpperCAmelCase_								=     CvtForImageClassification(_lowercase		)
						model.to(_lowercase		)
						model.eval()
						UpperCAmelCase_								=     model(_lowercase ,					labels=_lowercase		)
						self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Dict		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.prepare_config_and_inputs()
						UpperCAmelCase_								=     config_and_inputs
						UpperCAmelCase_								=     {'pixel_values': pixel_values}
						return config, inputs_dict







@require_torch
class   _UpperCamelCase  (       UpperCAmelCase_						,       UpperCAmelCase_						,       unittest.TestCase  ):







					'''simple docstring'''

					lowerCamelCase     :     Optional[int]			    =       (CvtModel, CvtForImageClassification) if is_torch_available() else ()
					lowerCamelCase     :     List[str]			    =       (
					    {'feature-extraction': CvtModel, 'image-classification': CvtForImageClassification}
					    if is_torch_available()
					    else {}
					)

					lowerCamelCase     :     Union[str, Any]			    =       False
					lowerCamelCase     :     Any			    =       False
					lowerCamelCase     :     Any			    =       False
					lowerCamelCase     :     Union[str, Any]			    =       False
					lowerCamelCase     :     Dict			    =       False





					def        SCREAMING_SNAKE_CASE							(						self    :  List[str]		):






						'''simple docstring'''







						UpperCAmelCase_								=     CvtModelTester(self		)
						UpperCAmelCase_								=     ConfigTester(self ,					config_class=_lowercase ,					has_text_modality=_lowercase ,					hidden_size=37		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[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        SCREAMING_SNAKE_CASE							(						self    :  Optional[int]		):






						'''simple docstring'''







						return





					@unittest.skip(reason="""Cvt does not output attentions"""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						pass





					@unittest.skip(reason="""Cvt does not use inputs_embeds"""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  Any		):






						'''simple docstring'''







						pass





					@unittest.skip(reason="""Cvt does not support input and output embeddings"""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						pass





					def        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs_for_common()

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

							UpperCAmelCase_								=     ['pixel_values']
							self.assertListEqual(arg_names[:1] ,					_lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple		):






						'''simple docstring'''







						def check_hidden_states_output(__lowercase    :  Union[str, Any] ,					__lowercase    :  str ,					__lowercase    :  Union[str, Any]		):
							UpperCAmelCase_								=     model_class(_lowercase		)
							model.to(_lowercase		)
							model.eval()

							with torch.no_grad():
								UpperCAmelCase_								=     model(**self._prepare_for_class(_lowercase ,					_lowercase		)		)

							UpperCAmelCase_								=     outputs.hidden_states

							UpperCAmelCase_								=     len(self.model_tester.depth		)
							self.assertEqual(len(_lowercase		) ,					_lowercase		)

							# 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_								=     self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							UpperCAmelCase_								=     True
							check_hidden_states_output(_lowercase ,					_lowercase ,					_lowercase		)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							UpperCAmelCase_								=     True

							check_hidden_states_output(_lowercase ,					_lowercase ,					_lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*_lowercase		)





					@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  Dict		):






						'''simple docstring'''







						pass





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_								=     CvtModel.from_pretrained(_lowercase		)
							self.assertIsNotNone(_lowercase		)




def    A_(			):
	UpperCAmelCase_								=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
	return image







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







					'''simple docstring'''

					@cached_property
					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[int]		):






						'''simple docstring'''







						return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(_lowercase		)

						UpperCAmelCase_								=     self.default_image_processor
						UpperCAmelCase_								=     prepare_img()
						UpperCAmelCase_								=     image_processor(images=_lowercase ,					return_tensors="""pt"""		).to(_lowercase		)

						# forward pass
						with torch.no_grad():
							UpperCAmelCase_								=     model(**_lowercase		)

						# verify the logits
						UpperCAmelCase_								=     torch.Size((1, 10_00)		)
						self.assertEqual(outputs.logits.shape ,					_lowercase		)

						UpperCAmelCase_								=     torch.tensor([0.9285, 0.9015, -0.3150]		).to(_lowercase		)

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









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


UpperCamelCase__   : Any					  = logging.get_logger(__name__)

UpperCamelCase__   : int					  = {
    """CarlCochet/trajectory-transformer-halfcheetah-medium-v2""": (
        """https://huggingface.co/CarlCochet/trajectory-transformer-halfcheetah-medium-v2/resolve/main/config.json"""
    ),
    # See all TrajectoryTransformer models at https://huggingface.co/models?filter=trajectory_transformer
}







class   _UpperCamelCase  (       A_  ):







					'''simple docstring'''

					lowerCamelCase     :     Tuple			    =       'trajectory_transformer'
					lowerCamelCase     :     Optional[Any]			    =       ['past_key_values']
					lowerCamelCase     :     Tuple			    =       {
					    'hidden_size': 'n_embd',
					    'num_attention_heads': 'n_head',
					    'num_hidden_layers': 'n_layer',
					}





					def __init__(						self    :  Union[str, Any] ,					__lowercase    :  Optional[Any]=1_00 ,					__lowercase    :  int=5 ,					__lowercase    :  str=1 ,					__lowercase    :  Union[str, Any]=1 ,					__lowercase    :  Optional[Any]=2_49 ,					__lowercase    :  Tuple=6 ,					__lowercase    :  Tuple=17 ,					__lowercase    :  List[str]=25 ,					__lowercase    :  Tuple=4 ,					__lowercase    :  Dict=4 ,					__lowercase    :  int=1_28 ,					__lowercase    :  List[str]=0.1 ,					__lowercase    :  Optional[Any]=0.1 ,					__lowercase    :  Union[str, Any]=0.1 ,					__lowercase    :  Tuple=0.0006 ,					__lowercase    :  Optional[int]=5_12 ,					__lowercase    :  Any=0.02 ,					__lowercase    :  Tuple=1e-12 ,					__lowercase    :  Optional[Any]=1 ,					__lowercase    :  Dict=True ,					__lowercase    :  Optional[Any]=1 ,					__lowercase    :  str=5_02_56 ,					__lowercase    :  Any=5_02_56 ,					**__lowercase    :  str ,					):






						'''simple docstring'''







						UpperCAmelCase_								=     vocab_size
						UpperCAmelCase_								=     action_weight
						UpperCAmelCase_								=     reward_weight
						UpperCAmelCase_								=     value_weight
						UpperCAmelCase_								=     max_position_embeddings
						UpperCAmelCase_								=     block_size
						UpperCAmelCase_								=     action_dim
						UpperCAmelCase_								=     observation_dim
						UpperCAmelCase_								=     transition_dim
						UpperCAmelCase_								=     learning_rate
						UpperCAmelCase_								=     n_layer
						UpperCAmelCase_								=     n_head
						UpperCAmelCase_								=     n_embd
						UpperCAmelCase_								=     embd_pdrop
						UpperCAmelCase_								=     attn_pdrop
						UpperCAmelCase_								=     resid_pdrop
						UpperCAmelCase_								=     initializer_range
						UpperCAmelCase_								=     layer_norm_eps
						UpperCAmelCase_								=     kaiming_initializer_range
						UpperCAmelCase_								=     use_cache
						super().__init__(pad_token_id=__lowercase ,					bos_token_id=__lowercase ,					eos_token_id=__lowercase ,					**__lowercase		)


