


"""simple docstring"""



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

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


__snake_case         =					logging.get_logger(__name__)

__snake_case         =					{
    """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/config.json""",
    # See all BART models at https://huggingface.co/models?filter=bart
}







class _lowerCAmelCase       (   snake_case_			):
   __UpperCAmelCase				: Union[str, Any]													=						'''bart'''
   __UpperCAmelCase				: List[Any]													=						['''past_key_values''']
   __UpperCAmelCase				: List[str]													=						{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}







   def __init__(			self		,			UpperCamelCase__=5_0265		,			UpperCamelCase__=1024		,			UpperCamelCase__=12		,			UpperCamelCase__=4096		,			UpperCamelCase__=16		,			UpperCamelCase__=12		,			UpperCamelCase__=4096		,			UpperCamelCase__=16		,			UpperCamelCase__=0.0		,			UpperCamelCase__=0.0		,			UpperCamelCase__="gelu"		,			UpperCamelCase__=1024		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.0		,			UpperCamelCase__=0.0		,			UpperCamelCase__=0.02		,			UpperCamelCase__=0.0		,			UpperCamelCase__=False		,			UpperCamelCase__=True		,			UpperCamelCase__=3		,			UpperCamelCase__=1		,			UpperCamelCase__=0		,			UpperCamelCase__=2		,			UpperCamelCase__=True		,			UpperCamelCase__=2		,			UpperCamelCase__=2		,			**UpperCamelCase__		,			)			->	int:

       '''simple docstring'''




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

       super().__init__(
           num_labels=UpperCamelCase__		,			pad_token_id=UpperCamelCase__		,			bos_token_id=UpperCamelCase__		,			eos_token_id=UpperCamelCase__		,			is_encoder_decoder=UpperCamelCase__		,			decoder_start_token_id=UpperCamelCase__		,			forced_eos_token_id=UpperCamelCase__		,			**UpperCamelCase__		,			)

       # ensure backward compatibility for BART CNN models
       if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated"		,			UpperCamelCase__		):
           snake_case					:   Optional[Any]         =					self.bos_token_id
           warnings.warn(
               F'Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '
               "The config can simply be saved and uploaded again to be fixed."		)







class _lowerCAmelCase       (   snake_case_			):







   @property
   def 				lowerCamelCase	(			self		)			->	Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




       if self.task in ["default", "seq2seq-lm"]:
           snake_case					:   Optional[int]         =					OrderedDict(
               [
                   ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                   ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
               ]		)

           if self.use_past:
               snake_case					:   int         =					{0: "batch"}
               snake_case					:   List[Any]         =					{0: "batch", 1: "past_decoder_sequence + sequence"}
           else:
               snake_case					:   List[str]         =					{0: "batch", 1: "decoder_sequence"}
               snake_case					:   Any         =					{0: "batch", 1: "decoder_sequence"}

           if self.use_past:
               self.fill_with_past_key_values_(UpperCamelCase__		,			direction="inputs"		)
       elif self.task == "causal-lm":
           # TODO: figure this case out.
           snake_case					:   List[Any]         =					OrderedDict(
               [
                   ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                   ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
               ]		)
           if self.use_past:
               snake_case      ,snake_case					:   Dict         =					self.num_layers
               for i in range(UpperCamelCase__		):
                   snake_case					:   Dict         =					{0: "batch", 2: "past_sequence + sequence"}
                   snake_case					:   Dict         =					{0: "batch", 2: "past_sequence + sequence"}
       else:
           snake_case					:   Any         =					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
   def 				lowerCamelCase	(			self		)			->	Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




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







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = False		,			UpperCamelCase__ = None		,			)			->	Mapping[str, Any]:

       '''simple docstring'''




       snake_case					:   str         =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		)

       # Generate decoder inputs
       snake_case					:   int         =					seq_length if not self.use_past else 1
       snake_case					:   Union[str, Any]         =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		)
       snake_case					:   Optional[int]         =					{F'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
       snake_case					:   str         =					dict(**UpperCamelCase__		,			**UpperCamelCase__		)

       if self.use_past:
           if not is_torch_available():
               raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."		)
           else:
               import torch
           snake_case      ,snake_case					:   List[str]         =					common_inputs["input_ids"].shape
           snake_case					:   Dict         =					common_inputs["decoder_input_ids"].shape[1]
           snake_case      ,snake_case					:   List[Any]         =					self.num_attention_heads
           snake_case					:   Union[str, Any]         =					(
               batch,
               num_encoder_attention_heads,
               encoder_seq_length,
               self._config.hidden_size // num_encoder_attention_heads,
           )
           snake_case					:   Any         =					decoder_seq_length + 3
           snake_case					:   Tuple         =					(
               batch,
               num_decoder_attention_heads,
               decoder_past_length,
               self._config.hidden_size // num_decoder_attention_heads,
           )

           snake_case					:   Optional[Any]         =					torch.cat(
               [common_inputs["decoder_attention_mask"], torch.ones(UpperCamelCase__		,			UpperCamelCase__		)]		,			dim=1		)

           snake_case					:   List[Any]         =					[]
           # If the number of encoder and decoder layers are present in the model configuration, both are considered
           snake_case      ,snake_case					:   Any         =					self.num_layers
           snake_case					:   Optional[int]         =					min(UpperCamelCase__		,			UpperCamelCase__		)
           snake_case					:   Any         =					max(UpperCamelCase__		,			UpperCamelCase__		) - min_num_layers
           snake_case					:   int         =					"encoder" if num_encoder_layers > num_decoder_layers else "decoder"

           for _ in range(UpperCamelCase__		):
               common_inputs["past_key_values"].append(
                   (
                       torch.zeros(UpperCamelCase__		),
                       torch.zeros(UpperCamelCase__		),
                       torch.zeros(UpperCamelCase__		),
                       torch.zeros(UpperCamelCase__		),
                   )		)
           # TODO: test this.
           snake_case					:   Optional[Any]         =					encoder_shape if remaining_side_name == "encoder" else decoder_shape
           for _ in range(UpperCamelCase__		,			UpperCamelCase__		):
               common_inputs["past_key_values"].append((torch.zeros(UpperCamelCase__		), torch.zeros(UpperCamelCase__		))		)
       return common_inputs







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = False		,			UpperCamelCase__ = None		,			)			->	Mapping[str, Any]:

       '''simple docstring'''




       snake_case					:   str         =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		)

       if self.use_past:
           if not is_torch_available():
               raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."		)
           else:
               import torch
           snake_case      ,snake_case					:   Dict         =					common_inputs["input_ids"].shape
           # Not using the same length for past_key_values
           snake_case					:   List[str]         =					seqlen + 2
           snake_case      ,snake_case					:   Optional[int]         =					self.num_layers
           snake_case      ,snake_case					:   Optional[int]         =					self.num_attention_heads
           snake_case					:   Optional[int]         =					(
               batch,
               num_encoder_attention_heads,
               past_key_values_length,
               self._config.hidden_size // num_encoder_attention_heads,
           )

           snake_case					:   Optional[int]         =					common_inputs["attention_mask"].dtype
           snake_case					:   str         =					torch.cat(
               [common_inputs["attention_mask"], torch.ones(UpperCamelCase__		,			UpperCamelCase__		,			dtype=UpperCamelCase__		)]		,			dim=1		)
           snake_case					:   List[Any]         =					[
               (torch.zeros(UpperCamelCase__		), torch.zeros(UpperCamelCase__		)) for _ in range(UpperCamelCase__		)
           ]
       return common_inputs







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = False		,			UpperCamelCase__ = None		,			)			->	Mapping[str, Any]:

       '''simple docstring'''




       snake_case					:   Optional[Any]         =					compute_effective_axis_dimension(
           UpperCamelCase__		,			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
       snake_case					:   int         =					tokenizer.num_special_tokens_to_add(UpperCamelCase__		)
       snake_case					:   str         =					compute_effective_axis_dimension(
           UpperCamelCase__		,			fixed_dimension=OnnxConfig.default_fixed_sequence		,			num_token_to_add=UpperCamelCase__		)

       # Generate dummy inputs according to compute batch and sequence
       snake_case					:   Optional[int]         =					[" ".join([tokenizer.unk_token]		) * seq_length] * batch_size
       snake_case					:   Tuple         =					dict(tokenizer(UpperCamelCase__		,			return_tensors=UpperCamelCase__		)		)
       return common_inputs







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = -1		,			UpperCamelCase__ = False		,			UpperCamelCase__ = None		,			)			->	Mapping[str, Any]:

       '''simple docstring'''




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

       elif self.task == "causal-lm":
           snake_case					:   Tuple         =					self._generate_dummy_inputs_for_causal_lm(
               UpperCamelCase__		,			batch_size=UpperCamelCase__		,			seq_length=UpperCamelCase__		,			is_pair=UpperCamelCase__		,			framework=UpperCamelCase__		)
       else:
           snake_case					:   Optional[int]         =					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               UpperCamelCase__		,			batch_size=UpperCamelCase__		,			seq_length=UpperCamelCase__		,			is_pair=UpperCamelCase__		,			framework=UpperCamelCase__		)

       return common_inputs







   def 				lowerCamelCase	(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		)			->	Dict:

       '''simple docstring'''




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









"""simple docstring"""



import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
  from transformers.models.roberta.modeling_flax_roberta import (
      FlaxRobertaForCausalLM,
      FlaxRobertaForMaskedLM,
      FlaxRobertaForMultipleChoice,
      FlaxRobertaForQuestionAnswering,
      FlaxRobertaForSequenceClassification,
      FlaxRobertaForTokenClassification,
      FlaxRobertaModel,
  )







class _lowerCAmelCase       (   unittest.TestCase			):







   def __init__(			self		,			UpperCamelCase__		,			UpperCamelCase__=13		,			UpperCamelCase__=7		,			UpperCamelCase__=True		,			UpperCamelCase__=True		,			UpperCamelCase__=True		,			UpperCamelCase__=True		,			UpperCamelCase__=99		,			UpperCamelCase__=32		,			UpperCamelCase__=5		,			UpperCamelCase__=4		,			UpperCamelCase__=37		,			UpperCamelCase__="gelu"		,			UpperCamelCase__=0.1		,			UpperCamelCase__=0.1		,			UpperCamelCase__=512		,			UpperCamelCase__=16		,			UpperCamelCase__=2		,			UpperCamelCase__=0.02		,			UpperCamelCase__=4		,			)			->	Dict:

       '''simple docstring'''




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







   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   Optional[int]         =					ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size		)

       snake_case					:   Optional[int]         =					None
       if self.use_attention_mask:
           snake_case					:   Optional[Any]         =					random_attention_mask([self.batch_size, self.seq_length]		)

       snake_case					:   List[Any]         =					None
       if self.use_token_type_ids:
           snake_case					:   Dict         =					ids_tensor([self.batch_size, self.seq_length]		,			self.type_vocab_size		)

       snake_case					:   Union[str, Any]         =					RobertaConfig(
           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		,			)

       return config, input_ids, token_type_ids, attention_mask







   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   List[str]         =					self.prepare_config_and_inputs()
       snake_case      ,snake_case      ,snake_case      ,snake_case					:   List[Any]         =					config_and_inputs
       snake_case					:   Tuple         =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
       return config, inputs_dict







   def 				lowerCamelCase	(			self		)			->	Any:

       '''simple docstring'''




       snake_case					:   List[str]         =					self.prepare_config_and_inputs()
       snake_case      ,snake_case      ,snake_case      ,snake_case					:   List[Any]         =					config_and_inputs

       snake_case					:   int         =					True
       snake_case					:   Optional[int]         =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
       snake_case					:   Optional[Any]         =					ids_tensor([self.batch_size, self.seq_length]		,			vocab_size=2		)

       return (
           config,
           input_ids,
           token_type_ids,
           encoder_hidden_states,
           encoder_attention_mask,
       )







@require_flax
class _lowerCAmelCase       (   snake_case_					,							unittest.TestCase			):
   __UpperCAmelCase				: int													=						True

   __UpperCAmelCase				: Optional[Any]													=						(
       (
           FlaxRobertaModel,
           FlaxRobertaForCausalLM,
           FlaxRobertaForMaskedLM,
           FlaxRobertaForSequenceClassification,
           FlaxRobertaForTokenClassification,
           FlaxRobertaForMultipleChoice,
           FlaxRobertaForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )







   def 				lowerCamelCase	(			self		)			->	Dict:

       '''simple docstring'''




       snake_case					:   Dict         =					FlaxRobertaModelTester(self		)







   @slow
   def 				lowerCamelCase	(			self		)			->	Union[str, Any]:

       '''simple docstring'''




       for model_class_name in self.all_model_classes:
           snake_case					:   Dict         =					model_class_name.from_pretrained("roberta-base"		,			from_pt=UpperCamelCase__		)
           snake_case					:   List[str]         =					model(np.ones((1, 1)		)		)
           self.assertIsNotNone(UpperCamelCase__		)





