'''simple docstring'''

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   SCREAMING_SNAKE_CASE			(						lowerCAmelCase__):







		"""simple docstring"""






		def 		UpperCamelCase__       (   self				) ->     Any:
								_lowerCAmelCase										=self.config_class(**self.inputs_dict				)
								self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE		,						'hidden_sizes'				)				)
								self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE		,						'neck_hidden_sizes'				)				)
								self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE		,						'num_attention_heads'				)				)

class   SCREAMING_SNAKE_CASE			:







		"""simple docstring"""






		def __init__(   self		,						__A		,						__A=13		,						__A=32		,						__A=2		,						__A=3		,						__A=640		,						__A=4		,						__A="silu"		,						__A=3		,						__A=32		,						__A=0.1		,						__A=0.1		,						__A=0.1		,						__A=0.02		,						__A=True		,						__A=True		,						__A=10		,						__A=None		,						) ->     Any:
								_lowerCAmelCase										=parent
								_lowerCAmelCase										=batch_size
								_lowerCAmelCase										=image_size
								_lowerCAmelCase										=patch_size
								_lowerCAmelCase										=num_channels
								_lowerCAmelCase										=last_hidden_size
								_lowerCAmelCase										=num_attention_heads
								_lowerCAmelCase										=hidden_act
								_lowerCAmelCase										=conv_kernel_size
								_lowerCAmelCase										=output_stride
								_lowerCAmelCase										=hidden_dropout_prob
								_lowerCAmelCase										=attention_probs_dropout_prob
								_lowerCAmelCase										=classifier_dropout_prob
								_lowerCAmelCase										=use_labels
								_lowerCAmelCase										=is_training
								_lowerCAmelCase										=num_labels
								_lowerCAmelCase										=initializer_range
								_lowerCAmelCase										=scope






		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								_lowerCAmelCase										=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

								_lowerCAmelCase										=self.get_config()

								return config, pixel_values, labels, pixel_labels






		def 		UpperCamelCase__       (   self				) ->     List[str]:
								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 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A				) ->     str:
								_lowerCAmelCase										=MobileViTModel(config=_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()
								_lowerCAmelCase										=model(_SCREAMING_SNAKE_CASE				)
								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		,						__A		,						__A		,						__A		,						__A				) ->     str:
								_lowerCAmelCase										=self.num_labels
								_lowerCAmelCase										=MobileViTForImageClassification(_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()
								_lowerCAmelCase										=model(_SCREAMING_SNAKE_CASE		,						labels=_SCREAMING_SNAKE_CASE				)
								self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.num_labels)				)






		def 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A				) ->     Tuple:
								_lowerCAmelCase										=self.num_labels
								_lowerCAmelCase										=MobileViTForSemanticSegmentation(_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()
								_lowerCAmelCase										=model(_SCREAMING_SNAKE_CASE				)
								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										=model(_SCREAMING_SNAKE_CASE		,						labels=_SCREAMING_SNAKE_CASE				)
								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				) ->     str:
								_lowerCAmelCase										=self.prepare_config_and_inputs()
								_lowerCAmelCase						,				_lowerCAmelCase						,				_lowerCAmelCase						,				_lowerCAmelCase										=config_and_inputs
								_lowerCAmelCase										={'pixel_values': pixel_values}
								return config, inputs_dict

@require_torch
class   SCREAMING_SNAKE_CASE			(						lowerCAmelCase__    ,			lowerCAmelCase__    ,			unittest.TestCase):







		"""simple docstring"""
		lowercase   :      Union[str, Any]       =							(
		    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
		    if is_torch_available()
		    else ()
		)
		lowercase   :      List[Any]       =							(
		    {
		        "feature-extraction": MobileViTModel,
		        "image-classification": MobileViTForImageClassification,
		        "image-segmentation": MobileViTForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		lowercase   :      List[str]       =							False
		lowercase   :      Union[str, Any]       =							False
		lowercase   :      List[Any]       =							False
		lowercase   :      Optional[int]       =							False






		def 		UpperCamelCase__       (   self				) ->     Any:
								_lowerCAmelCase										=MobileViTModelTester(self				)
								_lowerCAmelCase										=MobileViTConfigTester(self		,						config_class=_SCREAMING_SNAKE_CASE		,						has_text_modality=_SCREAMING_SNAKE_CASE				)






		def 		UpperCamelCase__       (   self				) ->     List[str]:
								self.config_tester.run_common_tests()






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






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






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






		def 		UpperCamelCase__       (   self				) ->     List[str]:
								_lowerCAmelCase						,				_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs_for_common()

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

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






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






		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE				)






		def 		UpperCamelCase__       (   self				) ->     List[str]:
								def check_hidden_states_output(__A		,						__A		,						__A				):
														_lowerCAmelCase										=model_class(_SCREAMING_SNAKE_CASE				)
														model.to(_SCREAMING_SNAKE_CASE				)
														model.eval()

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

														_lowerCAmelCase										=outputs.hidden_states

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

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

								_lowerCAmelCase						,				_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs_for_common()

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

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

														check_hidden_states_output(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE				)






		def 		UpperCamelCase__       (   self				) ->     Any:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE				)






		def 		UpperCamelCase__       (   self				) ->     Any:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE				)






		@slow
		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_lowerCAmelCase										=MobileViTModel.from_pretrained(_SCREAMING_SNAKE_CASE				)
														self.assertIsNotNone(_SCREAMING_SNAKE_CASE				)



def 							UpperCamelCase__			(			):


						'''simple docstring'''






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


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







		"""simple docstring"""






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






		@slow
		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=MobileViTForImageClassification.from_pretrained('apple/mobilevit-xx-small'				).to(_SCREAMING_SNAKE_CASE				)

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

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

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

								_lowerCAmelCase										=torch.tensor([-1.9_364, -1.2_327, -0.4_653]				).to(_SCREAMING_SNAKE_CASE				)

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






		@slow
		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=MobileViTForSemanticSegmentation.from_pretrained('apple/deeplabv3-mobilevit-xx-small'				)
								_lowerCAmelCase										=model.to(_SCREAMING_SNAKE_CASE				)

								_lowerCAmelCase										=MobileViTImageProcessor.from_pretrained('apple/deeplabv3-mobilevit-xx-small'				)

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

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

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

								_lowerCAmelCase										=torch.tensor(
								    [
								        [[6.9_713, 6.9_786, 7.2_422], [7.2_893, 7.2_825, 7.4_446], [7.6_580, 7.8_797, 7.9_420]],
								        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9_868, -9.7_132], [-11.0405, -11.0221, -10.7318]],
								        [[-3.3_089, -2.8_539, -2.6_740], [-3.2_706, -2.5_621, -2.5_108], [-3.2_534, -2.6_615, -2.6_651]],
								    ]		,						device=_SCREAMING_SNAKE_CASE		,						)

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






		@slow
		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								_lowerCAmelCase										=MobileViTForSemanticSegmentation.from_pretrained('apple/deeplabv3-mobilevit-xx-small'				)
								_lowerCAmelCase										=model.to(_SCREAMING_SNAKE_CASE				)

								_lowerCAmelCase										=MobileViTImageProcessor.from_pretrained('apple/deeplabv3-mobilevit-xx-small'				)

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

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

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

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

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






'''simple docstring'''

import os
from argparse import ArgumentParser, Namespace

from ..data import SingleSentenceClassificationProcessor as Processor
from ..pipelines import TextClassificationPipeline
from ..utils import is_tf_available, is_torch_available, logging
from . import BaseTransformersCLICommand


if not is_tf_available() and not is_torch_available():
							raise RuntimeError('''At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training''')

# TF training parameters
lowercase_     		= False
lowercase_     		= False



def 							UpperCamelCase__			(			a__      ):


						'''simple docstring'''






						return TrainCommand(a__      )


class   SCREAMING_SNAKE_CASE			(						__lowercase):







		"""simple docstring"""






		@staticmethod
		def 		UpperCamelCase__       (   __A				) ->     Tuple:
								_lowerCAmelCase										=parser.add_parser('train'		,						help='CLI tool to train a model on a task.'				)

								train_parser.add_argument(
								    '--train_data'		,						type=__A		,						required=__A		,						help='path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences.'		,						)
								train_parser.add_argument(
								    '--column_label'		,						type=__A		,						default=0		,						help='Column of the dataset csv file with example labels.'				)
								train_parser.add_argument(
								    '--column_text'		,						type=__A		,						default=1		,						help='Column of the dataset csv file with example texts.'				)
								train_parser.add_argument(
								    '--column_id'		,						type=__A		,						default=2		,						help='Column of the dataset csv file with example ids.'				)
								train_parser.add_argument(
								    '--skip_first_row'		,						action='store_true'		,						help='Skip the first row of the csv file (headers).'				)

								train_parser.add_argument('--validation_data'		,						type=__A		,						default=''		,						help='path to validation dataset.'				)
								train_parser.add_argument(
								    '--validation_split'		,						type=__A		,						default=0.1		,						help='if validation dataset is not provided, fraction of train dataset to use as validation dataset.'		,						)

								train_parser.add_argument('--output'		,						type=__A		,						default='./'		,						help='path to saved the trained model.'				)

								train_parser.add_argument(
								    '--task'		,						type=__A		,						default='text_classification'		,						help='Task to train the model on.'				)
								train_parser.add_argument(
								    '--model'		,						type=__A		,						default='bert-base-uncased'		,						help='Model\'s name or path to stored model.'				)
								train_parser.add_argument('--train_batch_size'		,						type=__A		,						default=32		,						help='Batch size for training.'				)
								train_parser.add_argument('--valid_batch_size'		,						type=__A		,						default=64		,						help='Batch size for validation.'				)
								train_parser.add_argument('--learning_rate'		,						type=__A		,						default=3E-5		,						help='Learning rate.'				)
								train_parser.add_argument('--adam_epsilon'		,						type=__A		,						default=1E-08		,						help='Epsilon for Adam optimizer.'				)
								train_parser.set_defaults(func=__A				)






		def __init__(   self		,						__A				) ->     List[str]:
								_lowerCAmelCase										=logging.get_logger('transformers-cli/training'				)

								_lowerCAmelCase										='tf' if is_tf_available() else 'torch'

								os.makedirs(args.output		,						exist_ok=__A				)
								_lowerCAmelCase										=args.output

								_lowerCAmelCase										=args.column_label
								_lowerCAmelCase										=args.column_text
								_lowerCAmelCase										=args.column_id

								self.logger.info(F'''Loading {args.task} pipeline for {args.model}'''				)
								if args.task == "text_classification":
														_lowerCAmelCase										=TextClassificationPipeline.from_pretrained(args.model				)
								elif args.task == "token_classification":
														raise NotImplementedError
								elif args.task == "question_answering":
														raise NotImplementedError

								self.logger.info(F'''Loading dataset from {args.train_data}'''				)
								_lowerCAmelCase										=Processor.create_from_csv(
								    args.train_data		,						column_label=args.column_label		,						column_text=args.column_text		,						column_id=args.column_id		,						skip_first_row=args.skip_first_row		,						)
								_lowerCAmelCase										=None
								if args.validation_data:
														self.logger.info(F'''Loading validation dataset from {args.validation_data}'''				)
														_lowerCAmelCase										=Processor.create_from_csv(
														    args.validation_data		,						column_label=args.column_label		,						column_text=args.column_text		,						column_id=args.column_id		,						skip_first_row=args.skip_first_row		,						)

								_lowerCAmelCase										=args.validation_split
								_lowerCAmelCase										=args.train_batch_size
								_lowerCAmelCase										=args.valid_batch_size
								_lowerCAmelCase										=args.learning_rate
								_lowerCAmelCase										=args.adam_epsilon






		def 		UpperCamelCase__       (   self				) ->     List[str]:
								if self.framework == "tf":
														return self.run_tf()
								return self.run_torch()






		def 		UpperCamelCase__       (   self				) ->     Union[str, Any]:
								raise NotImplementedError






		def 		UpperCamelCase__       (   self				) ->     List[Any]:
								self.pipeline.fit(
								    self.train_dataset		,						validation_data=self.valid_dataset		,						validation_split=self.validation_split		,						learning_rate=self.learning_rate		,						adam_epsilon=self.adam_epsilon		,						train_batch_size=self.train_batch_size		,						valid_batch_size=self.valid_batch_size		,						)

								# Save trained pipeline
								self.pipeline.save_pretrained(self.output				)





