





import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    BertTokenizer,
    ViltConfig,
    ViltForImageAndTextRetrieval,
    ViltForImagesAndTextClassification,
    ViltForMaskedLM,
    ViltForQuestionAnswering,
    ViltImageProcessor,
    ViltProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
snake_case_							=			logging.get_logger(__name__)


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_=False							,       SCREAMING_SNAKE_CASE_=False							,       SCREAMING_SNAKE_CASE_=False		)   ->     Optional[Any]:
		lowerCamelCase   : str								=[]
		for i in range(config.num_hidden_layers		):
				# encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
				rename_keys.append((F"transformer.blocks.{i}.norm1.weight", F"vilt.encoder.layer.{i}.layernorm_before.weight")		)
				rename_keys.append((F"transformer.blocks.{i}.norm1.bias", F"vilt.encoder.layer.{i}.layernorm_before.bias")		)
				rename_keys.append(
				    (F"transformer.blocks.{i}.attn.proj.weight", F"vilt.encoder.layer.{i}.attention.output.dense.weight")		)
				rename_keys.append(
				    (F"transformer.blocks.{i}.attn.proj.bias", F"vilt.encoder.layer.{i}.attention.output.dense.bias")		)
				rename_keys.append((F"transformer.blocks.{i}.norm2.weight", F"vilt.encoder.layer.{i}.layernorm_after.weight")		)
				rename_keys.append((F"transformer.blocks.{i}.norm2.bias", F"vilt.encoder.layer.{i}.layernorm_after.bias")		)
				rename_keys.append(
				    (F"transformer.blocks.{i}.mlp.fc1.weight", F"vilt.encoder.layer.{i}.intermediate.dense.weight")		)
				rename_keys.append((F"transformer.blocks.{i}.mlp.fc1.bias", F"vilt.encoder.layer.{i}.intermediate.dense.bias")		)
				rename_keys.append((F"transformer.blocks.{i}.mlp.fc2.weight", F"vilt.encoder.layer.{i}.output.dense.weight")		)
				rename_keys.append((F"transformer.blocks.{i}.mlp.fc2.bias", F"vilt.encoder.layer.{i}.output.dense.bias")		)

		# embeddings
		rename_keys.extend(
		    [
		        # text embeddings
		        ('''text_embeddings.word_embeddings.weight''', '''vilt.embeddings.text_embeddings.word_embeddings.weight'''),
		        (
		            '''text_embeddings.position_embeddings.weight''',
		            '''vilt.embeddings.text_embeddings.position_embeddings.weight''',
		        ),
		        ('''text_embeddings.position_ids''', '''vilt.embeddings.text_embeddings.position_ids'''),
		        (
		            '''text_embeddings.token_type_embeddings.weight''',
		            '''vilt.embeddings.text_embeddings.token_type_embeddings.weight''',
		        ),
		        ('''text_embeddings.LayerNorm.weight''', '''vilt.embeddings.text_embeddings.LayerNorm.weight'''),
		        ('''text_embeddings.LayerNorm.bias''', '''vilt.embeddings.text_embeddings.LayerNorm.bias'''),
		        # patch embeddings
		        ('''transformer.cls_token''', '''vilt.embeddings.cls_token'''),
		        ('''transformer.patch_embed.proj.weight''', '''vilt.embeddings.patch_embeddings.projection.weight'''),
		        ('''transformer.patch_embed.proj.bias''', '''vilt.embeddings.patch_embeddings.projection.bias'''),
		        ('''transformer.pos_embed''', '''vilt.embeddings.position_embeddings'''),
		        # token type embeddings
		        ('''token_type_embeddings.weight''', '''vilt.embeddings.token_type_embeddings.weight'''),
		    ]		)

		# final layernorm + pooler
		rename_keys.extend(
		    [
		        ('''transformer.norm.weight''', '''vilt.layernorm.weight'''),
		        ('''transformer.norm.bias''', '''vilt.layernorm.bias'''),
		        ('''pooler.dense.weight''', '''vilt.pooler.dense.weight'''),
		        ('''pooler.dense.bias''', '''vilt.pooler.dense.bias'''),
		    ]		)

		# classifier head(s)
		if vqa_model:
				# classification head
				rename_keys.extend(
				    [
				        ('''vqa_classifier.0.weight''', '''classifier.0.weight'''),
				        ('''vqa_classifier.0.bias''', '''classifier.0.bias'''),
				        ('''vqa_classifier.1.weight''', '''classifier.1.weight'''),
				        ('''vqa_classifier.1.bias''', '''classifier.1.bias'''),
				        ('''vqa_classifier.3.weight''', '''classifier.3.weight'''),
				        ('''vqa_classifier.3.bias''', '''classifier.3.bias'''),
				    ]		)
		elif nlvr_model:
				# classification head
				rename_keys.extend(
				    [
				        ('''nlvr2_classifier.0.weight''', '''classifier.0.weight'''),
				        ('''nlvr2_classifier.0.bias''', '''classifier.0.bias'''),
				        ('''nlvr2_classifier.1.weight''', '''classifier.1.weight'''),
				        ('''nlvr2_classifier.1.bias''', '''classifier.1.bias'''),
				        ('''nlvr2_classifier.3.weight''', '''classifier.3.weight'''),
				        ('''nlvr2_classifier.3.bias''', '''classifier.3.bias'''),
				    ]		)
		else:
				pass

		return rename_keys


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Optional[int]:







		for i in range(config.num_hidden_layers		):
				lowerCamelCase   : str								='vilt.'
				# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
				lowerCamelCase   : Optional[int]								=state_dict.pop(F"transformer.blocks.{i}.attn.qkv.weight"		)
				lowerCamelCase   : str								=state_dict.pop(F"transformer.blocks.{i}.attn.qkv.bias"		)
				# next, add query, keys and values (in that order) to the state dict
				lowerCamelCase   : List[str]								=in_proj_weight[
				    : config.hidden_size, :
				]
				lowerCamelCase   : List[str]								=in_proj_bias[: config.hidden_size]
				lowerCamelCase   : Optional[int]								=in_proj_weight[
				    config.hidden_size : config.hidden_size * 2, :
				]
				lowerCamelCase   : Union[str, Any]								=in_proj_bias[
				    config.hidden_size : config.hidden_size * 2
				]
				lowerCamelCase   : Optional[Any]								=in_proj_weight[
				    -config.hidden_size :, :
				]
				lowerCamelCase   : str								=in_proj_bias[-config.hidden_size :]


def 					A__	(       SCREAMING_SNAKE_CASE_		)   ->     Union[str, Any]:
		lowerCamelCase   : Dict								=['head.weight', 'head.bias']
		for k in ignore_keys:
				state_dict.pop(lowerCAmelCase__							,       lowerCAmelCase__		)


def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Union[str, Any]:
		lowerCamelCase   : Optional[int]								=dct.pop(lowerCAmelCase__		)
		lowerCamelCase   : Tuple								=val


@torch.no_grad()
def 					A__	(       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_		)   ->     Dict:
		lowerCamelCase   : Tuple								=ViltConfig(image_size=3_8_4							,       patch_size=3_2							,       tie_word_embeddings=lowerCAmelCase__		)
		lowerCamelCase   : str								=False
		lowerCamelCase   : Optional[Any]								=False
		lowerCamelCase   : Any								=False
		lowerCamelCase   : Dict								=False
		if "vqa" in checkpoint_url:
				lowerCamelCase   : str								=True
				lowerCamelCase   : Optional[Any]								=3_1_2_9
				lowerCamelCase   : str								='huggingface/label-files'
				lowerCamelCase   : Any								='vqa2-id2label.json'
				lowerCamelCase   : List[Any]								=json.load(open(hf_hub_download(lowerCAmelCase__							,       lowerCAmelCase__							,       repo_type='''dataset'''		)							,       '''r'''		)		)
				lowerCamelCase   : Dict								={int(lowerCAmelCase__		): v for k, v in idalabel.items()}
				lowerCamelCase   : Any								=idalabel
				lowerCamelCase   : List[str]								={v: k for k, v in idalabel.items()}
				lowerCamelCase   : Optional[Any]								=ViltForQuestionAnswering(lowerCAmelCase__		)
		elif "nlvr" in checkpoint_url:
				lowerCamelCase   : str								=True
				lowerCamelCase   : Any								=2
				lowerCamelCase   : Optional[int]								={0: 'False', 1: 'True'}
				lowerCamelCase   : Tuple								={v: k for k, v in config.idalabel.items()}
				lowerCamelCase   : Dict								=3
				lowerCamelCase   : str								=ViltForImagesAndTextClassification(lowerCAmelCase__		)
		elif "irtr" in checkpoint_url:
				lowerCamelCase   : str								=True
				lowerCamelCase   : List[Any]								=ViltForImageAndTextRetrieval(lowerCAmelCase__		)
		elif "mlm_itm" in checkpoint_url:
				lowerCamelCase   : List[Any]								=True
				lowerCamelCase   : Optional[int]								=ViltForMaskedLM(lowerCAmelCase__		)
		else:
				raise ValueError('''Unknown model type'''		)

		# load state_dict of original model, remove and rename some keys
		lowerCamelCase   : Optional[Any]								=torch.hub.load_state_dict_from_url(lowerCAmelCase__							,       map_location='''cpu'''		)['state_dict']
		lowerCamelCase   : Optional[int]								=create_rename_keys(lowerCAmelCase__							,       lowerCAmelCase__							,       lowerCAmelCase__							,       lowerCAmelCase__		)
		for src, dest in rename_keys:
				rename_key(lowerCAmelCase__							,       lowerCAmelCase__							,       lowerCAmelCase__		)
		read_in_q_k_v(lowerCAmelCase__							,       lowerCAmelCase__		)
		if mlm_model or irtr_model:
				lowerCamelCase   : str								=['itm_score.fc.weight', 'itm_score.fc.bias']
				for k in ignore_keys:
						state_dict.pop(lowerCAmelCase__							,       lowerCAmelCase__		)

    # load state dict into HuggingFace model
		model.eval()
		if mlm_model:
				lowerCamelCase   : Union[str, Any]								=model.load_state_dict(lowerCAmelCase__							,       strict=lowerCAmelCase__		)
				assert missing_keys == ["mlm_score.decoder.bias"]
		else:
				model.load_state_dict(lowerCAmelCase__		)

		# Define processor
		lowerCamelCase   : Tuple								=ViltImageProcessor(size=3_8_4		)
		lowerCamelCase   : Optional[int]								=BertTokenizer.from_pretrained('''bert-base-uncased'''		)
		lowerCamelCase   : int								=ViltProcessor(lowerCAmelCase__							,       lowerCAmelCase__		)

		# Forward pass on example inputs (image + text)
		if nlvr_model:
				lowerCamelCase   : str								=Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg'''							,       stream=lowerCAmelCase__		).raw		)
				lowerCamelCase   : Union[str, Any]								=Image.open(requests.get('''https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg'''							,       stream=lowerCAmelCase__		).raw		)
				lowerCamelCase   : int								=(
				    'The left image contains twice the number of dogs as the right image, and at least two dogs in total are'
				    ' standing.'
				)
				lowerCamelCase   : Optional[int]								=processor(lowerCAmelCase__							,       lowerCAmelCase__							,       return_tensors='''pt'''		)
				lowerCamelCase   : Optional[Any]								=processor(lowerCAmelCase__							,       lowerCAmelCase__							,       return_tensors='''pt'''		)
				lowerCamelCase   : Any								=model(
				    input_ids=encoding_a.input_ids							,       pixel_values=encoding_a.pixel_values							,       pixel_values_a=encoding_a.pixel_values							,       )
		else:
				lowerCamelCase   : List[Any]								=Image.open(requests.get('''http://images.cocodataset.org/val2017/000000039769.jpg'''							,       stream=lowerCAmelCase__		).raw		)
				if mlm_model:
						lowerCamelCase   : List[Any]								='a bunch of [MASK] laying on a [MASK].'
				else:
						lowerCamelCase   : str								='How many cats are there?'
				lowerCamelCase   : Optional[Any]								=processor(lowerCAmelCase__							,       lowerCAmelCase__							,       return_tensors='''pt'''		)
				lowerCamelCase   : Union[str, Any]								=model(**lowerCAmelCase__		)

		# Verify outputs
		if mlm_model:
				lowerCamelCase   : List[Any]								=torch.Size([1, 1_1, 3_0_5_2_2]		)
				lowerCamelCase   : Union[str, Any]								=torch.tensor([-1_2.5_0_6_1, -1_2.5_1_2_3, -1_2.5_1_7_4]		)
				assert outputs.logits.shape == expected_shape
				assert torch.allclose(outputs.logits[0, 0, :3]							,       lowerCAmelCase__							,       atol=1E-4		)

				# verify masked token prediction equals "cats"
				lowerCamelCase   : Any								=outputs.logits[0, 4, :].argmax(-1		).item()
				assert tokenizer.decode([predicted_id]		) == "cats"
		elif vqa_model:
				lowerCamelCase   : Dict								=torch.Size([1, 3_1_2_9]		)
				lowerCamelCase   : List[Any]								=torch.tensor([-1_5.9_4_9_5, -1_8.1_4_7_2, -1_0.3_0_4_1]		)
				assert torch.allclose(outputs.logits[0, :3]							,       lowerCAmelCase__							,       atol=1E-4		)
				assert outputs.logits.shape == expected_shape
				assert torch.allclose(outputs.logits[0, 0, :3]							,       lowerCAmelCase__							,       atol=1E-4		)

				# verify vqa prediction equals "2"
				lowerCamelCase   : Dict								=outputs.logits.argmax(-1		).item()
				assert model.config.idalabel[predicted_idx] == "2"
		elif nlvr_model:
				lowerCamelCase   : str								=torch.Size([1, 2]		)
				lowerCamelCase   : Union[str, Any]								=torch.tensor([-2.8_7_2_1, 2.1_2_9_1]		)
				assert torch.allclose(outputs.logits[0, :3]							,       lowerCAmelCase__							,       atol=1E-4		)
				assert outputs.logits.shape == expected_shape

		Path(lowerCAmelCase__		).mkdir(exist_ok=lowerCAmelCase__		)
		print(F"Saving model and processor to {pytorch_dump_folder_path}"		)
		model.save_pretrained(lowerCAmelCase__		)
		processor.save_pretrained(lowerCAmelCase__		)


if __name__ == "__main__":
				snake_case_							=			argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    '''--checkpoint_url''',
				    default='''https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt''',
				    type=str,
				    help='''URL of the checkpoint you\'d like to convert.''',
				)
				parser.add_argument(
				    '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.'''
				)

				snake_case_							=			parser.parse_args()
				convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)








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        snake_case_		(			_A):







		def 			__lowercase   (		self   )				->   Optional[Any]:
				lowerCamelCase   : Optional[Any]								=self.config_class(**self.inputs_dict   )
				self.parent.assertTrue(hasattr(__lowercase			,     '''tf_padding'''   )   )
				self.parent.assertTrue(hasattr(__lowercase			,     '''depth_multiplier'''   )   )

class        snake_case_		:







		def __init__(		self			,     __lowercase			,     __lowercase=1_3			,     __lowercase=3			,     __lowercase=3_2			,     __lowercase=0.2_5			,     __lowercase=8			,     __lowercase=8			,     __lowercase=6			,     __lowercase=3_2			,     __lowercase=True			,     __lowercase=True			,     __lowercase=True			,     __lowercase="relu6"			,     __lowercase=1_2_8_0			,     __lowercase=0.1			,     __lowercase=0.0_2			,     __lowercase=True			,     __lowercase=True			,     __lowercase=1_0			,     __lowercase=None			,     )				->   int:
				lowerCamelCase   : Union[str, Any]								=parent
				lowerCamelCase   : Union[str, Any]								=batch_size
				lowerCamelCase   : int								=num_channels
				lowerCamelCase   : str								=image_size
				lowerCamelCase   : List[Any]								=depth_multiplier
				lowerCamelCase   : Dict								=depth_divisible_by
				lowerCamelCase   : Optional[Any]								=min_depth
				lowerCamelCase   : Optional[Any]								=expand_ratio
				lowerCamelCase   : List[str]								=tf_padding
				lowerCamelCase   : int								=output_stride
				lowerCamelCase   : Optional[Any]								=first_layer_is_expansion
				lowerCamelCase   : List[Any]								=finegrained_output
				lowerCamelCase   : int								=hidden_act
				lowerCamelCase   : List[str]								=last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier   )
				lowerCamelCase   : str								=classifier_dropout_prob
				lowerCamelCase   : int								=use_labels
				lowerCamelCase   : Optional[int]								=is_training
				lowerCamelCase   : int								=num_labels
				lowerCamelCase   : Dict								=initializer_range
				lowerCamelCase   : Tuple								=scope







		def 			__lowercase   (		self   )				->   List[str]:
				lowerCamelCase   : Tuple								=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

				lowerCamelCase   : Optional[Any]								=self.get_config()

				return config, pixel_values, labels, pixel_labels







		def 			__lowercase   (		self   )				->   Optional[Any]:
				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 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     __lowercase   )				->   Dict:
				lowerCamelCase   : Optional[Any]								=MobileNetVaModel(config=__lowercase   )
				model.to(__lowercase   )
				model.eval()
				lowerCamelCase   : int								=model(__lowercase   )
				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 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     __lowercase   )				->   List[Any]:
				lowerCamelCase   : Optional[Any]								=self.num_labels
				lowerCamelCase   : Optional[int]								=MobileNetVaForImageClassification(__lowercase   )
				model.to(__lowercase   )
				model.eval()
				lowerCamelCase   : Any								=model(__lowercase			,     labels=__lowercase   )
				self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     __lowercase   )				->   str:
				lowerCamelCase   : int								=self.num_labels
				lowerCamelCase   : List[Any]								=MobileNetVaForSemanticSegmentation(__lowercase   )
				model.to(__lowercase   )
				model.eval()
				lowerCamelCase   : List[str]								=model(__lowercase   )
				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   : Union[str, Any]								=model(__lowercase			,     labels=__lowercase   )
				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   )				->   Optional[Any]:
				lowerCamelCase   : Any								=self.prepare_config_and_inputs()
				lowerCamelCase		,   lowerCamelCase		,   lowerCamelCase		,   lowerCamelCase   : str								=config_and_inputs
				lowerCamelCase   : Dict								={'''pixel_values''': pixel_values}
				return config, inputs_dict

@require_torch
class        snake_case_		(			_A						,					_A						,					unittest.TestCase):
		lowerCamelCase    :Union[str, Any]						=     (
		    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
		    if is_torch_available()
		    else ()
		)
		lowerCamelCase    :Any						=     (
		    {
		        "feature-extraction": MobileNetVaModel,
		        "image-classification": MobileNetVaForImageClassification,
		        "image-segmentation": MobileNetVaForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		lowerCamelCase    :List[str]						=     False
		lowerCamelCase    :Dict						=     False
		lowerCamelCase    :Any						=     False
		lowerCamelCase    :Dict						=     False







		def 			__lowercase   (		self   )				->   Any:
				lowerCamelCase   : Union[str, Any]								=MobileNetVaModelTester(self   )
				lowerCamelCase   : List[str]								=MobileNetVaConfigTester(self			,     config_class=__lowercase			,     has_text_modality=__lowercase   )







		def 			__lowercase   (		self   )				->   int:
				self.config_tester.run_common_tests()







		@unittest.skip(reason='''MobileNetV2 does not use inputs_embeds'''   )
		def 			__lowercase   (		self   )				->   Dict:
				pass







		@unittest.skip(reason='''MobileNetV2 does not support input and output embeddings'''   )
		def 			__lowercase   (		self   )				->   Optional[Any]:
				pass







		@unittest.skip(reason='''MobileNetV2 does not output attentions'''   )
		def 			__lowercase   (		self   )				->   List[Any]:
				pass







		def 			__lowercase   (		self   )				->   List[Any]:
				lowerCamelCase		,   lowerCamelCase   : Optional[Any]								=self.model_tester.prepare_config_and_inputs_for_common()

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

						lowerCamelCase   : Optional[Any]								=['''pixel_values''']
						self.assertListEqual(arg_names[:1]			,     __lowercase   )







		def 			__lowercase   (		self   )				->   Optional[Any]:
				lowerCamelCase   : Optional[Any]								=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*__lowercase   )







		def 			__lowercase   (		self   )				->   str:
				def check_hidden_states_output(__lowercase			,     __lowercase			,     __lowercase   ):
						lowerCamelCase   : Dict								=model_class(__lowercase   )
						model.to(__lowercase   )
						model.eval()

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

						lowerCamelCase   : Union[str, Any]								=outputs.hidden_states

						lowerCamelCase   : Tuple								=1_6
						self.assertEqual(len(__lowercase   )			,     __lowercase   )

				lowerCamelCase		,   lowerCamelCase   : Union[str, Any]								=self.model_tester.prepare_config_and_inputs_for_common()

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

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowerCamelCase   : Optional[int]								=True

						check_hidden_states_output(__lowercase			,     __lowercase			,     __lowercase   )







		def 			__lowercase   (		self   )				->   List[Any]:
				lowerCamelCase   : List[str]								=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*__lowercase   )







		def 			__lowercase   (		self   )				->   int:
				lowerCamelCase   : Any								=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*__lowercase   )







		@slow
		def 			__lowercase   (		self   )				->   List[Any]:
				for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowerCamelCase   : Dict								=MobileNetVaModel.from_pretrained(__lowercase   )
						self.assertIsNotNone(__lowercase   )


def 					A__	(       )   ->     List[Any]:
		lowerCamelCase   : List[str]								=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
		return image



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







		@cached_property
		def 			__lowercase   (		self   )				->   int:
				return (
				    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224'''   ) if is_vision_available() else None
				)







		@slow
		def 			__lowercase   (		self   )				->   int:
				lowerCamelCase   : Tuple								=MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''   ).to(__lowercase   )

				lowerCamelCase   : Dict								=self.default_image_processor
				lowerCamelCase   : Dict								=prepare_img()
				lowerCamelCase   : List[Any]								=image_processor(images=__lowercase			,     return_tensors='''pt'''   ).to(__lowercase   )

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

				# verify the logits
				lowerCamelCase   : Optional[Any]								=torch.Size((1, 1_0_0_1)   )
				self.assertEqual(outputs.logits.shape			,     __lowercase   )

				lowerCamelCase   : Optional[Any]								=torch.tensor([0.2_4_4_5, -1.1_9_9_3, 0.1_9_0_5]   ).to(__lowercase   )

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







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

				lowerCamelCase   : List[Any]								=MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''   )

				lowerCamelCase   : Any								=prepare_img()
				lowerCamelCase   : Dict								=image_processor(images=__lowercase			,     return_tensors='''pt'''   ).to(__lowercase   )

				# forward pass
				with torch.no_grad():
						lowerCamelCase   : Optional[Any]								=model(**__lowercase   )
				lowerCamelCase   : List[str]								=outputs.logits

				# verify the logits
				lowerCamelCase   : int								=torch.Size((1, 2_1, 6_5, 6_5)   )
				self.assertEqual(logits.shape			,     __lowercase   )

				lowerCamelCase   : List[Any]								=torch.tensor(
				    [
				        [[1_7.5_7_9_0, 1_7.7_5_8_1, 1_8.3_3_5_5], [1_8.3_2_5_7, 1_8.4_2_3_0, 1_8.8_9_7_3], [1_8.6_1_6_9, 1_8.8_6_5_0, 1_9.2_1_8_7]],
				        [[-2.1_5_9_5, -2.0_9_7_7, -2.3_7_4_1], [-2.4_2_2_6, -2.3_0_2_8, -2.6_8_3_5], [-2.7_8_1_9, -2.5_9_9_1, -2.7_7_0_6]],
				        [[4.2_0_5_8, 4.8_3_1_7, 4.7_6_3_8], [4.4_1_3_6, 5.0_3_6_1, 4.9_3_8_3], [4.5_0_2_8, 4.9_6_4_4, 4.8_7_3_4]],
				    ]			,     device=__lowercase			,     )

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

