



"""simple docstring"""





import dataclasses
import json
import warnings
from dataclasses import dataclass, field
from time import time
from typing import List

from ..utils import logging


lowerCamelCase  			=		logging.get_logger(__name__)
def   a__      ( lowerCAmelCase__=None      ,     lowerCAmelCase__=None ):
      return field(default_factory=lambda: default      ,     metadata=lowerCAmelCase__ )


@dataclass
class  lowercase__  :







       '''simple docstring'''


       UpperCamelCase		=	list_field(
           default=[]							,   metadata={
               '''help''': (
                   '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version'''
                   ''' of all available models'''
               )
           }							,   )

       UpperCamelCase		=	list_field(
           default=[8]							,   metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} )

       UpperCamelCase		=	list_field(
           default=[8, 32, 1_28, 5_12]							,   metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''}							,   )

       UpperCamelCase		=	field(
           default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''}							,   )
       UpperCamelCase		=	field(
           default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''}							,   )
       UpperCamelCase		=	field(
           default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} )
       UpperCamelCase		=	field(default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Use FP16 to accelerate inference.'''} )
       UpperCamelCase		=	field(default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Benchmark training of model'''} )
       UpperCamelCase		=	field(default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Verbose memory tracing'''} )
       UpperCamelCase		=	field(
           default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''}							,   )
       UpperCamelCase		=	field(
           default=SCREAMING_SNAKE_CASE							,   metadata={
               '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory'''
           }							,   )
       UpperCamelCase		=	field(default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Trace memory line by line'''} )
       UpperCamelCase		=	field(default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Save result to a CSV file'''} )
       UpperCamelCase		=	field(default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Save all print statements in a log file'''} )
       UpperCamelCase		=	field(default=SCREAMING_SNAKE_CASE							,   metadata={'''help''': '''Whether to print environment information'''} )
       UpperCamelCase		=	field(
           default=SCREAMING_SNAKE_CASE							,   metadata={
               '''help''': (
                   '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use'''
                   ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled'''
                   ''' for debugging / testing and on TPU.'''
               )
           }							,   )
       UpperCamelCase		=	field(
           default=F"inference_time_{round(time() )}.csv"							,   metadata={'''help''': '''CSV filename used if saving time results to csv.'''}							,   )
       UpperCamelCase		=	field(
           default=F"inference_memory_{round(time() )}.csv"							,   metadata={'''help''': '''CSV filename used if saving memory results to csv.'''}							,   )
       UpperCamelCase		=	field(
           default=F"train_time_{round(time() )}.csv"							,   metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''}							,   )
       UpperCamelCase		=	field(
           default=F"train_memory_{round(time() )}.csv"							,   metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''}							,   )
       UpperCamelCase		=	field(
           default=F"env_info_{round(time() )}.csv"							,   metadata={'''help''': '''CSV filename used if saving environment information.'''}							,   )
       UpperCamelCase		=	field(
           default=F"log_{round(time() )}.csv"							,   metadata={'''help''': '''Log filename used if print statements are saved in log.'''}							,   )
       UpperCamelCase		=	field(default=3							,   metadata={'''help''': '''Times an experiment will be run.'''} )
       UpperCamelCase		=	field(
           default=SCREAMING_SNAKE_CASE							,   metadata={
               '''help''': (
                   '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain'''
                   ''' model weights.'''
               )
           }							,   )



       def 	lowercase__							(				self     :    List[str]       )					->		Optional[Any]:


             '''simple docstring'''




             warnings.warn(
                 F"""The class {self.__class__} is deprecated. Hugging Face Benchmarking utils"""
                 " are deprecated in general and it is advised to use external Benchmarking libraries "
                 " to benchmark Transformer models."						,							_UpperCAmelCase						,							)



       def 	lowercase__							(				self     :    List[str]       )					->		Optional[Any]:


             '''simple docstring'''




             return json.dumps(dataclasses.asdict(self       )						,							indent=2       )



       @property
       def 	lowercase__							(				self     :    Union[str, Any]       )					->		List[str]:


             '''simple docstring'''




             if len(self.models       ) <= 0:
                   raise ValueError(
                       "Please make sure you provide at least one model name / model identifier, *e.g.* `--models"
                       " bert-base-cased` or `args.models = ['bert-base-cased']."       )
             return self.models







       @property
       def 	lowercase__							(				self     :    Union[str, Any]       )					->		List[str]:


             '''simple docstring'''




             if not self.multi_process:
                   return False
             elif self.is_tpu:
                   logger.info("Multiprocessing is currently not possible on TPU."       )
                   return False
             else:
                   return True









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	:   Any    =   ["names", "prefix"]
_lowerCAmelCase	:   str    =   ["warn_bad_lines", "error_bad_lines", "mangle_dupe_cols"]
_lowerCAmelCase	:   List[str]    =   ["encoding_errors", "on_bad_lines"]
_lowerCAmelCase	:   int    =   ["date_format"]


@dataclass
class 	__snake_case   ( datasets.BuilderConfig ):
		SCREAMING_SNAKE_CASE__								=	","
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	"infer"
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	"."
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	'"'
		SCREAMING_SNAKE_CASE__								=	0
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	0
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	10000
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	"strict"
		SCREAMING_SNAKE_CASE__								=	"error"
		SCREAMING_SNAKE_CASE__								=	None
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						if self.delimiter is not None:
										lowerCAmelCase__		  =  self.delimiter
						if self.column_names is not None:
										lowerCAmelCase__		  =  self.column_names



		@property
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  {
						    '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()    ,a_						):
														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 	__snake_case   ( datasets.ArrowBasedBuilder ):
		SCREAMING_SNAKE_CASE__								=	CsvConfig
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						return datasets.DatasetInfo(features=self.config.features						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







						"""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__		  =  dl_manager.download_and_extract(self.config.data_files						)
						if isinstance(a_    ,(str, list, tuple)						):
										lowerCAmelCase__		  =  data_files
										if isinstance(a_    ,a_						):
														lowerCAmelCase__		  =  [files]
										lowerCAmelCase__		  =  [dl_manager.iter_files(a_						) for file in files]
										return [datasets.SplitGenerator(name=datasets.Split.TRAIN    ,gen_kwargs={'files': files}						)]
						lowerCAmelCase__		  =  []
						for split_name, files in data_files.items():
										if isinstance(a_    ,a_						):
														lowerCAmelCase__		  =  [files]
										lowerCAmelCase__		  =  [dl_manager.iter_files(a_						) for file in files]
										splits.append(datasets.SplitGenerator(name=a_    ,gen_kwargs={'files': files}						)						)
						return splits
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







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



		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.config.features.arrow_schema if self.config.features else None
						# dtype allows reading an int column as str
						lowerCAmelCase__		  =  (
						    {
						        name: dtype.to_pandas_dtype() if not require_storage_cast(a_						) 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(a_						)						):
										lowerCAmelCase__		  =  pd.read_csv(a_    ,iterator=a_    ,dtype=a_    ,**self.config.pd_read_csv_kwargs						)
										try:
														for batch_idx, df in enumerate(a_						):
																		lowerCAmelCase__		  =  pa.Table.from_pandas(a_						)
																		# 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(a_						)
										except ValueError as e:
														logger.error(f'Failed to read file \'{file}\' with error {type(a_						)}: {e}'						)
														raise
