





'''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 lowerCamelCase    (unittest.TestCase       ):







  def 					UpperCAmelCase_					(		self  ,  lowercase__ = False		)		->    Tuple:






        """simple docstring"""





        _snake_case		:						List[str]         =  10

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






        """simple docstring"""





        _snake_case		:						List[Any]         =  2

        _snake_case		:						Any         =  torch.rand(__snake_case  ,  __snake_case  ,  3  ,  2		)
        _snake_case		:						Dict         =  torch.rand(__snake_case  ,  4  ,  __snake_case		)
        _snake_case		:						Any         =  None

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







  def 					UpperCAmelCase_					(		self		)		->    Union[str, Any]:






        """simple docstring"""





        torch.manual_seed(0		)

        _snake_case		:						Tuple         =  self.get_constructor_arguments(only_cross_attention=__snake_case		)
        _snake_case		:						Dict         =  Attention(**__snake_case		)

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

        _snake_case		:						Tuple         =  self.get_forward_arguments(
            query_dim=constructor_args['''query_dim''']  ,  added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']		)

        _snake_case		:						List[str]         =  attn(**__snake_case		)

        # only self attention

        torch.manual_seed(0		)

        _snake_case		:						str         =  self.get_constructor_arguments(only_cross_attention=__snake_case		)
        _snake_case		:						Tuple         =  Attention(**__snake_case		)

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

        _snake_case		:						Any         =  self.get_forward_arguments(
            query_dim=constructor_args['''query_dim''']  ,  added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']		)

        _snake_case		:						str         =  attn(**__snake_case		)

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





class lowerCamelCase    (unittest.TestCase       ):







  def 					UpperCAmelCase_					(		self		)		->    Optional[int]:






        """simple docstring"""





        _snake_case		:						Union[str, Any]         =  DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''  ,  safety_checker=__snake_case		)

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

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

        _snake_case		:						Tuple         =  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(__snake_case		)

              # can also load the converted weights
              _snake_case		:						int         =  DiffusionPipeline.from_pretrained(__snake_case  ,  device_map='''sequential'''  ,  safety_checker=__snake_case		)

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

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









def  A					(				UpperCAmelCase	,	UpperCAmelCase					):
					# "Simpson Rule"
					# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
					_snake_case							:       List[str]												= (boundary[1] - boundary[0]) / steps
					_snake_case							:       List[Any]												= boundary[0]
					_snake_case							:       Tuple												= boundary[1]
					_snake_case							:       str												= make_points(UpperCAmelCase	,	UpperCAmelCase	,	UpperCAmelCase					)
					_snake_case							:       str												= 0.0
					y += (h / 3.0) * f(UpperCAmelCase					)
					_snake_case							:       Tuple												= 2
					for i in x_i:
										y += (h / 3) * (4 - 2 * (cnt % 2)) * f(UpperCAmelCase					)
										cnt += 1
					y += (h / 3.0) * f(UpperCAmelCase					)
					return y







def  A					(				UpperCAmelCase	,	UpperCAmelCase	,	UpperCAmelCase					):
					_snake_case							:       Union[str, Any]												= a + h
					while x < (b - h):
										yield x
										_snake_case							:       Optional[Any]												= x + h







def  A					(				UpperCAmelCase					):  # enter your function here
					_snake_case							:       Dict												= (x - 0) * (x - 0)
					return y







def  A					(				):
					_snake_case							:       int												= 0.0  # Lower bound of integration
					_snake_case							:       Union[str, Any]												= 1.0  # Upper bound of integration
					_snake_case							:       List[str]												= 1_0.0  # define number of steps or resolution
					_snake_case							:       Optional[int]												= [a, b]  # define boundary of integration
					_snake_case							:       List[str]												= method_a(UpperCAmelCase	,	UpperCAmelCase					)
					print(F"""y = {y}"""					)


if __name__ == "__main__":
							main()