

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 					__lowercase  (						_SCREAMING_SNAKE_CASE				):





							"""simple docstring"""

							def       snake_case			(					self						)		->       Union[str, Any]:
														A      :					Optional[int]      		=					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 					__lowercase  :





							"""simple docstring"""

							def __init__(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase=13    ,						__UpperCAmelCase=32    ,						__UpperCAmelCase=2    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=6_40    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase="silu"    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=32    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.0_2    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=10    ,						__UpperCAmelCase=None    ,						)		->       Optional[Any]:
														A      :					Union[str, Any]      		=					parent
														A      :					int      		=					batch_size
														A      :					Tuple      		=					image_size
														A      :					Dict      		=					patch_size
														A      :					Dict      		=					num_channels
														A      :					List[str]      		=					last_hidden_size
														A      :					Any      		=					num_attention_heads
														A      :					Dict      		=					hidden_act
														A      :					str      		=					conv_kernel_size
														A      :					Tuple      		=					output_stride
														A      :					str      		=					hidden_dropout_prob
														A      :					Union[str, Any]      		=					attention_probs_dropout_prob
														A      :					str      		=					classifier_dropout_prob
														A      :					Optional[int]      		=					use_labels
														A      :					str      		=					is_training
														A      :					Union[str, Any]      		=					num_labels
														A      :					List[Any]      		=					initializer_range
														A      :					int      		=					scope




							def       snake_case			(					self						)		->       Optional[Any]:
														A      :					int      		=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

														A      :					int      		=					None
														A      :					Optional[Any]      		=					None
														if self.use_labels:
																					A      :					Union[str, Any]      		=					ids_tensor([self.batch_size]    ,						self.num_labels						)
																					A      :					Union[str, Any]      		=					ids_tensor([self.batch_size, self.image_size, self.image_size]    ,						self.num_labels						)

														A      :					int      		=					self.get_config()

														return config, pixel_values, labels, pixel_labels




							def       snake_case			(					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       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)		->       Any:
														A      :					Union[str, Any]      		=					MobileViTModel(config=__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()
														A      :					Tuple      		=					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       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)		->       Tuple:
														A      :					List[str]      		=					self.num_labels
														A      :					Tuple      		=					MobileViTForImageClassification(__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()
														A      :					Tuple      		=					model(__UpperCAmelCase    ,						labels=__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)		->       List[Any]:
														A      :					Any      		=					self.num_labels
														A      :					Dict      		=					MobileViTForSemanticSegmentation(__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()
														A      :					Dict      		=					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,
														    )    ,						)
														A      :					Union[str, Any]      		=					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       snake_case			(					self						)		->       Tuple:
														A      :					Any      		=					self.prepare_config_and_inputs()
														A		,		A		,		A		,		A      :					Dict      		=					config_and_inputs
														A      :					Any      		=					{'''pixel_values''': pixel_values}
														return config, inputs_dict







@require_torch
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Any								=	(
							    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase_						:		int								=	(
							    {
							        '''feature-extraction''': MobileViTModel,
							        '''image-classification''': MobileViTForImageClassification,
							        '''image-segmentation''': MobileViTForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							UpperCAmelCase_						:		Optional[Any]								=	False
							UpperCAmelCase_						:		Tuple								=	False
							UpperCAmelCase_						:		Dict								=	False
							UpperCAmelCase_						:		Any								=	False




							def       snake_case			(					self						)		->       str:
														A      :					str      		=					MobileViTModelTester(self						)
														A      :					Dict      		=					MobileViTConfigTester(self    ,						config_class=__UpperCAmelCase    ,						has_text_modality=__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Tuple:
														self.config_tester.run_common_tests()




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




							@unittest.skip(reason='''MobileViT does not support input and output embeddings'''						)
							def       snake_case			(					self						)		->       Tuple:
														pass




							@unittest.skip(reason='''MobileViT does not output attentions'''						)
							def       snake_case			(					self						)		->       str:
														pass




							def       snake_case			(					self						)		->       List[str]:
														A		,		A      :					Optional[Any]      		=					self.model_tester.prepare_config_and_inputs_for_common()

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

																					A      :					Union[str, Any]      		=					['''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       snake_case			(					self						)		->       Any:
														pass




							def       snake_case			(					self						)		->       Any:
														A      :					Any      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Union[str, Any]:
														def check_hidden_states_output(__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						):
																					A      :					Dict      		=					model_class(__UpperCAmelCase						)
																					model.to(__UpperCAmelCase						)
																					model.eval()

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

																					A      :					List[Any]      		=					outputs.hidden_states

																					A      :					Tuple      		=					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.
																					A      :					Dict      		=					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						)

														A		,		A      :					Tuple      		=					self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					A      :					Union[str, Any]      		=					True
																					check_hidden_states_output(__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)

																					# check that output_hidden_states also work using config
																					del inputs_dict["output_hidden_states"]
																					A      :					Union[str, Any]      		=					True

																					check_hidden_states_output(__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[int]:
														A      :					Tuple      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[int]:
														A      :					Optional[int]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCAmelCase						)






							@slow
							def       snake_case			(					self						)		->       Dict:
														for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					A      :					Optional[Any]      		=					MobileViTModel.from_pretrained(__UpperCAmelCase						)
																					self.assertIsNotNone(__UpperCAmelCase						)






def        snake_case__		(    ):
							A      :					Optional[int]      		=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
							return image







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





							"""simple docstring"""

							@cached_property
							def       snake_case			(					self						)		->       Optional[int]:
														return MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''						) if is_vision_available() else None




							@slow
							def       snake_case			(					self						)		->       Tuple:
														A      :					Dict      		=					MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''						).to(__UpperCAmelCase						)

														A      :					Any      		=					self.default_image_processor
														A      :					List[str]      		=					prepare_img()
														A      :					List[str]      		=					image_processor(images=__UpperCAmelCase    ,						return_tensors='''pt'''						).to(__UpperCAmelCase						)

														# forward pass
														with torch.no_grad():
																					A      :					Union[str, Any]      		=					model(**__UpperCAmelCase						)

														# verify the logits
														A      :					str      		=					torch.Size((1, 10_00)						)
														self.assertEqual(outputs.logits.shape    ,						__UpperCAmelCase						)

														A      :					str      		=					torch.tensor([-1.9_3_6_4, -1.2_3_2_7, -0.4_6_5_3]						).to(__UpperCAmelCase						)

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




							@slow
							def       snake_case			(					self						)		->       str:
														A      :					str      		=					MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)
														A      :					List[Any]      		=					model.to(__UpperCAmelCase						)

														A      :					int      		=					MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)

														A      :					str      		=					prepare_img()
														A      :					List[str]      		=					image_processor(images=__UpperCAmelCase    ,						return_tensors='''pt'''						).to(__UpperCAmelCase						)

														# forward pass
														with torch.no_grad():
																					A      :					Optional[Any]      		=					model(**__UpperCAmelCase						)
														A      :					Optional[int]      		=					outputs.logits

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

														A      :					int      		=					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=__UpperCAmelCase    ,						)

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






							@slow
							def       snake_case			(					self						)		->       Union[str, Any]:
														A      :					Tuple      		=					MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)
														A      :					str      		=					model.to(__UpperCAmelCase						)

														A      :					Union[str, Any]      		=					MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)

														A      :					str      		=					prepare_img()
														A      :					Union[str, Any]      		=					image_processor(images=__UpperCAmelCase    ,						return_tensors='''pt'''						).to(__UpperCAmelCase						)

														# forward pass
														with torch.no_grad():
																					A      :					Optional[Any]      		=					model(**__UpperCAmelCase						)

														A      :					Optional[int]      		=					outputs.logits.detach().cpu()

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

														A      :					Optional[Any]      		=					image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase						)
														A      :					int      		=					torch.Size((32, 32)						)
														self.assertEqual(segmentation[0].shape    ,						__UpperCAmelCase						)









import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_convbert import ConvBertTokenizer


lowercase			:				Optional[int]   =     logging.get_logger(__name__)

lowercase			:				Optional[Any]   =     {"vocab_file": "vocab.txt"}

lowercase			:				Optional[int]   =     {
    "vocab_file": {
        "YituTech/conv-bert-base": "https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt",
        "YituTech/conv-bert-medium-small": (
            "https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt"
        ),
        "YituTech/conv-bert-small": "https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt",
    }
}

lowercase			:				Tuple   =     {
    "YituTech/conv-bert-base": 5_12,
    "YituTech/conv-bert-medium-small": 5_12,
    "YituTech/conv-bert-small": 5_12,
}


lowercase			:				Optional[int]   =     {
    "YituTech/conv-bert-base": {"do_lower_case": True},
    "YituTech/conv-bert-medium-small": {"do_lower_case": True},
    "YituTech/conv-bert-small": {"do_lower_case": True},
}







class 					__lowercase  (						_SCREAMING_SNAKE_CASE				):





							"""simple docstring"""

							UpperCAmelCase_						:		int								=	VOCAB_FILES_NAMES
							UpperCAmelCase_						:		Optional[int]								=	PRETRAINED_VOCAB_FILES_MAP
							UpperCAmelCase_						:		List[Any]								=	PRETRAINED_INIT_CONFIGURATION
							UpperCAmelCase_						:		Optional[Any]								=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							UpperCAmelCase_						:		Optional[int]								=	ConvBertTokenizer




							def __init__(					self    ,						__UpperCAmelCase=None    ,						__UpperCAmelCase=None    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase="[UNK]"    ,						__UpperCAmelCase="[SEP]"    ,						__UpperCAmelCase="[PAD]"    ,						__UpperCAmelCase="[CLS]"    ,						__UpperCAmelCase="[MASK]"    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=None    ,						**__UpperCAmelCase    ,						)		->       Optional[int]:
														super().__init__(
														    __UpperCAmelCase    ,						tokenizer_file=__UpperCAmelCase    ,						do_lower_case=__UpperCAmelCase    ,						unk_token=__UpperCAmelCase    ,						sep_token=__UpperCAmelCase    ,						pad_token=__UpperCAmelCase    ,						cls_token=__UpperCAmelCase    ,						mask_token=__UpperCAmelCase    ,						tokenize_chinese_chars=__UpperCAmelCase    ,						strip_accents=__UpperCAmelCase    ,						**__UpperCAmelCase    ,						)

														A      :					Any      		=					json.loads(self.backend_tokenizer.normalizer.__getstate__()						)
														if (
														    normalizer_state.get('''lowercase'''    ,						__UpperCAmelCase						) != do_lower_case
														    or normalizer_state.get('''strip_accents'''    ,						__UpperCAmelCase						) != strip_accents
														    or normalizer_state.get('''handle_chinese_chars'''    ,						__UpperCAmelCase						) != tokenize_chinese_chars
														):
																					A      :					Tuple      		=					getattr(__UpperCAmelCase    ,						normalizer_state.pop('''type'''						)						)
																					A      :					int      		=					do_lower_case
																					A      :					int      		=					strip_accents
																					A      :					Union[str, Any]      		=					tokenize_chinese_chars
																					A      :					str      		=					normalizer_class(**__UpperCAmelCase						)

														A      :					str      		=					do_lower_case




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase=None						)		->       Optional[int]:
														A      :					Dict      		=					[self.cls_token_id] + token_ids_a + [self.sep_token_id]

														if token_ids_a:
																					output += token_ids_a + [self.sep_token_id]

														return output




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase = None						)		->       List[int]:
														A      :					List[Any]      		=					[self.sep_token_id]
														A      :					Optional[Any]      		=					[self.cls_token_id]
														if token_ids_a is None:
																					return len(cls + token_ids_a + sep						) * [0]
														return len(cls + token_ids_a + sep						) * [0] + len(token_ids_a + sep						) * [1]






							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase = None						)		->       Tuple[str]:
														A      :					Optional[int]      		=					self._tokenizer.model.save(__UpperCAmelCase    ,						name=__UpperCAmelCase						)
														return tuple(__UpperCAmelCase						)






