




'''simple docstring'''




import inspect
import unittest

from transformers import MobileViTConfig
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 MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
     from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import MobileViTImageProcessor


class   __UpperCamelCase       ( lowerCamelCase__				):






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.config_class(**self.inputs_dict  )
              self.parent.assertTrue(hasattr(lowerCAmelCase,		'''hidden_sizes'''  )  )
              self.parent.assertTrue(hasattr(lowerCAmelCase,		'''neck_hidden_sizes'''  )  )
              self.parent.assertTrue(hasattr(lowerCAmelCase,		'''num_attention_heads'''  )  )





class   __UpperCamelCase       :






       def __init__(							self,		lowerCAmelCase,		lowerCAmelCase=13,		lowerCAmelCase=32,		lowerCAmelCase=2,		lowerCAmelCase=3,		lowerCAmelCase=640,		lowerCAmelCase=4,		lowerCAmelCase="silu",		lowerCAmelCase=3,		lowerCAmelCase=32,		lowerCAmelCase=0.1,		lowerCAmelCase=0.1,		lowerCAmelCase=0.1,		lowerCAmelCase=0.0_2,		lowerCAmelCase=True,		lowerCAmelCase=True,		lowerCAmelCase=10,		lowerCAmelCase=None,		):



              """simple docstring"""







              lowerCamelCase_							=parent
              lowerCamelCase_							=batch_size
              lowerCamelCase_							=image_size
              lowerCamelCase_							=patch_size
              lowerCamelCase_							=num_channels
              lowerCamelCase_							=last_hidden_size
              lowerCamelCase_							=num_attention_heads
              lowerCamelCase_							=hidden_act
              lowerCamelCase_							=conv_kernel_size
              lowerCamelCase_							=output_stride
              lowerCamelCase_							=hidden_dropout_prob
              lowerCamelCase_							=attention_probs_dropout_prob
              lowerCamelCase_							=classifier_dropout_prob
              lowerCamelCase_							=use_labels
              lowerCamelCase_							=is_training
              lowerCamelCase_							=num_labels
              lowerCamelCase_							=initializer_range
              lowerCamelCase_							=scope






       def      lowercase__     (							self  ):



              """simple docstring"""







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

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

              lowerCamelCase_							=self.get_config()

              return config, pixel_values, labels, pixel_labels






       def      lowercase__     (							self  ):



              """simple docstring"""







              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      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=MobileViTModel(config=lowerCAmelCase  )
              model.to(lowerCAmelCase  )
              model.eval()
              lowerCamelCase_							=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,
                  ),		)






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=self.num_labels
              lowerCamelCase_							=MobileViTForImageClassification(lowerCAmelCase  )
              model.to(lowerCAmelCase  )
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase,		labels=lowerCAmelCase  )
              self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)  )






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=self.num_labels
              lowerCamelCase_							=MobileViTForSemanticSegmentation(lowerCAmelCase  )
              model.to(lowerCAmelCase  )
              model.eval()
              lowerCamelCase_							=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,
                  ),		)
              lowerCamelCase_							=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      lowercase__     (							self  ):



              """simple docstring"""







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





@require_torch
class   __UpperCamelCase       ( lowerCamelCase__			,					lowerCamelCase__			,					unittest.TestCase				):
       lowercase	:    List[Any]      						=(
           (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       lowercase	:    List[str]      						=(
           {
               'feature-extraction': MobileViTModel,
               'image-classification': MobileViTForImageClassification,
               'image-segmentation': MobileViTForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       lowercase	:    int      						=False
       lowercase	:    str      						=False
       lowercase	:    Optional[Any]      						=False
       lowercase	:    Union[str, Any]      						=False






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=MobileViTModelTester(self  )
              lowerCamelCase_							=MobileViTConfigTester(self,		config_class=lowerCAmelCase,		has_text_modality=lowerCAmelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              self.config_tester.run_common_tests()






       @unittest.skip(reason='''MobileViT does not use inputs_embeds'''  )
       def      lowercase__     (							self  ):



              """simple docstring"""







              pass






       @unittest.skip(reason='''MobileViT does not support input and output embeddings'''  )
       def      lowercase__     (							self  ):



              """simple docstring"""







              pass






       @unittest.skip(reason='''MobileViT does not output attentions'''  )
       def      lowercase__     (							self  ):



              """simple docstring"""







              pass






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_,      lowerCamelCase_							=self.model_tester.prepare_config_and_inputs_for_common()

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

                     lowerCamelCase_							=['''pixel_values''']
                     self.assertListEqual(arg_names[:1],		lowerCAmelCase  )






       @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
       def      lowercase__     (							self  ):



              """simple docstring"""







              pass






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*lowerCAmelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              def check_hidden_states_output(lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):
                     lowerCamelCase_							=model_class(lowerCAmelCase  )
                     model.to(lowerCAmelCase  )
                     model.eval()

                     with torch.no_grad():
                            lowerCamelCase_							=model(**self._prepare_for_class(lowerCAmelCase,		lowerCAmelCase  )  )

                     lowerCamelCase_							=outputs.hidden_states

                     lowerCamelCase_							=5
                     self.assertEqual(len(lowerCAmelCase  ),		lowerCAmelCase  )

                     # MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
                     # with the width and height being successively divided by 2.
                     lowerCamelCase_							=2
                     for i in range(len(lowerCAmelCase  )  ):
                            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  )

              lowerCamelCase_,      lowerCamelCase_							=self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     lowerCamelCase_							=True
                     check_hidden_states_output(lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  )

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

                     check_hidden_states_output(lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase  )






       @slow
       def      lowercase__     (							self  ):



              """simple docstring"""







              for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     lowerCamelCase_							=MobileViTModel.from_pretrained(lowerCAmelCase  )
                     self.assertIsNotNone(lowerCAmelCase  )





def      a_							(      )							->						List[Any]:



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


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






       @cached_property
       def      lowercase__     (							self  ):



              """simple docstring"""







              return MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''  ) if is_vision_available() else None






       @slow
       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''  ).to(lowerCAmelCase  )

              lowerCamelCase_							=self.default_image_processor
              lowerCamelCase_							=prepare_img()
              lowerCamelCase_							=image_processor(images=lowerCAmelCase,		return_tensors='''pt'''  ).to(lowerCAmelCase  )

              # forward pass
              with torch.no_grad():
                     lowerCamelCase_							=model(**lowerCAmelCase  )

              # verify the logits
              lowerCamelCase_							=torch.Size((1, 1_000)  )
              self.assertEqual(outputs.logits.shape,		lowerCAmelCase  )

              lowerCamelCase_							=torch.tensor([-1.9_3_6_4, -1.2_3_2_7, -0.4_6_5_3]  ).to(lowerCAmelCase  )

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






       @slow
       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )
              lowerCamelCase_							=model.to(lowerCAmelCase  )

              lowerCamelCase_							=MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )

              lowerCamelCase_							=prepare_img()
              lowerCamelCase_							=image_processor(images=lowerCAmelCase,		return_tensors='''pt'''  ).to(lowerCAmelCase  )

              # forward pass
              with torch.no_grad():
                     lowerCamelCase_							=model(**lowerCAmelCase  )
              lowerCamelCase_							=outputs.logits

              # verify the logits
              lowerCamelCase_							=torch.Size((1, 21, 32, 32)  )
              self.assertEqual(logits.shape,		lowerCAmelCase  )

              lowerCamelCase_							=torch.tensor(
                  [
                      [[6.9_7_1_3, 6.9_7_8_6, 7.2_4_2_2], [7.2_8_9_3, 7.2_8_2_5, 7.4_4_4_6], [7.6_5_8_0, 7.8_7_9_7, 7.9_4_2_0]],
                      [[-1_0.6_8_6_9, -1_0.3_2_5_0, -1_0.3_4_7_1], [-1_0.4_2_2_8, -9.9_8_6_8, -9.7_1_3_2], [-1_1.0_4_0_5, -1_1.0_2_2_1, -1_0.7_3_1_8]],
                      [[-3.3_0_8_9, -2.8_5_3_9, -2.6_7_4_0], [-3.2_7_0_6, -2.5_6_2_1, -2.5_1_0_8], [-3.2_5_3_4, -2.6_6_1_5, -2.6_6_5_1]],
                  ],		device=lowerCAmelCase,		)

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






       @slow
       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )
              lowerCamelCase_							=model.to(lowerCAmelCase  )

              lowerCamelCase_							=MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )

              lowerCamelCase_							=prepare_img()
              lowerCamelCase_							=image_processor(images=lowerCAmelCase,		return_tensors='''pt'''  ).to(lowerCAmelCase  )

              # forward pass
              with torch.no_grad():
                     lowerCamelCase_							=model(**lowerCAmelCase  )

              lowerCamelCase_							=outputs.logits.detach().cpu()

              lowerCamelCase_							=image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase,		target_sizes=[(50, 60)]  )
              lowerCamelCase_							=torch.Size((50, 60)  )
              self.assertEqual(segmentation[0].shape,		lowerCAmelCase  )

              lowerCamelCase_							=image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase  )
              lowerCamelCase_							=torch.Size((32, 32)  )
              self.assertEqual(segmentation[0].shape,		lowerCAmelCase  )





"""simple docstring"""


import argparse

from transformers import (
    TapasConfig,
    TapasForMaskedLM,
    TapasForQuestionAnswering,
    TapasForSequenceClassification,
    TapasModel,
    TapasTokenizer,
    load_tf_weights_in_tapas,
)
from transformers.utils import logging


logging.set_verbosity_info()






def 			__UpperCAmelCase    (	lowercase      ,lowercase      ,lowercase      ,lowercase      ,lowercase						):







  """simple docstring"""
  # Initialise PyTorch model.
  # If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of
  # TapasConfig to False.

  # initialize configuration from json file
  _UpperCAmelCase         =					TapasConfig.from_json_file(lowercase						)
  # set absolute/relative position embeddings parameter
  _UpperCAmelCase         =					reset_position_index_per_cell

  # set remaining parameters of TapasConfig as well as the model based on the task
  if task == "SQA":
    _UpperCAmelCase         =					TapasForQuestionAnswering(config=lowercase						)
  elif task == "WTQ":
    # run_task_main.py hparams
    _UpperCAmelCase         =					4
    _UpperCAmelCase         =					True
    # hparam_utils.py hparams
    _UpperCAmelCase         =					0.66_46_94
    _UpperCAmelCase         =					0.20_79_51
    _UpperCAmelCase         =					0.12_11_94
    _UpperCAmelCase         =					True
    _UpperCAmelCase         =					True
    _UpperCAmelCase         =					False
    _UpperCAmelCase         =					0.0_35_25_13

    _UpperCAmelCase         =					TapasForQuestionAnswering(config=lowercase						)
  elif task == "WIKISQL_SUPERVISED":
    # run_task_main.py hparams
    _UpperCAmelCase         =					4
    _UpperCAmelCase         =					False
    # hparam_utils.py hparams
    _UpperCAmelCase         =					36.45_19
    _UpperCAmelCase         =					0.90_34_21
    _UpperCAmelCase         =					2_22.0_88
    _UpperCAmelCase         =					True
    _UpperCAmelCase         =					True
    _UpperCAmelCase         =					True
    _UpperCAmelCase         =					0.76_31_41

    _UpperCAmelCase         =					TapasForQuestionAnswering(config=lowercase						)
  elif task == "TABFACT":
    _UpperCAmelCase         =					TapasForSequenceClassification(config=lowercase						)
  elif task == "MLM":
    _UpperCAmelCase         =					TapasForMaskedLM(config=lowercase						)
  elif task == "INTERMEDIATE_PRETRAINING":
    _UpperCAmelCase         =					TapasModel(config=lowercase						)
  else:
    raise ValueError(f'''Task {task} not supported.'''						)

  print(f'''Building PyTorch model from configuration: {config}'''						)
  # Load weights from tf checkpoint
  load_tf_weights_in_tapas(lowercase      ,lowercase      ,lowercase						)

  # Save pytorch-model (weights and configuration)
  print(f'''Save PyTorch model to {pytorch_dump_path}'''						)
  model.save_pretrained(lowercase						)

  # Save tokenizer files
  print(f'''Save tokenizer files to {pytorch_dump_path}'''						)
  _UpperCAmelCase         =					TapasTokenizer(vocab_file=tf_checkpoint_path[:-10] + """vocab.txt"""      ,model_max_length=5_12						)
  tokenizer.save_pretrained(lowercase						)

  print("""Used relative position embeddings:"""      ,model.config.reset_position_index_per_cell						)


if __name__ == "__main__":
    UpperCAmelCase__     						=  argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        """--task""", default="""SQA""", type=str, help="""Model task for which to convert a checkpoint. Defaults to SQA."""
    )
    parser.add_argument(
        """--reset_position_index_per_cell""",
        default=False,
        action="""store_true""",
        help="""Whether to use relative position embeddings or not. Defaults to True.""",
    )
    parser.add_argument(
        """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
    )
    parser.add_argument(
        """--tapas_config_file""",
        default=None,
        type=str,
        required=True,
        help=(
            """The config json file corresponding to the pre-trained TAPAS model. \n"""
            """This specifies the model architecture."""
        ),
    )
    parser.add_argument(
        """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
    )
    UpperCAmelCase__     						=  parser.parse_args()
    convert_tf_checkpoint_to_pytorch(
        args.task,
        args.reset_position_index_per_cell,
        args.tf_checkpoint_path,
        args.tapas_config_file,
        args.pytorch_dump_path,
    )
