



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





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







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  parent
							_UpperCamelCase   :						str												=  batch_size
							_UpperCamelCase   :						int												=  decoder_seq_length
							# For common tests
							_UpperCamelCase   :						int												=  self.decoder_seq_length
							_UpperCamelCase   :						str												=  is_training
							_UpperCamelCase   :						List[str]												=  use_attention_mask
							_UpperCamelCase   :						Tuple												=  use_labels

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

							_UpperCamelCase   :						List[str]												=  None
							_UpperCamelCase   :						Dict												=  decoder_seq_length
							_UpperCamelCase   :						List[Any]												=  2
							_UpperCamelCase   :						List[str]												=  1





	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Union[str, Any]												=  ids_tensor([self.batch_size, self.decoder_seq_length]					,self.vocab_size					)

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

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

							_UpperCamelCase   :						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       UpperCamelCase_    (					self   :							Tuple					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Optional[int]					,):







							'''simple docstring'''
							_UpperCamelCase   :						Union[str, Any]												=  True
							_UpperCamelCase   :						Any												=  TrOCRDecoder(config=lowerCamelCase__					).to(lowerCamelCase__					).eval()
							_UpperCamelCase   :						List[Any]												=  input_ids[:2]

							input_ids[input_ids == 0] += 1
							# first forward pass
							_UpperCamelCase   :						Dict												=  model(lowerCamelCase__					,use_cache=lowerCamelCase__					)
							_UpperCamelCase   :						int												=  model(lowerCamelCase__					)
							_UpperCamelCase   :						List[Any]												=  model(lowerCamelCase__					,use_cache=lowerCamelCase__					)

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

							_UpperCamelCase   :						Optional[int]												=  outputs['past_key_values']

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

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

							_UpperCamelCase   :						int												=  model(lowerCamelCase__					)['last_hidden_state']
							_UpperCamelCase   :						List[str]												=  model(lowerCamelCase__					,past_key_values=lowerCamelCase__					)['last_hidden_state']

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

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





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  self.prepare_config_and_inputs()
							_UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase   :						List[Any]												=  config_and_inputs

							_UpperCamelCase   :						str												=  {'input_ids': input_ids, 'attention_mask': attention_mask}
							return config, inputs_dict






@require_torch
class 				lowercase__      (				lowercase		,   lowercase		,   lowercase		,   unittest.TestCase  ):
	lowercase__    							=     (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
	lowercase__    							=     (TrOCRForCausalLM,) if is_torch_available() else ()
	lowercase__    							=     {"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
	lowercase__    							=     True
	lowercase__    							=     False





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  TrOCRStandaloneDecoderModelTester(self					,is_training=lowerCamelCase__					)
							_UpperCamelCase   :						Dict												=  ConfigTester(self					,config_class=lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							self.config_tester.run_common_tests()





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







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







							'''simple docstring'''
							pass







'''simple docstring'''
def A__       (	UpperCAmelCase_ = 1_0_0_0				):
						_UpperCamelCase   :						List[str]												=  3
						_UpperCamelCase   :						Any												=  0
						while a < n:
												if a % 3 == 0 or a % 5 == 0:
																		result += a
												elif a % 1_5 == 0:
																		result -= a
												a += 1
						return result


if __name__ == "__main__":
					print(F"""{solution() = }""")


