



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  __lowerCAmelCase     (	lowerCAmelCase__   ):




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.config_class(**self.inputs_dict				)
    self.parent.assertTrue(hasattr(__UpperCAmelCase	,				'''hidden_sizes'''				)				)
    self.parent.assertTrue(hasattr(__UpperCAmelCase	,				'''neck_hidden_sizes'''				)				)
    self.parent.assertTrue(hasattr(__UpperCAmelCase	,				'''num_attention_heads'''				)				)



class  __lowerCAmelCase     :




 def __init__(						self	,				__UpperCAmelCase	,				__UpperCAmelCase=13	,				__UpperCAmelCase=32	,				__UpperCAmelCase=2	,				__UpperCAmelCase=3	,				__UpperCAmelCase=640	,				__UpperCAmelCase=4	,				__UpperCAmelCase="silu"	,				__UpperCAmelCase=3	,				__UpperCAmelCase=32	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=0.02	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=10	,				__UpperCAmelCase=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        lowerCamelCase      (						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        lowerCamelCase      (						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        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    MobileViTModel(config=__UpperCAmelCase				)
    model.to(__UpperCAmelCase				)
    model.eval()
    __lowerCamelCase													=    model(__UpperCAmelCase				)
    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        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    self.num_labels
    __lowerCamelCase													=    MobileViTForImageClassification(__UpperCAmelCase				)
    model.to(__UpperCAmelCase				)
    model.eval()
    __lowerCamelCase													=    model(__UpperCAmelCase	,				labels=__UpperCAmelCase				)
    self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    self.num_labels
    __lowerCamelCase													=    MobileViTForSemanticSegmentation(__UpperCAmelCase				)
    model.to(__UpperCAmelCase				)
    model.eval()
    __lowerCamelCase													=    model(__UpperCAmelCase				)
    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(__UpperCAmelCase	,				labels=__UpperCAmelCase				)
    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        lowerCamelCase      (						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  __lowerCAmelCase     (	lowerCAmelCase__  ,	lowerCAmelCase__  ,	unittest.TestCase   ):
 lowerCAmelCase__       					=  (
     (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 lowerCAmelCase__       					=  (
     {
         """feature-extraction""": MobileViTModel,
         """image-classification""": MobileViTForImageClassification,
         """image-segmentation""": MobileViTForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    MobileViTModelTester(self				)
    __lowerCamelCase													=    MobileViTConfigTester(self	,				config_class=__UpperCAmelCase	,				has_text_modality=__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



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




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



    '''simple docstring'''
    pass




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



    '''simple docstring'''
    pass




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



    '''simple docstring'''
    pass




 def        lowerCamelCase      (						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(__UpperCAmelCase				)
       __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]	,				__UpperCAmelCase				)




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



    '''simple docstring'''
    pass




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    def check_hidden_states_output(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				):
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)
       model.to(__UpperCAmelCase				)
       model.eval()

       with torch.no_grad():
          __lowerCamelCase													=    model(**self._prepare_for_class(__UpperCAmelCase	,				__UpperCAmelCase				)				)

       __lowerCamelCase													=    outputs.hidden_states

       __lowerCamelCase													=    5
       self.assertEqual(len(__UpperCAmelCase				)	,				__UpperCAmelCase				)

       # 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(__UpperCAmelCase				)				):
          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(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				)

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

       check_hidden_states_output(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCAmelCase				)




 @slow
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCamelCase													=    MobileViTModel.from_pretrained(__UpperCAmelCase				)
       self.assertIsNotNone(__UpperCAmelCase				)





def 		a__			(		):
   __lowerCamelCase													=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
   return image





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




 @cached_property
 def        lowerCamelCase      (						self				):



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




 @slow
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''				).to(__UpperCAmelCase				)

    __lowerCamelCase													=    self.default_image_processor
    __lowerCamelCase													=    prepare_img()
    __lowerCamelCase													=    image_processor(images=__UpperCAmelCase	,				return_tensors='''pt'''				).to(__UpperCAmelCase				)

    # forward pass
    with torch.no_grad():
       __lowerCamelCase													=    model(**__UpperCAmelCase				)

    # verify the logits
    __lowerCamelCase													=    torch.Size((1, 1000)				)
    self.assertEqual(outputs.logits.shape	,				__UpperCAmelCase				)

    __lowerCamelCase													=    torch.tensor([-1.9_364, -1.2_327, -0.4_653]				).to(__UpperCAmelCase				)

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




 @slow
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''				)
    __lowerCamelCase													=    model.to(__UpperCAmelCase				)

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

    __lowerCamelCase													=    prepare_img()
    __lowerCamelCase													=    image_processor(images=__UpperCAmelCase	,				return_tensors='''pt'''				).to(__UpperCAmelCase				)

    # forward pass
    with torch.no_grad():
       __lowerCamelCase													=    model(**__UpperCAmelCase				)
    __lowerCamelCase													=    outputs.logits

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

    __lowerCamelCase													=    torch.tensor(
        [
            [[6.9_713, 6.9_786, 7.2_422], [7.2_893, 7.2_825, 7.4_446], [7.6_580, 7.8_797, 7.9_420]],
            [[-10.6_869, -10.3_250, -10.3_471], [-10.4_228, -9.9_868, -9.7_132], [-11.0_405, -11.0_221, -10.7_318]],
            [[-3.3_089, -2.8_539, -2.6_740], [-3.2_706, -2.5_621, -2.5_108], [-3.2_534, -2.6_615, -2.6_651]],
        ]	,				device=__UpperCAmelCase	,				)

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




 @slow
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''				)
    __lowerCamelCase													=    model.to(__UpperCAmelCase				)

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

    __lowerCamelCase													=    prepare_img()
    __lowerCamelCase													=    image_processor(images=__UpperCAmelCase	,				return_tensors='''pt'''				).to(__UpperCAmelCase				)

    # forward pass
    with torch.no_grad():
       __lowerCamelCase													=    model(**__UpperCAmelCase				)

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

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

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











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


a_			=					logging.get_logger(__name__)

a_			=					{
    """uclanlp/visualbert-vqa""": """https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json""",
    """uclanlp/visualbert-vqa-pre""": """https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json""",
    """uclanlp/visualbert-vqa-coco-pre""": (
        """https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json"""
    ),
    """uclanlp/visualbert-vcr""": """https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json""",
    """uclanlp/visualbert-vcr-pre""": """https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json""",
    """uclanlp/visualbert-vcr-coco-pre""": (
        """https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json"""
    ),
    """uclanlp/visualbert-nlvr2""": """https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json""",
    """uclanlp/visualbert-nlvr2-pre""": """https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json""",
    """uclanlp/visualbert-nlvr2-coco-pre""": (
        """https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json"""
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}



class  __lowerCAmelCase     (	lowerCAmelCase__   ):
 lowerCAmelCase__       					=  """visual_bert"""




 def __init__(						self	,				__UpperCAmelCase=30522	,				__UpperCAmelCase=768	,				__UpperCAmelCase=512	,				__UpperCAmelCase=12	,				__UpperCAmelCase=12	,				__UpperCAmelCase=3072	,				__UpperCAmelCase="gelu"	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=512	,				__UpperCAmelCase=2	,				__UpperCAmelCase=0.02	,				__UpperCAmelCase=1E-1_2	,				__UpperCAmelCase=False	,				__UpperCAmelCase=True	,				__UpperCAmelCase=1	,				__UpperCAmelCase=0	,				__UpperCAmelCase=2	,				**__UpperCAmelCase	,				):



    '''simple docstring'''
    super().__init__(pad_token_id=__UpperCAmelCase	,				bos_token_id=__UpperCAmelCase	,				eos_token_id=__UpperCAmelCase	,				**__UpperCAmelCase				)

    __lowerCamelCase													=    vocab_size
    __lowerCamelCase													=    max_position_embeddings
    __lowerCamelCase													=    hidden_size
    __lowerCamelCase													=    visual_embedding_dim
    __lowerCamelCase													=    num_hidden_layers
    __lowerCamelCase													=    num_attention_heads
    __lowerCamelCase													=    intermediate_size
    __lowerCamelCase													=    hidden_act
    __lowerCamelCase													=    hidden_dropout_prob
    __lowerCamelCase													=    attention_probs_dropout_prob
    __lowerCamelCase													=    initializer_range
    __lowerCamelCase													=    type_vocab_size
    __lowerCamelCase													=    layer_norm_eps
    __lowerCamelCase													=    bypass_transformer
    __lowerCamelCase													=    special_visual_initialize






