





import tempfile
import unittest

import numpy as np
import torch

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



class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    def    lowercase_				(					self      ,							A_ = False    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= 10

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





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= 2

        SCREAMING_SNAKE_CASE__       							= torch.rand(A_      ,							A_      ,							3      ,							2    )
        SCREAMING_SNAKE_CASE__       							= torch.rand(A_      ,							4      ,							A_    )
        SCREAMING_SNAKE_CASE__       							= None

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




    def    lowercase_				(					self    ):





        '''simple docstring'''

        torch.manual_seed(0    )

        SCREAMING_SNAKE_CASE__       							= self.get_constructor_arguments(only_cross_attention=A_    )
        SCREAMING_SNAKE_CASE__       							= Attention(**A_    )

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

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

        SCREAMING_SNAKE_CASE__       							= attn(**A_    )

        # only self attention

        torch.manual_seed(0    )

        SCREAMING_SNAKE_CASE__       							= self.get_constructor_arguments(only_cross_attention=A_    )
        SCREAMING_SNAKE_CASE__       							= Attention(**A_    )

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

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

        SCREAMING_SNAKE_CASE__       							= attn(**A_    )

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





class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''      ,							safety_checker=A_    )

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

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

        SCREAMING_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(A_    )

            # can also load the converted weights
            SCREAMING_SNAKE_CASE__       							= DiffusionPipeline.from_pretrained(A_      ,							device_map='''sequential'''      ,							safety_checker=A_    )

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








import numpy as np

_A				:							List[Any]							= [
    ["""a""", """b""", """c""", """d""", """e"""],
    ["""f""", """g""", """h""", """i""", """k"""],
    ["""l""", """m""", """n""", """o""", """p"""],
    ["""q""", """r""", """s""", """t""", """u"""],
    ["""v""", """w""", """x""", """y""", """z"""],
]



class 				__snake_case  :


    '''simple docstring'''







    def __init__(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= np.array(A_    )



    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= np.where(letter == self.SQUARE    )
        SCREAMING_SNAKE_CASE__       							= np.concatenate([indexa + 1, indexa + 1]    )
        return indexes



    def    lowercase_				(					self      ,							A_      ,							A_    ):





        '''simple docstring'''

        return self.SQUARE[indexa - 1, indexa - 1]



    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= message.lower()
        SCREAMING_SNAKE_CASE__       							= message.replace('''j'''      ,							'''i'''    )

        SCREAMING_SNAKE_CASE__       							= ''''''
        for letter_index in range(len(A_    )    ):
            if message[letter_index] != " ":
                SCREAMING_SNAKE_CASE__       							= self.letter_to_numbers(message[letter_index]    )
                SCREAMING_SNAKE_CASE__       							= encoded_message + str(numbers[0]    ) + str(numbers[1]    )
            elif message[letter_index] == " ":
                SCREAMING_SNAKE_CASE__       							= encoded_message + ''' '''

        return encoded_message




    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= message.replace(''' '''      ,							'''  '''    )
        SCREAMING_SNAKE_CASE__       							= ''''''
        for numbers_index in range(int(len(A_    ) / 2    )    ):
            if message[numbers_index * 2] != " ":
                SCREAMING_SNAKE_CASE__       							= message[numbers_index * 2]
                SCREAMING_SNAKE_CASE__       							= message[numbers_index * 2 + 1]

                SCREAMING_SNAKE_CASE__       							= self.numbers_to_letter(int(A_    )      ,							int(A_    )    )
                SCREAMING_SNAKE_CASE__       							= decoded_message + letter
            elif message[numbers_index * 2] == " ":
                SCREAMING_SNAKE_CASE__       							= decoded_message + ''' '''

        return decoded_message
