

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 							UpperCAmelCase_						(     __lowercase  ):
							"""simple docstring"""







							def 							__lowercase							(     self			)       ->       Dict:
												_a      :       int   =						self.config_class(**self.inputs_dict			)
												self.parent.assertTrue(hasattr(_a			,				'''width_multiplier'''			)			)



class 							UpperCAmelCase_						:
							"""simple docstring"""







							def __init__(     self			,				_a			,				_a=1_3			,				_a=6_4			,				_a=2			,				_a=3			,				_a="swish"			,				_a=3			,				_a=3_2			,				_a=0.1			,				_a=0.02			,				_a=True			,				_a=True			,				_a=1_0			,				_a=None			,				_a=0.25			,				_a=0.0			,				_a=0.0			,				)       ->       int:
												_a      :       List[Any]   =						parent
												_a      :       Union[str, Any]   =						batch_size
												_a      :       str   =						image_size
												_a      :       Optional[int]   =						patch_size
												_a      :       str   =						num_channels
												_a      :       Tuple   =						make_divisible(5_1_2 * width_multiplier			,				divisor=8			)
												_a      :       Optional[int]   =						hidden_act
												_a      :       List[Any]   =						conv_kernel_size
												_a      :       Union[str, Any]   =						output_stride
												_a      :       int   =						classifier_dropout_prob
												_a      :       str   =						use_labels
												_a      :       List[Any]   =						is_training
												_a      :       Optional[Any]   =						num_labels
												_a      :       List[Any]   =						initializer_range
												_a      :       List[Any]   =						scope
												_a      :       Optional[Any]   =						width_multiplier
												_a      :       Union[str, Any]   =						ffn_dropout
												_a      :       List[str]   =						attn_dropout


							def 							__lowercase							(     self			)       ->       Optional[int]:
												_a      :       Tuple   =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

												_a      :       int   =						None
												_a      :       Dict   =						None
												if self.use_labels:
																	_a      :       Any   =						ids_tensor([self.batch_size]			,				self.num_labels			)
																	_a      :       int   =						ids_tensor([self.batch_size, self.image_size, self.image_size]			,				self.num_labels			)

												_a      :       List[str]   =						self.get_config()

												return config, pixel_values, labels, pixel_labels


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


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



@require_torch
class 							UpperCAmelCase_						(     __lowercase							,			__lowercase							,			unittest.TestCase  ):
							"""simple docstring"""







							UpperCAmelCase__			:		Optional[int]             =       (
							    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)

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

							UpperCAmelCase__			:		int             =       False
							UpperCAmelCase__			:		Tuple             =       False
							UpperCAmelCase__			:		Tuple             =       False
							UpperCAmelCase__			:		List[Any]             =       False


							def 							__lowercase							(     self			)       ->       Tuple:
												_a      :       List[Any]   =						MobileViTVaModelTester(self			)
												_a      :       str   =						MobileViTVaConfigTester(self			,				config_class=_a			,				has_text_modality=_a			)


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


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


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


							@unittest.skip(reason='''MobileViTV2 does not output attentions'''			)
							def 							__lowercase							(     self			)       ->       Dict:
												pass


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


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


							def 							__lowercase							(     self			)       ->       List[str]:
												_a ,   _a      :       Dict   =						self.model_tester.prepare_config_and_inputs_for_common()

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

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


							def 							__lowercase							(     self			)       ->       str:
												_a      :       Union[str, Any]   =						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_a			)


							def 							__lowercase							(     self			)       ->       Optional[Any]:
												def check_hidden_states_output(_a			,				_a			,				_a			):
																	_a      :       Optional[int]   =						model_class(_a			)
																	model.to(_a			)
																	model.eval()

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

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

																	_a      :       Optional[int]   =						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.
																	_a      :       Union[str, Any]   =						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			)

												_a ,   _a      :       Optional[Any]   =						self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	_a      :       Optional[Any]   =						True
																	check_hidden_states_output(_a			,				_a			,				_a			)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	_a      :       Any   =						True

																	check_hidden_states_output(_a			,				_a			,				_a			)


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


							def 							__lowercase							(     self			)       ->       int:
												_a      :       Optional[int]   =						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_semantic_segmentation(*_a			)




							@slow
							def 							__lowercase							(     self			)       ->       Optional[Any]:
												for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_a      :       Tuple   =						MobileViTVaModel.from_pretrained(_a			)
																	self.assertIsNotNone(_a			)



def   __UpperCAmelCase					(      )     ->	Dict:

					"""simple docstring"""

					_a      :       str   =						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
					return image







@require_torch
@require_vision
class 							UpperCAmelCase_						(     unittest.TestCase  ):
							"""simple docstring"""







							@cached_property
							def 							__lowercase							(     self			)       ->       List[str]:
												return (
												    MobileViTImageProcessor.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''			)
												    if is_vision_available()
												    else None
												)


							@slow
							def 							__lowercase							(     self			)       ->       Optional[int]:
												_a      :       Union[str, Any]   =						MobileViTVaForImageClassification.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''			).to(
												    _a			)

												_a      :       List[str]   =						self.default_image_processor
												_a      :       Optional[int]   =						prepare_img()
												_a      :       List[str]   =						image_processor(images=_a			,				return_tensors='''pt'''			).to(_a			)

												# forward pass
												with torch.no_grad():
																	_a      :       List[str]   =						model(**_a			)

												# verify the logits
												_a      :       int   =						torch.Size((1, 1_0_0_0)			)
												self.assertEqual(outputs.logits.shape			,				_a			)

												_a      :       int   =						torch.tensor([-1.6_3_3_6e0_0, -7.3_2_0_4e-0_2, -5.1_8_8_3e-0_1]			).to(_a			)

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


							@slow
							def 							__lowercase							(     self			)       ->       Any:
												_a      :       Any   =						MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''			)
												_a      :       Optional[Any]   =						model.to(_a			)

												_a      :       Tuple   =						MobileViTImageProcessor.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''			)

												_a      :       Tuple   =						prepare_img()
												_a      :       List[Any]   =						image_processor(images=_a			,				return_tensors='''pt'''			).to(_a			)

												# forward pass
												with torch.no_grad():
																	_a      :       str   =						model(**_a			)
												_a      :       Optional[int]   =						outputs.logits

												# verify the logits
												_a      :       Union[str, Any]   =						torch.Size((1, 2_1, 3_2, 3_2)			)
												self.assertEqual(logits.shape			,				_a			)

												_a      :       Any   =						torch.tensor(
												    [
												        [[7.0863, 7.1525, 6.8201], [6.6931, 6.8770, 6.8933], [6.2978, 7.0366, 6.9636]],
												        [[-3.7134, -3.6712, -3.6675], [-3.5825, -3.3549, -3.4777], [-3.3435, -3.3979, -3.2857]],
												        [[-2.9329, -2.8003, -2.7369], [-3.0564, -2.4780, -2.0207], [-2.6889, -1.9298, -1.7640]],
												    ]			,				device=_a			,				)

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




							@slow
							def 							__lowercase							(     self			)       ->       int:
												_a      :       Any   =						MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''			)
												_a      :       Union[str, Any]   =						model.to(_a			)

												_a      :       int   =						MobileViTImageProcessor.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''			)

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

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

												_a      :       Dict   =						outputs.logits.detach().cpu()

												_a      :       Optional[int]   =						image_processor.post_process_semantic_segmentation(outputs=_a			,				target_sizes=[(5_0, 6_0)]			)
												_a      :       str   =						torch.Size((5_0, 6_0)			)
												self.assertEqual(segmentation[0].shape			,				_a			)

												_a      :       Union[str, Any]   =						image_processor.post_process_semantic_segmentation(outputs=_a			)
												_a      :       int   =						torch.Size((3_2, 3_2)			)
												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 							UpperCAmelCase_						(     __lowercase							,			unittest.TestCase  ):
							"""simple docstring"""







							UpperCAmelCase__			:		Any             =       LxmertTokenizer
							UpperCAmelCase__			:		Optional[Any]             =       LxmertTokenizerFast
							UpperCAmelCase__			:		Any             =       True
							UpperCAmelCase__			:		Dict             =       True


							def 							__lowercase							(     self			)       ->       Union[str, Any]:
												super().setUp()

												_a      :       int   =						[
												    '''[UNK]''',
												    '''[CLS]''',
												    '''[SEP]''',
												    '''want''',
												    '''##want''',
												    '''##ed''',
												    '''wa''',
												    '''un''',
												    '''runn''',
												    '''##ing''',
												    ''',''',
												    '''low''',
												    '''lowest''',
												]
												_a      :       Any   =						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 							__lowercase							(     self			,				_a			)       ->       List[str]:
												_a      :       Tuple   =						'''UNwant\u00E9d,running'''
												_a      :       str   =						'''unwanted, running'''
												return input_text, output_text


							def 							__lowercase							(     self			)       ->       List[Any]:
												_a      :       str   =						self.tokenizer_class(self.vocab_file			)

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




							def 							__lowercase							(     self			)       ->       List[Any]:
												if not self.test_rust_tokenizer:
																	return

												_a      :       Optional[Any]   =						self.get_tokenizer()
												_a      :       str   =						self.get_rust_tokenizer()

												_a      :       Optional[Any]   =						'''I was born in 92000, and this is falsé.'''

												_a      :       Optional[Any]   =						tokenizer.tokenize(_a			)
												_a      :       List[Any]   =						rust_tokenizer.tokenize(_a			)
												self.assertListEqual(_a			,				_a			)

												_a      :       List[Any]   =						tokenizer.encode(_a			,				add_special_tokens=_a			)
												_a      :       Any   =						rust_tokenizer.encode(_a			,				add_special_tokens=_a			)
												self.assertListEqual(_a			,				_a			)

												_a      :       Dict   =						self.get_rust_tokenizer()
												_a      :       Optional[int]   =						tokenizer.encode(_a			)
												_a      :       Dict   =						rust_tokenizer.encode(_a			)
												self.assertListEqual(_a			,				_a			)



