
'''simple docstring'''






import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor

class 							_snake_case     (  unittest.TestCase ):
	"""simple docstring"""





	def 	__A      (						self				: List[Any]					,						UpperCamelCase_				: bool = False      ):
			lowerCAmelCase_   :       Optional[Any]    							=10

			if only_cross_attention:
					lowerCAmelCase_   :       Optional[Any]    							=12
			else:
					# when only cross attention is not set, the cross attention dim must be the same as the query dim
					lowerCAmelCase_   :       str    							=query_dim

			return {
			    "query_dim": query_dim,
			    "cross_attention_dim": cross_attention_dim,
			    "heads": 2,
			    "dim_head": 4,
			    "added_kv_proj_dim": 6,
			    "norm_num_groups": 1,
			    "only_cross_attention": only_cross_attention,
			    "processor": AttnAddedKVProcessor(),
			}




	def 	__A      (						self				: Optional[int]					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Dict      ):
			lowerCAmelCase_   :       Optional[int]    							=2

			lowerCAmelCase_   :       List[Any]    							=torch.rand(UpperCamelCase_					,						UpperCamelCase_					,						3					,						2      )
			lowerCAmelCase_   :       Tuple    							=torch.rand(UpperCamelCase_					,						4					,						UpperCamelCase_      )
			lowerCAmelCase_   :       Tuple    							=None

			return {
			    "hidden_states": hidden_states,
			    "encoder_hidden_states": encoder_hidden_states,
			    "attention_mask": attention_mask,
			}





	def 	__A      (						self				: List[str]      ):
			# self and cross attention

			torch.manual_seed(0      )

			lowerCAmelCase_   :       Union[str, Any]    							=self.get_constructor_arguments(only_cross_attention=UpperCamelCase_      )
			lowerCAmelCase_   :       Union[str, Any]    							=Attention(**UpperCamelCase_      )

			self.assertTrue(attn.to_k is not None      )
			self.assertTrue(attn.to_v is not None      )

			lowerCAmelCase_   :       str    							=self.get_forward_arguments(
			    query_dim=constructor_args['''query_dim''']					,						added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']      )

			lowerCAmelCase_   :       Dict    							=attn(**UpperCamelCase_      )

			# only self attention

			torch.manual_seed(0      )

			lowerCAmelCase_   :       List[Any]    							=self.get_constructor_arguments(only_cross_attention=UpperCamelCase_      )
			lowerCAmelCase_   :       int    							=Attention(**UpperCamelCase_      )

			self.assertTrue(attn.to_k is None      )
			self.assertTrue(attn.to_v is None      )

			lowerCAmelCase_   :       str    							=self.get_forward_arguments(
			    query_dim=constructor_args['''query_dim''']					,						added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']      )

			lowerCAmelCase_   :       Optional[Any]    							=attn(**UpperCamelCase_      )

			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )







class 							_snake_case     (  unittest.TestCase ):
	"""simple docstring"""





	def 	__A      (						self				: List[str]      ):
			lowerCAmelCase_   :       Tuple    							=DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''					,						safety_checker=UpperCamelCase_      )

			lowerCAmelCase_   :       Tuple    							=pipe(
			    '''foo'''					,						num_inference_steps=2					,						generator=torch.Generator('''cpu'''      ).manual_seed(0      )					,						output_type='''np'''					,						).images

			# the initial conversion succeeds
			lowerCAmelCase_   :       List[Any]    							=DiffusionPipeline.from_pretrained(
			    '''hf-internal-testing/tiny-stable-diffusion-pipe'''					,						device_map='''sequential'''					,						safety_checker=UpperCamelCase_      )

			lowerCAmelCase_   :       List[Any]    							=pipe(
			    '''foo'''					,						num_inference_steps=2					,						generator=torch.Generator('''cpu'''      ).manual_seed(0      )					,						output_type='''np'''					,						).images

			with tempfile.TemporaryDirectory() as tmpdir:
					# save the converted model
					pipe.save_pretrained(UpperCamelCase_      )

					# can also load the converted weights
					lowerCAmelCase_   :       Union[str, Any]    							=DiffusionPipeline.from_pretrained(UpperCamelCase_					,						device_map='''sequential'''					,						safety_checker=UpperCamelCase_      )

			lowerCAmelCase_   :       List[str]    							=pipe(
			    '''foo'''					,						num_inference_steps=2					,						generator=torch.Generator('''cpu'''      ).manual_seed(0      )					,						output_type='''np'''					,						).images

			self.assertTrue(np.allclose(UpperCamelCase_					,						UpperCamelCase_					,						atol=1E-5      )      )
			self.assertTrue(np.allclose(UpperCamelCase_					,						UpperCamelCase_					,						atol=1E-5      )      )








'''simple docstring'''






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


__lowercase	=    logging.get_logger(__name__)

__lowercase	=    {
    '''google/pegasus-x-base''': '''https://huggingface.co/google/pegasus-x-base/resolve/main/config.json''',
    '''google/pegasus-x-large''': '''https://huggingface.co/google/pegasus-x-large/resolve/main/config.json''',
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}

class 							_snake_case     (  lowerCAmelCase_ ):
	"""simple docstring"""





	_UpperCamelCase							:  Dict								=		'''pegasus_x'''
	_UpperCamelCase							:  Optional[int]								=		['''past_key_values''']
	_UpperCamelCase							:  str								=		{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}




	def __init__(						self				: Any					,						UpperCamelCase_				: Tuple=96103					,						UpperCamelCase_				: List[str]=16384					,						UpperCamelCase_				: Any=16					,						UpperCamelCase_				: Dict=4096					,						UpperCamelCase_				: str=16					,						UpperCamelCase_				: Union[str, Any]=16					,						UpperCamelCase_				: str=4096					,						UpperCamelCase_				: Any=16					,						UpperCamelCase_				: str=0.0					,						UpperCamelCase_				: Dict=0.0					,						UpperCamelCase_				: Union[str, Any]=True					,						UpperCamelCase_				: Union[str, Any]=True					,						UpperCamelCase_				: List[str]="gelu"					,						UpperCamelCase_				: Dict=1024					,						UpperCamelCase_				: str=0.1					,						UpperCamelCase_				: str=0.0					,						UpperCamelCase_				: Optional[Any]=0.0					,						UpperCamelCase_				: str=0.0_2					,						UpperCamelCase_				: Optional[Any]=0					,						UpperCamelCase_				: Tuple=True					,						UpperCamelCase_				: Tuple=0					,						UpperCamelCase_				: Optional[int]=1					,						UpperCamelCase_				: List[Any]=1					,						UpperCamelCase_				: List[Any]=32					,						UpperCamelCase_				: Optional[int]=512					,						UpperCamelCase_				: Any=True					,						**UpperCamelCase_				: Dict					,						):
			lowerCAmelCase_   :       List[Any]    							=vocab_size
			lowerCAmelCase_   :       Any    							=max_position_embeddings
			lowerCAmelCase_   :       int    							=d_model
			lowerCAmelCase_   :       List[Any]    							=encoder_ffn_dim
			lowerCAmelCase_   :       Union[str, Any]    							=encoder_layers
			lowerCAmelCase_   :       Tuple    							=encoder_attention_heads
			lowerCAmelCase_   :       List[Any]    							=decoder_ffn_dim
			lowerCAmelCase_   :       List[Any]    							=decoder_layers
			lowerCAmelCase_   :       List[str]    							=decoder_attention_heads
			lowerCAmelCase_   :       str    							=dropout
			lowerCAmelCase_   :       Any    							=attention_dropout
			lowerCAmelCase_   :       Any    							=activation_dropout
			lowerCAmelCase_   :       Optional[int]    							=activation_function
			lowerCAmelCase_   :       List[Any]    							=init_std
			lowerCAmelCase_   :       List[str]    							=encoder_layerdrop
			lowerCAmelCase_   :       Optional[int]    							=decoder_layerdrop
			lowerCAmelCase_   :       int    							=use_cache
			lowerCAmelCase_   :       Union[str, Any]    							=encoder_layers
			lowerCAmelCase_   :       Tuple    							=scale_embedding  # scale factor will be sqrt(d_model) if True

			lowerCAmelCase_   :       Optional[int]    							=num_global_tokens
			lowerCAmelCase_   :       List[str]    							=block_size
			lowerCAmelCase_   :       Optional[int]    							=stagger_local_blocks

			super().__init__(
			    pad_token_id=UpperCamelCase_					,						eos_token_id=UpperCamelCase_					,						is_encoder_decoder=UpperCamelCase_					,						decoder_start_token_id=UpperCamelCase_					,						forced_eos_token_id=UpperCamelCase_					,						**UpperCamelCase_					,						)




	@property
	def 	__A      (						self				: List[str]      ):
			return self.encoder_attention_heads





	@property
	def 	__A      (						self				: int      ):
			return self.d_model






