


"""simple docstring"""







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_	( UpperCamelCase			,  UpperCamelCase			,  unittest.TestCase):

	"""simple docstring"""





	snake_case__				:  Any    					=			IFImgaImgSuperResolutionPipeline
	snake_case__				:  List[Any]    					=			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
	snake_case__				:  str    					=			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"original_image"})
	snake_case__				:  int    					=			PipelineTesterMixin.required_optional_params - {"latents"}




	def   UpperCAmelCase_      ( self			:				Any   )       ->	Any:
						return self._get_superresolution_dummy_components()




	def   UpperCAmelCase_      ( self			:				Optional[int]			,			UpperCAmelCase__			:				str			,			UpperCAmelCase__			:				Dict=0   )       ->	Optional[Any]:
						if str(UpperCAmelCase__   ).startswith("mps"   ):
											__SCREAMING_SNAKE_CASE						   =				torch.manual_seed(UpperCAmelCase__   )
						else:
											__SCREAMING_SNAKE_CASE						   =				torch.Generator(device=UpperCAmelCase__   ).manual_seed(UpperCAmelCase__   )

						__SCREAMING_SNAKE_CASE						   =				floats_tensor((1, 3, 3_2, 3_2)			,			rng=random.Random(UpperCAmelCase__   )   ).to(UpperCAmelCase__   )
						__SCREAMING_SNAKE_CASE						   =				floats_tensor((1, 3, 1_6, 1_6)			,			rng=random.Random(UpperCAmelCase__   )   ).to(UpperCAmelCase__   )

						__SCREAMING_SNAKE_CASE						   =				{
						    "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   UpperCAmelCase_      ( self			:				Dict   )       ->	int:
						self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )




	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	str:
						self._test_save_load_optional_components()




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




	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	Tuple:
						self._test_save_load_local()




	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	str:
						self._test_inference_batch_single_identical(
						    expected_max_diff=1E-2			,			)




"""simple docstring"""







import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate




def    UpperCAmelCase__       (lowerCAmelCase_   ):




					'''simple docstring'''


					return x + 2




class  UpperCamelCase_	( unittest.TestCase):

	"""simple docstring"""





	def   UpperCAmelCase_      ( self			:				Any   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"x = 3"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3}   )

						__SCREAMING_SNAKE_CASE						   =				"x = y"
						__SCREAMING_SNAKE_CASE						   =				{"y": 5}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 5, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Dict   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				"y = add_two(x)"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )

						# Won't work without the tool
						with CaptureStdout() as out:
											__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result is None
						assert "tried to execute add_two" in out.out




	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	List[Any]:
						__SCREAMING_SNAKE_CASE						   =				"x = 3"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3}   )




	def   UpperCAmelCase_      ( self			:				str   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"test_dict = {'x': x, 'y': add_two(x)}"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_dict": {"x": 3, "y": 5}}   )




	def   UpperCAmelCase_      ( self			:				int   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				"x = 3\ny = 5"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Any   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"text = f'This is x: {x}.'"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == "This is x: 3."
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "text": "This is x: 3."}   )




	def   UpperCAmelCase_      ( self			:				Union[str, Any]   )       ->	Optional[int]:
						__SCREAMING_SNAKE_CASE						   =				"if x <= 3:\n    y = 2\nelse:\n    y = 5"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 2
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 2}   )

						__SCREAMING_SNAKE_CASE						   =				{"x": 8}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 8, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	str:
						__SCREAMING_SNAKE_CASE						   =				"test_list = [x, add_two(x)]"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						self.assertListEqual(UpperCAmelCase__			,			[3, 5]   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_list": [3, 5]}   )




	def   UpperCAmelCase_      ( self			:				Any   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				"y = x"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 3}   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				"test_list = [x, add_two(x)]\ntest_list[1]"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_list": [3, 5]}   )

						__SCREAMING_SNAKE_CASE						   =				"test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_dict": {"x": 3, "y": 5}}   )




	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				"x = 0\nfor i in range(3):\n    x = i"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"range": range}			,			state=UpperCAmelCase__   )
						assert result == 2
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 2, "i": 2}   )
