from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block

@dataclass
class 				_lowercase						(    snake_case_ ):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:							Optional[int]        =						42





class 				_lowercase						(    snake_case_   ,     snake_case_ ):






    '''simple docstring'''




    @register_to_config
    def __init__(						self					,	lowerCamelCase__ = None					,	lowerCamelCase__ = 3					,	lowerCamelCase__ = 3					,	lowerCamelCase__ = False					,	lowerCamelCase__ = "positional"					,	lowerCamelCase__ = 0					,	lowerCamelCase__ = True					,	lowerCamelCase__ = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")					,	lowerCamelCase__ = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")					,	lowerCamelCase__ = (224, 448, 672, 896)					,	lowerCamelCase__ = 2					,	lowerCamelCase__ = 1					,	lowerCamelCase__ = 1					,	lowerCamelCase__ = "conv"					,	lowerCamelCase__ = "conv"					,	lowerCamelCase__ = "silu"					,	lowerCamelCase__ = 8					,	lowerCamelCase__ = 32					,	lowerCamelCase__ = 1E-5					,	lowerCamelCase__ = "default"					,	lowerCamelCase__ = True					,	lowerCamelCase__ = None					,	lowerCamelCase__ = None					,	):
       super().__init__()

       lowerCAmelCase_:       Union[str, Any]         =  sample_size
       lowerCAmelCase_:       Dict         =  block_out_channels[0] * 4

       # Check inputs
       if len(lowerCamelCase__   ) != len(lowerCamelCase__   ):
          raise ValueError(
              F'''Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: {down_block_types}. `up_block_types`: {up_block_types}.'''   )

       if len(lowerCamelCase__   ) != len(lowerCamelCase__   ):
          raise ValueError(
              F'''Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}.'''   )

       # input
       lowerCAmelCase_:       Union[str, Any]         =  nn.Convad(lowerCamelCase__					,	block_out_channels[0]					,	kernel_size=3					,	padding=(1, 1)   )

       # time
       if time_embedding_type == "fourier":
          lowerCAmelCase_:       Any         =  GaussianFourierProjection(embedding_size=block_out_channels[0]					,	scale=16   )
          lowerCAmelCase_:       Tuple         =  2 * block_out_channels[0]
       elif time_embedding_type == "positional":
          lowerCAmelCase_:       int         =  Timesteps(block_out_channels[0]					,	lowerCamelCase__					,	lowerCamelCase__   )
          lowerCAmelCase_:       Any         =  block_out_channels[0]

       lowerCAmelCase_:       Dict         =  TimestepEmbedding(lowerCamelCase__					,	lowerCamelCase__   )

       # class embedding
       if class_embed_type is None and num_class_embeds is not None:
          lowerCAmelCase_:       Dict         =  nn.Embedding(lowerCamelCase__					,	lowerCamelCase__   )
       elif class_embed_type == "timestep":
          lowerCAmelCase_:       int         =  TimestepEmbedding(lowerCamelCase__					,	lowerCamelCase__   )
       elif class_embed_type == "identity":
          lowerCAmelCase_:       Optional[int]         =  nn.Identity(lowerCamelCase__					,	lowerCamelCase__   )
       else:
          lowerCAmelCase_:       List[Any]         =  None

       lowerCAmelCase_:       Optional[int]         =  nn.ModuleList([]   )
       lowerCAmelCase_:       Optional[Any]         =  None
       lowerCAmelCase_:       List[Any]         =  nn.ModuleList([]   )

       # down
       lowerCAmelCase_:       Tuple         =  block_out_channels[0]
       for i, down_block_type in enumerate(lowerCamelCase__   ):
          lowerCAmelCase_:       List[str]         =  output_channel
          lowerCAmelCase_:       Union[str, Any]         =  block_out_channels[i]
          lowerCAmelCase_:       Union[str, Any]         =  i == len(lowerCamelCase__   ) - 1

          lowerCAmelCase_:       Dict         =  get_down_block(
              lowerCamelCase__					,	num_layers=lowerCamelCase__					,	in_channels=lowerCamelCase__					,	out_channels=lowerCamelCase__					,	temb_channels=lowerCamelCase__					,	add_downsample=not is_final_block					,	resnet_eps=lowerCamelCase__					,	resnet_act_fn=lowerCamelCase__					,	resnet_groups=lowerCamelCase__					,	attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel					,	downsample_padding=lowerCamelCase__					,	resnet_time_scale_shift=lowerCamelCase__					,	downsample_type=lowerCamelCase__					,	)
          self.down_blocks.append(lowerCamelCase__   )

       # mid
       lowerCAmelCase_:       Optional[int]         =  UNetMidBlockaD(
           in_channels=block_out_channels[-1]					,	temb_channels=lowerCamelCase__					,	resnet_eps=lowerCamelCase__					,	resnet_act_fn=lowerCamelCase__					,	output_scale_factor=lowerCamelCase__					,	resnet_time_scale_shift=lowerCamelCase__					,	attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]					,	resnet_groups=lowerCamelCase__					,	add_attention=lowerCamelCase__					,	)

       # up
       lowerCAmelCase_:       str         =  list(reversed(lowerCamelCase__   )   )
       lowerCAmelCase_:       List[Any]         =  reversed_block_out_channels[0]
       for i, up_block_type in enumerate(lowerCamelCase__   ):
          lowerCAmelCase_:       Optional[Any]         =  output_channel
          lowerCAmelCase_:       Optional[int]         =  reversed_block_out_channels[i]
          lowerCAmelCase_:       Any         =  reversed_block_out_channels[min(i + 1					,	len(lowerCamelCase__   ) - 1   )]

          lowerCAmelCase_:       List[str]         =  i == len(lowerCamelCase__   ) - 1

          lowerCAmelCase_:       Optional[Any]         =  get_up_block(
              lowerCamelCase__					,	num_layers=layers_per_block + 1					,	in_channels=lowerCamelCase__					,	out_channels=lowerCamelCase__					,	prev_output_channel=lowerCamelCase__					,	temb_channels=lowerCamelCase__					,	add_upsample=not is_final_block					,	resnet_eps=lowerCamelCase__					,	resnet_act_fn=lowerCamelCase__					,	resnet_groups=lowerCamelCase__					,	attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel					,	resnet_time_scale_shift=lowerCamelCase__					,	upsample_type=lowerCamelCase__					,	)
          self.up_blocks.append(lowerCamelCase__   )
          lowerCAmelCase_:       int         =  output_channel

       # out
       lowerCAmelCase_:       List[Any]         =  norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4					,	32   )
       lowerCAmelCase_:       List[str]         =  nn.GroupNorm(num_channels=block_out_channels[0]					,	num_groups=lowerCamelCase__					,	eps=lowerCamelCase__   )
       lowerCAmelCase_:       int         =  nn.SiLU()
       lowerCAmelCase_:       Union[str, Any]         =  nn.Convad(block_out_channels[0]					,	lowerCamelCase__					,	kernel_size=3					,	padding=1   )




    def     _a						(						self					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__ = None					,	lowerCamelCase__ = True					,	):
       # 0. center input if necessary
       if self.config.center_input_sample:
          lowerCAmelCase_:       Optional[Any]         =  2 * sample - 1.0

       # 1. time
       lowerCAmelCase_:       List[str]         =  timestep
       if not torch.is_tensor(lowerCamelCase__   ):
          lowerCAmelCase_:       Optional[Any]         =  torch.tensor([timesteps]					,	dtype=torch.long					,	device=sample.device   )
       elif torch.is_tensor(lowerCamelCase__   ) and len(timesteps.shape   ) == 0:
          lowerCAmelCase_:       Union[str, Any]         =  timesteps[None].to(sample.device   )

       # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
       lowerCAmelCase_:       Optional[Any]         =  timesteps * torch.ones(sample.shape[0]					,	dtype=timesteps.dtype					,	device=timesteps.device   )

       lowerCAmelCase_:       Any         =  self.time_proj(lowerCamelCase__   )

       # timesteps does not contain any weights and will always return f32 tensors
       # but time_embedding might actually be running in fp16. so we need to cast here.
       # there might be better ways to encapsulate this.
       lowerCAmelCase_:       Any         =  t_emb.to(dtype=self.dtype   )
       lowerCAmelCase_:       Tuple         =  self.time_embedding(lowerCamelCase__   )

       if self.class_embedding is not None:
          if class_labels is None:
             raise ValueError("class_labels should be provided when doing class conditioning"   )

          if self.config.class_embed_type == "timestep":
             lowerCAmelCase_:       Union[str, Any]         =  self.time_proj(lowerCamelCase__   )

          lowerCAmelCase_:       Optional[Any]         =  self.class_embedding(lowerCamelCase__   ).to(dtype=self.dtype   )
          lowerCAmelCase_:       List[str]         =  emb + class_emb

       # 2. pre-process
       lowerCAmelCase_:       Any         =  sample
       lowerCAmelCase_:       List[str]         =  self.conv_in(lowerCamelCase__   )

       # 3. down
       lowerCAmelCase_:       int         =  (sample,)
       for downsample_block in self.down_blocks:
          if hasattr(lowerCamelCase__					,	"skip_conv"   ):
             lowerCAmelCase_     ,  lowerCAmelCase_     ,  lowerCAmelCase_:       int         =  downsample_block(
                 hidden_states=lowerCamelCase__					,	temb=lowerCamelCase__					,	skip_sample=lowerCamelCase__   )
          else:
             lowerCAmelCase_     ,  lowerCAmelCase_:       Optional[int]         =  downsample_block(hidden_states=lowerCamelCase__					,	temb=lowerCamelCase__   )

          down_block_res_samples += res_samples

       # 4. mid
       lowerCAmelCase_:       Dict         =  self.mid_block(lowerCamelCase__					,	lowerCamelCase__   )

       # 5. up
       lowerCAmelCase_:       Any         =  None
       for upsample_block in self.up_blocks:
          lowerCAmelCase_:       Tuple         =  down_block_res_samples[-len(upsample_block.resnets   ) :]
          lowerCAmelCase_:       List[Any]         =  down_block_res_samples[: -len(upsample_block.resnets   )]

          if hasattr(lowerCamelCase__					,	"skip_conv"   ):
             lowerCAmelCase_     ,  lowerCAmelCase_:       Dict         =  upsample_block(lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__   )
          else:
             lowerCAmelCase_:       List[str]         =  upsample_block(lowerCamelCase__					,	lowerCamelCase__					,	lowerCamelCase__   )

        # 6. post-process
       lowerCAmelCase_:       Optional[Any]         =  self.conv_norm_out(lowerCamelCase__   )
       lowerCAmelCase_:       Dict         =  self.conv_act(lowerCamelCase__   )
       lowerCAmelCase_:       Tuple         =  self.conv_out(lowerCamelCase__   )

       if skip_sample is not None:
          sample += skip_sample

       if self.config.time_embedding_type == "fourier":
          lowerCAmelCase_:       Optional[int]         =  timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:]   )))   )
          lowerCAmelCase_:       Optional[Any]         =  sample / timesteps

       if not return_dict:
          return (sample,)

       return UNetaDOutput(sample=lowerCamelCase__   )
from typing import TYPE_CHECKING

from ..utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available


a     :			Optional[Any]					    =						{
    """configuration_utils""": ["""GenerationConfig"""],
    """streamers""": ["""TextIteratorStreamer""", """TextStreamer"""],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a     :			Union[str, Any]					    =						[
     """Constraint""",
     """ConstraintListState""",
     """DisjunctiveConstraint""",
     """PhrasalConstraint""",
 ]
 a     :			Optional[int]					    =						[
     """BeamHypotheses""",
     """BeamScorer""",
     """BeamSearchScorer""",
     """ConstrainedBeamSearchScorer""",
 ]
 a     :			Any					    =						[
     """EpsilonLogitsWarper""",
     """EtaLogitsWarper""",
     """ForcedBOSTokenLogitsProcessor""",
     """ForcedEOSTokenLogitsProcessor""",
     """HammingDiversityLogitsProcessor""",
     """InfNanRemoveLogitsProcessor""",
     """LogitsProcessor""",
     """LogitsProcessorList""",
     """LogitsWarper""",
     """MinLengthLogitsProcessor""",
     """MinNewTokensLengthLogitsProcessor""",
     """NoBadWordsLogitsProcessor""",
     """NoRepeatNGramLogitsProcessor""",
     """PrefixConstrainedLogitsProcessor""",
     """RepetitionPenaltyLogitsProcessor""",
     """SequenceBiasLogitsProcessor""",
     """EncoderRepetitionPenaltyLogitsProcessor""",
     """TemperatureLogitsWarper""",
     """TopKLogitsWarper""",
     """TopPLogitsWarper""",
     """TypicalLogitsWarper""",
     """EncoderNoRepeatNGramLogitsProcessor""",
     """ExponentialDecayLengthPenalty""",
     """LogitNormalization""",
 ]
 a     :			int					    =						[
     """MaxNewTokensCriteria""",
     """MaxLengthCriteria""",
     """MaxTimeCriteria""",
     """StoppingCriteria""",
     """StoppingCriteriaList""",
     """validate_stopping_criteria""",
 ]
 a     :			Dict					    =						[
     """GenerationMixin""",
     """top_k_top_p_filtering""",
     """GreedySearchEncoderDecoderOutput""",
     """GreedySearchDecoderOnlyOutput""",
     """SampleEncoderDecoderOutput""",
     """SampleDecoderOnlyOutput""",
     """BeamSearchEncoderDecoderOutput""",
     """BeamSearchDecoderOnlyOutput""",
     """BeamSampleEncoderDecoderOutput""",
     """BeamSampleDecoderOnlyOutput""",
     """ContrastiveSearchEncoderDecoderOutput""",
     """ContrastiveSearchDecoderOnlyOutput""",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a     :			Optional[Any]					    =						[
     """TFForcedBOSTokenLogitsProcessor""",
     """TFForcedEOSTokenLogitsProcessor""",
     """TFLogitsProcessor""",
     """TFLogitsProcessorList""",
     """TFLogitsWarper""",
     """TFMinLengthLogitsProcessor""",
     """TFNoBadWordsLogitsProcessor""",
     """TFNoRepeatNGramLogitsProcessor""",
     """TFRepetitionPenaltyLogitsProcessor""",
     """TFTemperatureLogitsWarper""",
     """TFTopKLogitsWarper""",
     """TFTopPLogitsWarper""",
     """TFForceTokensLogitsProcessor""",
     """TFSuppressTokensAtBeginLogitsProcessor""",
     """TFSuppressTokensLogitsProcessor""",
 ]
 a     :			Any					    =						[
     """TFGenerationMixin""",
     """tf_top_k_top_p_filtering""",
     """TFGreedySearchDecoderOnlyOutput""",
     """TFGreedySearchEncoderDecoderOutput""",
     """TFSampleEncoderDecoderOutput""",
     """TFSampleDecoderOnlyOutput""",
     """TFBeamSearchEncoderDecoderOutput""",
     """TFBeamSearchDecoderOnlyOutput""",
     """TFBeamSampleEncoderDecoderOutput""",
     """TFBeamSampleDecoderOnlyOutput""",
     """TFContrastiveSearchEncoderDecoderOutput""",
     """TFContrastiveSearchDecoderOnlyOutput""",
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a     :			Union[str, Any]					    =						[
     """FlaxForcedBOSTokenLogitsProcessor""",
     """FlaxForcedEOSTokenLogitsProcessor""",
     """FlaxLogitsProcessor""",
     """FlaxLogitsProcessorList""",
     """FlaxLogitsWarper""",
     """FlaxMinLengthLogitsProcessor""",
     """FlaxTemperatureLogitsWarper""",
     """FlaxTopKLogitsWarper""",
     """FlaxTopPLogitsWarper""",
 ]
 a     :			Dict					    =						[
     """FlaxGenerationMixin""",
     """FlaxGreedySearchOutput""",
     """FlaxSampleOutput""",
     """FlaxBeamSearchOutput""",
 ]

if TYPE_CHECKING:
 from .configuration_utils import GenerationConfig
 from .streamers import TextIteratorStreamer, TextStreamer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .beam_constraints import Constraint, ConstraintListState, DisjunctiveConstraint, PhrasalConstraint
  from .beam_search import BeamHypotheses, BeamScorer, BeamSearchScorer, ConstrainedBeamSearchScorer
  from .logits_process import (
      EncoderNoRepeatNGramLogitsProcessor,
      EncoderRepetitionPenaltyLogitsProcessor,
      EpsilonLogitsWarper,
      EtaLogitsWarper,
      ExponentialDecayLengthPenalty,
      ForcedBOSTokenLogitsProcessor,
      ForcedEOSTokenLogitsProcessor,
      HammingDiversityLogitsProcessor,
      InfNanRemoveLogitsProcessor,
      LogitNormalization,
      LogitsProcessor,
      LogitsProcessorList,
      LogitsWarper,
      MinLengthLogitsProcessor,
      MinNewTokensLengthLogitsProcessor,
      NoBadWordsLogitsProcessor,
      NoRepeatNGramLogitsProcessor,
      PrefixConstrainedLogitsProcessor,
      RepetitionPenaltyLogitsProcessor,
      SequenceBiasLogitsProcessor,
      TemperatureLogitsWarper,
      TopKLogitsWarper,
      TopPLogitsWarper,
      TypicalLogitsWarper,
  )
  from .stopping_criteria import (
      MaxLengthCriteria,
      MaxNewTokensCriteria,
      MaxTimeCriteria,
      StoppingCriteria,
      StoppingCriteriaList,
      validate_stopping_criteria,
  )
  from .utils import (
      BeamSampleDecoderOnlyOutput,
      BeamSampleEncoderDecoderOutput,
      BeamSearchDecoderOnlyOutput,
      BeamSearchEncoderDecoderOutput,
      ContrastiveSearchDecoderOnlyOutput,
      ContrastiveSearchEncoderDecoderOutput,
      GenerationMixin,
      GreedySearchDecoderOnlyOutput,
      GreedySearchEncoderDecoderOutput,
      SampleDecoderOnlyOutput,
      SampleEncoderDecoderOutput,
      top_k_top_p_filtering,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tf_logits_process import (
      TFForcedBOSTokenLogitsProcessor,
      TFForcedEOSTokenLogitsProcessor,
      TFForceTokensLogitsProcessor,
      TFLogitsProcessor,
      TFLogitsProcessorList,
      TFLogitsWarper,
      TFMinLengthLogitsProcessor,
      TFNoBadWordsLogitsProcessor,
      TFNoRepeatNGramLogitsProcessor,
      TFRepetitionPenaltyLogitsProcessor,
      TFSuppressTokensAtBeginLogitsProcessor,
      TFSuppressTokensLogitsProcessor,
      TFTemperatureLogitsWarper,
      TFTopKLogitsWarper,
      TFTopPLogitsWarper,
  )
  from .tf_utils import (
      TFBeamSampleDecoderOnlyOutput,
      TFBeamSampleEncoderDecoderOutput,
      TFBeamSearchDecoderOnlyOutput,
      TFBeamSearchEncoderDecoderOutput,
      TFContrastiveSearchDecoderOnlyOutput,
      TFContrastiveSearchEncoderDecoderOutput,
      TFGenerationMixin,
      TFGreedySearchDecoderOnlyOutput,
      TFGreedySearchEncoderDecoderOutput,
      TFSampleDecoderOnlyOutput,
      TFSampleEncoderDecoderOutput,
      tf_top_k_top_p_filtering,
  )

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .flax_logits_process import (
      FlaxForcedBOSTokenLogitsProcessor,
      FlaxForcedEOSTokenLogitsProcessor,
      FlaxLogitsProcessor,
      FlaxLogitsProcessorList,
      FlaxLogitsWarper,
      FlaxMinLengthLogitsProcessor,
      FlaxTemperatureLogitsWarper,
      FlaxTopKLogitsWarper,
      FlaxTopPLogitsWarper,
  )
  from .flax_utils import FlaxBeamSearchOutput, FlaxGenerationMixin, FlaxGreedySearchOutput, FlaxSampleOutput
else:
 import sys

 a     :			str					    =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)