code: 
  
  import tempfile
  import unittest
  
  import numpy as np
  import torch
  
  from diffusers import DiffusionPipeline
  from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
  
  
  
  
  
  
  class     lowerCamelCase_			(  unittest.TestCase  ):
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    List[str]				,			__A	:    bool = False       ):
  		__A   : int								= 10
  
  		if only_cross_attention:
  			__A   : List[str]								= 12
  		else:
  			# when only cross attention is not set, the cross attention dim must be the same as the query dim
  			__A   : int								= 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 					lowerCAmelCase_					(    self	:    Optional[Any]				,			__A	:    Tuple				,			__A	:    Dict       ):
  		__A   : Dict								= 2
  
  		__A   : Any								= torch.rand(__A				,			__A				,			3				,			2       )
  		__A   : Union[str, Any]								= torch.rand(__A				,			4				,			__A       )
  		__A   : Any								= None
  
  		return {
  		    "hidden_states": hidden_states,
  		    "encoder_hidden_states": encoder_hidden_states,
  		    "attention_mask": attention_mask,
  		}
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    List[str]       ):
  		# self and cross attention
  
  		torch.manual_seed(0       )
  
  		__A   : int								= self.get_constructor_arguments(only_cross_attention=__A       )
  		__A   : List[str]								= Attention(**__A       )
  
  		self.assertTrue(attn.to_k is not None       )
  		self.assertTrue(attn.to_v is not None       )
  
  		__A   : Union[str, Any]								= self.get_forward_arguments(
  		    query_dim=constructor_args["""query_dim"""]				,			added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]       )
  
  		__A   : Tuple								= attn(**__A       )
  
  		# only self attention
  
  		torch.manual_seed(0       )
  
  		__A   : str								= self.get_constructor_arguments(only_cross_attention=__A       )
  		__A   : Dict								= Attention(**__A       )
  
  		self.assertTrue(attn.to_k is None       )
  		self.assertTrue(attn.to_v is None       )
  
  		__A   : str								= self.get_forward_arguments(
  		    query_dim=constructor_args["""query_dim"""]				,			added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]       )
  
  		__A   : str								= attn(**__A       )
  
  		self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()       )
  
  
  
  
  
  
  class     lowerCamelCase_			(  unittest.TestCase  ):
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Dict       ):
  		__A   : Optional[int]								= DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""				,			safety_checker=__A       )
  
  		__A   : Optional[Any]								= pipe(
  		    """foo"""				,			num_inference_steps=2				,			generator=torch.Generator("""cpu"""       ).manual_seed(0       )				,			output_type="""np"""				,			).images
  
  		# the initial conversion succeeds
  		__A   : Any								= DiffusionPipeline.from_pretrained(
  		    """hf-internal-testing/tiny-stable-diffusion-pipe"""				,			device_map="""sequential"""				,			safety_checker=__A       )
  
  		__A   : 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(__A       )
  
  			# can also load the converted weights
  			__A   : List[str]								= DiffusionPipeline.from_pretrained(__A				,			device_map="""sequential"""				,			safety_checker=__A       )
  
  		__A   : Union[str, 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       )       )
  
  
  
  
  
  
style_context: 
  
  import unittest
  from pathlib import Path
  
  from transformers import is_vision_available, load_tool
  from transformers.testing_utils import get_tests_dir
  
  from .test_tools_common import ToolTesterMixin
  
  
  if is_vision_available():
  			from PIL import Image
  
  
  
  
  
  
  class     lowerCamelCase_			(  unittest.TestCase	,			_lowercase  ):
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Dict       ):
  		__A   : str								= load_tool("""image-captioning"""       )
  		self.tool.setup()
  		__A   : Dict								= load_tool("""image-captioning"""				,			remote=__A       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Optional[int]       ):
  		__A   : List[Any]								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : Optional[Any]								= self.tool(__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Dict       ):
  		__A   : Any								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : List[str]								= self.remote_tool(__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Union[str, Any]       ):
  		__A   : Optional[Any]								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : str								= self.tool(image=__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    str       ):
  		__A   : Dict								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : List[Any]								= self.remote_tool(image=__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  
  