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


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





            '''simple docstring'''





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

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

            SCREAMING_SNAKE_CASE							:					List[str]           =							None
            SCREAMING_SNAKE_CASE							:					str           =							decoder_seq_length
            SCREAMING_SNAKE_CASE							:					List[str]           =							2
            SCREAMING_SNAKE_CASE							:					str           =							1


      def  __A ( self			:       Tuple		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					Tuple           =							ids_tensor([self.batch_size, self.decoder_seq_length]					,	self.vocab_size		)

            SCREAMING_SNAKE_CASE							:					Optional[Any]           =							None
            if self.use_attention_mask:
                  SCREAMING_SNAKE_CASE							:					Tuple           =							ids_tensor([self.batch_size, self.decoder_seq_length]					,	vocab_size=2		)

            SCREAMING_SNAKE_CASE							:					Optional[Any]           =							None
            if self.use_labels:
                  SCREAMING_SNAKE_CASE							:					Optional[int]           =							ids_tensor([self.batch_size, self.decoder_seq_length]					,	self.vocab_size		)

            SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							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			:       Union[str, Any]					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Tuple					,	):





            '''simple docstring'''





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

            input_ids[input_ids == 0] += 1
            # first forward pass
            SCREAMING_SNAKE_CASE							:					Optional[int]           =							model(UpperCamelCase_					,	use_cache=UpperCamelCase_		)
            SCREAMING_SNAKE_CASE							:					Optional[int]           =							model(UpperCamelCase_		)
            SCREAMING_SNAKE_CASE							:					Optional[int]           =							model(UpperCamelCase_					,	use_cache=UpperCamelCase_		)

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

            SCREAMING_SNAKE_CASE							:					List[Any]           =							outputs['past_key_values']

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

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

            SCREAMING_SNAKE_CASE							:					int           =							model(UpperCamelCase_		)['last_hidden_state']
            SCREAMING_SNAKE_CASE							:					Dict           =							model(UpperCamelCase_					,	past_key_values=UpperCamelCase_		)['last_hidden_state']

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

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



      def  __A ( self			:       Dict		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					List[Any]           =							self.prepare_config_and_inputs()
            SCREAMING_SNAKE_CASE							:					Optional[int]           =							config_and_inputs

            SCREAMING_SNAKE_CASE							:					int           =							{'input_ids': input_ids, 'attention_mask': attention_mask}
            return config, inputs_dict






@require_torch
class       lowercase__		(     UpperCamelCase_			,							UpperCamelCase_			,							UpperCamelCase_			,							unittest.TestCase):
      UpperCamelCase_  =	(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
      UpperCamelCase_  =	(TrOCRForCausalLM,) if is_torch_available() else ()
      UpperCamelCase_  =	{"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
      UpperCamelCase_  =	True
      UpperCamelCase_  =	False


      def  __A ( self			:       Tuple		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					Tuple           =							TrOCRStandaloneDecoderModelTester(self					,	is_training=UpperCamelCase_		)
            SCREAMING_SNAKE_CASE							:					int           =							ConfigTester(self					,	config_class=UpperCamelCase_		)


      def  __A ( self			:       Optional[int]		):





            '''simple docstring'''





            pass


      def  __A ( self			:       Tuple		):





            '''simple docstring'''





            pass


      def  __A ( self			:       List[Any]		):





            '''simple docstring'''





            pass


      def  __A ( self			:       Union[str, Any]		):





            '''simple docstring'''





            self.config_tester.run_common_tests()


      def  __A ( self			:       Tuple		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					Optional[int]           =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase_		)


      def  __A ( self			:       Union[str, Any]		):





            '''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			:       int		):





            '''simple docstring'''





            pass






from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
					from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


__UpperCamelCase	: Optional[Any]    		=  logging.get_logger(__name__)

__UpperCamelCase	: Optional[Any]    		=  {
    'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json'
    ),
    'microsoft/deberta-v2-xxlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json'
    ),
}





class       lowercase__		(     UpperCamelCase_):
						UpperCamelCase_  =	"""deberta-v2"""


						def __init__( self			:       Optional[Any]					,	UpperCamelCase__			:       Any=12_8100					,	UpperCamelCase__			:       Optional[int]=1536					,	UpperCamelCase__			:       Dict=24					,	UpperCamelCase__			:       List[str]=24					,	UpperCamelCase__			:       Tuple=6144					,	UpperCamelCase__			:       List[Any]="gelu"					,	UpperCamelCase__			:       Optional[int]=0.1					,	UpperCamelCase__			:       List[str]=0.1					,	UpperCamelCase__			:       Optional[int]=512					,	UpperCamelCase__			:       Optional[Any]=0					,	UpperCamelCase__			:       Union[str, Any]=0.02					,	UpperCamelCase__			:       List[Any]=1E-7					,	UpperCamelCase__			:       List[str]=False					,	UpperCamelCase__			:       str=-1					,	UpperCamelCase__			:       List[Any]=0					,	UpperCamelCase__			:       List[Any]=True					,	UpperCamelCase__			:       Optional[Any]=None					,	UpperCamelCase__			:       Tuple=0					,	UpperCamelCase__			:       str="gelu"					,	**UpperCamelCase__			:       Optional[int]					,	):





												'''simple docstring'''





												super().__init__(**UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					List[Any]           =							hidden_size
												SCREAMING_SNAKE_CASE							:					int           =							num_hidden_layers
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							num_attention_heads
												SCREAMING_SNAKE_CASE							:					List[Any]           =							intermediate_size
												SCREAMING_SNAKE_CASE							:					Any           =							hidden_act
												SCREAMING_SNAKE_CASE							:					Dict           =							hidden_dropout_prob
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							attention_probs_dropout_prob
												SCREAMING_SNAKE_CASE							:					str           =							max_position_embeddings
												SCREAMING_SNAKE_CASE							:					Dict           =							type_vocab_size
												SCREAMING_SNAKE_CASE							:					str           =							initializer_range
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							relative_attention
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							max_relative_positions
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							pad_token_id
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							position_biased_input

												# Backwards compatibility
												if type(UpperCamelCase__		) == str:
																		SCREAMING_SNAKE_CASE							:					Optional[int]           =							[x.strip() for x in pos_att_type.lower().split('''|'''		)]

												SCREAMING_SNAKE_CASE							:					Any           =							pos_att_type
												SCREAMING_SNAKE_CASE							:					int           =							vocab_size
												SCREAMING_SNAKE_CASE							:					Dict           =							layer_norm_eps

												SCREAMING_SNAKE_CASE							:					Tuple           =							kwargs.get('''pooler_hidden_size'''					,	UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Any           =							pooler_dropout
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							pooler_hidden_act






class       lowercase__		(     UpperCamelCase_):


						@property
						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												if self.task == "multiple-choice":
																		SCREAMING_SNAKE_CASE							:					Optional[int]           =							{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
												else:
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							{0: '''batch''', 1: '''sequence'''}
												if self._config.type_vocab_size > 0:
																		return OrderedDict(
																		    [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)]		)
												else:
																		return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)]		)


						@property
						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												return 12



						def  __A ( self			:       Dict					,	UpperCamelCase__			:       Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]					,	UpperCamelCase__			:       int = -1					,	UpperCamelCase__			:       int = -1					,	UpperCamelCase__			:       int = -1					,	UpperCamelCase__			:       bool = False					,	UpperCamelCase__			:       Optional["TensorType"] = None					,	UpperCamelCase__			:       int = 3					,	UpperCamelCase__			:       int = 40					,	UpperCamelCase__			:       int = 40					,	UpperCamelCase__			:       "PreTrainedTokenizerBase" = None					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							super().generate_dummy_inputs(preprocessor=UpperCamelCase__					,	framework=UpperCamelCase__		)
												if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
																		del dummy_inputs["token_type_ids"]
												return dummy_inputs





