


'''simple docstring'''







import itertools
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast
from datasets.utils.py_utils import Literal


lowerCAmelCase     :  Any											=    datasets.utils.logging.get_logger(__name__)

lowerCAmelCase     :  str											=    ["""names""", """prefix"""]
lowerCAmelCase     :  str											=    ["""warn_bad_lines""", """error_bad_lines""", """mangle_dupe_cols"""]
lowerCAmelCase     :  Any											=    ["""encoding_errors""", """on_bad_lines"""]
lowerCAmelCase     :  Any											=    ["""date_format"""]
@dataclass
class 						UpperCamelCase__       (			datasets.BuilderConfig						):



     """simple docstring"""






     __magic_name__           =     ","
     __magic_name__           =     None
     __magic_name__           =     "infer"
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     True
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     False
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     True
     __magic_name__           =     True
     __magic_name__           =     False
     __magic_name__           =     True
     __magic_name__           =     None
     __magic_name__           =     "."
     __magic_name__           =     None
     __magic_name__           =     '"'
     __magic_name__           =     0
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     None
     __magic_name__           =     True
     __magic_name__           =     True
     __magic_name__           =     0
     __magic_name__           =     True
     __magic_name__           =     False
     __magic_name__           =     None
     __magic_name__           =     1_0_0_0_0
     __magic_name__           =     None
     __magic_name__           =     "strict"
     __magic_name__           =     "error"
     __magic_name__           =     None







     def a						(			self ):

            '''simple docstring'''

            if self.delimiter is not None:
                   _lowerCAmelCase     :				List[str]       =							self.delimiter
            if self.column_names is not None:
                   _lowerCAmelCase     :				Any       =							self.column_names







     @property
     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							{
                'sep': self.sep,
                'header': self.header,
                'names': self.names,
                'index_col': self.index_col,
                'usecols': self.usecols,
                'prefix': self.prefix,
                'mangle_dupe_cols': self.mangle_dupe_cols,
                'engine': self.engine,
                'converters': self.converters,
                'true_values': self.true_values,
                'false_values': self.false_values,
                'skipinitialspace': self.skipinitialspace,
                'skiprows': self.skiprows,
                'nrows': self.nrows,
                'na_values': self.na_values,
                'keep_default_na': self.keep_default_na,
                'na_filter': self.na_filter,
                'verbose': self.verbose,
                'skip_blank_lines': self.skip_blank_lines,
                'thousands': self.thousands,
                'decimal': self.decimal,
                'lineterminator': self.lineterminator,
                'quotechar': self.quotechar,
                'quoting': self.quoting,
                'escapechar': self.escapechar,
                'comment': self.comment,
                'encoding': self.encoding,
                'dialect': self.dialect,
                'error_bad_lines': self.error_bad_lines,
                'warn_bad_lines': self.warn_bad_lines,
                'skipfooter': self.skipfooter,
                'doublequote': self.doublequote,
                'memory_map': self.memory_map,
                'float_precision': self.float_precision,
                'chunksize': self.chunksize,
                'encoding_errors': self.encoding_errors,
                'on_bad_lines': self.on_bad_lines,
                'date_format': self.date_format,
            }

            # some kwargs must not be passed if they don't have a default value
            # some others are deprecated and we can also not pass them if they are the default value
            for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS:
                   if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig()   ,						snake_case__ ):
                          del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 2.0 new arguments
            if not (datasets.config.PANDAS_VERSION.major >= 2):
                   for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS:
                          del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 1.3 new arguments
            if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3):
                   for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS:
                          del pd_read_csv_kwargs[pd_read_csv_parameter]

            return pd_read_csv_kwargs
class 						UpperCamelCase__       (			datasets.ArrowBasedBuilder						):



     """simple docstring"""






     __magic_name__           =     CsvConfig







     def a						(			self ):

            '''simple docstring'''

            return datasets.DatasetInfo(features=self.config.features )







     def a						(			self   ,						snake_case__ ):

            '''simple docstring'''

            if not self.config.data_files:
                   raise ValueError(F'At least one data file must be specified, but got data_files={self.config.data_files}' )
            _lowerCAmelCase     :				Any       =							dl_manager.download_and_extract(self.config.data_files )
            if isinstance(snake_case__   ,						(str, list, tuple) ):
                   _lowerCAmelCase     :				Dict       =							data_files
                   if isinstance(snake_case__   ,						snake_case__ ):
                          _lowerCAmelCase     :				List[str]       =							[files]
                   _lowerCAmelCase     :				Optional[int]       =							[dl_manager.iter_files(snake_case__ ) for file in files]
                   return [datasets.SplitGenerator(name=datasets.Split.TRAIN   ,						gen_kwargs={'files': files} )]
            _lowerCAmelCase     :				int       =							[]
            for split_name, files in data_files.items():
                   if isinstance(snake_case__   ,						snake_case__ ):
                          _lowerCAmelCase     :				str       =							[files]
                   _lowerCAmelCase     :				Optional[Any]       =							[dl_manager.iter_files(snake_case__ ) for file in files]
                   splits.append(datasets.SplitGenerator(name=snake_case__   ,						gen_kwargs={'files': files} ) )
            return splits







     def a						(			self   ,						snake_case__ ):

            '''simple docstring'''

            if self.config.features is not None:
                   _lowerCAmelCase     :				List[Any]       =							self.config.features.arrow_schema
                   if all(not require_storage_cast(snake_case__ ) for feature in self.config.features.values() ):
                          # cheaper cast
                          _lowerCAmelCase     :				str       =							pa.Table.from_arrays([pa_table[field.name] for field in schema]   ,						schema=snake_case__ )
                   else:
                          # more expensive cast; allows str <-> int/float or str to Audio for example
                          _lowerCAmelCase     :				Tuple       =							table_cast(snake_case__   ,						snake_case__ )
            return pa_table







     def a						(			self   ,						snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				Any       =							self.config.features.arrow_schema if self.config.features else None
            # dtype allows reading an int column as str
            _lowerCAmelCase     :				Any       =							(
                {
                    name: dtype.to_pandas_dtype() if not require_storage_cast(snake_case__ ) else object
                    for name, dtype, feature in zip(schema.names   ,						schema.types   ,						self.config.features.values() )
                }
                if schema is not None
                else None
            )
            for file_idx, file in enumerate(itertools.chain.from_iterable(snake_case__ ) ):
                   _lowerCAmelCase     :				Optional[Any]       =							pd.read_csv(snake_case__   ,						iterator=snake_case__   ,						dtype=snake_case__   ,						**self.config.pd_read_csv_kwargs )
                   try:
                          for batch_idx, df in enumerate(snake_case__ ):
                                 _lowerCAmelCase     :				str       =							pa.Table.from_pandas(snake_case__ )
                                 # Uncomment for debugging (will print the Arrow table size and elements)
                                 # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                                 # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
                                 yield (file_idx, batch_idx), self._cast_table(snake_case__ )
                   except ValueError as e:
                          logger.error(F'Failed to read file \'{file}\' with error {type(snake_case__ )}: {e}' )
                          raise





'''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 get_down_block, get_mid_block, get_out_block, get_up_block
@dataclass
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_						):



     """simple docstring"""






     __magic_name__           =     42
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_     ,       SCREAMING_SNAKE_CASE_						):



     """simple docstring"""






     @register_to_config
     def __init__(			self   ,						snake_case__ = 6_5536   ,						snake_case__ = None   ,						snake_case__ = 2   ,						snake_case__ = 2   ,						snake_case__ = 0   ,						snake_case__ = "fourier"   ,						snake_case__ = True   ,						snake_case__ = False   ,						snake_case__ = 0.0   ,						snake_case__ = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")   ,						snake_case__ = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")   ,						snake_case__ = "UNetMidBlock1D"   ,						snake_case__ = None   ,						snake_case__ = (32, 32, 64)   ,						snake_case__ = None   ,						snake_case__ = 8   ,						snake_case__ = 1   ,						snake_case__ = False   ,						):

            '''simple docstring'''

            super().__init__()
            _lowerCAmelCase     :				Optional[int]       =							sample_size

            # time
            if time_embedding_type == "fourier":
                   _lowerCAmelCase     :				Tuple       =							GaussianFourierProjection(
                       embedding_size=8   ,						set_W_to_weight=snake_case__   ,						log=snake_case__   ,						flip_sin_to_cos=snake_case__ )
                   _lowerCAmelCase     :				List[Any]       =							2 * block_out_channels[0]
            elif time_embedding_type == "positional":
                   _lowerCAmelCase     :				Optional[Any]       =							Timesteps(
                       block_out_channels[0]   ,						flip_sin_to_cos=snake_case__   ,						downscale_freq_shift=snake_case__ )
                   _lowerCAmelCase     :				Optional[int]       =							block_out_channels[0]

            if use_timestep_embedding:
                   _lowerCAmelCase     :				Any       =							block_out_channels[0] * 4
                   _lowerCAmelCase     :				List[str]       =							TimestepEmbedding(
                       in_channels=snake_case__   ,						time_embed_dim=snake_case__   ,						act_fn=snake_case__   ,						out_dim=block_out_channels[0]   ,						)

            _lowerCAmelCase     :				Tuple       =							nn.ModuleList([] )
            _lowerCAmelCase     :				Tuple       =							None
            _lowerCAmelCase     :				Optional[Any]       =							nn.ModuleList([] )
            _lowerCAmelCase     :				str       =							None

            # down
            _lowerCAmelCase     :				List[str]       =							in_channels
            for i, down_block_type in enumerate(snake_case__ ):
                   _lowerCAmelCase     :				str       =							output_channel
                   _lowerCAmelCase     :				Any       =							block_out_channels[i]

                   if i == 0:
                          input_channel += extra_in_channels

                   _lowerCAmelCase     :				Tuple       =							i == len(snake_case__ ) - 1

                   _lowerCAmelCase     :				List[str]       =							get_down_block(
                       snake_case__   ,						num_layers=snake_case__   ,						in_channels=snake_case__   ,						out_channels=snake_case__   ,						temb_channels=block_out_channels[0]   ,						add_downsample=not is_final_block or downsample_each_block   ,						)
                   self.down_blocks.append(snake_case__ )

            # mid
            _lowerCAmelCase     :				List[str]       =							get_mid_block(
                snake_case__   ,						in_channels=block_out_channels[-1]   ,						mid_channels=block_out_channels[-1]   ,						out_channels=block_out_channels[-1]   ,						embed_dim=block_out_channels[0]   ,						num_layers=snake_case__   ,						add_downsample=snake_case__   ,						)

            # up
            _lowerCAmelCase     :				Union[str, Any]       =							list(reversed(snake_case__ ) )
            _lowerCAmelCase     :				List[Any]       =							reversed_block_out_channels[0]
            if out_block_type is None:
                   _lowerCAmelCase     :				Tuple       =							out_channels
            else:
                   _lowerCAmelCase     :				Tuple       =							block_out_channels[0]

            for i, up_block_type in enumerate(snake_case__ ):
                   _lowerCAmelCase     :				int       =							output_channel
                   _lowerCAmelCase     :				Dict       =							(
                       reversed_block_out_channels[i + 1] if i < len(snake_case__ ) - 1 else final_upsample_channels
                   )

                   _lowerCAmelCase     :				Union[str, Any]       =							i == len(snake_case__ ) - 1

                   _lowerCAmelCase     :				Tuple       =							get_up_block(
                       snake_case__   ,						num_layers=snake_case__   ,						in_channels=snake_case__   ,						out_channels=snake_case__   ,						temb_channels=block_out_channels[0]   ,						add_upsample=not is_final_block   ,						)
                   self.up_blocks.append(snake_case__ )
                   _lowerCAmelCase     :				List[str]       =							output_channel

            # out
            _lowerCAmelCase     :				Tuple       =							norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4   ,						32 )
            _lowerCAmelCase     :				Any       =							get_out_block(
                out_block_type=snake_case__   ,						num_groups_out=snake_case__   ,						embed_dim=block_out_channels[0]   ,						out_channels=snake_case__   ,						act_fn=snake_case__   ,						fc_dim=block_out_channels[-1] // 4   ,						)







     def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__ = True   ,						):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[int]       =							timestep
            if not torch.is_tensor(snake_case__ ):
                   _lowerCAmelCase     :				Optional[int]       =							torch.tensor([timesteps]   ,						dtype=torch.long   ,						device=sample.device )
            elif torch.is_tensor(snake_case__ ) and len(timesteps.shape ) == 0:
                   _lowerCAmelCase     :				List[Any]       =							timesteps[None].to(sample.device )

            _lowerCAmelCase     :				int       =							self.time_proj(snake_case__ )
            if self.config.use_timestep_embedding:
                   _lowerCAmelCase     :				Optional[int]       =							self.time_mlp(snake_case__ )
            else:
                   _lowerCAmelCase     :				List[str]       =							timestep_embed[..., None]
                   _lowerCAmelCase     :				Dict       =							timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype )
                   _lowerCAmelCase     :				Tuple       =							timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) )

            # 2. down
            _lowerCAmelCase     :				str       =							()
            for downsample_block in self.down_blocks:
                   _lowerCAmelCase      ,  _lowerCAmelCase     :				Union[str, Any]       =							downsample_block(hidden_states=snake_case__   ,						temb=snake_case__ )
                   down_block_res_samples += res_samples

            # 3. mid
            if self.mid_block:
                   _lowerCAmelCase     :				Any       =							self.mid_block(snake_case__   ,						snake_case__ )

            # 4. up
            for i, upsample_block in enumerate(self.up_blocks ):
                   _lowerCAmelCase     :				Union[str, Any]       =							down_block_res_samples[-1:]
                   _lowerCAmelCase     :				Union[str, Any]       =							down_block_res_samples[:-1]
                   _lowerCAmelCase     :				Union[str, Any]       =							upsample_block(snake_case__   ,						res_hidden_states_tuple=snake_case__   ,						temb=snake_case__ )

            # 5. post-process
            if self.out_block:
                   _lowerCAmelCase     :				Optional[Any]       =							self.out_block(snake_case__   ,						snake_case__ )

            if not return_dict:
                   return (sample,)

            return UNetaDOutput(sample=snake_case__ )

