



"""simple docstring"""

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       :

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

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

     snake_case_ :     Union[str, Any]				=	None
     snake_case_ :     int				=	decoder_seq_length
     snake_case_ :     List[str]				=	2
     snake_case_ :     Union[str, Any]				=	1

 def 						_snake_case					(     self		:       Any				):
     snake_case_ :     Optional[Any]				=	ids_tensor([self.batch_size, self.decoder_seq_length]  ,     self.vocab_size				)

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

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

     snake_case_ :     Optional[int]				=	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 						_snake_case					(     self		:       Tuple  ,     lowercase_		:       List[Any]  ,     lowercase_		:       List[Any]  ,     lowercase_		:       List[Any]  ,     lowercase_		:       List[str]  ,     ):
     snake_case_ :     Union[str, Any]				=	True
     snake_case_ :     Dict				=	TrOCRDecoder(config=a_				).to(a_				).eval()
     snake_case_ :     List[Any]				=	input_ids[:2]

     input_ids[input_ids == 0] += 1
     # first forward pass
     snake_case_ :     Optional[int]				=	model(a_  ,     use_cache=a_				)
     snake_case_ :     Union[str, Any]				=	model(a_				)
     snake_case_ :     Optional[Any]				=	model(a_  ,     use_cache=a_				)

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

     snake_case_ :     Any				=	outputs['''past_key_values''']

     # create hypothetical next token and extent to next_input_ids
     snake_case_ :     List[Any]				=	ids_tensor((2, 1)  ,     config.vocab_size - 1				) + 1

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

     snake_case_ :     Union[str, Any]				=	model(a_				)['''last_hidden_state''']
     snake_case_ :     Dict				=	model(a_  ,     past_key_values=a_				)['''last_hidden_state''']

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

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






 def 						_snake_case					(     self		:       Union[str, Any]				):
     snake_case_ :     str				=	self.prepare_config_and_inputs()
     snake_case_ :     List[Any]				=	config_and_inputs

     snake_case_ :     Tuple				=	{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
     return config, inputs_dict





@require_torch
class        _UpperCAmelCase       (			__UpperCamelCase    ,     __UpperCamelCase    ,     __UpperCamelCase    ,     unittest.TestCase):
 _lowerCAmelCase   :			Any							=		(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
 _lowerCAmelCase   :			Union[str, Any]							=		(TrOCRForCausalLM,) if is_torch_available() else ()
 _lowerCAmelCase   :			Optional[Any]							=		{"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
 _lowerCAmelCase   :			Any							=		True
 _lowerCAmelCase   :			Union[str, Any]							=		False

 def 						_snake_case					(     self		:       Any				):
     snake_case_ :     List[str]				=	TrOCRStandaloneDecoderModelTester(self  ,     is_training=a_				)
     snake_case_ :     Dict				=	ConfigTester(self  ,     config_class=a_				)

 def 						_snake_case					(     self		:       Optional[int]				):
     pass

 def 						_snake_case					(     self		:       Union[str, Any]				):
     pass

 def 						_snake_case					(     self		:       Union[str, Any]				):
     pass

 def 						_snake_case					(     self		:       Dict				):
     self.config_tester.run_common_tests()

 def 						_snake_case					(     self		:       List[str]				):
     snake_case_ :     Optional[int]				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past(*a_				)

 def 						_snake_case					(     self		:       List[Any]				):
     return






 @unittest.skip('''The model doesn\'t support left padding'''				)  # and it's not used enough to be worth fixing :)
 def 						_snake_case					(     self		:       str				):
     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


__A   			=logging.get_logger(__name__)

__A   			={
    "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 		UpperCAmelCase__			(			__UpperCamelCase   ):




			'''simple docstring'''






			UpperCamelCase									=       """deberta-v2"""




			def __init__(   self							: Optional[int]       ,       a_							: List[str]=12_81_00       ,       a_							: Optional[Any]=15_36       ,       a_							: Optional[Any]=24       ,       a_							: List[Any]=24       ,       a_							: Optional[int]=61_44       ,       a_							: List[Any]="gelu"       ,       a_							: Any=0.1       ,       a_							: Tuple=0.1       ,       a_							: Optional[Any]=5_12       ,       a_							: Tuple=0       ,       a_							: Dict=0.0_2       ,       a_							: Optional[Any]=1e-7       ,       a_							: List[str]=False       ,       a_							: List[Any]=-1       ,       a_							: List[str]=0       ,       a_							: Optional[Any]=True       ,       a_							: List[Any]=None       ,       a_							: Optional[int]=0       ,       a_							: Tuple="gelu"       ,       **a_							: List[str]       ,       ):
					'''simple docstring'''





					super().__init__(**a_ )

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

					# Backwards compatibility
					if type(a_ ) == str:
							__UpperCAmelCase    :     Optional[Any]													=	[x.strip() for x in pos_att_type.lower().split('''|''' )]

					__UpperCAmelCase    :     Tuple													=	pos_att_type
					__UpperCAmelCase    :     int													=	vocab_size
					__UpperCAmelCase    :     Optional[Any]													=	layer_norm_eps

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

class 		UpperCAmelCase__			(			__UpperCamelCase   ):




			'''simple docstring'''






			@property
			def  snake_case__     (   self							: Optional[Any] ):
					'''simple docstring'''





					if self.task == "multiple-choice":
							__UpperCAmelCase    :     List[Any]													=	{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
					else:
							__UpperCAmelCase    :     int													=	{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  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					return 12




			def  snake_case__     (   self							: Any       ,       a_							: Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]       ,       a_							: int = -1       ,       a_							: int = -1       ,       a_							: int = -1       ,       a_							: bool = False       ,       a_							: Optional["TensorType"] = None       ,       a_							: int = 3       ,       a_							: int = 40       ,       a_							: int = 40       ,       a_							: "PreTrainedTokenizerBase" = None       ,       ):
					'''simple docstring'''





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





