
"""simple docstring"""




import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class      UpperCamelCase	(       __magic_name__  ,      __magic_name__  ,      unittest.TestCase      ):

				"""simple docstring"""



				UpperCAmelCase_				:      Union[str, Any]						  =				StableDiffusionXLPipeline
				UpperCAmelCase_				:      Dict						  =				TEXT_TO_IMAGE_PARAMS
				UpperCAmelCase_				:      Dict						  =				TEXT_TO_IMAGE_BATCH_PARAMS
				UpperCAmelCase_				:      str						  =				TEXT_TO_IMAGE_IMAGE_PARAMS
				UpperCAmelCase_				:      Optional[Any]						  =				TEXT_TO_IMAGE_IMAGE_PARAMS


				def 							A					(				self			)    ->    Union[str, Any]:
									"""simple docstring"""







									torch.manual_seed(0			)
									SCREAMING_SNAKE_CASE				=	UNetaDConditionModel(
									    block_out_channels=(32, 64)		,	layers_per_block=2		,	sample_size=32		,	in_channels=4		,	out_channels=4		,	down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')		,	up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')		,	attention_head_dim=(2, 4)		,	use_linear_projection=lowercase__		,	addition_embed_type='text_time'		,	addition_time_embed_dim=8		,	transformer_layers_per_block=(1, 2)		,	projection_class_embeddings_input_dim=80		,	cross_attention_dim=64		,	)
									SCREAMING_SNAKE_CASE				=	EulerDiscreteScheduler(
									    beta_start=0.00_085		,	beta_end=0.012		,	steps_offset=1		,	beta_schedule='scaled_linear'		,	timestep_spacing='leading'		,	)
									torch.manual_seed(0			)
									SCREAMING_SNAKE_CASE				=	AutoencoderKL(
									    block_out_channels=[32, 64]		,	in_channels=3		,	out_channels=3		,	down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']		,	up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']		,	latent_channels=4		,	sample_size=128		,	)
									torch.manual_seed(0			)
									SCREAMING_SNAKE_CASE				=	CLIPTextConfig(
									    bos_token_id=0		,	eos_token_id=2		,	hidden_size=32		,	intermediate_size=37		,	layer_norm_eps=1E-05		,	num_attention_heads=4		,	num_hidden_layers=5		,	pad_token_id=1		,	vocab_size=1000		,	hidden_act='gelu'		,	projection_dim=32		,	)
									SCREAMING_SNAKE_CASE				=	CLIPTextModel(lowercase__			)
									SCREAMING_SNAKE_CASE				=	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		,	local_files_only=lowercase__			)

									SCREAMING_SNAKE_CASE				=	CLIPTextModelWithProjection(lowercase__			)
									SCREAMING_SNAKE_CASE				=	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		,	local_files_only=lowercase__			)

									SCREAMING_SNAKE_CASE				=	{
									    'unet': unet,
									    'scheduler': scheduler,
									    'vae': vae,
									    'text_encoder': text_encoder,
									    'tokenizer': tokenizer,
									    'text_encoder_2': text_encoder_a,
									    'tokenizer_2': tokenizer_a,
									    # "safety_checker": None,
									    # "feature_extractor": None,
									}
									return components


				def 							A					(				self		,	lowercase__		,	lowercase__=0			)    ->    Optional[int]:
									"""simple docstring"""







									if str(lowercase__			).startswith('mps'			):
														SCREAMING_SNAKE_CASE				=	torch.manual_seed(lowercase__			)
									else:
														SCREAMING_SNAKE_CASE				=	torch.Generator(device=lowercase__			).manual_seed(lowercase__			)
									SCREAMING_SNAKE_CASE				=	{
									    'prompt': 'A painting of a squirrel eating a burger',
									    'generator': generator,
									    'num_inference_steps': 2,
									    'guidance_scale': 5.0,
									    'output_type': 'numpy',
									}
									return inputs


				def 							A					(				self			)    ->    List[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	'cpu'  # ensure determinism for the device-dependent torch.Generator
									SCREAMING_SNAKE_CASE				=	self.get_dummy_components()
									SCREAMING_SNAKE_CASE				=	StableDiffusionXLPipeline(**lowercase__			)
									SCREAMING_SNAKE_CASE				=	sd_pipe.to(lowercase__			)
									sd_pipe.set_progress_bar_config(disable=lowercase__			)

									SCREAMING_SNAKE_CASE				=	self.get_dummy_inputs(lowercase__			)
									SCREAMING_SNAKE_CASE				=	sd_pipe(**lowercase__			).images
									SCREAMING_SNAKE_CASE				=	image[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									SCREAMING_SNAKE_CASE				=	np.array([0.5_873, 0.6_128, 0.4_797, 0.5_122, 0.5_674, 0.4_639, 0.5_227, 0.5_149, 0.4_747]			)

									assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2


				def 							A					(				self			)    ->    Tuple:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	self.get_dummy_components()
									SCREAMING_SNAKE_CASE				=	StableDiffusionXLPipeline(**lowercase__			)
									SCREAMING_SNAKE_CASE				=	sd_pipe.to(lowercase__			)
									SCREAMING_SNAKE_CASE				=	sd_pipe.to(lowercase__			)
									sd_pipe.set_progress_bar_config(disable=lowercase__			)

									# forward without prompt embeds
									SCREAMING_SNAKE_CASE				=	self.get_dummy_inputs(lowercase__			)
									SCREAMING_SNAKE_CASE				=	3 * ['this is a negative prompt']
									SCREAMING_SNAKE_CASE				=	negative_prompt
									SCREAMING_SNAKE_CASE				=	3 * [inputs['prompt']]

									SCREAMING_SNAKE_CASE				=	sd_pipe(**lowercase__			)
									SCREAMING_SNAKE_CASE				=	output.images[0, -3:, -3:, -1]

									# forward with prompt embeds
									SCREAMING_SNAKE_CASE				=	self.get_dummy_inputs(lowercase__			)
									SCREAMING_SNAKE_CASE				=	3 * ['this is a negative prompt']
									SCREAMING_SNAKE_CASE				=	3 * [inputs.pop('prompt'			)]

									(
									    (
									    SCREAMING_SNAKE_CASE
									)	,    (
									    SCREAMING_SNAKE_CASE
									)	,    (
									    SCREAMING_SNAKE_CASE
									)	,    (
									    SCREAMING_SNAKE_CASE
									)	,    
									)				=	sd_pipe.encode_prompt(lowercase__		,	negative_prompt=lowercase__			)

									SCREAMING_SNAKE_CASE				=	sd_pipe(
									    **lowercase__		,	prompt_embeds=lowercase__		,	negative_prompt_embeds=lowercase__		,	pooled_prompt_embeds=lowercase__		,	negative_pooled_prompt_embeds=lowercase__		,	)
									SCREAMING_SNAKE_CASE				=	output.images[0, -3:, -3:, -1]

									# make sure that it's equal
									assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()			).max() < 1E-4


				def 							A					(				self			)    ->    Optional[Any]:
									"""simple docstring"""







									super().test_attention_slicing_forward_pass(expected_max_diff=3E-3			)




				def 							A					(				self			)    ->    Any:
									"""simple docstring"""







									super().test_inference_batch_single_identical(expected_max_diff=3E-3			)






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

				"""simple docstring"""



				def 							A					(				self			)    ->    Tuple:
									"""simple docstring"""







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()


				def 							A					(				self		,	lowercase__		,	lowercase__="cpu"		,	lowercase__=torch.floataa		,	lowercase__=0			)    ->    Optional[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	torch.Generator(device=lowercase__			).manual_seed(lowercase__			)
									SCREAMING_SNAKE_CASE				=	np.random.RandomState(lowercase__			).standard_normal((1, 4, 64, 64)			)
									SCREAMING_SNAKE_CASE				=	torch.from_numpy(lowercase__			).to(device=lowercase__		,	dtype=lowercase__			)
									SCREAMING_SNAKE_CASE				=	{
									    'prompt': 'a photograph of an astronaut riding a horse',
									    'latents': latents,
									    'generator': generator,
									    'num_inference_steps': 3,
									    'guidance_scale': 7.5,
									    'output_type': 'numpy',
									}
									return inputs




				def 							A					(				self			)    ->    Any:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	DiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-base'			)
									pipe.to(lowercase__			)
									pipe.set_progress_bar_config(disable=lowercase__			)

									SCREAMING_SNAKE_CASE				=	self.get_inputs(lowercase__			)
									SCREAMING_SNAKE_CASE				=	pipe(**lowercase__			).images
									SCREAMING_SNAKE_CASE				=	image[0, -3:, -3:, -1].flatten()

									assert image.shape == (1, 512, 512, 3)
									SCREAMING_SNAKE_CASE				=	np.array([0.49_493, 0.47_896, 0.40_798, 0.54_214, 0.53_212, 0.48_202, 0.47_656, 0.46_329, 0.48_506]			)
									assert np.abs(image_slice - expected_slice			).max() < 7E-3


"""simple docstring"""




import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


snake_case    =		1_0_0_0_0_0_0_0_0_0_0_0
snake_case    =		1_0_0_0_0

snake_case   ,			snake_case    =		os.path.split(__file__)
snake_case    =		os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))

def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_			):
					SCREAMING_SNAKE_CASE				=	pa.Table.from_pydict({'col': [0] * chunk_size}			)
					SCREAMING_SNAKE_CASE				=	pa.concat_tables([table] * (num_examples // chunk_size)			)
					return datasets.Dataset(SCREAMING_SNAKE_CASE_,     fingerprint='table_100B'			)




@dataclass
class      UpperCamelCase	:

				"""simple docstring"""



				UpperCAmelCase_				:      int
				UpperCAmelCase_				:      int
				UpperCAmelCase_				:      int
				UpperCAmelCase_				:      int


				def 							A					(				self			)    ->    Optional[Any]:
									"""simple docstring"""







									SCREAMING_SNAKE_CASE				=	np.random.default_rng(self.seed			)
									SCREAMING_SNAKE_CASE				=	rng.integers(low=self.low		,	high=self.high		,	size=self.size			).tolist()


				def __iter__(				self			)    ->    Union[str, Any]:
									"""simple docstring"""







									return iter(self._sampled_values			)




				def __len__(				self			)    ->    Optional[Any]:
									"""simple docstring"""







									return self.size






@get_duration
def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_			):
					SCREAMING_SNAKE_CASE				=	dataset[0]

@get_duration
def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_			):
					SCREAMING_SNAKE_CASE				=	dataset[-1]

@get_duration
def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_			):
					SCREAMING_SNAKE_CASE				=	dataset[range(len(SCREAMING_SNAKE_CASE_			) // 2,     len(SCREAMING_SNAKE_CASE_			) // 2 + 1_0_2_4			)]

@get_duration
def   UpperCamelCase_  (		SCREAMING_SNAKE_CASE_			):
					SCREAMING_SNAKE_CASE				=	dataset[RandIter(0,     len(SCREAMING_SNAKE_CASE_			),     1_0_2_4,     seed=4_2			)]

def   UpperCamelCase_  (		):
					SCREAMING_SNAKE_CASE				=	{'num examples': SPEED_TEST_N_EXAMPLES}
					SCREAMING_SNAKE_CASE				=	(get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
					print('generating dataset'			)
					SCREAMING_SNAKE_CASE				=	generate_100B_dataset(num_examples=SCREAMING_SNAKE_CASE_,     chunk_size=SCREAMING_SNAKE_CASE_			)
					print('Functions'			)
					for func in functions:
										print(func.__name__			)
										SCREAMING_SNAKE_CASE				=	func(SCREAMING_SNAKE_CASE_			)

					with open(SCREAMING_SNAKE_CASE_,     'wb'			) as f:
										f.write(json.dumps(SCREAMING_SNAKE_CASE_			).encode('utf-8'			)			)


if __name__ == "__main__":  # useful to run the profiler
	benchmark_table_100B()
