
'''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 	__lowercase				(  _lowercase    ):

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[int]     =		self.config_class(**self.inputs_dict      )
								self.parent.assertTrue(hasattr(A			,   '''width_multiplier'''      )      )

class 	__lowercase				:

			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			,   ):
								lowerCamelCase_					:      Optional[Any]     =		parent
								lowerCamelCase_					:      Optional[Any]     =		batch_size
								lowerCamelCase_					:      Union[str, Any]     =		image_size
								lowerCamelCase_					:      Union[str, Any]     =		patch_size
								lowerCamelCase_					:      Dict     =		num_channels
								lowerCamelCase_					:      List[Any]     =		make_divisible(5_1_2 * width_multiplier			,   divisor=8      )
								lowerCamelCase_					:      List[Any]     =		hidden_act
								lowerCamelCase_					:      List[str]     =		conv_kernel_size
								lowerCamelCase_					:      int     =		output_stride
								lowerCamelCase_					:      Union[str, Any]     =		classifier_dropout_prob
								lowerCamelCase_					:      str     =		use_labels
								lowerCamelCase_					:      int     =		is_training
								lowerCamelCase_					:      Any     =		num_labels
								lowerCamelCase_					:      List[str]     =		initializer_range
								lowerCamelCase_					:      Tuple     =		scope
								lowerCamelCase_					:      Tuple     =		width_multiplier
								lowerCamelCase_					:      str     =		ffn_dropout
								lowerCamelCase_					:      str     =		attn_dropout

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[str]     =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

								lowerCamelCase_					:      Dict     =		self.get_config()

								return config, pixel_values, labels, pixel_labels

			def        UpperCAmelCase__ (self      ):
								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			,   A			,   A			,   A			,   A      ):
								lowerCamelCase_					:      List[str]     =		MobileViTVaModel(config=A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      str     =		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			,   A			,   A			,   A			,   A      ):
								lowerCamelCase_					:      Optional[Any]     =		self.num_labels
								lowerCamelCase_					:      Union[str, Any]     =		MobileViTVaForImageClassification(A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      Optional[Any]     =		model(A			,   labels=A      )
								self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.num_labels)      )

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

@require_torch
class 	__lowercase				(  _lowercase      ,     _lowercase      ,     unittest.TestCase    ):
			lowerCamelCase			:		List[str]	      =			(
			    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)

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

			lowerCamelCase			:		Optional[Any]	      =			False
			lowerCamelCase			:		Union[str, Any]	      =			False
			lowerCamelCase			:		List[Any]	      =			False
			lowerCamelCase			:		Tuple	      =			False

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Tuple     =		MobileViTVaModelTester(self      )
								lowerCamelCase_					:      List[Any]     =		MobileViTVaConfigTester(self			,   config_class=A			,   has_text_modality=A      )

			def        UpperCAmelCase__ (self      ):
								self.config_tester.run_common_tests()

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

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

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

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

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

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_,      lowerCamelCase_					:      List[Any]     =		self.model_tester.prepare_config_and_inputs_for_common()

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

													lowerCamelCase_					:      Union[str, Any]     =		['''pixel_values''']
													self.assertListEqual(arg_names[:1]			,   A      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Union[str, Any]     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*A      )

			def        UpperCAmelCase__ (self      ):
								def check_hidden_states_output(A			,   A			,   A      ):
													lowerCamelCase_					:      Dict     =		model_class(A      )
													model.to(A      )
													model.eval()

													with torch.no_grad():
																		lowerCamelCase_					:      List[Any]     =		model(**self._prepare_for_class(A			,   A      )      )

													lowerCamelCase_					:      Dict     =		outputs.hidden_states

													lowerCamelCase_					:      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.
													lowerCamelCase_					:      Optional[int]     =		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      )

								lowerCamelCase_,      lowerCamelCase_					:      Union[str, Any]     =		self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													lowerCamelCase_					:      List[Any]     =		True
													check_hidden_states_output(A			,   A			,   A      )

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

													check_hidden_states_output(A			,   A			,   A      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      int     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*A      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      str     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*A      )






			@slow
			def        UpperCAmelCase__ (self      ):
								for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowerCamelCase_					:      Any     =		MobileViTVaModel.from_pretrained(A      )
													self.assertIsNotNone(A      )




def lowercase_	(   )     ->							Any:
					'''simple docstring'''

					lowerCamelCase_					:      Optional[int]     =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
					return image






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

			@cached_property
			def        UpperCAmelCase__ (self      ):
								return (
								    MobileViTImageProcessor.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''      )
								    if is_vision_available()
								    else None
								)

			@slow
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[Any]     =		MobileViTVaForImageClassification.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''      ).to(
								    A      )

								lowerCamelCase_					:      Optional[int]     =		self.default_image_processor
								lowerCamelCase_					:      Any     =		prepare_img()
								lowerCamelCase_					:      Dict     =		image_processor(images=A			,   return_tensors='''pt'''      ).to(A      )

								# forward pass
								with torch.no_grad():
													lowerCamelCase_					:      str     =		model(**A      )

								# verify the logits
								lowerCamelCase_					:      Union[str, Any]     =		torch.Size((1, 1_0_0_0)      )
								self.assertEqual(outputs.logits.shape			,   A      )

								lowerCamelCase_					:      Tuple     =		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        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''      )
								lowerCamelCase_					:      Tuple     =		model.to(A      )

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

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

								# forward pass
								with torch.no_grad():
													lowerCamelCase_					:      int     =		model(**A      )
								lowerCamelCase_					:      Optional[Any]     =		outputs.logits

								# verify the logits
								lowerCamelCase_					:      List[Any]     =		torch.Size((1, 2_1, 3_2, 3_2)      )
								self.assertEqual(logits.shape			,   A      )

								lowerCamelCase_					:      Union[str, Any]     =		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      ):
								lowerCamelCase_					:      List[Any]     =		MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''      )
								lowerCamelCase_					:      Union[str, Any]     =		model.to(A      )

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

								lowerCamelCase_					:      str     =		prepare_img()
								lowerCamelCase_					:      int     =		image_processor(images=A			,   return_tensors='''pt'''      ).to(A      )

								# forward pass
								with torch.no_grad():
													lowerCamelCase_					:      int     =		model(**A      )

								lowerCamelCase_					:      str     =		outputs.logits.detach().cpu()

								lowerCamelCase_					:      Any     =		image_processor.post_process_semantic_segmentation(outputs=A			,   target_sizes=[(5_0, 6_0)]      )
								lowerCamelCase_					:      int     =		torch.Size((5_0, 6_0)      )
								self.assertEqual(segmentation[0].shape			,   A      )

								lowerCamelCase_					:      Dict     =		image_processor.post_process_semantic_segmentation(outputs=A      )
								lowerCamelCase_					:      List[Any]     =		torch.Size((3_2, 3_2)      )
								self.assertEqual(segmentation[0].shape			,   A      )




'''simple docstring'''


import json
import logging
import os
import sys
from pathlib import Path

import finetune_rag

from transformers.file_utils import is_apex_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    require_ray,
    require_torch_gpu,
    require_torch_multi_gpu,
)


logging.basicConfig(level=logging.DEBUG)
__lowercase							:   Tuple					=     logging.getLogger()

__lowercase							:   str					=     logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)

class 	__lowercase				(  _lowercase    ):

			def        UpperCAmelCase__ (self			,   A      ):
								os.makedirs(A			,   exist_ok=A      )
								lowerCamelCase_					:      Any     =		{'''source''': '''What is love ?''', '''target''': '''life'''}
								lowerCamelCase_					:      List[str]     =		{'''train''': 1_2, '''val''': 2, '''test''': 2}
								for split in ["train", "test", "val"]:
													for field in ["source", "target"]:
																		lowerCamelCase_					:      List[Any]     =		'''\n'''.join([contents[field]] * n_lines[split]      )
																		with open(os.path.join(A			,   F"""{split}.{field}"""      )			,   '''w'''      ) as f:
																							f.write(A      )

			def        UpperCAmelCase__ (self			,   A			,   A = "pytorch"      ):
								lowerCamelCase_					:      List[str]     =		self.get_auto_remove_tmp_dir()
								lowerCamelCase_					:      Tuple     =		os.path.join(A			,   '''output'''      )
								lowerCamelCase_					:      Union[str, Any]     =		os.path.join(A			,   '''data'''      )
								self._create_dummy_data(data_dir=A      )

								lowerCamelCase_					:      Tuple     =		F"""
                --data_dir {data_dir} \
                --output_dir {output_dir} \
                --model_name_or_path facebook/rag-sequence-base \
                --model_type rag_sequence \
                --do_train \
                --do_predict \
                --n_val -1 \
                --val_check_interval 1.0 \
                --train_batch_size 2 \
                --eval_batch_size 1 \
                --max_source_length 25 \
                --max_target_length 25 \
                --val_max_target_length 25 \
                --test_max_target_length 25 \
                --label_smoothing 0.1 \
                --dropout 0.1 \
                --attention_dropout 0.1 \
                --weight_decay 0.001 \
                --adam_epsilon 1e-08 \
                --max_grad_norm 0.1 \
                --lr_scheduler polynomial \
                --learning_rate 3e-04 \
                --num_train_epochs 1 \
                --warmup_steps 4 \
                --gradient_accumulation_steps 1 \
                --distributed-port 8787 \
                --use_dummy_dataset 1 \
                --distributed_retriever {distributed_retriever} \
            """.split()

								if gpus > 0:
													testargs.append(F"""--gpus={gpus}"""      )
													if is_apex_available():
																		testargs.append('''--fp16'''      )
								else:
													testargs.append('''--gpus=0'''      )
													testargs.append('''--distributed_backend=ddp_cpu'''      )
													testargs.append('''--num_processes=2'''      )

								lowerCamelCase_					:      str     =		[sys.executable, str(Path(finetune_rag.__file__      ).resolve()      )] + testargs
								execute_subprocess_async(A			,   env=self.get_env()      )

								lowerCamelCase_					:      List[Any]     =		os.path.join(A			,   '''metrics.json'''      )
								with open(A      ) as f:
													lowerCamelCase_					:      List[str]     =		json.load(A      )
								return result

			@require_torch_gpu
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Tuple     =		self._run_finetune(gpus=1      )
								self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']			,   0.2      )

			@require_torch_multi_gpu
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[str]     =		self._run_finetune(gpus=2      )
								self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']			,   0.2      )

			@require_torch_gpu
			@require_ray
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[int]     =		self._run_finetune(gpus=1			,   distributed_retriever='''ray'''      )
								self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']			,   0.2      )






			@require_torch_multi_gpu
			@require_ray
			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Union[str, Any]     =		self._run_finetune(gpus=1			,   distributed_retriever='''ray'''      )
								self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']			,   0.2      )


