




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





				'''simple docstring'''



				def __init__(			self							,			UpperCAmelCase_							,			UpperCAmelCase_=13							,			UpperCAmelCase_=32							,			UpperCAmelCase_=3							,			UpperCAmelCase_=[3, 3, 5]							,			UpperCAmelCase_=[32, 16, 24]							,			UpperCAmelCase_=[16, 24, 40]							,			UpperCAmelCase_=[1, 1, 2]							,			UpperCAmelCase_=[1, 1, 2]							,			UpperCAmelCase_=[1, 6, 6]							,			UpperCAmelCase_=True							,			UpperCAmelCase_=True							,			UpperCAmelCase_=37							,			UpperCAmelCase_="gelu"							,			UpperCAmelCase_=10							,			):
											lowerCAmelCase =		parent
											lowerCAmelCase =		batch_size
											lowerCAmelCase =		image_size
											lowerCAmelCase =		num_channels
											lowerCAmelCase =		kernel_sizes
											lowerCAmelCase =		in_channels
											lowerCAmelCase =		out_channels
											lowerCAmelCase =		strides
											lowerCAmelCase =		num_block_repeats
											lowerCAmelCase =		expand_ratios
											lowerCAmelCase =		is_training
											lowerCAmelCase =		hidden_act
											lowerCAmelCase =		num_labels
											lowerCAmelCase =		use_labels



				def    __snake_case		(			self    ):
											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    __snake_case		(			self    ):
											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    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											lowerCAmelCase =		EfficientNetModel(config=UpperCAmelCase_    )
											model.to(UpperCAmelCase_    )
											model.eval()
											lowerCAmelCase =		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    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											lowerCAmelCase =		EfficientNetForImageClassification(UpperCAmelCase_    )
											model.to(UpperCAmelCase_    )
											model.eval()
											lowerCAmelCase =		model(UpperCAmelCase_							,			labels=UpperCAmelCase_    )
											self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)    )






				def    __snake_case		(			self    ):
											lowerCAmelCase =		self.prepare_config_and_inputs()
											lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		config_and_inputs
											lowerCAmelCase =		{'''pixel_values''': pixel_values}
											return config, inputs_dict




@require_torch
class 				__UpperCamelCase      (			__UpperCAmelCase  ,       __UpperCAmelCase  ,       unittest.TestCase	):





				'''simple docstring'''



				__a				:			Optional[Any]					    =(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
				__a				:			Tuple					    =(
				    {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
				    if is_torch_available()
				    else {}
				)

				__a				:			Dict					    =False
				__a				:			Union[str, Any]					    =False
				__a				:			Dict					    =False
				__a				:			List[Any]					    =False
				__a				:			Tuple					    =False



				def    __snake_case		(			self    ):
											lowerCAmelCase =		EfficientNetModelTester(self    )
											lowerCAmelCase =		ConfigTester(
											    self							,			config_class=UpperCAmelCase_							,			has_text_modality=UpperCAmelCase_							,			hidden_size=37    )



				def    __snake_case		(			self    ):
											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    __snake_case		(			self    ):
											return



				@unittest.skip(reason='''EfficientNet does not use inputs_embeds'''    )
				def    __snake_case		(			self    ):
											pass



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



				@unittest.skip(reason='''EfficientNet does not use feedforward chunking'''    )
				def    __snake_case		(			self    ):
											pass



				def    __snake_case		(			self    ):
											lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs_for_common()

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

																		lowerCAmelCase =		['''pixel_values''']
																		self.assertListEqual(arg_names[:1]							,			UpperCAmelCase_    )



				def    __snake_case		(			self    ):
											lowerCAmelCase =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCAmelCase_    )



				def    __snake_case		(			self    ):
											def check_hidden_states_output(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
																		lowerCAmelCase =		model_class(UpperCAmelCase_    )
																		model.to(UpperCAmelCase_    )
																		model.eval()

																		with torch.no_grad():
																									lowerCAmelCase =		model(**self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )    )

																		lowerCAmelCase =		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
																		lowerCAmelCase =		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 =		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		lowerCAmelCase =		True
																		check_hidden_states_output(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )

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

																		check_hidden_states_output(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )



				def    __snake_case		(			self    ):
											lowerCAmelCase =		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    __snake_case		(			self    ):
											pass



				@slow
				def    __snake_case		(			self    ):
											for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowerCAmelCase =		EfficientNetModel.from_pretrained(UpperCAmelCase_    )
																		self.assertIsNotNone(UpperCAmelCase_    )






				@is_pipeline_test
				@require_vision
				@slow
				def    __snake_case		(			self    ):
											super().test_pipeline_image_classification()




def    UpperCAmelCase							(  ):
							lowerCAmelCase =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
							return image


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





				'''simple docstring'''



				@cached_property
				def    __snake_case		(			self    ):
											return AutoImageProcessor.from_pretrained('''google/efficientnet-b7'''    ) if is_vision_available() else None






				@slow
				def    __snake_case		(			self    ):
											lowerCAmelCase =		EfficientNetForImageClassification.from_pretrained('''google/efficientnet-b7'''    ).to(UpperCAmelCase_    )

											lowerCAmelCase =		self.default_image_processor
											lowerCAmelCase =		prepare_img()
											lowerCAmelCase =		image_processor(images=UpperCAmelCase_							,			return_tensors='''pt'''    ).to(UpperCAmelCase_    )

											# forward pass
											with torch.no_grad():
																		lowerCAmelCase =		model(**UpperCAmelCase_    )

											# verify the logits
											lowerCAmelCase =		torch.Size((1, 10_00)    )
											self.assertEqual(outputs.logits.shape							,			UpperCAmelCase_    )

											lowerCAmelCase =		torch.tensor([0.0001, 0.0002, 0.0002]    ).to(UpperCAmelCase_    )
											self.assertTrue(torch.allclose(outputs.logits[0, :3]							,			UpperCAmelCase_							,			atol=1E-4    )    )









from itertools import permutations



def    UpperCAmelCase							(  _snake_case = 5				):
							if gon_side < 3 or gon_side > 5:
														raise ValueError('''gon_side must be in the range [3, 5]'''				)

							# Since it's 16, we know 10 is on the outer ring
							# Put the big numbers at the end so that they are never the first number
							lowerCAmelCase =		list(range(gon_side + 1    ,			0    ,			-1				)				)
							lowerCAmelCase =		list(range(gon_side + 2    ,			gon_side * 2 + 1				)				)

							for perm in permutations(small_numbers + big_numbers				):
														lowerCAmelCase =		generate_gon_ring(_snake_case    ,			list(_snake_case				)				)
														if is_magic_gon(_snake_case				):
																					return int(''''''.join(str(_snake_case				) for n in numbers				)				)

							lowerCAmelCase =		F"""Magic {gon_side}-gon ring is impossible"""
							raise ValueError(_snake_case				)



def    UpperCAmelCase							(  _snake_case    ,			_snake_case				):
							lowerCAmelCase =		[0] * (gon_side * 3)
							lowerCAmelCase =		perm[0:3]
							perm.append(perm[1]				)

							lowerCAmelCase =		1 if gon_side < 5 else 2

							for i in range(1    ,			len(_snake_case				) // 3 + magic_number				):
														lowerCAmelCase =		perm[2 * i + 1]
														lowerCAmelCase =		result[3 * i - 1]
														lowerCAmelCase =		perm[2 * i + 2]

							return result



def    UpperCAmelCase							(  _snake_case				):
							if len(_snake_case				) % 3 != 0:
														raise ValueError('''a gon ring should have a length that is a multiple of 3'''				)

							if min(numbers[::3]				) != numbers[0]:
														return False

							lowerCAmelCase =		sum(numbers[:3]				)

							return all(sum(numbers[i : i + 3]				) == total for i in range(3    ,			len(_snake_case				)    ,			3				)				)


if __name__ == "__main__":
		print(solution())



