
import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
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      lowerCAmelCase__  (			__lowercase						,			__lowercase						,			unittest.TestCase    ):
		a__ :		Optional[Any]   						=	IFSuperResolutionPipeline
		a__ :		str   						=	TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
		a__ :		int   						=	TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
		a__ :		int   						=	PipelineTesterMixin.required_optional_params - {"""latents"""}






		def 				__A (	self	:							List[Any]						)  ->							int:
						return self._get_superresolution_dummy_components()






		def 				__A (	self	:							List[Any]							,    SCREAMING_SNAKE_CASE__	:							Any							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]=0						)  ->							Dict:
						if str(SCREAMING_SNAKE_CASE__						).startswith('''mps'''						):
										__lowerCamelCase					      =			torch.manual_seed(SCREAMING_SNAKE_CASE__						)
						else:
										__lowerCamelCase					      =			torch.Generator(device=SCREAMING_SNAKE_CASE__						).manual_seed(SCREAMING_SNAKE_CASE__						)

						__lowerCamelCase					      =			floats_tensor((1, 3, 32, 32)							,    rng=random.Random(SCREAMING_SNAKE_CASE__						)						).to(SCREAMING_SNAKE_CASE__						)

						__lowerCamelCase					      =			{
						    '''prompt''': '''A painting of a squirrel eating a burger''',
						    '''image''': 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 				__A (	self	:							Optional[int]						)  ->							Dict:
						self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3						)






		def 				__A (	self	:							Tuple						)  ->							str:
						self._test_save_load_optional_components()






		@unittest.skipIf(torch_device != '''cuda'''							,    reason='''float16 requires CUDA'''						)
		def 				__A (	self	:							int						)  ->							List[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 				__A (	self	:							int						)  ->							Optional[int]:
						self._test_attention_slicing_forward_pass(expected_max_diff=1e-2						)






		def 				__A (	self	:							Dict						)  ->							int:
						self._test_save_load_local()







		def 				__A (	self	:							Union[str, Any]						)  ->							List[Any]:
						self._test_inference_batch_single_identical(
						    expected_max_diff=1e-2							,    )




def 				__magic_name__				(     __lowerCAmelCase			:		list	,						__lowerCAmelCase			:		int	,						__lowerCAmelCase			:		int	,						__lowerCAmelCase			:		int							)    ->     int:
				__lowerCamelCase					      =			int((lower_bound + upper_bound) // 2							)
				if array[r] == value:
								return r
				if lower_bound >= upper_bound:
								return -1
				if array[r] < value:
								return binary_search(__lowerCAmelCase	,						r + 1	,						__lowerCAmelCase	,						__lowerCAmelCase							)
				else:
								return binary_search(__lowerCAmelCase	,						__lowerCAmelCase	,						r - 1	,						__lowerCAmelCase							)




def 				__magic_name__				(     __lowerCAmelCase			:		int	,						__lowerCAmelCase			:		list							)    ->     list:
				__lowerCamelCase					      =			0
				if matrix[index][0] == value:
								return [index, 0]
				while index < len(__lowerCAmelCase							) and matrix[index][0] < value:
								__lowerCamelCase					      =			binary_search(matrix[index]	,						0	,						len(matrix[index]							) - 1	,						__lowerCAmelCase							)
								if r != -1:
												return [index, r]
								index += 1
				return [-1, -1]


if __name__ == "__main__":
					import doctest

					doctest.testmod()



