



"""simple docstring"""





from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
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 TFResNetForImageClassification, TFResNetModel
					from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor



class  _lowerCAmelCase     :





		"""simple docstring"""






		def __init__(       self				,     _lowercase				,     _lowercase=3				,     _lowercase=3_2				,     _lowercase=3				,     _lowercase=1_0				,     _lowercase=[1_0, 2_0, 3_0, 4_0]				,     _lowercase=[1, 1, 2, 1]				,     _lowercase=True				,     _lowercase=True				,     _lowercase="relu"				,     _lowercase=3				,     _lowercase=None				,     )			->    Optional[int]:







								'''simple docstring'''

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





		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

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

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

								snake_case_ :			Optional[Any]            =      self.get_config()

								return config, pixel_values, labels





		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								return ResNetConfig(
								    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				,     image_size=self.image_size				,     )





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







								'''simple docstring'''

								snake_case_ :			Any            =      TFResNetModel(config=_lowercase )
								snake_case_ :			Tuple            =      model(_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 // 3_2, self.image_size // 3_2)				,     )





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase )			->    Dict:







								'''simple docstring'''

								snake_case_ :			Union[str, Any]            =      self.num_labels
								snake_case_ :			Optional[Any]            =      TFResNetForImageClassification(_lowercase )
								snake_case_ :			Optional[int]            =      model(_lowercase				,     labels=_lowercase )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels) )





		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								snake_case_ :			str            =      self.prepare_config_and_inputs()
								snake_case_      ,		snake_case_      ,		snake_case_ :			Dict            =      config_and_inputs
								snake_case_ :			List[str]            =      {"""pixel_values""": pixel_values}
								return config, inputs_dict




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





		"""simple docstring"""






		_lowerCamelCase      	=							(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
		_lowerCamelCase      	=							(
		    {'''feature-extraction''': TFResNetModel, '''image-classification''': TFResNetForImageClassification}
		    if is_tf_available()
		    else {}
		)

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





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			List[str]            =      TFResNetModelTester(self )
								snake_case_ :			List[Any]            =      ConfigTester(self				,     config_class=_lowercase				,     has_text_modality=_lowercase )





		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								self.create_and_test_config_common_properties()
								self.config_tester.create_and_test_config_to_json_string()
								self.config_tester.create_and_test_config_to_json_file()
								self.config_tester.create_and_test_config_from_and_save_pretrained()
								self.config_tester.create_and_test_config_with_num_labels()
								self.config_tester.check_config_can_be_init_without_params()
								self.config_tester.check_config_arguments_init()





		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								return





		@unittest.skip(reason="""ResNet does not use inputs_embeds""" )
		def  UpperCAmelCase__				(       self )			->    Optional[Any]:







								'''simple docstring'''

								pass





		@unittest.skip(reason="""ResNet does not support input and output embeddings""" )
		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								pass





		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								snake_case_      ,		snake_case_ :			List[Any]            =      self.model_tester.prepare_config_and_inputs_for_common()

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

														snake_case_ :			str            =      ["""pixel_values"""]
														self.assertListEqual(arg_names[:1]				,     _lowercase )





		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			List[Any]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowercase )





		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								def check_hidden_states_output(_lowercase				,     _lowercase				,     _lowercase ):
														snake_case_ :			str            =      model_class(_lowercase )
														snake_case_ :			Tuple            =      model(**self._prepare_for_class(_lowercase				,     _lowercase ) )

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

														snake_case_ :			Any            =      self.model_tester.num_stages
														self.assertEqual(len(_lowercase )				,     expected_num_stages + 1 )

														# ResNet'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 // 4, self.model_tester.image_size // 4]				,     )

								snake_case_      ,		snake_case_ :			Union[str, Any]            =      self.model_tester.prepare_config_and_inputs_for_common()
								snake_case_ :			Optional[Any]            =      ["""basic""", """bottleneck"""]
								for model_class in self.all_model_classes:
														for layer_type in layers_type:
																				snake_case_ :			List[str]            =      layer_type
																				snake_case_ :			Any            =      True
																				check_hidden_states_output(_lowercase				,     _lowercase				,     _lowercase )

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

																				check_hidden_states_output(_lowercase				,     _lowercase				,     _lowercase )





		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			List[str]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_lowercase )





		@slow
		def  UpperCAmelCase__				(       self )			->    List[str]:







								'''simple docstring'''

								for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														snake_case_ :			str            =      TFResNetModel.from_pretrained(_lowercase )
														self.assertIsNotNone(_lowercase )




def 						__lowerCAmelCase    (		):


						'''simple docstring'''






						snake_case_ :			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 )			->    Tuple:







								'''simple docstring'''

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





		@slow
		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								snake_case_ :			Optional[int]            =      TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

								snake_case_ :			str            =      self.default_image_processor
								snake_case_ :			Optional[Any]            =      prepare_img()
								snake_case_ :			Dict            =      image_processor(images=_lowercase				,     return_tensors="""tf""" )

								# forward pass
								snake_case_ :			Dict            =      model(**_lowercase )

								# verify the logits
								snake_case_ :			str            =      tf.TensorShape((1, 1_0_0_0) )
								self.assertEqual(outputs.logits.shape				,     _lowercase )

								snake_case_ :			Optional[Any]            =      tf.constant([-11.1069, -9.7877, -8.3777] )

								self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()				,     _lowercase				,     atol=1E-4 ) )





"""simple docstring"""



from typing import Mapping

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


__snake_case       =       logging.get_logger(__name__)

__snake_case       =       {
    """google/umt5-small""": """https://huggingface.co/google/umt5-small/resolve/main/config.json""",
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}







class _lowerCAmelCase							( snake_case_      ):
   __UpperCAmelCase					:			Dict				    =    '''umt5'''
   __UpperCAmelCase					:			str				    =    ['''past_key_values''']







   def __init__(      self		,      UpperCamelCase__=25_0112		,      UpperCamelCase__=512		,      UpperCamelCase__=64		,      UpperCamelCase__=1024		,      UpperCamelCase__=8		,      UpperCamelCase__=None		,      UpperCamelCase__=6		,      UpperCamelCase__=32		,      UpperCamelCase__=128		,      UpperCamelCase__=0.1		,      UpperCamelCase__=1e-6		,      UpperCamelCase__=1.0		,      UpperCamelCase__="gated-gelu"		,      UpperCamelCase__=True		,      UpperCamelCase__=True		,      UpperCamelCase__="T5Tokenizer"		,      UpperCamelCase__=True		,      UpperCamelCase__=0		,      UpperCamelCase__=1		,      UpperCamelCase__=0		,      **UpperCamelCase__		,      )      ->						Dict:

       '''simple docstring'''




       super().__init__(
           is_encoder_decoder=UpperCamelCase__		,      tokenizer_class=UpperCamelCase__		,      tie_word_embeddings=UpperCamelCase__		,      pad_token_id=UpperCamelCase__		,      eos_token_id=UpperCamelCase__		,      decoder_start_token_id=UpperCamelCase__		,      **UpperCamelCase__		,      )
       snake_case       : Optional[int]    =       vocab_size
       snake_case       : Any    =       d_model
       snake_case       : Optional[Any]    =       d_kv
       snake_case       : Any    =       d_ff
       snake_case       : Optional[Any]    =       num_layers
       snake_case       : Optional[Any]    =       (
           num_decoder_layers if num_decoder_layers is not None else self.num_layers
       )  # default = symmetry
       snake_case       : Optional[Any]    =       num_heads
       snake_case       : Optional[Any]    =       relative_attention_num_buckets
       snake_case       : Dict    =       relative_attention_max_distance
       snake_case       : Tuple    =       dropout_rate
       snake_case       : List[Any]    =       layer_norm_epsilon
       snake_case       : Any    =       initializer_factor
       snake_case       : Optional[Any]    =       feed_forward_proj
       snake_case       : Tuple    =       use_cache

       snake_case       : Any    =       self.feed_forward_proj.split("-"		)
       snake_case       : Tuple    =       act_info[-1]
       snake_case       : Optional[int]    =       act_info[0] == "gated"

       if len(UpperCamelCase__		) > 1 and act_info[0] != "gated" or len(UpperCamelCase__		) > 2:
           raise ValueError(
               F'`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'
               "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
               "'gated-gelu' or 'relu'"		)

       if feed_forward_proj == "gated-gelu":
           snake_case       : List[str]    =       "gelu_new"







   @property
   def 					lowerCamelCase						(      self		)      ->						Any:

       '''simple docstring'''




       return self.d_model







   @property
   def 					lowerCamelCase						(      self		)      ->						str:

       '''simple docstring'''




       return self.num_heads







   @property
   def 					lowerCamelCase						(      self		)      ->						Union[str, Any]:

       '''simple docstring'''




       return self.num_layers







class _lowerCAmelCase							( snake_case_      ):







   @property
   # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
   def 					lowerCamelCase						(      self		)      ->						Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




       snake_case       : Optional[int]    =       {
           "input_ids": {0: "batch", 1: "encoder_sequence"},
           "attention_mask": {0: "batch", 1: "encoder_sequence"},
       }
       if self.use_past:
           snake_case       : Tuple    =       "past_encoder_sequence + sequence"
           snake_case       : int    =       {0: "batch"}
           snake_case       : int    =       {0: "batch", 1: "past_decoder_sequence + sequence"}
       else:
           snake_case       : List[Any]    =       {0: "batch", 1: "decoder_sequence"}
           snake_case       : Any    =       {0: "batch", 1: "decoder_sequence"}

       if self.use_past:
           self.fill_with_past_key_values_(UpperCamelCase__		,      direction="inputs"		)

       return common_inputs







   @property
   # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
   def 					lowerCamelCase						(      self		)      ->						int:

       '''simple docstring'''




       return 13







   @property
   def 					lowerCamelCase						(      self		)      ->						float:

       '''simple docstring'''




       return 5e-4





