





"""simple docstring"""





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 							snake_case							:

      '''simple docstring'''



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


        '''simple docstring'''




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

      def       A_					(							self       :      Optional[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size )

        __UpperCAmelCase    :						Optional[Any]       =						None
        if self.use_input_mask:
          __UpperCAmelCase    :						Any       =						random_attention_mask([self.batch_size, self.seq_length] )

        __UpperCAmelCase    :						Optional[int]       =						None
        if self.use_token_type_ids:
          __UpperCAmelCase    :						Tuple       =						ids_tensor([self.batch_size, self.seq_length]						,				self.type_vocab_size )

        __UpperCAmelCase    :						Optional[Any]       =						None
        __UpperCAmelCase    :						Union[str, Any]       =						None
        __UpperCAmelCase    :						Tuple       =						None
        if self.use_labels:
          __UpperCAmelCase    :						Tuple       =						ids_tensor([self.batch_size]						,				self.type_sequence_label_size )
          __UpperCAmelCase    :						Any       =						ids_tensor([self.batch_size, self.seq_length]						,				self.num_labels )
          __UpperCAmelCase    :						List[Any]       =						ids_tensor([self.batch_size]						,				self.num_choices )

        __UpperCAmelCase    :						int       =						self.get_config()

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

      def       A_					(							self       :      int ):


        '''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       A_					(							self       :      Union[str, Any]						,				__lowercase       :      List[str]						,				__lowercase       :      Tuple						,				__lowercase       :      int						,				__lowercase       :      List[Any]						,				__lowercase       :      List[Any]						,				__lowercase       :      int						,				__lowercase       :      List[str] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						NystromformerModel(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						Any       =						model(__lowercase						,				attention_mask=__lowercase						,				token_type_ids=__lowercase )
        __UpperCAmelCase    :						Tuple       =						model(__lowercase						,				token_type_ids=__lowercase )
        __UpperCAmelCase    :						List[str]       =						model(__lowercase )
        self.parent.assertEqual(result.last_hidden_state.shape						,				(self.batch_size, self.seq_length, self.hidden_size) )

      def       A_					(							self       :      Union[str, Any]						,				__lowercase       :      Optional[int]						,				__lowercase       :      Optional[Any]						,				__lowercase       :      List[str]						,				__lowercase       :      Tuple						,				__lowercase       :      str						,				__lowercase       :      Tuple						,				__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						NystromformerForMaskedLM(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						str       =						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       A_					(							self       :      Dict						,				__lowercase       :      List[Any]						,				__lowercase       :      Tuple						,				__lowercase       :      Dict						,				__lowercase       :      Dict						,				__lowercase       :      Dict						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Tuple       =						NystromformerForQuestionAnswering(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						Optional[int]       =						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       A_					(							self       :      str						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      List[Any]						,				__lowercase       :      Optional[Any]						,				__lowercase       :      str						,				__lowercase       :      Any						,				__lowercase       :      str						,				__lowercase       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						self.num_labels
        __UpperCAmelCase    :						List[str]       =						NystromformerForSequenceClassification(__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						int       =						model(__lowercase						,				attention_mask=__lowercase						,				token_type_ids=__lowercase						,				labels=__lowercase )
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels) )

      def       A_					(							self       :      List[Any]						,				__lowercase       :      Dict						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      List[Any]						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      List[Any]						,				__lowercase       :      Tuple						,				__lowercase       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						self.num_labels
        __UpperCAmelCase    :						Dict       =						NystromformerForTokenClassification(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						str       =						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       A_					(							self       :      Union[str, Any]						,				__lowercase       :      Tuple						,				__lowercase       :      Any						,				__lowercase       :      Tuple						,				__lowercase       :      List[str]						,				__lowercase       :      Optional[Any]						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						self.num_choices
        __UpperCAmelCase    :						Union[str, Any]       =						NystromformerForMultipleChoice(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						Any       =						input_ids.unsqueeze(1 ).expand(-1						,				self.num_choices						,				-1 ).contiguous()
        __UpperCAmelCase    :						Tuple       =						token_type_ids.unsqueeze(1 ).expand(-1						,				self.num_choices						,				-1 ).contiguous()
        __UpperCAmelCase    :						Optional[Any]       =						input_mask.unsqueeze(1 ).expand(-1						,				self.num_choices						,				-1 ).contiguous()
        __UpperCAmelCase    :						Optional[Any]       =						model(
            __lowercase						,				attention_mask=__lowercase						,				token_type_ids=__lowercase						,				labels=__lowercase						,				)
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_choices) )







      def       A_					(							self       :      str ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						self.prepare_config_and_inputs()
        (
            __UpperCAmelCase     
        )    :						Union[str, Any]       =						config_and_inputs
        __UpperCAmelCase    :						Optional[Any]       =						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict




@require_torch
class 							snake_case							(			__UpperCAmelCase      ,  __UpperCAmelCase      ,  unittest.TestCase ):

      '''simple docstring'''



      _A     :       Any         =       (
          (
              NystromformerModel,
              NystromformerForMaskedLM,
              NystromformerForMultipleChoice,
              NystromformerForQuestionAnswering,
              NystromformerForSequenceClassification,
              NystromformerForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      _A     :       List[str]         =       (
          {
              'feature-extraction': NystromformerModel,
              'fill-mask': NystromformerForMaskedLM,
              'question-answering': NystromformerForQuestionAnswering,
              'text-classification': NystromformerForSequenceClassification,
              'token-classification': NystromformerForTokenClassification,
              'zero-shot': NystromformerForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _A     :       str         =       False
      _A     :       List[Any]         =       False

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						NystromformerModelTester(self )
        __UpperCAmelCase    :						Tuple       =						ConfigTester(self						,				config_class=__lowercase						,				hidden_size=37 )

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        self.config_tester.run_common_tests()

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


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowercase )

      def       A_					(							self       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Tuple       =						self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          __UpperCAmelCase    :						Dict       =						type
          self.model_tester.create_and_check_model(*__lowercase )

      def       A_					(							self       :      str ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*__lowercase )

      def       A_					(							self       :      str ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*__lowercase )

      def       A_					(							self       :      int ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__lowercase )

      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__lowercase )

      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						Tuple       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__lowercase )







      @slow
      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          __UpperCAmelCase    :						str       =						NystromformerModel.from_pretrained(__lowercase )
          self.assertIsNotNone(__lowercase )




@require_torch
class 							snake_case							(			unittest.TestCase ):

      '''simple docstring'''



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


        '''simple docstring'''




        __UpperCAmelCase    :						Union[str, Any]       =						NystromformerModel.from_pretrained('''uw-madison/nystromformer-512''' )
        __UpperCAmelCase    :						Optional[Any]       =						torch.tensor([[0, 1, 2, 3, 4, 5]] )

        with torch.no_grad():
          __UpperCAmelCase    :						str       =						model(__lowercase )[0]

        __UpperCAmelCase    :						Union[str, Any]       =						torch.Size((1, 6, 768) )
        self.assertEqual(output.shape						,				__lowercase )

        __UpperCAmelCase    :						Optional[Any]       =						torch.tensor(
            [[[-0.4_5_3_2, -0.0_9_3_6, 0.5_1_3_7], [-0.2_6_7_6, 0.0_6_2_8, 0.6_1_8_6], [-0.3_6_2_9, -0.1_7_2_6, 0.4_7_1_6]]] )

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







      @slow
      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						'''the [MASK] of Belgium is Brussels'''

        __UpperCAmelCase    :						List[Any]       =						AutoTokenizer.from_pretrained('''uw-madison/nystromformer-512''' )
        __UpperCAmelCase    :						Dict       =						NystromformerForMaskedLM.from_pretrained('''uw-madison/nystromformer-512''' )

        __UpperCAmelCase    :						str       =						tokenizer(__lowercase						,				return_tensors='''pt''' )

        with torch.no_grad():
          __UpperCAmelCase    :						Optional[int]       =						model(encoding.input_ids ).logits

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

        self.assertEqual(tokenizer.decode(__lowercase )						,				'''capital''' )






"""simple docstring"""





import argparse
import os

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_task_guides.py
lowercase__				:int				   =       'src/transformers'
lowercase__				:List[str]				   =       'docs/source/en/tasks'


def       lowerCamelCase_       (   UpperCAmelCase_  ,			UpperCAmelCase_  ,			UpperCAmelCase_  )						->str:





  """simple docstring"""


  with open(UpperCAmelCase_  ,			'''r'''  ,			encoding='''utf-8'''  ,			newline='''\n'''  ) as f:
    __UpperCAmelCase    :						Union[str, Any]       =						f.readlines()
  # Find the start prompt.
  __UpperCAmelCase    :						Any       =						0
  while not lines[start_index].startswith(UpperCAmelCase_  ):
    start_index += 1
  start_index += 1

  __UpperCAmelCase    :						Optional[Any]       =						start_index
  while not lines[end_index].startswith(UpperCAmelCase_  ):
    end_index += 1
  end_index -= 1

  while len(lines[start_index]  ) <= 1:
    start_index += 1
  while len(lines[end_index]  ) <= 1:
    end_index -= 1
  end_index += 1
  return "".join(lines[start_index:end_index]  ), start_index, end_index, lines


# This is to make sure the transformers module imported is the one in the repo.
lowercase__				:Any				   =       direct_transformers_import(TRANSFORMERS_PATH)

lowercase__				:List[Any]				   =       {
    'asr.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES,
    'audio_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES,
    'language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
    'image_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES,
    'masked_language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES,
    'multiple_choice.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES,
    'object_detection.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES,
    'question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES,
    'semantic_segmentation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES,
    'sequence_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES,
    'summarization.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    'token_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES,
    'translation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    'video_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES,
    'document_question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES,
    'monocular_depth_estimation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES,
}

# This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any
# `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`).
lowercase__				:Union[str, Any]				   =       {
    'summarization.md': ('nllb',),
    'translation.md': ('nllb',),
}


def       lowerCamelCase_       (   UpperCAmelCase_  )						->Union[str, Any]:





  """simple docstring"""


  __UpperCAmelCase    :						List[str]       =						TASK_GUIDE_TO_MODELS[task_guide]
  __UpperCAmelCase    :						Dict       =						SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(UpperCAmelCase_  ,			set()  )
  __UpperCAmelCase    :						List[Any]       =						{
      code: name
      for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
      if (code in model_maping_names or code in special_model_types)
  }
  return ", ".join([f'''[{name}](../model_doc/{code})''' for code, name in model_names.items()]  ) + "\n"


def       lowerCamelCase_       (   UpperCAmelCase_  ,			UpperCAmelCase_=False  )						->Tuple:





  """simple docstring"""


  __UpperCAmelCase       ,     __UpperCAmelCase       ,     __UpperCAmelCase       ,     __UpperCAmelCase    :						Dict       =						_find_text_in_file(
      filename=os.path.join(UpperCAmelCase_  ,			UpperCAmelCase_  )  ,			start_prompt='''<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->'''  ,			end_prompt='''<!--End of the generated tip-->'''  ,			)

  __UpperCAmelCase    :						List[str]       =						get_model_list_for_task(UpperCAmelCase_  )

  if current_list != new_list:
    if overwrite:
      with open(os.path.join(UpperCAmelCase_  ,			UpperCAmelCase_  )  ,			'''w'''  ,			encoding='''utf-8'''  ,			newline='''\n'''  ) as f:
        f.writelines(lines[:start_index] + [new_list] + lines[end_index:]  )
    else:
      raise ValueError(
          f'''The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`'''
          ''' to fix this.'''  )


if __name__ == "__main__":
  lowercase__				:int				   =       argparse.ArgumentParser()
  parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
  lowercase__				:Optional[Any]				   =       parser.parse_args()

  for task_guide in TASK_GUIDE_TO_MODELS.keys():
    check_model_list_for_task(task_guide, args.fix_and_overwrite)