
"""simple docstring"""


import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_FOR_PRETRAINING_MAPPING,
							    ElectraForCausalLM,
							    ElectraForMaskedLM,
							    ElectraForMultipleChoice,
							    ElectraForPreTraining,
							    ElectraForQuestionAnswering,
							    ElectraForSequenceClassification,
							    ElectraForTokenClassification,
							    ElectraModel,
							)
							from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST

class    A_     :



							"""simple docstring"""






							def __init__(  self  :List[Any]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Optional[Any]=13					,							lowerCamelCase_  :List[str]=7					,							lowerCamelCase_  :int=True					,							lowerCamelCase_  :List[str]=True					,							lowerCamelCase_  :Tuple=True					,							lowerCamelCase_  :Dict=True					,							lowerCamelCase_  :int=99					,							lowerCamelCase_  :List[str]=32					,							lowerCamelCase_  :str=5					,							lowerCamelCase_  :Tuple=4					,							lowerCamelCase_  :str=37					,							lowerCamelCase_  :List[str]="gelu"					,							lowerCamelCase_  :Optional[int]=0.1					,							lowerCamelCase_  :Optional[int]=0.1					,							lowerCamelCase_  :Dict=512					,							lowerCamelCase_  :Any=16					,							lowerCamelCase_  :Tuple=2					,							lowerCamelCase_  :List[Any]=0.02					,							lowerCamelCase_  :Any=3					,							lowerCamelCase_  :Optional[Any]=4					,							lowerCamelCase_  :List[str]=None					,							):





										"""simple docstring"""

										lowerCamelCase__     :					List[str]		=parent
										lowerCamelCase__     :					Optional[Any]		=batch_size
										lowerCamelCase__     :					Any		=seq_length
										lowerCamelCase__     :					List[str]		=is_training
										lowerCamelCase__     :					Optional[Any]		=use_input_mask
										lowerCamelCase__     :					str		=use_token_type_ids
										lowerCamelCase__     :					Dict		=use_labels
										lowerCamelCase__     :					Dict		=vocab_size
										lowerCamelCase__     :					Any		=hidden_size
										lowerCamelCase__     :					str		=num_hidden_layers
										lowerCamelCase__     :					str		=num_attention_heads
										lowerCamelCase__     :					int		=intermediate_size
										lowerCamelCase__     :					Tuple		=hidden_act
										lowerCamelCase__     :					Any		=hidden_dropout_prob
										lowerCamelCase__     :					List[str]		=attention_probs_dropout_prob
										lowerCamelCase__     :					Dict		=max_position_embeddings
										lowerCamelCase__     :					Dict		=type_vocab_size
										lowerCamelCase__     :					Optional[Any]		=type_sequence_label_size
										lowerCamelCase__     :					Any		=initializer_range
										lowerCamelCase__     :					Optional[Any]		=num_labels
										lowerCamelCase__     :					Union[str, Any]		=num_choices
										lowerCamelCase__     :					Tuple		=scope





							def 				UpperCAmelCase__							(  self  :Tuple	):





										"""simple docstring"""

										lowerCamelCase__     :					List[Any]		=ids_tensor([self.batch_size, self.seq_length]					,							self.vocab_size	)

										lowerCamelCase__     :					Optional[int]		=None
										if self.use_input_mask:
													lowerCamelCase__     :					List[str]		=random_attention_mask([self.batch_size, self.seq_length]	)

										lowerCamelCase__     :					Optional[Any]		=None
										if self.use_token_type_ids:
													lowerCamelCase__     :					Union[str, Any]		=ids_tensor([self.batch_size, self.seq_length]					,							self.type_vocab_size	)

										lowerCamelCase__     :					Any		=None
										lowerCamelCase__     :					Optional[int]		=None
										lowerCamelCase__     :					str		=None
										if self.use_labels:
													lowerCamelCase__     :					Dict		=ids_tensor([self.batch_size]					,							self.type_sequence_label_size	)
													lowerCamelCase__     :					List[Any]		=ids_tensor([self.batch_size, self.seq_length]					,							self.num_labels	)
													lowerCamelCase__     :					str		=ids_tensor([self.batch_size]					,							self.num_choices	)
													lowerCamelCase__     :					Dict		=ids_tensor([self.batch_size, self.seq_length]					,							1	)

										lowerCamelCase__     :					Optional[Any]		=self.get_config()

										return (
										    config,
										    input_ids,
										    token_type_ids,
										    input_mask,
										    sequence_labels,
										    token_labels,
										    choice_labels,
										    fake_token_labels,
										)





							def 				UpperCAmelCase__							(  self  :List[Any]	):





										"""simple docstring"""

										return ElectraConfig(
										    vocab_size=self.vocab_size					,							hidden_size=self.hidden_size					,							num_hidden_layers=self.num_hidden_layers					,							num_attention_heads=self.num_attention_heads					,							intermediate_size=self.intermediate_size					,							hidden_act=self.hidden_act					,							hidden_dropout_prob=self.hidden_dropout_prob					,							attention_probs_dropout_prob=self.attention_probs_dropout_prob					,							max_position_embeddings=self.max_position_embeddings					,							type_vocab_size=self.type_vocab_size					,							is_decoder=UpperCamelCase__					,							initializer_range=self.initializer_range					,							)





							def 				UpperCAmelCase__							(  self  :Optional[int]	):





										"""simple docstring"""

										(
										    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,       
										)     :					Optional[Any]		=self.prepare_config_and_inputs()

										lowerCamelCase__     :					Any		=True
										lowerCamelCase__     :					Union[str, Any]		=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
										lowerCamelCase__     :					str		=ids_tensor([self.batch_size, self.seq_length]					,							vocab_size=2	)

										return (
										    config,
										    input_ids,
										    token_type_ids,
										    input_mask,
										    sequence_labels,
										    token_labels,
										    choice_labels,
										    encoder_hidden_states,
										    encoder_attention_mask,
										)





							def 				UpperCAmelCase__							(  self  :Optional[int]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Optional[int]					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :str					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[Any]		=ElectraModel(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					Dict		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__	)
										lowerCamelCase__     :					int		=model(UpperCamelCase__					,							token_type_ids=UpperCamelCase__	)
										lowerCamelCase__     :					Union[str, Any]		=model(UpperCamelCase__	)
										self.parent.assertEqual(result.last_hidden_state.shape					,							(self.batch_size, self.seq_length, self.hidden_size)	)





							def 				UpperCAmelCase__							(  self  :Dict					,							lowerCamelCase_  :Dict					,							lowerCamelCase_  :int					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :Optional[int]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Any					,							lowerCamelCase_  :Any					,							lowerCamelCase_  :int					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[Any]		=True
										lowerCamelCase__     :					Any		=ElectraModel(UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					Tuple		=model(
										    UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							encoder_hidden_states=UpperCamelCase__					,							encoder_attention_mask=UpperCamelCase__					,							)
										lowerCamelCase__     :					Dict		=model(
										    UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							encoder_hidden_states=UpperCamelCase__					,							)
										lowerCamelCase__     :					Optional[Any]		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__	)
										self.parent.assertEqual(result.last_hidden_state.shape					,							(self.batch_size, self.seq_length, self.hidden_size)	)





							def 				UpperCAmelCase__							(  self  :Optional[Any]					,							lowerCamelCase_  :Union[str, Any]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Optional[int]					,							lowerCamelCase_  :int					,							lowerCamelCase_  :Union[str, Any]					,							lowerCamelCase_  :Optional[int]					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Dict		=ElectraForMaskedLM(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					str		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							labels=UpperCamelCase__	)
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.vocab_size)	)





							def 				UpperCAmelCase__							(  self  :Any					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Dict					,							lowerCamelCase_  :str					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :Dict					,							lowerCamelCase_  :Any					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :List[str]					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Dict		=ElectraForCausalLM(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					str		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							labels=UpperCamelCase__	)
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.vocab_size)	)





							def 				UpperCAmelCase__							(  self  :str					,							lowerCamelCase_  :int					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Optional[int]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :Optional[int]					,							):





										"""simple docstring"""

										lowerCamelCase__     :					int		=self.num_labels
										lowerCamelCase__     :					List[str]		=ElectraForTokenClassification(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					Optional[Any]		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							labels=UpperCamelCase__	)
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.num_labels)	)





							def 				UpperCAmelCase__							(  self  :Any					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :Optional[int]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :Tuple					,							):





										"""simple docstring"""

										lowerCamelCase__     :					int		=self.num_labels
										lowerCamelCase__     :					List[Any]		=ElectraForPreTraining(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					str		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							labels=UpperCamelCase__	)
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length)	)





							def 				UpperCAmelCase__							(  self  :Optional[int]					,							lowerCamelCase_  :Union[str, Any]					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :Any					,							lowerCamelCase_  :Union[str, Any]					,							lowerCamelCase_  :str					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :int					,							lowerCamelCase_  :Optional[int]					,							):





										"""simple docstring"""

										lowerCamelCase__     :					List[str]		=self.num_labels
										lowerCamelCase__     :					Optional[Any]		=ElectraForSequenceClassification(UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					Tuple		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							labels=UpperCamelCase__	)
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_labels)	)





							def 				UpperCAmelCase__							(  self  :List[Any]					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :int					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Any					,							lowerCamelCase_  :Optional[int]					,							lowerCamelCase_  :Union[str, Any]					,							lowerCamelCase_  :str					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[Any]		=ElectraForQuestionAnswering(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					int		=model(
										    UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							start_positions=UpperCamelCase__					,							end_positions=UpperCamelCase__					,							)
										self.parent.assertEqual(result.start_logits.shape					,							(self.batch_size, self.seq_length)	)
										self.parent.assertEqual(result.end_logits.shape					,							(self.batch_size, self.seq_length)	)





							def 				UpperCAmelCase__							(  self  :Tuple					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :List[str]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :List[Any]					,							):





										"""simple docstring"""

										lowerCamelCase__     :					List[Any]		=self.num_choices
										lowerCamelCase__     :					str		=ElectraForMultipleChoice(config=UpperCamelCase__	)
										model.to(UpperCamelCase__	)
										model.eval()
										lowerCamelCase__     :					str		=input_ids.unsqueeze(1	).expand(-1					,							self.num_choices					,							-1	).contiguous()
										lowerCamelCase__     :					List[str]		=token_type_ids.unsqueeze(1	).expand(-1					,							self.num_choices					,							-1	).contiguous()
										lowerCamelCase__     :					List[str]		=input_mask.unsqueeze(1	).expand(-1					,							self.num_choices					,							-1	).contiguous()
										lowerCamelCase__     :					List[str]		=model(
										    UpperCamelCase__					,							attention_mask=UpperCamelCase__					,							token_type_ids=UpperCamelCase__					,							labels=UpperCamelCase__					,							)
										self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_choices)	)





							def 				UpperCAmelCase__							(  self  :List[str]	):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[Any]		=self.prepare_config_and_inputs()
										(
										    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,    (
										    lowerCamelCase__   
										)	,       
										)     :					Optional[int]		=config_and_inputs
										lowerCamelCase__     :					Optional[int]		={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
										return config, inputs_dict






@require_torch
class    A_     (					_A				, _A				, unittest.TestCase       ):



							"""simple docstring"""






							SCREAMING_SNAKE_CASE_     				=       (
							    (
							        ElectraModel,
							        ElectraForPreTraining,
							        ElectraForMaskedLM,
							        ElectraForCausalLM,
							        ElectraForMultipleChoice,
							        ElectraForTokenClassification,
							        ElectraForSequenceClassification,
							        ElectraForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							SCREAMING_SNAKE_CASE_     				=       (
							    {
							        """feature-extraction""": ElectraModel,
							        """fill-mask""": ElectraForMaskedLM,
							        """question-answering""": ElectraForQuestionAnswering,
							        """text-classification""": ElectraForSequenceClassification,
							        """text-generation""": ElectraForCausalLM,
							        """token-classification""": ElectraForTokenClassification,
							        """zero-shot""": ElectraForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							SCREAMING_SNAKE_CASE_     				=       True





							def 				UpperCAmelCase__							(  self  :Dict					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Optional[int]					,							lowerCamelCase_  :List[str]=False	):





										"""simple docstring"""

										lowerCamelCase__     :					Dict		=super()._prepare_for_class(UpperCamelCase__					,							UpperCamelCase__					,							return_labels=UpperCamelCase__	)

										if return_labels:
													if model_class in get_values(UpperCamelCase__	):
																lowerCamelCase__     :					List[Any]		=torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.seq_length)					,							dtype=torch.long					,							device=UpperCamelCase__	)
										return inputs_dict





							def 				UpperCAmelCase__							(  self  :Optional[Any]	):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[int]		=ElectraModelTester(self	)
										lowerCamelCase__     :					Tuple		=ConfigTester(self					,							config_class=UpperCamelCase__					,							hidden_size=37	)





							def 				UpperCAmelCase__							(  self  :List[str]	):





										"""simple docstring"""

										self.config_tester.run_common_tests()





							def 				UpperCAmelCase__							(  self  :str	):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[Any]		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_model(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :str	):





										"""simple docstring"""

										lowerCamelCase__     :					Any		=self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_electra_model_as_decoder(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :Optional[Any]	):





										"""simple docstring"""

										lowerCamelCase__     :					Tuple		=self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
													lowerCamelCase__     :					List[Any]		=type
													self.model_tester.create_and_check_electra_model(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :int	):





										"""simple docstring"""

										lowerCamelCase__     :					List[str]		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_masked_lm(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :Tuple	):





										"""simple docstring"""

										lowerCamelCase__     :					int		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_token_classification(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :List[str]	):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[Any]		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_pretraining(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :List[Any]	):





										"""simple docstring"""

										lowerCamelCase__     :					str		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_sequence_classification(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :Any	):





										"""simple docstring"""

										lowerCamelCase__     :					str		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_question_answering(*UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :Any	):





										"""simple docstring"""

										lowerCamelCase__     :					Any		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_electra_for_multiple_choice(*UpperCamelCase__	)





							@slow
							def 				UpperCAmelCase__							(  self  :Tuple	):





										"""simple docstring"""

										for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowerCamelCase__     :					List[str]		=ElectraModel.from_pretrained(UpperCamelCase__	)
													self.assertIsNotNone(UpperCamelCase__	)





							def 				UpperCAmelCase__							(  self  :str	):





										"""simple docstring"""

										lowerCamelCase__     :					Tuple		=self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_electra_for_causal_lm(*UpperCamelCase__	)






@require_torch
class    A_     (					unittest.TestCase       ):



							"""simple docstring"""






							@slow
							def 				UpperCAmelCase__							(  self  :Union[str, Any]	):





										"""simple docstring"""

										lowerCamelCase__     :					Union[str, Any]		=ElectraModel.from_pretrained('google/electra-small-discriminator'	)
										lowerCamelCase__     :					List[Any]		=torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
										lowerCamelCase__     :					Optional[int]		=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
										lowerCamelCase__     :					Optional[int]		=model(UpperCamelCase__					,							attention_mask=UpperCamelCase__	)[0]
										lowerCamelCase__     :					int		=torch.Size((1, 11, 256)	)
										self.assertEqual(output.shape					,							UpperCamelCase__	)
										lowerCamelCase__     :					Dict		=torch.tensor(
										    [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]	)

										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,							UpperCamelCase__					,							atol=1e-4	)	)

"""simple docstring"""


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

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


lowerCAmelCase								=						logging.get_logger(__name__)

lowerCAmelCase								=						{
    """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json""",
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}

class    A_     (					A__       ):



							"""simple docstring"""






							SCREAMING_SNAKE_CASE_     				=       """blenderbot"""
							SCREAMING_SNAKE_CASE_     				=       ["""past_key_values"""]
							SCREAMING_SNAKE_CASE_     				=       {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}





							def __init__(  self  :Optional[int]					,							lowerCamelCase_  :Optional[Any]=8_008					,							lowerCamelCase_  :Optional[Any]=128					,							lowerCamelCase_  :Optional[int]=2					,							lowerCamelCase_  :Tuple=10_240					,							lowerCamelCase_  :Optional[int]=32					,							lowerCamelCase_  :Optional[int]=24					,							lowerCamelCase_  :Optional[Any]=10_240					,							lowerCamelCase_  :Union[str, Any]=32					,							lowerCamelCase_  :Any=0.0					,							lowerCamelCase_  :int=0.0					,							lowerCamelCase_  :str=True					,							lowerCamelCase_  :List[Any]=True					,							lowerCamelCase_  :Tuple="gelu"					,							lowerCamelCase_  :str=2_560					,							lowerCamelCase_  :Union[str, Any]=0.1					,							lowerCamelCase_  :Optional[int]=0.0					,							lowerCamelCase_  :Union[str, Any]=0.0					,							lowerCamelCase_  :List[str]=0.02					,							lowerCamelCase_  :Any=1					,							lowerCamelCase_  :List[Any]=False					,							lowerCamelCase_  :str=0					,							lowerCamelCase_  :Dict=1					,							lowerCamelCase_  :List[Any]=2					,							lowerCamelCase_  :int=3					,							lowerCamelCase_  :int=2					,							**lowerCamelCase_  :List[str]					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[int]		=vocab_size
										lowerCamelCase__     :					Optional[int]		=max_position_embeddings
										lowerCamelCase__     :					Dict		=d_model
										lowerCamelCase__     :					str		=encoder_ffn_dim
										lowerCamelCase__     :					Optional[Any]		=encoder_layers
										lowerCamelCase__     :					List[Any]		=encoder_attention_heads
										lowerCamelCase__     :					Any		=decoder_ffn_dim
										lowerCamelCase__     :					Any		=decoder_layers
										lowerCamelCase__     :					Any		=decoder_attention_heads
										lowerCamelCase__     :					Dict		=dropout
										lowerCamelCase__     :					Any		=attention_dropout
										lowerCamelCase__     :					Dict		=activation_dropout
										lowerCamelCase__     :					Tuple		=activation_function
										lowerCamelCase__     :					List[Any]		=init_std
										lowerCamelCase__     :					str		=encoder_layerdrop
										lowerCamelCase__     :					Any		=decoder_layerdrop
										lowerCamelCase__     :					Union[str, Any]		=use_cache
										lowerCamelCase__     :					List[str]		=encoder_layers
										lowerCamelCase__     :					Optional[int]		=scale_embedding  # scale factor will be sqrt(d_model) if True

										super().__init__(
										    pad_token_id=lowerCamelCase_					,							bos_token_id=lowerCamelCase_					,							eos_token_id=lowerCamelCase_					,							is_encoder_decoder=lowerCamelCase_					,							decoder_start_token_id=lowerCamelCase_					,							encoder_no_repeat_ngram_size=lowerCamelCase_					,							forced_eos_token_id=lowerCamelCase_					,							**lowerCamelCase_					,							)






class    A_     (					A__       ):



							"""simple docstring"""






							@property
							def 				UpperCAmelCase__							(  self  :Tuple	):





										"""simple docstring"""

										if self.task in ["default", "seq2seq-lm"]:
													lowerCamelCase__     :					Dict		=OrderedDict(
													    [
													        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
													        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
													    ]	)
													if self.use_past:
																lowerCamelCase__     :					Dict		={0: 'batch'}
																lowerCamelCase__     :					Tuple		={0: 'batch', 1: 'past_decoder_sequence + sequence'}
													else:
																lowerCamelCase__     :					Optional[int]		={0: 'batch', 1: 'decoder_sequence'}
																lowerCamelCase__     :					Tuple		={0: 'batch', 1: 'decoder_sequence'}
													if self.use_past:
																self.fill_with_past_key_values_(lowerCamelCase_					,							direction='inputs'	)
										elif self.task == "causal-lm":
													lowerCamelCase__     :					Any		=OrderedDict(
													    [
													        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
													        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
													    ]	)
													if self.use_past:
																lowerCamelCase__	,    lowerCamelCase__     :					List[Any]		=self.num_layers
																for i in range(lowerCamelCase_	):
																			lowerCamelCase__     :					Tuple		={0: 'batch', 2: 'past_sequence + sequence'}
																			lowerCamelCase__     :					Optional[int]		={0: 'batch', 2: 'past_sequence + sequence'}
										else:
													lowerCamelCase__     :					List[str]		=OrderedDict(
													    [
													        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
													        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
													        ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
													        ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
													    ]	)

										return common_inputs





							@property
							# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
							def 				UpperCAmelCase__							(  self  :List[str]	):





										"""simple docstring"""

										if self.task in ["default", "seq2seq-lm"]:
													lowerCamelCase__     :					Tuple		=super().outputs
										else:
													lowerCamelCase__     :					List[Any]		=super(lowerCamelCase_					,							self	).outputs
													if self.use_past:
																lowerCamelCase__	,    lowerCamelCase__     :					List[Any]		=self.num_layers
																for i in range(lowerCamelCase_	):
																			lowerCamelCase__     :					Tuple		={0: 'batch', 2: 'past_sequence + sequence'}
																			lowerCamelCase__     :					str		={0: 'batch', 2: 'past_sequence + sequence'}
										return common_outputs





							def 				UpperCAmelCase__							(  self  :List[Any]					,							lowerCamelCase_  :PreTrainedTokenizer					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :bool = False					,							lowerCamelCase_  :Optional[TensorType] = None					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Optional[int]		=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_	)
										# Generate decoder inputs
										lowerCamelCase__     :					List[str]		=seq_length if not self.use_past else 1
										lowerCamelCase__     :					Optional[Any]		=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_	)
										lowerCamelCase__     :					int		={f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
										lowerCamelCase__     :					int		=dict(**lowerCamelCase_					,							**lowerCamelCase_	)

										if self.use_past:
													if not is_torch_available():
																raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'	)
													else:
																import torch
													lowerCamelCase__	,    lowerCamelCase__     :					List[str]		=common_inputs['input_ids'].shape
													lowerCamelCase__     :					Tuple		=common_inputs['decoder_input_ids'].shape[1]
													lowerCamelCase__	,    lowerCamelCase__     :					int		=self.num_attention_heads
													lowerCamelCase__     :					Optional[Any]		=(
													    batch,
													    num_encoder_attention_heads,
													    encoder_seq_length,
													    self._config.hidden_size // num_encoder_attention_heads,
													)
													lowerCamelCase__     :					Any		=decoder_seq_length
													lowerCamelCase__     :					Optional[Any]		=(
													    batch,
													    num_decoder_attention_heads,
													    decoder_past_length,
													    self._config.hidden_size // num_decoder_attention_heads,
													)
													lowerCamelCase__     :					Optional[int]		=torch.cat(
													    [common_inputs['decoder_attention_mask'], torch.ones(lowerCamelCase_					,							lowerCamelCase_	)]					,							dim=1	)
													lowerCamelCase__     :					List[str]		=[]
													lowerCamelCase__	,    lowerCamelCase__     :					Dict		=self.num_layers

													for _ in range(lowerCamelCase_	):
																common_inputs["past_key_values"].append(
																    (
																        torch.zeros(lowerCamelCase_	),
																        torch.zeros(lowerCamelCase_	),
																        torch.zeros(lowerCamelCase_	),
																        torch.zeros(lowerCamelCase_	),
																    )	)
										return common_inputs





							def 				UpperCAmelCase__							(  self  :int					,							lowerCamelCase_  :PreTrainedTokenizer					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :bool = False					,							lowerCamelCase_  :Optional[TensorType] = None					,							):





										"""simple docstring"""

										lowerCamelCase__     :					Union[str, Any]		=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_	)

										if self.use_past:
													if not is_torch_available():
																raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'	)
													else:
																import torch
													lowerCamelCase__	,    lowerCamelCase__     :					Optional[int]		=common_inputs['input_ids'].shape
													lowerCamelCase__     :					List[Any]		=seqlen
													lowerCamelCase__	,    lowerCamelCase__     :					Tuple		=self.num_layers
													lowerCamelCase__	,    lowerCamelCase__     :					List[str]		=self.num_attention_heads
													lowerCamelCase__     :					Optional[int]		=(
													    batch,
													    num_encoder_attention_heads,
													    past_key_values_length,
													    self._config.hidden_size // num_encoder_attention_heads,
													)
													lowerCamelCase__     :					List[str]		=common_inputs['attention_mask'].dtype
													lowerCamelCase__     :					Tuple		=torch.cat(
													    [common_inputs['attention_mask'], torch.ones(lowerCamelCase_					,							lowerCamelCase_					,							dtype=lowerCamelCase_	)]					,							dim=1	)
													lowerCamelCase__     :					Optional[int]		=[
													    (torch.zeros(lowerCamelCase_	), torch.zeros(lowerCamelCase_	)) for _ in range(lowerCamelCase_	)
													]
										return common_inputs





							def 				UpperCAmelCase__							(  self  :Union[str, Any]					,							lowerCamelCase_  :PreTrainedTokenizer					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :bool = False					,							lowerCamelCase_  :Optional[TensorType] = None					,							):





										"""simple docstring"""

										lowerCamelCase__     :					List[Any]		=compute_effective_axis_dimension(
										    lowerCamelCase_					,							fixed_dimension=OnnxConfig.default_fixed_batch					,							num_token_to_add=0	)

										# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
										lowerCamelCase__     :					Optional[int]		=tokenizer.num_special_tokens_to_add(lowerCamelCase_	)
										lowerCamelCase__     :					int		=compute_effective_axis_dimension(
										    lowerCamelCase_					,							fixed_dimension=OnnxConfig.default_fixed_sequence					,							num_token_to_add=lowerCamelCase_	)

										# Generate dummy inputs according to compute batch and sequence
										lowerCamelCase__     :					List[Any]		=[' '.join([tokenizer.unk_token]	) * seq_length] * batch_size
										lowerCamelCase__     :					Optional[Any]		=dict(tokenizer(lowerCamelCase_					,							return_tensors=lowerCamelCase_	)	)
										return common_inputs





							def 				UpperCAmelCase__							(  self  :Tuple					,							lowerCamelCase_  :PreTrainedTokenizer					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :int = -1					,							lowerCamelCase_  :bool = False					,							lowerCamelCase_  :Optional[TensorType] = None					,							):





										"""simple docstring"""

										if self.task in ["default", "seq2seq-lm"]:
													lowerCamelCase__     :					str		=self._generate_dummy_inputs_for_default_and_seqaseq_lm(
													    lowerCamelCase_					,							batch_size=lowerCamelCase_					,							seq_length=lowerCamelCase_					,							is_pair=lowerCamelCase_					,							framework=lowerCamelCase_	)

										elif self.task == "causal-lm":
													lowerCamelCase__     :					List[Any]		=self._generate_dummy_inputs_for_causal_lm(
													    lowerCamelCase_					,							batch_size=lowerCamelCase_					,							seq_length=lowerCamelCase_					,							is_pair=lowerCamelCase_					,							framework=lowerCamelCase_	)
										else:
													lowerCamelCase__     :					Dict		=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
													    lowerCamelCase_					,							batch_size=lowerCamelCase_					,							seq_length=lowerCamelCase_					,							is_pair=lowerCamelCase_					,							framework=lowerCamelCase_	)

										return common_inputs





							def 				UpperCAmelCase__							(  self  :Optional[int]					,							lowerCamelCase_  :Tuple					,							lowerCamelCase_  :Optional[Any]					,							lowerCamelCase_  :List[Any]					,							lowerCamelCase_  :Dict	):





										"""simple docstring"""

										if self.task in ["default", "seq2seq-lm"]:
													lowerCamelCase__     :					List[Any]		=super()._flatten_past_key_values_(lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_	)
										else:
													lowerCamelCase__     :					List[Any]		=super(lowerCamelCase_					,							self	)._flatten_past_key_values_(
													    lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_					,							lowerCamelCase_	)





							def 				UpperCAmelCase__							(  self  :Any					,							lowerCamelCase_  :Mapping[str, Mapping[int, str]]					,							lowerCamelCase_  :str	):





										"""simple docstring"""

										if direction not in ["inputs", "outputs"]:
													raise ValueError(f"""direction must either be \"inputs\" or \"outputs\", but {direction} was given"""	)

										lowerCamelCase__     :					Optional[Any]		='past_key_values' if direction == 'inputs' else 'present'
										lowerCamelCase__	,    lowerCamelCase__     :					Union[str, Any]		=self.num_layers

										lowerCamelCase__     :					Optional[Any]		='past_encoder_sequence'
										lowerCamelCase__     :					Union[str, Any]		='past_decoder_sequence' if direction == 'inputs' else 'past_decoder_sequence + sequence'

										for i in range(lowerCamelCase_	):
													lowerCamelCase__     :					Union[str, Any]		={0: 'batch', 2: decoder_sequence}
													lowerCamelCase__     :					Any		={0: 'batch', 2: decoder_sequence}
													lowerCamelCase__     :					List[Any]		={0: 'batch', 2: encoder_sequence}
													lowerCamelCase__     :					Tuple		={0: 'batch', 2: encoder_sequence}