





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


import torch

from diffusers import LMSDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest



class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							Optional[int]										=       (LMSDiscreteScheduler,)
       snake_case__			:							int										=       10

       def 						_SCREAMING_SNAKE_CASE(     self				,   **__SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

        snake_case__      							=			{
            "num_train_timesteps": 1100,
            "beta_start": 0.0001,
            "beta_end": 0.02,
            "beta_schedule": "linear",
        }

        config.update(**__SCREAMING_SNAKE_CASE       )
        return config

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        for timesteps in [10, 50, 100, 1000]:
         self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]				,   [0.0002, 0.002, 0.02]       ):
         self.check_over_configs(beta_start=__SCREAMING_SNAKE_CASE				,   beta_end=__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        for schedule in ["linear", "scaled_linear"]:
         self.check_over_configs(beta_schedule=__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        for prediction_type in ["epsilon", "v_prediction"]:
         self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        for t in [0, 500, 800]:
         self.check_over_forward(time_step=__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.scheduler_classes[0]
        snake_case__      							=			self.get_scheduler_config()
        snake_case__      							=			scheduler_class(**__SCREAMING_SNAKE_CASE       )

        scheduler.set_timesteps(self.num_inference_steps       )

        snake_case__      							=			self.dummy_model()
        snake_case__      							=			self.dummy_sample_deter * scheduler.init_noise_sigma

        for i, t in enumerate(scheduler.timesteps       ):
         snake_case__      							=			scheduler.scale_model_input(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			scheduler.step(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         snake_case__      							=			output.prev_sample

        snake_case__      							=			torch.sum(torch.abs(__SCREAMING_SNAKE_CASE       )       )
        snake_case__      							=			torch.mean(torch.abs(__SCREAMING_SNAKE_CASE       )       )

        assert abs(result_sum.item() - 1006.388       ) < 1e-2
        assert abs(result_mean.item() - 1.31       ) < 1e-3

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.scheduler_classes[0]
        snake_case__      							=			self.get_scheduler_config(prediction_type="v_prediction"       )
        snake_case__      							=			scheduler_class(**__SCREAMING_SNAKE_CASE       )

        scheduler.set_timesteps(self.num_inference_steps       )

        snake_case__      							=			self.dummy_model()
        snake_case__      							=			self.dummy_sample_deter * scheduler.init_noise_sigma

        for i, t in enumerate(scheduler.timesteps       ):
         snake_case__      							=			scheduler.scale_model_input(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			scheduler.step(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         snake_case__      							=			output.prev_sample

        snake_case__      							=			torch.sum(torch.abs(__SCREAMING_SNAKE_CASE       )       )
        snake_case__      							=			torch.mean(torch.abs(__SCREAMING_SNAKE_CASE       )       )

        assert abs(result_sum.item() - 0.0017       ) < 1e-2
        assert abs(result_mean.item() - 2.2676e-06       ) < 1e-3

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.scheduler_classes[0]
        snake_case__      							=			self.get_scheduler_config()
        snake_case__      							=			scheduler_class(**__SCREAMING_SNAKE_CASE       )

        scheduler.set_timesteps(self.num_inference_steps				,   device=__SCREAMING_SNAKE_CASE       )

        snake_case__      							=			self.dummy_model()
        snake_case__      							=			self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
        snake_case__      							=			sample.to(__SCREAMING_SNAKE_CASE       )

        for i, t in enumerate(scheduler.timesteps       ):
         snake_case__      							=			scheduler.scale_model_input(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			scheduler.step(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         snake_case__      							=			output.prev_sample

        snake_case__      							=			torch.sum(torch.abs(__SCREAMING_SNAKE_CASE       )       )
        snake_case__      							=			torch.mean(torch.abs(__SCREAMING_SNAKE_CASE       )       )

        assert abs(result_sum.item() - 1006.388       ) < 1e-2
        assert abs(result_mean.item() - 1.31       ) < 1e-3

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.scheduler_classes[0]
        snake_case__      							=			self.get_scheduler_config()
        snake_case__      							=			scheduler_class(**__SCREAMING_SNAKE_CASE				,   use_karras_sigmas=__SCREAMING_SNAKE_CASE       )

        scheduler.set_timesteps(self.num_inference_steps				,   device=__SCREAMING_SNAKE_CASE       )

        snake_case__      							=			self.dummy_model()
        snake_case__      							=			self.dummy_sample_deter.to(__SCREAMING_SNAKE_CASE       ) * scheduler.init_noise_sigma
        snake_case__      							=			sample.to(__SCREAMING_SNAKE_CASE       )

        for t in scheduler.timesteps:
         snake_case__      							=			scheduler.scale_model_input(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			scheduler.step(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         snake_case__      							=			output.prev_sample

        snake_case__      							=			torch.sum(torch.abs(__SCREAMING_SNAKE_CASE       )       )
        snake_case__      							=			torch.mean(torch.abs(__SCREAMING_SNAKE_CASE       )       )

        assert abs(result_sum.item() - 3812.9927       ) < 2e-2
        assert abs(result_mean.item() - 4.9648       ) < 1e-3

