





'''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							):
     __A  				=    42







class    a_     (       _lowerCAmelCase			,		_lowerCAmelCase							):





     @register_to_config
     def __init__(				self	:      Optional[Any]			,   lowercase	:      Optional[Any] = None			,   lowercase	:      List[Any] = 3			,   lowercase	:      Dict = 3			,   lowercase	:      Any = False			,   lowercase	:      List[Any] = "positional"			,   lowercase	:      str = 0			,   lowercase	:      List[Any] = True			,   lowercase	:      Any = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")			,   lowercase	:      str = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")			,   lowercase	:      str = (224, 448, 672, 896)			,   lowercase	:      List[Any] = 2			,   lowercase	:      List[str] = 1			,   lowercase	:      Union[str, Any] = 1			,   lowercase	:      Tuple = "conv"			,   lowercase	:      Dict = "conv"			,   lowercase	:      Any = "silu"			,   lowercase	:      List[Any] = 8			,   lowercase	:      Dict = 32			,   lowercase	:      Tuple = 1e-5			,   lowercase	:      List[str] = "default"			,   lowercase	:      Union[str, Any] = True			,   lowercase	:      Optional[Any] = None			,   lowercase	:      int = None			,   ):





        """simple docstring"""




        super().__init__()

        lowercase_    :int					    =				sample_size
        lowercase_    :Tuple					    =				block_out_channels[0] * 4

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

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

        lowercase_    :List[str]					    =				TimestepEmbedding(lowercase			,   lowercase		)

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

        lowercase_    :Dict					    =				nn.ModuleList([]		)
        lowercase_    :str					    =				None
        lowercase_    :str					    =				nn.ModuleList([]		)

        # down
        lowercase_    :List[Any]					    =				block_out_channels[0]
        for i, down_block_type in enumerate(lowercase		):
           lowercase_    :Any					    =				output_channel
           lowercase_    :Dict					    =				block_out_channels[i]
           lowercase_    :Any					    =				i == len(lowercase		) - 1

           lowercase_    :Union[str, Any]					    =				get_down_block(
               lowercase			,   num_layers=lowercase			,   in_channels=lowercase			,   out_channels=lowercase			,   temb_channels=lowercase			,   add_downsample=not is_final_block			,   resnet_eps=lowercase			,   resnet_act_fn=lowercase			,   resnet_groups=lowercase			,   attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel			,   downsample_padding=lowercase			,   resnet_time_scale_shift=lowercase			,   downsample_type=lowercase			,   )
           self.down_blocks.append(lowercase		)

        # mid
        lowercase_    :List[str]					    =				UNetMidBlockaD(
            in_channels=block_out_channels[-1]			,   temb_channels=lowercase			,   resnet_eps=lowercase			,   resnet_act_fn=lowercase			,   output_scale_factor=lowercase			,   resnet_time_scale_shift=lowercase			,   attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]			,   resnet_groups=lowercase			,   add_attention=lowercase			,   )

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

           lowercase_    :str					    =				i == len(lowercase		) - 1

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

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







     def 							lowercase__   (				self	:      Any			,   lowercase	:      Dict			,   lowercase	:      Tuple			,   lowercase	:      List[Any] = None			,   lowercase	:      Union[str, Any] = True			,   ):





        """simple docstring"""




        if self.config.center_input_sample:
           lowercase_    :List[str]					    =				2 * sample - 1.0

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

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

        lowercase_    :Union[str, Any]					    =				self.time_proj(lowercase		)

        # 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_    :List[Any]					    =				t_emb.to(dtype=self.dtype		)
        lowercase_    :Optional[Any]					    =				self.time_embedding(lowercase		)

        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_    :List[str]					    =				self.time_proj(lowercase		)

           lowercase_    :Optional[Any]					    =				self.class_embedding(lowercase		).to(dtype=self.dtype		)
           lowercase_    :Optional[Any]					    =				emb + class_emb

        # 2. pre-process
        lowercase_    :Optional[int]					    =				sample
        lowercase_    :Tuple					    =				self.conv_in(lowercase		)

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

           down_block_res_samples += res_samples

        # 4. mid
        lowercase_    :List[Any]					    =				self.mid_block(lowercase			,   lowercase		)

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

           if hasattr(lowercase			,   "skip_conv"		):
              lowercase_    :Any					    =				upsample_block(lowercase			,   lowercase			,   lowercase			,   lowercase		)
           else:
              lowercase_    :List[Any]					    =				upsample_block(lowercase			,   lowercase			,   lowercase		)

        # 6. post-process
        lowercase_    :Dict					    =				self.conv_norm_out(lowercase		)
        lowercase_    :Any					    =				self.conv_act(lowercase		)
        lowercase_    :Optional[Any]					    =				self.conv_out(lowercase		)

        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_    :Union[str, Any]					    =				sample / timesteps

        if not return_dict:
           return (sample,)

        return UNetaDOutput(sample=lowercase		)









'''simple docstring'''


import multiprocessing
import os
from typing import BinaryIO, Optional, Union

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.csv.csv import Csv
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader
class    a_     (       _lowerCAmelCase							):





     def __init__(				self	:      Dict			,   lowercase	:      NestedDataStructureLike[PathLike]			,   lowercase	:      Optional[NamedSplit] = None			,   lowercase	:      Optional[Features] = None			,   lowercase	:      str = None			,   lowercase	:      bool = False			,   lowercase	:      bool = False			,   lowercase	:      Optional[int] = None			,   **lowercase	:      Any			,   ):





        """simple docstring"""




        super().__init__(
            lowercase			,   split=lowercase			,   features=lowercase			,   cache_dir=lowercase			,   keep_in_memory=lowercase			,   streaming=lowercase			,   num_proc=lowercase			,   **lowercase			,   )
        lowercase_    :Any					    =				path_or_paths if isinstance(lowercase			,   lowercase		) else {self.split: path_or_paths}
        lowercase_    :Any					    =				Csv(
            cache_dir=lowercase			,   data_files=lowercase			,   features=lowercase			,   **lowercase			,   )







     def 							lowercase__   (				self	:      Any		):





        """simple docstring"""




        if self.streaming:
           lowercase_    :Union[str, Any]					    =				self.builder.as_streaming_dataset(split=self.split		)
        # Build regular (map-style) dataset
        else:
           lowercase_    :Dict					    =				None
           lowercase_    :List[str]					    =				None
           lowercase_    :List[Any]					    =				None
           lowercase_    :Union[str, Any]					    =				None

           self.builder.download_and_prepare(
               download_config=lowercase			,   download_mode=lowercase			,   verification_mode=lowercase			,   base_path=lowercase			,   num_proc=self.num_proc			,   )
           lowercase_    :int					    =				self.builder.as_dataset(
               split=self.split			,   verification_mode=lowercase			,   in_memory=self.keep_in_memory		)
        return dataset







class    a_     :





     def __init__(				self	:      Dict			,   lowercase	:      Dataset			,   lowercase	:      Union[PathLike, BinaryIO]			,   lowercase	:      Optional[int] = None			,   lowercase	:      Optional[int] = None			,   **lowercase	:      Any			,   ):





        """simple docstring"""




        if num_proc is not None and num_proc <= 0:
           raise ValueError(F'num_proc {num_proc} must be an integer > 0.'		)

        lowercase_    :str					    =				dataset
        lowercase_    :List[Any]					    =				path_or_buf
        lowercase_    :Any					    =				batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
        lowercase_    :int					    =				num_proc
        lowercase_    :Optional[Any]					    =				"utf-8"
        lowercase_    :Dict					    =				to_csv_kwargs





     def 							lowercase__   (				self	:      Union[str, Any]		):





        """simple docstring"""




        lowercase_    :int					    =				self.to_csv_kwargs.pop("path_or_buf"			,   lowercase		)
        lowercase_    :List[str]					    =				self.to_csv_kwargs.pop("header"			,   lowercase		)
        lowercase_    :Optional[Any]					    =				self.to_csv_kwargs.pop("index"			,   lowercase		)

        if isinstance(self.path_or_buf			,   (str, bytes, os.PathLike)		):
           with open(self.path_or_buf			,   "wb+"		) as buffer:
              lowercase_    :List[Any]					    =				self._write(file_obj=lowercase			,   header=lowercase			,   index=lowercase			,   **self.to_csv_kwargs		)
        else:
           lowercase_    :Any					    =				self._write(file_obj=self.path_or_buf			,   header=lowercase			,   index=lowercase			,   **self.to_csv_kwargs		)
        return written





     def 							lowercase__   (				self	:      List[Any]			,   lowercase	:      Union[str, Any]		):





        """simple docstring"""




        lowercase_    ,							lowercase_    ,							lowercase_    ,							lowercase_    :Optional[Any]					    =				args

        lowercase_    :Dict					    =				query_table(
            table=self.dataset.data			,   key=slice(lowercase			,   offset + self.batch_size		)			,   indices=self.dataset._indices			,   )
        lowercase_    :List[str]					    =				batch.to_pandas().to_csv(
            path_or_buf=lowercase			,   header=header if (offset == 0) else False			,   index=lowercase			,   **lowercase		)
        return csv_str.encode(self.encoding		)







     def 							lowercase__   (				self	:      Dict			,   lowercase	:      BinaryIO			,   lowercase	:      Dict			,   lowercase	:      int			,   **lowercase	:      Any		):





        """simple docstring"""




        lowercase_    :Dict					    =				0

        if self.num_proc is None or self.num_proc == 1:
           for offset in logging.tqdm(
               range(0			,   len(self.dataset		)			,   self.batch_size		)			,   unit="ba"			,   disable=not logging.is_progress_bar_enabled()			,   desc="Creating CSV from Arrow format"			,   ):
              lowercase_    :Optional[Any]					    =				self._batch_csv((offset, header, index, to_csv_kwargs)		)
              written += file_obj.write(lowercase		)

        else:
           lowercase_    ,							lowercase_    :Optional[int]					    =				len(self.dataset		), self.batch_size
           with multiprocessing.Pool(self.num_proc		) as pool:
              for csv_str in logging.tqdm(
                  pool.imap(
                      self._batch_csv			,   [(offset, header, index, to_csv_kwargs) for offset in range(0			,   lowercase			,   lowercase		)]			,   )			,   total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size			,   unit="ba"			,   disable=not logging.is_progress_bar_enabled()			,   desc="Creating CSV from Arrow format"			,   ):
                 written += file_obj.write(lowercase		)

        return written


