


'''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 			lowerCAmelCase					(					a   ,					a   ,					unittest.TestCase				):
	_lowerCamelCase :       List[str]         =			StableDiffusionXLPipeline
	_lowerCamelCase :       str         =			TEXT_TO_IMAGE_PARAMS
	_lowerCamelCase :       Optional[int]         =			TEXT_TO_IMAGE_BATCH_PARAMS
	_lowerCamelCase :       Optional[Any]         =			TEXT_TO_IMAGE_IMAGE_PARAMS
	_lowerCamelCase :       Dict         =			TEXT_TO_IMAGE_IMAGE_PARAMS



	def 					lowercase  (       self     ):
		torch.manual_seed(0     )
		lowerCAmelCase       :     Dict   				=			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=snake_case__			,       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			,       )
		lowerCAmelCase       :     List[str]   				=			EulerDiscreteScheduler(
		    beta_start=0.0_0_0_8_5			,       beta_end=0.0_1_2			,       steps_offset=1			,       beta_schedule='scaled_linear'			,       timestep_spacing='leading'			,       )
		torch.manual_seed(0     )
		lowerCAmelCase       :     Any   				=			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     )
		lowerCAmelCase       :     Optional[int]   				=			CLIPTextConfig(
		    bos_token_id=0			,       eos_token_id=2			,       hidden_size=32			,       intermediate_size=37			,       layer_norm_eps=1e-0_5			,       num_attention_heads=4			,       num_hidden_layers=5			,       pad_token_id=1			,       vocab_size=1000			,       hidden_act='gelu'			,       projection_dim=32			,       )
		lowerCAmelCase       :     Optional[int]   				=			CLIPTextModel(snake_case__     )
		lowerCAmelCase       :     Optional[Any]   				=			CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'			,       local_files_only=snake_case__     )

		lowerCAmelCase       :     Tuple   				=			CLIPTextModelWithProjection(snake_case__     )
		lowerCAmelCase       :     List[Any]   				=			CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'			,       local_files_only=snake_case__     )

		lowerCAmelCase       :     List[str]   				=			{
		    '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 					lowercase  (       self			,       snake_case__			,       snake_case__=0     ):
		if str(snake_case__     ).startswith('mps'     ):
			lowerCAmelCase       :     Dict   				=			torch.manual_seed(snake_case__     )
		else:
			lowerCAmelCase       :     int   				=			torch.Generator(device=snake_case__     ).manual_seed(snake_case__     )
		lowerCAmelCase       :     Optional[int]   				=			{
		    '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 					lowercase  (       self     ):
		lowerCAmelCase       :     Any   				=			'cpu'  # ensure determinism for the device-dependent torch.Generator
		lowerCAmelCase       :     Optional[Any]   				=			self.get_dummy_components()
		lowerCAmelCase       :     Optional[int]   				=			StableDiffusionXLPipeline(**snake_case__     )
		lowerCAmelCase       :     List[str]   				=			sd_pipe.to(snake_case__     )
		sd_pipe.set_progress_bar_config(disable=snake_case__     )

		lowerCAmelCase       :     Optional[int]   				=			self.get_dummy_inputs(snake_case__     )
		lowerCAmelCase       :     List[str]   				=			sd_pipe(**snake_case__     ).images
		lowerCAmelCase       :     Union[str, Any]   				=			image[0, -3:, -3:, -1]

		assert image.shape == (1, 64, 64, 3)
		lowerCAmelCase       :     List[str]   				=			np.array([0.5_8_7_3, 0.6_1_2_8, 0.4_7_9_7, 0.5_1_2_2, 0.5_6_7_4, 0.4_6_3_9, 0.5_2_2_7, 0.5_1_4_9, 0.4_7_4_7]     )

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



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     Any   				=			self.get_dummy_components()
		lowerCAmelCase       :     str   				=			StableDiffusionXLPipeline(**snake_case__     )
		lowerCAmelCase       :     Any   				=			sd_pipe.to(snake_case__     )
		lowerCAmelCase       :     List[str]   				=			sd_pipe.to(snake_case__     )
		sd_pipe.set_progress_bar_config(disable=snake_case__     )

		# forward without prompt embeds
		lowerCAmelCase       :     int   				=			self.get_dummy_inputs(snake_case__     )
		lowerCAmelCase       :     int   				=			3 * ['this is a negative prompt']
		lowerCAmelCase       :     Union[str, Any]   				=			negative_prompt
		lowerCAmelCase       :     int   				=			3 * [inputs['prompt']]

		lowerCAmelCase       :     Optional[int]   				=			sd_pipe(**snake_case__     )
		lowerCAmelCase       :     int   				=			output.images[0, -3:, -3:, -1]

		# forward with prompt embeds
		lowerCAmelCase       :     str   				=			self.get_dummy_inputs(snake_case__     )
		lowerCAmelCase       :     Union[str, Any]   				=			3 * ['this is a negative prompt']
		lowerCAmelCase       :     Optional[int]   				=			3 * [inputs.pop('prompt'     )]

		(
		    (
		    lowerCAmelCase
		)				, (
		    lowerCAmelCase
		)				, (
		    lowerCAmelCase
		)				, (
		    lowerCAmelCase
		)				, 
		)       :     Dict   				=			sd_pipe.encode_prompt(snake_case__			,       negative_prompt=snake_case__     )

		lowerCAmelCase       :     Optional[int]   				=			sd_pipe(
		    **snake_case__			,       prompt_embeds=snake_case__			,       negative_prompt_embeds=snake_case__			,       pooled_prompt_embeds=snake_case__			,       negative_pooled_prompt_embeds=snake_case__			,       )
		lowerCAmelCase       :     Tuple   				=			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 					lowercase  (       self     ):
		super().test_attention_slicing_forward_pass(expected_max_diff=3e-3     )



	def 					lowercase  (       self     ):
		super().test_inference_batch_single_identical(expected_max_diff=3e-3     )



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



	def 					lowercase  (       self     ):
		super().tearDown()
		gc.collect()
		torch.cuda.empty_cache()



	def 					lowercase  (       self			,       snake_case__			,       snake_case__="cpu"			,       snake_case__=torch.floataa			,       snake_case__=0     ):
		lowerCAmelCase       :     Any   				=			torch.Generator(device=snake_case__     ).manual_seed(snake_case__     )
		lowerCAmelCase       :     List[Any]   				=			np.random.RandomState(snake_case__     ).standard_normal((1, 4, 64, 64)     )
		lowerCAmelCase       :     Dict   				=			torch.from_numpy(snake_case__     ).to(device=snake_case__			,       dtype=snake_case__     )
		lowerCAmelCase       :     int   				=			{
		    '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 					lowercase  (       self     ):
		lowerCAmelCase       :     Union[str, Any]   				=			DiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-base'     )
		pipe.to(snake_case__     )
		pipe.set_progress_bar_config(disable=snake_case__     )

		lowerCAmelCase       :     List[Any]   				=			self.get_inputs(snake_case__     )
		lowerCAmelCase       :     Optional[Any]   				=			pipe(**snake_case__     ).images
		lowerCAmelCase       :     Dict   				=			image[0, -3:, -3:, -1].flatten()

		assert image.shape == (1, 512, 512, 3)
		lowerCAmelCase       :     Dict   				=			np.array([0.4_9_4_9_3, 0.4_7_8_9_6, 0.4_0_7_9_8, 0.5_4_2_1_4, 0.5_3_2_1_2, 0.4_8_2_0_2, 0.4_7_6_5_6, 0.4_6_3_2_9, 0.4_8_5_0_6]     )
		assert np.abs(image_slice - expected_slice     ).max() < 7e-3




'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCAmelCase	:		List[str]		  =						logging.get_logger(__name__)

_lowerCAmelCase	:		int		  =						{
    'facebook/opt-125m': 'https://huggingface.co/facebook/opt-125m/blob/main/config.json',
    'facebook/opt-350m': 'https://huggingface.co/facebook/opt-350m/blob/main/config.json',
    'facebook/opt-1.3b': 'https://huggingface.co/facebook/opt-1.3b/blob/main/config.json',
    'facebook/opt-2.7b': 'https://huggingface.co/facebook/opt-2.7b/blob/main/config.json',
    'facebook/opt-6.7b': 'https://huggingface.co/facebook/opt-6.7b/blob/main/config.json',
    'facebook/opt-13b': 'https://huggingface.co/facebook/opt-13b/blob/main/config.json',
}



class 			lowerCAmelCase					(					a				):
	_lowerCamelCase :       Dict         =			"""opt"""
	_lowerCamelCase :       Optional[Any]         =			["""past_key_values"""]



	def __init__(       self			,       snake_case__=5_0272			,       snake_case__=768			,       snake_case__=12			,       snake_case__=3072			,       snake_case__=2048			,       snake_case__=True			,       snake_case__=False			,       snake_case__=None			,       snake_case__=0.1			,       snake_case__=0.0			,       snake_case__=12			,       snake_case__="relu"			,       snake_case__=0.0			,       snake_case__=0.0_2			,       snake_case__=True			,       snake_case__=1			,       snake_case__=2			,       snake_case__=2			,       snake_case__=True			,       snake_case__=True			,       **snake_case__			,       ):
		super().__init__(
		    pad_token_id=snake_case__			,       bos_token_id=snake_case__			,       eos_token_id=snake_case__			,       **snake_case__			,       )
		lowerCAmelCase       :     Union[str, Any]   				=			vocab_size
		lowerCAmelCase       :     List[Any]   				=			max_position_embeddings
		lowerCAmelCase       :     List[str]   				=			num_attention_heads
		lowerCAmelCase       :     Dict   				=			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
		lowerCAmelCase       :     Optional[Any]   				=			ffn_dim
		lowerCAmelCase       :     Optional[int]   				=			hidden_size
		lowerCAmelCase       :     List[str]   				=			num_hidden_layers
		lowerCAmelCase       :     Union[str, Any]   				=			dropout
		lowerCAmelCase       :     List[str]   				=			attention_dropout
		lowerCAmelCase       :     Union[str, Any]   				=			activation_function
		lowerCAmelCase       :     Optional[Any]   				=			init_std
		lowerCAmelCase       :     List[str]   				=			layerdrop
		lowerCAmelCase       :     Dict   				=			use_cache
		lowerCAmelCase       :     List[Any]   				=			do_layer_norm_before
		# We keep these variables at `True` for backward compatibility.
		lowerCAmelCase       :     List[str]   				=			enable_bias
		lowerCAmelCase       :     Dict   				=			layer_norm_elementwise_affine

		# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
		# with checkpoints that have been fine-tuned before transformers v4.20.1
		# see https://github.com/facebookresearch/metaseq/pull/164
		lowerCAmelCase       :     Tuple   				=			_remove_final_layer_norm
