




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






			'''simple docstring'''





			def __init__( self			:   Dict       ,						lowerCamelCase_			:   int       ,						lowerCamelCase_			:   List[str]=3       ,						lowerCamelCase_			:   Optional[Any]=32       ,						lowerCamelCase_			:   List[str]=3       ,						lowerCamelCase_			:   Optional[int]=10       ,						lowerCamelCase_			:   int=[10, 20, 30, 40]       ,						lowerCamelCase_			:   Optional[Any]=[1, 1, 2, 1]       ,						lowerCamelCase_			:   List[Any]=True       ,						lowerCamelCase_			:   Any=True       ,						lowerCamelCase_			:   Optional[Any]="relu"       ,						lowerCamelCase_			:   str=3       ,						lowerCamelCase_			:   int=None       ,						)						->   Union[str, Any]:
								__magic_name__							:	Union[str, Any]    							=							parent
								__magic_name__							:	Optional[Any]    							=							batch_size
								__magic_name__							:	Optional[Any]    							=							image_size
								__magic_name__							:	Tuple    							=							num_channels
								__magic_name__							:	Optional[Any]    							=							embeddings_size
								__magic_name__							:	List[Any]    							=							hidden_sizes
								__magic_name__							:	Optional[Any]    							=							depths
								__magic_name__							:	Dict    							=							is_training
								__magic_name__							:	Union[str, Any]    							=							use_labels
								__magic_name__							:	Tuple    							=							hidden_act
								__magic_name__							:	Union[str, Any]    							=							num_labels
								__magic_name__							:	Union[str, Any]    							=							scope
								__magic_name__							:	Union[str, Any]    							=							len(lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   int  )						->   str:
								__magic_name__							:	List[str]    							=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

								__magic_name__							:	Dict    							=							self.get_config()
								return config, pixel_values, labels

			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   str:
								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			:   int       ,						lowerCamelCase_			:   Union[str, Any]       ,						lowerCamelCase_			:   Tuple       ,						lowerCamelCase_			:   List[str]  )						->   Tuple:
								__magic_name__							:	Dict    							=							TFRegNetModel(config=lowerCamelCase_  )
								__magic_name__							:	int    							=							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			:   Union[str, Any]       ,						lowerCamelCase_			:   Union[str, Any]       ,						lowerCamelCase_			:   Optional[Any]       ,						lowerCamelCase_			:   Optional[int]  )						->   str:
								__magic_name__							:	int    							=							self.num_labels
								__magic_name__							:	Union[str, Any]    							=							TFRegNetForImageClassification(lowerCamelCase_  )
								__magic_name__							:	int    							=							model(lowerCamelCase_       ,						labels=lowerCamelCase_       ,						training=lowerCamelCase_  )
								self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.num_labels)  )


			def 							UpperCAmelCase__       ( self			:   str  )						->   Union[str, Any]:
								__magic_name__							:	str    							=							self.prepare_config_and_inputs()
								__magic_name__							,    __magic_name__							,    __magic_name__							:	Optional[int]    							=							config_and_inputs
								__magic_name__							:	str    							=							{'''pixel_values''': pixel_values}
								return config, inputs_dict






@require_tf
class    lowerCamelCase    (		_lowerCamelCase				,_lowerCamelCase				,unittest.TestCase						):






			'''simple docstring'''





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

			UpperCamelCase__								=False
			UpperCamelCase__								=False
			UpperCamelCase__								=False
			UpperCamelCase__								=False
			UpperCamelCase__								=False

			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Dict:
								__magic_name__							:	Tuple    							=							TFRegNetModelTester(self  )
								__magic_name__							:	Optional[Any]    							=							ConfigTester(self       ,						config_class=lowerCamelCase_       ,						has_text_modality=lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   int:
								return

			@unittest.skip(reason='''RegNet does not use inputs_embeds'''  )
			def 							UpperCAmelCase__       ( self			:   int  )						->   Union[str, Any]:
								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			:   Any  )						->   int:
								super().test_keras_fit()

			@unittest.skip(reason='''RegNet does not support input and output embeddings'''  )
			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Optional[int]:
								pass

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   List[str]:
								__magic_name__							,    __magic_name__							:	int    							=							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__magic_name__							:	Dict    							=							model_class(lowerCamelCase_  )
													__magic_name__							:	Union[str, Any]    							=							inspect.signature(model.call  )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													__magic_name__							:	List[str]    							=							[*signature.parameters.keys()]

													__magic_name__							:	Tuple    							=							['''pixel_values''']
													self.assertListEqual(arg_names[:1]       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   List[str]:
								__magic_name__							:	Optional[int]    							=							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Any  )						->   Dict:
								def check_hidden_states_output(lowerCamelCase_			:   Any       ,						lowerCamelCase_			:   int       ,						lowerCamelCase_			:   Optional[Any]  ):
													__magic_name__							:	str    							=							model_class(lowerCamelCase_  )
													__magic_name__							:	Optional[Any]    							=							model(**self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_  )       ,						training=lowerCamelCase_  )

													__magic_name__							:	str    							=							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													__magic_name__							:	List[str]    							=							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]       ,						)

								__magic_name__							,    __magic_name__							:	Tuple    							=							self.model_tester.prepare_config_and_inputs_for_common()
								__magic_name__							:	Optional[Any]    							=							['''basic''', '''bottleneck''']
								for model_class in self.all_model_classes:
													for layer_type in layers_type:
																		__magic_name__							:	Union[str, Any]    							=							layer_type
																		__magic_name__							:	str    							=							True
																		check_hidden_states_output(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_  )

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		__magic_name__							:	List[Any]    							=							True

																		check_hidden_states_output(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   List[str]:
								__magic_name__							,    __magic_name__							:	Dict    							=							self.model_tester.prepare_config_and_inputs_for_common()

								def check_equivalence(lowerCamelCase_			:   Union[str, Any]       ,						lowerCamelCase_			:   Union[str, Any]       ,						lowerCamelCase_			:   int       ,						lowerCamelCase_			:   str={}  ):
													__magic_name__							:	List[str]    							=							model(lowerCamelCase_       ,						return_dict=lowerCamelCase_       ,						**lowerCamelCase_  )
													__magic_name__							:	Tuple    							=							model(lowerCamelCase_       ,						return_dict=lowerCamelCase_       ,						**lowerCamelCase_  ).to_tuple()

													def recursive_check(lowerCamelCase_			:   Tuple       ,						lowerCamelCase_			:   List[Any]  ):
																		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:
													__magic_name__							:	Any    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Dict    							=							self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_  )
													__magic_name__							:	Any    							=							self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_  )
													check_equivalence(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_  )

													__magic_name__							:	List[str]    							=							self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_       ,						return_labels=lowerCamelCase_  )
													__magic_name__							:	List[Any]    							=							self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_       ,						return_labels=lowerCamelCase_  )
													check_equivalence(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_  )

													__magic_name__							:	Tuple    							=							self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_  )
													__magic_name__							:	List[str]    							=							self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_  )
													check_equivalence(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_       ,						{'''output_hidden_states''': True}  )

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

			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   List[Any]:
								__magic_name__							:	Any    							=							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_  )


			@slow
			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   Tuple:
								for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__magic_name__							:	int    							=							TFRegNetModel.from_pretrained(lowerCamelCase_  )
													self.assertIsNotNone(lowerCamelCase_  )







def 		lowercase__   (		):





					'''simple docstring'''






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


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






			'''simple docstring'''





			@cached_property
			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   str:
								return (
								    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )
								    if is_vision_available()
								    else None
								)


			@slow
			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   int:
								__magic_name__							:	Union[str, Any]    							=							TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )

								__magic_name__							:	Any    							=							self.default_image_processor
								__magic_name__							:	Dict    							=							prepare_img()
								__magic_name__							:	List[Any]    							=							image_processor(images=lowerCamelCase_       ,						return_tensors='''tf'''  )

								# forward pass
								__magic_name__							:	Optional[Any]    							=							model(**lowerCamelCase_       ,						training=lowerCamelCase_  )

								# verify the logits
								__magic_name__							:	Union[str, Any]    							=							tf.TensorShape((1, 1000)  )
								self.assertEqual(outputs.logits.shape       ,						lowerCamelCase_  )

								__magic_name__							:	str    							=							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  )









from typing import TYPE_CHECKING

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


__lowerCamelCase   :						List[str]				=			{
    '''configuration_roberta_prelayernorm''': [
        '''ROBERTA_PRELAYERNORM_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''RobertaPreLayerNormConfig''',
        '''RobertaPreLayerNormOnnxConfig''',
    ],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase   :						Dict				=			[
	    '''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:
	__lowerCamelCase   :						Optional[Any]				=			[
	    '''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:
	__lowerCamelCase   :						Optional[Any]				=			[
	    '''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

	__lowerCamelCase   :						Dict				=			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



