


'''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 						a__					:





			'''simple docstring'''

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

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

						__A=	self.get_config()
						return config, pixel_values, labels
			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		int:
						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							:			Optional[int]			,    lowerCAmelCase_							:			int			,    lowerCAmelCase_							:			Optional[Any]			,    lowerCAmelCase_							:			str	)      ->		Union[str, Any]:
						__A=	TFRegNetModel(config=lowerCAmelCase_	)
						__A=	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 					lowerCAmelCase     (  self							:			Union[str, Any]			,    lowerCAmelCase_							:			Union[str, Any]			,    lowerCAmelCase_							:			Union[str, Any]			,    lowerCAmelCase_							:			Optional[Any]	)      ->		Any:
						__A=	self.num_labels
						__A=	TFRegNetForImageClassification(lowerCAmelCase_	)
						__A=	model(lowerCAmelCase_			,    labels=lowerCAmelCase_			,    training=lowerCAmelCase_	)
						self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels)	)

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


@require_tf
class 						a__					(					a_      ,  a_      ,  unittest.TestCase   ):





			'''simple docstring'''

			A						:				Optional[int]					      =			(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
			A						:				Optional[int]					      =			(
			    {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
			    if is_tf_available()
			    else {}
			)

			A						:				int					      =			False
			A						:				Dict					      =			False
			A						:				Union[str, Any]					      =			False
			A						:				Any					      =			False
			A						:				str					      =			False
			def 					lowerCAmelCase     (  self							:			Any	)      ->		Optional[Any]:
						__A=	TFRegNetModelTester(self	)
						__A=	ConfigTester(self			,    config_class=lowerCAmelCase_			,    has_text_modality=lowerCAmelCase_	)
			def 					lowerCAmelCase     (  self							:			Optional[int]	)      ->		int:
						return
			@unittest.skip(reason='RegNet does not use inputs_embeds'	)
			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		str:
						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							:			Dict	)      ->		Optional[int]:
						super().test_keras_fit()
			@unittest.skip(reason='RegNet does not support input and output embeddings'	)
			def 					lowerCAmelCase     (  self							:			Dict	)      ->		Any:
						pass
			def 					lowerCAmelCase     (  self							:			Any	)      ->		Optional[int]:
						__A,	__A=	self.model_tester.prepare_config_and_inputs_for_common()

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

									__A=	['pixel_values']
									self.assertListEqual(arg_names[:1]			,    lowerCAmelCase_	)
			def 					lowerCAmelCase     (  self							:			Dict	)      ->		Tuple:
						__A=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase_	)
			def 					lowerCAmelCase     (  self							:			Dict	)      ->		Optional[Any]:
						def check_hidden_states_output(lowerCAmelCase_							:			List[Any]			,    lowerCAmelCase_							:			Any			,    lowerCAmelCase_							:			Tuple	):
									__A=	model_class(lowerCAmelCase_	)
									__A=	model(**self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_	)			,    training=lowerCAmelCase_	)

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

									__A=	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=	self.model_tester.prepare_config_and_inputs_for_common()
						__A=	['basic', 'bottleneck']
						for model_class in self.all_model_classes:
									for layer_type in layers_type:
												__A=	layer_type
												__A=	True
												check_hidden_states_output(lowerCAmelCase_			,    lowerCAmelCase_			,    lowerCAmelCase_	)

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

												check_hidden_states_output(lowerCAmelCase_			,    lowerCAmelCase_			,    lowerCAmelCase_	)
			def 					lowerCAmelCase     (  self							:			int	)      ->		str:
						__A,	__A=	self.model_tester.prepare_config_and_inputs_for_common()

						def check_equivalence(lowerCAmelCase_							:			int			,    lowerCAmelCase_							:			str			,    lowerCAmelCase_							:			Dict			,    lowerCAmelCase_							:			Optional[int]={}	):
									__A=	model(lowerCAmelCase_			,    return_dict=lowerCAmelCase_			,    **lowerCAmelCase_	)
									__A=	model(lowerCAmelCase_			,    return_dict=lowerCAmelCase_			,    **lowerCAmelCase_	).to_tuple()

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

									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_	)
									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_	)
									check_equivalence(lowerCAmelCase_			,    lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_			,    return_labels=lowerCAmelCase_	)
									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_			,    return_labels=lowerCAmelCase_	)
									check_equivalence(lowerCAmelCase_			,    lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_	)
									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_	)
									check_equivalence(lowerCAmelCase_			,    lowerCAmelCase_			,    lowerCAmelCase_			,    {'output_hidden_states': True}	)

									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_			,    return_labels=lowerCAmelCase_	)
									__A=	self._prepare_for_class(lowerCAmelCase_			,    lowerCAmelCase_			,    return_labels=lowerCAmelCase_	)
									check_equivalence(lowerCAmelCase_			,    lowerCAmelCase_			,    lowerCAmelCase_			,    {'output_hidden_states': True}	)
			def 					lowerCAmelCase     (  self							:			List[Any]	)      ->		List[str]:
						__A=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_	)

			@slow
			def 					lowerCAmelCase     (  self							:			Any	)      ->		str:
						for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	TFRegNetModel.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)






def 						UpperCAmelCase__(				):






			"""simple docstring"""




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






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





			'''simple docstring'''

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

			@slow
			def 					lowerCAmelCase     (  self							:			int	)      ->		Optional[Any]:
						__A=	TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

						__A=	self.default_image_processor
						__A=	prepare_img()
						__A=	image_processor(images=lowerCAmelCase_			,    return_tensors='tf'	)

						# forward pass
						__A=	model(**lowerCAmelCase_			,    training=lowerCAmelCase_	)

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

						__A=	tf.constant([-0.41_80, -1.50_51, -3.48_36]	)

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







'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase__  			=				logging.get_logger(__name__)

UpperCAmelCase__  			=				{
    '''facebook/xlm-roberta-xl''': '''https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json''',
    '''facebook/xlm-roberta-xxl''': '''https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json''',
    # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl
}
class 						a__					(					a_   ):





			'''simple docstring'''

			A						:				Tuple					      =			'''xlm-roberta-xl'''
			def __init__(  self							:			Union[str, Any]			,    lowerCAmelCase_							:			Tuple=250_880			,    lowerCAmelCase_							:			Optional[int]=2_560			,    lowerCAmelCase_							:			Optional[Any]=36			,    lowerCAmelCase_							:			List[str]=32			,    lowerCAmelCase_							:			Optional[Any]=10_240			,    lowerCAmelCase_							:			Union[str, Any]="gelu"			,    lowerCAmelCase_							:			Optional[Any]=0.1			,    lowerCAmelCase_							:			Optional[Any]=0.1			,    lowerCAmelCase_							:			Optional[int]=514			,    lowerCAmelCase_							:			List[str]=1			,    lowerCAmelCase_							:			Optional[Any]=0.02			,    lowerCAmelCase_							:			List[str]=1E-05			,    lowerCAmelCase_							:			int=1			,    lowerCAmelCase_							:			List[str]=0			,    lowerCAmelCase_							:			List[Any]=2			,    lowerCAmelCase_							:			Optional[int]="absolute"			,    lowerCAmelCase_							:			List[Any]=True			,    lowerCAmelCase_							:			Tuple=None			,    **lowerCAmelCase_							:			List[str]			,    )      ->		str:
						super().__init__(pad_token_id=lowerCAmelCase_			,    bos_token_id=lowerCAmelCase_			,    eos_token_id=lowerCAmelCase_			,    **lowerCAmelCase_	)
						__A=	vocab_size
						__A=	hidden_size
						__A=	num_hidden_layers
						__A=	num_attention_heads
						__A=	hidden_act
						__A=	intermediate_size
						__A=	hidden_dropout_prob
						__A=	attention_probs_dropout_prob
						__A=	max_position_embeddings
						__A=	type_vocab_size
						__A=	initializer_range
						__A=	layer_norm_eps
						__A=	position_embedding_type
						__A=	use_cache
						__A=	classifier_dropout


class 						a__					(					a_   ):





			'''simple docstring'''

			@property
			def 					lowerCAmelCase     (  self							:			Optional[int]	)      ->		Mapping[str, Mapping[int, str]]:
						if self.task == "multiple-choice":
									__A=	{0: 'batch', 1: 'choice', 2: 'sequence'}
						else:
									__A=	{0: 'batch', 1: 'sequence'}
						return OrderedDict(
						    [
						        ('input_ids', dynamic_axis),
						        ('attention_mask', dynamic_axis),
						    ]	)



