





import unittest

from transformers import AutoTokenizer, NystromformerConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
  import torch

  from transformers import (
      NystromformerForMaskedLM,
      NystromformerForMultipleChoice,
      NystromformerForQuestionAnswering,
      NystromformerForSequenceClassification,
      NystromformerForTokenClassification,
      NystromformerModel,
  )
  from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST





class  lowerCAmelCase							:







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






     '''simple docstring'''





     lowercase__			       =  parent
     lowercase__			       =  batch_size
     lowercase__			       =  seq_length
     lowercase__			       =  is_training
     lowercase__			       =  use_input_mask
     lowercase__			       =  use_token_type_ids
     lowercase__			       =  use_labels
     lowercase__			       =  vocab_size
     lowercase__			       =  hidden_size
     lowercase__			       =  num_hidden_layers
     lowercase__			       =  num_attention_heads
     lowercase__			       =  intermediate_size
     lowercase__			       =  hidden_act
     lowercase__			       =  hidden_dropout_prob
     lowercase__			       =  attention_probs_dropout_prob
     lowercase__			       =  max_position_embeddings
     lowercase__			       =  type_vocab_size
     lowercase__			       =  type_sequence_label_size
     lowercase__			       =  initializer_range
     lowercase__			       =  num_labels
     lowercase__			       =  num_choices
     lowercase__			       =  scope







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     lowercase__			       =  ids_tensor([self.batch_size, self.seq_length]   ,							self.vocab_size )

     lowercase__			       =  None
     if self.use_input_mask:
        lowercase__			       =  random_attention_mask([self.batch_size, self.seq_length] )

     lowercase__			       =  None
     if self.use_token_type_ids:
        lowercase__			       =  ids_tensor([self.batch_size, self.seq_length]   ,							self.type_vocab_size )

     lowercase__			       =  None
     lowercase__			       =  None
     lowercase__			       =  None
     if self.use_labels:
        lowercase__			       =  ids_tensor([self.batch_size]   ,							self.type_sequence_label_size )
        lowercase__			       =  ids_tensor([self.batch_size, self.seq_length]   ,							self.num_labels )
        lowercase__			       =  ids_tensor([self.batch_size]   ,							self.num_choices )

     lowercase__			       =  self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







  def   UpperCAmelCase					(      self  :List[str] ):






     '''simple docstring'''





     return NystromformerConfig(
         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=_lowercase   ,							initializer_range=self.initializer_range   ,							)







  def   UpperCAmelCase					(      self  :int   ,							_lowercase  :str   ,							_lowercase  :Optional[int]   ,							_lowercase  :str   ,							_lowercase  :int   ,							_lowercase  :Tuple   ,							_lowercase  :Union[str, Any]   ,							_lowercase  :int ):






     '''simple docstring'''





     lowercase__			       =  NystromformerModel(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(_lowercase   ,							attention_mask=_lowercase   ,							token_type_ids=_lowercase )
     lowercase__			       =  model(_lowercase   ,							token_type_ids=_lowercase )
     lowercase__			       =  model(_lowercase )
     self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size) )







  def   UpperCAmelCase					(      self  :List[str]   ,							_lowercase  :Optional[int]   ,							_lowercase  :Tuple   ,							_lowercase  :Tuple   ,							_lowercase  :int   ,							_lowercase  :str   ,							_lowercase  :Union[str, Any]   ,							_lowercase  :Tuple ):






     '''simple docstring'''





     lowercase__			       =  NystromformerForMaskedLM(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(_lowercase   ,							attention_mask=_lowercase   ,							token_type_ids=_lowercase   ,							labels=_lowercase )
     self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length, self.vocab_size) )







  def   UpperCAmelCase					(      self  :List[str]   ,							_lowercase  :List[str]   ,							_lowercase  :Optional[int]   ,							_lowercase  :Tuple   ,							_lowercase  :Optional[int]   ,							_lowercase  :List[str]   ,							_lowercase  :int   ,							_lowercase  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  NystromformerForQuestionAnswering(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(
         _lowercase   ,							attention_mask=_lowercase   ,							token_type_ids=_lowercase   ,							start_positions=_lowercase   ,							end_positions=_lowercase   ,							)
     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  :int   ,							_lowercase  :Tuple   ,							_lowercase  :str   ,							_lowercase  :Optional[Any]   ,							_lowercase  :int   ,							_lowercase  :int   ,							_lowercase  :Any   ,							_lowercase  :Tuple ):






     '''simple docstring'''





     lowercase__			       =  self.num_labels
     lowercase__			       =  NystromformerForSequenceClassification(_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(_lowercase   ,							attention_mask=_lowercase   ,							token_type_ids=_lowercase   ,							labels=_lowercase )
     self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_labels) )







  def   UpperCAmelCase					(      self  :int   ,							_lowercase  :str   ,							_lowercase  :int   ,							_lowercase  :Tuple   ,							_lowercase  :Dict   ,							_lowercase  :int   ,							_lowercase  :Dict   ,							_lowercase  :str ):






     '''simple docstring'''





     lowercase__			       =  self.num_labels
     lowercase__			       =  NystromformerForTokenClassification(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(_lowercase   ,							attention_mask=_lowercase   ,							token_type_ids=_lowercase   ,							labels=_lowercase )
     self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length, self.num_labels) )







  def   UpperCAmelCase					(      self  :Union[str, Any]   ,							_lowercase  :Optional[Any]   ,							_lowercase  :str   ,							_lowercase  :Optional[Any]   ,							_lowercase  :Tuple   ,							_lowercase  :Any   ,							_lowercase  :int   ,							_lowercase  :str ):






     '''simple docstring'''





     lowercase__			       =  self.num_choices
     lowercase__			       =  NystromformerForMultipleChoice(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  input_ids.unsqueeze(1 ).expand(-1   ,							self.num_choices   ,							-1 ).contiguous()
     lowercase__			       =  token_type_ids.unsqueeze(1 ).expand(-1   ,							self.num_choices   ,							-1 ).contiguous()
     lowercase__			       =  input_mask.unsqueeze(1 ).expand(-1   ,							self.num_choices   ,							-1 ).contiguous()
     lowercase__			       =  model(
         _lowercase   ,							attention_mask=_lowercase   ,							token_type_ids=_lowercase   ,							labels=_lowercase   ,							)
     self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_choices) )







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     lowercase__			       =  self.prepare_config_and_inputs()
     (
         (
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							
     )			       =  config_and_inputs
     lowercase__			       =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict





@require_torch
class  lowerCAmelCase							(       lowercase_							,							lowercase_							,							unittest.TestCase ):
  __lowerCamelCase		=      (
      (
          NystromformerModel,
          NystromformerForMaskedLM,
          NystromformerForMultipleChoice,
          NystromformerForQuestionAnswering,
          NystromformerForSequenceClassification,
          NystromformerForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  __lowerCamelCase		=      (
      {
          'feature-extraction': NystromformerModel,
          'fill-mask': NystromformerForMaskedLM,
          'question-answering': NystromformerForQuestionAnswering,
          'text-classification': NystromformerForSequenceClassification,
          'token-classification': NystromformerForTokenClassification,
          'zero-shot': NystromformerForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  __lowerCamelCase		=      False
  __lowerCamelCase		=      False







  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''simple docstring'''





     lowercase__			       =  NystromformerModelTester(self )
     lowercase__			       =  ConfigTester(self   ,							config_class=_lowercase   ,							hidden_size=37 )







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     self.config_tester.run_common_tests()







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowercase )







  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
        lowercase__			       =  type
        self.model_tester.create_and_check_model(*_lowercase )







  def   UpperCAmelCase					(      self  :List[str] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*_lowercase )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*_lowercase )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*_lowercase )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*_lowercase )







  def   UpperCAmelCase					(      self  :Any ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*_lowercase )







  @slow
  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase__			       =  NystromformerModel.from_pretrained(_lowercase )
        self.assertIsNotNone(_lowercase )





@require_torch
class  lowerCAmelCase							(       unittest.TestCase ):







  @slow
  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     lowercase__			       =  NystromformerModel.from_pretrained("uw-madison/nystromformer-512" )
     lowercase__			       =  torch.tensor([[0, 1, 2, 3, 4, 5]] )

     with torch.no_grad():
        lowercase__			       =  model(_lowercase )[0]

     lowercase__			       =  torch.Size((1, 6, 7_68) )
     self.assertEqual(output.shape   ,							_lowercase )

     lowercase__			       =  torch.tensor(
         [[[-0.4532, -0.0936, 0.5137], [-0.2676, 0.0628, 0.6186], [-0.3629, -0.1726, 0.4716]]] )

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







  @slow
  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     lowercase__			       =  "the [MASK] of Belgium is Brussels"

     lowercase__			       =  AutoTokenizer.from_pretrained("uw-madison/nystromformer-512" )
     lowercase__			       =  NystromformerForMaskedLM.from_pretrained("uw-madison/nystromformer-512" )

     lowercase__			       =  tokenizer(_lowercase   ,							return_tensors="pt" )

     with torch.no_grad():
        lowercase__			       =  model(encoding.input_ids ).logits

     lowercase__			       =  token_logits[:, 2, :].argmax(-1 )[0]

     self.assertEqual(tokenizer.decode(_lowercase )   ,							"capital" )













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

import yaml





class  lowerCAmelCase							(       yaml.SafeLoader ):







  def   UpperCAmelCase					(      self  :Optional[Any]   ,							_lowercase  :Any ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :Any   ,							_lowercase  :str   ,							_lowercase  :Dict=False ):






     '''simple docstring'''





     lowercase__			       =  super().construct_mapping(_lowercase   ,							deep=_lowercase )
     self._check_no_duplicates_on_constructed_node(_lowercase )
     return mapping


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

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





class  lowerCAmelCase							(       lowercase_ ):
  # class attributes
  __lowerCamelCase		=      {'train_eval_index'}  # train-eval-index in the YAML metadata







  @classmethod
  def   UpperCAmelCase					(      cls  :Dict   ,							_lowercase  :Path ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :Any   ,							_lowercase  :Path ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :List[str]   ,							_lowercase  :Optional[str] = None ):






     '''simple docstring'''





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







  @classmethod
  def   UpperCAmelCase					(      cls  :Any   ,							_lowercase  :str ):






     '''simple docstring'''





     lowercase__			       =  yaml.load(_lowercase   ,							Loader=_NoDuplicateSafeLoader ) or {}

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







  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''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=_lowercase   ,							allow_unicode=_lowercase   ,							encoding="utf-8"   ,							).decode("utf-8" )
_snake_case           =			{
    """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

  _snake_case           =			ArgumentParser(usage="""Validate the yaml metadata block of a README.md file.""")
  ap.add_argument("""readme_filepath""")
  _snake_case           =			ap.parse_args()

  _snake_case           =			Path(args.readme_filepath)
  _snake_case           =			DatasetMetadata.from_readme(readme_filepath)
  print(dataset_metadata)
  dataset_metadata.to_readme(readme_filepath)






