'''simple docstring'''


import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
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_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin






@skip_mps
class 						A   (   UpperCAmelCase       ,	UpperCAmelCase       ,	unittest.TestCase   ):
							a_        =      IFInpaintingSuperResolutionPipeline
							a_        =      TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'''width''', '''height'''}
							a_        =      TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'''original_image'''}   )
							a_        =      PipelineTesterMixin.required_optional_params - {'''latents'''}





							def       snake_case__						(      self				:							Tuple	)							->	List[str]:
								return self._get_superresolution_dummy_components()





							def       snake_case__						(      self				:							Optional[int]     ,		__a				:							List[str]     ,		__a				:							List[str]=0	)							->	Any:
								if str(__a	).startswith('''mps'''	):
									__UpperCAmelCase							=			torch.manual_seed(__a	)
								else:
									__UpperCAmelCase							=			torch.Generator(device=__a	).manual_seed(__a	)

								__UpperCAmelCase							=			floats_tensor((1, 3, 1_6, 1_6)     ,		rng=random.Random(__a	)	).to(__a	)
								__UpperCAmelCase							=			floats_tensor((1, 3, 3_2, 3_2)     ,		rng=random.Random(__a	)	).to(__a	)
								__UpperCAmelCase							=			floats_tensor((1, 3, 3_2, 3_2)     ,		rng=random.Random(__a	)	).to(__a	)

								__UpperCAmelCase							=			{
								    '''prompt''': '''A painting of a squirrel eating a burger''',
								    '''image''': image,
								    '''original_image''': original_image,
								    '''mask_image''': mask_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				:							Union[str, Any]	)							->	Union[str, Any]:
								self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3	)





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





							@unittest.skipIf(torch_device != '''cuda'''     ,		reason='''float16 requires CUDA'''	)
							def       snake_case__						(      self				:							str	)							->	Tuple:
								# 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]	)							->	Union[str, Any]:
								self._test_attention_slicing_forward_pass(expected_max_diff=1e-2	)





							def       snake_case__						(      self				:							Tuple	)							->	str:
								self._test_save_load_local()






							def       snake_case__						(      self				:							Any	)							->	Tuple:
								self._test_inference_batch_single_identical(
								    expected_max_diff=1e-2     ,		)

'''simple docstring'''


import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory





def       lowerCAmelCase					(		):


	"""simple docstring"""
	raise RuntimeError('''CUDA out of memory.'''       )






class 						A   (   nn.Module   ):





							def __init__(      self				:							Optional[Any]	)							->	int:
								super().__init__()
								__UpperCAmelCase							=			nn.Linear(3     ,		4	)
								__UpperCAmelCase							=			nn.BatchNormad(4	)
								__UpperCAmelCase							=			nn.Linear(4     ,		5	)






							def       snake_case__						(      self				:							List[str]     ,		__a				:							Optional[int]	)							->	Optional[int]:
								return self.lineara(self.batchnorm(self.lineara(__a	)	)	)






class 						A   (   unittest.TestCase   ):





							def       snake_case__						(      self				:							Optional[int]	)							->	Any:
								__UpperCAmelCase							=			[]

								@find_executable_batch_size(starting_batch_size=1_2_8	)
								def mock_training_loop_function(__a				:							Union[str, Any]	):
									nonlocal batch_sizes
									batch_sizes.append(__a	)
									if batch_size != 8:
										raise_fake_out_of_memory()

								mock_training_loop_function()
								self.assertListEqual(__a     ,		[1_2_8, 6_4, 3_2, 1_6, 8]	)





							def       snake_case__						(      self				:							str	)							->	int:
								__UpperCAmelCase							=			[]

								@find_executable_batch_size(starting_batch_size=1_2_8	)
								def mock_training_loop_function(__a				:							str     ,		__a				:							Optional[int]	):
									nonlocal batch_sizes
									batch_sizes.append(__a	)
									if batch_size != 8:
										raise_fake_out_of_memory()
									return batch_size, arga

								__UpperCAmelCase				,			__UpperCAmelCase							=			mock_training_loop_function('''hello'''	)
								self.assertListEqual(__a     ,		[1_2_8, 6_4, 3_2, 1_6, 8]	)
								self.assertListEqual([bs, arga]     ,		[8, '''hello''']	)





							def       snake_case__						(      self				:							Any	)							->	int:
								@find_executable_batch_size(starting_batch_size=0	)
								def mock_training_loop_function(__a				:							Optional[int]	):
									pass

								with self.assertRaises(__a	) as cm:
									mock_training_loop_function()
									self.assertIn('''No executable batch size found, reached zero.'''     ,		cm.exception.args[0]	)





							def       snake_case__						(      self				:							Any	)							->	List[Any]:
								@find_executable_batch_size(starting_batch_size=1_6	)
								def mock_training_loop_function(__a				:							Dict	):
									if batch_size > 0:
										raise_fake_out_of_memory()
									pass

								with self.assertRaises(__a	) as cm:
									mock_training_loop_function()
									self.assertIn('''No executable batch size found, reached zero.'''     ,		cm.exception.args[0]	)





							def       snake_case__						(      self				:							List[Any]	)							->	List[str]:
								@find_executable_batch_size(starting_batch_size=1_2_8	)
								def mock_training_loop_function(__a				:							str     ,		__a				:							Union[str, Any]     ,		__a				:							int	):
									if batch_size != 8:
										raise raise_fake_out_of_memory()

								with self.assertRaises(__a	) as cm:
									mock_training_loop_function(1_2_8     ,		'''hello'''     ,		'''world'''	)
									self.assertIn('''Batch size was passed into `f`'''     ,		cm.exception.args[0]	)
									self.assertIn('''`f(arg1=\'hello\', arg2=\'world\')'''     ,		cm.exception.args[0]	)





							def       snake_case__						(      self				:							Tuple	)							->	Optional[Any]:
								@find_executable_batch_size(starting_batch_size=1_6	)
								def mock_training_loop_function(__a				:							Tuple	):
									raise ValueError('''Oops, we had an error!'''	)

								with self.assertRaises(__a	) as cm:
									mock_training_loop_function()
									self.assertIn('''Oops, we had an error!'''     ,		cm.exception.args[0]	)






							@require_cuda
							def       snake_case__						(      self				:							Any	)							->	List[Any]:
								__UpperCAmelCase							=			torch.cuda.memory_allocated()
								__UpperCAmelCase							=			ModelForTest()
								model.cuda()
								self.assertGreater(torch.cuda.memory_allocated()     ,		__a	)
								__UpperCAmelCase							=			release_memory(__a	)
								self.assertEqual(torch.cuda.memory_allocated()     ,		__a	)
