






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    :    Any			,		UpperCamelCase__    :    List[str]			,		UpperCamelCase__    :    Union[str, Any]=9_9			,		UpperCamelCase__    :    List[Any]=1_3			,		UpperCamelCase__    :    Union[str, Any]=1_6			,		UpperCamelCase__    :    Union[str, Any]=7			,		UpperCamelCase__    :    Optional[Any]=True			,		UpperCamelCase__    :    Tuple=True			,		UpperCamelCase__    :    Dict=True			,		UpperCamelCase__    :    str=False			,		UpperCamelCase__    :    Dict=True			,		UpperCamelCase__    :    List[str]=2			,		UpperCamelCase__    :    Optional[Any]=3_2			,		UpperCamelCase__    :    str=4			,		UpperCamelCase__    :    Optional[int]=4			,		UpperCamelCase__    :    int=3_0			,		UpperCamelCase__    :    Any=0			,		UpperCamelCase__    :    Optional[int]=1			,		UpperCamelCase__    :    List[Any]=2			,		UpperCamelCase__    :    Optional[Any]=None			,		):






											'''simple docstring'''







											snake_case__	  =       parent
											snake_case__	  =       batch_size
											snake_case__	  =       decoder_seq_length
											# For common tests
											snake_case__	  =       self.decoder_seq_length
											snake_case__	  =       is_training
											snake_case__	  =       use_attention_mask
											snake_case__	  =       use_labels

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

											snake_case__	  =       None
											snake_case__	  =       decoder_seq_length
											snake_case__	  =       2
											snake_case__	  =       1



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       ids_tensor([self.batch_size, self.decoder_seq_length]			,		self.vocab_size)

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

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

											snake_case__	  =       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 					__magic_name__							(      self    :    Tuple			,		UpperCamelCase__    :    Tuple			,		UpperCamelCase__    :    int			,		UpperCamelCase__    :    Tuple			,		UpperCamelCase__    :    Any			,		):






											'''simple docstring'''







											snake_case__	  =       True
											snake_case__	  =       TrOCRDecoder(config=__SCREAMING_SNAKE_CASE).to(__SCREAMING_SNAKE_CASE).eval()
											snake_case__	  =       input_ids[:2]

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

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

											snake_case__	  =       outputs["""past_key_values"""]

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

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

											snake_case__	  =       model(__SCREAMING_SNAKE_CASE)["""last_hidden_state"""]
											snake_case__	  =       model(__SCREAMING_SNAKE_CASE			,		past_key_values=__SCREAMING_SNAKE_CASE)["""last_hidden_state"""]

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

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



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       self.prepare_config_and_inputs()
											snake_case__  , snake_case__  , snake_case__  , snake_case__	  =       config_and_inputs

											snake_case__	  =       {"""input_ids""": input_ids, """attention_mask""": attention_mask}
											return config, inputs_dict


@require_torch
class    _lowerCAmelCase			(		__UpperCAmelCase     ,      __UpperCAmelCase     ,      __UpperCAmelCase     ,      unittest.TestCase      ):



				"""simple docstring"""



				_lowercase				:   Optional[int]      =  (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
				_lowercase				:   Optional[Any]      =  (TrOCRForCausalLM,) if is_torch_available() else ()
				_lowercase				:   int      =  {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
				_lowercase				:   Optional[Any]      =  True
				_lowercase				:   int      =  False



				def 					__magic_name__							(      self    :    List[str]):






											'''simple docstring'''







											snake_case__	  =       TrOCRStandaloneDecoderModelTester(self			,		is_training=__SCREAMING_SNAKE_CASE)
											snake_case__	  =       ConfigTester(self			,		config_class=__SCREAMING_SNAKE_CASE)



				def 					__magic_name__							(      self    :    List[Any]):






											'''simple docstring'''







											pass



				def 					__magic_name__							(      self    :    Optional[Any]):






											'''simple docstring'''







											pass



				def 					__magic_name__							(      self    :    Dict):






											'''simple docstring'''







											pass



				def 					__magic_name__							(      self    :    str):






											'''simple docstring'''







											self.config_tester.run_common_tests()



				def 					__magic_name__							(      self    :    List[Any]):






											'''simple docstring'''







											snake_case__	  =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_decoder_model_past(*__SCREAMING_SNAKE_CASE)



				def 					__magic_name__							(      self    :    List[Any]):






											'''simple docstring'''







											return



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






											'''simple docstring'''







											pass








def _UpperCAmelCase   (	a						:	int  ,					a						:	Optional[Any] ):
							# "extended trapezoidal rule"
							# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
							snake_case__	  =       (boundary[1] - boundary[0]) / steps
							snake_case__	  =       boundary[0]
							snake_case__	  =       boundary[1]
							snake_case__	  =       make_points(a  ,					a  ,					a )
							snake_case__	  =       0.0
							y += (h / 2.0) * f(a )
							for i in x_i:
														# print(i)
														y += h * f(a )
							y += (h / 2.0) * f(a )
							return y

def _UpperCAmelCase   (	a						:	Dict  ,					a						:	Union[str, Any]  ,					a						:	Union[str, Any] ):
							snake_case__	  =       a + h
							while x < (b - h):
														yield x
														snake_case__	  =       x + h

def _UpperCAmelCase   (	a						:	Any ):  # enter your function here
							snake_case__	  =       (x - 0) * (x - 0)
							return y

def _UpperCAmelCase   (	):
							snake_case__	  =       0.0  # Lower bound of integration
							snake_case__	  =       1.0  # Upper bound of integration
							snake_case__	  =       10.0  # define number of steps or resolution
							snake_case__	  =       [a, b]  # define boundary of integration
							snake_case__	  =       method_a(a  ,					a )
							print(F'''y = {y}''' )


if __name__ == "__main__":
	main()
