






from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin




@dataclass
class    lowercase_    (_UpperCAmelCase     ):
      A__	:		torch.FloatTensor







class    lowercase_    (_UpperCAmelCase,    _UpperCAmelCase     ):






      @register_to_config
      def __init__(    self     ,     a_ = 1_6     ,     a_ = 8_8     ,     a_ = None     ,     a_ = None     ,     a_ = 1     ,     a_ = 0.0     ,     a_ = 3_2     ,     a_ = None     ,     a_ = False     ,     a_ = None     ,     a_ = "geglu"     ,     a_ = True     ,     a_ = True     ,     )	->int:







        '''simple docstring'''


        super().__init__()
        _a							=	num_attention_heads
        _a							=	attention_head_dim
        _a							=	num_attention_heads * attention_head_dim

        _a							=	in_channels

        _a							=	torch.nn.GroupNorm(num_groups=a_     ,     num_channels=a_     ,     eps=1E-6     ,     affine=a_	)
        _a							=	nn.Linear(a_     ,     a_	)

        # 3. Define transformers blocks
        _a							=	nn.ModuleList(
            [
                BasicTransformerBlock(
                    a_     ,     a_     ,     a_     ,     dropout=a_     ,     cross_attention_dim=a_     ,     activation_fn=a_     ,     attention_bias=a_     ,     double_self_attention=a_     ,     norm_elementwise_affine=a_     ,     )
                for d in range(a_	)
            ]	)

        _a							=	nn.Linear(a_     ,     a_	)






      def 			lowerCamelCase__    (    self     ,     a_     ,     a_=None     ,     a_=None     ,     a_=None     ,     a_=1     ,     a_=None     ,     a_ = True     ,     )	->List[Any]:







        '''simple docstring'''


        _a			,	_a			,	_a			,	_a							=	hidden_states.shape
        _a							=	batch_frames // num_frames

        _a							=	hidden_states

        _a							=	hidden_states[None, :].reshape(a_     ,     a_     ,     a_     ,     a_     ,     a_	)
        _a							=	hidden_states.permute(0     ,     2     ,     1     ,     3     ,     4	)

        _a							=	self.norm(a_	)
        _a							=	hidden_states.permute(0     ,     3     ,     4     ,     2     ,     1	).reshape(batch_size * height * width     ,     a_     ,     a_	)

        _a							=	self.proj_in(a_	)

        # 2. Blocks
        for block in self.transformer_blocks:
          _a							=	block(
              a_     ,     encoder_hidden_states=a_     ,     timestep=a_     ,     cross_attention_kwargs=a_     ,     class_labels=a_     ,     )

        # 3. Output
        _a							=	self.proj_out(a_	)
        _a							=	(
            hidden_states[None, None, :]
            .reshape(a_     ,     a_     ,     a_     ,     a_     ,     a_	)
            .permute(0     ,     3     ,     4     ,     1     ,     2	)
            .contiguous()
        )
        _a							=	hidden_states.reshape(a_     ,     a_     ,     a_     ,     a_	)

        _a							=	hidden_states + residual

        if not return_dict:
          return (output,)

        return TransformerTemporalModelOutput(sample=a_	)










"""simple docstring"""




import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest




class    lowercase_    (_UpperCAmelCase     ):
      A__	:		Tuple       			=				(KDPMaDiscreteScheduler,)
      A__	:		Tuple       			=				10






      def 			lowerCamelCase__    (    self     ,     **a_	)	->List[str]:







        '''simple docstring'''


        _a							=	{
            "num_train_timesteps": 1_1_0_0,
            "beta_start": 0.0_001,
            "beta_end": 0.02,
            "beta_schedule": "linear",
        }

        config.update(**a_	)
        return config






      def 			lowerCamelCase__    (    self	)	->Tuple:







        '''simple docstring'''


        for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]:
          self.check_over_configs(num_train_timesteps=a_	)






      def 			lowerCamelCase__    (    self	)	->List[Any]:







        '''simple docstring'''


        for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]     ,     [0.0_002, 0.002, 0.02]	):
          self.check_over_configs(beta_start=a_     ,     beta_end=a_	)






      def 			lowerCamelCase__    (    self	)	->Any:







        '''simple docstring'''


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






      def 			lowerCamelCase__    (    self	)	->int:







        '''simple docstring'''


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






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







        '''simple docstring'''


        _a							=	self.scheduler_classes[0]
        _a							=	self.get_scheduler_config(prediction_type="v_prediction"	)
        _a							=	scheduler_class(**a_	)

        scheduler.set_timesteps(self.num_inference_steps	)

        _a							=	self.dummy_model()
        _a							=	self.dummy_sample_deter * scheduler.init_noise_sigma
        _a							=	sample.to(a_	)

        for i, t in enumerate(scheduler.timesteps	):
          _a							=	scheduler.scale_model_input(a_     ,     a_	)

          _a							=	model(a_     ,     a_	)

          _a							=	scheduler.step(a_     ,     a_     ,     a_	)
          _a							=	output.prev_sample

        _a							=	torch.sum(torch.abs(a_	)	)
        _a							=	torch.mean(torch.abs(a_	)	)

        if torch_device in ["cpu", "mps"]:
          assert abs(result_sum.item() - 4.6_934E-07	) < 1E-2
          assert abs(result_mean.item() - 6.1_112E-10	) < 1E-3
        else:
          # CUDA
          assert abs(result_sum.item() - 4.693_428_650_170_972E-07	) < 1E-2
          assert abs(result_mean.item() - 0.0_002	) < 1E-3






      def 			lowerCamelCase__    (    self	)	->Any:







        '''simple docstring'''


        if torch_device == "mps":
          return
        _a							=	self.scheduler_classes[0]
        _a							=	self.get_scheduler_config()
        _a							=	scheduler_class(**a_	)

        scheduler.set_timesteps(self.num_inference_steps	)

        _a							=	self.dummy_model()
        _a							=	self.dummy_sample_deter * scheduler.init_noise_sigma
        _a							=	sample.to(a_	)

        for i, t in enumerate(scheduler.timesteps	):
          _a							=	scheduler.scale_model_input(a_     ,     a_	)

          _a							=	model(a_     ,     a_	)

          _a							=	scheduler.step(a_     ,     a_     ,     a_	)
          _a							=	output.prev_sample

        _a							=	torch.sum(torch.abs(a_	)	)
        _a							=	torch.mean(torch.abs(a_	)	)

        if torch_device in ["cpu", "mps"]:
          assert abs(result_sum.item() - 20.4_125	) < 1E-2
          assert abs(result_mean.item() - 0.0_266	) < 1E-3
        else:
          # CUDA
          assert abs(result_sum.item() - 20.4_125	) < 1E-2
          assert abs(result_mean.item() - 0.0_266	) < 1E-3






      def 			lowerCamelCase__    (    self	)	->int:







        '''simple docstring'''


        if torch_device == "mps":
          return
        _a							=	self.scheduler_classes[0]
        _a							=	self.get_scheduler_config()
        _a							=	scheduler_class(**a_	)

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

        _a							=	self.dummy_model()
        _a							=	self.dummy_sample_deter.to(a_	) * scheduler.init_noise_sigma

        for t in scheduler.timesteps:
          _a							=	scheduler.scale_model_input(a_     ,     a_	)

          _a							=	model(a_     ,     a_	)

          _a							=	scheduler.step(a_     ,     a_     ,     a_	)
          _a							=	output.prev_sample

        _a							=	torch.sum(torch.abs(a_	)	)
        _a							=	torch.mean(torch.abs(a_	)	)

        if str(a_	).startswith("cpu"	):
          # The following sum varies between 148 and 156 on mps. Why?
          assert abs(result_sum.item() - 20.4_125	) < 1E-2
          assert abs(result_mean.item() - 0.0_266	) < 1E-3
        else:
          # CUDA
          assert abs(result_sum.item() - 20.4_125	) < 1E-2
          assert abs(result_mean.item() - 0.0_266	) < 1E-3


