


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  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			str:
             a_					:		Dict							=  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  SCREAMING_SNAKE_CASE__  :
      def __init__(       self				:       str     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       str=1_3     ,							SCREAMING_SNAKE_CASE__				:       str=3     ,							SCREAMING_SNAKE_CASE__				:       str=3_2     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=0.25     ,							SCREAMING_SNAKE_CASE__				:       Dict=8     ,							SCREAMING_SNAKE_CASE__				:       str=8     ,							SCREAMING_SNAKE_CASE__				:       int=6     ,							SCREAMING_SNAKE_CASE__				:       int=3_2     ,							SCREAMING_SNAKE_CASE__				:       Dict=True     ,							SCREAMING_SNAKE_CASE__				:       Tuple=True     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=True     ,							SCREAMING_SNAKE_CASE__				:       List[str]="relu6"     ,							SCREAMING_SNAKE_CASE__				:       Tuple=1_2_8_0     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=0.1     ,							SCREAMING_SNAKE_CASE__				:       List[str]=0.02     ,							SCREAMING_SNAKE_CASE__				:       int=True     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=True     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=1_0     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							)		->			Optional[int]:
             a_					:		Any							=  parent
             a_					:		List[str]							=  batch_size
             a_					:		Optional[int]							=  num_channels
             a_					:		Optional[int]							=  image_size
             a_					:		List[Any]							=  depth_multiplier
             a_					:		List[Any]							=  depth_divisible_by
             a_					:		Optional[Any]							=  min_depth
             a_					:		Tuple							=  expand_ratio
             a_					:		Tuple							=  tf_padding
             a_					:		Dict							=  output_stride
             a_					:		Optional[int]							=  first_layer_is_expansion
             a_					:		int							=  finegrained_output
             a_					:		Tuple							=  hidden_act
             a_					:		Any							=  last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier     )
             a_					:		List[str]							=  classifier_dropout_prob
             a_					:		Any							=  use_labels
             a_					:		Dict							=  is_training
             a_					:		Optional[Any]							=  num_labels
             a_					:		str							=  initializer_range
             a_					:		str							=  scope
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			Optional[int]:
             a_					:		Dict							=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

             a_					:		Any							=  None
             a_					:		Optional[int]							=  None
             if self.use_labels:
                    a_					:		List[Any]							=  ids_tensor([self.batch_size]     ,							self.num_labels     )
                    a_					:		Tuple							=  ids_tensor([self.batch_size, self.image_size, self.image_size]     ,							self.num_labels     )

             a_					:		Dict							=  self.get_config()

             return config, pixel_values, labels, pixel_labels
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			int:
             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        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       str     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     )		->			Optional[Any]:
             a_					:		Optional[int]							=  MobileNetVaModel(config=SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()
             a_					:		List[Any]							=  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        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			Union[str, Any]:
             a_					:		str							=  self.num_labels
             a_					:		Union[str, Any]							=  MobileNetVaForImageClassification(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()
             a_					:		Optional[Any]							=  model(SCREAMING_SNAKE_CASE__     ,							labels=SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       str     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			Any:
             a_					:		Any							=  self.num_labels
             a_					:		str							=  MobileNetVaForSemanticSegmentation(SCREAMING_SNAKE_CASE__     )
             model.to(SCREAMING_SNAKE_CASE__     )
             model.eval()
             a_					:		Union[str, 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,
                 )     ,							)
             a_					:		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        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Optional[Any]:
             a_					:		List[Any]							=  self.prepare_config_and_inputs()
             a_							,		a_							,		a_							,		a_					:		str							=  config_and_inputs
             a_					:		Any							=  {'pixel_values': pixel_values}
             return config, inputs_dict
@require_torch
class  SCREAMING_SNAKE_CASE__  (				lowercase__						,    lowercase__						,    unittest.TestCase			):
      snake_case__				:     Optional[int]        =						(
          (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
          if is_torch_available()
          else ()
      )
      snake_case__				:     List[str]        =						(
          {
              '''feature-extraction''': MobileNetVaModel,
              '''image-classification''': MobileNetVaForImageClassification,
              '''image-segmentation''': MobileNetVaForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      snake_case__				:     int        =						False
      snake_case__				:     str        =						False
      snake_case__				:     str        =						False
      snake_case__				:     Optional[Any]        =						False
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     )		->			str:
             a_					:		Dict							=  MobileNetVaModelTester(self     )
             a_					:		Union[str, Any]							=  MobileNetVaConfigTester(self     ,							config_class=SCREAMING_SNAKE_CASE__     ,							has_text_modality=SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			Dict:
             self.config_tester.run_common_tests()
      @unittest.skip(reason='MobileNetV2 does not use inputs_embeds'     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			List[Any]:
             pass
      @unittest.skip(reason='MobileNetV2 does not support input and output embeddings'     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Optional[Any]:
             pass
      @unittest.skip(reason='MobileNetV2 does not output attentions'     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			List[str]:
             pass
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			int:
             a_							,		a_					:		Optional[Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

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

                    a_					:		Tuple							=  ['pixel_values']
                    self.assertListEqual(arg_names[:1]     ,							SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Optional[Any]:
             a_					:		Any							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			Optional[int]:
             def check_hidden_states_output(SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ):
                    a_					:		Optional[Any]							=  model_class(SCREAMING_SNAKE_CASE__     )
                    model.to(SCREAMING_SNAKE_CASE__     )
                    model.eval()

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

                    a_					:		Optional[Any]							=  outputs.hidden_states

                    a_					:		Optional[int]							=  1_6
                    self.assertEqual(len(SCREAMING_SNAKE_CASE__     )     ,							SCREAMING_SNAKE_CASE__     )

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

             for model_class in self.all_model_classes:
                    a_					:		List[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"]
                    a_					:		Tuple							=  True

                    check_hidden_states_output(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     )		->			int:
             a_					:		Dict							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			List[str]:
             a_					:		Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE__     )

      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Dict:
             for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_					:		Any							=  MobileNetVaModel.from_pretrained(SCREAMING_SNAKE_CASE__     )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )







def        SCREAMING_SNAKE_CASE_						(			)	->		Dict:

       """simple docstring"""

       a_					:		Union[str, Any]							=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
       return image
@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      @cached_property
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			Optional[Any]:
             return (
                 MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'     ) if is_vision_available() else None
             )
      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			Union[str, Any]:
             a_					:		Any							=  MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'     ).to(SCREAMING_SNAKE_CASE__     )

             a_					:		Any							=  self.default_image_processor
             a_					:		int							=  prepare_img()
             a_					:		Optional[Any]							=  image_processor(images=SCREAMING_SNAKE_CASE__     ,							return_tensors='pt'     ).to(SCREAMING_SNAKE_CASE__     )

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

             # verify the logits
             a_					:		Optional[int]							=  torch.Size((1, 1_0_0_1)     )
             self.assertEqual(outputs.logits.shape     ,							SCREAMING_SNAKE_CASE__     )

             a_					:		Tuple							=  torch.tensor([0.2445, -1.1993, 0.1905]     ).to(SCREAMING_SNAKE_CASE__     )

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

      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			Union[str, Any]:
             a_					:		Union[str, Any]							=  MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'     )
             a_					:		Optional[Any]							=  model.to(SCREAMING_SNAKE_CASE__     )

             a_					:		int							=  MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'     )

             a_					:		Dict							=  prepare_img()
             a_					:		List[Any]							=  image_processor(images=SCREAMING_SNAKE_CASE__     ,							return_tensors='pt'     ).to(SCREAMING_SNAKE_CASE__     )

             # forward pass
             with torch.no_grad():
                    a_					:		Dict							=  model(**SCREAMING_SNAKE_CASE__     )
             a_					:		str							=  outputs.logits

             # verify the logits
             a_					:		str							=  torch.Size((1, 2_1, 6_5, 6_5)     )
             self.assertEqual(logits.shape     ,							SCREAMING_SNAKE_CASE__     )

             a_					:		Dict							=  torch.tensor(
                 [
                     [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
                     [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
                     [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
                 ]     ,							device=SCREAMING_SNAKE_CASE__     ,							)

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






from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase_      : str  					=       logging.get_logger(__name__)

UpperCAmelCase_      : List[str]  					=       {
    'YituTech/conv-bert-base': 'https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json',
    'YituTech/conv-bert-medium-small': (
        'https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json'
    ),
    'YituTech/conv-bert-small': 'https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json',
    # See all ConvBERT models at https://huggingface.co/models?filter=convbert
}
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      snake_case__				:     Optional[int]        =						'''convbert'''
      def __init__(       self				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=3_0_5_2_2     ,							SCREAMING_SNAKE_CASE__				:       Dict=7_6_8     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=1_2     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=1_2     ,							SCREAMING_SNAKE_CASE__				:       str=3_0_7_2     ,							SCREAMING_SNAKE_CASE__				:       Dict="gelu"     ,							SCREAMING_SNAKE_CASE__				:       Dict=0.1     ,							SCREAMING_SNAKE_CASE__				:       Tuple=0.1     ,							SCREAMING_SNAKE_CASE__				:       List[str]=5_1_2     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=2     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=0.02     ,							SCREAMING_SNAKE_CASE__				:       Any=1E-12     ,							SCREAMING_SNAKE_CASE__				:       int=1     ,							SCREAMING_SNAKE_CASE__				:       int=0     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=2     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=7_6_8     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=2     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=9     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=1     ,							SCREAMING_SNAKE_CASE__				:       Dict=None     ,							**SCREAMING_SNAKE_CASE__				:       List[Any]     ,							)		->			Any:
             super().__init__(
                 pad_token_id=SCREAMING_SNAKE_CASE__     ,							bos_token_id=SCREAMING_SNAKE_CASE__     ,							eos_token_id=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     ,							)

             a_					:		Tuple							=  vocab_size
             a_					:		List[str]							=  hidden_size
             a_					:		List[str]							=  num_hidden_layers
             a_					:		Dict							=  num_attention_heads
             a_					:		Optional[int]							=  intermediate_size
             a_					:		int							=  hidden_act
             a_					:		Dict							=  hidden_dropout_prob
             a_					:		int							=  attention_probs_dropout_prob
             a_					:		str							=  max_position_embeddings
             a_					:		List[str]							=  type_vocab_size
             a_					:		List[str]							=  initializer_range
             a_					:		Tuple							=  layer_norm_eps
             a_					:		Optional[int]							=  embedding_size
             a_					:		List[Any]							=  head_ratio
             a_					:		List[Any]							=  conv_kernel_size
             a_					:		Tuple							=  num_groups
             a_					:		Tuple							=  classifier_dropout
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      @property
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			Mapping[str, Mapping[int, str]]:
             if self.task == "multiple-choice":
                    a_					:		Tuple							=  {0: 'batch', 1: 'choice', 2: 'sequence'}
             else:
                    a_					:		List[str]							=  {0: 'batch', 1: 'sequence'}
             return OrderedDict(
                 [
                     ('input_ids', dynamic_axis),
                     ('attention_mask', dynamic_axis),
                     ('token_type_ids', dynamic_axis),
                 ]     )


