


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




       '''simple docstring'''



       def __init__(						self				:				List[str]		,				__a				:				Optional[int]		,				__a				:				str=99		,				__a				:				Any=13		,				__a				:				Optional[int]=16		,				__a				:				Union[str, Any]=7		,				__a				:				Tuple=True		,				__a				:				Tuple=True		,				__a				:				int=True		,				__a				:				Tuple=False		,				__a				:				List[str]=True		,				__a				:				Optional[int]=2		,				__a				:				str=32		,				__a				:				List[str]=4		,				__a				:				Union[str, Any]=4		,				__a				:				int=30		,				__a				:				int=0		,				__a				:				Optional[int]=1		,				__a				:				Dict=2		,				__a				:				Optional[Any]=None		,				)					->Optional[Any]:
          lowerCamelCase_						:   str		= parent
          lowerCamelCase_						:   List[Any]		= batch_size
          lowerCamelCase_						:   Optional[Any]		= decoder_seq_length
          # For common tests
          lowerCamelCase_						:   List[str]		= self.decoder_seq_length
          lowerCamelCase_						:   Union[str, Any]		= is_training
          lowerCamelCase_						:   List[str]		= use_attention_mask
          lowerCamelCase_						:   Tuple		= use_labels

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

          lowerCamelCase_						:   List[Any]		= None
          lowerCamelCase_						:   Tuple		= decoder_seq_length
          lowerCamelCase_						:   str		= 2
          lowerCamelCase_						:   List[Any]		= 1



       def 				_lowerCAmelCase							(						self				:				Dict							)					->Any:
          lowerCamelCase_						:   List[str]		= ids_tensor([self.batch_size, self.decoder_seq_length]		,				self.vocab_size							)

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

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

          lowerCamelCase_						:   Union[str, Any]		= 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 				_lowerCAmelCase							(						self				:				Optional[Any]		,				__a				:				Optional[Any]		,				__a				:				int		,				__a				:				List[Any]		,				__a				:				str		,				)					->Optional[Any]:
          lowerCamelCase_						:   Tuple		= True
          lowerCamelCase_						:   int		= TrOCRDecoder(config=__a							).to(__a							).eval()
          lowerCamelCase_						:   Union[str, Any]		= input_ids[:2]

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

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

          lowerCamelCase_						:   str		= outputs["""past_key_values"""]

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

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

          lowerCamelCase_						:   Any		= model(__a							)["""last_hidden_state"""]
          lowerCamelCase_						:   Any		= model(__a		,				past_key_values=__a							)["""last_hidden_state"""]

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

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




       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->Union[str, Any]:
          lowerCamelCase_						:   Tuple		= self.prepare_config_and_inputs()
          lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_						:   List[Any]		= config_and_inputs

          lowerCamelCase_						:   Optional[int]		= {"""input_ids""": input_ids, """attention_mask""": attention_mask}
          return config, inputs_dict







@require_torch
class   SCREAMING_SNAKE_CASE_			(a__						,       a__						,       a__						,       unittest.TestCase						):




       '''simple docstring'''



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



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->List[Any]:
          lowerCamelCase_						:   Union[str, Any]		= TrOCRStandaloneDecoderModelTester(self		,				is_training=__a							)
          lowerCamelCase_						:   Tuple		= ConfigTester(self		,				config_class=__a							)



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Any:
          pass



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->int:
          pass



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Union[str, Any]:
          pass



       def 				_lowerCAmelCase							(						self				:				int							)					->Union[str, Any]:
          self.config_tester.run_common_tests()



       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->Optional[int]:
          lowerCamelCase_						:   Tuple		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_decoder_model_past(*__a							)



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




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









snake_case__				:	List[Any]					       =					[
    999,
    800,
    799,
    600,
    599,
    500,
    400,
    399,
    377,
    355,
    333,
    311,
    288,
    266,
    244,
    222,
    200,
    199,
    177,
    155,
    133,
    111,
    88,
    66,
    44,
    22,
    0,
]

snake_case__				:	Tuple					       =					[
    999,
    976,
    952,
    928,
    905,
    882,
    858,
    857,
    810,
    762,
    715,
    714,
    572,
    429,
    428,
    286,
    285,
    238,
    190,
    143,
    142,
    118,
    95,
    71,
    47,
    24,
    0,
]

snake_case__				:	List[Any]					       =					[
    999,
    988,
    977,
    966,
    955,
    944,
    933,
    922,
    911,
    900,
    899,
    879,
    859,
    840,
    820,
    800,
    799,
    766,
    733,
    700,
    699,
    650,
    600,
    599,
    500,
    499,
    400,
    399,
    350,
    300,
    299,
    266,
    233,
    200,
    199,
    179,
    159,
    140,
    120,
    100,
    99,
    88,
    77,
    66,
    55,
    44,
    33,
    22,
    11,
    0,
]

snake_case__				:	List[str]					       =					[
    999,
    995,
    992,
    989,
    985,
    981,
    978,
    975,
    971,
    967,
    964,
    961,
    957,
    956,
    951,
    947,
    942,
    937,
    933,
    928,
    923,
    919,
    914,
    913,
    908,
    903,
    897,
    892,
    887,
    881,
    876,
    871,
    870,
    864,
    858,
    852,
    846,
    840,
    834,
    828,
    827,
    820,
    813,
    806,
    799,
    792,
    785,
    784,
    777,
    770,
    763,
    756,
    749,
    742,
    741,
    733,
    724,
    716,
    707,
    699,
    698,
    688,
    677,
    666,
    656,
    655,
    645,
    634,
    623,
    613,
    612,
    598,
    584,
    570,
    569,
    555,
    541,
    527,
    526,
    505,
    484,
    483,
    462,
    440,
    439,
    396,
    395,
    352,
    351,
    308,
    307,
    264,
    263,
    220,
    219,
    176,
    132,
    88,
    44,
    0,
]

snake_case__				:	Any					       =					[
    999,
    997,
    995,
    992,
    990,
    988,
    986,
    984,
    981,
    979,
    977,
    975,
    972,
    970,
    968,
    966,
    964,
    961,
    959,
    957,
    956,
    954,
    951,
    949,
    946,
    944,
    941,
    939,
    936,
    934,
    931,
    929,
    926,
    924,
    921,
    919,
    916,
    914,
    913,
    910,
    907,
    905,
    902,
    899,
    896,
    893,
    891,
    888,
    885,
    882,
    879,
    877,
    874,
    871,
    870,
    867,
    864,
    861,
    858,
    855,
    852,
    849,
    846,
    843,
    840,
    837,
    834,
    831,
    828,
    827,
    824,
    821,
    817,
    814,
    811,
    808,
    804,
    801,
    798,
    795,
    791,
    788,
    785,
    784,
    780,
    777,
    774,
    770,
    766,
    763,
    760,
    756,
    752,
    749,
    746,
    742,
    741,
    737,
    733,
    730,
    726,
    722,
    718,
    714,
    710,
    707,
    703,
    699,
    698,
    694,
    690,
    685,
    681,
    677,
    673,
    669,
    664,
    660,
    656,
    655,
    650,
    646,
    641,
    636,
    632,
    627,
    622,
    618,
    613,
    612,
    607,
    602,
    596,
    591,
    586,
    580,
    575,
    570,
    569,
    563,
    557,
    551,
    545,
    539,
    533,
    527,
    526,
    519,
    512,
    505,
    498,
    491,
    484,
    483,
    474,
    466,
    457,
    449,
    440,
    439,
    428,
    418,
    407,
    396,
    395,
    381,
    366,
    352,
    351,
    330,
    308,
    307,
    286,
    264,
    263,
    242,
    220,
    219,
    176,
    175,
    132,
    131,
    88,
    44,
    0,
]

snake_case__				:	int					       =					[
    999,
    991,
    982,
    974,
    966,
    958,
    950,
    941,
    933,
    925,
    916,
    908,
    900,
    899,
    874,
    850,
    825,
    800,
    799,
    700,
    600,
    500,
    400,
    300,
    200,
    100,
    0,
]

snake_case__				:	Any					       =					[
    999,
    992,
    985,
    978,
    971,
    964,
    957,
    949,
    942,
    935,
    928,
    921,
    914,
    907,
    900,
    899,
    879,
    859,
    840,
    820,
    800,
    799,
    766,
    733,
    700,
    699,
    650,
    600,
    599,
    500,
    499,
    400,
    399,
    300,
    299,
    200,
    199,
    100,
    99,
    0,
]

snake_case__				:	Union[str, Any]					       =					[
    999,
    996,
    992,
    989,
    985,
    982,
    979,
    975,
    972,
    968,
    965,
    961,
    958,
    955,
    951,
    948,
    944,
    941,
    938,
    934,
    931,
    927,
    924,
    920,
    917,
    914,
    910,
    907,
    903,
    900,
    899,
    891,
    884,
    876,
    869,
    861,
    853,
    846,
    838,
    830,
    823,
    815,
    808,
    800,
    799,
    788,
    777,
    766,
    755,
    744,
    733,
    722,
    711,
    700,
    699,
    688,
    677,
    666,
    655,
    644,
    633,
    622,
    611,
    600,
    599,
    585,
    571,
    557,
    542,
    528,
    514,
    500,
    499,
    485,
    471,
    457,
    442,
    428,
    414,
    400,
    399,
    379,
    359,
    340,
    320,
    300,
    299,
    279,
    259,
    240,
    220,
    200,
    199,
    166,
    133,
    100,
    99,
    66,
    33,
    0,
]





