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




   def __init__(       self     :							Optional[Any]			,      _lowercase     :							str			,      _lowercase     :							int=13			,      _lowercase     :							Any=32			,      _lowercase     :							Dict=3			,      _lowercase     :							Dict=[3, 3, 5]			,      _lowercase     :							List[str]=[32, 16, 24]			,      _lowercase     :							Optional[Any]=[16, 24, 40]			,      _lowercase     :							Union[str, Any]=[1, 1, 2]			,      _lowercase     :							List[Any]=[1, 1, 2]			,      _lowercase     :							List[Any]=[1, 6, 6]			,      _lowercase     :							List[Any]=True			,      _lowercase     :							Any=True			,      _lowercase     :							str=37			,      _lowercase     :							Tuple="gelu"			,      _lowercase     :							int=10			,      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= parent
     SCREAMING_SNAKE_CASE__     				= batch_size
     SCREAMING_SNAKE_CASE__     				= image_size
     SCREAMING_SNAKE_CASE__     				= num_channels
     SCREAMING_SNAKE_CASE__     				= kernel_sizes
     SCREAMING_SNAKE_CASE__     				= in_channels
     SCREAMING_SNAKE_CASE__     				= out_channels
     SCREAMING_SNAKE_CASE__     				= strides
     SCREAMING_SNAKE_CASE__     				= num_block_repeats
     SCREAMING_SNAKE_CASE__     				= expand_ratios
     SCREAMING_SNAKE_CASE__     				= is_training
     SCREAMING_SNAKE_CASE__     				= hidden_act
     SCREAMING_SNAKE_CASE__     				= num_labels
     SCREAMING_SNAKE_CASE__     				= use_labels




   def       __a		(       self     :							int ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

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




   def       __a		(       self     :							List[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     :							List[Any]			,      _lowercase     :							Optional[int]			,      _lowercase     :							str			,      _lowercase     :							Optional[Any] ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= EfficientNetModel(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     SCREAMING_SNAKE_CASE__     				= 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       __a		(       self     :							Optional[Any]			,      _lowercase     :							Optional[Any]			,      _lowercase     :							Any			,      _lowercase     :							Dict ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= EfficientNetForImageClassification(_lowercase )
     model.to(_lowercase )
     model.eval()
     SCREAMING_SNAKE_CASE__     				= model(_lowercase			,      labels=_lowercase )
     self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_labels) )




   def       __a		(       self     :							Any ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= self.prepare_config_and_inputs()
     SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__     				= config_and_inputs
     SCREAMING_SNAKE_CASE__     				= {"""pixel_values""": pixel_values}
     return config, inputs_dict







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

   lowerCAmelCase_								=				False
   lowerCAmelCase_								=				False
   lowerCAmelCase_								=				False
   lowerCAmelCase_								=				False
   lowerCAmelCase_								=				False




   def       __a		(       self     :							Dict ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= EfficientNetModelTester(self )
     SCREAMING_SNAKE_CASE__     				= ConfigTester(
         self			,      config_class=_lowercase			,      has_text_modality=_lowercase			,      hidden_size=37 )




   def       __a		(       self     :							Optional[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       __a		(       self     :							Optional[Any] ):



     """simple docstring"""






     return




   @unittest.skip(reason="""EfficientNet does not use inputs_embeds""" )
   def       __a		(       self     :							Tuple ):



     """simple docstring"""






     pass




   @unittest.skip(reason="""EfficientNet does not support input and output embeddings""" )
   def       __a		(       self     :							Dict ):



     """simple docstring"""






     pass




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



     """simple docstring"""






     pass




   def       __a		(       self     :							List[str] ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__     				= self.model_tester.prepare_config_and_inputs_for_common()

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

       SCREAMING_SNAKE_CASE__     				= ["""pixel_values"""]
       self.assertListEqual(arg_names[:1]			,      _lowercase )




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



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowercase )




   def       __a		(       self     :							List[Any] ):



     """simple docstring"""






     def check_hidden_states_output(_lowercase     :							Optional[Any]			,      _lowercase     :							List[Any]			,      _lowercase     :							List[Any] ):
       SCREAMING_SNAKE_CASE__     				= model_class(_lowercase )
       model.to(_lowercase )
       model.eval()

       with torch.no_grad():
         SCREAMING_SNAKE_CASE__     				= model(**self._prepare_for_class(_lowercase			,      _lowercase ) )

       SCREAMING_SNAKE_CASE__     				= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
       SCREAMING_SNAKE_CASE__     				= 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]			,      )

     SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__     				= self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       SCREAMING_SNAKE_CASE__     				= True
       check_hidden_states_output(_lowercase			,      _lowercase			,      _lowercase )

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

       check_hidden_states_output(_lowercase			,      _lowercase			,      _lowercase )




   def       __a		(       self     :							Optional[int] ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= 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       __a		(       self     :							Tuple ):



     """simple docstring"""






     pass




   @slow
   def       __a		(       self     :							List[Any] ):



     """simple docstring"""






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




   @is_pipeline_test
   @require_vision
   @slow
   def       __a		(       self     :							Dict ):



     """simple docstring"""






     super().test_pipeline_image_classification()





def       __SCREAMING_SNAKE_CASE    (   )				->   List[Any]:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__     				= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
  return image







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




   @cached_property
   def       __a		(       self     :							List[Any] ):



     """simple docstring"""






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




   @slow
   def       __a		(       self     :							Dict ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= EfficientNetForImageClassification.from_pretrained("""google/efficientnet-b7""" ).to(_lowercase )

     SCREAMING_SNAKE_CASE__     				= self.default_image_processor
     SCREAMING_SNAKE_CASE__     				= prepare_img()
     SCREAMING_SNAKE_CASE__     				= image_processor(images=_lowercase			,      return_tensors="""pt""" ).to(_lowercase )

     # forward pass
     with torch.no_grad():
       SCREAMING_SNAKE_CASE__     				= model(**_lowercase )

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

     SCREAMING_SNAKE_CASE__     				= 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 ) )

import unittest

from transformers import is_torch_available, load_tool

from .test_tools_common import ToolTesterMixin


if is_torch_available():
   import torch







class __snake_case							(		unittest.TestCase  ,						lowerCamelCase_						):




   def       __a		(       self     :							Dict ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= load_tool("""speech-to-text""" )
     self.tool.setup()




   def       __a		(       self     :							Tuple ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= self.tool(torch.ones(30_00 ) )
     self.assertEqual(_lowercase			,      """ you""" )




   def       __a		(       self     :							Any ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= self.tool(audio=torch.ones(30_00 ) )
     self.assertEqual(_lowercase			,      """ you""" )
