
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
							from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


lowercase__		:Optional[int]   =    logging.get_logger(__name__)

lowercase__		:List[str]   =    {
    "bigscience/bloom": "https://huggingface.co/bigscience/bloom/resolve/main/config.json",
    "bigscience/bloom-560m": "https://huggingface.co/bigscience/bloom-560m/blob/main/config.json",
    "bigscience/bloom-1b1": "https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json",
    "bigscience/bloom-1b7": "https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json",
    "bigscience/bloom-3b": "https://huggingface.co/bigscience/bloom-3b/blob/main/config.json",
    "bigscience/bloom-7b1": "https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json",
}







class 				lowercase		(					SCREAMING_SNAKE_CASE__		):
							lowercase_       :       Optional[Any]       					='''bloom'''
							lowercase_       :       List[str]       					=['''past_key_values''']
							lowercase_       :       Tuple       					={
							    '''num_hidden_layers''': '''n_layer''',
							    '''num_attention_heads''': '''n_head''',
							}




							def __init__(				self   ,A__=2_5_0_8_8_0   ,A__=6_4   ,A__=2   ,A__=8   ,A__=1E-5   ,A__=0.02   ,A__=True   ,A__=1   ,A__=2   ,A__=False   ,A__=0.0   ,A__=0.0   ,A__=1   ,A__=False   ,**A__   ,):
												lowercase							=						vocab_size
												# Backward compatibility with n_embed kwarg
												lowercase							=						kwargs.pop('''n_embed'''   ,A__)
												lowercase							=						hidden_size if n_embed is None else n_embed
												lowercase							=						n_layer
												lowercase							=						n_head
												lowercase							=						layer_norm_epsilon
												lowercase							=						initializer_range
												lowercase							=						use_cache
												lowercase							=						pretraining_tp
												lowercase							=						apply_residual_connection_post_layernorm
												lowercase							=						hidden_dropout
												lowercase							=						attention_dropout

												lowercase							=						bos_token_id
												lowercase							=						eos_token_id
												lowercase							=						slow_but_exact

												super().__init__(bos_token_id=A__   ,eos_token_id=A__   ,**A__)







class 				lowercase		(					SCREAMING_SNAKE_CASE__		):
							lowercase_       :       List[str]       					=version.parse('''1.12'''		)




							def __init__(				self   ,A__   ,A__ = "default"   ,A__ = None   ,A__ = False   ,):
												super().__init__(A__   ,task=A__   ,patching_specs=A__   ,use_past=A__)
												if not getattr(self._config   ,'''pad_token_id'''   ,A__):
																	# TODO: how to do that better?
																	lowercase							=						0




							@property
							def  A__      (				self):
												lowercase							=						OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}})
												if self.use_past:
																	# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
																	self.fill_with_past_key_values_(A__   ,direction='''inputs'''   ,inverted_values_shape=A__)
																	lowercase							=						{0: '''batch''', 1: '''past_sequence + sequence'''}
												else:
																	lowercase							=						{0: '''batch''', 1: '''sequence'''}

												return common_inputs




							@property
							def  A__      (				self):
												return self._config.n_layer




							@property
							def  A__      (				self):
												return self._config.n_head




							@property
							def  A__      (				self):
												return 1E-3




							def  A__      (				self   ,A__   ,A__ = -1   ,A__ = -1   ,A__ = False   ,A__ = None   ,):
												lowercase							=						super(A__   ,self).generate_dummy_inputs(
												    A__   ,batch_size=A__   ,seq_length=A__   ,is_pair=A__   ,framework=A__)

												# We need to order the input in the way they appears in the forward()
												lowercase							=						OrderedDict({'''input_ids''': common_inputs['''input_ids''']})

												# Need to add the past_keys
												if self.use_past:
																	if not is_torch_available():
																						raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''')
																	else:
																						import torch

																						lowercase	,						lowercase							=						common_inputs['''input_ids'''].shape
																						# Not using the same length for past_key_values
																						lowercase							=						seqlen + 2
																						lowercase							=						self._config.hidden_size // self.num_attention_heads
																						lowercase							=						(
																						    batch * self.num_attention_heads,
																						    head_dim,
																						    past_key_values_length,
																						)
																						lowercase							=						(
																						    batch * self.num_attention_heads,
																						    past_key_values_length,
																						    head_dim,
																						)
																						lowercase							=						[
																						    (torch.zeros(A__), torch.zeros(A__)) for _ in range(self.num_layers)
																						]

												lowercase							=						common_inputs['''attention_mask''']
												if self.use_past:
																	lowercase							=						ordered_inputs['''attention_mask'''].dtype
																	lowercase							=						torch.cat(
																	    [ordered_inputs['''attention_mask'''], torch.ones(A__   ,A__   ,dtype=A__)]   ,dim=1)

												return ordered_inputs





							@property
							def  A__      (				self):
												return 1_3






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 				lowercase		(					SCREAMING_SNAKE_CASE__       ,  SCREAMING_SNAKE_CASE__       ,  unittest.TestCase		):
							lowercase_       :       Optional[Any]       					=IFPipeline
							lowercase_       :       List[str]       					=TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''}
							lowercase_       :       List[str]       					=TEXT_TO_IMAGE_BATCH_PARAMS
							lowercase_       :       int       					=PipelineTesterMixin.required_optional_params - {'''latents'''}




							def  A__      (				self):
												return self._get_dummy_components()




							def  A__      (				self   ,A__   ,A__=0):
												if str(A__).startswith('''mps'''):
																	lowercase							=						torch.manual_seed(A__)
												else:
																	lowercase							=						torch.Generator(device=A__).manual_seed(A__)

												lowercase							=						{
												    '''prompt''': '''A painting of a squirrel eating a burger''',
												    '''generator''': generator,
												    '''num_inference_steps''': 2,
												    '''output_type''': '''numpy''',
												}

												return inputs




							def  A__      (				self):
												self._test_save_load_optional_components()




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




							def  A__      (				self):
												self._test_save_load_local()




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







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




							def  A__      (				self):
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




							def  A__      (				self):
												# if

												lowercase							=						IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0'''   ,variant='''fp16'''   ,torch_dtype=torch.floataa)

												lowercase							=						IFSuperResolutionPipeline.from_pretrained(
												    '''DeepFloyd/IF-II-L-v1.0'''   ,variant='''fp16'''   ,torch_dtype=torch.floataa   ,text_encoder=A__   ,tokenizer=A__)

												# pre compute text embeddings and remove T5 to save memory

												pipe_a.text_encoder.to('''cuda''')

												lowercase	,						lowercase							=						pipe_a.encode_prompt('''anime turtle'''   ,device='''cuda''')

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

												lowercase							=						None
												lowercase							=						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(A__   ,A__   ,A__   ,A__)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# img2img

												lowercase							=						IFImgaImgPipeline(**pipe_a.components)
												lowercase							=						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(A__   ,A__   ,A__   ,A__)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# inpainting

												lowercase							=						IFInpaintingPipeline(**pipe_a.components)
												lowercase							=						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(A__   ,A__   ,A__   ,A__)




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												# pipeline 1

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)
												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,num_inference_steps=2   ,generator=A__   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (6_4, 6_4, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 1_3 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''')
												assert_mean_pixel_difference(A__   ,A__)

												# pipeline 2

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,generator=A__   ,num_inference_steps=2   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (2_5_6, 2_5_6, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''')
												assert_mean_pixel_difference(A__   ,A__)




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												# pipeline 1

												_start_torch_memory_measurement()

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,num_inference_steps=2   ,generator=A__   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (6_4, 6_4, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 1_0 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''')
												assert_mean_pixel_difference(A__   ,A__)

												# pipeline 2

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,original_image=A__   ,generator=A__   ,num_inference_steps=2   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (2_5_6, 2_5_6, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 1_0**9

												lowercase							=						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(A__   ,A__)





							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												# pipeline 1

												_start_torch_memory_measurement()

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(1)).to(A__)

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)
												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,mask_image=A__   ,num_inference_steps=2   ,generator=A__   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (6_4, 6_4, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 1_0 * 1_0**9

												lowercase							=						load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''')
												assert_mean_pixel_difference(A__   ,A__)

												# pipeline 2

												_start_torch_memory_measurement()

												lowercase							=						torch.Generator(device='''cpu''').manual_seed(0)

												lowercase							=						floats_tensor((1, 3, 6_4, 6_4)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,rng=random.Random(0)).to(A__)
												lowercase							=						floats_tensor((1, 3, 2_5_6, 2_5_6)   ,rng=random.Random(1)).to(A__)

												lowercase							=						pipe_a(
												    prompt_embeds=A__   ,negative_prompt_embeds=A__   ,image=A__   ,mask_image=A__   ,original_image=A__   ,generator=A__   ,num_inference_steps=2   ,output_type='''np'''   ,)

												lowercase							=						output.images[0]

												assert image.shape == (2_5_6, 2_5_6, 3)

												lowercase							=						torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 1_0**9

												lowercase							=						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(A__   ,A__)



def        UpperCamelCase    (							):
					'''simple docstring'''
					torch.cuda.empty_cache()
					torch.cuda.reset_max_memory_allocated()
					torch.cuda.reset_peak_memory_stats()




