


import inspect
import unittest

from transformers import MobileViTVaConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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 MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, MobileViTVaModel
							from transformers.models.mobilevitva.modeling_mobilevitva import (
							    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
							    make_divisible,
							)


if is_vision_available():
							from PIL import Image

							from transformers import MobileViTImageProcessor

class 						A__							(		UpperCamelCase__  ):




						def 							__UpperCamelCase				(       self					:      List[str]  )							->     Optional[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.config_class(**self.inputs_dict  )
										self.parent.assertTrue(hasattr(_a				,    '''width_multiplier'''  )  )

class 						A__							:




						def __init__(       self					:      int				,    _a					:      List[str]				,    _a					:      Union[str, Any]=13				,    _a					:      Tuple=64				,    _a					:      int=2				,    _a					:      Any=3				,    _a					:      List[str]="swish"				,    _a					:      Optional[int]=3				,    _a					:      str=32				,    _a					:      int=0.1				,    _a					:      int=0.02				,    _a					:      Any=True				,    _a					:      Union[str, Any]=True				,    _a					:      int=10				,    _a					:      int=None				,    _a					:      Any=0.25				,    _a					:      Union[str, Any]=0.0				,    _a					:      Any=0.0				,    )							->     Dict:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =parent
										_SCREAMING_SNAKE_CASE			  =batch_size
										_SCREAMING_SNAKE_CASE			  =image_size
										_SCREAMING_SNAKE_CASE			  =patch_size
										_SCREAMING_SNAKE_CASE			  =num_channels
										_SCREAMING_SNAKE_CASE			  =make_divisible(512 * width_multiplier				,    divisor=8  )
										_SCREAMING_SNAKE_CASE			  =hidden_act
										_SCREAMING_SNAKE_CASE			  =conv_kernel_size
										_SCREAMING_SNAKE_CASE			  =output_stride
										_SCREAMING_SNAKE_CASE			  =classifier_dropout_prob
										_SCREAMING_SNAKE_CASE			  =use_labels
										_SCREAMING_SNAKE_CASE			  =is_training
										_SCREAMING_SNAKE_CASE			  =num_labels
										_SCREAMING_SNAKE_CASE			  =initializer_range
										_SCREAMING_SNAKE_CASE			  =scope
										_SCREAMING_SNAKE_CASE			  =width_multiplier
										_SCREAMING_SNAKE_CASE			  =ffn_dropout
										_SCREAMING_SNAKE_CASE			  =attn_dropout




						def 							__UpperCamelCase				(       self					:      str  )							->     Dict:


										"""simple docstring"""





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

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

										_SCREAMING_SNAKE_CASE			  =self.get_config()

										return config, pixel_values, labels, pixel_labels




						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     Union[str, Any]:


										"""simple docstring"""





										return MobileViTVaConfig(
										    image_size=self.image_size				,    patch_size=self.patch_size				,    num_channels=self.num_channels				,    hidden_act=self.hidden_act				,    conv_kernel_size=self.conv_kernel_size				,    output_stride=self.output_stride				,    classifier_dropout_prob=self.classifier_dropout_prob				,    initializer_range=self.initializer_range				,    width_multiplier=self.width_multiplier				,    ffn_dropout=self.ffn_dropout_prob				,    attn_dropout=self.attn_dropout_prob				,    )




						def 							__UpperCamelCase				(       self					:      Dict				,    _a					:      str				,    _a					:      int				,    _a					:      str				,    _a					:      Dict  )							->     Optional[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MobileViTVaModel(config=_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a  )
										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 							__UpperCamelCase				(       self					:      List[Any]				,    _a					:      List[Any]				,    _a					:      Optional[Any]				,    _a					:      Optional[Any]				,    _a					:      Dict  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.num_labels
										_SCREAMING_SNAKE_CASE			  =MobileViTVaForImageClassification(_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a				,    labels=_a  )
										self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)  )




						def 							__UpperCamelCase				(       self					:      str				,    _a					:      Tuple				,    _a					:      List[Any]				,    _a					:      Tuple				,    _a					:      Any  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.num_labels
										_SCREAMING_SNAKE_CASE			  =MobileViTVaForSemanticSegmentation(_a  )
										model.to(_a  )
										model.eval()
										_SCREAMING_SNAKE_CASE			  =model(_a  )
										self.parent.assertEqual(
										    result.logits.shape				,    (
										        self.batch_size,
										        self.num_labels,
										        self.image_size // self.output_stride,
										        self.image_size // self.output_stride,
										    )				,    )
										_SCREAMING_SNAKE_CASE			  =model(_a				,    labels=_a  )
										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 							__UpperCamelCase				(       self					:      int  )							->     Tuple:


										"""simple docstring"""





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

@require_torch
class 						A__							(		UpperCamelCase__   ,     UpperCamelCase__   ,     unittest.TestCase  ):
						UpperCAmelCase             =						(
						    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
						    if is_torch_available()
						    else ()
						)

						UpperCAmelCase             =						(
						    {
						        "feature-extraction": MobileViTVaModel,
						        "image-classification": MobileViTVaForImageClassification,
						        "image-segmentation": MobileViTVaForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False
						UpperCAmelCase             =						False




						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MobileViTVaModelTester(self  )
										_SCREAMING_SNAKE_CASE			  =MobileViTVaConfigTester(self				,    config_class=_a				,    has_text_modality=_a  )




						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     Optional[int]:


										"""simple docstring"""





										self.config_tester.run_common_tests()




						@unittest.skip(reason='''MobileViTV2 does not use inputs_embeds'''  )
						def 							__UpperCamelCase				(       self					:      Any  )							->     Optional[int]:


										"""simple docstring"""





										pass




						@unittest.skip(reason='''MobileViTV2 does not support input and output embeddings'''  )
						def 							__UpperCamelCase				(       self					:      str  )							->     Any:


										"""simple docstring"""





										pass




						@unittest.skip(reason='''MobileViTV2 does not output attentions'''  )
						def 							__UpperCamelCase				(       self					:      str  )							->     Optional[int]:


										"""simple docstring"""





										pass




						@require_torch_multi_gpu
						@unittest.skip(reason='''Got `CUDA error: misaligned address` for tests after this one being run.'''  )
						def 							__UpperCamelCase				(       self					:      List[str]  )							->     str:


										"""simple docstring"""





										pass




						@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     str:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase				(       self					:      List[Any]  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

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

														_SCREAMING_SNAKE_CASE			  =['''pixel_values''']
														self.assertListEqual(arg_names[:1]				,    _a  )




						def 							__UpperCamelCase				(       self					:      int  )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_a  )




						def 							__UpperCamelCase				(       self					:      Tuple  )							->     List[str]:


										"""simple docstring"""





										def check_hidden_states_output(_a					:      int				,    _a					:      int				,    _a					:      str  ):
														_SCREAMING_SNAKE_CASE			  =model_class(_a  )
														model.to(_a  )
														model.eval()

														with torch.no_grad():
																		_SCREAMING_SNAKE_CASE			  =model(**self._prepare_for_class(_a				,    _a  )  )

														_SCREAMING_SNAKE_CASE			  =outputs.hidden_states

														_SCREAMING_SNAKE_CASE			  =5
														self.assertEqual(len(_a  )				,    _a  )

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

										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_SCREAMING_SNAKE_CASE			  =True
														check_hidden_states_output(_a				,    _a				,    _a  )

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

														check_hidden_states_output(_a				,    _a				,    _a  )




						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_a  )




						def 							__UpperCamelCase				(       self					:      Tuple  )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*_a  )




						@slow
						def 							__UpperCamelCase				(       self					:      Tuple  )							->     List[str]:


										"""simple docstring"""





										for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_SCREAMING_SNAKE_CASE			  =MobileViTVaModel.from_pretrained(_a  )
														self.assertIsNotNone(_a  )







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


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




						@cached_property
						def 							__UpperCamelCase				(       self					:      int  )							->     Tuple:


										"""simple docstring"""





										return (
										    MobileViTImageProcessor.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''  )
										    if is_vision_available()
										    else None
										)




						@slow
						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MobileViTVaForImageClassification.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''  ).to(
										    _a  )

										_SCREAMING_SNAKE_CASE			  =self.default_image_processor
										_SCREAMING_SNAKE_CASE			  =prepare_img()
										_SCREAMING_SNAKE_CASE			  =image_processor(images=_a				,    return_tensors='''pt'''  ).to(_a  )

										# forward pass
										with torch.no_grad():
														_SCREAMING_SNAKE_CASE			  =model(**_a  )

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

										_SCREAMING_SNAKE_CASE			  =torch.tensor([-1.63_36E00, -7.32_04E-02, -5.18_83E-01]  ).to(_a  )

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




						@slow
						def 							__UpperCamelCase				(       self					:      str  )							->     Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''  )
										_SCREAMING_SNAKE_CASE			  =model.to(_a  )

										_SCREAMING_SNAKE_CASE			  =MobileViTImageProcessor.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''  )

										_SCREAMING_SNAKE_CASE			  =prepare_img()
										_SCREAMING_SNAKE_CASE			  =image_processor(images=_a				,    return_tensors='''pt'''  ).to(_a  )

										# forward pass
										with torch.no_grad():
														_SCREAMING_SNAKE_CASE			  =model(**_a  )
										_SCREAMING_SNAKE_CASE			  =outputs.logits

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

										_SCREAMING_SNAKE_CASE			  =torch.tensor(
										    [
										        [[7.08_63, 7.15_25, 6.82_01], [6.69_31, 6.87_70, 6.89_33], [6.29_78, 7.03_66, 6.96_36]],
										        [[-3.71_34, -3.67_12, -3.66_75], [-3.58_25, -3.35_49, -3.47_77], [-3.34_35, -3.39_79, -3.28_57]],
										        [[-2.93_29, -2.80_03, -2.73_69], [-3.05_64, -2.47_80, -2.02_07], [-2.68_89, -1.92_98, -1.76_40]],
										    ]				,    device=_a				,    )

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




						@slow
						def 							__UpperCamelCase				(       self					:      List[str]  )							->     Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''  )
										_SCREAMING_SNAKE_CASE			  =model.to(_a  )

										_SCREAMING_SNAKE_CASE			  =MobileViTImageProcessor.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''  )

										_SCREAMING_SNAKE_CASE			  =prepare_img()
										_SCREAMING_SNAKE_CASE			  =image_processor(images=_a				,    return_tensors='''pt'''  ).to(_a  )

										# forward pass
										with torch.no_grad():
														_SCREAMING_SNAKE_CASE			  =model(**_a  )

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

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

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



import os
import unittest

from transformers import LxmertTokenizer, LxmertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class 						A__							(		UpperCamelCase__   ,     unittest.TestCase  ):
						UpperCAmelCase             =						LxmertTokenizer
						UpperCAmelCase             =						LxmertTokenizerFast
						UpperCAmelCase             =						True
						UpperCAmelCase             =						True




						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     Tuple:


										"""simple docstring"""





										super().setUp()

										_SCREAMING_SNAKE_CASE			  =[
										    '''[UNK]''',
										    '''[CLS]''',
										    '''[SEP]''',
										    '''want''',
										    '''##want''',
										    '''##ed''',
										    '''wa''',
										    '''un''',
										    '''runn''',
										    '''##ing''',
										    ''',''',
										    '''low''',
										    '''lowest''',
										]
										_SCREAMING_SNAKE_CASE			  =os.path.join(self.tmpdirname				,    VOCAB_FILES_NAMES['''vocab_file''']  )
										with open(self.vocab_file				,    '''w'''				,    encoding='''utf-8'''  ) as vocab_writer:
														vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]  )  )




						def 							__UpperCamelCase				(       self					:      List[str]				,    _a					:      Tuple  )							->     Optional[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  ='''UNwant\u00E9d,running'''
										_SCREAMING_SNAKE_CASE			  ='''unwanted, running'''
										return input_text, output_text




						def 							__UpperCamelCase				(       self					:      Tuple  )							->     List[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.tokenizer_class(self.vocab_file  )

										_SCREAMING_SNAKE_CASE			  =tokenizer.tokenize('''UNwant\u00E9d,running'''  )
										self.assertListEqual(_a				,    ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing''']  )
										self.assertListEqual(tokenizer.convert_tokens_to_ids(_a  )				,    [7, 4, 5, 10, 8, 9]  )




						def 							__UpperCamelCase				(       self					:      Dict  )							->     List[Any]:


										"""simple docstring"""





										if not self.test_rust_tokenizer:
														return

										_SCREAMING_SNAKE_CASE			  =self.get_tokenizer()
										_SCREAMING_SNAKE_CASE			  =self.get_rust_tokenizer()

										_SCREAMING_SNAKE_CASE			  ='''I was born in 92000, and this is falsé.'''

										_SCREAMING_SNAKE_CASE			  =tokenizer.tokenize(_a  )
										_SCREAMING_SNAKE_CASE			  =rust_tokenizer.tokenize(_a  )
										self.assertListEqual(_a				,    _a  )

										_SCREAMING_SNAKE_CASE			  =tokenizer.encode(_a				,    add_special_tokens=_a  )
										_SCREAMING_SNAKE_CASE			  =rust_tokenizer.encode(_a				,    add_special_tokens=_a  )
										self.assertListEqual(_a				,    _a  )

										_SCREAMING_SNAKE_CASE			  =self.get_rust_tokenizer()
										_SCREAMING_SNAKE_CASE			  =tokenizer.encode(_a  )
										_SCREAMING_SNAKE_CASE			  =rust_tokenizer.encode(_a  )
										self.assertListEqual(_a				,    _a  )