





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







     '''simple docstring'''




     def __init__(				self       :			Dict ,	UpperCamelCase_       :			int ,	UpperCamelCase_       :			Any=99 ,	UpperCamelCase_       :			int=13 ,	UpperCamelCase_       :			List[str]=16 ,	UpperCamelCase_       :			Tuple=7 ,	UpperCamelCase_       :			List[str]=True ,	UpperCamelCase_       :			List[str]=True ,	UpperCamelCase_       :			str=True ,	UpperCamelCase_       :			List[Any]=False ,	UpperCamelCase_       :			List[Any]=True ,	UpperCamelCase_       :			int=2 ,	UpperCamelCase_       :			str=32 ,	UpperCamelCase_       :			str=4 ,	UpperCamelCase_       :			Dict=4 ,	UpperCamelCase_       :			int=30 ,	UpperCamelCase_       :			Optional[int]=0 ,	UpperCamelCase_       :			str=1 ,	UpperCamelCase_       :			Any=2 ,	UpperCamelCase_       :			Union[str, Any]=None ,	):


         """simple docstring"""



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

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

         __A         =	None
         __A         =	decoder_seq_length
         __A         =	2
         __A         =	1




     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



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

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

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

         __A         =	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 		lowerCAmelCase_     (				self       :			Optional[Any] ,	UpperCamelCase_       :			Tuple ,	UpperCamelCase_       :			List[str] ,	UpperCamelCase_       :			Tuple ,	UpperCamelCase_       :			str ,	):


         """simple docstring"""



         __A         =	True
         __A         =	TrOCRDecoder(config=UpperCamelCase_				).to(UpperCamelCase_				).eval()
         __A         =	input_ids[:2]

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

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

         __A         =	outputs["""past_key_values"""]

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

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

         __A         =	model(UpperCamelCase_				)["""last_hidden_state"""]
         __A         =	model(UpperCamelCase_ ,	past_key_values=UpperCamelCase_				)["""last_hidden_state"""]

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

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





     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



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

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


@require_torch
class       __lowercase      (	lowercase_			,      lowercase_			,      lowercase_			,      unittest.TestCase  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE   						=				(TrOCRForCausalLM,) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE   						=				{"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
     SCREAMING_SNAKE_CASE   						=				True
     SCREAMING_SNAKE_CASE   						=				False




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         __A         =	TrOCRStandaloneDecoderModelTester(self ,	is_training=UpperCamelCase_				)
         __A         =	ConfigTester(self ,	config_class=UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			int				):


         """simple docstring"""



         pass




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         pass




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         pass




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         self.config_tester.run_common_tests()




     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



         __A         =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         return





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


         """simple docstring"""



         pass












from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments



def 			_SCREAMING_SNAKE_CASE						( )						->      Tuple:





    """simple docstring"""

    __A         =	HfArgumentParser(__lowercase    )
    __A         =	parser.parse_args_into_dataclasses()[0]
    __A         =	TensorFlowBenchmark(args=__lowercase    )
    try:
        __A         =	parser.parse_args_into_dataclasses()[0]
    except ValueError as e:
        __A         =	"""Arg --no_{0} is no longer used, please use --no-{0} instead."""
        __A         =	""" """.join(str(__lowercase    ).split(""" """    )[:-1]    )
        __A         =	""""""
        __A         =	eval(str(__lowercase    ).split(""" """    )[-1]    )
        __A         =	[]
        for arg in depreciated_args:
            # arg[2:] removes '--'
            if arg[2:] in TensorFlowBenchmark.deprecated_args:
                # arg[5:] removes '--no_'
                full_error_msg += arg_error_msg.format(arg[5:]    )
            else:
                wrong_args.append(__lowercase    )
        if len(__lowercase    ) > 0:
            __A         =	full_error_msg + begin_error_msg + str(__lowercase    )
        raise ValueError(__lowercase    )
    benchmark.run()


if __name__ == "__main__":
  main()




