





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


__lowercase  :   Optional[int]						      =logging.get_logger(__name__)

__lowercase  :   Optional[Any]						      ={
    """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      A       (	__lowercase						):
      _snake_case							     ='''bart'''
      _snake_case							     =['''past_key_values''']
      _snake_case							     ={'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}



      def __init__(	self:						List[Any]		,  _lowerCAmelCase:						List[Any]=5_0265		,  _lowerCAmelCase:						List[Any]=1024		,  _lowerCAmelCase:						Optional[Any]=12		,  _lowerCAmelCase:						List[Any]=4096		,  _lowerCAmelCase:						str=16		,  _lowerCAmelCase:						Union[str, Any]=12		,  _lowerCAmelCase:						str=4096		,  _lowerCAmelCase:						Tuple=16		,  _lowerCAmelCase:						int=0.0		,  _lowerCAmelCase:						Optional[Any]=0.0		,  _lowerCAmelCase:						Dict="gelu"		,  _lowerCAmelCase:						Tuple=1024		,  _lowerCAmelCase:						Optional[Any]=0.1		,  _lowerCAmelCase:						List[Any]=0.0		,  _lowerCAmelCase:						int=0.0		,  _lowerCAmelCase:						int=0.02		,  _lowerCAmelCase:						Optional[int]=0.0		,  _lowerCAmelCase:						Union[str, Any]=False		,  _lowerCAmelCase:						List[Any]=True		,  _lowerCAmelCase:						str=3		,  _lowerCAmelCase:						List[str]=1		,  _lowerCAmelCase:						Union[str, Any]=0		,  _lowerCAmelCase:						str=2		,  _lowerCAmelCase:						List[Any]=True		,  _lowerCAmelCase:						int=2		,  _lowerCAmelCase:						List[Any]=2		,  **_lowerCAmelCase:						Optional[int]		,  )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=vocab_size
         UpperCAmelCase_											=max_position_embeddings
         UpperCAmelCase_											=d_model
         UpperCAmelCase_											=encoder_ffn_dim
         UpperCAmelCase_											=encoder_layers
         UpperCAmelCase_											=encoder_attention_heads
         UpperCAmelCase_											=decoder_ffn_dim
         UpperCAmelCase_											=decoder_layers
         UpperCAmelCase_											=decoder_attention_heads
         UpperCAmelCase_											=dropout
         UpperCAmelCase_											=attention_dropout
         UpperCAmelCase_											=activation_dropout
         UpperCAmelCase_											=activation_function
         UpperCAmelCase_											=init_std
         UpperCAmelCase_											=encoder_layerdrop
         UpperCAmelCase_											=decoder_layerdrop
         UpperCAmelCase_											=classifier_dropout
         UpperCAmelCase_											=use_cache
         UpperCAmelCase_											=encoder_layers
         UpperCAmelCase_											=scale_embedding  # scale factor will be sqrt(d_model) if True

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

         # ensure backward compatibility for BART CNN models
         if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated"		,  _lowerCAmelCase       ):
            UpperCAmelCase_											=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      A       (	__lowercase						):



      @property
      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Mapping[str, Mapping[int, str]]:
         '''simple docstring'''

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

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

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

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



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						PreTrainedTokenizer		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						bool = False		,  _lowerCAmelCase:						Optional[TensorType] = None		,  )						->    Mapping[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase       )

         # Generate decoder inputs
         UpperCAmelCase_											=seq_length if not self.use_past else 1
         UpperCAmelCase_											=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase       )
         UpperCAmelCase_											={F'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
         UpperCAmelCase_											=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
            UpperCAmelCase_						,       UpperCAmelCase_											=common_inputs["input_ids"].shape
            UpperCAmelCase_											=common_inputs["decoder_input_ids"].shape[1]
            UpperCAmelCase_						,       UpperCAmelCase_											=self.num_attention_heads
            UpperCAmelCase_											=(
                batch,
                num_encoder_attention_heads,
                encoder_seq_length,
                self._config.hidden_size // num_encoder_attention_heads,
            )
            UpperCAmelCase_											=decoder_seq_length + 3
            UpperCAmelCase_											=(
                batch,
                num_decoder_attention_heads,
                decoder_past_length,
                self._config.hidden_size // num_decoder_attention_heads,
            )

            UpperCAmelCase_											=torch.cat(
                [common_inputs["decoder_attention_mask"], torch.ones(_lowerCAmelCase		,  _lowerCAmelCase       )]		,  dim=1       )

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

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



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						PreTrainedTokenizer		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						bool = False		,  _lowerCAmelCase:						Optional[TensorType] = None		,  )						->    Mapping[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=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
            UpperCAmelCase_						,       UpperCAmelCase_											=common_inputs["input_ids"].shape
            # Not using the same length for past_key_values
            UpperCAmelCase_											=seqlen + 2
            UpperCAmelCase_						,       UpperCAmelCase_											=self.num_layers
            UpperCAmelCase_						,       UpperCAmelCase_											=self.num_attention_heads
            UpperCAmelCase_											=(
                batch,
                num_encoder_attention_heads,
                past_key_values_length,
                self._config.hidden_size // num_encoder_attention_heads,
            )

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



      def 	lowerCAmelCase__    (	self:						Union[str, Any]		,  _lowerCAmelCase:						PreTrainedTokenizer		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						bool = False		,  _lowerCAmelCase:						Optional[TensorType] = None		,  )						->    Mapping[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=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
         UpperCAmelCase_											=tokenizer.num_special_tokens_to_add(_lowerCAmelCase       )
         UpperCAmelCase_											=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
         UpperCAmelCase_											=[" ".join([tokenizer.unk_token]       ) * seq_length] * batch_size
         UpperCAmelCase_											=dict(tokenizer(_lowerCAmelCase		,  return_tensors=_lowerCAmelCase       )       )
         return common_inputs



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						PreTrainedTokenizer		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						int = -1		,  _lowerCAmelCase:						bool = False		,  _lowerCAmelCase:						Optional[TensorType] = None		,  )						->    Mapping[str, Any]:
         '''simple docstring'''

         if self.task in ["default", "seq2seq-lm"]:
            UpperCAmelCase_											=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":
            UpperCAmelCase_											=self._generate_dummy_inputs_for_causal_lm(
                _lowerCAmelCase		,  batch_size=_lowerCAmelCase		,  seq_length=_lowerCAmelCase		,  is_pair=_lowerCAmelCase		,  framework=_lowerCAmelCase       )
         else:
            UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						Tuple       )						->    int:
         '''simple docstring'''

         if self.task in ["default", "seq2seq-lm"]:
            UpperCAmelCase_											=super()._flatten_past_key_values_(_lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase       )
         else:
            UpperCAmelCase_											=super(_lowerCAmelCase		,  self       )._flatten_past_key_values_(
                _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase       )











from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml

class      A       (	yaml.SafeLoader						):



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						int       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=[self.constructed_objects[key_node] for key_node, _ in node.value]
         UpperCAmelCase_											=[tuple(_lowerCAmelCase       ) if isinstance(_lowerCAmelCase		,  _lowerCAmelCase       ) else key for key in keys]
         UpperCAmelCase_											=Counter(_lowerCAmelCase       )
         UpperCAmelCase_											=[key for key in counter if counter[key] > 1]
         if duplicate_keys:
            raise TypeError(F'Got duplicate yaml keys: {duplicate_keys}'       )






      def 	lowerCAmelCase__    (	self:						Dict		,  _lowerCAmelCase:						List[str]		,  _lowerCAmelCase:						Optional[Any]=False       )						->    int:
         '''simple docstring'''

         UpperCAmelCase_											=super().construct_mapping(_lowerCAmelCase		,  deep=_lowerCAmelCase       )
         self._check_no_duplicates_on_constructed_node(_lowerCAmelCase       )
         return mapping




def     a__ (					lowercase__  ):






   '''simple docstring'''






   UpperCAmelCase_											=list(readme_content.splitlines()  )
   if full_content and full_content[0] == "---" and "---" in full_content[1:]:
      UpperCAmelCase_											=full_content[1:].index("---"  ) + 1
      UpperCAmelCase_											="\n".join(full_content[1:sep_idx]  )
      return yamlblock, "\n".join(full_content[sep_idx + 1 :]  )

   return None, "\n".join(lowercase__  )

class      A       (	__lowercase						):
      # class attributes
      _snake_case							     ={'''train_eval_index'''}  # train-eval-index in the YAML metadata



      @classmethod
      def 	lowerCAmelCase__    (	cls:						Any		,  _lowerCAmelCase:						Path       )						->    "DatasetMetadata":
         '''simple docstring'''

         with open(_lowerCAmelCase		,  encoding="utf-8"       ) as readme_file:
            UpperCAmelCase_						,       UpperCAmelCase_											=_split_yaml_from_readme(readme_file.read()       )
         if yaml_string is not None:
            return cls.from_yaml_string(_lowerCAmelCase       )
         else:
            return cls()



      def 	lowerCAmelCase__    (	self:						Dict		,  _lowerCAmelCase:						Path       )						->    List[str]:
         '''simple docstring'''

         if path.exists():
            with open(_lowerCAmelCase		,  encoding="utf-8"       ) as readme_file:
               UpperCAmelCase_											=readme_file.read()
         else:
            UpperCAmelCase_											=None
         UpperCAmelCase_											=self._to_readme(_lowerCAmelCase       )
         with open(_lowerCAmelCase		,  "w"		,  encoding="utf-8"       ) as readme_file:
            readme_file.write(_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						str		,  _lowerCAmelCase:						Optional[str] = None       )						->    str:
         '''simple docstring'''

         if readme_content is not None:
            UpperCAmelCase_						,       UpperCAmelCase_											=_split_yaml_from_readme(_lowerCAmelCase       )
            UpperCAmelCase_											="---\n" + self.to_yaml_string() + "---\n" + content
         else:
            UpperCAmelCase_											="---\n" + self.to_yaml_string() + "---\n"
         return full_content



      @classmethod
      def 	lowerCAmelCase__    (	cls:						Optional[Any]		,  _lowerCAmelCase:						str       )						->    "DatasetMetadata":
         '''simple docstring'''

         UpperCAmelCase_											=yaml.load(_lowerCAmelCase		,  Loader=_NoDuplicateSafeLoader       ) or {}

         # Convert the YAML keys to DatasetMetadata fields
         UpperCAmelCase_											={
             (key.replace("-"		,  "_"       ) if key.replace("-"		,  "_"       ) in cls._FIELDS_WITH_DASHES else key): value
             for key, value in metadata_dict.items()
         }
         return cls(**_lowerCAmelCase       )






      def 	lowerCAmelCase__    (	self:						int       )						->    str:
         '''simple docstring'''

         return yaml.safe_dump(
             {
                 (key.replace("_"		,  "-"       ) if key in self._FIELDS_WITH_DASHES else key): value
                 for key, value in self.items()
             }		,  sort_keys=_lowerCAmelCase		,  allow_unicode=_lowerCAmelCase		,  encoding="utf-8"		,  ).decode("utf-8"       )

__lowercase  :   int						      ={
    """image-classification""": [],
    """translation""": [],
    """image-segmentation""": [],
    """fill-mask""": [],
    """automatic-speech-recognition""": [],
    """token-classification""": [],
    """sentence-similarity""": [],
    """audio-classification""": [],
    """question-answering""": [],
    """summarization""": [],
    """zero-shot-classification""": [],
    """table-to-text""": [],
    """feature-extraction""": [],
    """other""": [],
    """multiple-choice""": [],
    """text-classification""": [],
    """text-to-image""": [],
    """text2text-generation""": [],
    """zero-shot-image-classification""": [],
    """tabular-classification""": [],
    """tabular-regression""": [],
    """image-to-image""": [],
    """tabular-to-text""": [],
    """unconditional-image-generation""": [],
    """text-retrieval""": [],
    """text-to-speech""": [],
    """object-detection""": [],
    """audio-to-audio""": [],
    """text-generation""": [],
    """conversational""": [],
    """table-question-answering""": [],
    """visual-question-answering""": [],
    """image-to-text""": [],
    """reinforcement-learning""": [],
    """voice-activity-detection""": [],
    """time-series-forecasting""": [],
    """document-question-answering""": [],
}


if __name__ == "__main__":
     from argparse import ArgumentParser

     __lowercase  :   List[Any]						      =ArgumentParser(usage="""Validate the yaml metadata block of a README.md file.""")
     ap.add_argument("""readme_filepath""")
     __lowercase  :   int						      =ap.parse_args()

     __lowercase  :   Optional[int]						      =Path(args.readme_filepath)
     __lowercase  :   Tuple						      =DatasetMetadata.from_readme(readme_filepath)
     print(dataset_metadata)
     dataset_metadata.to_readme(readme_filepath)




