code: 
  
  
  '''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 						__lowerCAmelCase		:
  
  
  
  
  
        '''simple docstring'''
  
  
  
  
  
  
        def __init__(self					:					Optional[Any]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Optional[Any]=13     ,		UpperCamelCase					:					List[Any]=32     ,		UpperCamelCase					:					Optional[Any]=3     ,		UpperCamelCase					:					Union[str, Any]=[3, 3, 5]     ,		UpperCamelCase					:					List[str]=[32, 16, 24]     ,		UpperCamelCase					:					Optional[Any]=[16, 24, 40]     ,		UpperCamelCase					:					Union[str, Any]=[1, 1, 2]     ,		UpperCamelCase					:					Any=[1, 1, 2]     ,		UpperCamelCase					:					List[str]=[1, 6, 6]     ,		UpperCamelCase					:					int=True     ,		UpperCamelCase					:					Optional[int]=True     ,		UpperCamelCase					:					List[str]=37     ,		UpperCamelCase					:					Any="gelu"     ,		UpperCamelCase					:					Optional[Any]=10     ,		):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				parent
               lowercase__       =				batch_size
               lowercase__       =				image_size
               lowercase__       =				num_channels
               lowercase__       =				kernel_sizes
               lowercase__       =				in_channels
               lowercase__       =				out_channels
               lowercase__       =				strides
               lowercase__       =				num_block_repeats
               lowercase__       =				expand_ratios
               lowercase__       =				is_training
               lowercase__       =				hidden_act
               lowercase__       =				num_labels
               lowercase__       =				use_labels
  
  
  
        def    UpperCamelCase__	(self					:					Dict				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)
  
               lowercase__       =				None
               if self.use_labels:
                      lowercase__       =				ids_tensor([self.batch_size]     ,		self.num_labels				)
  
               lowercase__       =				self.get_config()
               return config, pixel_values, labels
  
  
  
        def    UpperCamelCase__	(self					:					Tuple				):
  
               '''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    UpperCamelCase__	(self					:					Any     ,		UpperCamelCase					:					Union[str, Any]     ,		UpperCamelCase					:					Any     ,		UpperCamelCase					:					List[str]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				EfficientNetModel(config=lowercase_				)
               model.to(lowercase_				)
               model.eval()
               lowercase__       =				model(lowercase_				)
               # 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					:					List[Any]     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					Dict				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				EfficientNetForImageClassification(lowercase_				)
               model.to(lowercase_				)
               model.eval()
               lowercase__       =				model(lowercase_     ,		labels=lowercase_				)
               self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels)				)
  
  
  
  
  
  
        def    UpperCamelCase__	(self					:					int				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				self.prepare_config_and_inputs()
               lowercase__       =				config_and_inputs
               lowercase__       =				{'''pixel_values''': pixel_values}
               return config, inputs_dict
  
  
  
  
  
  
  @require_torch
  class 						__lowerCAmelCase		(__UpperCAmelCase						,  __UpperCAmelCase						,  unittest.TestCase  ):
  
  
  
  
  
        '''simple docstring'''
  
  
  
  
  
  
        lowerCAmelCase__      :  List[Any]						      =							(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
        lowerCAmelCase__      :  Dict						      =							(
            {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
            if is_torch_available()
            else {}
        )
  
        lowerCAmelCase__      :  Tuple						      =							False
        lowerCAmelCase__      :  str						      =							False
        lowerCAmelCase__      :  List[Any]						      =							False
        lowerCAmelCase__      :  int						      =							False
        lowerCAmelCase__      :  int						      =							False
  
  
  
        def    UpperCamelCase__	(self					:					Dict				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				EfficientNetModelTester(self				)
               lowercase__       =				ConfigTester(
                   self     ,		config_class=lowercase_     ,		has_text_modality=lowercase_     ,		hidden_size=37				)
  
  
  
        def    UpperCamelCase__	(self					:					Any				):
  
               '''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    UpperCamelCase__	(self					:					Any				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               return
  
  
  
        @unittest.skip(reason='''EfficientNet does not use inputs_embeds'''				)
        def    UpperCamelCase__	(self					:					List[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               pass
  
  
  
        @unittest.skip(reason='''EfficientNet does not support input and output embeddings'''				)
        def    UpperCamelCase__	(self					:					List[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               pass
  
  
  
        @unittest.skip(reason='''EfficientNet does not use feedforward chunking'''				)
        def    UpperCamelCase__	(self					:					List[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               pass
  
  
  
        def    UpperCamelCase__	(self					:					Dict				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				self.model_tester.prepare_config_and_inputs_for_common()
  
               for model_class in self.all_model_classes:
                      lowercase__       =				model_class(lowercase_				)
                      lowercase__       =				inspect.signature(model.forward				)
                      # signature.parameters is an OrderedDict => so arg_names order is deterministic
                      lowercase__       =				[*signature.parameters.keys()]
  
                      lowercase__       =				['''pixel_values''']
                      self.assertListEqual(arg_names[:1]     ,		lowercase_				)
  
  
  
        def    UpperCamelCase__	(self					:					Any				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				self.model_tester.prepare_config_and_inputs()
               self.model_tester.create_and_check_model(*lowercase_				)
  
  
  
        def    UpperCamelCase__	(self					:					Optional[int]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               def check_hidden_states_output(UpperCamelCase					:					int     ,		UpperCamelCase					:					Optional[Any]     ,		UpperCamelCase					:					List[str]				):
                      lowercase__       =				model_class(lowercase_				)
                      model.to(lowercase_				)
                      model.eval()
  
                      with torch.no_grad():
                             lowercase__       =				model(**self._prepare_for_class(lowercase_     ,		lowercase_				)				)
  
                      lowercase__       =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
                      lowercase__       =				sum(config.num_block_repeats				) * 4
                      self.assertEqual(len(lowercase_				)     ,		lowercase_				)
  
                      # 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__       =				self.model_tester.prepare_config_and_inputs_for_common()
  
               for model_class in self.all_model_classes:
                      lowercase__       =				True
                      check_hidden_states_output(lowercase_     ,		lowercase_     ,		lowercase_				)
  
                      # check that output_hidden_states also work using config
                      del inputs_dict["output_hidden_states"]
                      lowercase__       =				True
  
                      check_hidden_states_output(lowercase_     ,		lowercase_     ,		lowercase_				)
  
  
  
        def    UpperCamelCase__	(self					:					Tuple				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				self.model_tester.prepare_config_and_inputs()
               self.model_tester.create_and_check_for_image_classification(*lowercase_				)
  
  
  
        @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''				)
        def    UpperCamelCase__	(self					:					Dict				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               pass
  
  
  
        @slow
        def    UpperCamelCase__	(self					:					Optional[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                      lowercase__       =				EfficientNetModel.from_pretrained(lowercase_				)
                      self.assertIsNotNone(lowercase_				)
  
  
  
  
  
  
        @is_pipeline_test
        @require_vision
        @slow
        def    UpperCamelCase__	(self					:					List[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               super().test_pipeline_image_classification()
  
  
  
  
  
  
  def _SCREAMING_SNAKE_CASE   () -> Optional[Any]:
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         lowercase__       =				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
         return image
  
  @require_torch
  @require_vision
  class 						__lowerCAmelCase		(unittest.TestCase  ):
  
  
  
  
  
        '''simple docstring'''
  
  
  
  
  
  
        @cached_property
        def    UpperCamelCase__	(self					:					Optional[int]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               return AutoImageProcessor.from_pretrained('''google/efficientnet-b7'''				) if is_vision_available() else None
  
  
  
  
  
  
        @slow
        def    UpperCamelCase__	(self					:					List[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               lowercase__       =				EfficientNetForImageClassification.from_pretrained('''google/efficientnet-b7'''				).to(lowercase_				)
  
               lowercase__       =				self.default_image_processor
               lowercase__       =				prepare_img()
               lowercase__       =				image_processor(images=lowercase_     ,		return_tensors='''pt'''				).to(lowercase_				)
  
               # forward pass
               with torch.no_grad():
                      lowercase__       =				model(**lowercase_				)
  
               # verify the logits
               lowercase__       =				torch.Size((1, 1000)				)
               self.assertEqual(outputs.logits.shape     ,		lowercase_				)
  
               lowercase__       =				torch.tensor([0.00_01, 0.00_02, 0.00_02]				).to(lowercase_				)
               self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,		lowercase_     ,		atol=1E-4				)				)
  
  
  
  
  
style_context: 
  
  
  '''simple docstring'''
  
  import torch
  
  from accelerate import Accelerator, DistributedType
  class 						__lowerCAmelCase		:
  
  
  
  
  
        '''simple docstring'''
  
  
  
  
  
  
        def __enter__(self					:					List[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               if self.enabled:
                      lowercase__       =				self.model.no_sync()
                      self.model_sync_obj.__enter__()
  
               return self
  
  
  
        def __exit__(self					:					List[str]     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					Optional[int]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               if self.enabled:
                      # Average all models on exit
                      self._sync_and_avg_model_params()
                      self.model_sync_obj.__exit__(UpperCamelCase     ,		UpperCamelCase     ,		UpperCamelCase				)
  
  
  
        def __init__(self					:					Dict     ,		UpperCamelCase					:					Accelerator     ,		UpperCamelCase					:					torch.nn.Module     ,		UpperCamelCase					:					int     ,		UpperCamelCase					:					bool = True				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               if accelerator.distributed_type not in [
                   DistributedType.NO,
                   DistributedType.MULTI_CPU,
                   DistributedType.MULTI_GPU,
               ]:
                      raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'''				)
               lowercase__       =				enabled and accelerator.distributed_type != DistributedType.NO
               lowercase__       =				0
               if self.enabled:
                      lowercase__       =				accelerator
                      lowercase__       =				model
                      lowercase__       =				local_sgd_steps
  
  
  
        def    UpperCamelCase__	(self					:					List[str]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               self.num_steps += 1
               if not self.enabled:
                      return
  
               if self.num_steps % self.local_sgd_steps == 0:
                      self._sync_and_avg_model_params()
  
  
  
  
  
  
        def    UpperCamelCase__	(self					:					Any				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               self.accelerator.wait_for_everyone()
               with self.accelerator.autocast():
                      for param in self.model.parameters():
                             lowercase__       =				self.accelerator.reduce(param.data     ,		reduction='''mean'''				)
  
  
  
  
  