


import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin



@skip_mps
class 						_UpperCAmelCase      (     lowercase		,		lowercase		,		unittest.TestCase							):
		lowerCamelCase_	:			Union[str, Any]       			=  IFImgaImgSuperResolutionPipeline
		lowerCamelCase_	:			Optional[Any]       			=  TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
		lowerCamelCase_	:			Optional[int]       			=  TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""original_image"""}							)
		lowerCamelCase_	:			List[Any]       			=  PipelineTesterMixin.required_optional_params - {"""latents"""}






		def _snake_case		(						self				:     Dict):
				return self._get_superresolution_dummy_components()






		def _snake_case		(						self				:     Optional[int]   ,  UpperCAmelCase				:     Optional[Any]   ,  UpperCAmelCase				:     Dict=0):
				if str(UpperCAmelCase).startswith("mps"):
						SCREAMING_SNAKE_CASE_				:Dict				     =						torch.manual_seed(UpperCAmelCase)
				else:
						SCREAMING_SNAKE_CASE_				:Optional[int]				     =						torch.Generator(device=UpperCAmelCase).manual_seed(UpperCAmelCase)

				SCREAMING_SNAKE_CASE_				:List[str]				     =						floats_tensor((1, 3, 32, 32)   ,  rng=random.Random(UpperCAmelCase)).to(UpperCAmelCase)
				SCREAMING_SNAKE_CASE_				:Any				     =						floats_tensor((1, 3, 16, 16)   ,  rng=random.Random(UpperCAmelCase)).to(UpperCAmelCase)

				SCREAMING_SNAKE_CASE_				:str				     =						{
				    "prompt": "A painting of a squirrel eating a burger",
				    "image": image,
				    "original_image": original_image,
				    "generator": generator,
				    "num_inference_steps": 2,
				    "output_type": "numpy",
				}

				return inputs






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






		def _snake_case		(						self				:     Union[str, Any]):
				self._test_save_load_optional_components()






		@unittest.skipIf(torch_device != "cuda"   ,  reason="float16 requires CUDA")
		def _snake_case		(						self				:     str):
				# 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 _snake_case		(						self				:     List[str]):
				self._test_attention_slicing_forward_pass(expected_max_diff=1E-2)






		def _snake_case		(						self				:     List[Any]):
				self._test_save_load_local()







		def _snake_case		(						self				:     List[str]):
				self._test_inference_batch_single_identical(
				    expected_max_diff=1E-2   ,  )










import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
		import torch

		from transformers import AutoModelForImageClassification

if is_vision_available():
		from transformers import AutoImageProcessor



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






		@slow
		def _snake_case		(						self				:     List[str]):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						AutoImageProcessor.from_pretrained("microsoft/dit-base-finetuned-rvlcdip")
				SCREAMING_SNAKE_CASE_				:int				     =						AutoModelForImageClassification.from_pretrained("microsoft/dit-base-finetuned-rvlcdip")
				model.to(UpperCAmelCase)

				from datasets import load_dataset

				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						load_dataset("nielsr/rvlcdip-demo")

				SCREAMING_SNAKE_CASE_				:Dict				     =						dataset["train"][0]["image"].convert("RGB")

				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						image_processor(UpperCAmelCase   ,  return_tensors="pt").to(UpperCAmelCase)

				# forward pass
				with torch.no_grad():
						SCREAMING_SNAKE_CASE_				:Optional[int]				     =						model(**UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:List[str]				     =						outputs.logits

				SCREAMING_SNAKE_CASE_				:int				     =						torch.Size((1, 16))
				self.assertEqual(logits.shape   ,  UpperCAmelCase)

				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						torch.tensor(
				    [-0.4158, -0.4092, -0.4347]   ,  device=UpperCAmelCase   ,  dtype=torch.float   ,  )
				self.assertTrue(torch.allclose(logits[0, :3]   ,  UpperCAmelCase   ,  atol=1E-4))






