
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		(					SCREAMING_SNAKE_CASE__		):




							def  A__      (				self):
												lowercase							=						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   ,):
												lowercase							=						parent
												lowercase							=						batch_size
												lowercase							=						image_size
												lowercase							=						patch_size
												lowercase							=						num_channels
												lowercase							=						make_divisible(5_1_2 * width_multiplier   ,divisor=8)
												lowercase							=						hidden_act
												lowercase							=						conv_kernel_size
												lowercase							=						output_stride
												lowercase							=						classifier_dropout_prob
												lowercase							=						use_labels
												lowercase							=						is_training
												lowercase							=						num_labels
												lowercase							=						initializer_range
												lowercase							=						scope
												lowercase							=						width_multiplier
												lowercase							=						ffn_dropout
												lowercase							=						attn_dropout




							def  A__      (				self):
												lowercase							=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

												lowercase							=						self.get_config()

												return config, pixel_values, labels, pixel_labels




							def  A__      (				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  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												lowercase							=						MobileViTVaModel(config=A__)
												model.to(A__)
												model.eval()
												lowercase							=						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  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												lowercase							=						self.num_labels
												lowercase							=						MobileViTVaForImageClassification(A__)
												model.to(A__)
												model.eval()
												lowercase							=						model(A__   ,labels=A__)
												self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels))




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												lowercase							=						self.num_labels
												lowercase							=						MobileViTVaForSemanticSegmentation(A__)
												model.to(A__)
												model.eval()
												lowercase							=						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,
												    )   ,)
												lowercase							=						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  A__      (				self):
												lowercase							=						self.prepare_config_and_inputs()
												lowercase	,						lowercase	,						lowercase	,						lowercase							=						config_and_inputs
												lowercase							=						{'''pixel_values''': pixel_values}
												return config, inputs_dict







@require_torch
class 				lowercase		(					SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__       ,  unittest.TestCase		):
							lowercase_       :       str       					=(
							    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)

							lowercase_       :       List[str]       					=(
							    {
							        '''feature-extraction''': MobileViTVaModel,
							        '''image-classification''': MobileViTVaForImageClassification,
							        '''image-segmentation''': MobileViTVaForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							lowercase_       :       Any       					=False
							lowercase_       :       Tuple       					=False
							lowercase_       :       List[Any]       					=False
							lowercase_       :       List[Any]       					=False




							def  A__      (				self):
												lowercase							=						MobileViTVaModelTester(self)
												lowercase							=						MobileViTVaConfigTester(self   ,config_class=A__   ,has_text_modality=A__)




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




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




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




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




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




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




							def  A__      (				self):
												lowercase	,						lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowercase							=						['''pixel_values''']
																	self.assertListEqual(arg_names[:1]   ,A__)




							def  A__      (				self):
												lowercase							=						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*A__)




							def  A__      (				self):
												def check_hidden_states_output(A__   ,A__   ,A__):
																	lowercase							=						model_class(A__)
																	model.to(A__)
																	model.eval()

																	with torch.no_grad():
																						lowercase							=						model(**self._prepare_for_class(A__   ,A__))

																	lowercase							=						outputs.hidden_states

																	lowercase							=						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.
																	lowercase							=						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)

												lowercase	,						lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	lowercase							=						True
																	check_hidden_states_output(A__   ,A__   ,A__)

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

																	check_hidden_states_output(A__   ,A__   ,A__)




							def  A__      (				self):
												lowercase							=						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*A__)




							def  A__      (				self):
												lowercase							=						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_semantic_segmentation(*A__)





							@slow
							def  A__      (				self):
												for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase							=						MobileViTVaModel.from_pretrained(A__)
																	self.assertIsNotNone(A__)



def        UpperCamelCase    (							):
					'''simple docstring'''
					lowercase							=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
					return image







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




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




							@slow
							def  A__      (				self):
												lowercase							=						MobileViTVaForImageClassification.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256''').to(
												    A__)

												lowercase							=						self.default_image_processor
												lowercase							=						prepare_img()
												lowercase							=						image_processor(images=A__   ,return_tensors='''pt''').to(A__)

												# forward pass
												with torch.no_grad():
																	lowercase							=						model(**A__)

												# verify the logits
												lowercase							=						torch.Size((1, 1_0_0_0))
												self.assertEqual(outputs.logits.shape   ,A__)

												lowercase							=						torch.tensor([-1.6_336E00, -7.3_204E-02, -5.1_883E-01]).to(A__)

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




							@slow
							def  A__      (				self):
												lowercase							=						MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3''')
												lowercase							=						model.to(A__)

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

												lowercase							=						prepare_img()
												lowercase							=						image_processor(images=A__   ,return_tensors='''pt''').to(A__)

												# forward pass
												with torch.no_grad():
																	lowercase							=						model(**A__)
												lowercase							=						outputs.logits

												# verify the logits
												lowercase							=						torch.Size((1, 2_1, 3_2, 3_2))
												self.assertEqual(logits.shape   ,A__)

												lowercase							=						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  A__      (				self):
												lowercase							=						MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3''')
												lowercase							=						model.to(A__)

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

												lowercase							=						prepare_img()
												lowercase							=						image_processor(images=A__   ,return_tensors='''pt''').to(A__)

												# forward pass
												with torch.no_grad():
																	lowercase							=						model(**A__)

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

												lowercase							=						image_processor.post_process_semantic_segmentation(outputs=A__   ,target_sizes=[(5_0, 6_0)])
												lowercase							=						torch.Size((5_0, 6_0))
												self.assertEqual(segmentation[0].shape   ,A__)

												lowercase							=						image_processor.post_process_semantic_segmentation(outputs=A__)
												lowercase							=						torch.Size((3_2, 3_2))
												self.assertEqual(segmentation[0].shape   ,A__)






import gc
import random
import unittest

import torch

from diffusers import (
    IFImgaImgPipeline,
    IFImgaImgSuperResolutionPipeline,
    IFInpaintingPipeline,
    IFInpaintingSuperResolutionPipeline,
    IFPipeline,
    IFSuperResolutionPipeline,
)
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
from . import IFPipelineTesterMixin







@skip_mps
class 				lowercase		(					SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__       ,  unittest.TestCase		):
							lowercase_       :       Optional[Any]       					=IFPipeline
							lowercase_       :       List[str]       					=TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''}
							lowercase_       :       List[str]       					=TEXT_TO_IMAGE_BATCH_PARAMS
							lowercase_       :       int       					=PipelineTesterMixin.required_optional_params - {'''latents'''}




							def  A__      (				self):
												return self._get_dummy_components()




							def  A__      (				self   ,A__   ,A__=0):
												if str(A__).startswith('''mps'''):
																	lowercase							=						torch.manual_seed(A__)
												else:
																	lowercase							=						torch.Generator(device=A__).manual_seed(A__)

												lowercase							=						{
												    '''prompt''': '''A painting of a squirrel eating a burger''',
												    '''generator''': generator,
												    '''num_inference_steps''': 2,
												    '''output_type''': '''numpy''',
												}

												return inputs




							def  A__      (				self):
												self._test_save_load_optional_components()




							@unittest.skipIf(torch_device != '''cuda'''   ,reason='''float16 requires CUDA''')
							def  A__      (				self):
												# Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
												super().test_save_load_floataa(expected_max_diff=1E-1)




							def  A__      (				self):
												self._test_attention_slicing_forward_pass(expected_max_diff=1E-2)




							def  A__      (				self):
												self._test_save_load_local()




							def  A__      (				self):
												self._test_inference_batch_single_identical(
												    expected_max_diff=1E-2   ,)





							@unittest.skipIf(
							    torch_device != '''cuda''' or not is_xformers_available()   ,reason='''XFormers attention is only available with CUDA and `xformers` installed'''   ,)
							def  A__      (				self):
												self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3)







@slow
@require_torch_gpu
class 				lowercase		(					unittest.TestCase		):




							def  A__      (				self):
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




							def  A__      (				self):
												# if

												lowercase							=						IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0'''   ,variant='''fp16'''   ,torch_dtype=torch.floataa)

												lowercase							=						IFSuperResolutionPipeline.from_pretrained(
												    '''DeepFloyd/IF-II-L-v1.0'''   ,variant='''fp16'''   ,torch_dtype=torch.floataa   ,text_encoder=A__   ,tokenizer=A__)

												# pre compute text embeddings and remove T5 to save memory

												pipe_a.text_encoder.to('''cuda''')

												lowercase	,						lowercase							=						pipe_a.encode_prompt('''anime turtle'''   ,device='''cuda''')

												del pipe_a.tokenizer
												del pipe_a.text_encoder
												gc.collect()

												lowercase							=						None
												lowercase							=						None

												pipe_a.enable_model_cpu_offload()
												pipe_a.enable_model_cpu_offload()

												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())
												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())

												self._test_if(A__   ,A__   ,A__   ,A__)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# img2img

												lowercase							=						IFImgaImgPipeline(**pipe_a.components)
												lowercase							=						IFImgaImgSuperResolutionPipeline(**pipe_a.components)

												pipe_a.enable_model_cpu_offload()
												pipe_a.enable_model_cpu_offload()

												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())
												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())

												self._test_if_imgaimg(A__   ,A__   ,A__   ,A__)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# inpainting

												lowercase							=						IFInpaintingPipeline(**pipe_a.components)
												lowercase							=						IFInpaintingSuperResolutionPipeline(**pipe_a.components)

												pipe_a.enable_model_cpu_offload()
												pipe_a.enable_model_cpu_offload()

												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())
												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())

												self._test_if_inpainting(A__   ,A__   ,A__   ,A__)




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												# pipeline 1

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)
												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,num_inference_steps=2   ,generator=A__   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (6_4, 6_4, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 1_3 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''')
												assert_mean_pixel_difference(A__   ,A__)

												# pipeline 2

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,generator=A__   ,num_inference_steps=2   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (2_5_6, 2_5_6, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''')
												assert_mean_pixel_difference(A__   ,A__)




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												# pipeline 1

												_start_torch_memory_measurement()

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,num_inference_steps=2   ,generator=A__   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (6_4, 6_4, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 1_0 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''')
												assert_mean_pixel_difference(A__   ,A__)

												# pipeline 2

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,original_image=A__   ,generator=A__   ,num_inference_steps=2   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (2_5_6, 2_5_6, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''')
												assert_mean_pixel_difference(A__   ,A__)





							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												# pipeline 1

												_start_torch_memory_measurement()

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(1)).to(A__)

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)
												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,mask_image=A__   ,num_inference_steps=2   ,generator=A__   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (6_4, 6_4, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 1_0 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''')
												assert_mean_pixel_difference(A__   ,A__)

												# pipeline 2

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,rng=random.Random(1)).to(A__)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,mask_image=A__   ,original_image=A__   ,generator=A__   ,num_inference_steps=2   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (2_5_6, 2_5_6, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''')
												assert_mean_pixel_difference(A__   ,A__)



def        UpperCamelCase    (							):
					'''simple docstring'''
					torch.cuda.empty_cache()
					torch.cuda.reset_max_memory_allocated()
					torch.cuda.reset_peak_memory_stats()




