





"""simple docstring"""



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 		__snake_case					:





 """simple docstring"""


 def __init__(    self					,						__lowerCamelCase					,						__lowerCamelCase=3					,						__lowerCamelCase=32					,						__lowerCamelCase=3					,						__lowerCamelCase=10					,						__lowerCamelCase=[10, 20, 30, 40]					,						__lowerCamelCase=[1, 1, 2, 1]					,						__lowerCamelCase=True					,						__lowerCamelCase=True					,						__lowerCamelCase="relu"					,						__lowerCamelCase=3					,						__lowerCamelCase=None					,						):



     '''simple docstring'''




     __A    :						List[Any]						 =     parent
     __A    :						List[Any]						 =     batch_size
     __A    :						Tuple						 =     image_size
     __A    :						List[str]						 =     num_channels
     __A    :						int						 =     embeddings_size
     __A    :						List[Any]						 =     hidden_sizes
     __A    :						Any						 =     depths
     __A    :						Optional[Any]						 =     is_training
     __A    :						Optional[int]						 =     use_labels
     __A    :						Union[str, Any]						 =     hidden_act
     __A    :						Union[str, Any]						 =     num_labels
     __A    :						Union[str, Any]						 =     scope
     __A    :						Optional[int]						 =     len(__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

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

     __A    :						List[str]						 =     self.get_config()
     return config, pixel_values, labels

 def 					UpperCamelCase__(    self  ):



     '''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 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						int						 =     TFRegNetModel(config=__lowerCamelCase  )
     __A    :						List[Any]						 =     model(__lowerCamelCase					,						training=__lowerCamelCase  )
     # 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 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Union[str, Any]						 =     self.num_labels
     __A    :						Any						 =     TFRegNetForImageClassification(__lowerCamelCase  )
     __A    :						Optional[Any]						 =     model(__lowerCamelCase					,						labels=__lowerCamelCase					,						training=__lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_labels)  )







 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Dict						 =     self.prepare_config_and_inputs()
     __A ,       __A ,       __A    :						Optional[Any]						 =     config_and_inputs
     __A    :						str						 =     {'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_tf
class 		__snake_case					(		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase     ):





 """simple docstring"""


 _lowerCamelCase        =     (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
 _lowerCamelCase        =     (
     {"""feature-extraction""": TFRegNetModel, """image-classification""": TFRegNetForImageClassification}
     if is_tf_available()
     else {}
 )

 _lowerCamelCase        =     False
 _lowerCamelCase        =     False
 _lowerCamelCase        =     False
 _lowerCamelCase        =     False
 _lowerCamelCase        =     False

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Dict						 =     TFRegNetModelTester(self  )
     __A    :						Optional[Any]						 =     ConfigTester(self					,						config_class=__lowerCamelCase					,						has_text_modality=__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     return

 @unittest.skip(reason='''RegNet does not use inputs_embeds'''  )
 def 					UpperCamelCase__(    self  ):



     '''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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     super().test_keras_fit()

 @unittest.skip(reason='''RegNet does not support input and output embeddings'''  )
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     pass

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A ,       __A    :						List[str]						 =     self.model_tester.prepare_config_and_inputs_for_common()

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

         __A    :						Tuple						 =     ['''pixel_values''']
         self.assertListEqual(arg_names[:1]					,						__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						int						 =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     def check_hidden_states_output(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):
         __A    :						List[str]						 =     model_class(__lowerCamelCase  )
         __A    :						Tuple						 =     model(**self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase  )					,						training=__lowerCamelCase  )

         __A    :						Dict						 =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         __A    :						List[Any]						 =     self.model_tester.num_stages
         self.assertEqual(len(__lowerCamelCase  )					,						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]					,						)

     __A ,       __A    :						List[Any]						 =     self.model_tester.prepare_config_and_inputs_for_common()
     __A    :						Tuple						 =     ['''basic''', '''bottleneck''']
     for model_class in self.all_model_classes:
         for layer_type in layers_type:
             __A    :						Union[str, Any]						 =     layer_type
             __A    :						Dict						 =     True
             check_hidden_states_output(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  )

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

             check_hidden_states_output(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

     def check_equivalence(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase={}  ):
         __A    :						int						 =     model(__lowerCamelCase					,						return_dict=__lowerCamelCase					,						**__lowerCamelCase  )
         __A    :						int						 =     model(__lowerCamelCase					,						return_dict=__lowerCamelCase					,						**__lowerCamelCase  ).to_tuple()

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

             recursive_check(__lowerCamelCase					,						__lowerCamelCase  )

     for model_class in self.all_model_classes:
         __A    :						Union[str, Any]						 =     model_class(__lowerCamelCase  )

         __A    :						int						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase  )
         __A    :						int						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase  )
         check_equivalence(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  )

         __A    :						Optional[Any]						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase					,						return_labels=__lowerCamelCase  )
         __A    :						Optional[Any]						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase					,						return_labels=__lowerCamelCase  )
         check_equivalence(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  )

         __A    :						Optional[int]						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase  )
         __A    :						Tuple						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase  )
         check_equivalence(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase					,						{'''output_hidden_states''': True}  )

         __A    :						Optional[int]						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase					,						return_labels=__lowerCamelCase  )
         __A    :						str						 =     self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase					,						return_labels=__lowerCamelCase  )
         check_equivalence(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase					,						{'''output_hidden_states''': True}  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						str						 =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase  )







 @slow
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A    :						int						 =     TFRegNetModel.from_pretrained(__lowerCamelCase  )
         self.assertIsNotNone(__lowerCamelCase  )







def 		__lowercase     (   )   ->str:



    '''simple docstring'''







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






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





 """simple docstring"""


 @cached_property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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







 @slow
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						int						 =     TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )

     __A    :						List[str]						 =     self.default_image_processor
     __A    :						str						 =     prepare_img()
     __A    :						Optional[int]						 =     image_processor(images=__lowerCamelCase					,						return_tensors='''tf'''  )

     # forward pass
     __A    :						Dict						 =     model(**__lowerCamelCase					,						training=__lowerCamelCase  )

     # verify the logits
     __A    :						Optional[Any]						 =     tf.TensorShape((1, 1000)  )
     self.assertEqual(outputs.logits.shape					,						__lowerCamelCase  )

     __A    :						int						 =     tf.constant([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]  )

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











"""simple docstring"""



import logging

from transformers.configuration_utils import PretrainedConfig


a_ =			logging.getLogger(__name__)






class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 _lowerCamelCase        =     """masked_bert"""

 def __init__(    self					,						__lowerCamelCase=3_0522					,						__lowerCamelCase=768					,						__lowerCamelCase=12					,						__lowerCamelCase=12					,						__lowerCamelCase=3072					,						__lowerCamelCase="gelu"					,						__lowerCamelCase=0.1					,						__lowerCamelCase=0.1					,						__lowerCamelCase=512					,						__lowerCamelCase=2					,						__lowerCamelCase=0.0_2					,						__lowerCamelCase=1e-1_2					,						__lowerCamelCase=0					,						__lowerCamelCase="topK"					,						__lowerCamelCase="constant"					,						__lowerCamelCase=0.0					,						**__lowerCamelCase					,						):



     '''simple docstring'''




     super().__init__(pad_token_id=__lowerCamelCase					,						**__lowerCamelCase  )

     __A    :						Dict						 =     vocab_size
     __A    :						Union[str, Any]						 =     hidden_size
     __A    :						Tuple						 =     num_hidden_layers
     __A    :						Tuple						 =     num_attention_heads
     __A    :						Optional[Any]						 =     hidden_act
     __A    :						List[str]						 =     intermediate_size
     __A    :						Any						 =     hidden_dropout_prob
     __A    :						Optional[Any]						 =     attention_probs_dropout_prob
     __A    :						Any						 =     max_position_embeddings
     __A    :						str						 =     type_vocab_size
     __A    :						List[Any]						 =     initializer_range
     __A    :						str						 =     layer_norm_eps
     __A    :						Optional[int]						 =     pruning_method
     __A    :						str						 =     mask_init
     __A    :						Any						 =     mask_scale




