




"""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 					lowerCAmelCase_      (     lowerCAmelCase   ):



				"""simple docstring"""

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.config_class(**self.inputs_dict      )
										self.parent.assertTrue(hasattr(lowerCAmelCase						,      'width_multiplier'      )      )





class 					lowerCAmelCase_      :



				"""simple docstring"""

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






										"""simple docstring"""

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

				def 						snake_case	(			self      ):






										"""simple docstring"""

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

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

										snake_case        =     self.get_config()

										return config, pixel_values, labels, pixel_labels

				def 						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 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

										snake_case        =     MobileViTVaModel(config=lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()
										snake_case        =     model(lowerCAmelCase      )
										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						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

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

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

										snake_case        =     self.num_labels
										snake_case        =     MobileViTVaForSemanticSegmentation(lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()
										snake_case        =     model(lowerCAmelCase      )
										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        =     model(lowerCAmelCase						,      labels=lowerCAmelCase      )
										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      ):






										"""simple docstring"""

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





@require_torch
class 					lowerCAmelCase_      (     lowerCAmelCase       ,      lowerCAmelCase       ,      unittest.TestCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[str]     					=  (
				    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)

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

				_lowerCAmelCase       :      Optional[int]     					=  False
				_lowerCAmelCase       :      int     					=  False
				_lowerCAmelCase       :      Any     					=  False
				_lowerCAmelCase       :      Union[str, Any]     					=  False

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     MobileViTVaModelTester(self      )
										snake_case        =     MobileViTVaConfigTester(self						,      config_class=lowerCAmelCase						,      has_text_modality=lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										self.config_tester.run_common_tests()

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






										"""simple docstring"""

										pass

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






										"""simple docstring"""

										pass

				@unittest.skip(reason='MobileViTV2 does not output attentions'      )
				def 						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 						snake_case	(			self      ):






										"""simple docstring"""

										pass

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






										"""simple docstring"""

										pass

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case   ,snake_case        =     self.model_tester.prepare_config_and_inputs_for_common()

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

																snake_case        =     ['pixel_values']
																self.assertListEqual(arg_names[:1]						,      lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										def check_hidden_states_output(lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):
																snake_case        =     model_class(lowerCAmelCase      )
																model.to(lowerCAmelCase      )
																model.eval()

																with torch.no_grad():
																						snake_case        =     model(**self._prepare_for_class(lowerCAmelCase						,      lowerCAmelCase      )      )

																snake_case        =     outputs.hidden_states

																snake_case        =     5
																self.assertEqual(len(lowerCAmelCase      )						,      lowerCAmelCase      )

																# 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        =     2
																for i in range(len(lowerCAmelCase      )      ):
																						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        =     self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																snake_case        =     True
																check_hidden_states_output(lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      )

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

																check_hidden_states_output(lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase      )

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

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





def 				lowerCAmelCase__      (     )   ->			Tuple:





						"""simple docstring"""






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



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



				"""simple docstring"""

				@cached_property
				def 						snake_case	(			self      ):






										"""simple docstring"""

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

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     MobileViTVaForImageClassification.from_pretrained('apple/mobilevitv2-1.0-imagenet1k-256'      ).to(
										    lowerCAmelCase      )

										snake_case        =     self.default_image_processor
										snake_case        =     prepare_img()
										snake_case        =     image_processor(images=lowerCAmelCase						,      return_tensors='pt'      ).to(lowerCAmelCase      )

										# forward pass
										with torch.no_grad():
																snake_case        =     model(**lowerCAmelCase      )

										# verify the logits
										snake_case        =     torch.Size((1, 10_00)      )
										self.assertEqual(outputs.logits.shape						,      lowerCAmelCase      )

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

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

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

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

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

										snake_case        =     prepare_img()
										snake_case        =     image_processor(images=lowerCAmelCase						,      return_tensors='pt'      ).to(lowerCAmelCase      )

										# forward pass
										with torch.no_grad():
																snake_case        =     model(**lowerCAmelCase      )
										snake_case        =     outputs.logits

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

										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=lowerCAmelCase						,      )

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

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

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

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

										snake_case        =     prepare_img()
										snake_case        =     image_processor(images=lowerCAmelCase						,      return_tensors='pt'      ).to(lowerCAmelCase      )

										# forward pass
										with torch.no_grad():
																snake_case        =     model(**lowerCAmelCase      )

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

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

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



"""simple docstring"""
import math
import os
from copy import deepcopy

import datasets
import evaluate
import torch
import transformers
from datasets import load_dataset
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer

from accelerate import Accelerator
from accelerate.test_utils import RegressionDataset, RegressionModel
from accelerate.utils import is_tpu_available, set_seed


SCREAMING_SNAKE_CASE__   =					"true"





def 				lowerCAmelCase__      (     _UpperCamelCase    :			Optional[int]	,     _UpperCamelCase    :			Dict=8_2	,     _UpperCamelCase    :			int=1_6   )   ->			str:





						"""simple docstring"""






						set_seed(4_2   )
						snake_case        =     RegressionModel()
						snake_case        =     deepcopy(_UpperCamelCase   )
						snake_case        =     RegressionDataset(length=_UpperCamelCase   )
						snake_case        =     DataLoader(_UpperCamelCase	,     batch_size=_UpperCamelCase   )
						model.to(accelerator.device   )
						snake_case   ,snake_case        =     accelerator.prepare(_UpperCamelCase	,     _UpperCamelCase   )
						return model, ddp_model, dataloader





def 				lowerCAmelCase__      (     _UpperCamelCase    :			Accelerator	,     _UpperCamelCase    :			Optional[Any]=False   )   ->			List[str]:





						"""simple docstring"""






						snake_case        =     AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased'   )
						snake_case        =     load_dataset('glue'	,     'mrpc'	,     split='validation'   )

						def tokenize_function(_UpperCamelCase    :			Optional[Any]   ):
												snake_case        =     tokenizer(examples['sentence1']	,     examples['sentence2']	,     truncation=_UpperCamelCase	,     max_length=_UpperCamelCase   )
												return outputs

						with accelerator.main_process_first():
												snake_case        =     dataset.map(
												    _UpperCamelCase	,     batched=_UpperCamelCase	,     remove_columns=['idx', 'sentence1', 'sentence2']	,     )

						snake_case        =     tokenized_datasets.rename_column('label'	,     'labels'   )

						def collate_fn(_UpperCamelCase    :			Optional[Any]   ):
												if use_longest:
																		return tokenizer.pad(_UpperCamelCase	,     padding='longest'	,     return_tensors='pt'   )
												return tokenizer.pad(_UpperCamelCase	,     padding='max_length'	,     max_length=1_2_8	,     return_tensors='pt'   )

						return DataLoader(_UpperCamelCase	,     shuffle=_UpperCamelCase	,     collate_fn=_UpperCamelCase	,     batch_size=1_6   )





def 				lowerCAmelCase__      (     _UpperCamelCase    :			str	,     _UpperCamelCase    :			Any   )   ->			List[Any]:





						"""simple docstring"""






						snake_case        =     Accelerator(dispatch_batches=_UpperCamelCase	,     split_batches=_UpperCamelCase   )
						snake_case        =     get_dataloader(_UpperCamelCase	,     not dispatch_batches   )
						snake_case        =     AutoModelForSequenceClassification.from_pretrained(
						    'hf-internal-testing/mrpc-bert-base-cased'	,     return_dict=_UpperCamelCase   )
						snake_case   ,snake_case        =     accelerator.prepare(_UpperCamelCase	,     _UpperCamelCase   )
						return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator





def 				lowerCAmelCase__      (     _UpperCamelCase    :			int	,     _UpperCamelCase    :			Any	,     _UpperCamelCase    :			Optional[Any]   )   ->			Dict:





						"""simple docstring"""






						snake_case        =     []
						for batch in dataloader:
												snake_case   ,snake_case        =     batch.values()
												with torch.no_grad():
																		snake_case        =     model(_UpperCamelCase   )
																		snake_case   ,snake_case        =     accelerator.gather_for_metrics((logit, target)   )
																		logits_and_targets.append((logit, target)   )
						snake_case   ,snake_case        =     [], []
						for logit, targ in logits_and_targets:
												logits.append(_UpperCamelCase   )
												targs.append(_UpperCamelCase   )
						snake_case   ,snake_case        =     torch.cat(_UpperCamelCase   ), torch.cat(_UpperCamelCase   )
						return logits, targs





def 				lowerCAmelCase__      (     _UpperCamelCase    :			Accelerator	,     _UpperCamelCase    :			Tuple=8_2	,     _UpperCamelCase    :			Optional[Any]=False	,     _UpperCamelCase    :			int=False	,     _UpperCamelCase    :			List[str]=1_6   )   ->			Optional[Any]:





						"""simple docstring"""






						snake_case   ,snake_case   ,snake_case        =     get_basic_setup(_UpperCamelCase	,     _UpperCamelCase	,     _UpperCamelCase   )
						snake_case   ,snake_case        =     generate_predictions(_UpperCamelCase	,     _UpperCamelCase	,     _UpperCamelCase   )
						assert (
						    len(_UpperCamelCase   ) == num_samples
						), f"""Unexpected number of inputs:\n    Expected: {num_samples}\n    Actual: {len(_UpperCamelCase   )}"""





def 				lowerCAmelCase__      (     _UpperCamelCase    :			bool = False	,     _UpperCamelCase    :			bool = False   )   ->			Tuple:





						"""simple docstring"""






						snake_case        =     evaluate.load('glue'	,     'mrpc'   )
						snake_case   ,snake_case        =     get_mrpc_setup(_UpperCamelCase	,     _UpperCamelCase   )
						# First do baseline
						snake_case   ,snake_case   ,snake_case        =     setup['no']
						model.to(_UpperCamelCase   )
						model.eval()
						for batch in dataloader:
												batch.to(_UpperCamelCase   )
												with torch.inference_mode():
																		snake_case        =     model(**_UpperCamelCase   )
												snake_case        =     outputs.logits.argmax(dim=-1   )
												metric.add_batch(predictions=_UpperCamelCase	,     references=batch['labels']   )
						snake_case        =     metric.compute()

						# Then do distributed
						snake_case   ,snake_case   ,snake_case        =     setup['ddp']
						model.eval()
						for batch in dataloader:
												with torch.inference_mode():
																		snake_case        =     model(**_UpperCamelCase   )
												snake_case        =     outputs.logits.argmax(dim=-1   )
												snake_case        =     batch['labels']
												snake_case   ,snake_case        =     accelerator.gather_for_metrics((preds, references)   )
												metric.add_batch(predictions=_UpperCamelCase	,     references=_UpperCamelCase   )
						snake_case        =     metric.compute()

						for key in "accuracy f1".split():
												assert math.isclose(
												    baseline[key]	,     distributed[key]   ), f"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n"""





def 				lowerCAmelCase__      (     )   ->			Tuple:





						"""simple docstring"""






						snake_case        =     Accelerator(split_batches=_UpperCamelCase	,     dispatch_batches=_UpperCamelCase   )
						if accelerator.is_local_main_process:
												datasets.utils.logging.set_verbosity_warning()
												transformers.utils.logging.set_verbosity_warning()
						else:
												datasets.utils.logging.set_verbosity_error()
												transformers.utils.logging.set_verbosity_error()
						# These are a bit slower so they should only be ran on the GPU or TPU
						if torch.cuda.is_available() or is_tpu_available():
												if accelerator.is_local_main_process:
																		print('**Testing gather_for_metrics**'   )
												for split_batches in [True, False]:
																		for dispatch_batches in [True, False]:
																								if accelerator.is_local_main_process:
																														print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`"""   )
																								test_mrpc(_UpperCamelCase	,     _UpperCamelCase   )
																								accelerator.state._reset_state()
						if accelerator.is_local_main_process:
												print('**Test torch metrics**'   )
						for split_batches in [True, False]:
												for dispatch_batches in [True, False]:
																		snake_case        =     Accelerator(split_batches=_UpperCamelCase	,     dispatch_batches=_UpperCamelCase   )
																		if accelerator.is_local_main_process:
																								print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99"""   )
																		test_torch_metrics(_UpperCamelCase	,     9_9   )
																		accelerator.state._reset_state()
						if accelerator.is_local_main_process:
												print('**Test last batch is not dropped when perfectly divisible**'   )
						snake_case        =     Accelerator()
						test_torch_metrics(_UpperCamelCase	,     5_1_2   )
						accelerator.state._reset_state()





def 				lowerCAmelCase__      (     _UpperCamelCase    :			Tuple   )   ->			str:





						"""simple docstring"""






						main()


if __name__ == "__main__":
					main()


