






'''simple docstring'''



from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import numpy
 import tensorflow as tf

 from transformers import (
     TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
     TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
     TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
     BertConfig,
     DPRConfig,
     TFDPRContextEncoder,
     TFDPRQuestionEncoder,
     TFDPRReader,
 )
class 							__SCREAMING_SNAKE_CASE       :




      def __init__(  self ,   lowerCamelCase ,   lowerCamelCase=13 ,   lowerCamelCase=7 ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=99 ,   lowerCamelCase=32 ,   lowerCamelCase=2 ,   lowerCamelCase=4 ,   lowerCamelCase=37 ,   lowerCamelCase="gelu" ,   lowerCamelCase=0.1 ,   lowerCamelCase=0.1 ,   lowerCamelCase=512 ,   lowerCamelCase=16 ,   lowerCamelCase=2 ,   lowerCamelCase=0.02 ,   lowerCamelCase=3 ,   lowerCamelCase=4 ,   lowerCamelCase=None ,   lowerCamelCase=0 ,   )							->List[str]:



         '''simple docstring'''
         __a				     =    parent
         __a				     =    batch_size
         __a				     =    seq_length
         __a				     =    is_training
         __a				     =    use_input_mask
         __a				     =    use_token_type_ids
         __a				     =    use_labels
         __a				     =    vocab_size
         __a				     =    hidden_size
         __a				     =    num_hidden_layers
         __a				     =    num_attention_heads
         __a				     =    intermediate_size
         __a				     =    hidden_act
         __a				     =    hidden_dropout_prob
         __a				     =    attention_probs_dropout_prob
         __a				     =    max_position_embeddings
         __a				     =    type_vocab_size
         __a				     =    type_sequence_label_size
         __a				     =    initializer_range
         __a				     =    num_labels
         __a				     =    num_choices
         __a				     =    scope
         __a				     =    projection_dim




      def  __UpperCamelCase							(  self      )							->List[str]:



         '''simple docstring'''
         __a				     =    ids_tensor([self.batch_size, self.seq_length] ,   self.vocab_size      )

         __a				     =    None
         if self.use_input_mask:
            # follow test_modeling_tf_ctrl.py
            __a				     =    random_attention_mask([self.batch_size, self.seq_length]      )

         __a				     =    None
         if self.use_token_type_ids:
            __a				     =    ids_tensor([self.batch_size, self.seq_length] ,   self.type_vocab_size      )

         __a				     =    None
         __a				     =    None
         __a				     =    None
         if self.use_labels:
            __a				     =    ids_tensor([self.batch_size] ,   self.type_sequence_label_size      )
            __a				     =    ids_tensor([self.batch_size, self.seq_length] ,   self.num_labels      )
            __a				     =    ids_tensor([self.batch_size] ,   self.num_choices      )

         __a				     =    BertConfig(
             vocab_size=self.vocab_size ,   hidden_size=self.hidden_size ,   num_hidden_layers=self.num_hidden_layers ,   num_attention_heads=self.num_attention_heads ,   intermediate_size=self.intermediate_size ,   hidden_act=self.hidden_act ,   hidden_dropout_prob=self.hidden_dropout_prob ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob ,   max_position_embeddings=self.max_position_embeddings ,   type_vocab_size=self.type_vocab_size ,   is_decoder=lowercase_ ,   initializer_range=self.initializer_range ,   )
         __a				     =    DPRConfig(projection_dim=self.projection_dim ,   **config.to_dict()      )

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




      def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->Tuple:



         '''simple docstring'''
         __a				     =    TFDPRContextEncoder(config=lowercase_      )
         __a				     =    model(lowercase_ ,   attention_mask=lowercase_ ,   token_type_ids=lowercase_      )
         __a				     =    model(lowercase_ ,   token_type_ids=lowercase_      )
         __a				     =    model(lowercase_      )
         self.parent.assertEqual(result.pooler_output.shape ,   (self.batch_size, self.projection_dim or self.hidden_size)      )




      def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->List[str]:



         '''simple docstring'''
         __a				     =    TFDPRQuestionEncoder(config=lowercase_      )
         __a				     =    model(lowercase_ ,   attention_mask=lowercase_ ,   token_type_ids=lowercase_      )
         __a				     =    model(lowercase_ ,   token_type_ids=lowercase_      )
         __a				     =    model(lowercase_      )
         self.parent.assertEqual(result.pooler_output.shape ,   (self.batch_size, self.projection_dim or self.hidden_size)      )




      def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->List[Any]:



         '''simple docstring'''
         __a				     =    TFDPRReader(config=lowercase_      )
         __a				     =    model(lowercase_ ,   attention_mask=lowercase_      )

         self.parent.assertEqual(result.start_logits.shape ,   (self.batch_size, self.seq_length)      )
         self.parent.assertEqual(result.end_logits.shape ,   (self.batch_size, self.seq_length)      )
         self.parent.assertEqual(result.relevance_logits.shape ,   (self.batch_size,)      )






      def  __UpperCamelCase							(  self      )							->int:



         '''simple docstring'''
         __a				     =    self.prepare_config_and_inputs()
         (
             __a 
         )				     =    config_and_inputs
         __a				     =    {"input_ids": input_ids}
         return config, inputs_dict







@require_tf
class 							__SCREAMING_SNAKE_CASE       (							snake_case__      ,      snake_case__      ,      unittest.TestCase     ):
      __a  					=(
          (
              TFDPRContextEncoder,
              TFDPRQuestionEncoder,
              TFDPRReader,
          )
          if is_tf_available()
          else ()
      )
      __a  					={"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

      __a  					=False
      __a  					=False
      __a  					=False
      __a  					=False
      __a  					=False




      def  __UpperCamelCase							(  self      )							->str:



         '''simple docstring'''
         __a				     =    TFDPRModelTester(self      )
         __a				     =    ConfigTester(self ,   config_class=lowercase_ ,   hidden_size=37      )




      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         self.config_tester.run_common_tests()




      def  __UpperCamelCase							(  self      )							->int:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_context_encoder(*lowercase_      )




      def  __UpperCamelCase							(  self      )							->List[Any]:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_question_encoder(*lowercase_      )




      def  __UpperCamelCase							(  self      )							->Optional[Any]:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_reader(*lowercase_      )






      @slow
      def  __UpperCamelCase							(  self      )							->List[str]:



         '''simple docstring'''
         for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a				     =    TFDPRContextEncoder.from_pretrained(lowercase_      )
            self.assertIsNotNone(lowercase_      )

         for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a				     =    TFDPRContextEncoder.from_pretrained(lowercase_      )
            self.assertIsNotNone(lowercase_      )

         for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a				     =    TFDPRQuestionEncoder.from_pretrained(lowercase_      )
            self.assertIsNotNone(lowercase_      )

         for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a				     =    TFDPRReader.from_pretrained(lowercase_      )
            self.assertIsNotNone(lowercase_      )







@require_tf
class 							__SCREAMING_SNAKE_CASE       (							unittest.TestCase     ):




      @slow
      def  __UpperCamelCase							(  self      )							->int:



         '''simple docstring'''
         __a				     =    TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'      )

         __a				     =    tf.constant(
             [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]      )  # [CLS] hello, is my dog cute? [SEP]
         __a				     =    model(lowercase_      )[0]  # embedding shape = (1, 768)
         # compare the actual values for a slice.
         __a				     =    tf.constant(
             [
                 [
                     0.03236253,
                     0.12753335,
                     0.16818509,
                     0.00279786,
                     0.3896933,
                     0.24264945,
                     0.2178971,
                     -0.02335227,
                     -0.08481959,
                     -0.14324117,
                 ]
             ]      )
         self.assertTrue(numpy.allclose(output[:, :10].numpy() ,   expected_slice.numpy() ,   atol=1e-4      )      )







'''simple docstring'''



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       (							_lowerCAmelCase     ):




      def  __UpperCamelCase							(  self      )							->int:



         '''simple docstring'''
         __a				     =    self.config_class(**self.inputs_dict      )
         self.parent.assertTrue(hasattr(lowerCamelCase ,   'tf_padding'      )      )
         self.parent.assertTrue(hasattr(lowerCamelCase ,   'depth_multiplier'      )      )







class 							__SCREAMING_SNAKE_CASE       :




      def __init__(  self ,   lowerCamelCase ,   lowerCamelCase=13 ,   lowerCamelCase=3 ,   lowerCamelCase=32 ,   lowerCamelCase=0.25 ,   lowerCamelCase=8 ,   lowerCamelCase=8 ,   lowerCamelCase=6 ,   lowerCamelCase=32 ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase="relu6" ,   lowerCamelCase=1280 ,   lowerCamelCase=0.1 ,   lowerCamelCase=0.02 ,   lowerCamelCase=True ,   lowerCamelCase=True ,   lowerCamelCase=10 ,   lowerCamelCase=None ,   )							->Optional[int]:



         '''simple docstring'''
         __a				     =    parent
         __a				     =    batch_size
         __a				     =    num_channels
         __a				     =    image_size
         __a				     =    depth_multiplier
         __a				     =    depth_divisible_by
         __a				     =    min_depth
         __a				     =    expand_ratio
         __a				     =    tf_padding
         __a				     =    output_stride
         __a				     =    first_layer_is_expansion
         __a				     =    finegrained_output
         __a				     =    hidden_act
         __a				     =    last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier      )
         __a				     =    classifier_dropout_prob
         __a				     =    use_labels
         __a				     =    is_training
         __a				     =    num_labels
         __a				     =    initializer_range
         __a				     =    scope




      def  __UpperCamelCase							(  self      )							->Optional[Any]:



         '''simple docstring'''
         __a				     =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

         __a				     =    self.get_config()

         return config, pixel_values, labels, pixel_labels




      def  __UpperCamelCase							(  self      )							->Dict:



         '''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  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->Tuple:



         '''simple docstring'''
         __a				     =    MobileNetVaModel(config=lowerCamelCase      )
         model.to(lowerCamelCase      )
         model.eval()
         __a				     =    model(lowerCamelCase      )
         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  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->Dict:



         '''simple docstring'''
         __a				     =    self.num_labels
         __a				     =    MobileNetVaForImageClassification(lowerCamelCase      )
         model.to(lowerCamelCase      )
         model.eval()
         __a				     =    model(lowerCamelCase ,   labels=lowerCamelCase      )
         self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.num_labels)      )




      def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )							->Optional[int]:



         '''simple docstring'''
         __a				     =    self.num_labels
         __a				     =    MobileNetVaForSemanticSegmentation(lowerCamelCase      )
         model.to(lowerCamelCase      )
         model.eval()
         __a				     =    model(lowerCamelCase      )
         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				     =    model(lowerCamelCase ,   labels=lowerCamelCase      )
         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  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         __a				     =    self.prepare_config_and_inputs()
         __a     ,			__a     ,			__a     ,			__a				     =    config_and_inputs
         __a				     =    {'pixel_values': pixel_values}
         return config, inputs_dict







@require_torch
class 							__SCREAMING_SNAKE_CASE       (							_lowerCAmelCase      ,      _lowerCAmelCase      ,      unittest.TestCase     ):
      __a  					=(
          (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
          if is_torch_available()
          else ()
      )
      __a  					=(
          {
              "feature-extraction": MobileNetVaModel,
              "image-classification": MobileNetVaForImageClassification,
              "image-segmentation": MobileNetVaForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      __a  					=False
      __a  					=False
      __a  					=False
      __a  					=False




      def  __UpperCamelCase							(  self      )							->List[Any]:



         '''simple docstring'''
         __a				     =    MobileNetVaModelTester(self      )
         __a				     =    MobileNetVaConfigTester(self ,   config_class=lowerCamelCase ,   has_text_modality=lowerCamelCase      )




      def  __UpperCamelCase							(  self      )							->str:



         '''simple docstring'''
         self.config_tester.run_common_tests()




      @unittest.skip(reason='MobileNetV2 does not use inputs_embeds'      )
      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         pass




      @unittest.skip(reason='MobileNetV2 does not support input and output embeddings'      )
      def  __UpperCamelCase							(  self      )							->int:



         '''simple docstring'''
         pass




      @unittest.skip(reason='MobileNetV2 does not output attentions'      )
      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         pass




      def  __UpperCamelCase							(  self      )							->str:



         '''simple docstring'''
         __a     ,			__a				     =    self.model_tester.prepare_config_and_inputs_for_common()

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

            __a				     =    ['pixel_values']
            self.assertListEqual(arg_names[:1] ,   lowerCamelCase      )




      def  __UpperCamelCase							(  self      )							->Optional[Any]:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowerCamelCase      )




      def  __UpperCamelCase							(  self      )							->Dict:



         '''simple docstring'''
         def check_hidden_states_output(lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      ):
            __a				     =    model_class(lowerCamelCase      )
            model.to(lowerCamelCase      )
            model.eval()

            with torch.no_grad():
               __a				     =    model(**self._prepare_for_class(lowerCamelCase ,   lowerCamelCase      )      )

            __a				     =    outputs.hidden_states

            __a				     =    16
            self.assertEqual(len(lowerCamelCase      ) ,   lowerCamelCase      )

         __a     ,			__a				     =    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            __a				     =    True
            check_hidden_states_output(lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )

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

            check_hidden_states_output(lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase      )




      def  __UpperCamelCase							(  self      )							->Union[str, Any]:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*lowerCamelCase      )




      def  __UpperCamelCase							(  self      )							->List[str]:



         '''simple docstring'''
         __a				     =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase      )






      @slow
      def  __UpperCamelCase							(  self      )							->Optional[Any]:



         '''simple docstring'''
         for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a				     =    MobileNetVaModel.from_pretrained(lowerCamelCase      )
            self.assertIsNotNone(lowerCamelCase      )







def    __UpperCAmelCase	(				)  ->			Union[str, Any]:

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



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




      @cached_property
      def  __UpperCamelCase							(  self      )							->Optional[Any]:



         '''simple docstring'''
         return (
             MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'      ) if is_vision_available() else None
         )




      @slow
      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         __a				     =    MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'      ).to(lowerCamelCase      )

         __a				     =    self.default_image_processor
         __a				     =    prepare_img()
         __a				     =    image_processor(images=lowerCamelCase ,   return_tensors='pt'      ).to(lowerCamelCase      )

         # forward pass
         with torch.no_grad():
            __a				     =    model(**lowerCamelCase      )

         # verify the logits
         __a				     =    torch.Size((1, 1001)      )
         self.assertEqual(outputs.logits.shape ,   lowerCamelCase      )

         __a				     =    torch.tensor([0.2445, -1.1993, 0.1905]      ).to(lowerCamelCase      )

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






      @slow
      def  __UpperCamelCase							(  self      )							->int:



         '''simple docstring'''
         __a				     =    MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'      )
         __a				     =    model.to(lowerCamelCase      )

         __a				     =    MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'      )

         __a				     =    prepare_img()
         __a				     =    image_processor(images=lowerCamelCase ,   return_tensors='pt'      ).to(lowerCamelCase      )

         # forward pass
         with torch.no_grad():
            __a				     =    model(**lowerCamelCase      )
         __a				     =    outputs.logits

         # verify the logits
         __a				     =    torch.Size((1, 21, 65, 65)      )
         self.assertEqual(logits.shape ,   lowerCamelCase      )

         __a				     =    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=lowerCamelCase ,   )

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