






import tempfile
import unittest

import numpy as np
import torch

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


class 			__SCREAMING_SNAKE_CASE    (      unittest.TestCase):






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase = False      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  10

												if only_cross_attention:
																		lowerCAmelCase__			  =  12
												else:
																		# when only cross attention is not set, the cross attention dim must be the same as the query dim
																		lowerCAmelCase__			  =  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 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  2

												lowerCAmelCase__			  =  torch.rand(_UpperCamelCase   ,  _UpperCamelCase   ,  3   ,  2      )
												lowerCAmelCase__			  =  torch.rand(_UpperCamelCase   ,  4   ,  _UpperCamelCase      )
												lowerCAmelCase__			  =  None

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







						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												# self and cross attention

												torch.manual_seed(0      )

												lowerCAmelCase__			  =  self.get_constructor_arguments(only_cross_attention=_UpperCamelCase      )
												lowerCAmelCase__			  =  Attention(**_UpperCamelCase      )

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

												lowerCAmelCase__			  =  self.get_forward_arguments(
												    query_dim=constructor_args['query_dim']   ,  added_kv_proj_dim=constructor_args['added_kv_proj_dim']      )

												lowerCAmelCase__			  =  attn(**_UpperCamelCase      )

												# only self attention

												torch.manual_seed(0      )

												lowerCAmelCase__			  =  self.get_constructor_arguments(only_cross_attention=_UpperCamelCase      )
												lowerCAmelCase__			  =  Attention(**_UpperCamelCase      )

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

												lowerCAmelCase__			  =  self.get_forward_arguments(
												    query_dim=constructor_args['query_dim']   ,  added_kv_proj_dim=constructor_args['added_kv_proj_dim']      )

												lowerCAmelCase__			  =  attn(**_UpperCamelCase      )

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




class 			__SCREAMING_SNAKE_CASE    (      unittest.TestCase):






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe'   ,  safety_checker=_UpperCamelCase      )

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

												# the initial conversion succeeds
												lowerCAmelCase__			  =  DiffusionPipeline.from_pretrained(
												    'hf-internal-testing/tiny-stable-diffusion-pipe'   ,  device_map='sequential'   ,  safety_checker=_UpperCamelCase      )

												lowerCAmelCase__			  =  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__			  =  DiffusionPipeline.from_pretrained(_UpperCamelCase   ,  device_map='sequential'   ,  safety_checker=_UpperCamelCase      )

												lowerCAmelCase__			  =  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      )      )













from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__snake_case			:			Optional[int]			   =   {"""configuration_sew_d""": ["""SEW_D_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SEWDConfig"""]}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__snake_case			:			List[Any]			   =   [
					    """SEW_D_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """SEWDForCTC""",
					    """SEWDForSequenceClassification""",
					    """SEWDModel""",
					    """SEWDPreTrainedModel""",
					]

if TYPE_CHECKING:
					from .configuration_sew_d import SEW_D_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWDConfig

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_sew_d import (
										    SEW_D_PRETRAINED_MODEL_ARCHIVE_LIST,
										    SEWDForCTC,
										    SEWDForSequenceClassification,
										    SEWDModel,
										    SEWDPreTrainedModel,
										)


else:
					import sys

					__snake_case			:			Dict			   =   _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





