




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	,				lowerCamelCase	,				lowerCamelCase=13	,				lowerCamelCase=32	,				lowerCamelCase=3	,				lowerCamelCase=[3, 3, 5]	,				lowerCamelCase=[32, 16, 24]	,				lowerCamelCase=[16, 24, 40]	,				lowerCamelCase=[1, 1, 2]	,				lowerCamelCase=[1, 1, 2]	,				lowerCamelCase=[1, 6, 6]	,				lowerCamelCase=True	,				lowerCamelCase=True	,				lowerCamelCase=37	,				lowerCamelCase="gelu"	,				lowerCamelCase=10	,				) ->      List[Any]:





								"""simple docstring"""


								lowercase__  :	int        =							parent
								lowercase__  :	Optional[int]        =							batch_size
								lowercase__  :	Any        =							image_size
								lowercase__  :	Tuple        =							num_channels
								lowercase__  :	Dict        =							kernel_sizes
								lowercase__  :	List[str]        =							in_channels
								lowercase__  :	Union[str, Any]        =							out_channels
								lowercase__  :	Dict        =							strides
								lowercase__  :	Optional[int]        =							num_block_repeats
								lowercase__  :	Optional[Any]        =							expand_ratios
								lowercase__  :	List[Any]        =							is_training
								lowercase__  :	Optional[int]        =							hidden_act
								lowercase__  :	Any        =							num_labels
								lowercase__  :	str        =							use_labels

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





								"""simple docstring"""


								lowercase__  :	List[Any]        =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

								lowercase__  :	List[str]        =							None
								if self.use_labels:
															lowercase__  :	Tuple        =							ids_tensor([self.batch_size]	,				self.num_labels    )

								lowercase__  :	List[str]        =							self.get_config()
								return config, pixel_values, labels

	def    __a     (					self    ) ->      str:





								"""simple docstring"""


								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    __a     (					self	,				lowerCamelCase	,				lowerCamelCase	,				lowerCamelCase    ) ->      Tuple:





								"""simple docstring"""


								lowercase__  :	Any        =							EfficientNetModel(config=lowerCamelCase    )
								model.to(lowerCamelCase    )
								model.eval()
								lowercase__  :	Union[str, Any]        =							model(lowerCamelCase    )
								# 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    __a     (					self	,				lowerCamelCase	,				lowerCamelCase	,				lowerCamelCase    ) ->      Union[str, Any]:





								"""simple docstring"""


								lowercase__  :	Dict        =							EfficientNetForImageClassification(lowerCamelCase    )
								model.to(lowerCamelCase    )
								model.eval()
								lowercase__  :	Union[str, Any]        =							model(lowerCamelCase	,				labels=lowerCamelCase    )
								self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)    )







	def    __a     (					self    ) ->      Optional[Any]:





								"""simple docstring"""


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







@require_torch
class 				UpperCAmelCase(					snake_case_   ,     snake_case_   ,     unittest.TestCase					):



	"""simple docstring"""






	a     : List[Any]	  =					(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
	a     : int	  =					(
	    {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
	    if is_torch_available()
	    else {}
	)

	a     : str	  =					False
	a     : Optional[Any]	  =					False
	a     : Dict	  =					False
	a     : int	  =					False
	a     : List[str]	  =					False

	def    __a     (					self    ) ->      Optional[Any]:





								"""simple docstring"""


								lowercase__  :	int        =							EfficientNetModelTester(self    )
								lowercase__  :	Tuple        =							ConfigTester(
								    self	,				config_class=lowerCamelCase	,				has_text_modality=lowerCamelCase	,				hidden_size=37    )

	def    __a     (					self    ) ->      int:





								"""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    __a     (					self    ) ->      int:





								"""simple docstring"""


								return

	@unittest.skip(reason="EfficientNet does not use inputs_embeds"    )
	def    __a     (					self    ) ->      Union[str, Any]:





								"""simple docstring"""


								pass

	@unittest.skip(reason="EfficientNet does not support input and output embeddings"    )
	def    __a     (					self    ) ->      Union[str, Any]:





								"""simple docstring"""


								pass

	@unittest.skip(reason="EfficientNet does not use feedforward chunking"    )
	def    __a     (					self    ) ->      Optional[int]:





								"""simple docstring"""


								pass

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





								"""simple docstring"""


								lowercase__  :	Dict        =							self.model_tester.prepare_config_and_inputs_for_common()

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

															lowercase__  :	Union[str, Any]        =							["pixel_values"]
															self.assertListEqual(arg_names[:1]	,				lowerCamelCase    )

	def    __a     (					self    ) ->      str:





								"""simple docstring"""


								lowercase__  :	List[str]        =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase    )

	def    __a     (					self    ) ->      Tuple:





								"""simple docstring"""


								def check_hidden_states_output(lowerCamelCase	,				lowerCamelCase	,				lowerCamelCase    ):
															lowercase__  :	Optional[Any]        =							model_class(lowerCamelCase    )
															model.to(lowerCamelCase    )
															model.eval()

															with torch.no_grad():
																						lowercase__  :	Dict        =							model(**self._prepare_for_class(lowerCamelCase	,				lowerCamelCase    )    )

															lowercase__  :	Tuple        =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
															lowercase__  :	Tuple        =							sum(config.num_block_repeats    ) * 4
															self.assertEqual(len(lowerCamelCase    )	,				lowerCamelCase    )

															# 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]	,				)

								lowercase__  :	Union[str, Any]        =							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															lowercase__  :	str        =							True
															check_hidden_states_output(lowerCamelCase	,				lowerCamelCase	,				lowerCamelCase    )

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

															check_hidden_states_output(lowerCamelCase	,				lowerCamelCase	,				lowerCamelCase    )

	def    __a     (					self    ) ->      Tuple:





								"""simple docstring"""


								lowercase__  :	Dict        =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowerCamelCase    )

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





								"""simple docstring"""


								pass

	@slow
	def    __a     (					self    ) ->      Any:





								"""simple docstring"""


								for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowercase__  :	Union[str, Any]        =							EfficientNetModel.from_pretrained(lowerCamelCase    )
															self.assertIsNotNone(lowerCamelCase    )







	@is_pipeline_test
	@require_vision
	@slow
	def    __a     (					self    ) ->      Optional[Any]:





								"""simple docstring"""


								super().test_pipeline_image_classification()

def  snake_case_					(		)      ->			Union[str, Any]:
							lowercase__  :	Tuple        =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
							return image







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



	"""simple docstring"""






	@cached_property
	def    __a     (					self    ) ->      str:





								"""simple docstring"""


								return AutoImageProcessor.from_pretrained("google/efficientnet-b7"    ) if is_vision_available() else None







	@slow
	def    __a     (					self    ) ->      int:





								"""simple docstring"""


								lowercase__  :	Optional[Any]        =							EfficientNetForImageClassification.from_pretrained("google/efficientnet-b7"    ).to(lowerCamelCase    )

								lowercase__  :	List[Any]        =							self.default_image_processor
								lowercase__  :	int        =							prepare_img()
								lowercase__  :	Dict        =							image_processor(images=lowerCamelCase	,				return_tensors="pt"    ).to(lowerCamelCase    )

								# forward pass
								with torch.no_grad():
															lowercase__  :	Any        =							model(**lowerCamelCase    )

								# verify the logits
								lowercase__  :	int        =							torch.Size((1, 1000)    )
								self.assertEqual(outputs.logits.shape	,				lowerCamelCase    )

								lowercase__  :	str        =							torch.tensor([0.00_01, 0.00_02, 0.00_02]    ).to(lowerCamelCase    )
								self.assertTrue(torch.allclose(outputs.logits[0, :3]	,				lowerCamelCase	,				atol=1E-4    )    )





from __future__ import annotations

import math
import random
def  snake_case_					(		SCREAMING_SNAKE_CASE_    ,SCREAMING_SNAKE_CASE_    ,SCREAMING_SNAKE_CASE_    )      ->			list[int]:
							lowercase__  :	List[Any]        =							d * e - 1
							lowercase__  :	Any        =							0
							lowercase__  :	Dict        =							0
							while p == 0:
														lowercase__  :	Dict        =							random.randint(2    ,n - 1    )
														lowercase__  :	int        =							k
														while True:
																					if t % 2 == 0:
																												lowercase__  :	str        =							t // 2
																												lowercase__  :	int        =							(g**t) % n
																												lowercase__  :	List[Any]        =							math.gcd(x - 1    ,SCREAMING_SNAKE_CASE_    )
																												if x > 1 and y > 1:
																																			lowercase__  :	Any        =							y
																																			lowercase__  :	Optional[int]        =							n // y
																																			break  # find the correct factors
																					else:
																												break  # t is not divisible by 2, break and choose another g
							return sorted([p, q]    )


if __name__ == "__main__":
						import doctest

						doctest.testmod()