


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





							'''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      ,					) ->							Tuple:
														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(lowerCamelCase_     )







							def 						__SCREAMING_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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Any:
														lowerCAmelCase__      =			TFResNetModel(config=lowerCamelCase_     )
														lowerCAmelCase__      =			model(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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							List[Any]:
														lowerCAmelCase__      =			self.num_labels
														lowerCAmelCase__      =			TFResNetForImageClassification(lowerCamelCase_     )
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					labels=lowerCamelCase_     )
														self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_labels)     )







							def 						__SCREAMING_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 	a__       (							a__       ,       a__       ,       unittest.TestCase						):





							'''simple docstring'''






							lowercase__    :     Union[str, Any]					=   (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
							lowercase__    :     Any					=   (
							    {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification}
							    if is_tf_available()
							    else {}
							)

							lowercase__    :     int					=   False
							lowercase__    :     List[str]					=   False
							lowercase__    :     str					=   False
							lowercase__    :     Tuple					=   False
							lowercase__    :     Union[str, Any]					=   False







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Any:
														lowerCAmelCase__      =			TFResNetModelTester(self     )
														lowerCAmelCase__      =			ConfigTester(self      ,					config_class=lowerCamelCase_      ,					has_text_modality=lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Any:
														return







							@unittest.skip(reason='''ResNet does not use inputs_embeds'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														pass







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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__      =			model_class(lowerCamelCase_     )
																					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]      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														def check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ):
																					lowerCAmelCase__      =			model_class(lowerCamelCase_     )
																					lowerCAmelCase__      =			model(**self._prepare_for_class(lowerCamelCase_      ,					lowerCamelCase_     )     )

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

																					lowerCAmelCase__      =			self.model_tester.num_stages
																					self.assertEqual(len(lowerCamelCase_     )      ,					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]      ,					)

														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(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )

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

																												check_hidden_states_output(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_     )







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__      =			TFResNetModel.from_pretrained(lowerCamelCase_     )
																					self.assertIsNotNone(lowerCamelCase_     )


def       _snake_case     (  )		->					Dict:
							lowerCAmelCase__      =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
							return image






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





							'''simple docstring'''






							@cached_property
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														return (
														    AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )
														    if is_vision_available()
														    else None
														)







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )

														lowerCAmelCase__      =			self.default_image_processor
														lowerCAmelCase__      =			prepare_img()
														lowerCAmelCase__      =			image_processor(images=lowerCamelCase_      ,					return_tensors='''tf'''     )

														# forward pass
														lowerCAmelCase__      =			model(**lowerCamelCase_     )

														# verify the logits
														lowerCAmelCase__      =			tf.TensorShape((1, 10_00)     )
														self.assertEqual(outputs.logits.shape      ,					lowerCamelCase_     )

														lowerCAmelCase__      =			tf.constant([-11.1_069, -9.7_877, -8.3_777]     )

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



'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase					       =							logging.get_logger(__name__)

__UpperCAmelCase					       =							{'''ctrl''': '''https://huggingface.co/ctrl/resolve/main/config.json'''}




class 	a__       (							a__						):





							'''simple docstring'''






							lowercase__    :     Dict					=   "ctrl"
							lowercase__    :     str					=   ["past_key_values"]
							lowercase__    :     Optional[int]					=   {
							    "max_position_embeddings": "n_positions",
							    "hidden_size": "n_embd",
							    "num_attention_heads": "n_head",
							    "num_hidden_layers": "n_layer",
							}







							def __init__(	self      ,					lowerCamelCase_=24_65_34      ,					lowerCamelCase_=2_56      ,					lowerCamelCase_=12_80      ,					lowerCamelCase_=81_92      ,					lowerCamelCase_=48      ,					lowerCamelCase_=16      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=1e-6      ,					lowerCamelCase_=0.02      ,					lowerCamelCase_=True      ,					**lowerCamelCase_      ,					) ->							Union[str, Any]:
														lowerCAmelCase__      =			vocab_size
														lowerCAmelCase__      =			n_positions
														lowerCAmelCase__      =			n_embd
														lowerCAmelCase__      =			n_layer
														lowerCAmelCase__      =			n_head
														lowerCAmelCase__      =			dff
														lowerCAmelCase__      =			resid_pdrop
														lowerCAmelCase__      =			embd_pdrop
														lowerCAmelCase__      =			layer_norm_epsilon
														lowerCAmelCase__      =			initializer_range

														lowerCAmelCase__      =			use_cache

														super().__init__(**lowerCamelCase_     )