





"""simple docstring"""




import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import BlenderbotConfig, is_flax_available
from transformers.testing_utils import jax_device, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
		import os

		# The slow tests are often failing with OOM error on GPU
		# This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
		# but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
		a		:				List[str]  			=    '''platform'''

		import jax
		import jax.numpy as jnp

		from transformers import BlenderbotTokenizer
		from transformers.models.blenderbot.modeling_flax_blenderbot import (
		    FlaxBlenderbotForConditionalGeneration,
		    FlaxBlenderbotModel,
		    shift_tokens_right,
		)






def    _SCREAMING_SNAKE_CASE	(			_lowercase		:					Union[str, Any]					, _lowercase		:					List[str]					, _lowercase		:					Optional[Any]=None					, _lowercase		:					List[Any]=None					, _lowercase		:					List[str]=None					, _lowercase		:					Optional[int]=None					, _lowercase		:					Tuple=None					, _lowercase		:					str=None					, )				->int:






	'''simple docstring'''






	if attention_mask is None:
		a   :	str			=       np.where(input_ids != config.pad_token_id					, 1					, 0      )
	if decoder_attention_mask is None:
		a   :	Dict			=       np.where(decoder_input_ids != config.pad_token_id					, 1					, 0      )
	if head_mask is None:
		a   :	Union[str, Any]			=       np.ones((config.encoder_layers, config.encoder_attention_heads)      )
	if decoder_head_mask is None:
		a   :	Tuple			=       np.ones((config.decoder_layers, config.decoder_attention_heads)      )
	if cross_attn_head_mask is None:
		a   :	List[Any]			=       np.ones((config.decoder_layers, config.decoder_attention_heads)      )
	return {
	    "input_ids": input_ids,
	    "decoder_input_ids": decoder_input_ids,
	    "attention_mask": attention_mask,
	    "decoder_attention_mask": attention_mask,
	}


class  __UpperCamelCase       :






							def __init__(     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__=13   ,      lowerCAmelCase__=7   ,      lowerCAmelCase__=True   ,      lowerCAmelCase__=False   ,      lowerCAmelCase__=99   ,      lowerCAmelCase__=16   ,      lowerCAmelCase__=2   ,      lowerCAmelCase__=4   ,      lowerCAmelCase__=4   ,      lowerCAmelCase__="gelu"   ,      lowerCAmelCase__=0.1   ,      lowerCAmelCase__=0.1   ,      lowerCAmelCase__=32   ,      lowerCAmelCase__=2   ,      lowerCAmelCase__=1   ,      lowerCAmelCase__=0   ,      lowerCAmelCase__=0.02   ,      )			->				List[Any]:
								a   :	Any			=       parent
								a   :	Optional[Any]			=       batch_size
								a   :	Any			=       seq_length
								a   :	Tuple			=       is_training
								a   :	Optional[Any]			=       use_labels
								a   :	int			=       vocab_size
								a   :	List[Any]			=       hidden_size
								a   :	int			=       num_hidden_layers
								a   :	Optional[Any]			=       num_attention_heads
								a   :	List[str]			=       intermediate_size
								a   :	Optional[int]			=       hidden_act
								a   :	Optional[int]			=       hidden_dropout_prob
								a   :	Tuple			=       attention_probs_dropout_prob
								a   :	Dict			=       max_position_embeddings
								a   :	Optional[Any]			=       eos_token_id
								a   :	Tuple			=       pad_token_id
								a   :	Dict			=       bos_token_id
								a   :	Union[str, Any]			=       initializer_range






							def 					__a     (     self   )			->				Optional[Any]:
								a   :	Dict			=       np.clip(ids_tensor([self.batch_size, self.seq_length - 1]   ,      self.vocab_size   )   ,      3   ,      self.vocab_size   )
								a   :	Optional[Any]			=       np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)   ,      dtype=np.intaa   ))   ,      -1   )

								a   :	Union[str, Any]			=       shift_tokens_right(lowerCAmelCase__   ,      1   ,      2   )

								a   :	List[str]			=       BlenderbotConfig(
								    vocab_size=self.vocab_size   ,      d_model=self.hidden_size   ,      encoder_layers=self.num_hidden_layers   ,      decoder_layers=self.num_hidden_layers   ,      encoder_attention_heads=self.num_attention_heads   ,      decoder_attention_heads=self.num_attention_heads   ,      encoder_ffn_dim=self.intermediate_size   ,      decoder_ffn_dim=self.intermediate_size   ,      dropout=self.hidden_dropout_prob   ,      attention_dropout=self.attention_probs_dropout_prob   ,      max_position_embeddings=self.max_position_embeddings   ,      eos_token_id=self.eos_token_id   ,      bos_token_id=self.bos_token_id   ,      pad_token_id=self.pad_token_id   ,      initializer_range=self.initializer_range   ,      use_cache=lowerCAmelCase__   ,      )
								a   :	Optional[int]			=       prepare_blenderbot_inputs_dict(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )
								return config, inputs_dict






							def 					__a     (     self   )			->				Optional[int]:
								a, a   :	List[str]			=       self.prepare_config_and_inputs()
								return config, inputs_dict






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )			->				Optional[int]:
								a   :	Dict			=       20
								a   :	Dict			=       model_class_name(lowerCAmelCase__   )

								a   :	Optional[Any]			=       model.encode(inputs_dict["input_ids"]   )

								a, a   :	Optional[int]			=       (
								    inputs_dict["decoder_input_ids"],
								    inputs_dict["decoder_attention_mask"],
								)

								a   :	List[Any]			=       model.init_cache(decoder_input_ids.shape[0]   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )
								a   :	List[Any]			=       jnp.ones((decoder_input_ids.shape[0], max_decoder_length)   ,      dtype="i4"   )

								a   :	Tuple			=       jnp.broadcast_to(
								    jnp.arange(decoder_input_ids.shape[-1] - 1   )[None, :]   ,      (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1)   ,      )
								a   :	List[Any]			=       model.decode(
								    decoder_input_ids[:, :-1]   ,      lowerCAmelCase__   ,      decoder_attention_mask=lowerCAmelCase__   ,      past_key_values=lowerCAmelCase__   ,      decoder_position_ids=lowerCAmelCase__   ,      )

								a   :	Tuple			=       jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]]   ,      dtype="i4"   )
								a   :	Dict			=       model.decode(
								    decoder_input_ids[:, -1:]   ,      lowerCAmelCase__   ,      decoder_attention_mask=lowerCAmelCase__   ,      past_key_values=outputs_cache.past_key_values   ,      decoder_position_ids=lowerCAmelCase__   ,      )

								a   :	List[Any]			=       model.decode(lowerCAmelCase__   ,      lowerCAmelCase__   )

								a   :	List[str]			=       np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])   )   )
								self.parent.assertTrue(diff < 1E-3   ,      msg=f"""Max diff is {diff}"""   )






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )			->				Optional[int]:
								a   :	List[Any]			=       20
								a   :	Optional[Any]			=       model_class_name(lowerCAmelCase__   )

								a   :	Optional[int]			=       model.encode(inputs_dict["input_ids"]   )

								a, a   :	Union[str, Any]			=       (
								    inputs_dict["decoder_input_ids"],
								    inputs_dict["decoder_attention_mask"],
								)

								a   :	List[str]			=       jnp.concatenate(
								    [
								        decoder_attention_mask,
								        jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1])   ),
								    ]   ,      axis=-1   ,      )

								a   :	Union[str, Any]			=       model.init_cache(decoder_input_ids.shape[0]   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )
								a   :	Dict			=       jnp.broadcast_to(
								    jnp.arange(decoder_input_ids.shape[-1] - 1   )[None, :]   ,      (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1)   ,      )

								a   :	int			=       model.decode(
								    decoder_input_ids[:, :-1]   ,      lowerCAmelCase__   ,      decoder_attention_mask=lowerCAmelCase__   ,      past_key_values=lowerCAmelCase__   ,      decoder_position_ids=lowerCAmelCase__   ,      )
								a   :	List[Any]			=       jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]]   ,      dtype="i4"   )
								a   :	List[Any]			=       model.decode(
								    decoder_input_ids[:, -1:]   ,      lowerCAmelCase__   ,      past_key_values=outputs_cache.past_key_values   ,      decoder_attention_mask=lowerCAmelCase__   ,      decoder_position_ids=lowerCAmelCase__   ,      )

								a   :	Union[str, Any]			=       model.decode(lowerCAmelCase__   ,      lowerCAmelCase__   ,      decoder_attention_mask=lowerCAmelCase__   )

								a   :	Union[str, Any]			=       np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])   )   )
								self.parent.assertTrue(diff < 1E-3   ,      msg=f"""Max diff is {diff}"""   )


@require_flax
class  __UpperCamelCase       (		unittest.TestCase		):
							lowerCamelCase     :      int         =99






							def 					__a     (     self   )			->				Union[str, Any]:
								a   :	int			=       np.array(
								    [
								        [71, 82, 18, 33, 46, 91, 2],
								        [68, 34, 26, 58, 30, 82, 2],
								        [5, 97, 17, 39, 94, 40, 2],
								        [76, 83, 94, 25, 70, 78, 2],
								        [87, 59, 41, 35, 48, 66, 2],
								        [55, 13, 16, 58, 5, 2, 1],  # note padding
								        [64, 27, 31, 51, 12, 75, 2],
								        [52, 64, 86, 17, 83, 39, 2],
								        [48, 61, 9, 24, 71, 82, 2],
								        [26, 1, 60, 48, 22, 13, 2],
								        [21, 5, 62, 28, 14, 76, 2],
								        [45, 98, 37, 86, 59, 48, 2],
								        [70, 70, 50, 9, 28, 0, 2],
								    ]   ,      dtype=np.intaa   ,      )

								a   :	List[Any]			=       input_ids.shape[0]
								a   :	Any			=       BlenderbotConfig(
								    vocab_size=self.vocab_size   ,      d_model=24   ,      encoder_layers=2   ,      decoder_layers=2   ,      encoder_attention_heads=2   ,      decoder_attention_heads=2   ,      encoder_ffn_dim=32   ,      decoder_ffn_dim=32   ,      max_position_embeddings=48   ,      eos_token_id=2   ,      pad_token_id=1   ,      bos_token_id=0   ,      )
								return config, input_ids, batch_size






							def 					__a     (     self   )			->				Any:
								a, a, a   :	str			=       self._get_config_and_data()
								a   :	str			=       FlaxBlenderbotForConditionalGeneration(lowerCAmelCase__   )
								a   :	List[str]			=       lm_model(input_ids=lowerCAmelCase__   )
								a   :	Optional[Any]			=       (batch_size, input_ids.shape[1], config.vocab_size)
								self.assertEqual(outputs["logits"].shape   ,      lowerCAmelCase__   )






							def 					__a     (     self   )			->				str:
								a   :	Dict			=       BlenderbotConfig(
								    vocab_size=self.vocab_size   ,      d_model=14   ,      encoder_layers=2   ,      decoder_layers=2   ,      encoder_attention_heads=2   ,      decoder_attention_heads=2   ,      encoder_ffn_dim=8   ,      decoder_ffn_dim=8   ,      max_position_embeddings=48   ,      )
								a   :	List[Any]			=       FlaxBlenderbotForConditionalGeneration(lowerCAmelCase__   )
								a   :	List[Any]			=       np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]]   ,      dtype=np.intaa   )
								a   :	str			=       np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]]   ,      dtype=np.intaa   )
								a   :	List[Any]			=       lm_model(input_ids=lowerCAmelCase__   ,      decoder_input_ids=lowerCAmelCase__   )
								a   :	List[Any]			=       (*summary.shape, config.vocab_size)
								self.assertEqual(outputs["logits"].shape   ,      lowerCAmelCase__   )






							def 					__a     (     self   )			->				List[Any]:
								a   :	Optional[int]			=       np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]]   ,      dtype=np.intaa   )
								a   :	Optional[Any]			=       shift_tokens_right(lowerCAmelCase__   ,      1   ,      2   )
								a   :	Dict			=       np.equal(lowerCAmelCase__   ,      1   ).astype(np.floataa   ).sum()
								a   :	Optional[Any]			=       np.equal(lowerCAmelCase__   ,      1   ).astype(np.floataa   ).sum()
								self.assertEqual(shifted.shape   ,      input_ids.shape   )
								self.assertEqual(lowerCAmelCase__   ,      n_pad_before - 1   )
								self.assertTrue(np.equal(shifted[:, 0]   ,      2   ).all()   )


@require_flax
class  __UpperCamelCase       (		a__	, unittest.TestCase	, a__		):
							lowerCamelCase     :      Optional[int]         =True
							lowerCamelCase     :      Dict         =(
							    (
							        FlaxBlenderbotModel,
							        FlaxBlenderbotForConditionalGeneration,
							    )
							    if is_flax_available()
							    else ()
							)
							lowerCamelCase     :      Dict         =(FlaxBlenderbotForConditionalGeneration,) if is_flax_available() else ()






							def 					__a     (     self   )			->				Dict:
								a   :	Union[str, Any]			=       FlaxBlenderbotModelTester(self   )






							def 					__a     (     self   )			->				Optional[Any]:
								a, a   :	Tuple			=       self.model_tester.prepare_config_and_inputs()
								for model_class in self.all_model_classes:
									self.model_tester.check_use_cache_forward(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )






							def 					__a     (     self   )			->				List[str]:
								a, a   :	Dict			=       self.model_tester.prepare_config_and_inputs()
								for model_class in self.all_model_classes:
									self.model_tester.check_use_cache_forward_with_attn_mask(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )






							def 					__a     (     self   )			->				str:
								a, a   :	Any			=       self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									with self.subTest(model_class.__name__   ):
										a   :	Any			=       self._prepare_for_class(lowerCAmelCase__   ,      lowerCAmelCase__   )
										a   :	int			=       model_class(lowerCAmelCase__   )

										@jax.jit
										def encode_jitted(lowerCAmelCase__   ,      lowerCAmelCase__=None   ,      **lowerCAmelCase__   ):
											return model.encode(input_ids=lowerCAmelCase__   ,      attention_mask=lowerCAmelCase__   )

										with self.subTest("JIT Enabled"   ):
											a   :	Union[str, Any]			=       encode_jitted(**lowerCAmelCase__   ).to_tuple()

										with self.subTest("JIT Disabled"   ):
											with jax.disable_jit():
												a   :	Optional[Any]			=       encode_jitted(**lowerCAmelCase__   ).to_tuple()

										self.assertEqual(len(lowerCAmelCase__   )   ,      len(lowerCAmelCase__   )   )
										for jitted_output, output in zip(lowerCAmelCase__   ,      lowerCAmelCase__   ):
											self.assertEqual(jitted_output.shape   ,      output.shape   )






							def 					__a     (     self   )			->				str:
								a, a   :	Optional[Any]			=       self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									with self.subTest(model_class.__name__   ):
										a   :	Optional[int]			=       model_class(lowerCAmelCase__   )
										a   :	List[str]			=       model.encode(inputs_dict["input_ids"]   ,      inputs_dict["attention_mask"]   )

										a   :	Dict			=       {
										    "decoder_input_ids": inputs_dict["decoder_input_ids"],
										    "decoder_attention_mask": inputs_dict["decoder_attention_mask"],
										    "encoder_outputs": encoder_outputs,
										}

										@jax.jit
										def decode_jitted(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ):
											return model.decode(
											    decoder_input_ids=lowerCAmelCase__   ,      decoder_attention_mask=lowerCAmelCase__   ,      encoder_outputs=lowerCAmelCase__   ,      )

										with self.subTest("JIT Enabled"   ):
											a   :	List[str]			=       decode_jitted(**lowerCAmelCase__   ).to_tuple()

										with self.subTest("JIT Disabled"   ):
											with jax.disable_jit():
												a   :	Union[str, Any]			=       decode_jitted(**lowerCAmelCase__   ).to_tuple()

										self.assertEqual(len(lowerCAmelCase__   )   ,      len(lowerCAmelCase__   )   )
										for jitted_output, output in zip(lowerCAmelCase__   ,      lowerCAmelCase__   ):
											self.assertEqual(jitted_output.shape   ,      output.shape   )






							@slow
							def 					__a     (     self   )			->				Tuple:
								for model_class_name in self.all_model_classes:
									a   :	Optional[int]			=       model_class_name.from_pretrained("facebook/blenderbot-400M-distill"   )
									# FlaxBlenderbotForSequenceClassification expects eos token in input_ids
									a   :	Union[str, Any]			=       np.ones((1, 1)   ) * model.config.eos_token_id
									a   :	str			=       model(lowerCAmelCase__   )
									self.assertIsNotNone(lowerCAmelCase__   )






							@unittest.skipUnless(jax_device != "cpu"   ,      "3B test too slow on CPU."   )
							@slow
							def 					__a     (     self   )			->				List[Any]:
								a   :	Optional[Any]			=       {"num_beams": 1, "early_stopping": True, "min_length": 15, "max_length": 25}
								a   :	Tuple			=       {"skip_special_tokens": True, "clean_up_tokenization_spaces": True}

								a   :	Optional[int]			=       FlaxBlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-3B"   ,      from_pt=lowerCAmelCase__   )
								a   :	Tuple			=       BlenderbotTokenizer.from_pretrained("facebook/blenderbot-3B"   )

								a   :	Union[str, Any]			=       ["Sam"]
								a   :	Union[str, Any]			=       tokenizer(lowerCAmelCase__   ,      return_tensors="jax"   )

								a   :	int			=       model.generate(**lowerCAmelCase__   ,      **lowerCAmelCase__   )
								a   :	str			=       "Sam is a great name. It means \"sun\" in Gaelic."

								a   :	Optional[int]			=       tokenizer.batch_decode(lowerCAmelCase__   ,      **lowerCAmelCase__   )
								assert generated_txt[0].strip() == tgt_text









"""simple docstring"""




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  __UpperCamelCase       (		a__	, a__	, unittest.TestCase		):
							lowerCamelCase     :      Dict         =IFPipeline
							lowerCamelCase     :      int         =TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""}
							lowerCamelCase     :      int         =TEXT_TO_IMAGE_BATCH_PARAMS
							lowerCamelCase     :      int         =PipelineTesterMixin.required_optional_params - {"""latents"""}






							def 					__a     (     self   )			->				List[str]:
								return self._get_dummy_components()






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__=0   )			->				Dict:
								if str(lowerCAmelCase__   ).startswith("mps"   ):
									a   :	Tuple			=       torch.manual_seed(lowerCAmelCase__   )
								else:
									a   :	int			=       torch.Generator(device=lowerCAmelCase__   ).manual_seed(lowerCAmelCase__   )

								a   :	Optional[Any]			=       {
								    "prompt": "A painting of a squirrel eating a burger",
								    "generator": generator,
								    "num_inference_steps": 2,
								    "output_type": "numpy",
								}

								return inputs






							def 					__a     (     self   )			->				Union[str, Any]:
								self._test_save_load_optional_components()






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






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






							def 					__a     (     self   )			->				Tuple:
								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   )			->				str:
								self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )


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






							def 					__a     (     self   )			->				Optional[Any]:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()






							def 					__a     (     self   )			->				Tuple:
								# if

								a   :	Tuple			=       IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0"   ,      variant="fp16"   ,      torch_dtype=torch.floataa   )

								a   :	str			=       IFSuperResolutionPipeline.from_pretrained(
								    "DeepFloyd/IF-II-L-v1.0"   ,      variant="fp16"   ,      torch_dtype=torch.floataa   ,      text_encoder=lowerCAmelCase__   ,      tokenizer=lowerCAmelCase__   )

								# pre compute text embeddings and remove T5 to save memory

								pipe_a.text_encoder.to("cuda"   )

								a, a   :	List[str]			=       pipe_a.encode_prompt("anime turtle"   ,      device="cuda"   )

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

								a   :	Optional[int]			=       None
								a   :	Optional[int]			=       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(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )

								pipe_a.remove_all_hooks()
								pipe_a.remove_all_hooks()

								# img2img

								a   :	Union[str, Any]			=       IFImgaImgPipeline(**pipe_a.components   )
								a   :	List[Any]			=       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(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )

								pipe_a.remove_all_hooks()
								pipe_a.remove_all_hooks()

								# inpainting

								a   :	Union[str, Any]			=       IFInpaintingPipeline(**pipe_a.components   )
								a   :	List[str]			=       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(lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )			->				Dict:
								# pipeline 1

								_start_torch_memory_measurement()

								a   :	List[str]			=       torch.Generator(device="cpu"   ).manual_seed(0   )
								a   :	Dict			=       pipe_a(
								    prompt_embeds=lowerCAmelCase__   ,      negative_prompt_embeds=lowerCAmelCase__   ,      num_inference_steps=2   ,      generator=lowerCAmelCase__   ,      output_type="np"   ,      )

								a   :	List[str]			=       output.images[0]

								assert image.shape == (64, 64, 3)

								a   :	Dict			=       torch.cuda.max_memory_allocated()
								assert mem_bytes < 13 * 10**9

								a   :	Optional[Any]			=       load_numpy(
								    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy"   )
								assert_mean_pixel_difference(lowerCAmelCase__   ,      lowerCAmelCase__   )

								# pipeline 2

								_start_torch_memory_measurement()

								a   :	List[str]			=       torch.Generator(device="cpu"   ).manual_seed(0   )

								a   :	Optional[int]			=       floats_tensor((1, 3, 64, 64)   ,      rng=random.Random(0   )   ).to(lowerCAmelCase__   )

								a   :	Union[str, Any]			=       pipe_a(
								    prompt_embeds=lowerCAmelCase__   ,      negative_prompt_embeds=lowerCAmelCase__   ,      image=lowerCAmelCase__   ,      generator=lowerCAmelCase__   ,      num_inference_steps=2   ,      output_type="np"   ,      )

								a   :	List[str]			=       output.images[0]

								assert image.shape == (256, 256, 3)

								a   :	int			=       torch.cuda.max_memory_allocated()
								assert mem_bytes < 4 * 10**9

								a   :	Union[str, Any]			=       load_numpy(
								    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy"   )
								assert_mean_pixel_difference(lowerCAmelCase__   ,      lowerCAmelCase__   )






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )			->				int:
								# pipeline 1

								_start_torch_memory_measurement()

								a   :	int			=       floats_tensor((1, 3, 64, 64)   ,      rng=random.Random(0   )   ).to(lowerCAmelCase__   )

								a   :	Tuple			=       torch.Generator(device="cpu"   ).manual_seed(0   )

								a   :	List[Any]			=       pipe_a(
								    prompt_embeds=lowerCAmelCase__   ,      negative_prompt_embeds=lowerCAmelCase__   ,      image=lowerCAmelCase__   ,      num_inference_steps=2   ,      generator=lowerCAmelCase__   ,      output_type="np"   ,      )

								a   :	Tuple			=       output.images[0]

								assert image.shape == (64, 64, 3)

								a   :	int			=       torch.cuda.max_memory_allocated()
								assert mem_bytes < 10 * 10**9

								a   :	Optional[int]			=       load_numpy(
								    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy"   )
								assert_mean_pixel_difference(lowerCAmelCase__   ,      lowerCAmelCase__   )

								# pipeline 2

								_start_torch_memory_measurement()

								a   :	Optional[int]			=       torch.Generator(device="cpu"   ).manual_seed(0   )

								a   :	List[Any]			=       floats_tensor((1, 3, 256, 256)   ,      rng=random.Random(0   )   ).to(lowerCAmelCase__   )
								a   :	str			=       floats_tensor((1, 3, 64, 64)   ,      rng=random.Random(0   )   ).to(lowerCAmelCase__   )

								a   :	Dict			=       pipe_a(
								    prompt_embeds=lowerCAmelCase__   ,      negative_prompt_embeds=lowerCAmelCase__   ,      image=lowerCAmelCase__   ,      original_image=lowerCAmelCase__   ,      generator=lowerCAmelCase__   ,      num_inference_steps=2   ,      output_type="np"   ,      )

								a   :	int			=       output.images[0]

								assert image.shape == (256, 256, 3)

								a   :	str			=       torch.cuda.max_memory_allocated()
								assert mem_bytes < 4 * 10**9

								a   :	Any			=       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(lowerCAmelCase__   ,      lowerCAmelCase__   )






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )			->				Optional[int]:
								# pipeline 1

								_start_torch_memory_measurement()

								a   :	Union[str, Any]			=       floats_tensor((1, 3, 64, 64)   ,      rng=random.Random(0   )   ).to(lowerCAmelCase__   )
								a   :	List[Any]			=       floats_tensor((1, 3, 64, 64)   ,      rng=random.Random(1   )   ).to(lowerCAmelCase__   )

								a   :	Optional[int]			=       torch.Generator(device="cpu"   ).manual_seed(0   )
								a   :	List[str]			=       pipe_a(
								    prompt_embeds=lowerCAmelCase__   ,      negative_prompt_embeds=lowerCAmelCase__   ,      image=lowerCAmelCase__   ,      mask_image=lowerCAmelCase__   ,      num_inference_steps=2   ,      generator=lowerCAmelCase__   ,      output_type="np"   ,      )

								a   :	List[Any]			=       output.images[0]

								assert image.shape == (64, 64, 3)

								a   :	Tuple			=       torch.cuda.max_memory_allocated()
								assert mem_bytes < 10 * 10**9

								a   :	Union[str, Any]			=       load_numpy(
								    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy"   )
								assert_mean_pixel_difference(lowerCAmelCase__   ,      lowerCAmelCase__   )

								# pipeline 2

								_start_torch_memory_measurement()

								a   :	str			=       torch.Generator(device="cpu"   ).manual_seed(0   )

								a   :	Optional[int]			=       floats_tensor((1, 3, 64, 64)   ,      rng=random.Random(0   )   ).to(lowerCAmelCase__   )
								a   :	int			=       floats_tensor((1, 3, 256, 256)   ,      rng=random.Random(0   )   ).to(lowerCAmelCase__   )
								a   :	Dict			=       floats_tensor((1, 3, 256, 256)   ,      rng=random.Random(1   )   ).to(lowerCAmelCase__   )

								a   :	Optional[int]			=       pipe_a(
								    prompt_embeds=lowerCAmelCase__   ,      negative_prompt_embeds=lowerCAmelCase__   ,      image=lowerCAmelCase__   ,      mask_image=lowerCAmelCase__   ,      original_image=lowerCAmelCase__   ,      generator=lowerCAmelCase__   ,      num_inference_steps=2   ,      output_type="np"   ,      )

								a   :	List[str]			=       output.images[0]

								assert image.shape == (256, 256, 3)

								a   :	Tuple			=       torch.cuda.max_memory_allocated()
								assert mem_bytes < 4 * 10**9

								a   :	Optional[Any]			=       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(lowerCAmelCase__   ,      lowerCAmelCase__   )






def    _SCREAMING_SNAKE_CASE	(			)				->List[str]:






	'''simple docstring'''






	torch.cuda.empty_cache()
	torch.cuda.reset_max_memory_allocated()
	torch.cuda.reset_peak_memory_stats()


