
"""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    :Any		, lowerCamelCase_    :List[Any]		, lowerCamelCase_    :Dict=99		, lowerCamelCase_    :List[str]=13		, lowerCamelCase_    :List[Any]=16		, lowerCamelCase_    :List[Any]=7		, lowerCamelCase_    :Tuple=True		, lowerCamelCase_    :Optional[Any]=True		, lowerCamelCase_    :Dict=True		, lowerCamelCase_    :Dict=False		, lowerCamelCase_    :int=True		, lowerCamelCase_    :Union[str, Any]=2		, lowerCamelCase_    :Optional[int]=32		, lowerCamelCase_    :Tuple=4		, lowerCamelCase_    :Tuple=4		, lowerCamelCase_    :Dict=30		, lowerCamelCase_    :Tuple=0		, lowerCamelCase_    :Dict=1		, lowerCamelCase_    :List[Any]=2		, lowerCamelCase_    :List[str]=None		, )							->			List[Any]:







			'''simple docstring'''




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

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

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





	def   __lowerCAmelCase (						self    :List[Any]					)							->			Dict:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= ids_tensor([self.batch_size, self.decoder_seq_length]		, self.vocab_size					)

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

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

			SCREAMING_SNAKE_CASE       :     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   __lowerCAmelCase (						self    :List[Any]		, lowerCamelCase_    :Dict		, lowerCamelCase_    :List[Any]		, lowerCamelCase_    :List[str]		, lowerCamelCase_    :List[str]		, )							->			Any:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Any				= True
			SCREAMING_SNAKE_CASE       :     Dict				= TrOCRDecoder(config=_snake_case					).to(_snake_case					).eval()
			SCREAMING_SNAKE_CASE       :     str				= input_ids[:2]

			input_ids[input_ids == 0] += 1
			# first forward pass
			SCREAMING_SNAKE_CASE       :     Any				= model(_snake_case		, use_cache=_snake_case					)
			SCREAMING_SNAKE_CASE       :     Dict				= model(_snake_case					)
			SCREAMING_SNAKE_CASE       :     Tuple				= 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					)

			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= outputs['''past_key_values''']

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

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

			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= model(_snake_case					)['''last_hidden_state''']
			SCREAMING_SNAKE_CASE       :     Optional[int]				= model(_snake_case		, past_key_values=_snake_case					)['''last_hidden_state''']

			# select random slice
			SCREAMING_SNAKE_CASE       :     List[Any]				= ids_tensor((1,)		, output_from_past.shape[-1]					).item()
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= 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   __lowerCAmelCase (						self    :Optional[int]					)							->			Optional[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.prepare_config_and_inputs()
			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     Tuple				= config_and_inputs

			SCREAMING_SNAKE_CASE       :     Optional[Any]				= {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
			return config, inputs_dict





@require_torch
class 				lowercase__(				UpperCAmelCase_		,						UpperCAmelCase_		,						UpperCAmelCase_		,						unittest.TestCase   ):
	'''simple docstring'''

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





	def   __lowerCAmelCase (						self    :Tuple					)							->			Tuple:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[str]				= TrOCRStandaloneDecoderModelTester(self		, is_training=_snake_case					)
			SCREAMING_SNAKE_CASE       :     List[Any]				= ConfigTester(self		, config_class=_snake_case					)





	def   __lowerCAmelCase (						self    :Tuple					)							->			List[Any]:







			'''simple docstring'''




			pass





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







			'''simple docstring'''




			pass





	def   __lowerCAmelCase (						self    :Optional[int]					)							->			Dict:







			'''simple docstring'''




			pass





	def   __lowerCAmelCase (						self    :List[Any]					)							->			Optional[Any]:







			'''simple docstring'''




			self.config_tester.run_common_tests()





	def   __lowerCAmelCase (						self    :Optional[Any]					)							->			List[str]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     str				= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_decoder_model_past(*_snake_case					)





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







			'''simple docstring'''




			return







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







			'''simple docstring'''




			pass







"""simple docstring"""






import os
import sys

lowerCamelCase__ :       List[Any]						     =			os.path.join(os.path.dirname(__file__), "src")
sys.path.append(SRC_DIR)


from transformers import (
    AutoConfig,
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForMaskedLM,
    AutoModelForQuestionAnswering,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    add_start_docstrings,
)


lowerCamelCase__ :       str						     =			[
    "torch",
    "numpy",
    "tokenizers",
    "filelock",
    "requests",
    "tqdm",
    "regex",
    "sentencepiece",
    "sacremoses",
    "importlib_metadata",
    "huggingface_hub",
]





@add_start_docstrings(AutoConfig.__doc__     )
def __A						(   *a_ :							Any		,		**a_ :							Union[str, Any]     )->							Dict:




		'''simple docstring'''






		return AutoConfig.from_pretrained(*a_		,		**a_     )





@add_start_docstrings(AutoTokenizer.__doc__     )
def __A						(   *a_ :							str		,		**a_ :							Union[str, Any]     )->							Union[str, Any]:




		'''simple docstring'''






		return AutoTokenizer.from_pretrained(*a_		,		**a_     )





@add_start_docstrings(AutoModel.__doc__     )
def __A						(   *a_ :							List[str]		,		**a_ :							int     )->							Dict:




		'''simple docstring'''






		return AutoModel.from_pretrained(*a_		,		**a_     )





@add_start_docstrings(AutoModelForCausalLM.__doc__     )
def __A						(   *a_ :							Any		,		**a_ :							Tuple     )->							Dict:




		'''simple docstring'''






		return AutoModelForCausalLM.from_pretrained(*a_		,		**a_     )





@add_start_docstrings(AutoModelForMaskedLM.__doc__     )
def __A						(   *a_ :							Dict		,		**a_ :							Optional[Any]     )->							Optional[int]:




		'''simple docstring'''






		return AutoModelForMaskedLM.from_pretrained(*a_		,		**a_     )





@add_start_docstrings(AutoModelForSequenceClassification.__doc__     )
def __A						(   *a_ :							Optional[int]		,		**a_ :							str     )->							Optional[int]:




		'''simple docstring'''






		return AutoModelForSequenceClassification.from_pretrained(*a_		,		**a_     )





@add_start_docstrings(AutoModelForQuestionAnswering.__doc__     )
def __A						(   *a_ :							List[str]		,		**a_ :							int     )->							List[Any]:




		'''simple docstring'''






		return AutoModelForQuestionAnswering.from_pretrained(*a_		,		**a_     )





