






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			:
    """simple docstring"""

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

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

           __UpperCAmelCase     				=None
           __UpperCAmelCase     				=decoder_seq_length
           __UpperCAmelCase     				=2
           __UpperCAmelCase     				=1



    def _a							(				self      :     str      )      ->							Tuple:
           __UpperCAmelCase     				=ids_tensor([self.batch_size, self.decoder_seq_length] ,     self.vocab_size      )

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

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

           __UpperCAmelCase     				=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      :     Tuple ,     __SCREAMING_SNAKE_CASE      :     Dict ,     __SCREAMING_SNAKE_CASE      :     Dict ,     __SCREAMING_SNAKE_CASE      :     Optional[int] ,     __SCREAMING_SNAKE_CASE      :     Optional[Any] ,     )      ->							Optional[Any]:
           __UpperCAmelCase     				=True
           __UpperCAmelCase     				=TrOCRDecoder(config=__SCREAMING_SNAKE_CASE      ).to(__SCREAMING_SNAKE_CASE      ).eval()
           __UpperCAmelCase     				=input_ids[:2]

           input_ids[input_ids == 0] += 1
           # first forward pass
           __UpperCAmelCase     				=model(__SCREAMING_SNAKE_CASE ,     use_cache=__SCREAMING_SNAKE_CASE      )
           __UpperCAmelCase     				=model(__SCREAMING_SNAKE_CASE      )
           __UpperCAmelCase     				=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      )

           __UpperCAmelCase     				=outputs["""past_key_values"""]

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

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

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

           # select random slice
           __UpperCAmelCase     				=ids_tensor((1,) ,     output_from_past.shape[-1]      ).item()
           __UpperCAmelCase     				=output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
           __UpperCAmelCase     				=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 _a							(				self      :     Dict      )      ->							List[Any]:
           __UpperCAmelCase     				=self.prepare_config_and_inputs()
           __UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase     				=config_and_inputs

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




@require_torch
class 				_A			(     UpperCamelCase					,					UpperCamelCase					,					UpperCamelCase					,					unittest.TestCase				):
    """simple docstring"""

    lowerCamelCase	:							List[str]				= (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
    lowerCamelCase	:							Dict				= (TrOCRForCausalLM,) if is_torch_available() else ()
    lowerCamelCase	:							Optional[int]				= {'text-generation': TrOCRForCausalLM} if is_torch_available() else {}
    lowerCamelCase	:							int				= True
    lowerCamelCase	:							List[str]				= False



    def _a							(				self      :     Dict      )      ->							Dict:
           __UpperCAmelCase     				=TrOCRStandaloneDecoderModelTester(self ,     is_training=__SCREAMING_SNAKE_CASE      )
           __UpperCAmelCase     				=ConfigTester(self ,     config_class=__SCREAMING_SNAKE_CASE      )



    def _a							(				self      :     Tuple      )      ->							Any:
           pass



    def _a							(				self      :     Dict      )      ->							Union[str, Any]:
           pass



    def _a							(				self      :     List[Any]      )      ->							Any:
           pass



    def _a							(				self      :     Tuple      )      ->							Optional[Any]:
           self.config_tester.run_common_tests()



    def _a							(				self      :     Any      )      ->							Optional[Any]:
           __UpperCAmelCase     				=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_decoder_model_past(*__SCREAMING_SNAKE_CASE      )



    def _a							(				self      :     List[Any]      )      ->							Tuple:
           return



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











'''simple docstring'''




import random



def 		__A		(  lowerCamelCase_	):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE :				str		=			num - 1
		SCREAMING_SNAKE_CASE :				Union[str, Any]		=			0

		while s % 2 == 0:
				SCREAMING_SNAKE_CASE :				List[str]		=			s // 2
				t += 1

		for _ in range(5	):
				SCREAMING_SNAKE_CASE :				List[Any]		=			random.randrange(2			,			num - 1	)
				SCREAMING_SNAKE_CASE :				List[Any]		=			pow(lowerCamelCase_			,			lowerCamelCase_			,			lowerCamelCase_	)
				if v != 1:
						SCREAMING_SNAKE_CASE :				Tuple		=			0
						while v != (num - 1):
								if i == t - 1:
										return False
								else:
										SCREAMING_SNAKE_CASE :				Any		=			i + 1
										SCREAMING_SNAKE_CASE :				str		=			(v**2) % num
		return True



def 		__A		(  lowerCamelCase_	):


		"""simple docstring"""



		if num < 2:
				return False

		SCREAMING_SNAKE_CASE :				List[str]		=			[
		    2,
		    3,
		    5,
		    7,
		    11,
		    13,
		    17,
		    19,
		    23,
		    29,
		    31,
		    37,
		    41,
		    43,
		    47,
		    53,
		    59,
		    61,
		    67,
		    71,
		    73,
		    79,
		    83,
		    89,
		    97,
		    1_01,
		    1_03,
		    1_07,
		    1_09,
		    1_13,
		    1_27,
		    1_31,
		    1_37,
		    1_39,
		    1_49,
		    1_51,
		    1_57,
		    1_63,
		    1_67,
		    1_73,
		    1_79,
		    1_81,
		    1_91,
		    1_93,
		    1_97,
		    1_99,
		    2_11,
		    2_23,
		    2_27,
		    2_29,
		    2_33,
		    2_39,
		    2_41,
		    2_51,
		    2_57,
		    2_63,
		    2_69,
		    2_71,
		    2_77,
		    2_81,
		    2_83,
		    2_93,
		    3_07,
		    3_11,
		    3_13,
		    3_17,
		    3_31,
		    3_37,
		    3_47,
		    3_49,
		    3_53,
		    3_59,
		    3_67,
		    3_73,
		    3_79,
		    3_83,
		    3_89,
		    3_97,
		    4_01,
		    4_09,
		    4_19,
		    4_21,
		    4_31,
		    4_33,
		    4_39,
		    4_43,
		    4_49,
		    4_57,
		    4_61,
		    4_63,
		    4_67,
		    4_79,
		    4_87,
		    4_91,
		    4_99,
		    5_03,
		    5_09,
		    5_21,
		    5_23,
		    5_41,
		    5_47,
		    5_57,
		    5_63,
		    5_69,
		    5_71,
		    5_77,
		    5_87,
		    5_93,
		    5_99,
		    6_01,
		    6_07,
		    6_13,
		    6_17,
		    6_19,
		    6_31,
		    6_41,
		    6_43,
		    6_47,
		    6_53,
		    6_59,
		    6_61,
		    6_73,
		    6_77,
		    6_83,
		    6_91,
		    7_01,
		    7_09,
		    7_19,
		    7_27,
		    7_33,
		    7_39,
		    7_43,
		    7_51,
		    7_57,
		    7_61,
		    7_69,
		    7_73,
		    7_87,
		    7_97,
		    8_09,
		    8_11,
		    8_21,
		    8_23,
		    8_27,
		    8_29,
		    8_39,
		    8_53,
		    8_57,
		    8_59,
		    8_63,
		    8_77,
		    8_81,
		    8_83,
		    8_87,
		    9_07,
		    9_11,
		    9_19,
		    9_29,
		    9_37,
		    9_41,
		    9_47,
		    9_53,
		    9_67,
		    9_71,
		    9_77,
		    9_83,
		    9_91,
		    9_97,
		]

		if num in low_primes:
				return True

		for prime in low_primes:
				if (num % prime) == 0:
						return False

		return rabin_miller(lowerCamelCase_	)



def 		__A		(  lowerCamelCase_ = 10_24	):


		"""simple docstring"""



		while True:
				SCREAMING_SNAKE_CASE :				Optional[Any]		=			random.randrange(2 ** (keysize - 1)			,			2 ** (keysize)	)
				if is_prime_low_num(lowerCamelCase_	):
						return num


if __name__ == "__main__":
							__UpperCAmelCase            =       generate_large_prime()
							print(("""Prime number:""", num))
							print(("""is_prime_low_num:""", is_prime_low_num(num)))



