



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

						'''simple docstring'''




						def __init__(       self ,				_A ,				_A=99 ,				_A=13 ,				_A=16 ,				_A=7 ,				_A=True ,				_A=True ,				_A=True ,				_A=False ,				_A=True ,				_A=2 ,				_A=32 ,				_A=4 ,				_A=4 ,				_A=30 ,				_A=0 ,				_A=1 ,				_A=2 ,				_A=None ,				):







													'''simple docstring'''

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

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

													__SCREAMING_SNAKE_CASE										=   None
													__SCREAMING_SNAKE_CASE										=   decoder_seq_length
													__SCREAMING_SNAKE_CASE										=   2
													__SCREAMING_SNAKE_CASE										=   1






						def 			_A      (       self						):







													'''simple docstring'''

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

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

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

													__SCREAMING_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 			_A      (       self ,				_A ,				_A ,				_A ,				_A ,				):







													'''simple docstring'''

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

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

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

													__SCREAMING_SNAKE_CASE										=   outputs['past_key_values']

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

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

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

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

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






						def 			_A      (       self						):







													'''simple docstring'''

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

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







@require_torch
class   UpperCAmelCase_    (  UpperCamelCase_       ,       UpperCamelCase_       ,       UpperCamelCase_       ,       unittest.TestCase			):

						'''simple docstring'''




						UpperCamelCase__     :					str							    =    (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
						UpperCamelCase__     :					List[Any]							    =    (TrOCRForCausalLM,) if is_torch_available() else ()
						UpperCamelCase__     :					Any							    =    {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {}
						UpperCamelCase__     :					Dict							    =    True
						UpperCamelCase__     :					Tuple							    =    False






						def 			_A      (       self						):







													'''simple docstring'''

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






						def 			_A      (       self						):







													'''simple docstring'''

													pass






						def 			_A      (       self						):







													'''simple docstring'''

													pass






						def 			_A      (       self						):







													'''simple docstring'''

													pass






						def 			_A      (       self						):







													'''simple docstring'''

													self.config_tester.run_common_tests()






						def 			_A      (       self						):







													'''simple docstring'''

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






						def 			_A      (       self						):







													'''simple docstring'''

													return






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







													'''simple docstring'''

													pass












from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A								=       logging.get_logger(__name__)

__A								=       {
    "facebook/vit-mae-base": "https://huggingface.co/facebook/vit-mae-base/resolve/main/config.json",
    # See all ViT MAE models at https://huggingface.co/models?filter=vit-mae
}







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			'''vit_mae'''


			def __init__( self     ,		lowerCamelCase__=768     ,		lowerCamelCase__=12     ,		lowerCamelCase__=12     ,		lowerCamelCase__=3_072     ,		lowerCamelCase__="gelu"     ,		lowerCamelCase__=0.0     ,		lowerCamelCase__=0.0     ,		lowerCamelCase__=0.02     ,		lowerCamelCase__=1e-12     ,		lowerCamelCase__=224     ,		lowerCamelCase__=16     ,		lowerCamelCase__=3     ,		lowerCamelCase__=True     ,		lowerCamelCase__=16     ,		lowerCamelCase__=512     ,		lowerCamelCase__=8     ,		lowerCamelCase__=2_048     ,		lowerCamelCase__=0.75     ,		lowerCamelCase__=False     ,		**lowerCamelCase__     ,		)       -> Optional[int]:



							'''simple docstring'''
							super().__init__(**lowerCamelCase__				)

							__lowerCamelCase									=       hidden_size
							__lowerCamelCase									=       num_hidden_layers
							__lowerCamelCase									=       num_attention_heads
							__lowerCamelCase									=       intermediate_size
							__lowerCamelCase									=       hidden_act
							__lowerCamelCase									=       hidden_dropout_prob
							__lowerCamelCase									=       attention_probs_dropout_prob
							__lowerCamelCase									=       initializer_range
							__lowerCamelCase									=       layer_norm_eps
							__lowerCamelCase									=       image_size
							__lowerCamelCase									=       patch_size
							__lowerCamelCase									=       num_channels
							__lowerCamelCase									=       qkv_bias
							__lowerCamelCase									=       decoder_num_attention_heads
							__lowerCamelCase									=       decoder_hidden_size
							__lowerCamelCase									=       decoder_num_hidden_layers
							__lowerCamelCase									=       decoder_intermediate_size
							__lowerCamelCase									=       mask_ratio
							__lowerCamelCase									=       norm_pix_loss


