'''simple docstring'''


# 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  _lowerCamelCase   (				lowerCamelCase_ :							Union[str, Any]     ,					lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							Dict       ):



     """simple docstring"""





     UpperCAmelCase_							:       Any       =				multiprocessing.Manager()
     UpperCAmelCase_							:       Tuple       =				manager.list()

     UpperCAmelCase_							:       List[str]       =				multiprocessing.Process(target=lowerCamelCase_     ,					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  _lowerCamelCase   (				lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							Optional[int]     ,					lowerCamelCase_ :							Optional[int]       ):



     """simple docstring"""





     with create_tempdir():
          # These system calls are needed when cleaning up tempdir.
          import os
          import shutil

          UpperCAmelCase_							:       Optional[int]       =				shutil.rmtree
          UpperCAmelCase_							:       List[Any]       =				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(lowerCamelCase_       ):
                         exec(lowerCamelCase_     ,					lowerCamelCase_       )
               result.append('passed'       )
          except TimeoutException:
               result.append('timed out'       )
          except BaseException as e:
               result.append(F'''failed: {e}'''       )

          # Needed for cleaning up.
          UpperCAmelCase_							:       str       =				rmtree
          UpperCAmelCase_							:       str       =				rmdir
          UpperCAmelCase_							:       str       =				chdir


@contextlib.contextmanager
def  _lowerCamelCase   (				lowerCamelCase_ :							Union[str, Any]       ):



     """simple docstring"""





     def signal_handler(lowerCamelCase_ :							Optional[int]     ,					lowerCamelCase_ :							Union[str, Any]       ):
          raise TimeoutException('Timed out!'       )

     signal.setitimer(signal.ITIMER_REAL     ,					lowerCamelCase_       )
     signal.signal(signal.SIGALRM     ,					lowerCamelCase_       )
     try:
          yield
     finally:
          signal.setitimer(signal.ITIMER_REAL     ,					0       )


@contextlib.contextmanager
def  _lowerCamelCase   (				):



     """simple docstring"""





     UpperCAmelCase_							:       List[str]       =				WriteOnlyStringIO()
     with contextlib.redirect_stdout(lowerCamelCase_       ):
          with contextlib.redirect_stderr(lowerCamelCase_       ):
               with redirect_stdin(lowerCamelCase_       ):
                    yield


@contextlib.contextmanager
def  _lowerCamelCase   (				):



     """simple docstring"""





     with tempfile.TemporaryDirectory() as dirname:
          with chdir(lowerCamelCase_       ):
               yield dirname






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  pass







class 					__SCREAMING_SNAKE_CASE						(	io.StringIO	):







  '''simple docstring'''






  def _UpperCamelCase					(			self				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

       raise OSError


  def _UpperCamelCase					(			self				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

       raise OSError


  def _UpperCamelCase					(			self				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

       raise OSError






  def _UpperCamelCase					(			self				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

       return False







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







  '''simple docstring'''






  lowerCamelCase_		:Dict						   =		'''stdin'''







@contextlib.contextmanager
def  _lowerCamelCase   (				lowerCamelCase_ :							str       ):



     """simple docstring"""





     if root == ".":
          yield
          return
     UpperCAmelCase_							:       Optional[int]       =				os.getcwd()
     os.chdir(lowerCamelCase_       )
     try:
          yield
     except BaseException as exc:
          raise exc
     finally:
          os.chdir(lowerCamelCase_       )





def  _lowerCamelCase   (				lowerCamelCase_ :							List[Any]=None       ):



     """simple docstring"""





     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_							:       Optional[int]       =				None
     UpperCAmelCase_							:       Dict       =				None

     import os

     UpperCAmelCase_							:       List[Any]       =				'1'

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

     import shutil

     UpperCAmelCase_							:       Any       =				None
     UpperCAmelCase_							:       Tuple       =				None
     UpperCAmelCase_							:       Optional[Any]       =				None

     import subprocess

     UpperCAmelCase_							:       Optional[int]       =				None  # type: ignore

     UpperCAmelCase_							:       List[Any]       =				None

     import sys

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







'''simple docstring'''


import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)






class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				['a', 'b', 'c']

       # Defaults to last layer if both are None
       UpperCAmelCase_       ,      UpperCAmelCase_							:       List[str]       =				get_aligned_output_features_output_indices(snake_case_				,		snake_case_				,		snake_case_						)
       self.assertEqual(snake_case_				,		['c']						)
       self.assertEqual(snake_case_				,		[2]						)

       # Out indices set to match out features
       UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				get_aligned_output_features_output_indices(['a', 'c']				,		snake_case_				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[0, 2]						)

       # Out features set to match out indices
       UpperCAmelCase_       ,      UpperCAmelCase_							:       List[Any]       =				get_aligned_output_features_output_indices(snake_case_				,		[0, 2]				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[0, 2]						)

       # Out features selected from negative indices
       UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				get_aligned_output_features_output_indices(snake_case_				,		[-3, -1]				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[-3, -1]						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 1)				,		snake_case_						)

       # Out features must be a list
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(('a', 'b')				,		(0, 1)				,		['a', 'b']						)

       # Out features must be a subset of stage names
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 1)				,		['a']						)

       # Out indices must be a list or tuple
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(snake_case_				,		0				,		['a', 'b']						)

       # Out indices must be a subset of stage names
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(snake_case_				,		(0, 1)				,		['a']						)

       # Out features and out indices must be the same length
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0,)				,		['a', 'b', 'c']						)

       # Out features should match out indices
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 2)				,		['a', 'b', 'c']						)

       # Out features and out indices should be in order
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['b', 'a']				,		(0, 1)				,		['a', 'b']						)

       # Check passes with valid inputs
       verify_out_features_out_indices(['a', 'b', 'd']				,		(0, 1, -1)				,		['a', 'b', 'c', 'd']						)






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				BackboneMixin()

       UpperCAmelCase_							:       Any       =				['a', 'b', 'c']
       UpperCAmelCase_							:       str       =				['a', 'c']
       UpperCAmelCase_							:       str       =				[0, 2]

       # Check that the output features and indices are set correctly
       self.assertEqual(backbone.out_features				,		['a', 'c']						)
       self.assertEqual(backbone.out_indices				,		[0, 2]						)

       # Check out features and indices are updated correctly
       UpperCAmelCase_							:       str       =				['a', 'b']
       self.assertEqual(backbone.out_features				,		['a', 'b']						)
       self.assertEqual(backbone.out_indices				,		[0, 1]						)

       UpperCAmelCase_							:       Optional[int]       =				[-3, -1]
       self.assertEqual(backbone.out_features				,		['a', 'c']						)
       self.assertEqual(backbone.out_indices				,		[-3, -1]						)






