





"""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        lowercase__ (      unittest.TestCase  ):

			"""simple docstring"""







			def      _a			(			self	,      _A = False						):






									'''simple docstring'''







									UpperCamelCase							:		Dict							      = 1_0

									if only_cross_attention:
															UpperCamelCase							:		List[str]							      = 1_2
									else:
															# when only cross attention is not set, the cross attention dim must be the same as the query dim
															UpperCamelCase							:		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	,      _A	,      _A						):






									'''simple docstring'''







									UpperCamelCase							:		Union[str, Any]							      = 2

									UpperCamelCase							:		List[str]							      = torch.rand(_A	,      _A	,      3	,      2						)
									UpperCamelCase							:		Optional[Any]							      = torch.rand(_A	,      4	,      _A						)
									UpperCamelCase							:		Any							      = None

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







			def      _a			(			self						):






									'''simple docstring'''







									torch.manual_seed(0						)

									UpperCamelCase							:		Optional[Any]							      = self.get_constructor_arguments(only_cross_attention=_A						)
									UpperCamelCase							:		str							      = Attention(**_A						)

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

									UpperCamelCase							:		Optional[int]							      = self.get_forward_arguments(
									    query_dim=constructor_args["""query_dim"""]	,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]						)

									UpperCamelCase							:		str							      = attn(**_A						)

									# only self attention

									torch.manual_seed(0						)

									UpperCamelCase							:		List[str]							      = self.get_constructor_arguments(only_cross_attention=_A						)
									UpperCamelCase							:		List[str]							      = Attention(**_A						)

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

									UpperCamelCase							:		Union[str, Any]							      = self.get_forward_arguments(
									    query_dim=constructor_args["""query_dim"""]	,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]						)

									UpperCamelCase							:		List[Any]							      = attn(**_A						)

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






class        lowercase__ (      unittest.TestCase  ):

			"""simple docstring"""







			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		Union[str, Any]							      = DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""	,      safety_checker=_A						)

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

									# the initial conversion succeeds
									UpperCamelCase							:		Any							      = DiffusionPipeline.from_pretrained(
									    """hf-internal-testing/tiny-stable-diffusion-pipe"""	,      device_map="""sequential"""	,      safety_checker=_A						)

									UpperCamelCase							:		Optional[int]							      = 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(_A						)

															# can also load the converted weights
															UpperCamelCase							:		List[Any]							      = DiffusionPipeline.from_pretrained(_A	,      device_map="""sequential"""	,      safety_checker=_A						)

									UpperCamelCase							:		Any							      = pipe(
									    """foo"""	,      num_inference_steps=2	,      generator=torch.Generator("""cpu"""						).manual_seed(0						)	,      output_type="""np"""	,      ).images

									self.assertTrue(np.allclose(_A	,      _A	,      atol=1e-5						)						)
									self.assertTrue(np.allclose(_A	,      _A	,      atol=1e-5						)						)










"""simple docstring"""


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


__magic_name__					:				Any	= logging.get_logger(__name__)

__magic_name__					:				Dict	= {
    """uw-madison/yoso-4096""": """https://huggingface.co/uw-madison/yoso-4096/resolve/main/config.json""",
    # See all YOSO models at https://huggingface.co/models?filter=yoso
}






class        lowercase__ (      __SCREAMING_SNAKE_CASE  ):

			"""simple docstring"""
			__lowerCAmelCase    :							Optional[int]								=     """yoso"""







			def __init__(			self	,      _A=5_0_2_6_5	,      _A=7_6_8	,      _A=1_2	,      _A=1_2	,      _A=3_0_7_2	,      _A="gelu"	,      _A=0.1	,      _A=0.1	,      _A=4_0_9_6	,      _A=1	,      _A=0.02	,      _A=1e-1_2	,      _A="absolute"	,      _A=True	,      _A=9	,      _A=6_4	,      _A=None	,      _A=True	,      _A=True	,      _A=1	,      _A=0	,      _A=2	,      **_A	,      ):






									'''simple docstring'''







									super().__init__(pad_token_id=_A	,      bos_token_id=_A	,      eos_token_id=_A	,      **_A						)

									UpperCamelCase							:		Any							      = vocab_size
									UpperCamelCase							:		Any							      = max_position_embeddings
									UpperCamelCase							:		int							      = hidden_size
									UpperCamelCase							:		List[Any]							      = num_hidden_layers
									UpperCamelCase							:		Any							      = num_attention_heads
									UpperCamelCase							:		Optional[Any]							      = intermediate_size
									UpperCamelCase							:		Optional[int]							      = hidden_act
									UpperCamelCase							:		List[Any]							      = hidden_dropout_prob
									UpperCamelCase							:		int							      = attention_probs_dropout_prob
									UpperCamelCase							:		Any							      = initializer_range
									UpperCamelCase							:		Optional[Any]							      = type_vocab_size
									UpperCamelCase							:		Any							      = layer_norm_eps
									UpperCamelCase							:		List[Any]							      = position_embedding_type
									UpperCamelCase							:		Optional[Any]							      = use_expectation
									UpperCamelCase							:		Any							      = hash_code_len
									UpperCamelCase							:		Optional[int]							      = num_hash
									UpperCamelCase							:		List[Any]							      = conv_window
									UpperCamelCase							:		Tuple							      = use_fast_hash
									UpperCamelCase							:		Dict							      = lsh_backward



