'''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 __UpperCAmelCase							( self					:						Any ,		__lowerCAmelCase					:						bool = False)   ->       Optional[int]:
         lowercase_     			=   10

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

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

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


   def __UpperCAmelCase							( self					:						List[str])   ->       str:
         # self and cross attention

         torch.manual_seed(0)

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

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

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

         lowercase_     			=   attn(**__lowerCAmelCase)

         # only self attention

         torch.manual_seed(0)

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

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

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

         lowercase_     			=   attn(**__lowerCAmelCase)

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





class 				lowercase    (    unittest.TestCase			):


   def __UpperCAmelCase							( self					:						Dict)   ->       Union[str, Any]:
         lowercase_     			=   DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe" ,		safety_checker=__lowerCAmelCase)

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

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

         lowercase_     			=   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
               lowercase_     			=   DiffusionPipeline.from_pretrained(__lowerCAmelCase ,		device_map="sequential" ,		safety_checker=__lowerCAmelCase)

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

import timeit





class 				lowercase    :


   def __init__( self					:						Dict ,		__lowerCAmelCase					:						Any)   ->       Optional[int]:
         if isinstance(__lowerCAmelCase ,		__lowerCAmelCase):  # Initializes a matrix identical to the one provided.
               lowercase_     			=   arg
               lowercase_     			=   len(__lowerCAmelCase)
         else:  # Initializes a square matrix of the given size and set values to zero.
               lowercase_     			=   arg
               lowercase_     			=   [[0 for _ in range(self.n)] for _ in range(self.n)]


   def __mul__( self					:						int ,		__lowerCAmelCase					:						List[str])   ->       Union[str, Any]:
         lowercase_     			=   Matrix(self.n)
         for i in range(self.n):
               for j in range(self.n):
                     for k in range(self.n):
                           matrix.t[i][j] += self.t[i][k] * b.t[k][j]
         return matrix




def __a			(						__lowerCamelCase      :    List[str]			,		__lowerCamelCase      :    Dict					)						->				Optional[Any]:
      '''simple docstring'''


      lowercase_     			=   Matrix([[1, 0], [0, 1]]					)
      while b > 0:
            if b & 1:
                  matrix *= a
            a *= a
            b >>= 1
      return matrix


def __a			(						__lowerCamelCase      :    Tuple			,		__lowerCamelCase      :    Tuple			,		__lowerCamelCase      :    List[Any]					)						->				Dict:
      '''simple docstring'''


      if n == 1:
            return fa
      elif n == 2:
            return fa
      lowercase_     			=   Matrix([[1, 1], [1, 0]]					)
      lowercase_     			=   modular_exponentiation(__lowerCamelCase			,		n - 2					)
      return fa * matrix.t[0][0] + fa * matrix.t[0][1]


def __a			(						__lowerCamelCase      :    List[Any]			,		__lowerCamelCase      :    Union[str, Any]			,		__lowerCamelCase      :    List[str]					)						->				Optional[Any]:
      '''simple docstring'''


      if n == 1:
            return fa
      elif n == 2:
            return fa

      lowercase_     			=   fa
      lowercase_     			=   fa
      n -= 2

      while n > 0:
            lowercase_			,				lowercase_     			=   fn_a + fn_a, fn_a
            n -= 1

      return fn_a


def __a			(						)						->				str:
      '''simple docstring'''


      lowercase_     			=   "\nfrom random import randint\nfrom __main__ import fibonacci_with_matrix_exponentiation\n"
      lowercase_     			=   "fibonacci_with_matrix_exponentiation(randint(1,70000), 1, 1)"
      lowercase_     			=   timeit.timeit(setup=__lowerCamelCase			,		stmt=__lowerCamelCase			,		number=100					)
      print("With matrix exponentiation the average execution time is "			,		exec_time / 100					)
      return exec_time


def __a			(						)						->				Optional[Any]:
      '''simple docstring'''


      lowercase_     			=   "\nfrom random import randint\nfrom __main__ import simple_fibonacci\n"
      lowercase_     			=   "simple_fibonacci(randint(1,70000), 1, 1)"
      lowercase_     			=   timeit.timeit(setup=__lowerCamelCase			,		stmt=__lowerCamelCase			,		number=100					)
      print(
          "Without matrix exponentiation the average execution time is "			,		exec_time / 100					)
      return exec_time


def __a			(						)						->				Tuple:
      '''simple docstring'''


      matrix_exponentiation_time()
      simple_fibonacci_time()


if __name__ == "__main__":
       main()






