






import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import 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 MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
 from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import MobileNetVaImageProcessor





class 			__a  (    A__       ):







 def 					__lowercase					(		self					:   Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[Any]       				=  self.config_class(**self.inputs_dict						)
      self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE							,   "tf_padding"						)						)
      self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE							,   "depth_multiplier"						)						)







class 			__a  :







 def __init__(		self					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   Dict=13							,   SCREAMING_SNAKE_CASE					:   Dict=3							,   SCREAMING_SNAKE_CASE					:   Any=32							,   SCREAMING_SNAKE_CASE					:   Optional[int]=0.2_5							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=8							,   SCREAMING_SNAKE_CASE					:   List[str]=8							,   SCREAMING_SNAKE_CASE					:   Dict=6							,   SCREAMING_SNAKE_CASE					:   Tuple=32							,   SCREAMING_SNAKE_CASE					:   Tuple=True							,   SCREAMING_SNAKE_CASE					:   List[str]=True							,   SCREAMING_SNAKE_CASE					:   Any=True							,   SCREAMING_SNAKE_CASE					:   str="relu6"							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=12_80							,   SCREAMING_SNAKE_CASE					:   List[Any]=0.1							,   SCREAMING_SNAKE_CASE					:   Tuple=0.0_2							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=True							,   SCREAMING_SNAKE_CASE					:   str=True							,   SCREAMING_SNAKE_CASE					:   List[Any]=10							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=None							,   ):


      '''simple docstring'''






      UpperCamelCase__			:   Dict       				=  parent
      UpperCamelCase__			:   int       				=  batch_size
      UpperCamelCase__			:   Union[str, Any]       				=  num_channels
      UpperCamelCase__			:   List[str]       				=  image_size
      UpperCamelCase__			:   int       				=  depth_multiplier
      UpperCamelCase__			:   Optional[Any]       				=  depth_divisible_by
      UpperCamelCase__			:   List[str]       				=  min_depth
      UpperCamelCase__			:   Tuple       				=  expand_ratio
      UpperCamelCase__			:   Optional[Any]       				=  tf_padding
      UpperCamelCase__			:   Dict       				=  output_stride
      UpperCamelCase__			:   Any       				=  first_layer_is_expansion
      UpperCamelCase__			:   List[Any]       				=  finegrained_output
      UpperCamelCase__			:   Optional[Any]       				=  hidden_act
      UpperCamelCase__			:   List[str]       				=  last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier						)
      UpperCamelCase__			:   str       				=  classifier_dropout_prob
      UpperCamelCase__			:   str       				=  use_labels
      UpperCamelCase__			:   Optional[Any]       				=  is_training
      UpperCamelCase__			:   List[Any]       				=  num_labels
      UpperCamelCase__			:   Any       				=  initializer_range
      UpperCamelCase__			:   List[Any]       				=  scope







 def 					__lowercase					(		self					:   Optional[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Dict       				=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

      UpperCamelCase__			:   Any       				=  None
      UpperCamelCase__			:   Optional[int]       				=  None
      if self.use_labels:
           UpperCamelCase__			:   Union[str, Any]       				=  ids_tensor([self.batch_size]							,   self.num_labels						)
           UpperCamelCase__			:   str       				=  ids_tensor([self.batch_size, self.image_size, self.image_size]							,   self.num_labels						)

      UpperCamelCase__			:   Union[str, Any]       				=  self.get_config()

      return config, pixel_values, labels, pixel_labels







 def 					__lowercase					(		self					:   Optional[int]						):


      '''simple docstring'''






      return MobileNetVaConfig(
          num_channels=self.num_channels							,   image_size=self.image_size							,   depth_multiplier=self.depth_multiplier							,   depth_divisible_by=self.depth_divisible_by							,   min_depth=self.min_depth							,   expand_ratio=self.expand_ratio							,   output_stride=self.output_stride							,   first_layer_is_expansion=self.first_layer_is_expansion							,   finegrained_output=self.finegrained_output							,   hidden_act=self.hidden_act							,   tf_padding=self.tf_padding							,   classifier_dropout_prob=self.classifier_dropout_prob							,   initializer_range=self.initializer_range							,   )







 def 					__lowercase					(		self					:   List[str]							,   SCREAMING_SNAKE_CASE					:   int							,   SCREAMING_SNAKE_CASE					:   Any							,   SCREAMING_SNAKE_CASE					:   List[str]							,   SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Tuple       				=  MobileNetVaModel(config=SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()
      UpperCamelCase__			:   Dict       				=  model(SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(
          result.last_hidden_state.shape							,   (
              self.batch_size,
              self.last_hidden_size,
              self.image_size // self.output_stride,
              self.image_size // self.output_stride,
          )							,   )
      self.parent.assertEqual(
          result.pooler_output.shape							,   (self.batch_size, self.last_hidden_size)							,   )







 def 					__lowercase					(		self					:   Tuple							,   SCREAMING_SNAKE_CASE					:   Optional[Any]							,   SCREAMING_SNAKE_CASE					:   int							,   SCREAMING_SNAKE_CASE					:   Any							,   SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   List[str]       				=  self.num_labels
      UpperCamelCase__			:   Optional[int]       				=  MobileNetVaForImageClassification(SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()
      UpperCamelCase__			:   List[Any]       				=  model(SCREAMING_SNAKE_CASE							,   labels=SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.num_labels)						)







 def 					__lowercase					(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Optional[Any]							,   SCREAMING_SNAKE_CASE					:   int							,   SCREAMING_SNAKE_CASE					:   Optional[Any]							,   SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[Any]       				=  self.num_labels
      UpperCamelCase__			:   str       				=  MobileNetVaForSemanticSegmentation(SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()
      UpperCamelCase__			:   List[Any]       				=  model(SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(
          result.logits.shape							,   (
              self.batch_size,
              self.num_labels,
              self.image_size // self.output_stride,
              self.image_size // self.output_stride,
          )							,   )
      UpperCamelCase__			:   Union[str, Any]       				=  model(SCREAMING_SNAKE_CASE							,   labels=SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(
          result.logits.shape							,   (
              self.batch_size,
              self.num_labels,
              self.image_size // self.output_stride,
              self.image_size // self.output_stride,
          )							,   )







 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






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







@require_torch
class 			__a  (    A__       ,   A__       ,   unittest.TestCase       ):
 _lowerCAmelCase       :       Tuple        = (
     (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 _lowerCAmelCase       :       Optional[int]        = (
     {
         '''feature-extraction''': MobileNetVaModel,
         '''image-classification''': MobileNetVaForImageClassification,
         '''image-segmentation''': MobileNetVaForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 _lowerCAmelCase       :       Tuple        = False
 _lowerCAmelCase       :       Union[str, Any]        = False
 _lowerCAmelCase       :       List[Any]        = False
 _lowerCAmelCase       :       List[str]        = False







 def 					__lowercase					(		self					:   str						):


      '''simple docstring'''






      UpperCamelCase__			:   Tuple       				=  MobileNetVaModelTester(self						)
      UpperCamelCase__			:   str       				=  MobileNetVaConfigTester(self							,   config_class=SCREAMING_SNAKE_CASE							,   has_text_modality=SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   Optional[Any]						):


      '''simple docstring'''






      self.config_tester.run_common_tests()







 @unittest.skip(reason="MobileNetV2 does not use inputs_embeds"						)
 def 					__lowercase					(		self					:   int						):


      '''simple docstring'''






      pass







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


      '''simple docstring'''






      pass







 @unittest.skip(reason="MobileNetV2 does not output attentions"						)
 def 					__lowercase					(		self					:   Optional[int]						):


      '''simple docstring'''






      pass







 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      UpperCamelCase__				,     UpperCamelCase__			:   Tuple       				=  self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCamelCase__			:   Any       				=  ["pixel_values"]
           self.assertListEqual(arg_names[:1]							,   SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Any       				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      def check_hidden_states_output(SCREAMING_SNAKE_CASE					:   List[str]							,   SCREAMING_SNAKE_CASE					:   List[str]							,   SCREAMING_SNAKE_CASE					:   Optional[Any]						):
           UpperCamelCase__			:   Any       				=  model_class(SCREAMING_SNAKE_CASE						)
           model.to(SCREAMING_SNAKE_CASE						)
           model.eval()

           with torch.no_grad():
                UpperCamelCase__			:   Tuple       				=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE						)						)

           UpperCamelCase__			:   List[Any]       				=  outputs.hidden_states

           UpperCamelCase__			:   Union[str, Any]       				=  16
           self.assertEqual(len(SCREAMING_SNAKE_CASE						)							,   SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   str       				=  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           UpperCamelCase__			:   Union[str, Any]       				=  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"]
           UpperCamelCase__			:   str       				=  True

           check_hidden_states_output(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[int]       				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[int]       				=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE						)







 @slow
 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__			:   Optional[int]       				=  MobileNetVaModel.from_pretrained(SCREAMING_SNAKE_CASE						)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE						)







def 		SCREAMING_SNAKE_CASE							(					) ->	Optional[Any]:
     UpperCamelCase__			:   str       				=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
     return image





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







 @cached_property
 def 					__lowercase					(		self					:   str						):


      '''simple docstring'''






      return (
          MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224"						) if is_vision_available() else None
      )







 @slow
 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      UpperCamelCase__			:   List[Any]       				=  MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224"						).to(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__			:   Tuple       				=  self.default_image_processor
      UpperCamelCase__			:   Union[str, Any]       				=  prepare_img()
      UpperCamelCase__			:   str       				=  image_processor(images=SCREAMING_SNAKE_CASE							,   return_tensors="pt"						).to(SCREAMING_SNAKE_CASE						)

      # forward pass
      with torch.no_grad():
           UpperCamelCase__			:   List[str]       				=  model(**SCREAMING_SNAKE_CASE						)

      # verify the logits
      UpperCamelCase__			:   List[str]       				=  torch.Size((1, 10_01)						)
      self.assertEqual(outputs.logits.shape							,   SCREAMING_SNAKE_CASE						)

      UpperCamelCase__			:   Optional[Any]       				=  torch.tensor([0.2_4_4_5, -1.1_9_9_3, 0.1_9_0_5]						).to(SCREAMING_SNAKE_CASE						)

      self.assertTrue(torch.allclose(outputs.logits[0, :3]							,   SCREAMING_SNAKE_CASE							,   atol=1e-4						)						)







 @slow
 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      UpperCamelCase__			:   str       				=  MobileNetVaForSemanticSegmentation.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"						)
      UpperCamelCase__			:   Dict       				=  model.to(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__			:   Tuple       				=  MobileNetVaImageProcessor.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"						)

      UpperCamelCase__			:   Union[str, Any]       				=  prepare_img()
      UpperCamelCase__			:   List[str]       				=  image_processor(images=SCREAMING_SNAKE_CASE							,   return_tensors="pt"						).to(SCREAMING_SNAKE_CASE						)

      # forward pass
      with torch.no_grad():
           UpperCamelCase__			:   Optional[int]       				=  model(**SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Optional[int]       				=  outputs.logits

      # verify the logits
      UpperCamelCase__			:   List[Any]       				=  torch.Size((1, 21, 65, 65)						)
      self.assertEqual(logits.shape							,   SCREAMING_SNAKE_CASE						)

      UpperCamelCase__			:   Union[str, Any]       				=  torch.tensor(
          [
              [[1_7.5_7_9_0, 1_7.7_5_8_1, 1_8.3_3_5_5], [1_8.3_2_5_7, 1_8.4_2_3_0, 1_8.8_9_7_3], [1_8.6_1_6_9, 1_8.8_6_5_0, 1_9.2_1_8_7]],
              [[-2.1_5_9_5, -2.0_9_7_7, -2.3_7_4_1], [-2.4_2_2_6, -2.3_0_2_8, -2.6_8_3_5], [-2.7_8_1_9, -2.5_9_9_1, -2.7_7_0_6]],
              [[4.2_0_5_8, 4.8_3_1_7, 4.7_6_3_8], [4.4_1_3_6, 5.0_3_6_1, 4.9_3_8_3], [4.5_0_2_8, 4.9_6_4_4, 4.8_7_3_4]],
          ]							,   device=SCREAMING_SNAKE_CASE							,   )

      self.assertTrue(torch.allclose(logits[0, :3, :3, :3]							,   SCREAMING_SNAKE_CASE							,   atol=1e-4						)						)







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase	:							Optional[int]				=logging.get_logger(__name__)

lowerCamelCase	:							Dict				={
    '''facebook/s2t-wav2vec2-large-en-de''': (
        '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json'''
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}





class 			__a  (    A__       ):
 _lowerCAmelCase       :       Tuple        = '''speech_to_text_2'''
 _lowerCAmelCase       :       Dict        = ['''past_key_values''']
 _lowerCAmelCase       :       Any        = {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''}







 def __init__(		self					:   List[str]							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=1_00_00							,   SCREAMING_SNAKE_CASE					:   List[Any]=6							,   SCREAMING_SNAKE_CASE					:   List[Any]=20_48							,   SCREAMING_SNAKE_CASE					:   Dict=4							,   SCREAMING_SNAKE_CASE					:   List[Any]=0.0							,   SCREAMING_SNAKE_CASE					:   Optional[int]=True							,   SCREAMING_SNAKE_CASE					:   List[Any]="relu"							,   SCREAMING_SNAKE_CASE					:   Tuple=2_56							,   SCREAMING_SNAKE_CASE					:   List[str]=0.1							,   SCREAMING_SNAKE_CASE					:   Tuple=0.0							,   SCREAMING_SNAKE_CASE					:   Any=0.0							,   SCREAMING_SNAKE_CASE					:   int=0.0_2							,   SCREAMING_SNAKE_CASE					:   Dict=2							,   SCREAMING_SNAKE_CASE					:   List[str]=True							,   SCREAMING_SNAKE_CASE					:   Any=1							,   SCREAMING_SNAKE_CASE					:   List[Any]=0							,   SCREAMING_SNAKE_CASE					:   Any=2							,   SCREAMING_SNAKE_CASE					:   str=10_24							,   **SCREAMING_SNAKE_CASE					:   int							,   ):


      '''simple docstring'''






      UpperCamelCase__			:   int       				=  vocab_size
      UpperCamelCase__			:   Optional[Any]       				=  d_model
      UpperCamelCase__			:   Optional[Any]       				=  decoder_ffn_dim
      UpperCamelCase__			:   str       				=  decoder_layers
      UpperCamelCase__			:   Any       				=  decoder_attention_heads
      UpperCamelCase__			:   List[str]       				=  dropout
      UpperCamelCase__			:   int       				=  attention_dropout
      UpperCamelCase__			:   Optional[int]       				=  activation_dropout
      UpperCamelCase__			:   Union[str, Any]       				=  activation_function
      UpperCamelCase__			:   Tuple       				=  init_std
      UpperCamelCase__			:   Optional[int]       				=  decoder_layerdrop
      UpperCamelCase__			:   Dict       				=  use_cache
      UpperCamelCase__			:   str       				=  decoder_layers
      UpperCamelCase__			:   Union[str, Any]       				=  scale_embedding  # scale factor will be sqrt(d_model) if True
      UpperCamelCase__			:   Optional[Any]       				=  max_target_positions

      super().__init__(
          pad_token_id=SCREAMING_SNAKE_CASE							,   bos_token_id=SCREAMING_SNAKE_CASE							,   eos_token_id=SCREAMING_SNAKE_CASE							,   decoder_start_token_id=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE							,   )