

'''simple docstring'''






from argparse import ArgumentParser

from . import BaseTransformersCLICommand


def 	_A  (					A__   ):






 """simple docstring"""



 return DownloadCommand(args.model  ,       args.cache_dir  ,       args.force  ,       args.trust_remote_code   )






class     lowercase_			(lowerCamelCase__   ):




  """simple docstring"""




  @staticmethod
  def       SCREAMING_SNAKE_CASE   (   lowercase__     :  ArgumentParser    ):
   __lowercase													=				parser.add_parser('''download'''    )
   download_parser.add_argument(
       '''--cache-dir'''      ,type=lowercase__      ,default=lowercase__      ,help='''Path to location to store the models'''    )
   download_parser.add_argument(
       '''--force'''      ,action='''store_true'''      ,help='''Force the model to be download even if already in cache-dir'''    )
   download_parser.add_argument(
       '''--trust-remote-code'''      ,action='''store_true'''      ,help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine'''      ,)
   download_parser.add_argument('''model'''      ,type=lowercase__      ,help='''Name of the model to download'''    )
   download_parser.set_defaults(func=lowercase__    )






  def __init__(   self     :  str      ,lowercase__     :  str      ,lowercase__     :  str      ,lowercase__     :  bool      ,lowercase__     :  bool    ):
   __lowercase													=				model
   __lowercase													=				cache
   __lowercase													=				force
   __lowercase													=				trust_remote_code






  def       SCREAMING_SNAKE_CASE   (   self     :  Any    ):
   from ..models.auto import AutoModel, AutoTokenizer

   AutoModel.from_pretrained(
       self._model      ,cache_dir=self._cache      ,force_download=self._force      ,trust_remote_code=self._trust_remote_code    )
   AutoTokenizer.from_pretrained(
       self._model      ,cache_dir=self._cache      ,force_download=self._force      ,trust_remote_code=self._trust_remote_code    )




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




  """simple docstring"""




  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]    ):
   __lowercase													=				self.config_class(**self.inputs_dict    )
   self.parent.assertTrue(hasattr(lowercase__      ,'''embed_dim'''    )    )
   self.parent.assertTrue(hasattr(lowercase__      ,'''num_heads'''    )    )






class     lowercase_			:




  """simple docstring"""




  def __init__(   self     :  Optional[Any]      ,lowercase__     :  Optional[int]      ,lowercase__     :  Optional[int]=1_3      ,lowercase__     :  List[Any]=6_4      ,lowercase__     :  Optional[int]=3      ,lowercase__     :  Dict=[1_6, 4_8, 9_6]      ,lowercase__     :  Optional[Any]=[1, 3, 6]      ,lowercase__     :  Tuple=[1, 2, 1_0]      ,lowercase__     :  Optional[int]=[7, 3, 3]      ,lowercase__     :  str=[4, 2, 2]      ,lowercase__     :  Dict=[2, 1, 1]      ,lowercase__     :  Tuple=[2, 2, 2]      ,lowercase__     :  Tuple=[False, False, True]      ,lowercase__     :  int=[0.0, 0.0, 0.0]      ,lowercase__     :  str=0.0_2      ,lowercase__     :  Union[str, Any]=1e-1_2      ,lowercase__     :  Optional[int]=True      ,lowercase__     :  Union[str, Any]=True      ,lowercase__     :  Optional[Any]=2      ,):
   __lowercase													=				parent
   __lowercase													=				batch_size
   __lowercase													=				image_size
   __lowercase													=				patch_sizes
   __lowercase													=				patch_stride
   __lowercase													=				patch_padding
   __lowercase													=				is_training
   __lowercase													=				use_labels
   __lowercase													=				num_labels
   __lowercase													=				num_channels
   __lowercase													=				embed_dim
   __lowercase													=				num_heads
   __lowercase													=				stride_kv
   __lowercase													=				depth
   __lowercase													=				cls_token
   __lowercase													=				attention_drop_rate
   __lowercase													=				initializer_range
   __lowercase													=				layer_norm_eps






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase													=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

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






  def       SCREAMING_SNAKE_CASE   (   self     :  str    ):
   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     :  Dict      ,lowercase__     :  List[str]      ,lowercase__     :  Any      ,lowercase__     :  List[str]    ):
   __lowercase													=				CvtModel(config=lowercase__    )
   model.to(lowercase__    )
   model.eval()
   __lowercase													=				model(lowercase__    )
   __lowercase													=				(self.image_size, self.image_size)
   __lowercase						,			__lowercase													=				image_size[0], image_size[1]
   for i in range(len(self.depth    )    ):
    __lowercase													=				floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1    )
    __lowercase													=				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     :  Dict      ,lowercase__     :  Any      ,lowercase__     :  List[Any]      ,lowercase__     :  Dict    ):
   __lowercase													=				self.num_labels
   __lowercase													=				CvtForImageClassification(lowercase__    )
   model.to(lowercase__    )
   model.eval()
   __lowercase													=				model(lowercase__      ,labels=lowercase__    )
   self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)    )






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase													=				self.prepare_config_and_inputs()
   __lowercase						,			__lowercase						,			__lowercase													=				config_and_inputs
   __lowercase													=				{'''pixel_values''': pixel_values}
   return config, inputs_dict






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




  """simple docstring"""




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

  SCREAMING_SNAKE_CASE					:						Optional[int]           =    False
  SCREAMING_SNAKE_CASE					:						Union[str, Any]           =    False
  SCREAMING_SNAKE_CASE					:						int           =    False
  SCREAMING_SNAKE_CASE					:						Optional[Any]           =    False
  SCREAMING_SNAKE_CASE					:						str           =    False






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase													=				CvtModelTester(self    )
   __lowercase													=				ConfigTester(self      ,config_class=lowercase__      ,has_text_modality=lowercase__      ,hidden_size=3_7    )






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






  @unittest.skip(reason='''Cvt does not output attentions'''    )
  def       SCREAMING_SNAKE_CASE   (   self     :  Any    ):
   pass






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






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






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase						,			__lowercase													=				self.model_tester.prepare_config_and_inputs_for_common()

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

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






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]    ):
   __lowercase													=				self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple    ):
   def check_hidden_states_output(lowercase__     :  Union[str, Any]      ,lowercase__     :  Union[str, Any]      ,lowercase__     :  str    ):
    __lowercase													=				model_class(lowercase__    )
    model.to(lowercase__    )
    model.eval()

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

    __lowercase													=				outputs.hidden_states

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

   __lowercase						,			__lowercase													=				self.model_tester.prepare_config_and_inputs_for_common()

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

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

    check_hidden_states_output(lowercase__      ,lowercase__      ,lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Dict    ):
   __lowercase													=				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     :  Any    ):
   pass






  @slow
  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]    ):
   for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __lowercase													=				CvtModel.from_pretrained(lowercase__    )
    self.assertIsNotNone(lowercase__    )


def 	_A  (					):






 """simple docstring"""



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






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




  """simple docstring"""




  @cached_property
  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[Any]    ):
   return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )






  @slow
  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase													=				CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    ).to(lowercase__    )

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

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

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

   __lowercase													=				torch.tensor([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0]    ).to(lowercase__    )

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

