






'''simple docstring'''





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 				lowercase       :


    '''simple docstring'''

    def __init__(					self     :     Union[str, Any]				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Union[str, Any]=99				,     __lowerCamelCase     :     Tuple=13				,     __lowerCamelCase     :     List[Any]=16				,     __lowerCamelCase     :     int=7				,     __lowerCamelCase     :     Tuple=True				,     __lowerCamelCase     :     List[str]=True				,     __lowerCamelCase     :     Union[str, Any]=True				,     __lowerCamelCase     :     Any=False				,     __lowerCamelCase     :     str=True				,     __lowerCamelCase     :     Dict=2				,     __lowerCamelCase     :     Union[str, Any]=32				,     __lowerCamelCase     :     Optional[Any]=4				,     __lowerCamelCase     :     List[str]=4				,     __lowerCamelCase     :     List[Any]=30				,     __lowerCamelCase     :     List[Any]=0				,     __lowerCamelCase     :     int=1				,     __lowerCamelCase     :     Optional[Any]=2				,     __lowerCamelCase     :     int=None				,     )							->							List[Any]:

          '''simple docstring'''




          lowerCamelCase__  =     parent
          lowerCamelCase__  =     batch_size
          lowerCamelCase__  =     decoder_seq_length
          # For common tests
          lowerCamelCase__  =     self.decoder_seq_length
          lowerCamelCase__  =     is_training
          lowerCamelCase__  =     use_attention_mask
          lowerCamelCase__  =     use_labels

          lowerCamelCase__  =     vocab_size
          lowerCamelCase__  =     d_model
          lowerCamelCase__  =     d_model
          lowerCamelCase__  =     decoder_layers
          lowerCamelCase__  =     decoder_layers
          lowerCamelCase__  =     decoder_ffn_dim
          lowerCamelCase__  =     decoder_attention_heads
          lowerCamelCase__  =     decoder_attention_heads
          lowerCamelCase__  =     eos_token_id
          lowerCamelCase__  =     bos_token_id
          lowerCamelCase__  =     pad_token_id
          lowerCamelCase__  =     decoder_start_token_id
          lowerCamelCase__  =     use_cache
          lowerCamelCase__  =     max_position_embeddings

          lowerCamelCase__  =     None
          lowerCamelCase__  =     decoder_seq_length
          lowerCamelCase__  =     2
          lowerCamelCase__  =     1

    def    a__				(					self     :     List[str]     )							->							List[str]:

          '''simple docstring'''




          lowerCamelCase__  =     ids_tensor([self.batch_size, self.decoder_seq_length]				,     self.vocab_size     )

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

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

          lowerCamelCase__  =     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    a__				(					self     :     str				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     List[str]				,     __lowerCamelCase     :     str				,     )							->							Dict:

          '''simple docstring'''




          lowerCamelCase__  =     True
          lowerCamelCase__  =     TrOCRDecoder(config=_lowercase     ).to(_lowercase     ).eval()
          lowerCamelCase__  =     input_ids[:2]

          input_ids[input_ids == 0] += 1
          # first forward pass
          lowerCamelCase__  =     model(_lowercase				,     use_cache=_lowercase     )
          lowerCamelCase__  =     model(_lowercase     )
          lowerCamelCase__  =     model(_lowercase				,     use_cache=_lowercase     )

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

          lowerCamelCase__  =     outputs["""past_key_values"""]

          # create hypothetical next token and extent to next_input_ids
          lowerCamelCase__  =     ids_tensor((2, 1)				,     config.vocab_size - 1     ) + 1

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

          lowerCamelCase__  =     model(_lowercase     )["""last_hidden_state"""]
          lowerCamelCase__  =     model(_lowercase				,     past_key_values=_lowercase     )["""last_hidden_state"""]

          # select random slice
          lowerCamelCase__  =     ids_tensor((1,)				,     output_from_past.shape[-1]     ).item()
          lowerCamelCase__  =     output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
          lowerCamelCase__  =     output_from_past[:, 0, random_slice_idx].detach()

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




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

          '''simple docstring'''




          lowerCamelCase__  =     self.prepare_config_and_inputs()
          lowerCamelCase__  =     config_and_inputs

          lowerCamelCase__  =     {"""input_ids""": input_ids, """attention_mask""": attention_mask}
          return config, inputs_dict
@require_torch
class 				lowercase       (							SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__ ,       unittest.TestCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
    lowerCAmelCase__      				=							(TrOCRForCausalLM,) if is_torch_available() else ()
    lowerCAmelCase__      				=							{"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
    lowerCAmelCase__      				=							True
    lowerCAmelCase__      				=							False

    def    a__				(					self     :     Dict     )							->							Optional[Any]:

          '''simple docstring'''




          lowerCamelCase__  =     TrOCRStandaloneDecoderModelTester(self				,     is_training=_lowercase     )
          lowerCamelCase__  =     ConfigTester(self				,     config_class=_lowercase     )

    def    a__				(					self     :     Tuple     )							->							List[str]:

          '''simple docstring'''




          pass

    def    a__				(					self     :     int     )							->							List[str]:

          '''simple docstring'''




          pass

    def    a__				(					self     :     int     )							->							Tuple:

          '''simple docstring'''




          pass

    def    a__				(					self     :     Dict     )							->							Tuple:

          '''simple docstring'''




          self.config_tester.run_common_tests()

    def    a__				(					self     :     str     )							->							List[str]:

          '''simple docstring'''




          lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_decoder_model_past(*_lowercase     )

    def    a__				(					self     :     Union[str, Any]     )							->							Tuple:

          '''simple docstring'''




          return




    @unittest.skip("The model doesn't support left padding"     )  # and it's not used enough to be worth fixing :)
    def    a__				(					self     :     Union[str, Any]     )							->							int:

          '''simple docstring'''




          pass









'''simple docstring'''





from ..utils import DummyObject, requires_backends
class 				lowercase       (							metaclass=_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							["flax", "transformers"]

    def __init__(					self     :     List[str]				,     *__lowerCamelCase     :     int				,     **__lowerCamelCase     :     Any     )							->							Optional[int]:

     '''simple docstring'''




     requires_backends(self				,     ["flax", "transformers"]     )

    @classmethod
    def    a__				(					cls     :     Any				,     *__lowerCamelCase     :     Optional[int]				,     **__lowerCamelCase     :     Dict     )							->							str:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )




    @classmethod
    def    a__				(					cls     :     str				,     *__lowerCamelCase     :     str				,     **__lowerCamelCase     :     Any     )							->							str:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )
class 				lowercase       (							metaclass=_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							["flax", "transformers"]

    def __init__(					self     :     str				,     *__lowerCamelCase     :     List[Any]				,     **__lowerCamelCase     :     int     )							->							List[str]:

     '''simple docstring'''




     requires_backends(self				,     ["flax", "transformers"]     )

    @classmethod
    def    a__				(					cls     :     Dict				,     *__lowerCamelCase     :     Any				,     **__lowerCamelCase     :     str     )							->							Optional[int]:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )




    @classmethod
    def    a__				(					cls     :     Any				,     *__lowerCamelCase     :     int				,     **__lowerCamelCase     :     Dict     )							->							int:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )
class 				lowercase       (							metaclass=_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							["flax", "transformers"]

    def __init__(					self     :     Dict				,     *__lowerCamelCase     :     str				,     **__lowerCamelCase     :     List[Any]     )							->							str:

     '''simple docstring'''




     requires_backends(self				,     ["flax", "transformers"]     )

    @classmethod
    def    a__				(					cls     :     Optional[int]				,     *__lowerCamelCase     :     Optional[int]				,     **__lowerCamelCase     :     Optional[Any]     )							->							Dict:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )




    @classmethod
    def    a__				(					cls     :     Optional[int]				,     *__lowerCamelCase     :     Dict				,     **__lowerCamelCase     :     List[str]     )							->							Dict:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )
class 				lowercase       (							metaclass=_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							["flax", "transformers"]

    def __init__(					self     :     int				,     *__lowerCamelCase     :     List[str]				,     **__lowerCamelCase     :     int     )							->							Optional[int]:

     '''simple docstring'''




     requires_backends(self				,     ["flax", "transformers"]     )

    @classmethod
    def    a__				(					cls     :     Any				,     *__lowerCamelCase     :     Tuple				,     **__lowerCamelCase     :     str     )							->							List[str]:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )




    @classmethod
    def    a__				(					cls     :     List[Any]				,     *__lowerCamelCase     :     List[str]				,     **__lowerCamelCase     :     List[Any]     )							->							int:

     '''simple docstring'''




     requires_backends(cls				,     ["flax", "transformers"]     )

