





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







		def __init__(    self ,						__snake_case ,						__snake_case=9_9 ,						__snake_case=1_3 ,						__snake_case=1_6 ,						__snake_case=7 ,						__snake_case=True ,						__snake_case=True ,						__snake_case=True ,						__snake_case=False ,						__snake_case=True ,						__snake_case=2 ,						__snake_case=3_2 ,						__snake_case=4 ,						__snake_case=4 ,						__snake_case=3_0 ,						__snake_case=0 ,						__snake_case=1 ,						__snake_case=2 ,						__snake_case=None ,						)      ->      Optional[Any]:


							'''simple docstring'''

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

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

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







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


							'''simple docstring'''

							_snake_case       :					Dict				       =   ids_tensor([self.batch_size, self.decoder_seq_length] ,						self.vocab_size				)

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

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

							_snake_case       :					List[str]				       =   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        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						)      ->      List[Any]:


							'''simple docstring'''

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

							input_ids[input_ids == 0] += 1
							# first forward pass
							_snake_case       :					List[str]				       =   model(__snake_case ,						use_cache=__snake_case				)
							_snake_case       :					str				       =   model(__snake_case				)
							_snake_case       :					Optional[int]				       =   model(__snake_case ,						use_cache=__snake_case				)

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

							_snake_case       :					Tuple				       =   outputs["past_key_values"]

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

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

							_snake_case       :					Any				       =   model(__snake_case				)["last_hidden_state"]
							_snake_case       :					Optional[int]				       =   model(__snake_case ,						past_key_values=__snake_case				)["last_hidden_state"]

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

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







		def        lowercase       (    self				)      ->      Dict:


							'''simple docstring'''

							_snake_case       :					int				       =   self.prepare_config_and_inputs()
							_snake_case       , _snake_case       , _snake_case       , _snake_case       :					Tuple				       =   config_and_inputs

							_snake_case       :					Optional[int]				       =   {"input_ids": input_ids, "attention_mask": attention_mask}
							return config, inputs_dict






@require_torch
class        _a(    __A	,   __A	,   __A	,   unittest.TestCase    ):
		lowerCamelCase__     :Tuple   				=		(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
		lowerCamelCase__     :List[Any]   				=		(TrOCRForCausalLM,) if is_torch_available() else ()
		lowerCamelCase__     :Optional[int]   				=		{'text-generation': TrOCRForCausalLM} if is_torch_available() else {}
		lowerCamelCase__     :List[str]   				=		True
		lowerCamelCase__     :Dict   				=		False







		def        lowercase       (    self				)      ->      List[Any]:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   TrOCRStandaloneDecoderModelTester(self ,						is_training=__snake_case				)
							_snake_case       :					Union[str, Any]				       =   ConfigTester(self ,						config_class=__snake_case				)







		def        lowercase       (    self				)      ->      List[Any]:


							'''simple docstring'''

							pass







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							pass







		def        lowercase       (    self				)      ->      Tuple:


							'''simple docstring'''

							pass







		def        lowercase       (    self				)      ->      Any:


							'''simple docstring'''

							self.config_tester.run_common_tests()







		def        lowercase       (    self				)      ->      Tuple:


							'''simple docstring'''

							_snake_case       :					Tuple				       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*__snake_case				)







		def        lowercase       (    self				)      ->      Any:


							'''simple docstring'''

							return







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


							'''simple docstring'''

							pass






import math

from numpy import inf
from scipy.integrate import quad







def    A				(							UpperCAmelCase				):
					if num <= 0:
										raise ValueError("math domain error"				)

					return quad(UpperCAmelCase  ,  0  ,  UpperCAmelCase  ,  args=(UpperCAmelCase)				)[0]







def    A				(							UpperCAmelCase  ,  UpperCAmelCase				):
					return math.pow(UpperCAmelCase  ,  z - 1				) * math.exp(-x				)


if __name__ == "__main__":
							from doctest import testmod

							testmod()