






'''simple docstring'''







import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import EfficientNetForImageClassification, EfficientNetModel
			from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor

class _lowercase      :




							def __init__(				self:							Tuple     ,				UpperCamelCase__:							List[Any]     ,				UpperCamelCase__:							Union[str, Any]=13     ,				UpperCamelCase__:							Optional[int]=32     ,				UpperCamelCase__:							int=3     ,				UpperCamelCase__:							Any=[3, 3, 5]     ,				UpperCamelCase__:							Optional[Any]=[32, 16, 24]     ,				UpperCamelCase__:							int=[16, 24, 40]     ,				UpperCamelCase__:							Union[str, Any]=[1, 1, 2]     ,				UpperCamelCase__:							Union[str, Any]=[1, 1, 2]     ,				UpperCamelCase__:							int=[1, 6, 6]     ,				UpperCamelCase__:							List[Any]=True     ,				UpperCamelCase__:							List[str]=True     ,				UpperCamelCase__:							Tuple=37     ,				UpperCamelCase__:							Tuple="gelu"     ,				UpperCamelCase__:							int=10     ,				):
														lowerCamelCase__   :      List[Any]			    =			parent
														lowerCamelCase__   :      Tuple			    =			batch_size
														lowerCamelCase__   :      List[str]			    =			image_size
														lowerCamelCase__   :      List[str]			    =			num_channels
														lowerCamelCase__   :      Optional[Any]			    =			kernel_sizes
														lowerCamelCase__   :      Optional[int]			    =			in_channels
														lowerCamelCase__   :      Dict			    =			out_channels
														lowerCamelCase__   :      Union[str, Any]			    =			strides
														lowerCamelCase__   :      str			    =			num_block_repeats
														lowerCamelCase__   :      Union[str, Any]			    =			expand_ratios
														lowerCamelCase__   :      int			    =			is_training
														lowerCamelCase__   :      Dict			    =			hidden_act
														lowerCamelCase__   :      Any			    =			num_labels
														lowerCamelCase__   :      str			    =			use_labels




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Dict			    =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

														lowerCamelCase__   :      List[Any]			    =			None
														if self.use_labels:
																					lowerCamelCase__   :      Union[str, Any]			    =			ids_tensor([self.batch_size]     ,				self.num_labels	)

														lowerCamelCase__   :      Tuple			    =			self.get_config()
														return config, pixel_values, labels




							def 						lowerCamelCase_							(				self:							Dict	):
														return EfficientNetConfig(
														    num_channels=self.num_channels     ,				kernel_sizes=self.kernel_sizes     ,				in_channels=self.in_channels     ,				out_channels=self.out_channels     ,				strides=self.strides     ,				num_block_repeats=self.num_block_repeats     ,				expand_ratios=self.expand_ratios     ,				hidden_act=self.hidden_act     ,				num_labels=self.num_labels     ,				)




							def 						lowerCamelCase_							(				self:							List[Any]     ,				UpperCamelCase__:							List[str]     ,				UpperCamelCase__:							Optional[int]     ,				UpperCamelCase__:							Union[str, Any]	):
														lowerCamelCase__   :      Optional[Any]			    =			EfficientNetModel(config=UpperCamelCase__	)
														model.to(UpperCamelCase__	)
														model.eval()
														lowerCamelCase__   :      List[Any]			    =			model(UpperCamelCase__	)
														# expected last hidden states: B, C, H // 4, W // 4
														self.parent.assertEqual(
														    result.last_hidden_state.shape     ,				(self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)     ,				)




							def 						lowerCamelCase_							(				self:							Optional[int]     ,				UpperCamelCase__:							str     ,				UpperCamelCase__:							Optional[Any]     ,				UpperCamelCase__:							List[Any]	):
														lowerCamelCase__   :      int			    =			EfficientNetForImageClassification(UpperCamelCase__	)
														model.to(UpperCamelCase__	)
														model.eval()
														lowerCamelCase__   :      Any			    =			model(UpperCamelCase__     ,				labels=UpperCamelCase__	)
														self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.num_labels)	)




							def 						lowerCamelCase_							(				self:							Dict	):
														lowerCamelCase__   :      Any			    =			self.prepare_config_and_inputs()
														lowerCamelCase__    ,						lowerCamelCase__    ,						lowerCamelCase__   :      str			    =			config_and_inputs
														lowerCamelCase__   :      Any			    =			{"""pixel_values""": pixel_values}
														return config, inputs_dict



@require_torch
class _lowercase      (							_lowercase							, _lowercase							, unittest.TestCase						):
							a							 =			(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
							a							 =			(
							    {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
							    if is_torch_available()
							    else {}
							)

							a							 =			False
							a							 =			False
							a							 =			False
							a							 =			False
							a							 =			False




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__   :      Union[str, Any]			    =			EfficientNetModelTester(self	)
														lowerCamelCase__   :      Dict			    =			ConfigTester(
														    self     ,				config_class=UpperCamelCase__     ,				has_text_modality=UpperCamelCase__     ,				hidden_size=37	)




							def 						lowerCamelCase_							(				self:							Any	):
														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 						lowerCamelCase_							(				self:							Optional[Any]	):
														return




							@unittest.skip(reason="""EfficientNet does not use inputs_embeds"""	)
							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														pass




							@unittest.skip(reason="""EfficientNet does not support input and output embeddings"""	)
							def 						lowerCamelCase_							(				self:							Dict	):
														pass




							@unittest.skip(reason="""EfficientNet does not use feedforward chunking"""	)
							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														pass




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__    ,						lowerCamelCase__   :      Optional[Any]			    =			self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCamelCase__   :      Optional[Any]			    =			model_class(UpperCamelCase__	)
																					lowerCamelCase__   :      Any			    =			inspect.signature(model.forward	)
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					lowerCamelCase__   :      Dict			    =			[*signature.parameters.keys()]

																					lowerCamelCase__   :      Dict			    =			["""pixel_values"""]
																					self.assertListEqual(arg_names[:1]     ,				UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      int			    =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														def check_hidden_states_output(UpperCamelCase__:							List[str]     ,				UpperCamelCase__:							Union[str, Any]     ,				UpperCamelCase__:							Tuple	):
																					lowerCamelCase__   :      Tuple			    =			model_class(UpperCamelCase__	)
																					model.to(UpperCamelCase__	)
																					model.eval()

																					with torch.no_grad():
																												lowerCamelCase__   :      Optional[Any]			    =			model(**self._prepare_for_class(UpperCamelCase__     ,				UpperCamelCase__	)	)

																					lowerCamelCase__   :      Optional[Any]			    =			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
																					lowerCamelCase__   :      int			    =			sum(config.num_block_repeats	) * 4
																					self.assertEqual(len(UpperCamelCase__	)     ,				UpperCamelCase__	)

																					# EfficientNet'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__   :      Optional[int]			    =			self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCamelCase__   :      str			    =			True
																					check_hidden_states_output(UpperCamelCase__     ,				UpperCamelCase__     ,				UpperCamelCase__	)

																					# check that output_hidden_states also work using config
																					del inputs_dict["output_hidden_states"]
																					lowerCamelCase__   :      Optional[int]			    =			True

																					check_hidden_states_output(UpperCamelCase__     ,				UpperCamelCase__     ,				UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							Any	):
														lowerCamelCase__   :      Tuple			    =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__	)




							@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""	)
							def 						lowerCamelCase_							(				self:							str	):
														pass




							@slow
							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__   :      int			    =			EfficientNetModel.from_pretrained(UpperCamelCase__	)
																					self.assertIsNotNone(UpperCamelCase__	)




							@is_pipeline_test
							@require_vision
							@slow
							def 						lowerCamelCase_							(				self:							Optional[int]	):
														super().test_pipeline_image_classification()





def 					SCREAMING_SNAKE_CASE_			() ->			Optional[Any]:
							lowerCamelCase__   :      Dict			    =			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
							return image



@require_torch
@require_vision
class _lowercase      (							unittest.TestCase						):




							@cached_property
							def 						lowerCamelCase_							(				self:							List[str]	):
														return AutoImageProcessor.from_pretrained("""google/efficientnet-b7"""	) if is_vision_available() else None




							@slow
							def 						lowerCamelCase_							(				self:							int	):
														lowerCamelCase__   :      int			    =			EfficientNetForImageClassification.from_pretrained("""google/efficientnet-b7"""	).to(UpperCamelCase__	)

														lowerCamelCase__   :      List[Any]			    =			self.default_image_processor
														lowerCamelCase__   :      Union[str, Any]			    =			prepare_img()
														lowerCamelCase__   :      List[str]			    =			image_processor(images=UpperCamelCase__     ,				return_tensors="""pt"""	).to(UpperCamelCase__	)

														# forward pass
														with torch.no_grad():
																					lowerCamelCase__   :      List[str]			    =			model(**UpperCamelCase__	)

														# verify the logits
														lowerCamelCase__   :      int			    =			torch.Size((1, 1_000)	)
														self.assertEqual(outputs.logits.shape     ,				UpperCamelCase__	)

														lowerCamelCase__   :      List[Any]			    =			torch.tensor([0.0_001, 0.0_002, 0.0_002]	).to(UpperCamelCase__	)
														self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,				UpperCamelCase__     ,				atol=1e-4	)	)









'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_A      :     Tuple       	={
    '''configuration_imagegpt''': ['''IMAGEGPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ImageGPTConfig''', '''ImageGPTOnnxConfig''']
}

try:
			if not is_vision_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A      :     Dict       	=['''ImageGPTFeatureExtractor''']
			_A      :     Union[str, Any]       	=['''ImageGPTImageProcessor''']

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A      :     str       	=[
			    '''IMAGEGPT_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''ImageGPTForCausalImageModeling''',
			    '''ImageGPTForImageClassification''',
			    '''ImageGPTModel''',
			    '''ImageGPTPreTrainedModel''',
			    '''load_tf_weights_in_imagegpt''',
			]


if TYPE_CHECKING:
			from .configuration_imagegpt import IMAGEGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ImageGPTConfig, ImageGPTOnnxConfig

			try:
						if not is_vision_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .feature_extraction_imagegpt import ImageGPTFeatureExtractor
						from .image_processing_imagegpt import ImageGPTImageProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_imagegpt import (
						    IMAGEGPT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    ImageGPTForCausalImageModeling,
						    ImageGPTForImageClassification,
						    ImageGPTModel,
						    ImageGPTPreTrainedModel,
						    load_tf_weights_in_imagegpt,
						)

else:
			import sys

			_A      :     str       	=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

