'''simple docstring'''

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					(    UpperCAmelCase__      ):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE					:	torch.FloatTensor







class     _lowercase					(    UpperCAmelCase__  ,							UpperCAmelCase__      ):


 '''simple docstring'''






 @register_to_config
 def __init__(				self       :     str				,	SCREAMING_SNAKE_CASE__       :     Optional[Union[int, Tuple[int, int]]] = None				,	SCREAMING_SNAKE_CASE__       :     int = 3				,	SCREAMING_SNAKE_CASE__       :     int = 3				,	SCREAMING_SNAKE_CASE__       :     bool = False				,	SCREAMING_SNAKE_CASE__       :     str = "positional"				,	SCREAMING_SNAKE_CASE__       :     int = 0				,	SCREAMING_SNAKE_CASE__       :     bool = True				,	SCREAMING_SNAKE_CASE__       :     Tuple[str] = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")				,	SCREAMING_SNAKE_CASE__       :     Tuple[str] = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")				,	SCREAMING_SNAKE_CASE__       :     Tuple[int] = (2_24, 4_48, 6_72, 8_96)				,	SCREAMING_SNAKE_CASE__       :     int = 2				,	SCREAMING_SNAKE_CASE__       :     float = 1				,	SCREAMING_SNAKE_CASE__       :     int = 1				,	SCREAMING_SNAKE_CASE__       :     str = "conv"				,	SCREAMING_SNAKE_CASE__       :     str = "conv"				,	SCREAMING_SNAKE_CASE__       :     str = "silu"				,	SCREAMING_SNAKE_CASE__       :     Optional[int] = 8				,	SCREAMING_SNAKE_CASE__       :     int = 32				,	SCREAMING_SNAKE_CASE__       :     float = 1e-5				,	SCREAMING_SNAKE_CASE__       :     str = "default"				,	SCREAMING_SNAKE_CASE__       :     bool = True				,	SCREAMING_SNAKE_CASE__       :     Optional[str] = None				,	SCREAMING_SNAKE_CASE__       :     Optional[int] = None				,	)  ->   Any:
  super().__init__()

  __lowerCAmelCase    							=						sample_size
  __lowerCAmelCase    							=						block_out_channels[0] * 4

  # Check inputs
  if len(SCREAMING_SNAKE_CASE__							) != len(SCREAMING_SNAKE_CASE__							):
   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(SCREAMING_SNAKE_CASE__							) != len(SCREAMING_SNAKE_CASE__							):
   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    							=						nn.Convad(SCREAMING_SNAKE_CASE__				,	block_out_channels[0]				,	kernel_size=3				,	padding=(1, 1)							)

  # time
  if time_embedding_type == "fourier":
   __lowerCAmelCase    							=						GaussianFourierProjection(embedding_size=block_out_channels[0]				,	scale=16							)
   __lowerCAmelCase    							=						2 * block_out_channels[0]
  elif time_embedding_type == "positional":
   __lowerCAmelCase    							=						Timesteps(block_out_channels[0]				,	SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)
   __lowerCAmelCase    							=						block_out_channels[0]

  __lowerCAmelCase    							=						TimestepEmbedding(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)

  # class embedding
  if class_embed_type is None and num_class_embeds is not None:
   __lowerCAmelCase    							=						nn.Embedding(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)
  elif class_embed_type == "timestep":
   __lowerCAmelCase    							=						TimestepEmbedding(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)
  elif class_embed_type == "identity":
   __lowerCAmelCase    							=						nn.Identity(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)
  else:
   __lowerCAmelCase    							=						None

  __lowerCAmelCase    							=						nn.ModuleList([]							)
  __lowerCAmelCase    							=						None
  __lowerCAmelCase    							=						nn.ModuleList([]							)

  # down
  __lowerCAmelCase    							=						block_out_channels[0]
  for i, down_block_type in enumerate(SCREAMING_SNAKE_CASE__							):
   __lowerCAmelCase    							=						output_channel
   __lowerCAmelCase    							=						block_out_channels[i]
   __lowerCAmelCase    							=						i == len(SCREAMING_SNAKE_CASE__							) - 1

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

  # mid
  __lowerCAmelCase    							=						UNetMidBlockaD(
      in_channels=block_out_channels[-1]				,	temb_channels=SCREAMING_SNAKE_CASE__				,	resnet_eps=SCREAMING_SNAKE_CASE__				,	resnet_act_fn=SCREAMING_SNAKE_CASE__				,	output_scale_factor=SCREAMING_SNAKE_CASE__				,	resnet_time_scale_shift=SCREAMING_SNAKE_CASE__				,	attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]				,	resnet_groups=SCREAMING_SNAKE_CASE__				,	add_attention=SCREAMING_SNAKE_CASE__				,	)

  # up
  __lowerCAmelCase    							=						list(reversed(SCREAMING_SNAKE_CASE__							)							)
  __lowerCAmelCase    							=						reversed_block_out_channels[0]
  for i, up_block_type in enumerate(SCREAMING_SNAKE_CASE__							):
   __lowerCAmelCase    							=						output_channel
   __lowerCAmelCase    							=						reversed_block_out_channels[i]
   __lowerCAmelCase    							=						reversed_block_out_channels[min(i + 1				,	len(SCREAMING_SNAKE_CASE__							) - 1							)]

   __lowerCAmelCase    							=						i == len(SCREAMING_SNAKE_CASE__							) - 1

   __lowerCAmelCase    							=						get_up_block(
       SCREAMING_SNAKE_CASE__				,	num_layers=layers_per_block + 1				,	in_channels=SCREAMING_SNAKE_CASE__				,	out_channels=SCREAMING_SNAKE_CASE__				,	prev_output_channel=SCREAMING_SNAKE_CASE__				,	temb_channels=SCREAMING_SNAKE_CASE__				,	add_upsample=not is_final_block				,	resnet_eps=SCREAMING_SNAKE_CASE__				,	resnet_act_fn=SCREAMING_SNAKE_CASE__				,	resnet_groups=SCREAMING_SNAKE_CASE__				,	attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel				,	resnet_time_scale_shift=SCREAMING_SNAKE_CASE__				,	upsample_type=SCREAMING_SNAKE_CASE__				,	)
   self.up_blocks.append(SCREAMING_SNAKE_CASE__							)
   __lowerCAmelCase    							=						output_channel

  # out
  __lowerCAmelCase    							=						norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4				,	32							)
  __lowerCAmelCase    							=						nn.GroupNorm(num_channels=block_out_channels[0]				,	num_groups=SCREAMING_SNAKE_CASE__				,	eps=SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						nn.SiLU()
  __lowerCAmelCase    							=						nn.Convad(block_out_channels[0]				,	SCREAMING_SNAKE_CASE__				,	kernel_size=3				,	padding=1							)





 def a (				self       :     List[str]				,	SCREAMING_SNAKE_CASE__       :     torch.FloatTensor				,	SCREAMING_SNAKE_CASE__       :     Union[torch.Tensor, float, int]				,	SCREAMING_SNAKE_CASE__       :     Optional[torch.Tensor] = None				,	SCREAMING_SNAKE_CASE__       :     bool = True				,	)  ->   Union[UNetaDOutput, Tuple]:
  # 0. center input if necessary
  if self.config.center_input_sample:
   __lowerCAmelCase    							=						2 * sample - 1.0

  # 1. time
  __lowerCAmelCase    							=						timestep
  if not torch.is_tensor(SCREAMING_SNAKE_CASE__							):
   __lowerCAmelCase    							=						torch.tensor([timesteps]				,	dtype=torch.long				,	device=sample.device							)
  elif torch.is_tensor(SCREAMING_SNAKE_CASE__							) and len(timesteps.shape							) == 0:
   __lowerCAmelCase    							=						timesteps[None].to(sample.device							)

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

  __lowerCAmelCase    							=						self.time_proj(SCREAMING_SNAKE_CASE__							)

  # 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    							=						t_emb.to(dtype=self.dtype							)
  __lowerCAmelCase    							=						self.time_embedding(SCREAMING_SNAKE_CASE__							)

  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    							=						self.time_proj(SCREAMING_SNAKE_CASE__							)

   __lowerCAmelCase    							=						self.class_embedding(SCREAMING_SNAKE_CASE__							).to(dtype=self.dtype							)
   __lowerCAmelCase    							=						emb + class_emb

  # 2. pre-process
  __lowerCAmelCase    							=						sample
  __lowerCAmelCase    							=						self.conv_in(SCREAMING_SNAKE_CASE__							)

  # 3. down
  __lowerCAmelCase    							=						(sample,)
  for downsample_block in self.down_blocks:
   if hasattr(SCREAMING_SNAKE_CASE__				,	"""skip_conv"""							):
    __lowerCAmelCase							,	__lowerCAmelCase							,	__lowerCAmelCase    							=						downsample_block(
        hidden_states=SCREAMING_SNAKE_CASE__				,	temb=SCREAMING_SNAKE_CASE__				,	skip_sample=SCREAMING_SNAKE_CASE__							)
   else:
    __lowerCAmelCase							,	__lowerCAmelCase    							=						downsample_block(hidden_states=SCREAMING_SNAKE_CASE__				,	temb=SCREAMING_SNAKE_CASE__							)

   down_block_res_samples += res_samples

  # 4. mid
  __lowerCAmelCase    							=						self.mid_block(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)

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

   if hasattr(SCREAMING_SNAKE_CASE__				,	"""skip_conv"""							):
    __lowerCAmelCase							,	__lowerCAmelCase    							=						upsample_block(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)
   else:
    __lowerCAmelCase    							=						upsample_block(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)

        # 6. post-process
  __lowerCAmelCase    							=						self.conv_norm_out(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						self.conv_act(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						self.conv_out(SCREAMING_SNAKE_CASE__							)

  if skip_sample is not None:
   sample += skip_sample

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

  if not return_dict:
   return (sample,)

  return UNetaDOutput(sample=SCREAMING_SNAKE_CASE__							)







'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


_A      :				Optional[int] 				=    {
    '''configuration_speech_to_text_2''': ['''SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2Text2Config'''],
    '''processing_speech_to_text_2''': ['''Speech2Text2Processor'''],
    '''tokenization_speech_to_text_2''': ['''Speech2Text2Tokenizer'''],
}


try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _A      :				Any 				=    [
     '''SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''Speech2Text2ForCausalLM''',
     '''Speech2Text2PreTrainedModel''',
 ]


if TYPE_CHECKING:
 from .configuration_speech_to_text_a import SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextaConfig
 from .processing_speech_to_text_a import SpeechaTextaProcessor
 from .tokenization_speech_to_text_a import SpeechaTextaTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_speech_to_text_a import (
      SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST,
      SpeechaTextaForCausalLM,
      SpeechaTextaPreTrainedModel,
  )

else:
 import sys

 _A      :				Tuple 				=    _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






