


'''simple docstring'''



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   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    lowerCamelCase    :   torch.FloatTensor







class   UpperCAmelCase_    (_UpperCAmelCase   ,						_UpperCAmelCase    ):





    """simple docstring"""






    @register_to_config
    def __init__(				self     ,	SCREAMING_SNAKE_CASE_ = 16     ,	SCREAMING_SNAKE_CASE_ = 88     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = 1     ,	SCREAMING_SNAKE_CASE_ = 0.0     ,	SCREAMING_SNAKE_CASE_ = 32     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = False     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = "geglu"     ,	SCREAMING_SNAKE_CASE_ = True     ,	SCREAMING_SNAKE_CASE_ = True     ,	)							->	Optional[Any]:
         super().__init__()
         __lowerCamelCase  :      int														=						num_attention_heads
         __lowerCamelCase  :      str														=						attention_head_dim
         __lowerCamelCase  :      Optional[int]														=						num_attention_heads * attention_head_dim

         __lowerCamelCase  :      Tuple														=						in_channels

         __lowerCamelCase  :      List[str]														=						torch.nn.GroupNorm(num_groups=SCREAMING_SNAKE_CASE_     ,	num_channels=SCREAMING_SNAKE_CASE_     ,	eps=1E-6     ,	affine=SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      List[Any]														=						nn.Linear(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )

         # 3. Define transformers blocks
         __lowerCamelCase  :      List[str]														=						nn.ModuleList(
             [
                 BasicTransformerBlock(
                     SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	dropout=SCREAMING_SNAKE_CASE_     ,	cross_attention_dim=SCREAMING_SNAKE_CASE_     ,	activation_fn=SCREAMING_SNAKE_CASE_     ,	attention_bias=SCREAMING_SNAKE_CASE_     ,	double_self_attention=SCREAMING_SNAKE_CASE_     ,	norm_elementwise_affine=SCREAMING_SNAKE_CASE_     ,	)
                 for d in range(SCREAMING_SNAKE_CASE_      )
             ]      )

         __lowerCamelCase  :      Union[str, Any]														=						nn.Linear(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=1     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_ = True     ,	)							->	Union[str, Any]:
         __lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase  :      Dict														=						hidden_states.shape
         __lowerCamelCase  :      Dict														=						batch_frames // num_frames

         __lowerCamelCase  :      Tuple														=						hidden_states

         __lowerCamelCase  :      Any														=						hidden_states[None, :].reshape(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      int														=						hidden_states.permute(0     ,	2     ,	1     ,	3     ,	4      )

         __lowerCamelCase  :      List[str]														=						self.norm(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      Tuple														=						hidden_states.permute(0     ,	3     ,	4     ,	2     ,	1      ).reshape(batch_size * height * width     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Any														=						self.proj_in(SCREAMING_SNAKE_CASE_      )

         # 2. Blocks
         for block in self.transformer_blocks:
              __lowerCamelCase  :      List[Any]														=						block(
                  SCREAMING_SNAKE_CASE_     ,	encoder_hidden_states=SCREAMING_SNAKE_CASE_     ,	timestep=SCREAMING_SNAKE_CASE_     ,	cross_attention_kwargs=SCREAMING_SNAKE_CASE_     ,	class_labels=SCREAMING_SNAKE_CASE_     ,	)

         # 3. Output
         __lowerCamelCase  :      Dict														=						self.proj_out(SCREAMING_SNAKE_CASE_      )
         __lowerCamelCase  :      str														=						(
             hidden_states[None, None, :]
             .reshape(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )
             .permute(0     ,	3     ,	4     ,	1     ,	2      )
             .contiguous()
         )
         __lowerCamelCase  :      List[Any]														=						hidden_states.reshape(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )

         __lowerCamelCase  :      Dict														=						hidden_states + residual

         if not return_dict:
              return (output,)

         return TransformerTemporalModelOutput(sample=SCREAMING_SNAKE_CASE_      )













'''simple docstring'''




from typing import List, Optional, Tuple, Union

import torch

from ...schedulers import DDIMScheduler
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput






class 				__snake_case     (					__magic_name__							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_		)      ->		int:
		super().__init__()

		# make sure scheduler can always be converted to DDIM
		snake_case__	       =   DDIMScheduler.from_config(scheduler.config		)

		self.register_modules(unet=UpperCamelCase_      ,       scheduler=UpperCamelCase_		)





	@torch.no_grad()
	def __call__(		self      ,       UpperCamelCase_ = 1      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = 0.0      ,       UpperCamelCase_ = 50      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = "pil"      ,       UpperCamelCase_ = True      ,       )      ->		Union[ImagePipelineOutput, Tuple]:

		# Sample gaussian noise to begin loop
		if isinstance(self.unet.config.sample_size      ,       UpperCamelCase_		):
			snake_case__	       =   (
			    batch_size,
			    self.unet.config.in_channels,
			    self.unet.config.sample_size,
			    self.unet.config.sample_size,
			)
		else:
			snake_case__	       =   (batch_size, self.unet.config.in_channels, *self.unet.config.sample_size)

		if isinstance(UpperCamelCase_      ,       UpperCamelCase_		) and len(UpperCamelCase_		) != batch_size:
			raise ValueError(
			    F'''You have passed a list of generators of length {len(UpperCamelCase_		)}, but requested an effective batch'''
			    F''' size of {batch_size}. Make sure the batch size matches the length of the generators.'''		)

		snake_case__	       =   randn_tensor(UpperCamelCase_      ,       generator=UpperCamelCase_      ,       device=self.device      ,       dtype=self.unet.dtype		)

		# set step values
		self.scheduler.set_timesteps(UpperCamelCase_		)

		for t in self.progress_bar(self.scheduler.timesteps		):
			# 1. predict noise model_output
			snake_case__	       =   self.unet(UpperCamelCase_      ,       UpperCamelCase_		).sample

			# 2. predict previous mean of image x_t-1 and add variance depending on eta
			# eta corresponds to η in paper and should be between [0, 1]
			# do x_t -> x_t-1
			snake_case__	       =   self.scheduler.step(
			    UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       eta=UpperCamelCase_      ,       use_clipped_model_output=UpperCamelCase_      ,       generator=UpperCamelCase_		).prev_sample

		snake_case__	       =   (image / 2 + 0.5).clamp(0      ,       1		)
		snake_case__	       =   image.cpu().permute(0      ,       2      ,       3      ,       1		).numpy()
		if output_type == "pil":
			snake_case__	       =   self.numpy_to_pil(UpperCamelCase_		)

		if not return_dict:
			return (image,)

		return ImagePipelineOutput(images=UpperCamelCase_		)



