






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




      """simple docstring"""




      def 					A__   (			self      :		Tuple		)	->	List[str]:


       '''simple docstring'''

       lowercase       : Any						=self.config_class(**self.inputs_dict		)
       self.parent.assertTrue(hasattr(UpperCAmelCase  ,   '''embed_dim'''		)		)
       self.parent.assertTrue(hasattr(UpperCAmelCase  ,   '''num_heads'''		)		)





class UpperCAmelCase_       :




      """simple docstring"""




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


       '''simple docstring'''

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






      def 					A__   (			self      :		str		)	->	List[Any]:


       '''simple docstring'''

       lowercase       : str						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

       lowercase       : int						=self.get_config()
       return config, pixel_values, labels






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


       '''simple docstring'''

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


       '''simple docstring'''

       lowercase       : List[Any]						=self.num_labels
       lowercase       : str						=CvtForImageClassification(UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase       : int						=model(UpperCAmelCase  ,   labels=UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.num_labels)		)






      def 					A__   (			self      :		Any		)	->	Any:


       '''simple docstring'''

       lowercase       : int						=self.prepare_config_and_inputs()
       lowercase	,		lowercase	,		lowercase       : Optional[Any]						=config_and_inputs
       lowercase       : Tuple						={'''pixel_values''': pixel_values}
       return config, inputs_dict





@require_torch
class UpperCAmelCase_       (     __A					,							__A					,							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 					A__   (			self      :		Any		)	->	List[str]:


       '''simple docstring'''

       lowercase       : int						=CvtModelTester(self		)
       lowercase       : str						=ConfigTester(self  ,   config_class=UpperCAmelCase  ,   has_text_modality=UpperCAmelCase  ,   hidden_size=37		)






      def 					A__   (			self      :		Tuple		)	->	Tuple:


       '''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 					A__   (			self      :		Dict		)	->	Tuple:


       '''simple docstring'''

       return






      @unittest.skip(reason='''Cvt does not output attentions'''		)
      def 					A__   (			self      :		Union[str, Any]		)	->	Dict:


       '''simple docstring'''

       pass






      @unittest.skip(reason='''Cvt does not use inputs_embeds'''		)
      def 					A__   (			self      :		str		)	->	int:


       '''simple docstring'''

       pass






      @unittest.skip(reason='''Cvt does not support input and output embeddings'''		)
      def 					A__   (			self      :		Any		)	->	Dict:


       '''simple docstring'''

       pass






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


       '''simple docstring'''

       lowercase	,		lowercase       : int						=self.model_tester.prepare_config_and_inputs_for_common()

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

        lowercase       : str						=['''pixel_values''']
        self.assertListEqual(arg_names[:1]  ,   UpperCAmelCase		)






      def 					A__   (			self      :		str		)	->	Dict:


       '''simple docstring'''

       lowercase       : str						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCAmelCase		)






      def 					A__   (			self      :		Tuple		)	->	int:


       '''simple docstring'''

       def check_hidden_states_output(UpperCAmelCase      :		str  ,   UpperCAmelCase      :		Optional[int]  ,   UpperCAmelCase      :		List[Any]		):
        lowercase       : List[str]						=model_class(UpperCAmelCase		)
        model.to(UpperCAmelCase		)
        model.eval()

        with torch.no_grad():
         lowercase       : Tuple						=model(**self._prepare_for_class(UpperCAmelCase  ,   UpperCAmelCase		)		)

        lowercase       : List[str]						=outputs.hidden_states

        lowercase       : str						=len(self.model_tester.depth		)
        self.assertEqual(len(UpperCAmelCase		)  ,   UpperCAmelCase		)

        # 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,
            ]  ,   )

       lowercase	,		lowercase       : str						=self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        lowercase       : Dict						=True
        check_hidden_states_output(UpperCAmelCase  ,   UpperCAmelCase  ,   UpperCAmelCase		)

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

        check_hidden_states_output(UpperCAmelCase  ,   UpperCAmelCase  ,   UpperCAmelCase		)






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


       '''simple docstring'''

       lowercase       : Union[str, Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase		)






      @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''		)
      def 					A__   (			self      :		Tuple		)	->	Any:


       '''simple docstring'''

       pass






      @slow
      def 					A__   (			self      :		Dict		)	->	Tuple:


       '''simple docstring'''

       for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase       : List[str]						=CvtModel.from_pretrained(UpperCAmelCase		)
        self.assertIsNotNone(UpperCAmelCase		)





def 				lowercase_					(     )							->   List[str]:



 """simple docstring"""




 lowercase       : Tuple						=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
 return image





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




      """simple docstring"""




      @cached_property
      def 					A__   (			self      :		Union[str, Any]		)	->	Any:


       '''simple docstring'''

       return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)






      @slow
      def 					A__   (			self      :		Optional[Any]		)	->	Union[str, Any]:


       '''simple docstring'''

       lowercase       : Any						=CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(UpperCAmelCase		)

       lowercase       : Tuple						=self.default_image_processor
       lowercase       : str						=prepare_img()
       lowercase       : List[str]						=image_processor(images=UpperCAmelCase  ,   return_tensors='''pt'''		).to(UpperCAmelCase		)

       # forward pass
       with torch.no_grad():
        lowercase       : Optional[Any]						=model(**UpperCAmelCase		)

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

       lowercase       : Optional[int]						=torch.tensor([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0]		).to(UpperCAmelCase		)

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







_lowercase							:   Dict						=						"0.21.0"

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
				from .utils import rich



