





"""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 						lowercase   :





      def __init__(			self ,		lowercase ,		lowercase=3 ,		lowercase=32 ,		lowercase=3 ,		lowercase=10 ,		lowercase=[10, 20, 30, 40] ,		lowercase=[1, 1, 2, 1] ,		lowercase=True ,		lowercase=True ,		lowercase="relu" ,		lowercase=3 ,		lowercase=None ,		)						->	Optional[int]:
            lowerCAmelCase    		=			parent
            lowerCAmelCase    		=			batch_size
            lowerCAmelCase    		=			image_size
            lowerCAmelCase    		=			num_channels
            lowerCAmelCase    		=			embeddings_size
            lowerCAmelCase    		=			hidden_sizes
            lowerCAmelCase    		=			depths
            lowerCAmelCase    		=			is_training
            lowerCAmelCase    		=			use_labels
            lowerCAmelCase    		=			hidden_act
            lowerCAmelCase    		=			num_labels
            lowerCAmelCase    		=			scope
            lowerCAmelCase    		=			len(lowercase					)





      def        _snake_case     (			self					)						->	List[str]:
            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					)						->	List[Any]:
            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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase					)						->	Optional[Any]:
            lowerCAmelCase    		=			TFRegNetModel(config=lowercase					)
            lowerCAmelCase    		=			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        _snake_case     (			self ,		lowercase ,		lowercase ,		lowercase					)						->	Optional[Any]:
            lowerCAmelCase    		=			self.num_labels
            lowerCAmelCase    		=			TFRegNetForImageClassification(lowercase					)
            lowerCAmelCase    		=			model(lowercase ,		labels=lowercase ,		training=lowercase					)
            self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_labels)					)





      def        _snake_case     (			self					)						->	Tuple:
            lowerCAmelCase    		=			self.prepare_config_and_inputs()
            lowerCAmelCase		,						lowerCAmelCase		,						lowerCAmelCase    		=			config_and_inputs
            lowerCAmelCase    		=			{"""pixel_values""": pixel_values}
            return config, inputs_dict







@require_tf
class 						lowercase   (   _UpperCAmelCase	,    _UpperCAmelCase	,    unittest.TestCase   ):
      _SCREAMING_SNAKE_CASE							      = (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
      _SCREAMING_SNAKE_CASE							      = (
          {'feature-extraction': TFRegNetModel, 'image-classification': TFRegNetForImageClassification}
          if is_tf_available()
          else {}
      )

      _SCREAMING_SNAKE_CASE							      = False
      _SCREAMING_SNAKE_CASE							      = False
      _SCREAMING_SNAKE_CASE							      = False
      _SCREAMING_SNAKE_CASE							      = False
      _SCREAMING_SNAKE_CASE							      = False





      def        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			TFRegNetModelTester(self					)
            lowerCAmelCase    		=			ConfigTester(self ,		config_class=lowercase ,		has_text_modality=lowercase					)





      def        _snake_case     (			self					)						->	Dict:
            return





      @unittest.skip(reason="""RegNet does not use inputs_embeds"""					)
      def        _snake_case     (			self					)						->	Tuple:
            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        _snake_case     (			self					)						->	Optional[Any]:
            super().test_keras_fit()





      @unittest.skip(reason="""RegNet does not support input and output embeddings"""					)
      def        _snake_case     (			self					)						->	List[Any]:
            pass





      def        _snake_case     (			self					)						->	Any:
            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.call					)
                  # 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					)						->	int:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowercase					)





      def        _snake_case     (			self					)						->	Tuple:
            def check_hidden_states_output(lowercase ,		lowercase ,		lowercase					):
                  lowerCAmelCase    		=			model_class(lowercase					)
                  lowerCAmelCase    		=			model(**self._prepare_for_class(lowercase ,		lowercase					) ,		training=lowercase					)

                  lowerCAmelCase    		=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                  lowerCAmelCase    		=			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    		=			self.model_tester.prepare_config_and_inputs_for_common()
            lowerCAmelCase    		=			["""basic""", """bottleneck"""]
            for model_class in self.all_model_classes:
                  for layer_type in layers_type:
                        lowerCAmelCase    		=			layer_type
                        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					)						->	Tuple:
            lowerCAmelCase		,						lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs_for_common()

            def check_equivalence(lowercase ,		lowercase ,		lowercase ,		lowercase={}					):
                  lowerCAmelCase    		=			model(lowercase ,		return_dict=lowercase ,		**lowercase					)
                  lowerCAmelCase    		=			model(lowercase ,		return_dict=lowercase ,		**lowercase					).to_tuple()

                  def recursive_check(lowercase ,		lowercase					):
                        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    		=			model_class(lowercase					)

                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase					)
                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase					)
                  check_equivalence(lowercase ,		lowercase ,		lowercase					)

                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase ,		return_labels=lowercase					)
                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase ,		return_labels=lowercase					)
                  check_equivalence(lowercase ,		lowercase ,		lowercase					)

                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase					)
                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase					)
                  check_equivalence(lowercase ,		lowercase ,		lowercase ,		{"""output_hidden_states""": True}					)

                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase ,		return_labels=lowercase					)
                  lowerCAmelCase    		=			self._prepare_for_class(lowercase ,		lowercase ,		return_labels=lowercase					)
                  check_equivalence(lowercase ,		lowercase ,		lowercase ,		{"""output_hidden_states""": True}					)





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*lowercase					)





      @slow
      def        _snake_case     (			self					)						->	Optional[Any]:
            for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowerCAmelCase    		=			TFRegNetModel.from_pretrained(lowercase					)
                  self.assertIsNotNone(lowercase					)







def      UpperCAmelCase__				(    ):







      '''simple docstring'''




      lowerCAmelCase    		=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
      return image






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





      @cached_property
      def        _snake_case     (			self					)						->	int:
            return (
                AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)
                if is_vision_available()
                else None
            )





      @slow
      def        _snake_case     (			self					)						->	Dict:
            lowerCAmelCase    		=			TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)

            lowerCAmelCase    		=			self.default_image_processor
            lowerCAmelCase    		=			prepare_img()
            lowerCAmelCase    		=			image_processor(images=lowercase ,		return_tensors="""tf"""					)

            # forward pass
            lowerCAmelCase    		=			model(**lowercase ,		training=lowercase					)

            # verify the logits
            lowerCAmelCase    		=			tf.TensorShape((1, 1_000)					)
            self.assertEqual(outputs.logits.shape ,		lowercase					)

            lowerCAmelCase    		=			tf.constant([-0.4_180, -1.5_051, -3.4_836]					)

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













"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE__   =  {
    "configuration_roberta_prelayernorm": [
        "ROBERTA_PRELAYERNORM_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "RobertaPreLayerNormConfig",
        "RobertaPreLayerNormOnnxConfig",
    ],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__   =  [
           "ROBERTA_PRELAYERNORM_PRETRAINED_MODEL_ARCHIVE_LIST",
           "RobertaPreLayerNormForCausalLM",
           "RobertaPreLayerNormForMaskedLM",
           "RobertaPreLayerNormForMultipleChoice",
           "RobertaPreLayerNormForQuestionAnswering",
           "RobertaPreLayerNormForSequenceClassification",
           "RobertaPreLayerNormForTokenClassification",
           "RobertaPreLayerNormModel",
           "RobertaPreLayerNormPreTrainedModel",
       ]

try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__   =  [
           "TF_ROBERTA_PRELAYERNORM_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TFRobertaPreLayerNormForCausalLM",
           "TFRobertaPreLayerNormForMaskedLM",
           "TFRobertaPreLayerNormForMultipleChoice",
           "TFRobertaPreLayerNormForQuestionAnswering",
           "TFRobertaPreLayerNormForSequenceClassification",
           "TFRobertaPreLayerNormForTokenClassification",
           "TFRobertaPreLayerNormMainLayer",
           "TFRobertaPreLayerNormModel",
           "TFRobertaPreLayerNormPreTrainedModel",
       ]

try:
       if not is_flax_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__   =  [
           "FlaxRobertaPreLayerNormForCausalLM",
           "FlaxRobertaPreLayerNormForMaskedLM",
           "FlaxRobertaPreLayerNormForMultipleChoice",
           "FlaxRobertaPreLayerNormForQuestionAnswering",
           "FlaxRobertaPreLayerNormForSequenceClassification",
           "FlaxRobertaPreLayerNormForTokenClassification",
           "FlaxRobertaPreLayerNormModel",
           "FlaxRobertaPreLayerNormPreTrainedModel",
       ]


if TYPE_CHECKING:
       from .configuration_roberta_prelayernorm import (
           ROBERTA_PRELAYERNORM_PRETRAINED_CONFIG_ARCHIVE_MAP,
           RobertaPreLayerNormConfig,
           RobertaPreLayerNormOnnxConfig,
       )

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_roberta_prelayernorm import (
                  ROBERTA_PRELAYERNORM_PRETRAINED_MODEL_ARCHIVE_LIST,
                  RobertaPreLayerNormForCausalLM,
                  RobertaPreLayerNormForMaskedLM,
                  RobertaPreLayerNormForMultipleChoice,
                  RobertaPreLayerNormForQuestionAnswering,
                  RobertaPreLayerNormForSequenceClassification,
                  RobertaPreLayerNormForTokenClassification,
                  RobertaPreLayerNormModel,
                  RobertaPreLayerNormPreTrainedModel,
              )

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_roberta_prelayernorm import (
                  TF_ROBERTA_PRELAYERNORM_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFRobertaPreLayerNormForCausalLM,
                  TFRobertaPreLayerNormForMaskedLM,
                  TFRobertaPreLayerNormForMultipleChoice,
                  TFRobertaPreLayerNormForQuestionAnswering,
                  TFRobertaPreLayerNormForSequenceClassification,
                  TFRobertaPreLayerNormForTokenClassification,
                  TFRobertaPreLayerNormMainLayer,
                  TFRobertaPreLayerNormModel,
                  TFRobertaPreLayerNormPreTrainedModel,
              )

       try:
              if not is_flax_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_flax_roberta_prelayernorm import (
                  FlaxRobertaPreLayerNormForCausalLM,
                  FlaxRobertaPreLayerNormForMaskedLM,
                  FlaxRobertaPreLayerNormForMultipleChoice,
                  FlaxRobertaPreLayerNormForQuestionAnswering,
                  FlaxRobertaPreLayerNormForSequenceClassification,
                  FlaxRobertaPreLayerNormForTokenClassification,
                  FlaxRobertaPreLayerNormModel,
                  FlaxRobertaPreLayerNormPreTrainedModel,
              )

else:
       import sys

       SCREAMING_SNAKE_CASE__   =  _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






