






from __future__ import annotations

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.file_utils import is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
 import numpy as np
 import tensorflow as tf

 from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
 from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import MobileViTImageProcessor







class       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase	:Union[str, Any]     							=       self.config_class(**self.inputs_dict			)
          self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''hidden_sizes'''			)			)
          self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''neck_hidden_sizes'''			)			)
          self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''num_attention_heads'''			)			)







class       UpperCAmelCase_						:



      """simple docstring"""
      def __init__(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_=13   ,    SCREAMING_SNAKE_CASE_=32   ,    SCREAMING_SNAKE_CASE_=2   ,    SCREAMING_SNAKE_CASE_=3   ,    SCREAMING_SNAKE_CASE_=640   ,    SCREAMING_SNAKE_CASE_=4   ,    SCREAMING_SNAKE_CASE_="silu"   ,    SCREAMING_SNAKE_CASE_=3   ,    SCREAMING_SNAKE_CASE_=32   ,    SCREAMING_SNAKE_CASE_=0.1   ,    SCREAMING_SNAKE_CASE_=0.1   ,    SCREAMING_SNAKE_CASE_=0.1   ,    SCREAMING_SNAKE_CASE_=0.02   ,    SCREAMING_SNAKE_CASE_=True   ,    SCREAMING_SNAKE_CASE_=True   ,    SCREAMING_SNAKE_CASE_=10   ,    SCREAMING_SNAKE_CASE_=None   ,    )    ->				Optional[int]:
          UpperCamelCase	:str     							=       parent
          UpperCamelCase	:List[Any]     							=       batch_size
          UpperCamelCase	:Optional[Any]     							=       image_size
          UpperCamelCase	:Tuple     							=       patch_size
          UpperCamelCase	:int     							=       num_channels
          UpperCamelCase	:Optional[Any]     							=       last_hidden_size
          UpperCamelCase	:List[str]     							=       num_attention_heads
          UpperCamelCase	:Optional[Any]     							=       hidden_act
          UpperCamelCase	:Dict     							=       conv_kernel_size
          UpperCamelCase	:Dict     							=       output_stride
          UpperCamelCase	:List[Any]     							=       hidden_dropout_prob
          UpperCamelCase	:Union[str, Any]     							=       attention_probs_dropout_prob
          UpperCamelCase	:Any     							=       classifier_dropout_prob
          UpperCamelCase	:Dict     							=       use_labels
          UpperCamelCase	:Any     							=       is_training
          UpperCamelCase	:Dict     							=       num_labels
          UpperCamelCase	:Tuple     							=       initializer_range
          UpperCamelCase	:Union[str, Any]     							=       scope
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          UpperCamelCase	:List[Any]     							=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

          UpperCamelCase	:List[str]     							=       None
          UpperCamelCase	:List[Any]     							=       None
          if self.use_labels:
              UpperCamelCase	:Dict     							=       ids_tensor([self.batch_size]   ,    self.num_labels			)
              UpperCamelCase	:Tuple     							=       ids_tensor([self.batch_size, self.image_size, self.image_size]   ,    self.num_labels			)

          UpperCamelCase	:Dict     							=       self.get_config()

          return config, pixel_values, labels, pixel_labels
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          return MobileViTConfig(
              image_size=self.image_size   ,    patch_size=self.patch_size   ,    num_channels=self.num_channels   ,    num_attention_heads=self.num_attention_heads   ,    hidden_act=self.hidden_act   ,    conv_kernel_size=self.conv_kernel_size   ,    output_stride=self.output_stride   ,    hidden_dropout_prob=self.hidden_dropout_prob   ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,    classifier_dropout_prob=self.classifier_dropout_prob   ,    initializer_range=self.initializer_range   ,    )
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)    ->				List[Any]:
          UpperCamelCase	:Tuple     							=       TFMobileViTModel(config=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:Any     							=       model(SCREAMING_SNAKE_CASE_   ,    training=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:Optional[int]     							=       self.image_size // self.output_stride
          self.parent.assertEqual(
              result.last_hidden_state.shape   ,    (self.batch_size, self.last_hidden_size, expected_height, expected_width)			)
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)    ->				List[str]:
          UpperCamelCase	:Tuple     							=       self.num_labels
          UpperCamelCase	:Tuple     							=       TFMobileViTForImageClassification(SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:List[str]     							=       model(SCREAMING_SNAKE_CASE_   ,    labels=SCREAMING_SNAKE_CASE_   ,    training=SCREAMING_SNAKE_CASE_			)
          self.parent.assertEqual(result.logits.shape   ,    (self.batch_size, self.num_labels)			)
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)    ->				str:
          UpperCamelCase	:Dict     							=       self.num_labels
          UpperCamelCase	:int     							=       TFMobileViTForSemanticSegmentation(SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:str     							=       self.image_size // self.output_stride

          UpperCamelCase	:List[str]     							=       model(SCREAMING_SNAKE_CASE_   ,    training=SCREAMING_SNAKE_CASE_			)
          self.parent.assertEqual(
              result.logits.shape   ,    (self.batch_size, self.num_labels, expected_height, expected_width)			)

          UpperCamelCase	:List[Any]     							=       model(SCREAMING_SNAKE_CASE_   ,    labels=SCREAMING_SNAKE_CASE_   ,    training=SCREAMING_SNAKE_CASE_			)
          self.parent.assertEqual(
              result.logits.shape   ,    (self.batch_size, self.num_labels, expected_height, expected_width)			)




      def    UpperCAmelCase						(    self			)    ->				str:
          UpperCamelCase	:Optional[Any]     							=       self.prepare_config_and_inputs()
          UpperCamelCase							,       UpperCamelCase							,       UpperCamelCase							,       UpperCamelCase	:Dict     							=       config_and_inputs
          UpperCamelCase	:Optional[int]     							=       {'''pixel_values''': pixel_values}
          return config, inputs_dict







@require_tf
class       UpperCAmelCase_						( lowercase,     lowercase,     unittest.TestCase			):



      """simple docstring"""
      UpperCamelCase_    : List[str]									=(
          (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
          if is_tf_available()
          else ()
      )
      UpperCamelCase_    : List[str]									=(
          {'feature-extraction': TFMobileViTModel, 'image-classification': TFMobileViTForImageClassification}
          if is_tf_available()
          else {}
      )

      UpperCamelCase_    : Any									=False
      UpperCamelCase_    : Tuple									=False
      UpperCamelCase_    : str									=False
      UpperCamelCase_    : Optional[int]									=False
      UpperCamelCase_    : Optional[Any]									=False
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase	:Dict     							=       TFMobileViTModelTester(self			)
          UpperCamelCase	:List[Any]     							=       TFMobileViTConfigTester(self   ,    config_class=SCREAMING_SNAKE_CASE_   ,    has_text_modality=SCREAMING_SNAKE_CASE_			)
      def    UpperCAmelCase						(    self			)    ->				Optional[Any]:
          self.config_tester.run_common_tests()
      @unittest.skip(reason='''MobileViT does not use inputs_embeds'''			)
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          pass
      @unittest.skip(reason='''MobileViT does not support input and output embeddings'''			)
      def    UpperCAmelCase						(    self			)    ->				Any:
          pass
      @unittest.skip(reason='''MobileViT does not output attentions'''			)
      def    UpperCAmelCase						(    self			)    ->				List[Any]:
          pass
      def    UpperCAmelCase						(    self			)    ->				List[Any]:
          UpperCamelCase							,       UpperCamelCase	:Optional[Any]     							=       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCamelCase	:List[Any]     							=       model_class(SCREAMING_SNAKE_CASE_			)
              UpperCamelCase	:Tuple     							=       inspect.signature(model.call			)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              UpperCamelCase	:Any     							=       [*signature.parameters.keys()]

              UpperCamelCase	:str     							=       ['''pixel_values''']
              self.assertListEqual(arg_names[:1]   ,    SCREAMING_SNAKE_CASE_			)
      def    UpperCAmelCase						(    self			)    ->				Optional[Any]:
          UpperCamelCase	:Any     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_			)
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          def check_hidden_states_output(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			):
              UpperCamelCase	:List[str]     							=       model_class(SCREAMING_SNAKE_CASE_			)

              UpperCamelCase	:List[Any]     							=       model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)			)

              UpperCamelCase	:Optional[int]     							=       outputs.hidden_states

              UpperCamelCase	:List[str]     							=       5
              self.assertEqual(len(SCREAMING_SNAKE_CASE_			)   ,    SCREAMING_SNAKE_CASE_			)

              # MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
              # with the width and height being successively divided by 2.
              UpperCamelCase	:Optional[Any]     							=       2
              for i in range(len(SCREAMING_SNAKE_CASE_			)			):
                  self.assertListEqual(
                      list(hidden_states[i].shape[-2:]			)   ,    [self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]   ,    )
                  divisor *= 2

              self.assertEqual(self.model_tester.output_stride   ,    divisor // 2			)

          UpperCamelCase							,       UpperCamelCase	:int     							=       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCamelCase	: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"]
              UpperCamelCase	:Optional[int]     							=       True

              check_hidden_states_output(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)
      def    UpperCAmelCase						(    self			)    ->				int:
          UpperCamelCase	:Dict     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_			)
      def    UpperCAmelCase						(    self			)    ->				Optional[Any]:
          UpperCamelCase	:Any     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE_			)
      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''			)			) == 0   ,    reason='''TF does not support backprop for grouped convolutions on CPU.'''   ,    )
      def    UpperCAmelCase						(    self			)    ->				Tuple:
          super().test_dataset_conversion()
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_=2e-1   ,    SCREAMING_SNAKE_CASE_=2e-1			)    ->				int:
          self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    atol=SCREAMING_SNAKE_CASE_   ,    rtol=SCREAMING_SNAKE_CASE_			)			)
      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''			)			) == 0   ,    reason='''TF does not support backprop for grouped convolutions on CPU.'''   ,    )
      @slow
      def    UpperCAmelCase						(    self			)    ->				Tuple:
          UpperCamelCase							,       UpperCamelCase	:Dict     							=       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              # Since `TFMobileViTModel` cannot operate with the default `fit()` method.
              if model_class.__name__ != "TFMobileViTModel":
                  UpperCamelCase	:Tuple     							=       model_class(SCREAMING_SNAKE_CASE_			)
                  if getattr(SCREAMING_SNAKE_CASE_   ,    '''hf_compute_loss'''   ,    SCREAMING_SNAKE_CASE_			):
                      super().test_keras_fit()
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          UpperCamelCase							,       UpperCamelCase	:Tuple     							=       self.model_tester.prepare_config_and_inputs_for_common()
          for model_class in self.all_model_classes:
              # set an ignore index to correctly test the masked loss used in
              # `TFMobileViTForSemanticSegmentation`.
              if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
                  UpperCamelCase	:Any     							=       5

              UpperCamelCase	:Union[str, Any]     							=       model_class(SCREAMING_SNAKE_CASE_			)
              if getattr(SCREAMING_SNAKE_CASE_   ,    '''hf_compute_loss'''   ,    SCREAMING_SNAKE_CASE_			):
                  # The number of elements in the loss should be the same as the number of elements in the label
                  UpperCamelCase	:str     							=       self._prepare_for_class(inputs_dict.copy()   ,    SCREAMING_SNAKE_CASE_   ,    return_labels=SCREAMING_SNAKE_CASE_			)
                  UpperCamelCase	:Optional[int]     							=       prepared_for_class[
                      sorted(prepared_for_class.keys() - inputs_dict.keys()   ,    reverse=SCREAMING_SNAKE_CASE_			)[0]
                  ]
                  UpperCamelCase	:Any     							=       added_label.shape.as_list()[:1]

                  # Test that model correctly compute the loss with kwargs
                  UpperCamelCase	:Optional[int]     							=       self._prepare_for_class(inputs_dict.copy()   ,    SCREAMING_SNAKE_CASE_   ,    return_labels=SCREAMING_SNAKE_CASE_			)
                  UpperCamelCase	:Union[str, Any]     							=       {'''input_ids''', '''pixel_values''', '''input_features'''}
                  UpperCamelCase	:Optional[int]     							=       possible_input_names.intersection(set(SCREAMING_SNAKE_CASE_			)			).pop()
                  UpperCamelCase	:int     							=       prepared_for_class.pop(SCREAMING_SNAKE_CASE_			)

                  UpperCamelCase	:Any     							=       model(SCREAMING_SNAKE_CASE_   ,    **SCREAMING_SNAKE_CASE_			)[0]
                  self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)

                  # Test that model correctly compute the loss when we mask some positions
                  UpperCamelCase	:Tuple     							=       self._prepare_for_class(inputs_dict.copy()   ,    SCREAMING_SNAKE_CASE_   ,    return_labels=SCREAMING_SNAKE_CASE_			)
                  UpperCamelCase	:str     							=       {'''input_ids''', '''pixel_values''', '''input_features'''}
                  UpperCamelCase	:List[str]     							=       possible_input_names.intersection(set(SCREAMING_SNAKE_CASE_			)			).pop()
                  UpperCamelCase	:Optional[int]     							=       prepared_for_class.pop(SCREAMING_SNAKE_CASE_			)
                  if "labels" in prepared_for_class:
                      UpperCamelCase	:Optional[int]     							=       prepared_for_class['''labels'''].numpy()
                      if len(labels.shape			) > 1 and labels.shape[1] != 1:
                          # labels[0] = -100
                          UpperCamelCase	:Optional[int]     							=       tf.convert_to_tensor(SCREAMING_SNAKE_CASE_			)
                          UpperCamelCase	:Union[str, Any]     							=       model(SCREAMING_SNAKE_CASE_   ,    **SCREAMING_SNAKE_CASE_			)[0]
                          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)
                          self.assertTrue(not np.any(np.isnan(loss.numpy()			)			)			)

                # Test that model correctly compute the loss with a dict
                  UpperCamelCase	:Any     							=       self._prepare_for_class(inputs_dict.copy()   ,    SCREAMING_SNAKE_CASE_   ,    return_labels=SCREAMING_SNAKE_CASE_			)
                  UpperCamelCase	:Optional[int]     							=       model(SCREAMING_SNAKE_CASE_			)[0]
                  self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)

                  # Test that model correctly compute the loss with a tuple
                  UpperCamelCase	:Tuple     							=       self._prepare_for_class(inputs_dict.copy()   ,    SCREAMING_SNAKE_CASE_   ,    return_labels=SCREAMING_SNAKE_CASE_			)

                  # Get keys that were added with the _prepare_for_class function
                  UpperCamelCase	:Union[str, Any]     							=       prepared_for_class.keys() - inputs_dict.keys()
                  UpperCamelCase	:str     							=       inspect.signature(model.call			).parameters
                  UpperCamelCase	:Optional[Any]     							=       list(signature.keys()			)

                  # Create a dictionary holding the location of the tensors in the tuple
                  UpperCamelCase	:Optional[Any]     							=       {0: input_name}
                  for label_key in label_keys:
                      UpperCamelCase	:Union[str, Any]     							=       signature_names.index(SCREAMING_SNAKE_CASE_			)
                      UpperCamelCase	:Optional[int]     							=       label_key
                  UpperCamelCase	:List[Any]     							=       sorted(tuple_index_mapping.items()			)
                  # Initialize a list with their default values, update the values and convert to a tuple
                  UpperCamelCase	:Any     							=       []

                  for name in signature_names:
                      if name != "kwargs":
                          list_input.append(signature[name].default			)

                  for index, value in sorted_tuple_index_mapping:
                      UpperCamelCase	:Optional[int]     							=       prepared_for_class[value]

                  UpperCamelCase	:Optional[int]     							=       tuple(SCREAMING_SNAKE_CASE_			)

                  # Send to model
                  UpperCamelCase	:int     							=       model(tuple_input[:-1]			)[0]

                  self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]			)




      @slow
      def    UpperCAmelCase						(    self			)    ->				Tuple:
          for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCamelCase	:str     							=       TFMobileViTModel.from_pretrained(SCREAMING_SNAKE_CASE_			)
              self.assertIsNotNone(SCREAMING_SNAKE_CASE_			)







def   _A       (    ):
    UpperCamelCase	:int     							=       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
    return image







@require_tf
class       UpperCAmelCase_						( unittest.TestCase			):



      """simple docstring"""
      @slow
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase	:Optional[Any]     							=       TFMobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''			)

          UpperCamelCase	:int     							=       MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''			)
          UpperCamelCase	:Any     							=       prepare_img()
          UpperCamelCase	:str     							=       image_processor(images=SCREAMING_SNAKE_CASE_   ,    return_tensors='''tf'''			)

          # forward pass
          UpperCamelCase	:str     							=       model(**SCREAMING_SNAKE_CASE_   ,    training=SCREAMING_SNAKE_CASE_			)

          # verify the logits
          UpperCamelCase	:Dict     							=       tf.TensorShape((1, 1000)			)
          self.assertEqual(outputs.logits.shape   ,    SCREAMING_SNAKE_CASE_			)

          UpperCamelCase	:Optional[Any]     							=       tf.constant([-1.9364, -1.2327, -0.4653]			)

          tf.debugging.assert_near(outputs.logits[0, :3]   ,    SCREAMING_SNAKE_CASE_   ,    atol=1e-4   ,    rtol=1e-04			)




      @slow
      def    UpperCAmelCase						(    self			)    ->				Dict:
          # `from_pt` will be removed
          UpperCamelCase	:List[Any]     							=       TFMobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''			)

          UpperCamelCase	:List[Any]     							=       MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''			)

          UpperCamelCase	:List[Any]     							=       prepare_img()
          UpperCamelCase	:Tuple     							=       image_processor(images=SCREAMING_SNAKE_CASE_   ,    return_tensors='''tf'''			)

          # forward pass
          UpperCamelCase	:Any     							=       model(inputs.pixel_values   ,    training=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:Optional[Any]     							=       outputs.logits

          # verify the logits
          UpperCamelCase	:List[Any]     							=       tf.TensorShape((1, 21, 32, 32)			)
          self.assertEqual(logits.shape   ,    SCREAMING_SNAKE_CASE_			)

          UpperCamelCase	:List[str]     							=       tf.constant(
              [
                  [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
                  [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
                  [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
              ]			)

          tf.debugging.assert_near(logits[0, :3, :3, :3]   ,    SCREAMING_SNAKE_CASE_   ,    rtol=1e-4   ,    atol=1e-4			)













from transformers import (
    DPRContextEncoderTokenizer,
    DPRContextEncoderTokenizerFast,
    DPRQuestionEncoderTokenizer,
    DPRQuestionEncoderTokenizerFast,
    DPRReaderOutput,
    DPRReaderTokenizer,
    DPRReaderTokenizerFast,
)
from transformers.testing_utils import require_tokenizers, slow
from transformers.tokenization_utils_base import BatchEncoding

from ..bert.test_tokenization_bert import BertTokenizationTest







@require_tokenizers
class       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      UpperCamelCase_    : Optional[int]									=DPRContextEncoderTokenizer
      UpperCamelCase_    : Any									=DPRContextEncoderTokenizerFast
      UpperCamelCase_    : Optional[int]									=True







@require_tokenizers
class       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      UpperCamelCase_    : Tuple									=DPRQuestionEncoderTokenizer
      UpperCamelCase_    : Union[str, Any]									=DPRQuestionEncoderTokenizerFast
      UpperCamelCase_    : Any									=True







@require_tokenizers
class       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      UpperCamelCase_    : Tuple									=DPRReaderTokenizer
      UpperCamelCase_    : List[Any]									=DPRReaderTokenizerFast
      UpperCamelCase_    : Union[str, Any]									=True
      @slow
      def    UpperCAmelCase						(    self			)    ->				Dict:
          UpperCamelCase	:Optional[int]     							=       self.tokenizer_class.from_pretrained('''bert-base-uncased'''			)

          UpperCamelCase	:Optional[Any]     							=       tokenizer.encode('''question sequence'''   ,    add_special_tokens=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:Optional[Any]     							=       tokenizer.encode('''title sequence'''   ,    add_special_tokens=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:List[str]     							=       tokenizer.encode('''text sequence ''' * 4   ,    add_special_tokens=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:Optional[int]     							=       [[101] + text_a + [102] + text_a + [102] + text_a]
          UpperCamelCase	:Tuple     							=       BatchEncoding({'''input_ids''': input_ids}			)

          UpperCamelCase	:Dict     							=       [[0] * len(input_ids[0]			)]
          UpperCamelCase	:List[Any]     							=       [[0] * len(input_ids[0]			)]
          UpperCamelCase	:Tuple     							=       [0]
          UpperCamelCase	:Union[str, Any]     							=       DPRReaderOutput(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)

          UpperCamelCase							,       UpperCamelCase	:Union[str, Any]     							=       8, 9
          UpperCamelCase	:Any     							=       10
          UpperCamelCase	:Optional[Any]     							=       10
          UpperCamelCase	:Optional[int]     							=       tokenizer.decode_best_spans(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)
          self.assertEqual(predicted_spans[0].start_index   ,    SCREAMING_SNAKE_CASE_			)
          self.assertEqual(predicted_spans[0].end_index   ,    SCREAMING_SNAKE_CASE_			)
          self.assertEqual(predicted_spans[0].doc_id   ,    0			)




      @slow
      def    UpperCAmelCase						(    self			)    ->				Any:
          UpperCamelCase	:List[str]     							=       self.tokenizer_class.from_pretrained('''bert-base-uncased'''			)

          UpperCamelCase	:Any     							=       tokenizer.encode('''question sequence'''   ,    add_special_tokens=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:str     							=       tokenizer.encode('''title sequence'''   ,    add_special_tokens=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:Union[str, Any]     							=       tokenizer.encode('''text sequence'''   ,    add_special_tokens=SCREAMING_SNAKE_CASE_			)
          UpperCamelCase	:List[Any]     							=       [101] + text_a + [102] + text_a + [102] + text_a
          UpperCamelCase	:Any     							=       tokenizer(questions=['''question sequence''']   ,    titles=['''title sequence''']   ,    texts=['''text sequence''']			)
          self.assertIn('''input_ids'''   ,    SCREAMING_SNAKE_CASE_			)
          self.assertIn('''attention_mask'''   ,    SCREAMING_SNAKE_CASE_			)
          self.assertListEqual(encoded_input['''input_ids'''][0]   ,    SCREAMING_SNAKE_CASE_			)





