


import unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM
@require_torch
class 				SCREAMING_SNAKE_CASE__						:
      def __init__( self       : Optional[Any]  ,							SCREAMING_SNAKE_CASE__       : List[Any]  ,							SCREAMING_SNAKE_CASE__       : Tuple=9_9  ,							SCREAMING_SNAKE_CASE__       : Tuple=1_3  ,							SCREAMING_SNAKE_CASE__       : str=1_6  ,							SCREAMING_SNAKE_CASE__       : List[str]=7  ,							SCREAMING_SNAKE_CASE__       : Union[str, Any]=True  ,							SCREAMING_SNAKE_CASE__       : List[Any]=True  ,							SCREAMING_SNAKE_CASE__       : str=True  ,							SCREAMING_SNAKE_CASE__       : Dict=False  ,							SCREAMING_SNAKE_CASE__       : List[str]=True  ,							SCREAMING_SNAKE_CASE__       : List[Any]=2  ,							SCREAMING_SNAKE_CASE__       : List[Any]=3_2  ,							SCREAMING_SNAKE_CASE__       : Tuple=4  ,							SCREAMING_SNAKE_CASE__       : Optional[Any]=4  ,							SCREAMING_SNAKE_CASE__       : int=3_0  ,							SCREAMING_SNAKE_CASE__       : Tuple=0  ,							SCREAMING_SNAKE_CASE__       : Optional[Any]=1  ,							SCREAMING_SNAKE_CASE__       : Optional[int]=2  ,							SCREAMING_SNAKE_CASE__       : Union[str, Any]=None  ,							)	->    str:
             a_      :	Optional[int]							=						parent
             a_      :	Union[str, Any]							=						batch_size
             a_      :	int							=						decoder_seq_length
             # For common tests
             a_      :	Tuple							=						self.decoder_seq_length
             a_      :	str							=						is_training
             a_      :	List[Any]							=						use_attention_mask
             a_      :	List[Any]							=						use_labels

             a_      :	Dict							=						vocab_size
             a_      :	Dict							=						d_model
             a_      :	List[Any]							=						d_model
             a_      :	Dict							=						decoder_layers
             a_      :	str							=						decoder_layers
             a_      :	int							=						decoder_ffn_dim
             a_      :	Any							=						decoder_attention_heads
             a_      :	str							=						decoder_attention_heads
             a_      :	Optional[Any]							=						eos_token_id
             a_      :	Optional[int]							=						bos_token_id
             a_      :	Dict							=						pad_token_id
             a_      :	Any							=						decoder_start_token_id
             a_      :	Optional[int]							=						use_cache
             a_      :	int							=						max_position_embeddings

             a_      :	Union[str, Any]							=						None
             a_      :	List[Any]							=						decoder_seq_length
             a_      :	int							=						2
             a_      :	Dict							=						1
      def  SCREAMING_SNAKE_CASE							( self       : List[Any]  )	->    Dict:
             a_      :	str							=						ids_tensor([self.batch_size, self.decoder_seq_length]  ,							self.vocab_size  )

             a_      :	List[Any]							=						None
             if self.use_attention_mask:
                    a_      :	List[Any]							=						ids_tensor([self.batch_size, self.decoder_seq_length]  ,							vocab_size=2  )

             a_      :	Dict							=						None
             if self.use_labels:
                    a_      :	int							=						ids_tensor([self.batch_size, self.decoder_seq_length]  ,							self.vocab_size  )

             a_      :	Optional[int]							=						TrOCRConfig(
                 vocab_size=self.vocab_size  ,							d_model=self.d_model  ,							decoder_layers=self.decoder_layers  ,							decoder_ffn_dim=self.decoder_ffn_dim  ,							decoder_attention_heads=self.decoder_attention_heads  ,							eos_token_id=self.eos_token_id  ,							bos_token_id=self.bos_token_id  ,							use_cache=self.use_cache  ,							pad_token_id=self.pad_token_id  ,							decoder_start_token_id=self.decoder_start_token_id  ,							max_position_embeddings=self.max_position_embeddings  ,							)

             return (config, input_ids, attention_mask, lm_labels)
      def  SCREAMING_SNAKE_CASE							( self       : Any  ,							SCREAMING_SNAKE_CASE__       : Any  ,							SCREAMING_SNAKE_CASE__       : Union[str, Any]  ,							SCREAMING_SNAKE_CASE__       : Optional[int]  ,							SCREAMING_SNAKE_CASE__       : Dict  ,							)	->    int:
             a_      :	Union[str, Any]							=						True
             a_      :	str							=						TrOCRDecoder(config=SCREAMING_SNAKE_CASE__  ).to(SCREAMING_SNAKE_CASE__  ).eval()
             a_      :	Tuple							=						input_ids[:2]

             input_ids[input_ids == 0] += 1
             # first forward pass
             a_      :	str							=						model(SCREAMING_SNAKE_CASE__  ,							use_cache=SCREAMING_SNAKE_CASE__  )
             a_      :	str							=						model(SCREAMING_SNAKE_CASE__  )
             a_      :	Union[str, Any]							=						model(SCREAMING_SNAKE_CASE__  ,							use_cache=SCREAMING_SNAKE_CASE__  )

             self.parent.assertTrue(len(SCREAMING_SNAKE_CASE__  ) == len(SCREAMING_SNAKE_CASE__  )  )
             self.parent.assertTrue(len(SCREAMING_SNAKE_CASE__  ) == len(SCREAMING_SNAKE_CASE__  ) + 1  )

             a_      :	Any							=						outputs['past_key_values']

             # create hypothetical next token and extent to next_input_ids
             a_      :	Optional[Any]							=						ids_tensor((2, 1)  ,							config.vocab_size - 1  ) + 1

             # append to next input_ids and
             a_      :	Tuple							=						torch.cat([input_ids, next_tokens]  ,							dim=-1  )

             a_      :	Dict							=						model(SCREAMING_SNAKE_CASE__  )['last_hidden_state']
             a_      :	Optional[Any]							=						model(SCREAMING_SNAKE_CASE__  ,							past_key_values=SCREAMING_SNAKE_CASE__  )['last_hidden_state']

             # select random slice
             a_      :	Optional[int]							=						ids_tensor((1,)  ,							output_from_past.shape[-1]  ).item()
             a_      :	List[str]							=						output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
             a_      :	Dict							=						output_from_past[:, 0, random_slice_idx].detach()

             # test that outputs are equal for slice
             assert torch.allclose(SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  ,							atol=1E-3  )

      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    Any:
             a_      :	Any							=						self.prepare_config_and_inputs()
             a_							,	a_							,	a_							,	a_      :	List[str]							=						config_and_inputs

             a_      :	Optional[int]							=						{'input_ids': input_ids, 'attention_mask': attention_mask}
             return config, inputs_dict
@require_torch
class 				SCREAMING_SNAKE_CASE__						(       lowercase__		,					lowercase__		,					lowercase__		,					unittest.TestCase    ):
      snake_case__       :  Dict       =		(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
      snake_case__       :  Optional[int]       =		(TrOCRForCausalLM,) if is_torch_available() else ()
      snake_case__       :  Union[str, Any]       =		{'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
      snake_case__       :  str       =		True
      snake_case__       :  List[Any]       =		False
      def  SCREAMING_SNAKE_CASE							( self       : int  )	->    Optional[int]:
             a_      :	int							=						TrOCRStandaloneDecoderModelTester(self  ,							is_training=SCREAMING_SNAKE_CASE__  )
             a_      :	Any							=						ConfigTester(self  ,							config_class=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Optional[Any]  )	->    Union[str, Any]:
             pass
      def  SCREAMING_SNAKE_CASE							( self       : int  )	->    Dict:
             pass
      def  SCREAMING_SNAKE_CASE							( self       : Any  )	->    List[str]:
             pass
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    str:
             self.config_tester.run_common_tests()
      def  SCREAMING_SNAKE_CASE							( self       : List[Any]  )	->    Optional[Any]:
             a_      :	str							=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_decoder_model_past(*SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : List[str]  )	->    Dict:
             return

      @unittest.skip('The model doesn\'t support left padding'  )  # and it's not used enough to be worth fixing :)
      def  SCREAMING_SNAKE_CASE							( self       : Dict  )	->    Tuple:
             pass






# 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  SCREAMING_SNAKE_CASE_		(    __A				:    List[Any]						,					__A				:    List[Any]						,					__A				:    List[Any]						,					__A				:    Optional[int]       )			->	Dict:

       """simple docstring"""

       a_      :	Optional[Any]							=						multiprocessing.Manager()
       a_      :	Tuple							=						manager.list()

       a_      :	Optional[int]							=						multiprocessing.Process(target=__A						,					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  SCREAMING_SNAKE_CASE_		(    __A				:    Tuple						,					__A				:    Optional[Any]						,					__A				:    Optional[Any]       )			->	Dict:

       """simple docstring"""

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

              a_      :	Any							=						shutil.rmtree
              a_      :	List[str]							=						os.rmdir
              a_      :	List[Any]							=						os.chdir

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

              # Run program.
              try:
                     a_      :	Any							=						{}
                     with swallow_io():
                            with time_limit(__A       ):
                                   exec(__A						,					__A       )
                     result.append('passed'       )
              except TimeoutException:
                     result.append('timed out'       )
              except BaseException as e:
                     result.append(F"""failed: {e}"""       )

              # Needed for cleaning up.
              a_      :	Tuple							=						rmtree
              a_      :	Any							=						rmdir
              a_      :	Optional[int]							=						chdir







@contextlib.contextmanager
def  SCREAMING_SNAKE_CASE_		(    __A				:    List[str]       )			->	str:

       """simple docstring"""

       def signal_handler(__A				:    List[str]						,					__A				:    Dict       ):
              raise TimeoutException('Timed out!'       )

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







@contextlib.contextmanager
def  SCREAMING_SNAKE_CASE_		(    )			->	Dict:

       """simple docstring"""

       a_      :	int							=						WriteOnlyStringIO()
       with contextlib.redirect_stdout(__A       ):
              with contextlib.redirect_stderr(__A       ):
                     with redirect_stdin(__A       ):
                            yield







@contextlib.contextmanager
def  SCREAMING_SNAKE_CASE_		(    )			->	Optional[int]:

       """simple docstring"""

       with tempfile.TemporaryDirectory() as dirname:
              with chdir(__A       ):
                     yield dirname
class 				SCREAMING_SNAKE_CASE__						(       lowercase__    ):
      pass
class 				SCREAMING_SNAKE_CASE__						(       io.StringIO    ):
      def  SCREAMING_SNAKE_CASE							( self       : List[Any]  ,							*SCREAMING_SNAKE_CASE__       : List[str]  ,							**SCREAMING_SNAKE_CASE__       : List[Any]  )	->    int:
             raise OSError
      def  SCREAMING_SNAKE_CASE							( self       : Tuple  ,							*SCREAMING_SNAKE_CASE__       : Tuple  ,							**SCREAMING_SNAKE_CASE__       : Dict  )	->    List[str]:
             raise OSError
      def  SCREAMING_SNAKE_CASE							( self       : int  ,							*SCREAMING_SNAKE_CASE__       : Dict  ,							**SCREAMING_SNAKE_CASE__       : List[Any]  )	->    Optional[Any]:
             raise OSError

      def  SCREAMING_SNAKE_CASE							( self       : Any  ,							*SCREAMING_SNAKE_CASE__       : Union[str, Any]  ,							**SCREAMING_SNAKE_CASE__       : List[Any]  )	->    List[Any]:
             return False
class 				SCREAMING_SNAKE_CASE__						(       contextlib._RedirectStream    ):  # type: ignore
      snake_case__       :  Optional[int]       =		'''stdin'''







@contextlib.contextmanager
def  SCREAMING_SNAKE_CASE_		(    __A				:    Tuple       )			->	Union[str, Any]:

       """simple docstring"""

       if root == ".":
              yield
              return
       a_      :	str							=						os.getcwd()
       os.chdir(__A       )
       try:
              yield
       except BaseException as exc:
              raise exc
       finally:
              os.chdir(__A       )







def  SCREAMING_SNAKE_CASE_		(    __A				:    Dict=None       )			->	Dict:

       """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

       a_      :	str							=						None
       a_      :	str							=						None

       import os

       a_      :	Optional[int]							=						'1'

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

       import shutil

       a_      :	Dict							=						None
       a_      :	Tuple							=						None
       a_      :	str							=						None

       import subprocess

       a_      :	str							=						None  # type: ignore

       a_      :	List[Any]							=						None

       import sys

       a_      :	Optional[int]							=						None
       a_      :	int							=						None
       a_      :	Any							=						None
       a_      :	str							=						None
       a_      :	str							=						None


