




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     ,		lowerCamelCase__     ,		lowerCamelCase__=99     ,		lowerCamelCase__=13     ,		lowerCamelCase__=16     ,		lowerCamelCase__=7     ,		lowerCamelCase__=True     ,		lowerCamelCase__=True     ,		lowerCamelCase__=True     ,		lowerCamelCase__=False     ,		lowerCamelCase__=True     ,		lowerCamelCase__=2     ,		lowerCamelCase__=32     ,		lowerCamelCase__=4     ,		lowerCamelCase__=4     ,		lowerCamelCase__=30     ,		lowerCamelCase__=0     ,		lowerCamelCase__=1     ,		lowerCamelCase__=2     ,		lowerCamelCase__=None     ,		)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       parent
							__lowerCamelCase									=       batch_size
							__lowerCamelCase									=       decoder_seq_length
							# For common tests
							__lowerCamelCase									=       self.decoder_seq_length
							__lowerCamelCase									=       is_training
							__lowerCamelCase									=       use_attention_mask
							__lowerCamelCase									=       use_labels

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

							__lowerCamelCase									=       None
							__lowerCamelCase									=       decoder_seq_length
							__lowerCamelCase									=       2
							__lowerCamelCase									=       1


			def  lowercase_       ( self				)       -> List[str]:



							'''simple docstring'''
							__lowerCamelCase									=       ids_tensor([self.batch_size, self.decoder_seq_length]     ,		self.vocab_size				)

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

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

							__lowerCamelCase									=       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     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       True
							__lowerCamelCase									=       TrOCRDecoder(config=lowerCamelCase__				).to(lowerCamelCase__				).eval()
							__lowerCamelCase									=       input_ids[:2]

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

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

							__lowerCamelCase									=       outputs['past_key_values']

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

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

							__lowerCamelCase									=       model(lowerCamelCase__				)['last_hidden_state']
							__lowerCamelCase									=       model(lowerCamelCase__     ,		past_key_values=lowerCamelCase__				)['last_hidden_state']

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

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






			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       self.prepare_config_and_inputs()
							__lowerCamelCase			,		__lowerCamelCase			,		__lowerCamelCase			,		__lowerCamelCase									=       config_and_inputs

							__lowerCamelCase									=       {'input_ids': input_ids, 'attention_mask': attention_mask}
							return config, inputs_dict







@require_torch
class        __lowerCAmelCase					(			__magic_name__     ,							__magic_name__     ,							__magic_name__     ,							unittest.TestCase					):

			"""simple docstring"""







			snake_case_  						=			(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
			snake_case_  						=			(TrOCRForCausalLM,) if is_torch_available() else ()
			snake_case_  						=			{'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
			snake_case_  						=			True
			snake_case_  						=			False


			def  lowercase_       ( self				)       -> List[str]:



							'''simple docstring'''
							__lowerCamelCase									=       TrOCRStandaloneDecoderModelTester(self     ,		is_training=lowerCamelCase__				)
							__lowerCamelCase									=       ConfigTester(self     ,		config_class=lowerCamelCase__				)


			def  lowercase_       ( self				)       -> Optional[Any]:



							'''simple docstring'''
							pass


			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							pass


			def  lowercase_       ( self				)       -> Optional[Any]:



							'''simple docstring'''
							pass


			def  lowercase_       ( self				)       -> List[str]:



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


			def  lowercase_       ( self				)       -> Optional[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*lowerCamelCase__				)


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



							'''simple docstring'''
							pass








from string import ascii_uppercase

__A								=       {str(ord(c) - 55): c for c in ascii_uppercase}







def      lowerCamelCase_						(			UpperCamelCase__							: int					,	UpperCamelCase__							: int )	->			str:





				"""simple docstring"""


				if isinstance(UpperCamelCase__					,	UpperCamelCase__ ):
								raise TypeError('int() can\'t convert non-string with explicit base' )
				if num < 0:
								raise ValueError('parameter must be positive int' )
				if isinstance(UpperCamelCase__					,	UpperCamelCase__ ):
								raise TypeError('\'str\' object cannot be interpreted as an integer' )
				if isinstance(UpperCamelCase__					,	UpperCamelCase__ ):
								raise TypeError('\'float\' object cannot be interpreted as an integer' )
				if base in (0, 1):
								raise ValueError('base must be >= 2' )
				if base > 36:
								raise ValueError('base must be <= 36' )
				__lowerCamelCase									=       ''
				__lowerCamelCase									=       0
				__lowerCamelCase									=       0
				while div != 1:
								__lowerCamelCase			,		__lowerCamelCase									=       divmod(UpperCamelCase__					,	UpperCamelCase__ )
								if base >= 11 and 9 < mod < 36:
												__lowerCamelCase									=       ALPHABET_VALUES[str(UpperCamelCase__ )]
								else:
												__lowerCamelCase									=       str(UpperCamelCase__ )
								new_value += actual_value
								__lowerCamelCase									=       num // base
								__lowerCamelCase									=       div
								if div == 0:
												return str(new_value[::-1] )
								elif div == 1:
												new_value += str(UpperCamelCase__ )
												return str(new_value[::-1] )

				return new_value[::-1]


if __name__ == "__main__":
					import doctest

					doctest.testmod()

					for base in range(2, 37):
										for num in range(10_00):
															assert int(decimal_to_any(num, base), base) == num, (
															    num,
															    base,
															    decimal_to_any(num, base),
															    int(decimal_to_any(num, base), base),
															)


