





"""simple docstring"""
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


lowerCamelCase_          =	threading.Lock()
lowerCamelCase_          =	None

lowerCamelCase_          =	{
    "debug": logging.DEBUG,
    "info": logging.INFO,
    "warning": logging.WARNING,
    "error": logging.ERROR,
    "critical": logging.CRITICAL,
}

lowerCamelCase_          =	logging.WARNING

lowerCamelCase_          =	True



def   __lowerCamelCase     (							)    ->   Dict:
      __SCREAMING_SNAKE_CASE     :List[str]	       =						os.getenv('''DIFFUSERS_VERBOSITY'''				,     a_  )
      if env_level_str:
            if env_level_str in log_levels:
                  return log_levels[env_level_str]
            else:
                  logging.getLogger().warning(
                      f'''Unknown option DIFFUSERS_VERBOSITY={env_level_str}, '''
                      f'''has to be one of: { ", ".join(log_levels.keys()  ) }'''  )
      return _default_log_level



def   __lowerCamelCase     (							)    ->   str:
      return __name__.split('''.'''  )[0]



def   __lowerCamelCase     (							)    ->   logging.Logger:
      return logging.getLogger(_get_library_name()  )



def   __lowerCamelCase     (							)    ->   None:
      global _default_handler

      with _lock:
            if _default_handler:
                  # This library has already configured the library root logger.
                  return
            __SCREAMING_SNAKE_CASE     :Optional[int]	       =						logging.StreamHandler()  # Set sys.stderr as stream.
            __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						sys.stderr.flush

            # Apply our default configuration to the library root logger.
            __SCREAMING_SNAKE_CASE     :Optional[int]	       =						_get_library_root_logger()
            library_root_logger.addHandler(_default_handler  )
            library_root_logger.setLevel(_get_default_logging_level()  )
            __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						False



def   __lowerCamelCase     (							)    ->   None:
      global _default_handler

      with _lock:
            if not _default_handler:
                  return

            __SCREAMING_SNAKE_CASE     :Optional[int]	       =						_get_library_root_logger()
            library_root_logger.removeHandler(_default_handler  )
            library_root_logger.setLevel(logging.NOTSET  )
            __SCREAMING_SNAKE_CASE     :Tuple	       =						None



def   __lowerCamelCase     (							)    ->   List[str]:
      return log_levels



def   __lowerCamelCase     (							a_  :	Optional[str] = None  )    ->   logging.Logger:

      if name is None:
            __SCREAMING_SNAKE_CASE     :Optional[int]	       =						_get_library_name()

      _configure_library_root_logger()
      return logging.getLogger(a_  )



def   __lowerCamelCase     (							)    ->   int:
      _configure_library_root_logger()
      return _get_library_root_logger().getEffectiveLevel()



def   __lowerCamelCase     (							a_  :	int  )    ->   None:
      _configure_library_root_logger()
      _get_library_root_logger().setLevel(a_  )



def   __lowerCamelCase     (							)    ->   Optional[Any]:
      return set_verbosity(a_  )



def   __lowerCamelCase     (							)    ->   int:
      return set_verbosity(a_  )



def   __lowerCamelCase     (							)    ->   Optional[Any]:
      return set_verbosity(a_  )



def   __lowerCamelCase     (							)    ->   Optional[int]:
      return set_verbosity(a_  )



def   __lowerCamelCase     (							)    ->   None:
      _configure_library_root_logger()

      assert _default_handler is not None
      _get_library_root_logger().removeHandler(_default_handler  )



def   __lowerCamelCase     (							)    ->   None:
      _configure_library_root_logger()

      assert _default_handler is not None
      _get_library_root_logger().addHandler(_default_handler  )



def   __lowerCamelCase     (							a_  :	logging.Handler  )    ->   None:
      _configure_library_root_logger()

      assert handler is not None
      _get_library_root_logger().addHandler(a_  )



def   __lowerCamelCase     (							a_  :	logging.Handler  )    ->   None:
      _configure_library_root_logger()

      assert handler is not None and handler not in _get_library_root_logger().handlers
      _get_library_root_logger().removeHandler(a_  )



def   __lowerCamelCase     (							)    ->   None:
      _configure_library_root_logger()
      __SCREAMING_SNAKE_CASE     :Dict	       =						False



def   __lowerCamelCase     (							)    ->   None:
      _configure_library_root_logger()
      __SCREAMING_SNAKE_CASE     :str	       =						True



def   __lowerCamelCase     (							)    ->   None:
      __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						_get_library_root_logger().handlers

      for handler in handlers:
            __SCREAMING_SNAKE_CASE     :Any	       =						logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'''  )
            handler.setFormatter(a_  )



def   __lowerCamelCase     (							)    ->   None:
      __SCREAMING_SNAKE_CASE     :str	       =						_get_library_root_logger().handlers

      for handler in handlers:
            handler.setFormatter(a_  )



def   __lowerCamelCase     (							self  :	List[str]				,     *a_  :	Union[str, Any]				,     **a_  :	Optional[Any]  )    ->   Optional[int]:
      __SCREAMING_SNAKE_CASE     :int	       =						os.getenv('''DIFFUSERS_NO_ADVISORY_WARNINGS'''				,     a_  )
      if no_advisory_warnings:
            return
      self.warning(*a_				,     **a_  )


lowerCamelCase_          =	warning_advice






class       _SCREAMING_SNAKE_CASE:




       def __init__(		self	,*SCREAMING_SNAKE_CASE__	,**SCREAMING_SNAKE_CASE__							)    ->   int:  # pylint: disable=unused-argument





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Any	       =						args[0] if args else None




       def __iter__(		self							)    ->   str:





             """simple docstring"""






             return iter(self._iterator							)




       def __getattr__(		self	,SCREAMING_SNAKE_CASE__							)    ->   List[Any]:





             """simple docstring"""






             def empty_fn(*SCREAMING_SNAKE_CASE__	,**SCREAMING_SNAKE_CASE__							):  # pylint: disable=unused-argument
                   return

             return empty_fn




       def __enter__(		self							)    ->   Optional[int]:





             """simple docstring"""






             return self




       def __exit__(		self	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__							)    ->   List[Any]:





             """simple docstring"""






             return






class       _SCREAMING_SNAKE_CASE:




       def __call__(		self	,*SCREAMING_SNAKE_CASE__	,**SCREAMING_SNAKE_CASE__							)    ->   Optional[int]:





             """simple docstring"""






             if _tqdm_active:
                   return tqdm_lib.tqdm(*SCREAMING_SNAKE_CASE__	,**SCREAMING_SNAKE_CASE__							)
             else:
                   return EmptyTqdm(*SCREAMING_SNAKE_CASE__	,**SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self	,*SCREAMING_SNAKE_CASE__	,**SCREAMING_SNAKE_CASE__							)    ->   Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						None
             if _tqdm_active:
                   return tqdm_lib.tqdm.set_lock(*SCREAMING_SNAKE_CASE__	,**SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self							)    ->   Dict:





             """simple docstring"""






             if _tqdm_active:
                   return tqdm_lib.tqdm.get_lock()




lowerCamelCase_          =	_tqdm_cls()



def   __lowerCamelCase     (							)    ->   bool:
      global _tqdm_active
      return bool(_tqdm_active  )



def   __lowerCamelCase     (							)    ->   List[Any]:
      global _tqdm_active
      __SCREAMING_SNAKE_CASE     :Tuple	       =						True







def   __lowerCamelCase     (							)    ->   Optional[Any]:
      global _tqdm_active
      __SCREAMING_SNAKE_CASE     :List[Any]	       =						False






"""simple docstring"""
import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


lowerCamelCase_          =	(3_0, 4_8_7)
lowerCamelCase_          =	(3_6, 1_0_2_4)
lowerCamelCase_          =	(1_0_0, 1_0_0)
lowerCamelCase_          =	1_0_0

lowerCamelCase_          =	datasets.Features(
    {"text": ArrayaD(SHAPE_TEST_1, dtype="float32"), "image": ArrayaD(SHAPE_TEST_2, dtype="float32")}
)

lowerCamelCase_    ,  lowerCamelCase_          =	os.path.split(__file__)
lowerCamelCase_          =	os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))



@get_duration
def   __lowerCamelCase     (							a_  :	Optional[Any]				,     a_  :	List[str]				,     a_  :	List[str]							)    ->   List[str]:
      with ArrowWriter(features=a_				,     path=os.path.join(a_				,     '''beta.arrow'''							)							) as writer:
            for key, record in dummy_data:
                  __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						my_features.encode_example(a_							)
                  writer.write(a_							)
            __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :Optional[Any]	       =						writer.finalize()



@get_duration
def   __lowerCamelCase     (							a_  :	Tuple				,     a_  :	Optional[Any]							)    ->   Dict:
      __SCREAMING_SNAKE_CASE     :Tuple	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      for _ in dataset:
            pass



@get_duration
def   __lowerCamelCase     (							a_  :	List[str]				,     a_  :	List[str]							)    ->   List[Any]:
      __SCREAMING_SNAKE_CASE     :Optional[int]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      dataset.set_format('''numpy'''							)
      for _ in dataset:
            pass



@get_duration
def   __lowerCamelCase     (							a_  :	Tuple				,     a_  :	Optional[int]							)    ->   Optional[int]:
      __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						10
      __SCREAMING_SNAKE_CASE     :Dict	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      for i in range(0				,     len(a_							)				,     a_							):
            __SCREAMING_SNAKE_CASE     :Dict	       =						dataset[i : i + batch_size]



@get_duration
def   __lowerCamelCase     (							a_  :	Any				,     a_  :	Optional[Any]							)    ->   Optional[Any]:
      __SCREAMING_SNAKE_CASE     :List[Any]	       =						10
      __SCREAMING_SNAKE_CASE     :List[str]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      dataset.set_format('''numpy'''							)
      for i in range(0				,     len(a_							)				,     a_							):
            __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						dataset[i : i + batch_size]



@get_duration
def   __lowerCamelCase     (							a_  :	List[Any]				,     a_  :	Optional[Any]							)    ->   List[Any]:
      __SCREAMING_SNAKE_CASE     :Optional[int]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      for col in feats:
            __SCREAMING_SNAKE_CASE     :Tuple	       =						dataset[col]



@get_duration
def   __lowerCamelCase     (							a_  :	int				,     a_  :	List[str]							)    ->   str:
      __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      dataset.set_format('''numpy'''							)
      for col in feats:
            __SCREAMING_SNAKE_CASE     :List[Any]	       =						dataset[col]



def   __lowerCamelCase     (							)    ->   Union[str, Any]:
      __SCREAMING_SNAKE_CASE     :Tuple	       =						{}
      __SCREAMING_SNAKE_CASE     :int	       =						(
          read_unformated,
          read_formatted_as_numpy,
          read_batch_unformated,
          read_batch_formatted_as_numpy,
          read_col_unformated,
          read_col_formatted_as_numpy,
      )
      with tempfile.TemporaryDirectory() as tmp_dir:
            __SCREAMING_SNAKE_CASE     :Dict	       =						datasets.Features({'''image''': ArrayaD(a_				,     dtype='''float32'''							)}							)
            __SCREAMING_SNAKE_CASE     :Any	       =						generate_examples(features=a_				,     num_examples=a_							)
            __SCREAMING_SNAKE_CASE     :Dict	       =						write(a_				,     a_				,     a_							)
            for read_func in read_functions:
                  __SCREAMING_SNAKE_CASE     :str	       =						read_func(a_				,     a_							)

      with tempfile.TemporaryDirectory() as tmp_dir:
            # don't use fixed length for fair comparison
            # feats = datasets.Features(
            #     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
            # )
            __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						datasets.Features({'''image''': datasets.Sequence(datasets.Sequence(datasets.Value('''float32'''							)							)							)}							)
            __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						generate_examples(
                features=a_				,     num_examples=a_				,     seq_shapes={'''image''': SPEED_TEST_SHAPE}							)
            __SCREAMING_SNAKE_CASE     :Tuple	       =						write(a_				,     a_				,     a_							)
            for read_func in read_functions:
                  __SCREAMING_SNAKE_CASE     :Tuple	       =						read_func(a_				,     a_							)

      with tempfile.TemporaryDirectory() as tmp_dir:
            # don't use fixed length for fair comparison
            # feats = datasets.Features(
            #     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
            # )
            __SCREAMING_SNAKE_CASE     :Dict	       =						datasets.Features({'''image''': datasets.Sequence(datasets.Value('''float32'''							)							)}							)
            __SCREAMING_SNAKE_CASE     :Dict	       =						generate_examples(
                features=a_				,     num_examples=a_				,     seq_shapes={'''image''': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]}				,     )
            __SCREAMING_SNAKE_CASE     :Any	       =						write(a_				,     a_				,     a_							)
            for read_func in read_functions:
                  __SCREAMING_SNAKE_CASE     :str	       =						read_func(a_				,     a_							)

      with open(a_				,     '''wb'''							) as f:
            f.write(json.dumps(a_							).encode('''utf-8'''							)							)


if __name__ == "__main__":  # useful to run the profiler
  benchmark_array_xd()