

'''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      a__						(							lowerCamelCase_						):
   _SCREAMING_SNAKE_CASE					:  torch.FloatTensor




class      a__						(							lowerCamelCase_						,   lowerCamelCase_						):


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





      """simple docstring"""





      super().__init__()

      _lowercase    :   int      =     sample_size
      _lowercase    :   Dict      =     block_out_channels[0] * 4

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

      # time
      if time_embedding_type == "fourier":
         _lowercase    :   int      =     GaussianFourierProjection(embedding_size=block_out_channels[0]    ,       scale=16						)
         _lowercase    :   List[Any]      =     2 * block_out_channels[0]
      elif time_embedding_type == "positional":
         _lowercase    :   Optional[Any]      =     Timesteps(block_out_channels[0]    ,       _UpperCamelCase    ,       _UpperCamelCase						)
         _lowercase    :   Optional[Any]      =     block_out_channels[0]

      _lowercase    :   Tuple      =     TimestepEmbedding(_UpperCamelCase    ,       _UpperCamelCase						)

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

      _lowercase    :   str      =     nn.ModuleList([]						)
      _lowercase    :   List[str]      =     None
      _lowercase    :   List[Any]      =     nn.ModuleList([]						)

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

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

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

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

         _lowercase    :   Dict      =     i == len(_UpperCamelCase						) - 1

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

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






   def 		_lowerCamelCase     (				self    ,       _UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase = None    ,       _UpperCamelCase = True    ,       ):





      """simple docstring"""





      if self.config.center_input_sample:
         _lowercase    :   int      =     2 * sample - 1.0

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

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

      _lowercase    :   Tuple      =     self.time_proj(_UpperCamelCase						)

      # 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.
      _lowercase    :   Union[str, Any]      =     t_emb.to(dtype=self.dtype						)
      _lowercase    :   Tuple      =     self.time_embedding(_UpperCamelCase						)

      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":
            _lowercase    :   Tuple      =     self.time_proj(_UpperCamelCase						)

         _lowercase    :   Any      =     self.class_embedding(_UpperCamelCase						).to(dtype=self.dtype						)
         _lowercase    :   Any      =     emb + class_emb

      # 2. pre-process
      _lowercase    :   List[str]      =     sample
      _lowercase    :   str      =     self.conv_in(_UpperCamelCase						)

      # 3. down
      _lowercase    :   List[Any]      =     (sample,)
      for downsample_block in self.down_blocks:
         if hasattr(_UpperCamelCase    ,       "skip_conv"						):
            _lowercase  ,   _lowercase  ,   _lowercase    :   Optional[Any]      =     downsample_block(
                hidden_states=_UpperCamelCase    ,       temb=_UpperCamelCase    ,       skip_sample=_UpperCamelCase						)
         else:
            _lowercase  ,   _lowercase    :   Union[str, Any]      =     downsample_block(hidden_states=_UpperCamelCase    ,       temb=_UpperCamelCase						)

         down_block_res_samples += res_samples

      # 4. mid
      _lowercase    :   Tuple      =     self.mid_block(_UpperCamelCase    ,       _UpperCamelCase						)

      # 5. up
      _lowercase    :   Optional[int]      =     None
      for upsample_block in self.up_blocks:
         _lowercase    :   Union[str, Any]      =     down_block_res_samples[-len(upsample_block.resnets						) :]
         _lowercase    :   Any      =     down_block_res_samples[: -len(upsample_block.resnets						)]

         if hasattr(_UpperCamelCase    ,       "skip_conv"						):
            _lowercase  ,   _lowercase    :   int      =     upsample_block(_UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase						)
         else:
            _lowercase    :   str      =     upsample_block(_UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase						)

        # 6. post-process
      _lowercase    :   Optional[Any]      =     self.conv_norm_out(_UpperCamelCase						)
      _lowercase    :   Optional[int]      =     self.conv_act(_UpperCamelCase						)
      _lowercase    :   str      =     self.conv_out(_UpperCamelCase						)

      if skip_sample is not None:
         sample += skip_sample

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

      if not return_dict:
         return (sample,)

      return UNetaDOutput(sample=_UpperCamelCase						)









'''simple docstring'''
import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


_snake_case     						=						logging.get_logger(__name__)

if is_tf_available():
    import tensorflow as tf


@dataclass
class      a__						(							lowerCamelCase_						):
   _SCREAMING_SNAKE_CASE					:  int			 =							'tf'
   _SCREAMING_SNAKE_CASE					:  Optional[str]			 =							field(
       default=lowerCamelCase_						,   metadata={'help': 'Name of TPU'}						,   )

   _SCREAMING_SNAKE_CASE					:  Optional[str]			 =							field(
       default=lowerCamelCase_						,   metadata={'help': 'Zone of TPU'}						,   )

   _SCREAMING_SNAKE_CASE					:  Optional[str]			 =							field(
       default=lowerCamelCase_						,   metadata={'help': 'Name of Cloud TPU-enabled project'}						,   )

   _SCREAMING_SNAKE_CASE					:  float			 =							field(
       default=1.0						,   metadata={'help': 'Power for the Polynomial decay LR scheduler.'}						,   )

   _SCREAMING_SNAKE_CASE					:  bool			 =							field(default=lowerCamelCase_						,   metadata={'help': 'Whether to activate the XLA compilation or not'}						)


   @cached_property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      logger.info("Tensorflow: setting up strategy"						)

      _lowercase    :   Optional[int]      =     tf.config.list_physical_devices("GPU"						)

      # Set to float16 at first
      if self.fpaa:
         tf.keras.mixed_precision.set_global_policy("mixed_float16"						)

      if self.no_cuda:
         _lowercase    :   Optional[int]      =     tf.distribute.OneDeviceStrategy(device="/cpu:0"						)
      else:
         try:
            if self.tpu_name:
               _lowercase    :   int      =     tf.distribute.cluster_resolver.TPUClusterResolver(
                   self.tpu_name    ,       zone=self.tpu_zone    ,       project=self.gcp_project						)
            else:
               _lowercase    :   Any      =     tf.distribute.cluster_resolver.TPUClusterResolver()
         except ValueError:
            if self.tpu_name:
               raise RuntimeError(f'''Couldn\'t connect to TPU {self.tpu_name}!'''						)
            else:
               _lowercase    :   Tuple      =     None

         if tpu:
            # Set to bfloat16 in case of TPU
            if self.fpaa:
               tf.keras.mixed_precision.set_global_policy("mixed_bfloat16"						)

            tf.config.experimental_connect_to_cluster(_UpperCamelCase						)
            tf.tpu.experimental.initialize_tpu_system(_UpperCamelCase						)

            _lowercase    :   List[str]      =     tf.distribute.TPUStrategy(_UpperCamelCase						)

         elif len(_UpperCamelCase						) == 0:
            _lowercase    :   Optional[Any]      =     tf.distribute.OneDeviceStrategy(device="/cpu:0"						)
         elif len(_UpperCamelCase						) == 1:
            _lowercase    :   Optional[int]      =     tf.distribute.OneDeviceStrategy(device="/gpu:0"						)
         elif len(_UpperCamelCase						) > 1:
            # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
            _lowercase    :   Any      =     tf.distribute.MirroredStrategy()
         else:
            raise ValueError("Cannot find the proper strategy, please check your environment properties."						)

      return strategy


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      return self._setup_strategy


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      return self._setup_strategy.num_replicas_in_sync


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      return False  # TF Logging is handled by Keras not the Trainer


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      if self.per_gpu_train_batch_size:
         logger.warning(
             "Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future "
             "version. Using `--per_device_train_batch_size` is preferred."						)
      _lowercase    :   str      =     self.per_gpu_train_batch_size or self.per_device_train_batch_size
      return per_device_batch_size * self.n_replicas


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      if self.per_gpu_eval_batch_size:
         logger.warning(
             "Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future "
             "version. Using `--per_device_eval_batch_size` is preferred."						)
      _lowercase    :   List[Any]      =     self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
      return per_device_batch_size * self.n_replicas






   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      warnings.warn(
          "The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead."    ,       _UpperCamelCase    ,       )
      return self._setup_strategy.num_replicas_in_sync






