


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






		'''simple docstring'''

		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						int						=				self.config_class(**self.inputs_dict					)
			self.parent.assertTrue(hasattr(a_				,       '''width_multiplier'''					)					)



class 			_UpperCAmelCase :






		'''simple docstring'''

		def __init__(self				,       a_				,       a_=13				,       a_=64				,       a_=2				,       a_=3				,       a_="swish"				,       a_=3				,       a_=32				,       a_=0.1				,       a_=0.02				,       a_=True				,       a_=True				,       a_=10				,       a_=None				,       a_=0.25				,       a_=0.0				,       a_=0.0				,       ):





			'''simple docstring'''

			__snake_case	:						List[str]						=				parent
			__snake_case	:						int						=				batch_size
			__snake_case	:						Optional[int]						=				image_size
			__snake_case	:						List[str]						=				patch_size
			__snake_case	:						Tuple						=				num_channels
			__snake_case	:						Tuple						=				make_divisible(5_12 * width_multiplier				,       divisor=8					)
			__snake_case	:						List[Any]						=				hidden_act
			__snake_case	:						List[Any]						=				conv_kernel_size
			__snake_case	:						List[str]						=				output_stride
			__snake_case	:						int						=				classifier_dropout_prob
			__snake_case	:						Optional[int]						=				use_labels
			__snake_case	:						Optional[int]						=				is_training
			__snake_case	:						int						=				num_labels
			__snake_case	:						int						=				initializer_range
			__snake_case	:						Optional[Any]						=				scope
			__snake_case	:						str						=				width_multiplier
			__snake_case	:						Union[str, Any]						=				ffn_dropout
			__snake_case	:						Dict						=				attn_dropout



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Any						=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

			__snake_case	:						Tuple						=				self.get_config()

			return config, pixel_values, labels, pixel_labels



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''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 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						List[Any]						=				MobileViTVaModel(config=a_					)
			model.to(a_					)
			model.eval()
			__snake_case	:						int						=				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 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				self.num_labels
			__snake_case	:						int						=				MobileViTVaForImageClassification(a_					)
			model.to(a_					)
			model.eval()
			__snake_case	:						Any						=				model(a_				,       labels=a_					)
			self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)					)



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						List[Any]						=				self.num_labels
			__snake_case	:						List[Any]						=				MobileViTVaForSemanticSegmentation(a_					)
			model.to(a_					)
			model.eval()
			__snake_case	:						Union[str, Any]						=				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,
			    )				,       )
			__snake_case	:						str						=				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 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						List[Any]						=				self.prepare_config_and_inputs()
			__snake_case	,							__snake_case	,							__snake_case	,							__snake_case	:						Optional[int]						=				config_and_inputs
			__snake_case	:						Union[str, Any]						=				{'''pixel_values''': pixel_values}
			return config, inputs_dict



@require_torch
class 			_UpperCAmelCase (							__snake_case,				__snake_case,				unittest.TestCase				):






		'''simple docstring'''

		lowerCamelCase__				=(
		    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
		    if is_torch_available()
		    else ()
		)

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

		lowerCamelCase__				=False
		lowerCamelCase__				=False
		lowerCamelCase__				=False
		lowerCamelCase__				=False



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				MobileViTVaModelTester(self					)
			__snake_case	:						List[str]						=				MobileViTVaConfigTester(self				,       config_class=a_				,       has_text_modality=a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			self.config_tester.run_common_tests()



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





			'''simple docstring'''

			pass



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





			'''simple docstring'''

			pass



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





			'''simple docstring'''

			pass



		@require_torch_multi_gpu
		@unittest.skip(reason='''Got `CUDA error: misaligned address` for tests after this one being run.'''					)
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			pass



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





			'''simple docstring'''

			pass



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	,							__snake_case	:						List[str]						=				self.model_tester.prepare_config_and_inputs_for_common()

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

				__snake_case	:						Optional[int]						=				['''pixel_values''']
				self.assertListEqual(arg_names[:1]				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Union[str, Any]						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			def check_hidden_states_output(a_				,       a_				,       a_					):
				__snake_case	:						Optional[Any]						=				model_class(a_					)
				model.to(a_					)
				model.eval()

				with torch.no_grad():
					__snake_case	:						Optional[int]						=				model(**self._prepare_for_class(a_				,       a_					)					)

				__snake_case	:						List[Any]						=				outputs.hidden_states

				__snake_case	:						List[str]						=				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.
				__snake_case	:						Tuple						=				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					)

			__snake_case	,							__snake_case	:						List[str]						=				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__snake_case	:						Any						=				True
				check_hidden_states_output(a_				,       a_				,       a_					)

				# check that output_hidden_states also work using config
				del inputs_dict["output_hidden_states"]
				__snake_case	:						List[str]						=				True

				check_hidden_states_output(a_				,       a_				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						int						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Any						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_semantic_segmentation(*a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						List[str]						=				MobileViTVaModel.from_pretrained(a_					)
				self.assertIsNotNone(a_					)






def 		lowercase							(     )     ->str:
	"""simple docstring"""





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



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






		'''simple docstring'''

		@cached_property
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

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



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						List[Any]						=				MobileViTVaForImageClassification.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''					).to(
			    a_					)

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

			# forward pass
			with torch.no_grad():
				__snake_case	:						List[Any]						=				model(**a_					)

			# verify the logits
			__snake_case	:						Tuple						=				torch.Size((1, 10_00)					)
			self.assertEqual(outputs.logits.shape				,       a_					)

			__snake_case	:						int						=				torch.tensor([-1.6336E00, -7.3204E-02, -5.1883E-01]					).to(a_					)

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



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''					)
			__snake_case	:						List[Any]						=				model.to(a_					)

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

			__snake_case	:						List[Any]						=				prepare_img()
			__snake_case	:						Optional[int]						=				image_processor(images=a_				,       return_tensors='''pt'''					).to(a_					)

			# forward pass
			with torch.no_grad():
				__snake_case	:						Optional[int]						=				model(**a_					)
			__snake_case	:						Dict						=				outputs.logits

			# verify the logits
			__snake_case	:						Tuple						=				torch.Size((1, 21, 32, 32)					)
			self.assertEqual(logits.shape				,       a_					)

			__snake_case	:						List[str]						=				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 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Dict						=				MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''					)
			__snake_case	:						List[str]						=				model.to(a_					)

			__snake_case	:						List[str]						=				MobileViTImageProcessor.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''					)

			__snake_case	:						int						=				prepare_img()
			__snake_case	:						str						=				image_processor(images=a_				,       return_tensors='''pt'''					).to(a_					)

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

			__snake_case	:						Any						=				outputs.logits.detach().cpu()

			__snake_case	:						Any						=				image_processor.post_process_semantic_segmentation(outputs=a_				,       target_sizes=[(50, 60)]					)
			__snake_case	:						int						=				torch.Size((50, 60)					)
			self.assertEqual(segmentation[0].shape				,       a_					)

			__snake_case	:						Tuple						=				image_processor.post_process_semantic_segmentation(outputs=a_					)
			__snake_case	:						Dict						=				torch.Size((32, 32)					)
			self.assertEqual(segmentation[0].shape				,       a_					)









"""simple docstring"""
import argparse

import torch

from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()






def 		lowercase							(     _snake_case						:      List[Any]     ,   _snake_case						:      Tuple     ,   _snake_case						:      int     )     ->List[Any]:
	"""simple docstring"""





	if openai_config_file == "":
		__snake_case	:						Dict						=				OpenAIGPTConfig()
	else:
		__snake_case	:						int						=				OpenAIGPTConfig.from_json_file(_snake_case     )
	__snake_case	:						Tuple						=				OpenAIGPTModel(_snake_case     )

	# Load weights from numpy
	load_tf_weights_in_openai_gpt(_snake_case     ,   _snake_case     ,   _snake_case     )

	# Save pytorch-model
	__snake_case	:						str						=				pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
	__snake_case	:						Optional[Any]						=				pytorch_dump_folder_path + '''/''' + CONFIG_NAME
	print(f"""Save PyTorch model to {pytorch_weights_dump_path}"""     )
	torch.save(model.state_dict()     ,   _snake_case     )
	print(f"""Save configuration file to {pytorch_config_dump_path}"""     )
	with open(_snake_case     ,   '''w'''     ,   encoding='''utf-8'''     ) as f:
		f.write(config.to_json_string()     )


if __name__ == "__main__":
			SCREAMING_SNAKE_CASE :    Any									=				argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    """--openai_checkpoint_folder_path""",
			    default=None,
			    type=str,
			    required=True,
			    help="""Path to the TensorFlow checkpoint path.""",
			)
			parser.add_argument(
			    """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
			)
			parser.add_argument(
			    """--openai_config_file""",
			    default="""""",
			    type=str,
			    help=(
			        """An optional config json file corresponding to the pre-trained OpenAI model. \n"""
			        """This specifies the model architecture."""
			    ),
			)
			SCREAMING_SNAKE_CASE :    Optional[int]									=				parser.parse_args()
			convert_openai_checkpoint_to_pytorch(
			    args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path
			)





