



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      __lowerCAmelCase     :
					"""simple docstring"""






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






								"""simple docstring"""


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

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

								A__       =    None
								A__       =    decoder_seq_length
								A__       =    2
								A__       =    1



					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


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

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

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

								A__       =    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					:       List[Any]				,					_snake_case					:       Dict				,					_snake_case					:       List[Any]				,					_snake_case					:       List[str]				,					_snake_case					:       List[str]				,					):






								"""simple docstring"""


								A__       =    True
								A__       =    TrOCRDecoder(config=_snake_case					).to(_snake_case					).eval()
								A__       =    input_ids[:2]

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

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

								A__       =    outputs['past_key_values']

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

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

								A__       =    model(_snake_case					)['last_hidden_state']
								A__       =    model(_snake_case				,					past_key_values=_snake_case					)['last_hidden_state']

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

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



					def     _a							(     self					:       Optional[int]					):






								"""simple docstring"""


								A__       =    self.prepare_config_and_inputs()
								A__     , A__     , A__     , A__       =    config_and_inputs

								A__       =    {'input_ids': input_ids, 'attention_mask': attention_mask}
								return config, inputs_dict







@require_torch
class      __lowerCAmelCase     (		UpperCAmelCase_							,     UpperCAmelCase_							,     UpperCAmelCase_							,     unittest.TestCase      ):
					"""simple docstring"""






					A__				:		Any			       =    (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
					A__				:		List[str]			       =    (TrOCRForCausalLM,) if is_torch_available() else ()
					A__				:		List[str]			       =    {"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
					A__				:		Dict			       =    True
					A__				:		str			       =    False



					def     _a							(     self					:       Tuple					):






								"""simple docstring"""


								A__       =    TrOCRStandaloneDecoderModelTester(self				,					is_training=_snake_case					)
								A__       =    ConfigTester(self				,					config_class=_snake_case					)



					def     _a							(     self					:       Tuple					):






								"""simple docstring"""


								pass



					def     _a							(     self					:       Optional[Any]					):






								"""simple docstring"""


								pass



					def     _a							(     self					:       Optional[int]					):






								"""simple docstring"""


								pass



					def     _a							(     self					:       List[Any]					):






								"""simple docstring"""


								self.config_tester.run_common_tests()



					def     _a							(     self					:       Optional[Any]					):






								"""simple docstring"""


								A__       =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_decoder_model_past(*_snake_case					)



					def     _a							(     self					:       Any					):






								"""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]					):






								"""simple docstring"""


								pass









from typing import TYPE_CHECKING

from ..utils import _LazyModule


SCREAMING_SNAKE_CASE__       =      {
    '''config''': [
        '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''',
        '''OnnxConfig''',
        '''OnnxConfigWithPast''',
        '''OnnxSeq2SeqConfigWithPast''',
        '''PatchingSpec''',
    ],
    '''convert''': ['''export''', '''validate_model_outputs'''],
    '''features''': ['''FeaturesManager'''],
    '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''],
}


if TYPE_CHECKING:
							from .config import (
							    EXTERNAL_DATA_FORMAT_SIZE_LIMIT,
							    OnnxConfig,
							    OnnxConfigWithPast,
							    OnnxSeqaSeqConfigWithPast,
							    PatchingSpec,
							)
							from .convert import export, validate_model_outputs
							from .features import FeaturesManager
							from .utils import ParameterFormat, compute_serialized_parameters_size

else:
							import sys

							SCREAMING_SNAKE_CASE__       =      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




