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



							"""simple docstring"""






							def    UpperCAmelCase__							(						self						:List[str]  ,							lowerCamelCase_						:bool = False ):





										"""simple docstring"""

										lowerCamelCase__			:  Any  =10

										if only_cross_attention:
													lowerCamelCase__			:  List[str]  =12
										else:
													# when only cross attention is not set, the cross attention dim must be the same as the query dim
													lowerCamelCase__			:  List[Any]  =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						:Dict  ,							lowerCamelCase_						:Optional[int]  ,							lowerCamelCase_						:Dict ):





										"""simple docstring"""

										lowerCamelCase__			:  Union[str, Any]  =2

										lowerCamelCase__			:  Union[str, Any]  =torch.rand(lowerCamelCase_  ,							lowerCamelCase_  ,							3  ,							2 )
										lowerCamelCase__			:  Any  =torch.rand(lowerCamelCase_  ,							4  ,							lowerCamelCase_ )
										lowerCamelCase__			:  List[Any]  =None

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





							def    UpperCAmelCase__							(						self						:Optional[Any] ):





										"""simple docstring"""

										torch.manual_seed(0 )

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

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

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

										lowerCamelCase__			:  Dict  =attn(**lowerCamelCase_ )

										# only self attention

										torch.manual_seed(0 )

										lowerCamelCase__			:  Optional[int]  =self.get_constructor_arguments(only_cross_attention=lowerCamelCase_ )
										lowerCamelCase__			:  Optional[Any]  =Attention(**lowerCamelCase_ )

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

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

										lowerCamelCase__			:  Optional[Any]  =attn(**lowerCamelCase_ )

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






class 					A_			(  unittest.TestCase     ):



							"""simple docstring"""






							def    UpperCAmelCase__							(						self						:List[Any] ):





										"""simple docstring"""

										lowerCamelCase__			:  List[Any]  =DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe'  ,							safety_checker=lowerCamelCase_ )

										lowerCamelCase__			:  Optional[int]  =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=lowerCamelCase_ )

										lowerCamelCase__			:  Dict  =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(lowerCamelCase_ )

													# can also load the converted weights
													lowerCamelCase__			:  Tuple  =DiffusionPipeline.from_pretrained(lowerCamelCase_  ,							device_map='sequential'  ,							safety_checker=lowerCamelCase_ )

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

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

"""simple docstring"""


# A Bipartite Graph is a graph whose vertices can be divided into two independent sets,
# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.
from queue import Queue






def      lowerCAmelCase_     (	snake_case_	:    List[Any]    )     ->Optional[int]:
			lowerCamelCase__			:  Optional[int]  =Queue()
			lowerCamelCase__			:  Any  =[False] * len(snake_case_    )
			lowerCamelCase__			:  Optional[Any]  =[-1] * len(snake_case_    )

			def bfs():
						while not queue.empty():
									lowerCamelCase__			:  List[Any]  =queue.get()
									lowerCamelCase__			:  int  =True

									for neighbour in graph[u]:
												if neighbour == u:
															return False

												if color[neighbour] == -1:
															lowerCamelCase__			:  Union[str, Any]  =1 - color[u]
															queue.put(snake_case_    )

												elif color[neighbour] == color[u]:
															return False

						return True

			for i in range(len(snake_case_    )    ):
						if not visited[i]:
									queue.put(snake_case_    )
									lowerCamelCase__			:  Tuple  =0
									if bfs() is False:
												return False

			return True


if __name__ == "__main__":
							# Adjacency List of graph
							print(check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}))