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     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.config_class(**self.inputs_dict)
        self.parent.assertTrue(hasattr(lowercase__      ,   'embed_dim'))
        self.parent.assertTrue(hasattr(lowercase__      ,   'num_heads'))






class     a_			:






 '''simple docstring'''






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







        '''simple docstring'''



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




 def 		__snake_case						(      self :  str):







        '''simple docstring'''



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

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

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




 def 		__snake_case						(      self :  Dict):







        '''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 		__snake_case						(      self :  List[str]      ,   lowercase__ :  List[str]      ,   lowercase__ :  List[Any]      ,   lowercase__ :  Dict):







        '''simple docstring'''



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







        '''simple docstring'''



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




 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



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






@require_torch
class     a_			(						SCREAMING_SNAKE_CASE	,					SCREAMING_SNAKE_CASE	,					unittest.TestCase						):






 '''simple docstring'''






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

 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = CvtModelTester(self)
        lowerCAmelCase__					     = ConfigTester(self      ,   config_class=lowercase__      ,   has_text_modality=lowercase__      ,   hidden_size=37)




 def 		__snake_case						(      self :  str):







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







        '''simple docstring'''



        return




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







        '''simple docstring'''



        pass




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







        '''simple docstring'''



        pass




 @unittest.skip(reason='Cvt does not support input and output embeddings')
 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        pass




 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

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

               lowerCAmelCase__					     = ['pixel_values']
               self.assertListEqual(arg_names[:1]      ,   lowercase__)




 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase__)




 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



        def check_hidden_states_output(lowercase__ :  Tuple      ,   lowercase__ :  int      ,   lowercase__ :  Union[str, Any]):
               lowerCAmelCase__					     = model_class(lowercase__)
               model.to(lowercase__)
               model.eval()

               with torch.no_grad():
                      lowerCAmelCase__					     = model(**self._prepare_for_class(lowercase__      ,   lowercase__))

               lowerCAmelCase__					     = outputs.hidden_states

               lowerCAmelCase__					     = 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,
                   ]      ,   )

        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               lowerCAmelCase__					     = True
               check_hidden_states_output(lowercase__      ,   lowercase__      ,   lowercase__)

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

               check_hidden_states_output(lowercase__      ,   lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = 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 		__snake_case						(      self :  Optional[int]):







        '''simple docstring'''



        pass




 @slow
 def 		__snake_case						(      self :  int):







        '''simple docstring'''



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






def 				__lowerCamelCase		(						):
       lowerCAmelCase__					     = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
       return image



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






 '''simple docstring'''






 @cached_property
 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])




 @slow
 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(lowercase__)

        lowerCAmelCase__					     = self.default_image_processor
        lowerCAmelCase__					     = prepare_img()
        lowerCAmelCase__					     = image_processor(images=lowercase__      ,   return_tensors='pt').to(lowercase__)

        # forward pass
        with torch.no_grad():
               lowerCAmelCase__					     = model(**lowercase__)

        # verify the logits
        lowerCAmelCase__					     = torch.Size((1, 1_000))
        self.assertEqual(outputs.logits.shape      ,   lowercase__)

        lowerCAmelCase__					     = torch.tensor([0.9_285, 0.9_015, -0.3_150]).to(lowercase__)

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

import numpy as np






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       return 1 / (1 + np.exp(-vector     ))






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       return vector * sigmoid(lowerCAmelCase__     )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
