



'''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 		lowerCamelCase__      :




					'''simple docstring'''



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



						'''simple docstring'''





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

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

						_lowercase					:       List[str]       =		None
						_lowercase					:       str       =		decoder_seq_length
						_lowercase					:       List[str]       =		2
						_lowercase					:       str       =		1




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



						'''simple docstring'''





						_lowercase					:       Tuple       =		ids_tensor([self.batch_size, self.decoder_seq_length]     ,  self.vocab_size					)

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

						_lowercase					:       Optional[Any]       =		None
						if self.use_labels:
							_lowercase					:       Optional[int]       =		ids_tensor([self.batch_size, self.decoder_seq_length]     ,  self.vocab_size					)

						_lowercase					:       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 						__UpperCAmelCase     (  self				:	Union[str, Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Tuple     ,  )  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       List[str]       =		True
						_lowercase					:       Union[str, Any]       =		TrOCRDecoder(config=UpperCamelCase_					).to(UpperCamelCase_					).eval()
						_lowercase					:       int       =		input_ids[:2]

						input_ids[input_ids == 0] += 1
						# first forward pass
						_lowercase					:       Optional[int]       =		model(UpperCamelCase_     ,  use_cache=UpperCamelCase_					)
						_lowercase					:       Optional[int]       =		model(UpperCamelCase_					)
						_lowercase					:       Optional[int]       =		model(UpperCamelCase_     ,  use_cache=UpperCamelCase_					)

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

						_lowercase					:       List[Any]       =		outputs['past_key_values']

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

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

						_lowercase					:       int       =		model(UpperCamelCase_					)['last_hidden_state']
						_lowercase					:       Dict       =		model(UpperCamelCase_     ,  past_key_values=UpperCamelCase_					)['last_hidden_state']

						# select random slice
						_lowercase					:       List[str]       =		ids_tensor((1,)     ,  output_from_past.shape[-1]					).item()
						_lowercase					:       Union[str, Any]       =		output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
						_lowercase					:       Optional[int]       =		output_from_past[:, 0, random_slice_idx].detach()

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




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



						'''simple docstring'''





						_lowercase					:       List[Any]       =		self.prepare_config_and_inputs()
						_lowercase  ,    _lowercase  ,    _lowercase  ,    _lowercase					:       Optional[int]       =		config_and_inputs

						_lowercase					:       int       =		{'input_ids': input_ids, 'attention_mask': attention_mask}
						return config, inputs_dict






@require_torch
class 		lowerCamelCase__      (						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 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Optional[Any]:



						'''simple docstring'''





						_lowercase					:       Tuple       =		TrOCRStandaloneDecoderModelTester(self     ,  is_training=UpperCamelCase_					)
						_lowercase					:       int       =		ConfigTester(self     ,  config_class=UpperCamelCase_					)




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



						'''simple docstring'''





						pass




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       str:



						'''simple docstring'''





						pass




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



						'''simple docstring'''





						pass




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



						'''simple docstring'''





						self.config_tester.run_common_tests()




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase_					)




					def 						__UpperCAmelCase     (  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 						__UpperCAmelCase     (  self				:	int					)  ->       List[Any]:



						'''simple docstring'''





						pass





'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_A				:    Optional[Any]     						={'''configuration_xlnet''': ['''XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLNetConfig''']}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    Tuple     						=['''XLNetTokenizer''']

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    str     						=['''XLNetTokenizerFast''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    Any     						=[
		    '''XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''XLNetForMultipleChoice''',
		    '''XLNetForQuestionAnswering''',
		    '''XLNetForQuestionAnsweringSimple''',
		    '''XLNetForSequenceClassification''',
		    '''XLNetForTokenClassification''',
		    '''XLNetLMHeadModel''',
		    '''XLNetModel''',
		    '''XLNetPreTrainedModel''',
		    '''load_tf_weights_in_xlnet''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    str     						=[
		    '''TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFXLNetForMultipleChoice''',
		    '''TFXLNetForQuestionAnsweringSimple''',
		    '''TFXLNetForSequenceClassification''',
		    '''TFXLNetForTokenClassification''',
		    '''TFXLNetLMHeadModel''',
		    '''TFXLNetMainLayer''',
		    '''TFXLNetModel''',
		    '''TFXLNetPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_xlnet import XLNetTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_xlnet_fast import XLNetTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_xlnet import (
				    XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
				    XLNetForMultipleChoice,
				    XLNetForQuestionAnswering,
				    XLNetForQuestionAnsweringSimple,
				    XLNetForSequenceClassification,
				    XLNetForTokenClassification,
				    XLNetLMHeadModel,
				    XLNetModel,
				    XLNetPreTrainedModel,
				    load_tf_weights_in_xlnet,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_xlnet import (
				    TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFXLNetForMultipleChoice,
				    TFXLNetForQuestionAnsweringSimple,
				    TFXLNetForSequenceClassification,
				    TFXLNetForTokenClassification,
				    TFXLNetLMHeadModel,
				    TFXLNetMainLayer,
				    TFXLNetModel,
				    TFXLNetPreTrainedModel,
				)

else:
		import sys

		_A				:    str     						=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
