


from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_tf_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_tf_available():
       import tensorflow as tf


__UpperCamelCase				:  int      					=		logging.get_logger(__name__)





@dataclass
class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     int			= [
        'no_inference',
        'no_cuda',
        'no_tpu',
        'no_speed',
        'no_memory',
        'no_env_print',
        'no_multi_process',
    ]





    def __init__(	self				:   Tuple       ,   **__snake_case				:   Optional[int]	):





       '''simple docstring'''
       for deprecated_arg in self.deprecated_args:
          if deprecated_arg in kwargs:
             UpperCAmelCase_							:      Tuple						=   deprecated_arg[3:]
             UpperCAmelCase_							:      Tuple						=   not kwargs.pop(__A	)
             logger.warning(
                 f'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or'''
                 f''' {positive_arg}={kwargs[positive_arg]}'''	)
       UpperCAmelCase_							:      Dict						=   kwargs.pop('''tpu_name'''       ,   self.tpu_name	)
       UpperCAmelCase_							:      List[Any]						=   kwargs.pop('''device_idx'''       ,   self.device_idx	)
       UpperCAmelCase_							:      Optional[int]						=   kwargs.pop('''eager_mode'''       ,   self.eager_mode	)
       UpperCAmelCase_							:      Optional[Any]						=   kwargs.pop('''use_xla'''       ,   self.use_xla	)
       super().__init__(**__A	)

    A_  :     List[Any]			= field(
        default=snake_case__    ,    metadata={'help': 'Name of TPU'}    ,    )
    A_  :     Optional[Any]			= field(
        default=0    ,    metadata={'help': 'CPU / GPU device index. Defaults to 0.'}    ,    )
    A_  :     str			= field(default=snake_case__    ,    metadata={'help': 'Benchmark models in eager model.'}						)
    A_  :     Optional[Any]			= field(
        default=snake_case__    ,    metadata={
            'help': 'Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.'
        }    ,    )





    @cached_property
    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       requires_backends(self       ,   ['''tf''']	)
       UpperCAmelCase_							:      Any						=   None
       if self.tpu:
          try:
             if self.tpu_name:
                UpperCAmelCase_							:      Tuple						=   tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name	)
             else:
                UpperCAmelCase_							:      Optional[int]						=   tf.distribute.cluster_resolver.TPUClusterResolver()
          except ValueError:
             UpperCAmelCase_							:      Tuple						=   None
       return tpu





    @cached_property
    def 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       requires_backends(self       ,   ['''tf''']	)
       if self.is_tpu:
          tf.config.experimental_connect_to_cluster(self._setup_tpu	)
          tf.tpu.experimental.initialize_tpu_system(self._setup_tpu	)

          UpperCAmelCase_							:      int						=   tf.distribute.TPUStrategy(self._setup_tpu	)
       else:
          # currently no multi gpu is allowed
          if self.is_gpu:
             # TODO: Currently only single GPU is supported
             tf.config.set_visible_devices(self.gpu_list[self.device_idx]       ,   '''GPU'''	)
             UpperCAmelCase_							:      Tuple						=   tf.distribute.OneDeviceStrategy(device=f'''/gpu:{self.device_idx}'''	)
          else:
             tf.config.set_visible_devices([]       ,   '''GPU'''	)  # disable GPU
             UpperCAmelCase_							:      Optional[int]						=   tf.distribute.OneDeviceStrategy(device=f'''/cpu:{self.device_idx}'''	)

       return strategy





    @property
    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       requires_backends(self       ,   ['''tf''']	)
       return self._setup_tpu is not None





    @property
    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       requires_backends(self       ,   ['''tf''']	)
       return self._setup_strategy





    @property
    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       requires_backends(self       ,   ['''tf''']	)
       return tf.config.list_physical_devices('''GPU'''	)





    @property
    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       requires_backends(self       ,   ['''tf''']	)
       if self.cuda:
          return len(self.gpu_list	)
       return 0





    @property
    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





       '''simple docstring'''
       return self.n_gpu > 0



# This code is adapted from OpenAI's release
# https://github.com/openai/human-eval/blob/master/human_eval/execution.py

import contextlib
import faulthandler
import io
import multiprocessing
import os
import platform
import signal
import tempfile
def      snake_case_						( __lowercase			,       __lowercase			,       __lowercase			,       __lowercase       ):
   UpperCAmelCase_							:      int						=   multiprocessing.Manager()
   UpperCAmelCase_							:      Union[str, Any]						=   manager.list()

   UpperCAmelCase_							:      int						=   multiprocessing.Process(target=__lowercase			,       args=(check_program, result, timeout)       )
   p.start()
   p.join(timeout=timeout + 1       )
   if p.is_alive():
      p.kill()

   if not result:
      result.append('''timed out'''       )

   return {
       "task_id": task_id,
       "passed": result[0] == "passed",
       "result": result[0],
       "completion_id": completion_id,
   }
def      snake_case_						( __lowercase			,       __lowercase			,       __lowercase       ):
   with create_tempdir():
      # These system calls are needed when cleaning up tempdir.
      import os
      import shutil

      UpperCAmelCase_							:      str						=   shutil.rmtree
      UpperCAmelCase_							:      Tuple						=   os.rmdir
      UpperCAmelCase_							:      Dict						=   os.chdir

      # Disable functionalities that can make destructive changes to the test.
      reliability_guard()

      # Run program.
      try:
         UpperCAmelCase_							:      Optional[int]						=   {}
         with swallow_io():
            with time_limit(__lowercase       ):
               exec(__lowercase			,       __lowercase       )
         result.append('''passed'''       )
      except TimeoutException:
         result.append('''timed out'''       )
      except BaseException as e:
         result.append(F'''failed: {e}'''       )

      # Needed for cleaning up.
      UpperCAmelCase_							:      Optional[int]						=   rmtree
      UpperCAmelCase_							:      Optional[Any]						=   rmdir
      UpperCAmelCase_							:      Optional[Any]						=   chdir
@contextlib.contextmanager
def      snake_case_						( __lowercase       ):
   def signal_handler(__lowercase			,       __lowercase       ):
      raise TimeoutException('''Timed out!'''       )

   signal.setitimer(signal.ITIMER_REAL			,       __lowercase       )
   signal.signal(signal.SIGALRM			,       __lowercase       )
   try:
      yield
   finally:
      signal.setitimer(signal.ITIMER_REAL			,       0       )
@contextlib.contextmanager
def      snake_case_						( ):
   UpperCAmelCase_							:      Optional[Any]						=   WriteOnlyStringIO()
   with contextlib.redirect_stdout(__lowercase       ):
      with contextlib.redirect_stderr(__lowercase       ):
         with redirect_stdin(__lowercase       ):
            yield
@contextlib.contextmanager
def      snake_case_						( ):
   with tempfile.TemporaryDirectory() as dirname:
      with chdir(__lowercase       ):
         yield dirname





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    pass





class  lowerCAmelCase__(    io.StringIO						):






    '''simple docstring'''





    def 					_lowerCamelCase (	self				:   Dict       ,   *__snake_case				:   List[Any]       ,   **__snake_case				:   int	):





       '''simple docstring'''
       raise OSError





    def 					_lowerCamelCase (	self				:   Dict       ,   *__snake_case				:   int       ,   **__snake_case				:   Any	):





       '''simple docstring'''
       raise OSError





    def 					_lowerCamelCase (	self				:   int       ,   *__snake_case				:   List[str]       ,   **__snake_case				:   Optional[Any]	):





       '''simple docstring'''
       raise OSError





    def 					_lowerCamelCase (	self				:   Union[str, Any]       ,   *__snake_case				:   Optional[Any]       ,   **__snake_case				:   List[Any]	):





       '''simple docstring'''
       return False





class  lowerCAmelCase__(    contextlib._RedirectStream						):  # type: ignore






    '''simple docstring'''





    A_  :     Optional[Any]			= 'stdin'





@contextlib.contextmanager
def      snake_case_						( __lowercase       ):
   if root == ".":
      yield
      return
   UpperCAmelCase_							:      Tuple						=   os.getcwd()
   os.chdir(__lowercase       )
   try:
      yield
   except BaseException as exc:
      raise exc
   finally:
      os.chdir(__lowercase       )


def      snake_case_						( __lowercase=None       ):

   if maximum_memory_bytes is not None:
      import resource

      resource.setrlimit(resource.RLIMIT_AS			,       (maximum_memory_bytes, maximum_memory_bytes)       )
      resource.setrlimit(resource.RLIMIT_DATA			,       (maximum_memory_bytes, maximum_memory_bytes)       )
      if not platform.uname().system == "Darwin":
         resource.setrlimit(resource.RLIMIT_STACK			,       (maximum_memory_bytes, maximum_memory_bytes)       )

   faulthandler.disable()

   import builtins

   UpperCAmelCase_							:      Any						=   None
   UpperCAmelCase_							:      Any						=   None

   import os

   UpperCAmelCase_							:      Union[str, Any]						=   '''1'''

   UpperCAmelCase_							:      List[str]						=   None
   UpperCAmelCase_							:      Dict						=   None
   UpperCAmelCase_							:      Any						=   None
   UpperCAmelCase_							:      Optional[int]						=   None
   UpperCAmelCase_							:      List[str]						=   None
   UpperCAmelCase_							:      List[Any]						=   None
   UpperCAmelCase_							:      Union[str, Any]						=   None
   UpperCAmelCase_							:      str						=   None
   UpperCAmelCase_							:      Dict						=   None
   UpperCAmelCase_							:      int						=   None
   UpperCAmelCase_							:      Dict						=   None
   UpperCAmelCase_							:      List[Any]						=   None
   UpperCAmelCase_							:      List[Any]						=   None
   UpperCAmelCase_							:      Optional[Any]						=   None
   UpperCAmelCase_							:      Optional[Any]						=   None
   UpperCAmelCase_							:      Dict						=   None
   UpperCAmelCase_							:      Optional[Any]						=   None
   UpperCAmelCase_							:      Optional[Any]						=   None
   UpperCAmelCase_							:      Optional[Any]						=   None
   UpperCAmelCase_							:      List[Any]						=   None
   UpperCAmelCase_							:      int						=   None
   UpperCAmelCase_							:      Union[str, Any]						=   None
   UpperCAmelCase_							:      List[Any]						=   None
   UpperCAmelCase_							:      Dict						=   None
   UpperCAmelCase_							:      Union[str, Any]						=   None
   UpperCAmelCase_							:      Dict						=   None
   UpperCAmelCase_							:      Any						=   None

   import shutil

   UpperCAmelCase_							:      Union[str, Any]						=   None
   UpperCAmelCase_							:      Dict						=   None
   UpperCAmelCase_							:      Tuple						=   None

   import subprocess

   UpperCAmelCase_							:      Dict						=   None  # type: ignore

   UpperCAmelCase_							:      Union[str, Any]						=   None

   import sys

   UpperCAmelCase_							:      Optional[Any]						=   None
   UpperCAmelCase_							:      List[Any]						=   None
   UpperCAmelCase_							:      List[Any]						=   None
   UpperCAmelCase_							:      List[str]						=   None
   UpperCAmelCase_							:      Dict						=   None