
"""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 			UpperCamelCase      :
     def __init__(     self  ,     snake_case__  ,     snake_case__=13  ,     snake_case__=7  ,     snake_case__=True  ,     snake_case__=True  ,     snake_case__=True  ,     snake_case__=True  ,     snake_case__=99  ,     snake_case__=32  ,     snake_case__=5  ,     snake_case__=4  ,     snake_case__=37  ,     snake_case__="gelu"  ,     snake_case__=0.1  ,     snake_case__=0.1  ,     snake_case__=512  ,     snake_case__=16  ,     snake_case__=2  ,     snake_case__=0.02  ,     snake_case__=3  ,     snake_case__=4  ,     snake_case__=None  ,     ):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = parent
       _SCREAMING_SNAKE_CASE   :				int							    = batch_size
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = seq_length
       _SCREAMING_SNAKE_CASE   :				Tuple							    = is_training
       _SCREAMING_SNAKE_CASE   :				str							    = use_input_mask
       _SCREAMING_SNAKE_CASE   :				int							    = use_token_type_ids
       _SCREAMING_SNAKE_CASE   :				Dict							    = use_labels
       _SCREAMING_SNAKE_CASE   :				List[str]							    = vocab_size
       _SCREAMING_SNAKE_CASE   :				List[str]							    = hidden_size
       _SCREAMING_SNAKE_CASE   :				Dict							    = num_hidden_layers
       _SCREAMING_SNAKE_CASE   :				str							    = num_attention_heads
       _SCREAMING_SNAKE_CASE   :				int							    = intermediate_size
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = hidden_act
       _SCREAMING_SNAKE_CASE   :				int							    = hidden_dropout_prob
       _SCREAMING_SNAKE_CASE   :				Dict							    = attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE   :				str							    = max_position_embeddings
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = type_vocab_size
       _SCREAMING_SNAKE_CASE   :				List[str]							    = type_sequence_label_size
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = initializer_range
       _SCREAMING_SNAKE_CASE   :				int							    = num_labels
       _SCREAMING_SNAKE_CASE   :				int							    = num_choices
       _SCREAMING_SNAKE_CASE   :				int							    = scope
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size				)

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

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

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

       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """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=_a  ,     initializer_range=self.initializer_range  ,     )
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = NystromformerModel(config=_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = model(_a  ,     attention_mask=_a  ,     token_type_ids=_a				)
       _SCREAMING_SNAKE_CASE   :				Any							    = model(_a  ,     token_type_ids=_a				)
       _SCREAMING_SNAKE_CASE   :				Tuple							    = model(_a				)
       self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				int							    = NystromformerForMaskedLM(config=_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Tuple							    = model(_a  ,     attention_mask=_a  ,     token_type_ids=_a  ,     labels=_a				)
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[str]							    = NystromformerForQuestionAnswering(config=_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Dict							    = model(
           _a  ,     attention_mask=_a  ,     token_type_ids=_a  ,     start_positions=_a  ,     end_positions=_a  ,     )
       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 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = self.num_labels
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = NystromformerForSequenceClassification(_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = model(_a  ,     attention_mask=_a  ,     token_type_ids=_a  ,     labels=_a				)
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[str]							    = self.num_labels
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = NystromformerForTokenClassification(config=_a				)
       model.to(_a				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Dict							    = model(_a  ,     attention_mask=_a  ,     token_type_ids=_a  ,     labels=_a				)
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.num_labels)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


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




     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = self.prepare_config_and_inputs()
       (
           _SCREAMING_SNAKE_CASE					
       )   :				str							    = config_and_inputs
       _SCREAMING_SNAKE_CASE   :				Dict							    = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
       return config, inputs_dict
@require_torch
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE,       __SCREAMING_SNAKE_CASE,       unittest.TestCase  ):
     A__      =			(
         (
             NystromformerModel,
             NystromformerForMaskedLM,
             NystromformerForMultipleChoice,
             NystromformerForQuestionAnswering,
             NystromformerForSequenceClassification,
             NystromformerForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     A__      =			(
         {
             """feature-extraction""": NystromformerModel,
             """fill-mask""": NystromformerForMaskedLM,
             """question-answering""": NystromformerForQuestionAnswering,
             """text-classification""": NystromformerForSequenceClassification,
             """token-classification""": NystromformerForTokenClassification,
             """zero-shot""": NystromformerForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     A__      =			False
     A__      =			False
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = NystromformerModelTester(self				)
       _SCREAMING_SNAKE_CASE   :				Any							    = ConfigTester(self  ,     config_class=_a  ,     hidden_size=37				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       self.config_tester.run_common_tests()
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_a				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
         _SCREAMING_SNAKE_CASE   :				List[str]							    = type
         self.model_tester.create_and_check_model(*_a				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_a				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_a				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				str							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_a				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_a				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_a				)




     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _SCREAMING_SNAKE_CASE   :				Optional[int]							    = NystromformerModel.from_pretrained(_a				)
         self.assertIsNotNone(_a				)
@require_torch
class 			UpperCamelCase      (							unittest.TestCase  ):
     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


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

       with torch.no_grad():
         _SCREAMING_SNAKE_CASE   :				int							    = model(_a				)[0]

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

       _SCREAMING_SNAKE_CASE   :				Tuple							    = torch.tensor(
           [[[-0.4_532, -0.0_936, 0.5_137], [-0.2_676, 0.0_628, 0.6_186], [-0.3_629, -0.1_726, 0.4_716]]]				)

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




     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = """the [MASK] of Belgium is Brussels"""

       _SCREAMING_SNAKE_CASE   :				List[Any]							    = AutoTokenizer.from_pretrained("uw-madison/nystromformer-512"				)
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = NystromformerForMaskedLM.from_pretrained("uw-madison/nystromformer-512"				)

       _SCREAMING_SNAKE_CASE   :				Tuple							    = tokenizer(_a  ,     return_tensors="pt"				)

       with torch.no_grad():
         _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = model(encoding.input_ids				).logits

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

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





"""simple docstring"""
import os

import pytest
import yaml

from datasets.features.features import Features, Value
from datasets.info import DatasetInfo, DatasetInfosDict





@pytest.mark.parametrize(
    "files",						[
        ["full:README.md", "dataset_infos.json"],
        ["empty:README.md", "dataset_infos.json"],
        ["dataset_infos.json"],
        ["full:README.md"],
    ],						)
def    _lowerCAmelCase    (     lowerCamelCase__							:							str,						lowerCamelCase__							:							str				)  ->    Optional[int]:
  _SCREAMING_SNAKE_CASE   :				Optional[int]							    = tmp_path_factory.mktemp("dset_infos_dir"				)
  if "full:README.md" in files:
    with open(dataset_infos_dir / "README.md",						"w"				) as f:
      f.write("---\ndataset_info:\n  dataset_size: 42\n---"				)
  if "empty:README.md" in files:
    with open(dataset_infos_dir / "README.md",						"w"				) as f:
      f.write(""				)
    # we want to support dataset_infos.json for backward compatibility
  if "dataset_infos.json" in files:
    with open(dataset_infos_dir / "dataset_infos.json",						"w"				) as f:
      f.write("{\"default\": {\"dataset_size\": 42}}"				)
  _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = DatasetInfosDict.from_directory(lowerCamelCase__				)
  assert dataset_infos
  assert dataset_infos["default"].dataset_size == 4_2





@pytest.mark.parametrize(
    "dataset_info",						[
        DatasetInfo(),
        DatasetInfo(
            description="foo",						features=Features({"a": Value("int32"				)}				),						builder_name="builder",						config_name="config",						version="1.0.0",						splits=[{"name": "train"}],						download_size=4_2,						),
    ],						)
def    _lowerCAmelCase    (     lowerCamelCase__							:							Union[str, Any],						lowerCamelCase__							:							DatasetInfo				)  ->    List[Any]:
  _SCREAMING_SNAKE_CASE   :				Dict							    = str(lowerCamelCase__				)
  dataset_info.write_to_directory(lowerCamelCase__				)
  _SCREAMING_SNAKE_CASE   :				int							    = DatasetInfo.from_directory(lowerCamelCase__				)
  assert dataset_info == reloaded
  assert os.path.exists(os.path.join(lowerCamelCase__,						"dataset_info.json"				)				)





def    _lowerCAmelCase    (     )  ->    Union[str, Any]:
  _SCREAMING_SNAKE_CASE   :				str							    = DatasetInfo(
      description="foo",						citation="bar",						homepage="https://foo.bar",						license="CC0",						features=Features({"a": Value("int32"				)}				),						post_processed={},						supervised_keys=(),						task_templates=[],						builder_name="builder",						config_name="config",						version="1.0.0",						splits=[{"name": "train", "num_examples": 4_2}],						download_checksums={},						download_size=1_3_3_7,						post_processing_size=4_4_2,						dataset_size=1_2_3_4,						size_in_bytes=1_3_3_7 + 4_4_2 + 1_2_3_4,						)
  _SCREAMING_SNAKE_CASE   :				List[str]							    = dataset_info._to_yaml_dict()
  assert sorted(lowerCamelCase__				) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML				)
  for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
    assert key in dataset_info_yaml_dict
    assert isinstance(dataset_info_yaml_dict[key],						(list, dict, int, str)				)
  _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = yaml.safe_dump(lowerCamelCase__				)
  _SCREAMING_SNAKE_CASE   :				List[Any]							    = yaml.safe_load(lowerCamelCase__				)
  assert dataset_info_yaml_dict == reloaded





def    _lowerCAmelCase    (     )  ->    Tuple:
  _SCREAMING_SNAKE_CASE   :				Dict							    = DatasetInfo()
  _SCREAMING_SNAKE_CASE   :				List[Any]							    = dataset_info._to_yaml_dict()
  assert dataset_info_yaml_dict == {}





@pytest.mark.parametrize(
    "dataset_infos_dict",						[
        DatasetInfosDict(),
        DatasetInfosDict({"default": DatasetInfo()}				),
        DatasetInfosDict({"my_config_name": DatasetInfo()}				),
        DatasetInfosDict(
            {
                "default": DatasetInfo(
                    description="foo",						features=Features({"a": Value("int32"				)}				),						builder_name="builder",						config_name="config",						version="1.0.0",						splits=[{"name": "train"}],						download_size=4_2,						)
            }				),
        DatasetInfosDict(
            {
                "v1": DatasetInfo(dataset_size=4_2				),
                "v2": DatasetInfo(dataset_size=1_3_3_7				),
            }				),
    ],						)
def    _lowerCAmelCase    (     lowerCamelCase__							:							Any,						lowerCamelCase__							:							DatasetInfosDict				)  ->    Optional[int]:
  _SCREAMING_SNAKE_CASE   :				str							    = str(lowerCamelCase__				)
  dataset_infos_dict.write_to_directory(lowerCamelCase__				)
  _SCREAMING_SNAKE_CASE   :				str							    = DatasetInfosDict.from_directory(lowerCamelCase__				)

  # the config_name of the dataset_infos_dict take over the attribute
  for config_name, dataset_info in dataset_infos_dict.items():
    _SCREAMING_SNAKE_CASE   :				List[Any]							    = config_name
    # the yaml representation doesn't include fields like description or citation
    # so we just test that we can recover what we can from the yaml
    _SCREAMING_SNAKE_CASE   :				Dict							    = DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict()				)
  assert dataset_infos_dict == reloaded

  if dataset_infos_dict:
    assert os.path.exists(os.path.join(lowerCamelCase__,						"README.md"				)				)



