





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





 """simple docstring"""


 def __init__(    self					,						__lowerCamelCase					,						__lowerCamelCase=99					,						__lowerCamelCase=13					,						__lowerCamelCase=16					,						__lowerCamelCase=7					,						__lowerCamelCase=True					,						__lowerCamelCase=True					,						__lowerCamelCase=True					,						__lowerCamelCase=False					,						__lowerCamelCase=True					,						__lowerCamelCase=2					,						__lowerCamelCase=32					,						__lowerCamelCase=4					,						__lowerCamelCase=4					,						__lowerCamelCase=30					,						__lowerCamelCase=0					,						__lowerCamelCase=1					,						__lowerCamelCase=2					,						__lowerCamelCase=None					,						):



     '''simple docstring'''




     __A    :						List[Any]						 =     parent
     __A    :						int						 =     batch_size
     __A    :						List[str]						 =     decoder_seq_length
     # For common tests
     __A    :						Any						 =     self.decoder_seq_length
     __A    :						List[str]						 =     is_training
     __A    :						Dict						 =     use_attention_mask
     __A    :						List[str]						 =     use_labels

     __A    :						Dict						 =     vocab_size
     __A    :						Tuple						 =     d_model
     __A    :						Optional[int]						 =     d_model
     __A    :						Dict						 =     decoder_layers
     __A    :						Union[str, Any]						 =     decoder_layers
     __A    :						int						 =     decoder_ffn_dim
     __A    :						Optional[int]						 =     decoder_attention_heads
     __A    :						Any						 =     decoder_attention_heads
     __A    :						Optional[Any]						 =     eos_token_id
     __A    :						Tuple						 =     bos_token_id
     __A    :						Tuple						 =     pad_token_id
     __A    :						Optional[Any]						 =     decoder_start_token_id
     __A    :						str						 =     use_cache
     __A    :						Tuple						 =     max_position_embeddings

     __A    :						Optional[int]						 =     None
     __A    :						Any						 =     decoder_seq_length
     __A    :						Tuple						 =     2
     __A    :						Optional[int]						 =     1

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						List[Any]						 =     ids_tensor([self.batch_size, self.decoder_seq_length]					,						self.vocab_size  )

     __A    :						List[str]						 =     None
     if self.use_attention_mask:
         __A    :						Dict						 =     ids_tensor([self.batch_size, self.decoder_seq_length]					,						vocab_size=2  )

     __A    :						Tuple						 =     None
     if self.use_labels:
         __A    :						List[Any]						 =     ids_tensor([self.batch_size, self.decoder_seq_length]					,						self.vocab_size  )

     __A    :						Dict						 =     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 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase					,						):



     '''simple docstring'''




     __A    :						Dict						 =     True
     __A    :						Any						 =     TrOCRDecoder(config=__lowerCamelCase  ).to(__lowerCamelCase  ).eval()
     __A    :						int						 =     input_ids[:2]

     input_ids[input_ids == 0] += 1
     # first forward pass
     __A    :						List[Any]						 =     model(__lowerCamelCase					,						use_cache=__lowerCamelCase  )
     __A    :						Optional[Any]						 =     model(__lowerCamelCase  )
     __A    :						Optional[int]						 =     model(__lowerCamelCase					,						use_cache=__lowerCamelCase  )

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

     __A    :						Any						 =     outputs['''past_key_values''']

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

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

     __A    :						Dict						 =     model(__lowerCamelCase  )['''last_hidden_state''']
     __A    :						List[str]						 =     model(__lowerCamelCase					,						past_key_values=__lowerCamelCase  )['''last_hidden_state''']

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

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







 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Union[str, Any]						 =     self.prepare_config_and_inputs()
     __A ,       __A ,       __A ,       __A    :						List[str]						 =     config_and_inputs

     __A    :						Dict						 =     {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
     return config, inputs_dict






@require_torch
class 		__snake_case					(		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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						str						 =     TrOCRStandaloneDecoderModelTester(self					,						is_training=__lowerCamelCase  )
     __A    :						Dict						 =     ConfigTester(self					,						config_class=__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     pass

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     pass

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     pass

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Optional[int]						 =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past(*__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     return







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



     '''simple docstring'''




     pass











"""simple docstring"""



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a_ =			logging.get_logger(__name__)

a_ =			{
    """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""",
    """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""",
    """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""",
    """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""",
    """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""",
    """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""",
}






class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 _lowerCamelCase        =     """roberta"""

 def __init__(    self					,						__lowerCamelCase=5_0265					,						__lowerCamelCase=768					,						__lowerCamelCase=12					,						__lowerCamelCase=12					,						__lowerCamelCase=3072					,						__lowerCamelCase="gelu"					,						__lowerCamelCase=0.1					,						__lowerCamelCase=0.1					,						__lowerCamelCase=512					,						__lowerCamelCase=2					,						__lowerCamelCase=0.0_2					,						__lowerCamelCase=1e-1_2					,						__lowerCamelCase=1					,						__lowerCamelCase=0					,						__lowerCamelCase=2					,						__lowerCamelCase="absolute"					,						__lowerCamelCase=True					,						__lowerCamelCase=None					,						**__lowerCamelCase					,						):



     '''simple docstring'''




     super().__init__(pad_token_id=__lowerCamelCase					,						bos_token_id=__lowerCamelCase					,						eos_token_id=__lowerCamelCase					,						**__lowerCamelCase  )

     __A    :						str						 =     vocab_size
     __A    :						Any						 =     hidden_size
     __A    :						Any						 =     num_hidden_layers
     __A    :						int						 =     num_attention_heads
     __A    :						List[Any]						 =     hidden_act
     __A    :						Any						 =     intermediate_size
     __A    :						List[str]						 =     hidden_dropout_prob
     __A    :						List[Any]						 =     attention_probs_dropout_prob
     __A    :						Optional[Any]						 =     max_position_embeddings
     __A    :						str						 =     type_vocab_size
     __A    :						int						 =     initializer_range
     __A    :						Tuple						 =     layer_norm_eps
     __A    :						str						 =     position_embedding_type
     __A    :						int						 =     use_cache
     __A    :						Optional[int]						 =     classifier_dropout






class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 @property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     if self.task == "multiple-choice":
         __A    :						List[Any]						 =     {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
     else:
         __A    :						Optional[Any]						 =     {0: '''batch''', 1: '''sequence'''}
     return OrderedDict(
         [
             ('''input_ids''', dynamic_axis),
             ('''attention_mask''', dynamic_axis),
         ]  )




