
import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

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()
@skip_mps
class  lowercase__(				lowerCAmelCase_ ,						lowerCAmelCase_ ,						unittest.TestCase							):



	"""simple docstring"""




	a			:int         =	StableDiffusionPanoramaPipeline
	a			:List[str]         =	TEXT_TO_IMAGE_PARAMS
	a			:Dict         =	TEXT_TO_IMAGE_BATCH_PARAMS
	a			:Any         =	TEXT_TO_IMAGE_IMAGE_PARAMS
	a			:List[Any]         =	TEXT_TO_IMAGE_IMAGE_PARAMS
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Any:
					torch.manual_seed(0       )
					lowercase_       =					UNetaDConditionModel(
					    block_out_channels=(3_2, 6_4)       ,							layers_per_block=1       ,							sample_size=3_2       ,							in_channels=4       ,							out_channels=4       ,							down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,							up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')       ,							cross_attention_dim=3_2       ,							)
					lowercase_       =					DDIMScheduler()
					torch.manual_seed(0       )
					lowercase_       =					AutoencoderKL(
					    block_out_channels=[3_2, 6_4]       ,							in_channels=3       ,							out_channels=3       ,							down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']       ,							up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,							latent_channels=4       ,							)
					torch.manual_seed(0       )
					lowercase_       =					CLIPTextConfig(
					    bos_token_id=0       ,							eos_token_id=2       ,							hidden_size=3_2       ,							intermediate_size=3_7       ,							layer_norm_eps=1e-05       ,							num_attention_heads=4       ,							num_hidden_layers=5       ,							pad_token_id=1       ,							vocab_size=1_0_0_0       ,							)
					lowercase_       =					CLIPTextModel(_snake_case       )
					lowercase_       =					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''       )

					lowercase_       =					{
					    'unet': unet,
					    'scheduler': scheduler,
					    'vae': vae,
					    'text_encoder': text_encoder,
					    'tokenizer': tokenizer,
					    'safety_checker': None,
					    'feature_extractor': None,
					}
					return components
	def 						_lowercase  (       self  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			List[Any]       ,							SCREAMING_SNAKE_CASE_  :			Tuple=0       )       ->     Union[str, Any]:
					lowercase_       =					torch.manual_seed(_snake_case       )
					lowercase_       =					{
					    'prompt': 'a photo of the dolomites',
					    'generator': generator,
					    # Setting height and width to None to prevent OOMs on CPU.
					    'height': None,
					    'width': None,
					    'num_inference_steps': 1,
					    'guidance_scale': 6.0,
					    'output_type': 'numpy',
					}
					return inputs
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     str:
					lowercase_       =					'cpu'  # ensure determinism for the device-dependent torch.Generator
					lowercase_       =					self.get_dummy_components()
					lowercase_       =					StableDiffusionPanoramaPipeline(**_snake_case       )
					lowercase_       =					sd_pipe.to(_snake_case       )
					sd_pipe.set_progress_bar_config(disable=_snake_case       )

					lowercase_       =					self.get_dummy_inputs(_snake_case       )
					lowercase_       =					sd_pipe(**_snake_case       ).images
					lowercase_       =					image[0, -3:, -3:, -1]
					assert image.shape == (1, 6_4, 6_4, 3)

					lowercase_       =					np.array([0.61_86, 0.53_74, 0.49_15, 0.41_35, 0.41_14, 0.45_63, 0.51_28, 0.49_77, 0.47_57]       )

					assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2
	def 						_lowercase  (       self  :			Tuple       )       ->     Any:
					super().test_inference_batch_consistent(batch_sizes=[1, 2]       )
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     Any:
					super().test_inference_batch_single_identical(batch_size=2       ,							expected_max_diff=3.25e-3       )
	def 						_lowercase  (       self  :			str       )       ->     List[str]:
					lowercase_       =					'cpu'  # ensure determinism for the device-dependent torch.Generator
					lowercase_       =					self.get_dummy_components()
					lowercase_       =					StableDiffusionPanoramaPipeline(**_snake_case       )
					lowercase_       =					sd_pipe.to(_snake_case       )
					sd_pipe.set_progress_bar_config(disable=_snake_case       )

					lowercase_       =					self.get_dummy_inputs(_snake_case       )
					lowercase_       =					'french fries'
					lowercase_       =					sd_pipe(**_snake_case       ,							negative_prompt=_snake_case       )
					lowercase_       =					output.images
					lowercase_       =					image[0, -3:, -3:, -1]

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

					lowercase_       =					np.array([0.61_87, 0.53_75, 0.49_15, 0.41_36, 0.41_14, 0.45_63, 0.51_28, 0.49_76, 0.47_57]       )

					assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     Optional[Any]:
					lowercase_       =					'cpu'  # ensure determinism for the device-dependent torch.Generator
					lowercase_       =					self.get_dummy_components()
					lowercase_       =					StableDiffusionPanoramaPipeline(**_snake_case       )
					lowercase_       =					sd_pipe.to(_snake_case       )
					sd_pipe.set_progress_bar_config(disable=_snake_case       )

					lowercase_       =					self.get_dummy_inputs(_snake_case       )
					lowercase_       =					sd_pipe(**_snake_case       ,							view_batch_size=2       )
					lowercase_       =					output.images
					lowercase_       =					image[0, -3:, -3:, -1]

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

					lowercase_       =					np.array([0.61_87, 0.53_75, 0.49_15, 0.41_36, 0.41_14, 0.45_63, 0.51_28, 0.49_76, 0.47_57]       )

					assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2
	def 						_lowercase  (       self  :			Dict       )       ->     Any:
					lowercase_       =					'cpu'  # ensure determinism for the device-dependent torch.Generator
					lowercase_       =					self.get_dummy_components()
					lowercase_       =					EulerAncestralDiscreteScheduler(
					    beta_start=0.0_00_85       ,							beta_end=0.0_12       ,							beta_schedule='''scaled_linear'''       )
					lowercase_       =					StableDiffusionPanoramaPipeline(**_snake_case       )
					lowercase_       =					sd_pipe.to(_snake_case       )
					sd_pipe.set_progress_bar_config(disable=_snake_case       )

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

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

					lowercase_       =					np.array([0.40_24, 0.65_10, 0.49_01, 0.53_78, 0.58_13, 0.56_22, 0.47_95, 0.44_67, 0.49_52]       )

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




	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Any:
					lowercase_       =					'cpu'  # ensure determinism for the device-dependent torch.Generator
					lowercase_       =					self.get_dummy_components()
					lowercase_       =					PNDMScheduler(
					    beta_start=0.0_00_85       ,							beta_end=0.0_12       ,							beta_schedule='''scaled_linear'''       ,							skip_prk_steps=_snake_case       )
					lowercase_       =					StableDiffusionPanoramaPipeline(**_snake_case       )
					lowercase_       =					sd_pipe.to(_snake_case       )
					sd_pipe.set_progress_bar_config(disable=_snake_case       )

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

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

					lowercase_       =					np.array([0.63_91, 0.62_91, 0.48_61, 0.51_34, 0.55_52, 0.45_78, 0.50_32, 0.50_23, 0.45_39]       )

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

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



	"""simple docstring"""




	def 						_lowercase  (       self  :			Dict       )       ->     List[Any]:
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()
	def 						_lowercase  (       self  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			Any=0       )       ->     Any:
					lowercase_       =					torch.manual_seed(_snake_case       )
					lowercase_       =					{
					    'prompt': 'a photo of the dolomites',
					    'generator': generator,
					    'num_inference_steps': 3,
					    'guidance_scale': 7.5,
					    'output_type': 'numpy',
					}
					return inputs
	def 						_lowercase  (       self  :			Any       )       ->     Optional[Any]:
					lowercase_       =					'stabilityai/stable-diffusion-2-base'
					lowercase_       =					DDIMScheduler.from_pretrained(_snake_case       ,							subfolder='''scheduler'''       )
					lowercase_       =					StableDiffusionPanoramaPipeline.from_pretrained(_snake_case       ,							scheduler=_snake_case       ,							safety_checker=_snake_case       )
					pipe.to(_snake_case       )
					pipe.set_progress_bar_config(disable=_snake_case       )
					pipe.enable_attention_slicing()

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

					assert image.shape == (1, 5_1_2, 2_0_4_8, 3)

					lowercase_       =					np.array(
					    [
					        0.36_96_83_92,
					        0.27_02_53_72,
					        0.32_44_67_66,
					        0.28_37_93_87,
					        0.36_36_32_74,
					        0.30_73_33_47,
					        0.27_10_00_27,
					        0.27_05_41_25,
					        0.25_53_60_96,
					    ]       )

					assert np.abs(expected_slice - image_slice       ).max() < 1e-2
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     str:
					lowercase_       =					StableDiffusionPanoramaPipeline.from_pretrained(
					    '''stabilityai/stable-diffusion-2-base'''       ,							safety_checker=_snake_case       )
					lowercase_       =					LMSDiscreteScheduler.from_config(pipe.scheduler.config       )
					pipe.to(_snake_case       )
					pipe.set_progress_bar_config(disable=_snake_case       )
					pipe.enable_attention_slicing()

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

					assert image.shape == (1, 5_1_2, 2_0_4_8, 3)

					lowercase_       =					np.array(
					    [
					        [
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					        ]
					    ]       )

					assert np.abs(expected_slice - image_slice       ).max() < 1e-3
	def 						_lowercase  (       self  :			int       )       ->     Any:
					lowercase_       =					0

					def callback_fn(SCREAMING_SNAKE_CASE_  :			int       ,							SCREAMING_SNAKE_CASE_  :			int       ,							SCREAMING_SNAKE_CASE_  :			torch.FloatTensor       ) -> None:
									lowercase_       =					True
									nonlocal number_of_steps
									number_of_steps += 1
									if step == 1:
													lowercase_       =					latents.detach().cpu().numpy()
													assert latents.shape == (1, 4, 6_4, 2_5_6)
													lowercase_       =					latents[0, -3:, -3:, -1]

													lowercase_       =					np.array(
													    [
													        0.18_68_18_69,
													        0.33_90_78_16,
													        0.5_36_12_76,
													        0.14_43_28_65,
													        -0.02_85_66_11,
													        -0.73_94_11_23,
													        0.23_39_79_87,
													        0.47_32_26_82,
													        -0.37_82_31_64,
													    ]       )
													assert np.abs(latents_slice.flatten() - expected_slice       ).max() < 5e-2
									elif step == 2:
													lowercase_       =					latents.detach().cpu().numpy()
													assert latents.shape == (1, 4, 6_4, 2_5_6)
													lowercase_       =					latents[0, -3:, -3:, -1]

													lowercase_       =					np.array(
													    [
													        0.18_53_96_45,
													        0.33_98_72_48,
													        0.5_37_85_59,
													        0.14_43_71_42,
													        -0.02_45_52_61,
													        -0.7_33_83_17,
													        0.23_99_07_55,
													        0.47_35_62_72,
													        -0.3_78_65_05,
													    ]       )

													assert np.abs(latents_slice.flatten() - expected_slice       ).max() < 5e-2

					lowercase_       =					False

					lowercase_       =					'stabilityai/stable-diffusion-2-base'
					lowercase_       =					DDIMScheduler.from_pretrained(_snake_case       ,							subfolder='''scheduler'''       )
					lowercase_       =					StableDiffusionPanoramaPipeline.from_pretrained(_snake_case       ,							scheduler=_snake_case       ,							safety_checker=_snake_case       )
					lowercase_       =					pipe.to(_snake_case       )
					pipe.set_progress_bar_config(disable=_snake_case       )
					pipe.enable_attention_slicing()

					lowercase_       =					self.get_inputs()
					pipe(**_snake_case       ,							callback=_snake_case       ,							callback_steps=1       )
					assert callback_fn.has_been_called
					assert number_of_steps == 3




	def 						_lowercase  (       self  :			Optional[Any]       )       ->     Optional[int]:
					torch.cuda.empty_cache()
					torch.cuda.reset_max_memory_allocated()
					torch.cuda.reset_peak_memory_stats()

					lowercase_       =					'stabilityai/stable-diffusion-2-base'
					lowercase_       =					DDIMScheduler.from_pretrained(_snake_case       ,							subfolder='''scheduler'''       )
					lowercase_       =					StableDiffusionPanoramaPipeline.from_pretrained(_snake_case       ,							scheduler=_snake_case       ,							safety_checker=_snake_case       )
					lowercase_       =					pipe.to(_snake_case       )
					pipe.set_progress_bar_config(disable=_snake_case       )
					pipe.enable_attention_slicing(1       )
					pipe.enable_sequential_cpu_offload()

					lowercase_       =					self.get_inputs()
					lowercase_       =					pipe(**_snake_case       )

					lowercase_       =					torch.cuda.max_memory_allocated()
					# make sure that less than 5.2 GB is allocated
					assert mem_bytes < 5.5 * 1_0**9









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


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

a_   :Dict        =      {"openai-gpt": "https://huggingface.co/openai-gpt/resolve/main/config.json"}




class 					snake_case__						(       lowerCAmelCase_     ):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE						=	"""openai-gpt"""
     _SCREAMING_SNAKE_CASE						=	{
         """max_position_embeddings""": """n_positions""",
         """hidden_size""": """n_embd""",
         """num_attention_heads""": """n_head""",
         """num_hidden_layers""": """n_layer""",
     }






     def __init__(       self  :  Optional[int], _snake_case  :  Dict=4_0_4_7_8, _snake_case  :  str=5_1_2, _snake_case  :  int=7_6_8, _snake_case  :  Tuple=1_2, _snake_case  :  Any=1_2, _snake_case  :  str="gelu", _snake_case  :  List[str]=0.1, _snake_case  :  Any=0.1, _snake_case  :  Dict=0.1, _snake_case  :  int=1e-5, _snake_case  :  Optional[Any]=0.0_2, _snake_case  :  List[Any]="cls_index", _snake_case  :  Any=True, _snake_case  :  Any=None, _snake_case  :  int=True, _snake_case  :  Optional[Any]=0.1, **_snake_case  :  List[Any], )   ->Optional[int]:
            snake_case__							:	int   	=	vocab_size
            snake_case__							:	Dict   	=	n_positions
            snake_case__							:	str   	=	n_embd
            snake_case__							:	str   	=	n_layer
            snake_case__							:	List[Any]   	=	n_head
            snake_case__							:	List[Any]   	=	afn
            snake_case__							:	Optional[Any]   	=	resid_pdrop
            snake_case__							:	List[str]   	=	embd_pdrop
            snake_case__							:	List[Any]   	=	attn_pdrop
            snake_case__							:	Optional[int]   	=	layer_norm_epsilon
            snake_case__							:	str   	=	initializer_range
            snake_case__							:	List[str]   	=	summary_type
            snake_case__							:	Optional[int]   	=	summary_use_proj
            snake_case__							:	List[str]   	=	summary_activation
            snake_case__							:	Optional[Any]   	=	summary_first_dropout
            snake_case__							:	int   	=	summary_proj_to_labels
            super().__init__(**_snake_case					)






