




'''simple docstring'''





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


SCREAMING_SNAKE_CASE_						:					Union[str, Any]			   =       logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_						:					Dict			   =       {
    """facebook/mbart-large-cc25""": """https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json""",
    # See all MBART models at https://huggingface.co/models?filter=mbart
}







class 		__UpperCAmelCase(		a_ ):





     """simple docstring"""



     __UpperCAmelCase	: Union[str, Any]						=    'mbart'
     __UpperCAmelCase	: Any						=    ['past_key_values']
     __UpperCAmelCase	: List[str]						=    {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}

     def __init__(      self :       Optional[int]						,				A :       List[str]=5_02_65						,				A :       Dict=10_24						,				A :       Dict=12						,				A :       int=40_96						,				A :       List[str]=16						,				A :       List[Any]=12						,				A :       Dict=40_96						,				A :       Any=16						,				A :       Any=0.0						,				A :       str=0.0						,				A :       Union[str, Any]=True						,				A :       Tuple=True						,				A :       Tuple="gelu"						,				A :       List[str]=10_24						,				A :       int=0.1						,				A :       Any=0.0						,				A :       List[Any]=0.0						,				A :       Optional[int]=0.0_2						,				A :       Any=0.0						,				A :       List[str]=False						,				A :       Union[str, Any]=1						,				A :       Optional[int]=0						,				A :       str=2						,				A :       List[Any]=2						,				**A :       Tuple						,				)			->			Tuple:






         """simple docstring"""
         a__      =       vocab_size
         a__      =       max_position_embeddings
         a__      =       d_model
         a__      =       encoder_ffn_dim
         a__      =       encoder_layers
         a__      =       encoder_attention_heads
         a__      =       decoder_ffn_dim
         a__      =       decoder_layers
         a__      =       decoder_attention_heads
         a__      =       dropout
         a__      =       attention_dropout
         a__      =       activation_dropout
         a__      =       activation_function
         a__      =       init_std
         a__      =       encoder_layerdrop
         a__      =       decoder_layerdrop
         a__      =       classifier_dropout
         a__      =       use_cache
         a__      =       encoder_layers
         a__      =       scale_embedding  # scale factor will be sqrt(d_model) if True
         super().__init__(
             pad_token_id=_snake_case						,				bos_token_id=_snake_case						,				eos_token_id=_snake_case						,				is_encoder_decoder=_snake_case						,				forced_eos_token_id=_snake_case						,				**_snake_case						,				)







class 		__UpperCAmelCase(		a_ ):





     """simple docstring"""



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






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

             if self.use_past:
                 a__      =       {0: '''batch'''}
                 a__      =       {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
             else:
                 a__      =       {0: '''batch''', 1: '''decoder_sequence'''}
                 a__      =       {0: '''batch''', 1: '''decoder_sequence'''}

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






         """simple docstring"""
         if self.task in ["default", "seq2seq-lm"]:
             a__      =       super().outputs
         else:
             a__      =       super(_snake_case						,				self    ).outputs
             if self.use_past:
                 a__      =       self.num_layers
                 for i in range(_snake_case    ):
                     a__      =       {0: '''batch''', 2: '''past_sequence + sequence'''}
                     a__      =       {0: '''batch''', 2: '''past_sequence + sequence'''}
         return common_outputs

     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[int]						,				A :       Any						,				A :       int = -1						,				A :       int = -1						,				A :       Optional[int] = False						,				A :       Union[str, Any] = None						,				)			->			Mapping[str, Any]:






         """simple docstring"""
         a__      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _snake_case						,				_snake_case						,				_snake_case						,				_snake_case						,				_snake_case    )

         # Generate decoder inputs
         a__      =       seq_length if not self.use_past else 1
         a__      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _snake_case						,				_snake_case						,				_snake_case						,				_snake_case						,				_snake_case    )
         a__      =       {f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
         a__      =       dict(**_snake_case						,				**_snake_case    )

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

             a__      =       torch.cat(
                 [common_inputs["""decoder_attention_mask"""], torch.ones(_snake_case						,				_snake_case    )]						,				dim=1    )

             a__      =       []
             # If the number of encoder and decoder layers are present in the model configuration, both are considered
             a__      =       self.num_layers
             a__      =       min(_snake_case						,				_snake_case    )
             a__      =       max(_snake_case						,				_snake_case    ) - min_num_layers
             a__      =       '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

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

     def 				__SCREAMING_SNAKE_CASE		(      self :       Tuple						,				A :       Optional[int]						,				A :       Union[str, Any] = -1						,				A :       Dict = -1						,				A :       str = False						,				A :       Any = None						,				)			->			Mapping[str, Any]:






         """simple docstring"""
         a__      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _snake_case						,				_snake_case						,				_snake_case						,				_snake_case						,				_snake_case    )

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

             a__      =       common_inputs['''attention_mask'''].dtype
             a__      =       torch.cat(
                 [common_inputs["""attention_mask"""], torch.ones(_snake_case						,				_snake_case						,				dtype=_snake_case    )]						,				dim=1    )
             a__      =       [
                 (torch.zeros(_snake_case    ), torch.zeros(_snake_case    )) for _ in range(_snake_case    )
             ]
         return common_inputs

     def 				__SCREAMING_SNAKE_CASE		(      self :       Union[str, Any]						,				A :       str						,				A :       Dict = -1						,				A :       List[str] = -1						,				A :       Optional[Any] = False						,				A :       List[Any] = None						,				)			->			Mapping[str, Any]:






         """simple docstring"""
         # Copied from OnnxConfig.generate_dummy_inputs
         # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
         # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
         a__      =       compute_effective_axis_dimension(
             _snake_case						,				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
         a__      =       tokenizer.num_special_tokens_to_add(_snake_case    )
         a__      =       compute_effective_axis_dimension(
             _snake_case						,				fixed_dimension=OnnxConfig.default_fixed_sequence						,				num_token_to_add=_snake_case    )

         # Generate dummy inputs according to compute batch and sequence
         a__      =       [''' '''.join([tokenizer.unk_token]    ) * seq_length] * batch_size
         a__      =       dict(tokenizer(_snake_case						,				return_tensors=_snake_case    )    )
         return common_inputs

     def 				__SCREAMING_SNAKE_CASE		(      self :       Any						,				A :       List[str]						,				A :       Tuple = -1						,				A :       int = -1						,				A :       Union[str, Any] = False						,				A :       Optional[Any] = None						,				)			->			Mapping[str, Any]:






         """simple docstring"""
         if self.task in ["default", "seq2seq-lm"]:
             a__      =       self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                 _snake_case						,				batch_size=_snake_case						,				seq_length=_snake_case						,				is_pair=_snake_case						,				framework=_snake_case    )

         elif self.task == "causal-lm":
             a__      =       self._generate_dummy_inputs_for_causal_lm(
                 _snake_case						,				batch_size=_snake_case						,				seq_length=_snake_case						,				is_pair=_snake_case						,				framework=_snake_case    )
         else:
             a__      =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                 _snake_case						,				batch_size=_snake_case						,				seq_length=_snake_case						,				is_pair=_snake_case						,				framework=_snake_case    )

         return common_inputs






     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[int]						,				A :       int						,				A :       Union[str, Any]						,				A :       Any						,				A :       List[Any]    )			->			Optional[int]:






         """simple docstring"""
         if self.task in ["default", "seq2seq-lm"]:
             a__      =       super()._flatten_past_key_values_(_snake_case						,				_snake_case						,				_snake_case						,				_snake_case    )
         else:
             a__      =       super(_snake_case						,				self    )._flatten_past_key_values_(
                 _snake_case						,				_snake_case						,				_snake_case						,				_snake_case    )












import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


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

if is_tf_available():
 import tensorflow as tf







@dataclass
class 		__UpperCAmelCase(		lowercase__ ):





     """simple docstring"""



     __UpperCAmelCase	: int						=    """tf"""
     __UpperCAmelCase	: Optional[str]						=    field(
         default=lowercase__       ,	metadata={"""help""": """Name of TPU"""}       ,	)

     __UpperCAmelCase	: Optional[str]						=    field(
         default=lowercase__       ,	metadata={"""help""": """Zone of TPU"""}       ,	)

     __UpperCAmelCase	: Optional[str]						=    field(
         default=lowercase__       ,	metadata={"""help""": """Name of Cloud TPU-enabled project"""}       ,	)

     __UpperCAmelCase	: float						=    field(
         default=1.0       ,	metadata={"""help""": """Power for the Polynomial decay LR scheduler."""}       ,	)

     __UpperCAmelCase	: bool						=    field(default=lowercase__       ,	metadata={"""help""": """Whether to activate the XLA compilation or not"""} )

     @cached_property
     def 				__SCREAMING_SNAKE_CASE		(      self :       Dict    )			->			Tuple["tf.distribute.Strategy", int]:






         """simple docstring"""
         requires_backends(self						,				["""tf"""]    )
         logger.info("""Tensorflow: setting up strategy"""    )

         a__      =       tf.config.list_physical_devices("""GPU"""    )

         # Set to float16 at first
         if self.fpaa:
             tf.keras.mixed_precision.set_global_policy("""mixed_float16"""    )

         if self.no_cuda:
             a__      =       tf.distribute.OneDeviceStrategy(device="""/cpu:0"""    )
         else:
             try:
                 if self.tpu_name:
                     a__      =       tf.distribute.cluster_resolver.TPUClusterResolver(
                         self.tpu_name						,				zone=self.tpu_zone						,				project=self.gcp_project    )
                 else:
                     a__      =       tf.distribute.cluster_resolver.TPUClusterResolver()
             except ValueError:
                 if self.tpu_name:
                     raise RuntimeError(f"""Couldn't connect to TPU {self.tpu_name}!"""    )
                 else:
                     a__      =       None

             if tpu:
                 # Set to bfloat16 in case of TPU
                 if self.fpaa:
                     tf.keras.mixed_precision.set_global_policy("""mixed_bfloat16"""    )

                 tf.config.experimental_connect_to_cluster(A    )
                 tf.tpu.experimental.initialize_tpu_system(A    )

                 a__      =       tf.distribute.TPUStrategy(A    )

             elif len(A    ) == 0:
                 a__      =       tf.distribute.OneDeviceStrategy(device="""/cpu:0"""    )
             elif len(A    ) == 1:
                 a__      =       tf.distribute.OneDeviceStrategy(device="""/gpu:0"""    )
             elif len(A    ) > 1:
                 # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
                 a__      =       tf.distribute.MirroredStrategy()
             else:
                 raise ValueError("""Cannot find the proper strategy, please check your environment properties."""    )

         return strategy

     @property
     def 				__SCREAMING_SNAKE_CASE		(      self :       Tuple    )			->			"tf.distribute.Strategy":






         """simple docstring"""
         requires_backends(self						,				["""tf"""]    )
         return self._setup_strategy

     @property
     def 				__SCREAMING_SNAKE_CASE		(      self :       Dict    )			->			int:






         """simple docstring"""
         requires_backends(self						,				["""tf"""]    )
         return self._setup_strategy.num_replicas_in_sync

     @property
     def 				__SCREAMING_SNAKE_CASE		(      self :       Dict    )			->			List[Any]:






         """simple docstring"""
         return False  # TF Logging is handled by Keras not the Trainer

     @property
     def 				__SCREAMING_SNAKE_CASE		(      self :       Optional[Any]    )			->			int:






         """simple docstring"""
         if self.per_gpu_train_batch_size:
             logger.warning(
                 """Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future """
                 """version. Using `--per_device_train_batch_size` is preferred."""    )
         a__      =       self.per_gpu_train_batch_size or self.per_device_train_batch_size
         return per_device_batch_size * self.n_replicas

     @property
     def 				__SCREAMING_SNAKE_CASE		(      self :       List[str]    )			->			int:






         """simple docstring"""
         if self.per_gpu_eval_batch_size:
             logger.warning(
                 """Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future """
                 """version. Using `--per_device_eval_batch_size` is preferred."""    )
         a__      =       self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
         return per_device_batch_size * self.n_replicas






     @property
     def 				__SCREAMING_SNAKE_CASE		(      self :       str    )			->			int:






         """simple docstring"""
         requires_backends(self						,				["""tf"""]    )
         warnings.warn(
             """The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead."""						,				A						,				)
         return self._setup_strategy.num_replicas_in_sync






