

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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE = False							)->							Union[str, Any]:
													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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)->							Optional[int]:
													lowerCamelCase_								=2

													lowerCamelCase_								=torch.rand(lowercase_     ,						lowercase_     ,						3     ,						2							)
													lowerCamelCase_								=torch.rand(lowercase_     ,						4     ,						lowercase_							)
													lowerCamelCase_								=None

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







						def        _snake_case				(			self							)->							str:
													# self and cross attention

													torch.manual_seed(0							)

													lowerCamelCase_								=self.get_constructor_arguments(only_cross_attention=lowercase_							)
													lowerCamelCase_								=Attention(**lowercase_							)

													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(**lowercase_							)

													# only self attention

													torch.manual_seed(0							)

													lowerCamelCase_								=self.get_constructor_arguments(only_cross_attention=lowercase_							)
													lowerCamelCase_								=Attention(**lowercase_							)

													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(**lowercase_							)

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


class 			_SCREAMING_SNAKE_CASE      (  unittest.TestCase):







						def        _snake_case				(			self							)->							List[Any]:
													lowerCamelCase_								=DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""     ,						safety_checker=lowercase_							)

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

													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(lowercase_							)

																				# can also load the converted weights
																				lowerCamelCase_								=DiffusionPipeline.from_pretrained(lowercase_     ,						device_map="""sequential"""     ,						safety_checker=lowercase_							)

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

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



'''simple docstring'''

from __future__ import annotations







class lowerCamelCase   :

				def __init__(       self,						lowercase_ )    ->							None:
					snake_case							=					value
					snake_case							=					None
					snake_case							=					None







class lowerCamelCase   :
				snake_case_							     = 42

				def __init__(       self )    ->							None:
					snake_case							=					0

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_ )    ->							None:
					if node is None:
						return

					if path_sum == self.target:
						self.paths += 1

					if node.left:
						self.depth_first_search(node.left,						path_sum + node.left.value )
					if node.right:
						self.depth_first_search(node.right,						path_sum + node.right.value )






				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_ = None )    ->							int:
					if node is None:
						return 0
					if target is not None:
						snake_case							=					target

					self.depth_first_search(lowercase_,						node.value )
					self.path_sum(node.left )
					self.path_sum(node.right )

					return self.paths


if __name__ == "__main__":
						import doctest

						doctest.testmod()





