

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_      :




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

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

		__A :			Union[str, Any]		    =			None
		__A :			str		    =			decoder_seq_length
		__A :			Union[str, Any]		    =			2
		__A :			Tuple		    =			1




	def        lowerCAmelCase_       (     self						:     List[str]							):
		__A :			Optional[Any]		    =			ids_tensor([self.batch_size, self.decoder_seq_length]					,      self.vocab_size							)

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

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

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

		input_ids[input_ids == 0] += 1
		# first forward pass
		__A :			Any		    =			model(A_					,      use_cache=A_							)
		__A :			Dict		    =			model(A_							)
		__A :			Optional[int]		    =			model(A_					,      use_cache=A_							)

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

		__A :			int		    =			outputs["""past_key_values"""]

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

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

		__A :			str		    =			model(A_							)["""last_hidden_state"""]
		__A :			Tuple		    =			model(A_					,      past_key_values=A_							)["""last_hidden_state"""]

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

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




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		__A :			str		    =			self.prepare_config_and_inputs()
		__A    ,				__A    ,				__A    ,				__A :			str		    =			config_and_inputs

		__A :			List[Any]		    =			{"""input_ids""": input_ids, """attention_mask""": attention_mask}
		return config, inputs_dict






@require_torch
class      lowerCamelCase_      (	snake_case_						,      snake_case_						,      snake_case_						,      unittest.TestCase	):
	_lowercase		:   str     					=				(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
	_lowercase		:   Dict     					=				(TrOCRForCausalLM,) if is_torch_available() else ()
	_lowercase		:   Dict     					=				{'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
	_lowercase		:   str     					=				True
	_lowercase		:   Union[str, Any]     					=				False




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		__A :			Union[str, Any]		    =			TrOCRStandaloneDecoderModelTester(self					,      is_training=A_							)
		__A :			Optional[int]		    =			ConfigTester(self					,      config_class=A_							)




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




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




	def        lowerCAmelCase_       (     self						:     Optional[Any]							):
		pass




	def        lowerCAmelCase_       (     self						:     Optional[int]							):
		self.config_tester.run_common_tests()




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		__A :			int		    =			self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_decoder_model_past(*A_							)




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







'''simple docstring'''
import numpy as np







def 		A_(     A						:		str		,     A						:		Optional[Any]		,     A						:		Tuple		,     A						:		Optional[int]		,     A						:		str):
							UpperCamelCase   			=     int(np.ceil((x_end - xa) / h))
							UpperCamelCase   			=     np.zeros((n + 1,))
							UpperCamelCase   			=     ya
							UpperCamelCase   			=     xa

							for k in range(A):
														UpperCamelCase   			=     f(A		,     y[k])
														UpperCamelCase   			=     f(x + 0.5 * h		,     y[k] + 0.5 * h * ka)
														UpperCamelCase   			=     f(x + 0.5 * h		,     y[k] + 0.5 * h * ka)
														UpperCamelCase   			=     f(x + h		,     y[k] + h * ka)
														UpperCamelCase   			=     y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka)
														x += h

							return y


if __name__ == "__main__":
						import doctest

						doctest.testmod()




