


from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor




class A    :






      def __init__(   self		:	List[Any]     ,  lowercase_		:	Tuple     ,  lowercase_		:	Any=3     ,  lowercase_		:	Tuple=32     ,  lowercase_		:	int=3     ,  lowercase_		:	int=10     ,  lowercase_		:	Dict=[10, 20, 30, 40]     ,  lowercase_		:	Tuple=[1, 1, 2, 1]     ,  lowercase_		:	Any=True     ,  lowercase_		:	Union[str, Any]=True     ,  lowercase_		:	Dict="relu"     ,  lowercase_		:	Dict=3     ,  lowercase_		:	Optional[int]=None     ,  )   ->		Dict:
             """simple docstring"""




             _lowerCamelCase			:						Optional[Any]            =parent
             _lowerCamelCase			:						Dict            =batch_size
             _lowerCamelCase			:						Optional[Any]            =image_size
             _lowerCamelCase			:						str            =num_channels
             _lowerCamelCase			:						int            =embeddings_size
             _lowerCamelCase			:						List[Any]            =hidden_sizes
             _lowerCamelCase			:						List[Any]            =depths
             _lowerCamelCase			:						Any            =is_training
             _lowerCamelCase			:						List[str]            =use_labels
             _lowerCamelCase			:						Optional[int]            =hidden_act
             _lowerCamelCase			:						Optional[int]            =num_labels
             _lowerCamelCase			:						str            =scope
             _lowerCamelCase			:						str            =len(lowercase_					)






      def 			lowerCamelCase			(   self		:	int					)   ->		str:
             """simple docstring"""




             _lowerCamelCase			:						Union[str, Any]            =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

             _lowerCamelCase			:						int            =None
             if self.use_labels:
                    _lowerCamelCase			:						Optional[int]            =ids_tensor([self.batch_size]     ,  self.num_labels					)

             _lowerCamelCase			:						str            =self.get_config()
             return config, pixel_values, labels






      def 			lowerCamelCase			(   self		:	List[str]					)   ->		List[str]:
             """simple docstring"""




             return RegNetConfig(
                 num_channels=self.num_channels     ,  embeddings_size=self.embeddings_size     ,  hidden_sizes=self.hidden_sizes     ,  depths=self.depths     ,  hidden_act=self.hidden_act     ,  num_labels=self.num_labels     ,  )






      def 			lowerCamelCase			(   self		:	List[Any]     ,  lowercase_		:	List[Any]     ,  lowercase_		:	Union[str, Any]     ,  lowercase_		:	str					)   ->		List[Any]:
             """simple docstring"""




             _lowerCamelCase			:						int            =TFRegNetModel(config=lowercase_					)
             _lowerCamelCase			:						Dict            =model(lowercase_     ,  training=lowercase_					)
             # expected last hidden states: B, C, H // 32, W // 32
             self.parent.assertEqual(
                 result.last_hidden_state.shape     ,  (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)     ,  )






      def 			lowerCamelCase			(   self		:	Optional[int]     ,  lowercase_		:	Tuple     ,  lowercase_		:	Union[str, Any]     ,  lowercase_		:	Optional[Any]					)   ->		Optional[int]:
             """simple docstring"""




             _lowerCamelCase			:						str            =self.num_labels
             _lowerCamelCase			:						Dict            =TFRegNetForImageClassification(lowercase_					)
             _lowerCamelCase			:						Dict            =model(lowercase_     ,  labels=lowercase_     ,  training=lowercase_					)
             self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels)					)






      def 			lowerCamelCase			(   self		:	str					)   ->		List[Any]:
             """simple docstring"""




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




@require_tf
class A    (  UpperCamelCase_       ,				UpperCamelCase_       ,				unittest.TestCase	):
      UpperCamelCase__				:    List[str] =(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
      UpperCamelCase__				:    Optional[Any] =(
          {'feature-extraction': TFRegNetModel, 'image-classification': TFRegNetForImageClassification}
          if is_tf_available()
          else {}
      )

      UpperCamelCase__				:    Any =False
      UpperCamelCase__				:    int =False
      UpperCamelCase__				:    Optional[int] =False
      UpperCamelCase__				:    Optional[int] =False
      UpperCamelCase__				:    str =False






      def 			lowerCamelCase			(   self		:	str					)   ->		Any:
             """simple docstring"""




             _lowerCamelCase			:						Optional[int]            =TFRegNetModelTester(self					)
             _lowerCamelCase			:						Dict            =ConfigTester(self     ,  config_class=lowercase_     ,  has_text_modality=lowercase_					)






      def 			lowerCamelCase			(   self		:	Optional[int]					)   ->		Optional[int]:
             """simple docstring"""




             return






      @unittest.skip(reason='RegNet does not use inputs_embeds'					)
      def 			lowerCamelCase			(   self		:	str					)   ->		List[Any]:
             """simple docstring"""




             pass






      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('GPU'					)					) == 0     ,  reason='TF does not support backprop for grouped convolutions on CPU.'     ,  )
      @slow
      def 			lowerCamelCase			(   self		:	int					)   ->		Optional[int]:
             """simple docstring"""




             super().test_keras_fit()






      @unittest.skip(reason='RegNet does not support input and output embeddings'					)
      def 			lowerCamelCase			(   self		:	List[str]					)   ->		List[Any]:
             """simple docstring"""




             pass






      def 			lowerCamelCase			(   self		:	str					)   ->		Tuple:
             """simple docstring"""




             _lowerCamelCase      ,	_lowerCamelCase			:						Any            =self.model_tester.prepare_config_and_inputs_for_common()

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

                    _lowerCamelCase			:						Optional[Any]            =['pixel_values']
                    self.assertListEqual(arg_names[:1]     ,  lowercase_					)






      def 			lowerCamelCase			(   self		:	Union[str, Any]					)   ->		Optional[Any]:
             """simple docstring"""




             _lowerCamelCase			:						List[str]            =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*lowercase_					)






      def 			lowerCamelCase			(   self		:	Any					)   ->		Optional[Any]:
             """simple docstring"""




             def check_hidden_states_output(lowercase_		:	List[str]     ,  lowercase_		:	Optional[int]     ,  lowercase_		:	Any					):
                    _lowerCamelCase			:						List[str]            =model_class(lowercase_					)
                    _lowerCamelCase			:						Union[str, Any]            =model(**self._prepare_for_class(lowercase_     ,  lowercase_					)     ,  training=lowercase_					)

                    _lowerCamelCase			:						Optional[Any]            =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                    _lowerCamelCase			:						Any            =self.model_tester.num_stages
                    self.assertEqual(len(lowercase_					)     ,  expected_num_stages + 1					)

                    # RegNet's feature maps are of shape (batch_size, num_channels, height, width)
                    self.assertListEqual(
                        list(hidden_states[0].shape[-2:]					)     ,  [self.model_tester.image_size // 2, self.model_tester.image_size // 2]     ,  )

             _lowerCamelCase      ,	_lowerCamelCase			:						Any            =self.model_tester.prepare_config_and_inputs_for_common()
             _lowerCamelCase			:						Optional[Any]            =['basic', 'bottleneck']
             for model_class in self.all_model_classes:
                    for layer_type in layers_type:
                           _lowerCamelCase			:						Optional[Any]            =layer_type
                           _lowerCamelCase			:						Optional[Any]            =True
                           check_hidden_states_output(lowercase_     ,  lowercase_     ,  lowercase_					)

                           # check that output_hidden_states also work using config
                           del inputs_dict["output_hidden_states"]
                           _lowerCamelCase			:						Optional[int]            =True

                           check_hidden_states_output(lowercase_     ,  lowercase_     ,  lowercase_					)






      def 			lowerCamelCase			(   self		:	List[str]					)   ->		Any:
             """simple docstring"""




             _lowerCamelCase      ,	_lowerCamelCase			:						Optional[int]            =self.model_tester.prepare_config_and_inputs_for_common()

             def check_equivalence(lowercase_		:	Dict     ,  lowercase_		:	Dict     ,  lowercase_		:	List[str]     ,  lowercase_		:	str={}					):
                    _lowerCamelCase			:						Any            =model(lowercase_     ,  return_dict=lowercase_     ,  **lowercase_					)
                    _lowerCamelCase			:						Union[str, Any]            =model(lowercase_     ,  return_dict=lowercase_     ,  **lowercase_					).to_tuple()

                    def recursive_check(lowercase_		:	Any     ,  lowercase_		:	str					):
                           if isinstance(lowercase_     ,  (List, Tuple)					):
                                  for tuple_iterable_value, dict_iterable_value in zip(lowercase_     ,  lowercase_					):
                                         recursive_check(lowercase_     ,  lowercase_					)
                           elif tuple_object is None:
                                  return
                           else:
                                  self.assertTrue(
                                      all(tf.equal(lowercase_     ,  lowercase_					)					)     ,  msg=(
                                          'Tuple and dict output are not equal. Difference:'
                                          F''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object					)					)}'''
                                      )     ,  )

                           recursive_check(lowercase_     ,  lowercase_					)

             for model_class in self.all_model_classes:
                    _lowerCamelCase			:						Optional[Any]            =model_class(lowercase_					)

                    _lowerCamelCase			:						Dict            =self._prepare_for_class(lowercase_     ,  lowercase_					)
                    _lowerCamelCase			:						Optional[Any]            =self._prepare_for_class(lowercase_     ,  lowercase_					)
                    check_equivalence(lowercase_     ,  lowercase_     ,  lowercase_					)

                    _lowerCamelCase			:						List[str]            =self._prepare_for_class(lowercase_     ,  lowercase_     ,  return_labels=lowercase_					)
                    _lowerCamelCase			:						Tuple            =self._prepare_for_class(lowercase_     ,  lowercase_     ,  return_labels=lowercase_					)
                    check_equivalence(lowercase_     ,  lowercase_     ,  lowercase_					)

                    _lowerCamelCase			:						Tuple            =self._prepare_for_class(lowercase_     ,  lowercase_					)
                    _lowerCamelCase			:						Union[str, Any]            =self._prepare_for_class(lowercase_     ,  lowercase_					)
                    check_equivalence(lowercase_     ,  lowercase_     ,  lowercase_     ,  {'output_hidden_states': True}					)

                    _lowerCamelCase			:						Any            =self._prepare_for_class(lowercase_     ,  lowercase_     ,  return_labels=lowercase_					)
                    _lowerCamelCase			:						Any            =self._prepare_for_class(lowercase_     ,  lowercase_     ,  return_labels=lowercase_					)
                    check_equivalence(lowercase_     ,  lowercase_     ,  lowercase_     ,  {'output_hidden_states': True}					)






      def 			lowerCamelCase			(   self		:	Tuple					)   ->		List[str]:
             """simple docstring"""




             _lowerCamelCase			:						Optional[Any]            =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*lowercase_					)






      @slow
      def 			lowerCamelCase			(   self		:	Tuple					)   ->		List[Any]:
             """simple docstring"""




             for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _lowerCamelCase			:						str            =TFRegNetModel.from_pretrained(lowercase_					)
                    self.assertIsNotNone(lowercase_					)




def       a_ (       ):
       '''simple docstring'''





       _lowerCamelCase			:						List[str]            =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
       return image




@require_tf
@require_vision
class A    (  unittest.TestCase	):






      @cached_property
      def 			lowerCamelCase			(   self		:	Any					)   ->		List[str]:
             """simple docstring"""




             return (
                 AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)
                 if is_vision_available()
                 else None
             )






      @slow
      def 			lowerCamelCase			(   self		:	Dict					)   ->		Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase			:						Dict            =TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)

             _lowerCamelCase			:						Union[str, Any]            =self.default_image_processor
             _lowerCamelCase			:						Any            =prepare_img()
             _lowerCamelCase			:						Tuple            =image_processor(images=lowercase_     ,  return_tensors='tf'					)

             # forward pass
             _lowerCamelCase			:						List[str]            =model(**lowercase_     ,  training=lowercase_					)

             # verify the logits
             _lowerCamelCase			:						Any            =tf.TensorShape((1, 1000)					)
             self.assertEqual(outputs.logits.shape     ,  lowercase_					)

             _lowerCamelCase			:						Union[str, Any]            =tf.constant([-0.4180, -1.5051, -3.4836]					)

             tf.debugging.assert_near(outputs.logits[0, :3]     ,  lowercase_     ,  atol=1E-4					)






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


lowerCamelCase		   =			logging.get_logger(__name__)

lowerCamelCase		   =			{
    'sayakpaul/vit-msn-base': 'https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json',
    # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn
}




class A    (  UpperCamelCase_	):
      UpperCamelCase__				:    Union[str, Any] ='vit_msn'






      def __init__(   self		:	Union[str, Any]     ,  lowercase_		:	List[str]=768     ,  lowercase_		:	Optional[int]=12     ,  lowercase_		:	List[str]=12     ,  lowercase_		:	List[Any]=3072     ,  lowercase_		:	int="gelu"     ,  lowercase_		:	List[Any]=0.0     ,  lowercase_		:	Optional[int]=0.0     ,  lowercase_		:	Dict=0.02     ,  lowercase_		:	Any=1E-06     ,  lowercase_		:	Union[str, Any]=224     ,  lowercase_		:	Optional[int]=16     ,  lowercase_		:	List[Any]=3     ,  lowercase_		:	Any=True     ,  **lowercase_		:	Tuple     ,  )   ->		Dict:
             """simple docstring"""




             super().__init__(**lowercase_					)

             _lowerCamelCase			:						Dict            =hidden_size
             _lowerCamelCase			:						Any            =num_hidden_layers
             _lowerCamelCase			:						Union[str, Any]            =num_attention_heads
             _lowerCamelCase			:						List[Any]            =intermediate_size
             _lowerCamelCase			:						Tuple            =hidden_act
             _lowerCamelCase			:						Any            =hidden_dropout_prob
             _lowerCamelCase			:						Dict            =attention_probs_dropout_prob
             _lowerCamelCase			:						Any            =initializer_range
             _lowerCamelCase			:						List[Any]            =layer_norm_eps
             _lowerCamelCase			:						Optional[int]            =image_size
             _lowerCamelCase			:						Optional[int]            =patch_size
             _lowerCamelCase			:						List[Any]            =num_channels
             _lowerCamelCase			:						Optional[Any]            =qkv_bias


