





"""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							):

       def 		_SCREAMING_SNAKE_CASE(			self   ,					__SCREAMING_SNAKE_CASE = False     ):






        '''simple docstring'''

        snake_case__    							=				10

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






        '''simple docstring'''

        snake_case__    							=				2

        snake_case__    							=				torch.rand(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					3   ,					2     )
        snake_case__    							=				torch.rand(__SCREAMING_SNAKE_CASE   ,					4   ,					__SCREAMING_SNAKE_CASE     )
        snake_case__    							=				None

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

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        torch.manual_seed(0     )

        snake_case__    							=				self.get_constructor_arguments(only_cross_attention=__SCREAMING_SNAKE_CASE     )
        snake_case__    							=				Attention(**__SCREAMING_SNAKE_CASE     )

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

        snake_case__    							=				self.get_forward_arguments(
            query_dim=constructor_args["query_dim"]   ,					added_kv_proj_dim=constructor_args["added_kv_proj_dim"]     )

        snake_case__    							=				attn(**__SCREAMING_SNAKE_CASE     )

        # only self attention

        torch.manual_seed(0     )

        snake_case__    							=				self.get_constructor_arguments(only_cross_attention=__SCREAMING_SNAKE_CASE     )
        snake_case__    							=				Attention(**__SCREAMING_SNAKE_CASE     )

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

        snake_case__    							=				self.get_forward_arguments(
            query_dim=constructor_args["query_dim"]   ,					added_kv_proj_dim=constructor_args["added_kv_proj_dim"]     )

        snake_case__    							=				attn(**__SCREAMING_SNAKE_CASE     )

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



class      lowercase_				(     unittest.TestCase							):

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__    							=				DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe"   ,					safety_checker=__SCREAMING_SNAKE_CASE     )

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

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

        snake_case__    							=				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(__SCREAMING_SNAKE_CASE     )

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

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

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






"""simple docstring"""


class      lowercase_				:

       def __init__(			self   ,					__SCREAMING_SNAKE_CASE     ):






        '''simple docstring'''

        snake_case__    							=				data
        snake_case__    							=				42
        snake_case__    							=				42





def 							a__			(  _UpperCAmelCase	)    ->  None:

 '''simple docstring'''






 snake_case__    							=				0
 snake_case__    							=				x





def 							a__			(  _UpperCAmelCase	,     _UpperCAmelCase	)    ->  None:

 '''simple docstring'''






 snake_case__							,   snake_case__    							=				find_set(_UpperCAmelCase	), find_set(_UpperCAmelCase	)
 if x == y:
  return

 elif x.rank > y.rank:
  snake_case__    							=				x
 else:
  snake_case__    							=				y
  if x.rank == y.rank:
   y.rank += 1





def 							a__			(  _UpperCAmelCase	)    ->  Node:

 '''simple docstring'''






 if x != x.parent:
  snake_case__    							=				find_set(x.parent	)
 return x.parent





def 							a__			(  _UpperCAmelCase	)    ->  set:

 '''simple docstring'''






 snake_case__    							=				({0, 1, 2}, {3, 4, 5})
 for s in sets:
  if node.data in s:
   return s
 snake_case__    							=				f"{node.data} is not in {sets}"
 raise ValueError(_UpperCAmelCase	)





def 							a__			(  )    ->  None:

 '''simple docstring'''






 snake_case__    							=				[Node(_UpperCAmelCase	) for i in range(6	)]
 for v in vertex:
  make_set(_UpperCAmelCase	)

 union_set(vertex[0]	,     vertex[1]	)
 union_set(vertex[1]	,     vertex[2]	)
 union_set(vertex[3]	,     vertex[4]	)
 union_set(vertex[3]	,     vertex[5]	)

 for nodea in vertex:
  for nodea in vertex:
   if find_python_set(_UpperCAmelCase	).isdisjoint(find_python_set(_UpperCAmelCase	)	):
    assert find_set(_UpperCAmelCase	) != find_set(_UpperCAmelCase	)
   else:
    assert find_set(_UpperCAmelCase	) == find_set(_UpperCAmelCase	)


if __name__ == "__main__":
   test_disjoint_set()

