






"""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 	__UpperCamelCase    :
					def __init__(       self       :  Optional[int]    ,   UpperCAmelCase       :  List[Any]    ,   UpperCAmelCase       :  Union[str, Any]=13    ,   UpperCAmelCase       :  Any=32    ,   UpperCAmelCase       :  Dict=3    ,   UpperCAmelCase       :  Any=[3, 3, 5]    ,   UpperCAmelCase       :  int=[32, 16, 24]    ,   UpperCAmelCase       :  Dict=[16, 24, 40]    ,   UpperCAmelCase       :  Optional[int]=[1, 1, 2]    ,   UpperCAmelCase       :  Union[str, Any]=[1, 1, 2]    ,   UpperCAmelCase       :  Any=[1, 6, 6]    ,   UpperCAmelCase       :  int=True    ,   UpperCAmelCase       :  int=True    ,   UpperCAmelCase       :  Dict=37    ,   UpperCAmelCase       :  Optional[int]="gelu"    ,   UpperCAmelCase       :  Tuple=10    ,   )  ->					Union[str, Any]:
											lowerCAmelCase      :int				  = parent
											lowerCAmelCase      :int				  = batch_size
											lowerCAmelCase      :str				  = image_size
											lowerCAmelCase      :Optional[int]				  = num_channels
											lowerCAmelCase      :Optional[int]				  = kernel_sizes
											lowerCAmelCase      :str				  = in_channels
											lowerCAmelCase      :Tuple				  = out_channels
											lowerCAmelCase      :Dict				  = strides
											lowerCAmelCase      :Optional[Any]				  = num_block_repeats
											lowerCAmelCase      :Optional[int]				  = expand_ratios
											lowerCAmelCase      :Tuple				  = is_training
											lowerCAmelCase      :Dict				  = hidden_act
											lowerCAmelCase      :Dict				  = num_labels
											lowerCAmelCase      :Tuple				  = use_labels
					def 					UpperCAmelCase__   (       self       :  Union[str, Any]      )  ->					Dict:
											lowerCAmelCase      :Union[str, Any]				  = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

											lowerCAmelCase      :Optional[int]				  = None
											if self.use_labels:
																	lowerCAmelCase      :Dict				  = ids_tensor([self.batch_size]    ,   self.num_labels      )

											lowerCAmelCase      :Any				  = self.get_config()
											return config, pixel_values, labels
					def 					UpperCAmelCase__   (       self       :  List[str]      )  ->					Optional[int]:
											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 					UpperCAmelCase__   (       self       :  Union[str, Any]    ,   UpperCAmelCase       :  Optional[Any]    ,   UpperCAmelCase       :  Dict    ,   UpperCAmelCase       :  Union[str, Any]      )  ->					Union[str, Any]:
											lowerCAmelCase      :Tuple				  = EfficientNetModel(config=UpperCAmelCase      )
											model.to(UpperCAmelCase      )
											model.eval()
											lowerCAmelCase      :Optional[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 					UpperCAmelCase__   (       self       :  Optional[Any]    ,   UpperCAmelCase       :  str    ,   UpperCAmelCase       :  Tuple    ,   UpperCAmelCase       :  Dict      )  ->					str:
											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 					UpperCAmelCase__   (       self       :  List[Any]      )  ->					List[str]:
											lowerCAmelCase      :List[Any]				  = self.prepare_config_and_inputs()
											lowerCAmelCase  ,   lowerCAmelCase  ,   lowerCAmelCase      :str				  = config_and_inputs
											lowerCAmelCase      :List[Any]				  = {'pixel_values': pixel_values}
											return config, inputs_dict




@require_torch
class 	__UpperCamelCase    (    UpperCamelCase      ,    UpperCamelCase      ,    unittest.TestCase	):
					lowercase_						:							str			=     (EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
					lowercase_						:							Any			=     (
					    {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
					    if is_torch_available()
					    else {}
					)

					lowercase_						:							Tuple			=     False
					lowercase_						:							int			=     False
					lowercase_						:							List[Any]			=     False
					lowercase_						:							Tuple			=     False
					lowercase_						:							Tuple			=     False
					def 					UpperCAmelCase__   (       self       :  Dict      )  ->					str:
											lowerCAmelCase      :List[str]				  = EfficientNetModelTester(self      )
											lowerCAmelCase      :int				  = ConfigTester(
											    self    ,   config_class=UpperCAmelCase    ,   has_text_modality=UpperCAmelCase    ,   hidden_size=37      )
					def 					UpperCAmelCase__   (       self       :  Optional[Any]      )  ->					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 					UpperCAmelCase__   (       self       :  int      )  ->					Any:
											return
					@unittest.skip(reason='EfficientNet does not use inputs_embeds'      )
					def 					UpperCAmelCase__   (       self       :  Optional[int]      )  ->					Any:
											pass
					@unittest.skip(reason='EfficientNet does not support input and output embeddings'      )
					def 					UpperCAmelCase__   (       self       :  List[Any]      )  ->					Any:
											pass
					@unittest.skip(reason='EfficientNet does not use feedforward chunking'      )
					def 					UpperCAmelCase__   (       self       :  Union[str, Any]      )  ->					Any:
											pass
					def 					UpperCAmelCase__   (       self       :  str      )  ->					List[Any]:
											lowerCAmelCase  ,   lowerCAmelCase      :Dict				  = self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	lowerCAmelCase      :Optional[Any]				  = model_class(UpperCAmelCase      )
																	lowerCAmelCase      :Union[str, Any]				  = inspect.signature(model.forward      )
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	lowerCAmelCase      :List[Any]				  = [*signature.parameters.keys()]

																	lowerCAmelCase      :List[Any]				  = ['pixel_values']
																	self.assertListEqual(arg_names[:1]    ,   UpperCAmelCase      )
					def 					UpperCAmelCase__   (       self       :  str      )  ->					List[Any]:
											lowerCAmelCase      :Union[str, Any]				  = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCAmelCase      )
					def 					UpperCAmelCase__   (       self       :  Any      )  ->					Dict:
											def check_hidden_states_output(UpperCAmelCase       :  Optional[Any]    ,   UpperCAmelCase       :  List[Any]    ,   UpperCAmelCase       :  Any      ):
																	lowerCAmelCase      :Optional[int]				  = model_class(UpperCAmelCase      )
																	model.to(UpperCAmelCase      )
																	model.eval()

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

																	lowerCAmelCase      :List[Any]				  = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
																	lowerCAmelCase      :str				  = 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      :Dict				  = self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	lowerCAmelCase      :Optional[Any]				  = True
																	check_hidden_states_output(UpperCAmelCase    ,   UpperCAmelCase    ,   UpperCAmelCase      )

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

																	check_hidden_states_output(UpperCAmelCase    ,   UpperCAmelCase    ,   UpperCAmelCase      )
					def 					UpperCAmelCase__   (       self       :  Optional[int]      )  ->					Optional[Any]:
											lowerCAmelCase      :List[Any]				  = 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 					UpperCAmelCase__   (       self       :  str      )  ->					List[str]:
											pass
					@slow
					def 					UpperCAmelCase__   (       self       :  List[str]      )  ->					Optional[int]:
											for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowerCAmelCase      :List[Any]				  = EfficientNetModel.from_pretrained(UpperCAmelCase      )
																	self.assertIsNotNone(UpperCAmelCase      )
					@is_pipeline_test
					@require_vision
					@slow
					def 					UpperCAmelCase__   (       self       :  Any      )  ->					Union[str, Any]:
											super().test_pipeline_image_classification()


def 					UpperCAmelCase			(				):





						'''simple docstring'''






						lowerCAmelCase      :List[str]				  = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
						return image







@require_torch
@require_vision
class 	__UpperCamelCase    (    unittest.TestCase	):
					@cached_property
					def 					UpperCAmelCase__   (       self       :  Union[str, Any]      )  ->					Dict:
											return AutoImageProcessor.from_pretrained('google/efficientnet-b7'      ) if is_vision_available() else None
					@slow
					def 					UpperCAmelCase__   (       self       :  str      )  ->					Dict:
											lowerCAmelCase      :Optional[Any]				  = EfficientNetForImageClassification.from_pretrained('google/efficientnet-b7'      ).to(UpperCAmelCase      )

											lowerCAmelCase      :Optional[Any]				  = self.default_image_processor
											lowerCAmelCase      :str				  = prepare_img()
											lowerCAmelCase      :List[Any]				  = image_processor(images=UpperCAmelCase    ,   return_tensors='pt'      ).to(UpperCAmelCase      )

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

											# verify the logits
											lowerCAmelCase      :Tuple				  = torch.Size((1, 1000)      )
											self.assertEqual(outputs.logits.shape    ,   UpperCAmelCase      )

											lowerCAmelCase      :Any				  = torch.tensor([0.0_0_0_1, 0.0_0_0_2, 0.0_0_0_2]      ).to(UpperCAmelCase      )
											self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,   UpperCAmelCase    ,   atol=1e-4      )      )







"""simple docstring"""


from __future__ import annotations


def 					UpperCAmelCase			(				a__   ):





						'''simple docstring'''






						lowerCAmelCase      :Union[str, Any]				  = [0] * len(a__   )
						for i in range(1    ,			len(a__   )   ):
												lowerCAmelCase      :Optional[int]				  = duration_times[i - 1] + waiting_times[i - 1]
						return waiting_times


def 					UpperCAmelCase			(				a__    ,			a__   ):





						'''simple docstring'''






						return [
						    duration_time + waiting_times[i]
						    for i, duration_time in enumerate(a__   )
						]


def 					UpperCAmelCase			(				a__   ):





						'''simple docstring'''






						return sum(a__   ) / len(a__   )


def 					UpperCAmelCase			(				a__   ):





						'''simple docstring'''






						return sum(a__   ) / len(a__   )


if __name__ == "__main__":
		# process id's
		__SCREAMING_SNAKE_CASE      =      [1, 2, 3]

		# ensure that we actually have processes
		if len(processes) == 0:
				print('Zero amount of processes')
				raise SystemExit(0)

		# duration time of all processes
		__SCREAMING_SNAKE_CASE      =      [19, 8, 9]

		# ensure we can match each id to a duration time
		if len(duration_times) != len(processes):
				print('Unable to match all id\'s with their duration time')
				raise SystemExit(0)

		# get the waiting times and the turnaround times
		__SCREAMING_SNAKE_CASE      =      calculate_waiting_times(duration_times)
		__SCREAMING_SNAKE_CASE      =      calculate_turnaround_times(duration_times, waiting_times)

		# get the average times
		__SCREAMING_SNAKE_CASE      =      calculate_average_waiting_time(waiting_times)
		__SCREAMING_SNAKE_CASE      =      calculate_average_turnaround_time(turnaround_times)

		# print all the results
		print('Process ID\tDuration Time\tWaiting Time\tTurnaround Time')
		for i, process in enumerate(processes):
				print(
				    F"""{process}\t\t{duration_times[i]}\t\t{waiting_times[i]}\t\t"""
				    F"""{turnaround_times[i]}"""
				)
		print(F"""Average waiting time = {average_waiting_time}""")
		print(F"""Average turn around time = {average_turnaround_time}""")