



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     :




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

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

							lowercase__   					=			None
							lowercase__   					=			decoder_seq_length
							lowercase__   					=			2
							lowercase__   					=			1




				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			ids_tensor([self.batch_size, self.decoder_seq_length],   self.vocab_size  )

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

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

							lowercase__   					=			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   :  str,   __lowercase   :  Union[str, Any],   __lowercase   :  Dict,   __lowercase   :  Union[str, Any],   __lowercase   :  List[str],   ):
							lowercase__   					=			True
							lowercase__   					=			TrOCRDecoder(config=__lowercase  ).to(__lowercase  ).eval()
							lowercase__   					=			input_ids[:2]

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

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

							lowercase__   					=			outputs["past_key_values"]

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

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

							lowercase__   					=			model(__lowercase  )["last_hidden_state"]
							lowercase__   					=			model(__lowercase,   past_key_values=__lowercase  )["last_hidden_state"]

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

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




				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			self.prepare_config_and_inputs()
							lowercase__					,    lowercase__					,    lowercase__					,    lowercase__   					=			config_and_inputs

							lowercase__   					=			{"input_ids": input_ids, "attention_mask": attention_mask}
							return config, inputs_dict
@require_torch
class 			_snake_case     (   lowercase__      ,		lowercase__      ,		lowercase__      ,		unittest.TestCase):
				UpperCamelCase__			:   List[str]       						=(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
				UpperCamelCase__			:   List[str]       						=(TrOCRForCausalLM,) if is_torch_available() else ()
				UpperCamelCase__			:   List[Any]       						={"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
				UpperCamelCase__			:   int       						=True
				UpperCamelCase__			:   List[Any]       						=False




				def 							A__ (  self   :  Dict  ):
							lowercase__   					=			TrOCRStandaloneDecoderModelTester(self,   is_training=__lowercase  )
							lowercase__   					=			ConfigTester(self,   config_class=__lowercase  )




				def 							A__ (  self   :  List[str]  ):
							pass




				def 							A__ (  self   :  List[Any]  ):
							pass




				def 							A__ (  self   :  Union[str, Any]  ):
							pass




				def 							A__ (  self   :  List[str]  ):
							self.config_tester.run_common_tests()




				def 							A__ (  self   :  Optional[int]  ):
							lowercase__   					=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*__lowercase  )




				def 							A__ (  self   :  Optional[int]  ):
							return




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









from __future__ import annotations



def __lowerCAmelCase	(       SCREAMING_SNAKE_CASE_					):
			return len(set(SCREAMING_SNAKE_CASE_					)					) == len(SCREAMING_SNAKE_CASE_					)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




