

'''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   _SCREAMING_SNAKE_CASE    :



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



 def  snake_case__  (    self		:    Optional[Any]			):
      __magic_name__						   =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

      __magic_name__						   =				None
      if self.use_labels:
           __magic_name__						   =				ids_tensor([self.batch_size] ,      self.num_labels			)

      __magic_name__						   =				self.get_config()
      return config, pixel_values, labels



 def  snake_case__  (    self		:    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  snake_case__  (    self		:    Union[str, Any] ,      a__		:    Tuple ,      a__		:    int ,      a__		:    Dict			):
      __magic_name__						   =				EfficientNetModel(config=__SCREAMING_SNAKE_CASE			)
      model.to(__SCREAMING_SNAKE_CASE			)
      model.eval()
      __magic_name__						   =				model(__SCREAMING_SNAKE_CASE			)
      # 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		:    Optional[Any] ,      a__		:    Union[str, Any] ,      a__		:    str ,      a__		:    Optional[int]			):
      __magic_name__						   =				EfficientNetForImageClassification(__SCREAMING_SNAKE_CASE			)
      model.to(__SCREAMING_SNAKE_CASE			)
      model.eval()
      __magic_name__						   =				model(__SCREAMING_SNAKE_CASE ,      labels=__SCREAMING_SNAKE_CASE			)
      self.parent.assertEqual(result.logits.shape ,      (self.batch_size, self.num_labels)			)



 def  snake_case__  (    self		:    List[Any]			):
      __magic_name__						   =				self.prepare_config_and_inputs()
      __magic_name__   ,  __magic_name__   ,  __magic_name__						   =				config_and_inputs
      __magic_name__						   =				{'''pixel_values''': pixel_values}
      return config, inputs_dict






@require_torch
class   _SCREAMING_SNAKE_CASE    (    lowerCAmelCase_		,lowerCAmelCase_		,unittest.TestCase	):
 __SCREAMING_SNAKE_CASE     :str			       =     (EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
 __SCREAMING_SNAKE_CASE     :int			       =     (
     {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
     if is_torch_available()
     else {}
 )

 __SCREAMING_SNAKE_CASE     :List[Any]			       =     False
 __SCREAMING_SNAKE_CASE     :Optional[Any]			       =     False
 __SCREAMING_SNAKE_CASE     :Any			       =     False
 __SCREAMING_SNAKE_CASE     :str			       =     False
 __SCREAMING_SNAKE_CASE     :Dict			       =     False



 def  snake_case__  (    self		:    Tuple			):
      __magic_name__						   =				EfficientNetModelTester(self			)
      __magic_name__						   =				ConfigTester(
          self ,      config_class=__SCREAMING_SNAKE_CASE ,      has_text_modality=__SCREAMING_SNAKE_CASE ,      hidden_size=37			)



 def  snake_case__  (    self		:    int			):
      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		:    List[str]			):
      return



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



 @unittest.skip(reason='''EfficientNet does not support input and output embeddings'''			)
 def  snake_case__  (    self		:    Union[str, Any]			):
      pass



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



 def  snake_case__  (    self		:    str			):
      __magic_name__   ,  __magic_name__						   =				self.model_tester.prepare_config_and_inputs_for_common()

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

           __magic_name__						   =				['''pixel_values''']
           self.assertListEqual(arg_names[:1] ,      __SCREAMING_SNAKE_CASE			)



 def  snake_case__  (    self		:    Union[str, Any]			):
      __magic_name__						   =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE			)



 def  snake_case__  (    self		:    Optional[Any]			):

      def check_hidden_states_output(a__		:    Any ,      a__		:    Dict ,      a__		:    List[Any]			):
           __magic_name__						   =				model_class(__SCREAMING_SNAKE_CASE			)
           model.to(__SCREAMING_SNAKE_CASE			)
           model.eval()

           with torch.no_grad():
                __magic_name__						   =				model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE ,      __SCREAMING_SNAKE_CASE			)			)

           __magic_name__						   =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
           __magic_name__						   =				sum(config.num_block_repeats			) * 4
           self.assertEqual(len(__SCREAMING_SNAKE_CASE			) ,      __SCREAMING_SNAKE_CASE			)

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

      __magic_name__   ,  __magic_name__						   =				self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           __magic_name__						   =				True
           check_hidden_states_output(__SCREAMING_SNAKE_CASE ,      __SCREAMING_SNAKE_CASE ,      __SCREAMING_SNAKE_CASE			)

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

           check_hidden_states_output(__SCREAMING_SNAKE_CASE ,      __SCREAMING_SNAKE_CASE ,      __SCREAMING_SNAKE_CASE			)



 def  snake_case__  (    self		:    Any			):
      __magic_name__						   =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE			)



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



 @slow
 def  snake_case__  (    self		:    Dict			):

      for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __magic_name__						   =				EfficientNetModel.from_pretrained(__SCREAMING_SNAKE_CASE			)
           self.assertIsNotNone(__SCREAMING_SNAKE_CASE			)



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




def 						UpperCamelCase       ( )      ->	Dict:






     '''simple docstring'''
     __magic_name__						   =				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
     return image






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



 @cached_property
 def  snake_case__  (    self		:    Optional[Any]			):
      return AutoImageProcessor.from_pretrained('''google/efficientnet-b7'''			) if is_vision_available() else None



 @slow
 def  snake_case__  (    self		:    List[str]			):
      __magic_name__						   =				EfficientNetForImageClassification.from_pretrained('''google/efficientnet-b7'''			).to(__SCREAMING_SNAKE_CASE			)

      __magic_name__						   =				self.default_image_processor
      __magic_name__						   =				prepare_img()
      __magic_name__						   =				image_processor(images=__SCREAMING_SNAKE_CASE ,      return_tensors='''pt'''			).to(__SCREAMING_SNAKE_CASE			)

      # forward pass
      with torch.no_grad():
           __magic_name__						   =				model(**__SCREAMING_SNAKE_CASE			)

      # verify the logits
      __magic_name__						   =				torch.Size((1, 1000)			)
      self.assertEqual(outputs.logits.shape ,      __SCREAMING_SNAKE_CASE			)

      __magic_name__						   =				torch.tensor([0.0_001, 0.0_002, 0.0_002]			).to(__SCREAMING_SNAKE_CASE			)
      self.assertTrue(torch.allclose(outputs.logits[0, :3] ,      __SCREAMING_SNAKE_CASE ,      atol=1E-4			)			)













'''simple docstring'''







import torch
from torch import autograd





class     _UpperCAmelCase							(						autograd.Function   ):
		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				inputs.numel()
					__lowerCAmelCase											=				int(0.005 * nb_elems			) + 1
					if sigmoid:
								__lowerCAmelCase											=				(torch.sigmoid(__SCREAMING_SNAKE_CASE			) > threshold).type(inputs.type()			)
					else:
								__lowerCAmelCase											=				(inputs > threshold).type(inputs.type()			)
					if mask.sum() < nb_min:
								# We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
								__lowerCAmelCase											=				inputs.flatten().kthvalue(max(nb_elems - nb_min,1			)			).values
								__lowerCAmelCase											=				(inputs > k_threshold).type(inputs.type()			)
					return mask







		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					return gradOutput, None, None





class     _UpperCAmelCase							(						autograd.Function   ):
		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				inputs.clone()
					__lowerCAmelCase						,       __lowerCAmelCase											=				inputs.flatten().sort(descending=__SCREAMING_SNAKE_CASE			)
					__lowerCAmelCase											=				int(threshold * inputs.numel()			)

					# flat_out and mask access the same memory.
					__lowerCAmelCase											=				mask.flatten()
					__lowerCAmelCase											=				0
					__lowerCAmelCase											=				1
					return mask







		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					return gradOutput, None





class     _UpperCAmelCase							(						lowerCAmelCase_   ):
		@staticmethod
		def   lowerCamelCase__			(  __SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE			):



					'''simple docstring'''

					__lowerCAmelCase											=				inputs.clone()
					__lowerCAmelCase						,       __lowerCAmelCase											=				inputs.abs().flatten().sort(descending=__SCREAMING_SNAKE_CASE			)
					__lowerCAmelCase											=				int(threshold * inputs.numel()			)

					# flat_out and mask access the same memory.
					__lowerCAmelCase											=				mask.flatten()
					__lowerCAmelCase											=				0
					__lowerCAmelCase											=				1
					return mask

